From b3dd2d1e91a8690c7e254a70c7f6be1f4a4167cd Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Fri, 23 Aug 2024 08:54:59 -0500 Subject: [PATCH] [zeroD] Move syncState to ReactorNode --- include/cantera/zeroD/ReactorBase.h | 11 +++------- include/cantera/zeroD/ReactorNode.h | 9 +++++++++ include/cantera/zeroD/ReactorSurface.h | 2 +- interfaces/cython/cantera/reactor.pxd | 6 +----- interfaces/cython/cantera/reactor.pyx | 20 ++++++++++++++++--- .../matlab_experimental/Utility/ctRoot.m | 2 +- samples/python/reactors/pfr.py | 2 +- samples/python/reactors/surf_pfr_chain.py | 2 +- src/zeroD/ReactorNode.cpp | 12 +++++++++++ test/python/test_reactor.py | 10 +++++----- 10 files changed, 51 insertions(+), 25 deletions(-) diff --git a/include/cantera/zeroD/ReactorBase.h b/include/cantera/zeroD/ReactorBase.h index cacea1d016e..c5e51ba04e3 100644 --- a/include/cantera/zeroD/ReactorBase.h +++ b/include/cantera/zeroD/ReactorBase.h @@ -148,14 +148,9 @@ class ReactorBase : public ReactorNode //! @} - //! Set the state of the Phase object associated with this reactor to the - //! reactor's current state. - void restoreState(); - - //! Set the state of the reactor to correspond to the state of the - //! associated ThermoPhase object. This is the inverse of restoreState(). - //! Calling this will trigger integrator reinitialization. - virtual void syncState(); + void restoreState() override; + + void syncState() override; //! return a reference to the contents. ThermoPhase& contents() { diff --git a/include/cantera/zeroD/ReactorNode.h b/include/cantera/zeroD/ReactorNode.h index adcd62ef1dd..01be97b690f 100644 --- a/include/cantera/zeroD/ReactorNode.h +++ b/include/cantera/zeroD/ReactorNode.h @@ -53,6 +53,15 @@ class ReactorNode //! Set the default name of a reactor. Returns `false` if it was previously set. bool setDefaultName(map& counts); + //! Set the state of the Phase object associated with this reactor to the + //! reactor's current state. + virtual void restoreState(); + + //! Set the state of the reactor to correspond to the state of the + //! associated ThermoPhase object. This is the inverse of restoreState(). + //! Calling this will trigger integrator reinitialization. + virtual void syncState(); + protected: //! Composite thermo/kinetics handler. shared_ptr m_solution; diff --git a/include/cantera/zeroD/ReactorSurface.h b/include/cantera/zeroD/ReactorSurface.h index 7942c92a8dc..fb52ae73a49 100644 --- a/include/cantera/zeroD/ReactorSurface.h +++ b/include/cantera/zeroD/ReactorSurface.h @@ -72,7 +72,7 @@ class ReactorSurface : public ReactorNode //! Set the coverages and temperature in the surface phase object to the //! values for this surface. The temperature is set to match the bulk phase //! of the attached Reactor. - void syncState(); + void syncState() override; //! Enable calculation of sensitivities with respect to the rate constant //! for reaction `i`. diff --git a/interfaces/cython/cantera/reactor.pxd b/interfaces/cython/cantera/reactor.pxd index 8f649683901..85f52460dbf 100644 --- a/interfaces/cython/cantera/reactor.pxd +++ b/interfaces/cython/cantera/reactor.pxd @@ -40,13 +40,13 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera": string type() string name() void setName(string) + void syncState() except +translate_exception # reactors cdef cppclass CxxReactorBase "Cantera::ReactorBase" (CxxReactorNode): CxxReactorBase() void setSolution(shared_ptr[CxxSolution]) except +translate_exception void restoreState() except +translate_exception - void syncState() except +translate_exception double volume() void setInitialVolume(double) @@ -92,7 +92,6 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera": void setArea(double) void setCoverages(double*) void setCoverages(Composition&) except +translate_exception - void syncState() void addSensitivityReaction(size_t) except +translate_exception size_t nSensParams() @@ -109,9 +108,6 @@ cdef extern from "cantera/zerodim.h" namespace "Cantera": CxxWallBase() double area() void setArea(double) - void setCoverages(int, double*) - void setCoverages(int, Composition&) except +translate_exception - void syncCoverages(int) double expansionRate() except +translate_exception double vdot(double) except +translate_exception double heatRate() except +translate_exception diff --git a/interfaces/cython/cantera/reactor.pyx b/interfaces/cython/cantera/reactor.pyx index 27704e00e7e..b4bed139c37 100644 --- a/interfaces/cython/cantera/reactor.pyx +++ b/interfaces/cython/cantera/reactor.pyx @@ -42,6 +42,14 @@ cdef class ReactorNode: def name(self, name): self.node.setName(stringify(name)) + def sync_state(self): + """ + Set the state of the Reactor to match that of the associated `ThermoPhase` + object. After calling sync_state(), call ReactorNet.reinitialize() before + further integration. + """ + self.node.syncState() + def __reduce__(self): raise NotImplementedError('Reactor object is not picklable') @@ -90,8 +98,14 @@ cdef class ReactorBase(ReactorNode): Set the state of the Reactor to match that of the associated `ThermoPhase` object. After calling syncState(), call ReactorNet.reinitialize() before further integration. + + .. deprecated:: 3.1 + + To be removed after Cantera 3.1; renamed to sync_state. """ - self.rbase.syncState() + warnings.warn("Renamed to sync_state; to be removed after Cantera 3.1.", + DeprecationWarning) + self.sync_state() property thermo: """The `ThermoPhase` object representing the reactor's contents.""" @@ -863,7 +877,7 @@ cdef class ReactorSurface(ReactorNode): this surface. """ def __get__(self): - self.surface.syncState() + self.sync_state() return self._kinetics property coverages: @@ -873,7 +887,7 @@ cdef class ReactorSurface(ReactorNode): def __get__(self): if self._kinetics is None: raise CanteraError('No kinetics manager present') - self.surface.syncState() + self.sync_state() return self._kinetics.coverages def __set__(self, coverages): if self._kinetics is None: diff --git a/interfaces/matlab_experimental/Utility/ctRoot.m b/interfaces/matlab_experimental/Utility/ctRoot.m index c098916899a..fb6f00dbd32 100644 --- a/interfaces/matlab_experimental/Utility/ctRoot.m +++ b/interfaces/matlab_experimental/Utility/ctRoot.m @@ -1,3 +1,3 @@ function output = ctRoot() - output = ''; + output = '/opt/homebrew/Caskroom/miniforge/base/envs/cantera-dev'; end diff --git a/samples/python/reactors/pfr.py b/samples/python/reactors/pfr.py index d59ca382241..45736aade32 100644 --- a/samples/python/reactors/pfr.py +++ b/samples/python/reactors/pfr.py @@ -126,7 +126,7 @@ for n in range(n_steps): # Set the state of the reservoir to match that of the previous reactor gas2.TDY = r2.thermo.TDY - upstream.syncState() + upstream.sync_state() # integrate the reactor forward in time until steady state is reached sim2.reinitialize() sim2.advance_to_steady_state() diff --git a/samples/python/reactors/surf_pfr_chain.py b/samples/python/reactors/surf_pfr_chain.py index d076441d7a3..bb41938cdc5 100644 --- a/samples/python/reactors/surf_pfr_chain.py +++ b/samples/python/reactors/surf_pfr_chain.py @@ -106,7 +106,7 @@ for n in range(NReactors): # Set the state of the reservoir to match that of the previous reactor gas.TDY = r.thermo.TDY - upstream.syncState() + upstream.sync_state() sim.reinitialize() sim.advance_to_steady_state() dist = n * rlen * 1.0e3 # distance in mm diff --git a/src/zeroD/ReactorNode.cpp b/src/zeroD/ReactorNode.cpp index 6356a1e4dd0..7cd70140207 100644 --- a/src/zeroD/ReactorNode.cpp +++ b/src/zeroD/ReactorNode.cpp @@ -44,4 +44,16 @@ bool ReactorNode::setDefaultName(map& counts) return true; } +void ReactorNode::restoreState() +{ + throw NotImplementedError("ReactorNode::restoreState", + "Method needs to be overloaded."); +} + +void ReactorNode::syncState() +{ + throw NotImplementedError("ReactorNode::syncState", + "Method needs to be overloaded."); +} + } diff --git a/test/python/test_reactor.py b/test/python/test_reactor.py index 2ad578870e0..54ccd88772f 100644 --- a/test/python/test_reactor.py +++ b/test/python/test_reactor.py @@ -881,10 +881,10 @@ def test_reinitialize(self): T2a = self.r2.T self.r1.thermo.TD = 300, None - self.r1.syncState() + self.r1.sync_state() self.r2.thermo.TD = 1000, None - self.r2.syncState() + self.r2.sync_state() self.assertNear(self.r1.T, 300) self.assertNear(self.r2.T, 1000) @@ -1994,7 +1994,7 @@ def test_reinitialization(self): gas.TPX = 1700, 4000, 'NH3:1.0, SiF4:0.4' surf.TP = gas.TP r.mass_flow_rate = 0.01 - r.syncState() + r.sync_state() sim.initial_time = 0. sim.advance(0.6) @@ -2909,9 +2909,9 @@ def before_sync_state(self): r = DummyReactor(self.gas) net = ct.ReactorNet([r]) self.assertEqual(r.component_index("H2"), 5 + 3 + self.gas.species_index("H2")) - r.syncState() + r.sync_state() net.advance(1) - r.syncState() + r.sync_state() self.assertEqual(r.sync_calls, 2) def test_RHS_LHS(self):