Skip to content

Commit

Permalink
[Python] Make names in ExtensibleReactor consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
ischoegl authored and speth committed Jul 1, 2023
1 parent bde497c commit 10b1f25
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 26 deletions.
16 changes: 8 additions & 8 deletions include/cantera/zeroD/ReactorDelegator.h
Expand Up @@ -29,21 +29,21 @@ class ReactorAccessor
virtual void setNEq(size_t n) = 0;

//! Get the net rate of volume change (for example, from moving walls) [m^3/s]
virtual double vdot() const = 0;
virtual double expansionRate() const = 0;

//! Set the net rate of volume change (for example, from moving walls) [m^3/s]
virtual void setVdot(double v) = 0;
virtual void setExpansionRate(double v) = 0;

//! Get the net heat transfer rate (for example, through walls) into the
//! reactor [W]. This value is initialized and calculated as part of
//! Reactor::evalWalls().
virtual double qdot() const = 0;
virtual double heatRate() const = 0;

//! Set the net heat transfer rate (for example, through walls) into the
//! reactor [W]. For a value set using this method to affect the calculations done
//! by Reactor::eval, this method should be called in either a "replace" or "after"
//! delegate for Reactor::evalWalls().
virtual void setQdot(double q) = 0;
virtual void setHeatRate(double q) = 0;

//! Set the state of the thermo object to correspond to the state of the reactor
virtual void restoreThermoState() = 0;
Expand Down Expand Up @@ -160,19 +160,19 @@ class ReactorDelegator : public Delegator, public R, public ReactorAccessor
R::m_nv = n;
}

