https://mooseframework.inl.gov
Loading...
Searching...
No Matches
LStableDirk3.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 "LStableDirk3.h"
11#include "NonlinearSystem.h"
12#include "FEProblem.h"
13#include "PetscSupport.h"
15
18{
21 "Third order diagonally implicit Runge Kutta method (Dirk) with three stages.");
22 return params;
23}
24
26 : TimeIntegrator(parameters),
27 _stage(1),
28 _gamma(-std::sqrt(2.) * std::cos(std::atan(std::sqrt(2.) / 4.) / 3.) / 2. +
29 std::sqrt(6.) * std::sin(std::atan(std::sqrt(2.) / 4.) / 3.) / 2. + 1.)
30{
31 mooseInfo("LStableDirk3 and other multistage TimeIntegrators are known not to work with "
32 "Materials/AuxKernels that accumulate 'state' and should be used with caution.");
33
34 // Name the stage residuals "residual_stage1", "residual_stage2", etc.
35 for (unsigned int stage = 0; stage < 3; ++stage)
36 {
37 std::ostringstream oss;
38 oss << "residual_stage" << stage + 1;
39 _stage_residuals[stage] = addVector(oss.str(), false, GHOSTED);
40 }
41
42 // Initialize parameters
43 _c[0] = _gamma;
44 _c[1] = .5 * (1 + _gamma);
45 _c[2] = 1.0;
46
47 _a[0][0] = _gamma;
48 _a[1][0] = .5 * (1 - _gamma);
49 _a[1][1] = _gamma;
50 _a[2][0] = .25 * (-6 * _gamma * _gamma + 16 * _gamma - 1);
51 _a[2][1] = .25 * (6 * _gamma * _gamma - 20 * _gamma + 5);
52 _a[2][2] = _gamma;
53}
54
55void
57{
58 // We are multiplying by the method coefficients in postResidual(), so
59 // the time derivatives are of the same form at every stage although
60 // the current solution varies depending on the stage.
61 if (!_sys.solutionUDot())
62 mooseError("LStableDirk3: Time derivative of solution (`u_dot`) is not stored. Please set "
63 "uDotRequested() to true in FEProblemBase befor requesting `u_dot`.");
64
65 NumericVector<Number> & u_dot = *_sys.solutionUDot();
66 u_dot = *_solution;
68 u_dot.close();
70}
71
72void
74 const dof_id_type & dof,
75 ADReal & /*ad_u_dotdot*/) const
76{
78}
79
80void
82{
83 // Time at end of step
84 Real time_old = _fe_problem.timeOld();
85
86 // Reset iteration counts
89
90 // A for-loop would increment _stage too far, so we use an extra
91 // loop counter.
92 for (unsigned int current_stage = 1; current_stage < 4; ++current_stage)
93 {
94 // Set the current stage value
95 _stage = current_stage;
96
97 // This ensures that all the Output objects in the OutputWarehouse
98 // have had solveSetup() called, and sets the default solver
99 // parameters for PETSc.
101
102 _console << "Stage " << _stage << std::endl;
103
104 // Set the time for this stage
105 _fe_problem.time() = time_old + _c[_stage - 1] * _dt;
106
107 // If we previously used coloring, destroy the old object so it doesn't leak when we allocate a
108 // new object in the following lines
110
111 // Potentially setup finite differencing contexts for the solve
113
114 // Do the solve
115 _nl->system().solve();
116
117 // Update the iteration counts
120
121 // Abort time step immediately on stage failure - see TimeIntegrator doc page
123 return;
124 }
125}
126
127void
128LStableDirk3::postResidual(NumericVector<Number> & residual)
129{
130 // Error if _stage got messed up somehow.
131 if (_stage > 3)
133 "LStableDirk3::postResidual(): Member variable _stage can only have values 1, 2, or 3.");
134
135 // In the standard RK notation, the residual of stage 1 of s is given by:
136 //
137 // R := M*(Y_i - y_n)/dt - \sum_{j=1}^s a_{ij} * f(t_n + c_j*dt, Y_j) = 0
138 //
139 // where:
140 // .) M is the mass matrix
141 // .) Y_i is the stage solution
142 // .) dt is the timestep, and is accounted for in the _Re_time residual.
143 // .) f are the "non-time" residuals evaluated for a given stage solution.
144 // .) The minus signs are already "baked in" to the residuals and so do not appear below.
145
146 // Store this stage's non-time residual. We are calling operator=
147 // here, and that calls close().
149
150 // Build up the residual for this stage.
151 residual.add(1., *_Re_time);
152 for (unsigned int j = 0; j < _stage; ++j)
153 residual.add(_a[_stage - 1][j], *_stage_residuals[j]);
154 residual.close();
155}
DualNumber< Real, DNDerivativeType, true > ADReal
registerMooseObject("MooseApp", LStableDirk3)
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.
Third order diagonally implicit Runge Kutta method (Dirk) with three stages.
virtual void computeTimeDerivatives() override
Computes the time derivative and the Jacobian of the time derivative.
NumericVector< Number > * _stage_residuals[3]
const Real _gamma
void computeTimeDerivativeHelper(T &u_dot, const T2 &u_old) const
Helper function that actually does the math for computing the time derivative.
virtual void postResidual(NumericVector< Number > &residual) override
Callback to the NonLinearTimeIntegratorInterface called immediately after the residuals are computed ...
virtual void solve() override
Solves the time step and sets the number of nonlinear and linear iterations.
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 InputParameters validParams()
unsigned int _stage
Real _a[3][3]
LStableDirk3(const InputParameters &parameters)
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:113
virtual NumericVector< Number > * solutionUDot()
Definition SystemBase.h:289
unsigned int number() const
Gets the number of this system.
Base class for time integrators.
void computeDuDotDu()
Compute _du_dot_du.
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.
const NumericVector< Number > & _solution_old
virtual void add(const numeric_index_type i, const T value)=0
virtual void solve()