HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
io.h File Reference

Function declarations for file I/O functions. More...

Go to the source code of this file.

Functions

int ReadArray (int, int, int *, int *, int, void *, void *, double *, double *, char *, int *)
 
int ReadArraywInterp (int, int, int *, int *, int *, int, void *, void *, double *, double *, char *, int *)
 
int WriteArray (int, int, int *, int *, int, double *, double *, void *, void *, char *)
 
int WriteBinary (int, int, int *, double *, double *, char *, int *)
 
int WriteText (int, int, int *, double *, double *, char *, int *)
 
int WriteTecplot2D (int, int, int *, double *, double *, char *, int *)
 
int WriteTecplot3D (int, int, int *, double *, double *, char *, int *)
 

Detailed Description

Function declarations for file I/O functions.

Author
Debojyoti Ghosh

Definition in file io.h.

Function Documentation

◆ ReadArray()

int ReadArray ( int  ndims,
int  nvars,
int *  dim_global,
int *  dim_local,
int  ghosts,
void *  s,
void *  m,
double *  x,
double *  u,
char *  fname_root,
int *  read_flag 
)

Read in a vector field from file: wrapper function that calls the appropriate function depending on input mode (HyPar::input_mode).

The mode and type of input are specified through HyPar::input_mode and HyPar::ip_file_type. A vector field is read from file and stored in an array.

Parameters
ndimsNumber of spatial dimensions
nvarsNumber of variables per grid point
dim_globalInteger array of size ndims with global grid size in each dimension
dim_localInteger array of size ndims with local grid size in each dimension
ghostsNumber of ghost points
sSolver object of type HyPar
mMPI object of type MPIVariables
xGrid associated with the array (can be NULL)
uArray to hold the vector field
fname_rootFilename root
read_flagFlag to indicate if the file was read

Definition at line 25 of file ReadArray.c.

38 {
39  HyPar *solver = (HyPar*) s;
40  MPIVariables *mpi = (MPIVariables*) m;
42 
43  if (!strcmp(solver->input_mode,"serial")) {
44  IERR ReadArraySerial(ndims,nvars,dim_global,dim_local,ghosts,s,m,x,u,fname_root,read_flag);
45  CHECKERR(ierr);
46 #ifndef serial
47  } else if (!strcmp(solver->input_mode,"parallel")) {
48  ReadArrayParallel(ndims,nvars,dim_global,dim_local,ghosts,s,m,x,u,fname_root,read_flag);
49  CHECKERR(ierr);
50  } else if (!strcmp(solver->input_mode,"mpi-io" )) {
51  ReadArrayMPI_IO(ndims,nvars,dim_global,dim_local,ghosts,s,m,x,u,fname_root,read_flag);
52  CHECKERR(ierr);
53 #endif
54  } else {
55  fprintf(stderr,"Error: Illegal value (%s) for input_mode.\n",solver->input_mode);
56  return(1);
57  }
58 
59  if (x) {
60  int offset, d;
61  /* exchange MPI-boundary values of x between processors */
62  offset = 0;
63  for (d = 0; d < ndims; d++) {
64  IERR MPIExchangeBoundaries1D(mpi,&x[offset],dim_local[d],
65  ghosts,d,ndims); CHECKERR(ierr);
66  offset += (dim_local [d] + 2*ghosts);
67  }
68  /* fill in ghost values of x at physical boundaries by extrapolation */
69  offset = 0;
70  for (d = 0; d < ndims; d++) {
71  double *X = &x[offset];
72  int *dim = dim_local, i;
73  if (mpi->ip[d] == 0) {
74  /* fill left boundary along this dimension */
75  for (i = 0; i < ghosts; i++) {
76  int delta = ghosts - i;
77  X[i] = X[ghosts] + ((double) delta) * (X[ghosts]-X[ghosts+1]);
78  }
79  }
80  if (mpi->ip[d] == mpi->iproc[d]-1) {
81  /* fill right boundary along this dimension */
82  for (i = dim[d]+ghosts; i < dim[d]+2*ghosts; i++) {
83  int delta = i - (dim[d]+ghosts-1);
84  X[i] = X[dim[d]+ghosts-1]
85  + ((double) delta) * (X[dim[d]+ghosts-1]-X[dim[d]+ghosts-2]);
86  }
87  }
88  offset += (dim[d] + 2*ghosts);
89  }
90  }
91 
92  return(0);
93 }
#define IERR
Definition: basic.h:16
#define CHECKERR(ierr)
Definition: basic.h:18
char input_mode[_MAX_STRING_SIZE_]
Definition: hypar.h:177
int MPIExchangeBoundaries1D(void *, double *, int, int, int, int)
Structure containing all solver-specific variables and functions.
Definition: hypar.h:23
static int ReadArrayMPI_IO(int, int, int *, int *, int, void *, void *, double *, double *, char *, int *)
Definition: ReadArray.c:513
Structure of MPI-related variables.
#define _DECLARE_IERR_
Definition: basic.h:17
static int ReadArraySerial(int, int, int *, int *, int, void *, void *, double *, double *, char *, int *)
Definition: ReadArray.c:150
static int ReadArrayParallel(int, int, int *, int *, int, void *, void *, double *, double *, char *, int *)
Definition: ReadArray.c:341

