Skip to content

Commit 0828cd9

Browse files
Merge pull request #3800 from contradict/fix-formatter-crash
Fix formatter crash
2 parents 7b5c5f0 + 272b2a1 commit 0828cd9

File tree

86 files changed

+610
-371
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+610
-371
lines changed

docs/src/API/variables.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ For systems that contain parameters with metadata like described above, have som
293293
In the example below, we define a system with tunable parameters and extract bounds vectors
294294

295295
```@example metadata
296-
@variables x(t)=0 u(t)=0 [input = true] y(t)=0 [output = true]
296+
@variables x(t)=0 u(t)=0 [input=true] y(t)=0 [output=true]
297297
@parameters T [tunable = true, bounds = (0, Inf)]
298298
@parameters k [tunable = true, bounds = (0, Inf)]
299299
eqs = [D(x) ~ (-x + k * u) / T # A first-order system with time constant T and gain k

docs/src/basics/MTKLanguage.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,8 @@ Refer the following example for different ways to define symbolic arrays.
381381
@parameters begin
382382
p1[1:4]
383383
p2[1:N]
384-
p3[1:N, 1:M] = 10,
384+
p3[1:N,
385+
1:M] = 10,
385386
[description = "A multi-dimensional array of arbitrary length with description"]
386387
(p4[1:N, 1:M] = 10),
387388
[description = "An alternate syntax for p3 to match the syntax of vanilla parameters macro"]

docs/src/basics/Validation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function ModelingToolkit.get_unit(op::typeof(dummycomplex), args)
108108
end
109109
110110
sts = @variables a(t)=0 [unit = u"cm"]
111-
ps = @parameters s=-1 [unit = u"cm"] c=c [unit = u"cm"]
111+
ps = @parameters s=-1 [unit=u"cm"] c=c [unit=u"cm"]
112112
eqs = [D(a) ~ dummycomplex(c, s);]
113113
sys = System(
114114
eqs, t, [sts...;], [ps...;], name = :sys, checks = ~ModelingToolkit.CheckUnits)

docs/src/examples/perturbation.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ sol = solve(prob)
9999
plot(sol, idxs = substitute(x_series, ϵ => 0.1); label = "Perturbative (ϵ=0.1)")
100100
101101
x_exact(t, ϵ) = exp(-ϵ * t) * sin(√(1 - ϵ^2) * t) / √(1 - ϵ^2)
102-
@assert isapprox(sol(π/2; idxs = substitute(x_series, ϵ => 0.1)), x_exact(π/2, 0.1); atol = 1e-2) # compare around 1st peak # hide
102+
@assert isapprox(
103+
sol(π/2; idxs = substitute(x_series, ϵ => 0.1)), x_exact(π/2, 0.1); atol = 1e-2) # compare around 1st peak # hide
103104
plot!(sol.t, x_exact.(sol.t, 0.1); label = "Exact (ϵ=0.1)")
104105
```
105106

docs/src/examples/sparse_jacobians.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,20 @@ function brusselator_2d_loop(du, u, p, t)
2323
@inbounds for I in CartesianIndices((N, N))
2424
i, j = Tuple(I)
2525
x, y = xyd_brusselator[I[1]], xyd_brusselator[I[2]]
26-
ip1, im1, jp1, jm1 = limit(i + 1, N), limit(i - 1, N), limit(j + 1, N),
26+
ip1, im1, jp1,
27+
jm1 = limit(i + 1, N), limit(i - 1, N), limit(j + 1, N),
2728
limit(j - 1, N)
28-
du[i, j, 1] = alpha * (u[im1, j, 1] + u[ip1, j, 1] + u[i, jp1, 1] + u[i, jm1, 1] -
29-
4u[i, j, 1]) +
30-
B + u[i, j, 1]^2 * u[i, j, 2] - (A + 1) * u[i, j, 1] +
31-
brusselator_f(x, y, t)
32-
du[i, j, 2] = alpha * (u[im1, j, 2] + u[ip1, j, 2] + u[i, jp1, 2] + u[i, jm1, 2] -
33-
4u[i, j, 2]) +
34-
A * u[i, j, 1] - u[i, j, 1]^2 * u[i, j, 2]
29+
du[i,
30+
j,
31+
1] = alpha * (u[im1, j, 1] + u[ip1, j, 1] + u[i, jp1, 1] + u[i, jm1, 1] -
32+
4u[i, j, 1]) +
33+
B + u[i, j, 1]^2 * u[i, j, 2] - (A + 1) * u[i, j, 1] +
34+
brusselator_f(x, y, t)
35+
du[i,
36+
j,
37+
2] = alpha * (u[im1, j, 2] + u[ip1, j, 2] + u[i, jp1, 2] + u[i, jm1, 2] -
38+
4u[i, j, 2]) +
39+
A * u[i, j, 1] - u[i, j, 1]^2 * u[i, j, 2]
3540
end
3641
end
3742
p = (3.4, 1.0, 10.0, step(xyd_brusselator))

