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)
132 && getParam<bool>(
"Assembly.Multithreading",
true);
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(),
167 auto constantResidualComponent = (*residual_);
168 constantResidualComponent = 0.0;
169 for (std::size_t k = 0; k < stageParams_->size()-1; ++k)
171 if (!stageParams_->skipTemporal(k))
172 constantResidualComponent.axpy(stageParams_->temporalWeight(k), temporalOperatorEvaluations_[k]);
173 if (!stageParams_->skipSpatial(k))
174 constantResidualComponent.axpy(stageParams_->spatialWeight(k), spatialOperatorEvaluations_[k]);
178 for (std::size_t i = 0; i < constantResidualComponent.size(); ++i)
179 for (std::size_t ii = 0; ii < constantResidualComponent[i].size(); ++ii)
180 (*residual_)[i][ii] += constrainedDofs_[i][ii] > 0.5 ? 0.0 : constantResidualComponent[i][ii];
185 { DUNE_THROW(Dune::NotImplemented,
"residual"); }
193 jacobian_ = std::make_shared<JacobianMatrix>();
194 jacobian_->setBuildMode(JacobianMatrix::random);
195 residual_ = std::make_shared<ResidualType>();
197 setResidualSize_(*residual_);
198 setJacobianPattern_();
206 setResidualSize_(*residual_);
207 setJacobianPattern_();
208 maybeComputeColors_();
213 {
return gridGeometry_->numDofs(); }
217 {
return *problem_; }
221 {
return *gridGeometry_; }
229 {
return *gridVariables_; }
233 {
return *gridVariables_; }
237 {
return *jacobian_; }
241 {
return *residual_; }
245 {
return *prevSol_; }
251 {
return { LocalResidual{problem_.get(),
nullptr} }; }
270 spatialOperatorEvaluations_.clear();
271 temporalOperatorEvaluations_.clear();
272 stageParams_.reset();
275 template<
class StageParams>
278 stageParams_ = std::move(params);
279 const auto curStage = stageParams_->size() - 1;
286 setProblemTime_(*problem_, stageParams_->timeAtStage(0));
290 assert(spatialOperatorEvaluations_.size() >= 0);
291 if (spatialOperatorEvaluations_.size() == 0)
293 spatialOperatorEvaluations_.push_back(*residual_);
294 temporalOperatorEvaluations_.push_back(*residual_);
297 assemble_([&](
const auto& element)
299 LocalAssembler localAssembler(*
this, element, *prevSol_);
300 localAssembler.localResidual().spatialWeight(1.0);
301 localAssembler.localResidual().temporalWeight(1.0);
302 localAssembler.assembleCurrentResidual(spatialOperatorEvaluations_.back(), temporalOperatorEvaluations_.back());
312 else if (spatialOperatorEvaluations_.size() > 0)
315 spatialOperatorEvaluations_.resize(1);
316 temporalOperatorEvaluations_.resize(1);
321 setProblemTime_(*problem_, stageParams_->timeAtStage(curStage));
325 if (spatialOperatorEvaluations_.size() != curStage)
326 DUNE_THROW(Dune::InvalidStateException,
327 "Invalid state. Maybe you forgot to call clearStages()");
330 spatialOperatorEvaluations_.push_back(*residual_);
331 temporalOperatorEvaluations_.push_back(*residual_);
339 {
return timeSteppingMethod_->implicit(); }
345 void setJacobianPattern_()
352 if (timeSteppingMethod_->implicit())
353 getJacobianPattern<true>(
gridGeometry()).exportIdx(*jacobian_);
355 getJacobianPattern<false>(
gridGeometry()).exportIdx(*jacobian_);
363 void maybeComputeColors_()
365 if (enableMultithreading_)
370 void resetResidual_()
374 residual_ = std::make_shared<ResidualType>();
375 setResidualSize_(*residual_);
378 setResidualSize_(constrainedDofs_);
381 constrainedDofs_ = 0.0;
385 template <
class PartialReassembler = DefaultPartialReassembler>
386 void resetJacobian_(
const PartialReassembler *partialReassembler =
nullptr)
390 jacobian_ = std::make_shared<JacobianMatrix>();
391 jacobian_->setBuildMode(JacobianMatrix::random);
392 setJacobianPattern_();
395 if (partialReassembler)
396 partialReassembler->resetJacobian(*
this);
406 template<
typename AssembleElementFunc>
407 void assemble_(AssembleElementFunc&& assembleElement)
const
410 bool succeeded =
false;
415 if (enableMultithreading_)
417 assert(elementSets_.size() > 0);
423 for (
const auto& elements : elementSets_)
427 const auto element = gridView().grid().entity(elements[i]);
428 assembleElement(element);
433 for (
const auto& element : elements(
gridView()))
434 assembleElement(element);
440 catch (NumericalProblem &e)
442 std::cout <<
"rank " <<
gridView().comm().rank()
443 <<
" caught an exception while assembling:" << e.what()
450 succeeded =
gridView().comm().min(succeeded);
454 DUNE_THROW(NumericalProblem,
"A process did not succeed in linearizing the system");
459 void setProblemTime_(
const P& p,
const Scalar t)
460 { setProblemTimeImpl_(p, t, 0); }
463 auto setProblemTimeImpl_(
const P& p,
const Scalar t,
int) ->
decltype(p.setTime(0))
467 void setProblemTimeImpl_(
const P& p,
const Scalar t,
long)
470 std::shared_ptr<const Experimental::MultiStageMethod<Scalar>> timeSteppingMethod_;
471 std::vector<ResidualType> spatialOperatorEvaluations_;
472 std::vector<ResidualType> temporalOperatorEvaluations_;
474 std::shared_ptr<const StageParams> stageParams_;
477 std::shared_ptr<const Problem> problem_;
480 std::shared_ptr<const GridGeometry> gridGeometry_;
483 std::shared_ptr<GridVariables> gridVariables_;
489 std::shared_ptr<JacobianMatrix> jacobian_;
490 std::shared_ptr<ResidualType> residual_;
493 bool enableMultithreading_ =
false;
494 std::deque<std::vector<ElementSeed>> elementSets_;
An assembler for Jacobian and residual contribution per element (cell-centered methods)
Definition: experimental/assembly/cclocalassembler.hh:181
An assembler for Jacobian and residual contribution per element (CVFE methods)
Definition: experimental/assembly/cvfelocalassembler.hh:326
A linear system assembler (residual and Jacobian) for finite volume schemes (box, tpfa,...
Definition: multistagefvassembler.hh:89
const Problem & problem() const
The problem.
Definition: multistagefvassembler.hh:216
void assembleResidual(const SolutionVector &curSol)
compute the residuals using the internal residual
Definition: multistagefvassembler.hh:184
std::size_t numDofs() const
Returns the number of degrees of freedom.
Definition: multistagefvassembler.hh:212
MultiStageFVLocalOperator< LocalResidual > localResidual() const
Create a local residual object (used by the local assembler)
Definition: multistagefvassembler.hh:250
typename Dumux::Detail::NativeDuneVectorType< SolutionVector >::type ResidualType
Definition: multistagefvassembler.hh:106
void resetTimeStep(const SolutionVector &cursol)
Reset the gridVariables.
Definition: multistagefvassembler.hh:262
GetPropType< TypeTag, Properties::JacobianMatrix > JacobianMatrix
Definition: multistagefvassembler.hh:104
ResidualType & residual()
The residual vector (rhs)
Definition: multistagefvassembler.hh:240
void clearStages()
Definition: multistagefvassembler.hh:268
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:335
void setLinearSystem()
The version without arguments uses the default constructor to create the jacobian and residual object...
Definition: multistagefvassembler.hh:191
void prepareStage(SolutionVector &x, StageParams params)
Definition: multistagefvassembler.hh:276
GetPropType< TypeTag, Properties::GridVariables > GridVariables
Definition: multistagefvassembler.hh:108
void updateAfterGridAdaption()
Resizes jacobian and residual and recomputes colors.
Definition: multistagefvassembler.hh:204
const GridView & gridView() const
The gridview.
Definition: multistagefvassembler.hh:224
GridGeo GridGeometry
Definition: multistagefvassembler.hh:110
const SolutionVector & prevSol() const
The solution of the previous time step.
Definition: multistagefvassembler.hh:244
GridVariables & gridVariables()
The global grid variables.
Definition: multistagefvassembler.hh:228
GetPropType< TypeTag, Properties::Problem > Problem
Definition: multistagefvassembler.hh:111
void updateGridVariables(const SolutionVector &cursol)
Update the grid variables.
Definition: multistagefvassembler.hh:256
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: multistagefvassembler.hh:102
GetPropType< TypeTag, Properties::SolutionVector > SolutionVector
Definition: multistagefvassembler.hh:105
bool isImplicit() const
Definition: multistagefvassembler.hh:338
const GridVariables & gridVariables() const
The global grid variables.
Definition: multistagefvassembler.hh:232
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:220
JacobianMatrix & jacobian()
The jacobian matrix.
Definition: multistagefvassembler.hh:236
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:420
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
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
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.
constexpr Box box
Definition: method.hh:147
Definition: multistagefvassembler.hh:45
typename LocalAssemblerChooser< typename GetPropType< TypeTag, Properties::GridGeometry >::DiscretizationMethod >::template type< TypeTag, Impl, diffMethod > LocalAssemblerChooser_t
Definition: multistagefvassembler.hh:74
Definition: experimental/assembly/cclocalassembler.hh:36
bool supportsMultithreading(const GridView &gridView)
Definition: gridcapabilities.hh:74
auto computeColoring(const GridGeometry &gg, int verbosity=1)
Compute iterable lists of element seeds partitioned by color.
Definition: coloring.hh:239
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:59
Definition: multistagefvassembler.hh:48
Traits specifying if a given discretization tag supports coloring.
Definition: coloring.hh:292
Type traits to be used with vector types.