HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WriteBinary.c File Reference

Write a vector field and its grid to a binary file. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <basic.h>
#include <arrayfunctions.h>

Go to the source code of this file.

Functions

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

Detailed Description

Write a vector field and its grid to a binary file.

Author
Debojyoti Ghosh

Definition in file WriteBinary.c.

Function Documentation

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 }