version 3.10-dev
multistagetimestepper.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
12#ifndef DUMUX_TIMESTEPPING_MULTISTAGE_TIMESTEPPER_HH
13#define DUMUX_TIMESTEPPING_MULTISTAGE_TIMESTEPPER_HH
14
15#include <memory>
16#include <vector>
17#include <cmath>
18#include <iostream>
19
20#include <dumux/io/format.hh>
21
25
26namespace Dumux::Experimental {
27
29template<class Scalar>
31{
32 struct Params {
33 Scalar alpha, betaDt, timeAtStage, dtFraction;
35 };
36public:
38 MultiStageParams(const MultiStageMethod<Scalar>& m, std::size_t i, const Scalar t, const Scalar dt)
39 : size_(i+1)
40 {
41 params_.resize(size_);
42 for (std::size_t k = 0; k < size_; ++k)
43 {
44 auto& p = params_[k];
45 p.alpha = m.temporalWeight(i, k);
46 p.betaDt = m.spatialWeight(i, k)*dt;
47 p.timeAtStage = t + m.timeStepWeight(k)*dt;
48 p.dtFraction = m.timeStepWeight(k);
49
50 using std::abs;
51 p.skipTemporal = (abs(p.alpha) < 1e-6);
52 p.skipSpatial = (abs(p.betaDt) < 1e-6);
53 }
54 }
55
56 std::size_t size () const
57 { return size_; }
58
60 Scalar temporalWeight (std::size_t k) const
61 { return params_[k].alpha; }
62
64 Scalar spatialWeight (std::size_t k) const
65 { return params_[k].betaDt; }
66
68 Scalar timeAtStage (std::size_t k) const
69 { return params_[k].timeAtStage; }
70
72 Scalar timeStepFraction (std::size_t k) const
73 { return params_[k].dtFraction; }
74
76 Scalar skipTemporal (std::size_t k) const
77 { return params_[k].skipTemporal; }
78
80 Scalar skipSpatial (std::size_t k) const
81 { return params_[k].skipSpatial; }
82
83private:
84 std::size_t size_;
85 std::vector<Params> params_;
86};
87
94template<class PDESolver, class Scalar = double>
96{
97 using Variables = typename PDESolver::Variables;
100
101public:
102
109 MultiStageTimeStepper(std::shared_ptr<PDESolver> pdeSolver,
110 std::shared_ptr<const MultiStageMethod<Scalar>> msMethod,
111 const std::string& paramGroup = "")
112 : pdeSolver_(pdeSolver)
113 , msMethod_(msMethod)
114 {
115 initParams_(paramGroup);
116
117 if (pdeSolver_->verbosity() >= 1)
118 std::cout << "Initialize time stepper with method " << msMethod_->name()
119 << Fmt::format(" ({} stage{})", msMethod_->numStages(), (msMethod_->numStages() > 1 ? "s" : ""))
120 << std::endl;
121 }
122
130 void step(Variables& vars, const Scalar t, const Scalar dt)
131 {
132 // make sure there are no traces of previous stages
133 pdeSolver_->assembler().clearStages();
134
135 // do time integration
136 bool converged = step_(vars, t, dt);
137
138 // clear traces of previously registered stages
139 pdeSolver_->assembler().clearStages();
140
141 // if the solver didn't converge we can't recover
142 if (!converged)
143 DUNE_THROW(NumericalProblem, "Solver did not converge!");
144 }
145
152 void step(Variables& vars, TimeLoopBase<Scalar>& timeLoop)
153 {
154 // make sure there are no traces of previous stages
155 pdeSolver_->assembler().clearStages();
156
157 // try solving the non-linear system
158 for (std::size_t i = 0; i <= maxTimeStepDivisions_; ++i)
159 {
160 // try solving the non-linear system
161 bool converged = step_(vars, timeLoop.time(), timeLoop.timeStepSize());
162 if (converged)
163 {
164 // clear traces of previously registered stages
165 pdeSolver_->assembler().clearStages();
166 return;
167 }
168
169 else if (!converged && i < maxTimeStepDivisions_)
170 {
171 // set solution to previous solution & reset time step
172 Backend::update(vars, pdeSolver_->assembler().prevSol());
173 pdeSolver_->assembler().resetTimeStep(Backend::dofs(vars));
174
175 if (pdeSolver_->verbosity() >= 1)
176 {
177 const auto dt = timeLoop.timeStepSize();
178 std::cout << Fmt::format("Solver did not converge with dt = {} seconds. ", dt)
179 << Fmt::format("Retrying with time step of dt = {} seconds.\n", dt*retryTimeStepReductionFactor_);
180 }
181
182 // try again with dt = dt * retryTimeStepReductionFactor_
183 timeLoop.setTimeStepSize(timeLoop.timeStepSize() * retryTimeStepReductionFactor_);
184 }
185
186 else
187 {
188 pdeSolver_->assembler().clearStages();
189 DUNE_THROW(NumericalProblem,
190 Fmt::format("Solver didn't converge after {} time-step divisions; dt = {}.\n",
191 maxTimeStepDivisions_, timeLoop.timeStepSize()));
192 }
193 }
194
195 DUNE_THROW(Dune::InvalidStateException, "Unreachable");
196 }
197
198private:
199 bool step_(Variables& vars, const Scalar t, const Scalar dt)
200 {
201 for (auto stageIdx = 1UL; stageIdx <= msMethod_->numStages(); ++stageIdx)
202 {
203 // prepare the assembler for this stage
204 pdeSolver_->assembler().prepareStage(
205 vars,
206 std::make_shared<StageParams>(*msMethod_, stageIdx, t, dt)
207 );
208
209 // assemble & solve
210 bool converged = pdeSolver_->apply(vars);
211 if (!converged)
212 return false;
213 }
214
215 return true;
216 }
217
218 void initParams_(const std::string& group = "")
219 {
220 maxTimeStepDivisions_ = getParamFromGroup<std::size_t>(group, "TimeStepper.MaxTimeStepDivisions", 10);
221 retryTimeStepReductionFactor_ = getParamFromGroup<Scalar>(group, "TimeStepper.RetryTimeStepReductionFactor", 0.5);
222 }
223
224 std::shared_ptr<PDESolver> pdeSolver_;
225 std::shared_ptr<const MultiStageMethod<Scalar>> msMethod_;
226
227 Scalar maxTimeStepDivisions_;
228 Scalar retryTimeStepReductionFactor_;
229};
230
231} // end namespace Dumux::Experimental
232
233#endif
Definition: variablesbackend.hh:187
Abstract interface for one-step multi-stage method parameters in Shu/Osher form.
Definition: multistagemethods.hh:75
virtual Scalar timeStepWeight(std::size_t k) const =0
time step weights for each stage ( )
virtual Scalar temporalWeight(std::size_t i, std::size_t k) const =0
weights of the temporal operator residual ( )
virtual Scalar spatialWeight(std::size_t i, std::size_t k) const =0
weights of the spatial operator residual ( )
Data object for the parameters of a given stage.
Definition: multistagetimestepper.hh:31
Scalar timeAtStage(std::size_t k) const
the time at which we have to evaluate the operators
Definition: multistagetimestepper.hh:68
Scalar spatialWeight(std::size_t k) const
weights of the spatial operator residual ( )
Definition: multistagetimestepper.hh:64
Scalar timeStepFraction(std::size_t k) const
the fraction of a time step corresponding to the k-th stage
Definition: multistagetimestepper.hh:72
Scalar skipTemporal(std::size_t k) const
If .
Definition: multistagetimestepper.hh:76
std::size_t size() const
Definition: multistagetimestepper.hh:56
Scalar skipSpatial(std::size_t k) const
If .
Definition: multistagetimestepper.hh:80
Scalar temporalWeight(std::size_t k) const
weights of the temporal operator residual ( )
Definition: multistagetimestepper.hh:60
MultiStageParams(const MultiStageMethod< Scalar > &m, std::size_t i, const Scalar t, const Scalar dt)
Extract params for stage i from method m.
Definition: multistagetimestepper.hh:38
Time stepping with a multi-stage method.
Definition: multistagetimestepper.hh:96
MultiStageTimeStepper(std::shared_ptr< PDESolver > pdeSolver, std::shared_ptr< const MultiStageMethod< Scalar > > msMethod, const std::string &paramGroup="")
The constructor.
Definition: multistagetimestepper.hh:109
void step(Variables &vars, TimeLoopBase< Scalar > &timeLoop)
Advance one time step of the given time loop (adaptive time stepping on solver failure)
Definition: multistagetimestepper.hh:152
void step(Variables &vars, const Scalar t, const Scalar dt)
Advance one time step of the given time loop.
Definition: multistagetimestepper.hh:130
Class that represents the variables of a model. We assume that models are formulated on the basis of ...
Definition: variables.hh:41
Exception thrown if a fixable numerical problem occurs.
Definition: exceptions.hh:27
Detail::PDESolver::AssemblerVariables< Assembler > Variables
export the type of variables that represent a numerical solution
Definition: common/pdesolver.hh:71
virtual Scalar time() const =0
Return the time before the time integration. To get the time after the time integration you have to ...
virtual void setTimeStepSize(Scalar dt)=0
Set the current time step size to a given value.
virtual Scalar timeStepSize() const =0
Returns the suggested time step length .
Manages the handling of time dependent problems.
Formatting based on the fmt-library which implements std::format of C++20.
Parameters for different multistage time stepping methods.
Definition: experimental/assembly/cclocalassembler.hh:36
Backends for operations on different solution vector types or more generic variable classes to be use...