Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
writeflo.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 .flo mesh description file,
55 given a set of intersecting blocks.
56 
57 Orion Sky Lawlor, olawlor@acm.org, 6/7/2001
58 */
59 #include <stdio.h>
60 #include "face.h"
61 
62 /****************** .bc file input *****************/
63 class bcRec {
64  int bcNo;//Gridgen b.c. number
65  bool foundLoc; //Have we seen the location marker?
66  string pre,post;//Portion before and after location
67 public:
68  bcRec(int n)
69  :bcNo(n),foundLoc(false),pre(""),post("") { }
70  void addLine(string l) {
71  if (!foundLoc)
72  { //Check the line for a location marker
73  int loc=l.find("$");
74  if (loc==-1)
75  pre+=l; //no marker
76  else { //We found the marker!
77  foundLoc=true;
78  pre+=l.substr(0,loc);
79  post+=l.substr(loc+1);
80  }
81  }
82  else //Already found the marker
83  post+=l;//Just append
84  }
85  string getString(string insert) const {
86  return pre+insert+post;
87  }
88  bool hasNumber(int n) const { return n==bcNo; }
89 };
90 
91 class bcList {
92  vector<bcRec *> b;
93 public:
94  bcList(FILE *inF);
95  string lookup(int bcNo,string insert) const;
96 };
97 
98 //Read the boundary conditions from the file
99 bcList::bcList(FILE *inF)
100 {
101  int lineCount=0;
102  char line[200];
103  bcRec *curBC=NULL;
104  int bcNo;
105  while (NULL!=fgets(line,200,inF)) {
106  lineCount++;
107  switch(line[0]) {
108  case '#': break; //Skip comment line
109  case '\n': case '\r': case 0:
110  break; //Skip empty lines
111  case '@': //Start of new boundary condition
112  bcNo=0;
113  if (1!=sscanf(&line[1],"%d",&bcNo)) {
114  fprintf(stderr,"ERROR!\n"
115  "Can't parse boundary condition number from '%s',\n"
116  "found on line %d of the boundary condition file\n",
117  line,lineCount);
118  exit(1);
119  }
120  //Flush the old bc:
121  if (curBC!=NULL) b.push_back(curBC);
122  curBC=new bcRec(bcNo);
123  break;
124  default: //Just a regular description line
125  if (curBC!=NULL) curBC->addLine(line);
126  break;
127  };
128  }
129  if (curBC!=NULL) b.push_back(curBC);
130 }
131 
132 //Find a boundary condition and return its string
133 string bcList::lookup(int bcNo,string insert) const
134 {
135  unsigned int i;
136  for (i=0;i<b.size();i++)
137  if (b[i]->hasNumber(bcNo))
138  return b[i]->getString(insert);
139  //If we got here, we couldn't find bcNo
140  fprintf(stderr,"ERROR! Can't find boundary condition %d in .bc file!\n",bcNo);
141  exit(1);
142  return "";//<- for whining compilers
143 }
144 
145 /****************** .flo file output formatting **************/
146 
147 //Write the .flo file header
148 void writeHeader(FILE *out,int nBlocks,int nPEs) {
149  fprintf(out,
150  "%d %d ! ndom, nproc\n"
151  "\n"
152  "PROC_MAP_AUTO\n"
153  "\n",
154  nBlocks,nPEs);
155 }
156 
157 
158 //Print a range of grid indices
159 string getSpan(const blockSpan &s)
160 {
161  char buf[200];
162  sprintf(buf," %d %d %d %d %d %d",
163  1+s.start[0],s.end[0],
164  1+s.start[1],s.end[1],
165  1+s.start[2],s.end[2]);
166  return buf;
167 }
168 
169 //An exterior boundary-- lookup the type
170 void externalBCpatch::writeFlo(FILE *out,const bcList &bc)
171 {
172  string srcS=getSpan(srcSpan);
173  string bcDesc=bc.lookup(bcNo,srcS);
174  fprintf(out,"%s",bcDesc.c_str());
175 }
176 
177 //An interior boundary-- just print it
178 void internalBCpatch::writeFlo(FILE *out,const bcList &bc)
179 {
180  string srcS=getSpan(srcSpan);
181  string destS=getSpan(destSpan);
182  int destBlockNo=dest->getBlockNumber()+1;
183  const char *selfFlag="";
184  if (destBlockNo==src->getBlockNumber()+1) {
185  destBlockNo=0;//Self-connecting block
186  selfFlag="(self-connecting)";
187  }
188  fprintf(out,"%d %s ! Internal boundary %s%s\n",
189  type,srcS.c_str(),(type==2)?"(TYPE TWO)":"",selfFlag);
190  fprintf(out,"%-5d %s\n",
191  destBlockNo,destS.c_str());
192  fprintf(out,"%d %d %d ! Orientation\n",
193  1+orient[0],1+orient[1],1+orient[2]);
194 }
195 
196 
197 //Write a block's data
198 void writeBlock(FILE *out,const bcList &bc,const block *b) {
199  int blockNo=b->getBlockNumber()+1;
200  fprintf(out,
201  "%d %d 1 ! BLOCK %d (Split from source block %d) ======================\n"
202  "\n",
203  blockNo,blockNo,blockNo,b->getOriginalNumber()+1);
204  blockDim d=b->getDim();
205  fprintf(out,
206  "%d %d %d ! Block size (ni, nj, nk)\n",
207  d[0],d[1],d[2]);
208  //Get the fluid initial conditions
209  fprintf(out,"%s\n",bc.lookup(-1,"").c_str());
210 
211  //Loop over the faces
212  for (int f=0;f<block::nFaces;f++) {
213  const vector<patch *> &patches=b->getFace(f).getPatches();
214  int nPatches=patches.size();
215 
216  fprintf(out,
217  "%d ! Block %d, Face (%s) has %d patch\n",
218  nPatches,blockNo,block::face2name[f],nPatches);
219  //Loop over the patches
220  for (int p=0;p<nPatches;p++) {
221  patches[p]->writeFlo(out,bc);
222  }
223  fprintf(out,"\n");
224  }
225  fprintf(out,
226  "ENDBBC\n"
227  "\n");
228 }
229 
230 const char * writeFlo(vector<block *> &blocks,
231  int nPEs,
232  const char *inBcs,
233  const char *outFlo)
234 {
235  FILE *bcs=fopen(inBcs,"r");
236  if (bcs==NULL) {
237  char *ret=(char *)malloc(sizeof(char)*1000);
238  sprintf(ret,"Couldn't open input .bc file '%s'!\n",inBcs);
239  return ret;
240  }
241  bcList bc(bcs);
242  fclose(bcs);
243 
244  FILE *flo=fopen(outFlo,"w");
245  if (flo==NULL) return "Couldn't open output .flo file!\n";
246 
247  //Print the .flo file header
248  int nBlocks=blocks.size();
249  writeHeader(flo,nBlocks,nPEs);
250 
251  //Print out each block
252  for (int bn=0;bn<nBlocks;bn++)
253  writeBlock(flo,bc,blocks[bn]);
254  fclose(flo);
255  return NULL; //Everything worked!
256 }
257 
258 
259 
260 
261 
262 
263 
264 
265 
266 
267 
268 
269 
bool hasNumber(int n) const
Definition: writeflo.cpp:88
int getOriginalNumber(void) const
Definition: adj.h:220
const vector< patch * > & getPatches(void) const
Definition: face.h:145
string getSpan(const blockSpan &s)
Definition: writeflo.cpp:159
const NT & d
bcRec(int n)
Definition: writeflo.cpp:68
vector< bcRec * > b
Definition: writeflo.cpp:92
double s
Definition: blastest.C:80
string lookup(int bcNo, string insert) const
Definition: writeflo.cpp:133
string getString(string insert) const
Definition: writeflo.cpp:85
void addLine(string l)
Definition: writeflo.cpp:70
blockSpan srcSpan
Definition: patch.h:79
block * src
Definition: patch.h:78
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
int bcNo
Definition: writeflo.cpp:64
orient_t orient
Definition: patch.h:128
Definition: adj.h:203
static const char * face2name[nFaces]
Definition: adj.h:236
block * dest
Definition: patch.h:122
blockLoc start
Definition: gridutil.h:155
const blockDim & getDim(void) const
Definition: adj.h:222
string pre
Definition: writeflo.cpp:66
blockLoc i
Definition: read.cpp:79
string post
Definition: writeflo.cpp:66
virtual void writeFlo(FILE *out, const bcList &bc)
Definition: writeflo.cpp:178
blockLoc end
Definition: gridutil.h:156
const NT & n
bool blocks
Input data is block-structured grid.
Definition: hdf2pltV2.C:51
void writeHeader(FILE *out, int nBlocks, int nPEs)
Definition: writeflo.cpp:148
bool foundLoc
Definition: writeflo.cpp:65
blockSpan destSpan
Definition: patch.h:123
virtual void writeFlo(FILE *out, const bcList &bc)
Definition: writeflo.cpp:170
const char * writeFlo(vector< block * > &blocks, int nPEs, const char *inBcs, const char *out)
Definition: writeflo.cpp:230
int getBlockNumber(void) const
Definition: adj.h:219
bcList(FILE *inF)
Definition: writeflo.cpp:99
std::FILE * fopen(const char *const path, const char *const mode)
Open a file, and check for possible errors.
Definition: CImg.h:5494
face & getFace(int faceNo)
Definition: adj.h:248