53 for (
size_t i = 0; i <
m_mm; i++) {
119 "Lowercase species name '{}' is not unique. "
120 "Set Phase::caseSensitiveSpecies to true to "
121 "enforce case sensitive species names", nLower);
136 if (loc==
npos && raise) {
168 return { {
"T", 0}, {
"D", 1} };
170 return { {
"T", 0}, {
"P", 1} };
174 return { {
"T", 0}, {
"D", 1}, {
"Y", 2} };
176 return { {
"T", 0}, {
"P", 1}, {
"Y", 2} };
183 map<size_t, string> states;
185 states[value] =
name;
188 for (
auto& [value,
name] : states) {
198 return {
"TD",
"TP",
"UV",
"DP",
"HP",
"SP",
"SV"};
200 return {
"TP",
"HP",
"SP"};
204 return {
"TDX",
"TDY",
"TPX",
"TPY",
"UVX",
"UVY",
"DPX",
"DPY",
205 "HPX",
"HPY",
"SPX",
"SPY",
"SVX",
"SVY"};
207 return {
"TPX",
"TPY",
"HPX",
"HPY",
"SPX",
"SPY"};
218 return {
"TD",
"TP",
"UV",
"DP",
"HP",
"SP",
"SV"};
220 return {
"TP",
"HP",
"SP"};
231 state[native.at(
"D")] =
density();
262 if (native.count(
"X")) {
264 }
else if (native.count(
"Y")) {
275 if (native.count(
"X")) {
277 }
else if (native.count(
"Y")) {
290 for (
size_t k = 0; k <
m_kk; k++) {
291 double xk = std::max(x[k], 0.0);
300 const double invSum = 1.0/sum;
301 for (
size_t k=0; k <
m_kk; k++) {
307 for (
size_t k=0; k <
m_kk; k++) {
322 m_y.begin(), multiplies<double>());
340 for (
size_t k = 0; k <
m_kk; k++) {
341 m_y[k] = std::max(y[k], 0.0);
343 double norm = accumulate(
m_y.begin(),
m_y.end(), 0.0);
347 m_ym.begin(), multiplies<double>());
356 copy(y.begin(), y.begin() +
m_kk,
m_y.begin());
358 multiplies<double>());
359 sum = accumulate(
m_ym.begin(),
m_ym.end(), 0.0);
382 }
catch (std::exception&) {
397 checkArraySize(
"Phase::getMolecularWeights", weights.size(), mw.size());
398 copy(mw.begin(), mw.end(), weights.begin());
420 for (
size_t k = 0; k <
m_kk; k++) {
432 for (
size_t k = 0; k <
m_kk; k++) {
482 copy(
m_y.begin(),
m_y.end(), y.begin());
503 double sum = 0.0, norm = 0.0;
504 for (
size_t k = 0; k !=
m_kk; ++k) {
505 double ck = std::max(conc[k], 0.0);
512 double rsum = 1.0/sum;
513 for (
size_t k = 0; k !=
m_kk; ++k) {
525 double sum = 0.0, norm = 0.0;
526 for (
size_t k = 0; k !=
m_kk; ++k) {
532 double rsum = 1.0/sum;
533 for (
size_t k = 0; k !=
m_kk; ++k) {
534 m_ym[k] = conc[k] * rsum;
544 copy(N.begin(), N.begin() +
m_kk,
m_ym.begin());
545 double totalMoles = accumulate(
m_ym.begin(),
m_ym.end(), 0.0);
547 copy(N.begin(), N.begin() +
m_kk,
m_y.begin());
548 transform(
m_y.begin(),
m_y.end(),
m_molwts.begin(),
m_y.begin(), multiplies<double>());
549 double totalMass = accumulate(
m_y.begin(),
m_y.end(), 0.0);
551 m_mmw = totalMass/totalMoles;
563 for (
size_t k = 0; k !=
m_kk; ++k) {
573 for (
size_t k = 0; k <
m_kk; k++) {
575 for (
size_t j = 0; j <
nElements(); j++) {
580 double numerator = 0.0;
581 for (
size_t k = 0; k !=
m_kk; ++k) {
584 return numerator / denom;
600 if (density_ > 0.0) {
604 "density must be positive. density = {}", density_);
610 if (density_ > 0.0) {
614 "density must be positive. density = {}", density_);
621 for (
size_t k = 0; k <
m_kk; k++) {
630 return m_mmw*std::inner_product(
m_ym.begin(),
m_ym.end(), Q.begin(), 0.0);
636 for (
size_t k = 0; k <
m_kk; k++) {
643 double entropy298,
int elem_type)
652 }
else if (weight == -12345.0) {
660 "element-standard-entropies.yaml");
661 const AnyMap& elem = db[
"elements"].getMapWhere(
"symbol", symbol);
673 "Duplicate elements ({}) have different weights", symbol);
696 for (
size_t k = 0; k <
m_kk; k++) {
697 size_t m_old =
m_mm - 1;
698 for (
size_t m = 0; m < m_old; m++) {
712 "Cannot add species to ThermoPhase '{}' because it is being "
713 "used by another object,\nsuch as a Reactor, Domain1D (flame), "
714 "SolutionArray, or MultiPhase (Mixture) object.",
m_name);
719 "Phase '{}' already contains a species named '{}'.",
724 for (
const auto& [eName, stoich] : spec->composition) {
728 case UndefElement::ignore:
731 case UndefElement::add:
737 case UndefElement::error:
740 "Species '{}' contains an undefined element '{}'.",
749 if (spec->charge != 0.0) {
751 if (eindex !=
npos) {
752 double ecomp = comp[eindex];
753 if (fabs(spec->charge + ecomp) > 0.001) {
755 throw CanteraError(
"Phase::addSpecies",
756 "Input charge and element E compositions differ "
757 "for species " + spec->name);
761 comp[eindex] = -spec->charge;
769 comp[ne - 1] = - spec->charge;
774 for (
size_t m = 0; m < ne; m++) {
775 wt += comp[m] * aw[m];
781 wt = std::max(wt,
Tiny);
783 spec->setMolecularWeight(wt);
788 for (
size_t m = 0; m < ne; m++) {
825 "New species name '{}' does not match existing name '{}'",
828 const shared_ptr<Species>& old =
m_species[spec->name];
829 if (spec->composition != old->composition) {
831 "New composition for '{}' does not match existing composition",
842 "Invalid alias '{}': species already exists", alias);
849 "Unable to add alias '{}' "
850 "(original species '{}' not found).", alias,
name);
858 "ThermoPhase '{}' has no current species locks.",
m_name);
865 vector<string> isomerNames;
868 if (
species->composition == compMap) {
869 isomerNames.emplace_back(
name);
921 multiplies<double>());
931 vector<double> X(
m_kk);
932 for (
const auto& [
name, value] : comp) {
944 for (
size_t k = 0; k !=
m_kk; ++k) {
948 throw CanteraError(
"Phase::massFractionsToMoleFractions",
949 "no input composition given");
951 for (
size_t k = 0; k !=
m_kk; ++k) {
962 throw CanteraError(
"Phase::moleFractionsToMassFractions",
963 "no input composition given");
965 double rmmw = 1.0/mmw;
966 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.
void getMoleFractions(span< double > x) const
Get the species mole fraction vector.
void getMassFractions(span< double > y) const
Get the species mass fractions.
span< const double > inverseMolecularWeights() const
Return a const reference to the internal vector of molecular weights.
virtual vector< string > partialStates() const
Return a vector of settable partial property sets within a phase.
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.
int changeElementType(int m, int elem_type)
Change the element type of the mth constraint Reassigns an element type.
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.
vector< double > m_workS
Vector of size m_kk, used as a temporary holding area.
vector< double > m_speciesComp
Atomic composition of the species.
ValueCache m_cache
Cached for saved calculations within each ThermoPhase.
size_t m_nSpeciesLocks
Reference counter preventing species addition.
vector< string > m_speciesNames
Vector of the species names.
size_t nSpecies() const
Returns the number of species in the phase.
size_t checkElementIndex(size_t m) const
Check that the specified element index is in range.
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 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.
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.
virtual void setMolesNoTruncate(span< const double > N)
Set the state of the object with moles in [kmol].
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.
size_t speciesIndex(const string &name, bool raise=true) const
Returns the index of a species named 'name' within the Phase object.
void massFractionsToMoleFractions(span< const double > Y, span< double > X) const
Converts a mixture composition from mole fractions to mass fractions.
span< const double > molecularWeights() const
Return a const reference to the internal vector of molecular weights.
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).
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)
virtual void restorePartialState(span< const double > state)
Set the internal thermodynamic state of the phase, excluding composition.
span< const double > massFractions() const
Return a view of the mass fraction array.
void removeSpeciesLock()
Decrement species lock counter.
Composition getMoleFractionsByName(double threshold=0.0) const
Get the mole fractions by 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 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:
virtual void setConcentrationsNoNorm(span< const double > conc)
Set the concentrations without ignoring negative concentrations.
string speciesName(size_t k) const
Name of the species with index k.
void getMolecularWeights(span< double > weights) const
Copy the vector of molecular weights into array weights.
void moleFractionsToMassFractions(span< const double > X, span< double > Y) const
Converts a mixture composition from mass fractions to mole fractions.
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.
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...
map< string, shared_ptr< Species > > m_species
Map of Species objects.
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.
virtual bool isPure() const
Return whether phase represents a pure (single species) substance.
vector< double > m_y
Mass fractions of the species.
void setMoleFractionsByName(const Composition &xMap)
Set the species mole fractions by name.
vector< int > m_elem_type
Vector of element types.
double sum_xlogx() const
Evaluate .
double mean_X(span< const double > Q) const
Evaluate the mole-fraction-weighted mean of an array Q.
string m_name
Name of the phase.
double moleFraction(size_t k) const
Return the mole fraction of a single species.
double m_dens
Density (kg m-3).
virtual void setConcentrations(span< const double > conc)
Set the concentrations to the specified values within the phase.
const vector< string > & elementNames() const
Return a read-only reference to the vector of element names.
virtual void getConcentrations(span< double > c) const
Get the species concentrations (kmol/m^3).
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)
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).
span< const double > atomicWeights() const
Return a read-only reference to the vector of atomic weights.
size_t checkSpeciesIndex(size_t k) const
Check that the specified species index is in range.
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.
vector< double > m_entropy298
Entropy at 298.15 K and 1 bar of stable state pure elements (J kmol-1)
void getCharges(span< double > charges) const
Copy the vector of species charges into array charges.
virtual void setMoleFractions_NoNorm(span< const double > x)
Set the mole fractions to the specified values without normalizing.
vector< double > m_ym
m_ym[k] = mole fraction of species k divided by the mean molecular weight of mixture.
virtual void setMoleFractions(span< const double > x)
Set the mole fractions to the specified values.
const vector< string > & speciesNames() const
Return a const reference to the vector of species names.
size_t elementIndex(const string &name, bool raise=true) const
Return the index of element named 'name'.
virtual void setMassFractions(span< const double > y)
Set the mass fractions to the specified values and normalize them.
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.
virtual void setMassFractions_NoNorm(span< const double > y)
Set the mass fractions to the specified values without normalizing.
virtual size_t partialStateSize() const
Get the size of the partial state vector of the phase.
virtual void savePartialState(span< double > state) const
Save the current thermodynamic state of the phase, excluding composition.
virtual void restoreState(span< const double > state)
Restore the state of the phase from a previously saved state vector.
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...
double entropyElement298(size_t m) const
Entropy of the element in its standard state at 298 K and 1 bar.
vector< double > m_speciesCharge
Vector of species charges.
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.
virtual void saveState(span< double > state) const
Write to array 'state' the current internal state.
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.
map< string, double > Composition
Map from string names to doubles.
void checkArraySize(const char *procedure, size_t available, size_t required)
Wrapper for throwing ArraySizeError.
Contains declarations for string manipulation functions within Cantera.
Various templated functions that carry out common vector and polynomial operations (see Templated Arr...