https://mooseframework.inl.gov
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
AdjointSolve Class Reference

The solve object is responsible for solving the adjoint version of a forward model. More...

#include <AdjointSolve.h>

Inheritance diagram for AdjointSolve:
[legend]

Public Types

typedef DataFileName DataFileParameterType
 

Public Member Functions

 AdjointSolve (Executioner &ex)
 
virtual bool solve () override
 Solve the adjoint system with the following procedure: More...
 
virtual void initialSetup ()
 
virtual void setInnerSolve (SolveObject &solve)
 
virtual bool enabled () const
 
std::shared_ptr< MooseObjectgetSharedPtr ()
 
std::shared_ptr< const MooseObjectgetSharedPtr () const
 
MooseAppgetMooseApp () const
 
const std::string & type () const
 
virtual const std::string & name () const
 
std::string typeAndName () const
 
std::string errorPrefix (const std::string &error_type) const
 
void callMooseError (std::string msg, const bool with_prefix) const
 
MooseObjectParameterName uniqueParameterName (const std::string &parameter_name) const
 
const InputParametersparameters () const
 
MooseObjectName uniqueName () const
 
const T & getParam (const std::string &name) const
 
std::vector< std::pair< T1, T2 > > getParam (const std::string &param1, const std::string &param2) const
 
const T * queryParam (const std::string &name) const
 
const T & getRenamedParam (const std::string &old_name, const std::string &new_name) const
 
getCheckedPointerParam (const std::string &name, const std::string &error_string="") const
 
bool isParamValid (const std::string &name) const
 
bool isParamSetByUser (const std::string &nm) const
 
void paramError (const std::string &param, Args... args) const
 
void paramWarning (const std::string &param, Args... args) const
 
void paramInfo (const std::string &param, Args... args) const
 
void connectControllableParams (const std::string &parameter, const std::string &object_type, const std::string &object_name, const std::string &object_parameter) const
 
void mooseError (Args &&... args) const
 
void mooseErrorNonPrefixed (Args &&... args) const
 
void mooseDocumentedError (const std::string &repo_name, const unsigned int issue_num, Args &&... args) const
 
void mooseWarning (Args &&... args) const
 
void mooseWarningNonPrefixed (Args &&... args) const
 
void mooseDeprecated (Args &&... args) const
 
void mooseInfo (Args &&... args) const
 
std::string getDataFileName (const std::string &param) const
 
std::string getDataFileNameByName (const std::string &relative_path) const
 
std::string getDataFilePath (const std::string &relative_path) const
 
PerfGraphperfGraph ()
 
bool isDefaultPostprocessorValue (const std::string &param_name, const unsigned int index=0) const
 
bool hasPostprocessor (const std::string &param_name, const unsigned int index=0) const
 
bool hasPostprocessorByName (const PostprocessorName &name) const
 
std::size_t coupledPostprocessors (const std::string &param_name) const
 
const PostprocessorName & getPostprocessorName (const std::string &param_name, const unsigned int index=0) const
 
const PostprocessorValuegetPostprocessorValue (const std::string &param_name, const unsigned int index=0) const
 
const PostprocessorValuegetPostprocessorValue (const std::string &param_name, const unsigned int index=0) const
 
const PostprocessorValuegetPostprocessorValueOld (const std::string &param_name, const unsigned int index=0) const
 
const PostprocessorValuegetPostprocessorValueOld (const std::string &param_name, const unsigned int index=0) const
 
const PostprocessorValuegetPostprocessorValueOlder (const std::string &param_name, const unsigned int index=0) const
 
const PostprocessorValuegetPostprocessorValueOlder (const std::string &param_name, const unsigned int index=0) const
 
virtual const PostprocessorValuegetPostprocessorValueByName (const PostprocessorName &name) const
 
virtual const PostprocessorValuegetPostprocessorValueByName (const PostprocessorName &name) const
 
const PostprocessorValuegetPostprocessorValueOldByName (const PostprocessorName &name) const
 
