# Is it possible to simulate a resistive network using ModelingToolkit?

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit\
**Created:** [November 15, 2024, 6:07pm UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690 "2024-11-15T18:07:29Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ricardo\_Borges](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ricardo_borges/32/19918_2.png) [@Ricardo\_Borges](https://discourse.julialang.org/u/Ricardo_Borges)\
**Post date:** [November 15, 2024, 6:07pm UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690/1 "2024-11-15T18:07:29Z")

</div>

Hi,

I’m trying to simulate a simple circuit of a constant voltage source connected to a resistor, using MTK. But I’m getting empty results when I solve the problem. Bear in mind this is a simple, toy problem, which I could solve using Symbolics or SymPy, but Iḿ looking ahead where I need to simulate the system with varying voltages, etc.

Below is the toy problem:

```julia
using ModelingToolkit, OrdinaryDiffEq, Plots
using ModelingToolkitStandardLibrary.Electrical
using ModelingToolkitStandardLibrary.Blocks: Constant
using ModelingToolkit: t_nounits as t

@mtkmodel simple_resistor begin
    @parameters begin
        V = 800.0
        R1 = 4.0*499.0e3
    end
    @components begin
        r1 = Resistor(R = R1)
        source = Voltage()
        constant = Constant(k = V)
        ground = Ground()
    end
    @equations begin
        connect(constant.output, source.V)
        connect(source.p, r1.p)
        connect(source.n, r1.n, ground.g)
    end
end

@mtkbuild sys = simple_resistor()

prob=ODEProblem(sys, [], (0, 10.0))

sol = solve(prob)

```

The result of solve is:

```julia
retcode: Success
Interpolation: 1st order linear
t: 2-element Vector{Float64}:
  0.0
 10.0
u: 2-element Vector{Vector{Float64}}:
 []
 []

```

The return of using Pkg, Pkg.status() is

```julia
Status `~/.julia/environments/v1.11/Project.toml`
  [6e4b80f9] BenchmarkTools v1.5.0
  [a93c6f00] DataFrames v1.7.0
⌃ [82cc6244] DataInterpolations v6.5.2
⌃ [0c46a032] DifferentialEquations v7.14.0
  [0b43b601] Groebner v0.8.2
⌃ [7073ff75] IJulia v1.25.0
  [a98d9a8b] Interpolations v0.15.1
⌃ [961ee093] ModelingToolkit v9.49.0
  [16a59e39] ModelingToolkitStandardLibrary v2.17.0
⌅ [8913a72c] NonlinearSolve v3.15.1
⌃ [1dea7af3] OrdinaryDiffEq v6.89.0
⌃ [f0f68f2c] PlotlyJS v0.18.14
⌃ [91a5bcdd] Plots v1.40.8
  [24249f21] SymPy v2.2.0
  [2efcf032] SymbolicIndexingInterface v0.3.34

```

The return of versioninfo() is:

```julia
Julia Version 1.11.1
Commit 8f5b7ca12ad (2024-10-16 10:53 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 24 × AMD Ryzen 9 5900X 12-Core Processor
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, znver3)
Threads: 1 default, 0 interactive, 1 GC (on 24 virtual cores)

```

---

<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:** [November 15, 2024, 7:17pm UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690/2 "2024-11-15T19:17:06Z")

</div>

This system is linear with no differential equations so it analytically solves it. You can use symbolic indexing to recover any of the values.

```julia
julia> sol[sys.r1.v]
2-element Vector{Float64}:
 800.0
 800.0

julia> sol(5.0; idxs = sys.r1.v)
800.0

```

---

<div class="post-metadata">

**Author:** ![Ricardo\_Borges](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ricardo_borges/32/19918_2.png) [@Ricardo\_Borges](https://discourse.julialang.org/u/Ricardo_Borges)\
**Post date:** [November 15, 2024, 9:00pm UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690/3 "2024-11-15T21:00:19Z")

</div>

Many thanks! That solved the issue!

---

<div class="post-metadata">

**Author:** ![Ricardo\_Borges](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ricardo_borges/32/19918_2.png) [@Ricardo\_Borges](https://discourse.julialang.org/u/Ricardo_Borges)\
**Post date:** [November 15, 2024, 9:08pm UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690/4 "2024-11-15T21:08:49Z")

</div>

I’m finding another issue when I place a second resistor in series with r1. The updated script is below:

```julia
using ModelingToolkit, OrdinaryDiffEq, Plots
using ModelingToolkitStandardLibrary.Electrical
using ModelingToolkitStandardLibrary.Blocks: Constant
using ModelingToolkit: t_nounits as t

@mtkmodel simple_resistor begin
    @parameters begin
        V = 800.0
        R1 = 4.0*499.0e3
        R2 = 4.0*499.0e3
    end
    @components begin
        r1 = Resistor(R = R1)
        r2 = Resistor(R = R2)
        source = Voltage()
        constant = Constant(k = V)
        ground = Ground()
    end
    @equations begin
        connect(constant.output, source.V)
        connect(source.p, r1.p)
        connect(r1.n, r2.p)
        connect(source.n, r2.n, ground.g)
    end
end

@mtkbuild sys = simple_resistor()

prob=ODEProblem(sys, [], (0, 10.0))

sol = solve(prob)

```

The “prob=ODEProblem(sys, , (0, 10.0))” fails with:

```julia
┌ Warning: Did not converge after `maxiters = 0` substitutions. Either there is a cycle in the rules or `maxiters` needs to be higher.
└ @ Symbolics /home/ricardo/.julia/packages/Symbolics/6CYZh/src/variable.jl:528

{
	"name": "ArgumentError",
	"message": "ArgumentError: SymbolicUtils.BasicSymbolic{Real}[1.996e6r1₊i(t)] are either missing from the variable map or missing from the system's unknowns/parameters list.",
	"stack": "ArgumentError: SymbolicUtils.BasicSymbolic{Real}[1.996e6r1₊i(t)] are either missing from the variable map or missing from the system's unknowns/parameters list.

Stacktrace:
  [1] throw_missingvars_in_sys(vars::Vector{SymbolicUtils.BasicSymbolic{Real}})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/eiNg3/src/utils.jl:771
  [2] promote_to_concrete(vs::Vector{SymbolicUtils.BasicSymbolic{Real}}; tofloat::Bool, use_union::Bool)
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/eiNg3/src/utils.jl:790
  [3] better_varmap_to_vars(varmap::Dict{Any, Any}, vars::Vector{SymbolicUtils.BasicSymbolic{Real}}; tofloat::Bool, use_union::Bool, container_type::Type, toterm::Function, promotetoconcrete::Nothing, check::Bool, allow_symbolic::Bool)
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/eiNg3/src/systems/problem_utils.jl:288
  [4] process_SciMLProblem(constructor::Type, sys::NonlinearSystem, u0map::Dict{SymbolicUtils.BasicSymbolic{Real}, Float64}, pmap::Dict{SymbolicUtils.BasicSymbolic{Real}, Int64}; build_initializeprob::Bool, implicit_dae::Bool, t::Nothing, guesses::Dict{Any, Any}, warn_initialize_determined::Bool, initialization_eqs::Vector{Any}, eval_expression::Bool, eval_module::Module, fully_determined::Bool, check_initialization_units::Bool, tofloat::Bool, use_union::Bool, u0_constructor::typeof(identity), du0map::Nothing, check_length::Bool, symbolic_u0::Bool, kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/eiNg3/src/systems/problem_utils.jl:508
  [5] process_SciMLProblem
    @ ~/.julia/packages/ModelingToolkit/eiNg3/src/systems/problem_utils.jl:413 [inlined]
  [6] (NonlinearProblem{true})(sys::NonlinearSystem, u0map::Dict{SymbolicUtils.BasicSymbolic{Real}, Float64}, parammap::Dict{SymbolicUtils.BasicSymbolic{Real}, Int64}; check_length::Bool, kwargs::@Kwargs{eval_expression::Bool, eval_module::Module})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/eiNg3/src/systems/nonlinear/nonlinearsystem.jl:499
  [7] NonlinearProblem(::NonlinearSystem, ::Dict{SymbolicUtils.BasicSymbolic{Real}, Float64}, ::Vararg{Any}; kwargs::@Kwargs{eval_expression::Bool, eval_module::Module})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/eiNg3/src/systems/nonlinear/nonlinearsystem.jl:490
  [8] ModelingToolkit.InitializationProblem{true, SciMLBase.AutoSpecialize}(sys::ODESystem, t::Int64, u0map::Dict{Any, Any}, parammap::Dict{Any, Any}; guesses::Dict{Any, Any}, check_length::Bool, warn_initialize_determined::Bool, initialization_eqs::Vector{Any}, fully_determined::Bool, check_units::Bool, kwargs::@Kwargs{eval_expression::Bool, eval_module::Module})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/eiNg3/src/systems/diffeqs/abstractodesystem.jl:1330
  [9] (ModelingToolkit.InitializationProblem{true})(::ODESystem, ::Int64, ::Vararg{Any}; kwargs::@Kwargs{guesses::Dict{Any, Any}, warn_initialize_determined::Bool, initialization_eqs::Vector{Any}, eval_expression::Bool, eval_module::Module, fully_determined::Bool, check_units::Bool})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/eiNg3/src/systems/diffeqs/abstractodesystem.jl:1232
 [10] ModelingToolkit.InitializationProblem(::ODESystem, ::Int64, ::Vararg{Any}; kwargs::@Kwargs{guesses::Dict{Any, Any}, warn_initialize_determined::Bool, initialization_eqs::Vector{Any}, eval_expression::Bool, eval_module::Module, fully_determined::Bool, check_units::Bool})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/eiNg3/src/systems/diffeqs/abstractodesystem.jl:1220
 [11] process_SciMLProblem(constructor::Type, sys::ODESystem, u0map::Vector{Any}, pmap::SciMLBase.NullParameters; build_initializeprob::Bool, implicit_dae::Bool, t::Int64, guesses::Dict{Any, Any}, warn_initialize_determined::Bool, initialization_eqs::Vector{Any}, eval_expression::Bool, eval_module::Module, fully_determined::Bool, check_initialization_units::Bool, tofloat::Bool, use_union::Bool, u0_constructor::typeof(identity), du0map::Nothing, check_length::Bool, symbolic_u0::Bool, kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/eiNg3/src/systems/problem_utils.jl:466
 [12] process_SciMLProblem
    @ ~/.julia/packages/ModelingToolkit/eiNg3/src/systems/problem_utils.jl:413 [inlined]
 [13] (ODEProblem{true, SciMLBase.AutoSpecialize})(sys::ODESystem, u0map::Vector{Any}, tspan::Tuple{Int64, Float64}, parammap::SciMLBase.NullParameters; callback::Nothing, check_length::Bool, warn_initialize_determined::Bool, eval_expression::Bool, eval_module::Module, kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/eiNg3/src/systems/diffeqs/abstractodesystem.jl:811
 [14] ODEProblem
    @ ~/.julia/packages/ModelingToolkit/eiNg3/src/systems/diffeqs/abstractodesystem.jl:799 [inlined]
 [15] (ODEProblem{true, SciMLBase.AutoSpecialize})(sys::ODESystem, u0map::Vector{Any}, tspan::Tuple{Int64, Float64})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/eiNg3/src/systems/diffeqs/abstractodesystem.jl:799
 [16] (ODEProblem{true})(::ODESystem, ::Vector{Any}, ::Vararg{Any}; kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/eiNg3/src/systems/diffeqs/abstractodesystem.jl:786
 [17] (ODEProblem{true})(::ODESystem, ::Vector{Any}, ::Vararg{Any})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/eiNg3/src/systems/diffeqs/abstractodesystem.jl:785
 [18] ODEProblem(::ODESystem, ::Vector{Any}, ::Vararg{Any}; kwargs::@Kwargs{})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/eiNg3/src/systems/diffeqs/abstractodesystem.jl:775
 [19] ODEProblem(::ODESystem, ::Vector{Any}, ::Vararg{Any})
    @ ModelingToolkit ~/.julia/packages/ModelingToolkit/eiNg3/src/systems/diffeqs/abstractodesystem.jl:774
 [20] top-level scope
    @ ~/jupyter/isolation_sensing/jl_notebook_cell_df34fa98e69747e1a8f8a730347b8e2f_W3sZmlsZQ==.jl:1"
}

```

---

<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:** [November 15, 2024, 10:33pm UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690/5 "2024-11-15T22:33:08Z")

</div>

In your second case, there is one unknown:

```julia
julia> @mtkbuild sys = simple_resistor()
Model sys:
Equations (1):
  1 standard: see equations(sys)
Unknowns (1): see unknowns(sys)
  r2₊i(t)
Parameters (6): see parameters(sys)
  R1 [defaults to 1.996e6]
  constant₊k [defaults to V]: Constant output value of block
  r2₊R [defaults to R2]: Resistance
  R2 [defaults to 1.996e6]
  r1₊R [defaults to R1]: Resistance
  V [defaults to 800.0]
Observed (21): see observed(sys)

```

So you need to supply an initial condition for it. In this case, you can supply anything you want and it will be treated as an initial guess for the solver.

```julia
julia> prob=ODEProblem(sys, [0.0], (0, 10.0))
ODEProblem with uType Vector{Float64} and tType Float64. In-place: true
timespan: (0.0, 10.0)
u0: 1-element Vector{Float64}:
 0.0

julia> sol = solve(prob);

julia> sol(0, idxs=[sys.r1.v, sys.r2.v])
2-element Vector{Float64}:
 399.99999994373604
 400.00000005626396

```

---

<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:** [November 17, 2024, 10:59am UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690/6 "2024-11-17T10:59:44Z")

</div>

And you might as well use NonlinearProblem instead of ODEProblem in situations like this with no ODEs.

---

<div class="post-metadata">

**Author:** ![Ricardo\_Borges](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ricardo_borges/32/19918_2.png) [@Ricardo\_Borges](https://discourse.julialang.org/u/Ricardo_Borges)\
**Post date:** [November 18, 2024, 5:43pm UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690/7 "2024-11-18T17:43:43Z")

</div>

Many thanks for the tip!

By the way, is there a method to print the nice model information you quoted there?

```julia
Model sys:
Equations (1):
  1 standard: see equations(sys)
Unknowns (1): see unknowns(sys)
...

```

---

<div class="post-metadata">

**Author:** ![Ricardo\_Borges](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ricardo_borges/32/19918_2.png) [@Ricardo\_Borges](https://discourse.julialang.org/u/Ricardo_Borges)\
**Post date:** [November 18, 2024, 5:45pm UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690/8 "2024-11-18T17:45:24Z")

</div>

cool, thanks, good to know!

I checked in [this link](https://docs.sciml.ai/ModelingToolkit/stable/tutorials/nonlinear/) that the `@mtkmodel` macro does not support `NonlinearSystem` yet, so I’ll stick with the ODEProblem for the time being.

---

<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:** [November 18, 2024, 5:52pm UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690/9 "2024-11-18T17:52:08Z")

</div>

If you mean exactly what I quoted there, that is the `show` method as used by the REPL display. I evaluated the expression from your code in the REPL and quoted what was printed.

If you mean digging in to more details, you can use `unknowns(sys)`, `equations(sys)`, or `observed(sys)` to show the full list of each.

---

<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:** [November 18, 2024, 6:09pm UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690/10 "2024-11-18T18:09:14Z")

</div>

You just do `NonlinearProblem ` on an ODE and it’ll work.

---

<div class="post-metadata">

**Author:** ![Ricardo\_Borges](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ricardo_borges/32/19918_2.png) [@Ricardo\_Borges](https://discourse.julialang.org/u/Ricardo_Borges)\
**Post date:** [November 18, 2024, 7:33pm UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690/11 "2024-11-18T19:33:52Z")

</div>

By that, I guess you mean the following, which works fine:

```julia
using NonlinearSolve
prob_NL = NonlinearProblem(prob)

NonlinearProblem with uType Nothing. In-place: true
u0: nothing

```

but when I solve, it returns empty:

```julia
sol_NL = solve(prob_NL)
retcode: Success
u: Float64[]

show(sol_NL)
Float64[]

```

---

<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:** [November 18, 2024, 7:54pm UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690/12 "2024-11-18T19:54:16Z")

</div>

Yes because the solution is trivial (analytically solved, no numeric), but again symbolic indexing works fine.

---

<div class="post-metadata">

**Author:** ![Ricardo\_Borges](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ricardo_borges/32/19918_2.png) [@Ricardo\_Borges](https://discourse.julialang.org/u/Ricardo_Borges)\
**Post date:** [November 19, 2024, 1:57pm UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690/13 "2024-11-19T13:57:49Z")

</div>

> [@Ricardo\_Borges](#):
>
> `sol_NL = solve(prob_NL)`

by that, I guess you mean:

```julia
sol_NL[sys.r1.v]

```

but then I get:

```julia
BoundsError: attempt to access Tuple{Vector{Float64}, MTKParameters{Vector{Float64}, Tuple{}, Tuple{}, Tuple{}}} at index [3]

Stacktrace:
 [1] getindex
   @ ./tuple.jl:31 [inlined]
 [2] macro expansion
   @ ~/.julia/packages/RuntimeGeneratedFunctions/M9ZX8/src/RuntimeGeneratedFunctions.jl:162 [inlined]
 [3] macro expansion
   @ ./none:0 [inlined]
 [4] generated_callfunc
   @ ./none:0 [inlined]
 [5] (::RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(Symbol("##arg#5964805160111424296"), : ___mtkparameters___ , :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x280425a4, 0x4a32d630, 0x943b2a62, 0x38dff8e0, 0x05cd040d), Nothing})(::Vector{Float64}, ::MTKParameters{Vector{Float64}, Tuple{}, Tuple{}, Tuple{}})
   @ RuntimeGeneratedFunctions ~/.julia/packages/RuntimeGeneratedFunctions/M9ZX8/src/RuntimeGeneratedFunctions.jl:150
 [6] (::SymbolicIndexingInterface.TimeIndependentObservedFunction{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(Symbol("##arg#5964805160111424296"), : ___mtkparameters___ , :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x280425a4, 0x4a32d630, 0x943b2a62, 0x38dff8e0, 0x05cd040d), Nothing}})(::SymbolicIndexingInterface.NotTimeseries, prob::SciMLBase.NonlinearSolution{Float64, 1, Vector{Float64}, Nothing, NonlinearProblem{Nothing, true, MTKParameters{Vector{Float64}, Tuple{}, Tuple{}, Tuple{}}, NonlinearFunction{true, SciMLBase.AutoSpecialize, SciMLBase.var"#37#46"{ODEFunction{true, SciMLBase.AutoSpecialize, ModelingToolkit.var"#f#911"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x47755f63, 0x54879625, 0xa6ce3993, 0x677dc31f, 0x0ea26d1b), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xe4768855, 0xc75a2b9e, 0xbdbf2985, 0x3b9edfca, 0xfaebd888), Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ODESystem}, Nothing, ODESystem, Nothing, Nothing, Nothing, Nothing, Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ODESystem}, Nothing, ODESystem, Nothing}, @Kwargs{}, SciMLBase.StandardNonlinearProblem}, Nothing, Nothing, Nothing, Nothing, Nothing})
   @ SymbolicIndexingInterface ~/.julia/packages/SymbolicIndexingInterface/sGNlE/src/state_indexing.jl:142
 [7] (::SymbolicIndexingInterface.TimeIndependentObservedFunction{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(Symbol("##arg#5964805160111424296"), : ___mtkparameters___ , :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x280425a4, 0x4a32d630, 0x943b2a62, 0x38dff8e0, 0x05cd040d), Nothing}})(prob::SciMLBase.NonlinearSolution{Float64, 1, Vector{Float64}, Nothing, NonlinearProblem{Nothing, true, MTKParameters{Vector{Float64}, Tuple{}, Tuple{}, Tuple{}}, NonlinearFunction{true, SciMLBase.AutoSpecialize, SciMLBase.var"#37#46"{ODEFunction{true, SciMLBase.AutoSpecialize, ModelingToolkit.var"#f#911"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x47755f63, 0x54879625, 0xa6ce3993, 0x677dc31f, 0x0ea26d1b), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xe4768855, 0xc75a2b9e, 0xbdbf2985, 0x3b9edfca, 0xfaebd888), Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ODESystem}, Nothing, ODESystem, Nothing, Nothing, Nothing, Nothing, Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ODESystem}, Nothing, ODESystem, Nothing}, @Kwargs{}, SciMLBase.StandardNonlinearProblem}, Nothing, Nothing, Nothing, Nothing, Nothing})
   @ SymbolicIndexingInterface ~/.julia/packages/SymbolicIndexingInterface/sGNlE/src/value_provider_interface.jl:166
 [8] getindex(A::SciMLBase.NonlinearSolution{Float64, 1, Vector{Float64}, Nothing, NonlinearProblem{Nothing, true, MTKParameters{Vector{Float64}, Tuple{}, Tuple{}, Tuple{}}, NonlinearFunction{true, SciMLBase.AutoSpecialize, SciMLBase.var"#37#46"{ODEFunction{true, SciMLBase.AutoSpecialize, ModelingToolkit.var"#f#911"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x47755f63, 0x54879625, 0xa6ce3993, 0x677dc31f, 0x0ea26d1b), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0xe4768855, 0xc75a2b9e, 0xbdbf2985, 0x3b9edfca, 0xfaebd888), Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ODESystem}, Nothing, ODESystem, Nothing, Nothing, Nothing, Nothing, Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ODESystem}, Nothing, ODESystem, Nothing}, @Kwargs{}, SciMLBase.StandardNonlinearProblem}, Nothing, Nothing, Nothing, Nothing, Nothing}, sym::Num)
   @ SciMLBase ~/.julia/packages/SciMLBase/hJh6T/src/solutions/solution_interface.jl:117
 [9] top-level scope
   @ ~/jupyter/isolation_sensing/jl_notebook_cell_df34fa98e69747e1a8f8a730347b8e2f_W4sZmlsZQ==.jl:1

```

---

<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:** [November 19, 2024, 6:19pm UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690/14 "2024-11-19T18:19:38Z")

</div>

> [@Ricardo\_Borges](#):
>
> ```julia
> using NonlinearSolve
> prob_NL = NonlinearProblem(prob)
> 
> ```

Oh, when we nest we problems we might need to pass on SII @cryptic.ax .

Using SteadyStateProblem here is fine:

```julia
using NonlinearSolve
prob_NL = SteadyStateProblem(sys, [sys.r2.i => 0.0])
sol_NL = solve(prob_NL, TrustRegion())

```

```julia
julia> sol_NL[sys.r2.i]
0.0002004008016032064

```

---

<div class="post-metadata">

**Author:** ![cryptic.ax](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cryptic.ax/32/220540_2.png) [@cryptic.ax](https://discourse.julialang.org/u/cryptic.ax)\
**Post date:** [November 21, 2024, 7:16am UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690/15 "2024-11-21T07:16:13Z")

</div>

> Oh, when we nest we problems we might need to pass on SII @cryptic.ax .

That’s not the issue here. The system inside `sol_NL` is an `ODESystem`. It generates observed functions of the form `f(u, p, t)`, but since `sol_NL` is not time dependent, SII calls the function has `f(u, p)` which leads to the above issue. In SII terminology, the index provider is time-dependent but the value provider isn’t.

---

<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:** [November 21, 2024, 10:00am UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690/16 "2024-11-21T10:00:02Z")

</div>

Ahh we neeed to make that conversion better

---

<div class="post-metadata">

**Author:** ![Ricardo\_Borges](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ricardo_borges/32/19918_2.png) [@Ricardo\_Borges](https://discourse.julialang.org/u/Ricardo_Borges)\
**Post date:** [November 21, 2024, 12:15pm UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690/17 "2024-11-21T12:15:15Z")

</div>

Cool, thanks for the reply!

I opened a bug issue here, even though this seems to me more like an improvement (there was no “improvement” issue type):

> <https://github.com/SciML/NonlinearSolve.jl/issues/510>
>
> \*\*Describe the bug 🐞\*\*
> 
> This bug has been posted here:
> https://discourse.juli…alang.org/t/is-it-possible-to-simulate-a-resistive-network-using-modelingtoolkit/122690/13
> 
> When nesting an ODEProblem within a NonlinearProblem, an error happens.
> 
> \*\*Expected behavior\*\*
> 
> Nesting ODEProblems within NonlinearProblem should work fine.
> 
> \*\*Minimal Reproducible Example 👇\*\*
> 
> The following @mtkmodel works fine:
> 
> \`\`\`julia
> using ModelingToolkit, OrdinaryDiffEq, Plots
> using ModelingToolkitStandardLibrary.Electrical
> using ModelingToolkitStandardLibrary.Blocks: Constant
> using ModelingToolkit: t\_nounits as t
> 
> @mtkmodel simple\_resistor begin
> @parameters begin
> V = 800.0
> R1 = 4.0\*499.0e3
> R2 = 4.0\*499.0e3
> end
> @components begin
> r1 = Resistor(R = R1)
> r2 = Resistor(R = R2)
> source = Voltage()
> constant = Constant(k = V)
> ground = Ground()
> end
> @equations begin
> connect(constant.output, source.V)
> connect(source.p, r1.p)
> connect(r1.n, r2.p)
> connect(source.n, r2.n, ground.g)
> end
> end
> 
> @mtkbuild sys = simple\_resistor()
> 
> prob=ODEProblem(sys, \[0.0\], (0, 10.0))
> 
> sol = solve(prob)
> \`\`\`
> 
> But when the ODEProblem is nested within a NonlinearProblem like this:
> 
> \`\`\`julia
> using NonlinearSolve
> prob\_NL = NonlinearProblem(prob)
> sol\_NL = solve(prob\_NL)
> \`\`\`
> 
> when trying to access the solution using symbolic indexing
> 
> \`\`\`julia
> sol\_NL\[sys.r1.v\]
> \`\`\`
> 
> retunrs this error:
> 
> \*\*Error & Stacktrace ⚠️\*\*
> 
> \`\`\`julia
> BoundsError: attempt to access Tuple{Vector{Float64}, MTKParameters{Vector{Float64}, Tuple{}, Tuple{}, Tuple{}}} at index \[3\]
> 
> Stacktrace:
> \[1\] getindex
> @ ./tuple.jl:31 \[inlined\]
> \[2\] macro expansion
> @ ~/.julia/packages/RuntimeGeneratedFunctions/M9ZX8/src/RuntimeGeneratedFunctions.jl:162 \[inlined\]
> \[3\] macro expansion
> @ ./none:0 \[inlined\]
> \[4\] generated\_callfunc
> @ ./none:0 \[inlined\]
> \[5\] (::RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(Symbol("##arg#5964805160111424296"), :\_\_\_mtkparameters\_\_\_, :t), ModelingToolkit.var"#\_RGF\_ModTag", ModelingToolkit.var"#\_RGF\_ModTag", (0x280425a4, 0x4a32d630, 0x943b2a62, 0x38dff8e0, 0x05cd040d), Nothing})(::Vector{Float64}, ::MTKParameters{Vector{Float64}, Tuple{}, Tuple{}, Tuple{}})
> @ RuntimeGeneratedFunctions ~/.julia/packages/RuntimeGeneratedFunctions/M9ZX8/src/RuntimeGeneratedFunctions.jl:150
> \[6\] (::SymbolicIndexingInterface.TimeIndependentObservedFunction{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(Symbol("##arg#5964805160111424296"), :\_\_\_mtkparameters\_\_\_, :t), ModelingToolkit.var"#\_RGF\_ModTag", ModelingToolkit.var"#\_RGF\_ModTag", (0x280425a4, 0x4a32d630, 0x943b2a62, 0x38dff8e0, 0x05cd040d), Nothing}})(::SymbolicIndexingInterface.NotTimeseries, prob::SciMLBase.NonlinearSolution{Float64, 1, Vector{Float64}, Nothing, NonlinearProblem{Nothing, true, MTKParameters{Vector{Float64}, Tuple{}, Tuple{}, Tuple{}}, NonlinearFunction{true, SciMLBase.AutoSpecialize, SciMLBase.var"#37#46"{ODEFunction{true, SciMLBase.AutoSpecialize, ModelingToolkit.var"#f#911"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#\_RGF\_ModTag", ModelingToolkit.var"#\_RGF\_ModTag", (0x47755f63, 0x54879625, 0xa6ce3993, 0x677dc31f, 0x0ea26d1b), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#\_RGF\_ModTag", ModelingToolkit.var"#\_RGF\_ModTag", (0xe4768855, 0xc75a2b9e, 0xbdbf2985, 0x3b9edfca, 0xfaebd888), Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ODESystem}, Nothing, ODESystem, Nothing, Nothing, Nothing, Nothing, Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ODESystem}, Nothing, ODESystem, Nothing}, @Kwargs{}, SciMLBase.StandardNonlinearProblem}, Nothing, Nothing, Nothing, Nothing, Nothing})
> @ SymbolicIndexingInterface ~/.julia/packages/SymbolicIndexingInterface/sGNlE/src/state\_indexing.jl:142
> \[7\] (::SymbolicIndexingInterface.TimeIndependentObservedFunction{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(Symbol("##arg#5964805160111424296"), :\_\_\_mtkparameters\_\_\_, :t), ModelingToolkit.var"#\_RGF\_ModTag", ModelingToolkit.var"#\_RGF\_ModTag", (0x280425a4, 0x4a32d630, 0x943b2a62, 0x38dff8e0, 0x05cd040d), Nothing}})(prob::SciMLBase.NonlinearSolution{Float64, 1, Vector{Float64}, Nothing, NonlinearProblem{Nothing, true, MTKParameters{Vector{Float64}, Tuple{}, Tuple{}, Tuple{}}, NonlinearFunction{true, SciMLBase.AutoSpecialize, SciMLBase.var"#37#46"{ODEFunction{true, SciMLBase.AutoSpecialize, ModelingToolkit.var"#f#911"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#\_RGF\_ModTag", ModelingToolkit.var"#\_RGF\_ModTag", (0x47755f63, 0x54879625, 0xa6ce3993, 0x677dc31f, 0x0ea26d1b), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#\_RGF\_ModTag", ModelingToolkit.var"#\_RGF\_ModTag", (0xe4768855, 0xc75a2b9e, 0xbdbf2985, 0x3b9edfca, 0xfaebd888), Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ODESystem}, Nothing, ODESystem, Nothing, Nothing, Nothing, Nothing, Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ODESystem}, Nothing, ODESystem, Nothing}, @Kwargs{}, SciMLBase.StandardNonlinearProblem}, Nothing, Nothing, Nothing, Nothing, Nothing})
> @ SymbolicIndexingInterface ~/.julia/packages/SymbolicIndexingInterface/sGNlE/src/value\_provider\_interface.jl:166
> \[8\] getindex(A::SciMLBase.NonlinearSolution{Float64, 1, Vector{Float64}, Nothing, NonlinearProblem{Nothing, true, MTKParameters{Vector{Float64}, Tuple{}, Tuple{}, Tuple{}}, NonlinearFunction{true, SciMLBase.AutoSpecialize, SciMLBase.var"#37#46"{ODEFunction{true, SciMLBase.AutoSpecialize, ModelingToolkit.var"#f#911"{RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#\_RGF\_ModTag", ModelingToolkit.var"#\_RGF\_ModTag", (0x47755f63, 0x54879625, 0xa6ce3993, 0x677dc31f, 0x0ea26d1b), Nothing}, RuntimeGeneratedFunctions.RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#\_RGF\_ModTag", ModelingToolkit.var"#\_RGF\_ModTag", (0xe4768855, 0xc75a2b9e, 0xbdbf2985, 0x3b9edfca, 0xfaebd888), Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ODESystem}, Nothing, ODESystem, Nothing, Nothing, Nothing, Nothing, Nothing}}, LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.ObservedFunctionCache{ODESystem}, Nothing, ODESystem, Nothing}, @Kwargs{}, SciMLBase.StandardNonlinearProblem}, Nothing, Nothing, Nothing, Nothing, Nothing}, sym::Num)
> @ SciMLBase ~/.julia/packages/SciMLBase/hJh6T/src/solutions/solution\_interface.jl:117
> \[9\] top-level scope
> @ ~/jupyter/isolation\_sensing/jl\_notebook\_cell\_df34fa98e69747e1a8f8a730347b8e2f\_W4sZmlsZQ==.jl:1
> \`\`\`
> \*\*Additional context\*\*
> 
> A reply from \[cryptic.ax\](https://discourse.julialang.org/u/cryptic.ax) says:
> 
> "That’s not the issue here. The system inside sol\_NL is an ODESystem. It generates observed functions of the form f(u, p, t), but since sol\_NL is not time dependent, SII calls the function has f(u, p) which leads to the above issue. In SII terminology, the index provider is time-dependent but the value provider isn’t."
