pvsprimaryvariables.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18
19 Consult the COPYING file in the top-level source directory of this
20 module for the precise wording of the license and the list of
21 copyright holders.
22*/
28#ifndef EWOMS_PVS_PRIMARY_VARIABLES_HH
29#define EWOMS_PVS_PRIMARY_VARIABLES_HH
30
31#include <dune/common/fvector.hh>
32
33#include <opm/common/Exceptions.hpp>
34
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>
39
43
44#include <bit>
45#include <cassert>
46#include <cmath>
47#include <ostream>
48#include <type_traits>
49
50namespace Opm {
51
61template <class TypeTag>
63{
64 using ParentType = FvBasePrimaryVariables<TypeTag>;
67
74
75 // primary variable indices
76 enum { pressure0Idx = Indices::pressure0Idx };
77 enum { switch0Idx = Indices::switch0Idx };
78
79 enum { numPhases = getPropValue<TypeTag, Properties::NumPhases>() };
80 enum { numComponents = getPropValue<TypeTag, Properties::NumComponents>() };
81 static constexpr bool enableEnergy = getPropValue<TypeTag, Properties::EnableEnergy>();
82
83 using Toolbox = MathToolbox<Evaluation>;
84 using ComponentVector = Dune::FieldVector<Scalar, numComponents>;
86 using NcpFlash = ::Opm::NcpFlash<Scalar, FluidSystem>;
87
88public:
89 PvsPrimaryVariables() : ParentType()
90 { Valgrind::SetDefined(*this); }
91
96 PvsPrimaryVariables(const PvsPrimaryVariables& value) : ParentType(value)
97 {
98 Valgrind::SetDefined(*this);
99
100 phasePresence_ = value.phasePresence_;
101 }
102
106 template <class FluidState>
107 void assignMassConservative(const FluidState& fluidState,
108 const MaterialLawParams& matParams,
109 bool isInEquilibrium = false)
110 {
111#ifndef NDEBUG
112 // make sure the temperature is the same in all fluid phases
113 for (unsigned phaseIdx = 1; phaseIdx < numPhases; ++phaseIdx) {
114 assert(std::abs(fluidState.temperature(0) - fluidState.temperature(phaseIdx)) < 1e-30);
115 }
116#endif // NDEBUG
117
118 // for the equilibrium case, we don't need complicated
119 // computations.
120 if (isInEquilibrium) {
121 assignNaive(fluidState);
122 return;
123 }
124
125 // use a flash calculation to calculate a fluid state in
126 // thermodynamic equilibrium
127 typename FluidSystem::template ParameterCache<Scalar> paramCache;
128 CompositionalFluidState<Scalar, FluidSystem> fsFlash;
129
130 // use the externally given fluid state as initial value for
131 // the flash calculation
132 fsFlash.assign(fluidState);
133
134 // calculate the phase densities
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);
139 }
140 // calculate the "global molarities"
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);
146 }
147 }
148
149 // run the flash calculation
150 NcpFlash::template solve<MaterialLaw>(fsFlash, matParams, paramCache, globalMolarities);
151
152 // use the result to assign the primary variables
153 assignNaive(fsFlash);
154 }
155
160 short phasePresence() const
161 { return phasePresence_; }
162
169 void setPhasePresence(short value)
170 { phasePresence_ = value; }
171
179 void setPhasePresent(unsigned phaseIdx, bool yesno)
180 {
181 if (yesno) {
182 setPhasePresence(phasePresence_ | (1 << phaseIdx));
183 }
184 else {
185 setPhasePresence(phasePresence_ & ~(1 << phaseIdx));
186 }
187 }
188
193 unsigned implicitSaturationIdx() const
194 { return lowestPresentPhaseIdx(); }
195
204 static bool phaseIsPresent(unsigned phaseIdx, short phasePresence)
205 { return phasePresence & (1 << phaseIdx); }
206
213 bool phaseIsPresent(unsigned phaseIdx) const
214 { return phasePresence_ & (1 << phaseIdx); }
215
219 unsigned lowestPresentPhaseIdx() const
220 {
221 if (phasePresence_ == 0) {
222 throw NumericalProblem("Phase state was 0, i.e., no fluid is present");
223 }
224 return static_cast<unsigned>(std::countr_zero(static_cast<unsigned short>(phasePresence_)));
225 }
226
230 ThisType& operator=(const Implementation& value)
231 {
233 phasePresence_ = value.phasePresence_;
234
235 return *this;
236 }
237
241 ThisType& operator=(Scalar value)
242 {
244
245 phasePresence_ = 0;
246 return *this;
247 }
248
256 Evaluation explicitSaturationValue(unsigned phaseIdx, unsigned timeIdx) const
257 {
258 if (!phaseIsPresent(phaseIdx) || phaseIdx == lowestPresentPhaseIdx()) {
259 // non-present phases have saturation 0
260 return 0.0;
261 }
262
263 const unsigned varIdx = switch0Idx + phaseIdx - 1;
264 if constexpr (std::is_same_v<Evaluation, Scalar>) {
265 return (*this)[varIdx]; // finite differences
266 }
267 else {
268 // automatic differentiation
269 if (timeIdx != 0) {
270 Toolbox::createConstant((*this)[varIdx]);
271 }
272 return Toolbox::createVariable((*this)[varIdx], varIdx);
273 }
274 }
275
279 template <class FluidState>
280 void assignNaive(const FluidState& fluidState)
281 {
282 using FsToolbox = MathToolbox<typename FluidState::ValueType>;
283
284 // assign the phase temperatures. this is out-sourced to
285 // the energy module
286 EnergyModule::setPriVarTemperatures(*this, fluidState);
287
288 // set the pressure of the first phase
289 (*this)[pressure0Idx] = FsToolbox::value(fluidState.pressure(/*phaseIdx=*/0));
290 Valgrind::CheckDefined((*this)[pressure0Idx]);
291
292 // determine the phase presence.
293 phasePresence_ = 0;
294 for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
295 // use a NCP condition to determine if the phase is
296 // present or not
297 Scalar a = 1;
298 for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
299 a -= FsToolbox::value(fluidState.moleFraction(phaseIdx, compIdx));
300 }
301 const Scalar b = FsToolbox::value(fluidState.saturation(phaseIdx));
302
303 if (b > a) {
304 phasePresence_ |= (1 << phaseIdx);
305 }
306 }
307
308 // some phase must be present
309 if (phasePresence_ == 0) {
310 throw NumericalProblem("Phase state was 0, i.e., no fluid is present");
311 }
312
313 // set the primary variables which correspond to mole
314 // fractions of the present phase which has the lowest index.
315 const unsigned lowestPhaseIdx = lowestPresentPhaseIdx();
316 for (unsigned switchIdx = 0; switchIdx < numPhases - 1; ++switchIdx) {
317 unsigned phaseIdx = switchIdx;
318 const unsigned compIdx = switchIdx + 1;
319 if (switchIdx >= lowestPhaseIdx) {
320 ++phaseIdx;
321 }
322
323 if (phaseIsPresent(phaseIdx)) {
324 (*this)[switch0Idx + switchIdx] = FsToolbox::value(fluidState.saturation(phaseIdx));
325 Valgrind::CheckDefined((*this)[switch0Idx + switchIdx]);
326 }
327 else {
328 (*this)[switch0Idx + switchIdx] =
329 FsToolbox::value(fluidState.moleFraction(lowestPhaseIdx, compIdx));
330 Valgrind::CheckDefined((*this)[switch0Idx + switchIdx]);
331 }
332 }
333
334 // set the mole fractions in of the remaining components in
335 // the phase with the lowest index
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]);
340 }
341 }
342
346 void print(std::ostream& os) const
347 {
348 os << "(p_" << FluidSystem::phaseName(0) << " = "
349 << (*this)[pressure0Idx];
350 const unsigned lowestPhaseIdx = lowestPresentPhaseIdx();
351 for (unsigned switchIdx = 0; switchIdx < numPhases - 1; ++switchIdx) {
352 unsigned phaseIdx = switchIdx;
353 const unsigned compIdx = switchIdx + 1;
354 if (phaseIdx >= lowestPhaseIdx) {
355 ++phaseIdx; // skip the saturation of the present
356 // phase with the lowest index
357 }
358
359 if (phaseIsPresent(phaseIdx)) {
360 os << ", S_" << FluidSystem::phaseName(phaseIdx) << " = "
361 << (*this)[switch0Idx + switchIdx];
362 }
363 else {
364 os << ", x_" << FluidSystem::phaseName(lowestPhaseIdx) << "^"
365 << FluidSystem::componentName(compIdx) << " = "
366 << (*this)[switch0Idx + switchIdx];
367 }
368 }
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];
373 }
374 os << ")";
375 os << ", phase presence: " << static_cast<int>(phasePresence_) << std::flush;
376 }
377
378private:
379 short phasePresence_{};
380};
381
382} // namespace Opm
383
384#endif
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.