Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deepcopy leads to a failure in the initialization #3307

Closed
SebastianM-C opened this issue Jan 10, 2025 · 2 comments
Closed

deepcopy leads to a failure in the initialization #3307

SebastianM-C opened this issue Jan 10, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@SebastianM-C
Copy link
Contributor

Describe the bug 🐞

I noticed that with MTK@9.60 I can get an initialization failure if I deepcopy a particular system. I cannot reproduce it anymore with the current master, but I don't know if this issue was solve or it just no longer manifests.

Expected behavior

I would not expect to see any differences in behavior due to deepcopy.

Minimal Reproducible Example 👇

using ModelingToolkit, OrdinaryDiffEqRosenbrock
using ModelingToolkitStandardLibrary.Electrical
using ModelingToolkitStandardLibrary.Blocks: Sine
using ModelingToolkit: t_nounits as t

function create_model(; C₁=2.4, C₂=60.0, f=1.0)
    # V = 10.0
    @named resistor1 = Resistor(R=5.0)
    @named resistor2 = Resistor(R=2.0)
    @named capacitor1 = Capacitor(C=C₁)
    @named capacitor2 = Capacitor(C=C₂)
    @named source = Voltage()
    @named input_signal = Sine(frequency=f)
    @named ground = Ground()
    @named ampermeter = CurrentSensor()

    eqs = [connect(input_signal.output, source.V)
        connect(source.p, capacitor1.n, capacitor2.n)
        connect(source.n, resistor1.p, resistor2.p, ground.g)
        connect(resistor1.n, capacitor1.p, ampermeter.n)
        connect(resistor2.n, capacitor2.p, ampermeter.p)]

    @named circuit_model = ODESystem(eqs, t,
        systems=[
            resistor1, resistor2, capacitor1, capacitor2,
            source, input_signal, ground, ampermeter
        ])
end

model = create_model()
sys = structural_simplify(model)

@unpack ampermeter, capacitor2, capacitor1, resistor2 = model

ModelingToolkit.InitializationProblem(sys, 0, [capacitor2.v => 0.0,]) # works

ModelingToolkit.InitializationProblem(deepcopy(sys), 0, [capacitor2.v => 0.0,]) # ERROR: Initialization incomplete.

Error & Stacktrace ⚠️

ERROR: Initialization incomplete. Not all of the state variables of the
DAE system can be determined by the initialization. Missing
variables:

SymbolicUtils.BasicSymbolic{Real}[capacitor2₊v(t), capacitor1₊i(t)]

Stacktrace:
 [1] ModelingToolkit.InitializationProblem{…}(sys::ODESystem, t::Int64, u0map::Vector{…}, parammap::SciMLBase.NullParameters; guesses::Vector{…}, check_length::Bool, warn_initialize_determined::Bool, initialization_eqs::Vector{…}, fully_determined::Nothing, check_units::Bool, use_scc::Bool, kwargs::@Kwargs{})
   @ ModelingToolkit ~/.julia/packages/ModelingToolkit/iQ7So/src/systems/diffeqs/abstractodesystem.jl:1336
 [2] InitializationProblem
   @ ~/.julia/packages/ModelingToolkit/iQ7So/src/systems/diffeqs/abstractodesystem.jl:1285 [inlined]
 [3] ModelingToolkit.InitializationProblem{true, SciMLBase.AutoSpecialize}(sys::ODESystem, t::Int64, u0map::Vector{Pair{…}})
   @ ModelingToolkit ~/.julia/packages/ModelingToolkit/iQ7So/src/systems/diffeqs/abstractodesystem.jl:1285
 [4] (ModelingToolkit.InitializationProblem{true})(::ODESystem, ::Int64, ::Vararg{Any}; kwargs::@Kwargs{})
   @ ModelingToolkit ~/.julia/packages/ModelingToolkit/iQ7So/src/systems/diffeqs/abstractodesystem.jl:1263
 [5] (ModelingToolkit.InitializationProblem{true})(::ODESystem, ::Int64, ::Vararg{Any})
   @ ModelingToolkit ~/.julia/packages/ModelingToolkit/iQ7So/src/systems/diffeqs/abstractodesystem.jl:1262
 [6] ModelingToolkit.InitializationProblem(::ODESystem, ::Int64, ::Vararg{Any}; kwargs::@Kwargs{})
   @ ModelingToolkit ~/.julia/packages/ModelingToolkit/iQ7So/src/systems/diffeqs/abstractodesystem.jl:1251
 [7] ModelingToolkit.InitializationProblem(::ODESystem, ::Int64, ::Vararg{Any})
   @ ModelingToolkit ~/.julia/packages/ModelingToolkit/iQ7So/src/systems/diffeqs/abstractodesystem.jl:1250
 [8] top-level scope
   @ ~/bug.jl:194
Some type information was truncated. Use `show(err)` to see complete types.

Environment (please complete the following information):

  • Output of using Pkg; Pkg.status()
(dev) pkg> st -m ModelingToolkit ModelingToolkitStandardLibrary Symbolics SymbolicUtils
Status `~/dev/Manifest-v1.11.toml`
  [961ee093] ModelingToolkit v9.60.0
  [16a59e39] ModelingToolkitStandardLibrary v2.19.0
  [d1185830] SymbolicUtils v3.8.1
  [0c5d862f] Symbolics v6.22.1
  • Output of using Pkg; Pkg.status(; mode = PKGMODE_MANIFEST)
  • Output of versioninfo()
julia> versioninfo()
Julia Version 1.11.2
Commit 5e9a32e7af2 (2024-12-01 20:02 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 32 × Intel(R) Core(TM) i9-14900K
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, alderlake)
Threads: 32 default, 0 interactive, 16 GC (on 32 virtual cores)
Environment:
  JULIA_EDITOR = code
@AayushSabharwal
Copy link
Member

I'm not able to replicate this on master or MTK@9.60

@AayushSabharwal
Copy link
Member

Okay yeah this is a hashconsing issue. It is fixed on the latest releases of Symbolics/SymbolicUtils

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants