Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
read.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 Determines the format of, and calls the appropriate
55 routine to read a CSAR fluid block mesh.
56 
57 Orion Sky Lawlor, olawlor@acm.org, 6/8/2001
58 */
59 #include <cstring>
60 #include <cstdio>
61 #include <strings.h>
62 #include "gridutil.h"
63 #include "util.h"
64 //#include "charm-api.h"
65 #include "FC.h"
66 
68 
69 //Fortran numeric data type:
70 typedef double REAL;
71 
72 //Fortran-callable mesh input routine
73 extern "C" void FC_GLOBAL(accept_locations,ACCEPT_LOCATIONS)
74  (int *ni,int *nj,int *nk,
75  REAL *x,REAL *y,REAL *z)
76 {
77  blockDim dim(*ni,*nj,*nk);
78  vector3d *v=curConsumer->allocateBlock(dim);
80  for (i[2]=0; i[2]<dim[2]; i[2]++)
81  for (i[1]=0; i[1]<dim[1]; i[1]++)
82  for (i[0]=0; i[0]<dim[0]; i[0]++)
83  { //Copy data from Fortran array
84  int c_i=dim[i], f_i=dim[i];
85  v[c_i].x=x[f_i];
86  v[c_i].y=y[f_i];
87  v[c_i].z=z[f_i];
88  }
89  curConsumer->consume(dim,v);
90  curConsumer->freeBlock(v);
91 }
92 
93 //Prototypes for read functions:
94 extern "C" void FC_GLOBAL(read_msh,READ_MSH)(const char *fName);
95 const char *read_grd(const char *gridFile,blockConsumer &dest);
96 const char *read_grds(const char *gridFile,blockConsumer &dest);
97 const char *read_grdd(const char *gridFile,blockConsumer &dest);
98 const char *read_reg(const char *regFile,blockConsumer &dest);
99 const char *read_hdf(const char *hdfName,blockConsumer &dest);
100 
101 //Read blocks from the given file into the given consumer
102 const char *read_file(const char *file,blockConsumer &dest)
103 {
104  curConsumer=&dest;
105  if (endsWith(file,".msh"))
106  FC_GLOBAL(read_msh,READ_MSH)(fortranifyString(file));
107  else if (endsWith(file,".grd"))
108  return read_grd(file,dest);
109  else if (endsWith(file,".grds"))
110  return read_grds(file,dest);
111  else if (endsWith(file,".grdd"))
112  return read_grdd(file,dest);
113 #ifdef USE_HDF
114  else if (endsWith(file,".hdf"))
115  return read_hdf(file,dest);
116 #endif
117  else if (endsWith(file,".reg"))
118  return read_reg(file,dest);
119  else
120  return "Unrecognized input mesh file extension! ("__FILE__")";
121  return NULL;
122 }
123 
124 //Read blocks from the given file specification,
125 // which may contain several blocks.
126 const char *read_multiple(const char *file,blockConsumer &dest)
127 {
128  char tmp[1024];
129  strcpy(tmp,file);
130  do {
131  printf("Reading block file '%s'...\n",tmp);
132  const char *ret=read_file(tmp,dest);
133  if (ret!=NULL) return ret;
134  if (!incrementAscii(tmp)) return NULL;
135  } while (fileExists(tmp));
136  return NULL;
137 }
138 
139 /******* BlockConsumer implementation *********/
140 const char *blockConsumer::read(const char *file)
141 {
142  return read_multiple(file,*this);
143 }
144 
145 //Location vector block allocation/deallocation
147 {
148  return new vector3d[dim.getSize()];
149 }
151 {
152  delete[] blk;
153 }
154 
156 
157 
158 
159 #ifdef STANDALONE
160 /*Unit test driver program:
161  Prints out a debugging version of the input file.
162 */
163 
164 int printAll=0;
165 
166 class debugBlockConsumer : public blockConsumer{
167  int count;//Number of blocks seen so far
168 
169  void print(const vector3d &v,const char *post="") {
170  printf("%g\t%g\t%g%s",
171  v.x,v.y,v.z,post);
172  }
173 
174  void checkPoint(int i,int j,int k,
175  const blockDim &dim,const vector3d *locs)
176  {
177  const vector3d &o=locs[dim[blockLoc(i,j,k)]];
178  const vector3d &x=locs[dim[blockLoc(i+1,j,k)]];
179  const vector3d &y=locs[dim[blockLoc(i,j+1,k)]];
180  const vector3d &z=locs[dim[blockLoc(i,j,k+1)]];
181  vector3d del(o.dist(x),o.dist(y),o.dist(z));
182  printf("\tGrid spacings:");print(del,"\n");
183  vector3d nx=(x-o).dir();
184  vector3d ny=(y-o).dir();
185  vector3d nz=(z-o).dir();
186  printf("\tSlant-ness:xy=%.3f\tyz=%.3f\txz=%.3f\n",
187  nx.dot(ny),ny.dot(nz),nx.dot(nz));
188  }
189 
190 public:
191  debugBlockConsumer() {count=0;}
192 
193  virtual const char *consume(
194  const blockDim &dim,//Dimentions of incoming block
195  vector3d *locs) //X,Y,Z coordinates (ni x nj x nk)
196  {
197  printf("Block #%d: (%d x %d x %d)\n",++count,
198  dim[0],dim[1],dim[2]);
199  //Check the inter-point spacing along each axis
200  checkPoint(2,3,1, //Check some random point
201  dim,locs);
202  checkPoint(0,1,2, //Check another random point
203  dim,locs);
204 
205  //Print out a few points
206  int l;
207  int max=10;
208  if (max>dim.getSize() || printAll) max=dim.getSize();
209  for (l=0;l<max;l++) {
210  printf("\t[%d]=",l);
211  print(locs[l],"\n");
212  }
213  return NULL;
214  }
215 };
216 
217 int main(int argc,char *argv[]) {
218  if (argc<2) {printf("Usage: read <mesh file> [all]\n"); return 1;}
219  debugBlockConsumer dest;
220  const char *err;
221  if (argc>2) printAll=1;
222  if (NULL!=(err=dest.read(argv[1])))
223  printf("ERROR! %s\n",err);
224  else
225  printf("Finished successfully.\n");
226  return 0;
227 }
228 #endif
229 
230 
231 
bool fileExists(const char *fName)
real dist(const vector3d &b) const
Definition: vector3d.h:136
bool incrementAscii(char *cur)
j indices k indices k
Definition: Indexing.h:6
void int int REAL REAL * y
Definition: read.cpp:74
double REAL
Definition: read.cpp:70
void read_file(const char *fname, const string &wname, double alpha)
Definition: autosurfer.C:39
Vector_n max(const Array_n_const &v1, const Array_n_const &v2)
Definition: Vector_n.h:354
real x
Definition: vector3d.h:88
real y
Definition: vector3d.h:88
const char * read_hdf(const char *hdfName, blockConsumer &dest)
Definition: readhdf.cpp:76
void int int int REAL REAL REAL const char * fName
Definition: write.cpp:76
*********************************************************************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
const char * read_multiple(const char *file, blockConsumer &dest)
Definition: read.cpp:126
bool endsWith(const char *a, const char *suffix)
const char * read_reg(const char *regFile, blockConsumer &dest)
Definition: readreg.cpp:63
virtual vector3d * allocateBlock(blockDim &dim)
Definition: read.cpp:146
const char * read_grds(const char *gridFile, blockConsumer &dest)
Definition: readgrd.cpp:201
void int int int REAL REAL REAL * z
Definition: write.cpp:76
virtual void freeBlock(vector3d *blk)
Definition: read.cpp:150
virtual ~blockConsumer()
Definition: read.cpp:155
blockLoc i
Definition: read.cpp:79
void int int REAL * x
Definition: read.cpp:74
const char * read_grdd(const char *gridFile, blockConsumer &dest)
Definition: readgrd.cpp:206
int main(int argc, char *argv[])
Definition: blastest.C:94
const char * read(const char *gridFile)
Definition: read.cpp:140
void int int * nk
Definition: read.cpp:74
j indices j
Definition: Indexing.h:6
#define FC_GLOBAL(name, NAME)
Definition: FC.h:5
void int int REAL REAL REAL *z blockDim dim * ni
Definition: read.cpp:77
void int * nj
Definition: read.cpp:74
subroutine read_msh(F_name)
Definition: readmsh.f90:57
real dot(const vector3d &b) const
Definition: vector3d.h:139
const char * read_grd(const char *gridFile, blockConsumer &dest)
Definition: readgrd.cpp:196
int getSize(void) const
Definition: gridutil.h:126
const char * fortranifyString(const char *src)
virtual const char * consume(const blockDim &dim, vector3d *locs)=0
real z
Definition: vector3d.h:88
static blockConsumer * curConsumer
Definition: read.cpp:67