13#ifndef DUMUX_FC_LOCAL_ASSEMBLER_HH
14#define DUMUX_FC_LOCAL_ASSEMBLER_HH
16#include <dune/grid/common/gridenums.hh>
34 template<
class... Args>
38template<
class T,
class Default>
52template<
class TypeTag,
class Assembler,
class Implementation,
bool implicit>
66 using ParentType::ParentType;
78 template <
class Res
idualVector,
class PartialReassembler = DefaultPartialReassembler,
class CouplingFunction = Detail::NoOpFunctor>
81 const CouplingFunction& maybeAssembleCouplingBlocks = CouplingFunction{})
83 static_assert(!std::decay_t<
decltype(this->
asImp_().problem())>::enableInternalDirichletConstraints(),
84 "Internal Dirichlet constraints are currently not implemented for face-centered staggered models!");
86 this->
asImp_().bindLocalViews();
87 const auto& gridGeometry = this->
asImp_().problem().gridGeometry();
88 const auto eIdxGlobal = gridGeometry.elementMapper().index(this->
element());
89 if (partialReassembler
92 const auto residual = this->
asImp_().evalLocalResidual();
93 for (
const auto& scv : scvs(this->
fvGeometry()))
94 res[scv.dofIndex()] += residual[scv.localDofIndex()];
97 maybeAssembleCouplingBlocks(residual);
101 const auto residual = this->
asImp_().assembleJacobianAndResidualImpl(jac, gridVariables, partialReassembler);
103 if (this->
element().partitionType() == Dune::InteriorEntity)
105 for (
const auto& scv : scvs(this->
fvGeometry()))
106 res[scv.dofIndex()] += residual[scv.localDofIndex()];
111 for (
const auto& scv : scvs(this->
fvGeometry()))
113 const auto& facet = this->
element().template subEntity <1> (scv.indexInElement());
116 if (facet.partitionType() == Dune::BorderEntity)
117 res[scv.dofIndex()] += residual[scv.localDofIndex()];
123 const auto idx = scv.dofIndex();
125 for (
int i = 0; i < jac[idx][idx].size(); ++i)
126 jac[idx][idx][i][i] = 1.0;
133 maybeAssembleCouplingBlocks(residual);
136 DUNE_THROW(Dune::NotImplemented,
"Ghost elements not supported");
139 auto applyDirichlet = [&] (
const auto& scvI,
140 const auto& dirichletValues,
144 res[scvI.dofIndex()][eqIdx] = this->
curElemVolVars()[scvI].priVars()[pvIdx] - dirichletValues[pvIdx];
146 auto& row = jac[scvI.dofIndex()];
147 for (
auto col = row.begin(); col != row.end(); ++col)
148 row[col.index()][eqIdx] = 0.0;
150 jac[scvI.dofIndex()][scvI.dofIndex()][eqIdx][pvIdx] = 1.0;
153 if (this->
asImp_().
problem().gridGeometry().dofOnPeriodicBoundary(scvI.dofIndex()))
155 const auto periodicDof = this->
asImp_().problem().gridGeometry().periodicallyMappedDof(scvI.dofIndex());
156 res[periodicDof][eqIdx] = this->
asImp_().curSol()[periodicDof][pvIdx] - dirichletValues[pvIdx];
158 auto& rowP = jac[periodicDof];
159 for (
auto col = rowP.begin(); col != rowP.end(); ++col)
160 row[col.index()][eqIdx] = 0.0;
162 rowP[periodicDof][eqIdx][pvIdx] = 1.0;
166 this->
asImp_().enforceDirichletConstraints(applyDirichlet);
175 this->
asImp_().bindLocalViews();
176 this->
asImp_().assembleJacobianAndResidualImpl(jac, gridVariables);
178 auto applyDirichlet = [&] (
const auto& scvI,
179 const auto& dirichletValues,
183 auto& row = jac[scvI.dofIndex()];
184 for (
auto col = row.begin(); col != row.end(); ++col)
185 row[col.index()][eqIdx] = 0.0;
187 jac[scvI.dofIndex()][scvI.dofIndex()][eqIdx][pvIdx] = 1.0;
190 this->
asImp_().enforceDirichletConstraints(applyDirichlet);
196 template<
class Res
idualVector>
199 this->
asImp_().bindLocalViews();
202 for (
const auto& scv : scvs(this->
fvGeometry()))
203 res[scv.dofIndex()] += residual[scv.localDofIndex()];
205 auto applyDirichlet = [&] (
const auto& scvI,
206 const auto& dirichletValues,
210 res[scvI.dofIndex()][eqIdx] = this->
curElemVolVars()[scvI].priVars()[pvIdx] - dirichletValues[pvIdx];
213 this->
asImp_().enforceDirichletConstraints(applyDirichlet);
219 template<
typename ApplyFunction>
223 this->
asImp_().evalDirichletBoundaries(applyDirichlet);
225 this->
asImp_().enforceInternalDirichletConstraints(applyDirichlet);
231 template<
typename ApplyDirichletFunctionType >
238 for (
const auto& scvf : scvfs(this->
fvGeometry()))
240 if (scvf.isFrontal() && scvf.boundary())
242 const auto bcTypes = this->
elemBcTypes()[scvf.localIndex()];
243 if (bcTypes.hasDirichlet())
245 const auto& scv = this->
fvGeometry().scv(scvf.insideScvIdx());
246 const auto dirichletValues = this->
asImp_().problem().dirichlet(this->
element(), scvf);
249 for (
int eqIdx = 0; eqIdx < numEq; ++eqIdx)
251 static_assert(numEq == 1,
"Not yet implemented for more than one vector-valued primary variable");
252 const int pvIdx = eqIdx;
253 const int componentIdx = scv.dofAxis();
254 if (bcTypes.isDirichlet(componentIdx))
255 applyDirichlet(scv, std::array<Scalar,1>{{dirichletValues[componentIdx]}}, eqIdx, pvIdx);
267 template<
class... Args>
274 template<
class... Args>
287template<
class TypeTag,
class Assembler, DiffMethod diffMethod = DiffMethod::numeric,
bool implicit = true,
class Implementation =
void>
295template<
class TypeTag,
class Assembler,
class Implementation>
299 Detail::NonVoidOrDefault_t<Implementation, FaceCenteredLocalAssembler<TypeTag, Assembler, DiffMethod::numeric, true, Implementation>>,
308 using FVElementGeometry =
typename GridGeometry::LocalView;
309 using SubControlVolume =
typename FVElementGeometry::SubControlVolume;
310 using SubControlVolumeFace =
typename FVElementGeometry::SubControlVolumeFace;
322 using ParentType::ParentType;
330 template <
class PartialReassembler = DefaultPartialReassembler>
353 const auto numElementResiduals =
fvGeometry.numScv();
370 if (!scvf.processorBoundary())
374 const auto evalDerivative = [&] (
const auto& scvI,
const auto& scvJ)
377 for (
int pvIdx = 0; pvIdx < numEq; pvIdx++)
380 const auto& otherElement =
fvGeometry.gridGeometry().element(scvJ.elementIndex());
383 const VolumeVariables origOtherVolVars(curOtherVolVars);
385 auto evalResiduals = [&](Scalar priVar)
388 otherElemSol[scvJ.localDofIndex()][pvIdx] = priVar;
389 curOtherVolVars.update(otherElemSol,
problem, otherElement, scvJ);
390 this->
asImp_().maybeUpdateCouplingContext(scvJ, otherElemSol, pvIdx);
395 evalSource(residual, scvI);
397 if (!this->
assembler().isStationaryProblem())
398 evalStorage(residual, scvI);
400 for (
const auto& scvf : scvfs(
fvGeometry, scvI))
401 evalFlux(residual, scvf);
410 eps_(otherElemSol[scvJ.localDofIndex()][pvIdx], pvIdx), numDiffMethod);
412 const auto updateJacobian = [&]()
414 for (
int eqIdx = 0; eqIdx < numEq; eqIdx++)
420 A[scvI.dofIndex()][scvJ.dofIndex()][eqIdx][pvIdx] += partialDerivs[scvI.localDofIndex()][eqIdx];
424 if (
element.partitionType() == Dune::InteriorEntity)
428 const auto localIdxI = scvI.indexInElement();
429 const auto& facetI =
element.template subEntity <1> (localIdxI);
431 if (facetI.partitionType() == Dune::BorderEntity)
436 curOtherVolVars = origOtherVolVars;
439 otherElemSol[scvJ.localDofIndex()][pvIdx] =
curSol[scvJ.dofIndex()][pvIdx];
440 this->
asImp_().maybeUpdateCouplingContext(scvJ, otherElemSol, pvIdx);
449 evalDerivative(scvI, scvI);
452 const auto& otherScvIndices =
fvGeometry.gridGeometry().connectivityMap()[scvI.index()];
453 for (
const auto globalJ : otherScvIndices)
454 evalDerivative(scvI,
fvGeometry.scv(globalJ));
458 this->
asImp_().maybeEvalAdditionalDomainDerivatives(origResiduals, A, gridVariables);
460 return origResiduals;
470template<
class TypeTag,
class Assembler,
class Implementation>
474 Detail::NonVoidOrDefault_t<Implementation, FaceCenteredLocalAssembler<TypeTag, Assembler, DiffMethod::numeric, false, Implementation>>,
491 using ParentType::ParentType;
499 template <
class PartialReassembler = DefaultPartialReassembler>
503 if (partialReassembler)
504 DUNE_THROW(Dune::NotImplemented,
"partial reassembly for explicit time discretization");
535 const auto dofIdx = scv.dofIndex();
537 const VolumeVariables origVolVars(curVolVars);
540 for (
int pvIdx = 0; pvIdx < numEq; pvIdx++)
544 auto evalStorage = [&](Scalar priVar)
547 elemSol[scv.localDofIndex()][pvIdx] = priVar;
556 eps_(elemSol[scv.localDofIndex()][pvIdx], pvIdx), numDiffMethod);
559 for (
int eqIdx = 0; eqIdx < numEq; eqIdx++)
565 A[dofIdx][dofIdx][eqIdx][pvIdx] += partialDerivs[scv.localDofIndex()][eqIdx];
569 curVolVars = origVolVars;
572 elemSol[scv.localDofIndex()][pvIdx] =
curSol[scv.dofIndex()][pvIdx];
575 return origResiduals;
584template<
class TypeTag,
class Assembler,
class Implementation>
588 FaceCenteredLocalAssembler<TypeTag, Assembler, DiffMethod::analytic, true, Implementation>,
603 using ParentType::ParentType;
611 template <
class PartialReassembler = DefaultPartialReassembler>
615 if (partialReassembler)
616 DUNE_THROW(Dune::NotImplemented,
"partial reassembly for analytic differentiation");
640 const auto dofIdx = scv.dofIndex();
646 if (!this->
assembler().isStationaryProblem())
647 this->
localResidual().addStorageDerivatives(A[dofIdx][dofIdx],
656 this->
localResidual().addSourceDerivatives(A[dofIdx][dofIdx],
667 if (!scvf.boundary())
683 const auto& insideScv =
fvGeometry.scv(scvf.insideScvIdx());
684 if (this->
elemBcTypes()[insideScv.localDofIndex()].hasNeumann())
687 this->
localResidual().addRobinFluxDerivatives(A[insideScv.dofIndex()],
698 return origResiduals;
707template<
class TypeTag,
class Assembler,
class Implementation>
711 FaceCenteredLocalAssembler<TypeTag, Assembler, DiffMethod::analytic, false, Implementation>,
726 using ParentType::ParentType;
734 template <
class PartialReassembler = DefaultPartialReassembler>
738 if (partialReassembler)
739 DUNE_THROW(Dune::NotImplemented,
"partial reassembly for explicit time discretization");
762 const auto dofIdx = scv.dofIndex();
767 this->
localResidual().addStorageDerivatives(A[dofIdx][dofIdx],
775 return origResiduals;
A base class for all local assemblers.
void bindLocalViews()
Definition assembly/fvlocalassemblerbase.hh:173
ElementVolumeVariables & curElemVolVars()
Definition assembly/fvlocalassemblerbase.hh:253
ElementBoundaryTypes & elemBcTypes()
Definition assembly/fvlocalassemblerbase.hh:269
Implementation & asImp_()
Definition assembly/fvlocalassemblerbase.hh:297
ElementVolumeVariables & prevElemVolVars()
Definition assembly/fvlocalassemblerbase.hh:257
ElementResidualVector evalLocalResidual() const
Definition assembly/fvlocalassemblerbase.hh:108
const Problem & problem() const
Definition assembly/fvlocalassemblerbase.hh:229
FVLocalAssemblerBase(const Assembler &assembler, const Element &element, const SolutionVector &curSol)
Definition assembly/fvlocalassemblerbase.hh:61
FVElementGeometry & fvGeometry()
Definition assembly/fvlocalassemblerbase.hh:249
const Assembler & assembler() const
Definition assembly/fvlocalassemblerbase.hh:233
ElementFluxVariablesCache & elemFluxVarsCache()
Definition assembly/fvlocalassemblerbase.hh:261
bool elementIsGhost() const
Definition assembly/fvlocalassemblerbase.hh:241
std::decay_t< decltype(std::declval< Assembler >().localResidual())> LocalResidual
Definition assembly/fvlocalassemblerbase.hh:55
LocalResidual & localResidual()
Definition assembly/fvlocalassemblerbase.hh:265
const Element & element() const
Definition assembly/fvlocalassemblerbase.hh:237
VolumeVariables & getVolVarAccess(GridVolumeVariables &gridVolVars, ElementVolumeVariables &elemVolVars, const SubControlVolume &scv)
Definition assembly/fvlocalassemblerbase.hh:304
ElementResidualVector evalLocalStorageResidual() const
Definition assembly/fvlocalassemblerbase.hh:164
const SolutionVector & curSol() const
Definition assembly/fvlocalassemblerbase.hh:245
typename LocalResidual::ElementResidualVector ElementResidualVector
Definition fclocalassembler.hh:321
ElementResidualVector assembleJacobianAndResidualImpl(JacobianMatrix &A, GridVariables &gridVariables, const PartialReassembler *partialReassembler=nullptr)
Computes the derivatives with respect to the given element and adds them to the global matrix.
Definition fclocalassembler.hh:331
typename ParentType::LocalResidual LocalResidual
Definition fclocalassembler.hh:320
typename LocalResidual::ElementResidualVector ElementResidualVector
Definition fclocalassembler.hh:602
ElementResidualVector assembleJacobianAndResidualImpl(JacobianMatrix &A, GridVariables &gridVariables, const PartialReassembler *partialReassembler=nullptr)
Computes the derivatives with respect to the given element and adds them to the global matrix.
Definition fclocalassembler.hh:612
typename ParentType::LocalResidual LocalResidual
Definition fclocalassembler.hh:601
typename LocalResidual::ElementResidualVector ElementResidualVector
Definition fclocalassembler.hh:490
typename ParentType::LocalResidual LocalResidual
Definition fclocalassembler.hh:489
ElementResidualVector assembleJacobianAndResidualImpl(JacobianMatrix &A, GridVariables &gridVariables, const PartialReassembler *partialReassembler=nullptr)
Computes the derivatives with respect to the given element and adds them to the global matrix.
Definition fclocalassembler.hh:500
typename ParentType::LocalResidual LocalResidual
Definition fclocalassembler.hh:724
typename LocalResidual::ElementResidualVector ElementResidualVector
Definition fclocalassembler.hh:725
ElementResidualVector assembleJacobianAndResidualImpl(JacobianMatrix &A, GridVariables &gridVariables, const PartialReassembler *partialReassembler=nullptr)
Computes the derivatives with respect to the given element and adds them to the global matrix.
Definition fclocalassembler.hh:735
A base class for all local cell-centered assemblers.
Definition fclocalassembler.hh:54
void assembleJacobianAndResidual(JacobianMatrix &jac, ResidualVector &res, GridVariables &gridVariables, const PartialReassembler *partialReassembler, const CouplingFunction &maybeAssembleCouplingBlocks=CouplingFunction{})
Computes the derivatives with respect to the given element and adds them to the global matrix....
Definition fclocalassembler.hh:79
void enforceDirichletConstraints(const ApplyFunction &applyDirichlet)
Enforce Dirichlet constraints.
Definition fclocalassembler.hh:220
void assembleJacobian(JacobianMatrix &jac, GridVariables &gridVariables)
Computes the derivatives with respect to the given element and adds them to the global matrix.
Definition fclocalassembler.hh:173
void maybeUpdateCouplingContext(Args &&...)
Update the coupling context for coupled models.
Definition fclocalassembler.hh:268
void bindLocalViews()
Definition fclocalassembler.hh:68
void evalDirichletBoundaries(ApplyDirichletFunctionType applyDirichlet)
Evaluates Dirichlet boundaries.
Definition fclocalassembler.hh:232
void maybeEvalAdditionalDomainDerivatives(Args &&...)
Update the additional domain derivatives for coupled models.
Definition fclocalassembler.hh:275
void assembleResidual(ResidualVector &res)
Assemble the residual only.
Definition fclocalassembler.hh:197
An assembler for Jacobian and residual contribution per element (Face-centered methods).
Definition fclocalassembler.hh:288
static void partialDerivative(const Function &function, Scalar x0, FunctionEvalType &derivative, const FunctionEvalType &fx0, const int numericDifferenceMethod=1)
Computes the derivative of a function with respect to a function parameter.
Definition numericdifferentiation.hh:50
A helper class for local assemblers using numeric differentiation to determine the epsilon.
Definition numericepsilon.hh:29
detects which entries in the Jacobian have to be recomputed
Definition partialreassembler.hh:420
EntityColor elementColor(size_t idx) const
Definition partialreassembler.hh:491
Defines all properties used in Dumux.
An enum class to define various differentiation methods available in order to compute the derivatives...
An enum class to define the colors of elements and vertices required for partial Jacobian reassembly.
The global face variables class for staggered models.
DiffMethod
Differentiation methods in order to compute the derivatives of the residual i.e. the entries in the j...
Definition diffmethod.hh:25
@ analytic
Definition diffmethod.hh:26
@ numeric
Definition diffmethod.hh:26
@ green
does not need to be reassembled
Definition entitycolor.hh:40
auto elementSolution(const Element &element, const SolutionVector &sol, const GridGeometry &gg) -> std::enable_if_t< GridGeometry::discMethod==DiscretizationMethods::cctpfa||GridGeometry::discMethod==DiscretizationMethods::ccmpfa, CCElementSolution< typename GridGeometry::LocalView, std::decay_t< decltype(std::declval< SolutionVector >()[0])> > >
Make an element solution for cell-centered schemes.
Definition cellcentered/elementsolution.hh:101
T getParamFromGroup(Args &&... args)
A free function to get a parameter from the parameter tree singleton with a model group.
Definition parameters.hh:149
typename GetProp< TypeTag, Property >::type GetPropType
get the type alias defined in the property
Definition propertysystem.hh:296
Distance implementation details.
Definition cvfelocalresidual.hh:25
std::conditional_t<!std::is_same_v< T, void >, T, Default > NonVoidOrDefault_t
Definition fclocalassembler.hh:39
A class for numeric differentiation.
An adapter class for local assemblers using numeric differentiation.
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
Detects which entries in the Jacobian have to be recomputed.
Definition fclocalassembler.hh:33
constexpr void operator()(Args &&...) const
Definition fclocalassembler.hh:35