Grifter

Electron simulation

Jul 1st, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.48 KB | None | 0 0
  1. __author__ = 'Grifter'
  2.  
  3. from visual import *
  4. from random import randint
  5.  
  6.  
  7. class Simulation():
  8.     def __init__(self, num):
  9.         """
  10.        Initialize simulation with values for electrons, coulomb constant and sim dt.
  11.  
  12.        :param num: Number of electrons to simulate.
  13.        :return: Returns Sim object
  14.        """
  15.  
  16.         # Default sim constants
  17.         self.dt = 0.02
  18.         self.num = num
  19.         self.coulomb_constant = 8.878E9
  20.         self.electron_mass = 9.11E-31
  21.         self.electron_charge = -1.60E-19
  22.  
  23.         # Data repository
  24.         self.electron_list = []
  25.  
  26.         # Particle Init Loop
  27.         for _ in range(self.num):
  28.             self.electron_list.append(self.electron)
  29.  
  30.     @property
  31.     def electron(self):
  32.         """
  33.        This sim property is called to initialize an electron in the sim with random position and velocity
  34.  
  35.        :return: Electron sphere object
  36.        """
  37.         return sphere(pos=vector(randint(-20, 20), randint(-20, 20), randint(-20, 20)),
  38.                       mass=self.electron_mass, charge=self.electron_charge,
  39.                       velocity=vector(randint(-3, 3), randint(-3, 3), randint(-3, 3)),
  40.                       acceleration=vector(0, 0, 0),
  41.                       color=(randint(0, 1), randint(0, 1), randint(0, 1)),
  42.                       make_trail=True, interval=3, retain=50)
  43.  
  44.     def coulombs_law(self, body1, body2):
  45.         """
  46.        Calculate the force on an electron using Coulombs Law.
  47.        :param body1: The body the force is being calculated on.
  48.        :param body2: The body the force is being calculated with.
  49.        :return: Force vector
  50.        """
  51.         # Calculate the scalar value of the electromagnetic force
  52.         temp_scalar_force = -((self.coulomb_constant * self.electron_charge ** 2) / abs(body1.pos - body2.pos) ** 2)
  53.  
  54.         # Calculate a norm vector for the electromagnetic force
  55.         temp_norm_vector = (body2.pos - body1.pos) / abs(body2.pos - body1.pos)
  56.  
  57.         # Return the product of the norm vector and the scalar value of the force (The proper force vector)
  58.         return temp_scalar_force * temp_norm_vector
  59.  
  60.     def acceleration_calc(self):
  61.         """
  62.        Updates the net acceleration of each particle in the simulation.
  63.        :return: None
  64.        """
  65.         for i in self.electron_list:
  66.             # Initialize acceleration for this step
  67.             i.acceleration = vector(0, 0, 0)
  68.             for j in self.electron_list:
  69.                 # Ensure that the particle does not include self in force calc
  70.                 if not (i is j):
  71.                     # Use newtons second law and the coulomb function to calc acceleration
  72.                     i.acceleration += self.coulombs_law(i, j) / self.electron_mass
  73.  
  74.     def forward_euler(self):
  75.         """
  76.        Updates the position of each particle using a forward Euler method.
  77.        :return: None
  78.        """
  79.         for i in self.electron_list:
  80.             i.velocity += i.acceleration * self.dt
  81.             i.pos += i.velocity * self.dt
  82.  
  83.     def step(self):
  84.         """
  85.        Iterate the simulation calling the appropriate functions sequentially.
  86.        :return: None
  87.        """
  88.         # FPS = inverse dt (for dt = 0.02, FPS = 50)
  89.         rate(1 / self.dt)
  90.         self.acceleration_calc()
  91.         self.forward_euler()
  92.  
  93. ## ~~~~~~~~~~~ MAIN ~~~~~~~~~~~ ##
  94.        
  95. #Initialize sim
  96. sim = Simulation(int(input('How many electrons?: ')))
  97.  
  98. #Main Loop
  99. while True:
  100.     sim.step()
Advertisement
Add Comment
Please, Sign In to add comment