Skip to content
LIBRARY
Examples.InitialConditions.md

Examples.InitialConditions

Setting of initial conditions.

Replicates the structure of Modelica.Mechanics.Translational.Examples.InitialConditions.

Two systems demonstrate different ways of specifying initial conditions.

Top system: A chain fixed2 → s2 (spring) → m3 (mass) → sd2 (spring-damper) → m4 (mass) with positions chosen so the system starts at rest: m3.s = fixed2.s0 + s2.s_rel0 + m3.L/2 = 1 + 2 + 1.5 = 4.5 m m4.s = 4.5 + 1.5 + 4 + 2.5 = 12.5 m

Bottom system: A chain fixed1 → s1 (spring) → m1 (mass) → sd1 (spring-damper) → m2 (mass) with spring s1 starting compressed (s_rel=0.5 vs s_rel0=1), so the system is not at rest.

Usage

TranslationalComponents.Examples.InitialConditions()

Behavior

julia
using TranslationalComponents #hide
using ModelingToolkit #hide
@named sys = TranslationalComponents.Examples.InitialConditions() #hide
full_equations(sys) #hide
<< @example-block not executed in draft mode >>

Source

dyad
"""
Setting of initial conditions.

Replicates the structure of Modelica.Mechanics.Translational.Examples.InitialConditions.

Two systems demonstrate different ways of specifying initial conditions.

**Top system:** A chain fixed2 → s2 (spring) → m3 (mass) → sd2 (spring-damper) → m4 (mass)
with positions chosen so the system starts at rest:
  m3.s = fixed2.s0 + s2.s_rel0 + m3.L/2 = 1 + 2 + 1.5 = 4.5 m
  m4.s = 4.5 + 1.5 + 4 + 2.5 = 12.5 m

**Bottom system:** A chain fixed1 → s1 (spring) → m1 (mass) → sd1 (spring-damper) → m2 (mass)
with spring s1 starting compressed (s_rel=0.5 vs s_rel0=1), so the system
is not at rest.
"""
example component InitialConditions
  "Fixed reference at s=1 (top system)"
  fixed2 = TranslationalComponents.Components.Fixed(s0 = 1)
  "Spring s2 with unstretched length 2 m (top system)"
  s2 = TranslationalComponents.Components.Spring(s_rel0 = 2, c = 1e3)
  "Mass m3 with L=3, starting at rest at s=4.5 (top system)"
  m3 = TranslationalComponents.Components.Mass(L = 3, m = 1, s = initial 4.5, v = initial 0)
  "Spring-damper sd2 with unstretched length 4 m (top system)"
  sd2 = TranslationalComponents.Components.SpringDamper(s_rel0 = 4, c = 111, d = 1)
  "Mass m4 with L=5, starting at rest at s=12.5 (top system)"
  m4 = TranslationalComponents.Components.Mass(L = 5, m = 1, s = initial 12.5, v = initial 0)
  "Fixed reference at s=-1 (bottom system)"
  fixed1 = TranslationalComponents.Components.Fixed(s0 = -1)
  "Spring s1 with unstretched length 1 m, starting compressed (bottom system)"
  s1 = TranslationalComponents.Components.Spring(s_rel0 = 1, c = 1e3, s_rel = initial 0.5)
  "Mass m1 with L=1, starting with v=0 (bottom system)"
  m1 = TranslationalComponents.Components.Mass(L = 1, m = 1, v = initial 0)
  "Spring-damper sd1 (bottom system)"
  sd1 = TranslationalComponents.Components.SpringDamper(s_rel0 = 1, c = 111, d = 1, s_rel = initial 1, v_rel = initial 0)
  "Mass m2 with L=2 (bottom system)"
  m2 = TranslationalComponents.Components.Mass(L = 2, m = 1)
