Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SolutionArray] Add array size methods #1284

Merged
merged 2 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions interfaces/cython/cantera/composite.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# This file is part of Cantera. See License.txt in the top-level directory or
# at https://cantera.org/license.txt for license and copyright information.
from __future__ import annotations

from ._cantera import *
import numpy as np
Expand Down Expand Up @@ -654,6 +655,35 @@ def __call__(self, *species):
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.

.. versionadded:: 3.0
"""
return len(self.shape)

@property
def shape(self) -> tuple[int, ...]:
ischoegl marked this conversation as resolved.
Show resolved Hide resolved
"""The shape of the SolutionArray.

.. versionadded: 3.0

: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.

.. versionadded: 3.0
"""
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
Expand Down
4 changes: 4 additions & 0 deletions interfaces/cython/cantera/test/test_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ def test_append_state(self):
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")
Expand Down