Skip to content
LIBRARY
Examples.Oscillator.md

Examples.Oscillator

Oscillator demonstrates the use of initial conditions.

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

A spring–mass system is a mechanical oscillator. The resonant frequency is ω_res = sqrt(c / m). For c=10000 N/m and m=1 kg, f_res = ω_res/(2π) ≈ 15.9155 Hz.

System 1 (top, undamped): A sinusoidal force at the resonant frequency drives mass1 through a spring to a wall. Without damping, the amplitude grows without bound (resonance).

System 2 (bottom, damped): Same as system 1 but with a damper in parallel with the spring. Damping bounds the amplitude.

Note: MSL uses a one-flange Force source. Here we use the two-flange Force with flange_b connected to a Fixed ground, which is equivalent to a grounded one-flange Force.

Usage

TranslationalComponents.Examples.Oscillator()

Behavior

[connect(sine1+y,force1+f)connect(force1+flangeb,mass1+flangea)connect(force1+flangea,force1ground+flange)connect(mass1+flangeb,spring1+flangea)connect(spring1+flangeb,fixed1+flange)connect(sine2+y,force2+f)connect(force2+flangeb,mass2+flangea)connect(force2+flangea,force2ground+flange)connect(mass2+flangeb,spring2+flangea)connect(spring2+flangeb,fixed2+flange)connect(mass2+flangeb,damper1+flangea)connect(damper1+flangeb,fixed2+flange)sine1.y(t)=sine1.offset+sine1.amplitudeifelse(tsine1.starttime,sin(sine1.phase+6.283185307179586sine1.frequency(sine1.starttime+t)),sin(sine1.phase))force1.flangea.f(t)=force1.f(t)force1.flangeb.f(t)=force1.f(t)force1ground.flange.s(t)=force1ground.s0mass1.flangea.s(t)=12mass1.L+mass1.s(t)mass1.flangeb.s(t)=mass1.L2+mass1.s(t)mass1.v(t)=dmass1.s(t)dtmass1.a(t)=dmass1.v(t)dt(mass1.a(t)+mass1.gsin(mass1.theta))mass1.m=mass1.flangea.f(t)+mass1.flangeb.f(t)spring1.srel(t)=spring1.flangea.s(t)+spring1.flangeb.s(t)spring1.flangeb.f(t)=spring1.f(t)spring1.flangea.f(t)=spring1.f(t)spring1.f(t)=spring1.c(spring1.srel0+spring1.srel(t))fixed1.flange.s(t)=fixed1.s0sine2.y(t)=sine2.offset+sine2.amplitudeifelse(tsine2.starttime,sin(sine2.phase+6.283185307179586sine2.frequency(sine2.starttime+t)),sin(sine2.phase))force2.flangea.f(t)=force2.f(t)force2.flangeb.f(t)=force2.f(t)force2ground.flange.s(t)=force2ground.s0mass2.flangea.s(t)=12mass2.L+mass2.s(t)mass2.flangeb.s(t)=mass2.L2+mass2.s(t)mass2.v(t)=dmass2.s(t)dtmass2.a(t)=dmass2.v(t)dt(mass2.a(t)+mass2.gsin(mass2.theta))mass2.m=mass2.flangea.f(t)+mass2.flangeb.f(t)spring2.srel(t)=spring2.flangea.s(t)+spring2.flangeb.s(t)spring2.flangeb.f(t)=spring2.f(t)spring2.flangea.f(t)=spring2.f(t)spring2.f(t)=spring2.c(spring2.srel0+spring2.srel(t))damper1.srel(t)=damper1.flangeb.s(t)damper1.flangea.s(t)damper1.vrel(t)=ddamper1.srel(t)dtdamper1.flangeb.f(t)=damper1.f(t)damper1.flangea.f(t)=damper1.f(t)damper1.f(t)=damper1.ddamper1.vrel(t)damper1.lossPower(t)=damper1.vrel(t)damper1.f(t)fixed2.flange.s(t)=fixed2.s0]

