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 
15 #include <sys/stat.h>
16 
18 
21 {
23  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  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  params.suppressParameter<FileName>("es");
33  params.suppressParameter<std::string>("system");
34  params.suppressParameter<std::string>("timestep");
35 
36  return params;
37 }
38 
40  : SolutionUserObjectBase(parameters),
41  _reverse_time_end(getParam<Real>("reverse_time_end")),
42  _file_mod_time(std::numeric_limits<std::time_t>::min())
43 {
44  if (!MooseUtils::hasExtension(_mesh_file, "e", /*strip_exodus_ext =*/true))
45  paramError("mesh",
46  "Performing transient adjoint simulation currently only works if the forward "
47  "solution is written and read from exodus format.");
48 }
49 
50 Real
52 {
53  return _reverse_time_end - _t + _dt;
54 }
55 
56 void
58 {
59  // Re-read the mesh file if it has been modified
60  struct stat stats;
61  stat(_mesh_file.c_str(), &stats);
62  if (stats.st_mtime > _file_mod_time)
63  {
64  _file_mod_time = stats.st_mtime;
65 
66  // Tell the parent class that we do need to re-initialize
67  _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
75 
76  // Make sure to communicate what solution was actually loaded
78  }
79 
81 }
static InputParameters validParams()
std::unique_ptr< libMesh::EquationSystems > _es
virtual void initialSetup() override
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.
bool hasExtension(const std::string &filename, std::string ext, bool strip_exodus_ext=false)
void paramError(const std::string &param, Args... args) const
virtual Real solutionSampleTime() override
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)