Skip to content
LIBRARY
examples.SpringMassSystem.md

examples.SpringMassSystem

Spring-mass system

This example mirrors the Modelica spring-mass system and demonstrates that we can model a spring-mass system in two different ways: 2. Using a prismatic joint and a 1-dimensional translational spring attached to the joint's axis flange. The advantage of this approach is that the many elements from the Translational library can be easily used, and this implementation is usually more efficient than 3-dimensional springs.

  1. Using a 3-dimensional spring from MultibodyComponents.

Two parallel branches hang from a horizontal bar:

  • Left branch: bar1p1 (prismatic, downward) → body1, with a 1D translational spring spring1 connected between p1.support and p1.axis.

  • Right branch: bar2p2 (prismatic, downward) → body2, with a 3D multibody spring2 connected between the top (bar2.frame_b) and the body (body2.frame_a).

Both systems should produce identical motion, confirming that the 1D and 3D spring formulations are equivalent.

Usage

MultibodyComponents.examples.SpringMassSystem()

Behavior

Source

dyad
"""
# Spring-mass system

This example mirrors the [Modelica spring-mass system](https://doc.modelica.org/om/Modelica.Mechanics.MultiBody.Examples.Elementary.SpringMassSystem.html)
and demonstrates that we can model a spring-mass system in two different ways:

1. Using a prismatic joint and a 1-dimensional translational spring attached to
   the joint's axis flange. The advantage of this approach is that the many
   elements from the Translational library can be easily used, and this
   implementation is usually more efficient than 3-dimensional springs.
2. Using a 3-dimensional spring from MultibodyComponents.

Two parallel branches hang from a horizontal bar:
- Left branch: `bar1` → `p1` (prismatic, downward) → `body1`, with a 1D
  translational spring `spring1` connected between `p1.support` and `p1.axis`.
- Right branch: `bar2` → `p2` (prismatic, downward) → `body2`, with a 3D
  multibody `spring2` connected between the top (`bar2.frame_b`) and the
  body (`body2.frame_a`).

Both systems should produce identical motion, confirming that the 1D and 3D
spring formulations are equivalent.
"""
example component SpringMassSystem
  world = MultibodyComponents.World()
  # Horizontal bars
  bar1 = MultibodyComponents.FixedTranslation(r = [0.3, 0, 0])
  bar2 = MultibodyComponents.FixedTranslation(r = [0.3, 0, 0])
  # Left branch: prismatic joint + 1D spring + body
  p1 = MultibodyComponents.Prismatic(n = [0, -1, 0], s(initial = 0.1), v(initial = 0))
  spring1 = TranslationalComponents.Components.Spring(c = 30, s_rel0 = 0.1)
  body1 = MultibodyComponents.Body(m = 1, r_cm = [0, 0, 0], radius = 0.1, color = [1, 0.5, 0, 1])
  # Right branch: prismatic joint + 3D spring + body
  p2 = MultibodyComponents.Prismatic(n = [0, -1, 0], s(initial = 0.1), v(initial = 0))
  spring2 = MultibodyComponents.Spring(c = 30, s_unstretched = 0.1)
  body2 = MultibodyComponents.Body(m = 1, r_cm = [0, 0, 0], radius = 0.1)
relations
  # World → bar1
  connect(world.frame_b, bar1.frame_a)
  # Left branch: bar1 → p1 → body1 with 1D spring across the prismatic axis
  connect(bar1.frame_b, p1.frame_a)
  connect(p1.frame_b, body1.frame_a)
  connect(spring1.flange_a, p1.support)
  connect(spring1.flange_b, p1.axis)
  # bar1 → bar2
  connect(bar1.frame_b, bar2.frame_a)
  # Right branch: bar2 → p2 → body2 with 3D spring
  connect(bar2.frame_b, p2.frame_a)
  connect(p2.frame_b, body2.frame_a)
  connect(bar2.frame_b, spring2.frame_a)
  connect(body2.frame_a, spring2.frame_b)
end
Flattened Source
dyad
"""
# Spring-mass system

This example mirrors the [Modelica spring-mass system](https://doc.modelica.org/om/Modelica.Mechanics.MultiBody.Examples.Elementary.SpringMassSystem.html)
and demonstrates that we can model a spring-mass system in two different ways:

1. Using a prismatic joint and a 1-dimensional translational spring attached to
   the joint's axis flange. The advantage of this approach is that the many
   elements from the Translational library can be easily used, and this
   implementation is usually more efficient than 3-dimensional springs.
2. Using a 3-dimensional spring from MultibodyComponents.

Two parallel branches hang from a horizontal bar:
- Left branch: `bar1` → `p1` (prismatic, downward) → `body1`, with a 1D
  translational spring `spring1` connected between `p1.support` and `p1.axis`.
- Right branch: `bar2` → `p2` (prismatic, downward) → `body2`, with a 3D
  multibody `spring2` connected between the top (`bar2.frame_b`) and the
  body (`body2.frame_a`).

Both systems should produce identical motion, confirming that the 1D and 3D
spring formulations are equivalent.
"""
example component SpringMassSystem
  world = MultibodyComponents.World()
  # Horizontal bars
  bar1 = MultibodyComponents.FixedTranslation(r = [0.3, 0, 0])
  bar2 = MultibodyComponents.FixedTranslation(r = [0.3, 0, 0])
  # Left branch: prismatic joint + 1D spring + body
  p1 = MultibodyComponents.Prismatic(n = [0, -1, 0], s(initial = 0.1), v(initial = 0))
  spring1 = TranslationalComponents.Components.Spring(c = 30, s_rel0 = 0.1)
  body1 = MultibodyComponents.Body(m = 1, r_cm = [0, 0, 0], radius = 0.1, color = [1, 0.5, 0, 1])
  # Right branch: prismatic joint + 3D spring + body
  p2 = MultibodyComponents.Prismatic(n = [0, -1, 0], s(initial = 0.1), v(initial = 0))
  spring2 = MultibodyComponents.Spring(c = 30, s_unstretched = 0.1)
  body2 = MultibodyComponents.Body(m = 1, r_cm = [0, 0, 0], radius = 0.1)
relations
  # World → bar1
  connect(world.frame_b, bar1.frame_a)
  # Left branch: bar1 → p1 → body1 with 1D spring across the prismatic axis
  connect(bar1.frame_b, p1.frame_a)
  connect(p1.frame_b, body1.frame_a)
  connect(spring1.flange_a, p1.support)
  connect(spring1.flange_b, p1.axis)
  # bar1 → bar2
  connect(bar1.frame_b, bar2.frame_a)
  # Right branch: bar2 → p2 → body2 with 3D spring
  connect(bar2.frame_b, p2.frame_a)
  connect(p2.frame_b, body2.frame_a)
  connect(bar2.frame_b, spring2.frame_a)
  connect(body2.frame_a, spring2.frame_b)
metadata {}
end


Test Cases

No test cases defined.

  • Examples

  • Experiments

  • Analyses