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

**URL:** <https://discourse.julialang.org/t/programmatic-generation-of-reaction-catalyst-jl-with-user-defined-reaction-rate/81858>\
**Category:** New to Julia\
**Created:** [May 29, 2022, 7:38am UTC](https://discourse.julialang.org/t/programmatic-generation-of-reaction-catalyst-jl-with-user-defined-reaction-rate/81858 "2022-05-29T07:38:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SiddhantDeshmukh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/siddhantdeshmukh/32/35324_2.png) [@SiddhantDeshmukh](https://discourse.julialang.org/u/SiddhantDeshmukh)\
**Post date:** [May 29, 2022, 7:38am UTC](https://discourse.julialang.org/t/programmatic-generation-of-reaction-catalyst-jl-with-user-defined-reaction-rate/81858/1 "2022-05-29T07:38:51Z")

</div>

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

```julia
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

```julia
  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

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

```

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

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [May 29, 2022, 12:28pm UTC](https://discourse.julialang.org/t/programmatic-generation-of-reaction-catalyst-jl-with-user-defined-reaction-rate/81858/2 "2022-05-29T12:28:25Z")

</div>

As a general reference, I’d suggest taking a look at the [Catalyst symbolic reaction systems tutorial](https://catalyst.sciml.ai/dev/tutorials/reaction_systems/).

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

```julia
@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

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

```

```julia

```

---

<div class="post-metadata">

**Author:** ![SiddhantDeshmukh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/siddhantdeshmukh/32/35324_2.png) [@SiddhantDeshmukh](https://discourse.julialang.org/u/SiddhantDeshmukh)\
**Post date:** [May 31, 2022, 10:31am UTC](https://discourse.julialang.org/t/programmatic-generation-of-reaction-catalyst-jl-with-user-defined-reaction-rate/81858/3 "2022-05-31T10:31:56Z")

</div>

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

```julia
@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)”

EDIT:  
I should mention that format\_dict[“R”] and format\_dict[“P”] are symbols in this implementation!

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [May 31, 2022, 11:13am UTC](https://discourse.julialang.org/t/programmatic-generation-of-reaction-catalyst-jl-with-user-defined-reaction-rate/81858/4 "2022-05-31T11:13:51Z")

</div>

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

```julia
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](https://github.com/isaacsas/ReactionNetworkImporters.jl/blob/master/src/parsing_routines_bngnetworkfiles.jl)

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [May 31, 2022, 11:15am UTC](https://discourse.julialang.org/t/programmatic-generation-of-reaction-catalyst-jl-with-user-defined-reaction-rate/81858/5 "2022-05-31T11:15:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SiddhantDeshmukh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/siddhantdeshmukh/32/35324_2.png) [@SiddhantDeshmukh](https://discourse.julialang.org/u/SiddhantDeshmukh)\
**Post date:** [May 31, 2022, 1:54pm UTC](https://discourse.julialang.org/t/programmatic-generation-of-reaction-catalyst-jl-with-user-defined-reaction-rate/81858/6 "2022-05-31T13:54:12Z")

</div>

Thanks, this did the trick!

---

<div class="post-metadata">

**Author:** ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)\
**Post date:** [May 31, 2022, 2:10pm UTC](https://discourse.julialang.org/t/programmatic-generation-of-reaction-catalyst-jl-with-user-defined-reaction-rate/81858/7 "2022-05-31T14:10:04Z")

</div>

Great, glad that helped!
