-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpatialGrid.cpp
269 lines (235 loc) · 7.93 KB
/
SpatialGrid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "SpatialGrid.h"
#include <omp.h>
#include <cmath>
#include <cstdlib>
SpatialGrid::SpatialGrid(const float h)//s is the number of boxes for a side, h is the smoothing radius of our weighting function
{
this->boxLength = h;
this->p1 = 10619863;
this->p2 = 94418953;
this->p3 = 54018521;
this->newPositionsSet = false;
}
SpatialGrid::SpatialGrid(){
}
void SpatialGrid::clear(){
}
int SpatialGrid::addParticle(Particle p){
Point3D pos = p.getPosition();
float x = pos.getX();
float y = pos.getY();
float z = pos.getZ();
int xindex = (int)floor((x+10)/(this->boxLength));
int yindex = (int)floor((y+10)/(this->boxLength));
int zindex = (int)floor((z+10)/(this->boxLength));
int thisid = ((xindex*p1) ^ (yindex*p2) ^ (zindex*p3)); //no idea where this particle is in the grid yet
if(this->hashMap.find(thisid) != this->hashMap.end()){//such a bucket exists
std::map<int,Bucket>::iterator it;
it = this->hashMap.find(thisid);
it->second.addParticle(p);
}
else{//the bucket does not exist
Bucket b =Bucket(Point3D(xindex, yindex, zindex));
b.addParticle(p);
hashMap.insert ( std::pair<int, Bucket>(thisid,b) );
}
std::map<int,Bucket>::iterator it;
it = this->hashMap.find(thisid);
std::vector<int> ids = it->second.getNeighbors();
//either way we need to check neighbors of the bucket if they don't exist
if(it->second.getNeighbors().size() != 26){//26 surrounding boxes total
int i = -1;
while(i <= 1){
int j = -1;
while(j <= 1){
int k = -1;
while(k <= 1){
int xindex2 = xindex + i;
int yindex2 = yindex + j;
int zindex2 = zindex + k;
int id = ((xindex2*p1) ^ (yindex2*p2) ^ (zindex2*p3)); //instantiate neighbor ids
//if(this->hashMap.find(id) != this->hashMap.end()){//such a bucket exists
//}
//else{//the bucket does not exist
if (!(i==0 && j==0 && k==0)){
//this->hashMap[id] = Bucket(Point3D(xindex2, yindex2, zindex2));
ids.push_back(id);
}
//}
k++;
}
j++;
}
i++;
}
}
return thisid;
}
std::vector<Particle> SpatialGrid::getNeighbors(Particle p){
int thisid = p.getHashID();//((xindex*p1) ^ (yindex*p2) ^ (zindex*p3));
vector<Particle> list;
Bucket it = this->hashMap.find(thisid)->second;
//if(it != this->hashMap.end()){//such a bucket exists
std::vector<int> neighbors = it.getNeighbors();
std::vector<Particle> parts = it.getContents();
unsigned int i = 0;
//#pragma omp parallel for firstprivate(i)
for(i = 0; i < parts.size(); i++){
/*if(!(it->second.getContents()[i].getPosition().getX() == x &&
it->second.getContents()[i].getPosition().getY() == y &&
it->second.getContents()[i].getPosition().getZ() == z)){*/
list.push_back(parts[i]);
//}
}
unsigned int j = 0;
//#pragma omp parallel for firstprivate(j)
for(j = 0; j < neighbors.size(); j++){
unsigned int k = 0;
//std::map<int,Bucket>::iterator it2 = this->hashMap.find(neighbors[j]);
if(this->hashMap.find(neighbors[j]) != this->hashMap.end()) {
parts = this->hashMap.find(neighbors[j])->second.getContents();
for(k=0; k < parts.size(); k++){
list.push_back(parts[k]);
}
}
}
//}
//else{//the bucket does not exist
// it should never go in here since particles are in the grid/bucket in the first place
/*cout<<"ERROR: Checking neighbors of particle not in grid"<<endl;
int breakpoint = 0;
cin >> breakpoint;
exit(666);*/
//}
return list;
}
Particle SpatialGrid::updateBoxes(Particle partc){
//if(newPositionsSet == false){//haven't even initialized the new positions yet
unsigned int c = 0;
//while(c < particles.size()){
Point3D pos = partc.getPosition();//particle now has a new position
float x = pos.getX();
float y = pos.getY();
float z = pos.getZ();
Point3D OLDpos = partc.getOldPosition();
float OLDx = OLDpos.getX();
float OLDy = OLDpos.getY();
float OLDz = OLDpos.getZ();
int xindex = (int)floor((x+10)/(this->boxLength));//these are now new position indices
int yindex = (int)floor((y+10)/(this->boxLength));
int zindex = (int)floor((z+10)/(this->boxLength));
int thisid = partc.getHashID();//((OLDxindex*p1) ^ (OLDyindex*p2) ^ (OLDzindex*p3));//this is the OLD HASH ID
int newid = ((xindex*p1) ^ (yindex*p2) ^ (zindex*p3));//this is the NEW HASH ID we need to set
partc.setHashID(newid);
/*cout<<"the list parts hash id: "<<thisid<<endl;
cout<<"the calculated new position hash id: "<<newid<<endl;
cout<<"the calculated old position hash id: "<<((OLDxindex*p1) ^ (OLDyindex*p2) ^ (OLDzindex*p3))<<endl;
cout<<"the old position: "<<endl<<OLDpos<<endl;
cout<<"the new position: "<<endl<<pos<<endl;
int breakpoint;
cin >> breakpoint;*/
if(thisid != newid){
partc.setHashID(newid);
//need to put the particle into the new bucket and erase the other particle
//add particle in new position
if(this->hashMap.find(newid) != this->hashMap.end()){//such a bucket exists
std::map<int,Bucket>::iterator it;
it = this->hashMap.find(newid);
it->second.addParticle(partc);
}
else{//the bucket does not exist
Bucket b =Bucket(Point3D(xindex, yindex, zindex));
b.addParticle(partc);
hashMap.insert ( std::pair<int, Bucket>(newid,b) );
}
std::map<int,Bucket>::iterator it;
it = this->hashMap.find(newid);
std::vector<int> ids = it->second.getNeighbors();
//either way we need to check neighbors of the bucket if they don't exist
if(it->second.getNeighbors().size() != 26){//26 surrounding boxes total
int i = -1;
while(i <= 1){
int j = -1;
while(j <= 1){
int k = -1;
while(k <= 1){
int xindex2 = xindex + i;
int yindex2 = yindex + j;
int zindex2 = zindex + k;
int id = ((xindex2*p1) ^ (yindex2*p2) ^ (zindex2*p3)); //instantiate neighbor ids
//if(this->hashMap.find(id) != this->hashMap.end()){//such a bucket exists
//do nothing
//}
//else{//the bucket does not exist
if (!(i==0 && j==0 && k==0)){
//this->hashMap[id] = Bucket(Point3D(xindex2, yindex2, zindex2));
ids.push_back(id);
}
//}
k++;
}
j++;
}
i++;
}
it->second.setNeighbors(ids);
}
//delete old particle
if(this->hashMap.find(thisid) != this->hashMap.end()){//such a bucket exists
std::map<int,Bucket>::iterator it;
it = this->hashMap.find(thisid);
std::vector<Particle> list = it->second.getContents();
unsigned int d = 0;
while(d < list.size()){
if(list[d].getPosition().getX() == OLDx &&
list[d].getPosition().getY() == OLDy &&
list[d].getPosition().getZ() == OLDz){
list.erase(list.begin()+d);
}
d++;
}
/*it->second.setContents(list);
//update the list oldposition
it = this->hashMap.find(newid);
list = it->second.getContents();*/
d = 0;
while(d < list.size()){
if(list[d].getOldPosition().getX() == OLDx &&
list[d].getOldPosition().getY() == OLDy &&
list[d].getOldPosition().getZ() == OLDz){
list[d].setOldPosition(Point3D(x,y,z));
}
d++;
}
it->second.setContents(list);
}
else{//the bucket does not exist
//uhHHH?????? there should be an old particle so it shouldn't go in here
cout<<"ERROR: WEIRD CS ANOMALY"<<endl;
int breakpoint = 0;
cin >> breakpoint;
exit(1000);
}
}
else{
//cout<<"NO BUCKET CHANGE"<<endl;
//update the list
//std::map<int,Bucket>::iterator it;
//it = this->hashMap.find(thisid);
std::vector<Particle> list = this->hashMap.find(thisid)->second.getContents();
unsigned int d = 0;
while(d < list.size()){
if(list[d].getPosition().getX() == OLDx &&
list[d].getPosition().getY() == OLDy &&
list[d].getPosition().getZ() == OLDz){
list[d]=partc;
}
d++;
}
this->hashMap.find(thisid)->second.setContents(list);
}
c++;
//}
//}
return partc;
}