# Integration within ModelingToolkit Models

**URL:** <https://discourse.julialang.org/t/integration-within-modelingtoolkit-models/115015>\
**Category:** Modelling & Simulations\
**Tags:** question\
**Created:** [May 31, 2024, 2:22pm UTC](https://discourse.julialang.org/t/integration-within-modelingtoolkit-models/115015 "2024-05-31T14:22:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HSnowden](https://avatars.discourse-cdn.com/v4/letter/h/58f4c7/32.png) [@HSnowden](https://discourse.julialang.org/u/HSnowden)\
**Post date:** [May 31, 2024, 2:22pm UTC](https://discourse.julialang.org/t/integration-within-modelingtoolkit-models/115015/1 "2024-05-31T14:22:44Z")

</div>

Hello,

Sorry I am asking so many questions on here. I would like to be able to integrate part of an equation within a MTK model so that each time step the integration is performed with the current variables and parameters. However, when building the model, the integration wants to perform at that moment therefore it is unsolvable as it tries to perform symbolic rather than numeric integration. This is compounded by the fact that a component within the integral is an inteprolation function which can’t be integrated any way other than numerically. Here is a MWE:

```julia
function func()
    @parameters a b
    @variables x
    return a*x + b
end

function theintegral(int)
    @variables x
    return SymbolicNumericIntegration.integrate(x*int(x)*func(),(x,-10,10),symbolic=false)
end

function OurODE(;name)
    @variables a(t) placeholder(t)
    
    eqs = D(a) ~ placeholder
end

function main()
    int = get_interpolate(stuff)
    @named ODE = OurODE
    integral = theintegral(int)
    connections = [ODE.placeholder ~ integral]
    connected = compose(ODESystem(connections,t,name=:connected,ODE)
    connected_simp = structural_simplify(connected)
    u0=[ODE.a => 0.0]
    p[ODE.b => 1]
    prob=ODEProblem(connected_simp,u0,(0.0,500),p
    sol=solve(prob,AutoVern7(Rodas5());abstol=1e-10,reltol=1e-10)
end

```

This should have enough information that it can solve everything numerically at the time I want it solved but it tries to solve the integral at the call to theintegral(int) which makes sense but I want it to return almost a placeholder num that says it will integrate numerically when it gets there. Is this too much to ask/is there a different way of doing this  
Any help would be appreciated thank you.

---

<div class="post-metadata">

**Author:** ![HSnowden](https://avatars.discourse-cdn.com/v4/letter/h/58f4c7/32.png) [@HSnowden](https://discourse.julialang.org/u/HSnowden)\
**Post date:** [May 31, 2024, 2:57pm UTC](https://discourse.julialang.org/t/integration-within-modelingtoolkit-models/115015/2 "2024-05-31T14:57:54Z")

</div>

I had a thought that it might be possible with some callback that tells the system to update the placeholder parameter every time step with the solution to the equation but I’m not sure if that would work or is possible

---

<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:** [May 31, 2024, 3:35pm UTC](https://discourse.julialang.org/t/integration-within-modelingtoolkit-models/115015/3 "2024-05-31T15:35:30Z")

</div>

Why not just set it up with Integrals.jl and register the numerical approximation?

---

<div class="post-metadata">

**Author:** ![HSnowden](https://avatars.discourse-cdn.com/v4/letter/h/58f4c7/32.png) [@HSnowden](https://discourse.julialang.org/u/HSnowden)\
**Post date:** [May 31, 2024, 3:36pm UTC](https://discourse.julialang.org/t/integration-within-modelingtoolkit-models/115015/4 "2024-05-31T15:36:59Z")

</div>

How would I go about doing this?

As the function call to Integrals.jl in the MWE wouldn’t have numerical values for the parameters a and b yet so it would only be able to evaluate the integral once the ODESystem has been built and solution begun

---

<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:** [May 31, 2024, 3:58pm UTC](https://discourse.julialang.org/t/integration-within-modelingtoolkit-models/115015/5 "2024-05-31T15:58:58Z")

</div>

You need to write a function that calls solve and then @register it

---

<div class="post-metadata">

**Author:** ![HSnowden](https://avatars.discourse-cdn.com/v4/letter/h/58f4c7/32.png) [@HSnowden](https://discourse.julialang.org/u/HSnowden)\
**Post date:** [June 3, 2024, 9:17am UTC](https://discourse.julialang.org/t/integration-within-modelingtoolkit-models/115015/6 "2024-06-03T09:17:21Z")

</div>

Hi @ChrisRackauckas,

I’ve tried to do this is my real code except it is now passing nums to the integral which I don’t think it knows how to handle. Is there a keyword somewhere that tells register\_symbolic to unpack Nums into floats when sending to these functions.  
The documentation isn’t very clear and I couldn’t find a tutorial on how to use the macro.

This is the code for the integral component. kB,Tel and μ are parameters and variables while DOS is an interpolation that I pass to the function

```julia
function dFDdT(kB,Tel,μ,E)
    e = exp((E-μ)/kB*Tel)
    return (E-μ)*e/(kB*Tel^2*(e+1)^2)
end

function HeatCapacity(kB,Tel,μ,DOS)
    int(u,p)=dFDdT(kB,Tel,μ,u)*DOS(u)*u
    prob=IntegralProblem(int,0,20)
    sol = solve(prob,HCubatureJL(initdiv=2);reltol=1e-5,abstol=1e-5)
    return sol.u
end
@register_symbolic HeatCapacity(kB,Tel,μ,DOS)

```

---

<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 5, 2024, 7:26am UTC](https://discourse.julialang.org/t/integration-within-modelingtoolkit-models/115015/7 "2024-06-05T07:26:22Z")

</div>

`DOS` is a function and not symbolic? Then you need:

```julia
@register_symbolic HeatCapacity(kB,Tel,μ,DOS::Function)

```
