From 8e657d75f2348705455193c96fd31a3259c24f63 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Wed, 11 Sep 2024 14:07:10 +0200 Subject: [PATCH 1/3] add SpringDamper component --- .../TranslationalModelica.jl | 2 +- .../TranslationalModelica/components.jl | 34 +++++++++++++++++++ test/Mechanical/translational_modelica.jl | 28 +++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/Mechanical/TranslationalModelica/TranslationalModelica.jl b/src/Mechanical/TranslationalModelica/TranslationalModelica.jl index 0b685e429..ee16578d1 100644 --- a/src/Mechanical/TranslationalModelica/TranslationalModelica.jl +++ b/src/Mechanical/TranslationalModelica/TranslationalModelica.jl @@ -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 diff --git a/src/Mechanical/TranslationalModelica/components.jl b/src/Mechanical/TranslationalModelica/components.jl index 9af2166cb..f0b1a532e 100644 --- a/src/Mechanical/TranslationalModelica/components.jl +++ b/src/Mechanical/TranslationalModelica/components.jl @@ -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 diff --git a/test/Mechanical/translational_modelica.jl b/test/Mechanical/translational_modelica.jl index c059527af..4ecbd127b 100644 --- a/test/Mechanical/translational_modelica.jl +++ b/test/Mechanical/translational_modelica.jl @@ -56,3 +56,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 = TranslationalModelica.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 From 535b5a1ed1c1c2f1d02d085c26233e2814602aa1 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Wed, 11 Sep 2024 14:48:01 +0200 Subject: [PATCH 2/3] fix Fixed --- src/Mechanical/TranslationalModelica/components.jl | 4 ++-- test/Mechanical/translational_modelica.jl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Mechanical/TranslationalModelica/components.jl b/src/Mechanical/TranslationalModelica/components.jl index f0b1a532e..e401270ab 100644 --- a/src/Mechanical/TranslationalModelica/components.jl +++ b/src/Mechanical/TranslationalModelica/components.jl @@ -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 diff --git a/test/Mechanical/translational_modelica.jl b/test/Mechanical/translational_modelica.jl index 4ecbd127b..d0bdd8f2f 100644 --- a/test/Mechanical/translational_modelica.jl +++ b/test/Mechanical/translational_modelica.jl @@ -3,7 +3,7 @@ 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 @@ -60,7 +60,7 @@ end @testset "driven SpringDamper mass" begin @mtkmodel DrivenSpringDamperMass2 begin @components begin - springdamper = TranslationalModelica.SpringDamper(; d = 1, c = 1, s_rel0 = 1) + springdamper = SpringDamper(; d = 1, c = 1, s_rel0 = 1) mass = Mass(; m = 1, v = 1, s = 0) fixed = Fixed(; s0 = 1) force = Force() From 2ceb95321ec3d6fa4d15dd81523702469f4e45e0 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Wed, 11 Sep 2024 15:28:14 +0200 Subject: [PATCH 3/3] format --- test/Mechanical/translational_modelica.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/Mechanical/translational_modelica.jl b/test/Mechanical/translational_modelica.jl index d0bdd8f2f..9155a9a08 100644 --- a/test/Mechanical/translational_modelica.jl +++ b/test/Mechanical/translational_modelica.jl @@ -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, SpringDamper + Fixed, Force, + SpringDamper @testset "spring damper mass fixed" begin @mtkmodel SpringDamperMassFixed begin