You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I define two structs. The first struct Fit contains some coefficients for functions nb and ε. This is then nested inside another struct EOS, which defines a new method for ε that combines Fit's methods. The ODE structure! uses ε to close the system of equations, where an instance of EOS is passed as a parameter to the ODEProblem.
Finally, the ODE is solved in the main function.
using DifferentialEquations
const conversion_factor =755400.0struct Fit
m₁::Float64
c₁::Float64
m₂::Float64
c₂::Float64functionFit()
m₁ =1.595
c₁ =3.438
m₂ =1.075
c₂ =3.484new(m₁, c₁, m₂, c₂)
endendnb(fit::Fit, p) =10^((log10(p) - fit.c₁)/fit.m₁)
ε(fit::Fit, nb) =10^(fit.m₂*log10(nb) + fit.c₂)
struct EOS
fit::FitfunctionEOS()
fit =Fit()
new(fit)
endendε(eos::EOS, p) =ε(eos.fit, nb(eos.fit, conversion_factor*p))/conversion_factor
functionstructure!(dy_dr, y, eos, r)
m, p = y
εᵣ =ε(eos, p)
dν_dr =2*(m +4*π*r^3*p)/(r*(r -2*m))
dy_dr[1] =4*π*r^2*εᵣ
dy_dr[2] =- (εᵣ + p)*dν_dr/2
dy_dr
endfunctionmain()
eos =EOS()
pc =1.19e-4
prob =ODEProblem{true}(structure!, [0, pc], (1e-5, 10), eos)
sol =solve(prob, Tsit5())
end
Describe the bug 🐞
Solving an ODE with nested structs is type-unstable.
Expected behavior
Julia's compiler should be able to fully infer the type of the
ODESolution
.Minimal Reproducible Example 👇
The type-instability arises when one struct is nested within another. Here I include an example when integrating the relativistic stellar-structure equations.
I define two structs. The first struct
Fit
contains some coefficients for functionsnb
andε
. This is then nested inside another structEOS
, which defines a new method forε
that combinesFit
's methods. The ODEstructure!
usesε
to close the system of equations, where an instance ofEOS
is passed as a parameter to theODEProblem
.Finally, the ODE is solved in the
main
function.Error & Stacktrace⚠️
When I examine the types, I find
The type-instability can be seen with the
sol
variable as typeAny
.Environment (please complete the following information):
using Pkg; Pkg.status()
using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
versioninfo()
Additional context
This issue was initially raised on the Discourse. There are two current workarounds for this type-instability:
DiffEqBase.anyeltypedual(eos::EOS, counter=0) = Any
.The text was updated successfully, but these errors were encountered: