https://mooseframework.inl.gov
Loading...
Searching...
No Matches
ExplicitTimeIntegrator.C
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// MOOSE includes
12#include "NonlinearSystem.h"
13#include "FEProblem.h"
14#include "PetscSupport.h"
15#include "KernelBase.h"
16#include "DGKernelBase.h"
17#include "ScalarKernelBase.h"
18#include "FVElementalKernel.h"
19#include "FVFluxKernel.h"
20#include "NodalKernelBase.h"
21
22// libMesh includes
23#include "libmesh/enum_convergence_flags.h"
24
27{
29
30 MooseEnum solve_type("consistent lumped lump_preconditioned", "consistent");
31
32 params.addParam<MooseEnum>(
33 "solve_type",
34 solve_type,
35 "The way to solve the system. A 'consistent' solve uses the full mass matrix and actually "
36 "needs to use a linear solver to solve the problem. 'lumped' uses a lumped mass matrix with "
37 "a simple inversion - incredibly fast but may be less accurate. 'lump_preconditioned' uses "
38 "the lumped mass matrix as a preconditioner for the 'consistent' solve");
39
40 return params;
41}
42
44 : TimeIntegrator(parameters),
45 MeshChangedInterface(parameters),
46 _solve_type(getParam<MooseEnum>("solve_type")),
47 _explicit_residual(addVector("explicit_residual", false, PARALLEL)),
48 _solution_update(addVector("solution_update", true, PARALLEL)),
49 _mass_matrix_diag_inverted(addVector("mass_matrix_diag_inverted", true, GHOSTED)),
50 _ones(nullptr)
51{
53
54 // This effectively changes the default solve_type to LINEAR instead of PJFNK,
55 // so that it is valid to not supply solve_type in the Executioner block:
56 if (_nl)
58
60 _ones = addVector("ones", true, PARALLEL);
61 // don't set any of the common SNES and KSP-related petsc options to prevent unused option
62 // warnings
65}
66
67void
69{
71
72 if (_nl)
73 {
74 std::unordered_set<unsigned int> vars_to_check;
75 if (!_vars.empty())
76 vars_to_check = _vars;
77 else
78 for (const auto i : make_range(_nl->nVariables()))
79 vars_to_check.insert(i);
80
81 const auto mass_matrix_tag_id = massMatrixTagID();
82 std::set<TagID> matrix_tags = {mass_matrix_tag_id};
83 auto fv_object_starting_query = _fe_problem.theWarehouse()
84 .query()
85 .template condition<AttribMatrixTags>(matrix_tags)
86 .template condition<AttribSysNum>(_nl->number());
87
88 for (const auto var_id : vars_to_check)
89 {
90 const auto & var_name = _nl->system().variable_name(var_id);
91 const bool field_var = _nl->hasVariable(var_name);
92 if (!field_var)
93 mooseAssert(_nl->hasScalarVariable(var_name),
94 var_name << " should be either a field or scalar variable");
95 if (field_var)
96 {
97 const auto & field_var = _nl->getVariable(/*tid=*/0, var_name);
98 if (field_var.isFV())
99 {
100 std::vector<FVElementalKernel *> fv_elemental_kernels;
101 auto var_query = fv_object_starting_query.clone().template condition<AttribVar>(var_id);
102 auto var_query_clone = var_query.clone();
103 var_query.template condition<AttribSystem>("FVElementalKernel")
104 .queryInto(fv_elemental_kernels);
105 if (fv_elemental_kernels.size())
106 continue;
107
108 std::vector<FVFluxKernel *> fv_flux_kernels;
109 var_query_clone.template condition<AttribSystem>("FVFluxKernel")
110 .queryInto(fv_flux_kernels);
111 if (fv_flux_kernels.size())
112 continue;
113 }
114 else
115 {
116 // We are a finite element variable
118 .getMatrixTagObjectWarehouse(mass_matrix_tag_id, 0)
119 .hasVariableObjects(var_id))
120 continue;
122 .getMatrixTagObjectWarehouse(mass_matrix_tag_id, 0)
123 .hasVariableObjects(var_id))
124 continue;
126 .getMatrixTagObjectWarehouse(mass_matrix_tag_id, 0)
127 .hasVariableObjects(var_id))
128 continue;
129#ifdef MOOSE_KOKKOS_ENABLED
131 .getMatrixTagObjectWarehouse(mass_matrix_tag_id, 0)
132 .hasVariableObjects(var_id))
133 continue;
135 .getMatrixTagObjectWarehouse(mass_matrix_tag_id, 0)
136 .hasVariableObjects(var_id))
137 continue;
138#endif
139 }
140 }
141 else if (_nl->getScalarKernelWarehouse()
142 .getMatrixTagObjectWarehouse(mass_matrix_tag_id, 0)
143 .hasVariableObjects(var_id))
144 continue;
145
146 mooseError("No objects contributing to the mass matrix were found for variable '",
147 var_name,
148 "'. Did you, e.g., forget a time derivative term?");
149 }
150 }
151}
152
153void
155{
158 "The chosen time integrator requires 'solve_type = LINEAR' in the Executioner block.");
159}
160
161void
165
166void
171
172void
174{
175 // Can only be done after the system is initialized
176 if (_ones)
177 {
179 *_ones = 1.;
180 }
181
184
186 {
187 _preconditioner = std::make_unique<LumpedPreconditioner>(*_mass_matrix_diag_inverted);
188 _linear_solver->attach_preconditioner(_preconditioner.get());
189 _linear_solver->init();
190 }
191
194}
195
196bool
197ExplicitTimeIntegrator::performExplicitSolve(SparseMatrix<Number> & mass_matrix)
198{
199 bool converged = false;
200
201 switch (_solve_type)
202 {
203 case CONSISTENT:
204 {
205 converged = solveLinearSystem(mass_matrix);
206
207 break;
208 }
209 case LUMPED:
210 {
211 // Computes the sum of each row (lumping)
212 // Note: This is actually how PETSc does it
213 // It's not "perfectly optimal" - but it will be fast (and universal)
214 mass_matrix.vector_mult(*_mass_matrix_diag_inverted, *_ones);
215
216 // "Invert" the diagonal mass matrix
217 _mass_matrix_diag_inverted->reciprocal();
218
219 // Multiply the inversion by the RHS
221
222 // Check for convergence by seeing if there is a nan or inf
223 auto sum = _solution_update->sum();
224 converged = std::isfinite(sum);
225
226 // The linear iteration count remains zero
228
229 break;
230 }
232 {
233 mass_matrix.vector_mult(*_mass_matrix_diag_inverted, *_ones);
234 _mass_matrix_diag_inverted->reciprocal();
235
236 converged = solveLinearSystem(mass_matrix);
237
238 break;
239 }
240 default:
241 mooseError("Unknown solve_type in ExplicitTimeIntegrator.");
242 }
243
244 return converged;
245}
246
247bool
248ExplicitTimeIntegrator::solveLinearSystem(SparseMatrix<Number> & mass_matrix)
249{
250 auto & es = _fe_problem.es();
251
252 const auto num_its_and_final_tol =
253 _linear_solver->solve(mass_matrix,
256 es.parameters.get<Real>("linear solver tolerance"),
257 es.parameters.get<unsigned int>("linear solver maximum iterations"));
258
259 _n_linear_iterations += num_its_and_final_tol.first;
260
261 const bool converged = checkLinearConvergence();
262
263 return converged;
264}
265
266bool
268{
269 auto reason = _linear_solver->get_converged_reason();
270
271 switch (reason)
272 {
282 return true;
294 return false;
295 default:
296 mooseError("Unknown convergence reason in ExplicitTimeIntegrator.");
297 }
298}
virtual TagID massMatrixTagID() const
virtual void meshChanged() override
Called on this object when the mesh changes.
virtual void init() override
Called only before the very first timestep (t_step = 0) Never called again (not even during recover/r...
virtual void initialSetup() override
Called to setup datastructures.
virtual bool performExplicitSolve(SparseMatrix< Number > &mass_matrix)
Solves a linear system using the chosen solve type.
bool checkLinearConvergence()
Check for the linear solver convergence.
MooseEnum _solve_type
Solve type for how mass matrix is handled.
NumericVector< Real > * _mass_matrix_diag_inverted
Diagonal of the lumped mass matrix (and its inversion)
ExplicitTimeIntegrator(const InputParameters &parameters)
virtual void preSolve() override
NumericVector< Real > * _ones
Vector of 1's to help with creating the lumped mass matrix.
static InputParameters validParams()
std::unique_ptr< LumpedPreconditioner > _preconditioner
For solving with lumped preconditioning.
TagID _Ke_time_tag
For computing the mass matrix.
NumericVector< Real > * _solution_update
Solution vector for the linear solve.
std::unique_ptr< libMesh::LinearSolver< Number > > _linear_solver
For solving with the consistent matrix.
NumericVector< Real > * _explicit_residual
Residual used for the RHS.
bool solveLinearSystem(SparseMatrix< Number > &mass_matrix)
Solves a linear system.
virtual libMesh::EquationSystems & es() override
SolverParams & solverParams(unsigned int solver_sys_num=0)
Get the solver parameters.
TheWarehouse & theWarehouse() const
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addParam(const std::string &name, const S &value, const std::string &doc_string)
These methods add an optional parameter and a documentation string to the InputParameters object.
Interface for notifications that the mesh has changed.
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type and optionally a file path to the top-level block p...
Definition MooseBase.h:271
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
MooseObjectWarehouse< T > & getMatrixTagObjectWarehouse(TagID tag_id, THREAD_ID tid)
Retrieve a moose object warehouse in which every moose object has the given matrix tag.
bool hasVariableObjects(unsigned int variable_id, THREAD_ID tid=0) const
Checks for whether this warehouse has objects for a given variable.
MooseObjectTagWarehouse< ResidualObject > & getKokkosKernelWarehouse()
MooseObjectTagWarehouse< ResidualObject > & getKokkosNodalKernelWarehouse()
const MooseObjectTagWarehouse< NodalKernelBase > & getNodalKernelWarehouse() const
MooseObjectTagWarehouse< KernelBase > & getKernelWarehouse()
Access functions to Warehouses from outside NonlinearSystemBase.
virtual libMesh::System & system() override
Get the reference to the libMesh system.
MooseObjectTagWarehouse< DGKernelBase > & getDGKernelWarehouse()
const MooseObjectTagWarehouse< ScalarKernelBase > & getScalarKernelWarehouse() const
NonlinearSystemBase * _nl
Pointer to the nonlinear system, can happen that we dont have any.
NumericVector< Number > * addVector(const std::string &name, const bool project, const libMesh::ParallelType type)
Wrapper around vector addition for nonlinear time integrators.
Moose::SolveType _type
virtual TagID getMatrixTagID(const TagName &tag_name) const
Get a TagID from a TagName.
Definition SubProblem.C:341
virtual unsigned int nVariables() const
Get the number of variables in this system.
Definition SystemBase.C:890
MooseVariableFieldBase & getVariable(THREAD_ID tid, const std::string &var_name) const
Gets a reference to a variable of with specified name.
Definition SystemBase.C:89
unsigned int number() const
Gets the number of this system.
virtual bool hasScalarVariable(const std::string &var_name) const
Definition SystemBase.C:875
virtual bool hasVariable(const std::string &var_name) const
Query a system for a variable.
Definition SystemBase.C:850
Query query()
query creates and returns an initialized a query object for querying objects from the warehouse.
Base class for time integrators.
std::unordered_set< unsigned int > & _vars
The variables that this time integrator integrates.
unsigned int _n_linear_iterations
Total number of linear iterations over all stages of the time step.
static InputParameters validParams()
FEProblemBase & _fe_problem
Reference to the problem.
static std::unique_ptr< LinearSolver< T > > build(const libMesh::Parallel::Communicator &comm_in, const SolverPackage solver_package=libMesh::default_solver_package())
const Parallel::Communicator & comm() const
const std::string & variable_name(const unsigned int i) const
void dontAddCommonSNESOptions(FEProblemBase &fe_problem)
Function to ensure that common SNES options are not added to the PetscOptions storage object to be la...
void setLinearSolverDefaults(FEProblemBase &problem, libMesh::LinearSolver< T > &linear_solver)
Set the defaults for a libMesh LinearSolver.
void dontAddCommonKSPOptions(FEProblemBase &fe_problem)
Function to ensure that common KSP options are not added to the PetscOptions storage object to be lat...
@ ST_LINEAR
Solving a linear problem.
Definition MooseTypes.h:902
CONVERGED_HAPPY_BREAKDOWN
CONVERGED_CG_NEG_CURVE
CONVERGED_ATOL_NORMAL
DIVERGED_NONSYMMETRIC
CONVERGED_ITERATING
CONVERGED_CG_CONSTRAINED
DIVERGED_INDEFINITE_PC
CONVERGED_STEP_LENGTH
DIVERGED_PCSETUP_FAILED
CONVERGED_RTOL_NORMAL
DIVERGED_BREAKDOWN
DIVERGED_BREAKDOWN_BICG
DIVERGED_INDEFINITE_MAT