relations
  # Top system connections
  connect(s2.flange_a, fixed2.flange)
  connect(m3.flange_a, s2.flange_b)
  connect(sd2.flange_a, m3.flange_b)
  connect(m4.flange_a, sd2.flange_b)
  # Bottom system connections
  connect(s1.flange_a, fixed1.flange)
  connect(m1.flange_a, s1.flange_b)
  connect(sd1.flange_a, m1.flange_b)
  connect(m2.flange_a, sd1.flange_b)
metadata {
  "Dyad": {
    "icons": {"default": "dyad://TranslationalComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 5,
        "atol": {
          "m1.s": 0.01,
          "m1.v": 0.01,
          "m3.s": 0.001,
          "m3.v": 0.001,
          "m4.s": 0.001,
          "m4.v": 0.001,
          "sd1.s_rel": 0.01,
          "sd1.v_rel": 0.01
        },
        "expect": {
          "final": {"m3.s": 4.5, "m3.v": 0, "m4.s": 12.5, "m4.v": 0},
          "signals": [
            "m1.s",
            "m1.v",
            "m3.s",
            "m3.v",
            "m4.s",
            "m4.v",
            "sd1.s_rel",
            "sd1.v_rel",
            "sd2.s_rel",
            "sd2.v_rel"
          ]
        }
      }
    }
  }
}
end
Flattened Source
dyad
"""
Setting of initial conditions.

Replicates the structure of Modelica.Mechanics.Translational.Examples.InitialConditions.

Two systems demonstrate different ways of specifying initial conditions.

**Top system:** A chain fixed2 → s2 (spring) → m3 (mass) → sd2 (spring-damper) → m4 (mass)
with positions chosen so the system starts at rest:
  m3.s = fixed2.s0 + s2.s_rel0 + m3.L/2 = 1 + 2 + 1.5 = 4.5 m
  m4.s = 4.5 + 1.5 + 4 + 2.5 = 12.5 m

**Bottom system:** A chain fixed1 → s1 (spring) → m1 (mass) → sd1 (spring-damper) → m2 (mass)
with spring s1 starting compressed (s_rel=0.5 vs s_rel0=1), so the system
is not at rest.
"""
example component InitialConditions
  "Fixed reference at s=1 (top system)"
  fixed2 = TranslationalComponents.Components.Fixed(s0 = 1)
  "Spring s2 with unstretched length 2 m (top system)"
  s2 = TranslationalComponents.Components.Spring(s_rel0 = 2, c = 1e3)
  "Mass m3 with L=3, starting at rest at s=4.5 (top system)"
  m3 = TranslationalComponents.Components.Mass(L = 3, m = 1, s = initial 4.5, v = initial 0)
  "Spring-damper sd2 with unstretched length 4 m (top system)"
  sd2 = TranslationalComponents.Components.SpringDamper(s_rel0 = 4, c = 111, d = 1)
  "Mass m4 with L=5, starting at rest at s=12.5 (top system)"
  m4 = TranslationalComponents.Components.Mass(L = 5, m = 1, s = initial 12.5, v = initial 0)
  "Fixed reference at s=-1 (bottom system)"
  fixed1 = TranslationalComponents.Components.Fixed(s0 = -1)
  "Spring s1 with unstretched length 1 m, starting compressed (bottom system)"
  s1 = TranslationalComponents.Components.Spring(s_rel0 = 1, c = 1e3, s_rel = initial 0.5)
  "Mass m1 with L=1, starting with v=0 (bottom system)"
  m1 = TranslationalComponents.Components.Mass(L = 1, m = 1, v = initial 0)
  "Spring-damper sd1 (bottom system)"
  sd1 = TranslationalComponents.Components.SpringDamper(s_rel0 = 1, c = 111, d = 1, s_rel = initial 1, v_rel = initial 0)
  "Mass m2 with L=2 (bottom system)"
  m2 = TranslationalComponents.Components.Mass(L = 2, m = 1)
