12#ifndef DUMUX_FV_ASSEMBLER_HH
13#define DUMUX_FV_ASSEMBLER_HH
21#include <dune/istl/matrixindexset.hh>
48template<
class DiscretizationMethod>
54 template<
class TypeTag,
class Impl, DiffMethod diffMethod,
bool isImplicit>
61 template<
class TypeTag,
class Impl, DiffMethod diffMethod,
bool isImplicit>
68 template<
class TypeTag,
class Impl, DiffMethod diffMethod,
bool isImplicit>
75 template<
class TypeTag,
class Impl, DiffMethod diffMethod,
bool isImplicit>
79template<
class TypeTag,
class Impl, DiffMethod diffMethod,
bool isImplicit>
82>::template type<TypeTag, Impl, diffMethod, isImplicit>;
90{
return Dune::Std::is_detected<ProblemConstraintsDetector, P>::value; }
104template<
class TypeTag, DiffMethod diffMethod,
bool isImplicit = true,
class LocalRes
idual = GetPropType<TypeTag, Properties::LocalRes
idual>>
108 using GridView =
typename GridGeo::GridView;
109 using Element =
typename GridView::template Codim<0>::Entity;
110 using ElementSeed =
typename GridView::Grid::template Codim<0>::EntitySeed;
141 , isStationaryProblem_(true)
143 static_assert(isImplicit,
"Explicit assembler for stationary problem doesn't make sense!");
149 maybeComputeColors_();
160 std::shared_ptr<const TimeLoop> timeLoop,
165 , timeLoop_(timeLoop)
167 , isStationaryProblem_(!timeLoop)
174 maybeComputeColors_();
181 template<
class PartialReassembler = DefaultPartialReassembler>
184 checkAssemblerState_();
185 resetJacobian_(partialReassembler);
188 assemble_([&](
const Element& element)
190 LocalAssembler localAssembler(*
this, element, curSol);
191 localAssembler.assembleJacobianAndResidual(*jacobian_, *residual_, *gridVariables_, partialReassembler);
194 enforcePeriodicConstraints_(*jacobian_, *residual_, curSol, *gridGeometry_);
196 auto applyDirichletConstraint = [&] (
const auto& dofIdx,
201 (*residual_)[dofIdx][eqIdx] = curSol[dofIdx][pvIdx] - values[pvIdx];
203 auto& row = (*jacobian_)[dofIdx];
204 for (
auto col = row.begin(); col != row.end(); ++col)
205 row[col.index()][eqIdx] = 0.0;
207 (*jacobian_)[dofIdx][dofIdx][eqIdx][pvIdx] = 1.0;
209 enforceProblemConstraints_(*problem_, *gridGeometry_, applyDirichletConstraint);
217 checkAssemblerState_();
220 assemble_([&](
const Element& element)
222 LocalAssembler localAssembler(*
this, element, curSol);
223 localAssembler.assembleJacobian(*jacobian_, *gridVariables_);
226 enforcePeriodicConstraints_(*jacobian_, curSol, *gridGeometry_);
228 auto applyDirichletConstraint = [&] (
const auto& dofIdx,
233 auto& row = (*jacobian_)[dofIdx];
234 for (
auto col = row.begin(); col != row.end(); ++col)
235 row[col.index()][eqIdx] = 0.0;
237 (*jacobian_)[dofIdx][dofIdx][eqIdx][pvIdx] = 1.0;
239 enforceProblemConstraints_(*problem_, *gridGeometry_, applyDirichletConstraint);
252 checkAssemblerState_();
254 assemble_([&](
const Element& element)
256 LocalAssembler localAssembler(*
this, element, curSol);
257 localAssembler.assembleResidual(r);
260 enforcePeriodicConstraints_(r, curSol, *gridGeometry_);
262 auto applyDirichletConstraint = [&] (
const auto& dofIdx,
267 r[dofIdx][eqIdx] = curSol[dofIdx][pvIdx] - values[pvIdx];
269 enforceProblemConstraints_(*problem_, *gridGeometry_, applyDirichletConstraint);
278 std::shared_ptr<ResidualType> r)
284 if (jacobian_->buildMode() == JacobianMatrix::BuildMode::unknown)
285 jacobian_->setBuildMode(JacobianMatrix::random);
286 else if (jacobian_->buildMode() != JacobianMatrix::BuildMode::random)
287 DUNE_THROW(Dune::NotImplemented,
"Only BCRS matrices with random build mode are supported at the moment");
290 setJacobianPattern_();
299 jacobian_ = std::make_shared<JacobianMatrix>();
300 jacobian_->setBuildMode(JacobianMatrix::random);
301 residual_ = std::make_shared<ResidualType>();
304 setJacobianPattern_();
313 setJacobianPattern_();
314 maybeComputeColors_();
319 {
return gridGeometry_->numDofs(); }
323 {
return *problem_; }
327 {
return *gridGeometry_; }
335 {
return *gridVariables_; }
339 {
return *gridVariables_; }
343 {
return *jacobian_; }
347 {
return *residual_; }
351 {
return *prevSol_; }
358 { timeLoop_ = timeLoop; isStationaryProblem_ = !
static_cast<bool>(timeLoop); }
371 {
return isStationaryProblem_; }
377 {
return LocalResidual(problem_.get(), timeLoop_.get()); }
399 void setJacobianPattern_()
409 occupationPattern.exportIdx(*jacobian_);
413 void setResidualSize_()
414 { residual_->resize(
numDofs()); }
417 void maybeComputeColors_()
419 if (enableMultithreading_)
424 void resetResidual_()
428 residual_ = std::make_shared<ResidualType>();
436 template <
class PartialReassembler = DefaultPartialReassembler>
437 void resetJacobian_(
const PartialReassembler *partialReassembler =
nullptr)
441 jacobian_ = std::make_shared<JacobianMatrix>();
442 jacobian_->setBuildMode(JacobianMatrix::random);
443 setJacobianPattern_();
446 if (partialReassembler)
447 partialReassembler->resetJacobian(*
this);
453 void checkAssemblerState_()
const
455 if (!isStationaryProblem_ && !prevSol_)
456 DUNE_THROW(Dune::InvalidStateException,
"Assembling instationary problem but previous solution was not set!");
464 template<
typename AssembleElementFunc>
465 void assemble_(AssembleElementFunc&& assembleElement)
const
468 bool succeeded =
false;
473 if (enableMultithreading_)
475 assert(elementSets_.size() > 0);
481 for (
const auto& elements : elementSets_)
485 const auto element = gridView().grid().entity(elements[i]);
486 assembleElement(element);
491 for (
const auto& element : elements(
gridView()))
492 assembleElement(element);
498 catch (NumericalProblem &e)
500 std::cout <<
"rank " <<
gridView().comm().rank()
501 <<
" caught an exception while assembling:" << e.what()
508 succeeded =
gridView().comm().min(succeeded);
512 DUNE_THROW(NumericalProblem,
"A process did not succeed in linearizing the system");
522 const auto& periodicallyMappedDofs = Dumux::Deprecated::ensureRangeOfPeriodicDofs(m.second);
523 if (std::ranges::all_of(periodicallyMappedDofs, [&](
auto second){
return m.first < second; }))
525 for (
const auto& second : periodicallyMappedDofs)
528 res[m.first] += res[second];
529 const auto end = jac[second].end();
530 for (
auto it = jac[second].begin(); it != end; ++it)
531 jac[m.first][it.index()] += (*it);
534 res[second] = curSol[second] - curSol[m.first];
537 auto setMatrixBlock = [] (
auto& matrixBlock,
double diagValue)
539 for (
int eIdx = 0; eIdx < matrixBlock.N(); ++eIdx)
540 matrixBlock[eIdx][eIdx] = diagValue;
543 for (
auto it = jac[second].begin(); it != end; ++it)
545 auto& matrixBlock = *it;
548 assert(matrixBlock.N() == matrixBlock.M());
549 if(it.index() == second)
550 setMatrixBlock(matrixBlock, 1.0);
552 if(it.index() == m.first)
553 setMatrixBlock(matrixBlock, -1.0);
569 const auto& periodicallyMappedDofs = Dumux::Deprecated::ensureRangeOfPeriodicDofs(m.second);
570 if (std::ranges::all_of(periodicallyMappedDofs, [&](
auto second){
return m.first < second; }))
572 for (
const auto& second : periodicallyMappedDofs)
575 const auto end = jac[second].end();
576 for (
auto it = jac[second].begin(); it != end; ++it)
577 jac[m.first][it.index()] += (*it);
580 auto setMatrixBlock = [] (
auto& matrixBlock,
double diagValue)
582 for (
int eIdx = 0; eIdx < matrixBlock.N(); ++eIdx)
583 matrixBlock[eIdx][eIdx] = diagValue;
586 for (
auto it = jac[second].begin(); it != end; ++it)
588 auto& matrixBlock = *it;
591 assert(matrixBlock.N() == matrixBlock.M());
592 if(it.index() == second)
593 setMatrixBlock(matrixBlock, 1.0);
595 if(it.index() == m.first)
596 setMatrixBlock(matrixBlock, -1.0);
612 const auto& periodicallyMappedDofs = Dumux::Deprecated::ensureRangeOfPeriodicDofs(m.second);
613 if (std::ranges::all_of(periodicallyMappedDofs, [&](
auto second){
return m.first < second; }))
615 for (
const auto& second : periodicallyMappedDofs)
618 res[m.first] += res[second];
621 res[second] = curSol[second] - curSol[m.first];
628 template<
class Problem,
class GG,
typename ApplyFunction>
629 void enforceProblemConstraints_(
const Problem&
problem,
const GG&
gridGeometry,
const ApplyFunction& applyDirichletConstraint)
const
633 for (
const auto& constraintData :
problem.constraints())
635 const auto& constraintInfo = constraintData.constraintInfo();
636 const auto& values = constraintData.values();
637 const auto dofIdx = constraintData.dofIndex();
639 for (
int eqIdx = 0; eqIdx < constraintInfo.size(); ++eqIdx)
641 if (constraintInfo.isConstraintEquation(eqIdx))
643 const auto pvIdx = constraintInfo.eqToPriVarIndex(eqIdx);
644 assert(0 <= pvIdx && pvIdx < constraintInfo.size());
645 applyDirichletConstraint(dofIdx, values, eqIdx, pvIdx);
653 std::shared_ptr<const Problem> problem_;
656 std::shared_ptr<const GridGeometry> gridGeometry_;
659 std::shared_ptr<GridVariables> gridVariables_;
662 std::shared_ptr<const TimeLoop> timeLoop_;
668 bool isStationaryProblem_;
671 std::shared_ptr<JacobianMatrix> jacobian_;
672 std::shared_ptr<ResidualType> residual_;
675 bool enableMultithreading_ =
false;
676 std::deque<std::vector<ElementSeed>> elementSets_;
An assembler for Jacobian and residual contribution per element (cell-centered methods).
An assembler for Jacobian and residual contribution per element (CVFE methods).
An assembler for Jacobian and residual contribution per element (cell-centered methods).
Definition assembly/cclocalassembler.hh:124
An assembler for Jacobian and residual contribution per element (CVFE methods).
Definition assembly/cvfelocalassembler.hh:343
void setPreviousSolution(const SolutionVector &u)
Sets the solution from which to start the time integration. Has to be called prior to assembly for ti...
Definition assembly/fvassembler.hh:364
GridGeo GridGeometry
Definition assembly/fvassembler.hh:126
GetPropType< TypeTag, Properties::GridVariables > GridVariables
Definition assembly/fvassembler.hh:124
FVAssembler(std::shared_ptr< const Problem > problem, std::shared_ptr< const GridGeometry > gridGeometry, std::shared_ptr< GridVariables > gridVariables, std::shared_ptr< const TimeLoop > timeLoop, const SolutionVector &prevSol)
The constructor for instationary problems.
Definition assembly/fvassembler.hh:157
GridVariables & gridVariables()
Definition assembly/fvassembler.hh:334
std::size_t numDofs() const
Returns the number of degrees of freedom.
Definition assembly/fvassembler.hh:318
const GridVariables & gridVariables() const
The global grid variables.
Definition assembly/fvassembler.hh:338
LocalResidual localResidual() const
Create a local residual object (used by the local assembler).
Definition assembly/fvassembler.hh:376
GetPropType< TypeTag, Properties::SolutionVector > SolutionVector
Definition assembly/fvassembler.hh:121
void assembleJacobianAndResidual(const SolutionVector &curSol, const PartialReassembler *partialReassembler=nullptr)
Assembles the global Jacobian of the residual and the residual for the current solution.
Definition assembly/fvassembler.hh:182
void updateAfterGridAdaption()
Resizes jacobian and residual and recomputes colors.
Definition assembly/fvassembler.hh:310
const Problem & problem() const
Definition assembly/fvassembler.hh:322
bool isStationaryProblem() const
Whether we are assembling a stationary or instationary problem.
Definition assembly/fvassembler.hh:370
const GridGeometry & gridGeometry() const
Definition assembly/fvassembler.hh:326
void assembleResidual(ResidualType &r, const SolutionVector &curSol) const
assemble a residual r
Definition assembly/fvassembler.hh:250
typename Detail::NativeDuneVectorType< SolutionVector >::type ResidualType
Definition assembly/fvassembler.hh:122
void setTimeLoop(std::shared_ptr< const TimeLoop > timeLoop)
Set time loop for instationary problems.
Definition assembly/fvassembler.hh:357
void resetTimeStep(const SolutionVector &cursol)
Reset the gridVariables.
Definition assembly/fvassembler.hh:390
void assembleJacobian(const SolutionVector &curSol)
Assembles only the global Jacobian of the residual.
Definition assembly/fvassembler.hh:215
void updateGridVariables(const SolutionVector &cursol)
Update the grid variables.
Definition assembly/fvassembler.hh:382
JacobianMatrix & jacobian()
The jacobian matrix.
Definition assembly/fvassembler.hh:342
void setLinearSystem(std::shared_ptr< JacobianMatrix > A, std::shared_ptr< ResidualType > r)
Tells the assembler which jacobian and residual to use. This also resizes the containers to the requi...
Definition assembly/fvassembler.hh:277
GetPropType< TypeTag, Properties::JacobianMatrix > JacobianMatrix
Definition assembly/fvassembler.hh:120
GetPropType< TypeTag, Properties::Problem > Problem
Definition assembly/fvassembler.hh:127
void setLinearSystem()
The version without arguments uses the default constructor to create the jacobian and residual object...
Definition assembly/fvassembler.hh:297
const SolutionVector & prevSol() const
Definition assembly/fvassembler.hh:350
FVAssembler(std::shared_ptr< const Problem > problem, std::shared_ptr< const GridGeometry > gridGeometry, std::shared_ptr< GridVariables > gridVariables)
The constructor for stationary problems.
Definition assembly/fvassembler.hh:134
const GridView & gridView() const
The gridview.
Definition assembly/fvassembler.hh:330
ResidualType & residual()
The residual vector (rhs).
Definition assembly/fvassembler.hh:346
void assembleResidual(const SolutionVector &curSol)
compute the residuals using the internal residual
Definition assembly/fvassembler.hh:243
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition assembly/fvassembler.hh:119
An assembler for Jacobian and residual contribution per element (Face-centered methods).
Definition fclocalassembler.hh:328
detects which entries in the Jacobian have to be recomputed
Definition partialreassembler.hh:418
Manages the handling of time dependent problems.
Definition common/timeloop.hh:84
Coloring schemes for shared-memory-parallel assembly.
Defines all properties used in Dumux.
Manages the handling of time dependent problems.
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 (face-centered staggered 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:29
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.
Definition cellcentered/mpfa/elementvolumevariables.hh:24
Definition elementinterpolationpointdatacache.hh:21
Definition cvfelocalresidual.hh:25
constexpr bool hasGlobalConstraints()
Definition assembly/fvassembler.hh:89
constexpr bool hasPeriodicDofMap()
Definition periodic.hh:24
decltype(std::declval< P >().constraints()) ProblemConstraintsDetector
helper struct detecting if problem has a constraints() function
Definition assembly/fvassembler.hh:86
typename LocalAssemblerChooser< typename GetPropType< TypeTag, Properties::GridGeometry >::DiscretizationMethod >::template type< TypeTag, Impl, diffMethod, isImplicit > LocalAssemblerChooser_t
Definition assembly/fvassembler.hh:80
constexpr Box box
Definition method.hh:176
bool supportsMultithreading(const GridView &gridView)
Definition gridcapabilities.hh:36
const Scalar PengRobinsonMixture< Scalar, StaticParameters >::u
Definition pengrobinsonmixture.hh:138
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.
Type traits to detect periodicity support.
CCLocalAssembler< TypeTag, Impl, diffMethod, isImplicit > type
Definition assembly/fvassembler.hh:62
CCLocalAssembler< TypeTag, Impl, diffMethod, isImplicit > type
Definition assembly/fvassembler.hh:69
CVFELocalAssembler< TypeTag, Impl, diffMethod, isImplicit > type
Definition assembly/fvassembler.hh:55
FaceCenteredLocalAssembler< TypeTag, Impl, diffMethod, isImplicit > type
Definition assembly/fvassembler.hh:76
Definition assembly/fvassembler.hh:49
typename NativeDuneVectorTypeImpl< V, Dune::Std::is_detected< Detail::DuneVectors::StateDetector, V >{} >::type type
Definition dunevectors.hh:57
Traits specifying if a given discretization tag supports coloring.
Definition coloring.hh:296
Type traits to be used with vector types.