static Evaluation Sw(const Params& params, const FluidState& fs) { const Evaluation& pC = Opm::decay<Evaluation>(fs.pressure(Traits::nonWettingPhaseIdx)) - Opm::decay<Evaluation>(fs.pressure(Traits::wettingPhaseIdx)); return twoPhaseSatSw(params, pC); }
static Evaluation Sw(const Params ¶ms, const FluidState &fs) { typedef MathToolbox<typename FluidState::Scalar> FsToolbox; Evaluation pC = FsToolbox::template toLhs<Evaluation>(fs.pressure(Traits::nonWettingPhaseIdx)) - FsToolbox::template toLhs<Evaluation>(fs.pressure(Traits::wettingPhaseIdx)); return twoPhaseSatSw(params, pC); }
static Scalar heatCapacity(const FluidState &fluidState, const ParameterCache ¶mCache, int phaseIdx) { if(phaseIdx == lPhaseIdx) return H2O::liquidHeatCapacity(fluidState.temperature(phaseIdx), fluidState.pressure(phaseIdx)); else return CO2::gasHeatCapacity(fluidState.temperature(phaseIdx), fluidState.pressure(phaseIdx)); }
void completeReferenceFluidState(FluidState &fs, typename MaterialLaw::Params &matParams, int refPhaseIdx) { enum { numPhases = FluidSystem::numPhases }; typedef Dune::FieldVector<Scalar, numPhases> PhaseVector; int otherPhaseIdx = 1 - refPhaseIdx; // calculate the other saturation fs.setSaturation(otherPhaseIdx, 1.0 - fs.saturation(refPhaseIdx)); // calulate the capillary pressure PhaseVector pC; MaterialLaw::capillaryPressures(pC, matParams, fs); fs.setPressure(otherPhaseIdx, fs.pressure(refPhaseIdx) + (pC[otherPhaseIdx] - pC[refPhaseIdx])); // set all phase densities typename FluidSystem::ParameterCache paramCache; paramCache.updateAll(fs); for (int phaseIdx = 0; phaseIdx < numPhases; ++ phaseIdx) { Scalar rho = FluidSystem::density(fs, paramCache, phaseIdx); fs.setDensity(phaseIdx, rho); } }
static Scalar enthalpy(const FluidState &fluidState, const ParameterCache ¶mCache, int phaseIdx) { assert(0 <= phaseIdx && phaseIdx < numPhases); Scalar temperature = fluidState.temperature(phaseIdx); Scalar pressure = fluidState.pressure(phaseIdx); if (phaseIdx == lPhaseIdx) { Scalar XlCO2 = fluidState.massFraction(phaseIdx, CO2Idx); Scalar result = liquidEnthalpyBrineCO2_(temperature, pressure, Brine_IAPWS::salinity, XlCO2); Valgrind::CheckDefined(result); return result; } else { Scalar XCO2 = fluidState.massFraction(gPhaseIdx, CO2Idx); Scalar XBrine = fluidState.massFraction(gPhaseIdx, BrineIdx); Scalar result = 0; result += XBrine * Brine::gasEnthalpy(temperature, pressure); result += XCO2 * CO2::gasEnthalpy(temperature, pressure); Valgrind::CheckDefined(result); return result; } }
static Scalar enthalpy(const FluidState &fluidState, const ParameterCache ¶mCache, int phaseIdx) { Scalar T = fluidState.temperature(phaseIdx); Scalar p = fluidState.pressure(phaseIdx); Valgrind::CheckDefined(T); Valgrind::CheckDefined(p); if (phaseIdx == lPhaseIdx) { // TODO: correct way to deal with the solutes??? return H2O::liquidEnthalpy(T, p); } else if (phaseIdx == gPhaseIdx) { Scalar result = 0.0; result += H2O::gasEnthalpy(T, p) * fluidState.massFraction(gPhaseIdx, H2OIdx); result += Air::gasEnthalpy(T, p) * fluidState.massFraction(gPhaseIdx, AirIdx); return result; } OPM_THROW(std::logic_error, "Invalid phase index " << phaseIdx); }
static Scalar thermalConductivity(const FluidState &fluidState, const ParameterCache ¶mCache, int phaseIdx) { assert(0 <= phaseIdx && phaseIdx < numPhases); Scalar temperature = fluidState.temperature(phaseIdx) ; Scalar pressure = fluidState.pressure(phaseIdx); if (phaseIdx == lPhaseIdx){// liquid phase return H2O::liquidThermalConductivity(temperature, pressure); } else{// gas phase Scalar lambdaDryAir = Air::gasThermalConductivity(temperature, pressure); if (useComplexRelations){ Scalar xAir = fluidState.moleFraction(phaseIdx, AirIdx); Scalar xH2O = fluidState.moleFraction(phaseIdx, H2OIdx); Scalar lambdaAir = xAir * lambdaDryAir; // Assuming Raoult's, Daltons law and ideal gas // in order to obtain the partial density of water in the air phase Scalar partialPressure = pressure * xH2O; Scalar lambdaH2O = xH2O * H2O::gasThermalConductivity(temperature, partialPressure); return lambdaAir + lambdaH2O; } else return lambdaDryAir; // conductivity of Nitrogen [W / (m K ) ] } }
void completeReferenceFluidState(FluidState &fs, typename MaterialLaw::Params &matParams, int refPhaseIdx) { enum { numPhases = FluidSystem::numPhases }; typedef Opm::ComputeFromReferencePhase<Scalar, FluidSystem> ComputeFromReferencePhase; typedef Dune::FieldVector<Scalar, numPhases> PhaseVector; int otherPhaseIdx = 1 - refPhaseIdx; // calculate the other saturation fs.setSaturation(otherPhaseIdx, 1.0 - fs.saturation(refPhaseIdx)); // calulate the capillary pressure PhaseVector pC; MaterialLaw::capillaryPressures(pC, matParams, fs); fs.setPressure(otherPhaseIdx, fs.pressure(refPhaseIdx) + (pC[otherPhaseIdx] - pC[refPhaseIdx])); // make the fluid state consistent with local thermodynamic // equilibrium typename FluidSystem::ParameterCache paramCache; ComputeFromReferencePhase::solve(fs, paramCache, refPhaseIdx, /*setViscosity=*/false, /*setEnthalpy=*/false); }
static LhsEval enthalpy(const FluidState &fluidState, const ParameterCache &/*paramCache*/, unsigned phaseIdx) { typedef Opm::MathToolbox<typename FluidState::Scalar> FsToolbox; const auto& T = FsToolbox::template toLhs<LhsEval>(fluidState.temperature(phaseIdx)); const auto& p = FsToolbox::template toLhs<LhsEval>(fluidState.pressure(phaseIdx)); Valgrind::CheckDefined(T); Valgrind::CheckDefined(p); if (phaseIdx == liquidPhaseIdx) { // TODO: correct way to deal with the solutes??? return H2O::liquidEnthalpy(T, p); } else if (phaseIdx == gasPhaseIdx) { LhsEval result = 0.0; result += H2O::gasEnthalpy(T, p) * FsToolbox::template toLhs<LhsEval>(fluidState.massFraction(gasPhaseIdx, H2OIdx)); result += Air::gasEnthalpy(T, p) * FsToolbox::template toLhs<LhsEval>(fluidState.massFraction(gasPhaseIdx, AirIdx)); return result; } OPM_THROW(std::logic_error, "Invalid phase index " << phaseIdx); }
static LhsEval heatCapacity(const FluidState &fluidState, const ParameterCache &/*paramCache*/, unsigned phaseIdx) { typedef Opm::MathToolbox<typename FluidState::Scalar> FsToolbox; assert(0 <= phaseIdx && phaseIdx < numPhases); const auto& T = FsToolbox::template toLhs<LhsEval>(fluidState.temperature(phaseIdx)); const auto& p = FsToolbox::template toLhs<LhsEval>(fluidState.pressure(phaseIdx)); return Fluid::heatCapacity(T, p); }
static LhsEval thermalConductivity(const FluidState &fluidState, const ParameterCache ¶mCache, int phaseIdx) { typedef Opm::MathToolbox<typename FluidState::Scalar> FsToolbox; assert(0 <= phaseIdx && phaseIdx < numPhases); const auto& T = FsToolbox::template toLhs<LhsEval>(fluidState.temperature(phaseIdx)); const auto& p = FsToolbox::template toLhs<LhsEval>(fluidState.pressure(phaseIdx)); return Fluid::thermalConductivity(T, p); }
static Scalar diffusionCoefficient(const FluidState &fluidState, const ParameterCache ¶mCache, int phaseIdx, int compIdx) { Scalar temperature = fluidState.temperature(phaseIdx); Scalar pressure = fluidState.pressure(phaseIdx); if (phaseIdx == lPhaseIdx) return BinaryCoeffBrineCO2::liquidDiffCoeff(temperature, pressure); assert(phaseIdx == gPhaseIdx); return BinaryCoeffBrineCO2::gasDiffCoeff(temperature, pressure); }
static Scalar binaryDiffusionCoefficient(const FluidState &fluidState, const ParameterCache ¶mCache, int phaseIdx, int compIdx) { Scalar T = fluidState.temperature(phaseIdx); Scalar p = fluidState.pressure(phaseIdx); if (phaseIdx == lPhaseIdx) return BinaryCoeff::H2O_Air::liquidDiffCoeff(T, p); assert(phaseIdx == gPhaseIdx); return BinaryCoeff::H2O_Air::gasDiffCoeff(T, p); }
static Scalar fugacityCoefficient(const FluidState &fluidState, const ParameterCache ¶mCache, int phaseIdx, int compIdx) { assert(0 <= phaseIdx && phaseIdx < numPhases); assert(0 <= compIdx && compIdx < numComponents); if (phaseIdx == gPhaseIdx) // use the fugacity coefficients of an ideal gas. the // actual value of the fugacity is not relevant, as long // as the relative fluid compositions are observed, return 1.0; Scalar temperature = fluidState.temperature(phaseIdx); Scalar pressure = fluidState.pressure(phaseIdx); assert(temperature > 0); assert(pressure > 0); // calulate the equilibrium composition for the given // temperature and pressure. TODO: calculateMoleFractions() // could use some cleanup. Scalar xlH2O, xgH2O; Scalar xlCO2, xgCO2; BinaryCoeffBrineCO2::calculateMoleFractions(temperature, pressure, Brine_IAPWS::salinity, /*knownPhaseIdx=*/-1, xlCO2, xgH2O); // normalize the phase compositions xlCO2 = std::max(0.0, std::min(1.0, xlCO2)); xgH2O = std::max(0.0, std::min(1.0, xgH2O)); xlH2O = 1.0 - xlCO2; xgCO2 = 1.0 - xgH2O; if (compIdx == BrineIdx) { Scalar phigH2O = 1.0; return phigH2O * xgH2O / xlH2O; } else { assert(compIdx == CO2Idx); Scalar phigCO2 = 1.0; return phigCO2 * xgCO2 / xlCO2; }; }
void checkSame(const FluidState &fsRef, const FluidState &fsFlash) { enum { numPhases = FluidState::numPhases }; enum { numComponents = FluidState::numComponents }; for (int phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) { Scalar error; // check the pressures error = 1 - fsRef.pressure(phaseIdx)/fsFlash.pressure(phaseIdx); if (std::abs(error) > 1e-6) { std::cout << "pressure error phase " << phaseIdx << ": " << fsFlash.pressure(phaseIdx) << " flash vs " << fsRef.pressure(phaseIdx) << " reference" << " error=" << error << "\n"; } // check the saturations error = fsRef.saturation(phaseIdx) - fsFlash.saturation(phaseIdx); if (std::abs(error) > 1e-6) std::cout << "saturation error phase " << phaseIdx << ": " << fsFlash.saturation(phaseIdx) << " flash vs " << fsRef.saturation(phaseIdx) << " reference" << " error=" << error << "\n"; // check the compositions for (int compIdx = 0; compIdx < numComponents; ++ compIdx) { error = fsRef.moleFraction(phaseIdx, compIdx) - fsFlash.moleFraction(phaseIdx, compIdx); if (std::abs(error) > 1e-6) std::cout << "composition error phase " << phaseIdx << ", component " << compIdx << ": " << fsFlash.moleFraction(phaseIdx, compIdx) << " flash vs " << fsRef.moleFraction(phaseIdx, compIdx) << " reference" << " error=" << error << "\n"; } } }
static LhsEval binaryDiffusionCoefficient(const FluidState &fluidState, const ParameterCache &/*paramCache*/, unsigned phaseIdx, unsigned /*compIdx*/) { typedef Opm::MathToolbox<typename FluidState::Scalar> FsToolbox; const auto& T = FsToolbox::template toLhs<LhsEval>(fluidState.temperature(phaseIdx)); const auto& p = FsToolbox::template toLhs<LhsEval>(fluidState.pressure(phaseIdx)); if (phaseIdx == liquidPhaseIdx) return BinaryCoeff::H2O_Air::liquidDiffCoeff(T, p); assert(phaseIdx == gasPhaseIdx); return BinaryCoeff::H2O_Air::gasDiffCoeff(T, p); }
static Scalar density(const FluidState &fluidState, const ParameterCache ¶mCache, int phaseIdx) { assert(0 <= phaseIdx && phaseIdx < numPhases); Scalar temperature = fluidState.temperature(phaseIdx); Scalar pressure = fluidState.pressure(phaseIdx); if (phaseIdx == lPhaseIdx) { // use normalized composition for to calculate the density // (the relations don't seem to take non-normalized // compositions too well...) Scalar xlBrine = std::min(1.0, std::max(0.0, fluidState.moleFraction(lPhaseIdx, BrineIdx))); Scalar xlCO2 = std::min(1.0, std::max(0.0, fluidState.moleFraction(lPhaseIdx, CO2Idx))); Scalar sumx = xlBrine + xlCO2; xlBrine /= sumx; xlCO2 /= sumx; Scalar result = liquidDensity_(temperature, pressure, xlBrine, xlCO2); Valgrind::CheckDefined(result); return result; } assert(phaseIdx == gPhaseIdx); // use normalized composition for to calculate the density // (the relations don't seem to take non-normalized // compositions too well...) Scalar xgBrine = std::min(1.0, std::max(0.0, fluidState.moleFraction(gPhaseIdx, BrineIdx))); Scalar xgCO2 = std::min(1.0, std::max(0.0, fluidState.moleFraction(gPhaseIdx, CO2Idx))); Scalar sumx = xgBrine + xgCO2; xgBrine /= sumx; xgCO2 /= sumx; Scalar result = gasDensity_(temperature, pressure, xgBrine, xgCO2); Valgrind::CheckDefined(result); return result; }
void assign(const FluidState& fs) { if (enableTemperature || enableEnergy) setTemperature(fs.temperature(/*phaseIdx=*/0)); unsigned pvtRegionIdx = getPvtRegionIndex_<FluidState>(fs); setPvtRegionIndex(pvtRegionIdx); setRs(Opm::BlackOil::getRs_<FluidSystem, FluidState, Scalar>(fs, pvtRegionIdx)); setRv(Opm::BlackOil::getRv_<FluidSystem, FluidState, Scalar>(fs, pvtRegionIdx)); for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) { setSaturation(phaseIdx, fs.saturation(phaseIdx)); setPressure(phaseIdx, fs.pressure(phaseIdx)); setDensity(phaseIdx, fs.density(phaseIdx)); if (enableEnergy) setEnthalpy(phaseIdx, fs.enthalpy(phaseIdx)); setInvB(phaseIdx, getInvB_<FluidSystem, FluidState, Scalar>(fs, phaseIdx, pvtRegionIdx)); } }
static Scalar fugacityCoefficient(const FluidState &fluidState, const ParameterCache ¶mCache, int phaseIdx, int compIdx) { assert(0 <= phaseIdx && phaseIdx < numPhases); assert(0 <= compIdx && compIdx < numComponents); Scalar T = fluidState.temperature(phaseIdx); Scalar p = fluidState.pressure(phaseIdx); if (phaseIdx == lPhaseIdx) { if (compIdx == H2OIdx) return H2O::vaporPressure(T)/p; return Opm::BinaryCoeff::H2O_Air::henry(T)/p; } // for the gas phase, assume an ideal gas when it comes to // fugacity (-> fugacity == partial pressure) return 1.0; }
static Scalar fugacityCoefficient(const FluidState &fluidState, const ParameterCache ¶mCache, unsigned phaseIdx, unsigned compIdx) { assert(0 <= phaseIdx && phaseIdx <= numPhases); assert(0 <= compIdx && compIdx <= numComponents); static_assert(std::is_same<Evaluation, Scalar>::value, "The SPE-5 fluid system is currently only implemented for the scalar case."); if (phaseIdx == oilPhaseIdx || phaseIdx == gasPhaseIdx) return PengRobinsonMixture::computeFugacityCoefficient(fluidState, paramCache, phaseIdx, compIdx); else { assert(phaseIdx == waterPhaseIdx); return henryCoeffWater_(compIdx, fluidState.temperature(waterPhaseIdx)) / fluidState.pressure(waterPhaseIdx); } }
static Scalar viscosity(const FluidState &fluidState, const ParameterCache ¶mCache, int phaseIdx) { assert(0 <= phaseIdx && phaseIdx < numPhases); Scalar temperature = fluidState.temperature(phaseIdx); Scalar pressure = fluidState.pressure(phaseIdx); if (phaseIdx == lPhaseIdx) { // assume pure brine for the liquid phase. TODO: viscosity // of mixture Scalar result = Brine::liquidViscosity(temperature, pressure); Valgrind::CheckDefined(result); return result; } assert(phaseIdx == gPhaseIdx); Scalar result = CO2::gasViscosity(temperature, pressure); Valgrind::CheckDefined(result); return result; }
static LhsEval fugacityCoefficient(const FluidState &fluidState, const ParameterCache &/*paramCache*/, unsigned phaseIdx, unsigned compIdx) { typedef Opm::MathToolbox<typename FluidState::Scalar> FsToolbox; assert(0 <= phaseIdx && phaseIdx < numPhases); assert(0 <= compIdx && compIdx < numComponents); const auto& T = FsToolbox::template toLhs<LhsEval>(fluidState.temperature(phaseIdx)); const auto& p = FsToolbox::template toLhs<LhsEval>(fluidState.pressure(phaseIdx)); if (phaseIdx == liquidPhaseIdx) { if (compIdx == H2OIdx) return H2O::vaporPressure(T)/p; return Opm::BinaryCoeff::H2O_Air::henry(T)/p; } // for the gas phase, assume an ideal gas when it comes to // fugacity (-> fugacity == partial pressure) return 1.0; }
static LhsEval thermalConductivity(const FluidState &fluidState, const ParameterCache &/*paramCache*/, unsigned phaseIdx) { typedef MathToolbox<typename FluidState::Scalar> FsToolbox; assert(0 <= phaseIdx && phaseIdx < numPhases); const LhsEval& temperature = FsToolbox::template toLhs<LhsEval>(fluidState.temperature(phaseIdx)); const LhsEval& pressure = FsToolbox::template toLhs<LhsEval>(fluidState.pressure(phaseIdx)); if (phaseIdx == liquidPhaseIdx) return H2O::liquidThermalConductivity(temperature, pressure); else { // gas phase const LhsEval& lambdaDryAir = Air::gasThermalConductivity(temperature, pressure); if (useComplexRelations){ const LhsEval& xAir = FsToolbox::template toLhs<LhsEval>(fluidState.moleFraction(phaseIdx, AirIdx)); const LhsEval& xH2O = FsToolbox::template toLhs<LhsEval>(fluidState.moleFraction(phaseIdx, H2OIdx)); LhsEval lambdaAir = xAir*lambdaDryAir; // Assuming Raoult's, Daltons law and ideal gas // in order to obtain the partial density of water in the air phase LhsEval partialPressure = pressure*xH2O; LhsEval lambdaH2O = xH2O*H2O::gasThermalConductivity(temperature, partialPressure); return lambdaAir + lambdaH2O; } else return lambdaDryAir; // conductivity of Nitrogen [W / (m K ) ] } }
static void solveIdealMix_(FluidState &fluidState, ParameterCache ¶mCache, int phaseIdx, const ComponentVector &fugacities) { for (int i = 0; i < numComponents; ++ i) { const Evaluation& phi = FluidSystem::fugacityCoefficient(fluidState, paramCache, phaseIdx, i); const Evaluation& gamma = phi * fluidState.pressure(phaseIdx); Valgrind::CheckDefined(phi); Valgrind::CheckDefined(gamma); Valgrind::CheckDefined(fugacities[i]); fluidState.setFugacityCoefficient(phaseIdx, i, phi); fluidState.setMoleFraction(phaseIdx, i, fugacities[i]/gamma); }; paramCache.updatePhase(fluidState, phaseIdx); const Evaluation& rho = FluidSystem::density(fluidState, paramCache, phaseIdx); fluidState.setDensity(phaseIdx, rho); return; }
static void solve(FluidState &fluidState, ParameterCache ¶mCache, int phasePresence, const MMPCAuxConstraint<Evaluation> *auxConstraints, unsigned numAuxConstraints, bool setViscosity, bool setInternalEnergy) { typedef MathToolbox<typename FluidState::Scalar> FsToolbox; static_assert(std::is_same<typename FluidState::Scalar, Evaluation>::value, "The scalar type of the fluid state must be 'Evaluation'"); #ifndef NDEBUG // currently this solver can only handle fluid systems which // assume ideal mixtures of all fluids. TODO: relax this // (requires solving a non-linear system of equations, i.e. using // newton method.) for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) { assert(FluidSystem::isIdealMixture(phaseIdx)); } #endif // compute all fugacity coefficients for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) { paramCache.updatePhase(fluidState, phaseIdx); // since we assume ideal mixtures, the fugacity // coefficients of the components cannot depend on // composition, i.e. the parameters in the cache are valid for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) { Evaluation fugCoeff = FsToolbox::template toLhs<Evaluation>( FluidSystem::fugacityCoefficient(fluidState, paramCache, phaseIdx, compIdx)); fluidState.setFugacityCoefficient(phaseIdx, compIdx, fugCoeff); } } // create the linear system of equations which defines the // mole fractions static const int numEq = numComponents*numPhases; Dune::FieldMatrix<Evaluation, numEq, numEq> M(Toolbox::createConstant(0.0)); Dune::FieldVector<Evaluation, numEq> x(Toolbox::createConstant(0.0)); Dune::FieldVector<Evaluation, numEq> b(Toolbox::createConstant(0.0)); // assemble the equations expressing the fact that the // fugacities of each component are equal in all phases for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) { const Evaluation& entryCol1 = fluidState.fugacityCoefficient(/*phaseIdx=*/0, compIdx) *fluidState.pressure(/*phaseIdx=*/0); unsigned col1Idx = compIdx; for (unsigned phaseIdx = 1; phaseIdx < numPhases; ++phaseIdx) { unsigned rowIdx = (phaseIdx - 1)*numComponents + compIdx; unsigned col2Idx = phaseIdx*numComponents + compIdx; const Evaluation& entryCol2 = fluidState.fugacityCoefficient(phaseIdx, compIdx) *fluidState.pressure(phaseIdx); M[rowIdx][col1Idx] = entryCol1; M[rowIdx][col2Idx] = -entryCol2; } } // assemble the equations expressing the assumption that the // sum of all mole fractions in each phase must be 1 for the // phases present. unsigned presentPhases = 0; for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) { if (!(phasePresence & (1 << phaseIdx))) continue; unsigned rowIdx = numComponents*(numPhases - 1) + presentPhases; presentPhases += 1; b[rowIdx] = Toolbox::createConstant(1.0); for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) { unsigned colIdx = phaseIdx*numComponents + compIdx; M[rowIdx][colIdx] = Toolbox::createConstant(1.0); } } assert(presentPhases + numAuxConstraints == numComponents); // incorperate the auxiliary equations, i.e., the explicitly given mole fractions for (unsigned auxEqIdx = 0; auxEqIdx < numAuxConstraints; ++auxEqIdx) { unsigned rowIdx = numComponents*(numPhases - 1) + presentPhases + auxEqIdx; b[rowIdx] = auxConstraints[auxEqIdx].value(); unsigned colIdx = auxConstraints[auxEqIdx].phaseIdx()*numComponents + auxConstraints[auxEqIdx].compIdx(); M[rowIdx][colIdx] = 1.0; } // solve for all mole fractions try { Dune::FMatrixPrecision<Scalar>::set_singular_limit(1e-50); M.solve(x, b); } catch (const Dune::FMatrixError &e) { OPM_THROW(NumericalProblem, "Numerical problem in MiscibleMultiPhaseComposition::solve(): " << e.what() << "; M="<<M); } catch (...) { throw; } // set all mole fractions and the additional quantities in // the fluid state for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) { for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) { unsigned rowIdx = phaseIdx*numComponents + compIdx; fluidState.setMoleFraction(phaseIdx, compIdx, x[rowIdx]); } paramCache.updateComposition(fluidState, phaseIdx); const Evaluation& rho = FluidSystem::density(fluidState, paramCache, phaseIdx); fluidState.setDensity(phaseIdx, rho); if (setViscosity) { const Evaluation& mu = FluidSystem::viscosity(fluidState, paramCache, phaseIdx); fluidState.setViscosity(phaseIdx, mu); } if (setInternalEnergy) { const Evaluation& h = FluidSystem::enthalpy(fluidState, paramCache, phaseIdx); fluidState.setEnthalpy(phaseIdx, h); } } }
static Scalar computeFugacityCoefficient(const FluidState &fs, const Params ¶ms, int phaseIdx, int compIdx) { // note that we normalize the component mole fractions, so // that their sum is 100%. This increases numerical stability // considerably if the fluid state is not physical. Scalar Vm = params.molarVolume(phaseIdx); // Calculate b_i / b Scalar bi_b = params.bPure(phaseIdx, compIdx) / params.b(phaseIdx); // Calculate the compressibility factor Scalar RT = R*fs.temperature(phaseIdx); Scalar p = fs.pressure(phaseIdx); // molar volume in [bar] Scalar Z = p*Vm/RT; // compressibility factor // Calculate A^* and B^* (see: Reid, p. 42) Scalar Astar = params.a(phaseIdx)*p/(RT*RT); Scalar Bstar = params.b(phaseIdx)*p/(RT); // calculate delta_i (see: Reid, p. 145) Scalar sumMoleFractions = 0.0; for (int compJIdx = 0; compJIdx < numComponents; ++compJIdx) sumMoleFractions += fs.moleFraction(phaseIdx, compJIdx); Scalar deltai = 2*std::sqrt(params.aPure(phaseIdx, compIdx))/params.a(phaseIdx); Scalar tmp = 0; for (int compJIdx = 0; compJIdx < numComponents; ++compJIdx) { tmp += fs.moleFraction(phaseIdx, compJIdx) / sumMoleFractions * std::sqrt(params.aPure(phaseIdx, compJIdx)) * (1.0 - StaticParameters::interactionCoefficient(compIdx, compJIdx)); }; deltai *= tmp; Scalar base = (2*Z + Bstar*(u + std::sqrt(u*u - 4*w))) / (2*Z + Bstar*(u - std::sqrt(u*u - 4*w))); Scalar expo = Astar/(Bstar*std::sqrt(u*u - 4*w))*(bi_b - deltai); Scalar fugCoeff = std::exp(bi_b*(Z - 1))/std::max(1e-9, Z - Bstar) * std::pow(base, expo); //////// // limit the fugacity coefficient to a reasonable range: // // on one side, we want the mole fraction to be at // least 10^-3 if the fugacity is at the current pressure // fugCoeff = std::min(1e10, fugCoeff); // // on the other hand, if the mole fraction of the component is 100%, we want the // fugacity to be at least 10^-3 Pa // fugCoeff = std::max(1e-10, fugCoeff); /////////// return fugCoeff; }
static LhsEval density(const FluidState &fluidState, const ParameterCache &/*paramCache*/, unsigned phaseIdx) { typedef Opm::MathToolbox<typename FluidState::Scalar> FsToolbox; typedef Opm::MathToolbox<LhsEval> LhsToolbox; assert(0 <= phaseIdx && phaseIdx < numPhases); const auto& T = FsToolbox::template toLhs<LhsEval>(fluidState.temperature(phaseIdx)); LhsEval p; if (isCompressible(phaseIdx)) p = FsToolbox::template toLhs<LhsEval>(fluidState.pressure(phaseIdx)); else { // random value which will hopefully cause things to blow // up if it is used in a calculation! p = - 1e100; Valgrind::SetUndefined(p); } LhsEval sumMoleFrac = 0; for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) sumMoleFrac += FsToolbox::template toLhs<LhsEval>(fluidState.moleFraction(phaseIdx, compIdx)); if (phaseIdx == liquidPhaseIdx) { if (!useComplexRelations) // assume pure water return H2O::liquidDensity(T, p); else { // See: Ochs 2008 (2.6) const LhsEval& rholH2O = H2O::liquidDensity(T, p); const LhsEval& clH2O = rholH2O/H2O::molarMass(); const auto& xlH2O = FsToolbox::template toLhs<LhsEval>(fluidState.moleFraction(liquidPhaseIdx, H2OIdx)); const auto& xlAir = FsToolbox::template toLhs<LhsEval>(fluidState.moleFraction(liquidPhaseIdx, AirIdx)); return clH2O*(H2O::molarMass()*xlH2O + Air::molarMass()*xlAir)/sumMoleFrac; } } else if (phaseIdx == gasPhaseIdx) { if (!useComplexRelations) // for the gas phase assume an ideal gas return IdealGas::molarDensity(T, p) * FsToolbox::template toLhs<LhsEval>(fluidState.averageMolarMass(gasPhaseIdx)) / LhsToolbox::max(1e-5, sumMoleFrac); LhsEval partialPressureH2O = FsToolbox::template toLhs<LhsEval>(fluidState.moleFraction(gasPhaseIdx, H2OIdx)) *FsToolbox::template toLhs<LhsEval>(fluidState.pressure(gasPhaseIdx)); LhsEval partialPressureAir = FsToolbox::template toLhs<LhsEval>(fluidState.moleFraction(gasPhaseIdx, AirIdx)) *FsToolbox::template toLhs<LhsEval>(fluidState.pressure(gasPhaseIdx)); return H2O::gasDensity(T, partialPressureH2O) + Air::gasDensity(T, partialPressureAir); } OPM_THROW(std::logic_error, "Invalid phase index " << phaseIdx); }
static LhsEval viscosity(const FluidState &fluidState, const ParameterCache &/*paramCache*/, unsigned phaseIdx) { typedef Opm::MathToolbox<LhsEval> LhsToolbox; typedef Opm::MathToolbox<typename FluidState::Scalar> FsToolbox; assert(0 <= phaseIdx && phaseIdx < numPhases); const auto& T = FsToolbox::template toLhs<LhsEval>(fluidState.temperature(phaseIdx)); const auto& p = FsToolbox::template toLhs<LhsEval>(fluidState.pressure(phaseIdx)); if (phaseIdx == liquidPhaseIdx) { // assume pure water for the liquid phase // TODO: viscosity of mixture // couldn't find a way to solve the mixture problem return H2O::liquidViscosity(T, p); } else if (phaseIdx == gasPhaseIdx) { if(!useComplexRelations){ return Air::gasViscosity(T, p); } else //using a complicated version of this fluid system { /* Wilke method. See: * * See: R. Reid, et al.: The Properties of Gases and Liquids, * 4th edition, McGraw-Hill, 1987, 407-410 or * 5th edition, McGraw-Hill, 2000, p. 9.21/22 * */ LhsEval muResult = 0; const LhsEval mu[numComponents] = { H2O::gasViscosity(T, H2O::vaporPressure(T)), Air::gasViscosity(T, p) }; // molar masses const Scalar M[numComponents] = { H2O::molarMass(), Air::molarMass() }; for (unsigned i = 0; i < numComponents; ++i) { LhsEval divisor = 0; for (unsigned j = 0; j < numComponents; ++j) { LhsEval phiIJ = 1 + LhsToolbox::sqrt(mu[i]/mu[j]) * // 1 + (mu[i]/mu[j]^1/2 std::pow(M[j]/M[i], 1./4.0); // (M[i]/M[j])^1/4 phiIJ *= phiIJ; phiIJ /= std::sqrt(8*(1 + M[i]/M[j])); divisor += FsToolbox::template toLhs<LhsEval>(fluidState.moleFraction(phaseIdx, j))*phiIJ; } const auto& xAlphaI = FsToolbox::template toLhs<LhsEval>(fluidState.moleFraction(phaseIdx, i)); muResult += xAlphaI*mu[i]/divisor; } return muResult; } } OPM_THROW(std::logic_error, "Invalid phase index " << phaseIdx); }
static Scalar viscosity(const FluidState &fluidState, const ParameterCache ¶mCache, int phaseIdx) { assert(0 <= phaseIdx && phaseIdx < numPhases); Scalar T = fluidState.temperature(phaseIdx); Scalar p = fluidState.pressure(phaseIdx); if (phaseIdx == lPhaseIdx) { // assume pure water for the liquid phase // TODO: viscosity of mixture // couldn't find a way to solve the mixture problem return H2O::liquidViscosity(T, p); } else if (phaseIdx == gPhaseIdx) { if(!useComplexRelations){ return Air::gasViscosity(T, p); } else //using a complicated version of this fluid system { /* Wilke method. See: * * See: R. Reid, et al.: The Properties of Gases and Liquids, * 4th edition, McGraw-Hill, 1987, 407-410 or * 5th edition, McGraw-Hill, 2000, p. 9.21/22 * */ Scalar muResult = 0; const Scalar mu[numComponents] = { H2O::gasViscosity(T, H2O::vaporPressure(T)), Air::gasViscosity(T, p) }; // molar masses const Scalar M[numComponents] = { H2O::molarMass(), Air::molarMass() }; for (int i = 0; i < numComponents; ++i) { Scalar divisor = 0; for (int j = 0; j < numComponents; ++j) { Scalar phiIJ = 1 + std::sqrt(mu[i]/mu[j]) * // 1 + (mu[i]/mu[j]^1/2 std::pow(M[j]/M[i], 1./4.0); // (M[i]/M[j])^1/4 phiIJ *= phiIJ; phiIJ /= std::sqrt(8*(1 + M[i]/M[j])); divisor += fluidState.moleFraction(phaseIdx, j)*phiIJ; } muResult += fluidState.moleFraction(phaseIdx, i)*mu[i] / divisor; } return muResult; } } OPM_THROW(std::logic_error, "Invalid phase index " << phaseIdx); }
static Scalar density(const FluidState &fluidState, const ParameterCache ¶mCache, int phaseIdx) { assert(0 <= phaseIdx && phaseIdx < numPhases); Scalar T = fluidState.temperature(phaseIdx); Scalar p; if (isCompressible(phaseIdx)) p = fluidState.pressure(phaseIdx); else { // random value which will hopefully cause things to blow // up if it is used in a calculation! p = - 1e100; Valgrind::SetUndefined(p); } Scalar sumMoleFrac = 0; for (int compIdx = 0; compIdx < numComponents; ++compIdx) sumMoleFrac += fluidState.moleFraction(phaseIdx, compIdx); if (phaseIdx == lPhaseIdx) { if (!useComplexRelations) // assume pure water return H2O::liquidDensity(T, p); else { // See: Ochs 2008 (2.6) Scalar rholH2O = H2O::liquidDensity(T, p); Scalar clH2O = rholH2O/H2O::molarMass(); return clH2O * (H2O::molarMass()*fluidState.moleFraction(lPhaseIdx, H2OIdx) + Air::molarMass()*fluidState.moleFraction(lPhaseIdx, AirIdx)) / sumMoleFrac; } } else if (phaseIdx == gPhaseIdx) { if (!useComplexRelations) // for the gas phase assume an ideal gas return IdealGas::molarDensity(T, p) * fluidState.averageMolarMass(gPhaseIdx) / std::max(1e-5, sumMoleFrac); Scalar partialPressureH2O = fluidState.moleFraction(gPhaseIdx, H2OIdx) * fluidState.pressure(gPhaseIdx); Scalar partialPressureAir = fluidState.moleFraction(gPhaseIdx, AirIdx) * fluidState.pressure(gPhaseIdx); return H2O::gasDensity(T, partialPressureH2O) + Air::gasDensity(T, partialPressureAir); } OPM_THROW(std::logic_error, "Invalid phase index " << phaseIdx); }