Skip to content

Using continuous_events on connected circuits/systems #1409

Closed
@ven-k

Description

@ven-k
using ModelingToolkitStandardLibrary.Electrical, ModelingToolkit, OrdinaryDiffEq
using ModelingToolkitStandardLibrary.Electrical: Ground, Resistor, VoltageSensor, VoltageSource, Pin
using ModelingToolkitStandardLibrary.Blocks: SmoothSineFunction
# VoltageSource and SmoothSineFunction from this PR <https://github.com/SciML/ModelingToolkitStandardLibrary.jl/pull/18/files>

function StepFunction(;name, offset=0.0, starttime=0.0, height=1.0)
    o, st, h = offset, starttime, height

    @parameters offset starttime height
    @variables y(t)

    eqs = [
           y ~ offset + h * (t > st)
          ]
    defaults = Dict(zip((offset, starttime, height), (o, st, h)))
    ODESystem(eqs, t, [y], [offset, starttime, height],
              defaults=defaults, name=name)
              # continuous_events=[t ~ st] => [y ~ h])
end

st = 0.5
@parameters t
@named ground = Ground()
@named resistor = Resistor()
@named source = VoltageSource()
@named stepp = StepFunction(offset=0.4, starttime=0.5, height=4)
@named voltage_sensor = VoltageSensor()

rc_eqs = [
    source.v ~ stepp.y
    connect(source.p, resistor.p, voltage_sensor.p)
    connect(voltage_sensor.n, source.n, resistor.n, ground.g)
]

@named rc_model = ODESystem(rc_eqs, t, systems = [resistor, stepp, 
    source, voltage_sensor, ground],
    continuous_events = [t ~ st] => [source.v ~ h]
    )

sys = structural_simplify(rc_model)
prob(ODEProblem(sys, u0, (0, 2.0))

results LoadError: BoundsError: attempt to access 13-element Vector{Function} at index [] Stacktrace:

The ModelingToolkit.continuous_events(sys) has following output

13-element Vector{ModelingToolkit.SymbolicContinuousCallback}:
 ModelingToolkit.SymbolicContinuousCallback(Equation[t ~ 0.5], Equation[source₊v(t) ~ 2.5])
 ModelingToolkit.SymbolicContinuousCallback(Equation[], Equation[])
 ModelingToolkit.SymbolicContinuousCallback(Equation[], Equation[])
 ModelingToolkit.SymbolicContinuousCallback(Equation[], Equation[])
 ModelingToolkit.SymbolicContinuousCallback(Equation[], Equation[])
 ModelingToolkit.SymbolicContinuousCallback(Equation[], Equation[])
 ModelingToolkit.SymbolicContinuousCallback(Equation[], Equation[])
 ModelingToolkit.SymbolicContinuousCallback(Equation[], Equation[])
 ModelingToolkit.SymbolicContinuousCallback(Equation[], Equation[])
 ModelingToolkit.SymbolicContinuousCallback(Equation[], Equation[])
 ModelingToolkit.SymbolicContinuousCallback(Equation[], Equation[])
 ModelingToolkit.SymbolicContinuousCallback(Equation[], Equation[])
 ModelingToolkit.SymbolicContinuousCallback(Equation[], Equation[])

This no. 13 represents the no. of components used (5 directly in the rc_eqs and the Pins that connect them. Specifying a continuous_events for each of the used component fixes this issue. But a continuous_events for Pin doesn't seem like a necessity.

Sidenote: Even a dummy like continuous_events=1~0 at the component level by-passes the above error

Also, @variables in lower level continuous events must be mentioned in the final model (here rc_model).
For example, in the StepFunction above if continuous_events, instead of t which is passed to rc_model, is defined for y or any new variable then eventual solve throws LoadError: UndefVarError: stepp₊y not defined

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