Skip to content

Commit

Permalink
[base] Fix repeated slicing of SolutionArray
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl authored and speth committed Jun 26, 2024
1 parent bc24169 commit 90b4481
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/base/SolutionArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,19 @@ SolutionArray::SolutionArray(const SolutionArray& other,
, m_extra(other.m_extra)
, m_order(other.m_order)
, m_shared(true)
, m_active(selected)
{
m_sol->thermo()->addSpeciesLock();
if (!other.m_shared) {
// direct slicing is possible
m_active = selected;
} else {
// slicing a previously sliced SolutionArray
m_active.clear();
m_active.reserve(selected.size());
for (auto loc : selected) {
m_active.push_back(other.m_active.at(loc));
}
}
for (auto loc : m_active) {
if (loc < 0 || loc >= (int)m_dataSize) {
IndexError("SolutionArray::SolutionArray", "indices", loc, m_dataSize);
Expand Down
18 changes: 18 additions & 0 deletions test/python/test_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,24 @@ def test_from_state_array(self):
arr = ct.SolutionArray(self.gas, states=states)
assert arr.shape == (3, 5) # shape is based on numpy conversion

def test_slice_twice(self):
T_list = np.linspace(300, 1000, 8)
self.gas.TPX = T_list[0], ct.one_atm, {"H2": 1.}
arr = ct.SolutionArray(self.gas)
for T in T_list[1:]:
self.gas.TPX = T, ct.one_atm, {"H2": 1.}
arr.append(self.gas.state)
ix = 4
arr_trunc = arr[ix:]
assert arr_trunc.T[0] == arr.T[ix]
assert arr_trunc[0].T == arr.T[ix]
assert arr_trunc.T[-1] == arr.T[-1]
assert arr_trunc[-1].T == arr.T[-1]
assert (arr_trunc.T[:2] == arr.T[ix:ix+2]).all()
assert (arr_trunc[:2].T == arr.T[ix:ix+2]).all()
with self.assertRaises(IndexError):
arr_trunc[10]

def test_from_state_numpy(self):
states = np.array([[list(self.gas.state)] * 5] * 3)
arr = ct.SolutionArray(self.gas, states=states)
Expand Down

0 comments on commit 90b4481

Please sign in to comment.