Python Forum
Using an Array in a While Loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using an Array in a While Loop
#21
Haha, alright I'll keep messing with it. Thanks for all the help so far! I'll post an update if I get anywhere with it.
Reply
#22
I would add some print statements, e.g start with F and Fnet to see their values in every iteration, positions of the objects etc.
Reply
#23
Got it working with a little help on the math from my professor! The two main changes were making j conditional so that the calculation runs only when j != i, and also i needed a sign change in front of the F calculation because subtracting vectors is weird.

from visual import *

myuniv = display(range=vector(7*10**12,7*10**12,7*10**12))

G = 6.674*10**-11  #Gravitational constant

#Masses of each solar body in order starting with the Sun
mass = [1.989*10**30,3.285*10**23, 4.867*10**24, 
           5.972*10**24, 6.39*10**23, 1.898*10**27,
           5.68*10**26, 8.68*10**25, 1.024*10**26]

#Positions of each solar body starting with the Sun           
posit = [vector(0,0,0),vector(5.791*10**10,0,0),vector(1.082*10**11,0,0),
          vector(1.496*10**11,0,0),vector(2.279*10**11,0,0),vector(7.785*10**11,0,0),
          vector(1.429*10**12,0,0),vector(2.871*10**12,0,0),vector(4.498*10**12,0,0)]

#Velocities of each solar body starting with the Sun
vel = [vector(0,0,0),vector(0,4.87*10**4,0),vector(0,3.502*10**4,0),
          vector(0,2.98*10**4,0),vector(0,2.401*10**4,0),vector(0,1.307*10**4,0),
          vector(0,9.69*10**3,0),vector(0,6.81*10**3,0),vector(0,5.43*10**3,0)]

#Acceleration of each solar body starting with the Sun
acc = [vector(0,0,0),vector(0,0,0),vector(0,0,0),
          vector(0,0,0),vector(0,0,0),vector(0,0,0),
          vector(0,0,0),vector(0,0,0),vector(0,0,0)]

Ssun = vector(0,0,0)
Msun = 1.989*10**30

t = 0.0
dt = 150000

sun = sphere(pos=posit[0],radius=9.9*10**10,color=color.yellow,make_trail=True)
mercury = sphere(pos=posit[1],radius=2.44*10**6,color=color.red,make_trail=True)
venus = sphere(pos=posit[2],radius=6.052*10**6,make_trail=True)
earth = sphere(pos=posit[3],radius=6.371*10**6,color=color.green,make_trail=True)
mars = sphere(pos=posit[4],radius=3.39*10**6,color=color.red,make_trail=True)
jupiter = sphere(pos=posit[5],radius=6.99*10**7,color=color.blue,make_trail=True)
saturn = sphere(pos=posit[6],radius=5.823*10**7,color=color.yellow,make_trail=True)
uranus = sphere(pos=posit[7],radius=2.536*10**7,color=color.cyan,make_trail=True)
neptune = sphere(pos=posit[8],radius=2.462*10**7,make_trail=True)

while True:
    for i in range(0,9,1):
        
        Fnet = vector(0,0,0)
        for j in range(0,9,1):
            if j != i:
                rate(1000000)
                t=t+dt

                r = posit[i]-posit[j]
                F = -((G*mass[j]*mass[i])/(mag(r)**2))* norm(r) #Calculating the force of gravity from object j over object i
                Fnet = Fnet + F
            
        acc[i]=Fnet/mass[i]
        vel[i]+= acc[i]*dt
        posit[i]+= vel[i]*dt
    
        
        sun.pos=posit[0]
        mercury.pos=posit[1]
        venus.pos=posit[2]
        earth.pos=posit[3]
        mars.pos=posit[4]
        jupiter.pos=posit[5]
        saturn.pos=posit[6]
        uranus.pos=posit[7]
        neptune.pos=posit[8]
Thanks again for the help! Big Grin Big Grin
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020