Skip to content

Commit fcb10f9

Browse files
committed
format
1 parent b168032 commit fcb10f9

10 files changed

+163
-104
lines changed

src/systems/callbacks.jl

+107-60
Large diffs are not rendered by default.

src/systems/diffeqs/odesystem.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,8 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps;
305305
throw(ArgumentError("System names must be unique."))
306306
end
307307

308-
algeeqs = filter(eq -> eq.lhs isa Union{Symbolic, Number} && !is_diff_equation(eq), flatten_equations(deqs))
308+
algeeqs = filter(eq -> eq.lhs isa Union{Symbolic, Number} && !is_diff_equation(eq),
309+
flatten_equations(deqs))
309310
cont_callbacks = SymbolicContinuousCallbacks(continuous_events; algeeqs, iv)
310311
disc_callbacks = SymbolicDiscreteCallbacks(discrete_events; algeeqs, iv)
311312

src/systems/diffeqs/sdesystem.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ function SDESystem(deqs::AbstractVector{<:Equation}, neqs::AbstractArray, iv, dv
264264
Wfact = RefValue(EMPTY_JAC)
265265
Wfact_t = RefValue(EMPTY_JAC)
266266

267-
algeeqs = filter(eq -> eq.lhs isa Union{Symbolic, Number} && !is_diff_equation(eq), flatten_equations(deqs))
267+
algeeqs = filter(eq -> eq.lhs isa Union{Symbolic, Number} && !is_diff_equation(eq),
268+
flatten_equations(deqs))
268269
cont_callbacks = SymbolicContinuousCallbacks(continuous_events; algeeqs, iv)
269270
disc_callbacks = SymbolicDiscreteCallbacks(discrete_events; algeeqs, iv)
270271
if is_dde === nothing

src/systems/discrete_system/discrete_system.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ end
425425
function Base.:(==)(sys1::DiscreteSystem, sys2::DiscreteSystem)
426426
sys1 === sys2 && return true
427427
isequal(nameof(sys1), nameof(sys2)) &&
428-
isequal(get_iv(sys1), get_iv(sys2)) &&
428+
isequal(get_iv(sys1), get_iv(sys2)) &&
429429
_eq_unordered(get_eqs(sys1), get_eqs(sys2)) &&
430430
_eq_unordered(get_unknowns(sys1), get_unknowns(sys2)) &&
431431
_eq_unordered(get_ps(sys1), get_ps(sys2)) &&

src/systems/discrete_system/implicit_discrete_system.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ function flatten(sys::ImplicitDiscreteSystem, noeqs = false)
264264
end
265265

266266
function generate_function(
267-
sys::ImplicitDiscreteSystem, dvs = unknowns(sys), ps = parameters(sys); wrap_code = identity, cachesyms::Tuple = (), kwargs...)
267+
sys::ImplicitDiscreteSystem, dvs = unknowns(sys), ps = parameters(sys);
268+
wrap_code = identity, cachesyms::Tuple = (), kwargs...)
268269
iv = get_iv(sys)
269270
# Algebraic equations get shifted forward 1, to match with differential equations
270271
exprs = map(equations(sys)) do eq
@@ -447,7 +448,7 @@ end
447448
function Base.:(==)(sys1::ImplicitDiscreteSystem, sys2::ImplicitDiscreteSystem)
448449
sys1 === sys2 && return true
449450
isequal(nameof(sys1), nameof(sys2)) &&
450-
isequal(get_iv(sys1), get_iv(sys2)) &&
451+
isequal(get_iv(sys1), get_iv(sys2)) &&
451452
_eq_unordered(get_eqs(sys1), get_eqs(sys2)) &&
452453
_eq_unordered(get_unknowns(sys1), get_unknowns(sys2)) &&
453454
_eq_unordered(get_ps(sys1), get_ps(sys2)) &&

