next up previous
Next: Analytical Potentials Up: Elements of a Continuous-Space Previous: Elements of a Continuous-Space

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 FORTRAN and C, we might declare these arrays for 1,000 particles as

In FORTRAN In C
      parameter (N = 1000)
      real*8 rx(N), ry(N), rz(N)
  const 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 code fragment to write a configuration of $N$ atoms appears below:

In FORTRAN In C
	do 100 i=1,N
	   write(9,101) i,rx(i),ry(i),rz(i)
100     continue
101	format(i,e13.8,e13.8,e13.8)
! writes to file fort.9
  fp = fopen("file1.dat","w");
  for (i=0;i<N;i++) 
    fprintf(fp,"%i %.8le %.8le %.8le\n",
     i,rx[i],ry[i],rz[i]);
/* fp is declared as a pointer of type FILE */

It's quite easy to read ASCII data in:

In FORTRAN In C
	do 100 i=1,N
	   read(9,101) i,rx(i),ry(i),rz(i)
100     continue
101	format(i,e13.8,e13.8,e13.8)
! read from file fort.9
  fp = fopen("file1.dat","r");
  for (i=0;i<N;i++) 
    fscanf(fp,"%i %.8le %.8le %.8le\n",
     &i,&rx[i],&ry[i],&rz[i]);
/* fp is declared as a pointer of type FILE */


next up previous
Next: Analytical Potentials Up: Elements of a Continuous-Space Previous: Elements of a Continuous-Space
cfa22@drexel.edu