Source

dyad
"""
Oscillator demonstrates the use of initial conditions.

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

A spring–mass system is a mechanical oscillator. The resonant frequency is
ω_res = sqrt(c / m). For c=10000 N/m and m=1 kg, f_res = ω_res/(2π) ≈ 15.9155 Hz.

**System 1 (top, undamped):** A sinusoidal force at the resonant frequency
drives mass1 through a spring to a wall. Without damping, the amplitude grows
without bound (resonance).

**System 2 (bottom, damped):** Same as system 1 but with a damper in parallel
with the spring. Damping bounds the amplitude.

Note: MSL uses a one-flange Force source. Here we use the two-flange Force
with flange_b connected to a Fixed ground, which is equivalent to a grounded
one-flange Force.
"""
example component Oscillator
  "Sinusoidal force signal at resonance frequency"
  sine1 = BlockComponents.Sources.Sine(amplitude = 1, frequency = 15.9155) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 80, "y1": 30, "x2": 180, "y2": 130, "rot": 0}
      },
      "tags": []
    }
  }
  "Force source for system 1"
  force1 = TranslationalComponents.Sources.Force() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 210, "y1": 210, "x2": 310, "y2": 310, "rot": 0}
      },
      "tags": []
    }
  }
  "Ground for force source 1"
  force1_ground = TranslationalComponents.Components.Fixed() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 90, "y1": 440, "x2": 190, "y2": 340, "rot": 180}
      },
      "tags": []
    }
  }
  "Mass for system 1 (undamped)"
  mass1 = TranslationalComponents.Components.Mass(L = 1, m = 1, s = initial -0.5, v = initial 0) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 400, "y1": 210, "x2": 500, "y2": 310, "rot": 0}
      },
      "tags": []
    }
  }
  "Spring for system 1"
  spring1 = TranslationalComponents.Components.Spring(s_rel0 = 1, c = 10000) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 640, "y1": 210, "x2": 740, "y2": 310, "rot": 0}
      },
      "tags": []
    }
  }
  "Wall for system 1"
  fixed1 = TranslationalComponents.Components.Fixed(s0 = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 830, "y1": 340, "x2": 930, "y2": 440, "rot": 0}
      },
      "tags": []
    }
  }
  "Sinusoidal force signal at resonance frequency"
  sine2 = BlockComponents.Sources.Sine(amplitude = 1, frequency = 15.9155) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 80, "y1": 500, "x2": 180, "y2": 600, "rot": 0}
      },
      "tags": []
    }
  }
  "Force source for system 2"
  force2 = TranslationalComponents.Sources.Force() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 220, "y1": 620, "x2": 320, "y2": 720, "rot": 0}
      },
      "tags": []
    }
  }
  "Ground for force source 2"
  force2_ground = TranslationalComponents.Components.Fixed() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 70, "y1": 770, "x2": 170, "y2": 870, "rot": 0}
      },
      "tags": []
    }
  }
  "Mass for system 2 (damped)"
  mass2 = TranslationalComponents.Components.Mass(L = 1, m = 1, s = initial -0.5, v = initial 0) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 400, "y1": 620, "x2": 500, "y2": 720, "rot": 0}
      },
      "tags": []
    }
  }
  "Spring for system 2"
  spring2 = TranslationalComponents.Components.Spring(s_rel0 = 1, c = 10000) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 620, "y1": 770, "x2": 720, "y2": 870, "rot": 0}
      },
      "tags": []
    }
  }
  "Damper for system 2"
  damper1 = TranslationalComponents.Components.Damper(d = 10) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 610, "y1": 620, "x2": 710, "y2": 720, "rot": 0}
      },
      "tags": []
    }
  }
  "Wall for system 2"
  fixed2 = TranslationalComponents.Components.Fixed(s0 = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 830, "y1": 770, "x2": 930, "y2": 870, "rot": 0}
      },
      "tags": []
    }
  }
