void assign(const FluidState& fs) { for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) { for (unsigned compIdx = 0; compIdx < numComponents; ++compIdx) { fugacityCoefficient_[phaseIdx][compIdx] = fs.fugacityCoefficient(phaseIdx, compIdx); } } }
static Scalar calculateDefect_(const FluidState ¶ms, int phaseIdx, const ComponentVector &targetFug) { Scalar result = 0.0; for (int i = 0; i < numComponents; ++i) { // sum of the fugacity defect weighted by the inverse // fugacity coefficient result += std::abs( (targetFug[i] - params.fugacity(phaseIdx, i)) / params.fugacityCoefficient(phaseIdx, i) ); }; return result; }
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); } } }
void assign(const FluidState& fs) { for (unsigned phaseIdx = 0; phaseIdx < numPhases; ++phaseIdx) { fugacityCoefficient_[phaseIdx] = fs.fugacityCoefficient(phaseIdx, /*compIdx=*/phaseIdx); } }