Simulation of 2 Spring and 1 Mass System: VPython Tutorial 5 (Visual Python)

Simulation of Two Spring and one Mass System.
Simulation of Two Spring and one Mass System. (Source: Tech For Curious)

This VPython tutorial shows the simulation of two Spring and one Mass system. The generalised Spring-Mass system can be understood efficiently by exploring a single mass attached to two springs. In this system, we have two springs, one end of each spring is attached to the rigid support whereas another end is attached to the mass. The motion of mass is influenced by the forces of both springs.

The figure below shows that the position of left spring with respect to the left wall and right spring with respect to the right wall.

(1)   \begin{equation*} \vec{r}_L = \vec{r}_\text{Mass} - \vec{r}_\text{Left Wall} \end{equation*}

(2)   \begin{equation*} \vec{r}_R = \vec{r}_\text{Mass} - \vec{r}_\text{Right Wall} \end{equation*}

One Mass two Spring System

The spring constant of left and right spring are k_L and k_R respectively.  The rest length of left and right spring are l_L and l_R. The total force experience by the mass is

(3)   \begin{equation*} \vec{F}_\text{total} = \vec{F}_{L} + \vec{F}_{R}, \end{equation*}

where,

(4)   \begin{equation*}  \vec{F}_L = -k_L (|\vec{r}_L| - l_L) \hat{r}_L \end{equation*}

(5)   \begin{equation*}  \vec{F}_R = -k_R (|\vec{r}_R| - l_R) \hat{r}_R. \end{equation*}

Here, |\vec{r}_R| and \hat{r}_R are the magnitude and unit vector of \vec{r}_R.

After we have calculated the force, we need to find acceleration of mass (\vec{a}(t)) and then update the velocity  (\vec{v}(t)) and position (\vec{r}(t)) after time interval dt.

(6)   \begin{equation*} \vec{a}(t) = \frac{\vec{F}_{\text{total}}}{M} \end{equation*}

(7)   \begin{equation*}  \vec{v}(t+dt) = \vec{v}(t) + \vec{a}(t)dt \end{equation*}

(8)   \begin{equation*} \vec{r}(t+dt) = \vec{r}(t) + \vec{v}(t)dt \end{equation*}

We have understood the concept of this system of two spring and one mass. Now, we will write the code for simulation of this system in VPython (Visual Python). This code can be run on VPython installed on your system or online on glowscript.org.

First we will define geometry of walls, spring and ball as following:

from vpython import*
length = 10.
scene2 = canvas(title='One Mass with two Spring System', width=400, height=200, center=vector(0,0,0), background=color.white) 
ball=sphere(pos=vector(2,0,0),velocity=vector(0,0,0),radius=2.5,mass=1, color=color.blue)
wall_right=box(pos=vector(length,0,0), size= vector(0.2, 5, 5), color = color.green)
wall_left=box(pos=vector(-length,0,0), size= vector(0.2, 5, 5), color = color.green)
spring_right=helix(pos=wall_right.pos,axis=ball.pos - wall_right.pos,constant=1,coils = 10,thickness=0.2,radius=1, color = color.red)
spring_left=helix(pos=wall_left.pos,axis=ball.pos-wall_left.pos,constant=1,coils = 10,thickness=0.2,radius=1, color = color.red)

Run this code

Edit this code

In the first line, I have use vpython module instead of visual module because currently I am using vpython 7 which can be installed using pip as shown in previous tutorial. The variable “length” denotes the rest length of our spring which I taken same for both the spring. The position of ball of radius 2.5 unit is taken as (2,0,0) where the mass of ball is 1 unit. The right wall is placed at the position (10,0,0) which is actually equal to rest length of the spring. Similarly, the left wall is placed at (-10,0,0). The size of walls must be written is the form of vector.  The thickness of wall is chosen as 0.2 unit in the x direction which is small than the other sides (5 unit) in y and z direction.

one_mass_two_spring_system
Geometry of one mass two spring system. (Source: TechForCurious)

The position of spring is chosen as the position of wall and the axis of spring is the difference between position of ball and wall. The attribute “constant” is representing the spring constant which is taken as 1 unit for both the spring for simplicity. You can choose separate value of spring constants for both the springs if you want to study the effect of spring constant. The other attributes of spring like number of coils, thickness and radius is chosen arbitrary so as to look nice in the simulation, these attributes has no effect on the motion of ball.