virtual double vdot() const override {
virtual double expansionRate() const override {
return R::m_vdot;
}

virtual void setVdot(double v) override {
virtual void setExpansionRate(double v) override {
R::m_vdot = v;
}

virtual double qdot() const override {
virtual double heatRate() const override {
return R::m_Qdot;
}

virtual void setQdot(double q) override {
virtual void setHeatRate(double q) override {
R::m_Qdot = q;
}

Expand Down
8 changes: 4 additions & 4 deletions interfaces/cython/cantera/reactor.pxd
Expand Up @@ -206,10 +206,10 @@ cdef extern from "cantera/zeroD/ReactorDelegator.h" namespace "Cantera":
cdef cppclass CxxReactorAccessor "Cantera::ReactorAccessor":
CxxReactorAccessor()
void setNEq(size_t)
double vdot()
void setVdot(double)
double qdot()
void setQdot(double)
double expansionRate()
void setExpansionRate(double)
double heatRate()
void setHeatRate(double)
void restoreThermoState() except +translate_exception
void restoreSurfaceState(size_t) except +translate_exception

Expand Down
58 changes: 52 additions & 6 deletions interfaces/cython/cantera/reactor.pyx
Expand Up @@ -595,8 +595,8 @@ cdef class ExtensibleReactor(Reactor):
`n_vars`.
``eval_walls(self, t : double) -> None``
Responsible for calculating the net rate of volume change `vdot`
and the net rate of heat transfer `qdot` caused by walls connected
Responsible for calculating the net rate of volume change `expansion_rate`
and the net rate of heat transfer `heat_rate` caused by walls connected
to this reactor.
``eval_surfaces(LHS : double[:], RHS : double[:], sdot : double[:]) -> None``
Expand Down Expand Up @@ -654,20 +654,66 @@ cdef class ExtensibleReactor(Reactor):
property vdot:
"""
Get/Set the net rate of volume change (for example, from moving walls) [m^3/s]
.. deprecated:: 3.0
To be removed in Cantera 3.0; renamed to `expansion_rate`.
"""
def __get__(self):
return self.accessor.vdot()
warnings.warn(
"ExtensibleReactor.vdot: To be removed in Cantera 3.0; "
"renamed to 'expansion_rate'.", DeprecationWarning)
return self.accessor.expansionRate()
def __set__(self, vdot):
self.accessor.setVdot(vdot)
warnings.warn(
"ExtensibleReactor.vdot: To be removed in Cantera 3.0; "
"renamed to 'expansion_rate'.", DeprecationWarning)
self.accessor.setExpansionRate(vdot)

@property
def expansion_rate(self):
"""
Get/Set the net rate of volume change (for example, from moving walls) [m^3/s]
.. versionadded:: 3.0
"""
return self.accessor.expansionRate()

@expansion_rate.setter
def expansion_rate(self, vdot):
self.accessor.setExpansionRate(vdot)

property qdot:
"""
Get/Set the net heat transfer rate (for example, through walls) [W]
.. deprecated:: 3.0
To be removed in Cantera 3.0; renamed to `heat_rate`.
"""
def __get__(self):
return self.accessor.qdot()
warnings.warn(
"ExtensibleReactor.qdot: To be removed in Cantera 3.0; "
"renamed to 'heat_rate'.", DeprecationWarning)
return self.accessor.heatRate()
def __set__(self, qdot):
self.accessor.setQdot(qdot)
warnings.warn(
"ExtensibleReactor.qdot: To be removed in Cantera 3.0; "
"renamed to 'heat_rate'.", DeprecationWarning)
self.accessor.setHeatRate(qdot)

@property
def heat_rate(self):
"""
Get/Set the net heat transfer rate (for example, through walls) [W]
.. versionadded:: 3.0
"""
return self.accessor.heatRate()

@heat_rate.setter
def heat_rate(self, qdot):
self.accessor.setHeatRate(qdot)

def restore_thermo_state(self):
"""
Expand Down
17 changes: 9 additions & 8 deletions test/python/test_reactor.py
Expand Up @@ -2654,12 +2654,13 @@ def after_eval(self,t,LHS,RHS):
self.assertNear(add_heat, r1_heat, atol=1e-5)

def test_heat_addition(self):
# Applying heat via 'qdot' property should be equivalent to adding it via a wall
# Applying heat via 'heat_rate' property should be equivalent to adding it via
# a wall
Qext = 100
Qwall = -66
class HeatedReactor(ct.ExtensibleReactor):
def after_eval_walls(self, y):
self.qdot += Qext
self.heat_rate += Qext

self.gas.TPX = 300, ct.one_atm, "N2:1.0"
r1 = HeatedReactor(self.gas)
Expand All @@ -2671,7 +2672,7 @@ def after_eval_walls(self, y):
net.advance(t)
U = r1.thermo.int_energy_mass * r1.mass
self.assertNear(U - U0, (Qext + Qwall) * t)
self.assertNear(r1.qdot, Qext + Qwall)
self.assertNear(r1.heat_rate, Qext + Qwall)

def test_with_surface(self):
phase_defs = """
Expand Down Expand Up @@ -2774,8 +2775,8 @@ def after_update_connected(self, do_pressure):

def replace_eval_walls(self, t):
if self.neighbor:
self.vdot = np.clip(self.p_coeff * (self.P - self.neighbor.P),
-1.7, 1.7)
self.expansion_rate = np.clip(
self.p_coeff * (self.P - self.neighbor.P), -1.7, 1.7)

def after_eval(self, t, LHS, RHS):
if self.neighbor:
Expand Down Expand Up @@ -2804,14 +2805,14 @@ def deltaC():
V0 = r1.volume + r2.volume
for t in np.linspace(0.01, 0.2, 50):
net.advance(t)
self.assertNear(r1.vdot, -r2.vdot)
self.assertNear(r1.expansion_rate, -r2.expansion_rate)
self.assertNear(V0, r1.volume + r2.volume)
deltaCnow = deltaC()
self.assertLess(deltaCnow, deltaCprev) # difference is always decreasing
deltaCprev = deltaCnow
self.assertArrayNear(M0, r1.mass * r1.thermo.Y + r2.mass * r2.thermo.Y, rtol=2e-8)
states1.append(r1.thermo.state, t=net.time, mass=r1.mass, vdot=r1.vdot)
states2.append(r2.thermo.state, t=net.time, mass=r2.mass, vdot=r2.vdot)
states1.append(r1.thermo.state, t=net.time, mass=r1.mass, vdot=r1.expansion_rate)
states2.append(r2.thermo.state, t=net.time, mass=r2.mass, vdot=r2.expansion_rate)

# Regression test values
self.assertNear(r1.thermo.P, 151561.15, rtol=1e-6)
Expand Down

0 comments on commit 10b1f25

Please sign in to comment.