Data Representation and Input/Output

The information that must be stored and handled in an MC program are the particle positions. In 3D space, each particle has three components of its position. The simplest way to represent this information in a program is by using parallel arrays; that is, three arrays, dimensioned from 1 to $ N$, one for each dimension. In C, we might declare these arrays for 1,000 particles as

  int N = 1000;
  double rx[N], ry[N], rz[N];

This data can be stored in standard ASCII text files, or in unformatted binary files. In fact, a large part of most molecular simulation is producing and storing configurations which can be processed “offline” (away from the simulation program) to perform analyses. A simple and widely used ASCII configuration file format is called “XYZ”. A code fragment to write a configuration of $ N$ hydrogen atoms in XYZ format appears below:

  FILE * fp;
  ...
  fp = fopen("my_config.xyz","w");
  fprintf(fp,"%i\n",N);
  fprintf(fp,"This is a comment or just a blank line\n");
  for (i=0;i<N;i++) 
    fprintf(fp,"%s %.8le %.8le %.8le\n","H",rx[i],ry[i],rz[i]);

The main feature of XYZ files is that one can specify the element type of each atom; for simple visualization purposes of our toy systems, this can be anything, so we'll just use H for now.

It's quite easy to read ASCII data in that was written in XYZ format, e.g., using the fragment above, ignoring the element types (for now):

  FILE * fp;
  int N;
  double * rx, * ry, * rz;
  char dummy[4];
  ...
  fp = fopen("my_config.xyz","r");
  fscanf(fp,"%i\n",&N);
  /* allocate if not done already */
  rx=(int*)malloc(N*sizeof(int));
  ry=(int*)malloc(N*sizeof(int));
  rz=(int*)malloc(N*sizeof(int));
  fscanf(fp,"\n"); /* reads the comment line but throws it away */
  for (i=0;i<N;i++) 
    fscanf(fp,"%s %.8le %.8le %.8le\n",dummy,&rx[i],&ry[i],&rz[i]);

cfa22@drexel.edu