opm-simulators
ncpprimaryvariables.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_NCP_PRIMARY_VARIABLES_HH
29 #define EWOMS_NCP_PRIMARY_VARIABLES_HH
30 
31 #include <dune/common/fvector.hh>
32 
33 #include <opm/material/constraintsolvers/NcpFlash.hpp>
34 #include <opm/material/densead/Math.hpp>
35 #include <opm/material/fluidstates/CompositionalFluidState.hpp>
36 
40 
41 #include <cassert>
42 
43 namespace Opm {
44 
54 template <class TypeTag>
56 {
58 
64 
66  enum { pressure0Idx = Indices::pressure0Idx };
67  enum { saturation0Idx = Indices::saturation0Idx };
68  enum { fugacity0Idx = Indices::fugacity0Idx };
69 
70  enum { numPhases = getPropValue<TypeTag, Properties::NumPhases>() };
71  enum { numComponents = getPropValue<TypeTag, Properties::NumComponents>() };
72  using ComponentVector = Dune::FieldVector<Scalar, numComponents>;
73 
74  enum { enableEnergy = getPropValue<TypeTag, Properties::EnableEnergy>() };
76 
77  using NcpFlash = ::Opm::NcpFlash<Scalar, FluidSystem>;
78  using Toolbox = MathToolbox<Evaluation>;
79 
80 public:
81  NcpPrimaryVariables() = default;
82 
87  NcpPrimaryVariables(const NcpPrimaryVariables& value) = default;
88  NcpPrimaryVariables& operator=(const NcpPrimaryVariables& value) = default;
89 
90  using ParentType::operator=;
91 
95  template <class FluidState>
96  void assignMassConservative(const FluidState& fluidState,
97  const MaterialLawParams& matParams,
98  bool isInEquilibrium = false)
99  {
100  using FsToolbox = MathToolbox<typename FluidState::ValueType>;
101 
102 #ifndef NDEBUG
103  // make sure the temperature is the same in all fluid phases
104  for (unsigned phaseIdx = 1; phaseIdx < numPhases; ++phaseIdx) {
105  assert(fluidState.temperature(0) == fluidState.temperature(phaseIdx));
106  }
107 #endif // NDEBUG
108 
109  // for the equilibrium case, we don't need complicated
110  // computations.
111  if (isInEquilibrium) {
112  assignNaive(fluidState);
113  return;
114  }
115 
116  // use a flash calculation to calculate a fluid state in
117  // thermodynamic equilibrium
118  typename FluidSystem::template ParameterCache<Scalar> paramCache;
119  CompositionalFluidState<Scalar, FluidSystem> fsFlash;
120 
121  // use the externally given fluid state as initial value for
122  // the flash calculation
123  fsFlash.assign(fluidState);
124 
125  // calculate the phase densities
126  paramCache.updateAll(fsFlash);
127  for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
128  const Scalar rho = FluidSystem::density(fsFlash, paramCache, phaseIdx);
129  fsFlash.setDensity(phaseIdx, rho);
130  }
131 
132  // calculate the "global molarities"
133  ComponentVector globalMolarities(0.0);
134  for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
135  for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
136  globalMolarities[compIdx] +=
137  FsToolbox::value(fsFlash.saturation(phaseIdx)) *
138  FsToolbox::value(fsFlash.molarity(phaseIdx, compIdx));
139  }
140  }
141 
142  // run the flash calculation
143  NcpFlash::template solve<MaterialLaw>(fsFlash, matParams, paramCache, globalMolarities);
144 
145  // use the result to assign the primary variables
146  assignNaive(fsFlash);
147  }
148 
155  template <class FluidState>
156  void assignNaive(const FluidState& fluidState, unsigned refPhaseIdx = 0)
157  {
158  using FsToolbox = MathToolbox<typename FluidState::ValueType>;
159 
160  // assign the phase temperatures. this is out-sourced to
161  // the energy module
162  EnergyModule::setPriVarTemperatures(*this, fluidState);
163 
164  // assign fugacities.
165  typename FluidSystem::template ParameterCache<Scalar> paramCache;
166  paramCache.updatePhase(fluidState, refPhaseIdx);
167  const Scalar pRef = FsToolbox::value(fluidState.pressure(refPhaseIdx));
168  for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
169  // we always compute the fugacities because they are quite exotic quantities
170  // and this easily forgotten to be specified
171  const Scalar fugCoeff =
172  FluidSystem::template fugacityCoefficient<FluidState, Scalar>(fluidState,
173  paramCache,
174  refPhaseIdx,
175  compIdx);
176  (*this)[fugacity0Idx + compIdx] =
177  fugCoeff * fluidState.moleFraction(refPhaseIdx, compIdx) * pRef;
178  }
179 
180  // assign pressure of first phase
181  (*this)[pressure0Idx] = FsToolbox::value(fluidState.pressure(/*phaseIdx=*/0));
182 
183  // assign first M - 1 saturations
184  for (unsigned phaseIdx = 0; phaseIdx < numPhases - 1; ++phaseIdx) {
185  (*this)[saturation0Idx + phaseIdx] = FsToolbox::value(fluidState.saturation(phaseIdx));
186  }
187  }
188 };
189 
190 } // namespace Opm
191 
192 #endif
void assignMassConservative(const FluidState &fluidState, const MaterialLawParams &matParams, bool isInEquilibrium=false)
< Import base class assignment operators.
Definition: ncpprimaryvariables.hh:96
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
Represents the primary variables used by the compositional multi-phase NCP model. ...
Definition: ncpprimaryvariables.hh:55
Contains the classes required to consider energy as a conservation quantity in a multi-phase module...
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
Declares the properties required for the NCP compositional multi-phase model.
Represents the primary variables used by the a model.
Definition: fvbaseprimaryvariables.hh:51
void assignNaive(const FluidState &fluidState, unsigned refPhaseIdx=0)
Directly retrieve the primary variables from an arbitrary fluid state.
Definition: ncpprimaryvariables.hh:156
Represents the primary variables used by the a model.
Provides the auxiliary methods required for consideration of the energy equation. ...
Definition: energymodule.hh:54