Skip to content

Commit

Permalink
[zeroD] Move syncState to ReactorNode
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl committed Aug 23, 2024
1 parent 2334ad8 commit b3dd2d1
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 25 deletions.
11 changes: 3 additions & 8 deletions include/cantera/zeroD/ReactorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
9 changes: 9 additions & 0 deletions include/cantera/zeroD/ReactorNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ class ReactorNode
//! Set the default name of a reactor. Returns `false` if it was previously set.
bool setDefaultName(map<string, int>& 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<Solution> m_solution;
Expand Down
2 changes: 1 addition & 1 deletion include/cantera/zeroD/ReactorSurface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
6 changes: 1 addition & 5 deletions interfaces/cython/cantera/reactor.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()

Expand All @@ -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
Expand Down
20 changes: 17 additions & 3 deletions interfaces/cython/cantera/reactor.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -863,7 +877,7 @@ cdef class ReactorSurface(ReactorNode):
this surface.
"""
def __get__(self):
self.surface.syncState()
self.sync_state()
return self._kinetics

property coverages:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion interfaces/matlab_experimental/Utility/ctRoot.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
function output = ctRoot()
output = '';
output = '/opt/homebrew/Caskroom/miniforge/base/envs/cantera-dev';
end
2 changes: 1 addition & 1 deletion samples/python/reactors/pfr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion samples/python/reactors/surf_pfr_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/zeroD/ReactorNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,16 @@ bool ReactorNode::setDefaultName(map<string, int>& 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.");
}

}
10 changes: 5 additions & 5 deletions test/python/test_reactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit b3dd2d1

Please sign in to comment.