Skip to content

Commit aafddc9

Browse files
format
1 parent 0f3ba28 commit aafddc9

File tree

6 files changed

+71
-68
lines changed

6 files changed

+71
-68
lines changed

src/systems/diffeqs/abstractodesystem.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,8 +1492,8 @@ function InitializationProblem{iip, specialize}(sys::AbstractODESystem, u0map =
14921492
neqs = length(equations(isys))
14931493
nunknown = length(unknowns(isys))
14941494
if neqs == nunknown
1495-
NonlinearProblem(isys,u0map,parammap)
1495+
NonlinearProblem(isys, u0map, parammap)
14961496
else
1497-
NonlinearLeastSquaresProblem(isys,u0map,parammap)
1497+
NonlinearLeastSquaresProblem(isys, u0map, parammap)
14981498
end
1499-
end
1499+
end

src/systems/diffeqs/odesystem.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps;
257257
metadata, gui_metadata, checks = checks)
258258
end
259259

260-
261260
function ODESystem(eqs, iv; kwargs...)
262261
eqs = collect(eqs)
263262
# NOTE: this assumes that the order of algebraic equations doesn't matter
@@ -553,4 +552,4 @@ function add_accumulations(sys::ODESystem, vars::Vector{<:Pair})
553552
@set! sys.eqs = [eqs; Equation[D(a) ~ v[2] for (a, v) in zip(avars, vars)]]
554553
@set! sys.unknowns = [get_unknowns(sys); avars]
555554
@set! sys.defaults = merge(get_defaults(sys), Dict(a => 0.0 for a in avars))
556-
end
555+
end

