File: RandomAssembly2D.cpp

package info (click to toggle)
esys-particle 2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,284 kB
  • sloc: cpp: 77,304; python: 5,647; makefile: 1,176; sh: 10
file content (230 lines) | stat: -rw-r--r-- 6,409 bytes parent folder | download
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
/////////////////////////////////////////////////////////////
//                                                         //
// Copyright (c) 2003-2011 by The University of Queensland //
// Earth Systems Science Computational Centre (ESSCC)      //
// http://www.uq.edu.au/esscc                              //
//                                                         //
// Primary Business: Brisbane, Queensland, Australia       //
// Licensed under the Open Software License version 3.0    //
// http://www.opensource.org/licenses/osl-3.0.php          //
//                                                         //
/////////////////////////////////////////////////////////////

#include "Foundation/console.h"

#include "Geometry/RandomAssembly2D.h"

//-- project includes --
#include "Foundation/vec3.h"
#include "Geometry/SimpleParticle.h"

//-- STL includes --
#include <map>
#include <utility>

using std::map;
using std::pair;

//-- IO includes --
#include <iostream>

using std::cout;
using std::endl;


/*!
  get closest plane to a particle

  \param Po the particle
*/
Line *ARandomAssembly2D::getClosestPlane(const SimpleParticle& Po)
{
  //cout << "getClosestPlane : " << Po.getPos() << endl;
  vector<Line>::iterator PL=Borders.begin();

  Vec3 PoPos=Po.getPos();
  double dist=PL->sep(PoPos);
  //cout << "plane: " << PL.GetO() << PL.GetN() << dist << endl;
  for(vector<Line>::iterator iter=Borders.begin();iter!=Borders.end();iter++){
    double ndist=iter->sep(PoPos);
    //cout << "plane: " << iter->GetO() << iter->GetN() << ndist << endl;
    if(ndist<dist){
      PL=iter;
      dist=ndist;
    }
  }
  //cout << "closest plane: " << PL.GetO() << PL.GetN() << dist << endl;

  return &(*PL);
}


/*!
  Find a fit for a sphere using the list of neigbor list and a plane

  \param Po the particle to fit
  \param NL the list of neighbors
  \param PL the Plane

  \todo check for at least 2 particles
*/
bool ARandomAssembly2D::findAFit(SimpleParticle& Po, const vector<SimpleParticle>& NL, const Line& L)
{
  bool find_a_fit ;
  Vec3 M;
  double r;
  int id=Po.getID();

  Vec3 Pos1=NL[0].getPos();
  Vec3 Pos2=NL[1].getPos();
  Vec3 WallO=L.GetO();
  Vec3 WallD=L.GetU();

  find_a_fit=Sphere2D::FillInWP(Pos1,Pos2,WallO,WallD,NL[0].getRad(),NL[1].getRad(),M,r);

  Po=SimpleParticle(M,r,id);
  
  //cout << "fit with plane : " << L.GetO() << endl;

  return find_a_fit ;
}

/*!
  Find a fit for a sphere using the list of neigbors

  \param Po the particle to fit
  \param NL the list of neighbors

  \todo check for at least 3 particles
*/
bool ARandomAssembly2D::findAFit(SimpleParticle& Po, const vector<SimpleParticle>& NL)
{
  bool find_a_fit ;
  Vec3 M;
  double r;
  int id=Po.getID();

  Vec3 Pos1=NL[0].getPos();
  Vec3 Pos2=NL[1].getPos();
  Vec3 Pos3=NL[2].getPos();
  find_a_fit=Sphere2D::FillIn(Pos1,Pos2,Pos3,NL[0].getRad(),NL[1].getRad(),NL[2].getRad(),M,r);

  Po=SimpleParticle(M,r,id);

  return find_a_fit ;
}


