Skip to content

handle empty events #1566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ struct SymbolicContinuousCallback
end

Base.:(==)(e1::SymbolicContinuousCallback, e2::SymbolicContinuousCallback) = isequal(e1.eqs, e2.eqs) && isequal(e1.affect, e2.affect)
Base.isempty(cb::SymbolicContinuousCallback) = isempty(cb.eqs)

to_equation_vector(eq::Equation) = [eq]
to_equation_vector(eqs::Vector{Equation}) = eqs
Expand Down Expand Up @@ -482,11 +483,13 @@ end

function continuous_events(sys::AbstractSystem)
obs = get_continuous_events(sys)
filter(!isempty, obs)
systems = get_systems(sys)
[obs;
reduce(vcat,
cbs = [obs;
reduce(vcat,
(map(o->namespace_equation(o, s), continuous_events(s)) for s in systems),
init=SymbolicContinuousCallback[])]
filter(!isempty, cbs)
end

Base.@deprecate default_u0(x) defaults(x) false
Expand Down
51 changes: 51 additions & 0 deletions test/root_equations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ affect = [v ~ -v]
@test getfield(ball, :continuous_events)[] == SymbolicContinuousCallback(Equation[x ~ 0], Equation[v ~ -v])
ball = structural_simplify(ball)

@test length(ModelingToolkit.continuous_events(ball)) == 1

tspan = (0.0,5.0)
prob = ODEProblem(ball, Pair[], tspan)
sol = solve(prob,Tsit5())
Expand Down Expand Up @@ -305,3 +307,52 @@ prob = ODAEProblem(sys, zeros(2), (0.0, 5.1))
sol = solve(prob, Tsit5())
@test all(minimum((0:0.1:5) .- sol.t', dims=2) .< 0.0001) # test that the solver stepped every 0.1s as dictated by event
@test sol([0.25])[vmeasured][] == sol([0.23])[vmeasured][] # test the hold property



## https://github.com/SciML/ModelingToolkit.jl/issues/1528
Dₜ = Differential(t)

@parameters u(t) [input=true] # Indicate that this is a controlled input
@parameters y(t) [output=true] # Indicate that this is a measured output

function Mass(; name, m = 1.0, p = 0, v = 0)
ps = @parameters m=m
sts = @variables pos(t)=p vel(t)=v
eqs = Dₜ(pos) ~ vel
ODESystem(eqs, t, [pos, vel], ps; name)
end
function Spring(; name, k = 1e4)
ps = @parameters k=k
@variables x(t)=0 # Spring deflection
ODESystem(Equation[], t, [x], ps; name)
end
function Damper(; name, c = 10)
ps = @parameters c=c
@variables vel(t)=0
ODESystem(Equation[], t, [vel], ps; name)
end
function SpringDamper(; name, k=false, c=false)
spring = Spring(; name=:spring, k)
damper = Damper(; name=:damper, c)
compose(
ODESystem(Equation[], t; name),
spring, damper)
end
connect_sd(sd, m1, m2) = [sd.spring.x ~ m1.pos - m2.pos, sd.damper.vel ~ m1.vel - m2.vel]
sd_force(sd) = -sd.spring.k * sd.spring.x - sd.damper.c * sd.damper.vel
@named mass1 = Mass(; m=1)
@named mass2 = Mass(; m=1)
@named sd = SpringDamper(; k=1000, c=10)
function Model(u, d=0)
eqs = [
connect_sd(sd, mass1, mass2)
Dₜ(mass1.vel) ~ ( sd_force(sd) + u) / mass1.m
Dₜ(mass2.vel) ~ (-sd_force(sd) + d) / mass2.m
]
@named _model = ODESystem(eqs, t; observed=[y~mass2.pos])
@named model = compose(_model, mass1, mass2, sd)
end
model = Model(sin(30t))
sys = structural_simplify(model)
@test isempty(ModelingToolkit.continuous_events(sys))