LCOV - code coverage report
Current view: top level - src/userobjects - AdjointSolutionUserObject.C (source / functions) Hit Total Coverage
Test: idaholab/moose optimization: #31405 (292dce) with base fef103 Lines: 24 25 96.0 %
Date: 2025-09-04 07:54:57 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://mooseframework.inl.gov
       3             : //*
       4             : //* All rights reserved, see COPYRIGHT for full restrictions
       5             : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
       6             : //*
       7             : //* Licensed under LGPL 2.1, please see LICENSE for details
       8             : //* https://www.gnu.org/licenses/lgpl-2.1.html
       9             : 
      10             : #include "AdjointSolutionUserObject.h"
      11             : 
      12             : #include "libmesh/mesh_function.h"
      13             : #include "libmesh/exodusII_io.h"
      14             : 
      15             : #include <sys/stat.h>
      16             : 
      17             : registerMooseObject("OptimizationApp", AdjointSolutionUserObject);
      18             : 
      19             : InputParameters
      20         134 : AdjointSolutionUserObject::validParams()
      21             : {
      22         134 :   InputParameters params = SolutionUserObjectBase::validParams();
      23         134 :   params.addClassDescription(
      24             :       "Reads a variable from a mesh in one simulation to another specifically for loading forward "
      25             :       "solution in adjoint simulation during inverse optimization.");
      26             : 
      27         268 :   params.addRequiredParam<Real>(
      28             :       "reverse_time_end",
      29             :       "End time used for reversing the time integration when evaluating function derivative.");
      30             : 
      31             :   // Suppress some parameters that are irrelevant (i.e. users should use SolutionUserObject instead)
      32         134 :   params.suppressParameter<FileName>("es");
      33         134 :   params.suppressParameter<std::string>("system");
      34         134 :   params.suppressParameter<std::string>("timestep");
      35             : 
      36         134 :   return params;
      37           0 : }
      38             : 
      39          68 : AdjointSolutionUserObject::AdjointSolutionUserObject(const InputParameters & parameters)
      40             :   : SolutionUserObjectBase(parameters),
      41          68 :     _reverse_time_end(getParam<Real>("reverse_time_end")),
      42          68 :     _file_mod_time(std::numeric_limits<std::time_t>::min())
      43             : {
      44         136 :   if (!MooseUtils::hasExtension(_mesh_file, "e", /*strip_exodus_ext =*/true))
      45           2 :     paramError("mesh",
      46             :                "Performing transient adjoint simulation currently only works if the forward "
      47             :                "solution is written and read from exodus format.");
      48          66 : }
      49             : 
      50             : Real
      51        2586 : AdjointSolutionUserObject::solutionSampleTime()
      52             : {
      53        2586 :   return _reverse_time_end - _t + _dt;
      54             : }
      55             : 
      56             : void
      57        2462 : AdjointSolutionUserObject::timestepSetup()
      58             : {
      59             :   // Re-read the mesh file if it has been modified
      60             :   struct stat stats;
      61             :   stat(_mesh_file.c_str(), &stats);
      62        2462 :   if (stats.st_mtime > _file_mod_time)
      63             :   {
      64         124 :     _file_mod_time = stats.st_mtime;
      65             : 
      66             :     // Tell the parent class that we do need to re-initialize
      67         124 :     _initialized = false;
      68             :     // EquationSystems doesn't like the destructor that's called when there is a
      69             :     // unique_ptr::operator=
      70             :     _es.reset();
      71             :     _es2.reset();
      72             : 
      73             :     // Read the exodus file
      74         124 :     SolutionUserObjectBase::initialSetup();
      75             : 
      76             :     // Make sure to communicate what solution was actually loaded
      77         124 :     _interpolation_time = -_dt;
      78             :   }
      79             : 
      80        2462 :   SolutionUserObjectBase::timestepSetup();
      81        2462 : }

Generated by: LCOV version 1.14