From 366ff043ffc3a94db955ac4b5600af84397a5c9f Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Thu, 31 Mar 2016 23:30:17 -0400 Subject: [PATCH] [Python] Make SolutionArray compatible with PureFluid --- interfaces/cython/cantera/composite.py | 19 +++++++++---------- interfaces/cython/cantera/thermo.pyx | 11 +++++++++++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/interfaces/cython/cantera/composite.py b/interfaces/cython/cantera/composite.py index 3b2d45d20..c1ef3d61c 100644 --- a/interfaces/cython/cantera/composite.py +++ b/interfaces/cython/cantera/composite.py @@ -195,10 +195,6 @@ for _attr in dir(Solution): class SolutionArray(object): - _full_states = {frozenset(k): k - for k in ('TDX', 'TDY', 'TPX', 'TPY', 'UVX', 'UVY', 'DPX', - 'DPY', 'HPX', 'HPY', 'SPX', 'SPY', 'SVX', 'SVY')} - def __init__(self, phase, shape=(0,), states=None): self._phase = phase @@ -259,13 +255,13 @@ class SolutionArray(object): elif len(kwargs) == 1: attr, value = next(iter(kwargs.items())) - if frozenset(attr) not in self._full_states: + if frozenset(attr) not in self._phase._full_states: raise KeyError("{} does not specify a full thermodynamic state") setattr(self._phase, attr, value) else: try: - attr = self._full_states[frozenset(kwargs)] + attr = self._phase._full_states[frozenset(kwargs)] except KeyError: raise KeyError("{} is not a valid combination of properties " "for setting the thermodynamic state".format(tuple(kwargs))) @@ -347,7 +343,7 @@ def _make_functions(): # Factory for creating properties which consist of a tuple of two variables, # e.g. 'TP' or 'SV' - def state2_prop(name): + def state2_prop(name, doc_source): def getter(self): a = np.empty(self._shape) b = np.empty(self._shape) @@ -364,10 +360,13 @@ def _make_functions(): setattr(self._phase, name, (A[index], B[index])) self._states[index][:] = self._phase.state - return property(getter, setter, doc=getattr(Solution, name).__doc__) + return property(getter, setter, doc=getattr(doc_source, name).__doc__) for name in state2: - setattr(SolutionArray, name, state2_prop(name)) + setattr(SolutionArray, name, state2_prop(name, Solution)) + + for name in PureFluid._full_states.values(): + setattr(SolutionArray, name, state2_prop(name, PureFluid)) # Factory for creating properties which consist of a tuple of three # variables, e.g. 'TPY' or 'UVX' @@ -392,7 +391,7 @@ def _make_functions(): return property(getter, setter, doc=getattr(Solution, name).__doc__) - for name in SolutionArray._full_states.values(): + for name in Solution._full_states.values(): setattr(SolutionArray, name, state3_prop(name)) def scalar_prop(name): diff --git a/interfaces/cython/cantera/thermo.pyx b/interfaces/cython/cantera/thermo.pyx index 019ad984c..ae50fddc0 100644 --- a/interfaces/cython/cantera/thermo.pyx +++ b/interfaces/cython/cantera/thermo.pyx @@ -212,6 +212,12 @@ cdef class ThermoPhase(_SolutionBase): Class `ThermoPhase` is not usually instantiated directly. It is used as a base class for classes `Solution` and `Interface`. """ + + # Sets of parameters which set the full thermodynamic state + _full_states = {frozenset(k): k + for k in ('TDX', 'TDY', 'TPX', 'TPY', 'UVX', 'UVY', 'DPX', + 'DPY', 'HPX', 'HPY', 'SPX', 'SPY', 'SVX', 'SVY')} + # The signature of this function causes warnings for Sphinx documentation def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -1291,6 +1297,11 @@ cdef class PureFluid(ThermoPhase): A pure substance that can be a gas, a liquid, a mixed gas-liquid fluid, or a fluid beyond its critical point. """ + + _full_states = {frozenset(k): k + for k in ('TD', 'TP', 'UV', 'DP', 'HP', 'SP', 'SV', 'TX', + 'PX', 'ST', 'TV', 'PV', 'UP', 'VH', 'TH', 'SH')} + property X: """ Get/Set vapor fraction (quality). Can be set only when in the two-phase