24 m_undefinedElementBehavior(UndefElement::add),
25 m_caseSensitiveSpecies(false),
55 xroot->
copy(root_xml);
63 throw CanteraError(
"Phase::setXMLdata",
"XML 'phase' node not found");
66 throw CanteraError(
"Phase::setXMLdata",
"Root XML node not found");
89 throw IndexError(
"Phase::checkElementIndex",
"elements", m,
m_mm-1);
108 for (
size_t i = 0; i <
m_mm; i++) {
163 for (
size_t m = 0; m <
m_mm; m++) {
179 "Lowercase species name '{}' is not unique. "
180 "Set Phase::caseSensitiveSpecies to true to "
181 "enforce case sensitive species names", nLower);
214 throw IndexError(
"Phase::checkSpeciesIndex",
"species", k,
m_kk-1);
234 return { {
"T", 0}, {
"D", 1} };
236 return { {
"T", 0}, {
"P", 1} };
240 return { {
"T", 0}, {
"D", 1}, {
"Y", 2} };
242 return { {
"T", 0}, {
"P", 1}, {
"Y", 2} };
251 return {
"TD",
"TP",
"UV",
"DP",
"HP",
"SP",
"SV"};
253 return {
"TP",
"HP",
"SP"};
257 return {
"TDX",
"TDY",
"TPX",
"TPY",
"UVX",
"UVY",
"DPX",
"DPY",
258 "HPX",
"HPY",
"SPX",
"SPY",
"SVX",
"SVY"};
260 return {
"TPX",
"TPY",
"HPX",
"HPY",
"SPX",
"SPY"};
271 return {
"TD",
"TP",
"UV",
"DP",
"HP",
"SP",
"SV"};
273 return {
"TP",
"HP",
"SP"};
299 state[native.at(
"D")] =
density();
303 if (native.count(
"X")) {
305 }
else if (native.count(
"Y")) {
331 if (native.count(
"X")) {
333 }
else if (native.count(
"Y")) {
345 for (
size_t k = 0; k <
m_kk; k++) {
346 double xk = std::max(x[k], 0.0);
355 const double invSum = 1.0/sum;
356 for (
size_t k=0; k <
m_kk; k++) {
362 for (
size_t k=0; k <
m_kk; k++) {
376 m_y.begin(), multiplies<double>());
393 for (
size_t k = 0; k <
m_kk; k++) {
394 m_y[k] = std::max(y[k], 0.0);
396 double norm = accumulate(
m_y.begin(),
m_y.end(), 0.0);
400 m_ym.begin(), multiplies<double>());
408 copy(y, y +
m_kk,
m_y.begin());
410 multiplies<double>());
411 sum = accumulate(
m_ym.begin(),
m_ym.end(), 0.0);
506 copy(mw.begin(), mw.end(), weights);
522 for (
size_t k = 0; k <
m_kk; k++) {
534 for (
size_t k = 0; k <
m_kk; k++) {
587 copy(
m_y.begin(),
m_y.end(), y);
606 double sum = 0.0, norm = 0.0;
607 for (
size_t k = 0; k !=
m_kk; ++k) {
608 double ck = std::max(conc[k], 0.0);
615 double rsum = 1.0/sum;
616 for (
size_t k = 0; k !=
m_kk; ++k) {
627 double sum = 0.0, norm = 0.0;
628 for (
size_t k = 0; k !=
m_kk; ++k) {
634 double rsum = 1.0/sum;
635 for (
size_t k = 0; k !=
m_kk; ++k) {
636 m_ym[k] = conc[k] * rsum;
645 doublereal Z_m = 0.0;
646 for (
size_t k = 0; k !=
m_kk; ++k) {
657 for (
size_t k = 0; k <
m_kk; k++) {
659 for (
size_t j = 0; j <
nElements(); j++) {
664 doublereal numerator = 0.0;
665 for (
size_t k = 0; k !=
m_kk; ++k) {
668 return numerator / denom;
690 if (density_ > 0.0) {
694 "density must be positive. density = {}", density_);
700 if (density_ > 0.0) {
704 "density must be positive. density = {}", density_);
710 doublereal cdens = 0.0;
711 for (
size_t k = 0; k <
m_kk; k++) {
719 return m_mmw*std::inner_product(
m_ym.begin(),
m_ym.end(), Q, 0.0);
724 return m_mmw*std::inner_product(
m_ym.begin(),
m_ym.end(), Q.begin(), 0.0);
730 for (
size_t k = 0; k <
m_kk; k++) {
737 int atomic_number, doublereal entropy298,
747 }
else if (weight == -12345.0) {
755 "element-standard-entropies.yaml");
756 const AnyMap& elem = db[
"elements"].getMapWhere(
"symbol", symbol);
768 "Duplicate elements ({}) have different weights", symbol);
791 for (
size_t k = 0; k <
m_kk; k++) {
792 size_t m_old =
m_mm - 1;
793 for (
size_t m = 0; m < m_old; m++) {
804 if (m_species.find(spec->name) != m_species.end()) {
806 "Phase '{}' already contains a species named '{}'.",
810 for (
const auto& elem : spec->composition) {
814 case UndefElement::ignore:
817 case UndefElement::add:
823 case UndefElement::error:
826 "Species '{}' contains an undefined element '{}'.",
827 spec->name, elem.first);
830 comp[m] = elem.second;
834 m_species[spec->name] = spec;
848 if (spec->charge != 0.0) {
850 if (eindex !=
npos) {
851 doublereal ecomp = comp[eindex];
852 if (fabs(spec->charge + ecomp) > 0.001) {
855 "Input charge and element E compositions differ "
856 "for species " + spec->name);
860 comp[eindex] = -spec->charge;
868 comp[ne - 1] = - spec->charge;
871 for (
size_t m = 0; m < ne; m++) {
873 wt += comp[m] * aw[m];
879 wt = std::max(wt,
Tiny);
903 "New species name '{}' does not match existing name '{}'",
906 const shared_ptr<Species>& old = m_species[spec->name];
907 if (spec->composition != old->composition) {
909 "New composition for '{}' does not match existing composition",
912 m_species[spec->name] = spec;
920 "Invalid alias '{}': species already exists", alias);
927 "Unable to add alias '{}' "
928 "(original species '{}' not found).", alias,
name);
934 vector<std::string> isomerNames;
936 for (
const auto& k : m_species) {
937 if (k.second->composition == compMap) {
938 isomerNames.emplace_back(k.first);
957 "Unknown species '{}'",
name);
995 multiplies<double>());
1005 "This function has no effect. To be removed after Cantera 2.6.");
1011 for (
const auto& sp : comp) {
1015 "Unknown species '{}'", sp.first);
1025 for (
size_t k = 0; k !=
m_kk; ++k) {
1029 throw CanteraError(
"Phase::massFractionsToMoleFractions",
1030 "no input composition given");
1032 for (
size_t k = 0; k !=
m_kk; ++k) {
1041 throw CanteraError(
"Phase::moleFractionsToMassFractions",
1042 "no input composition given");
1044 double rmmw = 1.0/mmw;
1045 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.
Declaration for class Cantera::Species.
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.
double convert(const std::string &key, const std::string &units) const
Convert the item stored by the given key to the units specified in units.
static AnyMap fromYamlFile(const std::string &name, const std::string &parent_name="")
Create an AnyMap from a YAML file.
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.
Phase()
Default constructor.
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.
virtual void setRoot(std::shared_ptr< Solution > root)
Set root Solution holding all phase information.
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.
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.
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 (that is, a user-defined alternative species name).
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.
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.
const double * massFractions() const
Return a const pointer to the mass fraction array.
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).
virtual void setTemperature(double temp)
Set the internally stored temperature of the phase (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.
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.
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,...
std::string id() const
Return the id attribute, if present.
XML_Node & root() const
Return the root of the current XML_Node tree.
CTML ("Cantera Markup Language") is the variant of XML that Cantera uses to store data.
Namespace for the Cantera kernel.
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].
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.
void warn_deprecated(const std::string &source, const AnyBase &node, const std::string &message)
A deprecation warning for syntax in an input file.
const double SmallNumber
smallest number to compare to zero.
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::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.
compositionMap parseCompString(const std::string &ss, const std::vector< std::string > &names=std::vector< std::string >())
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)...