Skip to content

Commit 1e2e667

Browse files
committed
format
1 parent fc53824 commit 1e2e667

File tree

7 files changed

+62
-47
lines changed

7 files changed

+62
-47
lines changed

docs/src/systems/DiscreteSystem.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ DiscreteFunction(sys::DiscreteSystem, args...)
2828
```
2929

3030
## Discrete Domain
31+
3132
```@docs; canonical=false
3233
Shift
3334
Prev

docs/src/systems/ImplicitDiscreteSystem.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ ImplicitDiscreteFunction(sys::ImplicitDiscreteSystem, args...)
2828
```
2929

3030
## Discrete Domain
31+
3132
```@docs; canonical=false
3233
Shift
3334
Prev

src/ModelingToolkit.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ export DAEFunctionExpr, DAEProblemExpr
237237
export SDESystem, SDEFunction, SDEFunctionExpr, SDEProblemExpr
238238
export SystemStructure
239239
export DiscreteSystem, DiscreteProblem, DiscreteFunction, DiscreteFunctionExpr
240-
export ImplicitDiscreteSystem, ImplicitDiscreteProblem, ImplicitDiscreteFunction, ImplicitDiscreteFunctionExpr
240+
export ImplicitDiscreteSystem, ImplicitDiscreteProblem, ImplicitDiscreteFunction,
241+
ImplicitDiscreteFunctionExpr
241242
export JumpSystem
242243
export ODEProblem, SDEProblem
243244
export NonlinearFunction, NonlinearFunctionExpr

