23 m_undefinedElementBehavior(UndefElement::add),
24 m_caseSensitiveSpecies(false),
54 xroot->
copy(root_xml);
62 throw CanteraError(
"Phase::setXMLdata",
"XML 'phase' node not found");
65 throw CanteraError(
"Phase::setXMLdata",
"Root XML node not found");
72 "To be removed after Cantera 2.5. Usage merged with 'Phase::name'");
79 "To be removed after Cantera 2.5. Usage merged with 'Phase::setName'");
103 throw IndexError(
"Phase::checkElementIndex",
"elements", m,
m_mm-1);
122 for (
size_t i = 0; i <
m_mm; i++) {
177 for (
size_t m = 0; m <
m_mm; m++) {
193 "Lowercase species name '{}' is not unique. "
194 "Set Phase::caseSensitiveSpecies to true to "
195 "enforce case sensitive species names", nLower);
211 if (loc ==
npos && nameStr.find(
':') !=
npos) {
216 "Retrieval of species indices via 'PhaseId:speciesName' or "
217 "'phaseName:speciesName' to be removed after Cantera 2.5.");
243 throw IndexError(
"Phase::checkSpeciesIndex",
"species", k,
m_kk-1);
263 return { {
"T", 0}, {
"D", 1} };
265 return { {
"T", 0}, {
"P", 1} };
269 return { {
"T", 0}, {
"D", 1}, {
"Y", 2} };
271 return { {
"T", 0}, {
"P", 1}, {
"Y", 2} };
280 return {
"TD",
"TP",
"UV",
"DP",
"HP",
"SP",
"SV"};
282 return {
"TP",
"HP",
"SP"};
286 return {
"TDX",
"TDY",
"TPX",
"TPY",
"UVX",
"UVY",
"DPX",
"DPY",
287 "HPX",
"HPY",
"SPX",
"SPY",
"SVX",
"SVY"};
289 return {
"TPX",
"TPY",
"HPX",
"HPY",
"SPX",
"SPY"};
300 return {
"TD",
"TP",
"UV",
"DP",
"HP",
"SP",
"SV"};
302 return {
"TP",
"HP",
"SP"};
328 state[native.at(
"D")] =
density();
332 if (native.count(
"X")) {
334 }
else if (native.count(
"Y")) {
360 if (native.count(
"X")) {
362 }
else if (native.count(
"Y")) {
374 for (
size_t k = 0; k <
m_kk; k++) {
375 double xk = std::max(x[k], 0.0);
384 const double invSum = 1.0/sum;
385 for (
size_t k=0; k <
m_kk; k++) {
391 for (
size_t k=0; k <
m_kk; k++) {
405 m_y.begin(), multiplies<double>());
422 for (
size_t k = 0; k <
m_kk; k++) {
423 m_y[k] = std::max(y[k], 0.0);
425 double norm = accumulate(
m_y.begin(),
m_y.end(), 0.0);
429 m_ym.begin(), multiplies<double>());
437 copy(y, y +
m_kk,
m_y.begin());
439 multiplies<double>());
440 sum = accumulate(
m_ym.begin(),
m_ym.end(), 0.0);
535 copy(mw.begin(), mw.end(), weights);
551 for (
size_t k = 0; k <
m_kk; k++) {
563 for (
size_t k = 0; k <
m_kk; k++) {
616 copy(
m_y.begin(),
m_y.end(), y);
635 double sum = 0.0, norm = 0.0;
636 for (
size_t k = 0; k !=
m_kk; ++k) {
637 double ck = std::max(conc[k], 0.0);
644 double rsum = 1.0/sum;
645 for (
size_t k = 0; k !=
m_kk; ++k) {
656 double sum = 0.0, norm = 0.0;
657 for (
size_t k = 0; k !=
m_kk; ++k) {
663 double rsum = 1.0/sum;
664 for (
size_t k = 0; k !=
m_kk; ++k) {
665 m_ym[k] = conc[k] * rsum;
674 doublereal Z_m = 0.0;
675 for (
size_t k = 0; k !=
m_kk; ++k) {
686 for (
size_t k = 0; k <
m_kk; k++) {
688 for (
size_t j = 0; j <
nElements(); j++) {
693 doublereal numerator = 0.0;
694 for (
size_t k = 0; k !=
m_kk; ++k) {
697 return numerator / denom;
719 if (density_ > 0.0) {
723 "density must be positive. density = {}", density_);
729 if (density_ > 0.0) {
733 "density must be positive. density = {}", density_);
739 doublereal cdens = 0.0;
740 for (
size_t k = 0; k <
m_kk; k++) {
748 return m_mmw*std::inner_product(
m_ym.begin(),
m_ym.end(), Q, 0.0);
753 return m_mmw*std::inner_product(
m_ym.begin(),
m_ym.end(), Q.begin(), 0.0);
759 for (
size_t k = 0; k <
m_kk; k++) {
766 int atomic_number, doublereal entropy298,
776 }
else if (weight == -12345.0) {
785 if (elnode && elnode->
hasChild(
"entropy298")) {
798 "Duplicate elements ({}) have different weights", symbol);
821 for (
size_t k = 0; k <
m_kk; k++) {
822 size_t m_old =
m_mm - 1;
823 for (
size_t m = 0; m < m_old; m++) {
834 if (m_species.find(spec->name) != m_species.end()) {
836 "Phase '{}' already contains a species named '{}'.",
840 for (
const auto& elem : spec->composition) {
844 case UndefElement::ignore:
847 case UndefElement::add:
853 case UndefElement::error:
856 "Species '{}' contains an undefined element '{}'.",
857 spec->name, elem.first);
860 comp[m] = elem.second;
864 m_species[spec->name] = spec;
878 if (spec->charge != 0.0) {
880 if (eindex !=
npos) {
881 doublereal ecomp = comp[eindex];
882 if (fabs(spec->charge + ecomp) > 0.001) {
885 "Input charge and element E compositions differ "
886 "for species " + spec->name);
890 comp[eindex] = -spec->charge;
898 comp[ne - 1] = - spec->charge;
901 for (
size_t m = 0; m < ne; m++) {
903 wt += comp[m] * aw[m];
909 wt = std::max(wt,
Tiny);
933 "New species name '{}' does not match existing name '{}'",
936 const shared_ptr<Species>& old = m_species[spec->name];
937 if (spec->composition != old->composition) {
939 "New composition for '{}' does not match existing composition",
942 m_species[spec->name] = spec;
950 "Invalid alias '{}': species already exists", alias);
957 "Unable to add alias '{}' "
958 "(original species '{}' not found).", alias,
name);
964 vector<std::string> isomerNames;
966 for (
const auto& k : m_species) {
967 if (k.second->composition == compMap) {
968 isomerNames.emplace_back(k.first);
987 "Unknown species '{}'",
name);
1024 multiplies<double>());
1035 for (
const auto& sp : comp) {
1039 "Unknown species '{}'", sp.first);
1049 for (
size_t k = 0; k !=
m_kk; ++k) {
1053 throw CanteraError(
"Phase::massFractionsToMoleFractions",
1054 "no input composition given");
1056 for (
size_t k = 0; k !=
m_kk; ++k) {
1065 throw CanteraError(
"Phase::moleFractionsToMassFractions",
1066 "no input composition given");
1068 double rmmw = 1.0/mmw;
1069 for (
size_t k = 0; k !=
m_kk; ++k) {
#define CT_ELEM_TYPE_ELECTRONCHARGE
This refers to conservation of electrons.
#define ENTROPY298_UNKNOWN
Number indicating we don't know the entropy of the element in its most stable state at 298....
Header file for class Phase.
Headers for the factory class that can create known ThermoPhase objects (see Thermodynamic Properties...
Base class for exceptions thrown by Cantera classes.
An array index is out of range.
const vector_fp & atomicWeights() const
Return a read-only reference to the vector of atomic weights.
doublereal m_mmw
mean molecular weight of the mixture (kg kmol-1)
doublereal entropyElement298(size_t m) const
Entropy of the element in its standard state at 298 K and 1 bar.
void getCharges(double *charges) const
Copy the vector of species charges into array charges.
vector_int m_elem_type
Vector of element types.
void getConcentrations(double *const c) const
Get the species concentrations (kmol/m^3).
double massFraction(size_t k) const
Return the mass fraction of a single species.
void setName(const std::string &nm)
Sets the string name for the phase.
vector_int m_atomicNumbers
element atomic numbers
double molarDensity() const
Molar density (kmol/m^3).
void assignDensity(const double density_)
Set the internally stored constant density (kg/m^3) of the phase.
virtual bool addSpecies(shared_ptr< Species > spec)
Add a Species to this Phase.
size_t addElement(const std::string &symbol, doublereal weight=-12345.0, int atomicNumber=0, doublereal entropy298=ENTROPY298_UNKNOWN, int elem_type=CT_ELEM_TYPE_ABSPOS)
Add an element.
virtual void setMoleFractions(const double *const x)
Set the mole fractions to the specified values.
virtual std::vector< std::string > fullStates() const
Return a vector containing full states defining a phase.
int changeElementType(int m, int elem_type)
Change the element type of the mth constraint Reassigns an element type.
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.
vector_fp m_rmolwts
inverse of species molecular weights (kmol kg-1)
void checkSpeciesIndex(size_t k) const
Check that the specified species index is in range.
std::map< std::string, size_t > m_speciesIndices
Map of species names to indices.
std::string name() const
Return the name of the phase.
ValueCache m_cache
Cached for saved calculations within each ThermoPhase.
const double * moleFractdivMMW() const
Returns a const pointer to the start of the moleFraction/MW array.
size_t nSpecies() const
Returns the number of species in the phase.
void setMoleFractionsByName(const compositionMap &xMap)
Set the species mole fractions by name.
bool m_caseSensitiveSpecies
Flag determining whether case sensitive species names are enforced.
doublereal charge(size_t k) const
Dimensionless electrical charge of a single molecule of species k The charge is normalized by the the...
void ignoreUndefinedElements()
Set behavior when adding a species containing undefined elements to just skip the species.
UndefElement::behavior m_undefinedElementBehavior
Flag determining behavior when adding species with an undefined element.
virtual void setMassFractions_NoNorm(const double *const y)
Set the mass fractions to the specified values without normalizing.
void assertCompressible(const std::string &setter) const
Ensure that phase is compressible.
std::string speciesSPName(int k) const
Returns the expanded species name of a species, including the phase name This is guaranteed to be uni...
vector_fp getCompositionFromMap(const compositionMap &comp) const
Converts a compositionMap to a vector with entries for each species Species that are not specified ar...
vector_fp m_y
Mass fractions of the species.
std::vector< std::string > m_elementNames
element names
void addUndefinedElements()
Set behavior when adding a species containing undefined elements to add those elements to the phase.
virtual void setConcentrationsNoNorm(const double *const conc)
Set the concentrations without ignoring negative concentrations.
vector_fp m_molwts
species molecular weights (kg kmol-1)
void setState_TRX(doublereal t, doublereal dens, const doublereal *x)
Set the internally stored temperature (K), density, and mole fractions.
XML_Node & xml() const
Returns a const reference to the XML_Node that describes the phase.
size_t m_kk
Number of species in the phase.
int atomicNumber(size_t m) const
Atomic number of element m.
virtual void modifySpecies(size_t k, shared_ptr< Species > spec)
Modify the thermodynamic data associated with a species.
std::string m_id
ID of the phase.
std::string id() const
Return the string id for the phase.
void setState_RX(doublereal rho, doublereal *x)
Set the density (kg/m^3) and mole fractions.
virtual std::vector< std::string > findIsomers(const compositionMap &compMap) const
Return a vector with isomers names matching a given composition map.
std::map< std::string, size_t > m_speciesLower
Map of lower-case species names to indices.
void setXMLdata(XML_Node &xmlPhase)
Stores the XML tree information for the current phase.
void setState_RY(doublereal rho, doublereal *y)
Set the density (kg/m^3) and mass fractions.
std::vector< std::string > m_speciesNames
Vector of the species names.
void saveState(vector_fp &state) const
Save the current internal state of the phase.
void setState_TRY(doublereal t, doublereal dens, const doublereal *y)
Set the internally stored temperature (K), density, and mass fractions.
vector_fp m_speciesComp
Atomic composition of the species.
void getMolecularWeights(vector_fp &weights) const
Copy the vector of molecular weights into vector weights.
void setID(const std::string &id)
Set the string id for the phase.
virtual std::map< std::string, size_t > nativeState() const
Return a map of properties defining the native state of a substance.
virtual std::vector< std::string > partialStates() const
Return a vector of settable partial property sets within a phase.
const vector_fp & molecularWeights() const
Return a const reference to the internal vector of molecular weights.
doublereal atomicWeight(size_t m) const
Atomic weight of element m.
virtual void setPressure(double p)
Set the internally stored pressure (Pa) at constant temperature and composition.
virtual bool isCompressible() const
Return whether phase represents a compressible substance.
void moleFractionsToMassFractions(const double *X, double *Y) const
Converts a mixture composition from mass fractions to mole fractions.
size_t elementIndex(const std::string &name) const
Return the index of element named 'name'.
void addSpeciesAlias(const std::string &name, const std::string &alias)
Add a species alias (i.e.
virtual void setConcentrations(const double *const conc)
Set the concentrations to the specified values within the phase.
void setState_TNX(doublereal t, doublereal n, const doublereal *x)
Set the internally stored temperature (K), molar density (kmol/m^3), and mole fractions.
virtual void setMolarDensity(const double molarDensity)
Set the internally stored molar density (kmol/m^3) of the phase.
size_t findSpeciesLower(const std::string &nameStr) const
Find lowercase species name in m_speciesIndices when case sensitive species names are not enforced an...
doublereal molecularWeight(size_t k) const
Molecular weight of species k.
vector_fp m_ym
m_ym[k] = mole fraction of species k divided by the mean molecular weight of mixture.
double concentration(const size_t k) const
Concentration of species k.
void checkElementArraySize(size_t mm) const
Check that an array size is at least nElements().
int elementType(size_t m) const
Return the element constraint type Possible types include:
std::string speciesName(size_t k) const
Name of the species with index k.
doublereal elementalMassFraction(const size_t m) const
Elemental mass fraction of element m.
virtual void setDensity(const double density_)
Set the internally stored density (kg/m^3) of the phase.
virtual size_t stateSize() const
Return size of vector defining internal state of the phase.
const double * massFractions() const
Return a const pointer to the mass fraction array.
vector_fp m_entropy298
Entropy at 298.15 K and 1 bar of stable state pure elements (J kmol-1)
doublereal meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
virtual bool isPure() const
Return whether phase represents a pure (single species) substance.
void getMoleFractions(double *const x) const
Get the species mole fraction vector.
void setState_TX(doublereal t, doublereal *x)
Set the internally stored temperature (K) and mole fractions.
vector_fp m_atomicWeights
element atomic weights (kg kmol-1)
compositionMap getMassFractionsByName(double threshold=0.0) const
Get the mass fractions by name.
double moleFraction(size_t k) const
Return the mole fraction of a single species.
void setState_TR(doublereal t, doublereal rho)
Set the internally stored temperature (K) and density (kg/m^3)
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.
virtual double density() const
Density (kg/m^3).
virtual void compositionChanged()
Apply changes to the state which are needed after the composition changes.
void checkSpeciesArraySize(size_t kk) const
Check that an array size is at least nSpecies().
doublereal m_dens
Density (kg m-3).
doublereal elementalMoleFraction(const size_t m) const
Elemental mole fraction of element m.
doublereal temperature() const
Temperature (K).
size_t nElements() const
Number of elements.
void setMolecularWeight(const int k, const double mw)
Set the molecular weight of a single species to a given value.
XML_Node * m_xml
XML node containing the XML info for this phase.
virtual void setMassFractions(const double *const y)
Set the mass fractions to the specified values and normalize them.
void restoreState(const vector_fp &state)
Restore a state saved on a previous call to saveState.
const std::vector< std::string > & speciesNames() const
Return a const reference to the vector of species names.
std::string m_name
Name of the phase.
size_t speciesIndex(const std::string &name) const
Returns the index of a species named 'name' within the Phase object.
virtual void setTemperature(const doublereal temp)
Set the internally stored temperature of the phase (K).
void setState_TY(doublereal t, doublereal *y)
Set the internally stored temperature (K) and mass fractions.
virtual bool ready() const
Returns a bool indicating whether the object is ready for use.
double molarVolume() const
Molar volume (m^3/kmol).
virtual void invalidateCache()
Invalidate any cached values which are normally updated only when a change in state is detected.
void checkElementIndex(size_t m) const
Check that the specified element index is in range.
void setMassFractionsByName(const compositionMap &yMap)
Set the species mass fractions by name.
void getMassFractions(double *const y) const
Get the species mass fractions.
void getAtoms(size_t k, double *atomArray) const
Get a vector containing the atomic composition of species k.
int m_stateNum
State Change variable.
void throwUndefinedElements()
Set the behavior when adding a species containing undefined elements to throw an exception.
size_t m_mm
Number of elements.
virtual double pressure() const
Return the thermodynamic pressure (Pa).
std::string elementName(size_t m) const
Name of the element with index m.
shared_ptr< Species > species(const std::string &name) const
Return the Species object for the named species.
void massFractionsToMoleFractions(const double *Y, double *X) const
Converts a mixture composition from mole fractions to mass fractions.
compositionMap getMoleFractionsByName(double threshold=0.0) const
Get the mole fractions by name.
virtual void setMoleFractions_NoNorm(const double *const x)
Set the mole fractions to the specified values without normalizing.
doublereal chargeDensity() const
Charge density [C/m^3].
doublereal sum_xlogx() const
Evaluate .
void clear()
Clear all cached values.
Class XML_Node is a tree-based representation of the contents of an XML file.
XML_Node * findByAttr(const std::string &attr, const std::string &val, int depth=100000) const
This routine carries out a recursive search for an XML node based on an attribute of each XML node.
void copy(XML_Node *const node_dest) const
Copy all of the information in the current XML_Node tree into the destination XML_Node tree,...
bool hasChild(const std::string &ch) const
Tests whether the current node has a child node with a particular name.
std::string id() const
Return the id attribute, if present.
XML_Node & root() const
Return the root of the current XML_Node tree.
XML_Node & child(const size_t n) const
Return a changeable reference to the n'th child of the current node.
CTML ("Cantera Markup Language") is the variant of XML that Cantera uses to store data.
const size_t npos
index returned by functions to indicate "no position"
const double Tiny
Small number to compare differences of mole fractions against.
const double Faraday
Faraday constant [C/kmol].
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.
std::map< std::string, double > compositionMap
Map connecting a string name with a double.
Namespace for the Cantera kernel.
doublereal dot(InputIter x_begin, InputIter x_end, InputIter2 y_begin)
Function that calculates a templated inner product.
XML_Node * findXMLPhase(XML_Node *root, const std::string &phaseId)
Search an XML_Node tree for a named phase XML_Node.
double getElementWeight(const std::string &ename)
Get the atomic weight of an element.
std::string toLowerCopy(const std::string &input)
Convert to lower case.
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
std::string parseSpeciesName(const std::string &nameStr, std::string &phaseName)
Parse a name string, separating out the phase name from the species name.
doublereal fpValueCheck(const std::string &val)
Translate a string into one doublereal value, with error checking.
compositionMap parseCompString(const std::string &ss, const std::vector< std::string > &names)
Parse a composition string into a map consisting of individual key:composition pairs.
Contains declarations for string manipulation functions within Cantera.
Various templated functions that carry out common vector operations (see Templated Utility Functions)...