Skip to content

Commit feb862c

Browse files
Add a default evaluation function for reactors
It can sometimes be useful to add on to the default evaluation in such a way that the standard before or after delegations cannot handle. A default evaluation function allows for this.
1 parent 1fa099f commit feb862c

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

include/cantera/zeroD/ReactorDelegator.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class ReactorAccessor
5252
//! Set the state of the thermo object for surface *n* to correspond to the
5353
//! state of that surface
5454
virtual void restoreSurfaceState(size_t n) = 0;
55+
56+
//! Public access to the default evaluation function so it can be used in
57+
//! replace functions
58+
virtual void defaultEval(double t, double* LHS, double* RHS) = 0;
5559
};
5660

5761
//! Delegate methods of the Reactor class to external functions
@@ -161,6 +165,10 @@ class ReactorDelegator : public Delegator, public R, public ReactorAccessor
161165
m_build_jacobian(jacVector);
162166
}
163167

168+
void defaultEval(double t, double* LHS, double* RHS) override {
169+
R::eval(t, LHS, RHS);
170+
}
171+
164172
// Public access to protected Reactor variables needed by derived classes
165173

166174
void setNEq(size_t n) override {

interfaces/cython/cantera/reactor.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ cdef extern from "cantera/zeroD/ReactorDelegator.h" namespace "Cantera":
215215
void setHeatRate(double)
216216
void restoreThermoState() except +translate_exception
217217
void restoreSurfaceState(size_t) except +translate_exception
218+
void defaultEval(double time, double* LHS, double* RHS)
218219

219220

220221
ctypedef CxxReactorAccessor* CxxReactorAccessorPtr

interfaces/cython/cantera/reactor.pyx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# at https://cantera.org/license.txt for license and copyright information.
33

44
import warnings
5+
import numpy as np
56
from collections import defaultdict as _defaultdict
67
import numbers as _numbers
78
from cython.operator cimport dereference as deref
@@ -738,6 +739,16 @@ cdef class ExtensibleReactor(Reactor):
738739
"""
739740
self.accessor.restoreSurfaceState(n)
740741

742+
def default_eval(self, time, LHS, RHS):
743+
"""
744+
Evaluation of the base reactors `eval` function to be used in `replace`
745+
functions and maintain original functionality.
746+
"""
747+
assert len(LHS) == self.n_vars and len(RHS) == self.n_vars
748+
cdef np.ndarray[np.double_t, ndim=1, mode="c"] rhs = np.frombuffer(RHS)
749+
cdef np.ndarray[np.double_t, ndim=1, mode="c"] lhs = np.frombuffer(LHS)
750+
self.accessor.defaultEval(time, &lhs[0], &rhs[0])
751+
741752

742753
cdef class ExtensibleIdealGasReactor(ExtensibleReactor):
743754
"""

test/python/test_reactor.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3250,3 +3250,26 @@ def replace_build_jacobian(self, jac_vector):
32503250
# test that jacobian wall element is hard coded value
32513251
jac = r.jacobian
32523252
assert np.sum(jac) == 0
3253+
3254+
def test_replace_with_default_eval(self):
3255+
class ReplaceEvalReactor(ct.ExtensibleIdealGasConstPressureMoleReactor):
3256+
3257+
def replace_eval(self, t, LHS, RHS):
3258+
self.default_eval(t, LHS, RHS)
3259+
3260+
# setup thermo object
3261+
gas = ct.Solution("h2o2.yaml", "ohmech")
3262+
gas.set_equivalence_ratio(0.5, "H2:1.0", "O2:1.0")
3263+
gas.equilibrate("HP")
3264+
# replacement reactor
3265+
r = ReplaceEvalReactor(gas)
3266+
r.volume = 1.0
3267+
# default reactor
3268+
rstd = ct.IdealGasConstPressureMoleReactor(gas)
3269+
rstd.volume = r.volume
3270+
# network of both reactors
3271+
net = ct.ReactorNet([r, rstd])
3272+
net.preconditioner = ct.AdaptivePreconditioner()
3273+
net.advance_to_steady_state()
3274+
# reactors should have the same solution because the default is used
3275+
self.assertArrayNear(r.get_state(), rstd.get_state())

0 commit comments

Comments
 (0)