src/systems/nonlinear/initializesystem.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ $(TYPEDSIGNATURES)
33
44
Generate `NonlinearSystem` which initializes an ODE problem from specified initial conditions of an `ODESystem`.
55
"""
6-
function generate_initializesystem(sys::ODESystem; name = nameof(sys), guesses = Dict(), check_defguess = false, kwargs...)
6+
function generate_initializesystem(sys::ODESystem; name = nameof(sys),
7+
guesses = Dict(), check_defguess = false, kwargs...)
78
sts, eqs = unknowns(sys), equations(sys)
89
idxs_diff = isdiffeq.(eqs)
910
idxs_alge = .!idxs_diff
@@ -18,7 +19,7 @@ function generate_initializesystem(sys::ODESystem; name = nameof(sys), guesses =
1819

1920
# Refactor to ODESystem construction
2021
# should be ModelingToolkit.guesses(sys)
21-
22+
2223
guesses = merge(get_guesses(sys), todict(guesses))
2324

2425
for st in full_states

src/systems/nonlinear/nonlinearsystem.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ function SciMLBase.NonlinearFunction{iip}(sys::NonlinearSystem, dvs = unknowns(s
289289
NonlinearFunction{iip}(f,
290290
sys = sys,
291291
jac = _jac === nothing ? nothing : _jac,
292-
resid_prototype = length(dvs) == length(equations(sys)) ? nothing : zeros(length(equations(sys))),
292+
resid_prototype = length(dvs) == length(equations(sys)) ? nothing :
293+
zeros(length(equations(sys))),
293294
jac_prototype = sparse ?
294295
similar(calculate_jacobian(sys, sparse = sparse),
295296
Float64) : nothing,
@@ -334,7 +335,8 @@ function NonlinearFunctionExpr{iip}(sys::NonlinearSystem, dvs = unknowns(sys),
334335
end
335336

336337
jp_expr = sparse ? :(similar($(get_jac(sys)[]), Float64)) : :nothing
337-
resid_expr = length(dvs) == length(equations(sys)) ? :nothing : :(zeros($(length(equations(sys)))))
338+
resid_expr = length(dvs) == length(equations(sys)) ? :nothing :
339+
:(zeros($(length(equations(sys)))))
338340
ex = quote
339341
f = $f
340342
jac = $_jac

src/systems/systemstructure.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,8 @@ function _structural_simplify!(state::TearingState, io; simplify = false,
638638

639639
if sys isa ODESystem
640640
isys = ModelingToolkit.generate_initializesystem(sys)
641-
!isempty(equations(isys)) && (isys = structural_simplify(isys; fully_determined = false))
641+
!isempty(equations(isys)) &&
642+
(isys = structural_simplify(isys; fully_determined = false))
642643
@set! sys.initializesystem = isys
643644
neqs = length(equations(isys))
644645
nunknown = length(unknowns(isys))

test/initializationsystem.jl

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,65 @@ using ModelingToolkit: t_nounits as t, D_nounits as D
33

44
@connector Port begin
55
p(t)
6-
dm(t)=0, [connect = Flow]
6+
dm(t) = 0, [connect = Flow]
77
end
88

99
@connector Flange begin
10-
dx(t)=0
10+
dx(t) = 0
1111
f(t), [connect = Flow]
1212
end
1313

1414
# Components ----
1515
@mtkmodel Orifice begin
1616
@parameters begin
17-
Cₒ=2.7
18-
Aₒ=0.00094
19-
ρ₀=1000
20-
p′=0
17+
Cₒ = 2.7
18+
Aₒ = 0.00094
19+
ρ₀ = 1000
20+
p′ = 0
2121
end
2222
@variables begin
23-
dm(t)=0
24-
p₁(t)=p′
25-
p₂(t)=p′
23+
dm(t) = 0
24+
p₁(t) = p′
25+
p₂(t) = p′
2626
end
2727
@components begin
28-
port₁ = Port(p=p′)
29-
port₂ = Port(p=p′)
28+
port₁ = Port(p = p′)
29+
port₂ = Port(p = p′)
3030
end
3131
begin
32-
u = dm/(ρ₀*Aₒ)
32+
u = dm / (ρ₀ * Aₒ)
3333
end
3434
@equations begin
3535
dm ~ +port₁.dm
3636
dm ~ -port₂.dm
3737
p₁ ~ port₁.p
3838
p₂ ~ port₂.p
3939

40-
p₁ - p₂ ~ (1/2)*ρ₀*u^2*Cₒ
40+
p₁ - p₂ ~ (1 / 2) * ρ₀ * u^2 * Cₒ
4141
end
4242
end
4343

4444
@mtkmodel Volume begin
4545
@parameters begin
46-
A=0.1
47-
ρ₀=1000
48-
β=2e9
49-
direction=+1
46+
A = 0.1
47+
ρ₀ = 1000
48+
β = 2e9
49+
direction = +1
5050
p′
5151
x′
5252
end
5353
@variables begin
54-
p(t)=p′
55-
x(t)=x′
56-
dm(t)=0
57-
f(t)=p′ * A
58-
dx(t)=0
54+
p(t) = p′
55+
x(t) = x′
56+
dm(t) = 0
57+
f(t) = p′ * A
58+
dx(t) = 0
5959
r(t), [guess = 1000]
6060
dr(t), [guess = 1000]
6161
end
6262
@components begin
63-
port = Port(p=p′)
64-
flange = Flange(f=-p′ * A * direction)
63+
port = Port(p = p′)
64+
flange = Flange(f = -p′ * A * direction)
6565
end
6666
@equations begin
6767
D(x) ~ dx
@@ -72,8 +72,8 @@ end
7272
f ~ -flange.f * direction # force is leaving
7373
dx ~ flange.dx * direction
7474

75-
r ~ ρ₀*(1 + p/β)
76-
dm ~ (r*dx*A) + (dr*x*A)
75+
r ~ ρ₀ * (1 + p / β)
76+
dm ~ (r * dx * A) + (dr * x * A)
7777
f ~ p * A
7878
end
7979
end
@@ -84,13 +84,13 @@ end
8484
f′
8585
end
8686
@variables begin
87-
f(t)=f′
88-
x(t)=0
89-
dx(t)=0
90-
(t)=f′/m
87+
f(t) = f′
88+
x(t) = 0
89+
dx(t) = 0
90+
(t) = f′ / m
9191
end
9292
@components begin
93-
flange = Flange(f=f′)
93+
flange = Flange(f = f′)
9494
end
9595
@equations begin
9696
D(x) ~ dx
@@ -99,7 +99,7 @@ end
9999
f ~ flange.f
100100
dx ~ flange.dx
101101

102-
m*~ f
102+
m * ~ f
103103
end
104104
end
105105

@@ -109,16 +109,16 @@ end
109109
p₂′
110110
end
111111
begin #constants
112-
x′=0.5
113-
A=0.1
112+
x′ = 0.5
113+
A = 0.1
114114
end
115115
@components begin
116-
port₁ = Port(p=p₁′)
117-
port₂ = Port(p=p₂′)
118-
vol₁ = Volume(p′=p₁′, x′=x′, direction=-1)
119-
vol₂ = Volume(p′=p₂′, x′=x′, direction=+1)
120-
mass = Mass(f′=(p₂′ - p₁′)*A)
121-
flange = Flange(f=0)
116+
port₁ = Port(p = p₁′)
117+
port₂ = Port(p = p₂′)
118+
vol₁ = Volume(p′ = p₁′, x′ = x′, direction = -1)
119+
vol₂ = Volume(p′ = p₂′, x′ = x′, direction = +1)
120+
mass = Mass(f′ = (p₂′ - p₁′) * A)
121+
flange = Flange(f = 0)
122122
end
123123
@equations begin
124124
connect(port₁, vol₁.port)
@@ -132,7 +132,7 @@ end
132132
p′
133133
end
134134
@components begin
135-
port = Port(p=p′)
135+
port = Port(p = p′)
136136
end
137137
@equations begin
138138
port.p ~ p′
@@ -144,20 +144,20 @@ end
144144
c = 1000
145145
end
146146
@components begin
147-
flange = Flange(f=0)
147+
flange = Flange(f = 0)
148148
end
149149
@equations begin
150-
flange.f ~ c*flange.dx
150+
flange.f ~ c * flange.dx
151151
end
152152
end
153153

154154
@mtkmodel System begin
155155
@components begin
156-
res₁ = Orifice(p′=300e5)
157-
res₂ = Orifice(p′=0)
158-
act = Actuator(p₁′=300e5, p₂′=0)
159-
src = Source(p′=300e5)
160-
snk = Source(p′=0)
156+
res₁ = Orifice(p′ = 300e5)
157+
res₂ = Orifice(p′ = 0)
158+
act = Actuator(p₁′ = 300e5, p₂′ = 0)
159+
src = Source(p′ = 300e5)
160+
snk = Source(p′ = 0)
161161
dmp = Damper()
162162
end
163163
@equations begin
@@ -177,14 +177,14 @@ initsol = solve(initprob, reltol = 1e-12, abstol = 1e-12)
177177
@test SciMLBase.successful_retcode(initsol)
178178

179179
allinit = unknowns(sys) .=> initsol[unknowns(sys)]
180-
prob = ODEProblem(sys, allinit, (0,0.1))
180+
prob = ODEProblem(sys, allinit, (0, 0.1))
181181
sol = solve(prob, Rodas5P())
182182
# If initialized incorrectly, then it would be InitialFailure
183183
@test sol.retcode == SciMLBase.ReturnCode.Unstable
184184

185185
@connector Flange begin
186186
dx(t), [guess = 0]
187-
f(t), [guess = 0, connect=Flow]
187+
f(t), [guess = 0, connect = Flow]
188188
end
189189

190190
@mtkmodel Mass begin
@@ -202,9 +202,9 @@ end
202202
# connectors
203203
flange.dx ~ dx
204204
flange.f ~ -f
205-
205+
206206
# physics
207-
f ~ m*D(dx)
207+
f ~ m * D(dx)
208208
end
209209
end
210210

@@ -223,16 +223,16 @@ end
223223
# connectors
224224
flange.dx ~ dx
225225
flange.f ~ -f
226-
226+
227227
# physics
228-
f ~ d*dx
228+
f ~ d * dx
229229
end
230230
end
231231

232232
@mtkmodel MassDamperSystem begin
233233
@components begin
234-
mass = Mass(;dx=100,m=10)
235-
damper = Damper(;d=1)
234+
mass = Mass(; dx = 100, m = 10)
235+
damper = Damper(; d = 1)
236236
end
237237
@equations begin
238238
connect(mass.flange, damper.flange)
@@ -246,7 +246,7 @@ initsol = solve(initprob, reltol = 1e-12, abstol = 1e-12)
246246
@test SciMLBase.successful_retcode(initsol)
247247

248248
allinit = unknowns(sys) .=> initsol[unknowns(sys)]
249-
prob = ODEProblem(sys, allinit, (0,0.1))
249+
prob = ODEProblem(sys, allinit, (0, 0.1))
250250
sol = solve(prob, Rodas5P())
251251
# If initialized incorrectly, then it would be InitialFailure
252-
@test sol.retcode == SciMLBase.ReturnCode.Success
252+
@test sol.retcode == SciMLBase.ReturnCode.Success

0 commit comments

Comments
 (0)