The Barnes Hut Model is a method for calculating the gravitational force between charged particles. It is a tree-based algorithm that uses a quadtree or octree to calculate the force between particles. The algorithm is described in the paper A hierarchical O(N log N) force-calculation algorithm by Barnes and Hut.
- C/C++ Extension
- GSL (remember to compile with
-lgsl -lgslcblas -lm
flags)pacman -S mingw-w64-x86_64-gsl
- Eigen3 or
pacman -S mingw-w64-x86_64-eigen3
- matplotlib
- pandas
- numpy
- pyvista (for running vtk_animator.py)
- octree.cpp - Octree data structure
- octree.cpp - Update Center of Mass/Charge
- barnes.cpp - Calculate Force
- simulation_loop.cpp - Calculate Acceleration
- simulation_loop.cpp - Update tree and particle position
- initialConditions.cpp
- initialConditions.cpp - implement initial velocity
- Create initial values csv file
[ ] Utilise OpenMP/CUDA to parallelise the code- Different types of particles
- Optimise Insertion
- Check Energy Conservation
- Implement Magnetic Field
- Implement liner cylinder
- Move liner towards centre
Run plotter.py
to view initial_values.csv
generated by initial_conditions.cpp
. The plotter uses matplotlib to plot the points in 3D space.
Run main.cpp
to view simulation_values.csv
. main.cpp
automatically runs animator.py
to animate the movement of particles. The animation will be saved as output.mp4
.
The octree is a tree data structure that is used to partition space. Each node in the tree represents a cube in space. The root node represents the entire space. Each node has 8 children, which represent the 8 cubes that make up the parent cube.
The children of a node are only created if the node contains more than one particle. The particles are then distributed among the children. The algorithm for creating the octree is as follows:
- Create the root node, which represents the entire space
- When a new particle is added to the tree, check if there is a particle in the root node
- If there is no particle in the root node, add the particle to the root node
- If there is a particle in the root node, create 8 children for the root node (i.e split the root node into 8 3d octets) and move the particles to the appropriate children
- Repeat step 2 until all particles are in the tree
While summing the forces between every particle is an O(N^2) operation, the Barnes Hut Model uses the octree to reduce the number of particles that need to be considered when calculating the force between particles. The algorithm for calculating the force between particles is as follows:
- If the current node is an external node (and it is not body b), calculate the force exerted by the current node on b, and add this amount to b’s net force.
- Otherwise, calculate the ratio s/d, where s is the width of the region represented by the internal node, and d is the distance between the body and the node’s center-of-mass.
- If s/d < θ, treat this internal node as a single body, and calculate the force it exerts on body b, and add this amount to b’s net force.
- Otherwise, run the procedure recursively on each of the current node’s children.
The Barnes Hut Model is an O(N log N) algorithm. The value of θ is a parameter that affects the degree of approximation. The smaller the value of θ, the more accurate the simulation is. However, the smaller the value of θ, the longer the simulation takes to run.
By finding the centre of charge of each octet, we are able to treat all particles as a single particle at the point of the centre of charge. By splitting the centre of charge into its positive and negative components, (i.e different centre of charges for positive and negative charge), we are able to subtract the different values of positive and negative charge to obtain the correct resultant force. This allows us to simulate multiple particle types with different charges at the same time as well as preventing division by zeroes.
The octree allows for us to calculate the force between particles in O(N log N) time. For the calculation of the force between charged particles, we can use Coulomb's law:
Using the Biot-Savart Law, we can derive another equation to calculate the magnetic forces between moving charges:
The initial values for the particles are generated using the initial_conditions.cpp
file. The particles are generated in a sphere within the given boundaries in the octree. The particles are given a certain charge and mass (based on what particle we are simulating), as well as a certain coordinate distrubution using Gaussian Distribution:
where
The number of particles is given by the following equation:
where
- For a sphere,
$f(x) = \frac{4}{3} \pi r^3$ - For a cylinder,
$f(x) = \pi r^2 h$ - For a hollow cylinder,
$f(x) = \pi (r_2^2 - r_1^2) h$ , where$r_2$ is the outer radius and$r_1$ is the inner radius
Their initial velocities in the (x, y, z) directions are calculated using the Maxwell-Boltzmann distribution:
where k is the Boltzmann constant, T is the temperature, and m is the mass of the particle.