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

[Units] Add Cython interface for some functions #1285

Merged
merged 2 commits into from
May 30, 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
2 changes: 1 addition & 1 deletion include/cantera/base/Units.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Units
bool operator==(const Units& other) const;

//! Return dimension of primary unit component
//! ("mass", "length", "time", "temperature", "current" or "quantity")
//! ("mass", "length", "time", "temperature", "current", or "quantity")
double dimension(const std::string& primary) const;

private:
Expand Down
1 change: 1 addition & 0 deletions interfaces/cython/cantera/_cantera.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ cdef extern from "cantera/base/Units.h" namespace "Cantera":
CxxUnits(string, cbool) except +translate_exception
string str()
double factor()
double dimension(string) except +translate_exception

cdef cppclass CxxUnitSystem "Cantera::UnitSystem":
CxxUnitSystem()
Expand Down
4 changes: 2 additions & 2 deletions interfaces/cython/cantera/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ def ndim(self) -> int:
def shape(self) -> tuple[int, ...]:
"""The shape of the SolutionArray.

.. versionadded: 3.0
.. versionadded:: 3.0

:return: A tuple of integers with the number of elements in each dimension.
"""
Expand All @@ -680,7 +680,7 @@ def shape(self) -> tuple[int, ...]:
def size(self) -> int:
"""The number of elements in the SolutionArray.

.. versionadded: 3.0
.. versionadded:: 3.0
"""
return np.prod(self.shape)

Expand Down
4 changes: 2 additions & 2 deletions interfaces/cython/cantera/thermo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -732,10 +732,10 @@ cdef class ThermoPhase(_SolutionBase):
:param basis:
Determines if ``fuel`` and ``oxidizer`` are given in mole
fractions (``basis='mole'``) or mass fractions (``basis='mass'``)
:param: diluent:
:param diluent:
Optional parameter. Required if dilution is used. Specifies the composition
of the diluent in mole/mass fractions as a string, array or dict
:param: fraction:
:param fraction:
Optional parameter. Dilutes the fuel/oxidizer mixture with the diluent
according to ``fraction``. Fraction can refer to the fraction of diluent in
the mixture (for example ``fraction="diluent:0.7`` will create a mixture
Expand Down
37 changes: 35 additions & 2 deletions interfaces/cython/cantera/units.pyx
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 typing import Dict

cdef class Units:
"""
Expand All @@ -18,8 +19,40 @@ cdef class Units:
if name:
self.units = CxxUnits(stringify(name), True)

def __repr__(self):
return f"<Units({pystr(self.units.str())}) at {id(self):0x}>"
def __repr__(self) -> str:
return f"<Units({str(self)}) at {id(self):0x}>"

def __str__(self) -> str:
return pystr(self.units.str())

def dimension(self, primary: str) -> float:
"""The dimension of the given unit component.

.. versionadded:: 3.0

:param primary:
A string with the desired unit component. One of ``"mass"``,
``"length"``, ``"time"``, ``"temperature"``, ``"current"``, or
``"quantity"``.
"""
return self.units.dimension(stringify(primary))
bryanwweber marked this conversation as resolved.
Show resolved Hide resolved

@property
def dimensions(self) -> Dict[str, str]:
"""A dictionary of the primary unit components to their dimensions.

.. versionadded:: 3.0
"""
dimensions = ("mass", "length", "time", "temperature", "current", "quantity")
return {d: self.dimension(d) for d in dimensions}

@property
def factor(self) -> float:
"""The factor required to convert from this unit to Cantera's base units.

.. versionadded:: 3.0
"""
return self.units.factor()

@staticmethod
cdef copy(CxxUnits other):
Expand Down