https://mooseframework.inl.gov
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
11 #include "ExplicitTimeIntegrator.h"
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 
25 using namespace libMesh;
26 
29 {
31 
32  MooseEnum solve_type("consistent lumped lump_preconditioned", "consistent");
33 
34  params.addParam<MooseEnum>(
35  "solve_type",
36  solve_type,
37  "The way to solve the system. A 'consistent' solve uses the full mass matrix and actually "
38  "needs to use a linear solver to solve the problem. 'lumped' uses a lumped mass matrix with "
39  "a simple inversion - incredibly fast but may be less accurate. 'lump_preconditioned' uses "
40  "the lumped mass matrix as a preconditioner for the 'consistent' solve");
41 
42  return params;
43 }
44 
46  : TimeIntegrator(parameters),
47  MeshChangedInterface(parameters),
48 
49  _solve_type(getParam<MooseEnum>("solve_type")),
50  _explicit_residual(addVector("explicit_residual", false, PARALLEL)),
51  _solution_update(addVector("solution_update", true, PARALLEL)),
52  _mass_matrix_diag_inverted(addVector("mass_matrix_diag_inverted", true, GHOSTED))
53 {
55 
56  // This effectively changes the default solve_type to LINEAR instead of PJFNK,
57  // so that it is valid to not supply solve_type in the Executioner block:
58  if (_nl)
60 
62  _ones = addVector("ones", true, PARALLEL);
63  // don't set any of the common SNES-related petsc options to prevent unused option warnings
65 }
66 
67 void
69 {
70  meshChanged();
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
117  if (_nl->getKernelWarehouse()
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  }
130  }
131  else if (_nl->getScalarKernelWarehouse()
132  .getMatrixTagObjectWarehouse(mass_matrix_tag_id, 0)
133  .hasVariableObjects(var_id))
134  continue;
135 
136  mooseError("No objects contributing to the mass matrix were found for variable '",
137  var_name,
138  "'. Did you, e.g., forget a time derivative term?");
139  }
140  }
141 }
142 
143 void
145 {
147  mooseError(
148  "The chosen time integrator requires 'solve_type = LINEAR' in the Executioner block.");
149 }
150 
151 void
153 {
154 }
155 
156 void
158 {
159  // Can only be done after the system is initialized
161  *_ones = 1.;
162 
165 
167  {
168  _preconditioner = std::make_unique<LumpedPreconditioner>(*_mass_matrix_diag_inverted);
169  _linear_solver->attach_preconditioner(_preconditioner.get());
170  _linear_solver->init();
171  }
172 
175 }
176 
177 bool
179 {
180  bool converged = false;
181 
182  switch (_solve_type)
183  {
184  case CONSISTENT:
185  {
186  converged = solveLinearSystem(mass_matrix);
187 
188  break;
189  }
190  case LUMPED:
191  {
192  // Computes the sum of each row (lumping)
193  // Note: This is actually how PETSc does it
194  // It's not "perfectly optimal" - but it will be fast (and universal)
196 
197  // "Invert" the diagonal mass matrix
198  _mass_matrix_diag_inverted->reciprocal();
199 
200  // Multiply the inversion by the RHS
202 
203  // Check for convergence by seeing if there is a nan or inf
204  auto sum = _solution_update->sum();
205  converged = std::isfinite(sum);
206 
207  // The linear iteration count remains zero
209 
210  break;
211  }
212  case LUMP_PRECONDITIONED:
213  {
215  _mass_matrix_diag_inverted->reciprocal();
216 
217  converged = solveLinearSystem(mass_matrix);
218 
219  break;
220  }
221  default:
222  mooseError("Unknown solve_type in ExplicitTimeIntegrator.");
223  }
224 
225  return converged;
226 }
227 
228 bool
230 {
231  auto & es = _fe_problem.es();
232 
233  const auto num_its_and_final_tol =
234  _linear_solver->solve(mass_matrix,
237  es.parameters.get<Real>("linear solver tolerance"),
238  es.parameters.get<unsigned int>("linear solver maximum iterations"));
239 
240  _n_linear_iterations += num_its_and_final_tol.first;
241 
242  const bool converged = checkLinearConvergence();
243 
244  return converged;
245 }
246 
247 bool
249 {
250  auto reason = _linear_solver->get_converged_reason();
251 
252  switch (reason)
253  {
256  case CONVERGED_RTOL:
257  case CONVERGED_ATOL:
258  case CONVERGED_ITS:
263  return true;
264  case DIVERGED_NULL:
265  case DIVERGED_ITS:
266  case DIVERGED_DTOL:
267  case DIVERGED_BREAKDOWN:
271  case DIVERGED_NAN:
273  case CONVERGED_ITERATING:
275  return false;
276  default:
277  mooseError("Unknown convergence reason in ExplicitTimeIntegrator.");
278  }
279 }
bool checkLinearConvergence()
Check for the linear solver convergence.
NumericVector< Real > * _solution_update
Solution vector for the linear solve.
void setLinearSolverDefaults(FEProblemBase &problem, libMesh::LinearSolver< T > &linear_solver)
Set the defaults for a libMesh LinearSolver.
Definition: PetscSupport.h:93
DIVERGED_DTOL
NumericVector< Real > * _ones
Vector of 1&#39;s to help with creating the lumped mass matrix.
virtual void initialSetup() override
Called to setup datastructures.
void vector_mult(NumericVector< T > &dest, const NumericVector< T > &arg) const
std::unordered_set< unsigned int > & _vars
The variables that this time integrator integrates.
FEProblemBase & _fe_problem
Reference to the problem.
CONVERGED_ITERATING
MooseObjectTagWarehouse< KernelBase > & getKernelWarehouse()
Access functions to Warehouses from outside NonlinearSystemBase.
NonlinearSystemBase * _nl
Pointer to the nonlinear system, can happen that we dont have any.
TagID _Ke_time_tag
For computing the mass matrix.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
const Parallel::Communicator & comm() const
Solving a linear problem.
Definition: MooseTypes.h:848
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
DIVERGED_INDEFINITE_MAT
MooseEnum _solve_type
Solve type for how mass matrix is handled.
const MooseObjectTagWarehouse< ScalarKernelBase > & getScalarKernelWarehouse() const
DIVERGED_BREAKDOWN
static InputParameters validParams()
virtual unsigned int nVariables() const
Get the number of variables in this system.
Definition: SystemBase.C:874
DIVERGED_ITS
CONVERGED_ATOL_NORMAL
CONVERGED_CG_CONSTRAINED
const std::string _type
The type of this class.
Definition: MooseBase.h:87
CONVERGED_HAPPY_BREAKDOWN
Interface for notifications that the mesh has changed.
std::unique_ptr< libMesh::LinearSolver< Number > > _linear_solver
For solving with the consistent matrix.
TheWarehouse & theWarehouse() const
DIVERGED_NONSYMMETRIC
virtual TagID getMatrixTagID(const TagName &tag_name) const
Get a TagID from a TagName.
Definition: SubProblem.C:342
NumericVector< Real > * _explicit_residual
Residual used for the RHS.
virtual void preSolve() override
virtual libMesh::EquationSystems & es() override
unsigned int _n_linear_iterations
Total number of linear iterations over all stages of the time step.
MooseObjectWarehouse< T > & getMatrixTagObjectWarehouse(TagID tag_id, THREAD_ID tid)
Retrieve a moose object warehouse in which every moose object has the given matrix tag...
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:33
const std::string & variable_name(const unsigned int i) const
virtual bool performExplicitSolve(SparseMatrix< Number > &mass_matrix)
Solves a linear system using the chosen solve type.
unsigned int number() const
Gets the number of this system.
Definition: SystemBase.C:1159
std::unique_ptr< LumpedPreconditioner > _preconditioner
For solving with lumped preconditioning.
virtual bool hasVariable(const std::string &var_name) const
Query a system for a variable.
Definition: SystemBase.C:834
CONVERGED_CG_NEG_CURVE
void dontAddCommonSNESOptions(FEProblemBase &fe_problem)
Function to ensure that common SNES options are not added to the PetscOptions storage object to be la...
NumericVector< Real > * _mass_matrix_diag_inverted
Diagonal of the lumped mass matrix (and its inversion)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Base class for time integrators.
DIVERGED_NAN
Query query()
query creates and returns an initialized a query object for querying objects from the warehouse...
Definition: TheWarehouse.h:466
virtual void init() override
Called only before the very first timestep (t_step = 0) Never called again (not even during recover/r...
CONVERGED_RTOL_NORMAL
CONVERGED_STEP_LENGTH
IntRange< T > make_range(T beg, T end)
virtual TagID massMatrixTagID() const
void mooseError(Args &&... args) const
Emits an error prefixed with object name and type.
SolverParams & solverParams(unsigned int solver_sys_num=0)
Get the solver parameters.
DIVERGED_INDEFINITE_PC
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...
bool hasVariableObjects(unsigned int variable_id, THREAD_ID tid=0) const
Checks for whether this warehouse has objects for a given variable.
virtual void meshChanged() override
Called on this object when the mesh changes.
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
const MooseObjectTagWarehouse< NodalKernelBase > & getNodalKernelWarehouse() const
MooseObjectTagWarehouse< DGKernelBase > & getDGKernelWarehouse()
NumericVector< Number > * addVector(const std::string &name, const bool project, const libMesh::ParallelType type)
Wrapper around vector addition for nonlinear time integrators.
virtual bool hasScalarVariable(const std::string &var_name) const
Definition: SystemBase.C:859
bool solveLinearSystem(SparseMatrix< Number > &mass_matrix)
Solves a linear system.
DIVERGED_BREAKDOWN_BICG
static InputParameters validParams()
ExplicitTimeIntegrator(const InputParameters &parameters)
virtual libMesh::System & system() override
Get the reference to the libMesh system.