src/systems/index_cache.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ function IndexCache(sys::AbstractSystem)
117117
affs = [affs]
118118
end
119119
for affect in affs
120-
if affect isa AffectSystem || affect isa FunctionalAffect || affect isa ImperativeAffect
120+
if affect isa AffectSystem || affect isa FunctionalAffect ||
121+
affect isa ImperativeAffect
121122
union!(discs, unwrap.(discretes(affect)))
122123
elseif isnothing(affect)
123124
continue

src/systems/model_parsing.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ function _model_macro(mod, name, expr, isconnector)
119119
Ref(dict), [:constants, :defaults, :kwargs, :structural_parameters])
120120

121121
sys = :($ODESystem($(flatten_equations)(equations), $iv, variables, parameters;
122-
name, description = $description, systems, gui_metadata = $gui_metadata, defaults, continuous_events = cont_events, discrete_events = disc_events))
122+
name, description = $description, systems, gui_metadata = $gui_metadata,
123+
defaults, continuous_events = cont_events, discrete_events = disc_events))
123124

124125
if length(ext) == 0
125126
push!(exprs.args, :(var"#___sys___" = $sys))

src/systems/systemstructure.jl

-1
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,6 @@ function _structural_simplify!(state::TearingState, io; simplify = false,
688688
check_consistency = true, fully_determined = true, warn_initialize_determined = false,
689689
dummy_derivative = true,
690690
kwargs...)
691-
692691
if fully_determined isa Bool
693692
check_consistency &= fully_determined
694693
else

test/accessor_functions.jl

+5-3
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,16 @@ let
151151
# Checks `continuous_events_toplevel` and `discrete_events_toplevel` (straightforward
152152
# as I stored the same single event in all systems). Don't check for non-toplevel cases as
153153
# technically not needed for these tests and name spacing the events is a mess.
154-
bot_cev = ModelingToolkit.SymbolicContinuousCallback(cevs[1], algeeqs = [O ~ (d + p_bot) * X_bot + Y])
155-
mid_dev = ModelingToolkit.SymbolicDiscreteCallback(devs[1], algeeqs = [O ~ (d + p_mid1) * X_mid1 + Y])
154+
bot_cev = ModelingToolkit.SymbolicContinuousCallback(
155+
cevs[1], algeeqs = [O ~ (d + p_bot) * X_bot + Y])
156+
mid_dev = ModelingToolkit.SymbolicDiscreteCallback(
157+
devs[1], algeeqs = [O ~ (d + p_mid1) * X_mid1 + Y])
156158
@test all_sets_equal(
157159
continuous_events_toplevel.([sys_bot, sys_bot_comp, sys_bot_ss])...,
158160
[bot_cev])
159161
@test all_sets_equal(
160162
discrete_events_toplevel.(
161-
[sys_mid1, sys_mid1_comp, sys_mid1_ss])...,
163+
[sys_mid1, sys_mid1_comp, sys_mid1_ss])...,
162164
[mid_dev])
163165
@test all(sym_issubset(
164166
continuous_events_toplevel(sys), get_continuous_events(sys))

test/symbolic_events.jl

+39-33
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ using SciMLStructures: canonicalize, Discrete
33
using ModelingToolkit: SymbolicContinuousCallback,
44
SymbolicContinuousCallbacks,
55
SymbolicDiscreteCallback,
6-
SymbolicDiscreteCallbacks,
6+
SymbolicDiscreteCallbacks,
77
get_callback,
88
t_nounits as t,
99
D_nounits as D,
@@ -340,7 +340,7 @@ end
340340
D(v) ~ -9.8], t, continuous_events = root_eqs => affect)
341341

342342
@test only(continuous_events(ball)) ==
343-
SymbolicContinuousCallback(Equation[x ~ 0], Equation[v ~ -Pre(v)])
343+
SymbolicContinuousCallback(Equation[x ~ 0], Equation[v ~ -Pre(v)])
344344
ball = structural_simplify(ball)
345345