relations
  # Top system connections
  connect(s2.flange_a, fixed2.flange)
  connect(m3.flange_a, s2.flange_b)
  connect(sd2.flange_a, m3.flange_b)
  connect(m4.flange_a, sd2.flange_b)
  # Bottom system connections
  connect(s1.flange_a, fixed1.flange)
  connect(m1.flange_a, s1.flange_b)
  connect(sd1.flange_a, m1.flange_b)
  connect(m2.flange_a, sd1.flange_b)
metadata {
  "Dyad": {
    "icons": {"default": "dyad://TranslationalComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 5,
        "atol": {
          "m1.s": 0.01,
          "m1.v": 0.01,
          "m3.s": 0.001,
          "m3.v": 0.001,
          "m4.s": 0.001,
          "m4.v": 0.001,
          "sd1.s_rel": 0.01,
          "sd1.v_rel": 0.01
        },
        "expect": {
          "final": {"m3.s": 4.5, "m3.v": 0, "m4.s": 12.5, "m4.v": 0},
          "signals": [
            "m1.s",
            "m1.v",
            "m3.s",
            "m3.v",
            "m4.s",
            "m4.v",
            "sd1.s_rel",
            "sd1.v_rel",
            "sd2.s_rel",
            "sd2.v_rel"
          ]
        }
      }
    }
  }
}
end


Test Cases

julia
using TranslationalComponents
using DyadInterface: TransientAnalysis, rebuild_sol, ODEAlg
using ModelingToolkit: toggle_namespacing, get_initial_conditions, @named
using CSV, DataFrames, Plots

snapshotsdir = joinpath(dirname(dirname(pathof(TranslationalComponents))), "test", "snapshots")
<< @setup-block not executed in draft mode >>

Test Case case1

julia
@named model_case1 = TranslationalComponents.Examples.InitialConditions()
model_case1 = toggle_namespacing(model_case1, false)

model_case1 = toggle_namespacing(model_case1, true)
result_case1 = TransientAnalysis(; model = model_case1, alg = ODEAlg.Auto(), start = 0e+0, stop = 5e+0, abstol=1e-6, reltol=1e-6)
sol_case1 = rebuild_sol(result_case1)
<< @setup-block not executed in draft mode >>
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.m1.s])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.InitialConditions_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.m1.s], width=2, label="Actual value of m1.s")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of m1.s")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.m1.v])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.InitialConditions_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.m1.v], width=2, label="Actual value of m1.v")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of m1.v")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.m3.s])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.InitialConditions_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.m3.s], width=2, label="Actual value of m3.s")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of m3.s")
end
scatter!(plt, [df_case1.t[end]], [4.5], label="Final Condition for `m3.s`")
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.m3.v])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.InitialConditions_case1_sig3.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.m3.v], width=2, label="Actual value of m3.v")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of m3.v")
end
scatter!(plt, [df_case1.t[end]], [0], label="Final Condition for `m3.v`")
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.m4.s])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.InitialConditions_case1_sig4.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.m4.s], width=2, label="Actual value of m4.s")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of m4.s")
end
scatter!(plt, [df_case1.t[end]], [12.5], label="Final Condition for `m4.s`")
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.m4.v])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.InitialConditions_case1_sig5.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.m4.v], width=2, label="Actual value of m4.v")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of m4.v")
end
scatter!(plt, [df_case1.t[end]], [0], label="Final Condition for `m4.v`")
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.sd1.s_rel])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.InitialConditions_case1_sig6.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.sd1.s_rel], width=2, label="Actual value of sd1.s_rel")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of sd1.s_rel")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.sd1.v_rel])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.InitialConditions_case1_sig7.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.sd1.v_rel], width=2, label="Actual value of sd1.v_rel")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of sd1.v_rel")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.sd2.s_rel])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.InitialConditions_case1_sig8.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.sd2.s_rel], width=2, label="Actual value of sd2.s_rel")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of sd2.s_rel")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.sd2.v_rel])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "TranslationalComponents.Examples.InitialConditions_case1_sig9.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.sd2.v_rel], width=2, label="Actual value of sd2.v_rel")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of sd2.v_rel")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>
  • Examples

  • Experiments

  • Analyses