const PostprocessorValuegetPostprocessorValueOldByName (const PostprocessorName &name) const
 
const PostprocessorValuegetPostprocessorValueOlderByName (const PostprocessorName &name) const
 
const PostprocessorValuegetPostprocessorValueOlderByName (const PostprocessorName &name) const
 
const Parallel::Communicator & comm () const
 
processor_id_type n_processors () const
 
processor_id_type processor_id () const
 

Static Public Member Functions

static InputParameters validParams ()
 

Public Attributes

const ConsoleStream _console
 

Protected Member Functions

void checkIntegrity ()
 Checks whether the forward and adjoint systems are consistent. More...
 
virtual void assembleAdjointSystem (SparseMatrix< Number > &matrix, const NumericVector< Number > &solution, NumericVector< Number > &rhs)
 Assembles adjoint system. More...
 
void applyNodalBCs (SparseMatrix< Number > &matrix, const NumericVector< Number > &solution, NumericVector< Number > &rhs)
 Helper function for applying nodal BCs to the adjoint matrix and RHS. More...
 
PerfID registerTimedSection (const std::string &section_name, const unsigned int level) const
 
PerfID registerTimedSection (const std::string &section_name, const unsigned int level, const std::string &live_message, const bool print_dots=true) const
 
std::string timedSectionName (const std::string &section_name) const
 
virtual void addPostprocessorDependencyHelper (const PostprocessorName &) const
 

Protected Attributes

const unsigned int _forward_sys_num
 The number of the nonlinear system representing the forward model. More...
 
const unsigned int _adjoint_sys_num
 The number of the nonlinear system representing the adjoint model. More...
 
NonlinearSystemBase_nl_forward
 The nonlinear system representing the forward model. More...
 
NonlinearSystemBase_nl_adjoint
 The nonlinear system representing the adjoint model. More...
 
Executioner_executioner
 
FEProblemBase_problem
 
DisplacedProblem_displaced_problem
 
MooseMesh_mesh
 
MooseMesh_displaced_mesh
 
SystemBase_solver_sys
 
AuxiliarySystem_aux
 
SolveObject_inner_solve
 
const bool & _enabled
 
MooseApp_app
 
const std::string _type
 
const std::string _name
 
const InputParameters_pars
 
Factory_factory
 
ActionFactory_action_factory
 
MooseApp_pg_moose_app
 
const std::string _prefix
 
const Parallel::Communicator & _communicator
 

Detailed Description

The solve object is responsible for solving the adjoint version of a forward model.

It does this by solving a linear system with a transposed matrix and a source. The matrix is evaluated from the forward model's Jacobian, using the converged solution. The source is computed by evaluating the residual of a secondary nonlinear-system representing the adjoint system, in which the adjoint solution is 0.

Definition at line 31 of file AdjointSolve.h.

Constructor & Destructor Documentation

◆ AdjointSolve()

AdjointSolve::AdjointSolve ( Executioner ex)

Definition at line 39 of file AdjointSolve.C.