346346
@test length(ModelingToolkit.continuous_events(ball)) == 1
@@ -373,13 +373,13 @@ end
373373
cb = get_callback(prob)
374374
@test cb isa ModelingToolkit.DiffEqCallbacks.VectorContinuousCallback
375375
@test getfield(ball, :continuous_events)[1] ==
376-
SymbolicContinuousCallback(Equation[x ~ 0], Equation[vx ~ -Pre(vx)])
376+
SymbolicContinuousCallback(Equation[x ~ 0], Equation[vx ~ -Pre(vx)])
377377
@test getfield(ball, :continuous_events)[2] ==
378-
SymbolicContinuousCallback(Equation[y ~ -1.5, y ~ 1.5], Equation[vy ~ -Pre(vy)])
378+
SymbolicContinuousCallback(Equation[y ~ -1.5, y ~ 1.5], Equation[vy ~ -Pre(vy)])
379379
cond = cb.condition
380380
out = [0.0, 0.0, 0.0]
381-
p0 = 0.
382-
t0 = 0.
381+
p0 = 0.0
382+
t0 = 0.0
383383
cond.f_iip(out, [0, 0, 0, 0], p0, t0)
384384
@test out [0, 1.5, -1.5]
385385

@@ -396,10 +396,11 @@ end
396396
# in this test, there are two variables affected by a single event.
397397
events = [[x ~ 0] => [vx ~ -Pre(vx), vy ~ -Pre(vy)]]
398398

399-
@named ball = ODESystem([D(x) ~ vx
400-
D(y) ~ vy
401-
D(vx) ~ -1
402-
D(vy) ~ 0], t; continuous_events = events)
399+
@named ball = ODESystem(
400+
[D(x) ~ vx
401+
D(y) ~ vy
402+
D(vx) ~ -1
403+
D(vy) ~ 0], t; continuous_events = events)
403404

404405
ball_nosplit = structural_simplify(ball)
405406
ball = structural_simplify(ball)
@@ -479,12 +480,13 @@ end
479480
end
480481

