19vcs_VolPhase::vcs_VolPhase(
VCS_SOLVE* owningSolverObject,
21 : m_owningSolverObject(owningSolverObject)
23 if (!m_owningSolverObject) {
24 throw CanteraError(
"vcs_VolPhase::vcs_VolPhase",
25 "owningSolverObject must not be null");
28 throw CanteraError(
"vcs_VolPhase::vcs_VolPhase",
29 "thermoPhase must not be null");
33 PhaseName = thermoPhase->name().empty() ?
34 fmt::format(
"Phase_{}", VP_ID_) : thermoPhase->name();
36 m_numSpecies = thermoPhase->nSpecies();
39 m_singleSpecies = (m_numSpecies == 1);
42 p_activityConvention = thermoPhase->activityConvention();
44 IndSpecies.assign(m_numSpecies, npos);
45 Xmol_.assign(m_numSpecies, 0.0);
46 creationMoleNumbers_.assign(m_numSpecies, 0.0);
47 creationGlobalRxnNumbers_.assign(m_numSpecies, npos);
48 for (
size_t k = 0; k < m_numSpecies; k++) {
49 double defaultFrac = 1.0 / m_numSpecies;
50 Xmol_[k] = defaultFrac;
51 creationMoleNumbers_[k] = defaultFrac;
54 SS0ChemicalPotential.assign(m_numSpecies, -1.0);
55 StarChemicalPotential.assign(m_numSpecies, -1.0);
56 StarMolarVol.assign(m_numSpecies, -1.0);
57 PartialMolarVol.assign(m_numSpecies, -1.0);
58 ActCoeff.assign(m_numSpecies, 1.0);
59 np_dLnActCoeffdMolNumber.resize(m_numSpecies, m_numSpecies, 0.0);
64 m_UpToDate_AC =
false;
65 m_UpToDate_VolStar =
false;
66 m_UpToDate_VolPM =
false;
67 m_UpToDate_GStar =
false;
68 m_UpToDate_G0 =
false;
71 Temp_ = TP_ptr->temperature();
72 Pres_ = TP_ptr->pressure();
73 m_phi = TP_ptr->electricPotential();
75 transferElementsFM(TP_ptr);
79 size_t neVP = nElemConstraints();
80 for (
size_t eVP = 0; eVP < neVP; eVP++) {
81 string enVP = elementName(eVP);
82 size_t foundPos = m_owningSolverObject->elementIndex(enVP);
83 if (foundPos == npos) {
84 int elType = elementType(eVP);
85 int elactive = elementActive(eVP);
86 foundPos = m_owningSolverObject->addElement(enVP.c_str(), elType, elactive);
88 setElemGlobalIndex(eVP, foundPos);
91 setState_TP(Temp_, Pres_);
92 TP_ptr->getMoleFractions(Xmol_);
93 creationMoleNumbers_ = Xmol_;
94 _updateMoleFractionDependencies();
96 m_isIdealSoln = m_singleSpecies ? true : TP_ptr->isIdeal();
99void vcs_VolPhase::elemResize(
const size_t numElemConstraints)
101 m_elementNames.resize(numElemConstraints);
102 m_elementActive.resize(numElemConstraints+1, 1);
104 m_formulaMatrix.resize(m_numSpecies, numElemConstraints, 0.0);
105 m_elementNames.resize(numElemConstraints,
"");
106 m_elemGlobalIndex.resize(numElemConstraints, npos);
107 m_numElemConstraints = numElemConstraints;
156 if (std::fabs(sum) > 1.0E-13) {
169 TP_ptr->setMoleFractions(span<const double>(
184double vcs_VolPhase::moleFraction(
size_t k)
const
198 throw CanteraError(
"vcs_VolPhase::setMolesFractionsState",
199 "inappropriate usage");
204 throw CanteraError(
"vcs_VolPhase::setMolesFractionsState",
205 "inappropriate usage");
213 double fractotal = 1.0;
221 throw CanteraError(
"vcs_VolPhase::setMolesFractionsState",
222 "inappropriate usage");
224 if (sum != fractotal) {
226 Xmol_[k] *= (fractotal /sum);
233 span<const double> molesSpeciesVCS)
237 if (molesSpeciesVCS.empty()) {
243 throw CanteraError(
"vcs_VolPhase::setMolesFromVCS",
"shouldn't be here");
257 double tmp = std::max(0.0, molesSpeciesVCS[kglob]);
278 double phi = molesSpeciesVCS[kglob];
299 span<const double> molesSpeciesVCS,
300 span<const double> TPhMoles)
305 double Tcheck = TPhMoles[
VP_ID_];
310 throw CanteraError(
"vcs_VolPhase::setMolesFromVCSCheck",
311 "We have a consistency problem: {} {}", Tcheck,
v_totalMoles);
381 TP_ptr->setState_TP(temp, pres);
412 if (phaseTotalMoles < 1.0E-14) {
413 phaseTotalMoles = 1.0;
426 double moles_j_base = phaseTotalMoles *
Xmol_[j];
428 if (moles_j_base < 1.0E-200) {
429 moles_j_base = 1.0E-7 * moles_j_base + 1.0E-13 * phaseTotalMoles + 1.0E-150;
432 np_lnActCoeffCol[k] = np_lnActCoeffCol[k] * phaseTotalMoles / moles_j_base;
436 double deltaMoles_j = 0.0;
438 vector<double> ActCoeff_Base(
ActCoeff);
439 vector<double> Xmol_Base(
Xmol_);
440 double TMoles_base = phaseTotalMoles;
446 double moles_j_base = phaseTotalMoles * Xmol_Base[j];
447 deltaMoles_j = 1.0E-7 * moles_j_base + 1.0E-13 * phaseTotalMoles + 1.0E-150;
451 phaseTotalMoles = TMoles_base + deltaMoles_j;
453 Xmol_[k] = Xmol_Base[k] * TMoles_base / phaseTotalMoles;
455 Xmol_[j] = (moles_j_base + deltaMoles_j) / phaseTotalMoles;
502 span<const size_t> creationGlobalRxnNumbers)
513 span<size_t> creationGlobalRxnNumbers)
const
517 creationGlobalRxnNumbers.begin());
527 if (totalMols > 0.0) {
538 if (stateCalc != -1) {
559void vcs_VolPhase::setPhiVarIndex(
size_t phiVarIndex)
561 m_phiVarIndex = phiVarIndex;
563 if (m_singleSpecies && m_phiVarIndex == 0) {
578 "setting false existence for phase with moles");
582 "setting true existence for phase with no moles");
586 "Trying to set existence of an electron phase to false");
597 const size_t spGlobalIndex)
614 "vcs_VolPhase::setElemGlobalIndex");
635 for (
size_t k = 0; k < tPhase->
nSpecies(); k++) {
636 if (tPhase->
charge(k) != 0.0) {
677 size_t eFound =
npos;
686 for (
size_t eT = 0; eT < nebase; eT++) {
694 for (
size_t eT = 0; eT < nebase; eT++) {
701 if (eFound ==
npos) {
717 for (
size_t eT = 0; eT < nebase; eT++) {
724 string pname = tPhase->
name();
726 pname = fmt::format(
"phase{}",
VP_ID_);
732 for (
size_t k = 0; k < ns; k++) {
734 for (
size_t eT = 0; eT < nebase; eT++) {
738 if (eFound !=
npos) {
744 for (
size_t k = 0; k < ns; k++) {
752 if (ns == 1 && tPhase->
charge(0) != 0.0) {
775int vcs_VolPhase::elementActive(
const size_t e)
const
777 return m_elementActive[e];
Header file for class ThermoPhase, the base class for phases with thermodynamic properties,...
A class for 2D arrays stored in column-major (Fortran-compatible) form.
Base class for exceptions thrown by Cantera classes.
An array index is out of range.
size_t nSpecies() const
Returns the number of species in the phase.
int elementType(size_t m) const
Return the element constraint type Possible types include:
double nAtoms(size_t k, size_t m) const
Number of atoms of element m in species k.
size_t nElements() const
Number of elements.
string elementName(size_t m) const
Name of the element with index m.
double charge(size_t k) const
Dimensionless electrical charge of a single molecule of species k The charge is normalized by the the...
string name() const
Return the name of the phase.
Base class for a phase with thermodynamic properties.
bool chargeNeutralityNecessary() const
Returns the chargeNeutralityNecessity boolean.
This is the main structure used to hold the internal data used in vcs_solve_TP(), and to solve TP sys...
void setMoleFractions(span< const double > xmol)
Set the mole fractions from a conventional mole fraction vector.
vector< double > StarChemicalPotential
Vector of calculated Star chemical potentials for the current Temperature and pressure.
void setElectricPotential(const double phi)
set the electric potential of the phase
bool m_UpToDate_GStar
Boolean indicating whether GStar is up to date.
double electricPotential() const
Returns the electric field of the phase.
size_t m_phiVarIndex
If the potential is a solution variable in VCS, it acts as a species.
vector< double > Xmol_
Vector of the current mole fractions for species in the phase.
double Temp_
Current value of the temperature for this object, and underlying objects.
int speciesUnknownType(const size_t k) const
Returns the type of the species unknown.
size_t elemGlobalIndex(const size_t e) const
Returns the global index of the local element index for the phase.
void _updateGStar() const
Gibbs free energy calculation for standard states.
vector< double > SS0ChemicalPotential
Vector of calculated SS0 chemical potentials for the current Temperature.
void setMolesFromVCS(const int stateCalc, span< const double > molesSpeciesVCS={})
Set the moles within the phase.
size_t nSpecies() const
Return the number of species in the phase.
void setMolesOutOfDate(int stateCalc=-1)
Sets the mole flag within the object to out of date.
vector< string > m_elementNames
vector of strings containing the element constraint names
vector< int > m_elementActive
boolean indicating whether an element constraint is active for the current problem
vector< double > creationMoleNumbers_
Vector of current creationMoleNumbers_.
double _updateVolPM() const
Calculate the partial molar volumes of all species and return the total volume.
double m_totalVol
Total Volume of the phase. Units are m**3.
size_t ChargeNeutralityElement
This is the element number for the charge neutrality condition of the phase.
vector< size_t > m_elemGlobalIndex
Index of the element number in the global list of elements stored in VCS_SOLVE.
size_t VP_ID_
Original ID of the phase in the problem.
Array2D np_dLnActCoeffdMolNumber
Vector of the derivatives of the ln activity coefficient wrt to the current mole number multiplied by...
const Array2D & getFormulaMatrix() const
Get a constant form of the Species Formula Matrix.
vector< int > m_speciesUnknownType
Type of the species unknown.
void sendToVCS_ActCoeff(const int stateCalc, span< double > AC)
Fill in an activity coefficients vector within a VCS_SOLVE object.
bool m_UpToDate_VolStar
Boolean indicating whether Star volumes are up to date.
void updateFromVCS_MoleNumbers(const int stateCalc)
Update the moles within the phase, if necessary.
size_t phiVarIndex() const
Return the index of the species that represents the the voltage of the phase.
double v_totalMoles
Total mols in the phase. units are kmol.
bool m_singleSpecies
If true, this phase consists of a single species.
void setElemGlobalIndex(const size_t eLocal, const size_t eGlobal)
sets a local phase element to a global index value
int m_MFStartIndex
This is always equal to zero.
bool m_isIdealSoln
Boolean indicating whether the phase is an ideal solution and therefore its molar-based activity coef...
vector< double > ActCoeff
Vector of calculated activity coefficients for the current state.
int exists() const
Retrieve the kth Species structure for the species belonging to this phase.
vector< size_t > IndSpecies
Index into the species vectors.
size_t nElemConstraints() const
Returns the number of element constraints.
double m_phi
Value of the potential for the phase (Volts).
size_t spGlobalIndexVCS(const size_t spIndex) const
Return the Global VCS index of the kth species in the phase.
size_t transferElementsFM(const ThermoPhase *const tPhase)
Transfer all of the element information from the ThermoPhase object to the vcs_VolPhase object.
vector< int > m_elementType
Type of the element constraint.
span< const double > moleFractions() const
Return a const reference to the mole fractions stored in the object.
double GStar_calc_one(size_t kspec) const
Gibbs free energy calculation for standard state of one species.
void setMolesFromVCSCheck(const int vcsStateStatus, span< const double > molesSpeciesVCS, span< const double > TPhMoles)
Set the moles within the phase.
void _updateVolStar() const
Molar volume calculation for standard states.
int m_vcsStateStatus
Status.
bool m_UpToDate_G0
Boolean indicating whether G0 is up to date.
void _updateG0() const
Gibbs free energy calculation at a temperature for the reference state of each species.
double Pres_
Current value of the pressure for this object, and underlying objects.
vector< double > PartialMolarVol
Vector of the Partial molar Volumes of the species. units m3 / kmol.
double molefraction(size_t kspec) const
Returns the mole fraction of the kspec species.
Array2D m_formulaMatrix
Formula Matrix for the phase.
void _updateLnActCoeffJac()
Evaluation of Activity Coefficient Jacobians.
string elementName(size_t e) const
Name of the element constraint with index e.
void setTotalMoles(const double totalMols)
Sets the total moles in the phase.
void setCreationMoleNumbers(span< const double > n_k, span< const size_t > creationGlobalRxnNumbers)
Sets the creationMoleNum's within the phase object.
void setMolesCurrent(int vcsStateStatus)
Sets the mole flag within the object to be current.
size_t m_numElemConstraints
Number of element constraints within the problem.
vector< double > StarMolarVol
Vector of the Star molar Volumes of the species. units m3 / kmol.
void sendToVCS_LnActCoeffJac(Array2D &LnACJac_VCS)
Downloads the ln ActCoeff Jacobian into the VCS version of the ln ActCoeff Jacobian.
span< const double > creationMoleNumbers(span< size_t > creationGlobalRxnNumbers) const
Return a const reference to the creationMoleNumbers stored in the object.
double G0_calc_one(size_t kspec) const
Gibbs free energy calculation at a temperature for the reference state of a species,...
void _updateActCoeff() const
Evaluate the activity coefficients at the current conditions.
void setState_TP(const double temperature_Kelvin, const double pressure_PA)
Sets the temperature and pressure in this object and underlying ThermoPhase objects.
int m_existence
Current state of existence:
string eos_name() const
Return the name corresponding to the equation of state.
bool m_UpToDate_AC
Boolean indicating whether activity coefficients are up to date.
VCS_SOLVE * m_owningSolverObject
Backtrack value of VCS_SOLVE *.
void setMoleFractionsState(const double molNum, span< const double > moleFracVec, const int vcsStateStatus)
Set the moles and/or mole fractions within the phase.
ThermoPhase * TP_ptr
Vector of Species structures for the species belonging to this phase.
void _updateMoleFractionDependencies()
Updates the mole fraction dependencies.
double sendToVCS_VolPM(span< double > VolPM) const
Fill in the partial molar volume vector for VCS.
void sendToVCS_GStar(span< double > gstar) const
Fill in the standard state Gibbs free energy vector for VCS.
int elementType(const size_t e) const
Type of the element constraint with index e.
vector< size_t > creationGlobalRxnNumbers_
Vector of creation global reaction numbers for the phase stability problem.
void setSpGlobalIndexVCS(const size_t spIndex, const size_t spGlobalIndex)
set the Global VCS index of the kth species in the phase
size_t m_numSpecies
Number of species in the phase.
void setExistence(const int existence)
Set the existence flag in the object.
bool m_UpToDate
Boolean indicating whether the object has an up-to-date mole number vector and potential with respect...
bool m_UpToDate_VolPM
Boolean indicating whether partial molar volumes are up to date.
bool isIdealSoln() const
Returns whether the phase is an ideal solution phase.
double totalMoles() const
Return the total moles in the phase.
#define AssertThrow(expr, procedure)
Assertion must be true or an error is thrown.
#define AssertThrowMsg(expr, procedure,...)
Assertion must be true or an error is thrown.
Namespace for the Cantera kernel.
const size_t npos
index returned by functions to indicate "no position"
static bool chargeNeutralityElement(const ThermoPhase *const tPhase)
This utility routine decides whether a Cantera ThermoPhase needs a constraint equation representing t...
static bool hasChargedSpecies(const ThermoPhase *const tPhase)
This function decides whether a phase has charged species or not.
bool vcs_doubleEqual(double d1, double d2)
Simple routine to check whether two doubles are equal up to roundoff error.
void checkArraySize(const char *procedure, size_t available, size_t required)
Wrapper for throwing ArraySizeError.
Contains declarations for string manipulation functions within Cantera.
Header for the object representing each phase within vcs.
#define VCS_SPECIES_TYPE_INTERFACIALVOLTAGE
Unknown refers to the voltage level of a phase.
#define VCS_STATECALC_OLD
State Calculation based on the old or base mole numbers.
#define VCS_STATECALC_TMP
State Calculation based on a temporary set of mole numbers.
#define VCS_PHASE_EXIST_NO
Phase doesn't currently exist in the mixture.
#define VCS_PHASE_EXIST_ALWAYS
These defines are valid values for the phase existence flag.
#define VCS_ELEM_TYPE_ABSPOS
Normal element constraint consisting of positive coefficients for the formula matrix.
#define VCS_STATECALC_NEW
State Calculation based on the new or tentative mole numbers.
#define VCS_SPECIES_TYPE_MOLNUM
Unknown refers to mole number of a single species.
#define VCS_ELEM_TYPE_ELECTRONCHARGE
This refers to conservation of electrons.
#define VCS_ELEM_TYPE_CHARGENEUTRALITY
This refers to a charge neutrality of a single phase.
#define VCS_PHASE_EXIST_YES
Phase is a normal phase that currently exists.
#define VCS_PHASE_EXIST_ZEROEDPHASE
Phase currently is zeroed due to a programmatic issue.
Header file for the internal object that holds the vcs equilibrium problem (see Class VCS_SOLVE and C...