After running the above code, you will see the whole geometry in the simulation but nothing will move because we have not applied equaiton of motion. In this simulation, I have defined the equation of acceleration as a separate function.

def acc(): 
 dr_right = ball.pos - wall_right.pos
 force_right = -spring_right.constant*(mag(dr_right) - length)*norm(dr_right)
 dr_left = ball.pos - wall_left.pos
 force_left = -spring_left.constant*(mag(dr_left) - length)*norm(dr_left)
 return (force_right+force_left)/ball.mass

This acc() function is written to calculate the acceleration of ball caused by both the springs. Whenever, you call this acc() function, first it will calculate the relative position of ball with respect to the position of wall. Now, the force due to single spring will be calculated from this relative position as shown in Eq. 4 and 5. The funciton mag(\vec{A}) calculates the magnitude of vector \vec{A} whereas norm(\vec{A}) gives the unit vector \hat{A}. Finally, acc() function will add the forces due to left and right spring and return the value of acceleration causes by both the springs. Next, we will update the position of ball in loop with the final code shown below.

from vpython import*
length = 10.
scene2 = canvas(title='One Mass with two Spring System', width=400, height=200, center=vector(0,0,0), background=color.white) 
ball=sphere(pos=vector(2,0,0),velocity=vector(0,0,0),radius=2.5,mass=1, color=color.blue)
wall_right=box(pos=vector(length,0,0), size= vector(0.2, 5, 5), color = color.green)
wall_left=box(pos=vector(-length,0,0), size= vector(0.2, 5, 5), color = color.green)
spring_right=helix(pos=wall_right.pos,axis=ball.pos - wall_right.pos,constant=1,coils = 10,thickness=0.2,radius=1, color = color.red)
spring_left=helix(pos=wall_left.pos,axis=ball.pos-wall_left.pos,constant=1,coils = 10,thickness=0.2,radius=1, color = color.red)
def acc(): 
 dr_right = ball.pos - wall_right.pos
 force_right = -spring_right.constant*(mag(dr_right) - length)*norm(dr_right)
 dr_left = ball.pos - wall_left.pos
 force_left = -spring_left.constant*(mag(dr_left) - length)*norm(dr_left)
 return (force_right+force_left)/ball.mass

ball.trail=curve(color=ball.color)
t=0
dt=0.01
while (t<100):
 rate(500)
 ball.velocity=ball.velocity+acc()*dt
 ball.pos=ball.pos+ball.velocity*dt
 spring_right.axis=ball.pos - wall_right.pos
 spring_left.axis=ball.pos - wall_left.pos
 ball.trail.append(pos=ball.pos)
 t=t+dt

Run this code

Edit this code

Here, the position is update in time step of 0.01 until the t reaches 100. The maximum number of calculation done is 500 per seconds as shown by rate(500). The velocity and position of ball is update according to Eq. 7 and 8 respectively. The axis of spring must also be updated so that the ends of spring attached with ball always moves with ball. Here, I have used an attribute trail which will keep on showing track of ball which is linear when ball moves in 1 dimension but will show pattern for motion in 2 dimensional plane. After running the code you will see the ball moving back and forth in x direction.

Two Spring and One Mass Simulation
Two Spring and One Mass Simulation. (Credit: Tech For Curious)

Now change the position of ball from (2,0,0) to (2,5,0). You will see that the ball is now moving in a plane and shows a beautiful pettern.

ball=sphere(pos=vector(2,5,0),velocity=vector(0,0,0),radius=2.5,mass=1, color=color.blue)

Run this code

Edit this code

2D_motion_ball
2 dimensional motion of ball. (Source: TechForCurious)

You must be thinking that if I change the position of ball to (2,5,-5), I will observe some three dimension pattern. But this is not true, you will still observe the motion in plane just the plane will be tilted.

ball=sphere(pos=vector(2,5,-5),velocity=vector(0,0,0),radius=2.5, mass=1, color=color.blue)

Run this code

Edit this code

This simulation of ball with one mass with two springs is the general simulation and you can add more springs and ball to create a three dimensional network which will represent the structure of crystal. If you have queries please feel free to use comment box. Your suggestions help us to decide future tutorials. If you like this VPython tutorial, please share with someone who is interested in visualizing physics. You can follow us on facebook and twitter. You can subscribe us for Email Notification also to get an email whenever we publish a new post.


Leave a Reply

Your email address will not be published. Required fields are marked *