Skip to content

Commit b51aa00

Browse files
committed
switch to array r in Body and FixedTrans
1 parent c312d75 commit b51aa00

File tree

2 files changed

+36
-44
lines changed

2 files changed

+36
-44
lines changed

src/PlanarMechanics/components.jl

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -72,36 +72,27 @@ https://github.com/dzimmer/PlanarMechanics/blob/743462f58858a808202be93b70839146
7272
end
7373

7474
vars = @variables begin
75-
fx(t)
76-
fy(t)
77-
rx(t) = rx
78-
ry(t) = ry
79-
vx(t)
80-
vy(t)
81-
ax(t)
82-
ay(t)
75+
f(t)[1:2]
76+
r(t)[1:2] = [rx, ry]
77+
v(t)[1:2]
78+
a(t)[1:2]
8379
phi(t) = phi
8480
ω(t)
8581
α(t)
8682
end
8783

8884
eqs = [
8985
# velocity is the time derivative of position
90-
rx ~ frame.x,
91-
ry ~ frame.y,
92-
vx ~ D(rx),
93-
vy ~ D(ry),
94-
phi ~ frame.phi,
95-
ω ~ D(phi),
86+
r .~ [frame.x, frame.y]
87+
v .~ D.(r)
88+
phi ~ frame.phi
89+
ω .~ D.(phi)
9690
# acceleration is the time derivative of velocity
97-
ax ~ D(vx),
98-
ay ~ D(vy),
99-
α ~ D(ω),
91+
a .~ D.(v)
92+
α .~ D.(ω)
10093
# newton's law
101-
fx ~ frame.fx,
102-
fy ~ frame.fy,
103-
ax ~ fx / m,
104-
ay ~ fy / m + gy,#ifelse(gy !== nothing, fy / m + gy, fy / m),
94+
f .~ [frame.fx, frame.fy]
95+
f .~ m*a + m*[0, -gy]#ifelse(gy !== nothing, fy / m + gy, fy / m),
10596
I * α ~ frame.j
10697
]
10798

@@ -130,20 +121,19 @@ https://github.com/dzimmer/PlanarMechanics/blob/743462f58858a808202be93b70839146
130121
@extend frame_a, frame_b = partial_frames = PartialTwoFrames()
131122

132123
@parameters begin
133-
rx = 0,
124+
r[1:2] = [1.0, 0],
134125
[
135-
description = "Fixed x-length of the rod resolved w.r.t to body frame_a at phi = 0"
136-
]
137-
ry = 0,
138-
[
139-
description = "Fixed y-length of the rod resolved w.r.t to body frame_a at phi = 0"
126+
description = "Fixed x,y-length of the rod resolved w.r.t to body frame_a at phi = 0"
140127
]
141128
end
129+
begin
130+
r = collect(r)
131+
end
142132

143133
begin
144134
R = [cos(frame_a.phi) -sin(frame_a.phi);
145135
sin(frame_a.phi) cos(frame_a.phi)]
146-
r0 = R * [rx, ry]
136+
r0 = R * r
147137
end
148138

149139
@equations begin

test/test_PlanarMechanics.jl

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ g = -9.807
2626
@test SciMLBase.successful_retcode(sol)
2727

2828
free_falling_displacement = 0.5 * g * tspan[end]^2 # 0.5 * g * t^2
29-
@test sol[body.ry][end] free_falling_displacement
30-
@test sol[body.rx][end] == 0 # no horizontal displacement
29+
@test sol[body.r[2]][end] free_falling_displacement
30+
@test sol[body.r[1]][end] == 0 # no horizontal displacement
3131
@test all(sol[body.phi] .== 0)
32-
# plot(sol, idxs = [body.rx, body.ry])
32+
# plot(sol, idxs = [body.r[1], body.r[2]])
3333
end
3434

3535
@testset "Pendulum" begin
3636
# https://github.com/dzimmer/PlanarMechanics/blob/743462f58858a808202be93b708391461cbe2523/PlanarMechanics/Examples/Pendulum.mo
3737
@named ceiling = Planar.Fixed()
38-
@named rod = Planar.FixedTranslation(rx = 1.0, ry = 0.0)
38+
@named rod = Planar.FixedTranslation(r = [1.0, 0.0])
3939
@named body = Planar.Body(m = 1, I = 0.1)
4040
@named revolute = Planar.Revolute()
4141

@@ -74,7 +74,7 @@ end
7474