◆ ReadArraywInterp()

int ReadArraywInterp ( int  ndims,
int  nvars,
int *  dim_global,
int *  dim_local,
int *  dim_global_src,
int  ghosts,
void *  s,
void *  m,
double *  x,
double *  u,
char *  fname_root,
int *  read_flag 
)

Read in a vector field from file: This version allows reading data with different dimensions than the array being read in. The data is read in and stored in a new global array with the appropriate size, and the array to be read is filled by interpolation. Currently, the dimensions of the array to be read and the those of the actual data can only differ by factors that are integer powers of 2.

This is a wrapper function that calls the appropriate function depending on input mode (HyPar::input_mode).

The mode and type of input are specified through HyPar::input_mode and HyPar::ip_file_type. A vector field is read from file and stored in an array.

Parameters
ndimsNumber of spatial dimensions
nvarsNumber of variables per grid point
dim_globalInteger array of size ndims with global size in each dimension
dim_localInteger array of size ndims with local size in each dimension
dim_global_srcInteger array of size ndims with global size of the data in each dimension
ghostsNumber of ghost points
sSolver object of type HyPar
mMPI object of type MPIVariables
xGrid associated with the array (can be NULL)
uArray to hold the vector field
fname_rootFilename root
read_flagFlag to indicate if the file was read

Definition at line 28 of file ReadArraywInterp.c.

41 {
42  HyPar *solver = (HyPar*) s;
43  MPIVariables *mpi = (MPIVariables*) m;
45 
46  int retval = ReadArraywInterpSerial( ndims,
47  nvars,
48  dim_global,
49  dim_local,
50  dim_global_src,
51  ghosts,
52  s,
53  m,
54  x,
55  u,
56  fname_root,
57  read_flag );
58  if (retval) return retval;
59 
60  if (x) {
61  int offset, d;
62  /* exchange MPI-boundary values of x between processors */
63  offset = 0;
64  for (d = 0; d < ndims; d++) {
65  IERR MPIExchangeBoundaries1D(mpi,&x[offset],dim_local[d],
66  ghosts,d,ndims); CHECKERR(ierr);
67  offset += (dim_local [d] + 2*ghosts);
68  }
69  /* fill in ghost values of x at physical boundaries by extrapolation */
70  offset = 0;
71  for (d = 0; d < ndims; d++) {
72  double *X = &x[offset];
73  int *dim = dim_local, i;
74  if (mpi->ip[d] == 0) {
75  /* fill left boundary along this dimension */
76  for (i = 0; i < ghosts; i++) {
77  int delta = ghosts - i;
78  X[i] = X[ghosts] + ((double) delta) * (X[ghosts]-X[ghosts+1]);
79  }
80  }
81  if (mpi->ip[d] == mpi->iproc[d]-1) {
82  /* fill right boundary along this dimension */
83  for (i = dim[d]+ghosts; i < dim[d]+2*ghosts; i++) {
84  int delta = i - (dim[d]+ghosts-1);
85  X[i] = X[dim[d]+ghosts-1]
86  + ((double) delta) * (X[dim[d]+ghosts-1]-X[dim[d]+ghosts-2]);
87  }
88  }
89  offset += (dim[d] + 2*ghosts);
90  }
91  }
92 
93  return 0;
94 }
#define IERR
Definition: basic.h:16
#define CHECKERR(ierr)
Definition: basic.h:18
static int ReadArraywInterpSerial(int, int, int *, int *, int *, int, void *, void *, double *, double *, char *, int *)
int MPIExchangeBoundaries1D(void *, double *, int, int, int, int)
Structure containing all solver-specific variables and functions.
Definition: hypar.h:23
Structure of MPI-related variables.
#define _DECLARE_IERR_
Definition: basic.h:17

