Search code examples
pythonphysicsvpython

Two masses connected by a spring...trying to simulate flight but having difficulties


We have two masses of equal mass 0.1kg connected by a stiff spring with k = 10^5 N/m. The rest length of the spring (and the initial distance between the two masses) is d0 = 0.15m

Mass 1 has initial velocity (2.77, 1.25, 0) m/s while mass 2 has initial velocity (1.25, 4, 0) m/s.

I want to write a code that simulates the flight of this system under gravity while ignoring air resistance. I also want to place a third sphere located at the center of mass of the system.

Here's what I've written so far:

from visual import *

mass1 = sphere(radius = 0.01)
mass2 = sphere(pos = (0.15, 0, 0), radius = 0.01)
COM = sphere(pos = (0.15/2, 0, 0), radius = 0.01)

mass1.m = 0.1
mass2.m = 0.1

k = 1*10**5

mass1.v = vector(2.77, 1.25, 0)
mass2.v = vector(1.25, 4, 0)

mass1.p = mass1.v*mass1.m
mass2.p = mass2.v*mass2.m

dt = 0.0001
t = 0

while 1:
    g = 9.8
    d = (mass1.pos-mass2.pos)/mag(mass1.pos-mass2.pos)

    Ft12 = mass1.m*d*k*0.15
    Ft21 = mass2.m*d*k*0.15

    Fnet = Ft12 + Ft21

    mass1.p += Fnet*dt
    mass2.p += Fnet*dt

    mass1.vA = 0.5*(mass1.v + (mass1.p/mass1.m))
    mass2.vA = 0.5*(mass1.v + (mass2.p/mass2.m))

    mass1.pos += mass1.vA*dt
    mass2.pos += mass2.vA*dt
    COM.pos = (mass1.pos-mass2.pos)/2 + mass1.pos

    t += dt

Ft12 is the force of tension from ball 1 to ball 2, and Ft21 is from 2 to 1. Of course, I wouldn't be posting this if I were getting accurate results, but I'm not even getting results that I can readily observe. The screen just goes blank very quickly and I have no chance to see what's happening. I basically want to this system to be "thrown" under the effects of gravity, in which you can imagine that the COM's trajectory will be that of a parabola. Any help will be greatly appreciated. If I'm getting the forces wrong, or I need to add some in some places, please let me know!

Thank you, everyone!


Solution

  • I have grave doubts about this line:

    dt += 1
    

    I guess dt is the time step. You are incrementing the time step by a second each time around the loop. Normally you would increment time:

    t += dt
    

    but you don't have a time variable yet. Once your time step gets large, all hell will break loose. For now I would simply delete the dt += 1 line.

    As for the rest of the code, I've not looked at it in any detail and I can't guess what your notation signifies. There may well be more problems in the code.