Debian Codegraph @

src/HexAggregateInsertGenerator2DRand.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 "HexAggregateInsertGenerator2DRand.h"
14 // --- System includes ---
15
16 #include <cmath>
17 #include <cstdlib>
18 #include <sys/time.h>
19
20 using std::ceil;
21 using std::sqrt;
22 using std::sin;
23 using std::cos;
24
25 // --- project includes ---
26 #include "sphere_fitting/Sphere2DFitter.h"
27
28 HexAggregateInsertGenerator2DRand::HexAggregateInsertGenerator2DRand()
29 {}
30
31 /*!
32 Constructor
33
34 \param rmin minimum particle radius
35 \param rmax maximum particle radius
36 \param ntries max. nr. of tries to insert particle
37 \param max_iter maximum iterations within the iterative solvers
38 \param prec max. error in iterative solvers
39 \param rprob probablility (0..1) that one of the particles is removed
40 */
41 HexAggregateInsertGenerator2DRand::HexAggregateInsertGenerator2DRand(double rmin,
42 double rmax,
43 int tries,
44 int max_iter,
45 double prec,
46 double rprob) :
47 HexAggregateInsertGenerator2D(rmin,rmax,tries,max_iter,prec)
48 {
49 m_remove_prob=rprob;
50 struct timeval tv;
51 gettimeofday(&tv,NULL);
52 int random_seed=tv.tv_usec;
53 srand(random_seed);
54 }
55
56
57 /*!
58 seed the area with particles
59
60 \param vol a pointer to the packing volume
61 \param ntable a pointer to the neighbour table used
62 \param gid particle group id
63 \param tag the particle tag
64 */
65 void HexAggregateInsertGenerator2DRand::seedParticles(AVolume2D* vol,MNTable2D*ntable ,int gid,int tag)
66 {
67 std::cout << "HexAggregateInsertGenerator2DRand::seedParticles" << std::endl;
68 // get bounding box
69 pair<Vector3,Vector3> bbx=vol->getBoundingBox();
70 double dx=(bbx.second.X()-bbx.first.X())-2.0*m_rmax;
71 double dy=(bbx.second.Y()-bbx.first.Y())-2.0*m_rmax;
72 // get index limits for seeding
73 int imax=int(ceil(dx/(m_rmax*2.0)));
74 int jmax=int(ceil(dy/(m_rmax*sqrt(3.0))));
75 // seed positions
76 for(int i=0;i<imax;i++){
77 for(int j=0;j<jmax;j++){
78 // get position
79 double px=bbx.first.X()+m_rmax+(double(i)+0.5*double(j%2))*m_rmax*2.0;
80 double py=bbx.first.Y()+m_rmax+double(j)*sqrt(3.0)*m_rmax;
81
82 // get dist to egde
83 double dex=(bbx.second.X()-px) < (px-bbx.first.X()) ? bbx.second.X()-px : px-bbx.first.X();
84 double dey=(bbx.second.Y()-py) < (py-bbx.first.Y()) ? bbx.second.Y()-py : py-bbx.first.Y();
85
86 double de=(dex<dey) ? dex : dey;
87
88 // check max rad.
89 if(de>m_rmin){
90 // calc random radius
91 double r;
92 if(de<m_rmax) {
93 r=m_rmin+((de-m_rmin)*((double)(rand())/(double)(RAND_MAX)));
94 } else {
95 r=m_rmin+((m_rmax-m_rmin)*((double)(rand())/(double)(RAND_MAX)));
96 }
97 Sphere S(Vector3(px,py,0.0),r);
98 bool fit=vol->isIn(S) && ntable->checkInsertable(S,gid);
99 if(fit){
100 // -- insert 7 smaller particles instead of 1 large
101 double rn=r/3.0; // new radii
102 // center sphere
103 Sphere Sc(Vector3(px,py,0.0),rn);
104 Sc.setTag(tag);
105 ntable->insertChecked(Sc,gid);
106 int Sc_id=Sc.Id();
107 // outer spheres
108 int Sk_id[6];
109 // generate random value
110 double rval=((double)(rand())/(double)(RAND_MAX));
111 int npart;
112 if(rval>m_remove_prob){
113 // if random value exceed removal probability - all 6 particles
114 npart = 6;
115 } else {
116 npart = 5;
117 Sk_id[5]=-1;
118 }
119 for(int k=0;k<npart;k++){
120 double phi=double(k)*1.04719551; // k*pi/3
121 double pxk=px+2.0000*rn*sin(phi);
122 double pyk=py+2.0000*rn*cos(phi);
123 Sphere Sk(Vector3(pxk,pyk,0.0),rn*0.9999);
124 if(vol->isIn(Sk) && ntable->checkInsertable(Sk,gid)){
125 Sk.setTag(tag);
126 ntable->insert(Sk,gid);
127 Sk_id[k]=Sk.Id();
128 ntable->insertBond(Sc_id,Sk_id[k],0); // bond between center and outer
129 } else {
130 Sk_id[k]=-1;
131 }
132 }
133 for(int k=0;k<npart;k++){
134 int k2=(k+1) % 6;
135 if((Sk_id[k]!=-1) && (Sk_id[k2]!=-1)) {
136 ntable->insertBond(Sk_id[k],Sk_id[k2],0);
137 }
138 }
139 }
140 }
141 }
142 }
143 }
144
145 /*!
146 seed the area with particles
147
148 \param vol a pointer to the packing volume
149 \param ntable a pointer to the neighbour table used
150 \param gid particle group id
151 \param tag the particle tag
152 */
153 void HexAggregateInsertGenerator2DRand::fillIn(AVolume2D* vol,MNTable2D* ntable ,int gid,int tag)
154 {
155 Sphere nsph;
156
157 int total_tries=0;
158 int count_insert=0;
159
160 int nvol=vol->getNumberSubVolumes();
161 for(int ivol=0;ivol<nvol;ivol++){
162 int countfail=0; // number of failed attenpts since last successfull insertion
163 while(countfail<m_max_tries){
164 bool findfit=false;
165 Vector3 P=vol->getAPoint(ivol); // get random point within volume
166 multimap<double,const Sphere*> close_particles=ntable->getSpheresClosestTo(P,3); // get 3 nearest spheres
167 map<double,const Line2D*> close_lines=vol->getClosestPlanes(P,2); // get 2 nearest planes
168
169 map<double,const AGeometricObject*> geomap;
170 geomap.insert(close_particles.begin(),close_particles.end());
171 geomap.insert(close_lines.begin(),close_lines.end());
172
173 if(geomap.size()>=3){
174 map<double,const AGeometricObject*>::iterator iter=geomap.begin();
175 const AGeometricObject* GO1=iter->second;iter++;
176 const AGeometricObject* GO2=iter->second;iter++;
177 const AGeometricObject* GO3=iter->second;
178 nsph=FitSphere2D(GO1,GO2,GO3,P,m_max_iter,m_prec);
179 findfit=true;
180 }
181
182 if(findfit){
183 // check if within radius range
184 bool is_radius=(m_rmin<nsph.Radius()) && (m_rmax>nsph.Radius());
185
186 // check inside volume, intersections ...
187 bool fit=vol->isIn(nsph) && ntable->checkInsertable(nsph,gid);
188
189 if(fit && is_radius){ // acceptable particle
190 // -- insert 7 smaller particles instead of 1 large
191 double rn=nsph.Radius()/3.0; // new radii
192 double px=nsph.Center().X();
193 double py=nsph.Center().Y();
194 // center sphere
195 Sphere Sc(Vector3(px,py,0.0),rn);
196 Sc.setTag(tag);
197 ntable->insertChecked(Sc,gid);
198 int Sc_id=Sc.Id();
199 // outer sphere
200 int Sk_id[6];
201 // generate random value
202 double rval=((double)(rand())/(double)(RAND_MAX));
203 int npart;
204 if(rval>m_remove_prob){
205 // if random value exceed removal probability - all 6 particles
206 npart = 6;
207 } else {
208 npart = 5;
209 Sk_id[5]=-1;
210 }
211 for(int k=0;k<npart;k++){
212 double phi=double(k)*1.04719551; // k*pi/3
213 double pxk=px+2.0*rn*sin(phi);
214 double pyk=py+2.0*rn*cos(phi);
215 Sphere Sk(Vector3(pxk,pyk,0.0),rn*0.9999);
216 Sk.setTag(tag);
217 if(vol->isIn(Sk) && ntable->checkInsertable(Sk,gid)){
218 Sk.setTag(tag);
219 ntable->insert(Sk,gid);
220 Sk_id[k]=Sk.Id();
221 ntable->insertBond(Sc_id,Sk_id[k],0); // bond between center and outer
222 } else {
223 Sk_id[k]=-1;
224 }
225 }
226 for(int k=0;k<npart;k++){
227 int k2=(k+1) % 6;
228 if((Sk_id[k]!=-1) && (Sk_id[k2]!=-1)) {
229 ntable->insertBond(Sk_id[k],Sk_id[k2],0);
230 }
231 }
232 count_insert++;
233 if((count_insert%100)==0) std::cout << "inserted: " << count_insert << std::endl;
234 total_tries+=countfail;
235 if(countfail>m_max_tries/10) std::cout << countfail << " tries" << std::endl;
236 countfail=0; // reset failure counter
237 } else countfail++;
238 } else countfail++;
239 }
240 }
241 std::cout << "total tries: " << total_tries << std::endl;
242 }