Programmatic Generation of Reaction (Catalyst.jl) with User-Defined Reaction Rate

Hi everyone,
I’m using Catalyst.jl to create some reaction networks, and would like to read the reactions in from a file (port from a Python project). Every reaction has the same rate law with the user-defined function

arrhenius(a, b, c, T) = a .* (T ./ 300.).^b .* exp.(-c ./ T)

In the file, I store the rate expressions as, e.g.

1.3300d-11 * (Tgas / 3d2)**(-7.8000d-01) * exp(-4.0000d01 / Tgas)

From this string, I can parse the “a”, “b”, “c” components, but I don’t know how to create a Reaction using this function with a temperature symbol. In the ReactionSystem DSL I would use

  rn = @reaction_network begin
    arrhenius(1.8e-10, -0.78, 40., T), A + B --> C
  end T

I would like to emulate this behaviour with the Reaction syntax, namely

rxns = []
push!(rxns, Reaction(arrhenius(a, b, c, T), [A, B], [C]))

How can I use an arbitrary “T” in the same manner here?

As a general reference, I’d suggest taking a look at the Catalyst symbolic reaction systems tutorial.

For your specific question, you can do it in two ways. In the approach you are taking you need to declare, a,b,c,T as parameters and A,B,C as variables like

@parameters a b c T
@variables t A(t) B(t) C(t)
rxns = []
push!(rxns, Reaction(arrhenius(a, b, c, T), [A, B], [C]))
@named arrhenius_sys = ReactionSystem(rxns, t)

Alternatively, Catalyst also has a macro that works on a reaction level

rx = @reaction arrhenius(a, b, c, T), A + B --> C
rxns = [rx]
@variables t
@named arrhenius_sys = ReactionSystem(rxns, t)

I think I understand the gist of it, thanks. Since I’m reading everything in from a file, I don’t have the reactants and products at hand, so I can’t declare these as variables beforehand. Is there a way to do this once I’ve read in all of the species? Here’s a snippet of what I have so far; ‘format_dict’ contains the reactants, products and rate for a given reaction

@parameters a b c T
a, b, c = str_to_arrhenius(string(format_dict["rate"][1]))
reaction = Reaction(arrhenius(a, b, c, T), format_dict["R"], format_dict["P"])

I get an error “ERROR: MethodError: no method matching operation(::Symbol)”

I should mention that format_dict[“R”] and format_dict[“P”] are symbols in this implementation!

You can interpolate a symbol to generate a symbolic variable, i.e.

asymbol = :A
@variables t
Asymbolic = (@variables ($asymbol)(t))[1]

now Asymbolic will be the symbolic variable A(t).

An example of parsing a text file to a ReactionSystem that might help if you look through it is the Bionetgen .net file importer in ReactionNetworkImporters: ReactionNetworkImporters.jl/parsing_routines_bngnetworkfiles.jl at master · isaacsas/ReactionNetworkImporters.jl · GitHub

The Catalyst functions / types like Reaction and ReactionSystem don’t accept Julia Symbols. You need to create symbolic variables and parameters as in my example above, that is why you are getting an error.

Thanks, this did the trick!

Great, glad that helped!