40  : SolveObject(ex),
42  _problem.nlSysNum(getParam<std::vector<SolverSystemName>>("forward_system")[0])),
43  _adjoint_sys_num(_problem.nlSysNum(getParam<NonlinearSystemName>("adjoint_system"))),
46 {
47  // Disallow vectors of systems
48  if (getParam<std::vector<SolverSystemName>>("forward_system").size() != 1)
49  paramError("forward_system",
50  "Multiple nonlinear forward systems is not supported at the moment");
51 
52  // These should never be hit, but just in case
53  if (!dynamic_cast<NonlinearSystem *>(&_nl_forward))
54  paramError("forward_system", "Forward system does not appear to be a 'NonlinearSystem'.");
55  if (!dynamic_cast<NonlinearSystem *>(&_nl_adjoint))
56  paramError("adjoint_system", "Adjoint system does not appear to be a 'NonlinearSystem'.");
57  // Adjoint system should never perform its own automatic scaling. Scaling factors from the forward
58  // system are applied.
60 
61  // We need to force the forward system to have a scaling vector. This is
62  // in case a user provides scaling for an individual variables but doesn't have any
63  // AD objects.
65 
66  // Set the solver options for the adjoint system
67  mooseAssert(_problem.numSolverSystems() > 1,
68  "We should have forward and adjoint systems as evidenced by our initialization list");
69  const auto prefix = _nl_adjoint.prefix();
72  // Set solver parameter prefix
73  auto & solver_params = _problem.solverParams(_nl_adjoint.number());
74  solver_params._prefix = prefix;
75  solver_params._solver_sys_num = _nl_adjoint.number();
76 }
const unsigned int _forward_sys_num
The number of the nonlinear system representing the forward model.
Definition: AdjointSolve.h:86
FEProblemBase & _problem
std::string _prefix
bool automaticScaling() const
const T & getParam(const std::string &name) const
NonlinearSystemBase & getNonlinearSystemBase(const unsigned int sys_num)
void paramError(const std::string &param, Args... args) const
unsigned int number() const
SolveObject(Executioner &ex)
void setConvergedReasonFlags(FEProblemBase &fe_problem, const std::string &prefix)
const unsigned int _adjoint_sys_num
The number of the nonlinear system representing the adjoint model.
Definition: AdjointSolve.h:88
std::string prefix() const
SolverParams & solverParams(unsigned int solver_sys_num=0)
void storePetscOptions(FEProblemBase &fe_problem, const std::string &prefix, const ParallelParamObject &param_object)
NonlinearSystemBase & _nl_adjoint
The nonlinear system representing the adjoint model.
Definition: AdjointSolve.h:92
virtual std::size_t numSolverSystems() const override
virtual unsigned int nlSysNum(const NonlinearSystemName &nl_sys_name) const override
NonlinearSystemBase & _nl_forward
The nonlinear system representing the forward model.
Definition: AdjointSolve.h:90

Member Function Documentation

◆ applyNodalBCs()

void AdjointSolve::applyNodalBCs ( SparseMatrix< Number > &  matrix,
const NumericVector< Number > &  solution,
NumericVector< Number > &  rhs 
)
protected

Helper function for applying nodal BCs to the adjoint matrix and RHS.

Say there is a BC setting the d-th DoF to a dirichlet condition on the forward problem. This basically sets the d-th column of the matrix to zero, the d-th entry of the matrix diagonal to one, and the d-th entry of the RHS to the solution passed in.

Parameters
matrixThe matrix whose columns are set to 0
solutionThe solution to replace the entries of the RHS
rhsThe RHS to to replace with the solution

Definition at line 159 of file AdjointSolve.C.

Referenced by solve().

