3

I am building a computer simulation of an N Body Problem for my science fair project. I want the simulation/code to playback when it finishes so that it can be constantly viewed by other people that walk by. I am using VPython which has worked fine for making the simulation but I can't figure out a way to make all the objects go back to the initial position when restarting the whole code. My loop is using:

finished = False
while not finished:
    rate(100)

then when certain things happen I use if statements to get the next part of the simulation going, ex: more stars

if time >= 4.5:  #Millions of Years
   F = G*(m1*m2)*r/r**3

does any body know how to make the whole code repeat its self? the best I could come up with is putting all the objects in the loop and then using continue, but I don't fully know how to use continue and break.

talonmies
  • 70,661
  • 34
  • 192
  • 269
AstroPy
  • 63
  • 7

4 Answers4

1

Your question isn't entirely clear to me, but if you're asking about how break and continue work, this might help clear it up a little:

while True:
    # do_some_stuff
    if calculation_finished:
        break     # exit from the innermost loop
    elif iteration_finished:
        continue  # abort the current iteration and go to the start of the loop
    else:
        # do_some_more_stuff
# This is where we land after the break statement.
# If there's nothing here, the program will exit.
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
1

You can use one of the os.exec* methods to re-execute your program from scratch if you really need to, however what you want to do is probably better accomplished by refactoring your code such that you can reset all parameters to some initially specified constants. It's hard to say exactly how to refactor it without seeing more of your code.

mVChr
  • 49,587
  • 11
  • 107
  • 104
  • Thanks for the help, I have finally finished with my N-Body code, what I have done is implement the methods with if and else statements so that while the position of a star that i chose is less than a specific position that it doesn't reach until when i want it to reset, than as an else statement all the stars pos's are put back to origional pos's meaning that the if statement is re-entered and they repeat again. – AstroPy Jun 15 '13 at 02:52
1

Your question is unclear. Still, I believe that your issue is that the trajectory of an N body problem is chaotic for N >= 3. And you want to repeat the same trajectory or invert time and go backwards again. If this is your issue, there is no way other than only simulating the trajectory once (using your whatever method). While simulating, you display and record it. After this simulation run, you can do whatever you want with your recorded trajectory (play it again, play it backwards, ...)

Dr. Jan-Philip Gehrcke
  • 33,287
  • 14
  • 85
  • 130
  • this is what I ended up doing, but now I am building my own software that will record the data into a file and then the file gets opened in the program meaning that it can then go back and forth depending on the line that it reads. Still building this but it is getting there – AstroPy Dec 12 '13 at 15:49
0

EX:

restPos = (x,y,z)

while True:
    if Star1.pos = resetPos:
        #reset star positions, EX below
        star1.pos = (0,0,0)
    else:
        F = G*m1*m2*r/r**3
        Star1.pos += F*dt #or however i did this, haven't used the code in a bit
AstroPy
  • 63
  • 7
  • thanks this is what i ended up doing to reset it, and I will continue to use this method but i found recording the video could also make life easier – AstroPy Dec 12 '13 at 15:51