1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
|
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
Consult the COPYING file in the top-level source directory of this
module for the precise wording of the license and the list of
copyright holders.
*/
/*!
* \file
*
* \brief This file contains the necessary classes to calculate the
* volumetric fluxes out of a pressure potential gradient using the
* Darcy relation.
*/
#ifndef EWOMS_DARCY_FLUX_MODULE_HH
#define EWOMS_DARCY_FLUX_MODULE_HH
#include <dune/common/fvector.hh>
#include <dune/common/fmatrix.hh>
#include <opm/common/Exceptions.hpp>
#include <opm/material/common/Valgrind.hpp>
#include <opm/models/common/multiphasebaseparameters.hh>
#include <opm/models/common/multiphasebaseproperties.hh>
#include <opm/models/common/quantitycallbacks.hh>
namespace Opm {
template <class TypeTag>
class DarcyIntensiveQuantities;
template <class TypeTag>
class DarcyExtensiveQuantities;
template <class TypeTag>
class DarcyBaseProblem;
/*!
* \ingroup FluxModules
* \brief Specifies a flux module which uses the Darcy relation.
*/
template <class TypeTag>
struct DarcyFluxModule
{
using FluxIntensiveQuantities = DarcyIntensiveQuantities<TypeTag>;
using FluxExtensiveQuantities = DarcyExtensiveQuantities<TypeTag>;
using FluxBaseProblem = DarcyBaseProblem<TypeTag>;
/*!
* \brief Register all run-time parameters for the flux module.
*/
static void registerParameters()
{ }
};
/*!
* \ingroup FluxModules
* \brief Provides the defaults for the parameters required by the
* Darcy velocity approach.
*/
template <class TypeTag>
class DarcyBaseProblem
{ };
/*!
* \ingroup FluxModules
* \brief Provides the intensive quantities for the Darcy flux module
*/
template <class TypeTag>
class DarcyIntensiveQuantities
{
using ElementContext = GetPropType<TypeTag, Properties::ElementContext>;
protected:
void update_(const ElementContext&,
unsigned,
unsigned)
{ }
};
/*!
* \ingroup FluxModules
* \brief Provides the Darcy flux module
*
* The commonly used Darcy relation looses its validity for Reynolds numbers \f$ Re <
* 1\f$. If one encounters flow velocities in porous media above this threshold, the
* Forchheimer approach can be used.
*
* The Darcy equation is given by the following relation:
*
* \f[
\vec{v}_\alpha =
\left( \nabla p_\alpha - \rho_\alpha \vec{g}\right)
\frac{\mu_\alpha}{k_{r,\alpha} K}
\f]
*/
template <class TypeTag>
class DarcyExtensiveQuantities
{
using ElementContext = GetPropType<TypeTag, Properties::ElementContext>;
using Scalar = GetPropType<TypeTag, Properties::Scalar>;
using Evaluation = GetPropType<TypeTag, Properties::Evaluation>;
using GridView = GetPropType<TypeTag, Properties::GridView>;
using Implementation = GetPropType<TypeTag, Properties::ExtensiveQuantities>;
using FluidSystem = GetPropType<TypeTag, Properties::FluidSystem>;
using MaterialLaw = GetPropType<TypeTag, Properties::MaterialLaw>;
enum { dimWorld = GridView::dimensionworld };
enum { numPhases = getPropValue<TypeTag, Properties::NumPhases>() };
using Toolbox = MathToolbox<Evaluation>;
using ParameterCache = typename FluidSystem::template ParameterCache<Evaluation>;
using EvalDimVector = Dune::FieldVector<Evaluation, dimWorld>;
using DimVector = Dune::FieldVector<Scalar, dimWorld>;
using DimMatrix = Dune::FieldMatrix<Scalar, dimWorld, dimWorld>;
public:
/*!
* \brief Returns the intrinsic permeability tensor for a given
* sub-control volume face.
*/
const DimMatrix& intrinsicPermability() const
{ return K_; }
/*!
* \brief Return the pressure potential gradient of a fluid phase
* at the face's integration point [Pa/m]
*
* \param phaseIdx The index of the fluid phase
*/
const EvalDimVector& potentialGrad(unsigned phaseIdx) const
{ return potentialGrad_[phaseIdx]; }
/*!
* \brief Return the filter velocity of a fluid phase at the
* face's integration point [m/s]
*
* \param phaseIdx The index of the fluid phase
*/
const EvalDimVector& filterVelocity(unsigned phaseIdx) const
{ return filterVelocity_[phaseIdx]; }
/*!
* \brief Return the volume flux of a fluid phase at the face's integration point
* \f$[m^3/s / m^2]\f$
*
* This is the fluid volume of a phase per second and per square meter of face
* area.
*
* \param phaseIdx The index of the fluid phase
*/
const Evaluation& volumeFlux(unsigned phaseIdx) const
{ return volumeFlux_[phaseIdx]; }
protected:
short upstreamIndex_(unsigned phaseIdx) const
{ return upstreamDofIdx_[phaseIdx]; }
short downstreamIndex_(unsigned phaseIdx) const
{ return downstreamDofIdx_[phaseIdx]; }
/*!
* \brief Calculate the gradients which are required to determine the volumetric fluxes
*
* The the upwind directions is also determined by method.
*/
void calculateGradients_(const ElementContext& elemCtx,
unsigned faceIdx,
unsigned timeIdx)
{
const auto& gradCalc = elemCtx.gradientCalculator();
PressureCallback<TypeTag> pressureCallback(elemCtx);
const auto& scvf = elemCtx.stencil(timeIdx).interiorFace(faceIdx);
const auto& faceNormal = scvf.normal();
unsigned i = scvf.interiorIndex();
unsigned j = scvf.exteriorIndex();
interiorDofIdx_ = static_cast<short>(i);
exteriorDofIdx_ = static_cast<short>(j);
unsigned focusDofIdx = elemCtx.focusDofIndex();
// calculate the "raw" pressure gradient
for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
if (!elemCtx.model().phaseIsConsidered(phaseIdx)) {
Valgrind::SetUndefined(potentialGrad_[phaseIdx]);
continue;
}
pressureCallback.setPhaseIndex(phaseIdx);
gradCalc.calculateGradient(potentialGrad_[phaseIdx],
elemCtx,
faceIdx,
pressureCallback);
Valgrind::CheckDefined(potentialGrad_[phaseIdx]);
}
// correct the pressure gradients by the gravitational acceleration
if (Parameters::Get<Parameters::EnableGravity>()) {
// estimate the gravitational acceleration at a given SCV face
// using the arithmetic mean
const auto& gIn = elemCtx.problem().gravity(elemCtx, i, timeIdx);
const auto& gEx = elemCtx.problem().gravity(elemCtx, j, timeIdx);
const auto& intQuantsIn = elemCtx.intensiveQuantities(i, timeIdx);
const auto& intQuantsEx = elemCtx.intensiveQuantities(j, timeIdx);
const auto& posIn = elemCtx.pos(i, timeIdx);
const auto& posEx = elemCtx.pos(j, timeIdx);
const auto& posFace = scvf.integrationPos();
// the distance between the centers of the control volumes
DimVector distVecIn(posIn);
DimVector distVecEx(posEx);
DimVector distVecTotal(posEx);
distVecIn -= posFace;
distVecEx -= posFace;
distVecTotal -= posIn;
Scalar absDistTotalSquared = distVecTotal.two_norm2();
for (unsigned phaseIdx=0; phaseIdx < numPhases; phaseIdx++) {
if (!elemCtx.model().phaseIsConsidered(phaseIdx))
continue;
// calculate the hydrostatic pressure at the integration point of the face
Evaluation pStatIn;
if (std::is_same<Scalar, Evaluation>::value ||
interiorDofIdx_ == static_cast<int>(focusDofIdx))
{
const Evaluation& rhoIn = intQuantsIn.fluidState().density(phaseIdx);
pStatIn = - rhoIn*(gIn*distVecIn);
}
else {
Scalar rhoIn = Toolbox::value(intQuantsIn.fluidState().density(phaseIdx));
pStatIn = - rhoIn*(gIn*distVecIn);
}
// the quantities on the exterior side of the face do not influence the
// result for the TPFA scheme, so they can be treated as scalar values.
Evaluation pStatEx;
if (std::is_same<Scalar, Evaluation>::value ||
exteriorDofIdx_ == static_cast<int>(focusDofIdx))
{
const Evaluation& rhoEx = intQuantsEx.fluidState().density(phaseIdx);
pStatEx = - rhoEx*(gEx*distVecEx);
}
else {
Scalar rhoEx = Toolbox::value(intQuantsEx.fluidState().density(phaseIdx));
pStatEx = - rhoEx*(gEx*distVecEx);
}
// compute the hydrostatic gradient between the two control volumes (this
// gradient exhibitis the same direction as the vector between the two
// control volume centers and the length (pStaticExterior -
// pStaticInterior)/distanceInteriorToExterior
Dune::FieldVector<Evaluation, dimWorld> f(distVecTotal);
f *= (pStatEx - pStatIn)/absDistTotalSquared;
// calculate the final potential gradient
for (unsigned dimIdx = 0; dimIdx < dimWorld; ++dimIdx)
potentialGrad_[phaseIdx][dimIdx] += f[dimIdx];
for (unsigned dimIdx = 0; dimIdx < potentialGrad_[phaseIdx].size(); ++dimIdx) {
if (!isfinite(potentialGrad_[phaseIdx][dimIdx])) {
throw NumericalProblem("Non-finite potential gradient for phase '"
+ std::string(FluidSystem::phaseName(phaseIdx))+"'");
}
}
}
}
Valgrind::SetUndefined(K_);
elemCtx.problem().intersectionIntrinsicPermeability(K_, elemCtx, faceIdx, timeIdx);
Valgrind::CheckDefined(K_);
for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
if (!elemCtx.model().phaseIsConsidered(phaseIdx)) {
Valgrind::SetUndefined(potentialGrad_[phaseIdx]);
continue;
}
// determine the upstream and downstream DOFs
Evaluation tmp = 0.0;
for (unsigned dimIdx = 0; dimIdx < faceNormal.size(); ++dimIdx)
tmp += potentialGrad_[phaseIdx][dimIdx]*faceNormal[dimIdx];
if (tmp > 0) {
upstreamDofIdx_[phaseIdx] = exteriorDofIdx_;
downstreamDofIdx_[phaseIdx] = interiorDofIdx_;
}
else {
upstreamDofIdx_[phaseIdx] = interiorDofIdx_;
downstreamDofIdx_[phaseIdx] = exteriorDofIdx_;
}
// we only carry the derivatives along if the upstream DOF is the one which
// we currently focus on
const auto& up = elemCtx.intensiveQuantities(upstreamDofIdx_[phaseIdx], timeIdx);
if (upstreamDofIdx_[phaseIdx] == static_cast<int>(focusDofIdx))
mobility_[phaseIdx] = up.mobility(phaseIdx);
else
mobility_[phaseIdx] = Toolbox::value(up.mobility(phaseIdx));
}
}
/*!
* \brief Calculate the gradients at the grid boundary which are required to
* determine the volumetric fluxes
*
* The the upwind directions is also determined by method.
*/
template <class FluidState>
void calculateBoundaryGradients_(const ElementContext& elemCtx,
unsigned boundaryFaceIdx,
unsigned timeIdx,
const FluidState& fluidState)
{
const auto& gradCalc = elemCtx.gradientCalculator();
BoundaryPressureCallback<TypeTag, FluidState> pressureCallback(elemCtx, fluidState);
// calculate the pressure gradient
for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
if (!elemCtx.model().phaseIsConsidered(phaseIdx)) {
Valgrind::SetUndefined(potentialGrad_[phaseIdx]);
continue;
}
pressureCallback.setPhaseIndex(phaseIdx);
gradCalc.calculateBoundaryGradient(potentialGrad_[phaseIdx],
elemCtx,
boundaryFaceIdx,
pressureCallback);
Valgrind::CheckDefined(potentialGrad_[phaseIdx]);
}
const auto& scvf = elemCtx.stencil(timeIdx).boundaryFace(boundaryFaceIdx);
auto i = scvf.interiorIndex();
interiorDofIdx_ = static_cast<short>(i);
exteriorDofIdx_ = -1;
int focusDofIdx = elemCtx.focusDofIndex();
// calculate the intrinsic permeability
const auto& intQuantsIn = elemCtx.intensiveQuantities(i, timeIdx);
K_ = intQuantsIn.intrinsicPermeability();
// correct the pressure gradients by the gravitational acceleration
if (Parameters::Get<Parameters::EnableGravity>()) {
// estimate the gravitational acceleration at a given SCV face
// using the arithmetic mean
const auto& gIn = elemCtx.problem().gravity(elemCtx, i, timeIdx);
const auto& posIn = elemCtx.pos(i, timeIdx);
const auto& posFace = scvf.integrationPos();
// the distance between the face center and the center of the control volume
DimVector distVecIn(posIn);
distVecIn -= posFace;
Scalar absDistSquared = distVecIn.two_norm2();
Scalar gTimesDist = gIn*distVecIn;
for (unsigned phaseIdx=0; phaseIdx < numPhases; phaseIdx++) {
if (!elemCtx.model().phaseIsConsidered(phaseIdx))
continue;
// calculate the hydrostatic pressure at the integration point of the face
Evaluation rhoIn = intQuantsIn.fluidState().density(phaseIdx);
Evaluation pStatIn = - gTimesDist*rhoIn;
Valgrind::CheckDefined(pStatIn);
// compute the hydrostatic gradient between the control volume and face integration
// point. This gradient exhibitis the same direction as the vector between the
// control volume center and face integration point (-distVecIn / absDist) and the
// length of the vector is -pStaticIn / absDist. Note that the two negatives become
// + and the 1 / (absDist * absDist) -> absDistSquared.
EvalDimVector f(distVecIn);
f *= pStatIn / absDistSquared;
// calculate the final potential gradient
for (unsigned dimIdx = 0; dimIdx < dimWorld; ++dimIdx)
potentialGrad_[phaseIdx][dimIdx] += f[dimIdx];
Valgrind::CheckDefined(potentialGrad_[phaseIdx]);
for (unsigned dimIdx = 0; dimIdx < potentialGrad_[phaseIdx].size(); ++dimIdx) {
if (!isfinite(potentialGrad_[phaseIdx][dimIdx])) {
throw NumericalProblem("Non-finite potential gradient for phase '"
+ std::string(FluidSystem::phaseName(phaseIdx))+"'");
}
}
}
}
// determine the upstream and downstream DOFs
const auto& faceNormal = scvf.normal();
const auto& matParams = elemCtx.problem().materialLawParams(elemCtx, i, timeIdx);
Scalar kr[numPhases];
MaterialLaw::relativePermeabilities(kr, matParams, fluidState);
Valgrind::CheckDefined(kr);
for (unsigned phaseIdx=0; phaseIdx < numPhases; phaseIdx++) {
if (!elemCtx.model().phaseIsConsidered(phaseIdx))
continue;
Evaluation tmp = 0.0;
for (unsigned dimIdx = 0; dimIdx < faceNormal.size(); ++dimIdx)
tmp += potentialGrad_[phaseIdx][dimIdx]*faceNormal[dimIdx];
if (tmp > 0) {
upstreamDofIdx_[phaseIdx] = exteriorDofIdx_;
downstreamDofIdx_[phaseIdx] = interiorDofIdx_;
}
else {
upstreamDofIdx_[phaseIdx] = interiorDofIdx_;
downstreamDofIdx_[phaseIdx] = exteriorDofIdx_;
}
// take the phase mobility from the DOF in upstream direction
if (upstreamDofIdx_[phaseIdx] < 0) {
if (interiorDofIdx_ == focusDofIdx)
mobility_[phaseIdx] =
kr[phaseIdx] / fluidState.viscosity(phaseIdx);
else
mobility_[phaseIdx] =
Toolbox::value(kr[phaseIdx])
/ Toolbox::value(fluidState.viscosity(phaseIdx));
}
else if (upstreamDofIdx_[phaseIdx] != focusDofIdx)
mobility_[phaseIdx] = Toolbox::value(intQuantsIn.mobility(phaseIdx));
else
mobility_[phaseIdx] = intQuantsIn.mobility(phaseIdx);
Valgrind::CheckDefined(mobility_[phaseIdx]);
}
}
/*!
* \brief Calculate the volumetric fluxes of all phases
*
* The pressure potentials and upwind directions must already be
* determined before calling this method!
*/
void calculateFluxes_(const ElementContext& elemCtx, unsigned scvfIdx, unsigned timeIdx)
{
const auto& scvf = elemCtx.stencil(timeIdx).interiorFace(scvfIdx);
const DimVector& normal = scvf.normal();
Valgrind::CheckDefined(normal);
for (unsigned phaseIdx=0; phaseIdx < numPhases; phaseIdx++) {
filterVelocity_[phaseIdx] = 0.0;
volumeFlux_[phaseIdx] = 0.0;
if (!elemCtx.model().phaseIsConsidered(phaseIdx))
continue;
asImp_().calculateFilterVelocity_(phaseIdx);
Valgrind::CheckDefined(filterVelocity_[phaseIdx]);
volumeFlux_[phaseIdx] = 0.0;
for (unsigned i = 0; i < normal.size(); ++i)
volumeFlux_[phaseIdx] += filterVelocity_[phaseIdx][i] * normal[i];
}
}
/*!
* \brief Calculate the volumetric fluxes at a boundary face of all fluid phases
*
* The pressure potentials and upwind directions must already be determined before
* calling this method!
*/
void calculateBoundaryFluxes_(const ElementContext& elemCtx,
unsigned boundaryFaceIdx,
unsigned timeIdx)
{
const auto& scvf = elemCtx.stencil(timeIdx).boundaryFace(boundaryFaceIdx);
const DimVector& normal = scvf.normal();
Valgrind::CheckDefined(normal);
for (unsigned phaseIdx=0; phaseIdx < numPhases; phaseIdx++) {
if (!elemCtx.model().phaseIsConsidered(phaseIdx)) {
filterVelocity_[phaseIdx] = 0.0;
volumeFlux_[phaseIdx] = 0.0;
continue;
}
asImp_().calculateFilterVelocity_(phaseIdx);
Valgrind::CheckDefined(filterVelocity_[phaseIdx]);
volumeFlux_[phaseIdx] = 0.0;
for (unsigned i = 0; i < normal.size(); ++i)
volumeFlux_[phaseIdx] += filterVelocity_[phaseIdx][i] * normal[i];
}
}
void calculateFilterVelocity_(unsigned phaseIdx)
{
#ifndef NDEBUG
assert(isfinite(mobility_[phaseIdx]));
for (unsigned i = 0; i < K_.M(); ++ i)
for (unsigned j = 0; j < K_.N(); ++ j)
assert(std::isfinite(K_[i][j]));
#endif
K_.mv(potentialGrad_[phaseIdx], filterVelocity_[phaseIdx]);
filterVelocity_[phaseIdx] *= - mobility_[phaseIdx];
#ifndef NDEBUG
for (unsigned i = 0; i < filterVelocity_[phaseIdx].size(); ++ i)
assert(isfinite(filterVelocity_[phaseIdx][i]));
#endif
}
private:
Implementation& asImp_()
{ return *static_cast<Implementation*>(this); }
const Implementation& asImp_() const
{ return *static_cast<const Implementation*>(this); }
protected:
// intrinsic permeability tensor and its square root
DimMatrix K_;
// mobilities of all fluid phases [1 / (Pa s)]
Evaluation mobility_[numPhases];
// filter velocities of all phases [m/s]
EvalDimVector filterVelocity_[numPhases];
// the volumetric flux of all fluid phases over the control
// volume's face [m^3/s / m^2]
Evaluation volumeFlux_[numPhases];
// pressure potential gradients of all phases [Pa / m]
EvalDimVector potentialGrad_[numPhases];
// upstream, downstream, interior and exterior DOFs
short upstreamDofIdx_[numPhases];
short downstreamDofIdx_[numPhases];
short interiorDofIdx_;
short exteriorDofIdx_;
};
} // namespace Opm
#endif
|