162 {
163  std::vector<dof_id_type> nbc_dofs;
164  auto & nbc_warehouse = _nl_forward.getNodalBCWarehouse();
165  if (nbc_warehouse.hasActiveObjects())
166  {
167  for (const auto & bnode : *_mesh.getBoundaryNodeRange())
168  {
169  BoundaryID boundary_id = bnode->_bnd_id;
170  Node * node = bnode->_node;
171 
172  if (!nbc_warehouse.hasActiveBoundaryObjects(boundary_id) ||
173  node->processor_id() != processor_id())
174  continue;
175 
176  for (const auto & bc : nbc_warehouse.getActiveBoundaryObjects(boundary_id))
177  if (bc->shouldApply())
178  for (unsigned int c = 0; c < bc->variable().count(); ++c)
179  nbc_dofs.push_back(node->dof_number(_forward_sys_num, bc->variable().number() + c, 0));
180  }
181 
182  // Petsc has a nice interface for zeroing rows and columns, so we'll use it
183  auto petsc_matrix = dynamic_cast<PetscMatrix<Number> *>(&matrix);
184  auto petsc_solution = dynamic_cast<const PetscVector<Number> *>(&solution);
185  auto petsc_rhs = dynamic_cast<PetscVector<Number> *>(&rhs);
186  if (petsc_matrix && petsc_solution && petsc_rhs)
187  LibmeshPetscCall(MatZeroRowsColumns(petsc_matrix->mat(),
188  cast_int<PetscInt>(nbc_dofs.size()),
189  numeric_petsc_cast(nbc_dofs.data()),
190  1.0,
191  petsc_solution->vec(),
192  petsc_rhs->vec()));
193  else
194  mooseError("Using PETSc matrices and vectors is required for applying homogenized boundary "
195  "conditions.");
196  }
197 }
const unsigned int _forward_sys_num
The number of the nonlinear system representing the forward model.
Definition: AdjointSolve.h:86
const MooseObjectTagWarehouse< NodalBCBase > & getNodalBCWarehouse() const
dof_id_type dof_number(const unsigned int s, const unsigned int var, const unsigned int comp) const
MooseMesh & _mesh
PetscInt * numeric_petsc_cast(const numeric_index_type *p)
boundary_id_type BoundaryID
void mooseError(Args &&... args) const
processor_id_type processor_id() const
processor_id_type processor_id() const
libMesh::StoredRange< MooseMesh::const_bnd_node_iterator, const BndNode *> * getBoundaryNodeRange()
NonlinearSystemBase & _nl_forward
The nonlinear system representing the forward model.
Definition: AdjointSolve.h:90

◆ assembleAdjointSystem()

void AdjointSolve::assembleAdjointSystem ( SparseMatrix< Number > &  matrix,
const NumericVector< Number > &  solution,
NumericVector< Number > &  rhs 
)
protectedvirtual

Assembles adjoint system.

Parameters
matrixUn-transposed matrix (will be transposed later in solver)
solutionAdjoint solution (basically the initial guess for the solver)
rhsThe adjoint source (i.e. -residual)

Reimplemented in AdjointTransientSolve.

Definition at line 145 of file AdjointSolve.C.

Referenced by AdjointTransientSolve::assembleAdjointSystem(), and solve().

148 {
149 
151 
154 
155  rhs.scale(-1.0);
156 }
const unsigned int _forward_sys_num
The number of the nonlinear system representing the forward model.
Definition: AdjointSolve.h:86
FEProblemBase & _problem
TagID nonTimeVectorTag() const override
virtual void scale(const T factor)=0
void setCurrentNonlinearSystem(const unsigned int nl_sys_num)
virtual const NumericVector< Number > *const & currentSolution() const override final
virtual void computeJacobian(const NumericVector< libMesh::Number > &soln, libMesh::SparseMatrix< libMesh::Number > &jacobian, const unsigned int nl_sys_num)
const unsigned int _adjoint_sys_num
The number of the nonlinear system representing the adjoint model.
Definition: AdjointSolve.h:88
NonlinearSystemBase & _nl_adjoint
The nonlinear system representing the adjoint model.
Definition: AdjointSolve.h:92
virtual void computeResidualTag(const NumericVector< libMesh::Number > &soln, NumericVector< libMesh::Number > &residual, TagID tag)
NonlinearSystemBase & _nl_forward
The nonlinear system representing the forward model.
Definition: AdjointSolve.h:90

◆ checkIntegrity()

void AdjointSolve::checkIntegrity ( )
protected

Checks whether the forward and adjoint systems are consistent.

Definition at line 200 of file AdjointSolve.C.

Referenced by solve().

