From 7d907df7d0064b09d64a1b493b282a1936311372 Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Mon, 22 Jul 2024 11:53:35 +0200 Subject: [PATCH] test(TranslationalModelica): define the models well with init pos of the Mass This alleviates InitialFailure of models post removal of defaults --- test/Mechanical/translational_modelica.jl | 75 ++++++++++++----------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/test/Mechanical/translational_modelica.jl b/test/Mechanical/translational_modelica.jl index 3cb6fc3d5..c059527af 100644 --- a/test/Mechanical/translational_modelica.jl +++ b/test/Mechanical/translational_modelica.jl @@ -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 -lb≈ub atol=1e-2 @test -0.11 < lb < -0.1 end