Skip to content

Commit 6ee8bb3

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 67bccad commit 6ee8bb3

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
@@ -214,6 +214,7 @@ cdef extern from "cantera/zeroD/ReactorDelegator.h" namespace "Cantera":
214214
void setHeatRate(double)
215215
void restoreThermoState() except +translate_exception
216216
void restoreSurfaceState(size_t) except +translate_exception
217+
void defaultEval(double time, double* LHS, double* RHS)
217218

218219

219220
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
@@ -736,6 +737,16 @@ cdef class ExtensibleReactor(Reactor):
736737
"""
737738
self.accessor.restoreSurfaceState(n)
738739

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

740751
cdef class ExtensibleIdealGasReactor(ExtensibleReactor):
741752
"""

test/python/test_reactor.py

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

0 commit comments

Comments
 (0)