Skip to content

Commit

Permalink
[Units] Add Cython interface for some functions
Browse files Browse the repository at this point in the history
These functions are chosen for their potential utility. Setting units is
not enabled by this interface because it is meant to be handled
internally. However, these methods can help to determine the unit
factors for reaction rates.
  • Loading branch information
bryanwweber committed May 26, 2022
1 parent 790b507 commit 798dd19
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
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
24 changes: 24 additions & 0 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 @@ -21,6 +22,29 @@ cdef class Units:
def __repr__(self):
return f"<Units({pystr(self.units.str())}) at {id(self):0x}>"

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

def dimension(self, primary):
return self.units.dimension(stringify(primary))

@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):
"""Copy a C++ Units object to a Python object."""
Expand Down

0 comments on commit 798dd19

Please sign in to comment.