00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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;
00038
00039 const double diameter;
00040
00041 const double diameter_sqr;
00042
00043 const int nboxes_x, nboxes_y, nboxes_z;
00044
00045 std::vector< std::vector<Particle> > particles;
00046
00047
00048
00049
00050
00051
00052 int nx_particle, ny_particle, nz_particle;
00053 std::vector<Particle>::iterator particle_iterator;
00054 Particle::vector_t current_position;
00055
00056
00057 int nx_neighbor, ny_neighbor, nz_neighbor;
00058 std::vector<Particle>::iterator neighbor_iterator;
00059 std::vector<Particle>* neighbor_box_ptr;
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
00120
00121
00122
00123
00124
00125
00126
00127 if (nx<0 || ny<0 || nz<0 ||
00128 nx>=nboxes_x || ny>=nboxes_y || nz>=nboxes_z ) {
00129
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
00164 return *neighbor_box_ptr;
00165 }
00166
00169 const std::vector<Particle>& get_neighbor_box() const {
00170
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
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