PLUTO
bin_io.c File Reference

Functions for handling binary I/O. More...

#include "pluto.h"
Include dependency graph for bin_io.c:

Go to the source code of this file.

Functions

FILE * OpenBinaryFile (char *filename, int sz, char *mode)
 
int CloseBinaryFile (FILE *fbin, int sz)
 
void WriteBinaryArray (void *V, size_t dsize, int sz, FILE *fl, int istag)
 
void ReadBinaryArray (void *V, size_t dsize, int sz, FILE *fl, int istag, int swap_endian)
 
float *** Convert_dbl2flt (double ***Vdbl, double unit, int swap_endian)
 

Detailed Description

Functions for handling binary I/O.

This file provides a number of handy functions for opening, reading and writing binary files using single or double precision in serial or parallel mode. It is employed by the following output formats: .dbl, .flt and .vtk.

In parallel mode these functions work as wrappers to the actual parallel implementations contained in AL_io.c.

Pointer to data array must be cast into (void *) and are assumed to start with index 0 for both cell-centered and staggered data arrays. This means that if V3D is a 3D array then:

  • for a cell-centered array, V = (void *) V3D[0][0]
  • for a x-staggered array, V = (void *)(V3D[0][0]-1)
  • for a y-staggered array, V = (void *)(V3D[0][-1])
  • for a z-staggered array, V = (void *)(V3D[-1][0])
Authors
A. Mignone (migno.nosp@m.ne@p.nosp@m.h.uni.nosp@m.to.i.nosp@m.t)
G. Muscianisi (g.mus.nosp@m.cian.nosp@m.isi@c.nosp@m.inec.nosp@m.a.it)
Date
June 21, 2014

Definition in file bin_io.c.

Function Documentation

int CloseBinaryFile ( FILE *  fbin,
int  sz 
)

Close file.

Parameters
[in]fbinpointer to the FILE that needs to be closed (serial mode only)
[in]szthe distributed array descriptor for parallel mode
Returns
Returns 0 on success

Definition at line 78 of file bin_io.c.

88 {
89  #ifdef PARALLEL
90  AL_File_close(sz);
91  #else
92  fclose (fbin);
93  #endif
94  return(0);
95 }
int AL_File_close(int sz_ptr)
Definition: al_io.c:83

Here is the call graph for this function:

Here is the caller graph for this function:

float*** Convert_dbl2flt ( double ***  Vdbl,
double  unit,
int  swap_endian 
)

Convert the a double-precision 3D array into single precision. Swap endianity if swap_endian == 1.

Parameters
[in]Vdblpointer to a 3D double precision aray
[in]unita multiplicative constant typically used to write in c.g.s units.
[in]swap_endianwhen set to 1, swap endianity during the conversion.
Returns
a pointer to a 3D array in single precision.

Definition at line 216 of file bin_io.c.

228 {
229  int i, j, k;
230  float flt;
231  static float ***Vflt;
232 
233  if (Vflt == NULL) Vflt = ARRAY_3D(NX3_TOT, NX2_TOT, NX1_TOT, float);
234 
235  if (!swap_endian){
236  DOM_LOOP(k,j,i){
237  Vflt[k][j][i] = (float)(Vdbl[k][j][i]*unit);
238  }
239  }else{
240  DOM_LOOP(k,j,i){
241  flt = (float)(Vdbl[k][j][i]*unit);
242  SWAP_VAR(flt);
243  Vflt[k][j][i] = flt;
244  }
245  }
246 
247  return (Vflt);
248 }
DOM_LOOP(k, j, i)
Definition: analysis.c:22
long int NX2_TOT
Total number of zones in the X2 direction (boundaries included) for the local processor.
Definition: globals.h:57
#define ARRAY_3D(nx, ny, nz, type)
Definition: prototypes.h:172
#define SWAP_VAR(x)
Definition: macros.h:115
long int NX3_TOT
Total number of zones in the X3 direction (boundaries included) for the local processor.
Definition: globals.h:59
int j
Definition: analysis.c:2
int k
Definition: analysis.c:2
int i
Definition: analysis.c:2
long int NX1_TOT
Total number of zones in the X1 direction (boundaries included) for the local processor.
Definition: globals.h:55

Here is the call graph for this function:

Here is the caller graph for this function:

