# ParameterizedFunctions.jl @ode\_def programmatically generate model

**URL:** <https://discourse.julialang.org/t/parameterizedfunctions-jl-ode-def-programmatically-generate-model/37733>\
**Category:** Modelling & Simulations\
**Created:** [April 17, 2020, 3:29am UTC](https://discourse.julialang.org/t/parameterizedfunctions-jl-ode-def-programmatically-generate-model/37733 "2020-04-17T03:29:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MFairley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mfairley/32/8599_2.png) [@MFairley](https://discourse.julialang.org/u/MFairley)\
**Post date:** [April 17, 2020, 3:29am UTC](https://discourse.julialang.org/t/parameterizedfunctions-jl-ode-def-programmatically-generate-model/37733/1 "2020-04-17T03:29:54Z")

</div>

Is there a way to pass variables into @ode\_def that would be treated as constants in the ODE problem?

For example, something like

```julia
function create_model_def(male)
    h = male ? 2.0 : 7.0

    f = @ode_def begin
        dy₁ = -k₁*y₁+k₃*y₂*y₃
        dy₂ = k₁*y₁-k₂*y₂^2-k₃*y₂*y₃
        dy₃ = k₂*y₂^2 + h
    end k₁ k₂ k₃

end

```

This results in `LoadError: UndefVarError: h not defined` because h is not defined at the time the macro is expanded.

In my application, h would be a function with different constants that depend on if the model is for a male or female. I don’t want to pass in a bool as a model parameter so I can keep all parameters continuous to use the sensitivity analysis functions easily. Instead, I would like to generate separate ODE models programmatically. I also want to avoid re-writing the entire model for males and females because the vast majority of the model is identical.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 20, 2020, 5:15am UTC](https://discourse.julialang.org/t/parameterizedfunctions-jl-ode-def-programmatically-generate-model/37733/2 "2020-04-20T05:15:14Z")

</div>

Since macros act at parse time and `@ode_def` is a macro, you’d have to write a macro yourself to do this. It’s not too difficult, since it would just be:

```julia
using ParameterizedFunctions

macro create_model_def(male)
    val = male ? 2.0 : 7.0
    quote
        f = @ode_def begin
            dy₁ = -k₁*y₁+k₃*y₂*y₃
            dy₂ = k₁*y₁-k₂*y₂^2-k₃*y₂*y₃
            dy₃ = k₂*y₂^2 + $val
        end k₁ k₂ k₃
    end
end

@create_model_def true
@create_model_def false

```

However, this can get complicated fast, which is why we recommend the newer ModelingToolkit.jl for this kind of staged programming.

> **[GitHub - SciML/ModelingToolkit.jl: An acausal modeling framework for...](https://github.com/SciML/ModelingToolkit.jl)**
>
> An acausal modeling framework for automatically parallelized scientific machine learning (SciML) in Julia. A computer algebra system for integrated symbolics for physics-informed machine learning a...

In ModelingToolkit, this looks like:

```julia
using ModelingToolkit

function create_model_def(male)
    @variables t y₁(t) y₂(t) y₃(t)
    @parameters k₁ k₂ k₃
    @derivatives D'~t
    val = male ? 2.0 : 7.0
    eqs = [D(y₁) ~ -k₁*y₁+k₃*y₂*y₃
           D(y₂) ~ k₁*y₁-k₂*y₂^2-k₃*y₂*y₃
           D(y₃) ~ k₂*y₂^2 + val]
    ODEFunction(ODESystem(eqs))
end
create_model_def(true)
create_model_def(false)

```

---

<div class="post-metadata">

**Author:** ![MFairley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mfairley/32/8599_2.png) [@MFairley](https://discourse.julialang.org/u/MFairley)\
**Post date:** [April 20, 2020, 4:38pm UTC](https://discourse.julialang.org/t/parameterizedfunctions-jl-ode-def-programmatically-generate-model/37733/3 "2020-04-20T16:38:07Z")

</div>

Thank you! Is ModelingToolkit.jl stable?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 20, 2020, 7:30pm UTC](https://discourse.julialang.org/t/parameterizedfunctions-jl-ode-def-programmatically-generate-model/37733/4 "2020-04-20T19:30:41Z")

</div>

It just got to this point a few days ago, so it’s not like ParameterizedFunctions.jl which hasn’t changed syntax in 3 years and has no intention of ever changing again, but the current ModelingToolkit is the interface we plan on keeping and what we plan on generally pointing to as the symbolic front end for DifferentialEquations.jl and the SciML ecosystem as whole. At this point ParameterizedFunctions.jl is actually just converting that syntax to ModelingToolkit under the hood.
