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 "SolverSystem.h" 13 : #include "LinearFVGradientInterface.h" 14 : #include "PerfGraphInterface.h" 15 : #include "GradientLimiterType.h" 16 : 17 : #include <set> 18 : 19 : #include "libmesh/transient_system.h" 20 : #include "libmesh/linear_implicit_system.h" 21 : #include "libmesh/linear_solver.h" 22 : 23 : class LinearFVKernel; 24 : class InputParameters; 25 : namespace Moose::Kokkos 26 : { 27 : class LinearFVKernel; 28 : class LinearFVElementalKernel; 29 : class LinearFVFluxKernel; 30 : class LinearFVBoundaryCondition; 31 : class System; 32 : } 33 : 34 : // libMesh forward declarations 35 : namespace libMesh 36 : { 37 : template <typename T> 38 : class NumericVector; 39 : template <typename T> 40 : class SparseMatrix; 41 : template <typename T> 42 : class DiagonalMatrix; 43 : } // namespace libMesh 44 : 45 : /** 46 : * Linear system to be solved 47 : */ 48 : class LinearSystem : public SolverSystem, 49 : public PerfGraphInterface, 50 : public LinearFVGradientInterface 51 : { 52 : public: 53 : LinearSystem(FEProblemBase & problem, const std::string & name); 54 : virtual ~LinearSystem(); 55 : 56 : virtual void solve() override; 57 : virtual void preInit() override; 58 : 59 : /** 60 : * At the moment, this is only used for the multi-system fixed point 61 : * iteration. We return true here since ther is no way to specify 62 : * separate linear residuals in FEProblemSolve yet. 63 : */ 64 25708 : virtual bool converged() override { return _converged; } 65 : 66 : virtual void initialSetup() override; 67 : virtual void reinit() override; 68 : 69 : #ifdef MOOSE_KOKKOS_ENABLED 70 : /** 71 : * Add a Kokkos linear finite volume kernel to this system 72 : * @param kernel_name The type of the kernel to add 73 : * @param name The name of the kernel to add 74 : * @param parameters The input parameters of the kernel 75 : */ 76 : void addKokkosKernel(const std::string & kernel_name, 77 : const std::string & name, 78 : InputParameters & parameters); 79 : 80 : /** 81 : * Add a Kokkos linear finite volume boundary condition to this system 82 : * @param bc_name The type of the boundary condition to add 83 : * @param name The name of the boundary condition to add 84 : * @param parameters The input parameters of the boundary condition 85 : */ 86 : void addKokkosBoundaryCondition(const std::string & bc_name, 87 : const std::string & name, 88 : InputParameters & parameters); 89 : #endif 90 : 91 : // Overriding these to make sure the linear systems don't do anything during 92 : // residual/jacobian setup 93 0 : virtual void residualSetup() override {} 94 0 : virtual void jacobianSetup() override {} 95 : 96 : /** 97 : * Quit the current solve as soon as possible. 98 : */ 99 : virtual void stopSolve(const ExecFlagType & exec_flag, 100 : const std::set<TagID> & vector_tags_to_close) override; 101 : 102 : /** 103 : * If the system has a kernel that corresponds to a time derivative. 104 : * Considering that we don't have transient capabilities for linear 105 : * systems at the moment, this is false. 106 : */ 107 : virtual bool containsTimeKernel() override; 108 0 : virtual std::vector<std::string> timeKernelVariableNames() override { return {}; } 109 : 110 : /** 111 : * Compute the right hand side and the system matrix of the system for given tags. 112 : * @param vector_tags The IDs of the vector tags whose right hand side contribution should be 113 : * included 114 : * @param matrix_tags The IDs of the matrix tags whose matrix contribution should be included 115 : * @param compute_gradients A flag to disable the computation of new gradients during the 116 : * assembly, can be used to lag gradients 117 : */ 118 : void computeLinearSystemTags(const std::set<TagID> & vector_tags, 119 : const std::set<TagID> & matrix_tags, 120 : const bool compute_gradients = true); 121 : 122 : /** 123 : * Return a reference to the stored linear implicit system 124 : */ 125 103750 : libMesh::LinearImplicitSystem & linearImplicitSystem() { return _linear_implicit_system; } 126 : 127 : /** 128 : * Return a numeric vector that is associated with the time tag. 129 : */ 130 : NumericVector<Number> & getRightHandSideTimeVector(); 131 : 132 : /** 133 : * Return a numeric vector that is associated with the nontime tag. 134 : */ 135 : NumericVector<Number> & getRightHandSideNonTimeVector(); 136 : 137 : virtual void augmentSparsity(SparsityPattern::Graph & sparsity, 138 : std::vector<dof_id_type> & n_nz, 139 : std::vector<dof_id_type> & n_oz) override; 140 : 141 : /** 142 : * Return the number of linear iterations 143 : */ 144 160 : unsigned int nLinearIterations() const { return _n_linear_iters; } 145 : 146 907264 : virtual System & system() override { return _sys; } 147 3738072 : virtual const System & system() const override { return _sys; } 148 : 149 : ///@{ 150 : /// Accessors of important tag IDs 151 0 : TagID rightHandSideTimeVectorTag() const { return _rhs_time_tag; } 152 : TagID rightHandSideNonTimeVectorTag() const { return _rhs_non_time_tag; } 153 77376 : TagID rightHandSideVectorTag() const { return _rhs_tag; } 154 77376 : virtual TagID systemMatrixTag() const override { return _system_matrix_tag; } 155 : ///@} 156 : 157 : /// Fetching the right hand side vector from the libmesh system. 158 25792 : NumericVector<Number> & getRightHandSideVector() { return *_linear_implicit_system.rhs; } 159 : const NumericVector<Number> & getRightHandSideVector() const 160 : { 161 : return *_linear_implicit_system.rhs; 162 : } 163 : 164 : /// Fetching the system matrix from the libmesh system. 165 25792 : SparseMatrix<Number> & getSystemMatrix() { return *_linear_implicit_system.matrix; } 166 : const SparseMatrix<Number> & getSystemMatrix() const { return *_linear_implicit_system.matrix; } 167 : 168 : using LinearFVGradientInterface::computeGradients; 169 : using LinearFVGradientInterface::linearFVLimitedGradientContainer; 170 : using LinearFVGradientInterface::requestLinearFVLimitedGradients; 171 : 172 : virtual void compute(ExecFlagType type) override; 173 : 174 : protected: 175 : /** 176 : * Compute the right hand side and system matrix for given tags 177 : * @param vector_tags The tags of kernels for which the right hand side is to be computed. 178 : * @param matrix_tags The tags of kernels for which the system matrix is to be computed. 179 : * @param compute_gradients A flag to disable the computation of new gradients during the 180 : * assembly, can be used to lag gradients 181 : */ 182 : void computeLinearSystemInternal(const std::set<TagID> & vector_tags, 183 : const std::set<TagID> & matrix_tags, 184 : const bool compute_gradients = true); 185 : 186 : #ifdef MOOSE_KOKKOS_ENABLED 187 : /** 188 : * Perform the initial setup of the Kokkos linear finite volume kernels and boundary conditions 189 : */ 190 : void initialSetupKokkosLinearFV(); 191 : 192 : /** 193 : * Assemble the Kokkos contributions to the linear system for the given tags 194 : * @param vector_tags The vector (right-hand side) tags to assemble 195 : * @param matrix_tags The matrix tags to assemble 196 : */ 197 : void computeKokkosLinearSystem(const std::set<TagID> & vector_tags, 198 : const std::set<TagID> & matrix_tags); 199 : #endif 200 : 201 : /// Base class reference to the libmesh system 202 : System & _sys; 203 : 204 : /// The linear iterations needed for convergence 205 : unsigned int _current_l_its; 206 : 207 : /// Vector tags to temporarily store all tags associated with the current system. 208 : std::set<TagID> _vector_tags; 209 : 210 : /// Matrix tags to temporarily store all tags associated with the current system. 211 : std::set<TagID> _matrix_tags; 212 : 213 : /// Tag for time contribution rhs 214 : TagID _rhs_time_tag; 215 : 216 : /// right hand side vector for time contributions 217 : NumericVector<Number> * _rhs_time; 218 : 219 : /// Tag for non-time contribution rhs 220 : TagID _rhs_non_time_tag; 221 : 222 : /// right hand side vector for non-time contributions 223 : NumericVector<Number> * _rhs_non_time; 224 : 225 : /// Used for the right hand side vector from PETSc 226 : TagID _rhs_tag; 227 : 228 : /// Tag for non-time contribution to the system matrix 229 : TagID _system_matrix_non_time_tag; 230 : 231 : /// Tag for every contribution to system matrix 232 : TagID _system_matrix_tag; 233 : 234 : /// Number of linear iterations 235 : unsigned int _n_linear_iters; 236 : 237 : /// The initial linear residual 238 : Real _initial_linear_residual; 239 : 240 : /// The final linear residual 241 : Real _final_linear_residual; 242 : 243 : /// If the solve on the linear system converged 244 : bool _converged; 245 : 246 : /// Base class reference to the linear implicit system in libmesh 247 : libMesh::LinearImplicitSystem & _linear_implicit_system; 248 : 249 : private: 250 : /// The current states of the solution (0 = current, 1 = old, etc) 251 : std::vector<NumericVector<Number> *> _solution_state; 252 : };