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

Add translational library #85

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
Binary file added .DS_Store
Binary file not shown.
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add .vscode and .DS_Store in global git ignore setting. Check out https://sebastiandedeyne.com/setting-up-a-global-gitignore-file/

// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "julia",
"request": "launch",
"name": "Run active Julia file",
"program": "${file}",
"stopOnEntry": false,
"cwd": "${workspaceFolder}",
"juliaEnv": "${command:activeJuliaEnvironment}"
}
]
}
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/Mechanical/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions src/Mechanical/Mechanical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Mechanical
using ModelingToolkit

include("Rotational/Rotational.jl")
include("Translational/Translational.jl")

end

19 changes: 19 additions & 0 deletions src/Mechanical/Translational/Translational.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Translational

using ModelingToolkit, Symbolics, IfElse, OrdinaryDiffEq
using OffsetArrays
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove not needed packages

using ...Blocks: RealInput, RealOutput

@parameters t
D = Differential(t)

export Flange
include("utils.jl")

export Fixed, Inertia, Spring, Damper, IdealGear
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export Fixed, Inertia, Spring, Damper, IdealGear
export Fixed, Mass, Spring, Damper, IdealGear

include("components.jl")

export Force
include("sources.jl")

end
58 changes: 58 additions & 0 deletions src/Mechanical/Translational/components.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
function Fixed(; name, s0=0.0)
@named flange = Flange()
@parameters s0 = s0
eqs = [flange.s ~ s0]
return compose(ODESystem(eqs, t, [], [s0]; name=name), flange)
end

function Mass(; name, m, s_start=0.0, v_start=0.0, a_start=0.0)
@named flange_a = Flange()
@named flange_b = Flange()
@parameters m = m
sts = @variables begin
s(t) = s_start
v(t) = v_start
a(t) = a_start
end
eqs = [
s ~ flange_a.s
s ~ flange_b.s
D(s) ~ v
D(v) ~ a
m * a ~ flange_a.f + flange_b.f
]
return compose(ODESystem(eqs, t, sts, [m]; name=name), flange_a, flange_b)
end

function Spring(; name, c, s_rel0=0.0)
@named partial_comp = PartialCompliant()
@unpack s_rel, f = partial_comp
pars = @parameters begin
c = c
s_rel0 = s_rel0
end
eqs = [f ~ c * (s_rel - s_rel0)]
extend(ODESystem(eqs, t, [], pars; name=name), partial_comp)
end

function Damper(; name, d)
@named partial_comp = PartialCompliantWithRelativeStates()
@unpack v_rel, f = partial_comp
pars = @parameters d = d
eqs = [f ~ d * v_rel]
extend(ODESystem(eqs, t, [], pars; name=name), partial_comp)
end

function IdealGear(; name, ratio, use_support=false)
@named partial_element = PartialElementaryTwoFlangesAndSupport2(use_support=use_support)
@unpack s_support, flange_a, flange_b = partial_element
@parameters ratio = ratio
sts = @variables s_a(t) = 0.0 s_b(t) = 0.0
eqs = [
s_a ~ flange_a.s - s_support
s_b ~ flange_b.s - s_support
s_a ~ ratio * s_b
0 ~ ratio * flange_a.f + flange_b.f
]
extend(ODESystem(eqs, t, sts, [ratio]; name=name), partial_element)
end
7 changes: 7 additions & 0 deletions src/Mechanical/Translational/sources.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function Force(; name, use_support=false)
@named partial_element = PartialElementaryOneFlangeAndSupport2(use_support=use_support)
@unpack flange = partial_element
@named f = RealInput() # Accelerating torque acting at flange (= -flange.tau)
eqs = [flange.f ~ -f.u]
return extend(ODESystem(eqs, t, [], []; name=name, systems=[f]), partial_element)
end
84 changes: 84 additions & 0 deletions src/Mechanical/Translational/utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
@connector function Flange(; name)
sts = @variables begin
s(t)
f(t), [connect = Flow]
end
ODESystem(Equation[], t, sts, [], name=name, defaults=Dict(s => 0.0, f => 0.0))
end

