Skip to content

Commit

Permalink
Merge pull request #309 from ven-k/vkb/zero_defaults
Browse files Browse the repository at this point in the history
Fix TranslationalModelica tests
  • Loading branch information
ChrisRackauckas authored Jul 22, 2024
2 parents 39d7dc8 + 7d907df commit 282ad40
Showing 1 changed file with 39 additions and 36 deletions.
75 changes: 39 additions & 36 deletions test/Mechanical/translational_modelica.jl
Original file line number Diff line number Diff line change
@@ -1,55 +1,58 @@
using ModelingToolkit, OrdinaryDiffEq, Test
using ModelingToolkit: t_nounits as t, D_nounits as D

using ModelingToolkitStandardLibrary.Blocks
import ModelingToolkitStandardLibrary.Mechanical.TranslationalModelica as TP
using ModelingToolkitStandardLibrary.Blocks: Sine
using ModelingToolkitStandardLibrary.Mechanical.TranslationalModelica: Damper, Spring, Mass,
Fixed, Force

@testset "spring damper mass fixed" begin
@named damper = TP.Damper(; d = 1)
@named spring = TP.Spring(; c = 1, s_rel0 = 1)
@named mass = TP.Mass(; m = 1, v = 1)
@named fixed = TP.Fixed(s0 = 1)

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

@named model = ODESystem(eqs, t; systems = [fixed, mass, spring, damper])

sys = structural_simplify(model)

foreach(println, full_equations(sys))
@mtkmodel SpringDamperMassFixed begin
@components begin
damper = Damper(; d = 1)
spring = Spring(; c = 1, s_rel0 = 1)
mass = Mass(; m = 1, v = 1, s = 0)
fixed = Fixed(s0 = 1)
end
@equations begin
connect(spring.flange_a, mass.flange_a, damper.flange_a)
connect(spring.flange_b, damper.flange_b, fixed.flange)
end
end

@mtkbuild sys = SpringDamperMassFixed()

prob = ODEProblem(sys, [], (0, 20.0), [])
sol = solve(prob, ImplicitMidpoint(), dt = 0.01)

@test sol[mass.v][1] == 1.0
@test sol[mass.v][end]0.0 atol=1e-4
@test sol[sys.mass.v][1] == 1.0
@test sol[sys.mass.v][end]0.0 atol=1e-4
end

@testset "driven spring damper mass" begin
@named damper = TP.Damper(; d = 1)
@named spring = TP.Spring(; c = 1, s_rel0 = 1)
@named mass = TP.Mass(; m = 1, v = 1)
@named fixed = TP.Fixed(; s0 = 1)
@named force = TP.Force()
@named source = Sine(frequency = 3, amplitude = 2)

eqs = [connect(force.f, source.output)
connect(force.flange, mass.flange_a)
connect(spring.flange_a, mass.flange_b, damper.flange_a)
connect(spring.flange_b, damper.flange_b, fixed.flange)]

@named model = ODESystem(eqs, t;
systems = [fixed, mass, spring, damper, force, source])

sys = structural_simplify(model)

foreach(println, full_equations(sys))
@mtkmodel DrivenSpringDamperMass begin
@components begin
damper = Damper(; d = 1)
spring = Spring(; 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(spring.flange_a, mass.flange_b, damper.flange_a)
connect(spring.flange_b, damper.flange_b, fixed.flange)
end
end

@mtkbuild sys = DrivenSpringDamperMass()

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

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

0 comments on commit 282ad40

Please sign in to comment.