https://mooseframework.inl.gov
Loading...
Searching...
No Matches
ImplicitMidpoint.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 "ImplicitMidpoint.h"
11#include "libmesh/sparse_matrix.h"
12#include "NonlinearSystem.h"
13#include "FEProblem.h"
14#include "PetscSupport.h"
15
17
20{
22 params.addClassDescription("Second-order Runge-Kutta (implicit midpoint) time integration.");
23 return params;
24}
25
27 : TimeIntegrator(parameters),
28 _stage(1),
29 _residual_stage1(addVector("residual_stage1", false, libMesh::GHOSTED))
30{
32 mooseInfo("ImplicitMidpoint and other multistage TimeIntegrators are known not to work with "
33 "Materials/AuxKernels that accumulate 'state' and should be used with caution.");
34}
35
36void
38{
39 // We are multiplying by the method coefficients in postResidual(), so
40 // the time derivatives are of the same form at every stage although
41 // the current solution varies depending on the stage.
42 if (!_sys.solutionUDot())
43 mooseError("ImplicitMidpoint: Time derivative of solution (`u_dot`) is not stored. Please set "
44 "uDotRequested() to true in FEProblemBase befor requesting `u_dot`.");
45
46 NumericVector<Number> & u_dot = *_sys.solutionUDot();
47 u_dot = *_solution;
49 u_dot.close();
51}
52
53void
55 const dof_id_type & dof,
56 ADReal & /*ad_u_dotdot*/) const
57{
59}
60
61void
63{
64 Real time_new = _fe_problem.time();
65 Real time_old = _fe_problem.timeOld();
66 Real time_half = (time_new + time_old) / 2.;
67
68 // Reset iteration counts
71
72 // Compute first stage
74 _console << "1st stage" << std::endl;
75 _stage = 1;
76 _fe_problem.time() = time_half;
77 _nl->system().solve();
80
81 // Abort time step immediately on stage failure - see TimeIntegrator doc page
83 return;
84
85 // Compute second stage
87 _console << "2nd stage" << std::endl;
88 _stage = 2;
89 _fe_problem.time() = time_new;
90 _nl->system().solve();
93}
94
95void
96ImplicitMidpoint::postResidual(NumericVector<Number> & residual)
97{
98 if (_stage == 1)
99 {
100 // In the standard RK notation, the stage 1 residual is given by:
101 //
102 // R := M*(Y_1 - y_n)/dt - (1/2)*f(t_n + dt/2, Y_1) = 0
103 //
104 // where:
105 // .) M is the mass matrix
106 // .) f(t_n + dt/2, Y_1) is saved in _residual_stage1
107 // .) The minus sign is baked in to the non-time residuals, so it does not appear here.
110
111 residual.add(1., *_Re_time);
112 residual.add(0.5, *_Re_non_time);
113 residual.close();
114 }
115 else if (_stage == 2)
116 {
117 // The update step. In the standard RK notation, the update
118 // residual is given by:
119 //
120 // R := M*(y_{n+1} - y_n)/dt - f(t_n + dt/2, Y_1) = 0
121 //
122 // where:
123 // .) M is the mass matrix.
124 // .) f(t_n + dt/2, Y_1) is the residual from stage 1, it has already
125 // been saved as _residual_stage1.
126 // .) The minus signs are "baked in" to the non-time residuals, so
127 // they do not appear here.
128 residual.add(1., *_Re_time);
129 residual.add(1., *_residual_stage1);
130 residual.close();
131 }
132 else
134 "ImplicitMidpoint::postResidual(): _stage = ", _stage, ", only _stage = 1, 2 is allowed.");
135}
136
137void
138ImplicitMidpoint::postJacobian(SparseMatrix<Number> & jacobian)
139{
140 // stage 1 weights its non-time residual by 1/2; the update stage uses the non-time residual
141 // of stage 1 only, which does not depend on the current solution, see postResidual()
142 weightNonTimeJacobian(jacobian, _stage == 1 ? 0.5 : 0.0);
143}
DualNumber< Real, DNDerivativeType, true > ADReal
registerMooseObject("MooseApp", ImplicitMidpoint)
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.
Second-order Runge-Kutta (implicit midpoint) time integration.
virtual void computeTimeDerivatives() override
Computes the time derivative and the Jacobian of the time derivative.
static InputParameters validParams()
unsigned int _stage
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
virtual void solve() override
Solves the time step and sets the number of nonlinear and linear iterations.
virtual void postJacobian(libMesh::SparseMatrix< Number > &jacobian) override
Callback to the NonLinearTimeIntegratorInterface called after the element, face, Dirac and constraint...
virtual void postResidual(NumericVector< Number > &residual) override
Callback to the NonLinearTimeIntegratorInterface called immediately after the residuals are computed ...
NumericVector< Number > * _residual_stage1
Buffer to store non-time residual from the first stage.
void computeTimeDerivativeHelper(T &u_dot, const T2 &u_old) const
Helper function that actually does the math for computing the time derivative.
ImplicitMidpoint(const InputParameters &parameters)
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.
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 libMesh::System & system() override
Get the reference to the libMesh system.
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 > * _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.
void requestTimeJacobian()
Request the Jacobian of the time residual as a separate tagged matrix, which weightNonTimeJacobian() ...
const NumericVector< Number > & _solution_old
virtual void close()=0
virtual void solve()
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...