Line data Source code
1 : // The libMesh Finite Element Library. 2 : // Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner 3 : 4 : // This library is free software; you can redistribute it and/or 5 : // modify it under the terms of the GNU Lesser General Public 6 : // License as published by the Free Software Foundation; either 7 : // version 2.1 of the License, or (at your option) any later version. 8 : 9 : // This library is distributed in the hope that it will be useful, 10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 : // Lesser General Public License for more details. 13 : 14 : // You should have received a copy of the GNU Lesser General Public 15 : // License along with this library; if not, write to the Free Software 16 : // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 : 18 : 19 : 20 : #ifndef LIBMESH_LINEAR_IMPLICIT_SYSTEM_H 21 : #define LIBMESH_LINEAR_IMPLICIT_SYSTEM_H 22 : 23 : // Local Includes 24 : #include "libmesh/implicit_system.h" 25 : 26 : // C++ includes 27 : #include <cstddef> 28 : #include <optional> 29 : 30 : namespace libMesh 31 : { 32 : 33 : 34 : // Forward Declarations 35 : template <typename T> class LinearSolver; 36 : template <typename T> class ShellMatrix; 37 : class SolverConfiguration; 38 : 39 : /** 40 : * Per-call options for LinearImplicitSystem::solve(). 41 : */ 42 : struct LinearImplicitSystemSolveOptions 43 : { 44 : /** 45 : * Solver to use for this call. A null pointer selects the solver 46 : * owned by the system. The caller retains ownership of a borrowed 47 : * solver and must keep it alive for the duration of the call. 48 : */ 49 : LinearSolver<Number> * solver = nullptr; 50 : 51 : /** 52 : * Configuration to attach for this call. When non-null, the 53 : * configuration supplies rel_tol, abs_tol, and max_its. 54 : */ 55 : SolverConfiguration * solver_configuration = nullptr; 56 : 57 : /** 58 : * Per-call assembly policy. An unset value uses the system's 59 : * persistent assemble_before_solve setting. 60 : */ 61 : std::optional<bool> assemble_before_solve; 62 : }; 63 : 64 : /** 65 : * \brief Manages consistently variables, degrees of freedom, coefficient 66 : * vectors, matrices and linear solvers for implicit systems. 67 : * 68 : * An implicit system is a system that requires the solution of a 69 : * system of equations. This class has the ability to create and use a 70 : * linear solver to solve the system. 71 : * 72 : * The matrix LinearImplicitSystem::matrix and the vector 73 : * LinearImplicitSystem::rhs should be filled during assembly. 74 : * 75 : * \note Additional vectors/matrices can be added via parent class 76 : * interfaces. 77 : * 78 : * \author Benjamin Kirk 79 : * \date 2005 80 : */ 81 1240 : class LinearImplicitSystem : public ImplicitSystem 82 : { 83 : public: 84 : 85 : /** 86 : * Constructor. 87 : */ 88 : LinearImplicitSystem (EquationSystems & es, 89 : const std::string & name, 90 : const unsigned int number); 91 : 92 : /** 93 : * Special functions. 94 : * - This class has the same restrictions/defaults as its base class. 95 : * - The destructor is defaulted out-of-line. 96 : */ 97 : LinearImplicitSystem (const LinearImplicitSystem &) = delete; 98 : LinearImplicitSystem & operator= (const LinearImplicitSystem &) = delete; 99 : LinearImplicitSystem (LinearImplicitSystem &&) = default; 100 : LinearImplicitSystem & operator= (LinearImplicitSystem &&) = delete; 101 : virtual ~LinearImplicitSystem (); 102 : 103 : /** 104 : * The type of system. 105 : */ 106 : typedef LinearImplicitSystem sys_type; 107 : 108 : /** 109 : * The type of the parent. 110 : */ 111 : typedef ImplicitSystem Parent; 112 : 113 : /** 114 : * \returns A reference to *this. 115 : */ 116 : sys_type & system () { return *this; } 117 : 118 : /** 119 : * Clear all the data structures associated with 120 : * the system. 121 : */ 122 : virtual void clear () override; 123 : 124 : /** 125 : * Initializes new data members of the system 126 : */ 127 : virtual void init_data () override; 128 : 129 : /** 130 : * Reinitializes the member data fields associated with 131 : * the system, so that, e.g., \p assemble() may be used. 132 : */ 133 : virtual void reinit () override; 134 : 135 : /** 136 : * Prepares \p matrix and \p _dof_map for matrix assembly. 137 : * Does not actually assemble anything. For matrix assembly, 138 : * use the \p assemble() in derived classes. 139 : * Should be overridden in derived classes. 140 : */ 141 46122 : virtual void assemble () override { ImplicitSystem::assemble(); } 142 : 143 : /** 144 : * After calling this method, any solve will be limited to the given 145 : * subset. To disable this mode, call this method with \p subset 146 : * being a \p nullptr. 147 : */ 148 : virtual void restrict_solve_to (const SystemSubset * subset, 149 : const SubsetSolveMode subset_solve_mode=SUBSET_ZERO) override; 150 : 151 : /** 152 : * Assembles & solves the linear system A*x=b. 153 : */ 154 : virtual void solve () override; 155 : 156 : /** 157 : * Optionally assembles and solves the linear system A*x=b using 158 : * per-call solver options. 159 : */ 160 : void solve(const LinearImplicitSystemSolveOptions & options); 161 : 162 : /** 163 : * \returns A pointer to a linear solver appropriate for use in 164 : * adjoint and/or sensitivity solves 165 : */ 166 : virtual LinearSolver<Number> * get_linear_solver() const override; 167 : 168 : /** 169 : * Assembles a residual in \p rhs and/or a jacobian in \p matrix, 170 : * as requested. 171 : */ 172 : virtual void assembly(bool get_residual, 173 : bool get_jacobian, 174 : bool apply_heterogeneous_constraints = false, 175 : bool apply_no_constraints = false) override; 176 : 177 : /** 178 : * \returns \p "LinearImplicit". Helps in identifying 179 : * the system type in an equation system file. 180 : */ 181 11580 : virtual std::string system_type () const override { return "LinearImplicit"; } 182 : 183 : /** 184 : * \returns The number of iterations 185 : * taken for the most recent linear solve. 186 : */ 187 0 : unsigned int n_linear_iterations() const { return _n_linear_iterations; } 188 : 189 : /** 190 : * \returns The final residual for the linear system solve. 191 : */ 192 0 : Real final_linear_residual() const { return _final_linear_residual; } 193 : 194 : /** 195 : * This function enables the user to provide a shell matrix, i.e. a 196 : * matrix that is not stored element-wise, but as a function. When 197 : * you register your shell matrix using this function, calling \p 198 : * solve() will no longer use the \p matrix member but the 199 : * registered shell matrix instead. You can reset this behaviour to 200 : * its original state by supplying a \p nullptr to this 201 : * function. 202 : */ 203 : void attach_shell_matrix (ShellMatrix<Number> * shell_matrix); 204 : 205 : /** 206 : * Detaches a shell matrix. Same as \p attach_shell_matrix(nullptr). 207 : */ 208 : void detach_shell_matrix () { attach_shell_matrix(nullptr); } 209 : 210 : /** 211 : * \returns A pointer to the currently attached shell matrix, if any, 212 : * otherwise \p nullptr. 213 : */ 214 : ShellMatrix<Number> * get_shell_matrix() { return _shell_matrix; } 215 : 216 : virtual void create_static_condensation() override; 217 : 218 : protected: 219 : 220 : /** 221 : * The number of linear iterations required to solve the linear 222 : * system Ax=b. 223 : */ 224 : unsigned int _n_linear_iterations; 225 : 226 : /** 227 : * The final residual for the linear system Ax=b. 228 : */ 229 : Real _final_linear_residual; 230 : 231 : /** 232 : * User supplies shell matrix or \p nullptr if no shell matrix is used. 233 : */ 234 : ShellMatrix<Number> * _shell_matrix; 235 : 236 : /** 237 : * The current subset on which to solve (or \p nullptr if none). 238 : */ 239 : const SystemSubset * _subset; 240 : 241 : /** 242 : * If restrict-solve-to-subset mode is active, this member decides 243 : * what happens with the dofs outside the subset. 244 : */ 245 : SubsetSolveMode _subset_solve_mode; 246 : }; 247 : 248 : } // namespace libMesh 249 : 250 : #endif // LIBMESH_LINEAR_IMPLICIT_SYSTEM_H