29 IonsFromNeutralVPSSTP::IonsFromNeutralVPSSTP() :
30 ionSolnType_(cIonSolnType_SINGLEANION),
31 numNeutralMoleculeSpecies_(0),
32 indexSpecialSpecies_(
npos),
33 neutralMoleculePhase_(0),
39 const std::string& id_) :
40 ionSolnType_(cIonSolnType_SINGLEANION),
41 numNeutralMoleculeSpecies_(0),
42 indexSpecialSpecies_(
npos)
48 const std::string& id_) :
49 ionSolnType_(cIonSolnType_SINGLEANION),
50 numNeutralMoleculeSpecies_(0),
51 indexSpecialSpecies_(
npos)
92 vector_fp& charges, std::vector<size_t>& neutMolIndex)
const
105 for (
size_t k = 0; k <
m_kk; k++) {
115 doublereal xx, fact2;
121 case cIonSolnType_PASSTHROUGH:
124 case cIonSolnType_SINGLEANION:
126 fact2 = 2.0 *
RT() * log(2.0);
141 mu[icat] =
RT() * log(xx);
152 case cIonSolnType_SINGLECATION:
153 throw CanteraError(
"IonsFromNeutralVPSSTP::getChemPotentials",
"Unknown type");
154 case cIonSolnType_MULTICATIONANION:
155 throw CanteraError(
"IonsFromNeutralVPSSTP::getChemPotentials",
"Unknown type");
157 throw CanteraError(
"IonsFromNeutralVPSSTP::getChemPotentials",
"Unknown type");
167 for (
size_t k = 0; k <
m_kk; k++) {
175 for (
size_t k = 0; k <
m_kk; k++) {
190 for (
size_t k = 0; k <
m_kk; k++) {
196 for (
size_t k = 0; k <
m_kk; k++) {
206 for (
size_t k = 0; k <
m_kk; k++) {
216 for (
size_t k = 0; k <
m_kk; k++) {
226 for (
size_t k = 0; k <
m_kk; k++) {
227 for (
size_t m = 0; m <
m_kk; m++) {
228 dlnActCoeffdlnN[ld * k + m] = data[
m_kk * k + m];
250 for (
size_t k = 0; k <
m_kk; k++) {
256 for (
size_t k = 0; k <
m_kk; k++) {
263 doublereal sum = 0.0;
264 for (
size_t k = 0; k <
m_kk; k++) {
267 for (
size_t k = 0; k <
m_kk; k++) {
276 doublereal sum = 0.0;
283 for (
size_t k = 0; k <
m_kk; k++) {
286 if (fabs(sum) > 1.0E-11) {
287 throw CanteraError(
"IonsFromNeutralVPSSTP::calcNeutralMoleculeMoleFractions",
288 "molefracts don't sum to one: {}", sum);
292 case cIonSolnType_PASSTHROUGH:
293 for (
size_t k = 0; k <
m_kk; k++) {
298 case cIonSolnType_SINGLEANION:
321 for (
size_t k = 0; k <
m_kk; k++) {
325 for (
size_t k = 0; k <
m_kk; k++) {
330 for (
size_t k = 0; k <
m_kk; k++) {
334 throw CanteraError(
"IonsFromNeutralVPSSTP::calcNeutralMoleculeMoleFractions",
335 "neutral molecule calc error");
339 throw CanteraError(
"IonsFromNeutralVPSSTP::calcNeutralMoleculeMoleFractions",
340 "neutral molecule calc error - anion");
356 case cIonSolnType_SINGLECATION:
357 throw CanteraError(
"IonsFromNeutralVPSSTP::calcNeutralMoleculeMoleFractions",
"Unknown type");
359 case cIonSolnType_MULTICATIONANION:
360 throw CanteraError(
"IonsFromNeutralVPSSTP::calcNeutralMoleculeMoleFractions",
"Unknown type");
363 throw CanteraError(
"IonsFromNeutralVPSSTP::calcNeutralMoleculeMoleFractions",
"Unknown type");
370 doublereal sumy, sumdy;
381 case cIonSolnType_PASSTHROUGH:
382 for (
size_t k = 0; k <
m_kk; k++) {
387 case cIonSolnType_SINGLEANION:
395 const doublereal temp = 1.0/fmij;
396 dy[jNeut] += dx[icat] * temp;
405 const doublereal temp = 1.0/fmij;
406 dy[jNeut] += dx[icat] * temp;
418 dy[k] = dy[k] * sumy - y_[k]*sumdy*sumy*sumy;
423 case cIonSolnType_SINGLECATION:
424 throw CanteraError(
"IonsFromNeutralVPSSTP::getNeutralMoleculeMoleGrads",
427 case cIonSolnType_MULTICATIONANION:
428 throw CanteraError(
"IonsFromNeutralVPSSTP::getNeutralMoleculeMoleGrads",
432 throw CanteraError(
"IonsFromNeutralVPSSTP::getNeutralMoleculeMoleGrads",
458 const size_t nElementsN,
459 const std::vector<std::string>& elnamesVI ,
461 const size_t nElementsI)
463 double fMax = 1.0E100;
464 for (
size_t mi = 0; mi < nElementsI; mi++) {
465 if (elnamesVI[mi] !=
"E" && elemVectorI[mi] > 1.0E-13) {
466 double eiNum = elemVectorI[mi];
467 for (
size_t mn = 0; mn < nElementsN; mn++) {
468 if (elnamesVI[mi] == elnamesVN[mn]) {
469 if (elemVectorN[mn] <= 1.0E-13) {
472 fMax = std::min(fMax, elemVectorN[mn]/eiNum);
490 string neutralName =
m_input[
"neutral-phase"].asString();
491 const auto& slash = boost::ifind_last(neutralName,
"/");
493 string fileName(neutralName.begin(), slash.begin());
494 neutralName = string(slash.end(), neutralName.end());
497 AnyMap& phaseNode = infile[
"phases"].getMapWhere(
"name", neutralName);
498 setNeutralMoleculePhase(
newPhase(phaseNode, infile));
507 "IonsFromNeutralVPSSTP::initThermo",
508 "The neutral phase has not been initialized. Are you missing the "
509 "'neutral-phase' key?"
518 const std::vector<std::string>& elnamesVI =
elementNames();
523 "IonsFromNeutralVPSSTP::initThermo",
524 "No special-species were specified in the phase."
527 for (
size_t m = 0; m < nElementsI; m++) {
532 for (
size_t m = 0; m < nElementsN; m++) {
536 double fac =
factorOverlap(elnamesVN, elemVectorN, nElementsN,
537 elnamesVI ,elemVectorI, nElementsI);
539 for (
size_t m = 0; m < nElementsN; m++) {
540 for (
size_t mi = 0; mi < nElementsI; mi++) {
541 if (elnamesVN[m] == elnamesVI[mi]) {
542 elemVectorN[m] -= fac * elemVectorI[mi];
550 for (
size_t k = 0; k <
m_kk; k++) {
551 for (
size_t m = 0; m < nElementsI; m++) {
552 elemVectorI[m] =
nAtoms(k, m);
555 elnamesVI ,elemVectorI, nElementsI);
557 for (
size_t m = 0; m < nElementsN; m++) {
558 for (
size_t mi = 0; mi < nElementsI; mi++) {
559 if (elnamesVN[m] == elnamesVI[mi]) {
560 elemVectorN[m] -= fac * elemVectorI[mi];
564 bool notTaken =
true;
565 for (
size_t iNeut = 0; iNeut < jNeut; iNeut++) {
574 "Simple formula matrix generation failed, one cation is shared between two salts");
581 for (
size_t m = 0; m < nElementsN; m++) {
582 if (fabs(elemVectorN[m]) > 1.0E-13) {
584 "Simple formula matrix generation failed");
592 void IonsFromNeutralVPSSTP::setNeutralMoleculePhase(shared_ptr<ThermoPhase> neutral)
608 for (
size_t k = 0; k <
nSpecies(); k++) {
613 shared_ptr<ThermoPhase> IonsFromNeutralVPSSTP::getNeutralMoleculePhase()
624 m_work.push_back(0.0);
628 if (spec->charge > 0) {
630 }
else if (spec->charge < 0) {
636 if (spec->input.hasKey(
"equation-of-state")) {
637 auto& ss = spec->input[
"equation-of-state"].getMapWhere(
638 "model",
"ions-from-neutral-molecule");
639 if (ss.getBool(
"special-species",
false)) {
651 if (!thermoNode.
hasChild(
"neutralMoleculePhase")) {
652 throw CanteraError(
"IonsFromNeutralVPSSTP::setParametersFromXML",
653 "no neutralMoleculePhase XML node");
655 XML_Node& neutralMoleculeNode = thermoNode.
child(
"neutralMoleculePhase");
659 throw CanteraError(
"IonsFromNeutralVPSSTP::setParametersFromXML",
663 setNeutralMoleculePhase(shared_ptr<ThermoPhase>(
newPhase(*neut_ptr)));
673 case cIonSolnType_PASSTHROUGH:
675 case cIonSolnType_SINGLEANION:
698 case cIonSolnType_SINGLECATION:
699 throw CanteraError(
"IonsFromNeutralVPSSTP::s_update_lnActCoeff",
"Unimplemented type");
701 case cIonSolnType_MULTICATIONANION:
702 throw CanteraError(
"IonsFromNeutralVPSSTP::s_update_lnActCoeff",
"Unimplemented type");
705 throw CanteraError(
"IonsFromNeutralVPSSTP::s_update_lnActCoeff",
"Unimplemented type");
711 doublereal* dlnActCoeffds)
const
716 for (
size_t k = 0; k <
m_kk; k++) {
725 geThermo->
getdlnActCoeffds(dTds, dX_NeutralMolecule_.data(), dlnActCoeff_NeutralMolecule_.data());
728 case cIonSolnType_PASSTHROUGH:
730 case cIonSolnType_SINGLEANION:
737 dlnActCoeffds[icat] = dlnActCoeff_NeutralMolecule_[jNeut]/fmij;
743 dlnActCoeffds[icat]= 0.0;
749 dlnActCoeffds[icat] = dlnActCoeff_NeutralMolecule_[jNeut];
753 case cIonSolnType_SINGLECATION:
754 throw CanteraError(
"IonsFromNeutralVPSSTP::getdlnActCoeffds",
"Unimplemented type");
756 case cIonSolnType_MULTICATIONANION:
757 throw CanteraError(
"IonsFromNeutralVPSSTP::getdlnActCoeffds",
"Unimplemented type");
760 throw CanteraError(
"IonsFromNeutralVPSSTP::getdlnActCoeffds",
"Unimplemented type");
778 case cIonSolnType_PASSTHROUGH:
780 case cIonSolnType_SINGLEANION:
803 case cIonSolnType_SINGLECATION:
804 throw CanteraError(
"IonsFromNeutralVPSSTP::s_update_dlnActCoeffdT",
"Unimplemented type");
806 case cIonSolnType_MULTICATIONANION:
807 throw CanteraError(
"IonsFromNeutralVPSSTP::s_update_dlnActCoeffdT",
"Unimplemented type");
810 throw CanteraError(
"IonsFromNeutralVPSSTP::s_update_dlnActCoeffdT",
"Unimplemented type");
828 case cIonSolnType_PASSTHROUGH:
830 case cIonSolnType_SINGLEANION:
853 case cIonSolnType_SINGLECATION:
854 throw CanteraError(
"IonsFromNeutralVPSSTP::s_update_dlnActCoeff_dlnX_diag",
"Unimplemented type");
856 case cIonSolnType_MULTICATIONANION:
857 throw CanteraError(
"IonsFromNeutralVPSSTP::s_update_dlnActCoeff_dlnX_diag",
"Unimplemented type");
860 throw CanteraError(
"IonsFromNeutralVPSSTP::s_update_dlnActCoeff_dlnX_diag",
"Unimplemented type");
878 case cIonSolnType_PASSTHROUGH:
880 case cIonSolnType_SINGLEANION:
903 case cIonSolnType_SINGLECATION:
904 throw CanteraError(
"IonsFromNeutralVPSSTP::s_update_dlnActCoeff_dlnN_diag",
"Unimplemented type");
906 case cIonSolnType_MULTICATIONANION:
907 throw CanteraError(
"IonsFromNeutralVPSSTP::s_update_dlnActCoeff_dlnN_diag",
"Unimplemented type");
910 throw CanteraError(
"IonsFromNeutralVPSSTP::s_update_dlnActCoeff_dlnN_diag",
"Unimplemented type");
917 size_t kcat = 0, kNeut = 0, mcat = 0, mNeut = 0;
918 doublereal fmij = 0.0;
922 throw CanteraError(
"IonsFromNeutralVPSSTP::s_update_dlnActCoeff_dlnN",
"dynamic cast failed");
924 size_t nsp_ge = geThermo->
nSpecies();
928 case cIonSolnType_PASSTHROUGH:
930 case cIonSolnType_SINGLEANION:
957 for (
size_t k = 0; k <
m_kk; k++) {
968 for (
size_t m = 0; m <
m_kk; m++) {
982 case cIonSolnType_SINGLECATION:
983 throw CanteraError(
"IonsFromNeutralVPSSTP::s_update_dlnActCoeff_dlnN",
"Unimplemented type");
985 case cIonSolnType_MULTICATIONANION:
986 throw CanteraError(
"IonsFromNeutralVPSSTP::s_update_dlnActCoeff_dlnN",
"Unimplemented type");
989 throw CanteraError(
"IonsFromNeutralVPSSTP::s_update_dlnActCoeff_dlnN",
"Unimplemented type");
Header for intermediate ThermoPhase object for phases which consist of ions whose thermodynamics is c...
Declarations for the class PDSS_IonsFromNeutral ( which handles calculations for a single ion in a fl...
Headers for the factory class that can create known ThermoPhase objects (see Thermodynamic Properties...
A map of string keys to values whose type can vary at runtime.
const std::string & getString(const std::string &key, const std::string &default_) const
If key exists, return it as a string, otherwise return default_.
static AnyMap fromYamlFile(const std::string &name, const std::string &parent_name="")
Create an AnyMap from a YAML file.
bool hasKey(const std::string &key) const
Returns true if the map contains an item named key.
void resize(size_t n, size_t m, doublereal v=0.0)
Resize the array, and fill the new entries with 'v'.
void zero()
Set all of the entries to zero.
Base class for exceptions thrown by Cantera classes.
virtual void getdlnActCoeffdlnN(const size_t ld, doublereal *const dlnActCoeffdlnN)
Get the array of derivatives of the log activity coefficients with respect to the log of the species ...
Array2D dlnActCoeffdlnN_
Storage for the current derivative values of the gradients with respect to logarithm of the species m...
vector_fp dlnActCoeffdlnX_diag_
Storage for the current derivative values of the gradients with respect to logarithm of the mole frac...
vector_fp lnActCoeff_Scaled_
Storage for the current values of the activity coefficients of the species.
vector_fp moleFractions_
Storage for the current values of the mole fractions of the species.
vector_fp dlnActCoeffdlnN_diag_
Storage for the current derivative values of the gradients with respect to logarithm of the mole frac...
virtual void compositionChanged()
Apply changes to the state which are needed after the composition changes.
virtual void getdlnActCoeffdT(doublereal *dlnActCoeffdT) const
Get the array of temperature derivatives of the log activity coefficients.
vector_fp dlnActCoeffdT_Scaled_
Storage for the current derivative values of the gradients with respect to temperature of the log of ...
AnyMap m_rootNode
Root node of the AnyMap which contains this phase definition.
virtual void setParameters(const AnyMap &phaseNode, const AnyMap &rootNode=AnyMap())
Set equation of state parameters from an AnyMap phase description.
vector_fp dlnActCoeffdT_NeutralMolecule_
Storage vector for the neutral molecule d ln activity coefficients dT.
virtual void getActivityCoefficients(doublereal *ac) const
Get the array of non-dimensional molar-based activity coefficients at the current solution temperatur...
vector_fp lnActCoeff_NeutralMolecule_
Storage vector for the neutral molecule ln activity coefficients.
virtual void calcNeutralMoleculeMoleFractions() const
Calculate neutral molecule mole fractions.
virtual doublereal cp_mole() const
Molar heat capacity at constant pressure. Units: J/kmol/K.
virtual void getPartialMolarEnthalpies(doublereal *hbar) const
Returns an array of partial molar enthalpies for the species in the mixture.
void getNeutralMoleculeMoleGrads(const doublereal *const dx, doublereal *const dy) const
Calculate neutral molecule mole fractions.
virtual void getPartialMolarEntropies(doublereal *sbar) const
Returns an array of partial molar entropies for the species in the mixture.
vector_fp moleFractionsTmp_
Temporary mole fraction vector.
std::vector< size_t > passThroughList_
List of the species in this ThermoPhase which are passed through to the neutralMoleculePhase ThermoPh...
virtual doublereal enthalpy_mole() const
Return the Molar enthalpy. Units: J/kmol.
void s_update_dlnActCoeff_dlnN_diag() const
Update the derivative of the log of the activity coefficients wrt log(number of moles) - diagonal com...
virtual void getdlnActCoeffdlnN(const size_t ld, doublereal *const dlnActCoeffdlnN)
Get the array of derivatives of the log activity coefficients with respect to the log of the species ...
vector_fp muNeutralMolecule_
Storage vector for the neutral molecule chemical potentials.
virtual doublereal cv_mole() const
Molar heat capacity at constant volume. Units: J/kmol/K.
virtual void getdlnActCoeffdlnX_diag(doublereal *dlnActCoeffdlnX_diag) const
Get the array of ln mole fraction derivatives of the log activity coefficients - diagonal component o...
virtual void calcIonMoleFractions(doublereal *const mf) const
Calculate ion mole fractions from neutral molecule mole fractions.
size_t indexSpecialSpecies_
Index of special species.
vector_fp NeutralMolecMoleFractions_
Mole fractions using the Neutral Molecule Mole fraction basis.
vector_fp fm_neutralMolec_ions_
Formula Matrix for composition of neutral molecules in terms of the molecules in this ThermoPhase.
void getDissociationCoeffs(vector_fp &fm_neutralMolec_ions, vector_fp &charges, std::vector< size_t > &neutMolIndex) const
Get the Salt Dissociation Coefficients.
vector_fp dlnActCoeffdlnN_diag_NeutralMolecule_
Storage vector for the neutral molecule d ln activity coefficients dlnN.
virtual doublereal entropy_mole() const
Molar entropy. Units: J/kmol/K.
std::vector< size_t > cationList_
List of the species in this ThermoPhase which are cation species.
Array2D dlnActCoeffdlnN_NeutralMolecule_
Storage vector for the neutral molecule d ln activity coefficients dlnN.
virtual void initThermo()
void s_update_dlnActCoeff_dlnN() const
Update the derivative of the log of the activity coefficients wrt log(number of moles) - diagonal com...
shared_ptr< ThermoPhase > neutralMoleculePhase_
This is a pointer to the neutral Molecule Phase.
size_t numNeutralMoleculeSpecies_
Number of neutral molecule species.
virtual doublereal gibbs_mole() const
Molar Gibbs function. Units: J/kmol.
std::vector< size_t > fm_invert_ionForNeutral
Mapping between ion species and neutral molecule for quick invert.
vector_fp dlnActCoeffdlnX_diag_NeutralMolecule_
Storage vector for the neutral molecule d ln activity coefficients dX - diagonal component.
virtual void getdlnActCoeffdlnN_diag(doublereal *dlnActCoeffdlnN_diag) const
Get the array of log species mole number derivatives of the log activity coefficients.
virtual void getdlnActCoeffds(const doublereal dTds, const doublereal *const dXds, doublereal *dlnActCoeffds) const
Get the change in activity coefficients wrt changes in state (temp, mole fraction,...
virtual void compositionChanged()
Apply changes to the state which are needed after the composition changes.
void s_update_dlnActCoeff_dlnX_diag() const
Update the derivative of the log of the activity coefficients wrt log(mole fraction)
void s_update_lnActCoeff() const
Update the activity coefficients.
virtual void getChemPotentials(doublereal *mu) const
Get the species chemical potentials. Units: J/kmol.
void s_update_dlnActCoeffdT() const
Update the temperature derivative of the ln activity coefficients.
IonSolnType_enumType ionSolnType_
Ion solution type.
virtual void setParametersFromXML(const XML_Node &thermoNode)
Set equation of state parameter values from XML entries.
virtual void calcDensity()
Calculate the density of the mixture using the partial molar volumes and mole fractions as input.
std::vector< size_t > anionList_
List of the species in this ThermoPhase which are anion species.
virtual void setParent(VPStandardStateTP *phase, size_t k)
Set the parent VPStandardStateTP object of this PDSS object.
void assignDensity(const double density_)
Set the internally stored constant density (kg/m^3) of the phase.
doublereal mean_X(const doublereal *const Q) const
Evaluate the mole-fraction-weighted mean of an array Q.
vector_fp m_speciesCharge
Vector of species charges. length m_kk.
size_t nSpecies() const
Returns the number of species in the phase.
size_t m_kk
Number of species in the phase.
doublereal nAtoms(size_t k, size_t m) const
Number of atoms of element m in species k.
const std::vector< std::string > & elementNames() const
Return a read-only reference to the vector of element names.
doublereal temperature() const
Temperature (K).
size_t nElements() const
Number of elements.
doublereal RT() const
Return the Gas Constant multiplied by the current temperature.
virtual void initThermoFile(const std::string &inputFile, const std::string &id)
virtual void getPartialMolarCp(doublereal *cpbar) const
Return an array of partial molar heat capacities for the species in the mixture.
virtual void getdlnActCoeffdlnX_diag(doublereal *dlnActCoeffdlnX_diag) const
Get the array of ln mole fraction derivatives of the log activity coefficients - diagonal component o...
virtual void setParameters(int n, doublereal *const c)
Set the equation of state parameters.
virtual void getdlnActCoeffds(const doublereal dTds, const doublereal *const dXds, doublereal *dlnActCoeffds) const
Get the change in activity coefficients wrt changes in state (temp, mole fraction,...
AnyMap m_input
Data supplied via setParameters.
virtual void setParametersFromXML(const XML_Node &eosdata)
Set equation of state parameter values from XML entries.
virtual bool addSpecies(shared_ptr< Species > spec)
Add a Species to this Phase.
virtual void getdlnActCoeffdlnN_diag(doublereal *dlnActCoeffdlnN_diag) const
Get the array of log species mole number derivatives of the log activity coefficients.
virtual void getEntropy_R(doublereal *sr) const
Get the array of nondimensional Entropy functions for the standard state species at the current T and...
virtual void initThermo()
virtual doublereal pressure() const
Returns the current pressure of the phase.
virtual void getEnthalpy_RT(doublereal *hrt) const
Get the nondimensional Enthalpy functions for the species at their standard states at the current T a...
Class XML_Node is a tree-based representation of the contents of an XML file.
bool hasChild(const std::string &ch) const
Tests whether the current node has a child node with a particular name.
XML_Node & child(const size_t n) const
Return a changeable reference to the n'th child of the current node.
#define AssertTrace(expr)
Assertion must be true or an error is thrown.
const size_t npos
index returned by functions to indicate "no position"
const double SmallNumber
smallest number to compare to zero.
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
const double GasConstant
Universal Gas Constant [J/kmol/K].
void importPhase(XML_Node &phase, ThermoPhase *th)
Import a phase information into an empty ThermoPhase object.
Namespace for the Cantera kernel.
static double factorOverlap(const std::vector< std::string > &elnamesVN, const vector_fp &elemVectorN, const size_t nElementsN, const std::vector< std::string > &elnamesVI, const vector_fp &elemVectorI, const size_t nElementsI)
Return the factor overlap.
Contains declarations for string manipulation functions within Cantera.