From 0a48784564a0cfad3add2b450a1ecd016e17a243 Mon Sep 17 00:00:00 2001 From: "Bryan W. Weber" Date: Fri, 21 Aug 2020 20:07:05 -0400 Subject: [PATCH] [SolutionArray] Disallow empty extra columns If the extra columns are passed to the SolutionArray, they must either have initial values or the SolutionArray must be empty. --- interfaces/cython/cantera/composite.py | 4 ++++ interfaces/cython/cantera/test/test_thermo.py | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/interfaces/cython/cantera/composite.py b/interfaces/cython/cantera/composite.py index 8e3e801e9d5..df9ca7f1db5 100644 --- a/interfaces/cython/cantera/composite.py +++ b/interfaces/cython/cantera/composite.py @@ -543,6 +543,10 @@ 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): diff --git a/interfaces/cython/cantera/test/test_thermo.py b/interfaces/cython/cantera/test/test_thermo.py index df5e93dcc65..508735f01d1 100644 --- a/interfaces/cython/cantera/test/test_thermo.py +++ b/interfaces/cython/cantera/test/test_thermo.py @@ -1696,6 +1696,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)}) @@ -1735,10 +1744,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)