Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start to Translational library with both Velocity and Position based connectors #114

Merged
merged 31 commits into from
Oct 4, 2022
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d5d8167
Start of Translational
Sep 11, 2022
fe7d103
initial Body, Spring, Damper
Sep 14, 2022
b3133a1
Merge branch 'SciML:main' into main
bradcarman Sep 14, 2022
ac567e6
implemented runtests
Sep 14, 2022
e40ba3a
added Fixed and updated Spring
Sep 15, 2022
3318ffd
Draft report comparing Position and Velocity
Sep 17, 2022
0402860
fixed position bug
Sep 17, 2022
dc12bde
latest Translational changes
Sep 18, 2022
bd42fb3
added multi2d start
Sep 22, 2022
bb912fc
Add n-pendulum
YingboMa Sep 27, 2022
5959213
Prioritize the angle to be a state
YingboMa Sep 29, 2022
7a8d9a4
finished
Sep 29, 2022
646fab7
Merge branch 'bgc/multi2d' of https://github.com/bradcarman/ModelingT…
Sep 29, 2022
71e6eef
removed figures
Sep 29, 2022
a662ca9
fixed test
Sep 29, 2022
76bc87d
Merge branch 'SciML:main' into bgc/multi2d
bradcarman Sep 29, 2022
f319d05
format
Sep 29, 2022
b4dbc46
fixed docs/make.jl
Sep 29, 2022
95588a2
format update
Sep 29, 2022
04706e0
fixed interface
Sep 30, 2022
a8ed6f2
Format
YingboMa Sep 30, 2022
86b256d
changed from Port() to MechanicalPort()
Sep 30, 2022
4321a61
Merge branch 'bgc/multi2d' of https://github.com/bradcarman/ModelingT…
Sep 30, 2022
68448cf
changed ports to flange
Sep 30, 2022
41b1e45
Format
YingboMa Sep 30, 2022
af32447
delta_s_0
YingboMa Sep 30, 2022
b8b393c
multibody example
Oct 2, 2022
5df1d9a
Add state_priority annotation, bound MTK, and format
YingboMa Oct 3, 2022
5eecfce
Remove SineForce
YingboMa Oct 3, 2022
4dee843
fixed docs
Oct 4, 2022
516f3ab
Merge branch 'main' into bgc/multi2d
YingboMa Oct 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Format
  • Loading branch information
YingboMa committed Sep 30, 2022
commit 41b1e45bfe19f3c00e05ae341001d0508bad4868
51 changes: 20 additions & 31 deletions src/Mechanical/MultiBody2D/components.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
function Link(; name, m, l, I, g)

pars = @parameters begin
m = m
l = l
@@ -12,7 +11,7 @@ function Link(; name, m, l, I, g)
dA(t) = 0
ddA(t) = 0

x_cm(t) = l/2
x_cm(t) = l / 2
y_cm(t) = 0

dx_cm(t) = 0
@@ -26,42 +25,32 @@ function Link(; name, m, l, I, g)

x1(t) = 0
dx1(t) = 0

end


@named TX1 = MechanicalPort()


eqs = [
eqs = [D(x_cm) ~ dx_cm
D(y_cm) ~ dy_cm
D(dx_cm) ~ ddx_cm
D(dy_cm) ~ ddy_cm
D(A) ~ dA
D(dA) ~ ddA

D(x_cm) ~ dx_cm
D(y_cm) ~ dy_cm
# x forces
m * ddx_cm ~ fx1

D(dx_cm) ~ ddx_cm
D(dy_cm) ~ ddy_cm
# y forces
m * ddy_cm ~ m * g + fy1

D(A) ~ dA
D(dA) ~ ddA
# torques
I * ddA ~ m * g * (l / 2) * cos(A)

# x forces
m*ddx_cm ~ fx1
# geometry
x_cm ~ l * cos(A) / 2 + x1
y_cm ~ l * sin(A) / 2
TX1.f ~ fx1
D(x1) ~ TX1.v]

# y forces
m*ddy_cm ~ m*g + fy1

# torques
I * ddA ~ m * g * (l/2) * cos(A)

# geometry
x_cm ~ l * cos(A)/2 + x1
y_cm ~ l * sin(A)/2

TX1.f ~ fx1
D(x1) ~ TX1.v

]


return ODESystem(eqs, t, vars, pars; name = name, systems = [TX1], defaults = [TX1.v => 0])
return ODESystem(eqs, t, vars, pars; name = name, systems = [TX1],
defaults = [TX1.v => 0])
end
12 changes: 8 additions & 4 deletions src/Mechanical/Translational/components.jl
Original file line number Diff line number Diff line change
@@ -9,7 +9,8 @@ Fixes a flange position (velocity = 0)
function Fixed(; name)
@named flange = MechanicalPort()
eqs = [flange.v ~ 0]
return compose(ODESystem(eqs, t, [], []; name = name, defaults = [flange.v => 0]), flange)
return compose(ODESystem(eqs, t, [], []; name = name, defaults = [flange.v => 0]),
flange)
end

