HG-MD  1
ParticleHandler.h
Go to the documentation of this file.
00001 #ifndef ParticleHandler_H
00002 #define ParticleHandler_H
00003 
00004 #include "CParticle.h"
00005 #include <limits> //Used for DBL_MAX
00006 
00007 using namespace std;
00008 
00009 class ParticleHandler
00010 {
00011         public:
00013         ParticleHandler()
00014         {
00015                 maxParticles=0;
00016                 #ifdef CONSTUCTOR_OUTPUT
00017                         cerr << "ParticleHandler() finished" << endl;
00018                 #endif                          
00019         }
00020         
00022         ParticleHandler(const ParticleHandler &pH)
00023         {
00024                 cout<<"ParticleHandler copy constructor"<<endl;
00025                 for (unsigned int i=0;i<pH.particles.size();i++)
00026                 {
00027                         addParticle(pH.particles[i]->copy());
00028                 }
00029                 #ifdef CONSTUCTOR_OUTPUT
00030                         cerr << "ParticleHandler(const ParticleHandler &pH) finished" << endl;
00031                 #endif          
00032         }
00033         
00034         ParticleHandler operator = (const ParticleHandler right)
00035         {
00036                 cout<<"ParticleHandler assignment opperator"<<endl;
00037                   if (this != &right) 
00038                   {
00039                         for (unsigned int i=0;i<right.particles.size();i++)
00040                         {
00041                                 addParticle(right.particles[i]->copy());
00042                         }
00043                         
00044                 }
00045                 return *this;
00046         }
00047         
00049         ~ParticleHandler()
00050         {
00051                 for(vector<Particle*>::iterator it = particles.begin(); it!=particles.end();it++)
00052                 {
00053                         delete *it;
00054                 }
00055         }
00056         
00057         
00058         
00061         void copyAndAddParticle(Particle &P)
00062         {
00063                 //Puts the particle in the Particle list
00064                 particles.push_back(P.copy());
00065                 //Set the index of the particle
00066                 particles.back()->set_Index(particles.size()-1); 
00067                 //Update the maximum number of Particles
00068                 if(particles.size()>maxParticles) maxParticles=particles.size();
00069         }
00070         
00072         void addParticle(Particle* P)
00073         {
00074                 //Puts the particle in the Particle list
00075                 particles.push_back(P);
00076                 //Set the index of the particle
00077                 particles.back()->set_Index(particles.size()-1);
00078                 //Update the maximum number of Particles
00079                 if(particles.size()>maxParticles) maxParticles=particles.size();
00080         }       
00081 
00084         void removeParticle(int IP)
00085         {
00086                 //Physically remove Particle
00087                 delete particles[IP];
00088                 //Copy the pointer to the last Particle to position p
00089                 particles[IP] = particles.back();
00090                 //Set the correct (new) index
00091                 particles[IP]->set_Index(IP);
00092                 //Remove the (now Mdouble) reference to that last Particle
00093                 particles.pop_back();
00094                 
00095                 for(unsigned int i=0;i<particles[IP]->get_TangentialSprings().size();)
00096                 {
00098                         if (particles[IP]->get_TangentialSprings()[i].pParticle>IP)
00099                         {
00100                                 int JP=particles[IP]->get_TangentialSprings()[i].pParticle;
00101                                 
00102                                 //Copy Tangentalspring to new location
00103                                 CTangentialSpring CTS=particles[IP]->get_TangentialSprings()[i];
00104                                 CTS.pParticle=IP;
00105                                 particles[JP]->get_TangentialSprings().push_back(CTS);
00106                                 
00107                                 //Remove now unused tangentialspring
00108                                 particles[IP]->get_TangentialSprings()[i]=particles[IP]->get_TangentialSprings().back();
00109                                 particles[IP]->get_TangentialSprings().pop_back();
00110                         }       else i++;                               
00111                 }       
00112         }
00113         
00115         void removeLastParticle()
00116         {
00117                 //Physically removes Particle
00118                 delete particles.back();
00119                 //Remove the (now Mdouble) reference to that last Particle
00120                 particles.pop_back();
00121         }
00122         
00123         
00125         Particle* get_SmallestParticle(){
00126                 if (!particles.size()) cerr << "Warning: No particles to set get_SmallestParticle()" << endl;
00127                 Particle* p = NULL;
00128                 Mdouble minRadius = numeric_limits<Mdouble>::max();
00129                 for (unsigned int i=0; i<particles.size(); i++) {
00130                         if (particles[i]->get_Radius()<minRadius) {
00131                                 minRadius=particles[i]->get_Radius(); 
00132                                 p = particles[i]; 
00133                         }
00134                 }               
00135                 return p;
00136         }
00137         
00139         Particle* get_LightestParticle(){
00140                 if (!particles.size()) cerr << "Warning: No particles to set get_LightestParticle()" << endl;
00141                 Particle* p = NULL;
00142                 Mdouble minMass = numeric_limits<Mdouble>::max();
00143                 for (unsigned int i=0; i<particles.size(); i++) {
00144                         if (particles[i]->get_Mass()<minMass) {
00145                                 minMass=particles[i]->get_Mass(); 
00146                                 p = particles[i]; 
00147                         }
00148                 }
00149                 return p;
00150         }
00151         
00153         Particle* get_LargestParticle(){
00154                 if (!particles.size()) cerr << "Warning: No particles to set get_LargestParticle()" << endl;
00155                 Particle* p = NULL;
00156                 Mdouble maxRadius = 0;
00157                 for (unsigned int i=0; i<particles.size(); i++) { 
00158                         if (particles[i]->get_Radius()>maxRadius) {
00159                                 maxRadius=particles[i]->get_Radius(); 
00160                                 p = particles[i]; 
00161                         }
00162                 }
00163                 return p;
00164         }
00165         
00167         void clear()
00168         {
00169                 while (particles.size()>0)
00170                         removeLastParticle();
00171         }
00172         
00174         Particle* get_Particle(unsigned int p) {if (p<particles.size()) return particles[p]; else {cerr<<"No particle exist with index "<<p<<endl; return NULL;}}
00175         
00177         Particle* back() {return particles.back();}
00178         
00180         unsigned int get_NumberOfParticles() {return particles.size();}
00181         
00183         unsigned int get_StorageCapacity() {return particles.capacity();}
00184         
00186         unsigned int get_MaximumParticles() {return maxParticles;}
00187         
00189         void set_StorageCapacity(unsigned int N){particles.reserve(N);}
00190         
00192         vector<Particle*>::iterator begin() {return particles.begin();}
00194         vector<Particle*>::iterator end() {return particles.end();}
00195         
00196         private:
00198         vector<Particle*> particles;
00200         unsigned int maxParticles;
00201         
00202 };
00203 
00204 #endif
00205 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines