Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
readinp.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 Read exterior boundary conditions from a
55 Gridgen .inp file.
56 
57 This file is written by Pointwise(tm) Gridgen (ver 15) as:
58 From the top level menu:
59  Input/Output (e) ->
60  Export Analysis BCs (5) ->
61  Name (k) ->
62  (type in name, ending with ".inp")
63 
64 Orion Sky Lawlor, olawlor@acm.org, 6/7/2001
65 */
66 #include <stdio.h>
67 #include "makeflo.h"
68 #include "adj.h"
69 
70 class bcReader {
71  FILE *in;
72 public:
73  bcReader(FILE *in_)
74  :in(in_)
75  {
76 
77  }
79 
80  //Read an integer
81  bool read(int *dest) {
82  int count=fscanf(in,"%d",dest);
83  return (count==1);
84  }
85  //Read a (iMin-iMax) (jMin-jMax) (kMin-kMax) 1-based location
86  bool read(blockSpan &ret) {
87  blockLoc start,end;
88  for (int i=0;i<3;i++) {
89  if (!(read(&start[i]) && read(&end[i])))
90  return false;
91  //Fix up some oddities in the .inp files
92  if (end[i]<0) end[i]=-end[i];
93  if (start[i]<0) start[i]=-start[i];
94 
95  //Make zero-based
96  end[i]--; start[i]--;
97 
98  if (end[i]<start[i])
99  { //Start and end are backwards for this axis:
100  int tmp=end[i];
101  end[i]=start[i];
102  start[i]=tmp;
103  }
104  end[i]++; //Make end into c-style
105  }
106  ret=blockSpan(start,end);
107  return true;
108  }
109  //Read a dimension
110  bool read(blockDim &d) {
111  for (int i=0;i<3;i++)
112  if (!read(&d[i]))
113  return false;
114  return true;
115  }
116  //Skip a line of input
117  bool skipLine(void) {
118  char str[1024];
119  return NULL!=fgets(str,1024,in);
120  }
121 };
122 
123 void checkSpan(const blockSpan &s,const char *what) {
124  for (int axis=0;axis<3;axis++)
125  if ((s.start[axis]&parameters.levelBad)
126  ||((s.end[axis]-1)&parameters.levelBad)) {
127  fprintf(stderr,"%s",what);
129  }
130 }
131 
132 #define BC_EXTENTION ".inp"
133 #define ERR "Error reading " BC_EXTENTION " file: "
134 
135 const char * readBoundaries(vector<block *> &blocks,
136  const char *inMesh)
137 {
138  FILE *in=fopen(inMesh,"r");
139  if (in==NULL) {
140  fprintf(stderr,"Cannot open boundary condition file '%s'.\n",inMesh);
141  fprintf(stderr,"Continuing without external boundary conditions\n");
142  return NULL;//Ignore missing BC file
143  }
144  bcReader r(in);
145 
146  //Check the file header:
147  int solver;
148  if (!r.read(&solver)) return ERR "Bad file header (line 1)";
149  if (solver!=1) return ERR "Must use Gridgen generic solver";
150  int nBlocks;
151  if (!r.read(&nBlocks)) return ERR "Bad file header (line 2)";
152  if (nBlocks!=(int)blocks.size()) return ERR "mesh's block count does not match";
153 
154  //Read each block
155  for (int bn=0;bn<nBlocks;bn++) {
156  block *b=blocks[bn];
157  //Check the block size
158  blockDim dim;
159  if (!r.read(dim)) return ERR "Cannot read block size";
160  if (dim!=b->getDim()) return ERR "mesh's block size does not match";
161  //Skip over the block name line
162  r.skipLine();
163  r.skipLine();
164 
165  //Get the patch count and loop over patches
166  int nPatch;
167  if (!r.read(&nPatch)) return ERR "Cannot read patch count";
168  for (int p=0;p<nPatch;p++) {
169 
170  //Get the patch dimensions
171  blockSpan span;
172  if (!r.read(span)) return ERR "Cannot read patch dimensions";
173  //Get the patch type and handle patch
174  int type;
175  if (!r.read(&type)) return ERR "Cannot read patch type";
176  if (type>=0)
177  {//External boundary condition--add to block
178  b->addBC(span,type);
179  char errBuf[200];
180  sprintf(errBuf,"Bad boundary condition for block %d\n",1+bn);
181  checkSpan(span,errBuf);
182  }
183  else
184  {//Internal boundary condition-- read and ignore
185  blockSpan dest;
186  if (!r.read(dest)) return ERR "Cannot read internal patch dimensions";
187  int destBlock;
188  if (!r.read(&destBlock)) return ERR "Cannot read destination block";
189  destBlock--; //Make zero-based
190  }
191  }
192  }
193  return NULL;//Everything worked
194 }
195 
196 
197 
void addBC(const blockSpan &span, int bcNo)
Definition: adj.cpp:262
const NT & d
double s
Definition: blastest.C:80
~bcReader()
Definition: readinp.cpp:78
void multigridError(void)
Definition: split.cpp:69
bcReader(FILE *in_)
Definition: readinp.cpp:73
int fclose(std::FILE *file)
Close a file, and check for possible errors.
Definition: CImg.h:5507
Definition: adj.h:203
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
bool read(blockSpan &ret)
Definition: readinp.cpp:86
bool skipLine(void)
Definition: readinp.cpp:117
FILE * in
Definition: readinp.cpp:71
bool blocks
Input data is block-structured grid.
Definition: hdf2pltV2.C:51
unsigned int levelBad
Definition: makeflo.h:100
void checkSpan(const blockSpan &s, const char *what)
Definition: readinp.cpp:123
makefloParam parameters
Definition: makeflo.cpp:101
bool read(blockDim &d)
Definition: readinp.cpp:110
const char * readBoundaries(vector< block * > &blocks, const char *inMesh)
Definition: readinp.cpp:135
std::FILE * fopen(const char *const path, const char *const mode)
Open a file, and check for possible errors.
Definition: CImg.h:5494
#define ERR
Definition: readinp.cpp:133
bool read(int *dest)
Definition: readinp.cpp:81