Skip to content

Commit

Permalink
add Position source (#330)
Browse files Browse the repository at this point in the history
* add Position source

* add docstring
  • Loading branch information
baggepinnen authored Sep 16, 2024
1 parent f839b67 commit 252b0f5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ include("utils.jl")
export Fixed, Mass, Spring, Damper, SpringDamper
include("components.jl")

export Force
export Force, Position
include("sources.jl")

end
43 changes: 43 additions & 0 deletions src/Mechanical/TranslationalModelica/sources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,46 @@ Input signal acting as external force on a flange
flange.f ~ -f.u
end
end

"""
Position(; name, exact = false, f_crit = 50)
Forced movement of a flange according to a reference position
The input signal `s_ref` defines the reference position in [m]. Flange flange is forced to move relative to the support connector according to this reference motion. According to parameter `exact`, this is done in the following way:
- `exact=true`: The reference position is treated exactly. This is only possible, if the input signal is defined by an analytical function which can be differentiated at least twice. If this prerequisite is fulfilled, the Modelica translator will differentiate the input signal twice in order to compute the reference acceleration of the flange.
- `exact=false`: The reference position is filtered and the second derivative of the filtered curve is used to compute the reference acceleration of the flange. This second derivative is not computed by numerical differentiation but by an appropriate realization of the filter. For filtering, a second order Bessel filter is used. The critical frequency (also called cut-off frequency) of the filter is defined via parameter `f_crit` in [Hz]. This value should be selected in such a way that it is higher as the essential low frequencies in the signal.
The input signal can be provided from one of the signal generator blocks of the block library `Blocks.Sources`.
"""
@mtkmodel Position begin
@extend (s,) = ptf = PartialElementaryOneFlangeAndSupport2()
@structural_parameters begin
exact = false
end
@parameters begin
f_crit = 50
end
@variables begin
v(t)
a(t)
end
@components begin
s_ref = RealInput()
end
begin
w_crit = 2π * f_crit
af = 1.3617
bf = 0.6180
end
@equations begin
if exact
s ~ s_ref.u
else
a ~ ((s_ref.u - s) * w_crit - af * v) * (w_crit / bf)
end
v ~ D(s)
a ~ D(v)
end
end
24 changes: 23 additions & 1 deletion test/Mechanical/translational_modelica.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,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
SpringDamper,
Position

@testset "spring damper mass fixed" begin
@mtkmodel SpringDamperMassFixed begin
Expand Down Expand Up @@ -85,3 +86,24 @@ end
@test -lbub atol=1e-2
@test -0.11 < lb < -0.1
end

@testset "Position source" begin
@mtkmodel TestPositionSource begin
@components begin
p1 = Position(exact = true)
source = Sine(frequency = 3, amplitude = 2)
mass = Mass(m = 1, v = 1, s = 0)
end

@equations begin
connect(source.output, p1.s_ref)
connect(p1.flange, mass.flange_a)
end
end

@mtkbuild sys = TestPositionSource()
prob = ODEProblem(sys, [], (0, 2pi))
sol = solve(prob, Rodas4())
tv = 0:0.1:(2pi)
@test sol(tv, idxs = sys.mass.s)@.(2sin(2pi * tv * 3)) atol=1e-2
end

0 comments on commit 252b0f5

Please sign in to comment.