MercuryDPM  0.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HGRID_base.cc
Go to the documentation of this file.
1 //Copyright (c) 2013-2014, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #include "HGRID_base.h"
27 
29 {
30  constructor();
31  #ifdef CONSTUCTOR_OUTPUT
32  std::cerr << "HGRID_base() finished"<<std::endl;
33  #endif
34 }
35 
37  if (grid) { delete grid; grid=NULL; }
38 }
39 
40 HGRID_base::HGRID_base(MD& other) : MD(other)
41 {
42  constructor();
43  #ifdef CONSTUCTOR_OUTPUT
44  std::cerr << "HGRID_base(MD& other) finished " << std::endl;
45  #endif
46 }
47 
50 {
52  NUM_BUCKETS=250000;
54  grid = NULL;
55 
57 
61 
62 }
63 
64 
67 {
69 }
70 
71 void HGRID_base::set_HGRID_num_buckets(unsigned int new_num_buckets)
72 {
73  if (new_num_buckets>0)
74  {
75  NUM_BUCKETS=new_num_buckets;
76  }
77  else
78  {
79  std::cerr << "Error in set_HGRID_num_buckets" << std::endl;
80  exit(-1);
81  }
82 }
83 
85 {
86  set_HGRID_num_buckets_to_power(getParticleHandler().getNumberOfObjects());
87 }
88 
90 {
91  unsigned int NUM_BUCKETS = 32;
92  while (NUM_BUCKETS<2*N)
93  {
94  NUM_BUCKETS *= 2;
95  }
96  set_HGRID_num_buckets(NUM_BUCKETS);
97 }
98 
99 void HGRID_base::read(std::istream& is)
100 {
101  MD::read(is);
102 
103  std::stringstream line (std::stringstream::in | std::stringstream::out);
104  getLineFromStringStream(is,line);
105 
106  std::string dummy;
107  line>> dummy >> NUM_BUCKETS
108  >> dummy >> hGridMaxLevels_
109  >> dummy >> cellOverSizeRatio_;
110 
111 }
112 
113 void HGRID_base::write(std::ostream& os)
114 {
115  MD::write(os);
116  os << "NUM_BUCKETS " << NUM_BUCKETS << " "
117  << "hGridMaxLevels " << hGridMaxLevels_ << " "
118  << "cellOverSizeRatio " << cellOverSizeRatio_ << std::endl;
119 }
120 
121 void HGRID_base::print(std::ostream& os, bool print_all=false)
122 {
123  MD::print(os,print_all);
124  os << " NUM_BUCKETS:" << NUM_BUCKETS << ", hGridMaxLevels:" << hGridMaxLevels_ <<", cellOverSizeRatio:" << cellOverSizeRatio_<<std::endl;
125 }
126 
128 {
130 }
131 
133 {
135 }
136 
137 void HGRID_base::setHGridUpdateEachTimeStep(bool updateEachTimeStep)
138 {
139  updateEachTimeStep_=updateEachTimeStep;
140 }
141 
143  return updateEachTimeStep_;
144 }
145 
148 {
149  std::vector<double> cellSizes;
150  switch(getHGridDistribution())
151  {
152  case LINEAR:
153  {
154  double minCellSize=0.99999*2.0*getSmallestParticle()->get_InteractionRadius()*cellOverSizeRatio_;
155  double maxCellSize=1.00001*2.0*getLargestParticle()->get_InteractionRadius()*cellOverSizeRatio_;
156  std::cout<<"HGrid: using a linear cell size distribution from "<<minCellSize<<" to "<<maxCellSize<<" over "<<hGridMaxLevels_<<" levels"<<std::endl;
157  for (int i=0; i<hGridMaxLevels_; i++)
158  {
159  cellSizes.push_back(minCellSize+(maxCellSize-minCellSize)*(i+1.0)/hGridMaxLevels_);
160  }
161  break;
162  }
163  case EXPONENTIAL:
164  {
165  double minCellSize=0.99999*2.0*getSmallestParticle()->get_InteractionRadius()*cellOverSizeRatio_;
166  double maxCellSize=1.00001*2.0*getLargestParticle()->get_InteractionRadius()*cellOverSizeRatio_;
167  std::cout<<"HGrid: using an exponential cell size distribution from "<<minCellSize<<" to "<<maxCellSize<<" over "<<hGridMaxLevels_<<" levels"<<std::endl;
168  for (int i=0; i<hGridMaxLevels_; i++)
169  {
170  cellSizes.push_back(minCellSize*pow(maxCellSize/minCellSize,(i+1.0)/hGridMaxLevels_));
171  }
172  break;
173  }
174  case OLDHGRID:
175  {
177  for (int i=0; i<hGridMaxLevels_; i++)
178  {
179  cellSizes.push_back(minCellSize*pow(2,i));
180  }
181  break;
182  }
183 
184  }
185 
186  //Create grid object
187  if (grid!=NULL)
188  {
189  delete grid;
190  }
191  grid = new HGrid(NUM_BUCKETS, cellOverSizeRatio_, cellSizes);
192 
194 
196 
197  //New steps in initbroad_phase is to not only Insert the Particles in the grid but also update them
198  for (int i=0; i<grid->NUM_BUCKETS; i++)
199  {
200  grid->objectBucket[i] = NULL;
201  grid->bucketIsChecked[i] = false;
202  }
203 
204  for (std::vector<BaseParticle*>::iterator it = getParticleHandler().begin(); it!=getParticleHandler().end(); ++it)
205  {
208  (*it)->set_HGRID_x(9999);
210  }
211 
212 }
213 
215 {
217 }
218 
219 
222 {
223  static int stepsBeforeUpdate=0;
224 
225  for (int i=0; i<grid->NUM_BUCKETS; i++)
226  {
227  grid->bucketIsChecked[i] = false;
228  }
229 
231  {
232 #ifndef ContactListHgrid
233  for (int i=0; i<grid->NUM_BUCKETS; i++)
234  {
235  grid->objectBucket[i] = NULL;
236  }
237 #endif
239  for (std::vector<BaseParticle*>::iterator it = getParticleHandler().begin(); it!=getParticleHandler().end(); ++it)
240  {
242  }
243  stepsBeforeUpdate=0;
244  }
245  else
246  {
247  stepsBeforeUpdate++;
248  }
249 }
250 
252 {
253  //PI==PJ check is required because this function is called for all possible close Particle combination (even itself)
254  return pI->get_Index()==pJ->get_Index() || GetDistance2(pI->get_Position(),pJ->get_Position())>=sqr(pI->get_InteractionRadius()+pJ->get_InteractionRadius());
255 }
256 
257 
259 {
260  Mdouble currentRelativeDisplacement=move/(grid->cellSizes_[iP->get_HGRID_Level()]);
261  if(currentRelativeDisplacement>currentMaxRelativeDisplacement_)
262  {
263  currentMaxRelativeDisplacement_=currentRelativeDisplacement;
264  }
265 }
266 
268 {
270 }
271 
273 {
275 }
276 
277 int HGRID_base::readNextArgument(unsigned int& i, unsigned int argc, char *argv[])
278 {
279  if (!strcmp(argv[i],"-NUM_BUCKETS"))
280  {
281  NUM_BUCKETS = atoi(argv[i+1]);
282  }
283  else if (!strcmp(argv[i],"-hGridMaxLevels"))
284  {
285  hGridMaxLevels_ = atoi(argv[i+1]);
286  }
287  else if (!strcmp(argv[i],"-cellOverSizeRatio"))
288  {
289  cellOverSizeRatio_ = atof(argv[i+1]);
290  }
291  else
292  {
293  return MD::readNextArgument(i, argc, argv); //if argv[i] is not found, check the commands in HGRID_3D
294  }
295  return true; //returns true if argv[i] is found
296 }
297 
299 {
300  return hGridMethod_;
301 }
302 
304 {
305  hGridMethod_=hGridMethod;
306 }
307 
309 {
310  return hGridDistribution_;
311 }
312 
314 {
315  hGridDistribution_=hGridDistribution;
316 }
317 
319 {
320  return cellOverSizeRatio_;
321 }
322 
324 {
325  cellOverSizeRatio_=cellOverSizeRatio;
326 }
327 
328 void HGRID_base::setHGridMaxLevels(int hGridMaxLevels)
329 {
330  if (hGridMaxLevels>0)
331  {
332  hGridMaxLevels_=hGridMaxLevels;
333  }
334  else
335  {
336  std::cout<<"Error in void HGRID_base::set_HGridMaxLevel(int hGridMaxLevels), hGridMaxLevels should be strictly positive, while it is now "<<hGridMaxLevels<<std::endl;
337  }
338 }
339 
341 {
342  return hGridMaxLevels_;
343 }
HGridMethod getHGridMethod()
Definition: HGRID_base.cc:298
void HGRID_actions_after_integration()
Definition: HGRID_base.cc:272
void InsertParticleToHgrid(BaseParticle *obj)
This insert a particle given by CParticle in to the HGrid (i.e. it sets up the particle grid properts...
Definition: HGRID.cc:69
Mdouble getHGridCellOverSizeRatio()
Definition: HGRID_base.cc:318
void setHGridCellOverSizeRatio(Mdouble cellOverSizeRatio)
Definition: HGRID_base.cc:323
void setHGridUpdateEachTimeStep(bool updateEachTimeStep)
Definition: HGRID_base.cc:137
Mdouble get_InteractionRadius() const
bool updateEachTimeStep_
Definition: HGRID_base.h:140
void set_HGRID_num_buckets_to_power()
set number of buckets to the smallest power of two bigger than the number of particles ...
Definition: HGRID_base.cc:84
void getLineFromStringStream(std::istream &in, std::stringstream &out)
void set_HGRID_num_buckets(unsigned int new_num_buckets)
This sets the number of buckets for the HGRID.
Definition: HGRID_base.cc:71
void setHGridMethod(HGridMethod hGridMethod)
Definition: HGRID_base.cc:303
void InitBroadPhase()
This sets up the parameters required for the contact model.
Definition: HGRID_base.cc:147
#define sqr(a)
Definition: ExtendedMath.h:36
void setHGridMaxLevels(int HGridMaxLevels)
Definition: HGRID_base.cc:328
void print(std::ostream &os, bool print_all)
This function outputs all HGRID data.
Definition: HGRID_base.cc:121
virtual BaseParticle * getLargestParticle()
Definition: MD.h:419
Mdouble currentMaxRelativeDisplacement_
Definition: HGRID_base.h:137
int get_Index() const
HGridDistribution
Definition: HGRID_base.h:37
virtual void write(std::ostream &os)
Writes all MD data.
Definition: MD.cc:1985
Mdouble getHGridCurrentMaxRelativeDisplacement()
Definition: HGRID_base.cc:127
void constructor()
This is the actually constructor it is called do both constructors above.
Definition: HGRID_base.cc:49
int NUM_BUCKETS
Number of buckets used for hashing.
Definition: HGRID.h:47
std::vector< double > cellSizes_
Definition: HGRID.h:50
int hGridMaxLevels_
Definition: HGRID_base.h:142
HGRID_base()
This is the default constructor. All it does is set senible defaults.
Definition: HGRID_base.cc:28
void write(std::ostream &os)
This function writes all HGRID data.
Definition: HGRID_base.cc:113
Mdouble cellOverSizeRatio_
Definition: HGRID_base.h:143
double Mdouble
Definition: ExtendedMath.h:33
This is the HGRID class - This is the actually HGRID code.
Definition: HGRID.h:39
void HGRID_actions_before_integration()
Definition: HGRID_base.cc:267
virtual int readNextArgument(unsigned int &i, unsigned int argc, char *argv[])
Definition: MD.cc:2554
virtual bool TestObject(BaseParticle *pI, BaseParticle *pJ)
criterium for inserting a particle (returns false, if particles overlap;)
Definition: HGRID_base.cc:251
~HGRID_base()
This is the default destructor.
Definition: HGRID_base.cc:36
const std::vector< T * >::const_iterator end() const
Gets the end of the const_iterator over all BaseBoundary in this BaseHandler.
Definition: BaseHandler.h:233
HGridMethod
Definition: HGRID_base.h:36
virtual void HGRID_UpdateParticleInHgrid(BaseParticle *obj UNUSED)
Definition: MD.h:559
void Initialize_inv_size()
Definition: HGRID.cc:57
void setHGridDistribution(HGridDistribution hGridDistribution)
Definition: HGRID_base.cc:313
HGrid * grid
Definition: HGRID_base.h:131
const Vec3D & get_Position() const
bool * bucketIsChecked
bucketIsChecked[b] stores if hash bucket b is checked already; initially all zero ...
Definition: HGRID.h:61
int readNextArgument(unsigned int &i, unsigned int argc, char *argv[])
Definition: HGRID_base.cc:277
virtual void read(std::istream &is)
Reads all MD data.
Definition: MD.cc:2026
A class that defines and solves a MD problem.
Definition: MD.h:70
HGridMethod hGridMethod_
Definition: HGRID_base.h:134
HGridDistribution hGridDistribution_
Definition: HGRID_base.h:135
bool getHGridUpdateEachTimeStep()
Definition: HGRID_base.cc:142
void HGRID_InsertParticleToHgrid(BaseParticle *obj)
Inserts a single Particle to current grid.
Definition: HGRID_base.cc:214
int occupiedLevelsMask
l-th bit of occupiedLevelsMask is 1 if level l is contains particles; initially zero (Implies max 32 ...
Definition: HGRID.h:55
int getHGridMaxLevels()
Definition: HGRID_base.cc:340
ParticleHandler & getParticleHandler()
Definition: MD.h:147
void read(std::istream &is)
This function reads all HGRID data.
Definition: HGRID_base.cc:99
int NUM_BUCKETS
Definition: HGRID_base.h:141
virtual BaseParticle * getSmallestParticle()
Definition: MD.h:418
Mdouble getHGridTotalCurrentMaxRelativeDisplacement()
Definition: HGRID_base.cc:132
void HGRID_actions_before_time_step()
This resets all the bucket information.
Definition: HGRID_base.cc:221
void HGRID_actions_before_time_loop()
This sets up the broad phase information, has to be done at this stage becuase it requires the partcl...
Definition: HGRID_base.cc:66
Mdouble totalCurrentMaxRelativeDisplacement_
Definition: HGRID_base.h:138
HGridDistribution getHGridDistribution()
Definition: HGRID_base.cc:308
int get_HGRID_Level() const
BaseParticle ** objectBucket
objectBucket[b] stores pointer to first element in hash bucket b; initially all NULL ...
Definition: HGRID.h:58
virtual void print(std::ostream &os, bool print_all=false)
Outputs MD.
Definition: MD.cc:1806
void HGRID_update_move(BaseParticle *iP, Mdouble move)
Definition: HGRID_base.cc:258