FILE* OpenBinaryFile ( char *  filename,
int  sz,
char *  mode 
)

Open a file for write/reading in binary mode.

Parameters
[in]filenamea valid file name
[in]szthe distributed array descriptor. This parameter replaces dsize in parallel mode
[in]modea string giving the opening mode (only "w" or "r" are allowed)
Returns
The pointer to the file.

Definition at line 31 of file bin_io.c.

43 {
44  FILE *fp;
45 
46 /* ----------------------------------------------
47  when reading, check if file exists
48  ---------------------------------------------- */
49 
50  if (strcmp(mode,"r") == 0 && prank == 0){
51  fp = fopen(filename, "rb");
52  if (fp == NULL){
53  print1 ("! OpenBinaryFile: file %s does not exists\n", filename);
54  QUIT_PLUTO(1);
55  }
56  fclose(fp);
57  }
58 
59 /* ------------------------------------------------
60  file exists, keep going
61  ------------------------------------------------ */
62 
63  #ifdef PARALLEL
64  AL_File_open(filename, sz);
65  return NULL;
66  #else
67  if (strcmp(mode,"w") == 0) fp = fopen(filename, "wb");
68  else if (strcmp(mode,"r") == 0) fp = fopen(filename, "rb");
69  if (fp == NULL){
70  print1 ("! Cannot find file %s\n",filename);
71  QUIT_PLUTO(1);
72  }
73  return (fp);
74  #endif
75 }
void print1(const char *fmt,...)
Definition: amrPluto.cpp:511
int AL_File_open(char *filename, int sz_ptr)
Definition: al_io.c:27
int prank
Processor rank.
Definition: globals.h:33
FILE * fp
Definition: analysis.c:7
#define QUIT_PLUTO(e_code)
Definition: macros.h:125

Here is the call graph for this function:

Here is the caller graph for this function:

void ReadBinaryArray ( void *  V,
size_t  dsize,
int  sz,
FILE *  fl,
int  istag,
int  swap_endian 
)

Read a double-precision array from binary file.

Parameters
[in]Vpointer to a 3D array, V[k][j][i] –> V[i + NX1*j + NX1*NX2*k]. Must start with index 0
[in]dsizethe size of the each buffer element (sizeof(double) or sizeof(float)). This parameter is used only in serial mode.
[in]szthe distributed array descriptor. This parameter replaces dsize in parallel mode
[in]fla valid FILE pointer
[in]istaga flag to identify cell-centered (istag = -1) or staggered field data (istag = 0,1 or 2 for staggering in the x1, x2 or x3 directions)
[in]swap_endiana flag for swapping endianity
Returns
This function has no return value.
Remarks
The data array V is assumed to start always with index 0 for both cell-centered and staggered arrays.

Definition at line 143 of file bin_io.c.

167 {
168  int i, j, k;
169  int ioff, joff, koff;
170  char *Vc;
171 
172 /* ---------------------------------------
173  parallel reading handled by ArrayLib
174  --------------------------------------- */
175 
176  #ifdef PARALLEL
177  AL_Read_array (V, sz, istag);
178  #else
179 
180 /* ---------------------------------------
181  serial reading
182  --------------------------------------- */
183 
184  Vc = (char *) V;
185  ioff = (istag == 0);
186  joff = (istag == 1);
187  koff = (istag == 2);
188 
189  for (k = KBEG; k <= KEND + koff; k++) {
190  for (j = JBEG; j <= JEND + joff; j++) {
191  i = IBEG + (NX1_TOT + ioff)*(j + (NX2_TOT + joff)*k);
192  fread (Vc + i*dsize, dsize, NX1 + ioff, fl);
193  }}
194  #endif
195 
196 /* -----------------------------------
197  now swap endian if necessary
198  ----------------------------------- */
199 
200  if (swap_endian){
201  double *Vd;
202  Vd = (double *) V;
203  ioff = (istag == 0);
204  joff = (istag == 1);
205  koff = (istag == 2);
206 
207  for (k = KBEG-koff; k <= KEND + koff; k++) {
208  for (j = JBEG-joff; j <= JEND + joff; j++) {
209  for (i = IBEG-ioff; i <= IEND + ioff; i++) {
210  SWAP_VAR(Vd[i + (NX1_TOT + ioff)*(j + (NX2_TOT + joff)*k)]);
211  }}}
212  }
213 }
long int NX1
Number of interior zones in the X1 directions (boundaries excluded) for the local processor...
Definition: globals.h:48
long int NX2_TOT
Total number of zones in the X2 direction (boundaries included) for the local processor.
Definition: globals.h:57
#define SWAP_VAR(x)
Definition: macros.h:115
int j
Definition: analysis.c:2
long int IEND
Upper grid index of the computational domain in the the X1 direction for the local processor...
Definition: globals.h:37
int k
Definition: analysis.c:2
int i
Definition: analysis.c:2
long int KBEG
Lower grid index of the computational domain in the the X3 direction for the local processor...
Definition: globals.h:43
long int KEND
Upper grid index of the computational domain in the the X3 direction for the local processor...
Definition: globals.h:45
long int JBEG
Lower grid index of the computational domain in the the X2 direction for the local processor...
Definition: globals.h:39
long int JEND
Upper grid index of the computational domain in the the X2 direction for the local processor...
Definition: globals.h:41
long int NX1_TOT
Total number of zones in the X1 direction (boundaries included) for the local processor.
Definition: globals.h:55
long int IBEG
Lower grid index of the computational domain in the the X1 direction for the local processor...
Definition: globals.h:35
int AL_Read_array(void *va, int sz_ptr, int istag)
Definition: al_io.c:209

