Skip to content

Commit

Permalink
Fixed indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
sin-ha committed Apr 1, 2020
1 parent 8852375 commit f630bce
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
12 changes: 6 additions & 6 deletions interfaces/cython/cantera/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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"
Expand All @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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` """
Expand Down
11 changes: 10 additions & 1 deletion interfaces/cython/cantera/test/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit f630bce

Please sign in to comment.