Skip to content

Overridden default set on observed quantity not propagating to its alias #1968

Closed
@yanshuchang

Description

@yanshuchang
using ModelingToolkit, Plots, DifferentialEquations

@variables t

@connector function Pin(;name)
    sts = @variables v(t)=1.0 i(t)=1.0 [connect = Flow]
    ODESystem(Equation[], t, sts, []; name=name)
end

function Ground(;name)
    @named g = Pin()
    eqs = [g.v ~ 0]
    compose(ODESystem(eqs, t, [], []; name=name), g)
end

function OnePort(;name)
    @named p = Pin()
    @named n = Pin()
    sts = @variables v(t)=1.0 i(t)=1.0
    eqs = [
           v ~ p.v - n.v
           0 ~ p.i + n.i
           i ~ p.i
          ]
    compose(ODESystem(eqs, t, sts, []; name=name), p, n)
end

function Resistor(;name, R = 1.0)
    @named oneport = OnePort()
    @unpack v, i = oneport
    ps = @parameters R=R
    eqs = [
           v ~ i * R
          ]
    extend(ODESystem(eqs, t, [], ps; name=name), oneport)
end

function Capacitor(;name, C = 1.0)
    @named oneport = OnePort()
    @unpack v, i = oneport
    ps = @parameters C=C
    D = Differential(t)
    eqs = [
           D(v) ~ i / C
          ]
    extend(ODESystem(eqs, t, [], ps; name=name), oneport)
end

function ConstantVoltage(;name, V = 1.0)
    @named oneport = OnePort()
    @unpack v = oneport
    ps = @parameters V=V
    eqs = [
           V ~ v
          ]
    extend(ODESystem(eqs, t, [], ps; name=name), oneport)
end

function Inductor(;name, L = 1.0)
    @named oneport = OnePort()
    @unpack v, i = oneport
    pars = @parameters L = L
    D = Differential(t)

    eqs = [

       #D(v) ~ i / C
       D(i) ~ 1/L * v,
    ]
    extend(ODESystem(eqs, t, [], pars; name = name), oneport)
end


R = 1.0
C = 1.0
V = 20
L = 1.0


@named resistor   =  Resistor(R=R)
@named capacitor  =  Capacitor(C=C)
@named source     =  ConstantVoltage(V=V)
@named inductor   =  Inductor(L=L)
@named ground     =  Ground()

rc_eqs = [
    connect(source.p, inductor.p)
    connect(source.n, inductor.n)
    #connect(capacitor.n, source.n)
    connect(source.n, ground.g)
         ]

@named _rc_model = ODESystem(rc_eqs, t)
@named rc_model = compose(_rc_model,
                          [inductor, source, ground])
sys = structural_simplify(rc_model)
u0 = [
      inductor.v => -1
     ]
prob = ODAEProblem(sys, u0, (0, 10.0))
sol = solve(prob, Tsit5())
plot(sol)
plot(sol(0:0.1:10, idxs = inductor.i),label='A')
#plot(sol(inductor.v),label='A')
sol(0:0.1:10, idxs = inductor.v)

To put it simply, I connect the inductor and the 20V voltage source together, and then set the initial value of the inductor voltage to -1. It stands to reason that this situation does not exist and the program should report an error. Why does MTK still default the initial value of the voltage across the inductor to 20? Is this a bug of mtk?

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