HyPar  1.0
Finite-Difference Hyperbolic-Parabolic PDE Solver on Cartesian Grids
WriteBinary.c
Go to the documentation of this file.
1 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <basic.h>
9 #include <arrayfunctions.h>
10 
35  int ndims,
36  int nvars,
37  int *dim,
39  double *x,
41  double *u,
43  char *f,
44  int *index
45  )
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 }
Some basic definitions and macros.
int WriteBinary(int ndims, int nvars, int *dim, double *x, double *u, char *f, int *index)
Definition: WriteBinary.c:34
Contains macros and function definitions for common array operations.