19 #include "libmesh/fuzzy_equals.h" 20 #include "libmesh/petsc_matrix.h" 21 #include "libmesh/petsc_vector.h" 32 "Name of the nonlinear system representing the forward problem. Multiple and linear solver " 33 "systems are not currently supported.");
35 "adjoint_system",
"Name of the system representing the adjoint problem.");
42 _problem.nlSysNum(getParam<
std::vector<SolverSystemName>>(
"forward_system")[0])),
43 _adjoint_sys_num(_problem.nlSysNum(getParam<NonlinearSystemName>(
"adjoint_system"))),
44 _nl_forward(_problem.getNonlinearSystemBase(_forward_sys_num)),
45 _nl_adjoint(_problem.getNonlinearSystemBase(_adjoint_sys_num))
48 if (
getParam<std::vector<SolverSystemName>>(
"forward_system").size() != 1)
50 "Multiple nonlinear forward systems is not supported at the moment");
54 paramError(
"forward_system",
"Forward system does not appear to be a 'NonlinearSystem'.");
56 paramError(
"adjoint_system",
"Adjoint system does not appear to be a 'NonlinearSystem'.");
68 "We should have forward and adjoint systems as evidenced by our initialization list");
81 TIME_SECTION(
"execute", 1,
"Executing adjoint problem",
false);
84 "I don't see any code path in which this class winds up with an inner solve");
95 _console <<
"MultiApps failed to converge on ADJOINT_TIMESTEP_BEGIN!" << std::endl;
110 const auto tol = es.parameters.get<
Real>(
"linear solver tolerance");
111 const auto maxits = es.parameters.get<
unsigned int>(
"linear solver maximum iterations");
121 solver.adjoint_solve(matrix, solution, rhs,
tol, maxits);
127 if (solver.get_converged_reason() < 0)
129 _console <<
"Adjoint solve failed to converge with reason: " 130 << Utility::enum_to_string(solver.get_converged_reason()) << std::endl;
137 _console <<
"MultiApps failed to converge on ADJOINT_TIMESTEP_END!" << std::endl;
163 std::vector<dof_id_type> nbc_dofs;
165 if (nbc_warehouse.hasActiveObjects())
170 Node * node = bnode->_node;
172 if (!nbc_warehouse.hasActiveBoundaryObjects(boundary_id) ||
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)
186 if (petsc_matrix && petsc_solution && petsc_rhs)
187 LibmeshPetscCall(MatZeroRowsColumns(petsc_matrix->mat(),
188 cast_int<PetscInt>(nbc_dofs.size()),
191 petsc_solution->vec(),
194 mooseError(
"Using PETSc matrices and vectors is required for applying homogenized boundary " 203 for (
const auto & adj_var : adj_vars)
208 "User cannot supply scaling factors for adjoint variables. Adjoint system is scaled " 209 "automatically by the forward system.");
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.");
const unsigned int _forward_sys_num
The number of the nonlinear system representing the forward model.
const std::vector< MooseVariableFieldBase *> & getVariables(THREAD_ID tid)
Moose::PetscSupport::PetscOptions & getPetscOptions()
const MooseObjectTagWarehouse< NodalBCBase > & getNodalBCWarehouse() const
dof_id_type dof_number(const unsigned int s, const unsigned int var, const unsigned int comp) const
static InputParameters validParams()
bool hasVector(const std::string &tag_name) const
NumericVector< Number > & solution()
TagID nonTimeVectorTag() const override
The following methods are specializations for using the Parallel::packed_range_* routines for a vecto...
dof_id_type n_dofs() const
PetscInt * numeric_petsc_cast(const numeric_index_type *p)
virtual void scale(const T factor)=0
virtual void execute(const ExecFlagType &exec_type)
bool automaticScaling() const
void setCurrentNonlinearSystem(const unsigned int nl_sys_num)
virtual const NumericVector< Number > *const & currentSolution() const override final
void applyNodalBCs(SparseMatrix< Number > &matrix, const NumericVector< Number > &solution, NumericVector< Number > &rhs)
Helper function for applying nodal BCs to the adjoint matrix and RHS.
virtual void computeJacobian(const NumericVector< libMesh::Number > &soln, libMesh::SparseMatrix< libMesh::Number > &jacobian, const unsigned int nl_sys_num)
boundary_id_type BoundaryID
SolveObject * _inner_solve
virtual libMesh::EquationSystems & es() override
const T & getParam(const std::string &name) const
void checkIntegrity()
Checks whether the forward and adjoint systems are consistent.
void paramError(const std::string ¶m, Args... args) const
unsigned int number() const
virtual bool solve() override
Solve the adjoint system with the following procedure:
void petscSetOptions(const PetscOptions &po, const SolverParams &solver_params, FEProblemBase *const problem=nullptr)
void setConvergedReasonFlags(FEProblemBase &fe_problem, const std::string &prefix)
const ExecFlagType EXEC_ADJOINT_TIMESTEP_END
bool absolute_fuzzy_equals(const T &var1, const T2 &var2, const Real tol=TOLERANCE *TOLERANCE)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void removeVector(const std::string &name)
AdjointSolve(Executioner &ex)
const unsigned int _adjoint_sys_num
The number of the nonlinear system representing the adjoint model.
std::string prefix() const
void mooseError(Args &&... args) const
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.
const ConsoleStream _console
bool execMultiApps(ExecFlagType type, bool auto_advance=true)
void storePetscOptions(FEProblemBase &fe_problem, const std::string &prefix, const ParallelParamObject ¶m_object)
NumericVector< Number > & getResidualNonTimeVector()
NonlinearSystemBase & _nl_adjoint
The nonlinear system representing the adjoint model.
processor_id_type processor_id() const
virtual std::size_t numSolverSystems() const override
virtual NumericVector< Number > & getVector(const std::string &name)
processor_id_type processor_id() const
libMesh::StoredRange< MooseMesh::const_bnd_node_iterator, const BndNode *> * getBoundaryNodeRange()
virtual void outputStep(ExecFlagType type)
virtual void computeResidualTag(const NumericVector< libMesh::Number > &soln, NumericVector< libMesh::Number > &residual, TagID tag)
NonlinearSystemBase & _nl_forward
The nonlinear system representing the forward model.
virtual libMesh::System & system() override