Space Comps
 All Classes Files Functions Variables
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Algorithm Class Referenceabstract

The Algorithm class is a parent to all the specific Algorithms we test. More...

#include <algorithm.h>

Inheritance diagram for Algorithm:
BruteForceAlgorithm OctreeAlgorithm QuadtreeAlgorithm SpatialHashAlgorithm SpatialHashAlgorithm3d SweepNPruneAlgorithmMulti SweepNPruneAlgorithmSimple

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...
 
MultiQueuegetPositionQueue ()
 Returns the MultiQueue that contains upcoming Positions for each Particle. More...
 
std::mutex * getCollisionMutex ()
 Returns the Mutex locking the Collision queue. More...
 
ParticlegetCenterOfGravity ()
 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...
 
PhysicsgetPhysics ()
 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

ParticlegenerateParticleIn2D (int i)
 
ParticlegenerateParticleIn3D (int i)
 
ParticlegenerateParticleInOrbit (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
 
MultiQueuepositionQueue
 
Physicsphysics
 
bool running
 
int numParts
 
int timestep
 
double xWinSize
 
double yWinSize
 
double zWinSize
 
double systemKE
 
double systemPE
 

Detailed Description

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.

Constructor & Destructor Documentation

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.

Parameters
xInthe size of the x dimension of the space.
yInthe 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.

Parameters
xInthe size of the x dimension of the space.
yInthe size of the y dimension of the space.
zInthe 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.

Parameters
xInthe size of the x dimension of the space.
yInthe size of the y dimension of the space.
zInthe size of the z dimension of the space.
centerMassthe mass of the Particle responsible for gravitation.

Member Function Documentation

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.

Parameters
numParticlesthe 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.

Parameters
numParticlesthe 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.

Parameters
numParticlesthe 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.

Parameters
collisionsthe vector of collisions detected in the most recent timestep.
Particle * Algorithm::getCenterOfGravity ( )

Returns the massive Particle in the center responsible for Gravitation.

Returns
the Particle responsible for gravity and orbits.
std::mutex * Algorithm::getCollisionMutex ( )

Returns the Mutex locking the Collision queue.

Returns
the Mutex locking the Collision queue.
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.

Returns
The queue of Collisions we have detected, but haven't been warned about.
int Algorithm::getNumParts ( )

Returns the number of Particles the Algorithm is tracking.

Returns
the number of Particles moving in the space.
std::vector< Particle * > Algorithm::getParticles ( )

Returns the vector of Particles that the algorithm is tracking.

Returns
The vector of Particles the algorithm is tracking.
Physics * Algorithm::getPhysics ( )

Returns the Physics object working for this Algorithm.

Returns
the Physics associated with the Algorithm
MultiQueue * Algorithm::getPositionQueue ( )

Returns the MultiQueue that contains upcoming Positions for each Particle.

Returns
the multiQueue of upcoming Positions.
double Algorithm::getSystemKE ( )

Returns the system kinetic energy which is updated each timestep.

Returns
the system kinetic energy.
double Algorithm::getSystemPE ( )

Returns system potential energy which is updated each timestep.

Returns
the system potential energy
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.

Returns
the Algorithm timestep.
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)

Sets the number of Particles the Algorithm is tracking.

Parameters
numParticlesthe number of Particles the Algorithm will have.
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.


The documentation for this class was generated from the following files: