Debian Codegraph @

src/MNTable3D.cc

raw
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 "MNTable3D.h"
14
15 // --- STL includes ---
16 #include <utility>
17
18 using namespace std;
19 using std::make_pair;
20
21 // --- System includes ---
22 #include <cmath>
23
24 using std::floor;
25 using std::endl;
26 using std::ceil;
27
28 // --- IO includes ---
29 #include <iostream>
30 #include <fstream>
31
32 using std::ifstream;
33
34 int MNTable3D::s_output_style=0;
35 double MNTable3D::s_small_value=1e-7;
36
37 MNTable3D::MNTable3D():m_write_prec(10)
38 {}
39
40 /*!
41 construct MNTable3D
42
43 \param MinPt minimum point (z component ignored)
44 \param MaxPt maximum point (z component ignored)
45 \param cd cell dimension
46 \param ngroups initial number of particle groups
47 */
48 MNTable3D::MNTable3D(const Vector3& MinPt,const Vector3& MaxPt,double cd,unsigned int ngroups)
49 {
50 m_celldim=cd;
51 m_ngroups=ngroups;
52 m_min_pt=MinPt;
53
54 // get number of cells
55 Vector3 req_size=MaxPt-MinPt; // requested size
56 m_nx=int(ceil(req_size.X()/m_celldim))+2;
57 m_ny=int(ceil(req_size.Y()/m_celldim))+2;
58 m_nz=int(ceil(req_size.Z()/m_celldim))+2;
59 std::cout << "nx,ny,nz: " << m_nx << " , " << m_ny << " , " << m_nz << std::endl;
60
61 // add padding
62 m_origin=MinPt-Vector3(m_celldim,m_celldim,m_celldim);
63 m_max_pt=m_min_pt+Vector3(m_celldim*(m_nx-2),m_celldim*(m_ny-2),m_celldim*(m_nz-2));
64
65
66 // allocate cell array
67 m_data=new MNTCell[m_nx*m_ny*m_nz];
68 for(int i=0;i<m_nx*m_ny*m_nz;i++) {
69 m_data[i].SetNGroups(m_ngroups);
70 }
71 set_x_circ();
72 set_y_circ();
73 set_z_circ();
74
75 // setup default bounding box handling
76 m_bbx_tracking=false; // bounding box tracking off
77 m_has_tight_bbx=false; // tight bounding box has never been calculated
78 m_write_tight_bbx=false; // MinPt / MaxPt are written to the geo file
79 m_min_tbbx=MaxPt;
80 m_max_tbbx=MinPt; // dummy bbx corners
81
82 // default output precision
83 m_write_prec=10;
84
85 // no blocked writing
86 m_is_writing_blocks=false;
87 }
88
89 /*!
90 set circularity of x-dimension to 0
91 */
92 void MNTable3D::set_x_circ()
93 {
94 m_x_periodic=0;
95 }
96
97 /*!
98 set circularity of y-dimension to 0
99 */
100 void MNTable3D::set_y_circ()
101 {
102 m_y_periodic=0;
103 }
104
105 /*!
106 set circularity of z-dimension to 0
107 */
108 void MNTable3D::set_z_circ()
109 {
110 m_z_periodic=0;
111 }
112
113 /*!
114 destruct MNTable3D
115 */
116 MNTable3D::~MNTable3D()
117 {
118 if(m_data!=NULL) delete[] m_data;
119 }
120
121 /*!
122 Load particle from raw file: x y z r
123
124 \param filename the name of the input file
125 \param scale factor by which all data are scaled before insertion
126 \param tol allowed overlap
127 */
128 bool MNTable3D::insertFromRawFile(const string& filename,double scale,double tol)
129 {
130 ifstream infile(filename.c_str());
131 double x,y,z,r;
132 int count=0;
133
134 while(!infile.eof()){
135 infile >> x >> y >> z >> r;
136
137 bool can_insert=insertChecked(Sphere(Vector3(x*scale,y*scale,z*scale),r*scale),0,tol);
138 if(!can_insert){
139 std::cout << "couldn't insert particle : " << Vector3(x*scale,y*scale,z*scale) << " r= " << r*scale << std::endl;
140 } else {
141 count++;
142 }
143 }
144 std::cout << "inserted particle count:" << count << std::endl;
145
146 infile.close();
147
148 return true;
149 }
150
151 /*!
152 Increase the number of particle groups. Do nothing if the number is already large enough
153
154 \param ngroups the new number of particle groups
155 */
156 void MNTable3D::GrowNGroups(unsigned int ngroups)
157 {
158 if(ngroups>m_ngroups){
159 m_ngroups=ngroups;
160 for(int i=0;i<m_nx*m_ny*m_nz;i++) {
161 m_data[i].SetNGroups(m_ngroups);
162 }
163 }
164 }
165
166 /*!
167 get the cell index for a given position
168
169 \param Pos the position
170 \return the cell index if Pos is inside the table, -1 otherwise
171 */
172 int MNTable3D::getIndex(const Vector3& Pos) const
173 {
174 int ret;
175
176 int ix=int(floor((Pos.x()-m_origin.x())/m_celldim));
177 int iy=int(floor((Pos.y()-m_origin.y())/m_celldim));
178 int iz=int(floor((Pos.z()-m_origin.z())/m_celldim));
179
180 // check if pos is in table excl. padding
181 if((ix>0) && (ix<m_nx-1) && (iy>0) && (iy<m_ny-1) && (iz>0) && (iz<m_nz-1)){
182 ret=idx(ix,iy,iz);
183 } else {
184 ret=-1;
185 }
186
187 return ret;
188 }
189
190 /*!
191 insert sphere
192
193 \param S the Sphere
194 \param gid the group id
195 */
196 bool MNTable3D::insert(const Sphere& S,unsigned int gid)
197 {
198 bool res;
199
200 int id=this->getIndex(S.Center());
201
202 if((id!=-1) && (gid<m_ngroups)){
203 m_data[id].insert(S,gid);
204 res=true;
205 if(m_bbx_tracking){ // if bounding box tracking is on, update bbx
206 Vector3 pmin=S.Center()-Vector3(S.Radius(),S.Radius(),S.Radius());
207 Vector3 pmax=S.Center()+Vector3(S.Radius(),S.Radius(),S.Radius());
208 m_min_tbbx.X()=std::min(m_min_tbbx.X(),pmin.X());
209 m_min_tbbx.Y()=std::min(m_min_tbbx.Y(),pmin.Y());
210 m_min_tbbx.Z()=std::min(m_min_tbbx.Z(),pmin.Z());
211 m_max_tbbx.X()=std::max(m_max_tbbx.X(),pmax.X());
212 m_max_tbbx.Y()=std::max(m_max_tbbx.Y(),pmax.Y());
213 m_max_tbbx.Z()=std::max(m_max_tbbx.Z(),pmax.Z());
214 }
215 } else {
216 res=false;
217 }
218
219 return res;
220 }
221
222
223 /*!
224 insert sphere if it doesn't collide with other spheres
225
226 \param S the Sphere
227 \param gid the group id
228 */
229 bool MNTable3D::insertChecked(const Sphere& S,unsigned int gid,double tol)
230 {
231 bool res;
232
233 int id=this->getIndex(S.Center());
234
235 tol+=s_small_value;
236 if((id!=-1) && (gid<m_ngroups)){
237 multimap<double,const Sphere*> close_spheres=getSpheresFromGroupNear(S.Center(),S.Radius()-tol,gid);
238 if(close_spheres.size()==0){
239 m_data[id].insert(S,gid);
240 res=true;
241 if(m_bbx_tracking){ // if bounding box tracking is on, update bbx
242 Vector3 pmin=S.Center()-Vector3(S.Radius(),S.Radius(),S.Radius());
243 Vector3 pmax=S.Center()+Vector3(S.Radius(),S.Radius(),S.Radius());
244 m_min_tbbx.X()=std::min(m_min_tbbx.X(),pmin.X());
245 m_min_tbbx.Y()=std::min(m_min_tbbx.Y(),pmin.Y());
246 m_min_tbbx.Z()=std::min(m_min_tbbx.Z(),pmin.Z());
247 m_max_tbbx.X()=std::max(m_max_tbbx.X(),pmax.X());
248 m_max_tbbx.Y()=std::max(m_max_tbbx.Y(),pmax.Y());
249 m_max_tbbx.Z()=std::max(m_max_tbbx.Z(),pmax.Z());
250 }
251 } else res=false;
252 } else {
253 res=false;
254 }
255
256 return res;
257 }
258
259 /*!
260 get the closest spheres to a given point
261
262 \param p the point
263 \param nmax the maximum number of spheres returned
264 */
265 const multimap<double,const Sphere*> MNTable3D::getSpheresClosestTo(const Vector3& p,unsigned int nmax) const
266 {
267 multimap<double,const Sphere*> res;
268
269 for(int i=-1;i<=1;i++){
270 for(int j=-1;j<=1;j++){
271 for(int k=-1;k<=1;k++){
272 Vector3 pos=p+Vector3(double(i)*m_celldim,double(j)*m_celldim,double(k)*m_celldim);
273 int idx=this->getIndex(pos);
274 if(idx!=-1){
275 multimap<double,const Sphere*> smap=m_data[idx].getSpheresClosestTo(p,nmax);
276 res.insert(smap.begin(),smap.end());
277 }
278 }
279 }
280 }
281
282 return res;
283 }
284
285 /*!
286 get all spheres from one group within a given distance to a point
287
288 \param P the point
289 \param d max. distance between the point and the surface of the sphere
290 \param gid the group id
291
292 \warning no check for validity of gid
293 */
294 const multimap<double,const Sphere*> MNTable3D::getSpheresFromGroupNear(const Vector3& P,double d,int gid) const
295 {
296 multimap<double,const Sphere*> res;
297
298 for(int i=-1;i<=1;i++){
299 for(int j=-1;j<=1;j++){
300 for(int k=-1;k<=1;k++){
301 Vector3 np=P+Vector3(double(i)*m_celldim,double(j)*m_celldim,double(k)*m_celldim);
302 int idx=this->getIndex(np);
303 if(idx!=-1){
304 multimap<double,const Sphere*> v=m_data[idx].getSpheresFromGroupNear(P,d,gid);
305 res.insert(v.begin(),v.end());
306 }
307 }
308 }
309 }
310
311 return res;
312 }
313
314 /*!
315 get the sphere within a given group closest to given sphere
316
317 \param s the sphere
318 \param gid the group
319 */
320 const Sphere* MNTable3D::getClosestSphereFromGroup(const Sphere& s,int gid) const
321 {
322 const Sphere* res=NULL;
323 int max_dim=m_nx>m_ny ? m_nx : m_ny;
324 max_dim=m_nz>max_dim ? m_nz : max_dim;
325 int found=max_dim;
326 double dist=max_dim*m_celldim;
327
328 // search within the same cell
329 int idx=getIndex(s.Center());
330 const Sphere* v=m_data[idx].getClosestSphereFromGroup(s,gid,2.0*max_dim);
331 if(v!=NULL){
332 double d=(v->Center()-s.Center()).norm()-s.Radius();
333 found=1;
334 if(d<dist){
335 res=v;
336 dist=d;
337 }
338 }
339
340 // search outside origin cell
341 int range=1;
342 while((range<max_dim) && (range<found+1)){
343 for(int i=-range;i<=range;i++){
344 for(int j=-range;j<=range;j++){
345 for(int k=-range;k<=range;k++){
346 Vector3 np=s.Center()+Vector3(double(i)*m_celldim,double(j)*m_celldim,double(k)*m_celldim);
347 int idx=getIndex(np);
348 if(idx!=-1){
349 const Sphere* v=m_data[idx].getClosestSphereFromGroup(s,gid,dist);
350 if(v!=NULL){
351 double d=(v->Center()-s.Center()).norm()-s.Radius();
352 found=range+1;
353 if(d<dist){
354 res=v;
355 dist=d;
356 }
357 }
358 }
359 }
360 }
361 }
362 range++;
363
364 }
365 return res;
366 }
367
368 /*!
369 convenience function which calls getClosestSphereFromGroup and returns the
370 tag of the found sphere or -1 if none is found
371
372 \param s the sphere
373 \param gid the group
374 */
375 int MNTable3D::getTagOfClosestSphereFromGroup(const Sphere& s,int gid) const
376 {
377 int res;
378
379 const Sphere* sp=getClosestSphereFromGroup(s,gid);
380 if(sp!=NULL){
381 res=sp->Tag();
382 }else{
383 res=-1;
384 }
385
386 return res;
387 }
388
389 /*!
390 Get the sphere within a given group closest to given sphere, using anisotropic
391 weighted distance.
392
393 \param s the sphere
394 \param gid the group
395 \param wx weighting of the x-component of the distance
396 \param wy weighting of the y-component of the distance
397 \param wz weighting of the z-component of the distance
398 */
399 const Sphere* MNTable3D::getClosestSphereFromGroup(const Sphere& s,int gid,double wx, double wy, double wz) const
400 {
401 const Sphere* res=NULL;
402 int max_dim=m_nx>m_ny ? m_nx : m_ny;
403 max_dim=m_nz>max_dim ? m_nz : max_dim;
404 int found=max_dim;
405 double dist=max_dim*m_celldim;
406
407 // search within the same cell
408 int idx=getIndex(s.Center());
409 const Sphere* v=m_data[idx].getClosestSphereFromGroup(s,gid,2.0*max_dim,wx,wy,wz);
410 if(v!=NULL){
411 double d=(v->Center()-s.Center()).wnorm2(wx,wy,wz);
412 found=1;
413 if(d<dist){
414 res=v;
415 dist=d;
416 }
417 }
418
419 // search outside origin cell
420 int range=1;
421 while((range<max_dim) && (range<found+1)){
422 for(int i=-range;i<=range;i++){
423 for(int j=-range;j<=range;j++){
424 for(int k=-range;k<=range;k++){
425 Vector3 np=s.Center()+Vector3(double(i)*m_celldim,double(j)*m_celldim,double(k)*m_celldim);
426 int idx=getIndex(np);
427 if(idx!=-1){
428 const Sphere* v=m_data[idx].getClosestSphereFromGroup(s,gid,dist,wx,wy,wz);
429 if(v!=NULL){
430 double d=(v->Center()-s.Center()).wnorm2(wx,wy,wz);
431 if(d<dist){
432 res=v;
433 dist=d;
434 }
435 if(found==max_dim) { // 1st find -> need to limit search range
436 found=range+1;
437 }
438 }
439 }
440 }
441 }
442 }
443 range++;
444
445 }
446 return res;
447 }
448
449 /*!
450 Convenience function which calls getClosestSphereFromGroup, using anisotropic
451 weighted distance and returns the tag of the found sphere or -1 if none is found.
452
453 \param s the sphere
454 \param gid the group
455 \param wx weighting of the x-component of the distance
456 \param wy weighting of the y-component of the distance
457 \param wz weighting of the z-component of the distance
458 */
459 int MNTable3D::getTagOfClosestSphereFromGroup(const Sphere& s,int gid, double wx, double wy, double wz) const
460 {
461 int res;
462
463 const Sphere* sp=getClosestSphereFromGroup(s,gid,wx,wy,wz);
464 if(sp!=NULL){
465 res=sp->Tag();
466 }else{
467 res=-1;
468 }
469
470 return res;
471 }
472
473 /*!
474 check if sphere is insertable
475
476 \param S the Sphere
477 \param gid the group id
478 */
479 bool MNTable3D::checkInsertable(const Sphere& S,unsigned int gid)
480 {
481 bool res;
482
483 int id=this->getIndex(S.Center());
484 if((id!=-1) && (gid<m_ngroups)){
485 multimap<double,const Sphere*> close_spheres=getSpheresFromGroupNear(S.Center(),S.Radius()-s_small_value,gid);
486 if(close_spheres.size()==0){
487 res=true;
488 } else {
489 res=false;
490 // for(map<double,const Sphere*>::const_iterator iter=close_spheres.begin();
491 // iter!=close_spheres.end();
492 // iter++){
493 // std::cout << iter->first << " | " << *(iter->second) << std::endl;
494 // }
495 }
496 } else {
497 res=false;
498 }
499
500 return res;
501 }
502
503 /*
504 get the sum of the area/volume of all particles in a group
505
506 \param gid the group id
507 */
508 double MNTable3D::getSumVolume(int gid)
509 {
510 double res=0.0;
511
512 for(int i=1;i<m_nx-1;i++){
513 for(int j=1;j<m_ny-1;j++){
514 for(int k=1;k<m_nz-1;k++){
515 res+=m_data[idx(i,j,k)].getSumVolume3D(gid);
516 }
517 }
518 }
519
520 return res;
521 }
522
523 /*!
524 generate bonds between particles of a group
525
526 \param gid the group ID
527 \param tol max. difference between bond length and equilibrium dist.
528 \param btag bond tag
529 */
530 void MNTable3D::generateBonds(int gid,double tol,int btag)
531 {
532 std::cout << "MNTable3D::generateBonds( " << gid << " , " << tol << " , " << btag << " )" << std::endl; // loop over all cells
533 for(int i=1;i<m_nx-1;i++){
534 for(int j=1;j<m_ny-1;j++){
535 for(int k=1;k<m_nz-1;k++){
536 int id=idx(i,j,k);
537 // loop over "upper" neighbors of each cell
538 for(int ii=-1;ii<=1;ii++){
539 for(int jj=-1;jj<=1;jj++){
540 for(int kk=-1;kk<=1;kk++){
541 int id2=idx(i+ii,j+jj,k+kk);
542 vector<pair<int,int> > bonds;
543 if(((ii+jj+kk)==0)){ // intra-cell, not for boundary
544 bonds=m_data[id].getBonds(gid,tol);
545 } else if(id2>id){ // inter-cell
546 bonds=m_data[id].getBonds(gid,tol,m_data[id2]);
547 }
548 for(vector<pair<int,int> >::iterator iter=bonds.begin();
549 iter!=bonds.end();
550 iter++){
551 m_bonds[btag].insert(*iter);
552 }
553 }
554 }
555 }
556 }
557 }
558 }
559 }
560
561 /*!
562 generate bonds between particles with given tags of a group
563
564 \param gid the group ID
565 \param tol max. difference between bond length and equilibrium dist.
566 \param btag bond tag
567 \param tag1
568 \param tag2
569 */
570 void MNTable3D::generateBondsTagged(int gid,double tol,int btag,int tag1,int tag2)
571 {
572 std::cout << "MNTable3D::generateBondsTagged( " << gid << " , " << tol << " , " << btag << " , " << tag1 << " , " << tag2 << " )" << std::endl; // loop over all cells
573 for(int i=1;i<m_nx-1;i++){
574 for(int j=1;j<m_ny-1;j++){
575 for(int k=1;k<m_nz-1;k++){
576 int id=idx(i,j,k);
577 // loop over "upper" neighbors of each cell
578 for(int ii=-1;ii<=1;ii++){
579 for(int jj=-1;jj<=1;jj++){
580 for(int kk=-1;kk<=1;kk++){
581 int id2=idx(i+ii,j+jj,k+kk);
582 vector<pair<int,int> > bonds;
583 if(((ii+jj+kk)==0)){ // intra-cell, not for boundary
584 bonds=m_data[id].getBondsTagged(gid,tol,tag1,tag2);
585 } else if(id2>id){ // inter-cell
586 bonds=m_data[id].getBondsTagged(gid,tol,m_data[id2],tag1,tag2);
587 }
588 for(vector<pair<int,int> >::iterator iter=bonds.begin();
589 iter!=bonds.end();
590 iter++){
591 m_bonds[btag].insert(*iter);
592 }
593 }
594 }
595 }
596 }
597 }
598 }
599 }
600
601 /*!
602 generate bonds between particles with identical particle tag of a group with a given
603 probablility (0..1)
604
605 \param gid the group ID
606 \param tol max. difference between bond length and equilibrium dist.
607 \param prob probablility of bond generation
608 \param btag bond tag
609 \param ptag particle tag
610 \param mask
611 */
612 void MNTable3D::generateRandomBonds(int gid,double tol, double prob, int btag,int ptag,int mask)
613 {
614 std::cout << "MNTable3D::generateRandomBonds( " << gid << " , " << tol << " , " << prob << " , " << btag << " , " << ptag << " , " << mask << " )" << std::endl;
615 // loop over all cells
616 for(int i=1;i<m_nx-1;i++){
617 for(int j=1;j<m_ny-1;j++){
618 for(int k=1;k<m_nz-1;k++){
619 int id=idx(i,j,k);
620 // loop over "upper" neighbors of each cell
621 for(int ii=-1;ii<=1;ii++){
622 for(int jj=-1;jj<=1;jj++){
623 for(int kk=-1;kk<=1;kk++){
624 int id2=idx(i+ii,j+jj,k+kk);
625 vector<pair<int,int> > bonds;
626 if((id==id2) && (i!=0) && (j!=0)&& (k!=0) ){ // intra-cell, not for boundary
627 bonds=m_data[id].getBonds(gid,tol,ptag,mask);
628 } else if(id2>id){ // inter-cell
629 bonds=m_data[id].getBonds(gid,tol,m_data[id2],ptag,mask);
630 }
631 for(vector<pair<int,int> >::iterator iter=bonds.begin();
632 iter!=bonds.end();
633 iter++){
634 double rn=((double)(rand())/(double)(RAND_MAX));
635 if(rn<prob) m_bonds[btag].insert(*iter);
636 }
637 }
638 }
639 }
640 }
641 }
642 }
643 }
644
645 /*!
646 Generate bonds between clusters of particles of a group, i.e. bonds with one tag between particles with the same ID and bonds with another tag between particles with different ID.
647
648 \param gid the group ID
649 \param tol max. difference between bond length and equilibrium dist.
650 \param btag1 tag for bonds within clusters (same particle tag)
651 \param btag2 tag for bonds betweem clusters (different particle tag)
652 */
653 void MNTable3D::generateClusterBonds(int gid,double tol,int btag1, int btag2)
654 {
655 for(int i=1;i<m_nx-1;i++){
656 for(int j=1;j<m_ny-1;j++){
657 for(int k=1;k<m_nz-1;k++){
658 int id=idx(i,j,k);
659 // loop over "upper" neighbors of each cell
660 for(int ii=-1;ii<=1;ii++){
661 for(int jj=-1;jj<=1;jj++){
662 for(int kk=-1;kk<=1;kk++){
663 int id2=idx(i+ii,j+jj,k+kk);
664 vector<pair<int,int> > same_bonds;
665 vector<pair<int,int> > diff_bonds;
666 if((id==id2) && (i!=0) && (j!=0) && (k!=0)){ // intra-cell, not for boundary
667 same_bonds=m_data[id].getBondsSame(gid,tol);
668 diff_bonds=m_data[id].getBondsDiff(gid,tol);
669 } else if(id2>id){ // inter-cell
670 same_bonds=m_data[id].getBondsSame(gid,tol,m_data[id2]);
671 diff_bonds=m_data[id].getBondsDiff(gid,tol,m_data[id2]);
672 }
673 // insert intra-cluster bonds
674 for(vector<pair<int,int> >::iterator iter=same_bonds.begin();
675 iter!=same_bonds.end();
676 iter++){
677 if(m_bonds[btag1].find(*iter)==m_bonds[btag1].end()){
678 m_bonds[btag1].insert(*iter);
679 }
680 }
681 // insert inter-cluster bonds
682 for(vector<pair<int,int> >::iterator iter=diff_bonds.begin();
683 iter!=diff_bonds.end();
684 iter++){
685 if(m_bonds[btag2].find(*iter)==m_bonds[btag2].end()){
686 m_bonds[btag2].insert(*iter);
687 }
688 }
689 }
690 }
691 }
692 }
693 }
694 }
695 }
696
697 /*!
698 generate bonds taking a list of joint surfaces into account, i.e. bonds which cross one of the surface get tagged differently (according to the tag of the joint)
699
700 \param Joints the set of joint surfaces
701 \param gid the particle group id
702 \param tol the bond generation tolerance, i.e. bonds up to length (equilibrium length*tol) get generated
703 \param tag the tag for the bonds not crossing any surface
704 */
705 void MNTable3D::generateBondsWithJointSet(const TriPatchSet& Joints,int gid,double tol,int tag)
706 {
707 std::cout << "MNTable3D::generateBondsWithJointSet( " << tag << " )" << std::endl;
708 // loop over all cells
709 for(int i=1;i<m_nx-1;i++){
710 for(int j=1;j<m_ny-1;j++){
711 for(int k=1;k<m_nz-1;k++){
712 int id=idx(i,j,k);
713 // loop over "upper" neighbors of each cell
714 for(int ii=-1;ii<=1;ii++){
715 for(int jj=-1;jj<=1;jj++){
716 for(int kk=-1;kk<=1;kk++){
717 int id2=idx(i+ii,j+jj,k+kk);
718 vector<BondWithPos> bonds;
719 if(((ii+jj+kk)==0)){ // intra-cell, not for boundary
720 bonds=m_data[id].getBondsWithPos(gid,tol);
721 } else if(id2>id){ // inter-cell
722 bonds=m_data[id].getBondsWithPos(gid,tol,m_data[id2]);
723 }
724 for(vector<BondWithPos>::iterator iter=bonds.begin();
725 iter!=bonds.end();
726 iter++){
727 // check if bond crosses surface
728 int jtag=Joints.isCrossing(iter->firstPos,iter->secondPos) ;
729 if(jtag==-1){
730 m_bonds[tag].insert(make_pair(iter->first,iter->second));
731 } else {
732 m_bonds[jtag].insert(make_pair(iter->first,iter->second));
733 }
734 }
735 }
736 }
737 }
738 }
739 }
740 }
741 }
742
743 /*!
744 insert bond between particles with given ID
745
746 \param id1 id of 1st particle
747 \param id2 id of 2nd particle
748 \param btag bond tag
749 */
750 void MNTable3D::insertBond(int id1,int id2,int btag)
751 {
752 if(id1<id2){
753 m_bonds[btag].insert(make_pair(id1,id2));
754 } else {
755 m_bonds[btag].insert(make_pair(id2,id1));
756 }
757 }
758
759 /*!
760 remove all currently stored bonds
761 */
762 void MNTable3D::removeBonds()
763 {
764 /* for(map<int,set<pair<int,int> > >::const_iterator iter=T.m_bonds.begin();
765 * iter!=T.m_bonds.end();
766 * iter++){
767 * iter->clear();
768 * }
769 */
770 m_bonds.clear();
771 }
772
773 /*!
774 get vector of pointers to all spheres of a group
775
776 \param gid goup Id
777 */
778 const vector<const Sphere*> MNTable3D::getAllSpheresFromGroup(int gid) const
779 {
780 vector<const Sphere*> res;
781
782 for(int i=1;i<m_nx-1;i++){
783 for(int j=1;j<m_ny-1;j++){
784 for(int k=1;k<m_nz-1;k++){
785 vector<const Sphere*> cr=m_data[idx(i,j,k)].getAllSpheresFromGroup(gid);
786 res.insert(res.end(),cr.begin(),cr.end());
787 }
788 }
789 }
790
791 return res;
792 }
793
794 boost::python::list MNTable3D::getSphereListFromGroup(int gid) const
795 {
796 boost::python::list l;
797 vector<const Sphere*> sphere_vector;
798
799 sphere_vector = getAllSpheresFromGroup(gid);
800
801 for (vector<const Sphere*>::iterator iter=sphere_vector.begin(); iter!= sphere_vector.end(); iter++) {
802 l.append(*(*(iter)));
803 }
804
805 return l;
806 }
807
808 boost::python::list MNTable3D::getSphereListDist(const Vector3& pos, double dist, int gid) const
809 {
810 boost::python::list l;
811 multimap<double,const Sphere*> sphere_map = getSpheresFromGroupNear(pos,dist,gid);
812
813 for (map<double,const Sphere*>::iterator iter=sphere_map.begin();
814 iter!= sphere_map.end();
815 iter++) {
816 l.append(*(iter->second));
817 }
818
819 return l;
820 }
821
822 /*!
823 Set output style
824
825 \param style the output style: 0 = debug, 1 = .geo
826 */
827 void MNTable3D::SetOutputStyle(int style)
828 {
829 MNTable3D::s_output_style=style;
830 }
831
832 /*!
833 Output the content of a MNTable3D to an ostream. The output format depends on the value of MNTable3D::s_output_style (see MNTable3D::SetOutputStyle). If it is set to 0, output suitable for debugging is generated, if it is set to 1 output in the esys .geo format is generated. If MNTable3D::s_output_style is set to 2, the output format is VTK-XML.
834
835 \param ost the output stream
836 \param T the table
837 */
838 ostream& operator << (ostream& ost,const MNTable3D& T)
839 {
840 if(MNTable3D::s_output_style==0){ // debug style
841 // don't print padding
842 MNTCell::SetOutputStyle(0);
843 for(int i=0;i<T.m_nx;i++){
844 for(int j=1;j<T.m_ny-1;j++){
845 for(int k=1;k<T.m_nz-1;k++){
846 ost << "=== Cell " << i << " , " << j << " , " << k << " === " << T.idx(i,j,k) << " ===" << endl;
847 ost << T.m_data[T.idx(i,j,k)];
848 }
849 }
850 }
851 } else if (MNTable3D::s_output_style==1){ // geo style
852 int nparts=0;
853 for(int i=1;i<T.m_nx-1;i++){
854 for(int j=1;j<T.m_ny-1;j++){
855 for(int k=1;k<T.m_nz-1;k++){
856 nparts+=T.m_data[T.idx(i,j,k)].NParts();
857 }
858 }
859 }
860 // header
861 ost << "LSMGeometry 1.2" << endl;
862 if(T.m_write_tight_bbx & T.m_has_tight_bbx){
863 ost << "BoundingBox " << T.m_min_tbbx << " " << T.m_max_tbbx << endl;
864 } else {
865 ost << "BoundingBox " << T.m_min_pt << " " << T.m_max_pt << endl;
866 }
867 ost << "PeriodicBoundaries " << T.m_x_periodic << " " << T.m_y_periodic << " " << T.m_z_periodic << endl;
868 ost << "Dimension 3D" << endl;
869 // particles
870 ost << "BeginParticles" << endl;
871 ost << "Simple" << endl;
872 ost << nparts << endl;
873 MNTCell::SetOutputStyle(1);
874 for(int i=1;i<T.m_nx-1;i++){
875 for(int j=1;j<T.m_ny-1;j++){
876 for(int k=1;k<T.m_nz-1;k++){
877 ost << T.m_data[T.idx(i,j,k)];
878 }
879 }
880 }
881 ost << "EndParticles" << endl;
882 // bonds
883 ost << "BeginConnect" << endl;
884 // sum bonds
885 int nbonds=0;
886 for(map<int,set<pair<int,int> > >::const_iterator iter=T.m_bonds.begin();
887 iter!=T.m_bonds.end();
888 iter++){
889 nbonds+=iter->second.size();
890 }
891 ost << nbonds << endl;
892 for(map<int,set<pair<int,int> > >::const_iterator iter=T.m_bonds.begin();
893 iter!=T.m_bonds.end();
894 iter++){
895 for(set<pair<int,int> >::const_iterator v_iter=iter->second.begin();
896 v_iter!=iter->second.end();
897 v_iter++){
898 if(v_iter->first<=v_iter->second){
899 ost << v_iter->first << " " << v_iter->second << " " << iter->first << endl;
900 } else {
901 ost << v_iter->second << " " << v_iter->first << " " << iter->first << endl;
902 }
903 }
904 }
905 ost << "EndConnect" << endl;
906 } else if (MNTable3D::s_output_style==2){ // vtk-xml
907 T.WriteAsVtkXml(ost);
908 }
909
910 return ost;
911 }
912
913 /*!
914 tag all particles close to a plane
915
916 \param P the plane
917 \param dist max. distance
918 \param tag the tag
919 \param gid group Id
920 */
921 void MNTable3D::tagParticlesAlongPlane(const Plane& P,double dist,int tag,unsigned int gid)
922 {
923 for(int i=1;i<m_nx-1;i++){
924 for(int j=1;j<m_ny-1;j++){
925 for(int k=1;k<m_nz-1;k++){
926 vector<Sphere*> vs=m_data[idx(i,j,k)].getSpheresNearObject(&P,dist,gid);
927 for(vector<Sphere*>::iterator iter=vs.begin();
928 iter!=vs.end();
929 iter++){
930 (*iter)->setTag(tag);
931 }
932 }
933 }
934 }
935 }
936
937 /*!
938 tag all particles inside a specified volume
939
940 \param V the volume
941 \param tag the tag
942 \param gid group Id
943 */
944 void MNTable3D::tagParticlesInVolume(const AVolume& V,int tag,unsigned int gid)
945 {
946 for(int i=1;i<m_nx-1;i++){
947 for(int j=1;j<m_ny-1;j++){
948 for(int k=1;k<m_nz-1;k++){
949 vector<Sphere*> vs=m_data[idx(i,j,k)].getSpheresInVolume(&V,gid);
950 for(vector<Sphere*>::iterator iter=vs.begin();
951 iter!=vs.end();
952 iter++){
953 (*iter)->setTag(tag);
954 }
955 }
956 }
957 }
958 }
959
960 /*!
961 tag all particles close to a plane
962
963 \param P the plane
964 \param dist max. distance
965 \param the tag mask
966 \param tag the tag
967 \param gid group Id
968 */
969 void MNTable3D::tagParticlesAlongPlaneWithMask(const Plane& P,double dist,int tag,int mask,unsigned int gid)
970 {
971 for(int i=1;i<m_nx-1;i++){
972 for(int j=1;j<m_ny-1;j++){
973 for(int k=1;k<m_nz-1;k++){
974 vector<Sphere*> vs=m_data[idx(i,j,k)].getSpheresNearObject(&P,dist,gid);
975 for(vector<Sphere*>::iterator iter=vs.begin();
976 iter!=vs.end();
977 iter++){
978 int oldtag=(*iter)->Tag();
979 int newtag=(tag & mask) | (oldtag & ~mask);
980 (*iter)->setTag(newtag);
981 }
982 }
983 }
984 }
985 }
986
987 /*!
988 tag all particles close to a joint set
989
990 \param T the joint set
991 \param dist max. distance
992 \param the tag mask
993 \param tag the tag
994 \param gid group Id
995 */
996 void MNTable3D::tagParticlesAlongJoints(const TriPatchSet& T,double dist,int tag, int mask, unsigned int gid)
997 {
998 for(int i=1;i<m_nx-1;i++){
999 for(int j=1;j<m_ny-1;j++){
1000 for(int k=1;k<m_nz-1;k++){
1001 for(vector<Triangle3D>::const_iterator iter=T.triangles_begin();
1002 iter!=T.triangles_end();
1003 iter++){
1004 vector<Sphere*> vs=m_data[idx(i,j,k)].getSpheresNearObject(&(*iter),dist,gid);
1005 for(vector<Sphere*>::iterator iter=vs.begin();
1006 iter!=vs.end();
1007 iter++){
1008 int oldtag=(*iter)->Tag();
1009 int newtag=(tag & mask) | (oldtag & ~mask);
1010 (*iter)->setTag(newtag);
1011 }
1012 }
1013 }
1014 }
1015 }
1016 }
1017
1018 /*!
1019 tag all particles fully within a sphere
1020
1021 \param S the sphere
1022 \param tag the tag
1023 \param gid group Id
1024 */
1025 void MNTable3D::tagParticlesInSphere(const Sphere &S,int tag,unsigned int gid)
1026 {
1027 int range=int(ceil(S.Radius()/m_celldim));
1028 for(int i=-1*range;i<=range;i++){
1029 for(int j=-1*range;j<=range;j++){
1030 for(int k=-1*range;k<=range;k++){
1031 Vector3 np=S.Center()+Vector3(double(i)*m_celldim,double(j)*m_celldim,double(k)*m_celldim);
1032 int idx=this->getIndex(np);
1033 if(idx!=-1){
1034 multimap<double,Sphere*> v=m_data[idx].getSpheresFromGroupNearNC(S.Center(),S.Radius(),gid);
1035 for(multimap<double,Sphere*>::iterator iter=v.begin();
1036 iter!=v.end();
1037 iter++){
1038 double outer_dist=(iter->first+(iter->second)->Radius());
1039 if(outer_dist<=S.Radius()) (iter->second)->setTag(tag);
1040 }
1041 }
1042 }
1043 }
1044 }
1045 }
1046
1047
1048 /*!
1049 tag the spheres in a group A according to the tag of the closest spheres from a another group B
1050
1051 \param gid1 the group Id A
1052 \param gid1 the group Id B
1053 */
1054
1055 void MNTable3D::tagParticlesToClosest(int gid1, int gid2)
1056 {
1057 for(int i=0;i<m_nx-1;i++){
1058 for(int j=0;j<m_ny-1;j++){
1059 for(int k=0;k<m_nz-1;k++){
1060 vector<Sphere*> v=m_data[idx(i,j,k)].getAllSpheresFromGroupNC(gid1);
1061 for(vector<Sphere*>::iterator iter=v.begin();
1062 iter!=v.end();
1063 iter++){
1064 int t=getTagOfClosestSphereFromGroup(*(*iter),gid2);
1065 (*iter)->setTag(t);
1066 }
1067 }
1068 }
1069 }
1070 }
1071
1072 /*!
1073 Tag the spheres in a group A according to the tag of the closest spheres from a another group B, using an anisotropic distance measure
1074
1075 \param gid1 the group Id A
1076 \param gid1 the group Id B
1077 \param wx weighting of the x-component of the distance
1078 \param wy weighting of the y-component of the distance
1079 \param wz weighting of the z-component of the distance
1080 */
1081
1082 void MNTable3D::tagParticlesToClosestAnisotropic(int gid1, int gid2,
1083 double wx, double wy,double wz)
1084 {
1085 for(int i=0;i<m_nx-1;i++){
1086 for(int j=0;j<m_ny-1;j++){
1087 for(int k=0;k<m_nz-1;k++){
1088 vector<Sphere*> v=m_data[idx(i,j,k)].getAllSpheresFromGroupNC(gid1);
1089 for(vector<Sphere*>::iterator iter=v.begin();
1090 iter!=v.end();
1091 iter++){
1092 int t=getTagOfClosestSphereFromGroup(*(*iter),gid2,wx,wy,wz);
1093 (*iter)->setTag(t);
1094 }
1095 }
1096 }
1097 }
1098 }
1099
1100
1101 /*
1102 tag all particles within a group
1103
1104 \param gid the group Id
1105 \param tag the tag
1106 \param mask the tag mask (i.e. new_tag = new_tag=(old_tag & (~mask)) | (tag & mask))
1107 */
1108 void MNTable3D::tagParticlesInGroup(int gid, int tag, int mask)
1109 {
1110 for(int i=1;i<m_nx-1;i++){
1111 for(int j=1;j<m_ny-1;j++){
1112 for(int k=1;k<m_nz-1;k++){
1113 m_data[idx(i,j,k)].tagSpheresInGroup(gid,tag,mask);
1114 }
1115 }
1116 }
1117 }
1118
1119 /*!
1120 change particle IDs so that they are continous
1121 */
1122 void MNTable3D::renumberParticlesContinuous()
1123 {
1124 int count=0;
1125
1126 for(int i=1;i<m_nx-1;i++){
1127 for(int j=1;j<m_ny-1;j++){
1128 for(int k=1;k<m_nz-1;k++){
1129 count=m_data[idx(i,j,k)].renumberParticlesContinuous(count);
1130 }
1131 }
1132 }
1133 }
1134
1135 /*!
1136 remove all particles with a given tag within a group, using default mask -1
1137
1138 \param tag the tag
1139 \param gid the group Id
1140 */
1141 void MNTable3D::removeParticlesWithTag(int tag,unsigned int gid)
1142 {
1143 for(int i=0;i<m_nx;i++){
1144 for(int j=0;j<m_ny;j++){
1145 for(int k=0;k<m_nz;k++){
1146 m_data[idx(i,j,k)].removeTagged(gid,tag,-1);
1147 }
1148 }
1149 }
1150 }
1151
1152 /*!
1153 remove all particles with a given tag within a group
1154
1155 \param tag the tag
1156 \param gid the group Id
1157 \param mask the mask
1158 */
1159 void MNTable3D::removeParticlesWithTagMask(unsigned int gid,int tag, int mask)
1160 {
1161 for(int i=0;i<m_nx;i++){
1162 for(int j=0;j<m_ny;j++){
1163 for(int k=0;k<m_nz;k++){
1164 m_data[idx(i,j,k)].removeTagged(gid,tag,mask);
1165 }
1166 }
1167 }
1168 }
1169
1170 /*!
1171 Remove all particles inside a given volume. The parameter
1172 "full" decides if only particles completely inside the
1173 volume are removed (full==true) or all particles with the
1174 center inside the volume (full==false)
1175
1176 \param Vol the volume
1177 \param full remove particles fully inside / center inside
1178 */
1179 void MNTable3D::removeParticlesInVolume(AVolume3D* Vol, int gid,bool full)
1180 {
1181 for(int i=0;i<m_nx;i++){
1182 for(int j=0;j<m_ny;j++){
1183 for(int k=0;k<m_nz;k++){
1184 m_data[idx(i,j,k)].removeInVolume(Vol,gid,full);
1185 }
1186 }
1187 }
1188 }
1189
1190 /*!
1191 Remove all particle in a group. Convenience function combining
1192 tagParticlesInGroup and removeTaggedParticles
1193
1194 \param gid the ID of the group of the particles to be removed
1195 */
1196 void MNTable3D::removeParticlesInGroup(unsigned int gid)
1197 {
1198 tagParticlesInGroup(gid,0,-1);
1199 removeParticlesWithTag(0,gid);
1200 }
1201
1202 // ---------------
1203 // bbx handling
1204 // ---------------
1205
1206 /*!
1207 Calculate a the axis-aligned bounding box of the current particle set
1208 */
1209 void MNTable3D::calculateTightBoundingBox()
1210 {}
1211
1212 /*!
1213 Set bounding box tracking on/off
1214 */
1215 void MNTable3D::setBoundingBoxTracking(bool b)
1216 {
1217 m_bbx_tracking=b;
1218 }
1219
1220 /*!
1221 Switch if initial min/max points or current bounding box
1222 is written to geo file
1223 */
1224 void MNTable3D::SetWriteTightBoundingBox(bool b)
1225 {
1226 m_write_tight_bbx=b;
1227 if(b & !m_has_tight_bbx){
1228 std::cout << "WARNING: Writing of actual bounding box switched on, but bounding box not calculated yet!" << std::endl;
1229 }
1230 }
1231
1232 /*!
1233 output content as VTK-XML format
1234
1235 \param vtkfile the output stream
1236 */
1237 void MNTable3D::WriteAsVtkXml(ostream& vtkfile) const
1238 {
1239 // inverse mapping index<->particle id
1240 map<int,int> inv_map;
1241
1242 int pcount=0;
1243 for(int i=1;i<m_nx-1;i++){
1244 for(int j=1;j<m_ny-1;j++){
1245 for(int k=1;k<m_nz-1;k++){
1246 vector<int> ids=m_data[idx(i,j,k)].getIdList();
1247 for(vector<int>::iterator iter=ids.begin();
1248 iter!=ids.end();
1249 iter++){
1250 inv_map.insert(make_pair(*iter,pcount));
1251 pcount++;
1252 }
1253 }
1254 }
1255 }
1256
1257 // get bond count
1258 int bcount=0;
1259
1260 for(map<int,set<bond> >::const_iterator iter=m_bonds.begin();
1261 iter!=m_bonds.end();
1262 iter++){
1263 bcount+=iter->second.size();
1264 }
1265
1266 // write header
1267 vtkfile << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\">\n";
1268 vtkfile << "<UnstructuredGrid>\n";
1269 int nparts=0;
1270 for(int i=1;i<m_nx-1;i++){
1271 for(int j=1;j<m_ny-1;j++){
1272 for(int k=1;k<m_nz-1;k++){
1273 nparts+=m_data[idx(i,j,k)].NParts();
1274 }
1275 }
1276 }
1277 vtkfile << "<Piece NumberOfPoints=\"" << nparts << "\" NumberOfCells=\"" << bcount << "\">\n";
1278 // write particle pos
1279 vtkfile << "<Points>\n";
1280 vtkfile << "<DataArray NumberOfComponents=\"3\" type=\"Float64\" format=\"ascii\">\n";
1281 for(int i=1;i<m_nx-1;i++){
1282 for(int j=1;j<m_ny-1;j++){
1283 for(int k=1;k<m_nz-1;k++){
1284 m_data[idx(i,j,k)].writePositions(vtkfile);
1285 }
1286 }
1287 }
1288 vtkfile << endl;
1289 vtkfile << "</DataArray>\n";
1290 vtkfile << "</Points>\n";
1291
1292 // --- write particle data ---
1293 // radius
1294 vtkfile << "<PointData Scalars=\"radius\">\n";
1295 vtkfile << "<DataArray type=\"Float64\" Name=\"radius\" NumberOfComponents=\"1\" format=\"ascii\">\n";
1296 for(int i=1;i<m_nx-1;i++){
1297 for(int j=1;j<m_ny-1;j++){
1298 for(int k=1;k<m_nz-1;k++){
1299 m_data[idx(i,j,k)].writeRadii(vtkfile);
1300 }
1301 }
1302 }
1303 vtkfile << endl;
1304 vtkfile << "</DataArray>\n";
1305
1306 // ID
1307 vtkfile << "<DataArray type=\"Int32\" Name=\"particleID\" NumberOfComponents=\"1\" format=\"ascii\">\n";
1308 for(int i=1;i<m_nx-1;i++){
1309 for(int j=1;j<m_ny-1;j++){
1310 for(int k=1;k<m_nz-1;k++){
1311 m_data[idx(i,j,k)].writeIDs(vtkfile);
1312 }
1313 }
1314 }
1315 vtkfile << endl;
1316 vtkfile << "</DataArray>\n";
1317
1318 // Tag
1319 vtkfile << "<DataArray type=\"Int32\" Name=\"particleTag\" NumberOfComponents=\"1\" format=\"ascii\">\n";
1320 for(int i=1;i<m_nx-1;i++){
1321 for(int j=1;j<m_ny-1;j++){
1322 for(int k=1;k<m_nz-1;k++){
1323 m_data[idx(i,j,k)].writeTags(vtkfile);
1324 }
1325 }
1326 }
1327 vtkfile << endl;
1328 vtkfile << "</DataArray>\n";
1329
1330 vtkfile << "</PointData>\n";
1331 // --- write cells section ---
1332 // connections
1333 vtkfile << "<Cells>\n";
1334 vtkfile << "<DataArray type=\"Int32\" NumberOfComponents=\"1\" Name=\"connectivity\" format=\"ascii\">\n";
1335 for(map<int,set<bond> >::const_iterator iter=m_bonds.begin();
1336 iter!=m_bonds.end();
1337 iter++){
1338 for(set<bond>::const_iterator iter_b=iter->second.begin();
1339 iter_b!=iter->second.end();
1340 iter_b++){
1341 vtkfile << inv_map[iter_b->first] << " " << inv_map[iter_b->second] << endl;
1342 }
1343 }
1344
1345 vtkfile << "</DataArray>\n";
1346 // offsets
1347 vtkfile << "<DataArray type=\"Int32\" NumberOfComponents=\"1\" Name=\"offsets\" format=\"ascii\">\n";
1348 for (size_t i = 1; i < bcount*2; i+=2) vtkfile << i+1 << "\n";
1349 vtkfile << "</DataArray>\n";
1350
1351 // element type
1352 vtkfile << "<DataArray type=\"UInt8\" NumberOfComponents=\"1\" Name=\"types\" format=\"ascii\">\n";
1353 const int CELL_LINE_TYPE = 3;
1354 for (size_t i = 0; i < bcount; i++) vtkfile << CELL_LINE_TYPE << endl;
1355 vtkfile << "</DataArray>\n";
1356 vtkfile << "</Cells>\n";
1357
1358 // cell data
1359 vtkfile << "<CellData>\n";
1360 vtkfile << "<DataArray type=\"Int32\" Name=\"bondTag\" NumberOfComponents=\"1\" format=\"ascii\">\n";
1361 for(map<int,set<bond> >::const_iterator iter=m_bonds.begin();
1362 iter!=m_bonds.end();
1363 iter++){
1364 for(set<bond>::const_iterator iter_b=iter->second.begin();
1365 iter_b!=iter->second.end();
1366 iter_b++){
1367 vtkfile << iter->first << endl;
1368 }
1369 }
1370 vtkfile << "</DataArray>\n";
1371 vtkfile << "</CellData>\n";
1372 // write footer
1373 vtkfile << "</Piece>\n";
1374 vtkfile << "</UnstructuredGrid>\n";
1375 vtkfile << "</VTKFile>\n";
1376 }
1377
1378 /*!
1379 Set output precision
1380
1381 \param prec number of significant digits
1382 */
1383 void MNTable3D::SetOutputPrecision(int prec)
1384 {
1385 m_write_prec=prec;
1386 }
1387
1388 void MNTable3D::write(char *filename, int outputStyle)
1389 {
1390 ofstream outFile;
1391 outFile.precision(m_write_prec);
1392
1393 SetOutputStyle(outputStyle);
1394
1395 outFile.open(filename);
1396 outFile << *(this);
1397 outFile.close();
1398 }
1399
1400 // --- block writing ---
1401 void MNTable3D::initBlockWriting(const string& filename)
1402 {
1403 m_outfilename=filename;
1404 m_temp_bondfilename=filename+string("_tmp_bond");
1405 m_block_particles_written=0;
1406 m_block_bonds_written=0;
1407 // write header
1408 ofstream ost(filename.c_str());
1409 ost << "LSMGeometry 1.2" << endl;
1410 if(m_write_tight_bbx & m_has_tight_bbx){
1411 ost << "BoundingBox " << m_min_tbbx << " " << m_max_tbbx << endl;
1412 } else {
1413 ost << "BoundingBox " << m_min_pt << " " << m_max_pt << endl;
1414 }
1415 ost << "PeriodicBoundaries " << m_x_periodic << " " << m_y_periodic << " " << m_z_periodic << endl;
1416 ost << "Dimension 3D" << endl;
1417 // particles
1418 ost << "BeginParticles" << endl;
1419 ost << "Simple" << endl;
1420 m_np_write_pos=ost.tellp();
1421 ost << " " << endl; // IMPORTANT: 10 spaces as placeholder for the number of particles
1422 std::cout << m_np_write_pos << std::endl;
1423
1424 m_is_writing_blocks=true;
1425 }
1426
1427 void MNTable3D::writeBlock(const Vector3& min_pt,const Vector3& max_pt)
1428 {
1429 ofstream ost(m_outfilename.c_str(),ios::app); // open file in append mode
1430 ost.precision(m_write_prec);
1431 for(int i=1;i<m_nx-1;i++){
1432 for(int j=1;j<m_ny-1;j++){
1433 for(int k=1;k<m_nz-1;k++){
1434 m_block_particles_written+=m_data[idx(i,j,k)].writeParticlesInBlock(ost,min_pt,max_pt);
1435 }
1436 }
1437 }
1438 ost.close();
1439 std::cout << m_block_particles_written << std::endl;
1440 }
1441
1442 void MNTable3D::writeBondsBlocked()
1443 {
1444 ofstream ost(m_temp_bondfilename.c_str(),ios::app);
1445 for(map<int,set<pair<int,int> > >::const_iterator iter=m_bonds.begin();
1446 iter!=m_bonds.end();
1447 iter++){
1448 for(set<pair<int,int> >::const_iterator v_iter=iter->second.begin();
1449 v_iter!=iter->second.end();
1450 v_iter++){
1451 if(v_iter->first<=v_iter->second){
1452 ost << v_iter->first << " " << v_iter->second << " " << iter->first << endl;
1453 } else {
1454 ost << v_iter->second << " " << v_iter->first << " " << iter->first << endl;
1455 }
1456 m_block_bonds_written++;
1457 }
1458 }
1459 ost.close();
1460 std::cout << "bonds written: " << m_block_bonds_written << std::endl;
1461 }
1462
1463 void MNTable3D::finishBlockWriting()
1464 {
1465 ofstream ost(m_outfilename.c_str(),ios::in | ios::out | ios::ate); // open for writing, initially positioned at the end
1466 // -- write end of particles --
1467 ost << "EndParticles" << endl;
1468 // -- write bonds --
1469 ost << "BeginConnect" << endl;
1470 ost << m_block_bonds_written << endl;
1471 ifstream ifs(m_temp_bondfilename.c_str());
1472 ost << ifs.rdbuf();
1473 ifs.close();
1474 ost << "EndConnect" << endl;
1475 // -- reposition to the place where nr. of particles should be
1476 ost.seekp(m_np_write_pos);
1477 // overwrite placeholders
1478 ost << m_block_particles_written;
1479
1480 // close file
1481 ost.close();
1482 }