|
Spring-Mass-Damper System Simulation
This note provides an interactive simulation of a coupled spring-mass-damper system. Multiple masses are connected in series between two fixed walls, demonstrating the physics of coupled oscillators and normal modes.
The simulation allows you to configure the number of masses, spring constants, damping coefficients, and initial conditions. You can observe the system's behavior through real-time animations and plots showing position, velocity, and acceleration over time. This teaches the engineering principles behind mechanical vibrations and coupled systems.
NOTE : Refer to this note for the theory of this system
Math behind the Simulation
1. System Configuration
The simulation models an N-degree of freedom spring-mass-damper system:
-
N Masses: The system consists of N masses (m1, m2, ..., mN) connected in series.
-
N+1 Springs: For N masses, there are N+1 springs. The first spring connects the left wall to the first mass, springs 2 through N connect adjacent masses, and the last spring connects the last mass to the right wall (if fixed).
-
Boundary Conditions: The left wall is always fixed. The right wall can be either Fixed (default) or Open Ended (free end). When open-ended, the last spring is not attached to a wall, and the last mass experiences no force from the right side.
-
Damping: Each mass has a damping coefficient (c1, c2, ..., cN) that creates a velocity-proportional damping force.
2. Equations of Motion
The system is governed by coupled ordinary differential equations derived from Newton's second law (F = ma):
-
For each mass i: mi · ai = Fi, where Fi is the net force on mass i.
-
Spring Forces (Hooke's Law): Fspring = -k · Δx, where k is the spring constant and Δx is the displacement from equilibrium.
-
Damping Force: Fdamping = -c · v, where c is the damping coefficient and v is the velocity.
-
For the first mass (i=0): Connected to left wall via spring k0 and to second mass via spring k1:
- F0 = -k0(x0 - xwall_left - xeq) - k1(x0 - x1 - xeq) - c0v0
-
For middle masses (i=1 to N-2): Connected to adjacent masses:
- Fi = -ki(xi - xi-1 - xeq) - ki+1(xi - xi+1 - xeq) - civi
-
For the last mass (i=N-1):
- Fixed right wall: FN-1 = -kN-1(xN-1 - xN-2 - xeq) - kN(xN-1 - xwall_right + xeq) - cN-1vN-1
- Open-ended: FN-1 = -kN-1(xN-1 - xN-2 - xeq) - cN-1vN-1 (no right spring force)
-
State-Space Representation: The system can be written as:
- dxi/dt = vi (velocity)
- dvi/dt = Fi/mi (acceleration)
3. Numerical Integration (Runge-Kutta 4th Order)
The coupled ODEs are solved numerically using the Runge-Kutta 4th order method (RK4):
-
Time Step (dt): The simulation advances in discrete time steps. Smaller dt values provide higher accuracy but require more computation.
-
RK4 Algorithm: For each time step, RK4 calculates four intermediate derivatives (k1, k2, k3, k4) and combines them:
- k1 = f(t, y)
- k2 = f(t + dt/2, y + dt·k1/2)
- k3 = f(t + dt/2, y + dt·k2/2)
- k4 = f(t + dt, y + dt·k3)
- y(t + dt) = y(t) + (dt/6)(k1 + 2k2 + 2k3 + k4)
-
Accuracy: RK4 is a 4th-order method, meaning the local truncation error is O(dt5), providing excellent accuracy for smooth systems.
4. Equilibrium Positions
The equilibrium positions are calculated based on the boundary conditions:
-
Fixed right wall: Masses are evenly spaced between the two walls: xeq,i = xwall_left + (i+1) · L/(N+1), where L is the distance between walls.
-
Open-ended: Masses are evenly spaced from the left wall: xeq,i = xwall_left + (i+1) · L/(N+1) (same formula, but the last spring is not attached to a wall).
-
Displacement: The plots show displacement from equilibrium: δi = xi - xeq,i, which is more physically meaningful than absolute position.
Usage Example
Follow these steps to explore the Spring-Mass-Damper system simulation:
-
Configure the System: Use the "Number of Masses" input to set how many masses you want (1-5). Click "Apply Config" to update the system. The default is 2 masses with 3 springs.
-
Set Initial Conditions: You can adjust initial positions in two ways:
- Mouse Drag: Click and drag any mass in the physical animation to set its initial position. The mass will snap to your mouse position as you drag.
- Random Button: Click "Random" to randomly initialize all mass positions within the walls, ensuring minimum separation between masses.
-
Adjust Parameters: Use the sliders to modify system parameters:
- Time Step (dt): Controls the numerical integration step size (0.001 to 0.05). Smaller values are more accurate but slower. Default: 0.01.
- Damping: Controls the damping coefficient for all masses (0.0 to 2.0). Higher values create more energy dissipation. Default: 0.1.
- Spring k: Controls the spring constant for all springs (1.0 to 50.0). Higher values create stiffer springs and faster oscillations. Default: 10.0.
-
Set Boundary Condition: Use the "Right Wall" toggle to switch between:
- Fixed: The right wall is fixed, and the last spring connects the last mass to the wall. This creates a symmetric boundary condition.
- Open Ended: The right wall is removed, and the last mass experiences no force from the right side. This creates a free-end boundary condition, allowing the system to move freely.
-
Start the Simulation: Click "Start" to begin the simulation. The physical animation and all plots will update in real-time. Click "Start" again (now showing "Pause") to pause/resume.
-
Observe the Physical Animation:
- Watch the masses oscillate between the walls (or extend beyond if open-ended).
- Notice how the springs stretch and compress as masses move.
- Each mass is colored to match its corresponding plot line (blue, green, orange, red, purple).
- The current simulation time is displayed in the top-left corner.
-
Analyze Time Series Plots: Three plots show how the system evolves over time:
- Displacement vs Time: Shows how each mass's displacement from equilibrium changes over time. Notice the oscillatory behavior and how different masses may have different phases.
- Velocity vs Time: Shows the velocity of each mass. Velocity is maximum when displacement is zero (passing through equilibrium).
- Acceleration vs Time: Shows the acceleration of each mass. Acceleration is maximum when displacement is maximum (at the turning points).
-
Explore State Space Plots: Three plots show relationships between state variables:
- Displacement vs Velocity: Creates phase portraits showing the trajectory in state space. Closed loops indicate periodic motion.
- Displacement vs Acceleration: Shows the relationship between position and acceleration. For simple harmonic motion, this should be a straight line (F = -kx).
- Velocity vs Acceleration: Shows how velocity and acceleration are related. The shape depends on the system's dynamics.
-
Reset and Experiment: Click "Reset" to return to initial conditions, or modify parameters while the simulation is running to see real-time effects.
Tips:
-
Normal Modes: Try setting initial conditions where all masses start at the same displacement. You'll see synchronized motion (first normal mode). For 2 masses, try displacing them in opposite directions to see the second normal mode.
-
Damping Effect: Increase the damping slider and observe how oscillations decay faster. With very high damping, the system approaches equilibrium without oscillating (overdamped).
-
Spring Stiffness: Increase the spring constant to see faster oscillations (higher natural frequency). Decrease it to see slower, more gentle motion.
-
Open-Ended vs Fixed: Compare the behavior with fixed vs open-ended right wall. The open-ended case allows the system to translate as a whole, while the fixed case constrains all motion.
-
Multiple Masses: Try 3 or 4 masses to see more complex coupled oscillations. Notice how energy transfers between masses through the connecting springs.
-
Time Step Accuracy: If you see numerical instability (wild oscillations or divergence), reduce the time step. For smooth, accurate results, use dt ≤ 0.01.
-
Plot Scrolling: The time series plots show a rolling window of the last 10 seconds. New data appears on the right, and old data scrolls off the left, making it easy to observe long-term behavior.
Visualizations
The simulation provides multiple synchronized real-time visualizations that update as the simulation runs:
-
1. Physical System Animation: Shows a top-down view of the spring-mass-damper system with:
- Fixed Walls: Dark gray rectangles on the left (always) and right (if fixed boundary condition).
- Springs: Green zig-zag lines connecting masses to walls and to each other. Springs stretch and compress as masses move.
- Masses: Colored rectangles representing each mass. Colors match the corresponding plot lines: Mass 1 = Blue, Mass 2 = Green, Mass 3 = Orange, Mass 4 = Red, Mass 5 = Purple.
- Time Display: Current simulation time shown in the top-left corner.
The animation updates in real-time, showing the dynamic behavior of the coupled system. You can drag masses to adjust their initial positions before starting the simulation.
-
2. Time Series Plots: Three plots showing how state variables evolve over time:
- Displacement vs Time: Shows the displacement of each mass from its equilibrium position over time. The plot uses a rolling window of the last 10 seconds, with new data appearing on the right. This reveals oscillatory behavior, phase relationships, and damping effects.
- Velocity vs Time: Shows the velocity of each mass over time. Velocity is the time derivative of displacement. Notice how velocity is maximum when displacement is zero (passing through equilibrium).
- Acceleration vs Time: Shows the acceleration of each mass over time. Acceleration is the time derivative of velocity. For harmonic motion, acceleration is maximum when displacement is maximum (at turning points).
All time series plots use the same color scheme as the masses and share the same time axis (last 10 seconds). Each plot has a center-aligned title and shows grid lines for easy reading.
-
3. State Space Plots: Three plots showing relationships between different state variables:
- Displacement vs Velocity: Creates phase portraits showing the trajectory in state space. For periodic motion, this creates closed loops (limit cycles). The shape and size of the loops reveal information about the system's energy and damping.
- Displacement vs Acceleration: Shows the relationship between position and acceleration. For simple harmonic motion with a single spring, this should be a straight line (F = -kx, so a = -kx/m). For coupled systems, the relationship is more complex.
- Velocity vs Acceleration: Shows how velocity and acceleration are related. This plot helps visualize the system's dynamics and energy flow.
State space plots also use a rolling window and share the same color scheme. They help visualize the system's behavior in a different coordinate system, revealing patterns that may not be obvious in time series.
Controls
-
Number of Masses: Input field to set the number of masses (1-5). The default is 2. Click "Apply Config" to reconfigure the system with the new number of masses. When you change the number of masses, the system is reset with default parameters (evenly spaced masses, default spring constants and damping).
-
Apply Config Button: Applies the new number of masses configuration. The system is reinitialized with default parameters, and all masses are positioned evenly between the walls.
-
Random Button: Randomly initializes the position of each mass within the walls, ensuring minimum separation between masses. This is useful for exploring different initial conditions and observing how the system evolves from non-equilibrium states.
-
Reset Button: Resets the simulation to the initial conditions (current positions and zero velocities). The simulation time is reset to zero, and all plot histories are cleared.
-
Time Step (dt) Slider: Controls the numerical integration step size from 0.001 to 0.05 seconds. Smaller values provide higher accuracy but require more computation. Default: 0.01. The current value is displayed next to the slider.
-
Damping Slider: Controls the damping coefficient for all masses from 0.0 to 2.0. Higher values create more energy dissipation, causing oscillations to decay faster. Default: 0.1. The current value is displayed next to the slider.
-
Spring k Slider: Controls the spring constant for all springs from 1.0 to 50.0. Higher values create stiffer springs, resulting in faster oscillations (higher natural frequency). Default: 10.0. The current value is displayed next to the slider.
-
Right Wall Toggle: Checkbox to switch between boundary conditions:
- Fixed (checked): The right wall is fixed, and the last spring connects the last mass to the wall. This creates a symmetric boundary condition where both ends are constrained.
- Open Ended (unchecked): The right wall is removed, and the last mass experiences no force from the right side. This creates a free-end boundary condition, allowing the system to translate as a whole. The differential equation for the last mass changes accordingly (no right spring force term).
The toggle label shows "Fixed" or "Open Ended" based on the current state. Changing this setting resets the simulation.
-
Start/Pause Button: Starts or pauses the simulation. When running, the button shows "Pause"; when stopped, it shows "Start". Clicking while running pauses the simulation, and clicking while paused resumes from the current state.
-
Status Display: Shows the current simulation state and time (e.g., "Running - t = 5.23s" or "Paused - t = 3.45s").
-
Mouse Drag (Physical Animation): Click and drag any mass in the physical animation to adjust its initial position. This allows you to set custom initial conditions before starting the simulation. The mass will follow your mouse cursor as you drag. When you release the mouse, the position is set and velocities are reset to zero.
System Parameters
The simulation uses the following default parameters, which can be adjusted via the control panel:
-
Number of Masses (N): 1 to 5 (default: 2). Determines the number of degrees of freedom in the system.
-
Number of Springs: N+1 (one more than the number of masses). The first spring connects the left wall to the first mass, springs 2 through N connect adjacent masses, and the last spring connects the last mass to the right wall (if fixed).
-
Mass Values: All masses default to 1.0 kg. Currently, all masses have the same value, but the system architecture supports different masses per mass.
-
Spring Constants: All springs default to 10.0 N/m. The slider adjusts all springs simultaneously. The system architecture supports different spring constants per spring.
-
Damping Coefficients: All masses default to 0.1 N·s/m. The slider adjusts all damping coefficients simultaneously. The system architecture supports different damping per mass.
-
Wall Positions: Left wall at x = 0.0 m, right wall at x = 10.0 m. The distance between walls is fixed at 10.0 m.
-
Initial Conditions: By default, masses are positioned at their equilibrium positions with zero initial velocity. You can modify initial positions by dragging masses or using the Random button.
-
Time Step (dt): Default 0.01 seconds. This is the step size used in the Runge-Kutta 4th order numerical integration.
|
|