relations
  # System 1: force → mass → spring → fixed
  connect(sine1.y, force1.f) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 260, "y": 80}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(force1.flange_b, mass1.flange_a) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(force1.flange_a, force1_ground.flange) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 122.5, "y": 260}, {"x": 122.5, "y": 340}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(mass1.flange_b, spring1.flange_a) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(spring1.flange_b, fixed1.flange) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 880, "y": 260}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  # System 2: force → mass → [spring ‖ damper] → fixed
  connect(sine2.y, force2.f) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 270, "y": 550}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(force2.flange_b, mass2.flange_a) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(force2.flange_a, force2_ground.flange) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 120, "y": 670}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(mass2.flange_b, spring2.flange_a) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [], "E": -1},
        {"S": -1, "M": [{"x": 560, "y": 670}, {"x": 560, "y": 820}], "E": 2}
      ],
      "junctions": [{"x": 610, "y": 670}],
      "renderStyle": "standard"
    }
  }
  connect(spring2.flange_b, fixed2.flange) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [{"x": 750, "y": 820}], "E": -1},
        {"S": -1, "M": [{"x": 880, "y": 670}], "E": 2}
      ],
      "junctions": [{"x": 750, "y": 670}],
      "renderStyle": "standard"
    }
  }
  connect(mass2.flange_b, damper1.flange_a) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(damper1.flange_b, fixed2.flange) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 880, "y": 670}], "E": 2}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "icons": {"default": "dyad://TranslationalComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 1,
        "atol": {
          "damper1.s_rel": 0.001,
          "damper1.v_rel": 0.001,
          "mass1.s": 0.001,
          "mass1.v": 0.01
        },
        "expect": {
          "final": {
            "damper1.s_rel": 1.00086,
            "damper1.v_rel": 0.0502,
            "mass1.s": -0.50433,
            "mass1.v": -0.25276
          },
          "signals": ["damper1.s_rel", "damper1.v_rel", "mass1.s", "mass1.v"]
        }
      }
    }
  }
}
end
Flattened Source
dyad
"""
Oscillator demonstrates the use of initial conditions.

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

A spring–mass system is a mechanical oscillator. The resonant frequency is
ω_res = sqrt(c / m). For c=10000 N/m and m=1 kg, f_res = ω_res/(2π) ≈ 15.9155 Hz.

**System 1 (top, undamped):** A sinusoidal force at the resonant frequency
drives mass1 through a spring to a wall. Without damping, the amplitude grows
without bound (resonance).

**System 2 (bottom, damped):** Same as system 1 but with a damper in parallel
with the spring. Damping bounds the amplitude.

Note: MSL uses a one-flange Force source. Here we use the two-flange Force
with flange_b connected to a Fixed ground, which is equivalent to a grounded
one-flange Force.
"""
example component Oscillator
  "Sinusoidal force signal at resonance frequency"
  sine1 = BlockComponents.Sources.Sine(amplitude = 1, frequency = 15.9155) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 80, "y1": 30, "x2": 180, "y2": 130, "rot": 0}
      },
      "tags": []
    }
  }
  "Force source for system 1"
  force1 = TranslationalComponents.Sources.Force() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 210, "y1": 210, "x2": 310, "y2": 310, "rot": 0}
      },
      "tags": []
    }
  }
  "Ground for force source 1"
  force1_ground = TranslationalComponents.Components.Fixed() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 90, "y1": 440, "x2": 190, "y2": 340, "rot": 180}
      },
      "tags": []
    }
  }
  "Mass for system 1 (undamped)"
  mass1 = TranslationalComponents.Components.Mass(L = 1, m = 1, s = initial -0.5, v = initial 0) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 400, "y1": 210, "x2": 500, "y2": 310, "rot": 0}
      },
      "tags": []
    }
  }
  "Spring for system 1"
  spring1 = TranslationalComponents.Components.Spring(s_rel0 = 1, c = 10000) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 640, "y1": 210, "x2": 740, "y2": 310, "rot": 0}
      },
      "tags": []
    }
  }
  "Wall for system 1"
  fixed1 = TranslationalComponents.Components.Fixed(s0 = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 830, "y1": 340, "x2": 930, "y2": 440, "rot": 0}
      },
      "tags": []
    }
  }
  "Sinusoidal force signal at resonance frequency"
  sine2 = BlockComponents.Sources.Sine(amplitude = 1, frequency = 15.9155) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 80, "y1": 500, "x2": 180, "y2": 600, "rot": 0}
      },
      "tags": []
    }
  }
  "Force source for system 2"
  force2 = TranslationalComponents.Sources.Force() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 220, "y1": 620, "x2": 320, "y2": 720, "rot": 0}
      },
      "tags": []
    }
  }
  "Ground for force source 2"
  force2_ground = TranslationalComponents.Components.Fixed() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 70, "y1": 770, "x2": 170, "y2": 870, "rot": 0}
      },
      "tags": []
    }
  }
  "Mass for system 2 (damped)"
  mass2 = TranslationalComponents.Components.Mass(L = 1, m = 1, s = initial -0.5, v = initial 0) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 400, "y1": 620, "x2": 500, "y2": 720, "rot": 0}
      },
      "tags": []
    }
  }
  "Spring for system 2"
  spring2 = TranslationalComponents.Components.Spring(s_rel0 = 1, c = 10000) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 620, "y1": 770, "x2": 720, "y2": 870, "rot": 0}
      },
      "tags": []
    }
  }
  "Damper for system 2"
  damper1 = TranslationalComponents.Components.Damper(d = 10) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 610, "y1": 620, "x2": 710, "y2": 720, "rot": 0}
      },
      "tags": []
    }
  }
  "Wall for system 2"
  fixed2 = TranslationalComponents.Components.Fixed(s0 = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 830, "y1": 770, "x2": 930, "y2": 870, "rot": 0}
      },
      "tags": []
    }
  }
