13#ifndef DUMUX_EXPERIMENTAL_MULTISTAGE_FV_ASSEMBLER_HH
14#define DUMUX_EXPERIMENTAL_MULTISTAGE_FV_ASSEMBLER_HH
21#include <dune/istl/matrixindexset.hh>
47template<
class DiscretizationMethod>
53 template<
class TypeTag,
class Impl, DiffMethod diffMethod>
60 template<
class TypeTag,
class Impl, DiffMethod diffMethod>
67 template<
class TypeTag,
class Impl, DiffMethod diffMethod>
71template<
class TypeTag,
class Impl, DiffMethod diffMethod>
74>::template type<TypeTag, Impl, diffMethod>;
87template<
class TypeTag, DiffMethod diffMethod>
91 using GridView =
typename GridGeo::GridView;
93 using Element =
typename GridView::template Codim<0>::Entity;
94 using ElementSeed =
typename GridView::Grid::template Codim<0>::EntitySeed;
123 : timeSteppingMethod_(msMethod)
134 maybeComputeColors_();
141 template<
class PartialReassembler = DefaultPartialReassembler>
144 resetJacobian_(partialReassembler);
147 spatialOperatorEvaluations_.back() = 0.0;
148 temporalOperatorEvaluations_.back() = 0.0;
150 if (stageParams_->size() != spatialOperatorEvaluations_.size())
151 DUNE_THROW(Dune::InvalidStateException,
"Wrong number of residuals");
153 assemble_([&](
const Element& element)
155 LocalAssembler localAssembler(*
this, element, curSol);
156 localAssembler.assembleJacobianAndResidual(
157 *jacobian_, *residual_, *gridVariables_,
159 temporalOperatorEvaluations_.back(),
160 spatialOperatorEvaluations_.back(),
171 const auto curStage = stageParams_->size() - 1;
172 if (curStage > 1 && autonomousSpatialOperator_ && firstStageAssembly_)
174 spatialOperatorEvaluations_[curStage-1] = spatialOperatorEvaluations_.back();
175 temporalOperatorEvaluations_[curStage-1] = temporalOperatorEvaluations_.back();
177 firstStageAssembly_ =
false;
180 auto constantResidualComponent = (*residual_);
181 constantResidualComponent = 0.0;
182 for (std::size_t k = 0; k < stageParams_->size()-1; ++k)
184 if (!stageParams_->skipTemporal(k))
185 constantResidualComponent.axpy(stageParams_->temporalWeight(k), temporalOperatorEvaluations_[k]);
186 if (!stageParams_->skipSpatial(k))
187 constantResidualComponent.axpy(stageParams_->spatialWeight(k), spatialOperatorEvaluations_[k]);
191 for (std::size_t i = 0; i < constantResidualComponent.size(); ++i)
192 for (std::size_t ii = 0; ii < constantResidualComponent[i].size(); ++ii)
193 (*residual_)[i][ii] += constrainedDofs_[i][ii] > 0.5 ? 0.0 : constantResidualComponent[i][ii];
198 { DUNE_THROW(Dune::NotImplemented,
"residual"); }
206 jacobian_ = std::make_shared<JacobianMatrix>();
207 jacobian_->setBuildMode(JacobianMatrix::random);
208 residual_ = std::make_shared<ResidualType>();
210 setResidualSize_(*residual_);
211 setJacobianPattern_();
219 setResidualSize_(*residual_);
220 setJacobianPattern_();
221 maybeComputeColors_();
226 {
return gridGeometry_->numDofs(); }
230 {
return *problem_; }
234 {
return *gridGeometry_; }
242 {
return *gridVariables_; }
246 {
return *gridVariables_; }
250 {
return *jacobian_; }
254 {
return *residual_; }
258 {
return *prevSol_; }
264 {
return { LocalResidual{problem_.get(),
nullptr} }; }
283 spatialOperatorEvaluations_.clear();
284 temporalOperatorEvaluations_.clear();
285 stageParams_.reset();
288 template<
class StageParams>
291 stageParams_ = std::move(params);
292 const auto curStage = stageParams_->size() - 1;
293 firstStageAssembly_ =
true;
300 setProblemTime_(*problem_, stageParams_->timeAtStage(0));
304 assert(spatialOperatorEvaluations_.size() >= 0);
305 if (spatialOperatorEvaluations_.size() == 0)
307 spatialOperatorEvaluations_.push_back(*residual_);
308 temporalOperatorEvaluations_.push_back(*residual_);
311 assemble_([&](
const auto& element)
313 LocalAssembler localAssembler(*
this, element, *prevSol_);
314 localAssembler.localResidual().spatialWeight(1.0);
315 localAssembler.localResidual().temporalWeight(1.0);
316 localAssembler.assembleCurrentResidual(spatialOperatorEvaluations_.back(), temporalOperatorEvaluations_.back());
326 else if (spatialOperatorEvaluations_.size() > 0)
329 spatialOperatorEvaluations_.resize(1);
330 temporalOperatorEvaluations_.resize(1);
343 if (curStage > 1 && !autonomousSpatialOperator_)
345 setProblemTime_(*problem_, stageParams_->timeAtStage(curStage-1));
346 spatialOperatorEvaluations_.back() = 0.0;
347 temporalOperatorEvaluations_.back() = 0.0;
348 assemble_([&](
const auto& element)
350 LocalAssembler localAssembler(*
this, element, x);
351 localAssembler.localResidual().spatialWeight(1.0);
352 localAssembler.localResidual().temporalWeight(1.0);
353 localAssembler.assembleCurrentResidual(spatialOperatorEvaluations_.back(), temporalOperatorEvaluations_.back());
358 setProblemTime_(*problem_, stageParams_->timeAtStage(curStage));
362 if (spatialOperatorEvaluations_.size() != curStage)
363 DUNE_THROW(Dune::InvalidStateException,
364 "Invalid state. Maybe you forgot to call clearStages()");
367 spatialOperatorEvaluations_.push_back(*residual_);
368 temporalOperatorEvaluations_.push_back(*residual_);
376 {
return timeSteppingMethod_->implicit(); }
387 { autonomousSpatialOperator_ = a; }
393 void setJacobianPattern_()
400 if (timeSteppingMethod_->implicit())
411 void maybeComputeColors_()
413 if (enableMultithreading_)
418 void resetResidual_()
422 residual_ = std::make_shared<ResidualType>();
423 setResidualSize_(*residual_);
426 setResidualSize_(constrainedDofs_);
429 constrainedDofs_ = 0.0;
433 template <
class PartialReassembler = DefaultPartialReassembler>
434 void resetJacobian_(
const PartialReassembler *partialReassembler =
nullptr)
438 jacobian_ = std::make_shared<JacobianMatrix>();
439 jacobian_->setBuildMode(JacobianMatrix::random);
440 setJacobianPattern_();
443 if (partialReassembler)
444 partialReassembler->resetJacobian(*
this);
454 template<
typename AssembleElementFunc>
455 void assemble_(AssembleElementFunc&& assembleElement)
const
458 bool succeeded =
false;
463 if (enableMultithreading_)
465 assert(elementSets_.size() > 0);
471 for (
const auto& elements : elementSets_)
475 const auto element = gridView().grid().entity(elements[i]);
476 assembleElement(element);
481 for (
const auto& element : elements(
gridView()))
482 assembleElement(element);
488 catch (NumericalProblem &e)
490 std::cout <<
"rank " <<
gridView().comm().rank()
491 <<
" caught an exception while assembling:" << e.what()
498 succeeded =
gridView().comm().min(succeeded);
502 DUNE_THROW(NumericalProblem,
"A process did not succeed in linearizing the system");
506 void setProblemTime_(
const P& p,
const Scalar t)
508 if constexpr (
requires { p.setTime(t); })
511 static_assert(!
requires (P& q) { q.setTime(
Scalar{}); },
512 "The multi-stage assembler sets the stage time through a const problem: setTime has to be const.");
515 std::shared_ptr<const Experimental::MultiStageMethod<Scalar>> timeSteppingMethod_;
516 std::vector<ResidualType> spatialOperatorEvaluations_;
517 std::vector<ResidualType> temporalOperatorEvaluations_;
519 std::shared_ptr<const StageParams> stageParams_;
522 std::shared_ptr<const Problem> problem_;
525 std::shared_ptr<const GridGeometry> gridGeometry_;
528 std::shared_ptr<GridVariables> gridVariables_;
534 std::shared_ptr<JacobianMatrix> jacobian_;
535 std::shared_ptr<ResidualType> residual_;
538 bool autonomousSpatialOperator_ =
false;
541 bool firstStageAssembly_ =
false;
544 bool enableMultithreading_ =
false;
545 std::deque<std::vector<ElementSeed>> elementSets_;
An assembler for Jacobian and residual contribution per element (cell-centered methods).
Definition experimental/assembly/cclocalassembler.hh:186
An assembler for Jacobian and residual contribution per element (CVFE methods).
Definition experimental/assembly/cvfelocalassembler.hh:330
const Problem & problem() const
The problem.
Definition multistagefvassembler.hh:229
void assembleResidual(const SolutionVector &curSol)
compute the residuals using the internal residual
Definition multistagefvassembler.hh:197
std::size_t numDofs() const
Returns the number of degrees of freedom.
Definition multistagefvassembler.hh:225
MultiStageFVLocalOperator< LocalResidual > localResidual() const
Create a local residual object (used by the local assembler).
Definition multistagefvassembler.hh:263
void setAutonomousSpatialOperator(bool a)
Definition multistagefvassembler.hh:386
typename Dumux::Detail::NativeDuneVectorType< SolutionVector >::type ResidualType
Definition multistagefvassembler.hh:106
void resetTimeStep(const SolutionVector &cursol)
Reset the gridVariables.
Definition multistagefvassembler.hh:275
GetPropType< TypeTag, Properties::JacobianMatrix > JacobianMatrix
Definition multistagefvassembler.hh:104
ResidualType & residual()
The residual vector (rhs).
Definition multistagefvassembler.hh:253
void clearStages()
Definition multistagefvassembler.hh:281
MultiStageFVAssembler(std::shared_ptr< const Problem > problem, std::shared_ptr< const GridGeometry > gridGeometry, std::shared_ptr< GridVariables > gridVariables, std::shared_ptr< const Experimental::MultiStageMethod< Scalar > > msMethod, const SolutionVector &prevSol)
The constructor for instationary problems.
Definition multistagefvassembler.hh:118
bool isStationaryProblem() const
TODO get rid of this (called by Newton but shouldn't be necessary).
Definition multistagefvassembler.hh:372
Experimental::MultiStageParams< Scalar > StageParams
Definition multistagefvassembler.hh:103
void setLinearSystem()
The version without arguments uses the default constructor to create the jacobian and residual object...
Definition multistagefvassembler.hh:204
void prepareStage(SolutionVector &x, StageParams params)
Definition multistagefvassembler.hh:289
GetPropType< TypeTag, Properties::GridVariables > GridVariables
Definition multistagefvassembler.hh:108
void updateAfterGridAdaption()
Resizes jacobian and residual and recomputes colors.
Definition multistagefvassembler.hh:217
const GridView & gridView() const
The gridview.
Definition multistagefvassembler.hh:237
GridGeo GridGeometry
Definition multistagefvassembler.hh:110
const SolutionVector & prevSol() const
The solution of the previous time step.
Definition multistagefvassembler.hh:257
GridVariables & gridVariables()
The global grid variables.
Definition multistagefvassembler.hh:241
GetPropType< TypeTag, Properties::Problem > Problem
Definition multistagefvassembler.hh:111
void updateGridVariables(const SolutionVector &cursol)
Update the grid variables.
Definition multistagefvassembler.hh:269
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition multistagefvassembler.hh:102
GetPropType< TypeTag, Properties::SolutionVector > SolutionVector
Definition multistagefvassembler.hh:105
bool isImplicit() const
Definition multistagefvassembler.hh:375
const GridVariables & gridVariables() const
The global grid variables.
Definition multistagefvassembler.hh:245
void assembleJacobianAndResidual(const SolutionVector &curSol, const PartialReassembler *partialReassembler=nullptr)
Assembles the global Jacobian of the residual and the residual for the current solution.
Definition multistagefvassembler.hh:142
const GridGeometry & gridGeometry() const
The global finite volume geometry.
Definition multistagefvassembler.hh:233
JacobianMatrix & jacobian()
The jacobian matrix.
Definition multistagefvassembler.hh:249
Definition multistagefvlocaloperator.hh:23
Abstract interface for one-step multi-stage method parameters in Shu/Osher form.
Definition multistagemethods.hh:75
Data object for the parameters of a given stage.
Definition multistagetimestepper.hh:31
detects which entries in the Jacobian have to be recomputed
Definition partialreassembler.hh:418
Coloring schemes for shared-memory-parallel assembly.
Defines all properties used in Dumux.
An enum class to define various differentiation methods available in order to compute the derivatives...
Helper to extract native Dune vector types from particular Dumux types.
An assembler for Jacobian and residual contribution per element (cell-centered methods).
An assembler for Jacobian and residual contribution per element (CVFE methods).
dune-grid capabilities compatibility layer
Dune::MatrixIndexSet getJacobianPattern(const GridGeometry &gridGeometry)
Helper function to generate Jacobian pattern for cell-centered methods.
Definition jacobianpattern.hh:28
constexpr bool isSerial()
Checking whether the backend is serial.
Definition multithreading.hh:45
void parallelFor(const std::size_t count, const FunctorType &functor)
A parallel for loop (multithreading).
Definition parallel_for.hh:160
T getParam(Args &&... args)
A free function to get a parameter from the parameter tree singleton.
Definition parameters.hh:155
typename GetProp< TypeTag, Property >::type GetPropType
get the type alias defined in the property
Definition propertysystem.hh:296
Helper function to generate Jacobian pattern for different discretization methods.
The available discretization methods in Dumux.
A local operator wrapper for multi-stage time stepping schemes.
Parameters for different multistage time stepping methods.
A time stepper performing a single time step of a transient simulation.
Definition cellcentered/mpfa/elementvolumevariables.hh:24
constexpr Box box
Definition method.hh:176
Definition assembly/assembler.hh:44
typename LocalAssemblerChooser< typename GetPropType< TypeTag, Properties::GridGeometry >::DiscretizationMethod >::template type< TypeTag, Impl, diffMethod, isImplicit > LocalAssemblerChooser_t
Definition assembly/assembler.hh:57
Definition assembly/assembler.hh:44
bool supportsMultithreading(const GridView &gridView)
Definition gridcapabilities.hh:36
auto computeColoring(const GridGeometry &gg, int verbosity=1)
Compute iterable lists of element seeds partitioned by color.
Definition coloring.hh:243
Parallel for loop (multithreading).
Provides a helper class for nonoverlapping decomposition.
typename NativeDuneVectorTypeImpl< V, Dune::Std::is_detected< Detail::DuneVectors::StateDetector, V >{} >::type type
Definition dunevectors.hh:57
Experimental::CCLocalAssembler< TypeTag, Impl, diffMethod > type
Definition multistagefvassembler.hh:61
Experimental::CCLocalAssembler< TypeTag, Impl, diffMethod > type
Definition multistagefvassembler.hh:68
Dumux::Experimental::CVFELocalAssembler< TypeTag, Impl, diffMethod, isImplicit > type
Definition assembly/assembler.hh:53
Definition assembly/assembler.hh:47
Traits specifying if a given discretization tag supports coloring.
Definition coloring.hh:296
Type traits to be used with vector types.