https://mooseframework.inl.gov
Loading...
Searching...
No Matches
LStableDirk4.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 "LStableDirk4.h"
11#include "libmesh/sparse_matrix.h"
12#include "NonlinearSystemBase.h"
13#include "FEProblem.h"
14#include "PetscSupport.h"
15
17
20{
23 "Fourth-order diagonally implicit Runge Kutta method (Dirk) with five stages.");
24 return params;
25}
26
27// Initialize static data
28const Real LStableDirk4::_c[LStableDirk4::_n_stages] = {.25, 0., .5, 1., 1.};
29
31 {.25, 0, 0, 0, 0},
32 {-.25, .25, 0, 0, 0},
33 {.125, .125, .25, 0, 0},
34 {-1.5, .75, 1.5, .25, 0},
35 {0, 1. / 6, 2. / 3, -1. / 12, .25}};
36
38 : TimeIntegrator(parameters), _stage(1)
39{
41 mooseInfo("LStableDirk4 and other multistage TimeIntegrators are known not to work with "
42 "Materials/AuxKernels that accumulate 'state' and should be used with caution.");
43
44 // Name the stage residuals "residual_stage1", "residual_stage2", etc.
45 for (unsigned int stage = 0; stage < _n_stages; ++stage)
46 {
47 std::ostringstream oss;
48 oss << "residual_stage" << stage + 1;
49 _stage_residuals[stage] = addVector(oss.str(), false, GHOSTED);
50 }
51}
52
53void
55{
56 // We are multiplying by the method coefficients in postResidual(), so
57 // the time derivatives are of the same form at every stage although
58 // the current solution varies depending on the stage.
59 if (!_sys.solutionUDot())
60 mooseError("LStableDirk4: Time derivative of solution (`u_dot`) is not stored. Please set "
61 "uDotRequested() to true in FEProblemBase befor requesting `u_dot`.");
62
63 NumericVector<Number> & u_dot = *_sys.solutionUDot();
64 u_dot = *_solution;
66 u_dot.close();
68}
69
70void
72 const dof_id_type & dof,
73 ADReal & /*ad_u_dotdot*/) const
74{
76}
77
78void
80{
81 // Time at end of step
82 Real time_old = _fe_problem.timeOld();
83
84 // Reset iteration counts
87
88 // A for-loop would increment _stage too far, so we use an extra
89 // loop counter.
90 for (unsigned int current_stage = 1; current_stage <= _n_stages; ++current_stage)
91 {
92 // Set the current stage value
93 _stage = current_stage;
94
95 // This ensures that all the Output objects in the OutputWarehouse
96 // have had solveSetup() called, and sets the default solver
97 // parameters for PETSc.
99
100 _console << "Stage " << _stage << std::endl;
101
102 // Set the time for this stage
103 _fe_problem.time() = time_old + _c[_stage - 1] * _dt;
104
105 // If we previously used coloring, destroy the old object so it doesn't leak when we allocate a
106 // new object in the following lines
108
109 // Potentially setup finite differencing contexts for the solve
111
112 // Do the solve
113 _nl->system().solve();
114
115 // Update the iteration counts
118
119 // Abort time step immediately on stage failure - see TimeIntegrator doc page
121 return;
122 }
123}
124
125void
126LStableDirk4::postResidual(NumericVector<Number> & residual)
127{
128 // Error if _stage got messed up somehow.
129 if (_stage > _n_stages)
130 // the explicit cast prevents strange compiler weirdness with the static
131 // const variable and the variadic mooseError function
132 mooseError("LStableDirk4::postResidual(): Member variable _stage can only have values 1-",
133 (unsigned int)_n_stages,
134 ".");
135
136 // In the standard RK notation, the residual of stage 1 of s is given by:
137 //
138 // R := M*(Y_i - y_n)/dt - \sum_{j=1}^s a_{ij} * f(t_n + c_j*dt, Y_j) = 0
139 //
140 // where:
141 // .) M is the mass matrix
142 // .) Y_i is the stage solution
143 // .) dt is the timestep, and is accounted for in the _Re_time residual.
144 // .) f are the "non-time" residuals evaluated for a given stage solution.
145 // .) The minus signs are already "baked in" to the residuals and so do not appear below.
146
147 // Store this stage's non-time residual. We are calling operator=
148 // here, and that calls close().
150
151 // Build up the residual for this stage.
152 residual.add(1., *_Re_time);
153 for (unsigned int j = 0; j < _stage; ++j)
154 residual.add(_a[_stage - 1][j], *_stage_residuals[j]);
155 residual.close();
156}
157
158void
159LStableDirk4::postJacobian(SparseMatrix<Number> & jacobian)
160{
161 // the current stage solution enters the stage residual through the time residual and the
162 // diagonal coefficient times the non-time residual of the stage, see postResidual()
163 weightNonTimeJacobian(jacobian, _a[_stage - 1][_stage - 1]);
164}
DualNumber< Real, DNDerivativeType, true > ADReal
registerMooseObject("MooseApp", LStableDirk4)
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
virtual Real & timeOld() const
virtual Real & time() const
virtual void initPetscOutputAndSomeSolverSettings()
Reinitialize PETSc output for proper linear/nonlinear iteration display.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system.
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump.
Fourth-order diagonally implicit Runge Kutta method (Dirk) with five stages.
NumericVector< Number > * _stage_residuals[_n_stages]
LStableDirk4(const InputParameters &parameters)
static const Real _a[_n_stages][_n_stages]
static InputParameters validParams()
virtual void postJacobian(libMesh::SparseMatrix< Number > &jacobian) override
Callback to the NonLinearTimeIntegratorInterface called after the element, face, Dirac and constraint...
virtual void computeTimeDerivatives() override
Computes the time derivative and the Jacobian of the time derivative.
static const Real _c[_n_stages]
virtual void solve() override
Solves the time step and sets the number of nonlinear and linear iterations.
void computeTimeDerivativeHelper(T &u_dot, const T2 &u_old) const
Helper function that actually does the math for computing the time derivative.
unsigned int _stage
virtual void postResidual(NumericVector< Number > &residual) override
Callback to the NonLinearTimeIntegratorInterface called immediately after the residuals are computed ...
virtual void computeADTimeDerivatives(ADReal &ad_u_dot, const dof_id_type &dof, ADReal &ad_u_dotdot) const override
method for computing local automatic differentiation time derivatives
static const unsigned int _n_stages
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
void mooseInfo(Args &&... args) const
Definition MooseBase.h:334
virtual void potentiallySetupFiniteDifferencing()
Create finite differencing contexts for assembly of the Jacobian and/or approximating the action of t...
virtual libMesh::System & system() override
Get the reference to the libMesh system.
void destroyColoring()
Destroy the coloring object if it exists.
NonlinearSystemBase * _nl
Pointer to the nonlinear system, can happen that we dont have any.
NumericVector< Number > * _Re_non_time
residual vector for non-time contributions
NumericVector< Number > * addVector(const std::string &name, const bool project, const libMesh::ParallelType type)
Wrapper around vector addition for nonlinear time integrators.
NumericVector< Number > * _Re_time
residual vector for time contributions
virtual bool converged(const unsigned int sys_num)
Eventually we want to convert this virtual over to taking a solver system number argument.
Definition SubProblem.h:116
virtual NumericVector< Number > * solutionUDot()
Definition SystemBase.h:292
unsigned int number() const
Gets the number of this system.
Base class for time integrators.
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.
const NumericVector< Number > *const & _solution
unsigned int _n_linear_iterations
Total number of linear iterations over all stages of the time step.
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.
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.
Real & _dt
The current time step size.
void requestTimeJacobian()
Request the Jacobian of the time residual as a separate tagged matrix, which weightNonTimeJacobian() ...
const NumericVector< Number > & _solution_old
virtual void add(const numeric_index_type i, const T value)=0
virtual void solve()
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real