StackOverflowError() with ModellingToolkit.jl and NaNMath.jl

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

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)]

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

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

Here is my output from pkg.st():

(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> 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 =

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.

1 Like

There must be another package preventing ModellingToolkit from upgrading.

Here is the stacktrace:

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

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.

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