193 const auto& gradCalc = elemCtx.gradientCalculator();
196 const auto& scvf = elemCtx.stencil(timeIdx).interiorFace(faceIdx);
197 const auto& faceNormal = scvf.normal();
199 const unsigned i = scvf.interiorIndex();
200 const unsigned j = scvf.exteriorIndex();
201 interiorDofIdx_ =
static_cast<short>(i);
202 exteriorDofIdx_ =
static_cast<short>(j);
203 const unsigned focusDofIdx = elemCtx.focusDofIndex();
206 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
207 if (!elemCtx.model().phaseIsConsidered(phaseIdx)) {
208 Valgrind::SetUndefined(potentialGrad_[phaseIdx]);
213 gradCalc.calculateGradient(potentialGrad_[phaseIdx],
217 Valgrind::CheckDefined(potentialGrad_[phaseIdx]);
224 const auto& gIn = elemCtx.problem().gravity(elemCtx, i, timeIdx);
225 const auto& gEx = elemCtx.problem().gravity(elemCtx, j, timeIdx);
227 const auto& intQuantsIn = elemCtx.intensiveQuantities(i, timeIdx);
228 const auto& intQuantsEx = elemCtx.intensiveQuantities(j, timeIdx);
230 const auto& posIn = elemCtx.pos(i, timeIdx);
231 const auto& posEx = elemCtx.pos(j, timeIdx);
232 const auto& posFace = scvf.integrationPos();
235 DimVector distVecIn(posIn);
236 DimVector distVecEx(posEx);
237 DimVector distVecTotal(posEx);
239 distVecIn -= posFace;
240 distVecEx -= posFace;
241 distVecTotal -= posIn;
242 const Scalar absDistTotalSquared = distVecTotal.two_norm2();
243 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
244 if (!elemCtx.model().phaseIsConsidered(phaseIdx)) {
251 if (std::is_same<Scalar, Evaluation>::value ||
252 interiorDofIdx_ ==
static_cast<int>(focusDofIdx))
254 const Evaluation& rhoIn = intQuantsIn.fluidState().density(phaseIdx);
255 pStatIn = -rhoIn * (gIn * distVecIn);
258 const Scalar rhoIn = Toolbox::value(intQuantsIn.fluidState().density(phaseIdx));
259 pStatIn = -rhoIn * (gIn * distVecIn);
266 if (std::is_same<Scalar, Evaluation>::value ||
267 exteriorDofIdx_ ==
static_cast<int>(focusDofIdx))
269 const Evaluation& rhoEx = intQuantsEx.fluidState().density(phaseIdx);
270 pStatEx = -rhoEx * (gEx * distVecEx);
273 const Scalar rhoEx = Toolbox::value(intQuantsEx.fluidState().density(phaseIdx));
274 pStatEx = -rhoEx * (gEx * distVecEx);
281 Dune::FieldVector<Evaluation, dimWorld> f(distVecTotal);
282 f *= (pStatEx - pStatIn) / absDistTotalSquared;
285 for (
unsigned dimIdx = 0; dimIdx < dimWorld; ++dimIdx) {
286 potentialGrad_[phaseIdx][dimIdx] += f[dimIdx];
289 for (
unsigned dimIdx = 0; dimIdx < potentialGrad_[phaseIdx].size(); ++dimIdx) {
290 if (!isfinite(potentialGrad_[phaseIdx][dimIdx])) {
291 throw NumericalProblem(
"Non-finite potential gradient for phase '"
292 + std::string(FluidSystem::phaseName(phaseIdx)) +
"'");
298 Valgrind::SetUndefined(K_);
299 elemCtx.problem().intersectionIntrinsicPermeability(K_, elemCtx, faceIdx, timeIdx);
300 Valgrind::CheckDefined(K_);
302 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
303 if (!elemCtx.model().phaseIsConsidered(phaseIdx)) {
304 Valgrind::SetUndefined(potentialGrad_[phaseIdx]);
309 Evaluation tmp = 0.0;
310 for (
unsigned dimIdx = 0; dimIdx < faceNormal.size(); ++dimIdx) {
311 tmp += potentialGrad_[phaseIdx][dimIdx]*faceNormal[dimIdx];
315 upstreamDofIdx_[phaseIdx] = exteriorDofIdx_;
316 downstreamDofIdx_[phaseIdx] = interiorDofIdx_;
319 upstreamDofIdx_[phaseIdx] = interiorDofIdx_;
320 downstreamDofIdx_[phaseIdx] = exteriorDofIdx_;
325 const auto& up = elemCtx.intensiveQuantities(upstreamDofIdx_[phaseIdx], timeIdx);
326 if (upstreamDofIdx_[phaseIdx] ==
static_cast<int>(focusDofIdx)) {
327 mobility_[phaseIdx] = up.mobility(phaseIdx);
330 mobility_[phaseIdx] = Toolbox::value(up.mobility(phaseIdx));
343 unsigned boundaryFaceIdx,
345 const FluidState& fluidState)
347 const auto& gradCalc = elemCtx.gradientCalculator();
351 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
352 if (!elemCtx.model().phaseIsConsidered(phaseIdx)) {
353 Valgrind::SetUndefined(potentialGrad_[phaseIdx]);
358 gradCalc.calculateBoundaryGradient(potentialGrad_[phaseIdx],
362 Valgrind::CheckDefined(potentialGrad_[phaseIdx]);
365 const auto& scvf = elemCtx.stencil(timeIdx).boundaryFace(boundaryFaceIdx);
366 const auto i = scvf.interiorIndex();
367 interiorDofIdx_ =
static_cast<short>(i);
368 exteriorDofIdx_ = -1;
369 int focusDofIdx = elemCtx.focusDofIndex();
372 const auto& intQuantsIn = elemCtx.intensiveQuantities(i, timeIdx);
373 K_ = intQuantsIn.intrinsicPermeability();
379 const auto& gIn = elemCtx.problem().gravity(elemCtx, i, timeIdx);
380 const auto& posIn = elemCtx.pos(i, timeIdx);
381 const auto& posFace = scvf.integrationPos();
384 DimVector distVecIn(posIn);
385 distVecIn -= posFace;
386 const Scalar absDistSquared = distVecIn.two_norm2();
387 const Scalar gTimesDist = gIn * distVecIn;
389 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
390 if (!elemCtx.model().phaseIsConsidered(phaseIdx)) {
395 const Evaluation rhoIn = intQuantsIn.fluidState().density(phaseIdx);
396 const Evaluation pStatIn = -gTimesDist * rhoIn;
398 Valgrind::CheckDefined(pStatIn);
404 EvalDimVector f(distVecIn);
405 f *= pStatIn / absDistSquared;
408 for (
unsigned dimIdx = 0; dimIdx < dimWorld; ++dimIdx) {
409 potentialGrad_[phaseIdx][dimIdx] += f[dimIdx];
412 Valgrind::CheckDefined(potentialGrad_[phaseIdx]);
413 for (
unsigned dimIdx = 0; dimIdx < potentialGrad_[phaseIdx].size(); ++dimIdx) {
414 if (!isfinite(potentialGrad_[phaseIdx][dimIdx])) {
415 throw NumericalProblem(
"Non-finite potential gradient for phase '"
416 + std::string(FluidSystem::phaseName(phaseIdx)) +
"'");
423 const auto& faceNormal = scvf.normal();
425 const auto& matParams = elemCtx.problem().materialLawParams(elemCtx, i, timeIdx);
427 std::array<Scalar, numPhases> kr;
428 MaterialLaw::relativePermeabilities(kr, matParams, fluidState);
429 Valgrind::CheckDefined(kr);
431 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
432 if (!elemCtx.model().phaseIsConsidered(phaseIdx)) {
436 Evaluation tmp = 0.0;
437 for (
unsigned dimIdx = 0; dimIdx < faceNormal.size(); ++dimIdx) {
438 tmp += potentialGrad_[phaseIdx][dimIdx] * faceNormal[dimIdx];
442 upstreamDofIdx_[phaseIdx] = exteriorDofIdx_;
443 downstreamDofIdx_[phaseIdx] = interiorDofIdx_;
446 upstreamDofIdx_[phaseIdx] = interiorDofIdx_;
447 downstreamDofIdx_[phaseIdx] = exteriorDofIdx_;
451 if (upstreamDofIdx_[phaseIdx] < 0) {
452 if (interiorDofIdx_ == focusDofIdx) {
453 mobility_[phaseIdx] = kr[phaseIdx] / fluidState.viscosity(phaseIdx);
456 mobility_[phaseIdx] = Toolbox::value(kr[phaseIdx]) /
457 Toolbox::value(fluidState.viscosity(phaseIdx));
460 else if (upstreamDofIdx_[phaseIdx] != focusDofIdx) {
461 mobility_[phaseIdx] = Toolbox::value(intQuantsIn.mobility(phaseIdx));
464 mobility_[phaseIdx] = intQuantsIn.mobility(phaseIdx);
466 Valgrind::CheckDefined(mobility_[phaseIdx]);
478 const auto& scvf = elemCtx.stencil(timeIdx).interiorFace(scvfIdx);
479 const DimVector& normal = scvf.normal();
480 Valgrind::CheckDefined(normal);
482 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
483 filterVelocity_[phaseIdx] = 0.0;
484 volumeFlux_[phaseIdx] = 0.0;
485 if (!elemCtx.model().phaseIsConsidered(phaseIdx)) {
489 asImp_().calculateFilterVelocity_(phaseIdx);
490 Valgrind::CheckDefined(filterVelocity_[phaseIdx]);
492 volumeFlux_[phaseIdx] = 0.0;
493 for (
unsigned i = 0; i < normal.size(); ++i) {
494 volumeFlux_[phaseIdx] += filterVelocity_[phaseIdx][i] * normal[i];
506 unsigned boundaryFaceIdx,
509 const auto& scvf = elemCtx.stencil(timeIdx).boundaryFace(boundaryFaceIdx);
510 const DimVector& normal = scvf.normal();
511 Valgrind::CheckDefined(normal);
513 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
514 if (!elemCtx.model().phaseIsConsidered(phaseIdx)) {
515 filterVelocity_[phaseIdx] = 0.0;
516 volumeFlux_[phaseIdx] = 0.0;
520 asImp_().calculateFilterVelocity_(phaseIdx);
521 Valgrind::CheckDefined(filterVelocity_[phaseIdx]);
522 volumeFlux_[phaseIdx] = 0.0;
523 for (
unsigned i = 0; i < normal.size(); ++i) {
524 volumeFlux_[phaseIdx] += filterVelocity_[phaseIdx][i] * normal[i];