[Cython/Thermo] Add mass/mole_fraction_dict methods

This complements the ability to set the composition using a dict when working
with phases with different sets of species.
This commit is contained in:
Ray Speth 2014-09-18 22:59:02 +00:00
parent 09db5a498a
commit 606a18e951
4 changed files with 21 additions and 1 deletions

View file

@ -86,6 +86,7 @@ cdef extern from "cantera/thermo/ThermoPhase.h" namespace "Cantera":
void setMassFractionsByName(string) except +
void setMassFractionsByName(stdmap[string,double]&) except +
void setMassFractions_NoNorm(double*) except +
stdmap[string,double] getMassFractionsByName(double)
double massFraction(size_t) except +
double massFraction(string) except +
@ -93,6 +94,7 @@ cdef extern from "cantera/thermo/ThermoPhase.h" namespace "Cantera":
void setMoleFractionsByName(stdmap[string,double]&) except +
void setMoleFractions_NoNorm(double*) except +
void getMoleFractions(double*) except +
stdmap[string,double] getMoleFractionsByName(double)
double moleFraction(size_t) except +
double moleFraction(string) except +

View file

@ -5,7 +5,7 @@ import numpy as np
cimport numpy as np
import math
from cython.operator cimport dereference as deref
from cython.operator cimport dereference as deref, preincrement as inc
from _cantera cimport *

View file

@ -94,6 +94,16 @@ class TestThermoPhase(utilities.CanteraTest):
self.assertNear(Y[0], 0.25)
self.assertNear(Y[3], 0.75)
def test_getCompositionDict(self):
self.phase.X = 'OH:1e-9, O2:0.4, AR:0.6'
self.assertEqual(len(self.phase.mole_fraction_dict(1e-7)), 2)
self.assertEqual(len(self.phase.mole_fraction_dict()), 3)
self.phase.Y = 'O2:0.4, AR:0.6'
Y1 = self.phase.mass_fraction_dict()
self.assertNear(Y1['O2'], 0.4)
self.assertNear(Y1['AR'], 0.6)
def test_setCompositionNoNorm(self):
X = np.zeros(self.phase.n_species)
X[2] = 1.0

View file

@ -344,6 +344,14 @@ cdef class ThermoPhase(_SolutionBase):
raise ValueError("Array has incorrect length")
self.thermo.setMoleFractions_NoNorm(&data[0])
def mass_fraction_dict(self, double threshold=0.0):
Y = self.thermo.getMassFractionsByName(threshold)
return {pystr(item.first):item.second for item in Y}
def mole_fraction_dict(self, double threshold=0.0):
X = self.thermo.getMoleFractionsByName(threshold)
return {pystr(item.first):item.second for item in X}
######## Read-only thermodynamic properties ########
property P: