Skip to content

I meet a problem for swtich __ Anyone can help?  #1985

@yanshuchang

Description

@yanshuchang
# 使用
using ModelingToolkit,Plots,DifferentialEquations,Optimization,...
      Plots, DifferentialEquations,LinearAlgebra, Distributions,Unitful,ModelingToolkitStandardLibrary 
using Symbolics:scalarize

using IfElse: ifelse
# 以下按照 从左到右, 从上到下 的方式写模型



@variables t

@connector function Pin(; name)
    sts = @variables begin
        v(t)                    # Potential at the pin [V]
        i(t), [connect = Flow]    # Current flowing into the pin [A]
    end
    ODESystem(Equation[], t, sts, [], name = name, defaults = Dict(v => 1.0, i => 1.0))
end


function switch(;name)

    @named   p   =  Pin()
    @named   n   =  Pin()

    #@parameters sig = m

    sts = @variables begin
        v(t)
        i(t)
    end


    eqs = [v ~ p.v - n.v
           0 ~ p.i + n.i
           i ~ p.i]


    sc = [10] => [i ~ 0]

    

    injection = t == 10.0 => [i ~ 0]
    modelll=compose(ODESystem(eqs, t, sts, []; name=name, discrete_events = injection), p, n)
end



@named rm  =  ModelingToolkitStandardLibrary.Electrical.Resistor(R = 4)                # 电阻模块, 不要轻易加 name。
@named lm  =  ModelingToolkitStandardLibrary.Electrical.Inductor(L = 1)                 # 电感模块, 不要轻易加 name。
#@named sm  =  sdc(vf = Vf)                                                               # 电压源模块

    # 1. 测试一下模块的入口还有出口

@named input_v = ModelingToolkitStandardLibrary.Blocks.Constant(k=4)
@named input_pin = ModelingToolkitStandardLibrary.Electrical.Voltage()
@named g = ModelingToolkitStandardLibrary.Electrical.Ground()
@named sw = switch()
  
eqs = [
        connect(input_pin.V, input_v.output)
        connect(input_pin.p, rm.p)
        connect(rm.n, sw.p)
        connect(sw.n, g.g)
        connect(g.g, input_pin.n)
    ]

@named _mol = ODESystem(eqs, t)

@named  mol = compose(_mol,  [input_v, g, input_pin, rm, sw])


sys = structural_simplify(mol)
prob = ODEProblem(sys, [], (0., 20.))
sol = solve(prob, Rosenbrock23())

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