From f630bce74ac65e05971fb51da144b568ae39010f Mon Sep 17 00:00:00 2001 From: sin-ha Date: Wed, 1 Apr 2020 17:27:48 +0530 Subject: [PATCH] Fixed indexing --- interfaces/cython/cantera/composite.py | 12 ++++++------ interfaces/cython/cantera/test/test_thermo.py | 11 ++++++++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/interfaces/cython/cantera/composite.py b/interfaces/cython/cantera/composite.py index 2c126e08a08..415254c71b2 100644 --- a/interfaces/cython/cantera/composite.py +++ b/interfaces/cython/cantera/composite.py @@ -523,9 +523,9 @@ def __init__(self, phase, shape=(0,), states=None, extra=None): "Unable to create extra column '{}': name is already " "used by SolutionArray objects.".format(name)) if not np.shape(v): - self._extra[name] = [v]*self._shape[0] + self._extra[name] = np.array([v]*self._shape[0]) elif len(v) == self._shape[0]: - self._extra[name] = list(v) + self._extra[name] = np.array(list(v)) else: raise ValueError("Unable to map extra SolutionArray" "input for named {!r}".format(name)) @@ -536,7 +536,7 @@ def __init__(self, phase, shape=(0,), states=None, extra=None): raise ValueError( "Unable to create extra column '{}': name is already " "used by SolutionArray objects.".format(name)) - self._extra[name] = [] + self._extra[name] = np.array([]) elif extra: raise ValueError("Initial values for extra properties must be" @@ -555,7 +555,7 @@ def __getitem__(self, index): def __getattr__(self, name): if name in self._extra: - return np.array(self._extra[name]) + return self._extra[name] else: raise AttributeError("'{}' object has no attribute '{}'".format( self.__class__.__name__, name)) @@ -590,7 +590,7 @@ def append(self, state=None, **kwargs): raise IndexError("Can only append to 1D SolutionArray") for name, value in self._extra.items(): - value.append(kwargs.pop(name)) + np.append(value,kwargs.pop(name)) if state is not None: self._phase.state = state @@ -628,7 +628,7 @@ def sort(self, col, reverse=False): indices = indices[::-1] self._states = [self._states[ix] for ix in indices] for k, v in self._extra.items(): - self._extra[k] = list(np.array(v)[indices]) + self._extra[k] = np.array(v)[indices] def equilibrate(self, *args, **kwargs): """ See `ThermoPhase.equilibrate` """ diff --git a/interfaces/cython/cantera/test/test_thermo.py b/interfaces/cython/cantera/test/test_thermo.py index 16ba5f6b507..a5ae53f4653 100644 --- a/interfaces/cython/cantera/test/test_thermo.py +++ b/interfaces/cython/cantera/test/test_thermo.py @@ -1644,7 +1644,16 @@ def test_slicing_ndim(self): def test_extra(self): with self.assertRaises(ValueError): states = ct.SolutionArray(self.gas, extra=['creation_rates']) - + + # test indexed operations on extra arrays + states = ct.SolutionArray(self.gas, 7, extra={'n': 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) + def test_append(self): states = ct.SolutionArray(self.gas, 5) states.TPX = np.linspace(500, 1000, 5), 2e5, 'H2:0.5, O2:0.4'