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

Write a vector field and its grid to a text 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 WriteText (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 text file.

Author
Debojyoti Ghosh

Definition in file WriteText.c.

Function Documentation

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 _ArraySetValue_(x, size, value)
#define _ArrayIncrementIndex_(N, imax, i, done)
#define _ArrayIndex1D_(N, imax, i, ghost, index)