Skip to content

Commit 633d8bc

Browse files
refactor: update to new problem syntax
1 parent 189debf commit 633d8bc

File tree

9 files changed

+48
-48
lines changed

9 files changed

+48
-48
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ ChainRulesCore = "1.24"
1919
ControlSystemsBase = "1.4"
2020
DataFrames = "1.7"
2121
DataInterpolations = "6"
22-
ForwardDiff = "0.10"
2322
DiffEqBase = "6.152"
23+
ForwardDiff = "0.10"
2424
IfElse = "0.1"
2525
LinearAlgebra = "1.10"
2626
ModelingToolkit = "9.47, 10"

docs/src/connectors/connections.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ nothing # hide
113113
The solution shows what we would expect, a non-linear dissipation of voltage and related decrease in current flow…
114114

115115
```@example connections
116-
prob = ODEProblem(sys, [1.0], (0, 10.0), [])
116+
prob = ODEProblem(sys, [1.0], (0, 10.0))
117117
sol = solve(prob)
118118
119119
p1 = plot(sol, idxs = [capacitor.v])
@@ -156,7 +156,7 @@ As expected, we have a similar solution…
156156

157157
```@example connections
158158
prob = ODEProblem(
159-
sys, [], (0, 10.0), []; initialization_eqs = [sys.body.s ~ 0, sys.body.v ~ 1])
159+
sys, [], (0, 10.0); initialization_eqs = [sys.body.s ~ 0, sys.body.v ~ 1])
160160
sol_v = solve(prob)
161161
162162
p1 = plot(sol_v, idxs = [body.v])
@@ -191,7 +191,7 @@ nothing # hide
191191
As can be seen, we get exactly the same result. The only difference here is that we are solving an extra equation, which allows us to plot the body position as well.
192192

