Skip to content

Commit 880a8be

Browse files
fix: fix globalscoping in World
1 parent 4d149eb commit 880a8be

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/components.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ function ori(sys, varw = false)
4040
end
4141
end
4242

43+
const WORLD_N = GlobalScope(only(@parameters n[1:3] [description = "gravity direction"]))
44+
const WORLD_G = GlobalScope(only(@parameters g [description = "gravitational acceleration of world"]))
45+
const WORLD_MU = GlobalScope(only(@parameters mu [description = "Gravity field constant [m³/s²] (default = field constant of earth)"]))
46+
const WORLD_POINT_GRAVITY = GlobalScope(only(@parameters point_gravity))
47+
4348
"""
4449
World(; name, render=true, point_gravity=false, n = [0.0, -1.0, 0.0], g=9.80665, mu=3.986004418e14)
4550
@@ -65,18 +70,14 @@ If a connection to the world is needed in a component model, use [`Fixed`](@ref)
6570
mu0 = mu
6671
@named frame_b = Frame()
6772

68-
@parameters n[1:3] = n0 [description = "gravity direction"]
69-
@parameters g=g0 [description = "gravitational acceleration of world"]
70-
@parameters mu=mu0 [description = "Gravity field constant [m³/s²] (default = field constant of earth)"]
7173
@parameters render=render
72-
@parameters point_gravity = point_gravity
73-
74+
ics = Dict(WORLD_N => n0, WORLD_G => g0, WORLD_MU => mu0, WORLD_POINT_GRAVITY => point_gravity)
7475
O = ori(frame_b)
7576
eqs = Equation[
7677
frame_b.r_0 ~ zeros(3)
7778
O ~ nullrotation()
7879
]
79-
System(eqs, t, [], [n; g; mu; point_gravity; render]; name, systems = [frame_b])
80+
System(eqs, t, [], [render]; initial_conditions = ics, name, systems = [frame_b])
8081
end
8182

8283
"""
@@ -86,14 +87,14 @@ const world = World(; name = :world)
8687

8788
"Compute the gravity acceleration, resolved in world frame"
8889
function gravity_acceleration(r)
89-
inner_gravity(GlobalScope(world.point_gravity), GlobalScope(world.mu), GlobalScope(world.g), GlobalScope.(collect(world.n)), collect(r))
90+
@show inner_gravity(WORLD_POINT_GRAVITY, WORLD_MU, WORLD_G, collect(WORLD_N), collect(r))
9091
end
9192

9293
function inner_gravity(point_gravity, mu, g, n, r)
9394
# This is slightly inefficient, producing three if statements, one for each array entry. The function registration for array-valued does not work properly so this is a workaround for now. Hitting, among other problems, https://github.com/SciML/ModelingToolkit.jl/issues/2808
9495
gvp = -(mu/(r'r))*(r/_norm(r))
9596
gvu = g * n
96-
ifelse.(point_gravity==true, gvp, gvu)
97+
[ifelse(point_gravity == true, gvp[i], gvu[i]) for i in 1:3]
9798
end
9899
99100

0 commit comments

Comments
 (0)