24#ifndef DUMUX_FV_ASSEMBLER_HH
25#define DUMUX_FV_ASSEMBLER_HH
32#include <dune/istl/matrixindexset.hh>
52template<
class DiscretizationMethod>
58 template<
class TypeTag,
class Impl, DiffMethod diffMethod,
bool isImplicit>
65 template<
class TypeTag,
class Impl, DiffMethod diffMethod,
bool isImplicit>
72 template<
class TypeTag,
class Impl, DiffMethod diffMethod,
bool isImplicit>
79 template<
class TypeTag,
class Impl, DiffMethod diffMethod,
bool isImplicit>
83template<
class TypeTag,
class Impl, DiffMethod diffMethod,
bool isImplicit>
86>::template type<TypeTag, Impl, diffMethod, isImplicit>;
99template<
class TypeTag, DiffMethod diffMethod,
bool isImplicit = true>
103 using GridView =
typename GridGeo::GridView;
105 using Element =
typename GridView::template Codim<0>::Entity;
106 using ElementSeed =
typename GridView::Grid::template Codim<0>::EntitySeed;
136 , isStationaryProblem_(true)
138 static_assert(isImplicit,
"Explicit assembler for stationary problem doesn't make sense!");
141 && getParam<bool>(
"Assembly.Multithreading",
true);
143 maybeComputeColors_();
154 std::shared_ptr<const TimeLoop> timeLoop,
159 , timeLoop_(timeLoop)
161 , isStationaryProblem_(!timeLoop)
165 && getParam<bool>(
"Assembly.Multithreading",
true);
167 maybeComputeColors_();
174 template<
class PartialReassembler = DefaultPartialReassembler>
177 checkAssemblerState_();
178 resetJacobian_(partialReassembler);
181 assemble_([&](
const Element& element)
183 LocalAssembler localAssembler(*
this, element, curSol);
184 localAssembler.assembleJacobianAndResidual(*jacobian_, *residual_, *gridVariables_, partialReassembler);
187 enforcePeriodicConstraints_(*jacobian_, *residual_, curSol, *gridGeometry_);
195 checkAssemblerState_();
198 assemble_([&](
const Element& element)
200 LocalAssembler localAssembler(*
this, element, curSol);
201 localAssembler.assembleJacobianAndResidual(*jacobian_, *gridVariables_);
215 checkAssemblerState_();
217 assemble_([&](
const Element& element)
219 LocalAssembler localAssembler(*
this, element, curSol);
220 localAssembler.assembleResidual(r);
231 static bool warningIssued =
false;
237 using DM =
typename GridGeometry::VertexMapper;
240 PVHelper vectorHelper(
gridView(), gridGeometry_->vertexMapper());
242 vectorHelper.makeNonOverlappingConsistent(
residual);
245 else if (!warningIssued)
248 std::cout <<
"\nWarning: norm calculation adds entries corresponding to\n"
249 <<
"overlapping entities multiple times. Please use the norm\n"
250 <<
"function provided by a linear solver instead." << std::endl;
252 warningIssued =
true;
258 result2 =
gridView().comm().sum(result2);
261 return sqrt(result2);
270 std::shared_ptr<SolutionVector> r)
276 if (jacobian_->buildMode() == JacobianMatrix::BuildMode::unknown)
277 jacobian_->setBuildMode(JacobianMatrix::random);
278 else if (jacobian_->buildMode() != JacobianMatrix::BuildMode::random)
279 DUNE_THROW(Dune::NotImplemented,
"Only BCRS matrices with random build mode are supported at the moment");
282 setJacobianPattern_();
291 jacobian_ = std::make_shared<JacobianMatrix>();
292 jacobian_->setBuildMode(JacobianMatrix::random);
293 residual_ = std::make_shared<SolutionVector>();
296 setJacobianPattern_();
305 setJacobianPattern_();
306 maybeComputeColors_();
312 [[deprecated(
"Use updateAfterGridAdaption. Will be removed after release 3.5.")]]
320 const auto occupationPattern = getJacobianPattern<isImplicit>(
gridGeometry());
323 occupationPattern.exportIdx(*jacobian_);
326 maybeComputeColors_();
330 [[deprecated(
"Use updateAfterGridAdaption. Will be removed after release 3.5.")]]
332 { residual_->resize(
numDofs()); }
336 {
return gridGeometry_->numDofs(); }
340 {
return *problem_; }
344 {
return *gridGeometry_; }
352 {
return *gridVariables_; }
356 {
return *gridVariables_; }
360 {
return *jacobian_; }
364 {
return *residual_; }
368 {
return *prevSol_; }
375 { timeLoop_ = timeLoop; isStationaryProblem_ = !
static_cast<bool>(timeLoop); }
388 {
return isStationaryProblem_; }
394 {
return LocalResidual(problem_.get(), timeLoop_.get()); }
416 void setJacobianPattern_()
423 const auto occupationPattern = getJacobianPattern<isImplicit>(
gridGeometry());
426 occupationPattern.exportIdx(*jacobian_);
430 void setResidualSize_()
431 { residual_->resize(
numDofs()); }
434 void maybeComputeColors_()
436 if (enableMultithreading_)
441 void resetResidual_()
445 residual_ = std::make_shared<SolutionVector>();
453 template <
class PartialReassembler = DefaultPartialReassembler>
454 void resetJacobian_(
const PartialReassembler *partialReassembler =
nullptr)
458 jacobian_ = std::make_shared<JacobianMatrix>();
459 jacobian_->setBuildMode(JacobianMatrix::random);
460 setJacobianPattern_();
463 if (partialReassembler)
464 partialReassembler->resetJacobian(*
this);
470 void checkAssemblerState_()
const
472 if (!isStationaryProblem_ && !prevSol_)
473 DUNE_THROW(Dune::InvalidStateException,
"Assembling instationary problem but previous solution was not set!");
481 template<
typename AssembleElementFunc>
482 void assemble_(AssembleElementFunc&& assembleElement)
const
485 bool succeeded =
false;
490 if (enableMultithreading_)
492 assert(elementSets_.size() > 0);
498 for (
const auto& elements : elementSets_)
502 const auto element = gridView().grid().entity(elements[i]);
503 assembleElement(element);
508 for (
const auto& element : elements(
gridView()))
509 assembleElement(element);
515 catch (NumericalProblem &e)
517 std::cout <<
"rank " <<
gridView().comm().rank()
518 <<
" caught an exception while assembling:" << e.what()
525 succeeded =
gridView().comm().min(succeeded);
529 DUNE_THROW(NumericalProblem,
"A process did not succeed in linearizing the system");
532 template<
class GG> std::enable_if_t<GG::discMethod == DiscretizationMethods::box, void>
537 if (m.first < m.second)
540 res[m.first] += res[m.second];
541 const auto end = jac[m.second].end();
542 for (
auto it = jac[m.second].begin(); it != end; ++it)
543 jac[m.first][it.index()] += (*it);
546 res[m.second] = curSol[m.second] - curSol[m.first];
547 for (
auto it = jac[m.second].begin(); it != end; ++it)
548 (*it) = it.index() == m.second ? 1.0 : it.index() == m.first ? -1.0 : 0.0;
553 template<
class GG> std::enable_if_t<GG::discMethod != DiscretizationMethods::box, void>
554 enforcePeriodicConstraints_(
JacobianMatrix& jac, SolutionVector& res,
const SolutionVector& curSol,
const GG&
gridGeometry) {}
557 std::shared_ptr<const Problem> problem_;
560 std::shared_ptr<const GridGeometry> gridGeometry_;
563 std::shared_ptr<GridVariables> gridVariables_;
566 std::shared_ptr<const TimeLoop> timeLoop_;
569 const SolutionVector* prevSol_ =
nullptr;
572 bool isStationaryProblem_;
575 std::shared_ptr<JacobianMatrix> jacobian_;
576 std::shared_ptr<SolutionVector> residual_;
579 bool enableMultithreading_ =
false;
580 std::deque<std::vector<ElementSeed>> elementSets_;
An enum class to define various differentiation methods available in order to compute the derivatives...
An assembler for Jacobian and residual contribution per element (cell-centered methods)
An assembler for Jacobian and residual contribution per element (box method)
Coloring schemes for shared-memory-parallel assembly.
An assembler for Jacobian and residual contribution per element (face-centered staggered methods)
Helper function to generate Jacobian pattern for different discretization methods.
The available discretization methods in Dumux.
Provides a helper class for nonoverlapping decomposition.
Parallel for loop (multithreading)
constexpr bool isSerial()
Checking whether the backend is serial.
Definition: multithreading.hh:57
auto computeColoring(const GridGeometry &gg, int verbosity=1)
Compute iterable lists of element seeds partitioned by color.
Definition: coloring.hh:192
void parallelFor(const std::size_t count, const FunctorType &functor)
Definition: parallel_for.hh:173
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property
Definition: propertysystem.hh:150
Distance implementation details.
Definition: fclocalassembler.hh:42
typename LocalAssemblerChooser< typename GetPropType< TypeTag, Properties::GridGeometry >::DiscretizationMethod >::template type< TypeTag, Impl, diffMethod, isImplicit > LocalAssemblerChooser_t
Definition: assembly/fvassembler.hh:86
constexpr Box box
Definition: method.hh:139
An assembler for Jacobian and residual contribution per element (box methods)
Definition: boxlocalassembler.hh:326
An assembler for Jacobian and residual contribution per element (cell-centered methods)
Definition: cclocalassembler.hh:136
Traits specifying if a given discretization tag supports coloring.
Definition: coloring.hh:245
An assembler for Jacobian and residual contribution per element (Face-centered methods)
Definition: fclocalassembler.hh:295
Definition: assembly/fvassembler.hh:53
A linear system assembler (residual and Jacobian) for finite volume schemes (box, tpfa,...
Definition: assembly/fvassembler.hh:101
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: assembly/fvassembler.hh:116
void assembleJacobian(const SolutionVector &curSol)
Assembles only the global Jacobian of the residual.
Definition: assembly/fvassembler.hh:193
void updateAfterGridAdaption()
Resizes jacobian and residual and recomputes colors.
Definition: assembly/fvassembler.hh:302
const GridView & gridView() const
The gridview.
Definition: assembly/fvassembler.hh:347
const Problem & problem() const
The problem.
Definition: assembly/fvassembler.hh:339
void setLinearSystem()
The version without arguments uses the default constructor to create the jacobian and residual object...
Definition: assembly/fvassembler.hh:289
SolutionVector ResidualType
Definition: assembly/fvassembler.hh:122
bool isStationaryProblem() const
Whether we are assembling a stationary or instationary problem.
Definition: assembly/fvassembler.hh:387
GetPropType< TypeTag, Properties::JacobianMatrix > JacobianMatrix
Definition: assembly/fvassembler.hh:117
LocalResidual localResidual() const
Create a local residual object (used by the local assembler)
Definition: assembly/fvassembler.hh:393
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:151
Scalar residualNorm(const SolutionVector &curSol) const
compute the residual and return it's vector norm
Definition: assembly/fvassembler.hh:225
std::size_t numDofs() const
Returns the number of degrees of freedom.
Definition: assembly/fvassembler.hh:335
GridVariables & gridVariables()
The global grid variables.
Definition: assembly/fvassembler.hh:351
JacobianMatrix & jacobian()
The jacobian matrix.
Definition: assembly/fvassembler.hh:359
void assembleResidual(ResidualType &r, const SolutionVector &curSol) const
assemble a residual r
Definition: assembly/fvassembler.hh:213
GetPropType< TypeTag, Properties::Problem > Problem
Definition: assembly/fvassembler.hh:119
void setTimeLoop(std::shared_ptr< const TimeLoop > timeLoop)
Set time loop for instationary problems.
Definition: assembly/fvassembler.hh:374
void setResidualSize()
Resizes the residual.
Definition: assembly/fvassembler.hh:331
void assembleResidual(const SolutionVector &curSol)
compute the residuals using the internal residual
Definition: assembly/fvassembler.hh:206
GetPropType< TypeTag, Properties::GridVariables > GridVariables
Definition: assembly/fvassembler.hh:120
void setJacobianPattern()
Resizes the jacobian and sets the jacobian' sparsity pattern.
Definition: assembly/fvassembler.hh:313
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:381
const GridGeometry & gridGeometry() const
The global finite volume geometry.
Definition: assembly/fvassembler.hh:343
void resetTimeStep(const SolutionVector &cursol)
Reset the gridVariables.
Definition: assembly/fvassembler.hh:407
const GridVariables & gridVariables() const
The global grid variables.
Definition: assembly/fvassembler.hh:355
void setLinearSystem(std::shared_ptr< JacobianMatrix > A, std::shared_ptr< SolutionVector > r)
Tells the assembler which jacobian and residual to use. This also resizes the containers to the requi...
Definition: assembly/fvassembler.hh:269
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:175
GridGeo GridGeometry
Definition: assembly/fvassembler.hh:118
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:129
SolutionVector & residual()
The residual vector (rhs)
Definition: assembly/fvassembler.hh:363
const SolutionVector & prevSol() const
The solution of the previous time step.
Definition: assembly/fvassembler.hh:367
void updateGridVariables(const SolutionVector &cursol)
Update the grid variables.
Definition: assembly/fvassembler.hh:399
detects which entries in the Jacobian have to be recomputed
Definition: partialreassembler.hh:432
Manages the handling of time dependent problems.
Definition: common/timeloop.hh:69
Definition: parallelhelpers.hh:430
Declares all properties used in Dumux.
Manages the handling of time dependent problems.