Loading...
Searching...
No Matches
pvsprimaryvariables.hh
Go to the documentation of this file.
// -*- 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.
*/
#ifndef EWOMS_PVS_PRIMARY_VARIABLES_HH
#define EWOMS_PVS_PRIMARY_VARIABLES_HH
#include <dune/common/fvector.hh>
#include <opm/common/Exceptions.hpp>
#include <opm/material/common/MathToolbox.hpp>
#include <opm/material/common/Valgrind.hpp>
#include <opm/material/constraintsolvers/NcpFlash.hpp>
#include <opm/material/fluidstates/CompositionalFluidState.hpp>
#include <opm/models/common/energymodule.hh>
#include <opm/models/discretization/common/fvbaseprimaryvariables.hh>
#include <opm/models/pvs/pvsproperties.hh>
#include <cassert>
#include <cmath>
#include <ostream>
#include <type_traits>
namespace Opm {
template <class TypeTag>
class PvsPrimaryVariables : public FvBasePrimaryVariables<TypeTag>
{
using ParentType = FvBasePrimaryVariables<TypeTag>;
using ThisType = PvsPrimaryVariables<TypeTag>;
using Implementation = GetPropType<TypeTag, Properties::PrimaryVariables>;
using Scalar = GetPropType<TypeTag, Properties::Scalar>;
using Evaluation = GetPropType<TypeTag, Properties::Evaluation>;
using FluidSystem = GetPropType<TypeTag, Properties::FluidSystem>;
using MaterialLaw = GetPropType<TypeTag, Properties::MaterialLaw>;
using MaterialLawParams = GetPropType<TypeTag, Properties::MaterialLawParams>;
using Indices = GetPropType<TypeTag, Properties::Indices>;
// primary variable indices
enum { pressure0Idx = Indices::pressure0Idx };
enum { switch0Idx = Indices::switch0Idx };
enum { numPhases = getPropValue<TypeTag, Properties::NumPhases>() };
enum { numComponents = getPropValue<TypeTag, Properties::NumComponents>() };
enum { enableEnergy = getPropValue<TypeTag, Properties::EnableEnergy>() };
using Toolbox = MathToolbox<Evaluation>;
using ComponentVector = Dune::FieldVector<Scalar, numComponents>;
using EnergyModule = ::Opm::EnergyModule<TypeTag, enableEnergy>;
using NcpFlash = ::Opm::NcpFlash<Scalar, FluidSystem>;
public:
PvsPrimaryVariables() : ParentType()
{ Valgrind::SetDefined(*this); }
PvsPrimaryVariables(const PvsPrimaryVariables& value) : ParentType(value)
{
Valgrind::SetDefined(*this);
phasePresence_ = value.phasePresence_;
}
template <class FluidState>
void assignMassConservative(const FluidState& fluidState,
const MaterialLawParams& matParams,
bool isInEquilibrium = false)
{
#ifndef NDEBUG
// make sure the temperature is the same in all fluid phases
for (unsigned phaseIdx = 1; phaseIdx < numPhases; ++phaseIdx) {
assert(std::abs(fluidState.temperature(0) - fluidState.temperature(phaseIdx)) < 1e-30);
}
#endif // NDEBUG
// for the equilibrium case, we don't need complicated
// computations.
if (isInEquilibrium) {
assignNaive(fluidState);
return;
}
// use a flash calculation to calculate a fluid state in
// thermodynamic equilibrium
typename FluidSystem::template ParameterCache<Scalar> paramCache;
CompositionalFluidState<Scalar, FluidSystem> fsFlash;
// use the externally given fluid state as initial value for
// the flash calculation
fsFlash.assign(fluidState);
// calculate the phase densities
paramCache.updateAll(fsFlash);
for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
const Scalar rho = FluidSystem::density(fsFlash, paramCache, phaseIdx);
fsFlash.setDensity(phaseIdx, rho);
}
// calculate the "global molarities"
ComponentVector globalMolarities(0.0);
for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
globalMolarities[compIdx] +=
fsFlash.saturation(phaseIdx) * fsFlash.molarity(phaseIdx, compIdx);
}
}
// run the flash calculation
NcpFlash::template solve<MaterialLaw>(fsFlash, matParams, paramCache, globalMolarities);
// use the result to assign the primary variables
assignNaive(fsFlash);
}
short phasePresence() const
{ return phasePresence_; }
void setPhasePresence(short value)
{ phasePresence_ = value; }
void setPhasePresent(unsigned phaseIdx, bool yesno)
{
if (yesno) {
setPhasePresence(phasePresence_ | (1 << phaseIdx));
}
else {
setPhasePresence(phasePresence_ & ~(1 << phaseIdx));
}
}
unsigned implicitSaturationIdx() const
{ return lowestPresentPhaseIdx(); }
static bool phaseIsPresent(unsigned phaseIdx, short phasePresence)
{ return phasePresence & (1 << phaseIdx); }
bool phaseIsPresent(unsigned phaseIdx) const
{ return phasePresence_ & (1 << phaseIdx); }
unsigned lowestPresentPhaseIdx() const
{ return static_cast<unsigned>(ffs(phasePresence_) - 1); }
ThisType& operator=(const Implementation& value)
{
ParentType::operator=(value);
phasePresence_ = value.phasePresence_;
return *this;
}
ThisType& operator=(Scalar value)
{
ParentType::operator=(value);
phasePresence_ = 0;
return *this;
}
Evaluation explicitSaturationValue(unsigned phaseIdx, unsigned timeIdx) const
{
if (!phaseIsPresent(phaseIdx) || phaseIdx == lowestPresentPhaseIdx()) {
// non-present phases have saturation 0
return 0.0;
}
const unsigned varIdx = switch0Idx + phaseIdx - 1;
if constexpr (std::is_same_v<Evaluation, Scalar>) {
return (*this)[varIdx]; // finite differences
}
else {
// automatic differentiation
if (timeIdx != 0) {
Toolbox::createConstant((*this)[varIdx]);
}
return Toolbox::createVariable((*this)[varIdx], varIdx);
}
}
template <class FluidState>
void assignNaive(const FluidState& fluidState)
{
using FsToolbox = MathToolbox<typename FluidState::Scalar>;
// assign the phase temperatures. this is out-sourced to
// the energy module
EnergyModule::setPriVarTemperatures(*this, fluidState);
// set the pressure of the first phase
(*this)[pressure0Idx] = FsToolbox::value(fluidState.pressure(/*phaseIdx=*/0));
Valgrind::CheckDefined((*this)[pressure0Idx]);
// determine the phase presence.
phasePresence_ = 0;
for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
// use a NCP condition to determine if the phase is
// present or not
Scalar a = 1;
for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
a -= FsToolbox::value(fluidState.moleFraction(phaseIdx, compIdx));
}
const Scalar b = FsToolbox::value(fluidState.saturation(phaseIdx));
if (b > a) {
phasePresence_ |= (1 << phaseIdx);
}
}
// some phase must be present
if (phasePresence_ == 0) {
throw NumericalProblem("Phase state was 0, i.e., no fluid is present");
}
// set the primary variables which correspond to mole
// fractions of the present phase which has the lowest index.
const unsigned lowestPhaseIdx = lowestPresentPhaseIdx();
for (unsigned switchIdx = 0; switchIdx < numPhases - 1; ++switchIdx) {
unsigned phaseIdx = switchIdx;
const unsigned compIdx = switchIdx + 1;
if (switchIdx >= lowestPhaseIdx) {
++phaseIdx;
}
if (phaseIsPresent(phaseIdx)) {
(*this)[switch0Idx + switchIdx] = FsToolbox::value(fluidState.saturation(phaseIdx));
Valgrind::CheckDefined((*this)[switch0Idx + switchIdx]);
}
else {
(*this)[switch0Idx + switchIdx] =
FsToolbox::value(fluidState.moleFraction(lowestPhaseIdx, compIdx));
Valgrind::CheckDefined((*this)[switch0Idx + switchIdx]);
}
}
// set the mole fractions in of the remaining components in
// the phase with the lowest index
for (unsigned compIdx = numPhases - 1; compIdx < numComponents - 1; ++compIdx) {
(*this)[switch0Idx + compIdx] =
FsToolbox::value(fluidState.moleFraction(lowestPhaseIdx, compIdx + 1));
Valgrind::CheckDefined((*this)[switch0Idx + compIdx]);
}
}
void print(std::ostream& os) const
{
os << "(p_" << FluidSystem::phaseName(0) << " = "
<< (*this)[pressure0Idx];
const unsigned lowestPhaseIdx = lowestPresentPhaseIdx();
for (unsigned switchIdx = 0; switchIdx < numPhases - 1; ++switchIdx) {
unsigned phaseIdx = switchIdx;
const unsigned compIdx = switchIdx + 1;
if (phaseIdx >= lowestPhaseIdx) {
++phaseIdx; // skip the saturation of the present
// phase with the lowest index
}
if (phaseIsPresent(phaseIdx)) {
os << ", S_" << FluidSystem::phaseName(phaseIdx) << " = "
<< (*this)[switch0Idx + switchIdx];
}
else {
os << ", x_" << FluidSystem::phaseName(lowestPhaseIdx) << "^"
<< FluidSystem::componentName(compIdx) << " = "
<< (*this)[switch0Idx + switchIdx];
}
}
for (unsigned compIdx = numPhases - 1; compIdx < numComponents - 1; ++compIdx) {
os << ", x_" << FluidSystem::phaseName(lowestPhaseIdx) << "^"
<< FluidSystem::componentName(compIdx + 1) << " = "
<< (*this)[switch0Idx + compIdx];
}
os << ")";
os << ", phase presence: " << static_cast<int>(phasePresence_) << std::flush;
}
private:
short phasePresence_{};
};
} // namespace Opm
#endif
Opm::EnergyModule
Provides the auxiliary methods required for consideration of the energy equation.
Definition energymodule.hh:54
Opm::FvBasePrimaryVariables::operator=
FvBasePrimaryVariables & operator=(const FvBasePrimaryVariables &value)=default
Assignment from another primary variables object.
Opm::PvsPrimaryVariables::implicitSaturationIdx
unsigned implicitSaturationIdx() const
Returns the index of the phase with's its saturation is determined by the closure condition of satura...
Definition pvsprimaryvariables.hh:192
Opm::PvsPrimaryVariables::setPhasePresence
void setPhasePresence(short value)
Set which fluid phases are present in a given control volume.
Definition pvsprimaryvariables.hh:168
Opm::PvsPrimaryVariables::phaseIsPresent
static bool phaseIsPresent(unsigned phaseIdx, short phasePresence)
Returns true iff a phase is present for a given phase presence.
Definition pvsprimaryvariables.hh:203
Opm::PvsPrimaryVariables::operator=
ThisType & operator=(const Implementation &value)
Assignment operator from an other primary variables object.
Definition pvsprimaryvariables.hh:224
Opm::PvsPrimaryVariables::assignNaive
void assignNaive(const FluidState &fluidState)
Directly retrieve the primary variables from an arbitrary fluid state.
Definition pvsprimaryvariables.hh:274
Opm::PvsPrimaryVariables::PvsPrimaryVariables
PvsPrimaryVariables(const PvsPrimaryVariables &value)
Definition pvsprimaryvariables.hh:95
Opm::PvsPrimaryVariables::lowestPresentPhaseIdx
unsigned lowestPresentPhaseIdx() const
Returns the phase with the lowest index that is present.
Definition pvsprimaryvariables.hh:218
Opm::PvsPrimaryVariables::operator=
ThisType & operator=(Scalar value)
Assignment operator from a scalar value.
Definition pvsprimaryvariables.hh:235
Opm::PvsPrimaryVariables::print
void print(std::ostream &os) const
Prints the names of the primary variables and their values.
Definition pvsprimaryvariables.hh:340
Opm::PvsPrimaryVariables::explicitSaturationValue
Evaluation explicitSaturationValue(unsigned phaseIdx, unsigned timeIdx) const
Returns an explcitly stored saturation for a given phase.
Definition pvsprimaryvariables.hh:250
Opm::PvsPrimaryVariables::phaseIsPresent
bool phaseIsPresent(unsigned phaseIdx) const
Returns true iff a phase is present for the current phase presence.
Definition pvsprimaryvariables.hh:212
Opm::PvsPrimaryVariables::phasePresence
short phasePresence() const
Return the fluid phases which are present in a given control volume.
Definition pvsprimaryvariables.hh:159
Opm::PvsPrimaryVariables::assignMassConservative
void assignMassConservative(const FluidState &fluidState, const MaterialLawParams &matParams, bool isInEquilibrium=false)
< Import base class assignment operators.
Definition pvsprimaryvariables.hh:106
Opm::PvsPrimaryVariables::setPhasePresent
void setPhasePresent(unsigned phaseIdx, bool yesno)
Set whether a given indivividual phase should be present or not.
Definition pvsprimaryvariables.hh:178
energymodule.hh
Contains the classes required to consider energy as a conservation quantity in a multi-phase module.
fvbaseprimaryvariables.hh
Represents the primary variables used by the a model.
Opm
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition blackoilbioeffectsmodules.hh:43
Opm::GetPropType
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property (equivalent to old macro GET_PROP_TYPE(....
Definition propertysystem.hh:233
Opm::getPropValue
constexpr auto getPropValue()
get the value data member of a property
Definition propertysystem.hh:240
pvsproperties.hh
Declares the properties required for the compositional multi-phase primary variable switching model.