sumanifol/Particle-Trajectory
0
1import gradio as gr2 3# The main simulation function4def particle_simulation(x_i, y_i, V):5 # Constants6 m = 9.1093837e-317 q = -1.6021766e-198 k = 8.99e99 x_q = 0.06810 y_q = -0.02211 Q = -81e-1212 13 # Validate inputs14 x_i = int(x_i)15 y_i = int(y_i)16 V = int(V)17 18 if abs(x_i) > 20 or abs(y_i) > 20 or V < 10 or V > 10000:19 return "Invalid input: Parameters out of range."20 21 # Adjust initial positions22 x_i = x_i / 100 - x_q23 y_i = y_i / 100 - y_q24 25 # Simulation26 dt = 1e-9 # Time step27 r_x, r_y, r_z = [], [], []28 x_old, y_old, z_old = x_i, y_i, -5e229 vx_old, vy_old, vz_old = 0, 0, (abs(2 * q * V / m))**0.530 31 z_new = 032 is_forward = True33 i = 034 35 while z_new <= 0.5 and i <= 1e7 and is_forward:36 vx_new = vx_old + (k * Q * q * x_old / m) / ((x_old**2 + y_old**2 + z_old**2)**1.5) * dt37 vy_new = vy_old + (k * Q * q * y_old / m) / ((x_old**2 + y_old**2 + z_old**2)**1.5) * dt38 vz_new = vz_old + (k * Q * q * z_old / m) / ((x_old**2 + y_old**2 + z_old**2)**1.5) * dt39 40 x_new = x_old + vx_new * dt41 y_new = y_old + vy_new * dt42 z_new = z_old + vz_new * dt43 44 r_x.append(x_new)45 r_y.append(y_new)46 r_z.append(z_new)47 48 is_forward = z_new > z_old49 vx_old, vy_old, vz_old = vx_new, vy_new, vz_new50 x_old, y_old, z_old = x_new, y_new, z_new51 i += 152 53 if abs(r_x[-1] + x_q) > 0.5 or abs(r_y[-1] + y_q) > 0.5 or not is_forward:54 return "Beam did not reach the detector screen."55 else:56 return f"Beam reached the screen:\nX: {round((r_x[-1] + x_q) * 100, 2)} cm\nY: {round((r_y[-1] + y_q) * 100, 2)} cm\nZ: {round(r_z[-1], 2)} cm"57 58# Create the Gradio interface59iface = gr.Interface(60 fn=particle_simulation,61 inputs=[62 gr.Number(label="Initial X-axis Position (cm)"),63 gr.Number(label="Initial Y-axis Position (cm)"),64 gr.Number(label="Beam Accelerating Voltage (V)"),65 ],66 outputs="text",67 title="Particle Simulation",68 description="Enter the parameters to calculate the beam trajectory. Note: Inputs out of range will result in errors."69)70 71iface.launch(share = True)72 