Debian Codegraph @

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