https://mooseframework.inl.gov
Loading...
Searching...
No Matches
TimeIntegrator.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#include "TimeIntegrator.h"
11#include "FEProblem.h"
12#include "NonlinearSystemBase.h"
13
14#include "libmesh/nonlinear_implicit_system.h"
15#include "libmesh/nonlinear_solver.h"
16#include "libmesh/dof_map.h"
17#include "libmesh/sparse_matrix.h"
18#include "libmesh/petsc_matrix.h"
19#include "libmesh/petsc_solver_exception.h"
20
23{
25 params.addParam<std::vector<VariableName>>(
26 "variables", {}, "A subset of the variables that this time integrator should be applied to");
27 params.registerBase("TimeIntegrator");
28 return params;
29}
30
32 : MooseObject(parameters),
33 Restartable(this, "TimeIntegrators"),
34 NonlinearTimeIntegratorInterface(*getCheckedPointerParam<FEProblemBase *>("_fe_problem_base"),
35 *getCheckedPointerParam<SystemBase *>("_sys")),
36 LinearTimeIntegratorInterface(*getCheckedPointerParam<SystemBase *>("_sys")),
37 _fe_problem(*getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")),
38 _sys(*getCheckedPointerParam<SystemBase *>("_sys")),
39 _du_dot_du(_sys.duDotDus()),
40 _solution(_sys.currentSolution()),
41 _solution_old(_sys.solutionState(1)),
42 _solution_sub(declareRestartableDataWithContext<std::unique_ptr<NumericVector<Number>>>(
43 "solution_sub", &const_cast<libMesh::Parallel::Communicator &>(this->comm()))),
44 _solution_old_sub(declareRestartableDataWithContext<std::unique_ptr<NumericVector<Number>>>(
45 "solution_old_sub", &const_cast<libMesh::Parallel::Communicator &>(this->comm()))),
46 _t_step(_fe_problem.timeStep()),
47 _dt(_fe_problem.dt()),
48 _dt_old(_fe_problem.dtOld()),
49 _n_nonlinear_iterations(0),
50 _n_linear_iterations(0),
51 _is_lumped(false),
52 _var_restriction(declareRestartableData<bool>(
53 "var_restriction", !getParam<std::vector<VariableName>>("variables").empty())),
54 _local_indices(declareRestartableData<std::vector<dof_id_type>>("local_indices")),
55 _vars(declareRestartableData<std::unordered_set<unsigned int>>("vars")),
56 _from_subvector(
57 NumericVector<Number>::build(this->comm(), libMesh::default_solver_package(), PARALLEL))
58{
60}
61
62void
64{
66 return;
67
68 const auto & var_names = getParam<std::vector<VariableName>>("variables");
69 std::vector<unsigned int> var_num_vec;
70 auto & lm_sys = _sys.system();
71 lm_sys.get_all_variable_numbers(var_num_vec);
72 std::unordered_set<unsigned int> var_nums(var_num_vec.begin(), var_num_vec.end());
73 for (const auto & var_name : var_names)
74 if (lm_sys.has_variable(var_name))
75 {
76 const auto var_num = lm_sys.variable_number(var_name);
77 _vars.insert(var_num);
78 var_nums.erase(var_num);
79 }
80
81 // If var_nums is empty then that means the user has specified all the variables in this system
82 if (var_nums.empty())
83 {
84 _var_restriction = false;
85 return;
86 }
87
88 // the stage weights of weightNonTimeJacobian() apply to all rows of the Jacobian, which belong
89 // to this integrator only if it integrates all variables of the system
90 if (_Ke_time)
91 paramError("variables",
92 "The ",
93 type(),
94 " time integrator weights the time and non-time residuals of its stages "
95 "differently, so it must integrate all variables of the system");
96
97 std::vector<dof_id_type> var_dof_indices, work_vec;
98 for (const auto var_num : _vars)
99 {
100 work_vec = _local_indices;
101 _local_indices.clear();
102 lm_sys.get_dof_map().local_variable_indices(var_dof_indices, lm_sys.get_mesh(), var_num);
103 std::merge(work_vec.begin(),
104 work_vec.end(),
105 var_dof_indices.begin(),
106 var_dof_indices.end(),
107 std::back_inserter(_local_indices));
108 }
109
111 NumericVector<Number>::build(_solution->comm(), libMesh::default_solver_package(), PARALLEL);
112 _solution_old_sub = NumericVector<Number>::build(
114}
115
116void
118{
119 mooseError("Calling TimeIntegrator::solve() is no longer supported");
120}
121
122void
128
129unsigned int
134
135unsigned int
137{
138 auto & nonlinear_solver = _nonlinear_implicit_system->nonlinear_solver;
139 libmesh_assert(nonlinear_solver);
140
141 return nonlinear_solver->get_total_linear_iterations();
142}
143
144void
145TimeIntegrator::copyVector(const NumericVector<Number> & from, NumericVector<Number> & to)
146{
147 if (!_var_restriction)
148 to = from;
149 else
150 {
151 auto to_sub = to.get_subvector(_local_indices);
152 from.create_subvector(*_from_subvector, _local_indices, false);
153 *to_sub = *_from_subvector;
154 to.restore_subvector(std::move(to_sub), _local_indices);
155 }
156}
157
158bool
159TimeIntegrator::integratesVar(const unsigned int var_num) const
160{
161 if (!_var_restriction)
162 return true;
163
164 return _vars.count(var_num);
165}
166
167void
169{
170 if (!_nl)
171 return;
173}
174
175void
176TimeIntegrator::weightNonTimeJacobian(SparseMatrix<Number> & jacobian, const Real weight) const
177{
178 mooseAssert(_Ke_time, "requestTimeJacobian() must be called first");
179 if (weight == 1)
180 return;
181 // J_time + w J_non_time = w J + (1 - w) J_time, where J = J_time + J_non_time
182 // In the separate Jacobian assembly the cached element contributions are not assembled into
183 // these matrices yet; only the constraint assembly and the combined residual and Jacobian
184 // assembly close them earlier.
185 jacobian.close();
186 _Ke_time->close();
187 jacobian.scale(weight);
188 // Both matrices have the sparsity pattern of the DofMap, and the time Jacobian may have fewer
189 // entries. Adding with SparseMatrix::add() would rebuild the system matrix with a different
190 // pattern and lose the preallocation that the nodal boundary conditions rely on.
191 auto * petsc_jacobian = dynamic_cast<PetscMatrix<Number> *>(&jacobian);
192 auto * petsc_time = dynamic_cast<PetscMatrix<Number> *>(_Ke_time);
193 if (petsc_jacobian && petsc_time)
194 LibmeshPetscCall(
195 MatAXPY(petsc_jacobian->mat(), 1 - weight, petsc_time->mat(), SUBSET_NONZERO_PATTERN));
196 else
197 jacobian.add(1 - weight, *_Ke_time);
198}
199
200void
202{
203 const auto coeff = duDotDuCoeff();
204 for (const auto i : index_range(_du_dot_du))
205 if (integratesVar(i))
206 _du_dot_du[i] = coeff / _dt;
207}
void ErrorVector unsigned int
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
virtual void setUDotRequested(const bool u_dot_requested)
Set boolean flag to true to store solution time derivative.
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.
void registerBase(const std::string &value)
This method must be called from every base "Moose System" to create linkage with the Action System.
Interface class for routines and member variables for time integrators relying on linear system assem...
const std::string & type() const
Get the type of this class.
Definition MooseBase.h:93
void paramError(const std::string &param, Args... args) const
Emits an error prefixed with the file and line number of the given param (from the input file) along ...
Definition MooseBase.h:457
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
Every object that can be built by the factory should be derived from this class.
Definition MooseObject.h:31
static InputParameters validParams()
Definition MooseObject.C:25
Interface class for routines and member variables for time integrators relying on Newton's method.
NonlinearSystemBase * _nl
Pointer to the nonlinear system, can happen that we dont have any.
libMesh::NonlinearImplicitSystem * _nonlinear_implicit_system
libMesh nonlinear implicit system, if applicable; otherwise, nullptr
A class for creating restricted objects.
Definition Restartable.h:29
virtual TagID getMatrixTagID(const TagName &tag_name) const
Get a TagID from a TagName.
Definition SubProblem.C:334
Base class for a system (of equations)
Definition SystemBase.h:87
libMesh::SparseMatrix< Number > & addMatrix(TagID tag)
Adds a matrix with a given tag.
Definition SystemBase.C:570
virtual libMesh::System & system()=0
Get the reference to the libMesh system.
std::vector< dof_id_type > & _local_indices
The local degree of freedom indices this time integrator is being applied to.
bool & _var_restriction
Whether the user has requested that the time integrator be applied to a subset of variables.
std::unique_ptr< NumericVector< Number > > & _solution_old_sub
void setNumIterationsLastSolve()
Record the linear and nonlinear iterations from a just finished solve.
void computeDuDotDu()
Compute _du_dot_du.
void weightNonTimeJacobian(libMesh::SparseMatrix< Number > &jacobian, Real weight) const
Turn the system Jacobian J = J_time + J_non_time into J_time + weight * J_non_time,...
unsigned int getNumLinearIterationsLastSolve() const
Gets the number of linear iterations in the most recent solve.
std::unordered_set< unsigned int > & _vars
The variables that this time integrator integrates.
void copyVector(const NumericVector< Number > &from, NumericVector< Number > &to)
Copy from one vector into another.
const NumericVector< Number > *const & _solution
unsigned int _n_linear_iterations
Total number of linear iterations over all stages of the time step.
std::unique_ptr< NumericVector< Number > > _from_subvector
A vector that is used for creating 'from' subvectors in the copyVector() method.
bool integratesVar(const unsigned int var_num) const
unsigned int _n_nonlinear_iterations
Total number of nonlinear iterations over all stages of the time step.
static InputParameters validParams()
FEProblemBase & _fe_problem
Reference to the problem.
virtual void init()
Called only before the very first timestep (t_step = 0) Never called again (not even during recover/r...
unsigned int getNumNonlinearIterationsLastSolve() const
Gets the number of nonlinear iterations in the most recent solve.
libMesh::SparseMatrix< Number > * _Ke_time
Jacobian of the time residual, see requestTimeJacobian()
SystemBase & _sys
Reference to the system this time integrator operates on.
virtual void solve()
Solves the time step and sets the number of nonlinear and linear iterations.
Real & _dt
The current time step size.
void requestTimeJacobian()
Request the Jacobian of the time residual as a separate tagged matrix, which weightNonTimeJacobian() ...
TimeIntegrator(const InputParameters &parameters)
std::vector< Real > & _du_dot_du
Derivative of time derivative with respect to current solution: for the different variables.
std::unique_ptr< NumericVector< Number > > & _solution_sub
virtual Real duDotDuCoeff() const
const NumericVector< Number > & _solution_old
std::unique_ptr< NonlinearSolver< Number > > nonlinear_solver
unsigned int n_nonlinear_iterations() const
const Parallel::Communicator & comm() const
virtual void close()=0
void get_all_variable_numbers(std::vector< unsigned int > &all_variable_numbers) const
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
SolverPackage default_solver_package()