src/structural_transformation/utils.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ Distribute a shift applied to a whole expression or equation.
530530
Shift(t, 1)(x + y) will become Shift(t, 1)(x) + Shift(t, 1)(y).
531531
Only shifts variables whose independent variable is the same t that appears in the Shift (i.e. constants, time-independent parameters, etc. do not get shifted).
532532
"""
533-
function distribute_shift(var)
533+
function distribute_shift(var)
534534
var = unwrap(var)
535535
var isa Equation && return distribute_shift(var.lhs) ~ distribute_shift(var.rhs)
536536

@@ -553,11 +553,13 @@ function _distribute_shift(expr, shift)
553553
args = arguments(expr)
554554

555555
if ModelingToolkit.isvariable(expr)
556-
(length(args) == 1 && isequal(shift.t, only(args))) ? (return shift(expr)) : (return expr)
556+
(length(args) == 1 && isequal(shift.t, only(args))) ? (return shift(expr)) :
557+
(return expr)
557558
elseif op isa Shift
558559
return shift(expr)
559560
else
560-
return maketerm(typeof(expr), operation(expr), Base.Fix2(_distribute_shift, shift).(args),
561+
return maketerm(
562+
typeof(expr), operation(expr), Base.Fix2(_distribute_shift, shift).(args),
561563
unwrap(expr).metadata)
562564
end
563565
else

src/systems/discrete_system/implicit_discrete_system.jl

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ function ImplicitDiscreteSystem(eqs, iv; kwargs...)
239239
collect(allunknowns), collect(new_ps); kwargs...)
240240
end
241241

242-
ImplicitDiscreteSystem(eq::Equation, args...; kwargs...) = ImplicitDiscreteSystem([eq], args...; kwargs...)
242+
function ImplicitDiscreteSystem(eq::Equation, args...; kwargs...)
243+
ImplicitDiscreteSystem([eq], args...; kwargs...)
244+
end
243245

244246
function flatten(sys::ImplicitDiscreteSystem, noeqs = false)
245247
systems = get_systems(sys)
@@ -273,11 +275,13 @@ function generate_function(
273275
obs = observed(sys)
274276
shifted_obs = Symbolics.Equation[distribute_shift(Next(eq)) for eq in obs]
275277
obsidxs = observed_equations_used_by(sys, exprs; obs = shifted_obs)
276-
extra_assignments = [Assignment(shifted_obs[i].lhs, shifted_obs[i].rhs) for i in obsidxs]
278+
extra_assignments = [Assignment(shifted_obs[i].lhs, shifted_obs[i].rhs)
279+
for i in obsidxs]
277280

278281
u_next = map(Next, dvs)
279282
u = dvs
280-
build_function_wrapper(sys, exprs, u_next, u, ps..., iv; p_start = 3, extra_assignments, kwargs...)
283+
build_function_wrapper(
284+
sys, exprs, u_next, u, ps..., iv; p_start = 3, extra_assignments, kwargs...)
281285
end
282286

283287
function shift_u0map_forward(sys::ImplicitDiscreteSystem, u0map, defs)
@@ -341,11 +345,13 @@ function SciMLBase.ImplicitDiscreteFunction(sys::ImplicitDiscreteSystem, args...
341345
ImplicitDiscreteFunction{true}(sys, args...; kwargs...)
342346
end
343347

344-
function SciMLBase.ImplicitDiscreteFunction{true}(sys::ImplicitDiscreteSystem, args...; kwargs...)
348+
function SciMLBase.ImplicitDiscreteFunction{true}(
349+
sys::ImplicitDiscreteSystem, args...; kwargs...)
345350
ImplicitDiscreteFunction{true, SciMLBase.AutoSpecialize}(sys, args...; kwargs...)
346351
end
347352

348-
function SciMLBase.ImplicitDiscreteFunction{false}(sys::ImplicitDiscreteSystem, args...; kwargs...)
353+
function SciMLBase.ImplicitDiscreteFunction{false}(
354+
sys::ImplicitDiscreteSystem, args...; kwargs...)
349355
ImplicitDiscreteFunction{false, SciMLBase.FullSpecialize}(sys, args...; kwargs...)
350356
end
351357
function SciMLBase.ImplicitDiscreteFunction{iip, specialize}(
@@ -360,7 +366,6 @@ function SciMLBase.ImplicitDiscreteFunction{iip, specialize}(
360366
eval_module = @__MODULE__,
361367
analytic = nothing,
362368
kwargs...) where {iip, specialize}
363-
364369
if !iscomplete(sys)
365370
error("A completed `ImplicitDiscreteSystem` is required. Call `complete` or `structural_simplify` on the system before creating a `ImplicitDiscreteProblem`")
366371
end
@@ -404,9 +409,12 @@ struct ImplicitDiscreteFunctionClosure{O, I} <: Function
404409
f_iip::I
405410
end
406411
(f::ImplicitDiscreteFunctionClosure)(u_next, u, p, t) = f.f_oop(u_next, u, p, t)
407-
(f::ImplicitDiscreteFunctionClosure)(resid, u_next, u, p, t) = f.f_iip(resid, u_next, u, p, t)
412+
function (f::ImplicitDiscreteFunctionClosure)(resid, u_next, u, p, t)
413+
f.f_iip(resid, u_next, u, p, t)
414+
end
408415

409-
function ImplicitDiscreteFunctionExpr{iip}(sys::ImplicitDiscreteSystem, dvs = unknowns(sys),
416+
function ImplicitDiscreteFunctionExpr{iip}(
417+
sys::ImplicitDiscreteSystem, dvs = unknowns(sys),
410418
ps = parameters(sys), u0 = nothing;
411419
version = nothing, p = nothing,
412420
linenumbers = false,
@@ -427,4 +435,3 @@ end
427435
function ImplicitDiscreteFunctionExpr(sys::ImplicitDiscreteSystem, args...; kwargs...)
428436
ImplicitDiscreteFunctionExpr{true}(sys, args...; kwargs...)
429437
end
430-

test/implicit_discrete_system.jl

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,72 +3,73 @@ using ModelingToolkit: t_nounits as t
33
using StableRNGs
44

55
k = ShiftIndex(t)
6-
rng = StableRNG(22525)
6+
rng = StableRNG(22525)
77

88
@testset "Correct ImplicitDiscreteFunction" begin
99
@variables x(t) = 1
10-
@mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k)*x(k-1) - 3], t)
10+
@mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k) * x(k - 1) - 3], t)
1111
tspan = (0, 10)
1212

1313
# u[2] - u_next[1]
1414
# -3 - u_next[2] + u_next[2]*u_next[1]
1515
f = ImplicitDiscreteFunction(sys)
16-
u_next = [3., 1.5]
17-
@test f(u_next, [2.,3.], [], t) [0., 0.]
18-
u_next = [0., 0.]
19-
@test f(u_next, [2.,3.], [], t) [3., -3.]
20-
16+
u_next = [3.0, 1.5]
17+
@test f(u_next, [2.0, 3.0], [], t) [0.0, 0.0]
18+
u_next = [0.0, 0.0]
19+
@test f(u_next, [2.0, 3.0], [], t) [3.0, -3.0]
20+
2121
resid = rand(2)
22-
f(resid, u_next, [2.,3.], [], t)
23-
@test resid [3., -3.]
24-
25-
prob = ImplicitDiscreteProblem(sys, [x(k-1) => 3.], tspan)
26-
@test prob.u0 == [3., 1.]
22+
f(resid, u_next, [2.0, 3.0], [], t)
23+
@test resid [3.0, -3.0]
24+
25+
prob = ImplicitDiscreteProblem(sys, [x(k - 1) => 3.0], tspan)
26+
@test prob.u0 == [3.0, 1.0]
2727
prob = ImplicitDiscreteProblem(sys, [], tspan)
28-
@test prob.u0 == [1., 1.]
28+
@test prob.u0 == [1.0, 1.0]
2929
@variables x(t)
30-
@mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k)*x(k-1) - 3], t)
31-
@test_throws ErrorException prob = ImplicitDiscreteProblem(sys, [], tspan)
30+
@mtkbuild sys = ImplicitDiscreteSystem([x(k) ~ x(k) * x(k - 1) - 3], t)
31+
@test_throws ErrorException prob=ImplicitDiscreteProblem(sys, [], tspan)
3232
end
3333

3434
@testset "System with algebraic equations" begin
3535
@variables x(t) y(t)
36-
eqs = [x(k) ~ x(k-1) + x(k-2),
37-
x^2 ~ 1 - y^2]
36+
eqs = [x(k) ~ x(k - 1) + x(k - 2),
37+
x^2 ~ 1 - y^2]
3838
@mtkbuild sys = ImplicitDiscreteSystem(eqs, t)
3939
f = ImplicitDiscreteFunction(sys)
4040

41-
function correct_f(u_next, u, p, t)
41+
function correct_f(u_next, u, p, t)
4242
[u[2] - u_next[1],
43-
u[1] + u[2] - u_next[2],
44-
1 - (u_next[1]+u_next[2])^2 - u_next[3]^2]
43+
u[1] + u[2] - u_next[2],
44+
1 - (u_next[1] + u_next[2])^2 - u_next[3]^2]
4545
end
4646

4747
for _ in 1:10
4848
u_next = rand(rng, 3)
4949
u = rand(rng, 3)
50-
@test correct_f(u_next, u, [], 0.) f(u_next, u, [], 0.)
50+
@test correct_f(u_next, u, [], 0.0) f(u_next, u, [], 0.0)
5151
end
5252

5353
# Initialization is satisfied.
54-
prob = ImplicitDiscreteProblem(sys, [x(k-1) => .3, x(k-2) => .4], (0, 10), guesses = [y => 1])
54+
prob = ImplicitDiscreteProblem(
55+
sys, [x(k - 1) => 0.3, x(k - 2) => 0.4], (0, 10), guesses = [y => 1])
5556
@test (prob.u0[1] + prob.u0[2])^2 + prob.u0[3]^2 1
5657
end
5758

5859
@testset "Handle observables in function codegen" begin
5960
# Observable appears in differential equation
6061
@variables x(t) y(t) z(t)
61-
eqs = [x(k) ~ x(k-1) + x(k-2),
62-
y(k) ~ x(k) + x(k-2)*z(k-1),
63-
x + y + z ~ 2]
62+
eqs = [x(k) ~ x(k - 1) + x(k - 2),
63+
y(k) ~ x(k) + x(k - 2) * z(k - 1),
64+
x + y + z ~ 2]
6465
@mtkbuild sys = ImplicitDiscreteSystem(eqs, t)
6566
@test length(unknowns(sys)) == length(equations(sys)) == 3
6667
@test occursin("var\"y(t)\"", string(ImplicitDiscreteFunctionExpr(sys)))
6768

6869
# Shifted observable that appears in algebraic equation is properly handled.
69-
eqs = [z(k) ~ x(k) + sin(x(k)),
70-
y(k) ~ x(k-1) + x(k-2),
71-
z(k) * x(k) ~ 3]
70+
eqs = [z(k) ~ x(k) + sin(x(k)),
71+
y(k) ~ x(k - 1) + x(k - 2),
72+
z(k) * x(k) ~ 3]
7273
@mtkbuild sys = ImplicitDiscreteSystem(eqs, t)
7374
@test occursin("var\"Shift(t, 1)(z(t))\"", string(ImplicitDiscreteFunctionExpr(sys)))
7475
end

test/structural_transformation/utils.jl

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,21 @@ end
169169
k = ShiftIndex(t)
170170

171171
# Expand shifts
172-
@test isequal(ST.distribute_shift(Shift(t, -1)(x + y)), Shift(t, -1)(x) + Shift(t, -1)(y))
172+
@test isequal(
173+
ST.distribute_shift(Shift(t, -1)(x + y)), Shift(t, -1)(x) + Shift(t, -1)(y))
173174

174175
expr = a * Shift(t, -2)(x) + Shift(t, 2)(y) + b
175176
@test isequal(ST.simplify_shifts(ST.distribute_shift(Shift(t, 2)(expr))),
176-
a*x + Shift(t, 4)(y) + b)
177+
a * x + Shift(t, 4)(y) + b)
177178
@test isequal(ST.distribute_shift(Shift(t, 2)(exp(z))), exp(Shift(t, 2)(z)))
178179
@test isequal(ST.distribute_shift(Shift(t, 2)(exp(a) + b)), exp(a) + b)
179180

180-
expr = a^x - log(b*y) + z*x
181-
@test isequal(ST.distribute_shift(Shift(t, -3)(expr)), a^(Shift(t, -3)(x)) - log(b * Shift(t, -3)(y)) + Shift(t, -3)(z)*Shift(t, -3)(x))
181+
expr = a^x - log(b * y) + z * x
182+
@test isequal(ST.distribute_shift(Shift(t, -3)(expr)),
183+
a^(Shift(t, -3)(x)) - log(b * Shift(t, -3)(y)) + Shift(t, -3)(z) * Shift(t, -3)(x))
182184

183-
expr = x(k+1) ~ x + x(k-1)
184-
@test isequal(ST.distribute_shift(Shift(t, -1)(expr)), x ~ x(k-1) + x(k-2))
185+
expr = x(k + 1) ~ x + x(k - 1)
186+
@test isequal(ST.distribute_shift(Shift(t, -1)(expr)), x ~ x(k - 1) + x(k - 2))
185187
end
186188

187189
@testset "`map_variables_to_equations`" begin

0 commit comments

Comments
 (0)