Skip to content

Commit

Permalink
[Python] Make SolutionArray compatible with PureFluid
Browse files Browse the repository at this point in the history
  • Loading branch information
speth committed Jul 9, 2016
1 parent 5c23ea3 commit 366ff04
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
19 changes: 9 additions & 10 deletions interfaces/cython/cantera/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,6 @@ def setter(self, value):


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

Expand Down Expand Up @@ -259,13 +255,13 @@ def append(self, state=None, **kwargs):

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)))
Expand Down Expand Up @@ -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)
Expand All @@ -364,10 +360,13 @@ def setter(self, AB):
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'
Expand All @@ -392,7 +391,7 @@ def setter(self, ABC):

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):
Expand Down
11 changes: 11 additions & 0 deletions interfaces/cython/cantera/thermo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 366ff04

Please sign in to comment.