-
-
Notifications
You must be signed in to change notification settings - Fork 410
Create SolutionBase object in C++ #696
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
Changes from 14 commits
0aa5723
bc04a96
8036169
bc6ff91
1168132
04dced2
b643a63
93d9bd6
ee484ee
ec6f5e0
cf29e6b
5932a24
02175a3
ce70714
891ce3e
9696e55
6190b4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| //! @file Solution.h | ||
|
|
||
| // 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. | ||
|
|
||
| #ifndef CT_SOLUTION_H | ||
| #define CT_SOLUTION_H | ||
|
|
||
| #include "cantera/base/ctexceptions.h" | ||
|
|
||
| namespace Cantera | ||
| { | ||
|
|
||
| class ThermoPhase; | ||
| class Kinetics; | ||
| class Transport; | ||
|
|
||
| //! A container class holding managers for all pieces defining a phase | ||
| class Solution : public std::enable_shared_from_this<Solution> | ||
| { | ||
| private: | ||
| Solution(); | ||
|
|
||
| public: | ||
| ~Solution() {} | ||
| Solution(const Solution&) = delete; | ||
| Solution& operator=(const Solution&) = delete; | ||
|
|
||
| static shared_ptr<Solution> create() { | ||
| return shared_ptr<Solution>( new Solution ); | ||
| } | ||
|
|
||
| //! Return the name of this Solution object | ||
| std::string name() const; | ||
|
|
||
| //! Set the name of this Solution object | ||
| void setName(const std::string& name); | ||
|
|
||
| //! Set the ThermoPhase object | ||
| void setThermoPhase(shared_ptr<ThermoPhase> thermo); | ||
|
|
||
| //! Set the Kinetics object | ||
| void setKinetics(shared_ptr<Kinetics> kinetics); | ||
|
|
||
| //! Set the Transport object | ||
| void setTransport(shared_ptr<Transport> transport); | ||
|
|
||
| //! Accessor for the ThermoPhase object | ||
| ThermoPhase& thermo() { | ||
| return *m_thermo; | ||
| } | ||
|
|
||
| //! Accessor for the Kinetics object | ||
| Kinetics& kinetics() { | ||
| return *m_kinetics; | ||
| } | ||
|
|
||
| //! Accessor for the Transport object | ||
| Transport& transport() { | ||
| return *m_transport; | ||
| } | ||
|
|
||
| protected: | ||
| shared_ptr<ThermoPhase> m_thermo; //! ThermoPhase manager | ||
| shared_ptr<Kinetics> m_kinetics; //! Kinetics manager | ||
| shared_ptr<Transport> m_transport; //! Transport manager | ||
|
ischoegl marked this conversation as resolved.
Outdated
|
||
| }; | ||
|
|
||
| } | ||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,40 @@ | ||
| # 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 collections import defaultdict as _defaultdict | ||
|
|
||
| cdef class _SolutionBase: | ||
| def __cinit__(self, infile='', phaseid='', phases=(), origin=None, | ||
| def __cinit__(self, infile='', name='', adjacent=(), origin=None, | ||
| source=None, yaml=None, thermo=None, species=(), | ||
| kinetics=None, reactions=(), **kwargs): | ||
|
|
||
| if 'phaseid' in kwargs: | ||
| if name is not '': | ||
| raise AttributeError('duplicate specification of phase name') | ||
|
|
||
| warnings.warn("Keyword 'name' replaces 'phaseid'", | ||
| FutureWarning) | ||
| name = kwargs['phaseid'] | ||
|
|
||
| if 'phases' in kwargs: | ||
| if len(adjacent)>0: | ||
| raise AttributeError( | ||
| 'duplicate specification of adjacent phases') | ||
|
|
||
| warnings.warn("Keyword 'adjacent' replaces 'phases'", | ||
| FutureWarning) | ||
| adjacent = kwargs['phases'] | ||
|
|
||
| # Shallow copy of an existing Solution (for slicing support) | ||
| cdef _SolutionBase other | ||
| if origin is not None: | ||
| other = <_SolutionBase?>origin | ||
|
|
||
| self.base = other.base | ||
| self.thermo = other.thermo | ||
| self.kinetics = other.kinetics | ||
| self.transport = other.transport | ||
| self._base = other._base | ||
| self._thermo = other._thermo | ||
| self._kinetics = other._kinetics | ||
| self._transport = other._transport | ||
|
|
@@ -21,25 +43,67 @@ cdef class _SolutionBase: | |
| self._selected_species = other._selected_species.copy() | ||
| return | ||
|
|
||
| # Assign base and set managers to NULL | ||
| self._base = CxxNewSolution() | ||
| self.base = self._base.get() | ||
| self.thermo = NULL | ||
| self.kinetics = NULL | ||
| self.transport = NULL | ||
|
|
||
| # Parse inputs | ||
| if infile.endswith('.yml') or infile.endswith('.yaml') or yaml: | ||
| self._init_yaml(infile, phaseid, phases, yaml) | ||
| self._init_yaml(infile, name, adjacent, yaml) | ||
| elif infile or source: | ||
| self._init_cti_xml(infile, phaseid, phases, source) | ||
| self._init_cti_xml(infile, name, adjacent, source) | ||
| elif thermo and species: | ||
| self._init_parts(thermo, species, kinetics, phases, reactions) | ||
| self._init_parts(thermo, species, kinetics, adjacent, reactions) | ||
| else: | ||
| raise ValueError("Arguments are insufficient to define a phase") | ||
|
|
||
| # Initialization of transport is deferred to Transport.__init__ | ||
| self.transport = NULL | ||
| self.base.setThermoPhase(self._thermo) | ||
| self.base.setKinetics(self._kinetics) | ||
|
|
||
| self._selected_species = np.ndarray(0, dtype=np.integer) | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| if isinstance(self, Transport): | ||
| assert self.transport is not NULL | ||
|
|
||
| def _init_yaml(self, infile, phaseid, phases, source): | ||
| phase_name = pystr(self.base.name()) | ||
| name = kwargs.get('name') | ||
| if name is not None: | ||
| self.name = name | ||
| else: | ||
| self.name = phase_name | ||
|
ischoegl marked this conversation as resolved.
Outdated
|
||
|
|
||
| property name: | ||
| """ | ||
| The name assigned to this object. The default value corresponds | ||
| to the CTI/XML/YAML input file phase entry. | ||
| """ | ||
| def __get__(self): | ||
| return pystr(self.base.name()) | ||
|
|
||
| def __set__(self, name): | ||
| self.base.setName(stringify(name)) | ||
|
|
||
| property composite: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can I suggest something along the lines of
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a tuple of types/models, so I'd prefer to leave it as is (an alternate would be To clarify,
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I see your point on that being a bit misleading. Just |
||
| """ | ||
| Returns tuple of thermo/kinetics/transport models associated with | ||
| this SolutionBase object. | ||
| """ | ||
|
ischoegl marked this conversation as resolved.
|
||
| def __get__(self): | ||
| thermo = None if self.thermo == NULL \ | ||
| else pystr(self.thermo.type()) | ||
| kinetics = None if self.kinetics == NULL \ | ||
| else pystr(self.kinetics.kineticsType()) | ||
| transport = None if self.transport == NULL \ | ||
| else pystr(self.transport.transportType()) | ||
|
|
||
| return thermo, kinetics, transport | ||
|
|
||
| def _init_yaml(self, infile, name, adjacent, source): | ||
| """ | ||
| Instantiate a set of new Cantera C++ objects from a YAML | ||
| phase definition | ||
|
|
@@ -51,7 +115,7 @@ cdef class _SolutionBase: | |
| root = AnyMapFromYamlString(stringify(source)) | ||
|
|
||
| phaseNode = root[stringify("phases")].getMapWhere(stringify("name"), | ||
| stringify(phaseid)) | ||
| stringify(name)) | ||
|
|
||
| # Thermo | ||
| if isinstance(self, ThermoPhase): | ||
|
|
@@ -66,15 +130,15 @@ cdef class _SolutionBase: | |
|
|
||
| if isinstance(self, Kinetics): | ||
| v.push_back(self.thermo) | ||
| for phase in phases: | ||
| for phase in adjacent: | ||
| # adjacent bulk phases for a surface phase | ||
| v.push_back(phase.thermo) | ||
| self._kinetics = newKinetics(v, phaseNode, root) | ||
| self.kinetics = self._kinetics.get() | ||
| else: | ||
| self.kinetics = NULL | ||
|
|
||
| def _init_cti_xml(self, infile, phaseid, phases, source): | ||
| def _init_cti_xml(self, infile, name, adjacent, source): | ||
| """ | ||
| Instantiate a set of new Cantera C++ objects from a CTI or XML | ||
| phase definition | ||
|
|
@@ -86,8 +150,8 @@ cdef class _SolutionBase: | |
|
|
||
| # Get XML data | ||
| cdef XML_Node* phaseNode | ||
| if phaseid: | ||
| phaseNode = rootNode.findID(stringify(phaseid)) | ||
| if name: | ||
| phaseNode = rootNode.findID(stringify(name)) | ||
| else: | ||
| phaseNode = rootNode.findByName(stringify('phase')) | ||
| if phaseNode is NULL: | ||
|
|
@@ -106,15 +170,15 @@ cdef class _SolutionBase: | |
|
|
||
| if isinstance(self, Kinetics): | ||
| v.push_back(self.thermo) | ||
| for phase in phases: | ||
| for phase in adjacent: | ||
| # adjacent bulk phases for a surface phase | ||
| v.push_back(phase.thermo) | ||
| self.kinetics = newKineticsMgr(deref(phaseNode), v) | ||
| self._kinetics.reset(self.kinetics) | ||
| else: | ||
| self.kinetics = NULL | ||
|
|
||
| def _init_parts(self, thermo, species, kinetics, phases, reactions): | ||
| def _init_parts(self, thermo, species, kinetics, adjacent, reactions): | ||
| """ | ||
| Instantiate a set of new Cantera C++ objects based on a string defining | ||
| the model type and a list of Species objects. | ||
|
|
@@ -136,7 +200,8 @@ cdef class _SolutionBase: | |
| self.kinetics = CxxNewKinetics(stringify(kinetics)) | ||
| self._kinetics.reset(self.kinetics) | ||
| self.kinetics.addPhase(deref(self.thermo)) | ||
| for phase in phases: | ||
| for phase in adjacent: | ||
| # adjacent bulk phases for a surface phase | ||
| self.kinetics.addPhase(deref(phase.thermo)) | ||
| self.kinetics.init() | ||
| self.kinetics.skipUndeclaredThirdBodies(True) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.