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