From ba8c906cd866a9be9e897576336b6f3ad051433b Mon Sep 17 00:00:00 2001 From: "Bryan W. Weber" Date: Tue, 22 Dec 2015 16:46:58 -0500 Subject: [PATCH] [Cython] Add interface to new Element information functions --- interfaces/cython/cantera/_cantera.pxd | 10 +++ interfaces/cython/cantera/thermo.pyx | 96 ++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/interfaces/cython/cantera/_cantera.pxd b/interfaces/cython/cantera/_cantera.pxd index 4fc96a49e..846e6b974 100644 --- a/interfaces/cython/cantera/_cantera.pxd +++ b/interfaces/cython/cantera/_cantera.pxd @@ -995,3 +995,13 @@ cdef np.ndarray get_transport_2d(Transport tran, transportMethod2d method) cdef CxxIdealGasPhase* getIdealGasPhase(ThermoPhase phase) except * cdef wrapSpeciesThermo(shared_ptr[CxxSpeciesThermo] spthermo) cdef Reaction wrapReaction(shared_ptr[CxxReaction] reaction) + +cdef extern from "cantera/thermo/Elements.h" namespace "Cantera": + double getElementWeight(string ename) except + + double getElementWeight(int atomicNumber) except + + int numElementsDefined() + int getAtomicNumber(string ename) except + + string getElementSymbol(string ename) except + + string getElementSymbol(int atomicNumber) except + + string getElementName(string ename) except + + string getElementName(int atomicNumber) except + diff --git a/interfaces/cython/cantera/thermo.pyx b/interfaces/cython/cantera/thermo.pyx index da7d1a1c8..55e722635 100644 --- a/interfaces/cython/cantera/thermo.pyx +++ b/interfaces/cython/cantera/thermo.pyx @@ -1267,3 +1267,99 @@ cdef class PureFluid(ThermoPhase): """ def __get__(self): return self.s, self.v, self.X + + +class Element(object): + """ + An element or a named isotope defined in Cantera. + + Class `Element` gets data for the elements and isotopes defined in + `src/thermo/Elements.cpp`. This class can be used in two ways. The + first way is to get information about all of the elements stored in + Cantera. The three attributes `num_elements_defined`, + `element_symbols`, and `element_names` can be accessed by:: + + >>> ct.Element.num_elements_defined + >>> ct.Element.element_symbols + >>> ct.Element.element_names + + Otherwise, if the class `Element` is called with an argument, it + stores the data about that particular element. For example:: + + >>> ar_sym = ct.Element('Ar') + >>> ar_name = ct.Element('argon') + >>> ar_num = ct.Element(18) + + would all create instances with the information for argon. The + available argument options to create an instance of the `Element` + class with the element information are the `name`, `symbol`, and + `atomic_number`. Once an instance of the class is made, the `name`, + `atomic_number`, `symbol`, and atomic `weight` can be accessed as + attributes of the instance of the `Element` class. + + >>> ar_sym.name + 'argon' + >>> ar_sym.weight + 39.948 + >>> ar_sym.atomic_number + 18 + >>> ar_sym.symbol + 'Ar' + + The elements available are listed below, in the `element_symbols` + and `element_names` attribute documentation. + """ + + #: The number of named elements (not isotopes) defined in Cantera + num_elements_defined = numElementsDefined() + + #: A list of the symbols of all the elements (not isotopes) defined + #: in Cantera + element_symbols = [getElementSymbol((m+1)) + for m in range(num_elements_defined)] + + #: A list of the names of all the elements (not isotopes) defined + #: in Cantera + element_names = [getElementName(m+1) + for m in range(num_elements_defined)] + + def __init__(self, arg): + if isinstance(arg, (str, unicode, bytes)): + try: + self._name = getElementName(stringify(arg)) + except RuntimeError: + self._symbol = getElementSymbol(stringify(arg)) + self._name = arg + else: + self._symbol = arg + + self._atomic_number = getAtomicNumber(stringify(arg)) + self._weight = getElementWeight(stringify(arg)) + elif isinstance(arg, int): + self._atomic_number = arg + self._name = getElementName(arg) + self._symbol = getElementSymbol(arg) + self._weight = getElementWeight(arg) + else: + raise TypeError('The input argument to Element must be a string ' + 'or an integer') + + @property + def name(self): + """The name of the element or isotope.""" + return self._name + + @property + def atomic_number(self): + """The atomic number of the element or isotope.""" + return self._atomic_number + + @property + def symbol(self): + """The symbol of the element or isotope.""" + return self._symbol + + @property + def weight(self): + """The atomic weight of the element or isotope.""" + return self._weight