relations
  # System 1: force → mass → spring → fixed
  connect(sine1.y, force1.f) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 260, "y": 80}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(force1.flange_b, mass1.flange_a) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(force1.flange_a, force1_ground.flange) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 122.5, "y": 260}, {"x": 122.5, "y": 340}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(mass1.flange_b, spring1.flange_a) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(spring1.flange_b, fixed1.flange) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 880, "y": 260}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  # System 2: force → mass → [spring ‖ damper] → fixed
  connect(sine2.y, force2.f) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 270, "y": 550}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(force2.flange_b, mass2.flange_a) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(force2.flange_a, force2_ground.flange) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 120, "y": 670}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(mass2.flange_b, spring2.flange_a) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [], "E": -1},
        {"S": -1, "M": [{"x": 560, "y": 670}, {"x": 560, "y": 820}], "E": 2}
      ],
      "junctions": [{"x": 610, "y": 670}],
      "renderStyle": "standard"
    }
  }
  connect(spring2.flange_b, fixed2.flange) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [{"x": 750, "y": 820}], "E": -1},
        {"S": -1, "M": [{"x": 880, "y": 670}], "E": 2}
      ],
      "junctions": [{"x": 750, "y": 670}],
      "renderStyle": "standard"
    }
  }
  connect(mass2.flange_b, damper1.flange_a) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(damper1.flange_b, fixed2.flange) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 880, "y": 670}], "E": 2}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "icons": {"default": "dyad://TranslationalComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 1,
        "atol": {
          "damper1.s_rel": 0.001,
          "damper1.v_rel": 0.001,
          "mass1.s": 0.001,
          "mass1.v": 0.01
        },
        "expect": {
          "final": {
            "damper1.s_rel": 1.00086,
            "damper1.v_rel": 0.0502,
            "mass1.s": -0.50433,
            "mass1.v": -0.25276
          },
          "signals": ["damper1.s_rel", "damper1.v_rel", "mass1.s", "mass1.v"]
        }
      }
    }
  }
}
end


Test Cases

Test Case case1

julia
plt

julia
plt

julia
plt

julia
plt

  • Examples

  • Experiments

  • Analyses