The Algorithm class is a parent to all the specific Algorithms we test. More...
#include <algorithm.h>
Public Member Functions | |
Algorithm (double xIn, double yIn) | |
Constructs a 2 dimensional Algorithm. More... | |
Algorithm (double xIn, double yIn, double zIn) | |
Constructs a 3 dimensional Algorithm. More... | |
Algorithm (double xIn, double yIn, double zIn, double centerMass) | |
Constructs a 3 dimensional Algorithm with orbital Physics. More... | |
std::vector< Particle * > | getParticles () |
Returns the vector of Particles that the algorithm is tracking. More... | |
std::queue< Collision * > * | getCollisions () |
Returns the queue of Collisions that we haven't entered the warning window yet. More... | |
MultiQueue * | getPositionQueue () |
Returns the MultiQueue that contains upcoming Positions for each Particle. More... | |
std::mutex * | getCollisionMutex () |
Returns the Mutex locking the Collision queue. More... | |
Particle * | getCenterOfGravity () |
Returns the massive Particle in the center responsible for Gravitation. More... | |
int | getNumParts () |
Returns the number of Particles the Algorithm is tracking. More... | |
int | getTimestep () |
Returns the timestep the Algorithm is currently calculating. More... | |
double | getSystemKE () |
Returns the system kinetic energy which is updated each timestep. More... | |
double | getSystemPE () |
Returns system potential energy which is updated each timestep. More... | |
Physics * | getPhysics () |
Returns the Physics object working for this Algorithm. More... | |
void | setNumParts (int numParticles) |
Sets the number of Particles the Algorithm is tracking. More... | |
void | createParticles2D (int numParticles) |
Sets the number of Particles, then generates that many to track. More... | |
void | createParticles3D (int numParticles) |
Sets the number of Particles, then generates that many to track. More... | |
void | createParticlesOrbit (int numParticles) |
Sets the number of Particles, then generates that many to track. More... | |
void | respawnParticle (Particle *particle) |
void | enqueueCollisions (std::vector< Collision * > collisions) |
Pushes Collisions from the most recent timestep onto the queue of upcoming Collision$s. More... | |
void | runOneTimestep () |
Calculates the next timestep. More... | |
void | run () |
void | stop () |
void | stepTime () |
Calculates the next timestep and sets running to true. More... | |
Protected Member Functions | |
Particle * | generateParticleIn2D (int i) |
Particle * | generateParticleIn3D (int i) |
Particle * | generateParticleInOrbit (int i) |
void | regenerateParticleInOrbit (Particle *p) |
bool | checkForOverlap (Particle *p) |
virtual void | calculateNextTimestep ()=0 |
void | enqueuePositions () |
void | updateSystemKE () |
void | updateSystemPE () |
void | rescaleVelocities () |
void | checkTimestep () |
Protected Attributes | |
std::vector< Particle * > | particles |
std::queue< Collision * > | collisionQueue |
std::mutex | collisionMutex |
MultiQueue * | positionQueue |
Physics * | physics |
bool | running |
int | numParts |
int | timestep |
double | xWinSize |
double | yWinSize |
double | zWinSize |
double | systemKE |
double | systemPE |
The Algorithm class is a parent to all the specific Algorithms we test.
Algorithm contains most of the data and methods the various algorithms that we use to detect collisions have in common. It also allows the CollisionSystem to ask for information from Algorithms without having to know which type of Algorithm it is running.
Algorithm::Algorithm | ( | double | xIn, |
double | yIn | ||
) |
Constructs a 2 dimensional Algorithm.
Initializes a Physics2D, a MultiQueue for Particle's Positions, sets running to false, the timestep to 0, the x and y size of the window, sets the z size to 0, and sets the system kinetic energy to 0. Other things that must be initialized are handled by Algorithm's children.
xIn | the size of the x dimension of the space. |
yIn | the size of the y dimension of the space. |
Algorithm::Algorithm | ( | double | xIn, |
double | yIn, | ||
double | zIn | ||
) |
Constructs a 3 dimensional Algorithm.
Initializes a Physics3D, a MultiQueue for Particle's Positions, sets running to false, the timestep to 0, the x, y, and z size of the window, and sets the system kinetic energy to 0. Other things that must be initialized are handled by Algorithm's children.
xIn | the size of the x dimension of the space. |
yIn | the size of the y dimension of the space. |
zIn | the size of the z dimension of the space. |
Algorithm::Algorithm | ( | double | xIn, |
double | yIn, | ||
double | zIn, | ||
double | centerMass | ||
) |
Constructs a 3 dimensional Algorithm with orbital Physics.
Initializes an OrbitalPhysics, a MultiQueue for Particle's Positions, sets running to false, the timestep to 0, the x, y, and z size of the window, and sets the system kinetic energy to 0. Other things that must be initialized are handled by Algorithm's children.
xIn | the size of the x dimension of the space. |
yIn | the size of the y dimension of the space. |
zIn | the size of the z dimension of the space. |
centerMass | the mass of the Particle responsible for gravitation. |
void Algorithm::createParticles2D | ( | int | numParticles | ) |
Sets the number of Particles, then generates that many to track.
First this sets the number of Particles the Algorithm will be tracking Then it randomly generates that many 2d Particles within the space, ensuring that no 2 Particles are generated overlapping eachother. A 2d Particle is just a particle with 0 for the z coordinate and velocity.
numParticles | the number of Particles the Algorithm will have. |
void Algorithm::createParticles3D | ( | int | numParticles | ) |
Sets the number of Particles, then generates that many to track.
First this sets the number of Particles the Algorithm will be tracking Then it randomly generates that many 3d Particles within the space, ensuring that no 2 Particles are generated overlapping eachother.
numParticles | the number of Particles the Algorithm will have. |
void Algorithm::createParticlesOrbit | ( | int | numParticles | ) |
Sets the number of Particles, then generates that many to track.
First this sets the number of Particles the Algorithm will be tracking Then it randomly generates that many 2d Particles within the space, ensuring that no 2 Particles are generated overlapping eachother. The Particles are generated with KE + PE = 0 and with directions orthogonal to their position relative to the center of gravity, so they will have (close to) circular orbits around the center of gravity.
numParticles | the number of Particles the Algorithm will have. |
void Algorithm::enqueueCollisions | ( | std::vector< Collision * > | collisions | ) |
Pushes Collisions from the most recent timestep onto the queue of upcoming Collision$s.
For each Collision in the list of new Collisions found in the most recent timestep, this aquires the lock on the Collision queue, pushes a Collision, then releases the lock.
collisions | the vector of collisions detected in the most recent timestep. |
Particle * Algorithm::getCenterOfGravity | ( | ) |
std::mutex * Algorithm::getCollisionMutex | ( | ) |
std::queue< Collision * > * Algorithm::getCollisions | ( | ) |
Returns the queue of Collisions that we haven't entered the warning window yet.
When the Algorithm detects a Collision, it pushes it onto this queue. The Collision is removed from the queue when the timestep of the Collision is within the warning window of the CollisionDisplay. More directly, a Collision leaves this queue when its particles turn red.
int Algorithm::getNumParts | ( | ) |
std::vector< Particle * > Algorithm::getParticles | ( | ) |
Returns the vector of Particles that the algorithm is tracking.
Physics * Algorithm::getPhysics | ( | ) |
MultiQueue * Algorithm::getPositionQueue | ( | ) |
Returns the MultiQueue that contains upcoming Positions for each Particle.
double Algorithm::getSystemKE | ( | ) |
Returns the system kinetic energy which is updated each timestep.
double Algorithm::getSystemPE | ( | ) |
Returns system potential energy which is updated each timestep.
int Algorithm::getTimestep | ( | ) |
Returns the timestep the Algorithm is currently calculating.
The timestep the Algorithm is on is proportional to the timestep the CollisionDisplay is on. That is because the Algorithm updates Particle Positions ALG_SPEED_SCALE (usually 50) times for every time the CollisionDisplay refreshes so that gravity calculating can be more precise while still having the particles move at a reasonable rate.
void Algorithm::runOneTimestep | ( | ) |
Calculates the next timestep.
Calculates the next timestep which entails moving all the particles an appropriate amount, seeing if any Collisions have occured, and if so, resolving them. This is a public method that calls the private Algorithm::calculateNextTimestep() and is quite possibly bad coding practice.
void Algorithm::setNumParts | ( | int | numParticles | ) |
void Algorithm::stepTime | ( | ) |
Calculates the next timestep and sets running to true.
Calculates the next timestep which entails moving all the particles an appropriate amount, seeing if any Collisions have occured, and if so, resolving them. This is a public method that calls the private calculateNextTimestep() and is quite possibly bad coding practice.