[Python] Use shared_ptr for C++ objects owned by Solution

This commit is contained in:
Ray Speth 2019-01-05 19:16:28 -05:00
parent be4d9cbc55
commit 8a0eed3be5
3 changed files with 13 additions and 17 deletions

View file

@ -891,13 +891,15 @@ cdef class GasTransportData:
cdef _assign(self, shared_ptr[CxxTransportData] other)
cdef class _SolutionBase:
cdef shared_ptr[CxxThermoPhase] _thermo
cdef CxxThermoPhase* thermo
cdef shared_ptr[CxxKinetics] _kinetics
cdef CxxKinetics* kinetics
cdef shared_ptr[CxxTransport] _transport
cdef CxxTransport* transport
cdef int thermo_basis
cdef np.ndarray _selected_species
cdef object parent
cdef cbool is_slice
cdef class ThermoPhase(_SolutionBase):
cdef double _mass_factor(self)

View file

@ -10,21 +10,17 @@ cdef class _SolutionBase:
if origin is not None:
other = <_SolutionBase?>origin
# keep a reference to the parent to prevent the underlying
# C++ objects from being deleted
self.parent = origin
self.is_slice = True
self.thermo = other.thermo
self.kinetics = other.kinetics
self.transport = other.transport
self._thermo = other._thermo
self._kinetics = other._kinetics
self._transport = other._transport
self.thermo_basis = other.thermo_basis
self._selected_species = other._selected_species.copy()
return
self.is_slice = False
if infile or source:
self._init_cti_xml(infile, phaseid, phases, source)
elif thermo and species:
@ -63,6 +59,7 @@ cdef class _SolutionBase:
# Thermo
if isinstance(self, ThermoPhase):
self.thermo = newPhase(deref(phaseNode))
self._thermo.reset(self.thermo)
else:
self.thermo = NULL
@ -76,6 +73,7 @@ cdef class _SolutionBase:
# 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
@ -85,6 +83,7 @@ cdef class _SolutionBase:
the model type and a list of Species objects.
"""
self.thermo = newThermoPhase(stringify(thermo))
self._thermo.reset(self.thermo)
self.thermo.addUndefinedElements()
cdef Species S
for S in species:
@ -98,6 +97,7 @@ cdef class _SolutionBase:
cdef Reaction reaction
if isinstance(self, Kinetics):
self.kinetics = CxxNewKinetics(stringify(kinetics))
self._kinetics.reset(self.kinetics)
self.kinetics.addPhase(deref(self.thermo))
for phase in phases:
self.kinetics.addPhase(deref(phase.thermo))
@ -131,10 +131,3 @@ cdef class _SolutionBase:
def __copy__(self):
raise NotImplementedError('Solution object is not copyable')
def __dealloc__(self):
# only delete the C++ objects if this is the parent object
if not self.is_slice:
del self.thermo
del self.kinetics
del self.transport

View file

@ -143,6 +143,7 @@ cdef class Transport(_SolutionBase):
if not model:
model = 'None'
self.transport = newTransportMgr(stringify(model), self.thermo)
self._transport.reset(self.transport)
super().__init__(*args, **kwargs)
property transport_model:
@ -156,9 +157,8 @@ cdef class Transport(_SolutionBase):
return pystr(self.transport.transportType())
def __set__(self, model):
cdef CxxTransport* old = self.transport
self.transport = newTransportMgr(stringify(model), self.thermo)
del old # only if the new transport manager was successfully created
self._transport.reset(self.transport)
property viscosity:
"""Viscosity [Pa-s]."""
@ -242,6 +242,7 @@ cdef class DustyGasTransport(Transport):
# The signature of this function causes warnings for Sphinx documentation
def __init__(self, *args, **kwargs):
self.transport = newTransportMgr(stringify("DustyGas"), self.thermo)
self._transport.reset(self.transport)
super().__init__(*args, **kwargs)
property porosity: