Description
Abstract
For a ThermoPhase
in use by a Reactor
, the state of the ThermoPhase
does not necessarily correspond to the state of the Reactor
at the current time of the ReactorNet
. This can lead to users extracting incorrect state information during integration (see this UG post, for example). In addition, the current requirement that a single ThermoPhase
can be shared across multiple Reactor
s increases the complexity of evaluating the Reactor
's governing equations.
Motivation
After an integration step by ReactorNet
, the state of the ThermoPhase
object will generally be set to the latest time reached by the CVODES integrator, advance(t_user)
method). If the user accesses the ThermoPhase
object directly, they will get the state at reactor.thermo
property, in which CVODES will provide a result interpolated back to the correct time. For simple usage, like plotting the state as a function of time, getting the state at a slightly different time won't noticeably affect results. However, for cases like trying to evaluate sensitivities using a finite difference approach, two reactor networks will not have reached the same value of
Because the current structure allows multiple reactors to share a ThermoPhase
object, there is a significant amount of complexity in the logic for evaluating the governing equations for all reactors in the network.
The current evaluation logic amounts to the following (in Python pseudocode):
def eval(t, y, ydot):
# ReactorNet::updateState
for reactor in all_reactors:
reactor.thermo.set_TPY(y[...])
# save the current state information
reactor.state = reactor.thermo.state
# compute auxiliary quantities based on state, for any quantities that may need
# to be accessed from other reactors, e.g. mass fractions, pressure, or enthalpy
reactor.local_vars = f(reactor.thermo)
# ReactorNet::eval
for reactor in all_reactors:
reactor.thermo.restore_state(reactor.state)
ydot[...] = f(reactor.thermo, reactor.kinetics, reactor.local_vars, reactor.neighbors.local_vars)
This complexity has resulted in difficulties for users who want to implement modified governing equations using the ExtensibleReactor
framework. For example, see this problem posted on the UG, and this one too.
Possible Solutions
I think the solution to both of these problems to stop having multiple reactors share ThermoPhase
/Kinetics
objects, and to have ReactorNet.step
and ReactorNet.advance
automatically synchronize the state of the reactors after each call (by default).
-
Thanks to the implementation of
AnyMap
-based serialization, we can still allow users to use a singleSolution
object to set up multiple reactors. When initializing theReactorNet
, we can then check to see if anySolution
object is being used multiple times, and then clone it to create a distinctSolution
for eachReactor
.- If a gas object is being used only once, don't make a duplicate, and automatically synchronize it after every call to step/advance (TODO: verify if this is necessary for step; not entirely clear when that would be the case)
- If a gas object is being used for multiple objects, create an internal duplicate for all of the internal uses, leaving the external one corresponding to the initial condition for whichever reactor was set up last. The other reactors will be accessible via the
thermo
properties of the reactor objects. We will still automatically synchronize these after each step -- not rely on the magic call tosyncState
when the getter is used.
-
The automatic synchronization can be disabled by passing a
sync=False
option toadvance()
orstep()
. -
The "simple" alternative is to force this on the user -- we just check and tell the user that they can't use the same
Solution
object multiple times, but I think the automatic cloning is much more user friendly and shouldn't be that difficult to implement.
After this change, the evaluation logic would look more like:
def eval(t, y, ydot):
# ReactorNet::updateState
for reactor in all_reactors:
# Reactor.updateState
reactor.thermo.set_TPY(y[...])
# Calculation of reactor-local variables is optional, for efficiency only
# ReactorNet::eval
for reactor in all_reactors:
# Evaluation can directly access thermo from this reactor and neighboring reactors
ydot[...] = f(reactor.thermo, reactor.kinetics, reactor.local_vars, reactor.neighbors.thermo)
I'd like to make sure that any changes to how the work of evaluating the reactor governing equations is divided up would provide relatively clean solutions to a few scenarios that have come up on the Users' Group. Namely:
-
https://groups.google.com/g/cantera-users/c/0IeoYgEr-Ng: imposing
$T(t)$ while keeping other reactor properties (volume, density, etc.) consistent -
https://groups.google.com/g/cantera-users/c/ASrSvruxw-g: using thermo properties in an
ExtensibleReactor
override override ofupdate_state