def ComapareSolvers(h, t_final=1.): P_E = [Planet(3e-6, [1, 0, 0], [0, 2 * pi, 0], "Earth")] P_V = [Planet(3e-6, [1, 0, 0], [0, 2 * pi, 0], "Earth")] initial_en_E = P_E[0].total_energy() # initial energy P_E initial_en_V = P_V[0].total_energy() # initial energy P_V initial_l_E = P_E[0].angular_momentum() # initial angular momentum P_E initial_l_V = P_V[0].angular_momentum() # initial angular momentum P_E l = np.linalg.norm(np.cross(P_E[0].r, P_E[0].v * P_E[0].mass)) #print("before", initial_l_E/P_V[0].mass) time0 = time.time() x_verlet, y_verlet = solve(P_V, "VelocityVerlet", h, t_final) # solve with Velocity Verlet time_verlet = time.time() - time0 time0 = time.time() x_euler, y_euler = solve(P_E, "EulerCromer", h, t_final) # solve with Euler Cromer time_euler = time.time() - time0 diff_en_E = (initial_en_E - P_E[0].total_energy( )) / initial_en_E #relative difference in total energy Euler Cormer diff_en_V = (initial_en_V - P_V[0].total_energy( )) / initial_en_V #relatevi difference in total energy Velocity Verlet diff_l_E = (initial_l_E - P_E[0].angular_momentum( )) / initial_l_E #relative difference in angular momentum Euler Cromer diff_l_V = (initial_l_V - P_V[0].angular_momentum( )) / initial_l_V #relative difference in angular momentum Velocity Verlet #print("after",P_E[0].angular_momentum()/P_V[0].mass) l = np.linalg.norm(np.cross(P_E[0].r, P_E[0].v * P_E[0].mass)) #print(P_E[0].r, P_E[0].v) # plt.plot(x_euler[0],y_euler[0],label="EulerCromer") # plt.plot(x_verlet[0],y_verlet[0],label="VelocityVerlet") # plt.title("Solutions of the Sun-Earh system, h=%.2f" %h) # plt.xlabel("x/AU") # plt.ylabel("y/AU") # plt.axis('equal') # plt.legend() # plt.show() # plt.savefig('VV_vs_EC2',dpi=225) return (diff_en_E, diff_en_V, diff_l_E, diff_l_V, time_euler, time_verlet)
def GravitationalConstant(power, h, t_final=1.): for p in power: Earth = [Planet(3e-6, [1, 0, 0], [0, 2 * pi, 0], "Earth")] x, y = solve(Earth, "VelocityVerlet", h, t_final, p) x, y = x[0], y[0] plt.plot(x, y, label="beta = %.1f" % (p - 1)) plt.legend() plt.axis('equal') plt.xlabel("x/AU") plt.ylabel("y/AU") plt.title( "The orbit of Earth with different expressions for gravitational force" ) #plt.savefig("gravitationalForce") plt.show()
def InitialVelocity( velocities, h, t_final=3. ): # Input: a vector of initial x-velocities in multiples of pi, h: step length, t_final for v in velocities: Earth = [Planet(3e-6, [1, 0, 0], [0, v * pi, 0], "Earth")] print(Earth[0].total_energy()) x, y = solve(Earth, "VelocityVerlet", h, t_final) x, y = x[0], y[0] plt.plot(x, y, label="v= %.2f pi" % v) plt.legend() plt.axis('equal') plt.xlabel("x/AU") plt.ylabel("y/AU") plt.title( "The orbit of Earth with different inital velocities. t_final = %d yr " % t_final) #plt.savefig('%d year' %t_final, dpi=225) plt.show()
for j in range(N_p): for k in range(N_plot): #samples N_plot points from the solution vectors x[j] and y[j] X[k] = x[j,k*delta_t] Y[k] = y[j,k*delta_t] plt.plot(x[j],y[j],label=Planets[j].name) plt.title("The solar system. t_final = 2" ) plt.xlabel("x/AU") plt.ylabel("y/AU") plt.axis('equal') plt.legend() plt.show() #plt.savefig('solar_system',dpi=225) # Initial values of the planets from 2017-Oct-04: Mercury = Planet(1.65e-7, [-0.3789210,2.56223e-02,0],[-7.32704e-03*days, -2.68813e-02*days,0], "Mercury") MercuryNewtonian = Planet(1.65e-7, [-0.3789210,2.56223e-02,0],[-7.32704e-03*days, -2.68813e-02*days,0], "MercuryNewtonian") Venus = Planet(2.45e-6, [-.483111, .534117,0],[-1.49738e-02*days, -1.37846e-02*days,0 ], "Venus") Earth = Planet(3e-6,[.985157,.191737e-01,0],[-3.48636e-3*days,1.68385e-02*days,0],"Earth") Mars = Planet(3.3e-7, [-1.49970, 0.724618,0], [-5.52182e-03*days, -1.14214e-02*days,0],"Mars") Jupiter = Planet(9.5e-4,[-4.63556,-2.8425,0],[3.85608e-03*days,-6.075643e-03*days,0],"Jupiter") Saturn = Planet(2.75e-4,[-0.421224,-10.0462,0],[5.26820e-03*days,-2.52167e-04*days,0], "Saturn") Uranus = Planet(4.4e-5,[17.8825, 8.76632,0],[-1.75983e-03*days,3.34820e-03*days],"Uranus") Neptune = Planet(0.515e-4, [28.6020, -8.86057, 0],[9.08471e-04*days, 3.01748e-03*days,0],"Neptune") # A list of all the planets Planets = [Earth, Jupiter, Mercury, Venus, Mars, Saturn, Uranus, Neptune] x,y = solve(Planets, "VelocityVerlet", 1/100, 2) plot(Planets, x,y,10)
h=1/(360*60*60*8) from project3b import Planet # attributes: mass, r, v, a_s, a, name, total_energy(), angular_momentum() from project3b import Acceleration # attributes: N_p, Update(Planets, beta = 3.) from project3b import DiffEqSolver # attributes: h, EulerCromer(Planets), VelcityVerlet(Planets) from project3b import solve # input: (Planets, method, h, t_final=1., beta = 3.) # Initial values of the planets from 2017-Oct-04: Mercury = Planet(1.65e-7, [-0.3789210,2.56223e-02,0],[-7.32704e-03*days, -2.68813e-02*days,0], "Mercury") MercuryNewtonian = Planet(1.65e-7, [-0.3789210,2.56223e-02,0],[-7.32704e-03*days, -2.68813e-02*days,0], "MercuryNewtonian") # A list of all the planets OnlyMercury = [Mercury] OnlyMercury_Newtonian = [MercuryNewtonian] x_Newtonian, y_Newtonian = solve(OnlyMercury_Newtonian, "VelocityVerlet",h, 100) #The newtonian solution of the Mercury-Sun system #plt.plot(x_Newtonian[0], y_Newtonian[0]) x_Relatvistic, y_Relativistic = solve(OnlyMercury, "VelocityVerlet",h, 100) #The relativistic sloution of the Mercury-Sun system for i in range(len(x_Newtonian[0])):# if (x_Newtonian[0,i]**2+y_Newtonian[0,i]**2 - 0.3075**2)<1.e-7: # search for the perihelion (perihelion distance : 0.3075 AU) print(np.arctan(y_Newtonian[0,i]/x_Newtonian[0,i]), np.arctan(y_Relativistic[0,i]/x_Relatvistic[0,i])) # calculate perihelion angle plt.plot(x_Relatvistic[0], y_Relativistic[0]) plt.axis('equal') plt.title("Mercury") plt.xlabel("x/AU") plt.ylabel("y/AU") plt.show()