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} }; }
267 spatialOperatorEvaluations_.clear();
268 temporalOperatorEvaluations_.clear();
269 stageParams_.reset();
272 template<
class StageParams>
275 stageParams_ = std::move(params);
276 const auto curStage = stageParams_->size() - 1;
282 setProblemTime_(*problem_, stageParams_->timeAtStage(curStage));
285 spatialOperatorEvaluations_.push_back(*residual_);
286 temporalOperatorEvaluations_.push_back(*residual_);
289 assemble_([&](
const auto& element)
291 LocalAssembler localAssembler(*
this, element, *prevSol_);
292 localAssembler.localResidual().spatialWeight(1.0);
293 localAssembler.localResidual().temporalWeight(1.0);
294 localAssembler.assembleCurrentResidual(spatialOperatorEvaluations_.back(), temporalOperatorEvaluations_.back());
299 setProblemTime_(*problem_, stageParams_->timeAtStage(curStage));
302 spatialOperatorEvaluations_.push_back(*residual_);
303 temporalOperatorEvaluations_.push_back(*residual_);
311 {
return timeSteppingMethod_->implicit(); }
317 void setJacobianPattern_()
324 if (timeSteppingMethod_->implicit())
325 getJacobianPattern<true>(
gridGeometry()).exportIdx(*jacobian_);
327 getJacobianPattern<false>(
gridGeometry()).exportIdx(*jacobian_);
335 void maybeComputeColors_()
337 if (enableMultithreading_)
342 void resetResidual_()
346 residual_ = std::make_shared<ResidualType>();
347 setResidualSize_(*residual_);
350 setResidualSize_(constrainedDofs_);
353 constrainedDofs_ = 0.0;
357 template <
class PartialReassembler = DefaultPartialReassembler>
358 void resetJacobian_(
const PartialReassembler *partialReassembler =
nullptr)
362 jacobian_ = std::make_shared<JacobianMatrix>();
363 jacobian_->setBuildMode(JacobianMatrix::random);
364 setJacobianPattern_();
367 if (partialReassembler)
368 partialReassembler->resetJacobian(*
this);
378 template<
typename AssembleElementFunc>
379 void assemble_(AssembleElementFunc&& assembleElement)
const
382 bool succeeded =
false;
387 if (enableMultithreading_)
389 assert(elementSets_.size() > 0);
395 for (
const auto& elements : elementSets_)
399 const auto element = gridView().grid().entity(elements[i]);
400 assembleElement(element);
405 for (
const auto& element : elements(
gridView()))
406 assembleElement(element);
412 catch (NumericalProblem &e)
414 std::cout <<
"rank " <<
gridView().comm().rank()
415 <<
" caught an exception while assembling:" << e.what()
422 succeeded =
gridView().comm().min(succeeded);
426 DUNE_THROW(NumericalProblem,
"A process did not succeed in linearizing the system");
431 void setProblemTime_(
const P& p,
const Scalar t)
432 { setProblemTimeImpl_(p, t, 0); }
435 auto setProblemTimeImpl_(
const P& p,
const Scalar t,
int) ->
decltype(p.setTime(0))
439 void setProblemTimeImpl_(
const P& p,
const Scalar t,
long)
442 std::shared_ptr<const Experimental::MultiStageMethod<Scalar>> timeSteppingMethod_;
443 std::vector<ResidualType> spatialOperatorEvaluations_;
444 std::vector<ResidualType> temporalOperatorEvaluations_;
446 std::shared_ptr<const StageParams> stageParams_;
449 std::shared_ptr<const Problem> problem_;
452 std::shared_ptr<const GridGeometry> gridGeometry_;
455 std::shared_ptr<GridVariables> gridVariables_;
461 std::shared_ptr<JacobianMatrix> jacobian_;
462 std::shared_ptr<ResidualType> residual_;
465 bool enableMultithreading_ =
false;
466 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:265
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:307
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:273
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:310
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:29
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.