# Registering a time dependent function inside a function in ModelingToolkit.jl

**URL:** https://discourse.julialang.org/t/registering-a-time-dependent-function-inside-a-function-in-modelingtoolkit-jl/100402
**Category:** Modelling & Simulations
**Tags:** question, modelingtoolkit
**Created:** [June 15, 2023, 3:08pm UTC](https://discourse.julialang.org/t/registering-a-time-dependent-function-inside-a-function-in-modelingtoolkit-jl/100402 "2023-06-15T15:08:24Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)
#### Post date: [June 15, 2023, 3:08pm UTC](https://discourse.julialang.org/t/registering-a-time-dependent-function-inside-a-function-in-modelingtoolkit-jl/100402/1 "2023-06-15T15:08:24Z")

</div>

Let’s say that I build my model using functions that, if need be create variables and parameters, and return equations. In the end of the code i put all equations together. Let’s say I have an example block such as

```julia
using ModelingToolkit
@variables t # independent variable (time)
@variables f(t) = 0.0 # external forcing

function external_forcing_equation(; CO2_multiplier = 4, specs...)
    @parameters CO2_multiplier = CO2_multiplier
    @parameters CO2_forcing = 5.35 # 5.35 is A0
    # Register a time-dependent step-like increase of forcing
    CO2_instant_increase(t) = t < 1000 ? 0.0 : CO2_multiplier*CO_forcing
    @register_symbolic CO2_instant_increase(t)
    return f ~ CO2_instant_increase
end

```

This codeblock won’t work, it yields

```julia
ERROR: syntax: Global method definition around C:\Users\gd419\.julia\packages\Symbolics\CNA2l\src\register.jl:91 needs to be placed at the top level, or use "eval".
Stacktrace:
 [1] top-level scope
   @ REPL[31]:1

```

I am not sure how to proceed from here because putting `@eval` before `@register` doesn’t solve the problem either. How can I create a “registered function” within another function?

---

<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: [June 16, 2023, 12:27pm UTC](https://discourse.julialang.org/t/registering-a-time-dependent-function-inside-a-function-in-modelingtoolkit-jl/100402/2 "2023-06-16T12:27:53Z")

</div>

That won’t work because it has to define a dispatch, so instead you’d define a callable struct outside of the function and instantiate it with values.

But also, this case is easier with just `CO2_instant_increase(t) = ifelse(t < 1000,0.0, CO2_multiplier*CO_forcing)`

---

<div class="post-metadata">

### Author: ![Qfl3x](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qfl3x/32/16227_2.png) [@Qfl3x](https://discourse.julialang.org/u/Qfl3x)
#### Post date: [December 2, 2023, 10:28am UTC](https://discourse.julialang.org/t/registering-a-time-dependent-function-inside-a-function-in-modelingtoolkit-jl/100402/3 "2023-12-02T10:28:39Z")

</div>

Sorry for pinging an old thread, but got an example on defining a callable struct outside of the function?

Perhaps you mean something like this?

```julia
struct CO2_function
    CO2_multiplier::Any
    CO2_increase::Any
end
function (f::CO2_function)(t)
    ifelse(t < 1000,0.0, f.CO2_multiplier*f.CO_forcing)
end
@register_symbolic (f::CO2_function)(t)
```

---

<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: [December 4, 2023, 2:55am UTC](https://discourse.julialang.org/t/registering-a-time-dependent-function-inside-a-function-in-modelingtoolkit-jl/100402/4 "2023-12-04T02:55:07Z")

</div>

Yeah does that not work?

---

<div class="post-metadata">

### Author: ![Qfl3x](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qfl3x/32/16227_2.png) [@Qfl3x](https://discourse.julialang.org/u/Qfl3x)
#### Post date: [December 18, 2023, 10:05am UTC](https://discourse.julialang.org/t/registering-a-time-dependent-function-inside-a-function-in-modelingtoolkit-jl/100402/5 "2023-12-18T10:05:53Z")

</div>

Yes it works, just checking if you had something else in mind.

---

<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: [December 18, 2023, 1:21pm UTC](https://discourse.julialang.org/t/registering-a-time-dependent-function-inside-a-function-in-modelingtoolkit-jl/100402/6 "2023-12-18T13:21:07Z")

</div>

Nope that looks like it’s the correct way to do this.