◆ WriteArray()

int WriteArray ( int  ndims,
int  nvars,
int *  dim_global,
int *  dim_local,
int  ghosts,
double *  x,
double *  u,
void *  s,
void *  m,
char *  fname_root 
)

Write out a vector field, stored as an array, to file: wrapper function that calls the appropriate function depending on output mode (HyPar::output_mode). The output file format is determined by HyPar::op_file_format

Parameters
ndimsNumber of spatial dimensions
nvarsNumber of variables per grid point
dim_globalInteger array of size ndims with global grid size in each dimension
dim_localInteger array of size ndims with local grid size in each dimension
ghostsNumber of ghost points
xArray of spatial coordinates (i.e. the grid)
uVector field to write
sSolver object of type HyPar
mMPI object of type MPIVariables
fname_rootFilename root (extension is added automatically). For unsteady output, a numerical index is added that is the same as for the solution output files.

Definition at line 27 of file WriteArray.c.

40 {
41  HyPar *solver = (HyPar*) s;
42  MPIVariables *mpi = (MPIVariables*)m;
44 
45  /* if WriteOutput() is NULL, then return */
46  if (!solver->WriteOutput) return(0);
47 
48 #ifndef serial
49  if (!strcmp(solver->output_mode,"serial")) {
50 #endif
51  IERR WriteArraySerial(ndims,nvars,dim_global,dim_local,ghosts,x,u,
52  solver,mpi,fname_root); CHECKERR(ierr);
53 #ifndef serial
54  } else {
55  IERR WriteArrayParallel(ndims,nvars,dim_global,dim_local,ghosts,x,u,
56  solver,mpi,fname_root); CHECKERR(ierr);
57  }
58 #endif
59 
60  return(0);
61 }
#define IERR
Definition: basic.h:16
#define CHECKERR(ierr)
Definition: basic.h:18
char output_mode[_MAX_STRING_SIZE_]
Definition: hypar.h:183
Structure containing all solver-specific variables and functions.
Definition: hypar.h:23
int(* WriteOutput)(int, int, int *, double *, double *, char *, int *)
Definition: hypar.h:211
Structure of MPI-related variables.
#define _DECLARE_IERR_
Definition: basic.h:17
static int WriteArrayParallel(int, int, int *, int *, int, double *, double *, void *, void *, char *)
Definition: WriteArray.c:194
static int WriteArraySerial(int, int, int *, int *, int, double *, double *, void *, void *, char *)
Definition: WriteArray.c:74

◆ WriteBinary()

int WriteBinary ( int  ndims,
int  nvars,
int *  dim,
double *  x,
double *  u,
char *  f,
int *  index 
)

Write a vector field and its grid to a binary file. The data is written in the following format:

