# StackOverflowError() with ModellingToolkit.jl and NaNMath.jl

**URL:** <https://discourse.julialang.org/t/stackoverflowerror-with-modellingtoolkit-jl-and-nanmath-jl/104364>\
**Category:** Modelling & Simulations\
**Tags:** nan, modelingtoolkit, modelling\
**Created:** [September 28, 2023, 6:54pm UTC](https://discourse.julialang.org/t/stackoverflowerror-with-modellingtoolkit-jl-and-nanmath-jl/104364 "2023-09-28T18:54:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dleather](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dleather/32/43762_2.png) [@dleather](https://discourse.julialang.org/u/dleather)\
**Post date:** [September 28, 2023, 6:54pm UTC](https://discourse.julialang.org/t/stackoverflowerror-with-modellingtoolkit-jl-and-nanmath-jl/104364/1 "2023-09-28T18:54:32Z")

</div>

The following code results in a StackOverflowError() in Julia 1.9. Is this expected behavior?

```julia
using ModelingToolkit
using NaNMath

#Define Problem
@variables t g(t) π(t) r(t) 
@parameters α_g α_π α_r κ_g κ_π κ_r ϕ_g ϕ_π σ_g σ_π σ_r ζ_0g ζ_0π

D = Differential(t)

eqs = [D(g) ~ α_g - κ_g * g - ϕ_g * r + ϕ_g * π, 
       D(π) ~ α_π - κ_π * π + ϕ_π * g,
       D(r) ~ α_r - κ_r * r + ζ_0g * g + ζ_0π * π
    ]

noiseeqs = [σ_g,
            σ_π,
            σ_r * NaNMath.sqrt(r)]

```

---

<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:** [September 28, 2023, 7:56pm UTC](https://discourse.julialang.org/t/stackoverflowerror-with-modellingtoolkit-jl-and-nanmath-jl/104364/2 "2023-09-28T19:56:13Z")

</div>

It seems to work for me. What versions of `NaNMath` and `ModelingToolkit` are you using?

```julia
julia> noiseeqs = [σ_g,
                   σ_π,
                   σ_r * NaNMath.sqrt(r)]
3-element Vector{Num}:
     σ_g
     σ_π
 NaNMath.sqrt(r(t))*σ_r

(jl_sgol1e) pkg> st
Status `/tmp/jl_sgol1e/Project.toml`
  [961ee093] ModelingToolkit v8.71.0
  [77ba4419] NaNMath v1.0.2

julia> versioninfo()
Julia Version 1.9.3
Commit bed2cd540a1 (2023-08-24 14:43 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × Intel(R) Core(TM) i7-3770K CPU @ 3.50GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, ivybridge)
  Threads: 1 on 8 virtual cores
Environment:
  JULIA_REVISE_INCLUDE = 1

```

---

<div class="post-metadata">

**Author:** ![dleather](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dleather/32/43762_2.png) [@dleather](https://discourse.julialang.org/u/dleather)\
**Post date:** [September 28, 2023, 7:59pm UTC](https://discourse.julialang.org/t/stackoverflowerror-with-modellingtoolkit-jl-and-nanmath-jl/104364/3 "2023-09-28T19:59:11Z")

</div>

Here is my output from `pkg.st()`:

```julia
(NonlinearTSM) pkg> st
Project NonlinearTSM v0.1.0
Status `C:\Users\davle\Dropbox (Chapman)\Research\Projects\LeatherSagi (Private)\code\julia_conversion_simulation\NonlinearTSM\Project.toml`
  [6e4b80f9] BenchmarkTools v1.3.2
  [0c46a032] DifferentialEquations v7.10.0
  [31c24e10] Distributions v0.25.101
  [442a2c76] FastGaussQuadrature v0.5.1
  [5903a43b] Infiltrator v1.6.4
  [4138dd39] JLD v0.13.3
  [23992714] MAT v0.10.6
⌃ [961ee093] ModelingToolkit v8.70.0
  [77ba4419] NaNMath v1.0.2
  [d96e819e] Parameters v0.12.3
  [91a5bcdd] Plots v1.39.0
  [1fd47b50] QuadGK v2.9.1
  [295af30f] Revise v3.5.5
  [1ed8b502] SciMLSensitivity v7.40.0
⌃ [2913bbd2] StatsBase v0.34.0
  [789caeaf] StochasticDiffEq v6.62.0
  [220ca800] StructuralIdentifiability v0.4.10
  [fce5fe82] Turing v0.29.1
Info Packages marked with ⌃ have new versions available and may be upgradable.

```

And `versioninfo()`:

```julia
julia> versioninfo()
Julia Version 1.9.2
Commit e4ee485e90 (2023-07-05 09:39 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 12 × AMD Ryzen 5 5600 6-Core Processor
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, znver3)
  Threads: 1 on 12 virtual cores
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS =

```

---

<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:** [September 28, 2023, 8:00pm UTC](https://discourse.julialang.org/t/stackoverflowerror-with-modellingtoolkit-jl-and-nanmath-jl/104364/4 "2023-09-28T20:00:48Z")

</div>

Try updating, it looks like there is at least a new version of ModelingToolkit. If it still doesn’t work, post the full stack trace.

---

<div class="post-metadata">

**Author:** ![dleather](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dleather/32/43762_2.png) [@dleather](https://discourse.julialang.org/u/dleather)\
**Post date:** [September 28, 2023, 8:13pm UTC](https://discourse.julialang.org/t/stackoverflowerror-with-modellingtoolkit-jl-and-nanmath-jl/104364/5 "2023-09-28T20:13:08Z")

</div>

There must be another package preventing ModellingToolkit from upgrading.

Here is the stacktrace:

```julia
ERROR: StackOverflowError:
Stacktrace:
 [1] sqrt(x::Num) (repeats 79984 times)
   @ NaNMath C:\Users\davle\.julia\packages\NaNMath\ceWIc\src\NaNMath.jl:18

```

---

<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:** [September 28, 2023, 8:21pm UTC](https://discourse.julialang.org/t/stackoverflowerror-with-modellingtoolkit-jl-and-nanmath-jl/104364/6 "2023-09-28T20:21:27Z")

</div>

Yup, that looks bad. With the versions I have installed I hit an entirely different dispatch in `Symbolics.jl`. Try `add ModelingToolkit@8.71.0` in package mode, that should give you an error about what is holding it back.

---

<div class="post-metadata">

**Author:** ![dleather](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dleather/32/43762_2.png) [@dleather](https://discourse.julialang.org/u/dleather)\
**Post date:** [September 28, 2023, 9:04pm UTC](https://discourse.julialang.org/t/stackoverflowerror-with-modellingtoolkit-jl-and-nanmath-jl/104364/7 "2023-09-28T21:04:36Z")

</div>

The issue was with StructuralIdentifiability.jl downgrading the Symbolics.jl package.
