Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
adj.cpp
Go to the documentation of this file.
1 /* *******************************************************************
2  * Illinois Open Source License *
3  * *
4  * University of Illinois/NCSA *
5  * Open Source License *
6  * *
7  * Copyright@2008, University of Illinois. All rights reserved. *
8  * *
9  * Developed by: *
10  * *
11  * Center for Simulation of Advanced Rockets *
12  * *
13  * University of Illinois *
14  * *
15  * www.csar.uiuc.edu *
16  * *
17  * Permission is hereby granted, free of charge, to any person *
18  * obtaining a copy of this software and associated documentation *
19  * files (the "Software"), to deal with the Software without *
20  * restriction, including without limitation the rights to use, *
21  * copy, modify, merge, publish, distribute, sublicense, and/or *
22  * sell copies of the Software, and to permit persons to whom the *
23  * Software is furnished to do so, subject to the following *
24  * conditions: *
25  * *
26  * *
27  * @ Redistributions of source code must retain the above copyright *
28  * notice, this list of conditions and the following disclaimers. *
29  * *
30  * @ Redistributions in binary form must reproduce the above *
31  * copyright notice, this list of conditions and the following *
32  * disclaimers in the documentation and/or other materials *
33  * provided with the distribution. *
34  * *
35  * @ Neither the names of the Center for Simulation of Advanced *
36  * Rockets, the University of Illinois, nor the names of its *
37  * contributors may be used to endorse or promote products derived *
38  * from this Software without specific prior written permission. *
39  * *
40  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
41  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
42  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
43  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
44  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
45  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
46  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
47  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
48  *********************************************************************
49  * Please acknowledge The University of Illinois Center for *
50  * Simulation of Advanced Rockets in works and publications *
51  * resulting from this software or its derivatives. *
52  *********************************************************************/
53 /*
54 Utilities to help determine which nodes overlap,
55 where, and with whom.
56 
57 Orion Sky Lawlor, olawlor@acm.org, 5/30/2001
58 */
59 #include <stdio.h>
60 #include "makeflo.h"
61 #include "face.h"
62 
63 /************* Memory allocation pools **********/
66 
67 /************** AdjRec ***************/
68 void adjRec::print(void)
69 {
70  printf("%d",b->getBlock()->getBlockNumber()+1);
71  if (next!=NULL) {
72  printf(" -> "); next->print();
73  } else
74  printf(".\n");
75 }
76 
77 /****************** adjList *******************/
78 
80  adjRec *cur=next;
81  while (cur!=NULL) {
82  adjRec *old=cur;
83  cur=cur->getNext();
84  delete old;
85  }
86 }
87 int adjList::getLength(void) const {
88  int ret=0;
89  for (adjRec *cur=next;cur!=NULL;cur=cur->getNext())
90  ret++;
91  return ret;
92 }
93 bool adjList::hasFace(const face *test) const {
94  for (adjRec *cur=next;cur!=NULL;cur=cur->getNext())
95  if (test==cur->getFace())
96  return true;
97  return false;
98 }
99 bool adjList::hasLoc(const face *test,const blockLoc &l) const {
100  for (adjRec *cur=next;cur!=NULL;cur=cur->getNext())
101  if (test==cur->getFace() && l==cur->getLoc())
102  return true;
103  return false;
104 }
105 void adjList::addFace(face *b,const blockLoc &l)
106 {
107  if (!hasLoc(b,l))
108  next=new adjRec(b,l,next);
109 }
110 //Find our location in this block
111 const blockLoc &adjList::getLoc(const face *in) const
112 {
113  for (adjRec *cur=next;cur!=NULL;cur=cur->getNext())
114  if (in==cur->getFace())
115  return cur->getLoc();
116  static blockLoc badLoc(-1,-1,-1);
117  return badLoc;
118 }
119 
120 void adjList::print(void) const {
121  if (next) next->print();
122  else printf("Empty.\n");
123 }
124 
125 /******************* Node *********************/
126 /*Return the total number of hops to go from the origin
127 to the location "dir".
128 */
129 inline int nHops(const blockLoc &dir) {
130  return abs(dir[0])+abs(dir[1])+abs(dir[2]);
131 }
132 
133 //Return some face present in all 4 nodes,
134 // but different from notHim. Optionally return the
135 // destination location and orientations
137  const face *notHim,
138  blockLoc *retLoc,
139  blockLoc *oX, blockLoc *oY)
140 {
141 /*Nodes in "n" array should be laid out like:
142  [0] [1]
143  [2] [3]
144 */
145  const int nNodes=4;
146  const int expectedHops[nNodes]={0,1,1,2};
147  int l;
148 
149  //Check each entry in list 0,
150  // and return the first that is present everywhere.
151  for (adjRec *test=n[0]->next;test!=NULL;test=test->getNext()) {
152  face *tFace=test->getFace();
153 
154  //Check if it's the "bad" face
155  if (tFace==notHim) continue;
156 
157  //Check if it's in every list at some orientation
158  blockLoc orientX,orientY;
159  const blockLoc &tLoc=test->getLoc();
160  bool isPresent=true;
161  for (l=1;l<nNodes && isPresent;l++)
162  { //Check this list for the location
163  bool inList=false;
164  for (adjRec *c=n[l]->next;c!=NULL;c=c->getNext()) {
165  if (c->getFace()!=tFace)
166  continue;//Keep looking
167  blockLoc dir=c->getLoc()-tLoc;
168  //Make sure it's the proper number of hops to this location:
169  if (nHops(dir)==expectedHops[l]) {
170  inList=true;
171  if (l==1) orientX=dir; //X axis
172  if (l==2) orientY=dir; //Y axis
173  }
174  }
175  if (!inList) isPresent=false;
176  }
177 
178  //This location is in every list-- return it
179  if (isPresent) {
180  if (retLoc!=NULL) *retLoc=tLoc;
181  if (oX!=NULL) *oX=orientX;
182  if (oY!=NULL) *oY=orientY;
183  return tFace;
184  }
185  }
186  //If we get here, the intersection is empty--
187  // must be a boundary node
188  return NULL;
189 }
190 
191 
192 /************** nodeMatcher *************/
193 /*This value gives the quantization for nodes--
194  nodes less distant than this will be considered
195  identical. Because this value is used to round
196  the input coodinates to integers, extremely tiny
197  values may cause integer saturation on 32-bit
198  machines. For any usable coordinate x, we need
199  scale*x < 1e9 (since 2^31 is approx. 2e9 and we want + and -)
200 */
201 const double initNodeQuant=1.0e-6;//limits coordinates to < 1000
203 static int originMapsTo=(1<<30); //Origin (0) maps here; keeps the sign bit free
205 
206 /*Make sure our quantization is coarse enough to not overflow
207 if given a vector of this size.
208 */
210  const double safetyFactor=5; //<- Needed since we only check the corners
211  double mag=safetyFactor*v.mag();
212  while (mag*scale>1.0e9) {//If our hashableVectors would overflow:
213  scale*=0.1; //Use a smaller scale (coarser quantization)
214  double newQuant=1.0/scale;
215  offset=originMapsTo+0.5*newQuant;
216  printf("Encountered large node coordinates: quantizing to %.3g\n",newQuant);
217  }
218 }
219 
221 {
222  x=(int)(scale*v.x+offset);
223  y=(int)(scale*v.y+offset);
224  z=(int)(scale*v.z+offset);
225 }
226 
227 
228 //Map this location to a node.
229 // Creates a new node there if none exists
231  hashableVector3d hLoc(loc);
232  node *n=map.get(hLoc);
233  if (n==NULL)
234  { //Node is not yet in table-- add it
235  n=new node(loc);
236  map.put(hLoc)=n;
237  }
238  return n;
239 }
240 
241 /************** Block ***************/
242 static int blockBytes=0; //To keep track of memory usage
243 
244 block::block(const blockDim &dim_,int orig,int blockNo_,vector3d *nodeLocs_)
245  :dim(dim_), originalNo(orig),blockNo(blockNo_), nodeLocs(nodeLocs_)
246 {
249  blockBytes+=dim.getSize()*sizeof(nodeLocs[0]);
250  for (int i=0;i<nFaces;i++)
251  faces[i]=0;
252 }
253 
255 {
256  for (int i=0;i<nFaces;i++)
257  delete faces[i];
258  delete[] nodeLocs;
259 }
260 
261 //Add an external boundary condition at the given locations
262 void block::addBC(const blockSpan &span,int bcNo)
263 {
264  //Find the face the BC applies to
265  int faceNo=span.getFace();
266  if (faceNo==-1) {fprintf(stderr,"Boundary condition is volumetric!\n");abort();}
267 
268  //Apply the BC to that face
269  BCs[faceNo].push_back(externalBCpatch(NULL,this,span,bcNo));
270 }
271 
272 //Create the 6 faces of our block
274  int nx=dim[0];
275  int ny=dim[1];
276  int nz=dim[2];
277  faces[0]=new face(this,map,BCs[0],
278  1,2,blockSpan(blockLoc(0,0,0),blockLoc(1,ny,nz)));
279  faces[1]=new face(this,map,BCs[1],
280  0,2,blockSpan(blockLoc(0,0,0),blockLoc(nx,1,nz)));
281  faces[2]=new face(this,map,BCs[2],
282  0,1,blockSpan(blockLoc(0,0,0),blockLoc(nx,ny,1)));
283  faces[3]=new face(this,map,BCs[3],
284  1,2,blockSpan(blockLoc(nx-1,0,0),blockLoc(nx,ny,nz)));
285  faces[4]=new face(this,map,BCs[4],
286  0,2,blockSpan(blockLoc(0,ny-1,0),blockLoc(nx,ny,nz)));
287  faces[5]=new face(this,map,BCs[5],
288  0,1,blockSpan(blockLoc(0,0,nz-1),blockLoc(nx,ny,nz)));
289 }
290 
291 //This table lists human-readable names for
292 // the faces of each block, in storage order.
293 const char *block::face2name[block::nFaces]={
294  "iMin","jMin","kMin",
295  "iMax","jMax","kMax"
296 };
297 
298 /************** BlockReader **********/
300  const blockDim &dim,//Dimentions of incoming block
301  vector3d *locs) //X,Y,Z coordinates (ni x nj x nk)
302 {
303  block *b=new block(dim,curBlock,curBlock,locs);
304  curBlock++;
305 
306  for (int axis=0;axis<3;axis++)
307  if ((dim[axis]-1)&parameters.levelBad) {
308  fprintf(stderr,"ERROR! Block %d's side is %d long.\n",
309  curBlock,dim[axis]-1);
311  }
312 
313  blocks.push_back(b);
314  return NULL;
315 }
317  { /*The block will free its locs, not us*/ }
318 
319 
320 /************** Unit Test Harness **************/
321 
322 #ifdef STANDALONE
323 /*Unit test driver program:
324  Prints out the patches for each block in a
325 human-readable fashion.
326 */
327 
328 template <class T>
329 void poolStats(const allocPool<T> &p) {
330  printf("%.1f MB: (%d blocks serving %d allocd/%d freed)\n",
331  sizeof(T)*p.allocCount*1.0e-6,
333 }
334 
335 void memStats(void) {
336  printf("blocks> %.1f MB\n",blockBytes*1.0e-6);
337  printf("adjRecs> ");poolStats(adjRec::pool);
338  printf("nodes> ");poolStats(node::pool);
339 }
340 
341 void printSpan(const blockLoc &a,const blockLoc &b)
342 {
343  printf("(%d %d) (%d %d) (%d %d)\n",
344  1+a[0],1+b[0],1+a[1],1+b[1],1+a[2],1+b[2]);
345 }
346 
347 void printPatch(const patch &p)
348 {
349  printf(" %d's ",
350  p.src->getBlockNumber()+1);
351  printSpan(p.srcStart,p.srcEnd);
352  if (p.dest==NULL)
353  printf(" -> exterior\n");
354  else {
355  printf(" -> %d's ",p.dest->getBlockNumber()+1);
356  printSpan(p.destStart,p.destEnd);
357  printf(" Orient=(%d %d %d)\n",
358  1+p.orient[0],1+p.orient[1],1+p.orient[2]);
359  }
360 }
361 
362 int main(int argc,char *argv[])
363 {
364  if (argc<2) {printf("Usage: test <.grd file>\n"); return 1;}
365  vector<block *> blocks;
366  blockReader r(blocks);
367  const char *err;
368  printf("Starting read...\n");
369  if (NULL!=(err=r.read(argv[1])))
370  printf("ERROR! %s\n",err);
371  else
372  printf("File read successfully.\n");
373 
374  nodeMatcher map;
375 
376  //Print out the patches of each block
377  for (int bn=0;bn<blocks.size();bn++) {
378  memStats();
379  block *b=blocks[bn];
380  printf("Block %d:\n",b->getBlockNumber()+1);
381  b->buildFaces(map);
382  for (int f=0;f<block::nFaces;f++) {
383  printf("%d> Face %d:\n",b->getBlockNumber()+1,f);
384  vector<patch> patches;
385  b->faces[f]->getPatches(patches);
386  for (int p=0;p<patches.size();p++)
387  printPatch(patches[p]);
388  }
389  }
390 
391  return 0;
392 }
393 
394 #endif
395 
396 
397 
398 
399 
400 
401 
402 
403 
404 
405 
406 
const double initNodeQuant
Definition: adj.cpp:201
void addBC(const blockSpan &span, int bcNo)
Definition: adj.cpp:262
const vector< patch * > & getPatches(void) const
Definition: face.h:145
Definition: face.h:90
vector< externalBCpatch > BCs[nFaces]
Definition: adj.h:240
void addFace(face *b, const blockLoc &l)
Definition: adj.cpp:105
static allocPool< adjRec > pool
Definition: adj.h:108
face * faces[nFaces]
Definition: adj.h:241
block * getBlock(void) const
Definition: face.h:140
static int originMapsTo
Definition: adj.cpp:203
node * loc2node(const vector3d &loc)
Definition: adj.cpp:230
real x
Definition: vector3d.h:88
int nHops(const blockLoc &dir)
Definition: adj.cpp:129
void multigridError(void)
Definition: split.cpp:69
void print(void) const
Definition: adj.cpp:120
static double offset
Definition: adj.h:181
block * src
Definition: patch.h:78
real y
Definition: vector3d.h:88
Definition: adj.h:150
~block()
Definition: adj.cpp:254
*********************************************************************Illinois Open Source License ****University of Illinois NCSA **Open Source License University of Illinois All rights reserved ****Developed free of to any person **obtaining a copy of this software and associated documentation to deal with the Software without including without limitation the rights to and or **sell copies of the and to permit persons to whom the **Software is furnished to do subject to the following this list of conditions and the following disclaimers ****Redistributions in binary form must reproduce the above **copyright this list of conditions and the following **disclaimers in the documentation and or other materials **provided with the distribution ****Neither the names of the Center for Simulation of Advanced the University of nor the names of its **contributors may be used to endorse or promote products derived **from this Software without specific prior written permission ****THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY **EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES **OF FITNESS FOR A PARTICULAR PURPOSE AND **NONINFRINGEMENT IN NO EVENT SHALL THE CONTRIBUTORS OR **COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN AN ACTION OF TORT OR **ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE **USE OR OTHER DEALINGS WITH THE SOFTWARE v
Definition: roccomf90.h:20
int bufferCount
Definition: adj.h:82
CkHashtableTslow< hashableVector3d, node * > map
Definition: adj.h:195
bool hasFace(const face *test) const
Definition: adj.cpp:93
static allocPool< node > pool
Definition: adj.h:153
Definition: adj.h:203
vector< block * > & blocks
Definition: adj.h:260
Definition: patch.h:74
static void printSpan(FILE *out, const blockSpan &sp)
const blockLoc & getLoc(const face *b) const
Definition: adj.cpp:111
static const char * face2name[nFaces]
Definition: adj.h:236
static void checkVector(const vector3d &v)
Definition: adj.cpp:209
int allocCount
Definition: adj.h:82
adjRec * getNext(void)
Definition: adj.h:117
blockLoc i
Definition: read.cpp:79
void test(void)
Definition: flotsam.C:99
static double scale
Definition: adj.h:181
bool hasLoc(const face *test, const blockLoc &l) const
Definition: adj.cpp:99
const NT & n
virtual void freeBlock(vector3d *locs)
Definition: adj.cpp:316
bool blocks
Input data is block-structured grid.
Definition: hdf2pltV2.C:51
face * b
Definition: adj.h:104
Definition: adj.h:103
int getLength(void) const
Definition: adj.cpp:87
adjRec * next
Definition: adj.h:125
int main(int argc, char *argv[])
Definition: blastest.C:94
real mag(void) const
Definition: vector3d.h:130
static T_VertexSet * face
Definition: vinci_lass.c:79
unsigned int levelBad
Definition: makeflo.h:100
static face * intersect(const node **nodes, const face *notHim=NULL, blockLoc *loc=NULL, blockLoc *oX=NULL, blockLoc *oY=NULL)
Definition: adj.cpp:136
int curBlock
Definition: adj.h:259
adjRec * next
Definition: adj.h:106
virtual const char * consume(const blockDim &dim, vector3d *locs)
Definition: adj.cpp:299
vector3d * nodeLocs
Definition: adj.h:207
makefloParam parameters
Definition: makeflo.cpp:101
NT abs(const NT &x)
Definition: number_utils.h:130
int freeCount
Definition: adj.h:82
~adjList()
Definition: adj.cpp:79
hashableVector3d(const vector3d &v)
Definition: adj.cpp:220
void buildFaces(nodeMatcher &map)
Definition: adj.cpp:273
block(const blockDim &dim_, int originalNo_, int blockNo_, vector3d *nodeLocs_)
Definition: adj.cpp:244
int getSize(void) const
Definition: gridutil.h:126
int getBlockNumber(void) const
Definition: adj.h:219
real z
Definition: vector3d.h:88
int getFace(void) const
Definition: gridutil.h:191
void print(void)
Definition: adj.cpp:68
blockDim dim
Definition: adj.h:204
static int blockBytes
Definition: adj.cpp:242