481482
@testset "SDE/ODESystem Discrete Callbacks" begin
482-
function testsol(sys, probtype, solver, u0, p, tspan; tstops = Float64[], paramtotest = nothing,
483+
function testsol(
484+
sys, probtype, solver, u0, p, tspan; tstops = Float64[], paramtotest = nothing,
483485
kwargs...)
484486
prob = probtype(complete(sys), u0, tspan, p; kwargs...)
485487
sol = solve(prob, solver(); tstops = tstops, abstol = 1e-10, reltol = 1e-10)
486488
@test isapprox(sol(1.0000000001)[1] - sol(0.999999999)[1], 1.0; rtol = 1e-6)
487-
paramtotest === nothing || (@test sol.ps[paramtotest] == [0., 1.])
489+
paramtotest === nothing || (@test sol.ps[paramtotest] == [0.0, 1.0])
488490
@test isapprox(sol(4.0)[1], 2 * exp(-2.0); rtol = 1e-6)
489491
sol
490492
end
@@ -503,8 +505,7 @@ end
503505
∂ₜ = D
504506
eqs = [∂ₜ(A) ~ -k * A]
505507
@named osys = ODESystem(eqs, t, [A], [k, t1, t2], discrete_events = [cb1, cb2])
506-
@named ssys = SDESystem(eqs, [0.0], t, [A], [k, t1, t2],
507-
discrete_events = [cb1, cb2])
508+
@named ssys = SDESystem(eqs, [0.0], t, [A], [k, t1, t2], discrete_events = [cb1, cb2])
508509
u0 = [A => 1.0]
509510
p = [k => 0.0, t1 => 1.0, t2 => 2.0]
510511
tspan = (0.0, 4.0)
@@ -518,10 +519,12 @@ end
518519
@named ssys1 = SDESystem(eqs, [0.0], t, [A, B], [k, t1, t2],
519520
discrete_events = [cb1a, cb2])
520521
u0′ = [A => 1.0, B => 0.0]
521-
sol = testsol(osys1, ODEProblem, Tsit5, u0′, p, tspan; tstops = [1.0, 2.0], check_length = false, paramtotest = k)
522+
sol = testsol(osys1, ODEProblem, Tsit5, u0′, p, tspan;
523+
tstops = [1.0, 2.0], check_length = false, paramtotest = k)
522524
@test sol(1.0000001, idxs = B) == 2.0
523525

524-
sol = testsol(ssys1, SDEProblem, RI5, u0′, p, tspan; tstops = [1.0, 2.0], check_length = false, paramtotest = k)
526+
sol = testsol(ssys1, SDEProblem, RI5, u0′, p, tspan; tstops = [1.0, 2.0],
527+
check_length = false, paramtotest = k)
525528
@test sol(1.0000001, idxs = B) == 2.0
526529

527530
# same as above - but with set-time event syntax
@@ -589,7 +592,7 @@ end
589592
jprob = JumpProblem(jsys, dprob, Direct(); kwargs...)
590593
sol = solve(jprob, SSAStepper(); tstops = tstops)
591594
@test (sol(1.000000000001)[1] - sol(0.99999999999)[1]) == 1
592-
paramtotest === nothing || (@test sol.ps[paramtotest] == [0., 1.0])
595+
paramtotest === nothing || (@test sol.ps[paramtotest] == [0.0, 1.0])
593596
@test sol(40.0)[1] == 0
594597
sol
595598
end
@@ -1282,53 +1285,56 @@ end
12821285
eqs = [D(D(x)) ~ λ * x
12831286
D(D(y)) ~ λ * y - g
12841287
x^2 + y^2 ~ 1]
1285-
c_evt = [t ~ 5.] => [x ~ Pre(x) + 0.1]
1288+
c_evt = [t ~ 5.0] => [x ~ Pre(x) + 0.1]
12861289
@mtkbuild pend = ODESystem(eqs, t, continuous_events = c_evt)
1287-
prob = ODEProblem(pend, [x => -1, y => 0], (0., 10.), [g => 1], guesses ==> 1])
1290+
prob = ODEProblem(pend, [x => -1, y => 0], (0.0, 10.0), [g => 1], guesses ==> 1])
12881291
sol = solve(prob, FBDF())
12891292
@test (sol(5.000001, idxs = x) - sol(4.999999, idxs = x), 0.1, rtol = 1e-4)
12901293
@test (sol(5.000001, idxs = x)^2 + sol(5.000001, idxs = y)^2, 1, rtol = 1e-4)
12911294

12921295
# Implicit affect with Pre
1293-
c_evt = [t ~ 5.] => [x ~ Pre(x) + y^2]
1296+
c_evt = [t ~ 5.0] => [x ~ Pre(x) + y^2]
12941297
@mtkbuild pend = ODESystem(eqs, t, continuous_events = c_evt)
1295-
prob = ODEProblem(pend, [x => 1, y => 0], (0., 10.), [g => 1], guesses ==> 1])
1298+
prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 10.0), [g => 1], guesses ==> 1])
12961299
sol = solve(prob, FBDF())
1297-
@test (sol(5.000001, idxs = y)^2 + sol(4.999999, idxs = x), sol(5.000001, idxs = x), rtol = 1e-4)
1300+
@test (sol(5.000001, idxs = y)^2 + sol(4.999999, idxs = x),
1301+
sol(5.000001, idxs = x), rtol = 1e-4)
12981302
@test (sol(5.000001, idxs = x)^2 + sol(5.000001, idxs = y)^2, 1, rtol = 1e-4)
12991303