{
ndims
nvars
dim[0] dim[1] dim[2] ... dim[ndims-1]
x0_i (0 <= i < dim[0])
x1_i (0 <= i < dim[1])
...
x{ndims-1}_i (0 <= i < dim[ndims-1])
[u0,u1,...,u{nvars-1}]_p (0 <= p < N) (with no commas)
}

where
x0, x1, ..., x{ndims-1} represent the spatial dimensions (for a 3D problem, x0 = x, x1 = y, x2 = z),
u0, u1, ..., u{nvars-1} are each component of the vector u at a grid point,
N = dim[0]*dim[1]*...*dim[ndims-1] is the total number of points,
and p = i0 + dim[0]*( i1 + dim[1]*( i2 + dim[2]*( ... + dim[ndims-2] * i{ndims-1} ))) (see _ArrayIndexnD_)
with i0, i1, i2, etc representing grid indices along each spatial dimension, i.e.,
0 <= i0 < dim[0]-1
0 <= i1 < dim[1]-1
...
0 <= i{ndims-1} < dim[ndims=1]-1

Parameters
ndimsNumber of spatial dimensions
nvarsNumber of variables at each grid point
dimInteger array with the number of grid points in each spatial dimension as its entries
xArray of spatial coordinates representing a Cartesian grid (no ghost points)
uArray containing the vector field to write (no ghost points)
fFilename
indexPreallocated integer array of size ndims

Definition at line 34 of file WriteBinary.c.

46 {
47  int size, d;
48  size_t bytes;
49  FILE *out;
50  out = fopen(f,"wb");
51  if (!out) {
52  fprintf(stderr,"Error: could not open %s for writing.\n",f);
53  return(1);
54  }
55 
56  /* write ndims, nvars */
57  bytes = fwrite(&ndims,sizeof(int),1,out);
58  if ((int)bytes != 1) {
59  fprintf(stderr,"Error in WriteBinary(): Unable to write ndims to output file.\n");
60  }
61  bytes = fwrite(&nvars,sizeof(int),1,out);
62  if ((int)bytes != 1) {
63  fprintf(stderr,"Error in WriteBinary(): Unable to write nvars to output file.\n");
64  }
65 
66  /* write dimensions */
67  bytes = fwrite(dim,sizeof(int),ndims,out);
68  if ((int)bytes != ndims) {
69  fprintf(stderr,"Error in WriteBinary(): Unable to write dimensions to output file.\n");
70  }
71 
72  /* write grid */
73  size = 0;
74  for (d = 0; d < ndims; d++) size += dim[d];
75  bytes = fwrite(x,sizeof(double),size,out);
76  if ((int)bytes != size) {
77  fprintf(stderr,"Error in WriteBinary(): Unable to write grid to output file.\n");
78  }
79 
80  /* write solution */
81  size = 1;
82  for (d = 0; d < ndims; d++) size *= dim[d]; size *= nvars;
83  bytes = fwrite(u,sizeof(double),size,out);
84  if ((int)bytes != size) {
85  fprintf(stderr,"Error in WriteBinary(): Unable to write solution to output file.\n");
86  }
87 
88  fclose(out);
89  return(0);
90 }

◆ WriteText()

int WriteText ( int  ndims,
int  nvars,
int *  dim,
double *  x,
double *  u,
char *  f,
int *  index 
)

Write a vector field and its associated grid to a text file. The data is written to the text file in the following format:

Each line of the file contains
i0 i1 ... i{ndims-1} x0[i0] x1[i1] ... x{ndims-1}[i{ndims-1}] u0[p] u1[p] ... u{nvars-1}[p]

where
(i0,i1,...,i{ndims-1}) represents a ndims-dimensional grid index,
x0, x1, ..., x{ndims-1} are the spatial dimensions (e.g. in 3D, ndims = 3, x0 = x, x1 = y, x2 = z)
u0, u1, ..., u{nvars-1} are the components of the vector field u
p = i0 + dim[0] * (i1 + dim[1] * (i2 + dim[2] * ( ... + dim[ndims-2] * i{ndims-1} ))) (see _ArrayIndexnD_)
Thus, the number of lines is dim[0]*dim[1]*...*dim[ndims-1]

