# StackOverFlow Error in SIR model using ModelingTookit

**URL:** https://discourse.julialang.org/t/stackoverflow-error-in-sir-model-using-modelingtookit/64604
**Category:** General Usage
**Tags:** error, ode, stackoverflow, modelingtoolkit
**Created:** [July 13, 2021, 9:22pm UTC](https://discourse.julialang.org/t/stackoverflow-error-in-sir-model-using-modelingtookit/64604 "2021-07-13T21:22:26Z")
**Posts on this page:** 9
**Page:** 2

<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: [July 19, 2021, 11:08pm UTC](https://discourse.julialang.org/t/stackoverflow-error-in-sir-model-using-modelingtookit/64604/21 "2021-07-19T23:08:36Z")

</div>

You can add observed variables, which are just scalings of other state variables, and the system will automatically eliminate them from the solve. You then just do `sol[r_scaled]` or whatnot and it’ll give you those values in the solution.

---

<div class="post-metadata">

### Author: ![Michael\_Barmann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_barmann/32/20389_2.png) [@Michael\_Barmann](https://discourse.julialang.org/u/Michael_Barmann)
#### Post date: [July 21, 2021, 6:12pm UTC](https://discourse.julialang.org/t/stackoverflow-error-in-sir-model-using-modelingtookit/64604/22 "2021-07-21T18:12:11Z")

</div>

Hmm, I’m not sure how to implement that…Do you think you could illustrate this with a simple example? I’ve checked this [page](https://mtk.sciml.ai/stable/internals/#Observables-and-Variable-Elimination) for more info but this particular topic doesn’t have any code…

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [July 21, 2021, 7:19pm UTC](https://discourse.julialang.org/t/stackoverflow-error-in-sir-model-using-modelingtookit/64604/23 "2021-07-21T19:19:27Z")

</div>

I’m not sure how to add them back after the fact, but you can do this:

```julia
using ModelingToolkit, DifferentialEquations
using Plots

@parameters t β γ N
@variables S(t) I(t) R(t) s(t) i(t) r(t) x y
D = Differential(t)

SIR_eqs = [D(s) ~ -β*s*i,
           D(i) ~ β*s*i - γ*i,
           D(r) ~ γ*i,
            0 ~ s*N - S,
            0 ~ i*N - I,
            0 ~ r*N - R,
]

SIR = ODESystem(SIR_eqs, name = :SIR)

# After this, the system has only 3 states [s,i,r]
SIR = structural_simplify(SIR)

u0 = [s => 0.99, i => 0.01, r => 0.00]
pars = [β => 0.20, γ => 0.15, N=>1e3]

prob = ODEProblem(SIR, u0, (0.0, 200.0), pars)

sol = solve(prob; saveat=1:1:200)

```

To plot scaled

```julia
plot(sol; vars=[s,i,r])

```

Or to plot unscaled

```julia
plot(sol; vars=[S,I,R])

```

---

<div class="post-metadata">

### Author: ![Michael\_Barmann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_barmann/32/20389_2.png) [@Michael\_Barmann](https://discourse.julialang.org/u/Michael_Barmann)
#### Post date: [July 21, 2021, 7:42pm UTC](https://discourse.julialang.org/t/stackoverflow-error-in-sir-model-using-modelingtookit/64604/24 "2021-07-21T19:42:40Z")

</div>

@contradict Thanks, this helps a lot! Let me see if I understand correctly: by saying

`SIR = structural_simplify(SIR)`

the variables `s,i,r` are the variables which are actually being integrated, whereas as the variables S,I,R are the “observables” which are calculated from the solution for s(t),i(t),r(t)?

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [July 21, 2021, 8:50pm UTC](https://discourse.julialang.org/t/stackoverflow-error-in-sir-model-using-modelingtookit/64604/25 "2021-07-21T20:50:03Z")

</div>

Yes, in this case that is what happens. `structural_simplify` is kind of a magic box that applies several tools to try to make a simpler system to solve. As such, it isn’t very controllable so you sometimes have to play with the input equations if you want a particular shape. The fun starts [here](https://github.com/SciML/ModelingToolkit.jl/blob/master/src/systems/abstractsystem.jl#L765) if you want to see what it is doing.

---

<div class="post-metadata">

### Author: ![Michael\_Barmann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_barmann/32/20389_2.png) [@Michael\_Barmann](https://discourse.julialang.org/u/Michael_Barmann)
#### Post date: [July 21, 2021, 10:56pm UTC](https://discourse.julialang.org/t/stackoverflow-error-in-sir-model-using-modelingtookit/64604/26 "2021-07-21T22:56:53Z")

</div>

Awesome, this looks to be exactly what I originally wanted!

Another question: How does `structural_simplify` “know” that s,i,r are the observables? Is it the `0` on the LHS of the equations? Or the fact that the s,i,r equations don’t involve derivatives? Or did we just happen to get lucky in this case and `structural_simplify` somehow inferred what the observables would be?

…I’ll check out the source code in more detail (though based on a quick glance, it seems a bit over my head, currently 🙂 ).

---

<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: [August 6, 2021, 10:23am UTC](https://discourse.julialang.org/t/stackoverflow-error-in-sir-model-using-modelingtookit/64604/27 "2021-08-06T10:23:49Z")

</div>

> [@Michael\_Barmann](#):
>
> Another question: How does `structural_simplify` “know” that s,i,r are the observables? Is it the `0` on the LHS of the equations? Or the fact that the s,i,r equations don’t involve derivatives? Or did we just happen to get lucky in this case and `structural_simplify` somehow inferred what the observables would be?

It uses a graph algorithm to identify the minimal equation to solve and solves that, generating explicit representations of the other equations from the elimination equations.

---

<div class="post-metadata">

### Author: ![Michael\_Barmann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_barmann/32/20389_2.png) [@Michael\_Barmann](https://discourse.julialang.org/u/Michael_Barmann)
#### Post date: [August 6, 2021, 10:13pm UTC](https://discourse.julialang.org/t/stackoverflow-error-in-sir-model-using-modelingtookit/64604/28 "2021-08-06T22:13:07Z")

</div>

Hmm, very interesting!

I have some follow-up questions on this. I think I’ll probably start a new thread though since it doesn’t really have to do with my original title.

Also, would it be ok if I made a PR for the documentation to include the change of variables example with the SIR equations?

---

<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: [August 8, 2021, 10:29pm UTC](https://discourse.julialang.org/t/stackoverflow-error-in-sir-model-using-modelingtookit/64604/29 "2021-08-08T22:29:00Z")

</div>

> [@Michael\_Barmann](#):
>
> Also, would it be ok if I made a PR for the documentation to include the change of variables example with the SIR equations?

That would be nice.

[Previous page](https://discourse.julialang.org/t/stackoverflow-error-in-sir-model-using-modelingtookit/64604.md?page=1)
