Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
write.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 write a CSAR fluid block mesh.
56 
57 Orion Sky Lawlor, olawlor@acm.org, 6/13/2001
58 */
59 #include <stdio.h>
60 #include <strings.h>
61 #include "adj.h"
62 #include "util.h"
63 //#include "charm-api.h"
64 #include "FC.h"
65 
66 //Fortran numeric data type:
67 typedef double REAL;
68 
69 extern "C" {
70  typedef void (*fn_fortran_write)(int *block,int *ni,int *nj,int *nk,
71  REAL *x,REAL *y,REAL *z,const char *fName); // (JB: added block)
72 }
73 
74 //Prototypes for write functions: (JB: added block)
75 extern "C" {
76  void FC_GLOBAL(write_msh,WRITE_MSH)(int *block,int *ni,int *nj,int *nk,
77  REAL *x,REAL *y,REAL *z,const char *fName);
78  void FC_GLOBAL(write_grda,WRITE_GRDA)(int *block,int *ni,int *nj,int *nk,
79  REAL *x,REAL *y,REAL *z,const char *fName);
80  void FC_GLOBAL(write_grdb,WRITE_GRDB)(int *block,int *ni,int *nj,int *nk,
81  REAL *x,REAL *y,REAL *z,const char *fName);
82 }
83 
84 //Send this data to the given fortran routine (JB: added block)
85 static void write_fortran(int block,blockDim dim,const vector3d *locs,
86  const char *fName,fn_fortran_write writeFn)
87 {
88  //Copy the data from C array to fortran arrays
89  int len=dim.getSize();
90  REAL *x=new REAL[len];
91  REAL *y=new REAL[len];
92  REAL *z=new REAL[len];
93  blockLoc i;
94  for (i[2]=0; i[2]<dim[2]; i[2]++)
95  for (i[1]=0; i[1]<dim[1]; i[1]++)
96  for (i[0]=0; i[0]<dim[0]; i[0]++)
97  { //Copy data into fortran array
98  int c_i=dim[i], f_i=dim[i];
99  x[f_i]=locs[c_i].x;
100  y[f_i]=locs[c_i].y;
101  z[f_i]=locs[c_i].z;
102  }
103 
104  //Send the data off to fortran (JB: added block)
105  (writeFn)(&block,&dim[0],&dim[1],&dim[2],x,y,z,fortranifyString(fName));
106  delete[] x; delete[] y; delete[] z;
107 }
108 
109 void vecMax(const vector3d &a,vector3d &dest)
110 {
111  if (a.x>dest.x) dest.x=a.x;
112  if (a.y>dest.y) dest.y=a.y;
113  if (a.z>dest.z) dest.z=a.z;
114 }
115 void vecMin(const vector3d &a,vector3d &dest)
116 {
117  if (a.x<dest.x) dest.x=a.x;
118  if (a.y<dest.y) dest.y=a.y;
119  if (a.z<dest.z) dest.z=a.z;
120 }
121 
122 void write_bounds(const blockDim &dim,const vector3d *locs,
123  const char *filename_base)
124 {
125  char filename[1024];
126  sprintf(filename,"%s.bounds",filename_base);
127  FILE *f=fopen(filename,"w");
128  if (f==NULL) return;
129  //Find the bounds of all locations
130  double big=1.0e25;
131  vector3d max(-big,-big,-big),min(big,big,big);
132  blockLoc i;
133  for (i[2]=0; i[2]<dim[2]; i[2]++)
134  for (i[1]=0; i[1]<dim[1]; i[1]++)
135  for (i[0]=0; i[0]<dim[0]; i[0]++) {
136  vecMax(locs[dim[i]],max);
137  vecMin(locs[dim[i]],min);
138  }
139  fprintf(f,"%f %f %f %f %f %f\n",min.x,max.x,min.y,max.y,min.z,max.z);
140  fclose(f);
141 }
142 
143 const char *write_hdf(const blockDim &dim,const vector3d *locs,
144  const char *filename);
145 
146 //Write blocks to the given file
147 const char * writeBlocks(vector<block *> &blocks,
148  const char *outMesh)
149 {
150  char oName[1024];
151  bool isFortran=false, isHDF=false;
152  bool incrementFilenames=true;
153  fn_fortran_write writeFn=NULL;
154  strcpy(oName,outMesh);
155  if (endsWith(outMesh,".msh"))
156  { //Write .msh files:
157  isFortran=true;
158  writeFn=FC_GLOBAL(write_msh,WRITE_MSH);
159  }
160  else if (endsWith(outMesh,"1")||
161  endsWith(outMesh,".grda")||
162  endsWith(outMesh,".grdb"))
163  { //Probably a grda or grdb file:
164  isFortran=true;
165  incrementFilenames=false; //Same filename every time
166  char tmp[1024];
167  strcpy(tmp,outMesh);
168  //Clip off any trailing numbers:
169  int endIdx=strlen(tmp)-1;
170  while (endIdx>0&&(isdigit(tmp[endIdx])||(tmp[endIdx]=='_')))
171  tmp[endIdx--]=0;
172  if (endsWith(tmp,".grda"))
173  writeFn=FC_GLOBAL(write_grda,WRITE_GRDA);
174  else if (endsWith(tmp,".grdb"))
175  writeFn=FC_GLOBAL(write_grdb,WRITE_GRDB);
176  else return "Unrecognized output mesh extension! (2)";
177  }
178 #ifdef USE_HDF
179  else if (endsWith(outMesh,".hdf"))
180  {
181  isHDF=true;
182  }
183 #endif
184  else if (endsWith(outMesh,".null"))
185  {/*Just skip output*/}
186  else
187  return "Unrecognized output mesh file extension! (1)";
188 
189 
190  for (unsigned int i=0;i<blocks.size();i++) {
191  const blockDim &dim=blocks[i]->getDim();
192  const vector3d *locs=&blocks[i]->getLoc(blockLoc(0,0,0));
193 
194  if (isFortran)
195  if (i+1==blocks.size())
196  { /* Last time around, pass a negative block number
197  so fortran code can close its file. */
198  write_fortran(-(i+1),dim,locs,oName,writeFn);
199  } else {
200  write_fortran( i+1 ,dim,locs,oName,writeFn);
201  }
202 
203 #ifdef USE_HDF
204  else if (isHDF)
205  write_hdf(dim,locs,oName);
206 #endif
207 
208  printf("."); fflush(stdout);
209  if (incrementFilenames) {
210  if (!incrementAscii(oName))
211  return "Cannot increment output filename! Use something like 'foo_0001.msh'\n";
212  }
213  }
214  printf("\n");
215 
216  return NULL;
217 }
218 
219 
bool incrementAscii(char *cur)
void int int REAL REAL * y
Definition: read.cpp:74
double REAL
Definition: read.cpp:70
const char * writeBlocks(vector< block * > &blocks, const char *outMesh)
Definition: write.cpp:147
Vector_n max(const Array_n_const &v1, const Array_n_const &v2)
Definition: Vector_n.h:354
subroutine write_grda(iblock, ni, nj, nk, x, y, z, F_name)
Definition: writegrda.f90:64
real x
Definition: vector3d.h:88
subroutine write_grdb(iblock, ni, nj, nk, x, y, z, F_name)
Definition: writegrdb.f90:64
void(* fn_fortran_write)(int *block, int *ni, int *nj, int *nk, REAL *x, REAL *y, REAL *z, const char *fName)
Definition: write.cpp:70
real y
Definition: vector3d.h:88
int fclose(std::FILE *file)
Close a file, and check for possible errors.
Definition: CImg.h:5507
void vecMin(const vector3d &a, vector3d &dest)
Definition: write.cpp:115
void int int int REAL REAL REAL const char * fName
Definition: write.cpp:76
bool endsWith(const char *a, const char *suffix)
subroutine write_msh(iblock, ni, nj, nk, x, y, z, F_name)
Definition: writemsh.f90:58
Definition: adj.h:203
void vecMax(const vector3d &a, vector3d &dest)
Definition: write.cpp:109
void int int int REAL REAL REAL * z
Definition: write.cpp:76
blockLoc i
Definition: read.cpp:79
void int int REAL * x
Definition: read.cpp:74
bool blocks
Input data is block-structured grid.
Definition: hdf2pltV2.C:51
Vector_n min(const Array_n_const &v1, const Array_n_const &v2)
Definition: Vector_n.h:346
void int int * nk
Definition: read.cpp:74
#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
int getSize(void) const
Definition: gridutil.h:126
const char * fortranifyString(const char *src)
real z
Definition: vector3d.h:88
void write_bounds(const blockDim &dim, const vector3d *locs, const char *filename_base)
Definition: write.cpp:122
std::FILE * fopen(const char *const path, const char *const mode)
Open a file, and check for possible errors.
Definition: CImg.h:5494
const char * write_hdf(const blockDim &dim, const vector3d *locs, const char *filename)
Definition: writehdf.cpp:77
static void write_fortran(int block, blockDim dim, const vector3d *locs, const char *fName, fn_fortran_write writeFn)
Definition: write.cpp:85