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 SpringDamper component #327

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using ...Blocks: RealInput, RealOutput
export Flange
include("utils.jl")

export Fixed, Mass, Spring, Damper
export Fixed, Mass, Spring, Damper, SpringDamper
include("components.jl")

export Force
Expand Down
38 changes: 36 additions & 2 deletions src/Mechanical/TranslationalModelica/components.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ Flange fixed in housing at a given position.
"""
@mtkmodel Fixed begin
@parameters begin
s0
s0 = 0
end

@components begin
flange = Flange(; s = 0.0)
flange = Flange()
end

@equations begin
Expand Down Expand Up @@ -113,3 +113,37 @@ Linear 1D translational damper
lossPower ~ f * v_rel
end
end

"""
SpringDamper(; name, c = 0.0, d = 0.0, s_rel0 = 0.0)

Linear 1D translational spring and damper in parallel

# Parameters:
- `c`: [N/m] Spring constant
- `d`: [N.s/m] Damping constant
- `s_rel0`: Unstretched spring length

# Connectors:
- `flange_a: 1-dim. translational flange on one side of spring`
- `flange_b: 1-dim. translational flange on opposite side of spring`

# Variables:
- `lossPower`: [W] Power dissipated by the damper
- `f`: [N] Total force
"""
@mtkmodel SpringDamper begin
@extend flange_a, flange_b, s_rel, v_rel, f = pc = PartialCompliantWithRelativeStates()
@parameters begin
d = 0.0, [description = "Damping constant [Ns/m]"]
c = 0.0, [description = "Spring constant [N/m]"]
s_rel0 = 0.0, [description = "Unstretched spring length [m]"]
end
@variables begin
lossPower(t), [description = "Power dissipated by the damper [W]"]
end
@equations begin
f ~ c * (s_rel - s_rel0) + d * v_rel
lossPower ~ d * v_rel^2
end
end
31 changes: 30 additions & 1 deletion test/Mechanical/translational_modelica.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ using ModelingToolkit: t_nounits as t, D_nounits as D

using ModelingToolkitStandardLibrary.Blocks: Sine
using ModelingToolkitStandardLibrary.Mechanical.TranslationalModelica: Damper, Spring, Mass,
Fixed, Force
Fixed, Force,
SpringDamper

@testset "spring damper mass fixed" begin
@mtkmodel SpringDamperMassFixed begin
Expand Down Expand Up @@ -56,3 +57,31 @@ end
@test -lb≈ub atol=1e-2
@test -0.11 < lb < -0.1
end

@testset "driven SpringDamper mass" begin
@mtkmodel DrivenSpringDamperMass2 begin
@components begin
springdamper = SpringDamper(; d = 1, c = 1, s_rel0 = 1)
mass = Mass(; m = 1, v = 1, s = 0)
fixed = Fixed(; s0 = 1)
force = Force()
source = Sine(frequency = 3, amplitude = 2)
end

@equations begin
connect(force.f, source.output)
connect(force.flange, mass.flange_a)
connect(springdamper.flange_a, mass.flange_b)
connect(springdamper.flange_b, fixed.flange)
end
end

@mtkbuild sys = DrivenSpringDamperMass2()

prob = ODEProblem(sys, [], (0, 20.0), [])
sol = solve(prob, Rodas4())

lb, ub = extrema(sol(15:0.05:20, idxs = sys.mass.v).u)
@test -lb≈ub atol=1e-2
@test -0.11 < lb < -0.1
end
Loading