7575
# components
7676
@named body = Planar.Body(; m, I, gy = 0.0)
77-
@named fixed_translation = Planar.FixedTranslation(; rx = 10.0, ry = 0.0)
77+
@named fixed_translation = Planar.FixedTranslation(; r = [10, 0])
7878
@named fixed = Planar.Fixed()
7979
@named revolute = Planar.Revolute()#constant_ω = ω)
8080

@@ -204,18 +204,18 @@ end
204204
@test SciMLBase.successful_retcode(sol)
205205

206206
# the two bodyies falled the same distance, and so the absolute sensor attached to body1
207-
@test sol[abs_pos_sensor.y.u][end] sol[body1.ry][end] sol[body2.ry][end]
207+
@test sol[abs_pos_sensor.y.u][end] sol[body1.r[2]][end] sol[body2.r[2]][end]
208208
0.5 * g * tspan[end]^2
209209

210210
# sensor1 is attached to body1, so the relative y-position between body1 and the base is
211211
# equal to the absolute y-position of body1
212-
@test sol[body1.ry][end] -sol[rel_pos_sensor1.rel_y.u][end]
212+
@test sol[body1.r[2]][end] -sol[rel_pos_sensor1.rel_y.u][end]
213213

214214
# the relative y-position between body1 and body2 is zero
215215
@test sol[rel_pos_sensor2.rel_y.u][end] == 0
216216

217217
# no displacement in the x-direction
218-
@test sol[abs_pos_sensor.x.u][end] sol[body1.rx][end] sol[body2.rx][end]
218+
@test sol[abs_pos_sensor.x.u][end] sol[body1.r[1]][end] sol[body2.r[1]][end]
219219

220220
# velocity after t seconds v = g * t, so the relative y-velocity between body1 and the base is
221221
# equal to the absolute y-velocity of body1
@@ -238,10 +238,10 @@ end
238238
@testset "Measure Demo" begin
239239
# https://github.com/dzimmer/PlanarMechanics/blob/743462f58858a808202be93b708391461cbe2523/PlanarMechanics/Examples/MeasureDemo.mo
240240
@named body = Planar.Body(; m = 1, I = 0.1)
241-
@named fixed_translation = Planar.FixedTranslation(; rx = 1, ry = 0)
241+
@named fixed_translation = Planar.FixedTranslation(;)
242242
@named fixed = Planar.Fixed()
243243
@named body1 = Planar.Body(; m = 0.4, I = 0.02)
244-
@named fixed_translation1 = Planar.FixedTranslation(; rx = 0.4, ry = 0)
244+
@named fixed_translation1 = Planar.FixedTranslation(; r = [0.4, 0])
245245
@named abs_pos_sensor = Planar.AbsolutePosition(; resolve_in_frame = :world)
246246
@named rel_pos_sensor = Planar.RelativePosition(; resolve_in_frame = :world)
247247
@named revolute1 = Planar.Revolute()
@@ -304,7 +304,7 @@ end
304304
c_phi = 0)
305305
@named body = Planar.Body(; I = 0.1, m = 0.5, rx = 1, ry = 1)
306306
@named fixed = Planar.Fixed()
307-
@named fixed_translation = Planar.FixedTranslation(; rx = -1, ry = 0)
307+
@named fixed_translation = Planar.FixedTranslation(; r = [-1, 0])
308308

309309
connections = [
310310
connect(fixed.frame, fixed_translation.frame_a),
@@ -356,9 +356,11 @@ end
356356
damper,
357357
prismatic
358358
])
359-
sys = structural_simplify((model)) # Yingbo: fails with JSCompiler
360-
unset_vars = setdiff(unknowns(sys), keys(ModelingToolkit.defaults(sys)))
361-
prob = ODEProblem(sys, unset_vars .=> 0.0, (0, 5), [])
362-
sol = solve(prob, Rodas5P(), initializealg=BrownFullBasicInit())
363-
@test SciMLBase.successful_retcode(sol)
359+
@test_skip begin
360+
sys = structural_simplify(IRSystem(model)) # Yingbo: fails with JSCompiler
361+
unset_vars = setdiff(unknowns(sys), keys(ModelingToolkit.defaults(sys)))
362+
prob = ODEProblem(sys, unset_vars .=> 0.0, (0, 5), [])
363+
sol = solve(prob, Rodas5P(), initializealg=BrownFullBasicInit())
364+
@test SciMLBase.successful_retcode(sol)
365+
end
364366
end

0 commit comments

Comments
 (0)