https://mooseframework.inl.gov
AdjointSolutionUserObject.C
Go to the documentation of this file.
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 
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 
19 
22 {
24  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  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  params.suppressParameter<FileName>("es");
34  params.suppressParameter<std::string>("system");
35  params.suppressParameter<std::string>("timestep");
36 
37  return params;
38 }
39 
41  : SolutionUserObjectBase(parameters),
42  _reverse_time_end(getParam<Real>("reverse_time_end")),
43  _file_mod_time(std::numeric_limits<std::time_t>::min())
44 {
45  if (!MooseUtils::hasExtension(_mesh_file, "e", /*strip_exodus_ext =*/true))
46  paramError("mesh",
47  "Performing transient adjoint simulation currently only works if the forward "
48  "solution is written and read from exodus format.");
49 }
50 
51 Real
53 {
54  return _reverse_time_end - _t + _dt;
55 }
56 
57 void
59 {
60  // Re-read the mesh file if it has been modified
61  struct stat stats;
62  stat(_mesh_file.c_str(), &stats);
63  if (stats.st_mtime > _file_mod_time)
64  {
65  _file_mod_time = stats.st_mtime;
66 
67  // Tell the parent class that we do need to re-initialize
68  _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
76 
77  // Make sure to communicate what solution was actually loaded
79  }
80 
82 }
static InputParameters validParams()
std::unique_ptr< libMesh::EquationSystems > _es
virtual void initialSetup() override
void paramError(const std::string &param, Args... args) const
static InputParameters validParams()
const Real & _reverse_time_end
Mapping between adjoint simulation time and adjoint simulation time.
std::unique_ptr< libMesh::EquationSystems > _es2
void addRequiredParam(const std::string &name, const std::string &doc_string)
void suppressParameter(const std::string &name)
AdjointSolutionUserObject(const InputParameters &parameters)
virtual void timestepSetup() override
registerMooseObject("OptimizationApp", AdjointSolutionUserObject)
std::time_t _file_mod_time
The system time of the last instance the file was loaded.
virtual Real solutionSampleTime() override
bool hasExtension(const std::string &filename, std::string ext, bool strip_exodus_ext)
virtual void timestepSetup() override
This will read a the files again if they have been re-written from optimization iteration.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void addClassDescription(const std::string &doc_string)
auto min(const L &left, const R &right)