Skip to content

Commit

Permalink
Removed empty initialisation of extra columns and formatting updates
Browse files Browse the repository at this point in the history
  • Loading branch information
sin-ha committed May 17, 2020
1 parent 191b2a6 commit 18a1c1f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
12 changes: 2 additions & 10 deletions interfaces/cython/cantera/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,10 +517,10 @@ def __init__(self, phase, shape=(0,), states=None, extra=None):

self._extra = {}

if isinstance(extra,str):
if isinstance(extra, str):
extra = [extra]

if isinstance(extra, (list,tuple,np.ndarray)) and all(isinstance(name, str) for name in extra):
if isinstance(extra, (list, tuple, np.ndarray)) and all(isinstance(name, str) for name in extra):
#if extra is a list and all elements in extra are of type str
for name in extra:
if name in reserved:
Expand All @@ -543,14 +543,6 @@ def __init__(self, phase, shape=(0,), states=None, extra=None):
raise ValueError("Unable to map extra SolutionArray"
"input for named {!r}".format(name))

elif extra and sum(self._shape) == 0:
for name in extra:
if name in reserved:
raise ValueError(
"Unable to create extra column '{}': name is already "
"used by SolutionArray objects.".format(name))
self._extra[name] = np.array([])

elif extra:
raise ValueError("Initial values for extra properties must be"
" supplied in a dict or can be initialised with the property string"
Expand Down
38 changes: 19 additions & 19 deletions interfaces/cython/cantera/test/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1644,36 +1644,36 @@ def test_slicing_ndim(self):
def test_extra(self):
with self.assertRaises(ValueError):
states = ct.SolutionArray(self.gas, extra=['creation_rates'])
states = ct.SolutionArray(self.gas, shape=(0,0), extra=("prop1"))
self.assertEqual(states.prop1.shape,(0,0))
states = ct.SolutionArray(self.gas, shape=(0, 0), extra=("prop1"))
self.assertEqual(states.prop1.shape, (0, 0))

# test indexed operations on extra arrays
states = ct.SolutionArray(self.gas, 7, extra={'n': range(7)})
states = ct.SolutionArray(self.gas, 7, extra={'prop': range(7)})
array = np.array(range(7))
self.assertArrayNear(states.n , array)
states.n[1] = -5
states.n[3:5] = [0,1]
array_mod = np.array([ 0, -5, 2, 0, 1, 5, 6])
self.assertArrayNear(states.n ,array_mod)
self.assertArrayNear(states.prop, array)
states.prop[1] = -5
states.prop[3:5] = [0, 1]
array_mod = np.array([0, -5, 2, 0, 1, 5, 6])
self.assertArrayNear(states.prop, array_mod)

#test initialaisation of extra when a list is passed
states = ct.SolutionArray(self.gas, shape=(5,9), extra=["prop1","prop2"])
self.assertEqual(states.prop1.shape,(5,9))
states1 = ct.SolutionArray(self.gas, shape=(0,0,0), extra="prop1")
self.assertEqual(states1.prop1.shape,(0,0,0))
states = ct.SolutionArray(self.gas, shape=(5, 9), extra=["prop1", "prop2"])
self.assertEqual(states.prop1.shape, (5, 9))
states1 = ct.SolutionArray(self.gas, shape=(0, 0, 0), extra="prop1")
self.assertEqual(states1.prop1.shape, (0, 0, 0))

#test initialisation with string
states2 = ct.SolutionArray(self.gas, shape=(2,6,9), extra="prop1")
self.assertEqual(states2.prop1.shape,(2,6,9))
states2 = ct.SolutionArray(self.gas, shape=(2, 6, 9), extra="prop1")
self.assertEqual(states2.prop1.shape, (2, 6, 9))

#test initialisation with tuples
states3 = ct.SolutionArray(self.gas, shape=(2,6,9), extra=("prop1","prop2","prop3"))
self.assertEqual(states3.prop3.shape,(2,6,9))
states3 = ct.SolutionArray(self.gas, shape=(2, 6, 9), extra=("prop1", "prop2", "prop3"))
self.assertEqual(states3.prop3.shape, (2, 6, 9))

#test initialisation with numpy.ndarray
properties_array = np.array(["prop1","prop2","prop3"])
states4 = ct.SolutionArray(self.gas, shape=(2,6,9), extra=properties_array)
self.assertEqual(states4.prop2.shape,(2,6,9))
properties_array = np.array(["prop1", "prop2", "prop3"])
states4 = ct.SolutionArray(self.gas, shape=(2, 6, 9), extra=properties_array)
self.assertEqual(states4.prop2.shape, (2, 6, 9))


def test_append(self):
Expand Down

0 comments on commit 18a1c1f

Please sign in to comment.