The spring-mass system is one of the simplest systems in physics. The spring-mass system consists of a spring whose one end is attached to a rigid support and the other end is attached to a movable object. When the spring is stretched or compressed, the spring tries to restore its position which results in oscillation of object around the equilibrium position. The equilibrium position is defined as the position when spring is at its normal length and the object does not experience any force.

In the last post, we learned how to simulate a falling ball. In the case of falling ball, the force is constant and hence the acceleration is also constant (g=-9.8 m/s^2). But it case of the spring-mass system, the force is not constant instead it is proportional to the position of the object with respect to the equilibrium position.

The acceleration (*a(t)*) can be calculated from the position (*x(t)*) at time *t *using the expression:

(1)

where *k* is spring constant and *m* is the mass of the object. The velocity can be calculated as:

(2)

where *dt* is the small time interval. The position can be calculated from velocity as:

(3)

Now, we have learned the concept required to simulate the spring-mass system, we can proceed to learn code in VPython. If you are using VPython first time please go through the previous post. This time, I have installed the latest version of VPython in Linux, so we can use advanced features also. Although this simulation doesn’t require any advance feature.

You need to open VIDLE editor to write the script in a file and then save that as .py file. First of all, you need to import VPython module using the following lines.

from visual import* Mass=box(pos=vector(12,0,0),velocity=vector(0,0,0),size=vector(1,1,1), mass=1.0,color=color.blue

The above code shows two lines: the first line tell the compiler that please include visual python module and the second line creates a box whose name will be Mass. The position of the box is *(12,0,0)* which specifies *12* unit in the x-axis and 0 units in y and z-axis, the velocity is taken as zero in the starting. The size *(1,1,1)* specify that the length along x, y and z-axis should be 1 unit. We have also defined an attribute mass which is taken as *1* unit for simplicity.

Now, you need to save the program and name the file as spring.py (or anything you like with .py extension). You can run the program either through Run>Run Module or by typing f5. Now, you will see the following window pop up. Here, the background is black which I would like to change to white so that we can see the objects clearly. Now, let’s create a spring which is connected to this mass.

from visual import* display(width=600,height=600,center=vector(6,0,0),background=color.white) Mass=box(pos=vector(12,0,0),velocity=vector(0,0,0),size=vector(1,1,1),mass=1.0,color=color.blue) pivot=vector(0,0,0) spring=helix(pos=pivot, axis=Mass.pos-pivot, radius=0.4, constant=1, thickness=0.1, coils=20, color=color.red)

Here, we have changed the display size to *600 x 600*, the center of view to nearly center of system and background to white color. The fourth line defines the pivot which is the center of our coordinate system. The helix defines a spring whose position is the center of our coordinate system and the axis means another end is located according to the position of the mass. The axis specifies the other end of helix, suppose you give pos = (5,5,5) and axis =(1,1,0) then the other end will be present at (5,5,5) + (1,1,0) = (6,6,0). Hence, I have given the axis in terms of the position of mass and pivot. Here, you can always change the pivot position and system will still work normally. The radius of helix rings is 0.4 unit, thickness of the wire is 0.1 unit and number of coils are 20. The constant attribute is helix is the spring constant *k* which I took *1* for simplicity. Now, you will get the output as the image shown on right.

The basic objects are created, now we can calculate the acceleration and update the position of the mass.

from visual import* display(width=600,height=600,center=vector(6,0,0),background=color.white) Mass=box(pos=vector(12,0,0),velocity=vector(0,0,0),size=vector(1,1,1),mass=1.0,color=color.blue) pivot=vector(0,0,0) spring=helix(pos=pivot,axis=Mass.pos-pivot,radius=0.4,constant=1,thickness=0.1,coils=20,color=color.red) eq=vector(9,0,0) t=0 dt=0.01 while (t<50): rate(100) acc=(eq-Mass.pos)*(spring.constant/Mass.mass) Mass.velocity=Mass.velocity+acc*dt Mass.pos=Mass.pos+Mass.velocity*dt spring.axis=Mass.pos-spring.pos t=t+dt

Here, “eq” variable defines the equilibrium position. The “t” and “dt” specify time and small time interval. The position of the mass will be updated again and again inside the loop which will stop when t become greater than 50. The “rate(100)” specifies that the maximum number of calculation should not be more than 100 in one second. I have subtracted the position of mass from the equilibrium position to get *x*.

(4)

Here, I have chosen the equilibrium position different than the position of mass to get some nonzero value of x otherwise force will become zero and system will remain in equilibrium. The acceleration is calculated from x using the Equation 1.

Then, the velocity of the mass is updated using the second equation. This new velocity is used to calculate the updated position of the mass. Then, the spring axis is also updated using updated position of the mass. Remember the axis should be given as difference of both the ends of the spring. The other end is moving with the mass, hence, I have given axis as the difference between the position of the mass and position of spring. At the end, I have updated the time variable. Now, run the code and you will see the mass is moving and spring also compresses and stretches.

The basic model is working according to the physics but I like to add a wall at the one end of spring and a floor. The wall and floor can be made using the box. The size of the box can be adjusted properly to define a wall or floor, for example, the wall has very small x length and the floor has very small z length. You also need to take care that mass or spring should not merge into the floor. I have shifted the floor’s position downward by 0.6 to avoid any merging into spring or mass.

from visual import* display(width=600,height=600,center=vector(6,0,0),background=color.white) wall=box(pos=vector(0,1,0),size=vector(0.2,3,2),color=color.green) floor=box(pos=vector(6,-0.6,0),size=vector(14,0.2,4),color=color.green) Mass=box(pos=vector(12,0,0),velocity=vector(0,0,0),size=vector(1,1,1),mass=1.0,color=color.blue) pivot=vector(0,0,0) spring=helix(pos=pivot,axis=Mass.pos-pivot,radius=0.4,constant=1,thickness=0.1,coils=20,color=color.red) eq=vector(9,0,0) t=0 dt=0.01 while (t<50): rate(100) acc=(eq-Mass.pos)*(spring.constant/Mass.mass) Mass.velocity=Mass.velocity+acc*dt Mass.pos=Mass.pos+Mass.velocity*dt spring.axis=Mass.pos-spring.pos t=t+dt

I have also explained the above program in a video, please watch this video. In this video, I have also changed the various parameter like mass, spring constant and velocity etc. to see their effect on the time period. Please watch this video and comment your views. I hope you liked this interesting tutorial of simulation of the mass-spring system. Please share this post with you friends. 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.