"""
@@ -112,7 +113,8 @@ function Spring(::Val{:relative}; name, k, delta_s_0 = 0.0, v_a_0 = 0.0, v_b_0 =
flange_a.f ~ +f
flange_b.f ~ -f]
return compose(ODESystem(eqs, t, vars, pars; name = name,
defaults = [flange_a.v => v_a_0, flange_b.v => v_b_0]), flange_a,
defaults = [flange_a.v => v_a_0, flange_b.v => v_b_0]),
flange_a,
flange_b) #flange_a.f => +k*delta_s_0, flange_b.f => -k*delta_s_0
end

@@ -142,7 +144,8 @@ function Spring(::Val{:absolute}; name, k, s_a_0 = 0, s_b_0 = 0, v_a_0 = 0.0, v_
flange_a.f ~ +f
flange_b.f ~ -f]
return compose(ODESystem(eqs, t, vars, pars; name = name,
defaults = [flange_a.v => v_a_0, flange_b.v => v_b_0]), flange_a,
defaults = [flange_a.v => v_a_0, flange_b.v => v_b_0]),
flange_a,
flange_b) #, flange_a.f => k * (s_a_0 - s_b_0 - l)
end

@@ -179,6 +182,7 @@ function Damper(; name, d, v_a_0 = 0.0, v_b_0 = 0.0)
flange_a.f ~ +f
flange_b.f ~ -f]
return compose(ODESystem(eqs, t, vars, pars; name = name,
defaults = [flange_a.v => v_a_0, flange_b.v => v_b_0]), flange_a,
defaults = [flange_a.v => v_a_0, flange_b.v => v_b_0]),
flange_a,
flange_b) #flange_a.f => +(v_a_0 - v_b_0)*d, flange_b.f => -(v_a_0 - v_b_0)*d
end
10 changes: 5 additions & 5 deletions src/Mechanical/Translational/sources.jl
Original file line number Diff line number Diff line change
@@ -2,12 +2,12 @@ function SineForce(; name, amp, freq)
@named flange = MechanicalPort()

pars = @parameters begin
amp=amp
freq=freq
amp = amp
freq = freq
end
vars = []
eqs = [
flange.f ~ amp*sin(2*π*t*freq)
flange.f ~ amp * sin(2 * π * t * freq),
]
compose(ODESystem(eqs, t, vars, pars; name = name, defaults=[flange.v => 0]), flange)
end
compose(ODESystem(eqs, t, vars, pars; name = name, defaults = [flange.v => 0]), flange)
end
3 changes: 2 additions & 1 deletion src/Mechanical/TranslationalPosition/components.jl
Original file line number Diff line number Diff line change
@@ -59,7 +59,8 @@ function Mass(; name, m, s_0 = 0.0, v_0 = 0.0)
end

const REL = Val(:relative)
function Spring(::Val{:relative}; name, k, v_a_0 = 0.0, v_b_0 = 0.0, delta_s0 = 0, s_a_0 = 0,
function Spring(::Val{:relative}; name, k, v_a_0 = 0.0, v_b_0 = 0.0, delta_s0 = 0,
s_a_0 = 0,
s_b_0 = 0)
pars = @parameters begin
k = k
17 changes: 8 additions & 9 deletions test/Mechanical/multibody.jl
Original file line number Diff line number Diff line change
@@ -6,29 +6,28 @@ using DifferentialEquations

@parameters t

@named link = Link(;m=1, l=10, I=π*8.33, g=-9.807)
@named cart = Mass(;m=1, s_0=0)
@named link = Link(; m = 1, l = 10, I = π * 8.33, g = -9.807)
@named cart = Mass(; m = 1, s_0 = 0)
# @named force = SineForce(;amp=3e3, freq=15)
# @named fixed = Fixed()
# @named m1 = Mass(;m=0.5)
# @named m2 = Mass(;m=0.5)

eqs = [
connect(link.TX1, cart.flange) #, force.flange)
connect(link.TX1, cart.flange), #, force.flange)
# connect(link.TY1, fixed.flange)
# connect(link.TX2, m1.flange)
# connect(link.TY2, m2.flange)
]

@named model = ODESystem(eqs, t, [], []; systems=[link, cart])
@named model = ODESystem(eqs, t, [], []; systems = [link, cart])

sys = structural_simplify(model)
# sys = expand_connections(model)
# @set! sys.eqs = [(typeof(eq.lhs) != ModelingToolkit.Connection) & !ModelingToolkit._iszero(eq.lhs) & !ModelingToolkit.isdifferential(eq.lhs) ? 0 ~ eq.rhs - eq.lhs : eq for eq in sys.eqs]


prob = ODEProblem(sys, [], (0.0, 5), []; jac=true)
NEWTON = NLNewton(check_div = false, always_new = true, max_iter=100, relax=4//10)
sol = solve(prob, ImplicitEuler(nlsolve = NEWTON), dt=0.0001, adaptive=false)
prob = ODEProblem(sys, [], (0.0, 5), []; jac = true)
NEWTON = NLNewton(check_div = false, always_new = true, max_iter = 100, relax = 4 // 10)
sol = solve(prob, ImplicitEuler(nlsolve = NEWTON), dt = 0.0001, adaptive = false)
# sol = solve(prob)
plot(sol, idxs=[cart.s])
plot(sol, idxs = [cart.s])