13001304
# Impossible affect errors
1301-
c_evt = [t ~ 5.] => [x ~ Pre(x) + 2]
1305+
c_evt = [t ~ 5.0] => [x ~ Pre(x) + 2]
13021306
@mtkbuild pend = ODESystem(eqs, t, continuous_events = c_evt)
1303-
prob = ODEProblem(pend, [x => 1, y => 0], (0., 10.), [g => 1], guesses ==> 1])
1304-
@test_throws UnsolvableCallbackError sol = solve(prob, FBDF())
1305-
1307+
prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 10.0), [g => 1], guesses ==> 1])
1308+
@test_throws UnsolvableCallbackError sol=solve(prob, FBDF())
1309+
13061310
# Changing both variables and parameters in the same affect.
13071311
@parameters g(t)
13081312
eqs = [D(D(x)) ~ λ * x
13091313
D(D(y)) ~ λ * y - g
13101314
x^2 + y^2 ~ 1]
1311-
c_evt = SymbolicContinuousCallback([t ~ 5.0], [x ~ Pre(x) + 0.1, g ~ Pre(g) + 1], discrete_parameters = [g], iv = t)
1315+
c_evt = SymbolicContinuousCallback(
1316+
[t ~ 5.0], [x ~ Pre(x) + 0.1, g ~ Pre(g) + 1], discrete_parameters = [g], iv = t)
13121317
@mtkbuild pend = ODESystem(eqs, t, continuous_events = c_evt)
1313-
prob = ODEProblem(pend, [x => 1, y => 0], (0., 10.), [g => 1], guesses ==> 1])
1318+
prob = ODEProblem(pend, [x => 1, y => 0], (0.0, 10.0), [g => 1], guesses ==> 1])
13141319
sol = solve(prob, FBDF())
13151320
@test sol.ps[g] [1, 2]
1316-
@test (sol(5.0000001, idxs = x) - sol(4.999999, idxs = x), .1, rtol = 1e-4)
1321+
@test (sol(5.0000001, idxs = x) - sol(4.999999, idxs = x), 0.1, rtol = 1e-4)
13171322

13181323
# Proper re-initialization after parameter change
13191324
eqs = [y ~ g^2 - x, D(x) ~ x]
1320-
c_evt = SymbolicContinuousCallback([t ~ 5.0], [x ~ Pre(x) + 1, g ~ Pre(g) + 1], discrete_parameters = [g], iv = t)
1325+
c_evt = SymbolicContinuousCallback(
1326+
[t ~ 5.0], [x ~ Pre(x) + 1, g ~ Pre(g) + 1], discrete_parameters = [g], iv = t)
13211327
@mtkbuild sys = ODESystem(eqs, t, continuous_events = c_evt)
1322-
prob = ODEProblem(sys, [x => 1.0], (0., 10.), [g => 2])
1328+
prob = ODEProblem(sys, [x => 1.0], (0.0, 10.0), [g => 2])
13231329
sol = solve(prob, FBDF())
1324-
@test sol.ps[g] [2., 3.]
1330+
@test sol.ps[g] [2.0, 3.0]
13251331
@test (sol(5.00000001, idxs = x) - sol(4.9999999, idxs = x), 1; rtol = 1e-4)
13261332
@test (sol(5.00000001, idxs = y), 9 - sol(5.00000001, idxs = x), rtol = 1e-4)
13271333

13281334
# Parameters that don't appear in affects should not be mutated.
13291335
c_evt = [t ~ 5.0] => [x ~ Pre(x) + 1]
13301336
@mtkbuild sys = ODESystem(eqs, t, continuous_events = c_evt)
1331-
prob = ODEProblem(sys, [x => 0.5], (0., 10.), [g => 2], guesses = [y => 0])
1337+
prob = ODEProblem(sys, [x => 0.5], (0.0, 10.0), [g => 2], guesses = [y => 0])
13321338
sol = solve(prob, FBDF())
13331339
@test prob.ps[g] == sol.ps[g]
13341340
end

0 commit comments

Comments
 (0)