This is a plain text file, so it can be visualized using, say MATLAB, or any software that can read data from a plain text file. For 1D (ndims = 1), the data can be easily plotted in Gnuplot (for example).

Parameters
ndimsNumber of spatial dimensions
nvarsNumber of variables at each grid point
dimInteger array with the number of grid points in each spatial dimension as its entries
xArray of spatial coordinates representing a Cartesian grid (no ghost points)
uArray containing the vector field to write (no ghost points)
fFilename
indexPreallocated integer array of size ndims

Definition at line 27 of file WriteText.c.

39 {
40  FILE *out;
41  out = fopen(f,"w");
42  if (!out) {
43  fprintf(stderr,"Error: could not open %s for writing.\n",f);
44  return(1);
45  }
46 
47  int done = 0; _ArraySetValue_(index,ndims,0);
48  while (!done) {
49  int i, p;
50  _ArrayIndex1D_(ndims,dim,index,0,p);
51  for (i=0; i<ndims; i++) fprintf(out,"%4d ",index[i]);
52  for (i=0; i<ndims; i++) {
53  int j,offset = 0; for (j=0; j<i; j++) offset += dim[j];
54  fprintf(out,"%+1.16E ",x[offset+index[i]]);
55  }
56  for (i=0; i<nvars; i++) fprintf(out,"%+1.16E ",u[nvars*p+i]);
57  fprintf(out,"\n");
58  _ArrayIncrementIndex_(ndims,dim,index,done);
59  }
60  fclose(out);
61  return(0);
62 }
#define _ArrayIndex1D_(N, imax, i, ghost, index)
#define _ArraySetValue_(x, size, value)
#define _ArrayIncrementIndex_(N, imax, i, done)

◆ WriteTecplot2D()

int WriteTecplot2D ( int  ndims,
int  nvars,
int *  dim,
double *  x,
double *  u,
char *  f,
int *  index 
)

