opm-common
PTFlashParameterCache.hpp
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  Copyright 2022 NORCE.
5  Copyright 2022 SINTEF Digital, Mathematics and Cybernetics.
6 
7  This file is part of the Open Porous Media project (OPM).
8 
9  OPM is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 2 of the License, or
12  (at your option) any later version.
13 
14  OPM is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OPM. If not, see <http://www.gnu.org/licenses/>.
21 
22  Consult the COPYING file in the top-level source directory of this
23  module for the precise wording of the license and the list of
24  copyright holders.
25 */
30 #ifndef OPM_PTFlash_PARAMETER_CACHE_HPP
31 #define OPM_PTFlash_PARAMETER_CACHE_HPP
32 
35 #include <opm/material/eos/CubicEOS.hpp>
36 #include <opm/material/eos/CubicEOSParams.hpp>
37 
38 #include <opm/input/eclipse/EclipseState/Compositional/CompositionalConfig.hpp>
39 
40 #include <cassert>
41 
42 namespace Opm {
43 
48 template <class Scalar, class FluidSystem>
50  : public Opm::ParameterCacheBase<PTFlashParameterCache<Scalar, FluidSystem> >
51 {
55  using EOSType = CompositionalConfig::EOSType;
56 
57  enum { numPhases = FluidSystem::numPhases };
58  enum { oilPhaseIdx = FluidSystem::oilPhaseIdx };
59  enum { gasPhaseIdx = FluidSystem::gasPhaseIdx };
60  enum { waterPhaseIdx = FluidSystem::waterPhaseIdx };
61 
62  static constexpr bool waterEnabled = FluidSystem::waterEnabled;
63 
64  static_assert(static_cast<int>(oilPhaseIdx) >= 0, "Oil phase index must be non-negative");
65  static_assert(static_cast<int>(oilPhaseIdx) < static_cast<int>(numPhases),
66  "Oil phase index must be strictly less than FluidSystem's number of phases");
67 
68  static_assert(static_cast<int>(gasPhaseIdx) >= 0, "Gas phase index must be non-negative");
69  static_assert(static_cast<int>(gasPhaseIdx) < static_cast<int>(numPhases),
70  "Gas phase index must be strictly less than FluidSystem's number of phases");
71 
72 public:
75 
78 
79  explicit PTFlashParameterCache(EOSType eos_type)
80  {
81  VmUpToDate_[oilPhaseIdx] = false;
82  Valgrind::SetUndefined(Vm_[oilPhaseIdx]);
83  VmUpToDate_[gasPhaseIdx] = false;
84  Valgrind::SetUndefined(Vm_[gasPhaseIdx]);
85 
86  oilPhaseParams_.setEOSType(eos_type);
87  gasPhaseParams_.setEOSType(eos_type);
88  }
89 
91  template <class FluidState>
92  void updatePhase(const FluidState& fluidState,
93  unsigned phaseIdx,
94  int exceptQuantities = ParentType::None)
95  {
96  if (waterEnabled && phaseIdx == static_cast<unsigned int>(waterPhaseIdx)) {
97  return;
98  }
99 
100  assert ((phaseIdx == static_cast<unsigned int>(oilPhaseIdx)) ||
101  (phaseIdx == static_cast<unsigned int>(gasPhaseIdx)));
102 
103  updateEosParams(fluidState, phaseIdx, exceptQuantities);
104 
105  // update the phase's molar volume
106  updateMolarVolume_(fluidState, phaseIdx);
107  }
108 
110  template <class FluidState>
111  void updateSingleMoleFraction(const FluidState& fluidState,
112  unsigned phaseIdx,
113  unsigned compIdx)
114  {
115  if (phaseIdx == oilPhaseIdx)
116  oilPhaseParams_.updateSingleMoleFraction(fluidState, compIdx);
117  if (phaseIdx == gasPhaseIdx)
118  gasPhaseParams_.updateSingleMoleFraction(fluidState, compIdx);
119  else
120  return;
121 
122  // update the phase's molar volume
123  updateMolarVolume_(fluidState, phaseIdx);
124  }
125 
126  Scalar A(unsigned phaseIdx) const
127  {
128  switch (phaseIdx)
129  {
130  case oilPhaseIdx: return oilPhaseParams_.A();
131  case gasPhaseIdx: return gasPhaseParams_.A();
132  default:
133  throw std::logic_error("The A parameter is only defined for "
134  "oil and gas phases");
135  };
136  }
137 
138  Scalar B(unsigned phaseIdx) const
139  {
140  switch (phaseIdx)
141  {
142  case oilPhaseIdx: return oilPhaseParams_.B();
143  case gasPhaseIdx: return gasPhaseParams_.B();
144  default:
145  throw std::logic_error("The B parameter is only defined for "
146  "oil and gas phases");
147  };
148  }
149 
150  Scalar Bi(unsigned phaseIdx, unsigned compIdx) const
151  {
152  switch (phaseIdx)
153  {
154  case oilPhaseIdx: return oilPhaseParams_.Bi(compIdx);
155  case gasPhaseIdx: return gasPhaseParams_.Bi(compIdx);
156  default:
157  throw std::logic_error("The Bi parameter is only defined for "
158  "oil and gas phases");
159  };
160  }
161 
162  Scalar m1(unsigned phaseIdx) const
163  {
164  switch (phaseIdx)
165  {
166  case oilPhaseIdx: return oilPhaseParams_.m1();
167  case gasPhaseIdx: return gasPhaseParams_.m1();
168  default:
169  throw std::logic_error("The m1 parameter is only defined for "
170  "oil and gas phases");
171  };
172  }
173 
174  Scalar m2(unsigned phaseIdx) const
175  {
176  switch (phaseIdx)
177  {
178  case oilPhaseIdx: return oilPhaseParams_.m2();
179  case gasPhaseIdx: return gasPhaseParams_.m2();
180  default:
181  throw std::logic_error("The m2 parameter is only defined for "
182  "oil and gas phases");
183  };
184  }
185 
191  Scalar a(unsigned phaseIdx) const
192  {
193  switch (phaseIdx)
194  {
195  case oilPhaseIdx: return oilPhaseParams_.a();
196  case gasPhaseIdx: return gasPhaseParams_.a();
197  default:
198  throw std::logic_error("The a() parameter is only defined for "
199  "oil and gas phases");
200  };
201  }
202 
208  Scalar b(unsigned phaseIdx) const
209  {
210  switch (phaseIdx)
211  {
212  case oilPhaseIdx: return oilPhaseParams_.b();
213  case gasPhaseIdx: return gasPhaseParams_.b();
214  default:
215  throw std::logic_error("The b() parameter is only defined for "
216  "oil and gas phase");
217  };
218  }
219 
228  Scalar aPure(unsigned phaseIdx, unsigned compIdx) const
229  {
230  switch (phaseIdx)
231  {
232  case oilPhaseIdx: return oilPhaseParams_.pureParams(compIdx).a();
233  case gasPhaseIdx: return gasPhaseParams_.pureParams(compIdx).a();
234  default:
235  throw std::logic_error("The a() parameter is only defined for "
236  "oil and gas phase");
237  };
238  }
239 
247  Scalar bPure(unsigned phaseIdx, unsigned compIdx) const
248  {
249  switch (phaseIdx)
250  {
251  case oilPhaseIdx: return oilPhaseParams_.pureParams(compIdx).b();
252  case gasPhaseIdx: return gasPhaseParams_.pureParams(compIdx).b();
253  default:
254  throw std::logic_error("The b() parameter is only defined for "
255  "oil and gas phase");
256  };
257  }
258 
266  Scalar aCache(unsigned phaseIdx, unsigned compIdx, unsigned compJIdx) const
267  {
268  switch (phaseIdx)
269  {
270  case oilPhaseIdx: return oilPhaseParams_.aCache(compIdx, compJIdx);
271  case gasPhaseIdx: return gasPhaseParams_.aCache(compIdx, compJIdx);
272  default:
273  throw std::logic_error("The aCache parameter is only defined for "
274  "oil and gas phase");
275  };
276  }
277 
283  Scalar molarVolume(unsigned phaseIdx) const
284  {
285  assert(VmUpToDate_[phaseIdx]);
286  return Vm_[phaseIdx];
287  }
288 
289 
295  { return oilPhaseParams_; }
296 
302  // { throw std::invalid_argument("gas phase does not exist");}
303  { return gasPhaseParams_; }
304 
313  template <class FluidState>
314  void updateEosParams(const FluidState& fluidState,
315  unsigned phaseIdx,
316  int exceptQuantities = ParentType::None)
317  {
318  assert ((phaseIdx == static_cast<unsigned int>(oilPhaseIdx)) ||
319  (phaseIdx == static_cast<unsigned int>(gasPhaseIdx)));
320 
321  if (!(exceptQuantities & ParentType::Temperature))
322  {
323  updatePure_(fluidState, phaseIdx);
324  updateMix_(fluidState, phaseIdx);
325  VmUpToDate_[phaseIdx] = false;
326  }
327  else if (!(exceptQuantities & ParentType::Composition))
328  {
329  updateMix_(fluidState, phaseIdx);
330  VmUpToDate_[phaseIdx] = false;
331  }
332  else if (!(exceptQuantities & ParentType::Pressure)) {
333  VmUpToDate_[phaseIdx] = false;
334  }
335  }
336 
337 protected:
344  template <class FluidState>
345  void updatePure_(const FluidState& fluidState, unsigned phaseIdx)
346  {
347  Scalar T = decay<Scalar>(fluidState.temperature(phaseIdx));
348  Scalar p = decay<Scalar>(fluidState.pressure(phaseIdx));
349 
350  switch (phaseIdx)
351  {
352  case oilPhaseIdx: oilPhaseParams_.updatePure(T, p); break;
353  case gasPhaseIdx: gasPhaseParams_.updatePure(T, p); break;
354  }
355  }
356 
364  template <class FluidState>
365  void updateMix_(const FluidState& fluidState, unsigned phaseIdx)
366  {
367  Valgrind::CheckDefined(fluidState.averageMolarMass(phaseIdx));
368  switch (phaseIdx)
369  {
370  case oilPhaseIdx:
371  oilPhaseParams_.updateMix(fluidState);
372  break;
373  case gasPhaseIdx:
374  gasPhaseParams_.updateMix(fluidState);
375  break;
376  }
377  }
378 
379  template <class FluidState>
380  void updateMolarVolume_(const FluidState& fluidState,
381  unsigned phaseIdx)
382  {
383  VmUpToDate_[phaseIdx] = true;
384 
385  // calculate molar volume of the phase (we will need this for the
386  // fugacity coefficients and the density anyway)
387  switch (phaseIdx) {
388  case gasPhaseIdx: {
389  // calculate molar volumes for the given composition. although
390  // this isn't a Peng-Robinson parameter strictly speaking, the
391  // molar volume appears in basically every quantity the fluid
392  // system can get queried, so it is okay to calculate it
393  // here...
394  Vm_[gasPhaseIdx] = decay<Scalar> (
395  CubicEOS::computeMolarVolume(fluidState,
396  *this,
397  phaseIdx,
398  /*isGasPhase=*/true) );
399  break;
400  }
401  case oilPhaseIdx: {
402  // calculate molar volumes for the given composition. although
403  // this isn't a Peng-Robinson parameter strictly speaking, the
404  // molar volume appears in basically every quantity the fluid
405  // system can get queried, so it is okay to calculate it
406  // here...
407  Vm_[oilPhaseIdx] = decay<Scalar> (
408  CubicEOS::computeMolarVolume(fluidState,
409  *this,
410  phaseIdx,
411  /*isGasPhase=*/false) );
412 
413  break;
414  }
415  };
416  }
417 
418  bool VmUpToDate_[numPhases];
419  Scalar Vm_[numPhases];
420 
421  OilPhaseParams oilPhaseParams_;
422  GasPhaseParams gasPhaseParams_;
423 };
424 
425 } // namespace Opm
426 
427 #endif
Scalar bPure(unsigned phaseIdx, unsigned compIdx) const
The Peng-Robinson covolume for a pure component given the same temperature and pressure of the phase...
Definition: PTFlashParameterCache.hpp:247
const OilPhaseParams & oilPhaseParams() const
Returns the Peng-Robinson mixture parameters for the oil phase.
Definition: PTFlashParameterCache.hpp:294
void updateMix_(const FluidState &fluidState, unsigned phaseIdx)
Update all parameters of a phase which depend on the fluid composition.
Definition: PTFlashParameterCache.hpp:365
Specifies the parameter cache used by the SPE-5 fluid system.
Definition: PTFlashParameterCache.hpp:49
void updateSingleMoleFraction(const FluidState &fluidState, unsigned phaseIdx, unsigned compIdx)
Update all cached parameters of a specific fluid phase which depend on the mole fraction of a single ...
Definition: PTFlashParameterCache.hpp:111
void updatePure_(const FluidState &fluidState, unsigned phaseIdx)
Update all parameters of a phase which only depend on temperature and/or pressure.
Definition: PTFlashParameterCache.hpp:345
void updatePhase(const FluidState &fluidState, unsigned phaseIdx, int exceptQuantities=ParentType::None)
Update all cached parameters of a specific fluid phase.
Definition: PTFlashParameterCache.hpp:92
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition: Exceptions.hpp:30
Scalar molarVolume(unsigned phaseIdx) const
Returns the molar volume of a phase [m^3/mol].
Definition: PTFlashParameterCache.hpp:283
Definition: CubicEOS.hpp:33
The temperature has not been modified.
Definition: ParameterCacheBase.hpp:48
The compositions have not been modified.
Definition: ParameterCacheBase.hpp:54
All quantities have been (potentially) modified.
Definition: ParameterCacheBase.hpp:45
Scalar a(unsigned phaseIdx) const
The Peng-Robinson attractive parameter for a phase.
Definition: PTFlashParameterCache.hpp:191
Opm::CubicEOSParams< Scalar, FluidSystem, oilPhaseIdx > OilPhaseParams
The cached parameters for the oil phase.
Definition: PTFlashParameterCache.hpp:74
The pressures have not been modified.
Definition: ParameterCacheBase.hpp:51
Scalar aPure(unsigned phaseIdx, unsigned compIdx) const
The Peng-Robinson attractive parameter for a pure component given the same temperature and pressure o...
Definition: PTFlashParameterCache.hpp:228
Scalar aCache(unsigned phaseIdx, unsigned compIdx, unsigned compJIdx) const
TODO.
Definition: PTFlashParameterCache.hpp:266
The base class of the parameter caches of fluid systems.
Definition: ParameterCacheBase.hpp:37
void updateEosParams(const FluidState &fluidState, unsigned phaseIdx, int exceptQuantities=ParentType::None)
Update all parameters required by the equation of state to calculate some quantities for the phase...
Definition: PTFlashParameterCache.hpp:314
Scalar b(unsigned phaseIdx) const
The Peng-Robinson covolume for a phase.
Definition: PTFlashParameterCache.hpp:208
Opm::CubicEOSParams< Scalar, FluidSystem, gasPhaseIdx > GasPhaseParams
The cached parameters for the gas phase.
Definition: PTFlashParameterCache.hpp:77
Some templates to wrap the valgrind client request macros.
The base class of the parameter caches of fluid systems.
const GasPhaseParams & gasPhaseParams() const
Returns the Peng-Robinson mixture parameters for the gas phase.
Definition: PTFlashParameterCache.hpp:301