Skip to content

Commit

Permalink
[SolutionArray] Disallow empty extra columns
Browse files Browse the repository at this point in the history
If the extra columns are passed to the SolutionArray, they must either
have initial values or the SolutionArray must be empty.
  • Loading branch information
bryanwweber authored and speth committed Sep 1, 2020
1 parent b05dddd commit 3f455e1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
8 changes: 6 additions & 2 deletions interfaces/cython/cantera/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,14 +544,18 @@ def __init__(self, phase, shape=(0,), states=None, extra=None, meta=None):
raise ValueError("Unable to map extra SolutionArray "
"input named {!r}".format(name))
elif extra is not None:
if self._shape != (0,):
raise ValueError("Initial values for extra properties must be "
"supplied in a dictionary if the SolutionArray "
"is not initially empty.")
if isinstance(extra, np.ndarray):
extra = extra.flatten()
elif isinstance(extra, str):
extra = [extra]

try:
iter_extra = iter(extra)
except TypeError :
except TypeError:
raise ValueError(
"Extra properties can be created by passing an iterable "
"of names for the properties. If you want to supply initial "
Expand All @@ -568,7 +572,7 @@ def __init__(self, phase, shape=(0,), states=None, extra=None, meta=None):
raise ValueError(
"Unable to create extra column '{}': name is already "
"used by SolutionArray objects.".format(name))
self._extra[name] = np.empty(self._shape)
self._extra[name] = np.empty(shape=(0,))

if meta is None:
self._meta = {}
Expand Down
17 changes: 13 additions & 4 deletions interfaces/cython/cantera/test/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,15 @@ def test_extra_no_shape(self):
self.assertEqual(states.prop.shape, (2, 2))
self.assertArrayNear(states.prop, np.array(((3, 3), (3, 3))))

def test_extra_not_empty(self):
"""Test that a non-empty SolutionArray raises a ValueError if
initial values for properties are not supplied.
"""
with self.assertRaisesRegex(ValueError, "Initial values for extra properties"):
ct.SolutionArray(self.gas, 3, extra=["prop"])
with self.assertRaisesRegex(ValueError, "Initial values for extra properties"):
ct.SolutionArray(self.gas, 3, extra=np.array(["prop", "prop2"]))

def test_extra_wrong_shape(self):
with self.assertRaisesRegex(ValueError, "Unable to map"):
ct.SolutionArray(self.gas, (3, 3), extra={"prop": np.arange(3)})
Expand Down Expand Up @@ -1784,10 +1793,10 @@ def test_assign_to_slice(self):

def test_extra_create_by_ndarray(self):
properties_array = np.array(["prop1", "prop2", "prop3"])
states = ct.SolutionArray(self.gas, shape=(2, 6, 9), extra=properties_array)
self.assertEqual(states.prop1.shape, (2, 6, 9))
self.assertEqual(states.prop2.shape, (2, 6, 9))
self.assertEqual(states.prop3.shape, (2, 6, 9))
states = ct.SolutionArray(self.gas, shape=(0,), extra=properties_array)
self.assertEqual(states.prop1.shape, (0,))
self.assertEqual(states.prop2.shape, (0,))
self.assertEqual(states.prop3.shape, (0,))
# Ensure that a 2-dimensional array is flattened
properties_array = np.array((["prop1"], ["prop2"]))
states = ct.SolutionArray(self.gas, extra=properties_array)
Expand Down

0 comments on commit 3f455e1

Please sign in to comment.