193193
```@example connections
194-
prob = ODEProblem(sys, [], (0, 10.0), [], fully_determined=true)
194+
prob = ODEProblem(sys, [], (0, 10.0), fully_determined=true)
195195
sol_p = solve(prob)
196196
197197
p1 = plot(sol_p, idxs = [body.v])
@@ -281,7 +281,7 @@ function simplify_and_solve(damping, spring, body, ground; initialization_eqs =
281281
282282
println.(full_equations(sys))
283283
284-
prob = ODEProblem(sys, [], (0, 10.0), []; initialization_eqs, fully_determined=true)
284+
prob = ODEProblem(sys, [], (0, 10.0); initialization_eqs, fully_determined=true)
285285
sol = solve(prob; abstol = 1e-9, reltol = 1e-9)
286286
287287
return sol

src/Mechanical/TranslationalPosition/sources.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ Input signal acting as external force on a flange
1010
s = 0
1111
end
1212
@components begin
13-
flange = Flange(; s = s)
1413
f = RealInput()
1514
end
15+
begin
16+
defaults[flange.s] = s
17+
end
1618
@equations begin
1719
flange.f ~ -f.u
1820
end

test/Blocks/sources.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,8 @@ end
437437
sys = mtkcompile(iosys)
438438
s = complete(iosys)
439439
prob = ODEProblem(sys,
440-
[],
441-
(0.0, t_end),
442-
[s.src.buffer => Parameter(x, dt)];
440+
[s.src.buffer => Parameter(x, dt)],
441+
(0.0, t_end);
443442
tofloat = false)
444443
# prob = remake(prob; p = Parameter.(prob.p)) #<-- no longer needed with ModelingToolkit.jl PR #2231
445444

@@ -467,9 +466,8 @@ end
467466
sys = mtkcompile(iosys)
468467
s = complete(iosys)
469468
prob = ODEProblem(sys,
470-
[],
471-
(0.0, t_end),
472-
[s.src.buffer => x, s.src.sample_time => dt];
469+
[s.src.buffer => x, s.src.sample_time => dt],
470+
(0.0, t_end);
473471
tofloat = false)
474472

475473
sol = solve(prob, Rodas4())

test/Hydraulic/isothermal_compressible.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ NEWTON = NLNewton(
1010
check_div = false, always_new = true, max_iter = 100, relax = 9 // 10, κ = 1e-6)
1111

1212
@testset "Fluid Domain and Tube" begin
13-
function System(N; bulk_modulus, name)
13+
function FluidSystem(N; bulk_modulus, name)
1414
pars = @parameters begin
1515
bulk_modulus = bulk_modulus
1616
p_int = 0
@@ -33,9 +33,9 @@ NEWTON = NLNewton(
3333
System(eqs, t, [], pars; name, systems)
3434
end
3535

36-
@mtkcompile s1_1 = System(1; bulk_modulus = 1e9)
37-
@mtkcompile s1_2 = System(1; bulk_modulus = 2e9)
38-
@mtkcompile s5_1 = System(5; bulk_modulus = 1e9)
36+
@mtkcompile s1_1 = FluidSystem(1; bulk_modulus = 1e9)
37+
@mtkcompile s1_2 = FluidSystem(1; bulk_modulus = 2e9)
38+
@mtkcompile s5_1 = FluidSystem(5; bulk_modulus = 1e9)
3939

4040
p1_1 = ODEProblem(s1_1, [], (0, 0.05))
4141
p1_2 = ODEProblem(s1_2, [], (0, 0.05))
@@ -61,7 +61,7 @@ NEWTON = NLNewton(
6161
end
6262

6363
@testset "Valve" begin
64-
function System(; name)
64+
function ValveSystem(; name)
6565
pars = []
6666

6767
systems = @named begin
@@ -81,7 +81,7 @@ end
8181
System(eqs, t, [], pars; name, systems)
8282
end
8383

84-
@named valve_system = System()
84+
@named valve_system = ValveSystem()
8585
sys = mtkcompile(valve_system)
8686
prob = ODEProblem(sys, [], (0, 1))
8787
sol = solve(prob, Rodas5P(); abstol = 1e-6, reltol = 1e-9)
@@ -98,7 +98,7 @@ end
9898
end
9999

100100
@testset "DynamicVolume and minimum_volume feature" begin # Need help here
101-
function System(; name, area = 0.01, length = 0.1, damping_volume = length * area * 0.1)
101+
function TestSystem(; name, area = 0.01, length = 0.1, damping_volume = length * area * 0.1)
102102
pars = []
103103

104104
# DynamicVolume values
@@ -148,7 +148,7 @@ end
148148
System(eqs, t, [], pars; name, systems, initialization_eqs)
149149
end
150150

151-
@named sys = System()
151+
@named sys = TestSystem()
152152
sys = mtkcompile(sys; allow_symbolic = true)
153153
prob = ODEProblem(sys, [], (0, 5))
154154
sol = solve(prob, Rodas5P(); abstol = 1e-6, reltol = 1e-9)
@@ -190,7 +190,7 @@ end
190190
end
191191

192192
@testset "Actuator System" begin
193-
function System(use_input; name)
193+
function ActuatorSystem(use_input; name)
194194
pars = @parameters begin
195195
p_s = 200e5
196196
p_r = 5e5
@@ -280,12 +280,12 @@ end
280280
System(eqs, t, vars, pars; name, systems, initialization_eqs)
281281
end
282282

283-
@mtkcompile initsys = System(false)
283+
@mtkcompile initsys = ActuatorSystem(false)
284284

285285
initprob = ODEProblem(initsys, [], (0, 0))
286286
initsol = solve(initprob, Rodas5P())
287287

288-
@mtkcompile sys = System(true)
288+
@mtkcompile sys = ActuatorSystem(true)
289289

290290
dt = 1e-4
291291
time = 0:dt:0.1
@@ -318,7 +318,7 @@ end
318318
end
319319

320320
@testset "Prevent Negative Pressure" begin
321-
@component function System(; name)
321+
@component function HydraulicSystem(; name)
322322
pars = @parameters let_gas = 1
323323

324324
systems = @named begin
@@ -338,11 +338,11 @@ end
338338
return System(eqs, t, [], pars; name, systems, initialization_eqs)
339339
end
340340

341-
@mtkcompile sys = System()
341+
@mtkcompile sys = HydraulicSystem()
342342

343343
prob1 = ODEProblem(sys, [], (0, 0.05))
344344
# prob1 = remake(prob1; u0 = BigFloat.(prob1.u0))
345-
prob2 = ODEProblem(sys, [], (0, 0.05), [sys.let_gas => 0])
345+
prob2 = ODEProblem(sys, [sys.let_gas => 0], (0, 0.05))
346346

347347
# @time sol1 = solve(prob1, Rodas5P(); abstol=1e-9, reltol=1e-9) #BUG: Using BigFloat gives... ERROR: MethodError: no method matching getindex(::Missing, ::Int64)
348348
@time sol1 = solve(prob1, Rodas5P(); adaptive = false, dt = 1e-6) #TODO: fix BigFloat to implement abstol=1e-9, reltol=1e-9

test/Mechanical/rotational.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ using OrdinaryDiffEq: ReturnCode.Success
3030
@test SciMLBase.successful_retcode(sol1)
3131

3232
prob = DAEProblem(
33-
sys, D.(unknowns(sys)) .=> 0.0, [D(D(sys.inertia2.phi)) => 0.0], (0, 10.0))
33+
sys, [D.(unknowns(sys)) .=> 0.0; [D(D(sys.inertia2.phi)) => 0.0]], (0, 10.0))
3434
dae_sol = solve(prob, DFBDF())
3535
@test SciMLBase.successful_retcode(dae_sol)
3636
@test all(dae_sol[sys.inertia1.w] .== 0)
@@ -88,8 +88,8 @@ end
8888
end
8989

9090
@mtkcompile sys = TwoInertiasWithDrivingTorque()
91-
prob = DAEProblem(sys, D.(unknowns(sys)) .=> 0.0,
92-
[D(D(sys.inertia2.phi)) => 1.0, sys.spring.flange_b.phi => 0.0], (0, 10.0))
91+
prob = DAEProblem(sys, [D.(unknowns(sys)) .=> 0.0;
92+
[D(D(sys.inertia2.phi)) => 1.0, sys.spring.flange_b.phi => 0.0]], (0, 10.0))
9393
sol = solve(prob, DFBDF())
9494
@test SciMLBase.successful_retcode(sol)
9595

@@ -215,8 +215,8 @@ end
215215
end
216216

217217
@mtkcompile sys = StickSlip()
218-
prob = DAEProblem(sys, D.(unknowns(sys)) .=> 0.0,
219-
[sys.inertia.flange_b.tau => 0.0; unknowns(sys) .=> 0.0...], (0, 10.0))
218+
prob = DAEProblem(sys, [D.(unknowns(sys)) .=> 0.0;
219+
[sys.inertia.flange_b.tau => 0.0; unknowns(sys) .=> 0.0...]], (0, 10.0))
220220

221221
sol = solve(prob, DFBDF())
222222
@test SciMLBase.successful_retcode(sol)
@@ -264,7 +264,7 @@ end
264264
@test all(sol[sys.torque_sensor.tau.u] .== -sol[sys.inertia1.flange_b.tau])
265265

266266
prob = DAEProblem(
267-
sys, D.(unknowns(sys)) .=> 0.0, [D(D(sys.inertia2.phi)) => 0.0], (0, 10.0))
267+
sys, [D.(unknowns(sys)) .=> 0.0; [D(D(sys.inertia2.phi)) => 0.0]], (0, 10.0))
268268
sol = solve(prob, DFBDF())
269269
@test SciMLBase.successful_retcode(sol)
270270
@test all(sol[sys.inertia1.w] .== 0)

test/Mechanical/translational.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import ModelingToolkitStandardLibrary.Mechanical.Translational as TV
66
import ModelingToolkitStandardLibrary.Mechanical.TranslationalPosition as TP
77

88
@testset "Free" begin
9-
function System(; name)
9+
function TestSystem(; name)
1010
systems = @named begin
1111
acc = TV.Acceleration(false)
1212
a = Constant(; k = -10)
@@ -20,7 +20,7 @@ import ModelingToolkitStandardLibrary.Mechanical.TranslationalPosition as TP
2020
System(eqs, t, [], []; name, systems)
2121
end
2222

23-
@named system = System()
23+
@named system = TestSystem()
2424
s = complete(system)
2525
sys = mtkcompile(system)
2626
prob = ODEProblem(sys, [s.mass.s => 0], (0, 0.1))
@@ -53,7 +53,7 @@ end
5353
sys = mtkcompile(model)
5454

5555
prob = ODEProblem(
56-
sys, [], (0, 20.0), []; initialization_eqs, fully_determined = true)
56+
sys, [], (0, 20.0); initialization_eqs, fully_determined = true)
5757
sol = solve(prob; abstol = 1e-9, reltol = 1e-9)
5858

5959
return sol
@@ -88,7 +88,7 @@ end
8888

8989
@named source = Sine(frequency = 3, amplitude = 2)
9090

91-
function System(damping, spring, body, ground, f, source)
91+
function TestSystem(damping, spring, body, ground, f, source)
9292
eqs = [connect(f.f, source.output)
9393
connect(f.flange, body.flange)
9494
connect(spring.flange_a, body.flange, damping.flange_a)
@@ -100,15 +100,15 @@ end
100100
return model
101101
end
102102

103-
model = System(dv, sv, bv, gv, fv, source)
103+
model = TestSystem(dv, sv, bv, gv, fv, source)
104104
sys = mtkcompile(model)
105105
prob = ODEProblem(
106-
sys, [bv.s => 0, sv.delta_s => 1], (0, 20.0), [], fully_determined = true)
106+
sys, [bv.s => 0, sv.delta_s => 1], (0, 20.0), fully_determined = true)
107107
solv = solve(prob, Rodas4())
108108

109-
model = System(dp, sp, bp, gp, fp, source)
109+
model = TestSystem(dp, sp, bp, gp, fp, source)
110110
sys = mtkcompile(model)
111-
prob = ODEProblem(sys, [], (0, 20.0), [], fully_determined = true)
111+
prob = ODEProblem(sys, [], (0, 20.0), fully_determined = true)
112112
solp = solve(prob, Rodas4())
113113

114114
for sol in (solv, solp)
@@ -121,7 +121,7 @@ end
121121
@testset "sources & sensors" begin
122122
@testset "Translational" begin
123123
@testset "PositionSensor & ForceSensor" begin
124-
function System(; name)
124+
function TestSystem(; name)
125125
systems = @named begin
126126
pos = TV.Position()
127127
pos_sensor = TV.PositionSensor(; s = 1)
@@ -148,7 +148,7 @@ end
148148
System(eqs, t, [], []; name, systems)
149149
end
150150

151-
@named system = System()
151+
@named system = TestSystem()
152152
s = complete(system)
153153
sys = mtkcompile(system)
154154
prob = ODEProblem(sys, [], (0, 1 / 400))
@@ -231,7 +231,7 @@ end
231231
@named sys = System(
232232
eqs, t, [], []; systems = [force, source, mass, acc, acc_output])
233233
s = complete(mtkcompile(sys))
234-
prob = ODEProblem(s, [mass.s => 0], (0.0, pi), fully_determined = true)
234+
prob = ODEProblem(s, (0.0, pi), fully_determined = true)
235235
sol = solve(prob, Tsit5())
236236
@test sol[sys.acc_output.u] (sol[sys.mass.f] ./ m)
237237
end

test/Mechanical/translational_modelica.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ using ModelingToolkitStandardLibrary.Mechanical.TranslationalModelica: Damper, S
2323

2424
@mtkcompile sys = SpringDamperMassFixed()
2525

26-
prob = ODEProblem(sys, [], (0, 20.0), [])
26+
prob = ODEProblem(sys, [], (0, 20.0))
2727
sol = solve(prob, ImplicitMidpoint(), dt = 0.01)
2828

2929
@test sol[sys.mass.v][1] == 1.0
@@ -51,7 +51,7 @@ end
5151

5252
@mtkcompile sys = DrivenSpringDamperMass()
5353

54-
prob = ODEProblem(sys, [], (0, 20.0), [])
54+
prob = ODEProblem(sys, [], (0, 20.0))
5555
sol = solve(prob, Rodas4())
5656

5757
lb, ub = extrema(sol(15:0.05:20, idxs = sys.mass.v).u)
@@ -79,7 +79,7 @@ end
7979

8080
@mtkcompile sys = DrivenSpringDamperMass2()
8181

82-
prob = ODEProblem(sys, [], (0, 20.0), [])
82+
prob = ODEProblem(sys, [], (0, 20.0))
8383
sol = solve(prob, Rodas4())
8484

8585
lb, ub = extrema(sol(15:0.05:20, idxs = sys.mass.v).u)

test/multi_domain.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ using OrdinaryDiffEq: ReturnCode.Success
6969
@test sol[dc_motor.emf.i][idx_t](dc_gain * [V_step; -tau_L_step])[1] rtol=1e-3
7070

7171
@test_skip begin
72-
prob = DAEProblem(dc_motor, D.(unknowns(dc_motor)) .=> 0.0, Pair[], (0, 6.0))
72+
prob = DAEProblem(dc_motor, D.(unknowns(dc_motor)) .=> 0.0, (0, 6.0))
7373
sol = solve(prob, DFBDF())
7474
@test sol.retcode == Success
7575
# EMF equations
@@ -156,7 +156,7 @@ end
156156
@test all(sol[sys.inertia.w] .== sol[sys.speed_sensor.w.u])
157157

158158
@test_skip begin
159-
prob = DAEProblem(sys, D.(unknowns(sys)) .=> 0.0, Pair[], (0, 6.0))
159+
prob = DAEProblem(sys, D.(unknowns(sys)) .=> 0.0, (0, 6.0))
160160
sol = solve(prob, DFBDF())
161161
@test sol.retcode == Success
162162
# EMF equations

0 commit comments

Comments
 (0)