pythonpython-2.7animationscientific-computingvpython

How to stop overlapping of boxes in animation of n coupled masses in visual python?


This question can be more a physics-problem than a vpython one,because i dont know whether the code or the physics went wrong.I am trying to write a program on animation of n coupled spring mass system using visual module of python. I cant see,where it went wrong,but the masses(even when no.of masses is as less as two),often overlap,How can i change the boundary conditions,or other things so that they dont overlap(as in reality)? The code is:

from visual import *
from math import cos

m=[1,2]#list of masses
k=[1,2,1]#list of spring constants
R=len(m)#number of masses

floor=box(pos=(0,-0.5,0),size=(10,0.01,10),color=color.blue,material=materials.wood)#resting table
boxlist=[]#for creation of the masses
for z in range(0,R):
    boxlist.append(box(pos=(4*z,0,0),size=(0.5,0.5,0.5),color=color.red,material=materials.wood))
#a is the matrix whose eigenvalues are the square of angular frequencies,and eigenvectors are the amplitudes
a=[[0 for r in range(R)]for c in range(R)]
for i in range(0,R):
    if i>0:
        a[i][i-1]=-k[i]/float(m[i])
    if i<R-1:
        a[i][i+1]=-k[i+1]/float(m[i])
    a[i][i]=k[i]/float(m[i])+k[i+1]/float(m[i])

val=eig(a)[0]
vec=eig(a)[1]

t=0
dt=0.01
s=0
S=[]
while True:
    rate(100)
    for i in range(0,R):
        for j in range(0,R):
            s+=vec[j][i]*cos(((val[j])**0.5)*t)#x=Acos(wt),allowing phase angle to be zero
        S.append(s)
        s=0
    for z in range(0,R):
        boxlist[z].pos.x=S[z]
    S=[]
    t+=dt

Solution

  • You have provided a program that won't run, since "eig" is undefined. That makes it impossible to explore your problem.

    Here is an example of a program with many springs and masses: https://www.glowscript.org/#/user/GlowScriptDemos/folder/Examples/program/AtomicSolid-VPython

    Click "View this program" to see the code.

    I note that you are using a VERY old version of VPython. See vpython.org for information on how to use a current version.