[SolutionArray] Add array size methods

This commit is contained in:
Bryan Weber
2022-05-17 17:00:02 -04:00
committed by Ingmar Schoegl
parent 3947980eb2
commit 532e60c92d
2 changed files with 31 additions and 0 deletions

View File

@@ -654,6 +654,33 @@ class SolutionArray:
return SolutionArray(self._phase[species], states=self._states,
extra=self._extra)
def __len__(self) -> int:
return self._shape[0]
@property
def ndim(self) -> int:
"""The number of dimensions in the SolutionArray.
:return: `int`
"""
return len(self.shape)
@property
def shape(self) -> tuple[int, ...]:
"""The shape of the SolutionArray.
:return: A tuple of integers with the number of elements in each dimension.
"""
return self._shape
@property
def size(self) -> int:
"""The number of elements in the SolutionArray.
:return: `int`
"""
return np.prod(self.shape)
def append(self, state=None, normalize=True, **kwargs):
"""
Append an element to the array with the specified state. Elements can

View File

@@ -240,6 +240,10 @@ class TestSolutionArrayIO(utilities.CanteraTest):
self.assertEqual(states[0].T, gas.T)
self.assertEqual(states[0].P, gas.P)
self.assertArrayNear(states[0].X, gas.X)
self.assertEqual(len(states), 1)
self.assertEqual(states.shape, (1,))
self.assertEqual(states.ndim, 1)
self.assertEqual(states.size, 1)
def test_append_no_norm_data(self):
gas = ct.Solution("h2o2.yaml")