Cantera  2.4.0
Phase.cpp
Go to the documentation of this file.
1 /**
2  * @file Phase.cpp
3  * Definition file for class Phase.
4  */
5 
6 // This file is part of Cantera. See License.txt in the top-level directory or
7 // at http://www.cantera.org/license.txt for license and copyright information.
8 
9 #include "cantera/thermo/Phase.h"
10 #include "cantera/base/utilities.h"
12 #include "cantera/base/ctml.h"
14 
15 using namespace std;
16 
17 namespace Cantera
18 {
19 
20 Phase::Phase() :
21  m_kk(0),
22  m_ndim(3),
23  m_undefinedElementBehavior(UndefElement::add),
24  m_xml(new XML_Node("phase")),
25  m_id("<phase>"),
26  m_temp(0.001),
27  m_dens(0.001),
28  m_mmw(0.0),
29  m_stateNum(-1),
30  m_mm(0),
31  m_elem_type(0)
32 {
33 }
34 
35 Phase::~Phase()
36 {
37  if (m_xml) {
38  XML_Node* xroot = &m_xml->root();
39  delete xroot;
40  }
41  m_xml = 0;
42 }
43 
45 {
46  return *m_xml;
47 }
48 
49 void Phase::setXMLdata(XML_Node& xmlPhase)
50 {
51  XML_Node* xroot = &xmlPhase.root();
52  XML_Node *root_xml = new XML_Node();
53  xroot->copy(root_xml);
54  if (m_xml) {
55  XML_Node *rOld = &m_xml->root();
56  delete rOld;
57  m_xml = 0;
58  }
59  m_xml = findXMLPhase(root_xml, xmlPhase.id());
60  if (!m_xml) {
61  throw CanteraError("Phase::setXMLdata()", "XML 'phase' node not found");
62  }
63  if (&m_xml->root() != root_xml) {
64  throw CanteraError("Phase::setXMLdata()", "Root XML node not found");
65  }
66 }
67 
68 std::string Phase::id() const
69 {
70  return m_id;
71 }
72 
73 void Phase::setID(const std::string& id_)
74 {
75  m_id = id_;
76 }
77 
78 std::string Phase::name() const
79 {
80  return m_name;
81 }
82 
83 void Phase::setName(const std::string& nm)
84 {
85  m_name = nm;
86 }
87 
88 size_t Phase::nElements() const
89 {
90  return m_mm;
91 }
92 
93 void Phase::checkElementIndex(size_t m) const
94 {
95  if (m >= m_mm) {
96  throw IndexError("checkElementIndex", "elements", m, m_mm-1);
97  }
98 }
99 
100 void Phase::checkElementArraySize(size_t mm) const
101 {
102  if (m_mm > mm) {
103  throw ArraySizeError("checkElementArraySize", mm, m_mm);
104  }
105 }
106 
107 string Phase::elementName(size_t m) const
108 {
110  return m_elementNames[m];
111 }
112 
113 size_t Phase::elementIndex(const std::string& elementName) const
114 {
115  for (size_t i = 0; i < m_mm; i++) {
116  if (m_elementNames[i] == elementName) {
117  return i;
118  }
119  }
120  return npos;
121 }
122 
123 const vector<string>& Phase::elementNames() const
124 {
125  return m_elementNames;
126 }
127 
128 doublereal Phase::atomicWeight(size_t m) const
129 {
130  return m_atomicWeights[m];
131 }
132 
133 doublereal Phase::entropyElement298(size_t m) const
134 {
136  return m_entropy298[m];
137 }
138 
140 {
141  return m_atomicWeights;
142 }
143 
144 int Phase::atomicNumber(size_t m) const
145 {
146  return m_atomicNumbers[m];
147 }
148 
149 int Phase::elementType(size_t m) const
150 {
151  return m_elem_type[m];
152 }
153 
154 int Phase::changeElementType(int m, int elem_type)
155 {
156  int old = m_elem_type[m];
157  m_elem_type[m] = elem_type;
158  return old;
159 }
160 
161 doublereal Phase::nAtoms(size_t k, size_t m) const
162 {
165  return m_speciesComp[m_mm * k + m];
166 }
167 
168 void Phase::getAtoms(size_t k, double* atomArray) const
169 {
170  for (size_t m = 0; m < m_mm; m++) {
171  atomArray[m] = (double) m_speciesComp[m_mm * k + m];
172  }
173 }
174 
175 size_t Phase::speciesIndex(const std::string& nameStr) const
176 {
177  size_t loc = getValue(m_speciesIndices, toLowerCopy(nameStr), npos);
178  if (loc == npos && nameStr.find(':') != npos) {
179  std::string pn;
180  std::string sn = toLowerCopy(parseSpeciesName(nameStr, pn));
181  if (pn == "" || pn == m_name || pn == m_id) {
182  return getValue(m_speciesIndices, sn, npos);
183  } else {
184  return npos;
185  }
186  } else {
187  return loc;
188  }
189 }
190 
191 string Phase::speciesName(size_t k) const
192 {
194  return m_speciesNames[k];
195 }
196 
197 const vector<string>& Phase::speciesNames() const
198 {
199  return m_speciesNames;
200 }
201 
202 void Phase::checkSpeciesIndex(size_t k) const
203 {
204  if (k >= m_kk) {
205  throw IndexError("checkSpeciesIndex", "species", k, m_kk-1);
206  }
207 }
208 
209 void Phase::checkSpeciesArraySize(size_t kk) const
210 {
211  if (m_kk > kk) {
212  throw ArraySizeError("checkSpeciesArraySize", kk, m_kk);
213  }
214 }
215 
216 std::string Phase::speciesSPName(int k) const
217 {
218  return m_name + ":" + speciesName(k);
219 }
220 
221 void Phase::saveState(vector_fp& state) const
222 {
223  state.resize(nSpecies() + 2);
224  saveState(state.size(), &state[0]);
225 }
226 void Phase::saveState(size_t lenstate, doublereal* state) const
227 {
228  state[0] = temperature();
229  state[1] = density();
230  getMassFractions(state + 2);
231 }
232 
233 void Phase::restoreState(const vector_fp& state)
234 {
235  restoreState(state.size(),&state[0]);
237 }
238 
239 void Phase::restoreState(size_t lenstate, const doublereal* state)
240 {
241  if (lenstate >= nSpecies() + 2) {
242  setMassFractions_NoNorm(state + 2);
243  setTemperature(state[0]);
244  setDensity(state[1]);
245  } else {
246  throw ArraySizeError("Phase::restoreState",
247  lenstate,nSpecies()+2);
248  }
249 }
250 
251 void Phase::setMoleFractions(const doublereal* const x)
252 {
253  // Use m_y as a temporary work vector for the non-negative mole fractions
254  doublereal norm = 0.0;
255  // sum is calculated below as the unnormalized molecular weight
256  doublereal sum = 0;
257  for (size_t k = 0; k < m_kk; k++) {
258  double xk = std::max(x[k], 0.0); // Ignore negative mole fractions
259  m_y[k] = xk;
260  norm += xk;
261  sum += m_molwts[k] * xk;
262  }
263 
264  // Set m_ym_ to the normalized mole fractions divided by the normalized mean
265  // molecular weight:
266  // m_ym_k = X_k / (sum_k X_k M_k)
267  const doublereal invSum = 1.0/sum;
268  for (size_t k=0; k < m_kk; k++) {
269  m_ym[k] = m_y[k]*invSum;
270  }
271 
272  // Now set m_y to the normalized mass fractions:
273  // m_y = X_k M_k / (sum_k X_k M_k)
274  for (size_t k=0; k < m_kk; k++) {
275  m_y[k] = m_ym[k] * m_molwts[k];
276  }
277 
278  // Calculate the normalized molecular weight
279  m_mmw = sum/norm;
281 }
282 
283 void Phase::setMoleFractions_NoNorm(const doublereal* const x)
284 {
285  m_mmw = dot(x, x + m_kk, m_molwts.begin());
286  transform(x, x + m_kk, m_ym.begin(), timesConstant<double>(1.0/m_mmw));
287  transform(m_ym.begin(), m_ym.begin() + m_kk, m_molwts.begin(),
288  m_y.begin(), multiplies<double>());
290 }
291 
293 {
294  vector_fp mf(m_kk, 0.0);
295  for (const auto& sp : xMap) {
296  try {
297  mf[m_speciesIndices.at(toLowerCopy(sp.first))] = sp.second;
298  } catch (std::out_of_range&) {
299  throw CanteraError("Phase::setMoleFractionsByName",
300  "Unknown species '{}'", sp.first);
301  }
302  }
303  setMoleFractions(&mf[0]);
304 }
305 
306 void Phase::setMoleFractionsByName(const std::string& x)
307 {
309 }
310 
311 void Phase::setMassFractions(const doublereal* const y)
312 {
313  for (size_t k = 0; k < m_kk; k++) {
314  m_y[k] = std::max(y[k], 0.0); // Ignore negative mass fractions
315  }
316  doublereal norm = accumulate(m_y.begin(), m_y.end(), 0.0);
317  scale(m_y.begin(), m_y.end(), m_y.begin(), 1.0/norm);
318 
319  transform(m_y.begin(), m_y.end(), m_rmolwts.begin(),
320  m_ym.begin(), multiplies<double>());
321  m_mmw = 1.0 / accumulate(m_ym.begin(), m_ym.end(), 0.0);
323 }
324 
325 void Phase::setMassFractions_NoNorm(const doublereal* const y)
326 {
327  doublereal sum = 0.0;
328  copy(y, y + m_kk, m_y.begin());
329  transform(m_y.begin(), m_y.end(), m_rmolwts.begin(), m_ym.begin(),
330  multiplies<double>());
331  sum = accumulate(m_ym.begin(), m_ym.end(), 0.0);
332  m_mmw = 1.0/sum;
334 }
335 
337 {
338  vector_fp mf(m_kk, 0.0);
339  for (const auto& sp : yMap) {
340  try {
341  mf[m_speciesIndices.at(toLowerCopy(sp.first))] = sp.second;
342  } catch (std::out_of_range&) {
343  throw CanteraError("Phase::setMassFractionsByName",
344  "Unknown species '{}'", sp.first);
345  }
346  }
347  setMassFractions(&mf[0]);
348 }
349 
350 void Phase::setMassFractionsByName(const std::string& y)
351 {
353 }
354 
355 void Phase::setState_TRX(doublereal t, doublereal dens, const doublereal* x)
356 {
357  setMoleFractions(x);
358  setTemperature(t);
359  setDensity(dens);
360 }
361 
362 void Phase::setState_TNX(doublereal t, doublereal n, const doublereal* x)
363 {
364  setMoleFractions(x);
365  setTemperature(t);
366  setMolarDensity(n);
367 }
368 
369 void Phase::setState_TRX(doublereal t, doublereal dens, const compositionMap& x)
370 {
372  setTemperature(t);
373  setDensity(dens);
374 }
375 
376 void Phase::setState_TRY(doublereal t, doublereal dens, const doublereal* y)
377 {
378  setMassFractions(y);
379  setTemperature(t);
380  setDensity(dens);
381 }
382 
383 void Phase::setState_TRY(doublereal t, doublereal dens, const compositionMap& y)
384 {
386  setTemperature(t);
387  setDensity(dens);
388 }
389 
390 void Phase::setState_TR(doublereal t, doublereal rho)
391 {
392  setTemperature(t);
393  setDensity(rho);
394 }
395 
396 void Phase::setState_TX(doublereal t, doublereal* x)
397 {
398  setTemperature(t);
399  setMoleFractions(x);
400 }
401 
402 void Phase::setState_TY(doublereal t, doublereal* y)
403 {
404  setTemperature(t);
405  setMassFractions(y);
406 }
407 
408 void Phase::setState_RX(doublereal rho, doublereal* x)
409 {
410  setMoleFractions(x);
411  setDensity(rho);
412 }
413 
414 void Phase::setState_RY(doublereal rho, doublereal* y)
415 {
416  setMassFractions(y);
417  setDensity(rho);
418 }
419 
420 doublereal Phase::molecularWeight(size_t k) const
421 {
423  return m_molwts[k];
424 }
425 
427 {
428  weights = molecularWeights();
429 }
430 
431 void Phase::getMolecularWeights(doublereal* weights) const
432 {
433  const vector_fp& mw = molecularWeights();
434  copy(mw.begin(), mw.end(), weights);
435 }
436 
438 {
439  return m_molwts;
440 }
441 
443 {
444  compositionMap comp;
445  for (size_t k = 0; k < m_kk; k++) {
446  double x = moleFraction(k);
447  if (x > threshold) {
448  comp[speciesName(k)] = x;
449  }
450  }
451  return comp;
452 }
453 
455 {
456  compositionMap comp;
457  for (size_t k = 0; k < m_kk; k++) {
458  double x = massFraction(k);
459  if (x > threshold) {
460  comp[speciesName(k)] = x;
461  }
462  }
463  return comp;
464 }
465 
466 void Phase::getMoleFractions(doublereal* const x) const
467 {
468  scale(m_ym.begin(), m_ym.end(), x, m_mmw);
469 }
470 
471 doublereal Phase::moleFraction(size_t k) const
472 {
474  return m_ym[k] * m_mmw;
475 }
476 
477 doublereal Phase::moleFraction(const std::string& nameSpec) const
478 {
479  size_t iloc = speciesIndex(nameSpec);
480  if (iloc != npos) {
481  return moleFraction(iloc);
482  } else {
483  return 0.0;
484  }
485 }
486 
487 const doublereal* Phase::moleFractdivMMW() const
488 {
489  return &m_ym[0];
490 }
491 
492 doublereal Phase::massFraction(size_t k) const
493 {
495  return m_y[k];
496 }
497 
498 doublereal Phase::massFraction(const std::string& nameSpec) const
499 {
500  size_t iloc = speciesIndex(nameSpec);
501  if (iloc != npos) {
502  return massFractions()[iloc];
503  } else {
504  return 0.0;
505  }
506 }
507 
508 void Phase::getMassFractions(doublereal* const y) const
509 {
510  copy(m_y.begin(), m_y.end(), y);
511 }
512 
513 doublereal Phase::concentration(const size_t k) const
514 {
516  return m_y[k] * m_dens * m_rmolwts[k];
517 }
518 
519 void Phase::getConcentrations(doublereal* const c) const
520 {
521  scale(m_ym.begin(), m_ym.end(), c, m_dens);
522 }
523 
524 void Phase::setConcentrations(const doublereal* const conc)
525 {
526  // Use m_y as temporary storage for non-negative concentrations
527  doublereal sum = 0.0, norm = 0.0;
528  for (size_t k = 0; k != m_kk; ++k) {
529  double ck = std::max(conc[k], 0.0); // Ignore negative concentrations
530  m_y[k] = ck;
531  sum += ck * m_molwts[k];
532  norm += ck;
533  }
534  m_mmw = sum/norm;
535  setDensity(sum);
536  doublereal rsum = 1.0/sum;
537  for (size_t k = 0; k != m_kk; ++k) {
538  m_ym[k] = m_y[k] * rsum;
539  m_y[k] = m_ym[k] * m_molwts[k]; // m_y is now the mass fraction
540  }
542 }
543 
544 void Phase::setConcentrationsNoNorm(const double* const conc)
545 {
546  doublereal sum = 0.0, norm = 0.0;
547  for (size_t k = 0; k != m_kk; ++k) {
548  sum += conc[k] * m_molwts[k];
549  norm += conc[k];
550  }
551  m_mmw = sum/norm;
552  setDensity(sum);
553  doublereal rsum = 1.0/sum;
554  for (size_t k = 0; k != m_kk; ++k) {
555  m_ym[k] = conc[k] * rsum;
556  m_y[k] = m_ym[k] * m_molwts[k];
557  }
559 }
560 
561 doublereal Phase::elementalMassFraction(const size_t m) const
562 {
564  doublereal Z_m = 0.0;
565  for (size_t k = 0; k != m_kk; ++k) {
566  Z_m += nAtoms(k, m) * atomicWeight(m) / molecularWeight(k)
567  * massFraction(k);
568  }
569  return Z_m;
570 }
571 
572 doublereal Phase::elementalMoleFraction(const size_t m) const
573 {
575  double denom = 0;
576  for (size_t k = 0; k < m_kk; k++) {
577  double atoms = 0;
578  for (size_t j = 0; j < nElements(); j++) {
579  atoms += nAtoms(k, j);
580  }
581  denom += atoms * moleFraction(k);
582  }
583  doublereal numerator = 0.0;
584  for (size_t k = 0; k != m_kk; ++k) {
585  numerator += nAtoms(k, m) * moleFraction(k);
586  }
587  return numerator / denom;
588 }
589 
590 doublereal Phase::molarDensity() const
591 {
592  return density()/meanMolecularWeight();
593 }
594 
595 void Phase::setMolarDensity(const doublereal molar_density)
596 {
597  m_dens = molar_density*meanMolecularWeight();
598 }
599 
600 doublereal Phase::molarVolume() const
601 {
602  return 1.0/molarDensity();
603 }
604 
605 doublereal Phase::chargeDensity() const
606 {
607  doublereal cdens = 0.0;
608  for (size_t k = 0; k < m_kk; k++) {
609  cdens += charge(k)*moleFraction(k);
610  }
611  return cdens * Faraday;
612 }
613 
614 doublereal Phase::mean_X(const doublereal* const Q) const
615 {
616  return m_mmw*std::inner_product(m_ym.begin(), m_ym.end(), Q, 0.0);
617 }
618 
619 doublereal Phase::mean_X(const vector_fp& Q) const
620 {
621  return m_mmw*std::inner_product(m_ym.begin(), m_ym.end(), Q.begin(), 0.0);
622 }
623 
624 doublereal Phase::sum_xlogx() const
625 {
626  return m_mmw* Cantera::sum_xlogx(m_ym.begin(), m_ym.end()) + log(m_mmw);
627 }
628 
629 size_t Phase::addElement(const std::string& symbol, doublereal weight,
630  int atomic_number, doublereal entropy298,
631  int elem_type)
632 {
633  // Look up the atomic weight if not given
634  if (weight == 0.0) {
635  try {
636  weight = getElementWeight(symbol);
637  } catch (CanteraError&) {
638  // assume this is just a custom element with zero atomic weight
639  }
640  } else if (weight == -12345.0) {
641  weight = getElementWeight(symbol);
642  }
643 
644  // Try to look up the standard entropy if not given. Fail silently.
645  if (entropy298 == ENTROPY298_UNKNOWN) {
646  try {
647  XML_Node* db = get_XML_File("elements.xml");
648  XML_Node* elnode = db->findByAttr("name", symbol);
649  if (elnode && elnode->hasChild("entropy298")) {
650  entropy298 = fpValueCheck(elnode->child("entropy298")["value"]);
651  }
652  } catch (CanteraError&) {
653  }
654  }
655 
656  // Check for duplicates
657  auto iter = find(m_elementNames.begin(), m_elementNames.end(), symbol);
658  if (iter != m_elementNames.end()) {
659  size_t m = iter - m_elementNames.begin();
660  if (m_atomicWeights[m] != weight) {
661  throw CanteraError("Phase::addElement",
662  "Duplicate elements ({}) have different weights", symbol);
663  } else {
664  // Ignore attempt to add duplicate element with the same weight
665  return m;
666  }
667  }
668 
669  // Add the new element
670  m_atomicWeights.push_back(weight);
671  m_elementNames.push_back(symbol);
672  m_atomicNumbers.push_back(atomic_number);
673  m_entropy298.push_back(entropy298);
674  if (symbol == "E") {
676  } else {
677  m_elem_type.push_back(elem_type);
678  }
679  m_mm++;
680 
681  // Update species compositions
682  if (m_kk) {
684  m_speciesComp.resize(m_kk*m_mm, 0.0);
685  for (size_t k = 0; k < m_kk; k++) {
686  size_t m_old = m_mm - 1;
687  for (size_t m = 0; m < m_old; m++) {
688  m_speciesComp[k * m_mm + m] = old[k * (m_old) + m];
689  }
690  m_speciesComp[k * (m_mm) + (m_mm-1)] = 0.0;
691  }
692  }
693 
694  return m_mm-1;
695 }
696 
697 bool Phase::addSpecies(shared_ptr<Species> spec) {
698  if (m_species.find(toLowerCopy(spec->name)) != m_species.end()) {
699  throw CanteraError("Phase::addSpecies",
700  "Phase '{}' already contains a species named '{}'.",
701  m_name, spec->name);
702  }
703  vector_fp comp(nElements());
704  for (const auto& elem : spec->composition) {
705  size_t m = elementIndex(elem.first);
706  if (m == npos) { // Element doesn't exist in this phase
707  switch (m_undefinedElementBehavior) {
708  case UndefElement::ignore:
709  return false;
710 
711  case UndefElement::add:
712  addElement(elem.first);
713  comp.resize(nElements());
714  m = elementIndex(elem.first);
715  break;
716 
717  case UndefElement::error:
718  default:
719  throw CanteraError("Phase::addSpecies",
720  "Species '{}' contains an undefined element '{}'.",
721  spec->name, elem.first);
722  }
723  }
724  comp[m] = elem.second;
725  }
726 
727  m_speciesNames.push_back(spec->name);
728  m_species[toLowerCopy(spec->name)] = spec;
729  m_speciesIndices[toLowerCopy(spec->name)] = m_kk;
730  m_speciesCharge.push_back(spec->charge);
731  size_t ne = nElements();
732 
733  double wt = 0.0;
734  const vector_fp& aw = atomicWeights();
735  if (spec->charge != 0.0) {
736  size_t eindex = elementIndex("E");
737  if (eindex != npos) {
738  doublereal ecomp = comp[eindex];
739  if (fabs(spec->charge + ecomp) > 0.001) {
740  if (ecomp != 0.0) {
741  throw CanteraError("Phase::addSpecies",
742  "Input charge and element E compositions differ "
743  "for species " + spec->name);
744  } else {
745  // Just fix up the element E composition based on the input
746  // species charge
747  comp[eindex] = -spec->charge;
748  }
749  }
750  } else {
751  addElement("E", 0.000545, 0, 0.0, CT_ELEM_TYPE_ELECTRONCHARGE);
752  ne = nElements();
753  eindex = elementIndex("E");
754  comp.resize(ne);
755  comp[ne - 1] = - spec->charge;
756  }
757  }
758  for (size_t m = 0; m < ne; m++) {
759  m_speciesComp.push_back(comp[m]);
760  wt += comp[m] * aw[m];
761  }
762 
763  // Some surface phases may define species representing empty sites
764  // that have zero molecular weight. Give them a very small molecular
765  // weight to avoid dividing by zero.
766  wt = std::max(wt, Tiny);
767  m_molwts.push_back(wt);
768  m_rmolwts.push_back(1.0/wt);
769  m_kk++;
770 
771  // Ensure that the Phase has a valid mass fraction vector that sums to
772  // one. We will assume that species 0 has a mass fraction of 1.0 and mass
773  // fraction of all other species is 0.0.
774  if (m_kk == 1) {
775  m_y.push_back(1.0);
776  m_ym.push_back(m_rmolwts[0]);
777  m_mmw = 1.0 / m_ym[0];
778  } else {
779  m_y.push_back(0.0);
780  m_ym.push_back(0.0);
781  }
782  invalidateCache();
783  return true;
784 }
785 
786 void Phase::modifySpecies(size_t k, shared_ptr<Species> spec)
787 {
788  if (speciesName(k) != spec->name) {
789  throw CanteraError("Phase::modifySpecies",
790  "New species name '{}' does not match existing name '{}'",
791  spec->name, speciesName(k));
792  }
793  const shared_ptr<Species>& old = m_species[toLowerCopy(spec->name)];
794  if (spec->composition != old->composition) {
795  throw CanteraError("Phase::modifySpecies",
796  "New composition for '{}' does not match existing composition",
797  spec->name);
798  }
799  m_species[toLowerCopy(spec->name)] = spec;
800  invalidateCache();
801 }
802 
803 shared_ptr<Species> Phase::species(const std::string& name) const
804 {
805  return m_species.at(toLowerCopy(name));
806 }
807 
808 shared_ptr<Species> Phase::species(size_t k) const
809 {
810  return species(m_speciesNames[k]);
811 }
812 
814  m_undefinedElementBehavior = UndefElement::ignore;
815 }
816 
818  m_undefinedElementBehavior = UndefElement::add;
819 }
820 
822  m_undefinedElementBehavior = UndefElement::error;
823 }
824 
825 bool Phase::ready() const
826 {
827  return (m_kk > 0);
828 }
829 
831  m_cache.clear();
832 }
833 
835  m_stateNum++;
836 }
837 
838 } // namespace Cantera
std::map< std::string, doublereal > compositionMap
Map connecting a string name with a double.
Definition: ct_defs.h:149
const vector_fp & molecularWeights() const
Return a const reference to the internal vector of molecular weights.
Definition: Phase.cpp:437
virtual void setMoleFractions_NoNorm(const doublereal *const x)
Set the mole fractions to the specified values without normalizing.
Definition: Phase.cpp:283
vector_fp m_y
Mass fractions of the species.
Definition: Phase.h:836
void setState_RY(doublereal rho, doublereal *y)
Set the density (kg/m^3) and mass fractions.
Definition: Phase.cpp:414
doublereal molarVolume() const
Molar volume (m^3/kmol).
Definition: Phase.cpp:600
Array size error.
Definition: ctexceptions.h:128
std::vector< std::string > m_elementNames
element names
Definition: Phase.h:855
size_t nElements() const
Number of elements.
Definition: Phase.cpp:88
void restoreState(const vector_fp &state)
Restore a state saved on a previous call to saveState.
Definition: Phase.cpp:233
XML_Node * get_XML_File(const std::string &file, int debug)
Return a pointer to the XML tree for a Cantera input file.
Definition: global.cpp:105
CTML ("Cantera Markup Language") is the variant of XML that Cantera uses to store data...
void setState_TRY(doublereal t, doublereal dens, const doublereal *y)
Set the internally stored temperature (K), density, and mass fractions.
Definition: Phase.cpp:376
XML_Node * findXMLPhase(XML_Node *root, const std::string &idtarget)
Search an XML_Node tree for a named phase XML_Node.
Definition: xml.cpp:1038
void getMassFractions(doublereal *const y) const
Get the species mass fractions.
Definition: Phase.cpp:508
vector_fp m_atomicWeights
element atomic weights (kg kmol-1)
Definition: Phase.h:853
doublereal temperature() const
Temperature (K).
Definition: Phase.h:601
const doublereal * massFractions() const
Return a const pointer to the mass fraction array.
Definition: Phase.h:478
Various templated functions that carry out common vector operations (see Templated Utility Functions)...
size_t speciesIndex(const std::string &name) const
Returns the index of a species named &#39;name&#39; within the Phase object.
Definition: Phase.cpp:175
void saveState(vector_fp &state) const
Save the current internal state of the phase.
Definition: Phase.cpp:221
doublereal moleFraction(size_t k) const
Return the mole fraction of a single species.
Definition: Phase.cpp:471
const size_t npos
index returned by functions to indicate "no position"
Definition: ct_defs.h:165
vector_fp m_speciesCharge
Vector of species charges. length m_kk.
Definition: Phase.h:799
Header file for class Phase.
size_t elementIndex(const std::string &name) const
Return the index of element named &#39;name&#39;.
Definition: Phase.cpp:113
doublereal sum_xlogx() const
Evaluate .
Definition: Phase.cpp:624
Headers for the factory class that can create known ThermoPhase objects (see Thermodynamic Properties...
vector_fp m_entropy298
Entropy at 298.15 K and 1 bar of stable state pure elements (J kmol-1)
Definition: Phase.h:859
compositionMap getMoleFractionsByName(double threshold=0.0) const
Get the mole fractions by name.
Definition: Phase.cpp:442
Class XML_Node is a tree-based representation of the contents of an XML file.
Definition: xml.h:97
std::map< std::string, size_t > m_speciesIndices
Map of species names to indices.
Definition: Phase.h:850
void checkSpeciesIndex(size_t k) const
Check that the specified species index is in range.
Definition: Phase.cpp:202
size_t nSpecies() const
Returns the number of species in the phase.
Definition: Phase.h:266
STL namespace.
virtual doublereal density() const
Density (kg/m^3).
Definition: Phase.h:607
doublereal m_mmw
mean molecular weight of the mixture (kg kmol-1)
Definition: Phase.h:825
#define CT_ELEM_TYPE_ELECTRONCHARGE
This refers to conservation of electrons.
Definition: Elements.h:43
virtual bool ready() const
Returns a bool indicating whether the object is ready for use.
Definition: Phase.cpp:825
Unary operator to multiply the argument by a constant.
Definition: utilities.h:33
UndefElement::behavior m_undefinedElementBehavior
Flag determining behavior when adding species with an undefined element.
Definition: Phase.h:804
std::string m_name
Name of the phase.
Definition: Phase.h:816
const std::vector< std::string > & speciesNames() const
Return a const reference to the vector of species names.
Definition: Phase.cpp:197
doublereal mean_X(const doublereal *const Q) const
Evaluate the mole-fraction-weighted mean of an array Q.
Definition: Phase.cpp:614
int atomicNumber(size_t m) const
Atomic number of element m.
Definition: Phase.cpp:144
void setName(const std::string &nm)
Sets the string name for the phase.
Definition: Phase.cpp:83
doublereal elementalMoleFraction(const size_t m) const
Elemental mole fraction of element m.
Definition: Phase.cpp:572
void throwUndefinedElements()
Set the behavior when adding a species containing undefined elements to throw an exception.
Definition: Phase.cpp:821
int m_stateNum
State Change variable.
Definition: Phase.h:844
doublereal atomicWeight(size_t m) const
Atomic weight of element m.
Definition: Phase.cpp:128
void ignoreUndefinedElements()
Set behavior when adding a species containing undefined elements to just skip the species...
Definition: Phase.cpp:813
std::vector< std::string > m_speciesNames
Vector of the species names.
Definition: Phase.h:847
void setState_TRX(doublereal t, doublereal dens, const doublereal *x)
Set the internally stored temperature (K), density, and mole fractions.
Definition: Phase.cpp:355
void getAtoms(size_t k, double *atomArray) const
Get a vector containing the atomic composition of species k.
Definition: Phase.cpp:168
void checkElementArraySize(size_t mm) const
Check that an array size is at least nElements().
Definition: Phase.cpp:100
doublereal molarDensity() const
Molar density (kmol/m^3).
Definition: Phase.cpp:590
shared_ptr< Species > species(const std::string &name) const
Return the Species object for the named species.
Definition: Phase.cpp:803
std::string speciesName(size_t k) const
Name of the species with index k.
Definition: Phase.cpp:191
virtual void compositionChanged()
Apply changes to the state which are needed after the composition changes.
Definition: Phase.cpp:834
doublereal entropyElement298(size_t m) const
Entropy of the element in its standard state at 298 K and 1 bar.
Definition: Phase.cpp:133
std::string speciesSPName(int k) const
Returns the expanded species name of a species, including the phase name This is guaranteed to be uni...
Definition: Phase.cpp:216
void clear()
Clear all cached values.
Definition: ValueCache.cpp:27
const doublereal * moleFractdivMMW() const
Returns a const pointer to the start of the moleFraction/MW array.
Definition: Phase.cpp:487
void setMoleFractionsByName(const compositionMap &xMap)
Set the species mole fractions by name.
Definition: Phase.cpp:292
ValueCache m_cache
Cached for saved calculations within each ThermoPhase.
Definition: Phase.h:765
void setMassFractionsByName(const compositionMap &yMap)
Set the species mass fractions by name.
Definition: Phase.cpp:336
const U & getValue(const std::map< T, U > &m, const T &key, const U &default_val)
Const accessor for a value in a std::map.
Definition: utilities.h:504
virtual void modifySpecies(size_t k, shared_ptr< Species > spec)
Modify the thermodynamic data associated with a species.
Definition: Phase.cpp:786
Base class for exceptions thrown by Cantera classes.
Definition: ctexceptions.h:65
void setState_TNX(doublereal t, doublereal n, const doublereal *x)
Set the internally stored temperature (K), molar density (kmol/m^3), and mole fractions.
Definition: Phase.cpp:362
double getElementWeight(const std::string &ename)
Get the atomic weight of an element.
Definition: Elements.cpp:166
virtual void invalidateCache()
Invalidate any cached values which are normally updated only when a change in state is detected...
Definition: Phase.cpp:830
virtual void setConcentrations(const doublereal *const conc)
Set the concentrations to the specified values within the phase.
Definition: Phase.cpp:524
virtual void setMoleFractions(const doublereal *const x)
Set the mole fractions to the specified values.
Definition: Phase.cpp:251
std::string parseSpeciesName(const std::string &nameStr, std::string &phaseName)
Parse a name string, separating out the phase name from the species name.
doublereal sum_xlogx(InputIter begin, InputIter end)
Compute .
Definition: utilities.h:389
doublereal concentration(const size_t k) const
Concentration of species k.
Definition: Phase.cpp:513
virtual bool addSpecies(shared_ptr< Species > spec)
Add a Species to this Phase.
Definition: Phase.cpp:697
void getMoleFractions(doublereal *const x) const
Get the species mole fraction vector.
Definition: Phase.cpp:466
virtual void setMolarDensity(const doublereal molarDensity)
Set the internally stored molar density (kmol/m^3) of the phase.
Definition: Phase.cpp:595
XML_Node & xml() const
Returns a const reference to the XML_Node that describes the phase.
Definition: Phase.cpp:44
virtual void setConcentrationsNoNorm(const double *const conc)
Set the concentrations without ignoring negative concentrations.
Definition: Phase.cpp:544
doublereal dot(InputIter x_begin, InputIter x_end, InputIter2 y_begin)
Function that calculates a templated inner product.
Definition: utilities.h:107
compositionMap getMassFractionsByName(double threshold=0.0) const
Get the mass fractions by name.
Definition: Phase.cpp:454
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...
Definition: xml.cpp:825
#define ENTROPY298_UNKNOWN
Number indicating we don&#39;t know the entropy of the element in its most stable state at 298...
Definition: Elements.h:87
virtual void setMassFractions_NoNorm(const doublereal *const y)
Set the mass fractions to the specified values without normalizing.
Definition: Phase.cpp:325
bool hasChild(const std::string &ch) const
Tests whether the current node has a child node with a particular name.
Definition: xml.cpp:536
XML_Node & child(const size_t n) const
Return a changeable reference to the n&#39;th child of the current node.
Definition: xml.cpp:546
void setState_TX(doublereal t, doublereal *x)
Set the internally stored temperature (K) and mole fractions.
Definition: Phase.cpp:396
XML_Node & root() const
Return the root of the current XML_Node tree.
Definition: xml.cpp:1025
vector_int m_elem_type
Vector of element types.
Definition: Phase.h:856
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.
Definition: stringUtils.cpp:60
doublereal elementalMassFraction(const size_t m) const
Elemental mass fraction of element m.
Definition: Phase.cpp:561
doublereal chargeDensity() const
Charge density [C/m^3].
Definition: Phase.cpp:605
std::string id() const
Return the string id for the phase.
Definition: Phase.cpp:68
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.
Definition: Phase.cpp:629
void checkSpeciesArraySize(size_t kk) const
Check that an array size is at least nSpecies().
Definition: Phase.cpp:209
vector_fp m_molwts
species molecular weights (kg kmol-1)
Definition: Phase.h:838
void setState_TY(doublereal t, doublereal *y)
Set the internally stored temperature (K) and mass fractions.
Definition: Phase.cpp:402
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
Definition: ct_defs.h:157
doublereal meanMolecularWeight() const
The mean molecular weight. Units: (kg/kmol)
Definition: Phase.h:661
void addUndefinedElements()
Set behavior when adding a species containing undefined elements to add those elements to the phase...
Definition: Phase.cpp:817
size_t m_mm
Number of elements.
Definition: Phase.h:852
virtual void setTemperature(const doublereal temp)
Set the internally stored temperature of the phase (K).
Definition: Phase.h:637
void scale(InputIter begin, InputIter end, OutputIter out, S scale_factor)
Multiply elements of an array by a scale factor.
Definition: utilities.h:130
const doublereal Tiny
Small number to compare differences of mole fractions against.
Definition: ct_defs.h:143
std::string m_id
ID of the phase.
Definition: Phase.h:811
doublereal m_dens
Density (kg m-3).
Definition: Phase.h:823
vector_fp m_ym
m_ym[k] = mole fraction of species k divided by the mean molecular weight of mixture.
Definition: Phase.h:829
int elementType(size_t m) const
Return the element constraint type Possible types include:
Definition: Phase.cpp:149
std::string id() const
Return the id attribute, if present.
Definition: xml.cpp:428
void getMolecularWeights(vector_fp &weights) const
Copy the vector of molecular weights into vector weights.
Definition: Phase.cpp:426
XML_Node * m_xml
XML node containing the XML info for this phase.
Definition: Phase.h:807
std::string name() const
Return the name of the phase.
Definition: Phase.cpp:78
vector_int m_atomicNumbers
element atomic numbers
Definition: Phase.h:854
Contains declarations for string manipulation functions within Cantera.
virtual void setMassFractions(const doublereal *const y)
Set the mass fractions to the specified values and normalize them.
Definition: Phase.cpp:311
vector_fp m_rmolwts
inverse of species molecular weights (kmol kg-1)
Definition: Phase.h:840
vector_fp m_speciesComp
Atomic composition of the species.
Definition: Phase.h:797
void setXMLdata(XML_Node &xmlPhase)
Stores the XML tree information for the current phase.
Definition: Phase.cpp:49
void setState_RX(doublereal rho, doublereal *x)
Set the density (kg/m^3) and mole fractions.
Definition: Phase.cpp:408
size_t m_kk
Number of species in the phase.
Definition: Phase.h:788
An array index is out of range.
Definition: ctexceptions.h:158
int changeElementType(int m, int elem_type)
Change the element type of the mth constraint Reassigns an element type.
Definition: Phase.cpp:154
void setID(const std::string &id)
Set the string id for the phase.
Definition: Phase.cpp:73
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...
Definition: xml.cpp:661
doublereal molecularWeight(size_t k) const
Molecular weight of species k.
Definition: Phase.cpp:420
std::string toLowerCopy(const std::string &input)
Convert to lower case.
Namespace for the Cantera kernel.
Definition: AnyMap.cpp:8
void getConcentrations(doublereal *const c) const
Get the species concentrations (kmol/m^3).
Definition: Phase.cpp:519
doublereal nAtoms(size_t k, size_t m) const
Number of atoms of element m in species k.
Definition: Phase.cpp:161
doublereal fpValueCheck(const std::string &val)
Translate a string into one doublereal value, with error checking.
void setState_TR(doublereal t, doublereal rho)
Set the internally stored temperature (K) and density (kg/m^3)
Definition: Phase.cpp:390
const std::vector< std::string > & elementNames() const
Return a read-only reference to the vector of element names.
Definition: Phase.cpp:123
const vector_fp & atomicWeights() const
Return a read-only reference to the vector of atomic weights.
Definition: Phase.cpp:139
doublereal charge(size_t k) const
Dimensionless electrical charge of a single molecule of species k The charge is normalized by the the...
Definition: Phase.h:577
std::string elementName(size_t m) const
Name of the element with index m.
Definition: Phase.cpp:107
void checkElementIndex(size_t m) const
Check that the specified element index is in range.
Definition: Phase.cpp:93
virtual void setDensity(const doublereal density_)
Set the internally stored density (kg/m^3) of the phase.
Definition: Phase.h:622
doublereal massFraction(size_t k) const
Return the mass fraction of a single species.
Definition: Phase.cpp:492