How does ModelingToolkit substitute the u0map?

Hi there, I’m trying to port some code over to use Catalyst.

using Catalyst, Symbolics

rs = @reaction_network begin
    1, 2A --> B
    2, B + C --> 0
end

v = Catalyst.oderatelaw.(reactions(rs))
u0 = [1,1,1]

Now if i want to substitute u0 into v:

substitute(convert.(Num, v), Dict(species(rs) .=> u0))

This works, but the convert in there feels clunky. Is this what’s going on under the hood or is there a nicer way of doing it?

Many thanks!

The code looks like this, where u0map is a dict from symbolic variables to values and comes from the user, and dvs are typically the states of the system (dependent variables).

defs = defaults(sys)
u0 = ModelingToolkit.varmap_to_vars(u0map, dvs; defaults=defs)

The varmap_to_vars helps you map symbolic names to indices

1 Like

You can find more info here
https://mtk.sciml.ai/dev/basics/FAQ/#Transforming-value-maps-to-arrays

1 Like