Loading...
Searching...
No Matches
FluidStateCompositionModules.hpp
Go to the documentation of this file.
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
Consult the COPYING file in the top-level source directory of this
module for the precise wording of the license and the list of
copyright holders.
*/
#ifndef OPM_FLUID_STATE_COMPOSITION_MODULES_HPP
#define OPM_FLUID_STATE_COMPOSITION_MODULES_HPP
#include <opm/material/common/Valgrind.hpp>
#include <opm/material/common/MathToolbox.hpp>
#include <algorithm>
#include <array>
#include <cmath>
namespace Opm {
template <class Scalar,
class FluidSystem,
class Implementation>
class FluidStateExplicitCompositionModule
{
enum { numPhases = FluidSystem::numPhases };
enum { numComponents = FluidSystem::numComponents };
public:
FluidStateExplicitCompositionModule()
{
for (int phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx)
for (int compIdx = 0; compIdx < numComponents; ++compIdx)
moleFraction_[phaseIdx][compIdx] = 0.0;
// TODO: possilby we should begin with totalMoleFractions_ here
// the current implementation assuming we have the moleFractions_ for each
// individual phases first.
Valgrind::SetDefined(moleFraction_);
Valgrind::SetUndefined(averageMolarMass_);
Valgrind::SetUndefined(sumMoleFractions_);
}
const Scalar& moleFraction(unsigned phaseIdx, unsigned compIdx) const
{ return moleFraction_[phaseIdx][compIdx]; }
const Scalar& moleFraction(unsigned compIdx) const
{ return totalModelFractions_[compIdx]; }
Scalar massFraction(unsigned phaseIdx, unsigned compIdx) const
{
return
abs(sumMoleFractions_[phaseIdx])
*moleFraction_[phaseIdx][compIdx]
*FluidSystem::molarMass(compIdx)
/ max(1e-40, abs(averageMolarMass_[phaseIdx]));
}
const Scalar& averageMolarMass(unsigned phaseIdx) const
{ return averageMolarMass_[phaseIdx]; }
Scalar molarity(unsigned phaseIdx, unsigned compIdx) const
{ return asImp_().molarDensity(phaseIdx)*moleFraction(phaseIdx, compIdx); }
void setMoleFraction(unsigned phaseIdx, unsigned compIdx, const Scalar& value)
{
Valgrind::CheckDefined(value);
Valgrind::SetUndefined(sumMoleFractions_[phaseIdx]);
Valgrind::SetUndefined(averageMolarMass_[phaseIdx]);
Valgrind::SetUndefined(moleFraction_[phaseIdx][compIdx]);
moleFraction_[phaseIdx][compIdx] = value;
// re-calculate the mean molar mass
sumMoleFractions_[phaseIdx] = 0.0;
averageMolarMass_[phaseIdx] = 0.0;
for (unsigned compJIdx = 0; compJIdx < numComponents; ++compJIdx) {
sumMoleFractions_[phaseIdx] += moleFraction_[phaseIdx][compJIdx];
averageMolarMass_[phaseIdx] += moleFraction_[phaseIdx][compJIdx]*FluidSystem::molarMass(compJIdx);
}
}
void setMoleFraction(unsigned compIdx, const Scalar& value)
{
Valgrind::CheckDefined(value);
totalModelFractions_[compIdx] = value;
}
void setCompressFactor(unsigned phaseIdx, const Scalar& value) {
Valgrind::CheckDefined(value);
Z_[phaseIdx] = value;
}
Scalar compressFactor(unsigned phaseIdx) const {
return Z_[phaseIdx];
}
template <class FluidState>
void assign(const FluidState& fs)
{
for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) {
averageMolarMass_[phaseIdx] = 0;
sumMoleFractions_[phaseIdx] = 0;
for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) {
moleFraction_[phaseIdx][compIdx] =
decay<Scalar>(fs.moleFraction(phaseIdx, compIdx));
averageMolarMass_[phaseIdx] += moleFraction_[phaseIdx][compIdx]*FluidSystem::molarMass(compIdx);
sumMoleFractions_[phaseIdx] += moleFraction_[phaseIdx][compIdx];
}
}
}
void checkDefined() const
{
Valgrind::CheckDefined(moleFraction_);
Valgrind::CheckDefined(averageMolarMass_);
Valgrind::CheckDefined(sumMoleFractions_);
Valgrind::CheckDefined(K_);
Valgrind::CheckDefined(L_);
}
const Scalar& K(unsigned compIdx) const
{
return K_[compIdx];
}
void setKvalue(unsigned compIdx, const Scalar& value)
{
K_[compIdx] = value;
}
const Scalar& L() const
{
return L_;
}
void setLvalue(const Scalar& value)
{
L_ = value;
}
Scalar wilsonK_(unsigned compIdx) const
{
const auto& acf = FluidSystem::acentricFactor(compIdx);
const auto& T_crit = FluidSystem::criticalTemperature(compIdx);
const auto& T = asImp_().temperature(0);
const auto& p_crit = FluidSystem::criticalPressure(compIdx);
const auto& p = asImp_().pressure(0); //for now assume no capillary pressure
const auto tmp = exp(5.37 * (1+acf) * (1-T_crit/T)) * (p_crit/p);
return tmp;
}
protected:
const Implementation& asImp_() const
{
return *static_cast<const Implementation*>(this);
}
std::array<std::array<Scalar,numComponents>,numPhases> moleFraction_{};
std::array<Scalar,numPhases> averageMolarMass_{};
std::array<Scalar,numPhases> sumMoleFractions_{}; // per phase
std::array<Scalar, numComponents> totalModelFractions_{}; // total mole fractions for each component
std::array<Scalar,numPhases> Z_{};
std::array<Scalar,numComponents> K_{};
Scalar L_{};
};
template <class Scalar,
class FluidSystem,
class Implementation>
class FluidStateImmiscibleCompositionModule
{
enum { numPhases = FluidSystem::numPhases };
public:
enum { numComponents = FluidSystem::numComponents };
static_assert(static_cast<int>(numPhases) == static_cast<int>(numComponents),
"The number of phases must be the same as the number of (pseudo-) components if you assume immiscibility");
FluidStateImmiscibleCompositionModule() = default;
Scalar moleFraction(unsigned phaseIdx, unsigned compIdx) const
{ return (phaseIdx == compIdx)?1.0:0.0; }
Scalar massFraction(unsigned phaseIdx, unsigned compIdx) const
{ return (phaseIdx == compIdx)?1.0:0.0; }
Scalar averageMolarMass(unsigned phaseIdx) const
{ return FluidSystem::molarMass(/*compIdx=*/phaseIdx); }
Scalar molarity(unsigned phaseIdx, unsigned compIdx) const
{ return asImp_().molarDensity(phaseIdx)*moleFraction(phaseIdx, compIdx); }
template <class FluidState>
void assign(const FluidState& /* fs */)
{ }
void checkDefined() const
{ }
protected:
const Implementation& asImp_() const
{ return *static_cast<const Implementation*>(this); }
};
template <class Scalar>
class FluidStateNullCompositionModule
{
public:
enum { numComponents = 0 };
FluidStateNullCompositionModule() = default;
Scalar moleFraction(unsigned /* phaseIdx */, unsigned /* compIdx */) const
{ throw std::logic_error("Mole fractions are not provided by this fluid state"); }
Scalar massFraction(unsigned /* phaseIdx */, unsigned /* compIdx */) const
{ throw std::logic_error("Mass fractions are not provided by this fluid state"); }
Scalar averageMolarMass(unsigned /* phaseIdx */) const
{ throw std::logic_error("Mean molar masses are not provided by this fluid state"); }
Scalar molarity(unsigned /* phaseIdx */, unsigned /* compIdx */) const
{ throw std::logic_error("Molarities are not provided by this fluid state"); }
void checkDefined() const
{ }
};
} // namespace Opm
#endif
MathToolbox.hpp
A traits class which provides basic mathematical functions for arbitrary scalar floating point values...
Valgrind.hpp
Some templates to wrap the valgrind client request macros.
Opm::Valgrind::SetDefined
OPM_HOST_DEVICE void SetDefined(const T &value)
Make the memory on which an object resides defined.
Definition Valgrind.hpp:225
Opm::Valgrind::SetUndefined
OPM_HOST_DEVICE void SetUndefined(const T &value)
Make the memory on which an object resides undefined in valgrind runs.
Definition Valgrind.hpp:174
Opm::Valgrind::CheckDefined
OPM_HOST_DEVICE bool CheckDefined(const T &value)
Make valgrind complain if any of the memory occupied by an object is undefined.
Definition Valgrind.hpp:76
Opm::FluidStateExplicitCompositionModule::setLvalue
void setLvalue(const Scalar &value)
Set the L value [-].
Definition FluidStateCompositionModules.hpp:216
Opm::FluidStateExplicitCompositionModule::molarity
Scalar molarity(unsigned phaseIdx, unsigned compIdx) const
The concentration of a component in a phase [mol/m^3].
Definition FluidStateCompositionModules.hpp:111
Opm::FluidStateExplicitCompositionModule::L
const Scalar & L() const
The L value of a composition [-].
Definition FluidStateCompositionModules.hpp:208
Opm::FluidStateExplicitCompositionModule::wilsonK_
Scalar wilsonK_(unsigned compIdx) const
Wilson formula to calculate K.
Definition FluidStateCompositionModules.hpp:225
Opm::FluidStateExplicitCompositionModule::assign
void assign(const FluidState &fs)
Retrieve all parameters from an arbitrary fluid state.
Definition FluidStateCompositionModules.hpp:160
Opm::FluidStateExplicitCompositionModule::averageMolarMass
const Scalar & averageMolarMass(unsigned phaseIdx) const
The mean molar mass of a fluid phase [kg/mol].
Definition FluidStateCompositionModules.hpp:99
Opm::FluidStateExplicitCompositionModule::moleFraction
const Scalar & moleFraction(unsigned phaseIdx, unsigned compIdx) const
The mole fraction of a component in a phase [].
Definition FluidStateCompositionModules.hpp:70
Opm::FluidStateExplicitCompositionModule::moleFraction
const Scalar & moleFraction(unsigned compIdx) const
The total mole fraction of a component [].
Definition FluidStateCompositionModules.hpp:76
Opm::FluidStateExplicitCompositionModule::setMoleFraction
void setMoleFraction(unsigned compIdx, const Scalar &value)
Set the total mole fraction of a component.
Definition FluidStateCompositionModules.hpp:140
Opm::FluidStateExplicitCompositionModule::setMoleFraction
void setMoleFraction(unsigned phaseIdx, unsigned compIdx, const Scalar &value)
Set the mole fraction of a component in a phase [] and update the average molar mass [kg/mol] accordi...
Definition FluidStateCompositionModules.hpp:119
Opm::FluidStateExplicitCompositionModule::setKvalue
void setKvalue(unsigned compIdx, const Scalar &value)
Set the K value of a component [-].
Definition FluidStateCompositionModules.hpp:200
Opm::FluidStateExplicitCompositionModule::massFraction
Scalar massFraction(unsigned phaseIdx, unsigned compIdx) const
The mass fraction of a component in a phase [].
Definition FluidStateCompositionModules.hpp:82
Opm::FluidStateExplicitCompositionModule::checkDefined
void checkDefined() const
Make sure that all attributes are defined.
Definition FluidStateCompositionModules.hpp:183
Opm::FluidStateImmiscibleCompositionModule::massFraction
Scalar massFraction(unsigned phaseIdx, unsigned compIdx) const
The mass fraction of a component in a phase [].
Definition FluidStateCompositionModules.hpp:279
Opm::FluidStateImmiscibleCompositionModule::molarity
Scalar molarity(unsigned phaseIdx, unsigned compIdx) const
The concentration of a component in a phase [mol/m^3].
Definition FluidStateCompositionModules.hpp:302
Opm::FluidStateImmiscibleCompositionModule::checkDefined
void checkDefined() const
Make sure that all attributes are defined.
Definition FluidStateCompositionModules.hpp:321
Opm::FluidStateImmiscibleCompositionModule::assign
void assign(const FluidState &)
Retrieve all parameters from an arbitrary fluid state.
Definition FluidStateCompositionModules.hpp:310
Opm::FluidStateImmiscibleCompositionModule::moleFraction
Scalar moleFraction(unsigned phaseIdx, unsigned compIdx) const
The mole fraction of a component in a phase [].
Definition FluidStateCompositionModules.hpp:273
Opm::FluidStateImmiscibleCompositionModule::averageMolarMass
Scalar averageMolarMass(unsigned phaseIdx) const
The mean molar mass of a fluid phase [kg/mol].
Definition FluidStateCompositionModules.hpp:290
Opm::FluidStateNullCompositionModule::moleFraction
Scalar moleFraction(unsigned, unsigned) const
The mole fraction of a component in a phase [].
Definition FluidStateCompositionModules.hpp:344
Opm::FluidStateNullCompositionModule::averageMolarMass
Scalar averageMolarMass(unsigned) const
The mean molar mass of a fluid phase [kg/mol].
Definition FluidStateCompositionModules.hpp:361
Opm::FluidStateNullCompositionModule::molarity
Scalar molarity(unsigned, unsigned) const
The concentration of a component in a phase [mol/m^3].
Definition FluidStateCompositionModules.hpp:373
Opm::FluidStateNullCompositionModule::checkDefined
void checkDefined() const
Make sure that all attributes are defined.
Definition FluidStateCompositionModules.hpp:384
Opm::FluidStateNullCompositionModule::massFraction
Scalar massFraction(unsigned, unsigned) const
The mass fraction of a component in a phase [].
Definition FluidStateCompositionModules.hpp:350
Opm
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30