Hi I am trying to figure out a way to dynamically add/remove reactions once I have defined a ReactionSystem programmatically.
Suppose I define the system using this function.
function generate_creation(n,MAT)
# Creates a vector `X` with n species.
@species X(t)[1:n]
@species A(t),B(t)
@parameters k
rxs = [
[Reaction(k, [A,B], [X[i]]) for i in 1:n];
[ADD REACTIONS USING MATRIX MAT]
]
# Assembly and return a complete `ReactionSystem`
@named lp_n = ReactionSystem(rxs, t)
return complete(lp_n)
end
I let the system reach the equilibrium. Then change the matrix mat. This changes the set of reactions. Can i update the set of reactions without needing to call this function once again.
Can you maybe elaborate on what you wish to do exactly? Do you want the system to reach equilibrium, and then add some reactions and continue from where you were before? Do you know beforehand already what reactions will be added and when? Depending on the answers, I see different approaches.
Yes It is close to the former. The reactions that will be removed will depend on the state at the time the system has reached the equilibrium. I am basically trying to implement an evolutionary algorithm. So the matrix (which creates the set of reactions) changes depending on which molecule exist at the end of the jump process/ODE solution.
If the system size does not change, you can use remake
or setu
/setp
to effectively change the parameters (and the meaning) of the compartments/reactions. Yet if you want to effectively add or remove species, it becomes a bit trickier. Perhaps you can wrap the change in the system into some callback, so that you do not need to manually restart the numerical integration.
Generally, in (open-ended) evolutionary settings I think that there is no easy way out, as your entire reaction system can change, and you do not necessarily know the changes beforehand. In this extreme case, I see no other way then to remake the entire reaction system essentially from scratch after equilibrium has been reached. Depending on the total sizes of your system, this does not need to be much of a hassle though, but if it turns out to be a hassle I’d say perhaps a pure ODEProblem
using in-place functions may be a better/faster option.