/*!
  check if Po is within the Space and is not crossing any boundary or 
  overlapping with other particles.

  \param Po the particle
*/
bool ARandomAssembly2D::checkAFit(const SimpleParticle& Po)
{
  bool fail=false;
 
  // check vs. radius
  if((Po.getRad()<m_rmin) || (Po.getRad()>m_rmax)) {
    fail=true;
  }
  // check vs. borders
  double px=Po.getPos().X();
  double py=Po.getPos().Y();
  // double r=Po.getRad(); unused.
  if((px<m_xmin-m_small_value) || (px-m_small_value>m_xmax) || (py<m_ymin-m_small_value) || (py-m_small_value>m_ymax)){
    fail=true;
    //cout << "Fail : outside" << endl;
  }
  // check vs. all neighbors
  if(!fail){
    vector<SimpleParticle> NL=getNeighborList(Po); // get the NL here because Po may have moved during fit
    vector<SimpleParticle>::const_iterator iter=NL.begin();
    while(!fail && iter!=NL.end()){
      double dist=(Po.getPos()-iter->getPos()).norm()+m_small_value;;
      if(dist<(Po.getRad()+iter->getRad())){
        fail=true;
        //cout << "Fail : particle collision" << endl;
      }
      iter++;
    }
  }
  // check vs. closest plane
  if(!fail){
    Line *L=getClosestPlane(Po);
    fail=(Po.getRad()-L->sep(Po.getPos()))>m_small_value;
  }
  
  return !fail ;
}


    
/*!
  Fill the space in the skeleton after it has been seeded

  \param tries the number of tries
*/
void ARandomAssembly2D::fillSpace(int tries)
{
  int countfail=0;
  int countfound=0;
  //bool fail;
  
  while(countfail<tries){
    bool findfit=false;
    Vec3 P=getAPoint();
    double r=m_random(m_rmin,m_rmax);
    SimpleParticle Po=SimpleParticle(P,r,getNParts());
    vector<SimpleParticle> T3=getClosestNeighbors(Po,3); // get closest neighbors (max 3) 
    Line* L=getClosestPlane(Po);                            // get closest plane/line
    if(T3.size()>2){ // 3 neighbors, closest 
      SimpleParticle Pi=T3[0];
      double ndist=(Po.getPos()-Pi.getPos()).norm();
      if( ndist==0.0){
        findfit=false;
      } else {
        if( ndist < Pi.getRad()){ // if Po inside Pi -> move Po to the surface of Pi
          Vec3 npos=Pi.getPos()+((Po.getPos()-Pi.getPos())*(Pi.getRad()/ndist));
          Po.moveTo(npos);
        }
        Vec3 PoPos=Po.getPos();
        double dist_p=L->sep(PoPos);
        double dist_3=(PoPos-T3[2].getPos()).norm()-T3[2].getRad();
        if(dist_p>dist_3){  // 3rd particle closer than plane -> fit 3 particles
          findfit=findAFit(Po,T3);
        } else { // plane closer than 3rd particle -> fit 2 particles + plane
          findfit=findAFit(Po,T3,*L);
        }
      }
    } else if(T3.size()==2) { // 2 neighbors  -> try  2 particles + plane
      SimpleParticle Pi=T3[0];
      double ndist=(Po.getPos()-Pi.getPos()).norm();
      if( ndist==0.0){
        findfit=false;
      } else {
        if( ndist < Pi.getRad()){ // if Po inside Pi -> move Po to the surface of Pi
          Vec3 npos=Pi.getPos()+((Po.getPos()-Pi.getPos())*(Pi.getRad()/ndist));
          Po.moveTo(npos);
        }
        findfit=findAFit(Po,T3,*L);
      }
    } 
    if(findfit){ // found something, check
      findfit=checkAFit(Po);
    } 
    if(findfit){  // found & checked -> insert
      insertParticle(Po);
      //cout << "found particle " << Po.getID() << " after " << countfail << " tries" << endl;
      countfail=0;
      countfound++;
    } else {
      countfail++;
    }
  }
  
  console.Info() << "inserted " << countfound << " Particles" << "\n";
}