Skip to content

Commit

Permalink
[Thermo] update checks for available Python setters
Browse files Browse the repository at this point in the history
- add checks for remaining setters
- make sure that getters remain enabled
  • Loading branch information
Ingmar Schoegl authored and ischoegl committed Oct 23, 2019
1 parent 9eb9b8b commit ef3359a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
6 changes: 4 additions & 2 deletions interfaces/cython/cantera/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,8 @@ def restore_data(self, data, labels):

# get full state information (may differ depending on ThermoPhase type)
states = list(self._phase._full_states.values())

# add partial and/or potentially non-unique state definitions
if isinstance(self._phase, PureFluid):
states += ['TPX']
states += list(self._phase._partial_states.values())
Expand Down Expand Up @@ -690,7 +692,7 @@ def restore_data(self, data, labels):
'U': ('u', 'int_energy_{}'.format(basis)),
'V': ('v', 'volume_{}'.format(basis)),
'H': ('h', 'enthalpy_{}'.format(basis)),
'S': ('s', 'entropy_{}'.format(basis))}
'S': ('s', 'entropy_{}'.format(basis))}
for st in states:
# identify property specifiers
state = [{st[i]: labels.index(p) for p in prop[st[i]] if p in labels}
Expand Down Expand Up @@ -782,7 +784,7 @@ def collect_data(self, cols=None, threshold=0, species='Y'):
# Create default columns (including complete state information)
if cols is None:
cols = ('extra',) + self._phase._default_state

# Expand cols to include the individual items in 'extra'
expanded_cols = []
for c in cols:
Expand Down
6 changes: 2 additions & 4 deletions interfaces/cython/cantera/test/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1281,8 +1281,7 @@ def test_shomate_create(self):
self.assertNear(st.s(T), st2.s(T))

def test_piecewise_gibbs_load(self):
# @todo: replace by ct.Solution once issue #595 is resolved
sol = ct.ThermoPhase('thermo-models.yaml', 'HMW-NaCl-electrolyte')
sol = ct.Solution('thermo-models.yaml', 'HMW-NaCl-electrolyte')
st = sol.species(1).thermo
self.assertIsInstance(st, ct.Mu0Poly)
self.assertEqual(st.n_coeffs, len(st.coeffs))
Expand All @@ -1303,8 +1302,7 @@ def test_piecewise_gibbs_create1(self):
self.assertEqual(st2.n_coeffs, len(st2.coeffs))

def test_piecewise_gibbs_create2(self):
# @todo: replace by ct.Solution once issue #595 is resolved
sol = ct.ThermoPhase('thermo-models.yaml', 'HMW-NaCl-electrolyte')
sol = ct.Solution('thermo-models.yaml', 'HMW-NaCl-electrolyte')
st = sol.species(1).thermo
t_min = st.min_temp
t_max = st.max_temp
Expand Down
51 changes: 24 additions & 27 deletions interfaces/cython/cantera/thermo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,14 @@ cdef class ThermoPhase(_SolutionBase):
states = [s.decode('utf-8') for s in states]
return {frozenset(k): k for k in states}

def _check_property(self, prop):
def _check_setter(self, prop):
"""
Check whether *prop* represents a valid (partial) thermodynamic state
Check whether *prop* represents a settable (partial) thermodynamic state
"""
valid = any([prop in self._full_states.values(),
prop in self._partial_states.values()])
if not valid:
raise AttributeError("Setter/getter '{}' is not defined "
raise AttributeError("Setter '{}' is not available "
"for phase '{}'".format(prop, self.name))
return valid

Expand Down Expand Up @@ -1042,10 +1042,9 @@ cdef class ThermoPhase(_SolutionBase):
property TD:
"""Get/Set temperature [K] and density [kg/m^3 or kmol/m^3]."""
def __get__(self):
self._check_property('TD')
return self.T, self.density
def __set__(self, values):
self._check_property('TD')
self._check_setter('TD')
assert len(values) == 2, 'incorrect number of values'
T = values[0] if values[0] is not None else self.T
D = values[1] if values[1] is not None else self.density
Expand All @@ -1057,10 +1056,9 @@ cdef class ThermoPhase(_SolutionBase):
fractions.
"""
def __get__(self):
self._check_property('TDX')
return self.T, self.density, self.X
def __set__(self, values):
self._check_property('TDX')
self._check_setter('TDX')
assert len(values) == 3, 'incorrect number of values'
T = values[0] if values[0] is not None else self.T
D = values[1] if values[1] is not None else self.density
Expand All @@ -1073,10 +1071,9 @@ cdef class ThermoPhase(_SolutionBase):
fractions.
"""
def __get__(self):
self._check_property('TDY')
return self.T, self.density, self.Y
def __set__(self, values):
self._check_property('TDY')
self._check_setter('TDY')
assert len(values) == 3, 'incorrect number of values'
T = values[0] if values[0] is not None else self.T
D = values[1] if values[1] is not None else self.density
Expand All @@ -1088,6 +1085,7 @@ cdef class ThermoPhase(_SolutionBase):
def __get__(self):
return self.T, self.P
def __set__(self, values):
self._check_setter('TP')
assert len(values) == 2, 'incorrect number of values'
T = values[0] if values[0] is not None else self.T
P = values[1] if values[1] is not None else self.P
Expand All @@ -1098,6 +1096,7 @@ cdef class ThermoPhase(_SolutionBase):
def __get__(self):
return self.T, self.P, self.X
def __set__(self, values):
self._check_setter('TPX')
assert len(values) == 3, 'incorrect number of values'
T = values[0] if values[0] is not None else self.T
P = values[1] if values[1] is not None else self.P
Expand All @@ -1109,6 +1108,7 @@ cdef class ThermoPhase(_SolutionBase):
def __get__(self):
return self.T, self.P, self.Y
def __set__(self, values):
self._check_setter('TPY')
assert len(values) == 3, 'incorrect number of values'
T = values[0] if values[0] is not None else self.T
P = values[1] if values[1] is not None else self.P
Expand All @@ -1121,10 +1121,9 @@ cdef class ThermoPhase(_SolutionBase):
[m^3/kg or m^3/kmol].
"""
def __get__(self):
self._check_property('UV')
return self.u, self.v
def __set__(self, values):
self._check_property('UV')
self._check_setter('UV')
assert len(values) == 2, 'incorrect number of values'
U = values[0] if values[0] is not None else self.u
V = values[1] if values[1] is not None else self.v
Expand All @@ -1137,10 +1136,9 @@ cdef class ThermoPhase(_SolutionBase):
[m^3/kg or m^3/kmol], and mole fractions.
"""
def __get__(self):
self._check_property('UVX')
return self.u, self.v, self.X
def __set__(self, values):
self._check_property('UVX')
self._check_setter('UVX')
assert len(values) == 3, 'incorrect number of values'
U = values[0] if values[0] is not None else self.u
V = values[1] if values[1] is not None else self.v
Expand All @@ -1154,10 +1152,9 @@ cdef class ThermoPhase(_SolutionBase):
[m^3/kg or m^3/kmol], and mass fractions.
"""
def __get__(self):
self._check_property('UVY')
return self.u, self.v, self.Y
def __set__(self, values):
self._check_property('UVY')
self._check_setter('UVY')
assert len(values) == 3, 'incorrect number of values'
U = values[0] if values[0] is not None else self.u
V = values[1] if values[1] is not None else self.v
Expand All @@ -1168,10 +1165,9 @@ cdef class ThermoPhase(_SolutionBase):
property DP:
"""Get/Set density [kg/m^3] and pressure [Pa]."""
def __get__(self):
self._check_property('DP')
return self.density, self.P
def __set__(self, values):
self._check_property('DP')
self._check_setter('DP')
assert len(values) == 2, 'incorrect number of values'
D = values[0] if values[0] is not None else self.density
P = values[1] if values[1] is not None else self.P
Expand All @@ -1180,10 +1176,9 @@ cdef class ThermoPhase(_SolutionBase):
property DPX:
"""Get/Set density [kg/m^3], pressure [Pa], and mole fractions."""
def __get__(self):
self._check_property('DPX')
return self.density, self.P, self.X
def __set__(self, values):
self._check_property('DPX')
self._check_setter('DPX')
assert len(values) == 3, 'incorrect number of values'
D = values[0] if values[0] is not None else self.density
P = values[1] if values[1] is not None else self.P
Expand All @@ -1193,10 +1188,9 @@ cdef class ThermoPhase(_SolutionBase):
property DPY:
"""Get/Set density [kg/m^3], pressure [Pa], and mass fractions."""
def __get__(self):
self._check_property('DPY')
return self.density, self.P, self.Y
def __set__(self, values):
self._check_property('DPY')
self._check_setter('DPY')
assert len(values) == 3, 'incorrect number of values'
D = values[0] if values[0] is not None else self.density
P = values[1] if values[1] is not None else self.P
Expand All @@ -1208,6 +1202,7 @@ cdef class ThermoPhase(_SolutionBase):
def __get__(self):
return self.h, self.P
def __set__(self, values):
self._check_setter('HP')
assert len(values) == 2, 'incorrect number of values'
H = values[0] if values[0] is not None else self.h
P = values[1] if values[1] is not None else self.P
Expand All @@ -1218,6 +1213,7 @@ cdef class ThermoPhase(_SolutionBase):
def __get__(self):
return self.h, self.P, self.X
def __set__(self, values):
self._check_setter('HPX')
assert len(values) == 3, 'incorrect number of values'
H = values[0] if values[0] is not None else self.h
P = values[1] if values[1] is not None else self.P
Expand All @@ -1229,6 +1225,7 @@ cdef class ThermoPhase(_SolutionBase):
def __get__(self):
return self.h, self.P, self.Y
def __set__(self, values):
self._check_setter('HPY')
assert len(values) == 3, 'incorrect number of values'
H = values[0] if values[0] is not None else self.h
P = values[1] if values[1] is not None else self.P
Expand All @@ -1240,6 +1237,7 @@ cdef class ThermoPhase(_SolutionBase):
def __get__(self):
return self.s, self.P
def __set__(self, values):
self._check_setter('SP')
assert len(values) == 2, 'incorrect number of values'
S = values[0] if values[0] is not None else self.s
P = values[1] if values[1] is not None else self.P
Expand All @@ -1250,6 +1248,7 @@ cdef class ThermoPhase(_SolutionBase):
def __get__(self):
return self.s, self.P, self.X
def __set__(self, values):
self._check_setter('SPX')
assert len(values) == 3, 'incorrect number of values'
S = values[0] if values[0] is not None else self.s
P = values[1] if values[1] is not None else self.P
Expand All @@ -1261,6 +1260,7 @@ cdef class ThermoPhase(_SolutionBase):
def __get__(self):
return self.s, self.P, self.Y
def __set__(self, values):
self._check_setter('SPY')
assert len(values) == 3, 'incorrect number of values'
S = values[0] if values[0] is not None else self.s
P = values[1] if values[1] is not None else self.P
Expand All @@ -1273,10 +1273,9 @@ cdef class ThermoPhase(_SolutionBase):
m^3/kmol].
"""
def __get__(self):
self._check_property('SV')
return self.s, self.v
def __set__(self, values):
self._check_property('SV')
self._check_setter('SV')
assert len(values) == 2, 'incorrect number of values'
S = values[0] if values[0] is not None else self.s
V = values[1] if values[1] is not None else self.v
Expand All @@ -1289,10 +1288,9 @@ cdef class ThermoPhase(_SolutionBase):
m^3/kmol], and mole fractions.
"""
def __get__(self):
self._check_property('SVX')
return self.s, self.v, self.X
def __set__(self, values):
self._check_property('SVX')
self._check_setter('SVX')
assert len(values) == 3, 'incorrect number of values'
S = values[0] if values[0] is not None else self.s
V = values[1] if values[1] is not None else self.v
Expand All @@ -1306,10 +1304,9 @@ cdef class ThermoPhase(_SolutionBase):
m^3/kmol], and mass fractions.
"""
def __get__(self):
self._check_property('SVY')
return self.s, self.v, self.Y
def __set__(self, values):
self._check_property('SVY')
self._check_setter('SVY')
assert len(values) == 3, 'incorrect number of values'
S = values[0] if values[0] is not None else self.s
V = values[1] if values[1] is not None else self.v
Expand Down

0 comments on commit ef3359a

Please sign in to comment.