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
20{
22 params.addParam<std::vector<VariableName>>(
23 "variables", {}, "A subset of the variables that this time integrator should be applied to");
24 params.registerBase("TimeIntegrator");
25 return params;
26}
27
29 : MooseObject(parameters),
30 Restartable(this, "TimeIntegrators"),
31 NonlinearTimeIntegratorInterface(*getCheckedPointerParam<FEProblemBase *>("_fe_problem_base"),
32 *getCheckedPointerParam<SystemBase *>("_sys")),
33 LinearTimeIntegratorInterface(*getCheckedPointerParam<SystemBase *>("_sys")),
34 _fe_problem(*getCheckedPointerParam<FEProblemBase *>("_fe_problem_base")),
35 _sys(*getCheckedPointerParam<SystemBase *>("_sys")),
36 _du_dot_du(_sys.duDotDus()),
37 _solution(_sys.currentSolution()),
38 _solution_old(_sys.solutionState(1)),
39 _solution_sub(declareRestartableDataWithContext<std::unique_ptr<NumericVector<Number>>>(
40 "solution_sub", &const_cast<libMesh::Parallel::Communicator &>(this->comm()))),
41 _solution_old_sub(declareRestartableDataWithContext<std::unique_ptr<NumericVector<Number>>>(
42 "solution_old_sub", &const_cast<libMesh::Parallel::Communicator &>(this->comm()))),
43 _t_step(_fe_problem.timeStep()),
44 _dt(_fe_problem.dt()),
45 _dt_old(_fe_problem.dtOld()),
46 _n_nonlinear_iterations(0),
47 _n_linear_iterations(0),
48 _is_lumped(false),
49 _var_restriction(declareRestartableData<bool>(
50 "var_restriction", !getParam<std::vector<VariableName>>("variables").empty())),
51 _local_indices(declareRestartableData<std::vector<dof_id_type>>("local_indices")),
52 _vars(declareRestartableData<std::unordered_set<unsigned int>>("vars")),
53 _from_subvector(
54 NumericVector<Number>::build(this->comm(), libMesh::default_solver_package(), PARALLEL))
55{
57}
58
59void
61{
63 return;
64
65 const auto & var_names = getParam<std::vector<VariableName>>("variables");
66 std::vector<unsigned int> var_num_vec;
67 auto & lm_sys = _sys.system();
68 lm_sys.get_all_variable_numbers(var_num_vec);
69 std::unordered_set<unsigned int> var_nums(var_num_vec.begin(), var_num_vec.end());
70 for (const auto & var_name : var_names)
71 if (lm_sys.has_variable(var_name))
72 {
73 const auto var_num = lm_sys.variable_number(var_name);
74 _vars.insert(var_num);
75 var_nums.erase(var_num);
76 }
77
78 // If var_nums is empty then that means the user has specified all the variables in this system
79 if (var_nums.empty())
80 {
81 _var_restriction = false;
82 return;
83 }
84
85 std::vector<dof_id_type> var_dof_indices, work_vec;
86 for (const auto var_num : _vars)
87 {
88 work_vec = _local_indices;
89 _local_indices.clear();
90 lm_sys.get_dof_map().local_variable_indices(var_dof_indices, lm_sys.get_mesh(), var_num);
91 std::merge(work_vec.begin(),
92 work_vec.end(),
93 var_dof_indices.begin(),
94 var_dof_indices.end(),
95 std::back_inserter(_local_indices));
96 }
97
99 NumericVector<Number>::build(_solution->comm(), libMesh::default_solver_package(), PARALLEL);
100 _solution_old_sub = NumericVector<Number>::build(
102}
103
104void
106{
107 mooseError("Calling TimeIntegrator::solve() is no longer supported");
108}
109
110void
116
117unsigned int
122
123unsigned int
125{
126 auto & nonlinear_solver = _nonlinear_implicit_system->nonlinear_solver;
127 libmesh_assert(nonlinear_solver);
128
129 return nonlinear_solver->get_total_linear_iterations();
130}
131
132void
133TimeIntegrator::copyVector(const NumericVector<Number> & from, NumericVector<Number> & to)
134{
135 if (!_var_restriction)
136 to = from;
137 else
138 {
139 auto to_sub = to.get_subvector(_local_indices);
140 from.create_subvector(*_from_subvector, _local_indices, false);
141 *to_sub = *_from_subvector;
142 to.restore_subvector(std::move(to_sub), _local_indices);
143 }
144}
145
146bool
147TimeIntegrator::integratesVar(const unsigned int var_num) const
148{
149 if (!_var_restriction)
150 return true;
151
152 return _vars.count(var_num);
153}
154
155void
157{
158 const auto coeff = duDotDuCoeff();
159 for (const auto i : index_range(_du_dot_du))
160 if (integratesVar(i))
161 _du_dot_du[i] = coeff / _dt;
162}
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...
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.
libMesh::NonlinearImplicitSystem * _nonlinear_implicit_system
libMesh nonlinear implicit system, if applicable; otherwise, nullptr
A class for creating restricted objects.
Definition Restartable.h:29
Base class for a system (of equations)
Definition SystemBase.h:87
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.
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.
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.
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
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()