Skip to content

Commit 17c21c8

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 4c35cfb commit 17c21c8

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
@@ -728,6 +729,16 @@ cdef class ExtensibleReactor(Reactor):
728729
"""
729730
self.accessor.restoreSurfaceState(n)
730731

732+
def default_eval(self, time, LHS, RHS):
733+
"""
734+
Evaluation of the base reactors `eval` function to be used in `replace`
735+
functions and maintain original functionality.
736+
"""
737+
assert len(LHS) == self.n_vars and len(RHS) == self.n_vars
738+
cdef np.ndarray[np.double_t, ndim=1, mode="c"] rhs = np.frombuffer(RHS)
739+
cdef np.ndarray[np.double_t, ndim=1, mode="c"] lhs = np.frombuffer(LHS)
740+
self.accessor.defaultEval(time, &lhs[0], &rhs[0])
741+
731742

732743
cdef class ExtensibleIdealGasReactor(ExtensibleReactor):
733744
"""

test/python/test_reactor.py

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

0 commit comments

Comments
 (0)