@connector function Support(; name)
sts = @variables begin
s(t)
f(t), [connect = Flow]
end
ODESystem(Equation[], t, sts, [], name=name, defaults=Dict(s => 0.0, f => 0.0))
end

function PartialCompliant(; name, s_rel_start=0.0, f_start=0.0)
@named flange_a = Flange()
@named flange_b = Flange()
sts = @variables begin
s_rel(t) = s_rel_start
f(t) = f_start
end
eqs = [
s_rel ~ flange_b.s - flange_a.s
flange_b.f ~ f
flange_a.f ~ -f
]
return compose(ODESystem(eqs, t, sts, []; name=name), flange_a, flange_b)
end

function PartialCompliantWithRelativeStates(; name, s_rel_start=0.0, v_start=0.0, a_start=0.0, f_start=0.0)
@named flange_a = Flange()
@named flange_b = Flange()
sts = @variables begin
s_rel(t) = s_rel_start
v_rel(t) = v_start
a_rel(t) = a_start
f(t) = f_start
end
eqs = [
s_rel ~ flange_b.s - flange_a.s
D(s_rel) ~ v_rel
D(v_rel) ~ a_rel
flange_b.f ~ f
flange_a.f ~ -f
]
return compose(ODESystem(eqs, t, sts, []; name=name), flange_a, flange_b)
end

function PartialElementaryOneFlangeAndSupport2(; name, use_support=false)
@named flange = Flange()
sys = [flange]
@variables s_support(t)
if use_support
@named support = Support()
eqs = [
support.s ~ s_support
support.f ~ -flange.f
]
push!(sys, support)
else
eqs = [s_support ~ 0]
end
return compose(ODESystem(eqs, t, [s_support], []; name=name), sys)
end

function PartialElementaryTwoFlangesAndSupport2(; name, use_support=false)
@named flange_a = Flange()
@named flange_b = Flange()
sys = [flange_a, flange_b]
@variables s_support(t) = 0.0
if use_support
@named support = Support()
eqs = [
support.s ~ s_support
support.f ~ -flange_a.f - flange_b.f
]
push!(sys, support)
else
eqs = [s_support ~ 0]
end
return compose(ODESystem(eqs, t, [s_support], []; name=name), sys)
end
Binary file added test/.DS_Store
Binary file not shown.
Binary file added test/Mechanical/.DS_Store
Binary file not shown.
36 changes: 36 additions & 0 deletions test/Mechanical/translational.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import ModelingToolkitStandardLibrary.Blocks
include("/Users/kianmolani/Documents/Project Files/Codebases/ModelingToolkitStandardLibrary.jl/src/Mechanical/Translational/Translational.jl")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should be removed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can I add the Translational module to the package and be able to import it using use ModelingToolkitStandardLibrary.Mechanical.Translational? Do I have to first merge this PR? I have tried some possible solutions but without success.

using ModelingToolkit, OrdinaryDiffEq, Test
using Plots
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't import Plots in test. CI cannot actually see plots.


@parameters t
D = Differential(t)

function fixed_mass_damper_system()
m = 1 # mass of sliding mass
L = 1 # length of mass, from left flange to right flange (undefined)
s_start = 3 # absolute position of sliding mass (ought to be of component center, if L were defined)
v_start = 10 # absolute velocity of sliding mass
d = 25 # damping constant of damper (expressed here in units of N*s/m)
s0 = 4.5 # fixed offset position of housing

@named fixed = Main.Translational.Fixed(s0=s0)
@named mass = Main.Translational.Mass(m=m, s_start=s_start, v_start=v_start)
@named damper = Main.Translational.Damper(d=d)

connections = [
connect(mass.flange_b, damper.flange_a)
connect(damper.flange_b, fixed.flange)
]

@named model = ODESystem(connections, t, systems=[fixed, mass, damper])
sys = structural_simplify(model)
prob = DAEProblem(sys, D.(states(sys)) .=> 0.0, [D(D(mass.s)) => 1.0], (0, 1.0))
sol = solve(prob, DFBDF())

display(Plots.plot(sol; vars=[mass.s]))
display(Plots.plot(sol; vars=[mass.v]))
display(Plots.plot(sol; vars=[mass.a]))
end

fixed_mass_damper_system()