201 {
202  const auto adj_vars = _nl_adjoint.getVariables(0);
203  for (const auto & adj_var : adj_vars)
204  // If the user supplies any scaling factors for individual variables the
205  // adjoint system won't be consistent.
206  if (!absolute_fuzzy_equals(adj_var->scalingFactor(), 1.0))
207  mooseError(
208  "User cannot supply scaling factors for adjoint variables. Adjoint system is scaled "
209  "automatically by the forward system.");
210 
211  // This is to prevent automatic scaling of the adjoint system. Scaling is
212  // taken from the forward system
213  if (_nl_adjoint.hasVector("scaling_factors"))
214  _nl_adjoint.removeVector("scaling_factors");
215 
216  // Main thing is that the number of dofs in each system is the same
218  mooseError(
219  "The forward and adjoint systems do not seem to be the same size. This could be due to (1) "
220  "the number of variables added to each system is not the same, (2) variables do not have "
221  "consistent family/order, (3) variables do not have the same block restriction.");
222 }
const std::vector< MooseVariableFieldBase *> & getVariables(THREAD_ID tid)
bool hasVector(const std::string &tag_name) const
dof_id_type n_dofs() const
bool absolute_fuzzy_equals(const T &var1, const T2 &var2, const Real tol=TOLERANCE *TOLERANCE)
void removeVector(const std::string &name)
void mooseError(Args &&... args) const
NonlinearSystemBase & _nl_adjoint
The nonlinear system representing the adjoint model.
Definition: AdjointSolve.h:92
NonlinearSystemBase & _nl_forward
The nonlinear system representing the forward model.
Definition: AdjointSolve.h:90
virtual libMesh::System & system() override

◆ solve()

bool AdjointSolve::solve ( )
overridevirtual

Solve the adjoint system with the following procedure:

  1. Call the _inner_solve
  2. Execute user-objects, auxkernels, and multiapps on ADJOINT_TIMESTEP_BEGIN
  3. Assemble the adjoint system: 3a. Evaluate forward system Jacobian 3b. Evaluate adjoint system residual
  4. Solve adjoint system by calling libMesh::linearSolver::adjoint_solve
  5. Execute user-objects, auxkernels, and multiapps on ADJOINT_TIMESTEP_END
Returns
true Inner solve, multiapps, and adjoint solve all converged
false Inner solve, multiapps, or adjoint solve did not converge

Implements SolveObject.

Reimplemented in AdjointTransientSolve.

Definition at line 79 of file AdjointSolve.C.

Referenced by SteadyAndAdjoint::execute(), and AdjointTransientSolve::solve().

