28#ifndef EWOMS_PVS_PRIMARY_VARIABLES_HH
29#define EWOMS_PVS_PRIMARY_VARIABLES_HH
31#include <dune/common/fvector.hh>
33#include <opm/common/Exceptions.hpp>
35#include <opm/material/common/MathToolbox.hpp>
36#include <opm/material/common/Valgrind.hpp>
37#include <opm/material/constraintsolvers/NcpFlash.hpp>
38#include <opm/material/fluidstates/CompositionalFluidState.hpp>
61template <
class TypeTag>
76 enum { pressure0Idx = Indices::pressure0Idx };
77 enum { switch0Idx = Indices::switch0Idx };
79 enum { numPhases = getPropValue<TypeTag, Properties::NumPhases>() };
80 enum { numComponents = getPropValue<TypeTag, Properties::NumComponents>() };
81 static constexpr bool enableEnergy = getPropValue<TypeTag, Properties::EnableEnergy>();
83 using Toolbox = MathToolbox<Evaluation>;
84 using ComponentVector = Dune::FieldVector<Scalar, numComponents>;
86 using NcpFlash = ::Opm::NcpFlash<Scalar, FluidSystem>;
90 { Valgrind::SetDefined(*
this); }
98 Valgrind::SetDefined(*
this);
100 phasePresence_ = value.phasePresence_;
106 template <
class Flu
idState>
108 const MaterialLawParams& matParams,
109 bool isInEquilibrium =
false)
113 for (
unsigned phaseIdx = 1; phaseIdx < numPhases; ++phaseIdx) {
114 assert(std::abs(fluidState.temperature(0) - fluidState.temperature(phaseIdx)) < 1e-30);
120 if (isInEquilibrium) {
127 typename FluidSystem::template ParameterCache<Scalar> paramCache;
128 CompositionalFluidState<Scalar, FluidSystem> fsFlash;
132 fsFlash.assign(fluidState);
135 paramCache.updateAll(fsFlash);
136 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
137 const Scalar rho = FluidSystem::density(fsFlash, paramCache, phaseIdx);
138 fsFlash.setDensity(phaseIdx, rho);
141 ComponentVector globalMolarities(0.0);
142 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
143 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
144 globalMolarities[compIdx] +=
145 fsFlash.saturation(phaseIdx) * fsFlash.molarity(phaseIdx, compIdx);
150 NcpFlash::template solve<MaterialLaw>(fsFlash, matParams, paramCache, globalMolarities);
161 {
return phasePresence_; }
170 { phasePresence_ = value; }
214 {
return phasePresence_ & (1 << phaseIdx); }
221 if (phasePresence_ == 0) {
222 throw NumericalProblem(
"Phase state was 0, i.e., no fluid is present");
224 return static_cast<unsigned>(std::countr_zero(
static_cast<unsigned short>(phasePresence_)));
233 phasePresence_ = value.phasePresence_;
263 const unsigned varIdx = switch0Idx + phaseIdx - 1;
264 if constexpr (std::is_same_v<Evaluation, Scalar>) {
265 return (*
this)[varIdx];
270 Toolbox::createConstant((*
this)[varIdx]);
272 return Toolbox::createVariable((*
this)[varIdx], varIdx);
279 template <
class Flu
idState>
282 using FsToolbox = MathToolbox<typename FluidState::ValueType>;
286 EnergyModule::setPriVarTemperatures(*
this, fluidState);
289 (*this)[pressure0Idx] = FsToolbox::value(fluidState.pressure(0));
290 Valgrind::CheckDefined((*
this)[pressure0Idx]);
294 for (
unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
298 for (
unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
299 a -= FsToolbox::value(fluidState.moleFraction(phaseIdx, compIdx));
301 const Scalar b = FsToolbox::value(fluidState.saturation(phaseIdx));
304 phasePresence_ |= (1 << phaseIdx);
309 if (phasePresence_ == 0) {
310 throw NumericalProblem(
"Phase state was 0, i.e., no fluid is present");
316 for (
unsigned switchIdx = 0; switchIdx < numPhases - 1; ++switchIdx) {
317 unsigned phaseIdx = switchIdx;
318 const unsigned compIdx = switchIdx + 1;
319 if (switchIdx >= lowestPhaseIdx) {
324 (*this)[switch0Idx + switchIdx] = FsToolbox::value(fluidState.saturation(phaseIdx));
325 Valgrind::CheckDefined((*
this)[switch0Idx + switchIdx]);
328 (*this)[switch0Idx + switchIdx] =
329 FsToolbox::value(fluidState.moleFraction(lowestPhaseIdx, compIdx));
330 Valgrind::CheckDefined((*
this)[switch0Idx + switchIdx]);
336 for (
unsigned compIdx = numPhases - 1; compIdx < numComponents - 1; ++compIdx) {
337 (*this)[switch0Idx + compIdx] =
338 FsToolbox::value(fluidState.moleFraction(lowestPhaseIdx, compIdx + 1));
339 Valgrind::CheckDefined((*
this)[switch0Idx + compIdx]);
348 os <<
"(p_" << FluidSystem::phaseName(0) <<
" = "
349 << (*this)[pressure0Idx];
351 for (
unsigned switchIdx = 0; switchIdx < numPhases - 1; ++switchIdx) {
352 unsigned phaseIdx = switchIdx;
353 const unsigned compIdx = switchIdx + 1;
354 if (phaseIdx >= lowestPhaseIdx) {
360 os <<
", S_" << FluidSystem::phaseName(phaseIdx) <<
" = "
361 << (*this)[switch0Idx + switchIdx];
364 os <<
", x_" << FluidSystem::phaseName(lowestPhaseIdx) <<
"^"
365 << FluidSystem::componentName(compIdx) <<
" = "
366 << (*this)[switch0Idx + switchIdx];
369 for (
unsigned compIdx = numPhases - 1; compIdx < numComponents - 1; ++compIdx) {
370 os <<
", x_" << FluidSystem::phaseName(lowestPhaseIdx) <<
"^"
371 << FluidSystem::componentName(compIdx + 1) <<
" = "
372 << (*this)[switch0Idx + compIdx];
375 os <<
", phase presence: " <<
static_cast<int>(phasePresence_) << std::flush;
379 short phasePresence_{};
Provides the auxiliary methods required for consideration of the energy equation.
Definition: energymodule.hh:55
Represents the primary variables used by the a model.
Definition: fvbaseprimaryvariables.hh:54
FvBasePrimaryVariables & operator=(const FvBasePrimaryVariables &value)=default
Assignment from another primary variables object.
Represents the primary variables used in the primary variable switching compositional model.
Definition: pvsprimaryvariables.hh:63
unsigned implicitSaturationIdx() const
Returns the index of the phase with's its saturation is determined by the closure condition of satura...
Definition: pvsprimaryvariables.hh:193
void setPhasePresence(short value)
Set which fluid phases are present in a given control volume.
Definition: pvsprimaryvariables.hh:169
static bool phaseIsPresent(unsigned phaseIdx, short phasePresence)
Returns true iff a phase is present for a given phase presence.
Definition: pvsprimaryvariables.hh:204
ThisType & operator=(const Implementation &value)
Assignment operator from an other primary variables object.
Definition: pvsprimaryvariables.hh:230
void assignNaive(const FluidState &fluidState)
Directly retrieve the primary variables from an arbitrary fluid state.
Definition: pvsprimaryvariables.hh:280
PvsPrimaryVariables(const PvsPrimaryVariables &value)
Definition: pvsprimaryvariables.hh:96
unsigned lowestPresentPhaseIdx() const
Returns the phase with the lowest index that is present.
Definition: pvsprimaryvariables.hh:219
ThisType & operator=(Scalar value)
Assignment operator from a scalar value.
Definition: pvsprimaryvariables.hh:241
void print(std::ostream &os) const
Prints the names of the primary variables and their values.
Definition: pvsprimaryvariables.hh:346
Evaluation explicitSaturationValue(unsigned phaseIdx, unsigned timeIdx) const
Returns an explcitly stored saturation for a given phase.
Definition: pvsprimaryvariables.hh:256
bool phaseIsPresent(unsigned phaseIdx) const
Returns true iff a phase is present for the current phase presence.
Definition: pvsprimaryvariables.hh:213
short phasePresence() const
Return the fluid phases which are present in a given control volume.
Definition: pvsprimaryvariables.hh:160
void assignMassConservative(const FluidState &fluidState, const MaterialLawParams &matParams, bool isInEquilibrium=false)
< Import base class assignment operators.
Definition: pvsprimaryvariables.hh:107
void setPhasePresent(unsigned phaseIdx, bool yesno)
Set whether a given indivividual phase should be present or not.
Definition: pvsprimaryvariables.hh:179
PvsPrimaryVariables()
Definition: pvsprimaryvariables.hh:89
Contains the classes required to consider energy as a conservation quantity in a multi-phase module.
Definition: blackoilbioeffectsmodules.hh:45
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
Declares the properties required for the compositional multi-phase primary variable switching model.