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 : #include "libmesh/nemesis_io.h" 15 : 16 : #include <sys/stat.h> 17 : 18 : registerMooseObject("OptimizationApp", AdjointSolutionUserObject); 19 : 20 : InputParameters 21 68 : AdjointSolutionUserObject::validParams() 22 : { 23 68 : InputParameters params = SolutionUserObjectBase::validParams(); 24 68 : params.addClassDescription( 25 : "Reads a variable from a mesh in one simulation to another specifically for loading forward " 26 : "solution in adjoint simulation during inverse optimization."); 27 : 28 136 : params.addRequiredParam<Real>( 29 : "reverse_time_end", 30 : "End time used for reversing the time integration when evaluating function derivative."); 31 : 32 : // Suppress some parameters that are irrelevant (i.e. users should use SolutionUserObject instead) 33 68 : params.suppressParameter<FileName>("es"); 34 68 : params.suppressParameter<std::string>("system"); 35 68 : params.suppressParameter<std::string>("timestep"); 36 : 37 68 : return params; 38 0 : } 39 : 40 35 : AdjointSolutionUserObject::AdjointSolutionUserObject(const InputParameters & parameters) 41 : : SolutionUserObjectBase(parameters), 42 35 : _reverse_time_end(getParam<Real>("reverse_time_end")), 43 35 : _file_mod_time(std::numeric_limits<std::time_t>::min()) 44 : { 45 70 : if (!MooseUtils::hasExtension(_mesh_file, "e", /*strip_exodus_ext =*/true)) 46 2 : paramError("mesh", 47 : "Performing transient adjoint simulation currently only works if the forward " 48 : "solution is written and read from exodus format."); 49 33 : } 50 : 51 : Real 52 1734 : AdjointSolutionUserObject::solutionSampleTime() 53 : { 54 1734 : return _reverse_time_end - _t + _dt; 55 : } 56 : 57 : void 58 1604 : AdjointSolutionUserObject::timestepSetup() 59 : { 60 : // Re-read the mesh file if it has been modified 61 : struct stat stats; 62 : stat(_mesh_file.c_str(), &stats); 63 1604 : if (stats.st_mtime > _file_mod_time) 64 : { 65 65 : _file_mod_time = stats.st_mtime; 66 : 67 : // Tell the parent class that we do need to re-initialize 68 65 : _initialized = false; 69 : // EquationSystems doesn't like the destructor that's called when there is a 70 : // unique_ptr::operator= 71 : _es.reset(); 72 : _es2.reset(); 73 : 74 : // Read the exodus file 75 65 : SolutionUserObjectBase::initialSetup(); 76 : 77 : // Make sure to communicate what solution was actually loaded 78 65 : _interpolation_time = -_dt; 79 : } 80 : 81 1604 : SolutionUserObjectBase::timestepSetup(); 82 1604 : }