Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
readgrd.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 Reads a Gridgen .grd, .grds, or .grdb input file
55 and passes it to a blockConsumer. Returns an
56 error string on errors; returns NULL on sucess.
57 
58 This file is written by Pointwise(tm) Gridgen (ver 15) as:
59 From the top level menu:
60  Input/Output (e) ->
61  Grid Pts Export (8) ->
62  Block Volumes (4) ->
63  Name (k) ->
64  (type in name, ending with ".grd", ".grds", or ".grdd") ->
65  style == PLOT3D (2), ->
66 
67 For .grd:
68  format == ascii (a), precision == double (d),
69 For .grds:
70  format == binary (^b), precision == single (s),
71 For .grdd:
72  format == binary (^b), precision == double (d),
73 
74  Done (ent) ->
75  Pick All (a), Done (ent)
76 
77 Orion Sky Lawlor, olawlor@acm.org, 2004/2/18
78 */
79 #include <stdio.h>
80 #include "gridutil.h"
81 
82 /* Data types in Gridgen output file */
83 typedef int grdb_int_t;
84 typedef double grdb_coord_t;
85 
86 /* Read an integer from this native-endian binary file */
87 static const char *readInt(FILE *f,int *dest) {
88  grdb_int_t g;
89  if (1!=fread(&g,sizeof(g),1,f))
90  return "Error reading integer from .grdb file";
91  *dest=g;
92  return NULL;
93 }
94 
95 /* Read a double from this native-endian binary file */
96 static const char *readDouble(FILE *f,double *dest) {
97  grdb_coord_t g;
98  if (1!=fread(&g,sizeof(g),1,f))
99  return "Error reading coordinate from .grdb file";
100  *dest=g;
101  return NULL;
102 }
103 
106 protected:
107  FILE *f;
108 public:
109  void setFile(FILE *f_) {f=f_;}
110  virtual const char *readInt(int *dest) =0;
111  virtual const char *readDouble(double *dest) =0;
112 };
113 
116 public:
117  virtual const char *readInt(int *dest) {
118  if (1!=fscanf(f,"%d",dest))
119  return "Could not read integer from .grd file\n";
120  else return NULL;
121  }
122  virtual const char *readDouble(double *dest) {
123  if (1!=fscanf(f,"%lf",dest))
124  return "Could not read coordinate from .grd file\n";
125  else return NULL;
126  }
127 };
128 
130 template <class coordT>
132 public:
133  virtual const char *readInt(int *dest) {
134  if (1!=fread(dest,sizeof(*dest),1,f))
135  return "Could not read integer from binary .grd[sd] file\n";
136  else return NULL;
137  }
138  virtual const char *readDouble(double *dest) {
139  coordT in;
140  if (1!=fread(&in,sizeof(in),1,f))
141  return "Could not read coordinate from binary .grd[sd] file\n";
142  *dest=in;
143  return NULL;
144  }
145 };
146 
147 // Work around template instantiation problems on, e.g., Sun CC.
150 
151 
152 const char *read_general(const char *gridFile,gridgenGrdFormatter &fmt,blockConsumer &dest)
153 {
154  const char *err;
155  //Open the input file
156  FILE *f=fopen(gridFile,"r");
157  if (f==NULL) return "Couldn't open input .grd file";
158  fmt.setFile(f);
159 
160  //Read the header
161  int nBlocks=0;
162  if (NULL!=(err=fmt.readInt(&nBlocks))) return err;
163 
164  //Read the block sizes
165  blockDim *dims=new blockDim[nBlocks];
166  int b;
167  for (b=0;b<nBlocks;b++) {
168  int nx,ny,nz;
169  if (NULL!=(err=fmt.readInt(&nx))) return err;
170  if (NULL!=(err=fmt.readInt(&ny))) return err;
171  if (NULL!=(err=fmt.readInt(&nz))) return err;
172  dims[b]=blockDim(nx,ny,nz);
173  }
174 
175  //Read and consume each block's coordinates
176  for (b=0;b<nBlocks;b++) {
177  int nLocs=dims[b].getSize();
178  vector3d *locs=dest.allocateBlock(dims[b]);
179  for (int c=0;c<3;c++) //*outer* loop is coordinate axis
180  for (int i=0;i<nLocs;i++) {//*inner* loop is location #
181  double loc;
182  if (NULL!=(err=fmt.readDouble(&loc))) return err;
183  locs[i][c]=loc;
184  }
185  if (NULL!=(err=dest.consume(dims[b],locs))) return err;
186  dest.freeBlock(locs);
187  }
188 
189  //Finish up
190  delete[] dims;
191  fclose(f);
192  return NULL; //Everything worked
193 }
194 
195 
196 const char *read_grd(const char *gridFile,blockConsumer &dest)
197 {
199  return read_general(gridFile,fmt,dest);
200 }
201 const char *read_grds(const char *gridFile,blockConsumer &dest)
202 {
204  return read_general(gridFile,fmt,dest);
205 }
206 const char *read_grdd(const char *gridFile,blockConsumer &dest)
207 {
209  return read_general(gridFile,fmt,dest);
210 }
211 
int fread(T *const ptr, const unsigned int nmemb, std::FILE *stream)
Read file data, and check for possible errors.
Definition: CImg.h:5569
virtual const char * readDouble(double *dest)
Definition: readgrd.cpp:138
static const char * readInt(FILE *f, int *dest)
Definition: readgrd.cpp:87
virtual const char * readDouble(double *dest)=0
virtual const char * readDouble(double *dest)
Definition: readgrd.cpp:122
int fclose(std::FILE *file)
Close a file, and check for possible errors.
Definition: CImg.h:5507
ASCII format input file:
Definition: readgrd.cpp:115
double grdb_coord_t
Definition: readgrd.cpp:84
virtual vector3d * allocateBlock(blockDim &dim)
Definition: read.cpp:146
Machine binary input file:
Definition: readgrd.cpp:131
const char * read_grds(const char *gridFile, blockConsumer &dest)
Definition: readgrd.cpp:201
Abstracts the file format of a gridgen .grd file.
Definition: readgrd.cpp:105
void setFile(FILE *f_)
Definition: readgrd.cpp:109
virtual void freeBlock(vector3d *blk)
Definition: read.cpp:150
blockLoc i
Definition: read.cpp:79
static const char * readDouble(FILE *f, double *dest)
Definition: readgrd.cpp:96
int grdb_int_t
Definition: readgrd.cpp:83
const char * read_grdd(const char *gridFile, blockConsumer &dest)
Definition: readgrd.cpp:206
virtual const char * readInt(int *dest)
Definition: readgrd.cpp:117
const char * read_grd(const char *gridFile, blockConsumer &dest)
Definition: readgrd.cpp:196
virtual const char * readInt(int *dest)=0
int getSize(void) const
Definition: gridutil.h:126
virtual const char * readInt(int *dest)
Definition: readgrd.cpp:133
const char * read_general(const char *gridFile, gridgenGrdFormatter &fmt, blockConsumer &dest)
Definition: readgrd.cpp:152
virtual const char * consume(const blockDim &dim, vector3d *locs)=0
std::FILE * fopen(const char *const path, const char *const mode)
Open a file, and check for possible errors.
Definition: CImg.h:5494