Main Page | Class Hierarchy | Class List | File List | Class Members

space.h

00001 /***************************************************************************
00002                           space.h  -  description
00003                              -------------------
00004     begin                : Fri Jan 3 2003
00005     copyright            : (C) 2003 by Moritz Franosch
00006     email                : mail@Franosch.org
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #ifndef SPACE_H
00019 #define SPACE_H
00020 
00021 #include <vector>
00022 
00023 #include "particle.h"
00024 
00025 
00033 class Space {
00034 
00035 private:
00036 
00037   const double length_x, length_y, length_z; // dimensions of the space
00038 
00039   const double diameter; // longest interacting distance
00040 
00041   const double diameter_sqr; // diameter^2
00042 
00043   const int nboxes_x, nboxes_y, nboxes_z; // number of boxes in each direction
00044 
00045   std::vector< std::vector<Particle> > particles; 
00046   // stores all particles in boxes
00047   // particles[nz*nboxes_y*nboxes_x + ny*nboxes_x + nx][n]
00048   // is the particle number n in box (nx,ny,nz),
00049   // 0 <= nx < nboxes_x, 0 <= ny < nboxes_y, 0 <= nz < nboxes_z
00050 
00051   // iterator through particles
00052   int nx_particle, ny_particle, nz_particle; // box of particle or of current position
00053   std::vector<Particle>::iterator particle_iterator; // particle in the box
00054   Particle::vector_t current_position; // position of current particle or current position
00055 
00056   // iterator through neighboring particles
00057   int nx_neighbor, ny_neighbor, nz_neighbor; // box
00058   std::vector<Particle>::iterator neighbor_iterator; // particles in the box
00059   std::vector<Particle>* neighbor_box_ptr; // point to the current neighboring box
00060 
00063   static int nboxes(const double length, const double diameter);
00064 
00067   int nbox(const double x) const;
00068 
00075   bool is_neighbor(const Particle::vector_t& p1, const Particle::vector_t& p2,
00076                    double& r_sqr) const {
00077     r_sqr = sqr_euclidian_distance(p1,p2);
00078     return r_sqr < diameter_sqr;
00079   }
00080 
00084   bool is_neighbor(const Particle::vector_t& p1, const Particle::vector_t& p2) const {
00085     double r_sqr;
00086     return is_neighbor(p1,p2,r_sqr);
00087   }
00088 
00096   bool find_particle();
00097 
00105   bool find_neighbor();
00106 
00113   bool find_neighbor(double& r_sqr);
00114 
00117   int get_n(const int nx, const int ny, const int nz) const {
00118     /*
00119     REQUIRE(nx>=0);
00120     REQUIRE(ny>=0);
00121     REQUIRE(nz>=0);
00122     REQUIRE(nx<nboxes_x);
00123     REQUIRE(ny<nboxes_y);
00124     REQUIRE(nz<nboxes_z);
00125     */
00126     // If position is outside of the space ...
00127     if (nx<0 || ny<0 || nz<0 || 
00128         nx>=nboxes_x || ny>=nboxes_y || nz>=nboxes_z ) {
00129       // ..., then return the number of a box that is always empty.
00130       ENSURE( particles[particles.size()-1].empty() );
00131       return particles.size()-1;
00132     }
00133     int n=nz*nboxes_y*nboxes_x + ny*nboxes_x + nx;
00134     ENSURE(n>=0);
00135     ENSURE( n<particles.size() );
00136     return n;
00137   }
00138 
00141   int get_n(const Particle::vector_t& p) const;
00142 
00146   int get_n(const Particle& particle) const;
00147 
00150   std::vector<Particle>& get_box() {
00151     return particles[get_n(nx_particle,ny_particle,nz_particle)];
00152   }
00153 
00156   const std::vector<Particle>& get_box() const {
00157     return particles[get_n(nx_particle,ny_particle,nz_particle)];
00158   }
00159 
00162   std::vector<Particle>& get_neighbor_box() {
00163     //    return particles[get_n(nx_neighbor,ny_neighbor,nz_neighbor)];
00164     return *neighbor_box_ptr;
00165   }
00166 
00169   const std::vector<Particle>& get_neighbor_box() const {
00170     //    return particles[get_n(nx_neighbor,ny_neighbor,nz_neighbor)];
00171     return *neighbor_box_ptr;
00172   }
00173 
00177   bool next_neighbor_box();
00178 
00181   bool in_space(const Particle::vector_t& p) const;
00182 
00185   bool in_space(const Particle& particle) const;
00186 
00189   bool in_box(const Particle& particle, 
00190               const int nx, const int ny, const int nz) const;
00191 
00194   bool particles_in_their_box() const;
00195 
00196 protected:
00197 
00200   Particle& get_particle() {
00201     REQUIRE( !at_end() );
00202     return *particle_iterator;
00203   }
00204 
00208   Particle& get_neighbor_particle() {
00209     REQUIRE( !at_end_of_neighborhood() );
00210     return *neighbor_iterator;
00211   }
00212 
00213 public: 
00214 
00220   Space(const double length_x, const double length_y, const double length_z, 
00221         const double diameter);
00222 
00225   double get_diameter() const;
00226 
00229   double get_length_x() const;
00230 
00233   double get_length_y() const;
00234 
00237   double get_length_z() const;
00238 
00241   double get_length(const int i) const;
00242 
00249   void insert_particle(const Particle::vector_t& p);
00250 
00253   void reset();
00254 
00259   bool next();
00260 
00263   bool at_end() const;
00264 
00267   const Particle& get_particle() const {
00268     REQUIRE( !at_end() );
00269     return *particle_iterator;
00270   }
00271 
00274   void reset_neighborhood();
00275 
00282   void reset_neighborhood(double& r_sqr);
00283 
00288   void reset_neighborhood(const Particle::vector_t& p, double& r_sqr);
00289 
00294   //  bool has_neighbor(const Particle::vector_t& p);
00295 
00300   bool next_neighbor() {
00301     REQUIRE( !at_end_of_neighborhood() );
00302     ++neighbor_iterator;
00303     return find_neighbor();
00304   }
00305 
00312   bool next_neighbor(double& r_sqr) {
00313     REQUIRE( !at_end_of_neighborhood() );
00314     ++neighbor_iterator;
00315     return find_neighbor(r_sqr);
00316   }
00317 
00320   bool at_end_of_neighborhood() const {
00321     return neighbor_iterator==get_neighbor_box().end();
00322   }
00323 
00327   const Particle& get_neighbor_particle() const {
00328     REQUIRE( !at_end_of_neighborhood() );
00329     return *neighbor_iterator;
00330   }
00331 
00338   void reorder_particles();
00339 
00340 };
00341 
00342 
00343 #endif // SPACE_H

Generated on Fri Apr 22 11:06:42 2005 for partsim.kdevelop by doxygen 1.3.6