ptflash/flashintensivequantities.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 OPM_FLASH_INTENSIVE_QUANTITIES_HH
29#define OPM_FLASH_INTENSIVE_QUANTITIES_HH
30
31#include <dune/common/fmatrix.hh>
32#include <dune/common/fvector.hh>
33
34#include <opm/common/OpmLog/OpmLog.hpp>
35
36#include <opm/material/Constants.hpp>
37#include <opm/material/common/Valgrind.hpp>
38#include <opm/material/fluidstates/CompositionalFluidState.hpp>
39
42
44
47
48#include <fmt/format.h>
49
50#include <array>
51#include <iterator>
52#include <string>
53
54namespace Opm {
55
62template <class TypeTag>
63class FlashIntensiveQuantities
64 : public GetPropType<TypeTag, Properties::DiscIntensiveQuantities>
65 , public DiffusionIntensiveQuantities<TypeTag, getPropValue<TypeTag, Properties::EnableDiffusion>() >
66 , public EnergyIntensiveQuantities<TypeTag, getPropValue<TypeTag, Properties::EnableEnergy>() >
67 , public GetPropType<TypeTag, Properties::FluxModule>::FluxIntensiveQuantities
68{
69 using ParentType = GetPropType<TypeTag, Properties::DiscIntensiveQuantities>;
70
71 using ElementContext = GetPropType<TypeTag, Properties::ElementContext>;
72 using MaterialLaw = GetPropType<TypeTag, Properties::MaterialLaw>;
73 using MaterialLawParams = GetPropType<TypeTag, Properties::MaterialLawParams>;
74 using Indices = GetPropType<TypeTag, Properties::Indices>;
75 using FluxModule = GetPropType<TypeTag, Properties::FluxModule>;
76 using GridView = GetPropType<TypeTag, Properties::GridView>;
77 using ThreadManager = GetPropType<TypeTag, Properties::ThreadManager>;
78
79 // primary variable indices
80 enum { z0Idx = Indices::z0Idx };
81 enum { numPhases = getPropValue<TypeTag, Properties::NumPhases>() };
82 enum { numComponents = getPropValue<TypeTag, Properties::NumComponents>() };
83 static constexpr bool enableDiffusion = getPropValue<TypeTag, Properties::EnableDiffusion>();
84 static constexpr bool enableEnergy = getPropValue<TypeTag, Properties::EnableEnergy>();
85 enum { dimWorld = GridView::dimensionworld };
86 enum { pressure0Idx = Indices::pressure0Idx };
87 enum { water0Idx = Indices::water0Idx};
88
89 static constexpr bool waterEnabled = Indices::waterEnabled;
90
91 using Scalar = GetPropType<TypeTag, Properties::Scalar>;
92 using Evaluation = GetPropType<TypeTag, Properties::Evaluation>;
93 using FluidSystem = GetPropType<TypeTag, Properties::FluidSystem>;
94 using FlashSolver = GetPropType<TypeTag, Properties::FlashSolver>;
95
96 using ComponentVector = Dune::FieldVector<Evaluation, numComponents>;
97 using DimMatrix = Dune::FieldMatrix<Scalar, dimWorld, dimWorld>;
98
99 using DiffusionIntensiveQuantities = ::Opm::DiffusionIntensiveQuantities<TypeTag, enableDiffusion>;
100 using EnergyIntensiveQuantities = ::Opm::EnergyIntensiveQuantities<TypeTag, enableEnergy>;
101 using FluxIntensiveQuantities = typename FluxModule::FluxIntensiveQuantities;
102
103public:
105 using FluidState = CompositionalFluidState<Evaluation, FluidSystem, enableEnergy>;
106
108
110
112
116 void update(const ElementContext& elemCtx, unsigned dofIdx, unsigned timeIdx)
117 {
118 ParentType::update(elemCtx, dofIdx, timeIdx);
119 EnergyIntensiveQuantities::updateTemperatures_(fluidState_, elemCtx, dofIdx, timeIdx);
120
121 const auto& priVars = elemCtx.primaryVars(dofIdx, timeIdx);
122 const auto& problem = elemCtx.problem();
123
124 const Scalar flashTolerance = Parameters::Get<Parameters::FlashTolerance<Scalar>>();
125 const int flashVerbosity = Parameters::Get<Parameters::FlashVerbosity>();
126 const std::string flashTwoPhaseMethod = Parameters::Get<Parameters::FlashTwoPhaseMethod>();
127 // TODO: the formulation here is still to begin with XMF and YMF values to derive ZMF value
128 // TODO: we should check how we update ZMF in the newton update, since it is the primary variables.
129
130 // extract the total molar densities of the components
131 ComponentVector z(0.);
132 {
133 Evaluation lastZ = 1.0;
134 for (unsigned compIdx = 0; compIdx < numComponents - 1; ++compIdx) {
135 z[compIdx] = priVars.makeEvaluation(z0Idx + compIdx, timeIdx);
136 lastZ -= z[compIdx];
137 }
138 z[numComponents - 1] = lastZ;
139
140 Evaluation sumz = 0.0;
141 for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
142 // Clamp only the value; preserve derivatives. Replacing the Evaluation with
143 // max() when the bound applies removes composition derivatives from a
144 // vanished component's conservation equation and makes the cell Jacobian
145 // block singular.
146 if (z[compIdx] < 1e-8) {
147 z[compIdx].setValue(1e-8);
148 }
149 sumz += z[compIdx];
150 }
151 z /= sumz;
152 }
153
154 for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
155 fluidState_.setMoleFraction(compIdx, z[compIdx]);
156 }
157
158 Evaluation p = priVars.makeEvaluation(pressure0Idx, timeIdx);
159 for (int phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
160 fluidState_.setPressure(phaseIdx, p);
161 }
162
163 // Get initial K and L from storage initially (if enabled)
164 const auto* hint = elemCtx.thermodynamicHint(dofIdx, timeIdx);
165 if (hint) {
166 for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
167 const Evaluation& Ktmp = hint->fluidState().K(compIdx);
168 fluidState_.setKvalue(compIdx, Ktmp);
169 }
170 const Evaluation& Ltmp = hint->fluidState().L();
171 fluidState_.setLvalue(Ltmp);
172 }
173 else if (timeIdx == 0 && elemCtx.thermodynamicHint(dofIdx, 1)) {
174 // checking the storage cache
175 const auto& hint2 = elemCtx.thermodynamicHint(dofIdx, 1);
176 for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
177 const Evaluation& Ktmp = hint2->fluidState().K(compIdx);
178 fluidState_.setKvalue(compIdx, Ktmp);
179 }
180 const Evaluation& Ltmp = hint2->fluidState().L();
181 fluidState_.setLvalue(Ltmp);
182 }
183 else {
184 for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
185 const Evaluation Ktmp = fluidState_.wilsonK_(compIdx);
186 fluidState_.setKvalue(compIdx, Ktmp);
187 }
188 const Evaluation& Ltmp = -1.0;
189 fluidState_.setLvalue(Ltmp);
190 }
191
193 // Compute the phase compositions and densities
195 if (flashVerbosity >= 1) {
196 OpmLog::debug(fmt::format("Updating the intensive quantities for cell {}",
197 elemCtx.globalSpaceIndex(dofIdx, timeIdx)));
198 }
199 const auto& eos_type = problem.getEosType();
200 FlashSolver::solve(fluidState_, flashTwoPhaseMethod, flashTolerance, eos_type, flashVerbosity);
201
202 if (flashVerbosity >= 5) {
203 std::string phaseCompositions;
204 for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
205 fmt::format_to(
206 std::back_inserter(phaseCompositions),
207 " component {}: x = {}, y = {}\n",
208 compIdx,
209 getValue(fluidState_.moleFraction(FluidSystem::oilPhaseIdx, compIdx)),
210 getValue(fluidState_.moleFraction(FluidSystem::gasPhaseIdx, compIdx)));
211 }
212 OpmLog::debug(fmt::format("After the flash for cell {}: liquid fraction = {}\n{}",
213 elemCtx.globalSpaceIndex(dofIdx, timeIdx),
214 getValue(fluidState_.L()),
215 phaseCompositions));
216 }
217
218 // Update phases
219 typename FluidSystem::template ParameterCache<Evaluation> paramCache(eos_type);
220 paramCache.updatePhase(fluidState_, FluidSystem::oilPhaseIdx);
221 paramCache.updatePhase(fluidState_, FluidSystem::gasPhaseIdx);
222
223 // Update saturation
224 Evaluation Sw = 0.0;
225 if constexpr (waterEnabled) {
226 Sw = priVars.makeEvaluation(water0Idx, timeIdx);
227 }
228 const Evaluation L = fluidState_.L();
229 const Evaluation Vm_L = paramCache.correctedMolarVolume(FluidSystem::oilPhaseIdx);
230 const Evaluation Vm_V = paramCache.correctedMolarVolume(FluidSystem::gasPhaseIdx);
231 Evaluation So = max((1 - Sw) * (L * Vm_L / ( L * Vm_L + (1 - L) * Vm_V)), 0.0);
232 Evaluation Sg = max(1 - So - Sw, 0.0);
233 const Scalar sumS = getValue(So) + getValue(Sg) + getValue(Sw);
234 So /= sumS;
235 Sg /= sumS;
236
237 fluidState_.setSaturation(FluidSystem::oilPhaseIdx, So);
238 fluidState_.setSaturation(FluidSystem::gasPhaseIdx, Sg);
239 if constexpr (waterEnabled) {
240 Sw /= sumS;
241 fluidState_.setSaturation(FluidSystem::waterPhaseIdx, Sw);
242 }
243
244 // The compressibility factor comes from the unshifted EOS root, unlike
245 // the saturations above.
246 const Scalar R = Opm::Constants<Scalar>::R;
247 const Evaluation Z_L = (paramCache.molarVolume(FluidSystem::oilPhaseIdx) *
248 fluidState_.pressure(FluidSystem::oilPhaseIdx)) /
249 (R * fluidState_.temperature(FluidSystem::oilPhaseIdx));
250 const Evaluation Z_V = (paramCache.molarVolume(FluidSystem::gasPhaseIdx) *
251 fluidState_.pressure(FluidSystem::gasPhaseIdx)) /
252 (R * fluidState_.temperature(FluidSystem::gasPhaseIdx));
253 fluidState_.setCompressFactor(FluidSystem::oilPhaseIdx, Z_L);
254 fluidState_.setCompressFactor(FluidSystem::gasPhaseIdx, Z_V);
255
256 if (flashVerbosity >= 5) {
257 OpmLog::debug(fmt::format("Flash phase properties for cell {}: "
258 "oil saturation = {}, gas saturation = {}, "
259 "oil molar volume = {}, gas molar volume = {}",
260 elemCtx.globalSpaceIndex(dofIdx, timeIdx),
261 getValue(So),
262 getValue(Sg),
263 getValue(Vm_L),
264 getValue(Vm_V)));
265 }
266
268 // Compute rel. perm and viscosity and densities
270 const MaterialLawParams& materialParams = problem.materialLawParams(elemCtx, dofIdx, timeIdx);
271
272 // calculate relative permeability
273 MaterialLaw::relativePermeabilities(relativePermeability_,
274 materialParams, fluidState_);
275 Valgrind::CheckDefined(relativePermeability_);
276
277 // set the phase viscosity and density
278 for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
279 if (phaseIdx == static_cast<unsigned int>(FluidSystem::oilPhaseIdx) ||
280 phaseIdx == static_cast<unsigned int>(FluidSystem::gasPhaseIdx))
281 {
282 paramCache.updatePhase(fluidState_, phaseIdx);
283 }
284
285 const Evaluation& mu = FluidSystem::viscosity(fluidState_, paramCache, phaseIdx);
286
287 fluidState_.setViscosity(phaseIdx, mu);
288
289 mobility_[phaseIdx] = relativePermeability_[phaseIdx] / mu;
290 Valgrind::CheckDefined(mobility_[phaseIdx]);
291
292 const Evaluation& rho = FluidSystem::density(fluidState_, paramCache, phaseIdx);
293 fluidState_.setDensity(phaseIdx, rho);
294 }
295
297 // Compute the remaining quantities
299
300 // porosity
301 porosity_ = problem.porosity(elemCtx, dofIdx, timeIdx);
302 Valgrind::CheckDefined(porosity_);
303
304 // intrinsic permeability
305 intrinsicPerm_ = problem.intrinsicPermeability(elemCtx, dofIdx, timeIdx);
306
307 // update the quantities specific for the velocity model
308 FluxIntensiveQuantities::update_(elemCtx, dofIdx, timeIdx);
309
310 // energy related quantities
311 EnergyIntensiveQuantities::update_(fluidState_, paramCache, elemCtx, dofIdx, timeIdx);
312
313 // update the diffusion specific quantities of the intensive quantities
314 DiffusionIntensiveQuantities::update_(fluidState_, paramCache, elemCtx, dofIdx, timeIdx);
315 }
316
320 const FluidState& fluidState() const
321 { return fluidState_; }
322
326 const DimMatrix& intrinsicPermeability() const
327 { return intrinsicPerm_; }
328
332 const Evaluation& relativePermeability(unsigned phaseIdx) const
333 { return relativePermeability_[phaseIdx]; }
334
338 const Evaluation& mobility(unsigned phaseIdx) const
339 { return mobility_[phaseIdx]; }
340
344 const Evaluation& porosity() const
345 { return porosity_; }
346
347private:
348 DimMatrix intrinsicPerm_;
349 FluidState fluidState_;
350 Evaluation porosity_;
351 std::array<Evaluation,numPhases> relativePermeability_;
352 std::array<Evaluation,numPhases> mobility_;
353};
354
355} // namespace Opm
356
357#endif
Provides the volumetric quantities required for the calculation of molecular diffusive fluxes.
Definition: diffusionmodule.hh:143
Provides the volumetric quantities required for the energy equation.
Definition: energymodule.hh:536
Contains the intensive quantities of the flash-based compositional multi-phase model.
Definition: flash/flashintensivequantities.hh:60
const Evaluation & mobility(unsigned phaseIdx) const
Returns the effective mobility of a given phase within the control volume.
Definition: ptflash/flashintensivequantities.hh:338
const DimMatrix & intrinsicPermeability() const
Returns the intrinsic permeability tensor a degree of freedom.
Definition: ptflash/flashintensivequantities.hh:326
FlashIntensiveQuantities(const FlashIntensiveQuantities &other)=default
const FluidState & fluidState() const
Returns the phase state for the control-volume.
Definition: ptflash/flashintensivequantities.hh:320
const Evaluation & porosity() const
Returns the average porosity within the control volume.
Definition: ptflash/flashintensivequantities.hh:344
const Evaluation & relativePermeability(unsigned phaseIdx) const
Returns the relative permeability of a given phase within the control volume.
Definition: ptflash/flashintensivequantities.hh:332
CompositionalFluidState< Evaluation, FluidSystem, enableEnergy > FluidState
The type of the object returned by the fluidState() method.
Definition: flash/flashintensivequantities.hh:93
void update(const ElementContext &elemCtx, unsigned dofIdx, unsigned timeIdx)
Definition: ptflash/flashintensivequantities.hh:116
FlashIntensiveQuantities & operator=(const FlashIntensiveQuantities &other)=default
Classes required for molecular diffusion.
Contains the classes required to consider energy as a conservation quantity in a multi-phase module.
Declares the properties required by the compositional multi-phase model based on flash calculations.
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 parameters for the compositional multi-phase model based on flash calculations.