Debian Codegraph @

src/CircMNTableXY2D.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 "CircMNTableXY2D.h"
14
15 // --- System includes ---
16 #include <cmath>
17
18 // --- IO includes ---
19 #include <iostream>
20
21 using std::endl;
22
23 using std::floor;
24
25 CircMNTableXY2D::CircMNTableXY2D()
26 {}
27
28 /*!
29 Construct CircMNTableXY2D.
30
31 \param MinPt minimum point (z component ignored)
32 \param MaxPt maximum point (z component ignored)
33 \param cd cell dimension
34 \param ngroups initial number of particle groups
35 */
36 CircMNTableXY2D::CircMNTableXY2D(const Vector3& MinPt,const Vector3& MaxPt,double cd,unsigned int ngroups):
37 CircMNTable2D(MinPt, MaxPt, cd, ngroups)
38 {
39 set_y_circ();
40
41 // check if grid spacing fits size in circular direction:
42 double ny=(MaxPt-MinPt).Y()/m_celldim;
43 // error message if not
44 if(ny!=floor(ny)){
45 std::cout << "WARNING! grid spacing " << m_celldim << " doesn't fit periodic y-dimension " << (MaxPt-MinPt).Y() << std::endl;
46 }
47 double shift_y=(m_ny-2)*m_celldim;
48 m_shift_vec_y=Vector3(0.0,shift_y,0.0);
49 }
50
51 /*!
52 Destruct CircMNTable2D. Just calls MNTable2D destructor.
53 */
54 CircMNTableXY2D::~CircMNTableXY2D()
55 {}
56
57 /*!
58 set circularity of y-dimension to 1
59 */
60 void CircMNTableXY2D::set_y_circ()
61 {
62 m_y_periodic=1;
63 }
64
65 /*!
66 get the cell index for a given position
67
68 \param Pos the position
69 \return the cell index if Pos is inside the table, -1 otherwise
70 */
71 int CircMNTableXY2D::getIndex(const Vector3& Pos) const
72 {
73 int ret;
74
75 int ix=int(floor((Pos.x()-m_x0)/m_celldim));
76 int iy=int(floor((Pos.y()-m_y0)/m_celldim));
77
78 // check if pos is in table excl. padding
79 if((ix>=0) && (ix<=m_nx-1) && (iy>=0) && (iy<=m_ny-1)){
80 ret=idx(ix,iy);
81 } else {
82 ret=-1;
83 }
84
85 return ret;
86 }
87
88 /*!
89 Insert sphere. Insert clone into other side of Table.
90
91 \param S the Sphere
92 \param gid the group id
93 */
94 bool CircMNTableXY2D::insert(const Sphere& S,unsigned int gid)
95 {
96 bool res;
97
98 int id=getIndex(S.Center());
99 int idx=getXIndex(S.Center());
100 int idy=getYIndex(S.Center());
101
102
103 if((id!=-1) && (idx!=0) && (idx!=m_nx-1) && (idy!=0) && (idy!=m_ny-1) && (gid<m_ngroups)){ // valid index
104 // insert sphere
105 m_data[id].insert(S,gid);
106 res=true;
107 int xidx=getXIndex(S.Center());
108 int yidx=getYIndex(S.Center());
109 // insert x-clone
110 if (xidx==1){
111 Sphere SClone=S;
112 SClone.shift(m_shift_vec_x);
113 int clone_id=getFullIndex(SClone.Center());
114 m_data[clone_id].insert(SClone,gid);
115 } else if (xidx==m_nx-2){
116 Sphere SClone=S;
117 SClone.shift(-1.0*m_shift_vec_x);
118 int clone_id=getFullIndex(SClone.Center());
119 m_data[clone_id].insert(SClone,gid);
120 }
121 // insert y-clone
122 if (yidx==1){
123 Sphere SClone=S;
124 SClone.shift(m_shift_vec_y);
125 int clone_id=getFullIndex(SClone.Center());
126 m_data[clone_id].insert(SClone,gid);
127 } else if (yidx==m_ny-2){
128 Sphere SClone=S;
129 SClone.shift(-1.0*m_shift_vec_y);
130 int clone_id=getFullIndex(SClone.Center());
131 m_data[clone_id].insert(SClone,gid);
132 }
133 } else {
134 res=false;
135 }
136
137 return res;
138 }
139
140
141 /*!
142 insert sphere if it doesn't collide with other spheres
143
144 \param S the Sphere
145 \param gid the group id
146 */
147 bool CircMNTableXY2D::checkInsertable(const Sphere& S,unsigned int gid)
148 {
149 bool res;
150
151 int id=getIndex(S.Center());
152 int idx=getXIndex(S.Center());
153 int idy=getYIndex(S.Center());
154
155 if((id!=-1) && (idx!=0) && (idx!=m_nx-1) && (idy!=0) && (idy!=m_ny-1) && (gid<m_ngroups)){
156 multimap<double,const Sphere*> close_spheres=getSpheresFromGroupNear(S.Center(),S.Radius()-s_small_value,gid);
157 if(close_spheres.size()==0){
158 res=true;
159 } else res=false;
160 } else {
161 res=false;
162 }
163
164 return res;
165 }
166
167
168 /*!
169 Insert sphere if it doesn't collide with other spheres. Insert clone into other side of Table.
170
171 \param S the Sphere
172 \param gid the group id
173 */
174 bool CircMNTableXY2D::insertChecked(const Sphere& S,unsigned int gid,double tol)
175 {
176 bool res;
177 int id=getIndex(S.Center());
178 int idx=getXIndex(S.Center());
179 int idy=getYIndex(S.Center());
180
181 // std::cout << "CircMNTable2D::insertChecked(" << S << ") id=" << id << " idx= " << idx << std::endl;
182
183 tol+=s_small_value;
184 if((id!=-1) && (idx!=0) && (idx!=m_nx-1) && (idy!=0) && (idy!=m_ny-1) && (gid<m_ngroups)){
185 // insert original
186 multimap<double,const Sphere*> close_spheres=getSpheresFromGroupNear(S.Center(),S.Radius()-tol,gid);
187 if(close_spheres.size()==0){
188 m_data[id].insert(S,gid);
189 res=true;
190 } else res=false;
191 // insert x-clone
192 if (idx==1){
193 Sphere SClone=S;
194 SClone.shift(m_shift_vec_x);
195 multimap<double,const Sphere*> close_spheres=getSpheresFromGroupNear(SClone.Center(),SClone.Radius()-tol,gid);
196 if(close_spheres.size()==0){
197 int clone_id=getFullIndex(SClone.Center());
198 m_data[clone_id].insert(SClone,gid);
199 }
200 } else if (idx==m_nx-2){
201 Sphere SClone=S;
202 SClone.shift(-1.0*m_shift_vec_x);
203 multimap<double,const Sphere*> close_spheres=getSpheresFromGroupNear(SClone.Center(),SClone.Radius()-tol,gid);
204 if(close_spheres.size()==0){
205 int clone_id=getFullIndex(SClone.Center());
206 m_data[clone_id].insert(SClone,gid);
207 }
208 }
209 // insert y-clone
210 if (idy==1){
211 Sphere SClone=S;
212 SClone.shift(m_shift_vec_y);
213 multimap<double,const Sphere*> close_spheres=getSpheresFromGroupNear(SClone.Center(),SClone.Radius()-tol,gid);
214 if(close_spheres.size()==0){
215 int clone_id=getFullIndex(SClone.Center());
216 m_data[clone_id].insert(SClone,gid);
217 }
218 } else if (idy==m_ny-2){
219 Sphere SClone=S;
220 SClone.shift(-1.0*m_shift_vec_y);
221 multimap<double,const Sphere*> close_spheres=getSpheresFromGroupNear(SClone.Center(),SClone.Radius()-tol,gid);
222 if(close_spheres.size()==0){
223 int clone_id=getFullIndex(SClone.Center());
224 m_data[clone_id].insert(SClone,gid);
225 }
226 }
227 } else {
228 res=false;
229 }
230
231 return res;
232 }
233
234 /*!
235 Generate bonds between particles of a group. Takes cloned particles into account.
236
237 \param gid the group ID
238 \param tol max. difference between bond length and equilibrium dist.
239 \param btag bond tag
240 */
241 void CircMNTableXY2D::generateBonds(int gid,double tol,int btag)
242 {
243 std::cout << "CircMNTableXY2D::generateBonds( " << gid << " , " << tol << " , " << btag << " )" << std::endl;
244 // loop over all cells
245 for(int i=0;i<m_nx-1;i++){
246 for(int j=0;j<m_ny-1;j++){
247 int id=idx(i,j);
248 for(int ii=-1;ii<=1;ii++){
249 for(int jj=-1;jj<=1;jj++){
250 int id2=idx(i+ii,j+jj);
251 vector<pair<int,int> > bonds;
252 if(((ii+jj)==0)){ // intra-cell, not for boundary
253 bonds=m_data[id].getBonds(gid,tol);
254 } else if(id2>id){ // inter-cell
255 bonds=m_data[id].getBonds(gid,tol,m_data[id2]);
256 }
257 for(vector<pair<int,int> >::iterator iter=bonds.begin();
258 iter!=bonds.end();
259 iter++){
260 m_bonds[btag].insert(*iter);
261 }
262 }
263 }
264 }
265 }
266 }
267
268 /*!
269 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.
270
271 \param ost the output stream
272 \param T the table
273 */
274 ostream& operator << (ostream& ost,const CircMNTableXY2D& T)
275 {
276 if(CircMNTableXY2D::s_output_style==0){ // debug style
277 // don't print padding
278 MNTCell::SetOutputStyle(0);
279 for(int i=0;i<T.m_nx;i++){
280 for(int j=0;j<T.m_ny-1;j++){
281 ost << "=== Cell " << i << " , " << j << " === " << endl;
282 ost << T.m_data[T.idx(i,j)];
283 }
284 }
285 } else if (CircMNTable2D::s_output_style==1){ // geo style
286 int nparts=0;
287 for(int i=1;i<T.m_nx-1;i++){
288 for(int j=1;j<T.m_ny-1;j++){
289 nparts+=T.m_data[T.idx(i,j)].NParts();
290 }
291 }
292 // header
293 ost << "LSMGeometry 1.2" << endl;
294 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;
295 ost << "PeriodicBoundaries " << T.m_x_periodic << " " << T.m_y_periodic << " 0" << endl;
296 ost << "Dimension 2D" << endl;
297 // particles
298 ost << "BeginParticles" << endl;
299 ost << "Simple" << endl;
300 ost << nparts << endl;
301 MNTCell::SetOutputStyle(1);
302 for(int i=1;i<T.m_nx-1;i++){
303 for(int j=1;j<T.m_ny-1;j++){
304 ost << T.m_data[T.idx(i,j)];
305 }
306 }
307 ost << "EndParticles" << endl;
308 // bonds
309 ost << "BeginConnect" << endl;
310 // sum bonds
311 int nbonds=0;
312 for(map<int,set<pair<int,int> > >::const_iterator iter=T.m_bonds.begin();
313 iter!=T.m_bonds.end();
314 iter++){
315 nbonds+=iter->second.size();
316 }
317 ost << nbonds << endl;
318 for(map<int,set<pair<int,int> > >::const_iterator iter=T.m_bonds.begin();
319 iter!=T.m_bonds.end();
320 iter++){
321 for(set<pair<int,int> >::const_iterator v_iter=iter->second.begin();
322 v_iter!=iter->second.end();
323 v_iter++){
324 ost << v_iter->first << " " << v_iter->second << " " << iter->first << endl;
325 }
326 }
327 ost << "EndConnect" << endl;
328 } else if (CircMNTable2D::s_output_style==2){ // vtk-xml
329 T.WriteAsVtkXml(ost);
330 }
331
332 return ost;
333 }