Debian Codegraph @

src/MNTable2D.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 "MNTable2D.h"
14
15 // --- STL includes ---
16 #include <utility>
17
18 using namespace std;
19
20 using std::make_pair;
21
22 // --- System includes ---
23 #include <cmath>
24 #include <cstdlib>
25 #include <iostream>
26 #include <fstream>
27
28 using std::ceil;
29 using std::endl;
30 using std::rand;
31
32 int MNTable2D::s_output_style=0;
33 double MNTable2D::s_small_value=1e-7;
34
35 MNTable2D::MNTable2D():m_write_prec(10)
36 {
37 }
38
39 /*!
40 construct MNTable2D
41
42 \param MinPt minimum point (z component ignored)
43 \param MaxPt maximum point (z component ignored)
44 \param cd cell dimension
45 \param ngroups initial number of particle groups
46 */
47 MNTable2D::MNTable2D(const Vector3& MinPt,const Vector3& MaxPt,double cd,unsigned int ngroups)
48 {
49 m_celldim=cd;
50 m_ngroups=ngroups;
51
52 // get number of cells
53 Vector3 req_size=MaxPt-MinPt; // requested size
54 m_nx=int(ceil(req_size.X()/m_celldim))+2;
55 m_ny=int(ceil(req_size.Y()/m_celldim))+2;
56
57 // add padding
58 m_x0=MinPt.x()-m_celldim;
59 m_y0=MinPt.y()-m_celldim;
60
61 // allocate cell array
62 m_data=new MNTCell[m_nx*m_ny];
63 for(int i=0;i<m_nx*m_ny;i++) {
64 m_data[i].SetNGroups(m_ngroups);
65 }
66
67 // periodicity
68 m_x_periodic=0;
69 m_y_periodic=0;
70
71 // default output precision
72 m_write_prec=10;
73 }
74
75 /*!
76 destruct MNTable2D
77 */
78 MNTable2D::~MNTable2D()
79 {
80 if(m_data!=NULL) delete[] m_data;
81 }
82
83
84 /*!
85 Increase the number of particle groups. Do nothing if the number is already large enough
86
87 \param ngroups the new number of particle groups
88 */
89 void MNTable2D::GrowNGroups(unsigned int ngroups)
90 {
91 if(ngroups>m_ngroups){
92 m_ngroups=ngroups;
93 for(int i=0;i<m_nx*m_ny;i++) {
94 m_data[i].SetNGroups(m_ngroups);
95 }
96 }
97 }
98
99 /*!
100 get the cell index for a given position
101
102 \param Pos the position
103 \return the cell index if Pos is inside the table, -1 otherwise
104 */
105 int MNTable2D::getIndex(const Vector3& Pos) const
106 {
107 int ret;
108
109 int ix=int(floor((Pos.x()-m_x0)/m_celldim));
110 int iy=int(floor((Pos.y()-m_y0)/m_celldim));
111
112 // check if pos is in table excl. padding
113 if((ix>0) && (ix<m_nx-1) && (iy>0) && (iy<m_ny-1)){
114 ret=idx(ix,iy);
115 } else {
116 ret=-1;
117 }
118
119 return ret;
120 }
121
122 /*!
123 insert sphere
124
125 \param S the Sphere
126 \param gid the group id
127 */
128 bool MNTable2D::insert(const Sphere& S,unsigned int gid)
129 {
130 bool res;
131
132 int id=getIndex(S.Center());
133 // std::cout << "center : " << S.Center() << " index : " << id << std::endl;
134 if((id!=-1) && (gid<m_ngroups)){
135 m_data[id].insert(S,gid);
136 res=true;
137 } else {
138 res=false;
139 }
140
141 return res;
142 }
143
144
145 /*!
146 insert sphere if it doesn't collide with other spheres
147
148 \param S the Sphere
149 \param gid the group id
150 */
151 bool MNTable2D::insertChecked(const Sphere& S,unsigned int gid,double tol)
152 {
153 bool res;
154
155 int id=getIndex(S.Center());
156 // std::cout << "center : " << S.Center() << " index : " << id << std::endl;
157 if((id!=-1) && (gid<m_ngroups)){
158 const multimap<double,const Sphere*> close_spheres=getSpheresFromGroupNear(S.Center(),S.Radius()-s_small_value,gid);
159 if(close_spheres.size()==0){
160 m_data[id].insert(S,gid);
161 res=true;
162 } else res=false;
163 } else {
164 res=false;
165 }
166
167 return res;
168 }
169
170 /*!
171 get the closest spheres to a given point
172
173 \param p the point
174 \param nmax the maximum number of spheres returned
175 */
176 const multimap<double,const Sphere*> MNTable2D::getSpheresClosestTo(const Vector3& p,unsigned int nmax) const
177 {
178 multimap<double,const Sphere*> res;
179
180 for(int i=-1;i<=1;i++){
181 for(int j=-1;j<=1;j++){
182 Vector3 pos=p+Vector3(double(i)*m_celldim,double(j)*m_celldim,0.0);
183 int idx=getIndex(pos);
184 if(idx!=-1){
185 multimap<double,const Sphere*> smap=m_data[idx].getSpheresClosestTo(p,nmax);
186 res.insert(smap.begin(),smap.end());
187 }
188 }
189 }
190
191 return res;
192 }
193
194 /*!
195 get all spheres from one group within a given distance to a point
196
197 \param P the point
198 \param d max. distance between the point and the surface of the sphere
199 \param gid the group id
200
201 \warning no check for validity of gid
202 */
203 const multimap<double,const Sphere*> MNTable2D::getSpheresFromGroupNear(const Vector3& P,double d,int gid) const
204 {
205 multimap<double,const Sphere*> res;
206
207 for(int i=-1;i<=1;i++){
208 for(int j=-1;j<=1;j++){
209 Vector3 np=P+Vector3(double(i)*m_celldim,double(j)*m_celldim,0.0);
210 int idx=getIndex(np);
211 if(idx!=-1){
212 multimap<double,const Sphere*> v=m_data[idx].getSpheresFromGroupNear(P,d,gid);
213 res.insert(v.begin(),v.end());
214 }
215 }
216 }
217
218 return res;
219 }
220
221 /*!
222 get vector of pointers to all spheres of a group
223
224 \param gid goup Id
225 */
226 const vector<const Sphere*> MNTable2D::getAllSpheresFromGroup(int gid) const
227 {
228 vector<const Sphere*> res;
229
230 for(int i=1;i<m_nx-1;i++){
231 for(int j=1;j<m_ny-1;j++){
232 vector<const Sphere*> cr=m_data[idx(i,j)].getAllSpheresFromGroup(gid);
233 res.insert(res.end(),cr.begin(),cr.end());
234 }
235 }
236
237 return res;
238 }
239
240 /*!
241 get the sphere within a given group closest to given sphere
242
243 \param s the sphere
244 \param gid the group
245 */
246 const Sphere* MNTable2D::getClosestSphereFromGroup(const Sphere& s,int gid) const
247 {
248 const Sphere* res=NULL;
249 int max_dim=m_nx>m_ny ? m_nx : m_ny;
250 int found=max_dim;
251 double dist=max_dim*m_celldim;
252
253 // search within the same cell
254 int idx=getIndex(s.Center());
255 const Sphere* v=m_data[idx].getClosestSphereFromGroup(s,gid,2.0*max_dim);
256 if(v!=NULL){
257 double d=(v->Center()-s.Center()).norm()-s.Radius();
258 found=1;
259 if(d<dist){
260 res=v;
261 dist=d;
262 }
263 }
264
265 // search outside origin cell
266 // get bigger of the 2 table dimensions
267 int range=1;
268 while((range<max_dim) && (range<found+1)){
269 for(int i=-range;i<=range;i++){
270 for(int j=-range;j<=range;j++){
271 Vector3 np=s.Center()+Vector3(double(i)*m_celldim,double(j)*m_celldim,0.0);
272 int idx=getIndex(np);
273 if(idx!=-1){
274 const Sphere* v=m_data[idx].getClosestSphereFromGroup(s,gid,dist);
275 if(v!=NULL){
276 double d=(v->Center()-s.Center()).norm()-s.Radius();
277 found=range+1;
278 if(d<dist){
279 res=v;
280 dist=d;
281 }
282 }
283 }
284 }
285 }
286 range++;
287
288 }
289 return res;
290 }
291
292 /*!
293 get the sphere within a given group closest to point
294
295 \param p the point
296 \param gid the group
297 */
298 Sphere* MNTable2D::getClosestSphereFromGroup(const Vector3& p,int gid) const
299 {
300 Sphere* res=NULL;
301 int max_dim=m_nx>m_ny ? m_nx : m_ny;
302 int found=max_dim;
303 double dist=max_dim*m_celldim;
304
305 // search within the same cell
306 int idx=getIndex(p);
307 Sphere* v=m_data[idx].getClosestSphereFromGroup(p,gid,2.0*max_dim);
308 if(v!=NULL){
309 double d=(v->Center()-p).norm();
310 found=1;
311 if(d<dist){
312 res=v;
313 dist=d;
314 }
315 }
316
317 // search outside origin cell
318 // get bigger of the 2 table dimensions
319 int range=1;
320 while((range<max_dim) && (range<found+1)){
321 for(int i=-range;i<=range;i++){
322 for(int j=-range;j<=range;j++){
323 Vector3 np=p+Vector3(double(i)*m_celldim,double(j)*m_celldim,0.0);
324 int idx=getIndex(np);
325 if(idx!=-1){
326 Sphere* v=m_data[idx].getClosestSphereFromGroup(p,gid,dist);
327 if(v!=NULL){
328 double d=(v->Center()-p).norm();
329 found=range+1;
330 if(d<dist){
331 res=v;
332 dist=d;
333 }
334 }
335 }
336 }
337 }
338 range++;
339
340 }
341 return res;
342 }
343
344 /*!
345 convenience function which calls getClosestSphereFromGroup and returns the
346 tag of the found sphere or -1 if none is found
347
348 \param s the sphere
349 \param gid the group
350 */
351 int MNTable2D::getTagOfClosestSphereFromGroup(const Sphere& s,int gid) const
352 {
353 int res;
354
355 const Sphere* sp=getClosestSphereFromGroup(s,gid);
356 if(sp!=NULL){
357 res=sp->Tag();
358 }else{
359 res=-1;
360 }
361
362 return res;
363 }
364
365 /*!
366 tag the spheres in a group A according to the tag of the closest spheres from a another group B
367
368 \param gid1 the group Id A
369 \param gid1 the group Id B
370 */
371
372 void MNTable2D::tagParticlesToClosest(int gid1, int gid2)
373 {
374 for(int i=0;i<m_nx-1;i++){
375 for(int j=0;j<m_ny-1;j++){
376 vector<Sphere*> v=m_data[idx(i,j)].getAllSpheresFromGroupNC(gid1);
377 for(vector<Sphere*>::iterator iter=v.begin();
378 iter!=v.end();
379 iter++){
380 int t=getTagOfClosestSphereFromGroup(*(*iter),gid2);
381 (*iter)->setTag(t);
382 }
383 }
384 }
385 }
386
387 /*!
388 tag the closest spheres to a given point
389
390 \param p the point
391 \param dist max distance
392 \param gid the group id
393 \param tag the tag
394 */
395 void MNTable2D::tagParticlesNear(const Vector3& p,double dist,int gid, int tag)
396 {
397 for(int i=-1;i<=1;i++){
398 for(int j=-1;j<=1;j++){
399 Vector3 np=p+Vector3(double(i)*m_celldim,double(j)*m_celldim,0.0);
400 int idx=getIndex(np);
401 if(idx!=-1){
402 m_data[idx].tagSpheresNear(p,dist,gid,tag);
403 }
404 }
405 }
406 }
407
408 /*!
409 tag all particles inside a specified volume
410
411 \param V the volume
412 \param tag the tag
413 \param gid group Id
414 */
415 void MNTable2D::tagParticlesInVolume(const AVolume& V,int tag,unsigned int gid)
416 {
417 for(int i=0;i<m_nx;i++){
418 for(int j=0;j<m_ny;j++){
419 vector<Sphere*> vs=m_data[idx(i,j)].getSpheresInVolume(&V,gid);
420 for(vector<Sphere*>::iterator iter=vs.begin();
421 iter!=vs.end();
422 iter++){
423 (*iter)->setTag(tag);
424 }
425 }
426 }
427 }
428
429
430 /*!
431 tag the closest sphere to a given point
432
433 \param p the point
434 \param gid the group id
435 \param tag the tag
436 */
437 void MNTable2D::tagClosestParticle(const Vector3& p,int gid, int tag)
438 {
439 Sphere* sp=getClosestSphereFromGroup(p,gid);
440 if(sp!=NULL){
441 sp->setTag(tag);
442 }
443 }
444
445
446
447 /*!
448 insert sphere if it doesn't collide with other spheres
449
450 \param S the Sphere
451 \param gid the group id
452 */
453 bool MNTable2D::checkInsertable(const Sphere& S,unsigned int gid)
454 {
455 bool res;
456
457 int id=getIndex(S.Center());
458 // std::cout << "center : " << S.Center() << " index : " << id << std::endl;
459 if((id!=-1) && (gid<m_ngroups)){
460 multimap<double,const Sphere*> close_spheres=getSpheresFromGroupNear(S.Center(),S.Radius()-s_small_value,gid);
461 if(close_spheres.size()==0){
462 res=true;
463 } else res=false;
464 } else {
465 res=false;
466 }
467
468 return res;
469 }
470
471 /*
472 get the sum of the area/volume of all particles in a group
473
474 \param gid the group id
475 */
476 double MNTable2D::getSumVolume(int gid)
477 {
478 double res=0.0;
479
480 for(int i=1;i<m_nx-1;i++){
481 for(int j=1;j<m_ny-1;j++){
482 res+=m_data[idx(i,j)].getSumVolume2D(gid);
483 }
484 }
485
486 return res;
487 }
488
489 /*
490 get the number of particles in a group
491
492 \param gid the group id
493 */
494 int MNTable2D::getNrParticles(int gid)
495 {
496 int res=0.0;
497
498 for(int i=1;i<m_nx-1;i++){
499 for(int j=1;j<m_ny-1;j++){
500 res+=m_data[idx(i,j)].getNrParticles(gid);
501 }
502 }
503
504 return res;
505 }
506
507 /*!
508 */
509 void MNTable2D::removeTagged(int gid,int tag,int mask)
510 {
511 for(int i=1;i<m_nx-1;i++){
512 for(int j=1;j<m_ny-1;j++){
513 m_data[idx(i,j)].removeTagged(gid,tag,mask);
514 }
515 }
516 }
517
518
519
520 /*!
521 generate bonds between particles of a group
522
523 \param gid the group ID
524 \param tol max. difference between bond length and equilibrium dist.
525 \param btag bond tag
526 */
527 void MNTable2D::generateBonds(int gid,double tol,int btag)
528 {
529 //std::cout << "MNTable2D::generateBonds( " << gid << " , " << tol << " , " << btag << " )" << std::endl; // loop over all cells
530 // loop over all cells
531 for(int i=0;i<m_nx-1;i++){
532 for(int j=0;j<m_ny-1;j++){
533 int id=idx(i,j);
534 for(int ii=-1;ii<=1;ii++){
535 for(int jj=-1;jj<=1;jj++){
536 int id2=idx(i+ii,j+jj);
537 vector<pair<int,int> > bonds;
538 if((ii==0) && (jj==0) && (i!=0) && (j!=0)){ // intra-cell, not for boundary
539 bonds=m_data[id].getBonds(gid,tol);
540 } else if(id2>id){ // inter-cell
541 bonds=m_data[id].getBonds(gid,tol,m_data[id2]);
542 }
543 for(vector<pair<int,int> >::iterator iter=bonds.begin();
544 iter!=bonds.end();
545 iter++){
546 if(m_bonds[btag].find(*iter)==m_bonds[btag].end()){
547 m_bonds[btag].insert(*iter);
548 }
549 }
550 }
551 }
552 }
553 }
554 }
555
556 /*!
557 generate bonds between particles with identical particle tag of a group
558
559 \param gid the group ID
560 \param tol max. difference between bond length and equilibrium dist.
561 \param btag bond tag
562 \param ptag the particle tag
563 \param mask the mask for the particle tag
564 */
565 void MNTable2D::generateBondsWithMask(int gid,double tol,int btag, int ptag, int mask)
566 {
567 //std::cout << "MNTable2D::generateBonds( " << gid << " , " << tol << " , " << btag <<
568 // " " << ptag <<" " << mask<<" )" << std::endl; // loop over all cells
569 // loop over all cells
570 for(int i=0;i<m_nx-1;i++){
571 for(int j=0;j<m_ny-1;j++){
572 int id=idx(i,j);
573 for(int ii=-1;ii<=1;ii++){
574 for(int jj=-1;jj<=1;jj++){
575 int id2=idx(i+ii,j+jj);
576 vector<pair<int,int> > bonds;
577 if((ii==0) && (jj==0) && (i!=0) && (j!=0)){ // intra-cell, not for boundary
578 bonds=m_data[id].getBonds(gid,tol,ptag,mask);
579 } else if(id2>id){ // inter-cell
580 bonds=m_data[id].getBonds(gid,tol,m_data[id2],ptag,mask);
581 }
582 for(vector<pair<int,int> >::iterator iter=bonds.begin();
583 iter!=bonds.end();
584 iter++){
585 m_bonds[btag].insert(*iter);
586 }
587 }
588 }
589 }
590 }
591 }
592
593
594 /*!
595 generate bonds between particles with given particle tag
596
597 \param gid the group ID
598 \param tol max. difference between bond length and equilibrium dist.
599 \param btag bond tag
600 \param ptag1 the 1st particle tag
601 \param mask1 the mask for the 1st particle tag
602 \param ptag2 the 2nd particle tag
603 \param mask2 the mask for the 2nd particle tag
604 */
605 void MNTable2D::generateBondsTaggedMasked(int gid,double tol,int btag,int ptag1,int mask1,int ptag2,int mask2)
606 {
607 for(int i=0;i<m_nx-1;i++){
608 for(int j=0;j<m_ny-1;j++){
609 int id=idx(i,j);
610 for(int ii=-1;ii<=1;ii++){
611 for(int jj=-1;jj<=1;jj++){
612 int id2=idx(i+ii,j+jj);
613 vector<pair<int,int> > bonds;
614 if((ii==0) && (jj==0) && (i!=0) && (j!=0)){ // intra-cell, not for boundary
615 bonds=m_data[id].getBondsTaggedMasked(gid,tol,ptag1,mask1,ptag2,mask2);
616 } else if(id2>id){ // inter-cell
617 bonds=m_data[id].getBondsTaggedMasked(gid,tol,m_data[id2],ptag1,mask1,ptag2,mask2);
618 }
619 for(vector<pair<int,int> >::iterator iter=bonds.begin();
620 iter!=bonds.end();
621 iter++){
622 m_bonds[btag].insert(*iter);
623 }
624 }
625 }
626 }
627 }
628 }
629
630 /*!
631 generate bonds between particles with identical particle tag of a group with a given
632 probablility (0..1)
633
634 \param gid the group ID
635 \param tol max. difference between bond length and equilibrium dist.
636 \param prob probablility of bond generation
637 \param btag bond tag
638 \param ptag the particle tag
639 \param mask the mask for the particle tag
640 */
641 void MNTable2D::generateRandomBonds(int gid,double tol,double prob,int btag, int ptag, int mask)
642 {
643 //std::cout << "MNTable2D::generateBonds( " << gid << " , " << tol << " , " << btag <<
644 // " " << ptag <<" " << mask<<" )" << std::endl; // loop over all cells
645 // loop over all cells
646 for(int i=0;i<m_nx-1;i++){
647 for(int j=0;j<m_ny-1;j++){
648 int id=idx(i,j);
649 for(int ii=-1;ii<=1;ii++){
650 for(int jj=-1;jj<=1;jj++){
651 int id2=idx(i+ii,j+jj);
652 vector<pair<int,int> > bonds;
653 if((ii==0) && (jj==0) && (i!=0) && (j!=0)){ // intra-cell, not for boundary
654 bonds=m_data[id].getBonds(gid,tol,ptag,mask);
655 } else if(id2>id){ // inter-cell
656 bonds=m_data[id].getBonds(gid,tol,m_data[id2],ptag,mask);
657 }
658 for(vector<pair<int,int> >::iterator iter=bonds.begin();
659 iter!=bonds.end();
660 iter++){
661 double rn=((double)(rand())/(double)(RAND_MAX));
662 if(rn<prob) m_bonds[btag].insert(*iter);
663 }
664 }
665 }
666 }
667 }
668 }
669
670 /*!
671 generate bonds between particles of a group
672
673 \param gid the group ID
674 \param tol max. difference between bond length and equilibrium dist.
675 \param btag1 tag for bonds within clusters (same particle tag)
676 \param btag2 tag for bonds betweem clusters (different particle tag)
677 */
678 void MNTable2D::generateClusterBonds(int gid,double tol,int btag1, int btag2)
679 {
680 //std::cout << "MNTable2D::generateBonds( " << gid << " , " << tol << " , " << btag << " )" << std::endl; // loop over all cells
681 // loop over all cells
682 for(int i=0;i<m_nx-1;i++){
683 for(int j=0;j<m_ny-1;j++){
684 int id=idx(i,j);
685 for(int ii=-1;ii<=1;ii++){
686 for(int jj=-1;jj<=1;jj++){
687 int id2=idx(i+ii,j+jj);
688 vector<pair<int,int> > same_bonds;
689 vector<pair<int,int> > diff_bonds;
690 if((ii==0) && (jj==0) && (i!=0) && (j!=0)){ // intra-cell, not for boundary
691 same_bonds=m_data[id].getBondsSame(gid,tol);
692 diff_bonds=m_data[id].getBondsDiff(gid,tol);
693 } else if(id2>id){ // inter-cell
694 same_bonds=m_data[id].getBondsSame(gid,tol,m_data[id2]);
695 diff_bonds=m_data[id].getBondsDiff(gid,tol,m_data[id2]);
696 }
697 // insert intra-cluster bonds
698 for(vector<pair<int,int> >::iterator iter=same_bonds.begin();
699 iter!=same_bonds.end();
700 iter++){
701 if(m_bonds[btag1].find(*iter)==m_bonds[btag1].end()){
702 m_bonds[btag1].insert(*iter);
703 }
704 }
705 // insert inter-cluster bonds
706 for(vector<pair<int,int> >::iterator iter=diff_bonds.begin();
707 iter!=diff_bonds.end();
708 iter++){
709 if(m_bonds[btag2].find(*iter)==m_bonds[btag2].end()){
710 m_bonds[btag2].insert(*iter);
711 }
712 }
713 }
714 }
715 }
716 }
717 }
718
719
720
721 /*!
722 insert bond between particles with given ID
723
724 \param id1 id of 1st particle
725 \param id2 id of 2nd particle
726 \param btag bond tag
727 */
728 void MNTable2D::insertBond(int id1,int id2,int btag)
729 {
730 if(id1<id2){
731 m_bonds[btag].insert(make_pair(id1,id2));
732 } else {
733 m_bonds[btag].insert(make_pair(id2,id1));
734 }
735 }
736
737 /*!
738 tag all particles close to a line
739
740 \param L the line
741 \param dist max. distance
742 \param tag the tag
743 \param gid group Id
744 */
745 void MNTable2D::tagParticlesAlongLine(const Line2D& L,double dist,int tag,unsigned int gid)
746 {
747 for(int i=1;i<m_nx-1;i++){
748 for(int j=1;j<m_ny-1;j++){
749 vector<Sphere*> vs=m_data[idx(i,j)].getSpheresNearObject(&L,dist,gid);
750 for(vector<Sphere*>::iterator iter=vs.begin();
751 iter!=vs.end();
752 iter++){
753 (*iter)->setTag(tag);
754 }
755 }
756 }
757 }
758
759 /*!
760 tag all particles close to a line
761
762 \param L the line
763 \param dist max. distance
764 \param tag the tag
765 \param the tag mask
766 \param gid group Id
767 */
768 void MNTable2D::tagParticlesAlongLineWithMask(const Line2D& L,double dist,int tag,int mask,unsigned int gid)
769 {
770 for(int i=1;i<m_nx-1;i++){
771 for(int j=1;j<m_ny-1;j++){
772 vector<Sphere*> vs=m_data[idx(i,j)].getSpheresNearObject(&L,dist,gid);
773 for(vector<Sphere*>::iterator iter=vs.begin();
774 iter!=vs.end();
775 iter++){
776 int oldtag=(*iter)->Tag();
777 int newtag=(tag & mask) | (oldtag & ~mask);
778 (*iter)->setTag(newtag);
779 }
780 }
781 }
782 }
783
784 /*!
785 tag all particles close to a line segment
786
787 \param L the line segment
788 \param dist max. distance
789 \param tag the tag
790 \param mask the tag mask
791 \param gid group Id
792 */
793 void MNTable2D::tagParticlesAlongLineSegment(const LineSegment2D& L,double dist,int tag ,int mask, unsigned int gid)
794 {
795 for(int i=1;i<m_nx-1;i++){
796 for(int j=1;j<m_ny-1;j++){
797 vector<Sphere*> vs=m_data[idx(i,j)].getSpheresNearObject(&L,dist,gid);
798 for(vector<Sphere*>::iterator iter=vs.begin();
799 iter!=vs.end();
800 iter++){
801 int oldtag=(*iter)->Tag();
802 int newtag=(tag & mask) | (oldtag & ~mask);
803 (*iter)->setTag(newtag);
804 }
805 }
806 }
807 }
808
809 /*!
810 break bonds along a line segment
811
812 \param L the line segment
813 \param dist max. distance from the line
814 \param tag the bond tag
815 \param gid group Id
816 */
817 void MNTable2D::breakBondsAlongLineSegment(const LineSegment2D& L,double dist,int tag, unsigned int gid)
818 {
819 // get particles near line
820 set<int> near_particle_set;
821
822 for(int i=1;i<m_nx-1;i++){
823 for(int j=1;j<m_ny-1;j++){
824 vector<Sphere*> vs=m_data[idx(i,j)].getSpheresNearObject(&L,dist,gid);
825 for(vector<Sphere*>::iterator iter=vs.begin();
826 iter!=vs.end();
827 iter++){
828 near_particle_set.insert((*iter)->Id());
829 }
830 }
831 }
832
833 // for all bonds with tag
834 set<pair<int,int> >::iterator iter=m_bonds[tag].begin();
835 while(iter!=m_bonds[tag].end()){
836 // check if both particles near line
837 if(near_particle_set.find(iter->first)!=near_particle_set.end() &&
838 near_particle_set.find(iter->second)!=near_particle_set.end()){
839 // remove
840 set<pair<int,int> >::iterator iter_t=iter;
841 iter++;
842 m_bonds[tag].erase(iter_t);
843 } else {
844 iter++;
845 }
846 }
847 }
848
849 /*!
850 Set output style
851
852 \param style the output style: 0 = debug, 1 = .geo
853 */
854 void MNTable2D::SetOutputStyle(int style)
855 {
856 MNTable2D::s_output_style=style;
857 }
858
859 /*!
860 Set output precision
861
862 \param prec number of significant digits
863 */
864 void MNTable2D::SetOutputPrecision(int prec)
865 {
866 m_write_prec=prec;
867 }
868
869 /*!
870 Output the content of a MNTable2D to an ostream. The output format depends on the value of MNTable2D::s_output_style (see MNTable2D::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 MNTable2D::s_output_style is set to 2, the output format is VTK-XML.
871
872 \param ost the output stream
873 \param T the table
874 */
875 ostream& operator << (ostream& ost,const MNTable2D& T)
876 {
877 if(MNTable2D::s_output_style==0){ // debug style
878 // don't print padding
879 MNTCell::SetOutputStyle(0);
880 for(int i=1;i<T.m_nx;i++){
881 for(int j=1;j<T.m_ny-1;j++){
882 ost << "=== Cell " << i << " , " << j << " === " << endl;
883 ost << T.m_data[T.idx(i,j)];
884 }
885 }
886 } else if (MNTable2D::s_output_style==1){ // geo style
887 int nparts=0;
888 for(int i=1;i<T.m_nx-1;i++){
889 for(int j=1;j<T.m_ny-1;j++){
890 nparts+=T.m_data[T.idx(i,j)].NParts();
891 }
892 }
893 // header
894 ost << "LSMGeometry 1.2" << endl;
895 ost << "BoundingBox " << T.m_x0+T.m_celldim << " " << T.m_y0+T.m_celldim << " 0.0 " << T.m_x0+double(T.m_nx-1)*T.m_celldim << " " << T.m_y0+double(T.m_ny-1)*T.m_celldim << " 0.0 " << endl;
896 ost << "PeriodicBoundaries " << T.m_x_periodic << " " << T.m_y_periodic << " 0" << endl;
897 ost << "Dimension 2D" << endl;
898 // particles
899 ost << "BeginParticles" << endl;
900 ost << "Simple" << endl;
901 ost << nparts << endl;
902 MNTCell::SetOutputStyle(1);
903 for(int i=1;i<T.m_nx-1;i++){
904 for(int j=1;j<T.m_ny-1;j++){
905 ost << T.m_data[T.idx(i,j)];
906 }
907 }
908 ost << "EndParticles" << endl;
909
910 // bonds
911 ost << "BeginConnect" << endl;
912 // sum bonds
913 int nbonds=0;
914 for(map<int,set<pair<int,int> > >::const_iterator iter=T.m_bonds.begin();
915 iter!=T.m_bonds.end();
916 iter++){
917 nbonds+=iter->second.size();
918 }
919 ost << nbonds << endl;
920 for(map<int,set<pair<int,int> > >::const_iterator iter=T.m_bonds.begin();
921 iter!=T.m_bonds.end();
922 iter++){
923 for(set<pair<int,int> >::const_iterator v_iter=iter->second.begin();
924 v_iter!=iter->second.end();
925 v_iter++){
926 ost << v_iter->first << " " << v_iter->second << " " << iter->first << endl;
927 }
928 }
929 ost << "EndConnect" << endl;
930 } else if (MNTable2D::s_output_style==2){ // vtk-xml
931 T.WriteAsVtkXml(ost);
932 }
933
934 return ost;
935 }
936
937 /*!
938 output content as VTK-XML format
939
940 \param vtkfile the output stream
941 */
942 void MNTable2D::WriteAsVtkXml(ostream& vtkfile) const
943 {
944 // inverse mapping index<->particle id
945 map<int,int> inv_map;
946
947 int pcount=0;
948 for(int i=1;i<m_nx-1;i++){
949 for(int j=1;j<m_ny-1;j++){
950 vector<int> ids=m_data[idx(i,j)].getIdList();
951 for(vector<int>::iterator iter=ids.begin();
952 iter!=ids.end();
953 iter++){
954 inv_map.insert(make_pair(*iter,pcount));
955 pcount++;
956 }
957 }
958 }
959
960 // get bond count
961 int bcount=0;
962
963 for(map<int,set<pair<int,int> > >::const_iterator iter=m_bonds.begin();
964 iter!=m_bonds.end();
965 iter++){
966 bcount+=iter->second.size();
967 }
968 // write header
969 vtkfile << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\">\n";
970 vtkfile << "<UnstructuredGrid>\n";
971 int nparts=0;
972 for(int i=1;i<m_nx-1;i++){
973 for(int j=1;j<m_ny-1;j++){
974 nparts+=m_data[idx(i,j)].NParts();
975 }
976 }
977 vtkfile << "<Piece NumberOfPoints=\"" << nparts << "\" NumberOfCells=\"" << bcount << "\">\n";
978 // write particle pos
979 vtkfile << "<Points>\n";
980 vtkfile << "<DataArray NumberOfComponents=\"3\" type=\"Float64\" format=\"ascii\">\n";
981 for(int i=1;i<m_nx-1;i++){
982 for(int j=1;j<m_ny-1;j++){
983 m_data[idx(i,j)].writePositions(vtkfile);
984 }
985 }
986 vtkfile << endl;
987 vtkfile << "</DataArray>\n";
988 vtkfile << "</Points>\n";
989
990 // --- write particle data ---
991 // radius
992 vtkfile << "<PointData Scalars=\"radius\">\n";
993 vtkfile << "<DataArray type=\"Float64\" Name=\"radius\" NumberOfComponents=\"1\" format=\"ascii\">\n";
994 for(int i=1;i<m_nx-1;i++){
995 for(int j=1;j<m_ny-1;j++){
996 m_data[idx(i,j)].writeRadii(vtkfile);
997 }
998 }
999 vtkfile << endl;
1000 vtkfile << "</DataArray>\n";
1001
1002 // ID
1003 vtkfile << "<DataArray type=\"Int32\" Name=\"particleID\" NumberOfComponents=\"1\" format=\"ascii\">\n";
1004 for(int i=1;i<m_nx-1;i++){
1005 for(int j=1;j<m_ny-1;j++){
1006 m_data[idx(i,j)].writeIDs(vtkfile);
1007 }
1008 }
1009 vtkfile << endl;
1010 vtkfile << "</DataArray>\n";
1011
1012 // Tag
1013 vtkfile << "<DataArray type=\"Int32\" Name=\"particleTag\" NumberOfComponents=\"1\" format=\"ascii\">\n";
1014 for(int i=1;i<m_nx-1;i++){
1015 for(int j=1;j<m_ny-1;j++){
1016 m_data[idx(i,j)].writeTags(vtkfile);
1017 }
1018 }
1019 vtkfile << endl;
1020 vtkfile << "</DataArray>\n";
1021
1022 vtkfile << "</PointData>\n";
1023
1024 // --- write cells section ---
1025 // connections
1026 vtkfile << "<Cells>\n";
1027 vtkfile << "<DataArray type=\"Int32\" NumberOfComponents=\"1\" Name=\"connectivity\" format=\"ascii\">\n";
1028 for(map<int,set<pair<int,int> > >::const_iterator iter=m_bonds.begin();
1029 iter!=m_bonds.end();
1030 iter++){
1031 for(set<pair<int,int> >::const_iterator iter_b=iter->second.begin();
1032 iter_b!=iter->second.end();
1033 iter_b++){
1034 vtkfile << inv_map[iter_b->first] << " " << inv_map[iter_b->second] << endl;
1035 }
1036 }
1037
1038 vtkfile << "</DataArray>\n";
1039 // offsets
1040 vtkfile << "<DataArray type=\"Int32\" NumberOfComponents=\"1\" Name=\"offsets\" format=\"ascii\">\n";
1041 for (size_t i = 1; i < bcount*2; i+=2) vtkfile << i+1 << "\n";
1042 vtkfile << "</DataArray>\n";
1043
1044 // element type
1045 vtkfile << "<DataArray type=\"UInt8\" NumberOfComponents=\"1\" Name=\"types\" format=\"ascii\">\n";
1046 const int CELL_LINE_TYPE = 3;
1047 for (size_t i = 0; i < bcount; i++) vtkfile << CELL_LINE_TYPE << endl;
1048 vtkfile << "</DataArray>\n";
1049 vtkfile << "</Cells>\n";
1050
1051 // cell data
1052 vtkfile << "<CellData>\n";
1053 vtkfile << "<DataArray type=\"Int32\" Name=\"bondTag\" NumberOfComponents=\"1\" format=\"ascii\">\n";
1054 for(map<int,set<pair<int,int> > >::const_iterator iter=m_bonds.begin();
1055 iter!=m_bonds.end();
1056 iter++){
1057 for(set<pair<int,int> >::const_iterator iter_b=iter->second.begin();
1058 iter_b!=iter->second.end();
1059 iter_b++){
1060 vtkfile << iter->first << endl;
1061 }
1062 }
1063 vtkfile << "</DataArray>\n";
1064 vtkfile << "</CellData>\n";
1065 // write footer
1066 vtkfile << "</Piece>\n";
1067 vtkfile << "</UnstructuredGrid>\n";
1068 vtkfile << "</VTKFile>\n";
1069 }
1070
1071 void MNTable2D::write(char *filename, int outputStyle)
1072 {
1073 ofstream outFile;
1074
1075 // set output precision
1076 outFile.precision(m_write_prec);
1077
1078 SetOutputStyle(outputStyle);
1079
1080 outFile.open(filename);
1081 outFile << *(this);
1082 outFile.close();
1083 }
1084
1085 boost::python::list MNTable2D::getSphereListFromGroup(int gid) const
1086 {
1087 boost::python::list l;
1088 vector<const Sphere*> sphere_vector;
1089
1090 sphere_vector = getAllSpheresFromGroup(gid);
1091
1092 for (vector<const Sphere*>::iterator iter=sphere_vector.begin(); iter!= sphere_vector.end(); iter++) {
1093 l.append(*(*(iter)));
1094 }
1095
1096 return l;
1097 }
1098
1099 /*!
1100 return the bonds of a given tag as list of <int,int> pairs
1101
1102 \param btag the bond tag
1103 */
1104 boost::python::list MNTable2D::getBondList(int btag)
1105 {
1106 boost::python::list l;
1107
1108 for(set<pair<int,int> >::const_iterator iter=m_bonds[btag].begin();
1109 iter!=m_bonds[btag].end();
1110 iter++){
1111 l.append(boost::python::make_tuple(iter->first,iter->second));
1112 }
1113 return l;
1114 }