Write a vector field and its associated grid to a 2D Tecplot file. This file can then be visualized using Tecplot (http://www.tecplot.org) (if available).

Note: It's essentially a text file, and apart from the first two lines with Tecplot specific headers, the data is written out in the same format as WriteText().

Parameters
ndimsNumber of spatial dimensions
nvarsNumber of variables at each grid point
dimInteger array with the number of grid points in each spatial dimension as its entries
xArray of spatial coordinates representing a Cartesian grid (no ghost points)
uArray containing the vector field to write (no ghost points)
fFilename
indexPreallocated integer array of size ndims

Definition at line 19 of file WriteTecplot2D.c.

31 {
32  if (ndims !=2) {
33  fprintf(stderr,"Error in WriteTecplot2D(): This functions is hardcoded for 2-dimensional ");
34  fprintf(stderr,"problems only. Instead, ndims=%d.\n",ndims);
35  return(1);
36  }
37  int i;
38  int imax = dim[0];
39  int jmax = dim[1];
40 
41  FILE *out;
42  out = fopen(f,"w");
43  if (!out) {
44  fprintf(stderr,"Error: could not open %s for writing.\n",f);
45  return(1);
46  }
47 
48  /* writing tecplot data file headers */
49  fprintf(out,"VARIABLES=\"I\",\"J\",\"X\",\"Y\",");
50  char varname[3] = "00";
51  for (i = 0; i < nvars; i++) {
52  fprintf(out,"\"%s\",",varname);
53  if (varname[1] == '9') { varname[0]++; varname[1] = '0'; }
54  else varname[1]++;
55  }
56  fprintf(out,"\n");
57  fprintf(out,"ZONE I=%d,J=%d,F=POINT\n",imax,jmax);
58 
59  /* writing the data */
60  int done = 0; _ArraySetValue_(index,ndims,0);
61  while (!done) {
62  int i, p;
63  _ArrayIndex1D_(ndims,dim,index,0,p);
64  for (i=0; i<ndims; i++) fprintf(out,"%4d ",index[i]);
65  for (i=0; i<ndims; i++) {
66  int j,offset = 0; for (j=0; j<i; j++) offset += dim[j];
67  fprintf(out,"%+1.16E ",x[offset+index[i]]);
68  }
69  for (i=0; i<nvars; i++) fprintf(out,"%+1.16E ",u[nvars*p+i]);
70  fprintf(out,"\n");
71  _ArrayIncrementIndex_(ndims,dim,index,done);
72  }
73  fclose(out);
74  return(0);
75 }
#define _ArrayIndex1D_(N, imax, i, ghost, index)
#define _ArraySetValue_(x, size, value)
#define _ArrayIncrementIndex_(N, imax, i, done)

◆ WriteTecplot3D()

int WriteTecplot3D ( int  ndims,
int  nvars,
int *  dim,
double *  x,
double *  u,
char *  f,
int *  index 
)

Write a vector field and its associated grid to a 3D Tecplot file. This file can then be visualized using Tecplot (http://www.tecplot.org) (if available)

Note: It's essentially a text file, and apart from the first two lines with Tecplot specific headers, the data is written out in the same format as WriteText().

Parameters
ndimsNumber of spatial dimensions
nvarsNumber of variables at each grid point
dimInteger array with the number of grid points in each spatial dimension as its entries
xArray of spatial coordinates representing a Cartesian grid (no ghost points)
uArray containing the vector field to write (no ghost points)
fFilename
indexPreallocated integer array of size ndims

Definition at line 19 of file WriteTecplot3D.c.

31 {
32  if (ndims !=3) {
33  fprintf(stderr,"Error in WriteTecplot3D(): This functions is hardcoded for 3-dimensional ");
34  fprintf(stderr,"problems only. Instead, ndims=%d.\n",ndims);
35  return(1);
36  }
37  int i;
38  int imax = dim[0];
39  int jmax = dim[1];
40  int kmax = dim[2];
41 
42  FILE *out;
43  out = fopen(f,"w");
44  if (!out) {
45  fprintf(stderr,"Error: could not open %s for writing.\n",f);
46  return(1);
47  }
48 
49  /* writing tecplot data file headers */
50  fprintf(out,"VARIABLES=\"I\",\"J\",\"K\",\"X\",\"Y\",\"Z\",");
51  char varname[3] = "00";
52  for (i = 0; i < nvars; i++) {
53  fprintf(out,"\"%s\",",varname);
54  if (varname[1] == '9') { varname[0]++; varname[1] = '0'; }
55  else varname[1]++;
56  }
57  fprintf(out,"\n");
58  fprintf(out,"ZONE I=%d,J=%d,K=%d,F=POINT\n",imax,jmax,kmax);
59 
60  /* writing the data */
61  int done = 0; _ArraySetValue_(index,ndims,0);
62  while (!done) {
63  int i, p;
64  _ArrayIndex1D_(ndims,dim,index,0,p);
65  for (i=0; i<ndims; i++) fprintf(out,"%4d ",index[i]);
66  for (i=0; i<ndims; i++) {
67  int j,offset = 0; for (j=0; j<i; j++) offset += dim[j];
68  fprintf(out,"%+1.16E ",x[offset+index[i]]);
69  }
70  for (i=0; i<nvars; i++) fprintf(out,"%+1.16E ",u[nvars*p+i]);
71  fprintf(out,"\n");
72  _ArrayIncrementIndex_(ndims,dim,index,done);
73  }
74  fclose(out);
75  return(0);
76 }
#define _ArrayIndex1D_(N, imax, i, ghost, index)
#define _ArraySetValue_(x, size, value)
#define _ArrayIncrementIndex_(N, imax, i, done)