From 606a18e9510f7fca1e3d9e75fda6e02bfded7feb Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Thu, 18 Sep 2014 22:59:02 +0000 Subject: [PATCH] [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. --- interfaces/cython/cantera/_cantera.pxd | 2 ++ interfaces/cython/cantera/_cantera.pyx | 2 +- interfaces/cython/cantera/test/test_thermo.py | 10 ++++++++++ interfaces/cython/cantera/thermo.pyx | 8 ++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index 7aab1f1e2..a88b16621 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -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 + diff --git a/interfaces/cython/cantera/_cantera.pyx b/interfaces/cython/cantera/_cantera.pyx index 67cce0bbb..109f0c46d 100644 --- a/interfaces/cython/cantera/_cantera.pyx +++ b/interfaces/cython/cantera/_cantera.pyx @@ -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 * diff --git a/interfaces/cython/cantera/test/test_thermo.py b/interfaces/cython/cantera/test/test_thermo.py index 33bef9515..cf46bc1ea 100644 --- a/interfaces/cython/cantera/test/test_thermo.py +++ b/interfaces/cython/cantera/test/test_thermo.py @@ -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 diff --git a/interfaces/cython/cantera/thermo.pyx b/interfaces/cython/cantera/thermo.pyx index 11a043214..2a3cee2e1 100644 --- a/interfaces/cython/cantera/thermo.pyx +++ b/interfaces/cython/cantera/thermo.pyx @@ -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: