Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
writetop.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 Routines to write a .top mesh topology file,
55 given a set of intersecting blocks.
56 
57 Orion Sky Lawlor, olawlor@acm.org, 2/1/2002
58 */
59 #include <stdio.h>
60 #include "makeflo.h"
61 #include "face.h"
62 
63 /****************** .bc file input *****************/
64 class bcRecTop {
65  int inNo;//gridgen b.c. number
66 public:
67  int outNo;//RocfloMP b.c. number
68  int isCoupled;
69  bcRecTop(int in,int out,int coupled)
70  :inNo(in),outNo(out),isCoupled(coupled) { }
71  bool hasNumber(int n) const { return n==inNo; }
72 };
73 
74 class bcListTop {
75  vector<bcRecTop *> b;
76 public:
77  bcListTop(FILE *inF);
78  bcRecTop *lookup(int bcNo) const;
79 };
80 
81 
82 //Read the boundary conditions from the file
84 {
85  int lineCount=0;
86  char lineBuf[200];
87  while (NULL!=fgets(lineBuf,200,inF)) {
88  char *line=lineBuf;
89  while (isspace(*line)) line++; //Skip white space
90  line[strlen(line)-1]=0; //Erase newline
91  lineCount++;
92  switch(line[0]) {
93  case '#': case '!': break; //Skip comment line
94  case '\n': case '\r': case 0:
95  break; //Skip empty lines
96  case ' ': case '\t':
97  case '0': case '1': case '2': case '3': case '4':
98  case '5': case '6': case '7': case '8': case '9':
99  { //A new boundary condition
100  int in,out,coupled=0;
101  if (sscanf(line,"%d%d%d",&in,&out,&coupled)<2) {
102  fprintf(stderr,"ERROR!\n"
103  "Can't parse boundary condition number from '%s',\n"
104  "found on line %d of the boundary condition file\n",
105  line,lineCount);
106  exit(1);
107  }
108  b.push_back(new bcRecTop(in,out,coupled));
109  break;
110  }
111  case '@': //.flo-style boundary condition
112  fprintf(stderr,"ERROR!\n"
113  "This appears to be a rocflo-style boundary condition file--\n"
114  "RocfloMP does not use @ signs.\n");
115  exit(1);
116  default: //Something bizarre:
117  fprintf(stderr,"ERROR!\n"
118  "Can't parse line of boundary condition \n"
119  "'%s', found on line %d.\n",
120  line,lineCount);
121  exit(1);
122  };
123  }
124 }
125 
126 //Find a boundary condition and return its string
127 bcRecTop *bcListTop::lookup(int bcNo) const
128 {
129  unsigned int i;
130  for (i=0;i<b.size();i++)
131  if (b[i]->hasNumber(bcNo))
132  return b[i];
133  //If we got here, we couldn't find bcNo
134  fprintf(stderr,"ERROR! Can't find boundary condition %d in .bc file!\n",bcNo);
135  exit(1);
136  return NULL;//<- for whining compilers
137 }
138 
139 /****************** .top file output formatting **************/
140 
141 //Print a face number/l1-range/l2-range
142 static void write5(FILE *out,int face,int l1lo,int l1hi,int l2lo,int l2hi)
143 {
144  fprintf(out,"%d %3d %3d %3d %3d ",face,l1lo,l1hi,l2lo,l2hi);
145 }
146 
147 //Convert a run of nodes (a...b-1) to a run of blocks (o..p)
148 void node2block(int a,int b,int &o,int &p) {
149  if (a<b)
150  { //Nodes are oriented the right way:
151  o=a;
152  p=b-2; //Minus 1 for node->block, minus 1 again for excl->incl
153  } else {//Nodes run the other way:
154  o=a-1;
155  p=b-1;
156  }
157 }
158 
159 //Print a patch extent
160 static void writeSpan(FILE *out,const blockSpan &s,const blockLoc &sign)
161 {
162  //Map our face numbering (iMin, jMin, kMin, iMax, ...) to
163  // rocfloMP face numbering (iMin,iMax, jMin,jMax, ...)
164  const static int faceMap[6]=
165  {1,3,5,2,4,6};
166  int face=faceMap[s.getFace()];
167  // l1 and l2 axes parameterize the face
168  int flatAxis=(s.getFace()%3);
169  int l1Axis=(flatAxis+1)%3;
170  int l2Axis=(flatAxis+2)%3;
171 
172  int l1S=s.start[l1Axis], l1E=s.end[l1Axis];
173  int l2S=s.start[l2Axis], l2E=s.end[l2Axis];
174  node2block(l1S,l1E, l1S,l1E);
175  node2block(l2S,l2E, l2S,l2E);
176  int l1Sign=sign[l1Axis];
177  int l2Sign=sign[l2Axis];
178  write5(out,face,
179  l1Sign*(1+l1S),l1Sign*(1+l1E),
180  l2Sign*(1+l2S),l2Sign*(1+l2E));
181 }
182 
183 static void writePatchStart(FILE *out,int patchType) {
184  fprintf(out,"%4d ",patchType);
185 }
186 static void writePatchMiddle(FILE *out,int connBlock) {
187  fprintf(out,"%5d ",connBlock);
188 }
189 static void writePatchEnd(FILE *out,int coupled) {
190  fprintf(out," %d\n",coupled);
191 }
192 
193 void externalBCpatch::writeTop(FILE *out,const bcListTop &bc)
194 {
195  bcRecTop *r=bc.lookup(bcNo);
196  writePatchStart(out,r->outNo);
197  writeSpan(out,srcSpan,blockLoc(1,1,1));
198  //Destination block and span are zero for external boundaries
199  writePatchMiddle(out,0);
200  write5(out,0,0,0,0,0);
201  writePatchEnd(out,r->isCoupled);
202 }
203 
204 void internalBCpatch::writeTop(FILE *out,const bcListTop &bc)
205 {
206  //Compute where the orientation-indicating minus signs should go:
207  int flatAxis=(srcSpan.getFace()%3);
208  int l1Axis=(flatAxis+1)%3;
209  blockLoc srcSign(1,1,1), destSign(1,1,1);
210  srcSign[flatAxis]=0; //Shouldn't ever print flat axis
211  destSign[orient[flatAxis]]=0;
212  srcSign[l1Axis]=-1; //Flip sign on l1 axis
213  destSign[orient[l1Axis]]=-1;
214 
215  const int internalBcType=30;
216  writePatchStart(out,internalBcType);
217  writeSpan(out,srcSpan,srcSign);
219  writeSpan(out,destSpan,destSign);
220  writePatchEnd(out,0); //Internal patches are never coupled
221 }
222 
223 
224 //Write a block's data
225 static void writeBlock(FILE *out,const bcListTop &bc,const block *b) {
226  int blockNo=b->getBlockNumber()+1;
227  int nGridLevels=parameters.nLevels;
228  fprintf(out,
229  "%d %d ! ======== BLOCK %d (Split from source block %d) ========\n",
230  blockNo,nGridLevels,blockNo,b->getOriginalNumber()+1);
231  blockDim d=b->getDim();
232  int f,nPatches=0;
233  for (f=0;f<block::nFaces;f++)
234  nPatches+=b->getFace(f).getPatches().size();
235 
236  fprintf(out,
237  "%d %d %d %d ! number of patches; block size (ni, nj, nk)\n",
238  nPatches,d[0]-1,d[1]-1,d[2]-1);
239 
240  //Loop over the faces, writing out each patch
241  for (f=0;f<block::nFaces;f++) {
242  static int order[6]={0,3,1,4,2,5};
243  const vector<patch *> &patches=b->getFace(order[f]).getPatches();
244  int nPatches=patches.size();
245  //Loop over the patches
246  for (int p=0;p<nPatches;p++) {
247  patches[p]->writeTop(out,bc);
248  }
249  }
250 }
251 
252 const char * writeTop(vector<block *> &blocks,
253  const char *inBcs,
254  const char *outTop)
255 {
256  FILE *bcs=fopen(inBcs,"r");
257  if (bcs==NULL) {
258  char *ret=(char *)malloc(sizeof(char)*1000);
259  sprintf(ret,"Couldn't open input .bcmp file '%s'!\n",inBcs);
260  return ret;
261  }
262  bcListTop bc(bcs);
263  fclose(bcs);
264 
265  FILE *top=fopen(outTop,"w");
266  if (top==NULL) return "Couldn't open output .top file!\n";
267 
268  //Print the .top file header
269  int nBlocks=blocks.size();
270  fprintf(top,"# RocfloMP topology file, generated by makeflo\n"
271  "# \n"
272  "%d ! Total number of blocks in this file\n",
273  nBlocks);
274 
275  //Print out each block
276  for (int bn=0;bn<nBlocks;bn++)
277  writeBlock(top,bc,blocks[bn]);
278  fclose(top);
279  return NULL; //Everything worked!
280 }
281 
282 
283 
284 
285 
286 
287 
288 
289 
290 
291 
292 
293 
bcRecTop * lookup(int bcNo) const
Definition: writetop.cpp:127
int getOriginalNumber(void) const
Definition: adj.h:220
static SURF_BEGIN_NAMESPACE double sign(double x)
int isCoupled
Definition: writetop.cpp:68
const vector< patch * > & getPatches(void) const
Definition: face.h:145
Definition: face.h:90
const NT & d
double s
Definition: blastest.C:80
int nLevels
Definition: makeflo.h:99
vector< bcRecTop * > b
Definition: writetop.cpp:75
blockSpan srcSpan
Definition: patch.h:79
static void write5(FILE *out, int face, int l1lo, int l1hi, int l2lo, int l2hi)
Definition: writetop.cpp:142
void writeBlock(FILE *out, const bcList &bc, const block *b)
Definition: writeflo.cpp:198
int fclose(std::FILE *file)
Close a file, and check for possible errors.
Definition: CImg.h:5507
virtual void writeTop(FILE *out, const bcListTop &d)
Definition: writetop.cpp:204
static void writePatchEnd(FILE *out, int coupled)
Definition: writetop.cpp:189
virtual void writeTop(FILE *out, const bcListTop &d)
Definition: writetop.cpp:193
orient_t orient
Definition: patch.h:128
Definition: adj.h:203
const char * writeTop(vector< block * > &blocks, const char *inBcs, const char *out)
Definition: writetop.cpp:252
block * dest
Definition: patch.h:122
blockLoc start
Definition: gridutil.h:155
const blockDim & getDim(void) const
Definition: adj.h:222
blockLoc i
Definition: read.cpp:79
blockLoc end
Definition: gridutil.h:156
static void writePatchMiddle(FILE *out, int connBlock)
Definition: writetop.cpp:186
bool hasNumber(int n) const
Definition: writetop.cpp:71
const NT & n
int inNo
Definition: writetop.cpp:65
bool blocks
Input data is block-structured grid.
Definition: hdf2pltV2.C:51
bcRecTop(int in, int out, int coupled)
Definition: writetop.cpp:69
bcListTop(FILE *inF)
Definition: writetop.cpp:83
int outNo
Definition: writetop.cpp:67
blockSpan destSpan
Definition: patch.h:123
static void writeSpan(FILE *out, const blockSpan &s, const blockLoc &sign)
Definition: writetop.cpp:160
makefloParam parameters
Definition: makeflo.cpp:101
int getBlockNumber(void) const
Definition: adj.h:219
std::FILE * fopen(const char *const path, const char *const mode)
Open a file, and check for possible errors.
Definition: CImg.h:5494
int getFace(void) const
Definition: gridutil.h:191
face & getFace(int faceNo)
Definition: adj.h:248
void node2block(int a, int b, int &o, int &p)
Definition: writetop.cpp:148
static void writePatchStart(FILE *out, int patchType)
Definition: writetop.cpp:183