Here is the call graph for this function:

Here is the caller graph for this function:

void WriteBinaryArray ( void *  V,
size_t  dsize,
int  sz,
FILE *  fl,
int  istag 
)

Write an array to disk in binary format.

Parameters
[in]Vsingle pointer to a 3D array, V[k][j][i] –> V[i + NX1*j + NX1*NX2*k]. Must start with index 0
[in]dsizethe size of the each buffer element (sizeof(double) or sizeof(float)). This parameter is used only in serial mode.
[in]szthe distributed array descriptor. This parameter replaces dsize in parallel mode
[in]fla valid FILE pointer
[in]istaga flag to identify cell-centered (istag = -1) or staggered field data (istag = 0,1 or 2 for staggering in the x1, x2 or x3 directions)
Returns
This function has no return value.
Remarks
The data array is assumed to start always with index 0 for both cell-centered and staggered arrays.

Definition at line 98 of file bin_io.c.

120 {
121  int i, j, k;
122  int ioff, joff, koff;
123  char *Vc;
124 
125  #ifdef PARALLEL
126  MPI_Barrier (MPI_COMM_WORLD);
127  AL_Write_array (V, sz, istag);
128  return;
129  #else
130  Vc = (char *) V;
131  ioff = (istag == 0);
132  joff = (istag == 1);
133  koff = (istag == 2);
134 
135  for (k = KBEG; k <= KEND + koff; k++) {
136  for (j = JBEG; j <= JEND + joff; j++) {
137  i = IBEG + (NX1_TOT + ioff)*(j + (NX2_TOT + joff)*k);
138  fwrite (Vc + i*dsize, dsize, NX1 + ioff, fl);
139  }}
140  #endif
141 }
long int NX1
Number of interior zones in the X1 directions (boundaries excluded) for the local processor...
Definition: globals.h:48
long int NX2_TOT
Total number of zones in the X2 direction (boundaries included) for the local processor.
Definition: globals.h:57
int AL_Write_array(void *va, int sz_ptr, int istag)
Definition: al_io.c:115
int j
Definition: analysis.c:2
int k
Definition: analysis.c:2
int i
Definition: analysis.c:2
long int KBEG
Lower grid index of the computational domain in the the X3 direction for the local processor...
Definition: globals.h:43
long int KEND
Upper grid index of the computational domain in the the X3 direction for the local processor...
Definition: globals.h:45
long int JBEG
Lower grid index of the computational domain in the the X2 direction for the local processor...
Definition: globals.h:39
long int JEND
Upper grid index of the computational domain in the the X2 direction for the local processor...
Definition: globals.h:41
long int NX1_TOT
Total number of zones in the X1 direction (boundaries included) for the local processor.
Definition: globals.h:55
long int IBEG
Lower grid index of the computational domain in the the X1 direction for the local processor...
Definition: globals.h:35

Here is the call graph for this function:

Here is the caller graph for this function: