Memosa-FVM  0.2
CellMark_impl.cpp
Go to the documentation of this file.
1 // This file os part of FVM
2 // Copyright (c) 2012 FVM Authors
3 // See LICENSE file for terms.
4 
5 #include "CellMark_impl.h"
6 #include "Mesh.h"
7 #include <iostream>
8 #include <string>
9 
10 
13 
16 
17 void CellMark_Impl(Mesh& mesh, const GeomFields& geomFields, const string fileBase,
18  Octree& O, MPM& solid, const int option)
19 
20 {
21 
22  const StorageSite& cells = mesh.getCells();
23  const int nCells = cells.getCount(); //need to include BC cells?
24  const int nSelfCells = cells.getSelfCount();
25  const VecD3Array& cellCentroid = dynamic_cast<const VecD3Array& > (geomFields.coordinate[cells]);
26  const StorageSite& faces = mesh.getFaces();
27  const int nFaces = faces.getCount();
28  const CRConnectivity& faceCells = mesh.getAllFaceCells();
29  const CRConnectivity& cellFaces = mesh.getCellFaces();
30  const CRConnectivity& cellCells = mesh.getCellCells();
31  const VecD3Array& faceArea =
32  dynamic_cast<const VecD3Array&>(geomFields.area[faces]);
33  const VecD3Array& faceCentroid =
34  dynamic_cast<const VecD3Array&> (geomFields.coordinate[faces]);
35 
36 
37  const int writeOption = 0;
38  /***************************************************************************************/
39  //---build up Octree for cell centroid---//
40 
41  /*
42  Point *points = new Point [nCells];
43 
44  for(int i=0; i<nCells; i++){
45  points[i].coordinate=cellCentroid[i];
46  points[i].cellIndex=i;
47  points[i].code=0;
48  }
49 
50  //Octree build up parameters
51 
52  const int threshold=1;
53  const int maxDepth=20;
54  const int count=nCells;
55  int currentDepth=0;
56 
57  //define a new Octree object
58 
59  // Octree O;
60 
61  //calculate entire domain bounds
62 
63  Bounds bounds=O.calcCubicBounds(points, count);
64 
65  //build Octree
66  O.build(points, count, threshold, maxDepth, bounds, currentDepth);
67 
68 #if 0
69  //Report Octree
70  const string fileName1=fileBase+"Octree_report.dat";
71  char* file1;
72  file1=&fileName1[0];
73  FILE *fp;
74  fp=fopen(file1,"w");
75  O.report(fp);
76  fclose(fp);
77 #endif
78 
79  */
80  //---so far, we have the Octree ready for search---//
81 
82 
83 
84  /**************************************************************************************/
85  /*
86  //---set up MPM particles---//
87  string fileName2=fileBase+"MPMs.dat";
88  char* file2;
89  file2=&fileName2[0];
90 
91 
92  // MPM solid;
93  //initialize particle velocity and coordinate and write into file
94  solid.setandwriteParticles(file2);
95 
96  //get coordinate
97  const shared_ptr<VecD3Array> MPM_Coordinates = solid.readCoordinates(file2);
98 
99  //get velocity
100  const shared_ptr<VecD3Array> MPM_Velocities = solid.readVelocities(file2);
101 
102  //get type
103  const shared_ptr<Array<int> > MPM_Types = solid.readTypes(file2);
104 
105  //store all the information in MPM class
106  solid.Init(MPM_Coordinates, MPM_Velocities, MPM_Types);
107  */
108 
109 
110 
111  const StorageSite& particles = solid.getParticles();
112 
113  const int nMPM = particles.getCount();
114 
115  //cout<<"nMPM is "<<nMPM<<endl;
116 
117  const shared_ptr<VecD3Array>& MPM_Points = solid.getCoordinates();
118  const shared_ptr<VecD3Array>& MPM_Vels = solid.getVelocities();
119  const shared_ptr<Array<int> >& particleTypes = solid.getTypes();
120 
121 
122 
123  cout<<"count of cells is "<<nCells<<endl;
124  /***************************************************************************************/
125  //---Find out each solid point falls to which cells---//
126 
127  //a temporary array to store point to cell connectivity
128  Array<int> MPM_PointstoCells (nMPM);
129 
130  //search options:
131  //option 1: search the nearest cell to this particle and see if this particle falls to
132  //--->this cell; if not, search the neighbors of this cell, until find the cell which
133  //--->contains this particle
134  //option 2: search the cells within a radius to this particle using Octree,
135  //---> this radius is pre-estimated so that it must include the cell which contains
136  //---> the particle. Loop over the cells within this radius and find the cell containi//ng the particle
137  //option 3: naive search over all cells to find out the cells within the radius, then
138  //---> find out which cell contains the particle
139  //option 4: naive search all the cells and find out which cell contains the particle
140 
141  if (option == 1){
142  for(int p=0; p<nMPM; p++){
143  const VecD3 MPM_point = (*MPM_Points)[p];
144  MPM_PointstoCells[p]=-1;
145  const int nearestCell = O.getNode(MPM_point);
146  int flag=0;
147  int inCellorNot=inCell(nearestCell, MPM_point, faceCells, cellFaces,
148  faceArea,faceCentroid);
149  if (inCellorNot==1) {
150  flag = 1;
151  MPM_PointstoCells[p]=nearestCell;
152  }
153  int levelCount = 0;
154  while (flag == 0 && levelCount <= 2){
155  levelCount ++;
156  const int nc = cellCells.getCount(nearestCell);
157  for (int c = 0; c < nc; c ++){
158  int cellCandidate = cellCells(nearestCell, c);
159  inCellorNot=inCell(cellCandidate, MPM_point, faceCells, cellFaces,
160  faceArea,faceCentroid);
161  if (inCellorNot==1) {
162  flag = 1;
163  MPM_PointstoCells[p]=cellCandidate;
164  }
165  // if (inCellorNot == 0){
166  //flag = 1;
167  //}
168  }
169  }
170  }
171  }
172 
173 
174 
175  if (option == 2){
176  const double radius = 2.5;
177  for(int p=0; p<nMPM; p++){
178  const VecD3 MPM_point = (*MPM_Points)[p];
179  vector<int> cellIndexList;
180  O.getNodes(MPM_point, radius, cellIndexList);
181  MPM_PointstoCells[p]=-1;
182  for (int i=0; i< (int) cellIndexList.size(); i++) {
183  int cellCandidate = cellIndexList[i];
184  const int inCellorNot=inCell(cellCandidate, MPM_point, faceCells, cellFaces,
185  faceArea,faceCentroid);
186  if (inCellorNot==1){
187  MPM_PointstoCells[p]=cellCandidate;
188  }
189  }
190  }
191  }
192  /*
193  if (option == 3){
194  double radius = 0.03;
195  for(int p=0; p<nMPM; p++){
196  VecD3 MPM_point = (*MPM_Points)[p];
197  vector<int> cellIndexList=O.Naive_getNodes(MPM_point, count, points, radius);
198  MPM_PointstoCells[p]=-1;
199  for (int i=0; i< (int) cellIndexList.size(); i++) {
200  int cellCandidate = cellIndexList[i];
201  const int inCellorNot=inCell(cellCandidate, MPM_point, faceCells, cellFaces,
202  faceArea,faceCentroid);
203  if (inCellorNot==1){
204  MPM_PointstoCells[p]=cellCandidate;
205  }
206  }
207  }
208  }
209  */
210  if (option == 4){
211  for(int p=0; p<nMPM; p++){
212  VecD3 MPM_point = (*MPM_Points)[p];
213  MPM_PointstoCells[p]=-1;
214  for(int i=0; i< nCells; i++) {
215  const int inCellorNot=inCell(i, MPM_point, faceCells, cellFaces,
216  faceArea,faceCentroid);
217  if (inCellorNot==1){
218  MPM_PointstoCells[p]=i;
219  }
220  }
221  }
222  }
223 
224 
225 if (writeOption ==1)
226  {
227  FILE *fp;
228  string fileName20=fileBase+"particles.dat";
229  char* file20;
230  file20=&fileName20[0];
231 
232  fp=fopen(file20,"w");
233  for (int c=0; c<nMPM; c++){
234  if((*particleTypes)[c]==1){
235  fprintf(fp, "%i\t%e\t%e\t%e\n", c, (*MPM_Points)[c][0],(*MPM_Points)[c][1],(*MPM_Points)[c][2]);
236  }
237  }
238  }
239 
240  /************************************************************************************/
241  //---create the CRconnectivity for solid and cells---//
242 
243  shared_ptr<CRConnectivity> particleCellsCR =
244  setParticleCells(particles, cells, MPM_PointstoCells);
245 
246  //store the CRconnectivity to mesh class
247 
248  mesh.setConnectivity( particles, cells, particleCellsCR);
249 
250  const CRConnectivity& particleCells = mesh.getConnectivity(particles, cells);
251 
252 if (writeOption ==1)
253  {
254  FILE *fp;
255  string fileName14=fileBase+"particletocells.dat";
256  char* file14;
257  file14=&fileName14[0];
258 
259  fp=fopen(file14,"w");
260 
261  //test
262  for(int c=0; c<nMPM; c++){
263  //for each solid point, find out how many cells have connectivity to it
264  int np = particleCells.getCount(c);
265  //what they are
266  for(int p=0; p<np; p++){
267  fprintf(fp, "%i\t%i\n", c, particleCells(c, p));
268  }
269  }
270  fclose(fp);
271  }
272  /************************************************************************************/
273  //---create the CRconnectivity for cells and solid---//
274 
275  shared_ptr<CRConnectivity> cellParticlesCR = (*particleCellsCR).getTranspose();
276 
277  //store the CRconnectivity to mesh class
278 
279  mesh.setConnectivity(cells, particles, cellParticlesCR);
280 
281  const CRConnectivity& cellParticles = mesh.getConnectivity(cells, particles);
282 
283 
284 if (writeOption ==1)
285  {
286  FILE *fp;
287  string fileName13=fileBase+"celltoparticles.dat";
288  char* file13;
289  file13=&fileName13[0];
290 
291  fp=fopen(file13,"w");
292  //test
293  for(int c=0; c<nCells; c++){
294  //for each solid point, find out how many cells have connectivity to it
295  int np = cellParticles.getCount(c);
296  //what they are
297  for(int p=0; p<np; p++){
298  //if (cellParticles(c,p) < 0)
299 
300  int pID = cellParticles(c,p);
301  if ((*particleTypes)[pID] == 1)
302  fprintf(fp,"%i\t%e\t%e\t%e\t%i\n", c, (*MPM_Points)[pID][0],(*MPM_Points)[pID][1],(*MPM_Points)[pID][2],(*particleTypes)[pID]);
303  }
304  }
305  fclose(fp);
306  }
307 
308 
309 
310  /**************************mark cell**********************************/
311  markCell ( mesh, nCells,nSelfCells, cellParticles, cellCells);
312 
313  //test mark cell
314 
315  if (writeOption ==1)
316  reportCellMark (mesh, nCells, cellCentroid, fileBase);
317 
318 
319 
320  /***************************mark IBFaces********************/
321 
322  markIBFaces (mesh, nFaces, faceCells);
323 
324  const StorageSite& ibFaces = mesh.getIBFaces();
325 
326  const Array<int>& ibFaceList = mesh.getIBFaceList();
327 
328 if (writeOption ==1)
329  {
330  FILE *fp;
331  string fileName15=fileBase+"ibfaces.dat";
332  char* file15;
333  file15=&fileName15[0];
334 
335  fp=fopen(file15,"w");
336  //test
337  for(int f=0; f<ibFaces.getCount(); f++){
338  int fID = ibFaceList[f];
339  fprintf(fp,"%i\t%e\t%e\t%e\n", fID, faceCentroid[fID][0],faceCentroid[fID][1],faceCentroid[fID][2]);
340  }
341  fclose(fp);
342  }
343  /*********check ibFaces***********************/
344  checkIBFaces(ibFaceList, faceArea, faceCells, mesh);
345 
346 
347  /************create ibFace to Particle connectivity****************/
348 
349  shared_ptr<CRConnectivity> ibFaceParticlesCR =
350  setibFaceParticles (mesh, ibFaces, ibFaceList, particles,faceCells, cellParticles, cellCells, *particleTypes);
351 
352  //store the connectivity to mesh
353 
354  mesh.setConnectivity(ibFaces, particles, ibFaceParticlesCR);
355 
356  const CRConnectivity& ibFaceParticles = mesh.getConnectivity(ibFaces, particles);
357 
358 
359  /************************create ibFace to Cells connectivity**********/
360 
361  shared_ptr<CRConnectivity> ibFaceCellsCR =
362  setibFaceCells (mesh, ibFaceList, ibFaces, cells, faceCells, cellFaces, faceCentroid);
363 
364  //store the CRconnectivity to mesh class
365 
366  mesh.setConnectivity(ibFaces, cells, ibFaceCellsCR);
367 
368  const CRConnectivity& ibFaceCells = mesh.getConnectivity(ibFaces, cells);
369 
370 if (writeOption ==1)
371  {
372  FILE *fp;
373  string fileName11=fileBase+"ibfacetoparticle.dat";
374  char* file11;
375  file11=&fileName11[0];
376 
377  fp=fopen(file11,"w");
378  //test
379  for(int f=0; f<ibFaces.getCount(); f++){
380  const int faceIndex = ibFaceList[f];
381  //cout << faceCentroid[faceIndex] << endl;
382  //for each ibface find out how many fluid cells have connectivity to it
383  int nc = ibFaceParticles.getCount(f);
384  //what they are
385  for(int c=0; c<nc; c++){
386  int pID = ibFaceParticles(f,c);
387  //cout<<"face "<<faceIndex<<" has "<<ibFaceParticles(f, c)<<endl;
388  //cout<<(*MPM_Points)[ibFaceParticles(f,c)]<<endl;
389  fprintf(fp, "%i\t%i\t%e\t%e\t%e\t%i\n", faceIndex,pID, (*MPM_Points)[pID][0],(*MPM_Points)[pID][1],(*MPM_Points)[pID][2],(*particleTypes)[pID]);
390  //fprintf(fp, "%i\t%i\t%lf\t%lf\t%lf\t%i\n", faceIndex,pID, (*MPM_Vels)[pID][0],(*MPM_Vels)[pID][1],(*MPM_Vels)[pID][2],(*particleTypes)[pID]);
391  }
392  }
393 
394 
395  string fileName12=fileBase+"ibfacetocell.dat";
396  char* file12;
397  file12=&fileName12[0];
398 
399  fp=fopen(file12,"w");
400  //test
401  for(int f=0; f<ibFaces.getCount(); f++){
402  const int faceIndex = ibFaceList[f];
403  //cout << faceCentroid[faceIndex] << endl;
404  //for each ibface find out how many fluid cells have connectivity to it
405  int nc = ibFaceCells.getCount(f);
406  //what they are
407  for(int c=0; c<nc; c++){
408  //cout<<"face "<<faceIndex<<" has "<<ibFaceCells(f, c)<<endl;
409  //cout<<(cellCentroid)[ibFaceCells(f,c)]<<endl;
410  int cID = ibFaceCells(f,c);
411  fprintf(fp, "%i\t%i\t%e\t%e\t%e\n",faceIndex,cID, (cellCentroid)[cID][0],(cellCentroid)[cID][1],(cellCentroid)[cID][2]);
412  }
413  }
414  fclose(fp);
415  }
416 
417 
418 }
void checkIBFaces(const Array< int > &ibFaceList, const VectorT3Array &faceArea, const CRConnectivity &faceCells, const Mesh &mesh)
Definition: CellMark.cpp:212
int getCount(const int i) const
const shared_ptr< Array< VecD3 > > & getCoordinates()
Definition: MPM_Particles.h:33
int getSelfCount() const
Definition: StorageSite.h:40
const StorageSite & getIBFaces() const
Definition: Mesh.h:111
Field coordinate
Definition: GeomFields.h:19
const CRConnectivity & getConnectivity(const StorageSite &from, const StorageSite &to) const
Definition: Mesh.cpp:416
void CellMark_Impl(Mesh &mesh, const GeomFields &geomFields, const string fileBase, Octree &O, MPM &solid, const int option)
const shared_ptr< CRConnectivity > setParticleCells(const StorageSite &rowSite, const StorageSite &colSite, const Array< int > &connectivity)
Definition: CellMark.cpp:456
const Array< int > & getIBFaceList() const
Definition: Mesh.cpp:686
Definition: Mesh.h:49
Octree::Point Point
const shared_ptr< Array< VecD3 > > & getVelocities()
Definition: MPM_Particles.h:35
void setConnectivity(const StorageSite &rowSite, const StorageSite &colSite, shared_ptr< CRConnectivity > conn)
Definition: Mesh.cpp:352
void markCell(Mesh &mesh, const int nCells, const int nSelfCells, const CRConnectivity &cellParticles, const CRConnectivity &cellCells)
Definition: CellMark.cpp:77
void markIBFaces(Mesh &mesh, const int nFaces, const CRConnectivity &faceCells)
Definition: CellMark.cpp:166
const CRConnectivity & getAllFaceCells() const
Definition: Mesh.cpp:378
const CRConnectivity & getCellFaces() const
Definition: Mesh.cpp:454
Definition: Octree.h:22
Array< VecD3 > VecD3Array
const StorageSite & getParticles() const
Definition: MPM_Particles.h:27
Octree::Bounds Bounds
void getNodes(const VectorT3 coordinate, const double radius, vector< int > &cellList)
Get all objects closest to a x/y/z within a radius. search mechanism similar to getNode(coordinate) ...
Definition: Octree.cpp:466
const StorageSite & getFaces() const
Definition: Mesh.h:108
const CRConnectivity & getCellCells() const
Definition: Mesh.cpp:480
const StorageSite & getCells() const
Definition: Mesh.h:109
Definition: Array.h:14
const int getNode(const double x, const double y, const double z)
Definition: Octree.cpp:441
int getCount() const
Definition: StorageSite.h:39
Field area
Definition: GeomFields.h:23
int inCell(const int cellIndex, const VectorT3 &point, const CRConnectivity &faceCells, const CRConnectivity &cellFaces, const VectorT3Array &faceArea, const VectorT3Array &faceCentroid)
Definition: CellMark.cpp:11
void reportCellMark(const Mesh &mesh, const int nCells, const VectorT3Array &cellCentroid, const string fileBase)
Definition: CellMark.cpp:118
const shared_ptr< CRConnectivity > setibFaceCells(const Mesh &mesh, const Array< int > &ibFaceList, const StorageSite &ibFaces, const StorageSite &cells, const CRConnectivity &faceCells, const CRConnectivity &cellFaces, const VecD3Array &faceCentroid)
Definition: CellMark.cpp:357
const shared_ptr< Array< int > > & getTypes()
Definition: MPM_Particles.h:39
const shared_ptr< CRConnectivity > setibFaceParticles(const Mesh &mesh, const StorageSite &ibFaces, const Array< int > &ibFaceList, const StorageSite &particles, const CRConnectivity &faceCells, const CRConnectivity &cellParticles, const CRConnectivity &cellCells, const Array< int > &particleType)
Definition: CellMark.cpp:243
Vector< double, 3 > VecD3