38 throw IndexError(
"Phase::checkElementIndex",
"elements", m,
m_mm-1);
57 for (
size_t i = 0; i <
m_mm; i++) {
113 "Use 'nAtoms(species_index, element_index)' instead.");
114 for (
size_t m = 0; m <
m_mm; m++) {
130 "Lowercase species name '{}' is not unique. "
131 "Set Phase::caseSensitiveSpecies to true to "
132 "enforce case sensitive species names", nLower);
165 throw IndexError(
"Phase::checkSpeciesIndex",
"species", k,
m_kk-1);
178 warn_deprecated(
"Phase::speciesSPName",
"To be removed after Cantera 3.0");
186 return { {
"T", 0}, {
"D", 1} };
188 return { {
"T", 0}, {
"P", 1} };
192 return { {
"T", 0}, {
"D", 1}, {
"Y", 2} };
194 return { {
"T", 0}, {
"P", 1}, {
"Y", 2} };
201 map<size_t, string> states;
203 states[value] =
name;
206 for (
auto& [value,
name] : states) {
216 return {
"TD",
"TP",
"UV",
"DP",
"HP",
"SP",
"SV"};
218 return {
"TP",
"HP",
"SP"};
222 return {
"TDX",
"TDY",
"TPX",
"TPY",
"UVX",
"UVY",
"DPX",
"DPY",
223 "HPX",
"HPY",
"SPX",
"SPY",
"SVX",
"SVY"};
225 return {
"TPX",
"TPY",
"HPX",
"HPY",
"SPX",
"SPY"};
236 return {
"TD",
"TP",
"UV",
"DP",
"HP",
"SP",
"SV"};
238 return {
"TP",
"HP",
"SP"};
264 state[native.at(
"D")] =
density();
268 if (native.count(
"X")) {
270 }
else if (native.count(
"Y")) {
296 if (native.count(
"X")) {
298 }
else if (native.count(
"Y")) {
310 for (
size_t k = 0; k <
m_kk; k++) {
311 double xk = std::max(x[k], 0.0);
320 const double invSum = 1.0/sum;
321 for (
size_t k=0; k <
m_kk; k++) {
327 for (
size_t k=0; k <
m_kk; k++) {
341 m_y.begin(), multiplies<double>());
358 for (
size_t k = 0; k <
m_kk; k++) {
359 m_y[k] = std::max(y[k], 0.0);
361 double norm = accumulate(
m_y.begin(),
m_y.end(), 0.0);
365 m_ym.begin(), multiplies<double>());
373 copy(y, y +
m_kk,
m_y.begin());
375 multiplies<double>());
376 sum = accumulate(
m_ym.begin(),
m_ym.end(), 0.0);
395 "To be removed after Cantera 3.0. Replaceable by calls to "
396 "setMoleFractions and setState_TD.");
403 warn_deprecated(
"Phase::setState_TNX",
"To be removed after Cantera 3.0. "
404 "Use 'setMoleFractions' and 'setState_TD' instead.");
413 "To be removed after Cantera 3.0. Replaceable by calls to "
414 "setMoleFractionsByName and setState_TD.");
422 "To be removed after Cantera 3.0. Replaceable by calls to "
423 "setMassFractions and setState_TD.");
431 "To be removed after Cantera 3.0. Replaceable by calls to "
432 "setMassFractionsByName and setState_TD.");
440 "To be removed after Cantera 3.0. Renamed to setState_TD.");
452 warn_deprecated(
"Phase::setState_TX",
"To be removed after Cantera 3.0. "
453 "Use calls to 'setTemperature' and 'setMoleFractions' instead.");
460 warn_deprecated(
"Phase::setState_TY",
"To be removed after Cantera 3.0. "
461 "Use calls to 'setTemperature' and 'setMassFractions' instead.");
468 warn_deprecated(
"Phase::setState_RX",
"To be removed after Cantera 3.0. "
469 "Use calls to 'setDensity' and 'setMoleFractions' instead.");
476 warn_deprecated(
"Phase::setState_RY",
"To be removed after Cantera 3.0. "
477 "Use calls to 'setDensity' and 'setMassFractions' instead.");
490 warn_deprecated(
"Phase::getMolecularWeights(vector<double>&)",
"To be removed after "
491 "Cantera 3.0. Use 'getMolecularWeights(vec.data())' instead.");
498 copy(mw.begin(), mw.end(), weights);
519 for (
size_t k = 0; k <
m_kk; k++) {
531 for (
size_t k = 0; k <
m_kk; k++) {
563 warn_deprecated(
"Phase::moleFractdivMMW",
"To be removed after Cantera 3.0. "
564 "Generally replaceable by 'getMoleFractions' and 'meanMolecularWeight'.");
586 copy(
m_y.begin(),
m_y.end(), y);
605 double sum = 0.0, norm = 0.0;
606 for (
size_t k = 0; k !=
m_kk; ++k) {
607 double ck = std::max(conc[k], 0.0);
614 double rsum = 1.0/sum;
615 for (
size_t k = 0; k !=
m_kk; ++k) {
626 double sum = 0.0, norm = 0.0;
627 for (
size_t k = 0; k !=
m_kk; ++k) {
633 double rsum = 1.0/sum;
634 for (
size_t k = 0; k !=
m_kk; ++k) {
635 m_ym[k] = conc[k] * rsum;
645 double totalMoles = accumulate(
m_ym.begin(),
m_ym.end(), 0.0);
647 copy(N, N +
m_kk,
m_y.begin());
648 transform(
m_y.begin(),
m_y.end(),
m_molwts.begin(),
m_y.begin(), multiplies<double>());
649 double totalMass = accumulate(
m_y.begin(),
m_y.end(), 0.0);
651 m_mmw = totalMass/totalMoles;
664 for (
size_t k = 0; k !=
m_kk; ++k) {
675 for (
size_t k = 0; k <
m_kk; k++) {
677 for (
size_t j = 0; j <
nElements(); j++) {
682 double numerator = 0.0;
683 for (
size_t k = 0; k !=
m_kk; ++k) {
686 return numerator / denom;
696 warn_deprecated(
"Phase::setMolarDensity",
"To be removed after Cantera 3.0. "
697 "Use 'setDensity(molar_density * meanMolecularWeight())' instead.");
710 if (density_ > 0.0) {
714 "density must be positive. density = {}", density_);
720 if (density_ > 0.0) {
724 "density must be positive. density = {}", density_);
731 for (
size_t k = 0; k <
m_kk; k++) {
739 return m_mmw*std::inner_product(
m_ym.begin(),
m_ym.end(), Q, 0.0);
744 return m_mmw*std::inner_product(
m_ym.begin(),
m_ym.end(), Q.begin(), 0.0);
750 for (
size_t k = 0; k <
m_kk; k++) {
757 double entropy298,
int elem_type)
766 }
else if (weight == -12345.0) {
774 "element-standard-entropies.yaml");
775 const AnyMap& elem = db[
"elements"].getMapWhere(
"symbol", symbol);
787 "Duplicate elements ({}) have different weights", symbol);
810 for (
size_t k = 0; k <
m_kk; k++) {
811 size_t m_old =
m_mm - 1;
812 for (
size_t m = 0; m < m_old; m++) {
823 if (m_species.find(spec->name) != m_species.end()) {
825 "Phase '{}' already contains a species named '{}'.",
830 for (
const auto& [eName, stoich] : spec->composition) {
834 case UndefElement::ignore:
837 case UndefElement::add:
843 case UndefElement::error:
846 "Species '{}' contains an undefined element '{}'.",
855 if (spec->charge != 0.0) {
857 if (eindex !=
npos) {
858 double ecomp = comp[eindex];
859 if (fabs(spec->charge + ecomp) > 0.001) {
862 "Input charge and element E compositions differ "
863 "for species " + spec->name);
867 comp[eindex] = -spec->charge;
875 comp[ne - 1] = - spec->charge;
880 for (
size_t m = 0; m < ne; m++) {
881 wt += comp[m] * aw[m];
887 wt = std::max(wt,
Tiny);
889 spec->setMolecularWeight(wt);
894 for (
size_t m = 0; m < ne; m++) {
899 m_species[spec->name] = spec;
930 "New species name '{}' does not match existing name '{}'",
933 const shared_ptr<Species>& old = m_species[spec->name];
934 if (spec->composition != old->composition) {
936 "New composition for '{}' does not match existing composition",
939 m_species[spec->name] = spec;
947 "Invalid alias '{}': species already exists", alias);
954 "Unable to add alias '{}' "
955 "(original species '{}' not found).", alias,
name);
961 vector<string> isomerNames;
964 if (
species->composition == compMap) {
965 isomerNames.emplace_back(
name);
984 "Unknown species '{}'",
name);
1022 multiplies<double>());
1032 vector<double> X(
m_kk);
1033 for (
const auto& [
name, value] : comp) {
1037 "Unknown species '{}'",
name);
1047 for (
size_t k = 0; k !=
m_kk; ++k) {
1051 throw CanteraError(
"Phase::massFractionsToMoleFractions",
1052 "no input composition given");
1054 for (
size_t k = 0; k !=
m_kk; ++k) {
1063 throw CanteraError(
"Phase::moleFractionsToMassFractions",
1064 "no input composition given");
1066 double rmmw = 1.0/mmw;
1067 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 string &key, const string &units) const
Convert the item stored by the given key to the units specified in units.
static AnyMap fromYamlFile(const string &name, const 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.
virtual vector< string > partialStates() const
Return a vector of settable partial property sets within a phase.
void getCharges(double *charges) const
Copy the vector of species charges into array charges.
virtual void getConcentrations(double *const c) const
Get the species concentrations (kmol/m^3).
map< string, size_t > m_speciesLower
Map of lower-case species names to indices.
double massFraction(size_t k) const
Return the mass fraction of a single species.
virtual 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.
virtual void setMoleFractions(const double *const x)
Set the mole fractions to the specified values.
int changeElementType(int m, int elem_type)
Change the element type of the mth constraint Reassigns an element type.
const vector< double > & atomicWeights() const
Return a read-only reference to the vector of atomic weights.
virtual vector< string > fullStates() const
Return a vector containing full states defining a phase.
void assertCompressible(const string &setter) const
Ensure that phase is compressible.
void checkSpeciesIndex(size_t k) const
Check that the specified species index is in range.
void restoreState(const vector< double > &state)
Restore a state saved on a previous call to saveState.
vector< double > m_speciesComp
Atomic composition of the species.
ValueCache m_cache
Cached for saved calculations within each ThermoPhase.
void getMolecularWeights(vector< double > &weights) const
Copy the vector of molecular weights into vector weights.
const double * moleFractdivMMW() const
Returns a const pointer to the start of the moleFraction/MW array.
vector< string > m_speciesNames
Vector of the species names.
void setState_RY(double rho, double *y)
Set the density (kg/m^3) and mass fractions.
size_t nSpecies() const
Returns the number of species in the phase.
bool m_caseSensitiveSpecies
Flag determining whether case sensitive species names are enforced.
vector< string > m_elementNames
element names
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.
virtual map< string, size_t > nativeState() const
Return a map of properties defining the native state of a substance.
double chargeDensity() const
Charge density [C/m^3].
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< int > m_atomicNumbers
element atomic numbers
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.
void setState_TRY(double t, double dens, const double *y)
Set the internally stored temperature (K), density, and mass fractions.
double m_mmw
mean molecular weight of the mixture (kg kmol-1)
double elementalMoleFraction(const size_t m) const
Elemental mole fraction of element m.
void setState_TR(double t, double rho)
Set the internally stored temperature (K) and density (kg/m^3)
void setState_TD(double t, double rho)
Set the internally stored temperature (K) and density (kg/m^3)
vector< double > m_rmolwts
inverse of species molecular weights (kmol kg-1)
double temperature() const
Temperature (K).
string speciesSPName(int k) const
Returns the expanded species name of a species, including the phase name This is guaranteed to be uni...
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.
double meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
void moleFractionsToMassFractions(const double *X, double *Y) const
Converts a mixture composition from mass fractions to mole fractions.
virtual void setConcentrations(const double *const conc)
Set the concentrations to the specified values within the phase.
void setState_RX(double rho, double *x)
Set the density (kg/m^3) and mole fractions.
virtual void setMolarDensity(const double molarDensity)
Set the internally stored molar density (kmol/m^3) of the phase.
void saveState(vector< double > &state) const
Save the current internal state of the phase.
void setState_TX(double t, double *x)
Set the internally stored temperature (K) and mole fractions.
Composition getMoleFractionsByName(double threshold=0.0) const
Get the mole fractions by name.
size_t elementIndex(const string &name) const
Return the index of element named 'name'.
virtual double concentration(const size_t k) const
Concentration of species k.
double atomicWeight(size_t m) const
Atomic weight of element m.
void checkElementArraySize(size_t mm) const
Check that an array size is at least nElements().
void setMassFractionsByName(const Composition &yMap)
Set the species mass fractions by name.
int elementType(size_t m) const
Return the element constraint type Possible types include:
string speciesName(size_t k) const
Name of the species with index k.
map< string, size_t > m_speciesIndices
Map of species names to indices.
virtual void setDensity(const double density_)
Set the internally stored density (kg/m^3) of the phase.
Composition getMassFractionsByName(double threshold=0.0) const
Get the mass fractions by name.
void setState_TRX(double t, double dens, const double *x)
Set the internally stored temperature (K), density, and mole fractions.
virtual size_t stateSize() const
Return size of vector defining internal state of the phase.
string nativeMode() const
Return string acronym representing the native state of a Phase.
vector< double > getCompositionFromMap(const Composition &comp) const
Converts a Composition to a vector with entries for each species Species that are not specified are s...
size_t findSpeciesLower(const string &nameStr) const
Find lowercase species name in m_speciesIndices when case sensitive species names are not enforced an...
vector< double > m_molwts
species molecular weights (kg kmol-1)
virtual vector< string > findIsomers(const Composition &compMap) const
Return a vector with isomers names matching a given composition map.
const vector< double > & inverseMolecularWeights() const
Return a const reference to the internal vector of molecular weights.
virtual bool isPure() const
Return whether phase represents a pure (single species) substance.
vector< double > m_y
Mass fractions of the species.
void getMoleFractions(double *const x) const
Get the species mole fraction vector.
void setMoleFractionsByName(const Composition &xMap)
Set the species mole fractions by name.
const double * massFractions() const
Return a const pointer to the mass fraction array.
vector< int > m_elem_type
Vector of element types.
void setState_TNX(double t, double n, const double *x)
Set the internally stored temperature (K), molar density (kmol/m^3), and mole fractions.
double sum_xlogx() const
Evaluate .
string m_name
Name of the phase.
const vector< double > & molecularWeights() const
Return a const reference to the internal vector of molecular weights.
size_t speciesIndex(const string &name) const
Returns the index of a species named 'name' within the Phase object.
double moleFraction(size_t k) const
Return the mole fraction of a single species.
double m_dens
Density (kg m-3).
const vector< 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.
vector< double > m_atomicWeights
element atomic weights (kg kmol-1)
void checkSpeciesArraySize(size_t kk) const
Check that an array size is at least nSpecies().
double nAtoms(size_t k, size_t m) const
Number of atoms of element m in species k.
void addSpeciesAlias(const string &name, const string &alias)
Add a species alias (that is, a user-defined alternative species name).
virtual void setMolesNoTruncate(const double *const N)
Set the state of the object with moles in [kmol].
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.
double mean_X(const double *const Q) const
Evaluate the mole-fraction-weighted mean of an array Q.
vector< double > m_entropy298
Entropy at 298.15 K and 1 bar of stable state pure elements (J kmol-1)
vector< double > m_ym
m_ym[k] = mole fraction of species k divided by the mean molecular weight of mixture.
virtual void setMassFractions(const double *const y)
Set the mass fractions to the specified values and normalize them.
const vector< string > & speciesNames() const
Return a const reference to the vector of species names.
virtual bool ready() const
Returns a bool indicating whether the object is ready for use.
double molecularWeight(size_t k) const
Molecular weight of species k.
double elementalMassFraction(const size_t m) const
Elemental mass fraction of element m.
shared_ptr< Species > species(const string &name) const
Return the Species object for the named species.
virtual 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 getMassFractions(double *const y) const
Get the species mass fractions.
void setState_TY(double t, double *y)
Set the internally stored temperature (K) and 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.
void setName(const string &nm)
Sets the string name for the phase.
size_t m_mm
Number of elements.
virtual double pressure() const
Return the thermodynamic pressure (Pa).
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...
void massFractionsToMoleFractions(const double *Y, double *X) const
Converts a mixture composition from mole fractions to mass fractions.
double entropyElement298(size_t m) const
Entropy of the element in its standard state at 298 K and 1 bar.
virtual void setMoleFractions_NoNorm(const double *const x)
Set the mole fractions to the specified values without normalizing.
vector< double > m_speciesCharge
Vector of species charges. length m_kk.
size_t addElement(const string &symbol, double weight=-12345.0, int atomicNumber=0, double entropy298=ENTROPY298_UNKNOWN, int elem_type=CT_ELEM_TYPE_ABSPOS)
Add an element.
string name() const
Return the name of the phase.
void clear()
Clear all cached values.
string toLowerCopy(const string &input)
Convert to lower case.
Composition parseCompString(const string &ss, const vector< string > &names)
Parse a composition string into a map consisting of individual key:composition pairs.
double dot(InputIter x_begin, InputIter x_end, InputIter2 y_begin)
Function that calculates a templated inner product.
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
const double Faraday
Faraday constant [C/kmol].
Namespace for the Cantera kernel.
const size_t npos
index returned by functions to indicate "no position"
double getElementWeight(const string &ename)
Get the atomic weight of an element.
const double Tiny
Small number to compare differences of mole fractions against.
const double SmallNumber
smallest number to compare to zero.
void warn_deprecated(const string &source, const AnyBase &node, const string &message)
A deprecation warning for syntax in an input file.
map< string, double > Composition
Map from string names to doubles.
Contains declarations for string manipulation functions within Cantera.
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...