https://mooseframework.inl.gov
LinearSystem.h
Go to the documentation of this file.
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"
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;
29 class LinearFVFluxKernel;
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 
48 class LinearSystem : public SolverSystem,
49  public PerfGraphInterface,
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 
64  virtual bool converged() override { return _converged; }
65 
66  virtual void initialSetup() override;
67  virtual void reinit() override;
68 
69 #ifdef MOOSE_KOKKOS_ENABLED
70 
76  void addKokkosKernel(const std::string & kernel_name,
77  const std::string & name,
78  InputParameters & parameters);
79 
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  virtual void residualSetup() override {}
94  virtual void jacobianSetup() override {}
95 
99  virtual void stopSolve(const ExecFlagType & exec_flag,
100  const std::set<TagID> & vector_tags_to_close) override;
101 
107  virtual bool containsTimeKernel() override;
108  virtual std::vector<std::string> timeKernelVariableNames() override { return {}; }
109 
118  void computeLinearSystemTags(const std::set<TagID> & vector_tags,
119  const std::set<TagID> & matrix_tags,
120  const bool compute_gradients = true);
121 
126 
131 
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 
144  unsigned int nLinearIterations() const { return _n_linear_iters; }
145 
146  virtual System & system() override { return _sys; }
147  virtual const System & system() const override { return _sys; }
148 
154  virtual TagID systemMatrixTag() const override { return _system_matrix_tag; }
156 
160  {
162  }
163 
167 
171 
172  virtual void compute(ExecFlagType type) override;
173 
174 protected:
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 
191 
197  void computeKokkosLinearSystem(const std::set<TagID> & vector_tags,
198  const std::set<TagID> & matrix_tags);
199 #endif
200 
202  System & _sys;
203 
205  unsigned int _current_l_its;
206 
208  std::set<TagID> _vector_tags;
209 
211  std::set<TagID> _matrix_tags;
212 
215 
218 
221 
224 
227 
230 
233 
235  unsigned int _n_linear_iters;
236 
239 
242 
245 
248 
249 private:
251  std::vector<NumericVector<Number> *> _solution_state;
252 };
virtual void stopSolve(const ExecFlagType &exec_flag, const std::set< TagID > &vector_tags_to_close) override
Quit the current solve as soon as possible.
Definition: LinearSystem.C:335
virtual void preInit() override
This is called prior to the libMesh system has been init&#39;d.
Definition: LinearSystem.C:107
Base class for boundary conditions for linear FV systems.
Base class for finite volume kernels that contribute to a linear systems.
unsigned int _n_linear_iters
Number of linear iterations.
Definition: LinearSystem.h:235
unsigned int TagID
Definition: MooseTypes.h:238
TagID _system_matrix_non_time_tag
Tag for non-time contribution to the system matrix.
Definition: LinearSystem.h:229
TagID rightHandSideVectorTag() const
Definition: LinearSystem.h:153
virtual bool containsTimeKernel() override
If the system has a kernel that corresponds to a time derivative.
Definition: LinearSystem.C:344
libMesh::LinearImplicitSystem & linearImplicitSystem()
Return a reference to the stored linear implicit system.
Definition: LinearSystem.h:125
NumericVector< Number > & getRightHandSideNonTimeVector()
Return a numeric vector that is associated with the nontime tag.
Definition: LinearSystem.C:294
Finite volume kernel that contributes approximations of discretized face flux terms to the matrix and...
virtual TagID systemMatrixTag() const override
Return the Matrix Tag ID for System.
Definition: LinearSystem.h:154
const NumericVector< Number > & getRightHandSideVector() const
Definition: LinearSystem.h:159
void computeLinearSystemInternal(const std::set< TagID > &vector_tags, const std::set< TagID > &matrix_tags, const bool compute_gradients=true)
Compute the right hand side and system matrix for given tags.
Definition: LinearSystem.C:211
virtual void compute(ExecFlagType type) override
Compute time derivatives, auxiliary variables, etc.
Definition: LinearSystem.C:366
TagID rightHandSideNonTimeVectorTag() const
Definition: LinearSystem.h:152
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
NumericVector< Number > * rhs
std::set< TagID > _vector_tags
Vector tags to temporarily store all tags associated with the current system.
Definition: LinearSystem.h:208
TagID rightHandSideTimeVectorTag() const
Definition: LinearSystem.h:151
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
unsigned int nLinearIterations() const
Return the number of linear iterations.
Definition: LinearSystem.h:144
unsigned int _current_l_its
The linear iterations needed for convergence.
Definition: LinearSystem.h:205
void initialSetupKokkosLinearFV()
Perform the initial setup of the Kokkos linear finite volume kernels and boundary conditions...
SparseMatrix< Number > & getSystemMatrix()
Fetching the system matrix from the libmesh system.
Definition: LinearSystem.h:165
Real _initial_linear_residual
The initial linear residual.
Definition: LinearSystem.h:238
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
const SparseMatrix< Number > & getSystemMatrix() const
Definition: LinearSystem.h:166
Finite volume kernel that contributes approximations of volumetric integral terms to the matrix and r...
System & _sys
Base class reference to the libmesh system.
Definition: LinearSystem.h:202
const std::vector< std::unique_ptr< libMesh::NumericVector< libMesh::Number > > > & linearFVLimitedGradientContainer(const Moose::FV::GradientLimiterType limiter_type) const
Access the stored raw or limited cell-centered gradient components.
void requestLinearFVLimitedGradients(const Moose::FV::GradientLimiterType limiter_type, unsigned int variable_number)
Request storage and assembly of limiter-specific cell gradients.
virtual void initialSetup() override
Setup Functions.
Definition: LinearSystem.C:118
virtual const std::string & name() const
Definition: SystemBase.C:1342
libMesh::LinearImplicitSystem & _linear_implicit_system
Base class reference to the linear implicit system in libmesh.
Definition: LinearSystem.h:247
NumericVector< Number > * _rhs_time
right hand side vector for time contributions
Definition: LinearSystem.h:217
Real _final_linear_residual
The final linear residual.
Definition: LinearSystem.h:241
std::set< TagID > _matrix_tags
Matrix tags to temporarily store all tags associated with the current system.
Definition: LinearSystem.h:211
std::vector< NumericVector< Number > * > _solution_state
The current states of the solution (0 = current, 1 = old, etc)
Definition: LinearSystem.h:251
TagID _rhs_tag
Used for the right hand side vector from PETSc.
Definition: LinearSystem.h:226
virtual void jacobianSetup() override
Definition: LinearSystem.h:94
TagID _rhs_non_time_tag
Tag for non-time contribution rhs.
Definition: LinearSystem.h:220
virtual void solve() override
Solve the system (using libMesh magic)
Definition: LinearSystem.C:308
virtual const System & system() const override
Definition: LinearSystem.h:147
Interface for objects interacting with the PerfGraph.
TagID _rhs_time_tag
Tag for time contribution rhs.
Definition: LinearSystem.h:214
void addKokkosBoundaryCondition(const std::string &bc_name, const std::string &name, InputParameters &parameters)
Add a Kokkos linear finite volume boundary condition to this system.
TagID _system_matrix_tag
Tag for every contribution to system matrix.
Definition: LinearSystem.h:232
bool _converged
If the solve on the linear system converged.
Definition: LinearSystem.h:244
NumericVector< Number > * _rhs_non_time
right hand side vector for non-time contributions
Definition: LinearSystem.h:223
NumericVector< Number > & getRightHandSideVector()
Fetching the right hand side vector from the libmesh system.
Definition: LinearSystem.h:158
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual void reinit() override
Reinitialize the system when the degrees of freedom in this system have changed.
Definition: LinearSystem.C:178
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:18
NumericVector< Number > & getRightHandSideTimeVector()
Return a numeric vector that is associated with the time tag.
Definition: LinearSystem.C:288
SparseMatrix< Number > * matrix
virtual void residualSetup() override
Definition: LinearSystem.h:93
virtual std::vector< std::string > timeKernelVariableNames() override
Returns the names of the variables that have time derivative kernels in the system.
Definition: LinearSystem.h:108
void addKokkosKernel(const std::string &kernel_name, const std::string &name, InputParameters &parameters)
Add a Kokkos linear finite volume kernel to this system.
Linear system to be solved.
Definition: LinearSystem.h:48
virtual void augmentSparsity(SparsityPattern::Graph &sparsity, std::vector< dof_id_type > &n_nz, std::vector< dof_id_type > &n_oz) override
Will modify the sparsity pattern to add logical geometric connections.
Definition: LinearSystem.C:300
void computeKokkosLinearSystem(const std::set< TagID > &vector_tags, const std::set< TagID > &matrix_tags)
Assemble the Kokkos contributions to the linear system for the given tags.
virtual ~LinearSystem()
LinearSystem(FEProblemBase &problem, const std::string &name)
Definition: LinearSystem.C:78
virtual System & system() override
Get the reference to the libMesh system.
Definition: LinearSystem.h:146
void computeLinearSystemTags(const std::set< TagID > &vector_tags, const std::set< TagID > &matrix_tags, const bool compute_gradients=true)
Compute the right hand side and the system matrix of the system for given tags.
Definition: LinearSystem.C:185
virtual bool converged() override
At the moment, this is only used for the multi-system fixed point iteration.
Definition: LinearSystem.h:64
void computeGradients()
Compute and store raw and requested limited Green-Gauss gradients for linear FV variables.
Shared storage and allocation logic for linear finite-volume cell gradients for variables in the syst...