mirror of
https://github.com/Cantera/cantera.git
synced 2025-02-25 18:55:29 -06:00
[Input] Refactor to reduce Python manipulations of AnyMap
Eliminates the 'mergeAnyMap' function, and introduces a 'parameters' method for classes to return an AnyMap which can optionally contain the user-provided input data, rather than needing to create the AnyMap in advance and add the user-created fields separately.
This commit is contained in:
committed by
Ingmar Schoegl
parent
df262a0f92
commit
716e397c2e
@@ -125,8 +125,7 @@ cdef extern from "cantera/thermo/SpeciesThermoInterpType.h":
|
||||
double refPressure()
|
||||
void reportParameters(size_t&, int&, double&, double&, double&, double* const) except +translate_exception
|
||||
int nCoeffs() except +translate_exception
|
||||
void getParameters(CxxAnyMap&) except +translate_exception
|
||||
CxxAnyMap& input()
|
||||
CxxAnyMap parameters(cbool) except +translate_exception
|
||||
|
||||
cdef extern from "cantera/thermo/SpeciesThermoFactory.h":
|
||||
cdef CxxSpeciesThermo* CxxNewSpeciesThermo "Cantera::newSpeciesThermoInterpType"\
|
||||
@@ -145,8 +144,7 @@ cdef extern from "cantera/thermo/Species.h" namespace "Cantera":
|
||||
Composition composition
|
||||
double charge
|
||||
double size
|
||||
void getParameters(CxxAnyMap&) except +translate_exception
|
||||
CxxAnyMap input
|
||||
CxxAnyMap parameters(CxxThermoPhase*) except +translate_exception
|
||||
|
||||
cdef shared_ptr[CxxSpecies] CxxNewSpecies "newSpecies" (XML_Node&)
|
||||
cdef vector[shared_ptr[CxxSpecies]] CxxGetSpecies "getSpecies" (XML_Node&)
|
||||
@@ -162,6 +160,7 @@ cdef extern from "cantera/base/Solution.h" namespace "Cantera":
|
||||
void setThermo(shared_ptr[CxxThermoPhase])
|
||||
void setKinetics(shared_ptr[CxxKinetics])
|
||||
void setTransport(shared_ptr[CxxTransport])
|
||||
CxxAnyMap parameters(cbool) except +translate_exception
|
||||
|
||||
cdef shared_ptr[CxxSolution] CxxNewSolution "Cantera::Solution::create" ()
|
||||
|
||||
@@ -178,8 +177,6 @@ cdef extern from "cantera/thermo/ThermoPhase.h" namespace "Cantera":
|
||||
# miscellaneous
|
||||
string type()
|
||||
string phaseOfMatter() except +translate_exception
|
||||
CxxAnyMap& input()
|
||||
void getParameters(CxxAnyMap&) except +translate_exception
|
||||
void getSpeciesParameters(string, CxxAnyMap&) except +translate_exception
|
||||
string report(cbool, double) except +translate_exception
|
||||
cbool hasPhaseTransition()
|
||||
@@ -369,7 +366,7 @@ cdef extern from "cantera/kinetics/Reaction.h" namespace "Cantera":
|
||||
string equation()
|
||||
string type()
|
||||
void validate() except +translate_exception
|
||||
void getParameters(CxxAnyMap&) except +translate_exception
|
||||
CxxAnyMap parameters(cbool) except +translate_exception
|
||||
int reaction_type
|
||||
Composition reactants
|
||||
Composition products
|
||||
@@ -379,7 +376,6 @@ cdef extern from "cantera/kinetics/Reaction.h" namespace "Cantera":
|
||||
cbool duplicate
|
||||
cbool allow_nonreactant_orders
|
||||
cbool allow_negative_orders
|
||||
CxxAnyMap input
|
||||
|
||||
cdef cppclass CxxReaction2 "Cantera::Reaction2" (CxxReaction):
|
||||
CxxReaction2()
|
||||
@@ -491,7 +487,6 @@ cdef extern from "cantera/kinetics/Kinetics.h" namespace "Cantera":
|
||||
void addReaction(shared_ptr[CxxReaction]) except +translate_exception
|
||||
void modifyReaction(int, shared_ptr[CxxReaction]) except +translate_exception
|
||||
void invalidateCache() except +translate_exception
|
||||
void getParameters(CxxAnyMap&) except +translate_exception
|
||||
|
||||
shared_ptr[CxxReaction] reaction(size_t) except +translate_exception
|
||||
cbool isReversible(int) except +translate_exception
|
||||
@@ -517,7 +512,6 @@ cdef extern from "cantera/transport/TransportBase.h" namespace "Cantera":
|
||||
cdef cppclass CxxTransport "Cantera::Transport":
|
||||
CxxTransport(CxxThermoPhase*)
|
||||
string transportType()
|
||||
void getParameters(CxxAnyMap&) except +translate_exception
|
||||
double viscosity() except +translate_exception
|
||||
double thermalConductivity() except +translate_exception
|
||||
double electricalConductivity() except +translate_exception
|
||||
@@ -537,8 +531,7 @@ cdef extern from "cantera/transport/DustyGasTransport.h" namespace "Cantera":
|
||||
cdef extern from "cantera/transport/TransportData.h" namespace "Cantera":
|
||||
cdef cppclass CxxTransportData "Cantera::TransportData":
|
||||
CxxTransportData()
|
||||
CxxAnyMap input
|
||||
void getParameters(CxxAnyMap&) except +translate_exception
|
||||
CxxAnyMap parameters(cbool) except +translate_exception
|
||||
|
||||
cdef cppclass CxxGasTransportData "Cantera::GasTransportData" (CxxTransportData):
|
||||
CxxGasTransportData()
|
||||
|
||||
@@ -230,14 +230,7 @@ cdef class _SolutionBase:
|
||||
definition.
|
||||
"""
|
||||
def __get__(self):
|
||||
cdef CxxAnyMap params
|
||||
if self.thermo:
|
||||
self.thermo.getParameters(params)
|
||||
if self.kinetics:
|
||||
self.kinetics.getParameters(params)
|
||||
if self.transport:
|
||||
self.transport.getParameters(params)
|
||||
return mergeAnyMap(params, self.thermo.input())
|
||||
return anymapToPython(self.base.parameters(True))
|
||||
|
||||
def write_yaml(self, filename, phases=None, units=None, precision=None,
|
||||
skip_user_defined=None):
|
||||
|
||||
@@ -327,9 +327,7 @@ cdef class Reaction:
|
||||
definition.
|
||||
"""
|
||||
def __get__(self):
|
||||
cdef CxxAnyMap params
|
||||
self.reaction.getParameters(params)
|
||||
return mergeAnyMap(params, self.reaction.input)
|
||||
return anymapToPython(self.reaction.parameters(True))
|
||||
|
||||
def __repr__(self):
|
||||
return '<{}: {}>'.format(self.__class__.__name__, self.equation)
|
||||
|
||||
@@ -88,9 +88,7 @@ cdef class SpeciesThermo:
|
||||
|
||||
property input_data:
|
||||
def __get__(self):
|
||||
cdef CxxAnyMap params
|
||||
self.spthermo.getParameters(params)
|
||||
return mergeAnyMap(params, self.spthermo.input())
|
||||
return anymapToPython(self.spthermo.parameters(True))
|
||||
|
||||
def cp(self, T):
|
||||
"""
|
||||
|
||||
@@ -264,11 +264,8 @@ cdef class Species:
|
||||
|
||||
property input_data:
|
||||
def __get__(self):
|
||||
cdef CxxAnyMap params
|
||||
self.species.getParameters(params)
|
||||
if self._phase:
|
||||
self._phase.thermo.getSpeciesParameters(self.species.name, params)
|
||||
return mergeAnyMap(params, self.species.input)
|
||||
cdef CxxThermoPhase* phase = self._phase.thermo if self._phase else NULL
|
||||
return anymapToPython(self.species.parameters(phase))
|
||||
|
||||
def __repr__(self):
|
||||
return '<Species {}>'.format(self.name)
|
||||
|
||||
@@ -58,9 +58,7 @@ cdef class GasTransportData:
|
||||
|
||||
property input_data:
|
||||
def __get__(self):
|
||||
cdef CxxAnyMap params
|
||||
self.data.getParameters(params)
|
||||
return mergeAnyMap(params, self.data.input)
|
||||
return anymapToPython(self.data.parameters(True))
|
||||
|
||||
property geometry:
|
||||
"""
|
||||
|
||||
@@ -119,19 +119,3 @@ cdef anymapToPython(CxxAnyMap& m):
|
||||
m.applyUnits()
|
||||
return {pystr(item.first): anyvalueToPython(item.first, item.second)
|
||||
for item in m.ordered()}
|
||||
|
||||
|
||||
cdef mergeAnyMap(CxxAnyMap& primary, CxxAnyMap& extra):
|
||||
"""
|
||||
Combine two AnyMaps into a single Python dict. Items from the second map
|
||||
are included only if there is no corresponding key in the first map.
|
||||
|
||||
Used to combine generated data representing the current state of the object
|
||||
(primary) with user-supplied fields (extra) not directly used by Cantera.
|
||||
"""
|
||||
out = {pystr(item.first): anyvalueToPython(item.first, item.second)
|
||||
for item in primary.ordered()}
|
||||
for item in extra:
|
||||
if not primary.hasKey(item.first):
|
||||
out[pystr(item.first)] = anyvalueToPython(item.first, item.second)
|
||||
return out
|
||||
|
||||
Reference in New Issue
Block a user