return 1000 * np.exp(-lamb * t) t = 0 h = 0.5 y_euler, y_rk2, y_rk4, y_rk45n = 1000, 1000, 1000, 1000 tt, exsol = [], [] nuclear_euler, nuclear_rk2, nuclear_rk4, nuclear_rk45n = [], [], [], [] abserr_euler, abserr_rk2, abserr_rk4, abserr_rk45n = [], [], [], [] clf_euler, clf_rk2, clf_rk4, clf_rk45n = [], [], [], [] while t <= 8: yy_euler = ode.Euler(decay, [y_euler], t, h) yy_rk2 = ode.rk2(decay, [y_rk2], t, h) yy_rk4 = ode.rk4(decay, [y_rk4], t, h) yy_rk45n = ode.RK45n(decay, [y_rk45n], t, h) exs = exact_sol(t) tt.append(t) exsol.append(exs) nuclear_euler.append(yy_euler[0]) nuclear_rk2.append(yy_rk2[0]) nuclear_rk4.append(yy_rk4[0]) nuclear_rk45n.append(yy_rk45n[0]) abserr_euler.append(abs(yy_euler[0] - exs)) abserr_rk2.append(abs(yy_rk2[0] - exs)) abserr_rk4.append(abs(yy_rk4[0] - exs)) abserr_rk45n.append(abs(yy_rk45n[0] - exs))
def simulate_baseball(v0, omega, r0=None, h=0.01, C_D=0.40, g=9.81, rho=1.225, r=0.07468/2, m=0.14883, R_homeplate=18.4): """simulate baseball pitch Parameters ---------- v0 : array initial velocity (vx, vy, vz) in m/s omega : array angular velocity vector of the ball ("spin"), in rad/s r0 : array, optional initial position of the ball (in m) when it leaves the pitcher's hand as (x, y, z); the default is (0, 2, 0) h : float, optional integration time step in s, default is 0.01 s C_D : float, optional drag coefficient, default is 0.40 g : float, optional acceleration due to gravity, default 9.81 kg/(m*s^2) rho : float, optional density of air, default 1.225 kg/m^3 r : float, optional radius of the baseball m : float, optional mass of the baseball R_homeplate : float, optional distance of the catcher from the pitcher Returns ------- positions : array The array contains an entry (time, x, y, z) for each time step. """ # all SI units (kg, m) if r0 is None: r0 = np.array([0, 2, 0]) # pitching at 2m height omega = np.asarray(omega) domega = np.linalg.norm(omega) A = np.pi*r**2 rhoArm = rho * A * r / m b2 = 0.5 * C_D * rho * A a_gravity = np.array([0, -g, 0]) def f(t, y): # y = [x, y, z, vx, vy, vz] v = y[3:] dv = np.linalg.norm(v) S = r*domega/dv a_magnus = 0.5 * C_L(S) * rhoArm / S * np.cross(omega, v) a_drag = -b2/m * dv * v a = a_gravity + a_drag + a_magnus return np.array([y[3], y[4], y[5], a[0], a[1], a[2]]) t = 0 # initialize 3D! y = np.array([r0[0], r0[1], r0[2], v0[0], v0[1], v0[2]], dtype=np.float64) positions = [[t, y[0], y[1], y[2]]] # record t, x and y, z while y[0] < R_homeplate and y[1] >= 0.2: y[:] = ode.rk4(y, f, t, h) positions.append([t, y[0], y[1], y[2]]) # record t, x and y, z t += h return np.array(positions)
def main(): # Initial velocity [m/s] vy0 = 2.0e5 vz0 = -2.56e5 # B-field max [T] B0 = mu0 * I0 * Nt / 2.0 / np.pi / Ra # Larmor pulsation [rad/s] w_L = B0 * qm # Larmor period [s] tau_L = 2.0 * np.pi / w_L # Larmor radius [m] r_L = vy0 / w_L # Mirror Ratio Rm = (vy0**2 + vz0**2) / (vy0**2) # Sine of the Loss angle sth = np.sqrt(1.0 / Rm) # Loss angle thm = np.arcsin(sth) * 180.0 / np.pi # Initial conditions X0 = np.array((0.001, 0.0, Ra, 0.0, vy0, vz0)) # Number of Larmor gyrations N_gyros = 2 # Time grid time = np.linspace(0.0, tau_L * N_gyros, 75 * N_gyros) # Runge-Kutta 4 X = ode.rk4(fun, time, X0) # Collect components x = X[:, 0] y = X[:, 1] z = X[:, 2] vx = X[:, 3] vy = X[:, 4] vz = X[:, 5] # Current Loop points theta = np.linspace(0.0, 2 * np.pi, 100) Xloop = Ra * np.cos(theta) Yloop = np.zeros(100) Zloop = Ra * np.sin(theta) # Plot 1 - Trajectory plt.figure(1) plt.plot(x, z, 'b-', label='orbit') plt.plot(Xloop, Yloop, 'r', label='loop') plt.xlabel('x [m]') plt.ylabel('z [m]') plt.axis('equal') plt.legend() # Add few fieldlines # Initial position of the field line Nlines = 10 fieldlines_X0 = np.linspace(0, Ra * 0.98, Nlines) fieldlines_Y0 = np.linspace(0, 0, Nlines) fieldlines_Z0 = np.linspace(0, 0, Nlines) fieldlines_direction = np.ones(Nlines) fieldlines_length = np.ones(Nlines) * 0.1 Center = np.array([0, 0, 0]) Uhat = np.array([0, 1, 0]) Npoints = 20 filament = bfield.makeloop(Ra, Center, Uhat, 100) for i in range(np.size(fieldlines_X0, 0)): # Top portion Y0 = np.array([ fieldlines_X0[i], fieldlines_Y0[i], fieldlines_Z0[i], fieldlines_direction[i] ]) interval = x = np.arange(0.0, fieldlines_length[i], 1e-4) fieldlines = odeint(bfield.blines, Y0, interval, args=(filament, I0)) plt.plot(fieldlines[:, 0], fieldlines[:, 1], 'k-') # Bottom portion Y0 = np.array([ fieldlines_X0[i], fieldlines_Y0[i], fieldlines_Z0[i], -fieldlines_direction[i] ]) interval = x = np.arange(0.0, fieldlines_length[i], 1e-4) fieldlines = odeint(bfield.blines, Y0, interval, args=(filament, I0)) plt.plot(fieldlines[:, 0], fieldlines[:, 1], 'k-') plt.xlim([0, 1.5 * Ra]) plt.ylim([-0.1 * Ra, 1.4 * Ra]) plt.xlabel('X [m]') plt.ylabel('Z [m]') plt.title('Mirroring of a particle launched toward a current loop') plt.savefig('ex05_mirror_trajectory.png', dpi=150) plt.show() plt.figure(2) plt.plot(time * 1e6, vz) plt.xlabel('time [micro-seconds]') plt.ylabel('Vz [m/s]') plt.title('Velocity along the axis of the current loop') plt.savefig('ex05_mirror_vaxial.png', dpi=150) plt.show()
from ode import rk4,print_to_file e = 2.71828 def f(y,x): dydx=y[1] ddydxx=1-x-y[1] return [dydx,ddydxx] dt=0.1 N=5/dt #finding solution for 0 to 5 x1, y1 = rk4(f, [2,1], 0, 5, N) # dt=0.1 print_to_file("data_files/q2_part1.txt",x1,*y1) # finding soln for 0 to -5 x2, y2 = rk4(f, [2,1], 0, -5, N) #dt=0.1 print_to_file("data_files/q2_part2.txt",x2,*y2)
def main(): # Initial position [m] x0 = 0.5 y0 = 0.0 z0 = 0.0 # Initial velocity [m/s] vx0 = 1000 vy0 = 0.0 vz0 = 0.0 # Initial conditions X0 = np.array([x0, y0, z0, vx0, vy0, vz0]) # Time interval T = 5.0e-6 # Time grid for the RK4 solution time_rk4 = np.linspace(0.0, T, 100) # Solve ODE (Runge-Kutta 4) X = ode.rk4(fun, time_rk4, X0) # Get components of the state vector x = X[:, 0] y = X[:, 1] z = X[:, 2] vx = X[:, 3] vy = X[:, 4] vz = X[:, 5] # Analytical solution time_an = np.linspace(0.0, T, 1000) x_an = np.zeros(time_an.size) v_an = np.zeros(time_an.size) for i in range(time_an.size): xa, va = analytical(time_an[i], x0, vx0) x_an[i] = xa v_an[i] = va # Characteristic freq a1 = qm * Eg w = np.sqrt(np.absolute(a1)) tau = 2.0 * np.pi / w plt.figure(1) plt.plot(time_an, x_an, 'k-', label='Analytical Solution') plt.plot(time_rk4, x, 'ro', label='Runge-Kutta (4th)') plt.xlabel('t [s]') plt.ylabel('x [m]') plt.legend(loc=3) plt.savefig('ex07_grad_E_linear_x.png') plt.figure(2) plt.plot(time_an, v_an, 'k-', label='Analytical Solution') plt.plot(time_rk4, vx, 'ro', label='Runge-Kutta (4th)') plt.xlabel('t [s]') plt.ylabel('v [m/s]') plt.legend(loc=3) plt.savefig('ex07_grad_E_linear_v.png') plt.show()
def main(): # Thermal speed T_eV = 1.0 v_th = np.sqrt(2*KB*T_eV*11600/mp) # Initial velocity [m/s] vy0 = v_th vz0 = v_th # Reference Magnetic Field Bx,By,Bz = Bfield(0.72,0,0) B0 = np.sqrt(Bx*Bx + By*By + Bz*Bz) # Larmor pulsation [rad/s] w_L = np.abs(qm * B0) # Larmor period [s] tau_L = 2.0*np.pi / w_L # Larmor radius [m] r_L = vy0 / w_L # Initial conditions X0 = np.array( [ 0.72, 0.0, 0.0, 0.0, vy0, vz0 ] ) # Number of Larmor gyrations N_gyro = 200 # Number of points per gyration N_points_per_gyration = 100 # Time grid time = np.linspace( 0.0, tau_L*N_gyro, N_gyro*N_points_per_gyration ) # Solve ODE (Runge-Kutta 4) X = ode.rk4( fun, time, X0 ) # Get components of the state vector x = X[:,0] y = X[:,1] z = X[:,2] vx = X[:,3] vy = X[:,4] vz = X[:,5] R = np.sqrt(x*x + y*y) plt.figure(1) plt.plot( x, y, 'b-', label='Runge-Kutta (4th)' ) plt.xlabel('x [m]') plt.ylabel('y [m]') plt.axis('equal') plt.legend(loc=3) plt.savefig('ex02_drift_grad_B_trajectory.png') plt.show() plt.figure(2) plt.plot( R, z, 'b-') plt.xlabel('R, Radius [m]') plt.ylabel('Z, Vertical Coordinate [m]') plt.axis('equal') plt.savefig('ex02_drift_grad_B_vertical_drift.png') plt.show()