| 1 |
///////////////////////////////////////////////////////////// |
| 2 |
// // |
| 3 |
// Copyright (c) 2007-2017 by The University of Queensland // |
| 4 |
// Centre for Geoscience Computing // |
| 5 |
// http://earth.uq.edu.au/centre-geoscience-computing // |
| 6 |
// // |
| 7 |
// Primary Business: Brisbane, Queensland, Australia // |
| 8 |
// Licensed under the Open Software License version 3.0 // |
| 9 |
// http://www.apache.org/licenses/LICENSE-2.0 // |
| 10 |
// // |
| 11 |
///////////////////////////////////////////////////////////// |
| 12 |
|
| 13 |
#include "CircMNTable3D.h" |
| 14 |
|
| 15 |
// --- System includes --- |
| 16 |
#include <cmath> |
| 17 |
|
| 18 |
using std::floor; |
| 19 |
|
| 20 |
CircMNTable3D::CircMNTable3D() |
| 21 |
{} |
| 22 |
|
| 23 |
/*! |
| 24 |
Construct CircMNTable3D. Just calls MNTable3D constructor. |
| 25 |
|
| 26 |
\param MinPt minimum point (z component ignored) |
| 27 |
\param MaxPt maximum point (z component ignored) |
| 28 |
\param cd cell dimension |
| 29 |
\param ngroups initial number of particle groups |
| 30 |
*/ |
| 31 |
CircMNTable3D::CircMNTable3D(const Vector3& MinPt,const Vector3& MaxPt,double cd,unsigned int ngroups): |
| 32 |
MNTable3D(MinPt, MaxPt, cd, ngroups) |
| 33 |
{ |
| 34 |
// check if grid spacing fits size in circular direction: |
| 35 |
double nx=(MaxPt-MinPt).X()/m_celldim; |
| 36 |
// error message if not |
| 37 |
if(nx!=floor(nx)){ |
| 38 |
std::cout << "WARNING! grid spacing " << m_celldim << " doesn't fit periodic x-dimension " << (MaxPt-MinPt).X() << std::endl; |
| 39 |
} |
| 40 |
m_shift_x=Vector3((m_max_pt-m_min_pt).X(),0.0,0.0); |
| 41 |
set_x_circ(); |
| 42 |
} |
| 43 |
|
| 44 |
/*! |
| 45 |
Destruct CircMNTable3D. Just calls MNTable3D destructor. |
| 46 |
*/ |
| 47 |
CircMNTable3D::~CircMNTable3D() |
| 48 |
{} |
| 49 |
|
| 50 |
/*! |
| 51 |
set circularity of x-dimension to 1 |
| 52 |
*/ |
| 53 |
void CircMNTable3D::set_x_circ() |
| 54 |
{ |
| 55 |
m_x_periodic=1; |
| 56 |
} |
| 57 |
|
| 58 |
int CircMNTable3D::getXIndex(const Vector3& Pos) const |
| 59 |
{ |
| 60 |
return int(floor((Pos.x()-m_origin.x())/m_celldim)); |
| 61 |
} |
| 62 |
|
| 63 |
int CircMNTable3D::getYIndex(const Vector3& Pos) const |
| 64 |
{ |
| 65 |
return int(floor((Pos.y()-m_origin.y())/m_celldim)); |
| 66 |
} |
| 67 |
|
| 68 |
int CircMNTable3D::getZIndex(const Vector3& Pos) const |
| 69 |
{ |
| 70 |
return int(floor((Pos.z()-m_origin.z())/m_celldim)); |
| 71 |
} |
| 72 |
|
| 73 |
int CircMNTable3D::getFullIndex(const Vector3& Pos) const |
| 74 |
{ |
| 75 |
int ix=int(floor((Pos.x()-m_origin.x())/m_celldim)); |
| 76 |
int iy=int(floor((Pos.y()-m_origin.y())/m_celldim)); |
| 77 |
int iz=int(floor((Pos.z()-m_origin.z())/m_celldim)); |
| 78 |
|
| 79 |
return idx(ix,iy,iz); |
| 80 |
} |
| 81 |
|
| 82 |
/*! |
| 83 |
get the cell index for a given position |
| 84 |
|
| 85 |
\param Pos the position |
| 86 |
\return the cell index if Pos is inside the table, -1 otherwise |
| 87 |
*/ |
| 88 |
int CircMNTable3D::getIndex(const Vector3& Pos) const |
| 89 |
{ |
| 90 |
int ret; |
| 91 |
|
| 92 |
int ix=int(floor((Pos.x()-m_origin.x())/m_celldim)); |
| 93 |
int iy=int(floor((Pos.y()-m_origin.y())/m_celldim)); |
| 94 |
int iz=int(floor((Pos.z()-m_origin.z())/m_celldim)); |
| 95 |
|
| 96 |
// check if pos is in table excl. padding |
| 97 |
if((ix>=0) && (ix<=m_nx-1) && (iy>0) && (iy<m_ny-1) && (iz>0) && (iz<m_nz-1)){ |
| 98 |
ret=idx(ix,iy,iz); |
| 99 |
} else { |
| 100 |
ret=-1; |
| 101 |
} |
| 102 |
|
| 103 |
return ret; |
| 104 |
} |
| 105 |
|
| 106 |
/*! |
| 107 |
Insert sphere. Insert clone into other side of Table. |
| 108 |
|
| 109 |
\param S the Sphere |
| 110 |
\param gid the group id |
| 111 |
*/ |
| 112 |
bool CircMNTable3D::insert(const Sphere& S,unsigned int gid) |
| 113 |
{ |
| 114 |
bool res; |
| 115 |
|
| 116 |
int id=getIndex(S.Center()); |
| 117 |
int idx=getXIndex(S.Center()); |
| 118 |
|
| 119 |
if((id!=-1) && (idx>0) && (idx<m_nx-1) && (gid<m_ngroups)){ // valid index |
| 120 |
// insert sphere |
| 121 |
m_data[id].insert(S,gid); |
| 122 |
res=true; |
| 123 |
int xidx=getXIndex(S.Center()); |
| 124 |
// insert clone |
| 125 |
if (xidx==1){ |
| 126 |
Sphere SClone=S; |
| 127 |
SClone.shift(m_shift_x); |
| 128 |
int clone_id=getFullIndex(SClone.Center()); |
| 129 |
m_data[clone_id].insert(SClone,gid); |
| 130 |
} else if (xidx==m_nx-2){ |
| 131 |
Sphere SClone=S; |
| 132 |
SClone.shift(-1.0*m_shift_x); |
| 133 |
int clone_id=getFullIndex(SClone.Center()); |
| 134 |
m_data[clone_id].insert(SClone,gid); |
| 135 |
} |
| 136 |
} else { |
| 137 |
res=false; |
| 138 |
} |
| 139 |
|
| 140 |
return res; |
| 141 |
} |
| 142 |
/*! |
| 143 |
check if sphere is insertable |
| 144 |
|
| 145 |
\param S the Sphere |
| 146 |
\param gid the group id |
| 147 |
*/ |
| 148 |
bool CircMNTable3D::checkInsertable(const Sphere& S,unsigned int gid) |
| 149 |
{ |
| 150 |
bool orig_insertable=false; |
| 151 |
bool clone_insertable=false; |
| 152 |
bool needs_cloning=false; |
| 153 |
int id=getIndex(S.Center()); |
| 154 |
int idx=getXIndex(S.Center()); |
| 155 |
Sphere SClone=S; |
| 156 |
|
| 157 |
if((id!=-1) && (idx>0) && (idx<m_nx-1) && (gid<m_ngroups)){ // original position is inside "core" of the table |
| 158 |
// check if original is insertable |
| 159 |
multimap<double,const Sphere*> close_spheres=getSpheresFromGroupNear(S.Center(),S.Radius()-s_small_value,gid); |
| 160 |
orig_insertable=(close_spheres.size()==0); |
| 161 |
// calculate clone location |
| 162 |
int xidx=getXIndex(S.Center()); |
| 163 |
// check if clone is insertable |
| 164 |
if (xidx==1){ // original in x-min slice of the table -> shift clone to +x |
| 165 |
needs_cloning=true; |
| 166 |
SClone.shift(m_shift_x); |
| 167 |
multimap<double,const Sphere*> close_spheres=getSpheresFromGroupNear(SClone.Center(),SClone.Radius()-s_small_value,gid); |
| 168 |
clone_insertable=(close_spheres.size()==0); |
| 169 |
} else if (xidx==m_nx-2){ // original in x-max slice of the table -> shift clone to -x |
| 170 |
needs_cloning=true; |
| 171 |
SClone.shift(-1.0*m_shift_x); |
| 172 |
multimap<double,const Sphere*> close_spheres=getSpheresFromGroupNear(SClone.Center(),SClone.Radius()-s_small_value,gid); |
| 173 |
clone_insertable=(close_spheres.size()==0); |
| 174 |
} // original not in end slices -> no cloning |
| 175 |
} |
| 176 |
|
| 177 |
return (orig_insertable && (clone_insertable || (!needs_cloning))); // original is insertable and clone is either insertable or not needed |
| 178 |
} |
| 179 |
|
| 180 |
/*! |
| 181 |
Insert sphere if it doesn't collide with other spheres. Insert clone into other side of Table. |
| 182 |
|
| 183 |
\param S the Sphere |
| 184 |
\param gid the group id |
| 185 |
*/ |
| 186 |
bool CircMNTable3D::insertChecked(const Sphere& S,unsigned int gid,double tol) |
| 187 |
{ |
| 188 |
bool res; |
| 189 |
bool orig_insertable=false; |
| 190 |
bool clone_insertable=false; |
| 191 |
bool needs_cloning=false; |
| 192 |
int id=getIndex(S.Center()); |
| 193 |
int idx=getXIndex(S.Center()); |
| 194 |
Sphere SClone=S; |
| 195 |
|
| 196 |
tol+=s_small_value; |
| 197 |
if((id!=-1) && (idx>0) && (idx<m_nx-1) && (gid<m_ngroups)){ // original position is inside "core" of the table |
| 198 |
// check if original is insertable |
| 199 |
multimap<double,const Sphere*> close_spheres=getSpheresFromGroupNear(S.Center(),S.Radius()-tol,gid); |
| 200 |
orig_insertable=(close_spheres.size()==0); // insertable if there are no spheres too close |
| 201 |
// calculate clone location |
| 202 |
int xidx=getXIndex(S.Center()); |
| 203 |
// check if clone is insertable |
| 204 |
if (xidx==1){ // original in x-min slice of the table -> shift clone to +x |
| 205 |
needs_cloning=true; |
| 206 |
SClone.shift(m_shift_x); |
| 207 |
multimap<double,const Sphere*> close_spheres=getSpheresFromGroupNear(SClone.Center(),SClone.Radius()-tol,gid); |
| 208 |
clone_insertable=(close_spheres.size()==0); |
| 209 |
} else if (xidx==m_nx-2){ // original in x-max slice of the table -> shift clone to -x |
| 210 |
needs_cloning=true; |
| 211 |
SClone.shift(-1.0*m_shift_x); |
| 212 |
multimap<double,const Sphere*> close_spheres=getSpheresFromGroupNear(SClone.Center(),SClone.Radius()-tol,gid); |
| 213 |
clone_insertable=(close_spheres.size()==0); |
| 214 |
} // original not in end slices -> no cloning |
| 215 |
} |
| 216 |
|
| 217 |
if(orig_insertable && (clone_insertable || (!needs_cloning))){ // if origin is insertable and clone is either insertable or not needed |
| 218 |
m_data[id].insert(S,gid); // insert original |
| 219 |
if (needs_cloning) { // if needed, insert clone |
| 220 |
int clone_id=getFullIndex(SClone.Center()); |
| 221 |
m_data[clone_id].insert(SClone,gid); |
| 222 |
} |
| 223 |
res=true; |
| 224 |
} else { |
| 225 |
res=false; |
| 226 |
} |
| 227 |
|
| 228 |
return res; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
/*! |
| 233 |
generate bonds between particles of a group |
| 234 |
|
| 235 |
\param gid the group ID |
| 236 |
\param tol max. difference between bond length and equilibrium dist. |
| 237 |
\param btag bond tag |
| 238 |
*/ |
| 239 |
void CircMNTable3D::generateBonds(int gid,double tol,int btag) |
| 240 |
{ |
| 241 |
std::cout << "MNTable3D::generateBonds( " << gid << " , " << tol << " , " << btag << " )" << std::endl; // loop over all cells |
| 242 |
for(int i=0;i<m_nx-1;i++){ |
| 243 |
for(int j=1;j<m_ny-1;j++){ |
| 244 |
for(int k=1;k<m_nz-1;k++){ |
| 245 |
int id=idx(i,j,k); |
| 246 |
// loop over "upper" neighbors of each cell |
| 247 |
for(int ii=-1;ii<=1;ii++){ |
| 248 |
for(int jj=-1;jj<=1;jj++){ |
| 249 |
for(int kk=-1;kk<=1;kk++){ |
| 250 |
int id2=idx(i+ii,j+jj,k+kk); |
| 251 |
vector<pair<int,int> > bonds; |
| 252 |
if(((ii+jj+kk)==0)){ // intra-cell, not for boundary |
| 253 |
bonds=m_data[id].getBonds(gid,tol); |
| 254 |
} else if(id2>id){ // inter-cell |
| 255 |
bonds=m_data[id].getBonds(gid,tol,m_data[id2]); |
| 256 |
} |
| 257 |
for(vector<pair<int,int> >::iterator iter=bonds.begin(); |
| 258 |
iter!=bonds.end(); |
| 259 |
iter++){ |
| 260 |
m_bonds[btag].insert(*iter); |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
/*! |
| 271 |
generate bonds between particles of a group |
| 272 |
|
| 273 |
\param gid the group ID |
| 274 |
\param tol max. difference between bond length and equilibrium dist. |
| 275 |
\param btag1 tag for bonds within clusters (same particle tag) |
| 276 |
\param btag2 tag for bonds betweem clusters (different particle tag) |
| 277 |
*/ |
| 278 |
void CircMNTable3D::generateClusterBonds(int gid,double tol,int btag1, int btag2) |
| 279 |
{ |
| 280 |
for(int i=0;i<m_nx-1;i++){ |
| 281 |
for(int j=1;j<m_ny-1;j++){ |
| 282 |
for(int k=1;k<m_nz-1;k++){ |
| 283 |
int id=idx(i,j,k); |
| 284 |
// loop over "upper" neighbors of each cell |
| 285 |
for(int ii=-1;ii<=1;ii++){ |
| 286 |
for(int jj=-1;jj<=1;jj++){ |
| 287 |
for(int kk=-1;kk<=1;kk++){ |
| 288 |
int id2=idx(i+ii,j+jj,k+kk); |
| 289 |
vector<pair<int,int> > same_bonds; |
| 290 |
vector<pair<int,int> > diff_bonds; |
| 291 |
if((id==id2) && (id!=-1)){ // intra-cell, not for boundary |
| 292 |
same_bonds=m_data[id].getBondsSame(gid,tol); |
| 293 |
diff_bonds=m_data[id].getBondsDiff(gid,tol); |
| 294 |
} else if((id2>id) && (id!=-1) && (id2!=-1) ){ // inter-cell |
| 295 |
same_bonds=m_data[id].getBondsSame(gid,tol,m_data[id2]); |
| 296 |
diff_bonds=m_data[id].getBondsDiff(gid,tol,m_data[id2]); |
| 297 |
} |
| 298 |
// insert intra-cluster bonds |
| 299 |
for(vector<pair<int,int> >::iterator iter=same_bonds.begin(); |
| 300 |
iter!=same_bonds.end(); |
| 301 |
iter++){ |
| 302 |
if(m_bonds[btag1].find(*iter)==m_bonds[btag1].end()){ |
| 303 |
m_bonds[btag1].insert(*iter); |
| 304 |
} |
| 305 |
} |
| 306 |
// insert inter-cluster bonds |
| 307 |
for(vector<pair<int,int> >::iterator iter=diff_bonds.begin(); |
| 308 |
iter!=diff_bonds.end(); |
| 309 |
iter++){ |
| 310 |
if(m_bonds[btag2].find(*iter)==m_bonds[btag2].end()){ |
| 311 |
m_bonds[btag2].insert(*iter); |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
} |