diff --git a/opm/material/constraintsolvers/PTFlash.hpp b/opm/material/constraintsolvers/PTFlash.hpp index 4321c7150..afe0b354a 100644 --- a/opm/material/constraintsolvers/PTFlash.hpp +++ b/opm/material/constraintsolvers/PTFlash.hpp @@ -198,7 +198,7 @@ public: * zero... */ template - static void solve(FluidState& fluidState, + static void solve(FluidState& fluid_state, const ComponentVector& globalMolarities, Scalar tolerance = 0.0) { @@ -207,27 +207,27 @@ public: using MaterialLawParams = typename MaterialLaw::Params; MaterialLawParams matParams; - solve(fluidState, matParams, globalMolarities, tolerance); + solve(fluid_state, matParams, globalMolarities, tolerance); } protected: template - static typename FlashFluidState::Scalar wilsonK_(const FlashFluidState& fluidState, int compIdx) + static typename FlashFluidState::Scalar wilsonK_(const FlashFluidState& fluid_state, int compIdx) { const auto& acf = FluidSystem::acentricFactor(compIdx); const auto& T_crit = FluidSystem::criticalTemperature(compIdx); - const auto& T = fluidState.temperature(0); + const auto& T = fluid_state.temperature(0); const auto& p_crit = FluidSystem::criticalPressure(compIdx); - const auto& p = fluidState.pressure(0); //for now assume no capillary pressure + const auto& p = fluid_state.pressure(0); //for now assume no capillary pressure const auto& tmp = Opm::exp(5.3727 * (1+acf) * (1-T_crit/T)) * (p_crit/p); return tmp; } template - static typename Vector::field_type li_single_phase_label_(const FlashFluidState& fluidState, const Vector& globalComposition, int verbosity) + static typename Vector::field_type li_single_phase_label_(const FlashFluidState& fluid_state, const Vector& z, int verbosity) { // Calculate intermediate sum typename Vector::field_type sumVz = 0.0; @@ -236,7 +236,7 @@ protected: const auto& V_crit = FluidSystem::criticalVolume(compIdx); // Sum calculation - sumVz += (V_crit * globalComposition[compIdx]); + sumVz += (V_crit * z[compIdx]); } // Calculate approximate (pseudo) critical temperature using Li's method @@ -247,11 +247,11 @@ protected: const auto& T_crit = FluidSystem::criticalTemperature(compIdx); // Sum calculation - Tc_est += (V_crit * T_crit * globalComposition[compIdx] / sumVz); + Tc_est += (V_crit * T_crit * z[compIdx] / sumVz); } // Get temperature - const auto& T = fluidState.temperature(0); + const auto& T = fluid_state.temperature(0); // If temperature is below estimated critical temperature --> phase = liquid; else vapor typename Vector::field_type L; @@ -280,27 +280,27 @@ protected: // TODO: not totally sure whether ValueType can be obtained from Vector::field_type template - static typename Vector::field_type rachfordRice_g_(const Vector& K, typename Vector::field_type L, const Vector& globalComposition) + static typename Vector::field_type rachfordRice_g_(const Vector& K, typename Vector::field_type L, const Vector& z) { typename Vector::field_type g=0; for (int compIdx=0; compIdx - static typename Vector::field_type rachfordRice_dg_dL_(const Vector& K, const typename Vector::field_type L, const Vector& globalComposition) + static typename Vector::field_type rachfordRice_dg_dL_(const Vector& K, const typename Vector::field_type L, const Vector& z) { typename Vector::field_type dg=0; for (int compIdx=0; compIdx - static typename Vector::field_type solveRachfordRice_g_(const Vector& K, const Vector& globalComposition, int verbosity) + static typename Vector::field_type solveRachfordRice_g_(const Vector& K, const Vector& z, int verbosity) { // Find min and max K. Have to do a laborious for loop to avoid water component (where K=0) // TODO: Replace loop with Dune::min_value() and Dune::max_value() when water component is properly handled @@ -337,8 +337,8 @@ protected: // Newton-Raphson loop for (int iteration=1; iteration<100; ++iteration){ // Calculate function and derivative values - auto g = rachfordRice_g_(K, L, globalComposition); - auto dg_dL = rachfordRice_dg_dL_(K, L, globalComposition); + auto g = rachfordRice_g_(K, L, z); + auto dg_dL = rachfordRice_dg_dL_(K, L, z); // Lnew = Lold - g/dg; auto delta = g/dg_dL; @@ -353,7 +353,7 @@ protected: } // Run bisection - L = bisection_g_(K, Lmin, Lmax, globalComposition, verbosity); + L = bisection_g_(K, Lmin, Lmax, z, verbosity); // Ensure that L is in the range (0, 1) L = Opm::min(Opm::max(L, 0.0), 1.0); @@ -387,10 +387,10 @@ protected: template static typename Vector::field_type bisection_g_(const Vector& K, typename Vector::field_type Lmin, - typename Vector::field_type Lmax, const Vector& globalComposition, int verbosity) + typename Vector::field_type Lmax, const Vector& z, int verbosity) { // Calculate for g(Lmin) for first comparison with gMid = g(L) - typename Vector::field_type gLmin = rachfordRice_g_(K, Lmin, globalComposition); + typename Vector::field_type gLmin = rachfordRice_g_(K, Lmin, z); // Print new header if (verbosity == 3 || verbosity == 4) { @@ -401,7 +401,7 @@ protected: for (int iteration=1; iteration<100; ++iteration){ // New midpoint auto L = (Lmin + Lmax) / 2; - auto gMid = rachfordRice_g_(K, L, globalComposition); + auto gMid = rachfordRice_g_(K, L, z); if (verbosity == 3 || verbosity == 4) { std::cout << std::setw(10) << iteration << std::setw(16) << gMid << std::setw(16) << L << std::endl; } @@ -426,7 +426,7 @@ protected: } template - static void phaseStabilityTest_(bool& isStable, ComponentVector& K, FlashFluidState& fluidState, const ComponentVector& globalComposition, int verbosity) + static void phaseStabilityTest_(bool& isStable, ComponentVector& K, FlashFluidState& fluid_state, const ComponentVector& z, int verbosity) { // Declarations bool isTrivialL, isTrivialV; @@ -439,24 +439,24 @@ protected: if (verbosity == 3 || verbosity == 4) { std::cout << "Stability test for vapor phase:" << std::endl; } - checkStability_(fluidState, isTrivialV, K0, y, S_v, globalComposition, /*isGas=*/true, verbosity); + checkStability_(fluid_state, isTrivialV, K0, y, S_v, z, /*isGas=*/true, verbosity); bool V_unstable = (S_v < (1.0 + 1e-5)) || isTrivialV; // Check for liquids stable phase if (verbosity == 3 || verbosity == 4) { std::cout << "Stability test for liquid phase:" << std::endl; } - checkStability_(fluidState, isTrivialL, K1, x, S_l, globalComposition, /*isGas=*/false, verbosity); + checkStability_(fluid_state, isTrivialL, K1, x, S_l, z, /*isGas=*/false, verbosity); bool L_stable = (S_l < (1.0 + 1e-5)) || isTrivialL; // L-stable means success in making liquid, V-unstable means no success in making vapour isStable = L_stable && V_unstable; if (isStable) { // Single phase, i.e. phase composition is equivalent to the global composition - // Update fluidstate with mole fraction + // Update fluid_state with mole fraction for (int compIdx=0; compIdx - static void checkStability_(const FlashFluidState& fluidState, bool& isTrivial, ComponentVector& K, ComponentVector& xy_loc, - typename FlashFluidState::Scalar& S_loc, const ComponentVector& globalComposition, bool isGas, int verbosity) + static void checkStability_(const FlashFluidState& fluid_state, bool& isTrivial, ComponentVector& K, ComponentVector& xy_loc, + typename FlashFluidState::Scalar& S_loc, const ComponentVector& z, bool isGas, int verbosity) { using FlashEval = typename FlashFluidState::Scalar; using PengRobinsonMixture = typename Opm::PengRobinsonMixture; // Declarations - FlashFluidState fluidState_fake = fluidState; - FlashFluidState fluidState_global = fluidState; + FlashFluidState fluid_state_fake = fluid_state; + FlashFluidState fluid_state_global = fluid_state; // Setup output if (verbosity >= 3 || verbosity >= 4) { @@ -489,22 +489,22 @@ protected: S_loc = 0.0; if (isGas) { for (int compIdx=0; compIdx(oilPhaseIdx) : static_cast(gasPhaseIdx)); // TODO: not sure the following makes sense for (int compIdx=0; compIdx paramCache_fake; - paramCache_fake.updatePhase(fluidState_fake, phaseIdx); + paramCache_fake.updatePhase(fluid_state_fake, phaseIdx); typename FluidSystem::template ParameterCache paramCache_global; - paramCache_global.updatePhase(fluidState_global, phaseIdx2); + paramCache_global.updatePhase(fluid_state_global, phaseIdx2); //fugacity for fake phases each component for (int compIdx=0; compIdx - static void computeLiquidVapor_(FlashFluidState& fluidState, typename FlashFluidState::Scalar& L, ComponentVector& K, const ComponentVector& globalComposition) + static void computeLiquidVapor_(FlashFluidState& fluid_state, typename FlashFluidState::Scalar& L, ComponentVector& K, const ComponentVector& z) { // Calculate x and y, and normalize ComponentVector x; @@ -584,19 +584,17 @@ protected: typename FlashFluidState::Scalar sumx=0; typename FlashFluidState::Scalar sumy=0; for (int compIdx=0; compIdx static void newtonComposition_(ComponentVector& K, typename FlashFluidState::Scalar& L, - FlashFluidState& fluidState, const ComponentVector& globalComposition, + FlashFluidState& fluid_state, const ComponentVector& z, int verbosity) { // Note: due to the need for inverse flash update for derivatives, the following two can be different @@ -653,7 +651,7 @@ protected: NewtonMatrix jac (0.); // Compute x and y from K, L and Z - computeLiquidVapor_(fluidState, L, K, globalComposition); + computeLiquidVapor_(fluid_state, L, K, z); std::cout << " the current L is " << Opm::getValue(L) << std::endl; // Print initial condition @@ -661,16 +659,16 @@ protected: std::cout << "Initial guess: x = ["; for (int compIdx=0; compIdx - static void updateCurrentSol_(DefectVector& x, DefectVector& d) - { - // Find smallest percentage update - Scalar w = 1.0; - for (size_t i=0; i - static bool checkFugacityEquil_(DefectVector& b) - { - // Init. fugacity vector - DefectVector fugVec; - - // Loop over b and find the fugacity equilibrium - // OBS: If the equations in b changes in evalDefect_ then you have to change here as well! - for (int compIdx=0; compIdx z - x; + // z - L*x - (1-L) * y ---> z - x; auto local_res = -global_composition[compIdx] + x[compIdx]; res[compIdx] = Opm::getValue(local_res); for (unsigned i = 0; i < num_primary; ++i) { @@ -915,8 +874,7 @@ protected: } { - // f_liquid - f_vapor = 0 - // -->z - y; + // f_liquid - f_vapor = 0 -->z - y; auto local_res = -global_composition[compIdx] + y[compIdx]; res[compIdx + numComponents] = Opm::getValue(local_res); for (unsigned i = 0; i < num_primary; ++i) { @@ -924,8 +882,7 @@ protected: } } } - - + // sum(x) - sum(y) = 0 auto local_res = l; if(isGas) { auto local_res = l-1; @@ -933,7 +890,6 @@ protected: else { auto local_res = l; } - res[num_equation - 1] = Opm::getValue(local_res); for (unsigned i = 0; i < num_primary; ++i) { jac[num_equation - 1][i] = local_res.derivative(i); @@ -1016,7 +972,7 @@ protected: using PrimaryFlashFluidState = Opm::CompositionalFluidState; PrimaryFlashFluidState primary_fluid_state; - // primary_z is not needed, because we use the globalComposition will be okay here + // primary_z is not needed, because we use z will be okay here PrimaryComponentVector primary_z; for (unsigned comp_idx = 0; comp_idx < numComponents; ++comp_idx) { primary_z[comp_idx] = Opm::getValue(z[comp_idx]); @@ -1152,94 +1108,11 @@ protected: } }//end updateDerivatives - template - static void evalDefect_(DefectVector& b, - DefectVector& x, - const FluidState& fluidStateIn, - const ComponentVector& globalComposition) - { - // Put x and y in a FluidState instance for fugacity calculations - FluidState fluidState(fluidStateIn); - ComponentVector K; - for (int compIdx=0; compIdx; - ParamCache paramCache; - for (int phaseIdx=0; phaseIdx - static void evalJacobian_(DefectMatrix& A, - const DefectVector& xIn, - const FluidState& fluidStateIn, - const ComponentVector& globalComposition) - { - // TODO: Use AD instead - // Calculate response of current state x - DefectVector x; - DefectVector b0; - for(size_t j=0; j - static void successiveSubstitutionComposition_(ComponentVector& K, typename ComponentVector::field_type& L, FlashFluidState& fluidState, const ComponentVector& globalComposition, + static void successiveSubstitutionComposition_(ComponentVector& K, typename ComponentVector::field_type& L, FlashFluidState& fluid_state, const ComponentVector& z, const bool newton_afterwards, const int verbosity) { - // std::cout << " Evaluation in successiveSubstitutionComposition_ is " << Dune::className(L) << std::endl; // Determine max. iterations based on if it will be used as a standalone flash or as a pre-process to Newton (or other) method. const int maxIterations = newton_afterwards ? 3 : 10; @@ -1261,16 +1134,16 @@ protected: // for (int i=0; i < maxIterations; ++i){ // Compute (normalized) liquid and vapor mole fractions - computeLiquidVapor_(fluidState, L, K, globalComposition); + computeLiquidVapor_(fluid_state, L, K, z); // Calculate fugacity coefficient using ParamCache = typename FluidSystem::template ParameterCache; ParamCache paramCache; for (int phaseIdx=0; phaseIdx