80 {
81  TIME_SECTION("execute", 1, "Executing adjoint problem", false);
82 
83  mooseAssert(!_inner_solve,
84  "I don't see any code path in which this class winds up with an inner solve");
85 
86  // enforce PETSc options are passed to the adjoint solve as well, as set in the user file
87  auto & petsc_options = _problem.getPetscOptions();
88  auto & pars = _problem.solverParams(_nl_adjoint.number());
89  Moose::PetscSupport::petscSetOptions(petsc_options, pars);
90 
92 
94  {
95  _console << "MultiApps failed to converge on ADJOINT_TIMESTEP_BEGIN!" << std::endl;
96  return false;
97  }
98  // Output results between the forward and adjoint solve.
100 
101  checkIntegrity();
102 
103  // Convenient references
104  // Adjoint matrix, solution, and right-hand-side
105  auto & matrix = static_cast<ImplicitSystem &>(_nl_forward.system()).get_system_matrix();
106  auto & solution = _nl_adjoint.solution();
107  auto & rhs = _nl_adjoint.getResidualNonTimeVector();
108  // Linear solver parameters
109  auto & es = _problem.es();
110  const auto tol = es.parameters.get<Real>("linear solver tolerance");
111  const auto maxits = es.parameters.get<unsigned int>("linear solver maximum iterations");
112  // Linear solver for adjoint system
113  auto & solver = *static_cast<ImplicitSystem &>(_nl_adjoint.system()).get_linear_solver();
114 
115  // Assemble adjoint system by evaluating the forward Jacobian, computing the adjoint
116  // residual/source, and homogenizing nodal BCs
117  assembleAdjointSystem(matrix, solution, rhs);
118  applyNodalBCs(matrix, solution, rhs);
119 
120  // Solve the adjoint system
121  solver.adjoint_solve(matrix, solution, rhs, tol, maxits);
122 
123  // For scaling of the forward problem we need to apply correction factor
124  solution *= _nl_forward.getVector("scaling_factors");
125 
127  if (solver.get_converged_reason() < 0)
128  {
129  _console << "Adjoint solve failed to converge with reason: "
130  << Utility::enum_to_string(solver.get_converged_reason()) << std::endl;
131  return false;
132  }
133 
136  {
137  _console << "MultiApps failed to converge on ADJOINT_TIMESTEP_END!" << std::endl;
138  return false;
139  }
140 
141  return true;
142 }
FEProblemBase & _problem
Moose::PetscSupport::PetscOptions & getPetscOptions()
NumericVector< Number > & solution()
const double tol
virtual void execute(const ExecFlagType &exec_type)
void applyNodalBCs(SparseMatrix< Number > &matrix, const NumericVector< Number > &solution, NumericVector< Number > &rhs)
Helper function for applying nodal BCs to the adjoint matrix and RHS.
Definition: AdjointSolve.C:159
SolveObject * _inner_solve
virtual libMesh::EquationSystems & es() override
void checkIntegrity()
Checks whether the forward and adjoint systems are consistent.
Definition: AdjointSolve.C:200
unsigned int number() const
void petscSetOptions(const PetscOptions &po, const SolverParams &solver_params, FEProblemBase *const problem=nullptr)
const ExecFlagType EXEC_ADJOINT_TIMESTEP_END
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
SolverParams & solverParams(unsigned int solver_sys_num=0)
const ExecFlagType EXEC_ADJOINT_TIMESTEP_BEGIN
virtual void assembleAdjointSystem(SparseMatrix< Number > &matrix, const NumericVector< Number > &solution, NumericVector< Number > &rhs)
Assembles adjoint system.
Definition: AdjointSolve.C:145
const ConsoleStream _console
bool execMultiApps(ExecFlagType type, bool auto_advance=true)
NumericVector< Number > & getResidualNonTimeVector()
NonlinearSystemBase & _nl_adjoint
The nonlinear system representing the adjoint model.
Definition: AdjointSolve.h:92
virtual NumericVector< Number > & getVector(const std::string &name)
virtual void outputStep(ExecFlagType type)
NonlinearSystemBase & _nl_forward
The nonlinear system representing the forward model.
Definition: AdjointSolve.h:90
virtual libMesh::System & system() override

◆ validParams()

InputParameters AdjointSolve::validParams ( )
static

Definition at line 27 of file AdjointSolve.C.

Referenced by SteadyAndAdjoint::validParams(), and AdjointTransientSolve::validParams().

28 {
30  params.addRequiredParam<std::vector<SolverSystemName>>(
31  "forward_system",
32  "Name of the nonlinear system representing the forward problem. Multiple and linear solver "
33  "systems are not currently supported.");
34  params.addRequiredParam<NonlinearSystemName>(
35  "adjoint_system", "Name of the system representing the adjoint problem.");
36  return params;
37 }
void addRequiredParam(const std::string &name, const std::string &doc_string)
InputParameters emptyInputParameters()

Member Data Documentation

◆ _adjoint_sys_num

const unsigned int AdjointSolve::_adjoint_sys_num
protected

The number of the nonlinear system representing the adjoint model.

Definition at line 88 of file AdjointSolve.h.

Referenced by assembleAdjointSystem().

◆ _forward_sys_num

const unsigned int AdjointSolve::_forward_sys_num
protected

The number of the nonlinear system representing the forward model.

Definition at line 86 of file AdjointSolve.h.

Referenced by applyNodalBCs(), assembleAdjointSystem(), and AdjointTransientSolve::evaluateTimeResidual().

◆ _nl_adjoint

NonlinearSystemBase& AdjointSolve::_nl_adjoint
protected

◆ _nl_forward

NonlinearSystemBase& AdjointSolve::_nl_forward
protected

The documentation for this class was generated from the following files: