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 : #pragma once 11 : 12 : #include "SolveObject.h" 13 : 14 : // Forward declarations 15 : class NonlinearSystemBase; 16 : namespace libMesh 17 : { 18 : template <typename T> 19 : class SparseMatrix; 20 : template <typename T> 21 : class NumericVector; 22 : } 23 : 24 : /** 25 : * The solve object is responsible for solving the adjoint version of a forward model. It does this 26 : * by solving a linear system with a transposed matrix and a source. The matrix is evaluated from 27 : * the forward model's Jacobian, using the converged solution. The source is computed by evaluating 28 : * the residual of a secondary nonlinear-system representing the adjoint system, in which the 29 : * adjoint solution is 0. 30 : */ 31 0 : class AdjointSolve : public SolveObject 32 : { 33 : public: 34 : AdjointSolve(Executioner & ex); 35 : 36 : static InputParameters validParams(); 37 : 38 : /** 39 : * Solve the adjoint system with the following procedure: 40 : * 1. Call the _inner_solve 41 : * 2. Execute user-objects, auxkernels, and multiapps on ADJOINT_TIMESTEP_BEGIN 42 : * 3. Assemble the adjoint system: 43 : * 3a. Evaluate forward system Jacobian 44 : * 3b. Evaluate adjoint system residual 45 : * 4. Solve adjoint system by calling libMesh::linearSolver::adjoint_solve 46 : * 5. Execute user-objects, auxkernels, and multiapps on ADJOINT_TIMESTEP_END 47 : * 48 : * @return true Inner solve, multiapps, and adjoint solve all converged 49 : * @return false Inner solve, multiapps, or adjoint solve did not converge 50 : */ 51 : virtual bool solve() override; 52 : 53 : protected: 54 : /** 55 : * Checks whether the forward and adjoint systems are consistent 56 : */ 57 : void checkIntegrity(); 58 : 59 : /** 60 : * Assembles adjoint system 61 : * 62 : * @param matrix Un-transposed matrix (will be transposed later in solver) 63 : * @param solution Adjoint solution (basically the initial guess for the solver) 64 : * @param rhs The adjoint source (i.e. -residual) 65 : */ 66 : virtual void assembleAdjointSystem(SparseMatrix<Number> & matrix, 67 : const NumericVector<Number> & solution, 68 : NumericVector<Number> & rhs); 69 : 70 : /** 71 : * Helper function for applying nodal BCs to the adjoint matrix and RHS. 72 : * Say there is a BC setting the d-th DoF to a dirichlet condition on the forward problem. 73 : * This basically sets the d-th column of the matrix to zero, 74 : * the d-th entry of the matrix diagonal to one, 75 : * and the d-th entry of the RHS to the solution passed in. 76 : * 77 : * @param matrix The matrix whose columns are set to 0 78 : * @param solution The solution to replace the entries of the RHS 79 : * @param rhs The RHS to to replace with the solution 80 : */ 81 : void applyNodalBCs(SparseMatrix<Number> & matrix, 82 : const NumericVector<Number> & solution, 83 : NumericVector<Number> & rhs); 84 : 85 : /// The number of the nonlinear system representing the forward model 86 : const unsigned int _forward_sys_num; 87 : /// The number of the nonlinear system representing the adjoint model 88 : const unsigned int _adjoint_sys_num; 89 : /// The nonlinear system representing the forward model 90 : NonlinearSystemBase & _nl_forward; 91 : /// The nonlinear system representing the adjoint model 92 : NonlinearSystemBase & _nl_adjoint; 93 : };