Advertisement
Guest User

Penultimate Euler

a guest
Oct 27th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. """
  2. CMod Ex3: Program to run a 3D symplectic Euler time integration
  3. of a particle in a gravitational potential. The program produces a plot
  4. of the orbiting mass' trajectory and outputs, to a file, the
  5. x and y positions of the mass at each time step.
  6.  
  7. Author: Taylor Baird
  8. Version:26/10/16
  9. """
  10. import sys
  11. import matplotlib.pyplot as plt
  12. from Particle3D import Particle3D
  13. import numpy as np
  14.  
  15. G = 1.0                #Universal gravitational constant
  16.  
  17. #Check that an output file has been supplied
  18. if len(sys.argv) != 2:
  19.     print "Wrong number of arguments"
  20.     print "Usage: " + sys.argv[0] + "<output file>"
  21.     quit()
  22. else:
  23.     outfileName = sys.argv[1]
  24.  
  25. # open an output file for writing
  26. outfile = open(outfileName, "w")
  27.  
  28. #Initialize particle with supplied mass and initial position and velocity.
  29. mass = 0.0001
  30. position = np.array([1,0,0])
  31. velocity = np.array([0,1,0])
  32. label = "orbiting mass"
  33. p = Particle3D(mass, label, position, velocity)
  34.  
  35. # Initialize central particle. It is located at the origin, with a mass of 1.
  36. #TODO Maybe remove this object since it is constant
  37. mass = 1.0
  38. label = "central_particle"
  39. position = np.array([0, 0, 0])
  40. velocity = np.array([0, 0, 0])
  41. central_particle = Particle3D(mass, label, position, velocity)
  42.  
  43. #Set simulation parameters
  44. numstep = 100
  45. time = 0.0
  46. timestep = 0.1
  47.  
  48. #Initialize x and y position lists and time list
  49. tVals = [time]
  50. xVals = [p.position[0]]
  51. yVals = [p.position[1]]
  52.  
  53. # Calculation of the initial total energy of the particle as a reference
  54. # so that we can calculate how the energy of the system fluctuates with successive timesteps.
  55. potentialEnergy = -(G * central_particle.mass * p.mass) / np.linalg.norm(p.position)
  56. initialTotalEnergy = potentialEnergy + p.getKineticEnergy()
  57.  
  58. outfile.write("{0:f}\t{1}\t{2}\n".format(time, p.position, initialTotalEnergy))
  59.  
  60. for i in range(numstep):
  61.     #Use first order position update to update particle's position
  62.     p.leapPos(timestep)
  63.     #update force
  64.     force = -((p.mass * central_particle.mass * G)/(np.linalg.norm(p.position))**3)*p.position
  65.     #update particle velocity
  66.     p.leapVel(timestep, force)
  67.  
  68.     #update time
  69.     time += timestep
  70.     #update time and position lists
  71.     tVals.append(time)
  72.     xVals.append(p.position[0])
  73.     yVals.append(p.position[1])
  74.  
  75.     # Calculation of new total energy and fluctuation from initial total energy
  76.     potentialEnergy = -(central_particle.mass * p.mass)/(np.linalg.norm(p.position))
  77.     totalEnergy = potentialEnergy + p.getKineticEnergy()
  78.     totalEnergyFluctuation = (totalEnergy - initialTotalEnergy)/initialTotalEnergy
  79.  
  80.     # TODO Format better, remove energy?
  81.     # output current position and time to file
  82.     outfile.write("{0:f}\t{1}\t{2}\t{3}\n".format(time, p.position, totalEnergy, totalEnergyFluctuation))
  83.  
  84. outfile.close()
  85.  
  86. plt.ylabel("y position")
  87. plt.xlabel("x position")
  88. plt.title("Plot of y position against x position for orbiting mass in\ngravitational potential using symplectic Euler method")
  89. #Plot the y position of the particle against x position for successive time steps.
  90. plt.plot(xVals, yVals)
  91. #Make the scales of the axes equal
  92. plt.gca().set_aspect('equal', adjustable='box')
  93. #Save the obtained plot as a png image.
  94. plt.savefig("symplecticEulerImg.png")
  95. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement