26#ifndef DUMUX_TRACER_LOCAL_RESIDUAL_HH
27#define DUMUX_TRACER_LOCAL_RESIDUAL_HH
29#include <dune/common/exceptions.hh>
46template<
class TypeTag>
53 using FVElementGeometry =
typename GridGeometry::LocalView;
54 using SubControlVolume =
typename GridGeometry::SubControlVolume;
55 using SubControlVolumeFace =
typename GridGeometry::SubControlVolumeFace;
61 using Element =
typename GridView::template Codim<0>::Entity;
67 static constexpr int numComponents = ModelTraits::numFluidComponents();
68 static constexpr bool useMoles = getPropValue<TypeTag, Properties::UseMoles>();
69 static constexpr int phaseIdx = 0;
72 using ParentType::ParentType;
86 const SubControlVolume& scv,
87 const VolumeVariables& volVars)
const
89 NumEqVector storage(0.0);
95 const Scalar
saturation = max(1e-8, volVars.saturation(phaseIdx));
100 for (
int compIdx = 0; compIdx < numComponents; ++compIdx)
101 storage[compIdx] += volVars.porosity()
102 * volVars.molarDensity(phaseIdx)
103 * volVars.moleFraction(phaseIdx, compIdx)
109 for (
int compIdx = 0; compIdx < numComponents; ++compIdx)
110 storage[compIdx] += volVars.porosity()
111 * volVars.density(phaseIdx)
112 * volVars.massFraction(phaseIdx, compIdx)
131 const Element& element,
132 const FVElementGeometry& fvGeometry,
133 const ElementVolumeVariables& elemVolVars,
134 const SubControlVolumeFace& scvf,
135 const ElementFluxVariablesCache& elemFluxVarsCache)
const
137 FluxVariables fluxVars;
138 fluxVars.init(problem, element, fvGeometry, elemVolVars, scvf, elemFluxVarsCache);
140 NumEqVector flux(0.0);
141 const auto diffusiveFluxes = fluxVars.molecularDiffusionFlux(phaseIdx);
143 static constexpr auto referenceSystemFormulation = FluxVariables::MolecularDiffusionType::referenceSystemFormulation();
147 for (
int compIdx = 0; compIdx < numComponents; ++compIdx)
150 auto upwindTerm = [compIdx](
const auto& volVars)
151 {
return volVars.molarDensity()*volVars.moleFraction(phaseIdx, compIdx); };
154 flux[compIdx] += fluxVars.advectiveFlux(phaseIdx, upwindTerm);
157 flux[compIdx] += diffusiveFluxes[compIdx]/FluidSystem::molarMass(compIdx);
159 flux[compIdx] += diffusiveFluxes[compIdx];
161 DUNE_THROW(Dune::NotImplemented,
"other reference systems than mass and molar averaged are not implemented");
166 for (
int compIdx = 0; compIdx < numComponents; ++compIdx)
169 auto upwindTerm = [compIdx](
const auto& volVars)
170 {
return volVars.density()*volVars.massFraction(phaseIdx, compIdx); };
173 flux[compIdx] += fluxVars.advectiveFlux(phaseIdx, upwindTerm);
176 flux[compIdx] += diffusiveFluxes[compIdx];
178 flux[compIdx] += diffusiveFluxes[compIdx]*FluidSystem::molarMass(compIdx);
180 DUNE_THROW(Dune::NotImplemented,
"other reference systems than mass and molar averaged are not implemented");
184 if constexpr (Deprecated::hasEnableCompositionalDispersion<ModelTraits>())
186 if constexpr (ModelTraits::enableCompositionalDispersion())
188 const auto dispersionFluxes = fluxVars.compositionalDispersionFlux(phaseIdx);
189 for (
int compIdx = 0; compIdx < numComponents; ++compIdx)
191 flux[compIdx] += dispersionFluxes[compIdx];
196 enableCompositionalDispersionMissing_<ModelTraits>();
211 template<
class PartialDerivativeMatrix>
213 const Problem& problem,
214 const Element& element,
215 const FVElementGeometry& fvGeometry,
216 const VolumeVariables& curVolVars,
217 const SubControlVolume& scv)
const
223 const auto saturation = max(1e-8, curVolVars.saturation(phaseIdx));
225 const auto porosity = curVolVars.porosity();
226 const auto rho = useMoles ? curVolVars.molarDensity() : curVolVars.density();
229 for (
int compIdx = 0; compIdx < numComponents; ++compIdx)
230 partialDerivatives[compIdx][compIdx] += d_storage;
243 template<
class PartialDerivativeMatrix>
245 const Problem& problem,
246 const Element& element,
247 const FVElementGeometry& fvGeometry,
248 const VolumeVariables& curVolVars,
249 const SubControlVolume& scv)
const
254 template<
class PartialDerivativeMatrices,
class T = TypeTag>
257 const Problem& problem,
258 const Element& element,
259 const FVElementGeometry& fvGeometry,
260 const ElementVolumeVariables& curElemVolVars,
261 const ElementFluxVariablesCache& elemFluxVarsCache,
262 const SubControlVolumeFace& scvf)
const
265 DUNE_THROW(Dune::NotImplemented,
"Analytic flux differentiation only implemented for tpfa");
268 auto rho = [](
const VolumeVariables& volVars)
269 {
return useMoles ? volVars.molarDensity() : volVars.density(); };
272 const auto volFlux = problem.spatialParams().volumeFlux(element, fvGeometry, curElemVolVars, scvf);
275 static const Scalar upwindWeight = getParam<Scalar>(
"Flux.UpwindWeight");
278 const auto& insideVolVars = curElemVolVars[scvf.insideScvIdx()];
279 const auto& outsideVolVars = curElemVolVars[scvf.outsideScvIdx()];
281 const Scalar insideWeight = std::signbit(volFlux) ? (1.0 - upwindWeight) : upwindWeight;
282 const Scalar outsideWeight = 1.0 - insideWeight;
283 const auto advDerivII = volFlux*rho(insideVolVars)*insideWeight;
284 const auto advDerivIJ = volFlux*rho(outsideVolVars)*outsideWeight;
287 static constexpr auto referenceSystemFormulation = FluxVariables::MolecularDiffusionType::referenceSystemFormulation();
288 const auto& fluxCache = elemFluxVarsCache[scvf];
289 const Scalar rhoInside =
massOrMolarDensity(insideVolVars, referenceSystemFormulation, phaseIdx);
290 const Scalar rhoOutside =
massOrMolarDensity(outsideVolVars, referenceSystemFormulation, phaseIdx);
293 for (
int compIdx = 0; compIdx < numComponents; ++compIdx)
296 Scalar diffDeriv = 0.0;
299 diffDeriv = useMoles ?
massOrMolarDensity*fluxCache.diffusionTij(phaseIdx, compIdx)/FluidSystem::molarMass(compIdx)
305 :
massOrMolarDensity*fluxCache.diffusionTij(phaseIdx, compIdx)*FluidSystem::molarMass(compIdx);
308 DUNE_THROW(Dune::NotImplemented,
"other reference systems than mass and molar averaged are not implemented");
310 derivativeMatrices[scvf.insideScvIdx()][compIdx][compIdx] += (advDerivII + diffDeriv);
311 if (!scvf.boundary())
312 derivativeMatrices[scvf.outsideScvIdx()][compIdx][compIdx] += (advDerivIJ - diffDeriv);
316 template<
class JacobianMatrix,
class T = TypeTag>
319 const Problem& problem,
320 const Element& element,
321 const FVElementGeometry& fvGeometry,
322 const ElementVolumeVariables& curElemVolVars,
323 const ElementFluxVariablesCache& elemFluxVarsCache,
324 const SubControlVolumeFace& scvf)
const
328 auto rho = [](
const VolumeVariables& volVars)
329 {
return useMoles ? volVars.molarDensity() : volVars.density(); };
332 const auto volFlux = problem.spatialParams().volumeFlux(element, fvGeometry, curElemVolVars, scvf);
335 static const Scalar upwindWeight = getParamFromGroup<Scalar>(problem.paramGroup(),
"Flux.UpwindWeight");
338 const auto& insideVolVars = curElemVolVars[scvf.insideScvIdx()];
339 const auto& outsideVolVars = curElemVolVars[scvf.outsideScvIdx()];
341 const auto insideWeight = std::signbit(volFlux) ? (1.0 - upwindWeight) : upwindWeight;
342 const auto outsideWeight = 1.0 - insideWeight;
343 const auto advDerivII = volFlux*rho(insideVolVars)*insideWeight;
344 const auto advDerivIJ = volFlux*rho(outsideVolVars)*outsideWeight;
347 static constexpr auto referenceSystemFormulation = FluxVariables::MolecularDiffusionType::referenceSystemFormulation();
349 const auto ti = DiffusionType::calculateTransmissibilities(problem,
354 elemFluxVarsCache[scvf],
356 const auto& insideScv = fvGeometry.scv(scvf.insideScvIdx());
357 const auto& outsideScv = fvGeometry.scv(scvf.outsideScvIdx());
359 for (
int compIdx = 0; compIdx < numComponents; ++compIdx)
361 for (
const auto& scv : scvs(fvGeometry))
364 auto diffDeriv = 0.0;
366 diffDeriv += useMoles ? ti[compIdx][scv.indexInElement()]/FluidSystem::molarMass(compIdx)
367 : ti[compIdx][scv.indexInElement()];
369 diffDeriv += useMoles ? ti[compIdx][scv.indexInElement()]
370 : ti[compIdx][scv.indexInElement()]*FluidSystem::molarMass(compIdx);
372 DUNE_THROW(Dune::NotImplemented,
"other reference systems than mass and molar averaged are not implemented");
373 A[insideScv.dofIndex()][scv.dofIndex()][compIdx][compIdx] += diffDeriv;
374 A[outsideScv.dofIndex()][scv.dofIndex()][compIdx][compIdx] -= diffDeriv;
377 A[insideScv.dofIndex()][insideScv.dofIndex()][compIdx][compIdx] += advDerivII;
378 A[insideScv.dofIndex()][outsideScv.dofIndex()][compIdx][compIdx] += advDerivIJ;
379 A[outsideScv.dofIndex()][outsideScv.dofIndex()][compIdx][compIdx] -= advDerivII;
380 A[outsideScv.dofIndex()][insideScv.dofIndex()][compIdx][compIdx] -= advDerivIJ;
384 template<
class PartialDerivativeMatrices>
386 const Problem& problem,
387 const Element& element,
388 const FVElementGeometry& fvGeometry,
389 const ElementVolumeVariables& curElemVolVars,
390 const ElementFluxVariablesCache& elemFluxVarsCache,
391 const SubControlVolumeFace& scvf)
const
395 curElemVolVars, elemFluxVarsCache, scvf);
398 template<
class PartialDerivativeMatrices>
400 const Problem& problem,
401 const Element& element,
402 const FVElementGeometry& fvGeometry,
403 const ElementVolumeVariables& curElemVolVars,
404 const ElementFluxVariablesCache& elemFluxVarsCache,
405 const SubControlVolumeFace& scvf)
const
411 template <
class T = ModelTraits>
412 [[deprecated(
"All compositional models must specifiy if dispersion is enabled."
413 "Please add enableCompositionalDispersion to the ModelTraits in your model header.")]]
414 void enableCompositionalDispersionMissing_()
const {}
A helper to deduce a vector with the same size as numbers of equations.
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
Helper classes to compute the integration elements.
The available discretization methods in Dumux.
The reference frameworks and formulations available for splitting total fluxes into a advective and d...
VolumeVariables::PrimaryVariables::value_type massOrMolarDensity(const VolumeVariables &volVars, ReferenceSystemFormulation referenceSys, const int phaseIdx)
evaluates the density to be used in Fick's law based on the reference system
Definition: referencesystemformulation.hh:55
typename NumEqVectorTraits< PrimaryVariables >::type NumEqVector
A vector with the same size as numbers of equations This is the default implementation and has to be ...
Definition: numeqvector.hh:46
typename Extrusion< T >::type Extrusion_t
Convenience alias for obtaining the extrusion type.
Definition: extrusion.hh:177
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property
Definition: propertysystem.hh:150
constexpr CCTpfa cctpfa
Definition: method.hh:137
constexpr Box box
Definition: method.hh:139
std::string saturation(int phaseIdx) noexcept
I/O name of saturation for multiphase systems.
Definition: name.hh:43
std::string porosity() noexcept
I/O name of porosity.
Definition: name.hh:139
Scalar volume(Shape shape, Scalar inscribedRadius)
Returns the volume of a given geometry based on the inscribed radius.
Definition: poreproperties.hh:73
Element-wise calculation of the local residual for problems using fully implicit tracer model.
Definition: porousmediumflow/tracer/localresidual.hh:48
NumEqVector computeStorage(const Problem &problem, const SubControlVolume &scv, const VolumeVariables &volVars) const
Evaluates the amount of all conservation quantities (e.g. phase mass) within a sub-control volume.
Definition: porousmediumflow/tracer/localresidual.hh:85
void addRobinFluxDerivatives(PartialDerivativeMatrices &derivativeMatrices, const Problem &problem, const Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &curElemVolVars, const ElementFluxVariablesCache &elemFluxVarsCache, const SubControlVolumeFace &scvf) const
Definition: porousmediumflow/tracer/localresidual.hh:399
std::enable_if_t< GetPropType< T, Properties::GridGeometry >::discMethod !=DiscretizationMethods::box, void > addFluxDerivatives(PartialDerivativeMatrices &derivativeMatrices, const Problem &problem, const Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &curElemVolVars, const ElementFluxVariablesCache &elemFluxVarsCache, const SubControlVolumeFace &scvf) const
Definition: porousmediumflow/tracer/localresidual.hh:256
void addSourceDerivatives(PartialDerivativeMatrix &partialDerivatives, const Problem &problem, const Element &element, const FVElementGeometry &fvGeometry, const VolumeVariables &curVolVars, const SubControlVolume &scv) const
TODO docme!
Definition: porousmediumflow/tracer/localresidual.hh:244
void addStorageDerivatives(PartialDerivativeMatrix &partialDerivatives, const Problem &problem, const Element &element, const FVElementGeometry &fvGeometry, const VolumeVariables &curVolVars, const SubControlVolume &scv) const
TODO docme!
Definition: porousmediumflow/tracer/localresidual.hh:212
NumEqVector computeFlux(const Problem &problem, const Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &elemVolVars, const SubControlVolumeFace &scvf, const ElementFluxVariablesCache &elemFluxVarsCache) const
Evaluates the total flux of all conservation quantities over a face of a sub-control volume.
Definition: porousmediumflow/tracer/localresidual.hh:130
std::enable_if_t< GetPropType< T, Properties::GridGeometry >::discMethod==DiscretizationMethods::box, void > addFluxDerivatives(JacobianMatrix &A, const Problem &problem, const Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &curElemVolVars, const ElementFluxVariablesCache &elemFluxVarsCache, const SubControlVolumeFace &scvf) const
Definition: porousmediumflow/tracer/localresidual.hh:318
void addCCDirichletFluxDerivatives(PartialDerivativeMatrices &derivativeMatrices, const Problem &problem, const Element &element, const FVElementGeometry &fvGeometry, const ElementVolumeVariables &curElemVolVars, const ElementFluxVariablesCache &elemFluxVarsCache, const SubControlVolumeFace &scvf) const
Definition: porousmediumflow/tracer/localresidual.hh:385
Declares all properties used in Dumux.