docs/src/examples/tearing_parallelism.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ using ModelingToolkit: t_nounits as t, D_nounits as D
1515
1616
# Basic electric components
1717
@connector function Pin(; name)
18-
@variables v(t)=1.0 i(t)=1.0 [connect = Flow]
18+
@variables v(t)=1.0 i(t)=1.0 [connect=Flow]
1919
System(Equation[], t, [v, i], [], name = name)
2020
end
2121
@@ -36,7 +36,7 @@ function ConstantVoltage(; name, V = 1.0)
3636
end
3737
3838
@connector function HeatPort(; name)
39-
@variables T(t)=293.15 Q_flow(t)=0.0 [connect = Flow]
39+
@variables T(t)=293.15 Q_flow(t)=0.0 [connect=Flow]
4040
System(Equation[], t, [T, Q_flow], [], name = name)
4141
end
4242

docs/src/tutorials/disturbance_modeling.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ disturbance_inputs = [ssys.d1, ssys.d2]
188188
P = ssys.system_model
189189
outputs = [P.inertia1.phi, P.inertia2.phi, P.inertia1.w, P.inertia2.w]
190190
191-
(f_oop, f_ip), x_sym, p_sym, io_sys = ModelingToolkit.generate_control_function(
191+
(f_oop, f_ip), x_sym,
192+
p_sym,
193+
io_sys = ModelingToolkit.generate_control_function(
192194
model_with_disturbance, inputs, disturbance_inputs; disturbance_argument = true)
193195
194196
g = ModelingToolkit.build_explicit_observed_function(

ext/MTKCasADiDynamicOptExt.jl

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ struct MXLinearInterpolation
1717
t::Vector{Float64}
1818
dt::Float64
1919
end
20-
Base.getindex(m::MXLinearInterpolation, i...) = length(i) == length(size(m.u)) ? m.u[i...] : m.u[i..., :]
20+
function Base.getindex(m::MXLinearInterpolation, i...)
21+
length(i) == length(size(m.u)) ? m.u[i...] : m.u[i..., :]
22+
end
2123

2224
mutable struct CasADiModel
2325
model::Opti
@@ -55,7 +57,7 @@ function (M::MXLinearInterpolation)(τ)
5557
(i > length(M.t) || i < 1) && error("Cannot extrapolate past the tspan.")
5658
colons = ntuple(_ -> (:), length(size(M.u)) - 1)
5759
if i < length(M.t)
58-
M.u[colons..., i] + Δ*(M.u[colons..., i+1] - M.u[colons..., i])
60+
M.u[colons..., i] + Δ*(M.u[colons..., i + 1] - M.u[colons..., i])
5961
else
6062
M.u[colons..., i]
6163
end
@@ -65,7 +67,9 @@ function MTK.CasADiDynamicOptProblem(sys::System, op, tspan;
6567
dt = nothing,
6668
steps = nothing,
6769
guesses = Dict(), kwargs...)
68-
prob, _ = MTK.process_DynamicOptProblem(CasADiDynamicOptProblem, CasADiModel, sys, op, tspan; dt, steps, guesses, kwargs...)
70+
prob,
71+
_ = MTK.process_DynamicOptProblem(
72+
CasADiDynamicOptProblem, CasADiModel, sys, op, tspan; dt, steps, guesses, kwargs...)
6973
prob
7074
end
7175

@@ -127,10 +131,10 @@ function MTK.lowered_integral(model::CasADiModel, expr, lo, hi)
127131
for (i, t) in enumerate(model.U.t)
128132
if lo < t < hi
129133
Δt = min(dt, t - lo)
130-
total += (0.5*Δt*(expr[i] + expr[i-1]))
134+
total += (0.5*Δt*(expr[i] + expr[i - 1]))
131135
elseif t >= hi && (t - dt < hi)
132136
Δt = hi - t + dt
133-
total += (0.5*Δt*(expr[i] + expr[i-1]))
137+
total += (0.5*Δt*(expr[i] + expr[i - 1]))
134138
end
135139
end
136140
model.tₛ * total
@@ -186,9 +190,13 @@ struct CasADiCollocation <: AbstractCollocation
186190
tableau::DiffEqBase.ODERKTableau
187191
end
188192

189-
MTK.CasADiCollocation(solver, tableau = MTK.constructDefault()) = CasADiCollocation(solver, tableau)
193+
function MTK.CasADiCollocation(solver, tableau = MTK.constructDefault())
194+
CasADiCollocation(solver, tableau)
195+
end
190196

191-
function MTK.prepare_and_optimize!(prob::CasADiDynamicOptProblem, solver::CasADiCollocation; verbose = false, solver_options = Dict(), plugin_options = Dict(), kwargs...)
197+
function MTK.prepare_and_optimize!(
198+
prob::CasADiDynamicOptProblem, solver::CasADiCollocation; verbose = false,
199+
solver_options = Dict(), plugin_options = Dict(), kwargs...)
192200
solver_opti = add_solve_constraints!(prob, solver.tableau)
193201
verbose || (solver_options["print_level"] = 0)
194202
solver!(solver_opti, "$(solver.solver)", plugin_options, solver_options)
@@ -224,9 +232,11 @@ function MTK.get_t_values(model::CasADiModel)
224232
value_getter = MTK.successful_solve(model) ? CasADi.debug_value : CasADi.value
225233
ts = value_getter(model.solver_opti, model.tₛ) .* model.U.t
226234
end
227-
MTK.objective_value(model::CasADiModel) = CasADi.pyconvert(Float64, model.solver_opti.py.value(model.solver_opti.py.f))
235+
function MTK.objective_value(model::CasADiModel)
236+
CasADi.pyconvert(Float64, model.solver_opti.py.value(model.solver_opti.py.f))
237+
end
228238

229-
function MTK.successful_solve(m::CasADiModel)
239+
function MTK.successful_solve(m::CasADiModel)
230240
isnothing(m.solver_opti) && return false
231241
retcode = CasADi.return_status(m.solver_opti)
232242
retcode == "Solve_Succeeded" || retcode == "Solved_To_Acceptable_Level"

ext/MTKInfiniteOptExt.jl

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,26 @@ struct InfiniteOptDynamicOptProblem{uType, tType, isinplace, P, F, K} <:
4848
end
4949

5050
MTK.generate_internal_model(m::Type{InfiniteOptModel}) = InfiniteModel()
51-
MTK.generate_time_variable!(m::InfiniteModel, tspan, tsteps) = @infinite_parameter(m, t in [tspan[1], tspan[2]], num_supports = length(tsteps))
52-
MTK.generate_state_variable!(m::InfiniteModel, u0::Vector, ns, ts) = @variable(m, U[i = 1:ns], Infinite(m[:t]), start=u0[i])
53-
MTK.generate_input_variable!(m::InfiniteModel, c0, nc, ts) = @variable(m, V[i = 1:nc], Infinite(m[:t]), start=c0[i])
51+
function MTK.generate_time_variable!(m::InfiniteModel, tspan, tsteps)
52+
@infinite_parameter(m, t in [tspan[1], tspan[2]], num_supports = length(tsteps))
53+
end
54+
function MTK.generate_state_variable!(m::InfiniteModel, u0::Vector, ns, ts)
55+
@variable(m, U[i = 1:ns], Infinite(m[:t]), start=u0[i])
56+
end
57+
function MTK.generate_input_variable!(m::InfiniteModel, c0, nc, ts)
58+
@variable(m, V[i = 1:nc], Infinite(m[:t]), start=c0[i])
59+
end
5460

5561
function MTK.generate_timescale!(m::InfiniteModel, guess, is_free_t)
5662
@variable(m, tₛ 0, start = guess)
5763
if !is_free_t
58-
fix(tₛ, 1, force=true)
64+
fix(tₛ, 1, force = true)
5965
set_start_value(tₛ, 1)
6066
end
6167
tₛ
6268
end
6369

64-
function MTK.add_constraint!(m::InfiniteOptModel, expr::Union{Equation, Inequality})
70+
function MTK.add_constraint!(m::InfiniteOptModel, expr::Union{Equation, Inequality})
6571
if expr isa Equation
6672
@constraint(m.model, expr.lhs - expr.rhs == 0)
6773
elseif expr.relational_op === Symbolics.geq
@@ -76,20 +82,26 @@ function MTK.JuMPDynamicOptProblem(sys::System, op, tspan;
7682
dt = nothing,
7783
steps = nothing,
7884
guesses = Dict(), kwargs...)
79-
prob, _ = MTK.process_DynamicOptProblem(JuMPDynamicOptProblem, InfiniteOptModel, sys, op, tspan; dt, steps, guesses, kwargs...)
85+
prob,
86+
_ = MTK.process_DynamicOptProblem(JuMPDynamicOptProblem, InfiniteOptModel, sys,
87+
op, tspan; dt, steps, guesses, kwargs...)
8088
prob
8189
end
8290

8391
function MTK.InfiniteOptDynamicOptProblem(sys::System, op, tspan;
8492
dt = nothing,
8593
steps = nothing,
8694
guesses = Dict(), kwargs...)
87-
prob, pmap = MTK.process_DynamicOptProblem(InfiniteOptDynamicOptProblem, InfiniteOptModel, sys, op, tspan; dt, steps, guesses, kwargs...)
95+
prob,
96+
pmap = MTK.process_DynamicOptProblem(InfiniteOptDynamicOptProblem, InfiniteOptModel,
97+
sys, op, tspan; dt, steps, guesses, kwargs...)
8898
MTK.add_equational_constraints!(prob.wrapped_model, sys, pmap, tspan)
8999
prob
90100
end
91101

92-
MTK.lowered_integral(model::InfiniteOptModel, expr, lo, hi) = model.tₛ * InfiniteOpt.(expr, model.model[:t], lo, hi)
102+
function MTK.lowered_integral(model::InfiniteOptModel, expr, lo, hi)
103+
model.tₛ * InfiniteOpt.(expr, model.model[:t], lo, hi)
104+
end
93105
MTK.lowered_derivative(model::InfiniteOptModel, i) = (model.U[i], model.model[:t])
94106

95107
function MTK.process_integral_bounds(model::InfiniteOptModel, integral_span, tspan)
@@ -125,7 +137,7 @@ function add_solve_constraints!(prob::JuMPDynamicOptProblem, tableau)
125137
nᵥ = length(V)
126138
if MTK.is_explicit(tableau)
127139
K = Any[]
128-
for τ in tsteps[1:end-1]
140+
for τ in tsteps[1:(end - 1)]
129141
for (i, h) in enumerate(c)
130142
ΔU = sum([A[i, j] * K[j] for j in 1:(i - 1)], init = zeros(nᵤ))
131143
Uₙ = [U[i](τ) + ΔU[i] * dt for i in 1:nᵤ]
@@ -142,14 +154,15 @@ function add_solve_constraints!(prob::JuMPDynamicOptProblem, tableau)
142154
K = @variable(model, K[1:length(α), 1:nᵤ], Infinite(model[:t]))
143155
ΔUs = A * K
144156
ΔU_tot = dt * (K' * α)
145-
for τ in tsteps[1:end-1]
157+
for τ in tsteps[1:(end - 1)]
146158
for (i, h) in enumerate(c)
147159
ΔU = @view ΔUs[i, :]
148160
Uₙ = U + ΔU * dt
149161
@constraint(model, [j = 1:nᵤ], K[i, j]==(tₛ * f(Uₙ, V, p, τ + h * dt)[j]),
150162
DomainRestrictions(t => τ), base_name="solve_K$i()")
151163
end
152-
@constraint(model, [n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](min+ dt, tsteps[end])),
164+
@constraint(model,
165+
[n = 1:nᵤ], U[n](τ) + ΔU_tot[n]==U[n](min+ dt, tsteps[end])),
153166
DomainRestrictions(t => τ), base_name="solve_U()")
154167
end
155168
end
@@ -159,15 +172,21 @@ struct JuMPCollocation <: AbstractCollocation
159172
solver::Any
160173
tableau::DiffEqBase.ODERKTableau
161174
end
162-
MTK.JuMPCollocation(solver, tableau = MTK.constructDefault()) = JuMPCollocation(solver, tableau)
175+
function MTK.JuMPCollocation(solver, tableau = MTK.constructDefault())
176+
JuMPCollocation(solver, tableau)
177+
end
163178

164179
struct InfiniteOptCollocation <: AbstractCollocation
165180
solver::Any
166181
derivative_method::InfiniteOpt.AbstractDerivativeMethod
167182
end
168-
MTK.InfiniteOptCollocation(solver, derivative_method = InfiniteOpt.FiniteDifference(InfiniteOpt.Backward())) = InfiniteOptCollocation(solver, derivative_method)
183+
function MTK.InfiniteOptCollocation(
184+
solver, derivative_method = InfiniteOpt.FiniteDifference(InfiniteOpt.Backward()))
185+
InfiniteOptCollocation(solver, derivative_method)
186+
end
169187

170-
function MTK.prepare_and_optimize!(prob::JuMPDynamicOptProblem, solver::JuMPCollocation; verbose = false, kwargs...)
188+
function MTK.prepare_and_optimize!(
189+
prob::JuMPDynamicOptProblem, solver::JuMPCollocation; verbose = false, kwargs...)
171190
model = prob.wrapped_model.model
172191
verbose || set_silent(model)
173192
# Unregister current solver constraints
@@ -190,7 +209,8 @@ function MTK.prepare_and_optimize!(prob::JuMPDynamicOptProblem, solver::JuMPColl
190209
model
191210
end
192211

193-
function MTK.prepare_and_optimize!(prob::InfiniteOptDynamicOptProblem, solver::InfiniteOptCollocation; verbose = false, kwargs...)
212+
function MTK.prepare_and_optimize!(prob::InfiniteOptDynamicOptProblem,
213+
solver::InfiniteOptCollocation; verbose = false, kwargs...)
194214
model = prob.wrapped_model.model
195215
verbose || set_silent(model)
196216
set_derivative_method(model[:t], solver.derivative_method)
@@ -223,8 +243,8 @@ function MTK.successful_solve(model::InfiniteModel)
223243
error("Model not solvable; please report this to github.com/SciML/ModelingToolkit.jl with a MWE.")
224244

225245
pstatus === FEASIBLE_POINT &&
226-
(tstatus === OPTIMAL || tstatus === LOCALLY_SOLVED || tstatus === ALMOST_OPTIMAL ||
227-
tstatus === ALMOST_LOCALLY_SOLVED)
246+
(tstatus === OPTIMAL || tstatus === LOCALLY_SOLVED || tstatus === ALMOST_OPTIMAL ||
247+
tstatus === ALMOST_LOCALLY_SOLVED)
228248
end
229249

230250
import InfiniteOpt: JuMP, GeneralVariableRef

ext/MTKPyomoDynamicOptExt.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ function MTK.add_constraint!(pmodel::PyomoDynamicOptModel, cons; n_idxs = 1)
108108
else
109109
cons.lhs - cons.rhs 0
110110
end
111-
expr = Symbolics.substitute(Symbolics.unwrap(expr), SPECIAL_FUNCTIONS_DICT, fold = false)
111+
expr = Symbolics.substitute(
112+
Symbolics.unwrap(expr), SPECIAL_FUNCTIONS_DICT, fold = false)
112113

113114
cons_sym = Symbol("cons", hash(cons))
114115
if occursin(Symbolics.unwrap(t_sym), expr)

0 commit comments

Comments
 (0)