Skip to content

Voltage divider gives wrong answer with Rosenbrock23 #2045

@oxinabox

Description

@oxinabox

I haven't yet got a MWE without ModellingToolkit standard library.
but I am pretty sure this isn't MSL,
as this is a reproduction of the same failure on the same circuit when implemented in Cedar, which doesn't use MSL

This is a classic circuit, with the well known solution that the voltage across each of 2 equal resistors in series is half the voltage across both of them.

using ModelingToolkitStandardLibrary.Electrical, ModelingToolkit, OrdinaryDiffEq, Test
using ModelingToolkitStandardLibrary.Blocks: Step,
    Constant, Sine, Cosine, ExpSine, Ramp,
    Square, Triangular
using ModelingToolkitStandardLibrary.Blocks: square, triangular
using OrdinaryDiffEq: ReturnCode.Success

@parameters t
@testset "voltage divider" begin
    @named source = Sine(offset = 0, amplitude = 1.0, frequency = 1e3, start_time = 0.5,phase = 0)
    @named voltage = Voltage()
    @named R1 = Resistor(R = 1e3)
    @named R2 = Resistor(R = 1e3)
    @named ground = Ground()

    connections = [connect(source.output, voltage.V)
        connect(voltage.p, R1.p)
        connect(R1.n, R2.p)
        connect(R2.n, voltage.n, ground.g)]

    @named model = ODESystem(connections, t,
        systems = [R1, R2, source, voltage, ground])
    sys = structural_simplify(model)
    prob = ODEProblem(sys, Pair[R2.i => 0.0], (0, 2.0))
    sol = solve(prob,  Rosenbrock23())
    @test sol.retcode == Success
    @test sol[voltage.p.v]  2*sol[R2.v]
end

output is

voltage divider: Test Failed at /home/oxinabox/JuliaEnvs/ModelingToolkitStandardLibrary.jl/test/Electrical/analog.jl:29
  Expression: sol[voltage.p.v] ≈ 2 * sol[R2.v]
   Evaluated: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.6422526531740537, -5.09502586896226e-13] ≈ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.9082824125929643, 3029.774147437094]

WE can see at the end the voltage goes completely wild
3029.7 is not a voltage that can occur given the voltage source is a amplitude 1 sin wave.

This does not error with Rodas4, and Tsit5 gives an appropriate error.

I suspect that this should also error, and this is some issue with Rosenbrock that it doesn't actually support purely algebraic equations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions