Ejemplo n.º 1
0
bool XtcLoader::
reinit (const char * filename)
{
  char tmpname[2048];
  strncpy (tmpname, filename, 2047);
  
  xd = xdrfile_open (filename, "r");
  if (xd == NULL){
    std::cerr << "cannot open file " << filename << std::endl;
    return false;
  }
  read_xtc_natoms (tmpname, &natoms);
  step = 0;
  time = 0.;
  box = vector<double > (3, 0.);
  xx = (rvec *) malloc (sizeof(rvec) * natoms);
  prec = 1000.;

  inited = true;

  load ();

  xdrfile_close (xd);
  xd = xdrfile_open (filename, "r");  
  if (xd == NULL){
    std::cerr << "cannot open file " << filename << std::endl;
    clear ();
    return false;
  }
  
  return true;
}
Ejemplo n.º 2
0
int read_trr_nframes(char *fn, unsigned long *nframes) {
    XDRFILE *xd;
    int result, step;
    float time, lambda;
	int natoms;
	matrix box;
	rvec *x;
	*nframes = 0;

	read_trr_natoms(fn, &natoms);
	x = malloc(natoms * sizeof(*x));

    xd = xdrfile_open(fn, "r");
    if (NULL == xd)
        return exdrFILENOTFOUND;

	do {
		result = read_trr(xd, natoms, &step, &time, &lambda,
						  box, x, NULL, NULL);
		if (exdrENDOFFILE != result) {
			(*nframes)++;
		}
	} while (result == exdrOK);

	xdrfile_close(xd);
	free(x);
    return exdrOK;
}
Ejemplo n.º 3
0
TRR::TRR(std::string const & path) {
    const char * path_char_const = path.c_str();
    char * path_char = new char[path.length()+1];
    strcpy(path_char,path_char_const);
    return_code = read_trr_natoms(path_char, &this->number_of_atoms);
    this->file = xdrfile_open(path_char_const, "r");
    if (this->file == NULL){
        throw new std::runtime_error("the trr file was not opened correctly");
    }
}
Ejemplo n.º 4
0
Archivo: io.cpp Proyecto: hax3l/faunus
 /*!
  * This will open an xtc file for reading. The number of atoms in each frame
  * is saved and memory for the coordinate array is allocated.
  */
 bool FormatXTC::open(string s) {
   if (xd!=NULL)
     close();
   xd = xdrfile_open(&s[0], "r");
   if (xd!=NULL) {
     int rc = read_xtc_natoms(&s[0], &natoms_xtc); // get number of atoms
     if (rc==exdrOK) {
       x_xtc = new rvec [natoms_xtc]; // resize coordinate array
       return true;
     }
   } else
     std::cerr << "# ioxtc error: xtc file could not be opened." << endl;
   return false;
 }
Ejemplo n.º 5
0
int read_xtc_natoms(char *fn,int *natoms)
{
	XDRFILE *xd;
	int step,result;
	float time;
	
	xd = xdrfile_open(fn,"r");
	if (NULL == xd)
		return exdrFILENOTFOUND;
	result = xtc_header(xd,natoms,&step,&time,TRUE);
	xdrfile_close(xd);
	
	return result;
}
Ejemplo n.º 6
0
bool TrrLoader::
reinit (const char * filename)
{
  char tmpname[2048];
  strncpy (tmpname, filename, 2047);
  
  xd = xdrfile_open (filename, "r");
  if (xd == NULL){
    std::cerr << "cannot open file " << filename << std::endl;
    return false;
  }
  read_trr_natoms (tmpname, &natoms);
  step = 0;
  time = 0.;
  box = vector<double > (3, 0.);
  xx = (rvec *) malloc (sizeof(rvec) * natoms);
  vv = (rvec *) malloc (sizeof(rvec) * natoms);
  ff = (rvec *) malloc (sizeof(rvec) * natoms);

  inited = true;

  if (load ()) {
    xdrfile_close (xd);
    xd = xdrfile_open (filename, "r");  
    if (xd == NULL){
      std::cerr << "cannot open file " << filename << std::endl;
      clear ();
      return false;
    }
  }
  else {
    std::cerr << "fail to load the 1st frame" << endl;
    return false;
  }
  
  return true;
}
Ejemplo n.º 7
0
int read_trr_natoms(char *fn,int *natoms)
{
	XDRFILE *xd;
	t_trnheader sh;
	int  result;
	
	xd = xdrfile_open(fn,"r");
	if (NULL == xd)
		return exdrFILENOTFOUND;
	if ((result = do_trnheader(xd,1,&sh)) != exdrOK)
		return result;
	xdrfile_close(xd);
	*natoms = sh.natoms;
	
	return exdrOK;
}
Ejemplo n.º 8
0
Archivo: io.cpp Proyecto: hax3l/faunus
 /*!
  * This will take an arbitrary particle vector and add it
  * to an xtc file. No shifting is done - only modification is conversion
  * from aangstom to nanometers. The box dimensions for the frame must be manually
  * set by the ioxtc::setbox() function before calling this.
  */
 bool FormatXTC::save(string file, const p_vec &p) {
   if (xd==NULL)
     xd=xdrfile_open(&file[0], "w");
   if (xd!=NULL) {
     rvec *x = new rvec [p.size()];
     int i=0;
     for (auto &pi : p) {
       x[i][0] = (pi.x() ) * 0.1;      // AA->nm
       x[i][1] = (pi.y() ) * 0.1;
       x[i][2] = (pi.z() ) * 0.1;
       i++;
     }
     write_xtc(xd,p.size(),step_xtc++,time_xtc++,xdbox,x,prec_xtc);
     delete[] x;
     return true;
   }
   return false;
 }
Ejemplo n.º 9
0
int main (int argc, char *argv[]) {
  int st;
  SimParams params;
  int max_steps = std::numeric_limits<int>::max();

  std::string within_filename, output_filename;
  po::options_description desc("Options");
  desc.add_options()
    ("help,h",  "Print help messages")
    ("group,g", po::value<std::string>()->default_value("He"),
     "Group for temperature profiles")
    ("index,n", po::value<std::string>()->default_value("index.ndx"),
     ".ndx file containing atomic indices for groups")
    ("gro", po::value<std::string>()->default_value("conf.gro"),
     ".gro file containing list of atoms/molecules")
    ("top", po::value<std::string>()->default_value("topol.top"),
     ".top file containing atomic/molecular properties")
    ("within,w",
     po::value<std::string>(&within_filename)->default_value("within.dat"),
     ".dat file specifying solvating molecules")
    ("output,o",
     po::value<std::string>(&output_filename)->default_value("qDist.txt"),
     "Output filename");

  po::variables_map vm;
  po::store(po::parse_command_line(argc, argv, desc), vm);
  po::notify(vm);
  if (vm.count("help")) {
    std::cout << desc << "\n";
    exit(EXIT_SUCCESS);
  }

  std::map<std::string, std::vector<int> > groups;
  groups = ReadNdx(vm["index"].as<std::string>());

  std::vector<Molecule> molecules = GenMolecules(vm["top"].as<std::string>(),
                                                 params);
  AtomGroup all_atoms(vm["gro"].as<std::string>(), molecules);
  AtomGroup selected_group(vm["group"].as<std::string>(),
                           SelectGroup(groups, vm["group"].as<std::string>()),
                           all_atoms);

  std::fstream within_file(within_filename.c_str());
  assert(within_file.is_open());

  arma::irowvec within = arma::zeros<arma::irowvec>(selected_group.size());
  arma::irowvec nearest = arma::zeros<arma::irowvec>(4);
  Histogram q_hist(100, 0.01);

  rvec *x_in = NULL;
  matrix box_mat;
  arma::rowvec box = arma::zeros<arma::rowvec>(DIMS);
  std::string xtc_filename = "prod.xtc";
  XDRFILE *xtc_file;
  params.ExtractTrajMetadata(strdup(xtc_filename.c_str()), (&x_in), box);
  xtc_file = xdrfile_open(strdup(xtc_filename.c_str()), "r");
  params.set_max_time(vm["max_time"].as<double>());

  arma::rowvec dx;
  arma::mat dx_near = arma::zeros<arma::mat>(4,DIMS);
  arma::rowvec r2 = arma::zeros<arma::rowvec>(selected_group.size());
  float time,prec;
  for (int step=0; step<max_steps; step++) {
    if(read_xtc(xtc_file, params.num_atoms(), &st, &time, box_mat, x_in, &prec))
      break;
    params.set_box(box_mat);
    int i = 0;
    for (std::vector<int>::iterator i_atom = selected_group.begin();
         i_atom != selected_group.end(); i_atom++, i++) {
      selected_group.set_position(i, x_in[*i_atom]);
    }
    ReadInt(within, within_file);
    for (int i_atom = 0; i_atom < selected_group.size(); ++i_atom) {
      if (!within(i_atom)) continue;
      selected_group.FindNearestk(selected_group.position(i_atom), params.box(),
                                  selected_group.index_to_molecule(i_atom), 4,
                                  dx, r2, nearest);
      int q = 0;
      for (int i_near = 0; i_near < 4; ++i_near) {
        FindDxNoShift(dx, selected_group.position(i_atom),
                      selected_group.position(i_near), box);
        dx_near.row(i_near) = arma::normalise(dx);

        for (int i_near2 = 0; i_near2 < i_near; ++i_near2) {
          double sqrt_q =
              arma::dot(dx_near.row(i_near),dx_near.row(i_near2)) + 1.0/3.0;
          q += sqrt_q*sqrt_q;
        }
      }
      q_hist.Add(q);
    }
  }
  q_hist.Print(output_filename, true);
}
Ejemplo n.º 10
0
int read_xtc_numframes(char *fn, int *numframes, int64_t **offsets)
{
    XDRFILE *xd;
    int framebytes, natoms, step;
    float time;
    int64_t filesize;

	if ((xd = xdrfile_open(fn,"r"))==NULL)
		return exdrFILENOTFOUND;

	if (xtc_header(xd,&natoms,&step,&time,TRUE) != exdrOK)
    {
	    xdrfile_close(xd);
        return exdrHEADER;
    }

    if (xdr_seek(xd, 0L, SEEK_END) != exdrOK)
    {
	    xdrfile_close(xd);
        return exdrNR;
    }
    filesize = xdr_tell(xd);

    /* Case of fewer than 10 atoms. Framesize known. */
    if (natoms < 10)
    {
        int i;
	    xdrfile_close(xd);
        framebytes = XTC_SHORTHEADER_SIZE + XTC_SHORT_BYTESPERATOM*natoms;
        *numframes = filesize/framebytes; /* Should we complain if framesize doesn't divide filesize? */
        /* Allocate memory for the frame index array */
	    if ((*offsets=(int64_t *)malloc(sizeof(int64_t)*(*numframes)))==NULL)
	    	return exdrNOMEM;
        for (i=0; i<*numframes; i++)
        {
            (*offsets)[i] = i*framebytes;
        }
	    return exdrOK;
    }
    else /* No easy way out. We must iterate. */
    {
        int est_nframes;
        /* Estimation of number of frames, with 20% allowance for error. */
        if (xdr_seek(xd, (int64_t) XTC_HEADER_SIZE, SEEK_SET) != exdrOK)
        {
	        xdrfile_close(xd);
            return exdrNR;
        }
        if (xdrfile_read_int(&framebytes,1,xd) == 0)
        {
	        xdrfile_close(xd);
            return exdrENDOFFILE;
        }
        framebytes = (framebytes + 3) & ~0x03; //Rounding to the next 32-bit boundary
        est_nframes = (int) (filesize/((int64_t) (framebytes+XTC_HEADER_SIZE)) + 1); // add one because it'd be easy to underestimate low frame numbers. 
        est_nframes += est_nframes/5;

        /* Allocate memory for the frame index array */
	    if ((*offsets=(int64_t *)malloc(sizeof(int64_t)*est_nframes))==NULL)
        {
	        xdrfile_close(xd);
	    	return exdrNOMEM;
        }
        (*offsets)[0] = 0L;
        *numframes = 1;
        while (1)
        {
            if (xdr_seek(xd, (int64_t) (framebytes+XTC_HEADER_SIZE), SEEK_CUR) != exdrOK) {
                free(*offsets);
	            xdrfile_close(xd);
                return exdrNR;
            }
            if (xdrfile_read_int(&framebytes,1,xd) == 0)
                break;
            /* Read was successful; this is another frame */
            /* Check if we need to enlarge array */
            if (*numframes == est_nframes){
                est_nframes += est_nframes/5 + 1; // Increase in 20% stretches
                if ((*offsets = realloc(*offsets, sizeof(int64_t)*est_nframes))==NULL)
                {
                    free(*offsets);
	                xdrfile_close(xd);
	        	    return exdrNOMEM;
                }
            }
            (*offsets)[*numframes] = xdr_tell(xd) - 4L - (int64_t) (XTC_HEADER_SIZE); //Account for the header and the nbytes bytes we read.
            (*numframes)++;
            framebytes = (framebytes + 3) & ~0x03; //Rounding to the next 32-bit boundary
        }
	    xdrfile_close(xd);
	    return exdrOK;
    }
}
Ejemplo n.º 11
0
   int main( int argc, char *argv[] )
   {

/********************************************************************
 *    Define variables                                              *
 ********************************************************************/

/*
 *    Constants    
 *
 *    Source: 2010 CODATA
 *    c (speed of light in vacuum):  299792458 m s^-1
 *    mu_0 (magnetic constant):      4*pi * 10^-7 N A^-2 (A=Ampere)
 *    e (elementary charge):         1.602176565 * 10^-19 C 
 *    nA (Avogadro constant):        6.02214129 * 10^23 mol^-1
 *    kB (Boltzmann constant):       1.3806488 * 10^-23 J K^-1
 *
 *    Coulomb constant k=1/(4*pi*epsilon_0)=c^2*mu_0/(4*pi)
 *    fQQ = 2.99792458^2 * 10^16 * 10^-7 N m^2 C^-2
 *        = 2.99792458^2 * 6.02214129 * 1.602176565^2 kJ mol^-1 nm e^-2
 *        = 138.93545782935
 *
 *    PI to 20 decimal places:       3.14159265358979323846
 */
      const double kB   =   1.3806488e-3 ; /* 10^-23 kJ K^-1 */
      const double fQQ  = 138.93545782935 ;    /* kJ mol^-1 nm e^-2 */
      const double pi   =   3.14159265358979323846 ;
      const double nA   =   6.02214129 ;  /* 10^23 mol^-1 */
      double *averDens, *averPU, *dens, *pU ;
/*
 *    frame
 */
      int frame;
/*
 *    pair pointers for loop   
 */
      long int pair, totalPairs;
      int    *molI, *molJ;
/*
 *    center of mass   
 */
      double msum ;
/*
 *    variables for pair force calculation   
 */
      int    i, j, im, jm, iMin, iMax, jMin, jMax ;
      double mi, qi, sigi, epsi, qj, sigj, epsj;
/*
 *    variables for xdrfile_trr  
 */
      int     gmxStep,read_return,gmxNAtoms;
      float   gmxTime,gmxLambda;
      matrix  gmxBox;
      rvec    *x, *v, *f;
      XDRFILE *trr;

/********************************************************************
 *    Reading parameters from param.txt                             *
 ********************************************************************/

      FILE    *file_par;
/*
 *    atom = index of atoms in each molecule   
 *    mol  = index of molecules in the system 
 */
      int     mol, atom, molTypes;
/*
 *    atomNr = number of atoms in each type of molecule  
 *    molNr  = number of each type of molecule in the system 
 */
      int     *atomNr, *molNr, *iAtom, *iMol, *mini, *maxi;
      char    line[256], tmp[256], filenameTRR[256];
      double  **mass, **charge, **sigma, **epsilon;
      int     k;
      int     nAtoms, nMols;
      double  *molMass;
/*
 *    open param.txt 
 */
      file_par = fopen("param.txt","r") ;
      if ( NULL==file_par ) {
         printf( "<> Error: Cannot open file param.txt !\n" ) ;
         exit(1);
      }
      mol = 0;
/*
 *    read comments (two lines)
 */
      fgets( line, sizeof( line ), file_par );
      fgets( line, sizeof( line ), file_par );
/*
 *    read name of trr file
 */
      fgets( line, sizeof( line ), file_par );
      sscanf( line, "%s%s", tmp, filenameTRR );
/*
 *    read number of molecules
 */
      fgets( line, sizeof( line ), file_par );
      sscanf( line, "%s%d", tmp, &molTypes );
      atomNr = malloc(molTypes * sizeof(int));
      molNr  = malloc(molTypes * sizeof(int));

      printf ( "   There are %d types of molecules:\n", molTypes );
      printf ( "   %-16s", "MoleculeName" );

      for ( mol=0; mol<molTypes; mol++ ) 
      {
         fgets( line, sizeof( line ), file_par );
         sscanf( line, "%s%d", tmp, &molNr[mol] );

         printf ( "%8s", tmp );

      }

      printf ( "\n" );
/*
 *    read atomic information
 */
      charge  = malloc(molTypes * sizeof(double *));
      mass    = malloc(molTypes * sizeof(double *));
      sigma   = malloc(molTypes * sizeof(double *));
      epsilon = malloc(molTypes * sizeof(double *));

      for ( mol=0; mol<molTypes; mol++ ) 
      {
         fgets( line, sizeof( line ), file_par );
         sscanf( line, "%s%d", tmp, &atomNr[mol] );

         charge[mol]  = malloc(atomNr[mol] * sizeof(double));
         mass[mol]    = malloc(atomNr[mol] * sizeof(double));
         sigma[mol]   = malloc(atomNr[mol] * sizeof(double));
         epsilon[mol] = malloc(atomNr[mol] * sizeof(double));
         for ( atom=0; atom<atomNr[mol]; atom++ ) 
         {
            fgets( line, sizeof( line ), file_par );
            sscanf( line, "%lf%lf%lf%lf",
                    &charge[mol][atom], &mass[mol][atom],
                    &sigma[mol][atom],  &epsilon[mol][atom] );
         }
      }

/*
 *    close param.txt
 */
      fclose( file_par );
/*
 *    print number of atoms in each type of molecule
 */
      printf ( "   %-16s", "ContainAtoms" );

      for ( mol=0; mol<molTypes; mol++ )
      {

         printf ( "%8d", atomNr[mol] );

      }

      printf ( "\n" );

/********************************************************************
 *    Check number of molecules and atoms in each molecule          *
 ********************************************************************/

/*
 *    calculate total number of atoms and molecules
 */
      nAtoms = 0;
      nMols = 0;
      for ( mol=0; mol<molTypes; mol++ ) 
      {
         nAtoms += molNr[mol]*atomNr[mol];
         nMols += molNr[mol];
      }
      iAtom = malloc(nAtoms * sizeof(int));
      iMol  = malloc(nAtoms * sizeof(int));
/*
 *    determine mass of each type of molecule
 */
      molMass = malloc(molTypes * sizeof(double));

      printf ( "   %-16s", "MolarMass" );

      for ( mol=0; mol<molTypes; mol++ ) 
      {
         molMass[mol] = 0.0;
         for ( atom=0; atom<atomNr[mol]; atom++ ) 
         {
            molMass[mol] += mass[mol][atom];
         }

         printf ( "%8.2f", molMass[mol] );

      }

      printf ( "\n" );

/*
 *    print number of atoms in each type of molecule
 */
      printf ( "   %-16s", "N_Molecules" );

      for ( mol=0; mol<molTypes; mol++ )
      {

         printf ( "%8d", molNr[mol] );

      }

      printf ( "\n" );

/*
 *    assign atom index and mol index for each atom in the system 
 *    i = atom index, j = mol index
 */
      i = 0;
      j = 0;
      for ( mol=0; mol<molTypes; mol++ ) 
      {
         for ( k=0; k<molNr[mol]; k++ ) 
         {
            for ( atom=0; atom<atomNr[mol]; atom++ ) 
            {
               iMol[i] = mol;
               iAtom[i] = atom;
               i++;
            }
            j++;
         }
      }

      if ( nAtoms != i ) 
      {
        printf("Incorrect number of atoms in the system!\n");
        printf("Program stops at assigning atom index and mol index!\n");
        exit(1);
      }

      if ( nMols != j ) 
      {
        printf("Incorrect number of molecules in the system!\n");
        printf("Program stops at assigning atom index and mol index!\n");
        exit(1);
      }
/*
 *    assign starting index and ending index for each molecule in the system 
 *    i = atom index, j = mol index
 */
      mini = malloc(nMols * sizeof(int));
      maxi = malloc(nMols * sizeof(int));
      i = 0;
      j = 0;
      for ( mol=0; mol<molTypes; mol++ ) 
      {
         for ( k=0; k<molNr[mol]; k++ ) 
         {
            mini[j] = i;
            i += atomNr[mol];
            maxi[j] = i;
            j++;
         }
      }

      if ( nAtoms != i ) 
      {
        printf("Incorrect number of atoms in the system!\n");
        printf("Program stops at assigning atom index and mol index!\n");
        exit(1);
      }

      if ( nMols != j ) 
      {
        printf("Incorrect number of molecules in the system!\n");
        printf("Program stops at assigning atom index and mol index!\n");
        exit(1);
      }

/********************************************************************
 *    Allocating arrays                                             *
 ********************************************************************/

      x = calloc(nAtoms,sizeof(x[0]));
      v = calloc(nAtoms,sizeof(x[0]));
      f = calloc(nAtoms,sizeof(x[0]));

      totalPairs = nMols*(nMols-1)/2 ;
      molI = malloc( sizeof( int ) * totalPairs );
      molJ = malloc( sizeof( int ) * totalPairs );

/*
 *    read trr files for coordinates   
 */
      trr = xdrfile_open (filenameTRR,"r");
      read_trr_natoms (filenameTRR, &gmxNAtoms);
      /*printf("Total Number of Frames %10d\n", nFrames);*/
      /*printf("Starting from Frame    %10d\n", nStart);*/
/*
 *    start of loop (frame)   
 */
      for (frame=0; frame<2; frame++) 
      {
/*
 *       read trr file   
 */
         read_return = read_trr (trr,gmxNAtoms,&gmxStep,&gmxTime,&gmxLambda,gmxBox,x,v,f);
         if (read_return!=0) {
            printf ("Failed to read trr file!\n");
            exit(1);
         }
         if (gmxNAtoms!=nAtoms) {
            printf ("Incorrect number of Atoms!\n");
            exit(1);
         }
/*
 *       generate density profile   
 */
         for (im=0; im<nMols; im++) 
         {
            msum   = 0.0;
            iMin = mini[im];
            iMax = maxi[im];
            for (i=iMin; i<iMax; i++) 
            {
               mi = mass[iMol[i]][iAtom[i]];
               msum   += mi;
            }
            if ( fabs(msum-molMass[iMol[iMin]])>0.01 ) 
            {
               printf ("Incorrect molar weight of molecule %d!\n", im);
               exit(1);
            }
         }
/*
 *       find all pairs   
 */
         pair = 0;
         for ( im=0; im<nMols-1; im++ ) 
         {
            for ( jm=im+1; jm<nMols; jm++ ) 
            {
               molI[pair] = im;
               molJ[pair] = jm;
               pair++;
            }
         }
         if ( totalPairs != pair ) 
         {
            printf ( "Incorrect number of pairs of molecules!\n" );
            exit(1);
         }
/*
 *    end of loop (frame)   
 */
      }

/*
 *    close trajectory file
 */
      xdrfile_close (trr);

      printf ( "   There are %d molecules in total.\n", nMols );
      printf ( "   There are %d atoms in total.\n", nAtoms );


/*****************************************************************************
 *    End of function main                                                   *
 *****************************************************************************/

   return 0;
   }
Ejemplo n.º 12
0
static void test_xtc()
{
	char *testfn = "test.xtc";
	XDRFILE *xd;
	int result,i,j,k,nframes=13;
	int natoms2,natoms1=173;
	int step2,step1=1993;
	float time2,time1=1097.23;
	matrix box2,box1;
	rvec *x2,*x1;
	float prec2,prec1=1000;
	float toler=1e-3;
	
	printf("Testing xtc functionality:");
	for(i=0; (i<DIM); i++)
		for(j=0; (j<DIM); j++)
			box1[i][j] = (i+1)*3.7 + (j+1);
	x1 = calloc(natoms1,sizeof(*x1));
	if (NULL == x1)
		die("Allocating memory for x1 in test_xtc");
	
	for(i=0; (i<natoms1); i++)
		for(j=0; (j<DIM); j++)
			x1[i][j] = (i+1)*3.7 + (j+1);
	xd = xdrfile_open(testfn,"w");
	if (NULL == xd)
		die("Opening xdrfile for writing");
	for(k=0; (k<nframes); k++)
		{
			result = write_xtc(xd,natoms1,step1+k,time1+k,box1,x1,prec1);
			if (0 != result)
				die_r("Writing xtc file",result);
		}
	xdrfile_close(xd);
	
	result = read_xtc_natoms(testfn,&natoms2);
	if (exdrOK != result)
		die_r("read_xtc_natoms",result);
	if (natoms2 != natoms1)
		die("Number of atoms incorrect when reading trr");
	x2 = calloc(natoms2,sizeof(x2[0]));
	if (NULL == x2)
		die("Allocating memory for x2");
		
		
	xd = xdrfile_open(testfn,"r");
	if (NULL == xd)
		die("Opening xdrfile for reading");
	
	k = 0;
	do
		{
			result = read_xtc(xd,natoms2,&step2,&time2,box2,x2,&prec2);
			if (exdrENDOFFILE != result)
				{
					if (exdrOK != result)
						die_r("read_xtc",result);
					if (natoms2 != natoms1)
						die("natoms2 != natoms1");
					if (step2-step1 != k)
						die("incorrect step");
					if (fabs(time2-time1-k) > toler)
						die("incorrect time");
					if (fabs(prec2-prec1) > toler)
						die("incorrect precision");
					for(i=0; (i<DIM); i++)
						for(j=0; (j<DIM); j++)
							if (fabs(box2[i][j] - box1[i][j]) > toler)
								die("box incorrect");
					for(i=0; (i<natoms1); i++)
						for(j=0; (j<DIM); j++)
							if (fabs(x2[i][j] - x1[i][j]) > toler)
								die("x incorrect");
				}
			k++;
		} while (result == exdrOK);
		
	xdrfile_close(xd);
#ifdef HAVE_UNISTD
	unlink(testfn);
#endif
	printf(" PASSED\n");
}
Ejemplo n.º 13
0
void ReadWrite(char *rfile, char *wfile, int in_xtcBool, int out_xtcBool, int in_trrBool, int out_trrBool)
{
  XDRFILE *xd_read, *xd_write;
  int result_xtc, result_trr;
  int natoms_xtc, natoms_trr;
  int step_xtc, step_trr;
  float time_xtc, time_trr;
  matrix box_xtc, box_trr;
  rvec *x_xtc, *x_trr, *v_trr, *f_trr;
  float prec_xtc = 1000.0;
  float lambda_trr = 0.0;
  
      
  xd_read = xdrfile_open(rfile, "r");
  if (NULL == xd_read)
    die("Opening xdrfile for reading");
  
  /* Test whether output file exists */
  if ((xd_write = xdrfile_open(wfile,"r")) != NULL) {
    xdrfile_close(xd_write);
    die("Output file exists.");
  }
  
  /* Output file does not exist. Now we can open it for writing */
  xd_write = xdrfile_open(wfile, "w");
  if (NULL == xd_write)
    die("Opening xdrfile for writing");
  
  
  /* .xtc -> .xtc */
  if(in_xtcBool && out_xtcBool)
    {
      result_xtc = read_xtc_natoms(rfile, &natoms_xtc);
      if (exdrOK != result_xtc)
	die_r("read_xtc_natoms",result_xtc);
      
      x_xtc = (rvec *)calloc(natoms_xtc, sizeof(x_xtc[0]));

      while(1)
	{
	  result_xtc = read_xtc(xd_read, natoms_xtc, &step_xtc, &time_xtc,
				box_xtc, x_xtc, &prec_xtc);
	    
	  if (result_xtc == 0) // if not reach the end of file, write it to the output.xtc file
	    {
	      if (exdrOK != result_xtc)
		die_r("Reading xtc file", result_xtc);
	      
	      result_xtc = write_xtc(xd_write, natoms_xtc, step_xtc, time_xtc,
				     box_xtc, x_xtc, prec_xtc);
	      
	      if (result_xtc != 0)
		die_r("Writing xtc file", result_xtc);
	    }
	  else
	    break;
	}
    }
  

  /* .xtc -> .trr */
  if(in_xtcBool && out_trrBool)
    {
      result_xtc = read_xtc_natoms(rfile, &natoms_xtc);
      if (exdrOK != result_xtc)
	die_r("read_xtc_natoms",result_xtc);
      
      x_xtc = (rvec *)calloc(natoms_xtc, sizeof(x_xtc[0]));
      
      while(1)
	{
	  result_xtc = read_xtc(xd_read, natoms_xtc, &step_xtc, &time_xtc,
				box_xtc, x_xtc, &prec_xtc);
	  
	  if (result_xtc == 0) // if not reach the end of file, write it to the output.trr file
	    {
	      if (exdrOK != result_xtc)
		die_r("Reading xtc file", result_xtc);
	      
	      result_trr = write_trr(xd_write, natoms_xtc, step_xtc, time_xtc, lambda_trr,
				     box_xtc, x_xtc, NULL, NULL);
	      
	      if (0 != result_trr)
		die_r("Writing trr file",result_trr);
	      
	    }
	  else
	    break;
	}
    }
  
  
  /* .trr -> .trr */
  if(in_trrBool && out_trrBool)
    {
      result_trr = read_trr_natoms(rfile, &natoms_trr);
      
      if (exdrOK != result_trr)
	die_r("read_trr_natoms",result_trr);
      
      x_trr = (rvec *)calloc(natoms_trr, sizeof(x_trr[0]));
      v_trr = (rvec *)calloc(natoms_trr, sizeof(v_trr[0]));
      f_trr = (rvec *)calloc(natoms_trr, sizeof(f_trr[0]));
      
      while (1)
	{
	  result_trr = read_trr(xd_read, natoms_trr, &step_trr, &time_trr, &lambda_trr,
				box_trr, x_trr, v_trr, f_trr);
	      
	  int ii_trr, jj_trr, x_ck=0, v_ck=0, f_ck=0;
	  int x_ck_bool=0, v_ck_bool=0, f_ck_bool=0;
	  
	  for (ii_trr = 0; ii_trr < natoms_trr; ii_trr++)
	    {
	      for(jj_trr = 0; jj_trr < DIM; jj_trr++)
		{
		  if (x_trr[ii_trr][jj_trr] == 0)
		    x_ck++;
		  if (v_trr[ii_trr][jj_trr] == 0)
		    v_ck++;
		  if (f_trr[ii_trr][jj_trr] == 0)
		    f_ck++;
		}
	    }
	  
	  if (x_ck == natoms_trr*DIM)
	    x_ck_bool = 1;
	  if (v_ck == natoms_trr*DIM)
	    v_ck_bool = 1;
	  if (f_ck == natoms_trr*DIM)
	    f_ck_bool = 1;
	      
	  if (result_trr == 0) // if not reach the end of file, write it to the output.trr file
	    {
	      if (exdrOK != result_trr)
		die_r("Reading trr file",result_trr);
	      
	      if(v_ck_bool && f_ck_bool)
		result_trr = write_trr(xd_write, natoms_trr, step_trr, time_trr, lambda_trr,
				       box_trr, x_trr, NULL, NULL);
	      else if(v_ck_bool)
		result_trr = write_trr(xd_write, natoms_trr, step_trr, time_trr, lambda_trr,
				       box_trr, x_trr, NULL, f_trr);
	      else if(f_ck_bool)
		result_trr = write_trr(xd_write, natoms_trr, step_trr, time_trr, lambda_trr,
				       box_trr, x_trr, v_trr, NULL);
	      else
		result_trr = write_trr(xd_write, natoms_trr, step_trr, time_trr, lambda_trr,
				       box_trr, x_trr, v_trr, f_trr);
	      
	      if (0 != result_trr)
		die_r("Writing trr file",result_trr);
	      
	    }
	  else
	    break;
	}
    }
  
  
  /* .trr -> .xtc */
  if(in_trrBool && out_xtcBool)
    {
      result_trr = read_trr_natoms(rfile, &natoms_trr);
      
      if (exdrOK != result_trr)
	die_r("read_trr_natoms",result_trr);
      
      x_trr = (rvec *)calloc(natoms_trr, sizeof(x_trr[0]));
      v_trr = (rvec *)calloc(natoms_trr, sizeof(v_trr[0]));
      f_trr = (rvec *)calloc(natoms_trr, sizeof(f_trr[0]));
      
      while(1)
	{
	  result_trr = read_trr(xd_read, natoms_trr, &step_trr, &time_trr, &lambda_trr,
				box_trr, x_trr, v_trr, f_trr);
	  
	  if (result_trr == 0) // if not reach the end of file, write it to the output.trr file
	    {
	      if (exdrOK != result_trr)
		die_r("Reading trr file", result_trr);
	      
	      result_xtc = write_xtc(xd_write, natoms_trr, step_trr, time_trr,
				     box_trr, x_trr, prec_xtc);
	      
	      if (result_xtc != 0)
		die_r("Writing xtc file", result_xtc);
		}
	  else
	    break;
	}
    }
  
  xdrfile_close(xd_read);
  xdrfile_close(xd_write);
  
}
Ejemplo n.º 14
0
int main (int argc, char *argv[]) {
  int st;
  SimParams params;

  enum RdfWeighting { kNone, kTotalDipole, kPermanentDipole, kInducedDipole };

  po::options_description desc("Options");
  desc.add_options()
    ("help,h",  "Print help messages")
    ("group1", po::value<std::string>()->required(),
     "Group around which rdf is centered")
    ("group2", po::value<std::string>()->required(),
     "Group for which rdf will be calculated around the positions of group 1")
    ("index,n", po::value<std::string>()->default_value("index.ndx"),
     ".ndx file containing atomic indices for groups")
    ("gro", po::value<std::string>()->default_value("conf.gro"),
     ".gro file containing list of atoms/molecules")
    ("top", po::value<std::string>()->default_value("topol.top"),
     ".top file containing atomic/molecular properties")
    ("output,o", po::value<std::string>()->default_value("rdf.txt"),
     "Name for output file.")
    ("max_time,t",
     po::value<double>()->default_value(0.0),
     "Maximum simulation time to use in calculations")
    ("rdf_weighting,w", po::value<std::string>()->default_value("none"),
     "weighting factor for rdf. Choose from 'none', 'dipole', "
     "'permanent_dipole', and 'induced_dipole'");

  po::variables_map vm;
  po::store(po::parse_command_line(argc, argv, desc), vm);
  po::notify(vm);
  if (vm.count("help")) {
    std::cout << desc << "\n";
    exit(EXIT_SUCCESS);
  }

  RdfWeighting rdf_weighting;
  bool need_field = false;
  if (vm["rdf_weighting"].as<std::string>() == "none") {
    rdf_weighting = kNone;
  } else if (vm["rdf_weighting"].as<std::string>() == "dipole") {
    rdf_weighting = kTotalDipole;
    need_field = true;
  } else if (vm["rdf_weighting"].as<std::string>() == "permanent_dipole") {
    rdf_weighting = kPermanentDipole;
  } else if (vm["rdf_weighting"].as<std::string>() == "induced_dipole") {
    rdf_weighting = kInducedDipole;
    need_field = true;
  }

  std::map<std::string, std::vector<int> > groups;
  groups = ReadNdx(vm["index"].as<std::string>());
  std::vector<Molecule> molecules = GenMolecules(vm["top"].as<std::string>(),
                                                 params);

  SystemGroup all_atoms(vm["gro"].as<std::string>(), molecules);
  SubsystemGroup *first_group_pointer =
      SubsystemGroup::MakeSubsystemGroup(
          vm["group1"].as<std::string>(),
          SelectGroup(groups, vm["group1"].as<std::string>()), all_atoms);
  SubsystemGroup &first_group = *first_group_pointer;
  SubsystemGroup *second_group_pointer =
      SubsystemGroup::MakeSubsystemGroup(
          vm["group2"].as<std::string>(),
          SelectGroup(groups, vm["group2"].as<std::string>()), all_atoms);
  SubsystemGroup &second_group = *second_group_pointer;

  rvec *x_in = NULL;
  matrix box_mat;
  arma::rowvec box = arma::zeros<arma::rowvec>(DIMS);
  std::string xtc_filename = "prod.xtc";
  XDRFILE *xtc_file;
  params.ExtractTrajMetadata(strdup(xtc_filename.c_str()), (&x_in), box);
  xtc_file = xdrfile_open(strdup(xtc_filename.c_str()), "r");
  params.set_box(box);
  params.set_max_time(vm["max_time"].as<double>());

  if (need_field)
    second_group.OpenFieldFile();

  Histogram rdf_hist(params.box(0)/2.0/0.05, 0.05);

  arma::rowvec dx, dipole;
  float time, prec;
  int step = 0;
  double avg_volume;
  for (step = 0; step < params.max_steps(); ++step) {
    if(read_xtc(xtc_file, params.num_atoms(), &st, &time, box_mat, x_in, &prec))
      break;
    params.set_box(box_mat);
    avg_volume += params.volume();

    first_group.set_positions(x_in);
    second_group.set_positions(x_in);

    if (need_field) {
      second_group.SetElectricField(all_atoms, params.box());
      if (!second_group.field_check())
        second_group.WriteElectricField();
    }
    second_group.UpdateCom();
    for (int i_other = 0; i_other < second_group.num_molecules(); ++i_other) {
      switch(rdf_weighting) {
        case kPermanentDipole:
          second_group.PermanentDipole(i_other, params.box(), dipole, true);
          dipole *= ENM_TO_D;
          break;
        case kInducedDipole:
          second_group.InducedDipole(i_other, dipole, true);
          dipole *= ENM_TO_D;
          break;
        case kTotalDipole:
          second_group.PermanentDipole(i_other, params.box(), dipole, true);
          second_group.InducedDipole(i_other, dipole);
          dipole *= ENM_TO_D;
          break;
        default:
          break;
      }
      for (int i_atom = 0; i_atom < first_group.size(); ++i_atom) {
        FindDxNoShift(dx, first_group.position(i_atom),
                      second_group.com_position(i_other), params.box());
        double distance = arma::norm(dx);
        double weight;
        if (rdf_weighting == kTotalDipole || rdf_weighting == kInducedDipole ||
            rdf_weighting == kPermanentDipole) {
          dx = arma::normalise(dx);
          weight = arma::dot(dx,dipole);
        } else {
          weight = 1.0;
        }
        rdf_hist.Add(distance, weight);
      }
    }

  }
  xdrfile_close(xtc_file);

  avg_volume /= step;
  rdf_hist.Multiply(1.0/avg_volume/step/first_group.size()/
                    second_group.num_molecules());
  rdf_hist.WeightByShellVolume();

  rdf_hist.Print(vm["output"].as<std::string>(), true);
}
Ejemplo n.º 15
0
int main(int argc, char * argv[])
{
  ValueType begin, end, rup, refh, cellSize;
  std::string ifile, ofile, method;
  ValueType x0, x1;
  
  po::options_description desc ("Allow options");
  desc.add_options()
    ("help,h", "print this message")
    ("begin,b", po::value<ValueType > (&begin)->default_value(0.f), "start time")
    ("end,e",   po::value<ValueType > (&end  )->default_value(0.f), "end   time")
    ("x0", po::value<ValueType > (&x0)->default_value(0.f), "lower bound of the interval")
    ("x1", po::value<ValueType > (&x1)->default_value(1.f), "upper bound of the interval, if x1 == 0, use the whole box")
    ("rup,u",   po::value<ValueType > (&rup)->default_value(3.f), "max r to make rdf")
    ("refh",  po::value<ValueType > (&refh)->default_value(0.01f), "bin size")
    ("cell-size,c", po::value<ValueType > (&cellSize)->default_value(1.f), "cell list radius")
    ("method,m",  po::value<std::string > (&method)->default_value ("adress"), "type of simulation to analyze")
    ("input,f",   po::value<std::string > (&ifile)->default_value ("traj.xtc"), "the input .xtc file")
    ("output,o",  po::value<std::string > (&ofile)->default_value ("rdf.out"), "the output file");
  
  po::variables_map vm;
  po::store(po::parse_command_line(argc, argv, desc), vm);
  po::notify (vm);
  if (vm.count("help")){
    std::cout << desc<< "\n";
    return 0;
  }

  if (x0 > x1) {
    ValueType tmpx = x0;
    x0 = x1;
    x1 = tmpx;
  }  
  
  std::cout << "###################################################" << std::endl;
  std::cout << "# begin->end: " << begin << " " << end << std::endl;
  std::cout << "# [x0,  x1 ]: " << x0 << " " << x1 << std::endl;
  std::cout << "# rup: " << rup << std::endl;
  std::cout << "# refh: " << refh << std::endl;
  std::cout << "# method: " << method << std::endl;
  std::cout << "# input: " << ifile << std::endl;
  std::cout << "###################################################" << std::endl;  
  
  XDRFILE *fp;
  int natoms, step;
  float time;
  matrix box;
  rvec * xx;
  float prec = 1000;
  float time_prec = .01;

  char tmpfname[1024];
  strncpy (tmpfname, ifile.c_str(), 1023);
  int c;
  if ((c = read_xtc_natoms (tmpfname, &natoms)) == 0) {
    // printf ("%d %d\n", c, natoms);
    xx = (rvec *) malloc (sizeof(rvec) * natoms);
  }
  else {
    // printf ("%d %d\n", c, natoms);
    fprintf (stderr, "error read_xtc_natoms");
    exit (1);
  }

  fp = xdrfile_open (ifile.c_str(), "r");
  if (fp == NULL){
    std::cerr << "cannot open file " << ifile << std::endl;
    exit (1);
  }
  if (read_xtc (fp, natoms, &step, &time, box, xx, &prec) != 0) {
    std::cerr << "error reading file " << ifile << std::endl;
    return 1;
  }
  
  int nmolecules = 0;
  if (method == std::string("adress")){
    nmolecules = natoms / 4;
  }
  else if (method == std::string("atom")){
    nmolecules = natoms / 3;
  }
  else if (method == std::string("cg")){
    nmolecules = natoms;
  }
  else {
    std::cerr << "wrong method" << std::endl;
    return 1;
  }

  VectorType vbox;
  vbox.x = box[0][0];
  vbox.y = box[1][1];
  vbox.z = box[2][2];
  if (cellSize >= .5 * vbox.x){
    std::cerr << "the cell size should be less than half of the box size" << std::endl;
    return 1;
  }
  
  std::vector<std::vector<ValueType > > coms;
  coms.reserve (nmolecules);

  CellList clist (nmolecules, vbox, cellSize);

  Rdf myrdf;
  myrdf.reinit (rup, refh, x0, x1);

  int countread = 0;
  while (read_xtc (fp, natoms, &step, &time, box, xx, &prec) == 0){
    if (end != 0.f) {
      if (time < begin - time_prec){
	continue;
      }
      else if (time > end + time_prec) {
	break;
      }	
    }
    else {
      if (time < begin - time_prec) continue;
    }
    if (countread++ % 1 == 0){
      printf ("# load frame at time: %.1f ps\r", time);
      fflush (stdout);
    }
    
    coms.clear ();
    if (method == std::string ("adress")){
      int nmol = natoms / 4;
      for (int i = 0; i < nmol; ++i){
	if      (xx[i*4+3][0] <  0        ) xx[i*4+3][0] += box[0][0];
	else if (xx[i*4+3][0] >= box[0][0]) xx[i*4+3][0] -= box[0][0];
	if      (xx[i*4+3][1] <  0        ) xx[i*4+3][1] += box[1][1];
	else if (xx[i*4+3][1] >= box[1][1]) xx[i*4+3][1] -= box[1][1];
	if      (xx[i*4+3][2] <  0        ) xx[i*4+3][2] += box[2][2];
	else if (xx[i*4+3][2] >= box[2][2]) xx[i*4+3][2] -= box[2][2];
	std::vector<ValueType > tmp(3);
	tmp[0] = xx[i*4+3][0];
	tmp[1] = xx[i*4+3][1];
	tmp[2] = xx[i*4+3][2];
	coms.push_back(tmp);
      }
    }
    else if (method == std::string ("atom")){
      int nmol = natoms / 3;
      for (int i = 0; i < nmol; ++i){
	std::vector<ValueType > com(3, 0.);
	for (int dd = 0; dd < 3; ++dd){
	  ValueType dx1, dx2;
	  dx1 = xx[i*3+1][dd] - xx[i*3+0][dd];
	  dx2 = xx[i*3+2][dd] - xx[i*3+0][dd];
	  if (dx1 > 0.5 * box[dd][dd]) {dx1 -= box[dd][dd]; printf ("hit\n");}
	  if (dx1 <-0.5 * box[dd][dd]) {dx1 += box[dd][dd]; printf ("hit\n");}
	  if (dx2 > 0.5 * box[dd][dd]) {dx2 -= box[dd][dd]; printf ("hit\n");}
	  if (dx2 <-0.5 * box[dd][dd]) {dx2 += box[dd][dd]; printf ("hit\n");}
	  com[dd] = 16. * xx[i*3+0][dd] +
	    1. * (xx[i*3+0][dd] + dx1) +
	    1. * (xx[i*3+0][dd] + dx2);
	  com[dd] /= 18.;
	  if      (com[dd] <  0          ) com[dd] += box[dd][dd];
	  else if (com[dd] >= box[dd][dd]) com[dd] -= box[dd][dd];
	}
	coms.push_back (com);
      }
    }
    else if (method == std::string ("cg")){
      int nmol = natoms;
      for (int i = 0; i < nmol; ++i){
	if      (xx[i][0] <  0        ) xx[i][0] += box[0][0];
	else if (xx[i][0] >= box[0][0]) xx[i][0] -= box[0][0];
	if      (xx[i][1] <  0        ) xx[i][1] += box[1][1];
	else if (xx[i][1] >= box[1][1]) xx[i][1] -= box[1][1];
	if      (xx[i][2] <  0        ) xx[i][2] += box[2][2];
	else if (xx[i][2] >= box[2][2]) xx[i][2] -= box[2][2];
	std::vector<ValueType > tmp(3);
	tmp[0] = xx[i][0];
	tmp[1] = xx[i][1];
	tmp[2] = xx[i][2];
	coms.push_back(tmp);
      }
    }

    clist.rebuild (coms);
    myrdf.deposit (coms, vbox, clist);
  }
  printf ("\n");
  
  xdrfile_close (fp);
  free (xx);


  myrdf.calculate();
  
  FILE *fout = fopen (ofile.c_str(), "w");
  if (fout == NULL){
    std::cerr << "cannot open file " << ofile << std::endl;
    exit (1);
  }

  fprintf (fout, "%f %f\n", 0., myrdf.getValue(0));
  for (unsigned i = 1; i < myrdf.getN(); ++i){
    fprintf (fout, "%f %f\n", (i) * refh, myrdf.getValue(i));
  }

  fclose (fout);

  return 0;
}
vector<double>  read_xtc2dist(char * filename,vector<int> serial_1,vector<int> serial_2,vector<double> mass_1,vector<double> mass_2)

{
        int natoms,step;
        float time_temp;
        float p;
		vector<double> coor_x;
		vector<double> coor_y;
		vector<double> coor_z;
		vector<double> result;
        matrix box;
        rvec *x;
        XDRFILE *xtc;
        xtc=xdrfile_open(filename,"r");
        int read_return=read_xtc_natoms(filename,&natoms);


        x=(rvec * )calloc(natoms,sizeof(x[0]));
        while(1)
        {
                read_return=read_xtc(xtc,natoms,&step,&time_temp,box,x,&p);
		if(step%100000==0)
		{
			
			cout<<"Reading frame"<<"\t"<<step<<" time "<<time_temp<<endl;
		}
                if(read_return!=0)
                {
                        break;
                }

				coor_x.clear();
				coor_y.clear();
				coor_z.clear();

				for(int i=0;i<serial_1.size();i++)
                {
       //                 cout<<step<<"\t"<<time_temp<<"\t"<<natom<<"\t"<<x[natom][0]<<"\t"<<x[natom][1]<<"\t"<<x[natom][2]<<endl;
					
					int natom=serial_1.at(i);
					coor_x.push_back(x[natom][0]);
					coor_y.push_back(x[natom][1]);
					coor_z.push_back(x[natom][2]);
				}
				vector<double> cent_1 =get_center(coor_x,coor_y,coor_z,mass_1);

				coor_x.clear();
				coor_y.clear();
				coor_z.clear();
				for(int i=0;i<serial_2.size();i++)
				{
					int natom=serial_2.at(i);
					coor_x.push_back(x[natom][0]);
					coor_y.push_back(x[natom][1]);
					coor_z.push_back(x[natom][2]);
				}
				vector<double> cent_2 =get_center(coor_x,coor_y,coor_z,mass_2);

				double dist_temp=get_dist(cent_1,cent_2);

				result.push_back(time_temp);
				result.push_back(dist_temp);

        }
        xdrfile_close(xtc);
		return result;

}
void  read_xtc2structure(char * filename,char * out_file, int ion_position,vector<int> quartet_1_serial,vector<int> quartet_2_serial,struct atom * atom_head,int step_frame )
{
        int natoms,step;
        float time_temp;
        float p;
        matrix box;
        rvec *x;
        XDRFILE *xtc;
        xtc=xdrfile_open(filename,"r");
        int read_return=read_xtc_natoms(filename,&natoms);
		
		int num_step=0;

		ofstream out(out_file);
		out<<"#calculate the distence of  ion to the center of the two quartets in z-axis/"<<endl;
		out<<"#Time\t"<<"dist2ions"<<"\t"<<"dist2quartets"<<endl;

		double * ion_coor;
		ion_coor=dvector(0,2);


		struct chain * chain_head;
		chain_head=read_pdb_to_chain(atom_head);
		struct chain * ch_q_1_G_1;
		struct chain * ch_q_1_G_2;
		struct chain * ch_q_1_G_3;
		struct chain * ch_q_1_G_4;
		struct chain * ch_q_2_G_1;
		struct chain * ch_q_2_G_2;
		struct chain * ch_q_2_G_3;
		struct chain * ch_q_2_G_4;
		ch_q_1_G_1=get_base_chain(chain_head,quartet_1_serial.at(0));
		ch_q_1_G_2=get_base_chain(chain_head,quartet_1_serial.at(1));
		ch_q_1_G_3=get_base_chain(chain_head,quartet_1_serial.at(2));
		ch_q_1_G_4=get_base_chain(chain_head,quartet_1_serial.at(3));
		ch_q_2_G_1=get_base_chain(chain_head,quartet_2_serial.at(0));
		ch_q_2_G_2=get_base_chain(chain_head,quartet_2_serial.at(1));
		ch_q_2_G_3=get_base_chain(chain_head,quartet_2_serial.at(2));
		ch_q_2_G_4=get_base_chain(chain_head,quartet_2_serial.at(3));

		struct base_purine_serial * bs_q_1_G_1;
		struct base_purine_serial * bs_q_1_G_2;
		struct base_purine_serial * bs_q_1_G_3;
		struct base_purine_serial * bs_q_1_G_4;
		struct base_purine_serial * bs_q_2_G_1;
		struct base_purine_serial * bs_q_2_G_2;
		struct base_purine_serial * bs_q_2_G_3;
		struct base_purine_serial * bs_q_2_G_4;
//得到G碱基的原子的序号。
		bs_q_1_G_1=read_pdb2purine_base(atom_head,*ch_q_1_G_1);
		bs_q_1_G_2=read_pdb2purine_base(atom_head,*ch_q_1_G_2);
		bs_q_1_G_3=read_pdb2purine_base(atom_head,*ch_q_1_G_3);
		bs_q_1_G_4=read_pdb2purine_base(atom_head,*ch_q_1_G_4);
		bs_q_2_G_1=read_pdb2purine_base(atom_head,*ch_q_2_G_1);
		bs_q_2_G_2=read_pdb2purine_base(atom_head,*ch_q_2_G_2);
		bs_q_2_G_3=read_pdb2purine_base(atom_head,*ch_q_2_G_3);
		bs_q_2_G_4=read_pdb2purine_base(atom_head,*ch_q_2_G_4);



        x=(rvec * )calloc(natoms,sizeof(x[0]));
        while(1)
       {

              read_return=read_xtc(xtc,natoms,&step,&time_temp,box,x,&p);
			  num_step++;

			if(step%10000==0)
			{
				cout<<"Reading frame : "<<step<<"\t time :"<<time_temp<<endl;
			}
             if(read_return!=0)
            {
                 break;
             }
			if(num_step%step_frame==0)
			{
					double ** q_1_G_1_matrix;
					double ** q_1_G_2_matrix;
					double ** q_1_G_3_matrix;
					double ** q_1_G_4_matrix;
					double ** q_2_G_1_matrix;
					double ** q_2_G_2_matrix;
					double ** q_2_G_3_matrix;
					double ** q_2_G_4_matrix;

					 q_1_G_1_matrix=dmatrix(0,8,0,2);
					 q_1_G_2_matrix=dmatrix(0,8,0,2);
					 q_1_G_3_matrix=dmatrix(0,8,0,2);
					 q_1_G_4_matrix=dmatrix(0,8,0,2);
					 q_2_G_1_matrix=dmatrix(0,8,0,2);
					 q_2_G_2_matrix=dmatrix(0,8,0,2);
					 q_2_G_3_matrix=dmatrix(0,8,0,2);
					 q_2_G_4_matrix=dmatrix(0,8,0,2);

					for(int i=0;i<natoms;i++)
			     {
       //    cout<<step<<"\t"<<time_temp<<"\t"<<natom<<"\t"<<x[natom][0]<<"\t"<<x[natom][1]<<"\t"<<x[natom][2]<<endl;
						if((i+1)==ion_position)
						{
							ion_coor[0]=10*x[i][0];
							ion_coor[1]=10*x[i][1];
							ion_coor[2]=10*x[i][2];
	//						cout<<"get ions coordination"<<endl;
						}

					 //q_1_G_1
						 if((i+1)==bs_q_1_G_1->C2_serial)
						{
							q_1_G_1_matrix[6][0]=10*x[i][0];
							q_1_G_1_matrix[6][1]=10*x[i][1];
							q_1_G_1_matrix[6][2]=10*x[i][2];
							
						}
						if((i+1)==bs_q_1_G_1->C4_serial)
						{
							q_1_G_1_matrix[8][0]=10*x[i][0];
							q_1_G_1_matrix[8][1]=10*x[i][1];
							q_1_G_1_matrix[8][2]=10*x[i][2];	

						}
						if((i+1)==bs_q_1_G_1->C5_serial)
						{
							q_1_G_1_matrix[3][0]=10*x[i][0];
							q_1_G_1_matrix[3][1]=10*x[i][1];
							q_1_G_1_matrix[3][2]=10*x[i][2];
						}
						if((i+1)==bs_q_1_G_1->C6_serial)
						{
							q_1_G_1_matrix[4][0]=10*x[i][0];
							q_1_G_1_matrix[4][1]=10*x[i][1];
							q_1_G_1_matrix[4][2]=10*x[i][2];
						}
						if((i+1)==bs_q_1_G_1->C8_serial)
						{
							q_1_G_1_matrix[1][0]=10*x[i][0];
							q_1_G_1_matrix[1][1]=10*x[i][1];
							q_1_G_1_matrix[1][2]=10*x[i][2];
						}
						if((i+1)==bs_q_1_G_1->N1_serial)
						{
							q_1_G_1_matrix[5][0]=10*x[i][0];
							q_1_G_1_matrix[5][1]=10*x[i][1];
							q_1_G_1_matrix[5][2]=10*x[i][2];
						}
						if((i+1)==bs_q_1_G_1->N3_serial)
						{
							q_1_G_1_matrix[7][0]=10*x[i][0];
							q_1_G_1_matrix[7][1]=10*x[i][1];
							q_1_G_1_matrix[7][2]=10*x[i][2];
						}
						if((i+1)==bs_q_1_G_1->N7_serial)
						{
							q_1_G_1_matrix[2][0]=10*x[i][0];
							q_1_G_1_matrix[2][1]=10*x[i][1];
							q_1_G_1_matrix[2][2]=10*x[i][2];	
						}
						if((i+1)==bs_q_1_G_1->N9_serial)
						{
							q_1_G_1_matrix[0][0]=10*x[i][0];
							q_1_G_1_matrix[0][1]=10*x[i][1];
							q_1_G_1_matrix[0][2]=10*x[i][2];
						}
// q_1_G_2
						if((i+1)==bs_q_1_G_2->C2_serial)
						{
							q_1_G_2_matrix[6][0]=10*x[i][0];
							q_1_G_2_matrix[6][1]=10*x[i][1];
							q_1_G_2_matrix[6][2]=10*x[i][2];
						}
						if((i+1)==bs_q_1_G_2->C4_serial)
						{
							q_1_G_2_matrix[8][0]=10*x[i][0];
							q_1_G_2_matrix[8][1]=10*x[i][1];
							q_1_G_2_matrix[8][2]=10*x[i][2];						
						}
						if((i+1)==bs_q_1_G_2->C5_serial)
						{
							q_1_G_2_matrix[3][0]=10*x[i][0];
							q_1_G_2_matrix[3][1]=10*x[i][1];
							q_1_G_2_matrix[3][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_1_G_2->C6_serial)
						{
							q_1_G_2_matrix[4][0]=10*x[i][0];
							q_1_G_2_matrix[4][1]=10*x[i][1];
							q_1_G_2_matrix[4][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_1_G_2->C8_serial)
						{
							q_1_G_2_matrix[1][0]=10*x[i][0];
							q_1_G_2_matrix[1][1]=10*x[i][1];
							q_1_G_2_matrix[1][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_1_G_2->N1_serial)
						{
							q_1_G_2_matrix[5][0]=10*x[i][0];
							q_1_G_2_matrix[5][1]=10*x[i][1];
							q_1_G_2_matrix[5][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_1_G_2->N3_serial)
						{
							q_1_G_2_matrix[7][0]=10*x[i][0];
							q_1_G_2_matrix[7][1]=10*x[i][1];
							q_1_G_2_matrix[7][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_1_G_2->N7_serial)
						{
							q_1_G_2_matrix[2][0]=10*x[i][0];
							q_1_G_2_matrix[2][1]=10*x[i][1];
							q_1_G_2_matrix[2][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_1_G_2->N9_serial)
						{
							q_1_G_2_matrix[0][0]=10*x[i][0];
							q_1_G_2_matrix[0][1]=10*x[i][1];
							q_1_G_2_matrix[0][2]=10*x[i][2];		
						}
//q_1_G_3
						if((i+1)==bs_q_1_G_3->C2_serial)
						{
							q_1_G_3_matrix[6][0]=10*x[i][0];
							q_1_G_3_matrix[6][1]=10*x[i][1];
							q_1_G_3_matrix[6][2]=10*x[i][2];
						}
						if((i+1)==bs_q_1_G_3->C4_serial)
						{
							q_1_G_3_matrix[8][0]=10*x[i][0];
							q_1_G_3_matrix[8][1]=10*x[i][1];
							q_1_G_3_matrix[8][2]=10*x[i][2];						
						}
						if((i+1)==bs_q_1_G_3->C5_serial)
						{
							q_1_G_3_matrix[3][0]=10*x[i][0];
							q_1_G_3_matrix[3][1]=10*x[i][1];
							q_1_G_3_matrix[3][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_1_G_3->C6_serial)
						{
							q_1_G_3_matrix[4][0]=10*x[i][0];
							q_1_G_3_matrix[4][1]=10*x[i][1];
							q_1_G_3_matrix[4][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_1_G_3->C8_serial)
						{
							q_1_G_3_matrix[1][0]=10*x[i][0];
							q_1_G_3_matrix[1][1]=10*x[i][1];
							q_1_G_3_matrix[1][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_1_G_3->N1_serial)
						{
							q_1_G_3_matrix[5][0]=10*x[i][0];
							q_1_G_3_matrix[5][1]=10*x[i][1];
							q_1_G_3_matrix[5][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_1_G_3->N3_serial)
						{
							q_1_G_3_matrix[7][0]=10*x[i][0];
							q_1_G_3_matrix[7][1]=10*x[i][1];
							q_1_G_3_matrix[7][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_1_G_3->N7_serial)
						{
							q_1_G_3_matrix[2][0]=10*x[i][0];
							q_1_G_3_matrix[2][1]=10*x[i][1];
							q_1_G_3_matrix[2][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_1_G_3->N9_serial)
						{
							q_1_G_3_matrix[0][0]=10*x[i][0];
							q_1_G_3_matrix[0][1]=10*x[i][1];
							q_1_G_3_matrix[0][2]=10*x[i][2];		
						}
//q_1_G_4
						if((i+1)==bs_q_1_G_4->C2_serial)
						{
							q_1_G_4_matrix[6][0]=10*x[i][0];
							q_1_G_4_matrix[6][1]=10*x[i][1];
							q_1_G_4_matrix[6][2]=10*x[i][2];
						}
						if((i+1)==bs_q_1_G_4->C4_serial)
						{
							q_1_G_4_matrix[8][0]=10*x[i][0];
							q_1_G_4_matrix[8][1]=10*x[i][1];
							q_1_G_4_matrix[8][2]=10*x[i][2];						
						}
						if((i+1)==bs_q_1_G_4->C5_serial)
						{
							q_1_G_4_matrix[3][0]=10*x[i][0];
							q_1_G_4_matrix[3][1]=10*x[i][1];
							q_1_G_4_matrix[3][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_1_G_4->C6_serial)
						{
							q_1_G_4_matrix[4][0]=10*x[i][0];
							q_1_G_4_matrix[4][1]=10*x[i][1];
							q_1_G_4_matrix[4][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_1_G_4->C8_serial)
						{
							q_1_G_4_matrix[1][0]=10*x[i][0];
							q_1_G_4_matrix[1][1]=10*x[i][1];
							q_1_G_4_matrix[1][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_1_G_4->N1_serial)
						{
							q_1_G_4_matrix[5][0]=10*x[i][0];
							q_1_G_4_matrix[5][1]=10*x[i][1];
							q_1_G_4_matrix[5][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_1_G_4->N3_serial)
						{
							q_1_G_4_matrix[7][0]=10*x[i][0];
							q_1_G_4_matrix[7][1]=10*x[i][1];
							q_1_G_4_matrix[7][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_1_G_4->N7_serial)
						{
							q_1_G_4_matrix[2][0]=10*x[i][0];
							q_1_G_4_matrix[2][1]=10*x[i][1];
							q_1_G_4_matrix[2][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_1_G_4->N9_serial)
						{
							q_1_G_4_matrix[0][0]=10*x[i][0];
							q_1_G_4_matrix[0][1]=10*x[i][1];
							q_1_G_4_matrix[0][2]=10*x[i][2];		
						}
//q_2_G_1
						if((i+1)==bs_q_2_G_1->C2_serial)
						{
							q_2_G_1_matrix[6][0]=10*x[i][0];
							q_2_G_1_matrix[6][1]=10*x[i][1];
							q_2_G_1_matrix[6][2]=10*x[i][2];
						}
						if((i+1)==bs_q_2_G_1->C4_serial)
						{
							q_2_G_1_matrix[8][0]=10*x[i][0];
							q_2_G_1_matrix[8][1]=10*x[i][1];
							q_2_G_1_matrix[8][2]=10*x[i][2];						
						}
						if((i+1)==bs_q_2_G_1->C5_serial)
						{
							q_2_G_1_matrix[3][0]=10*x[i][0];
							q_2_G_1_matrix[3][1]=10*x[i][1];
							q_2_G_1_matrix[3][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_1->C6_serial)
						{
							q_2_G_1_matrix[4][0]=10*x[i][0];
							q_2_G_1_matrix[4][1]=10*x[i][1];
							q_2_G_1_matrix[4][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_1->C8_serial)
						{
							q_2_G_1_matrix[1][0]=10*x[i][0];
							q_2_G_1_matrix[1][1]=10*x[i][1];
							q_2_G_1_matrix[1][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_1->N1_serial)
						{
							q_2_G_1_matrix[5][0]=10*x[i][0];
							q_2_G_1_matrix[5][1]=10*x[i][1];
							q_2_G_1_matrix[5][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_1->N3_serial)
						{
							q_2_G_1_matrix[7][0]=10*x[i][0];
							q_2_G_1_matrix[7][1]=10*x[i][1];
							q_2_G_1_matrix[7][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_1->N7_serial)
						{
							q_2_G_1_matrix[2][0]=10*x[i][0];
							q_2_G_1_matrix[2][1]=10*x[i][1];
							q_2_G_1_matrix[2][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_1->N9_serial)
						{
							q_2_G_1_matrix[0][0]=10*x[i][0];
							q_2_G_1_matrix[0][1]=10*x[i][1];
							q_2_G_1_matrix[0][2]=10*x[i][2];		
						}
//q_2_G_2
						if((i+1)==bs_q_2_G_2->C2_serial)
						{
							q_2_G_2_matrix[6][0]=10*x[i][0];
							q_2_G_2_matrix[6][1]=10*x[i][1];
							q_2_G_2_matrix[6][2]=10*x[i][2];
						}
						if((i+1)==bs_q_2_G_2->C4_serial)
						{
							q_2_G_2_matrix[8][0]=10*x[i][0];
							q_2_G_2_matrix[8][1]=10*x[i][1];
							q_2_G_2_matrix[8][2]=10*x[i][2];						
						}
						if((i+1)==bs_q_2_G_2->C5_serial)
						{
							q_2_G_2_matrix[3][0]=10*x[i][0];
							q_2_G_2_matrix[3][1]=10*x[i][1];
							q_2_G_2_matrix[3][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_2->C6_serial)
						{
							q_2_G_2_matrix[4][0]=10*x[i][0];
							q_2_G_2_matrix[4][1]=10*x[i][1];
							q_2_G_2_matrix[4][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_2->C8_serial)
						{
							q_2_G_2_matrix[1][0]=10*x[i][0];
							q_2_G_2_matrix[1][1]=10*x[i][1];
							q_2_G_2_matrix[1][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_2->N1_serial)
						{
							q_2_G_2_matrix[5][0]=10*x[i][0];
							q_2_G_2_matrix[5][1]=10*x[i][1];
							q_2_G_2_matrix[5][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_2->N3_serial)
						{
							q_2_G_2_matrix[7][0]=10*x[i][0];
							q_2_G_2_matrix[7][1]=10*x[i][1];
							q_2_G_2_matrix[7][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_2->N7_serial)
						{
							q_2_G_2_matrix[2][0]=10*x[i][0];
							q_2_G_2_matrix[2][1]=10*x[i][1];
							q_2_G_2_matrix[2][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_2->N9_serial)
						{
							q_2_G_2_matrix[0][0]=10*x[i][0];
							q_2_G_2_matrix[0][1]=10*x[i][1];
							q_2_G_2_matrix[0][2]=10*x[i][2];		
						}
//q_2_G_3
						if((i+1)==bs_q_2_G_3->C2_serial)
						{
							q_2_G_3_matrix[6][0]=10*x[i][0];
							q_2_G_3_matrix[6][1]=10*x[i][1];
							q_2_G_3_matrix[6][2]=10*x[i][2];
						}
						if((i+1)==bs_q_2_G_3->C4_serial)
						{
							q_2_G_3_matrix[8][0]=10*x[i][0];
							q_2_G_3_matrix[8][1]=10*x[i][1];
							q_2_G_3_matrix[8][2]=10*x[i][2];						
						}
						if((i+1)==bs_q_2_G_3->C5_serial)
						{
							q_2_G_3_matrix[3][0]=10*x[i][0];
							q_2_G_3_matrix[3][1]=10*x[i][1];
							q_2_G_3_matrix[3][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_3->C6_serial)
						{
							q_2_G_3_matrix[4][0]=10*x[i][0];
							q_2_G_3_matrix[4][1]=10*x[i][1];
							q_2_G_3_matrix[4][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_3->C8_serial)
						{
							q_2_G_3_matrix[1][0]=10*x[i][0];
							q_2_G_3_matrix[1][1]=10*x[i][1];
							q_2_G_3_matrix[1][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_3->N1_serial)
						{
							q_2_G_3_matrix[5][0]=10*x[i][0];
							q_2_G_3_matrix[5][1]=10*x[i][1];
							q_2_G_3_matrix[5][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_3->N3_serial)
						{
							q_2_G_3_matrix[7][0]=10*x[i][0];
							q_2_G_3_matrix[7][1]=10*x[i][1];
							q_2_G_3_matrix[7][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_3->N7_serial)
						{
							q_2_G_3_matrix[2][0]=10*x[i][0];
							q_2_G_3_matrix[2][1]=10*x[i][1];
							q_2_G_3_matrix[2][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_3->N9_serial)
						{
							q_2_G_3_matrix[0][0]=10*x[i][0];
							q_2_G_3_matrix[0][1]=10*x[i][1];
							q_2_G_3_matrix[0][2]=10*x[i][2];		
						}
//q_2_G_4
						if((i+1)==bs_q_2_G_4->C2_serial)
						{
							q_2_G_4_matrix[6][0]=10*x[i][0];
							q_2_G_4_matrix[6][1]=10*x[i][1];
							q_2_G_4_matrix[6][2]=10*x[i][2];
						}
						if((i+1)==bs_q_2_G_4->C4_serial)
						{
							q_2_G_4_matrix[8][0]=10*x[i][0];
							q_2_G_4_matrix[8][1]=10*x[i][1];
							q_2_G_4_matrix[8][2]=10*x[i][2];						
						}
						if((i+1)==bs_q_2_G_4->C5_serial)
						{
							q_2_G_4_matrix[3][0]=10*x[i][0];
							q_2_G_4_matrix[3][1]=10*x[i][1];
							q_2_G_4_matrix[3][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_4->C6_serial)
						{
							q_2_G_4_matrix[4][0]=10*x[i][0];
							q_2_G_4_matrix[4][1]=10*x[i][1];
							q_2_G_4_matrix[4][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_4->C8_serial)
						{
							q_2_G_4_matrix[1][0]=10*x[i][0];
							q_2_G_4_matrix[1][1]=10*x[i][1];
							q_2_G_4_matrix[1][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_4->N1_serial)
						{
							q_2_G_4_matrix[5][0]=10*x[i][0];
							q_2_G_4_matrix[5][1]=10*x[i][1];
							q_2_G_4_matrix[5][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_4->N3_serial)
						{
							q_2_G_4_matrix[7][0]=10*x[i][0];
							q_2_G_4_matrix[7][1]=10*x[i][1];
							q_2_G_4_matrix[7][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_4->N7_serial)
						{
							q_2_G_4_matrix[2][0]=10*x[i][0];
							q_2_G_4_matrix[2][1]=10*x[i][1];
							q_2_G_4_matrix[2][2]=10*x[i][2];		
						}
						if((i+1)==bs_q_2_G_4->N9_serial)
						{
							q_2_G_4_matrix[0][0]=10*x[i][0];
							q_2_G_4_matrix[0][1]=10*x[i][1];
							q_2_G_4_matrix[0][2]=10*x[i][2];		
						}
					}
					
					double ** q_1_G_1_rotation_matrix;
					double ** q_1_G_2_rotation_matrix;
					double ** q_1_G_3_rotation_matrix;
					double ** q_1_G_4_rotation_matrix;
					double ** q_2_G_1_rotation_matrix;
					double ** q_2_G_2_rotation_matrix;
					double ** q_2_G_3_rotation_matrix;
					double ** q_2_G_4_rotation_matrix;

					q_1_G_1_rotation_matrix=dmatrix(0,2,0,2);
					q_1_G_2_rotation_matrix=dmatrix(0,2,0,2);
					q_1_G_3_rotation_matrix=dmatrix(0,2,0,2);
					q_1_G_4_rotation_matrix=dmatrix(0,2,0,2);
					q_2_G_1_rotation_matrix=dmatrix(0,2,0,2);
					q_2_G_2_rotation_matrix=dmatrix(0,2,0,2);
					q_2_G_3_rotation_matrix=dmatrix(0,2,0,2);
					q_2_G_4_rotation_matrix=dmatrix(0,2,0,2);

					Rotation_matrix(q_1_G_1_matrix,'G',q_1_G_1_rotation_matrix);
					Rotation_matrix(q_1_G_2_matrix,'G',q_1_G_2_rotation_matrix);
					Rotation_matrix(q_1_G_3_matrix,'G',q_1_G_3_rotation_matrix);
					Rotation_matrix(q_1_G_4_matrix,'G',q_1_G_4_rotation_matrix);
					Rotation_matrix(q_2_G_1_matrix,'G',q_2_G_1_rotation_matrix);
					Rotation_matrix(q_2_G_2_matrix,'G',q_2_G_2_rotation_matrix);
					Rotation_matrix(q_2_G_3_matrix,'G',q_2_G_3_rotation_matrix);
					Rotation_matrix(q_2_G_4_matrix,'G',q_2_G_4_rotation_matrix);

					//以q_1_G_1为基准,校正z-axis的方向。
					double * vector_q_1_G_1;
					double * vector_q_1_G_2;
					double * vector_q_1_G_3;
					double * vector_q_1_G_4;
					double * vector_q_2_G_1;
					double * vector_q_2_G_2;
					double * vector_q_2_G_3;
					double * vector_q_2_G_4;

					vector_q_1_G_1=dvector(0,2);
					vector_q_1_G_2=dvector(0,2);
					vector_q_1_G_3=dvector(0,2);
					vector_q_1_G_4=dvector(0,2);
					vector_q_2_G_1=dvector(0,2);
					vector_q_2_G_2=dvector(0,2);
					vector_q_2_G_3=dvector(0,2);
					vector_q_2_G_4=dvector(0,2);

					for(int i=0;i<3;i++)
					{
						vector_q_1_G_1[i]=q_1_G_1_rotation_matrix[i][2];
						vector_q_1_G_2[i]=q_1_G_2_rotation_matrix[i][2];
						vector_q_1_G_3[i]=q_1_G_3_rotation_matrix[i][2];
						vector_q_1_G_4[i]=q_1_G_4_rotation_matrix[i][2];
						vector_q_2_G_1[i]=q_2_G_1_rotation_matrix[i][2];
						vector_q_2_G_2[i]=q_2_G_2_rotation_matrix[i][2];
						vector_q_2_G_3[i]=q_2_G_3_rotation_matrix[i][2];
						vector_q_2_G_4[i]=q_2_G_4_rotation_matrix[i][2];
					}

					if(dot_product_vector(vector_q_1_G_1,vector_q_1_G_2,3)<0)
					{
						for(int i=1;i<3;i++)
						{
							for(int j=0;j<3;j++)
							{
								q_1_G_2_rotation_matrix[j][i]= - q_1_G_2_rotation_matrix[j][i];
							}
						}
					}
					if(dot_product_vector(vector_q_1_G_1,vector_q_1_G_3,3)<0)
					{
						for(int i=1;i<3;i++)
						{
							for(int j=0;j<3;j++)
							{
								q_1_G_3_rotation_matrix[j][i]= - q_1_G_3_rotation_matrix[j][i];
							}
						}
					}
					if(dot_product_vector(vector_q_1_G_1,vector_q_1_G_4,3)<0)
					{
						for(int i=1;i<3;i++)
						{
							for(int j=0;j<3;j++)
							{
								q_1_G_4_rotation_matrix[j][i]= - q_1_G_4_rotation_matrix[j][i];
							}
						}
					}
					if(dot_product_vector(vector_q_1_G_1,vector_q_2_G_1,3)<0)
					{
						for(int i=1;i<3;i++)
						{
							for(int j=0;j<3;j++)
							{
								q_2_G_1_rotation_matrix[j][i]= - q_2_G_1_rotation_matrix[j][i];
							}
						}
					}
					if(dot_product_vector(vector_q_1_G_1,vector_q_2_G_2,3)<0)
					{
						for(int i=1;i<3;i++)
						{
							for(int j=0;j<3;j++)
							{
								q_2_G_2_rotation_matrix[j][i]= - q_2_G_2_rotation_matrix[j][i];
							}
						}
					}
					if(dot_product_vector(vector_q_1_G_1,vector_q_2_G_3,3)<0)
					{
						for(int i=1;i<3;i++)
						{
							for(int j=0;j<3;j++)
							{
								q_2_G_3_rotation_matrix[j][i]= - q_2_G_3_rotation_matrix[j][i];
							}
						}
					}
					if(dot_product_vector(vector_q_1_G_1,vector_q_2_G_4,3)<0)
					{
						for(int i=1;i<3;i++)
						{
							for(int j=0;j<3;j++)
							{
								q_2_G_4_rotation_matrix[j][i]= - q_2_G_4_rotation_matrix[j][i];
							}
						}
					}


					free_dvector(vector_q_1_G_1,0,2);
					free_dvector(vector_q_1_G_2,0,2);
					free_dvector(vector_q_1_G_3,0,2);
					free_dvector(vector_q_1_G_4,0,2);
					free_dvector(vector_q_2_G_1,0,2);
					free_dvector(vector_q_2_G_2,0,2);
					free_dvector(vector_q_2_G_3,0,2);
					free_dvector(vector_q_2_G_4,0,2);


					//get the origin position of 8 guanine.
					double * origin_vector_q_1_G_1;
					double * origin_vector_q_1_G_2;
					double * origin_vector_q_1_G_3;
					double * origin_vector_q_1_G_4;
					double * origin_vector_q_2_G_1;
					double * origin_vector_q_2_G_2;
					double * origin_vector_q_2_G_3;
					double * origin_vector_q_2_G_4;

					origin_vector_q_1_G_1=dvector(0,2);
					origin_vector_q_1_G_2=dvector(0,2);
					origin_vector_q_1_G_3=dvector(0,2);
					origin_vector_q_1_G_4=dvector(0,2);
					origin_vector_q_2_G_1=dvector(0,2);
					origin_vector_q_2_G_2=dvector(0,2);
					origin_vector_q_2_G_3=dvector(0,2);
					origin_vector_q_2_G_4=dvector(0,2);


					origin_vector(q_1_G_1_matrix,q_1_G_1_rotation_matrix,'G',origin_vector_q_1_G_1);
					origin_vector(q_1_G_2_matrix,q_1_G_2_rotation_matrix,'G',origin_vector_q_1_G_2);
					origin_vector(q_1_G_3_matrix,q_1_G_3_rotation_matrix,'G',origin_vector_q_1_G_3);
					origin_vector(q_1_G_4_matrix,q_1_G_4_rotation_matrix,'G',origin_vector_q_1_G_4);
					origin_vector(q_2_G_1_matrix,q_2_G_1_rotation_matrix,'G',origin_vector_q_2_G_1);
					origin_vector(q_2_G_2_matrix,q_2_G_2_rotation_matrix,'G',origin_vector_q_2_G_2);
					origin_vector(q_2_G_3_matrix,q_2_G_3_rotation_matrix,'G',origin_vector_q_2_G_3);
					origin_vector(q_2_G_4_matrix,q_2_G_4_rotation_matrix,'G',origin_vector_q_2_G_4);
				
					//get the z-axis
					vector_q_1_G_1=dvector(0,2);
					vector_q_1_G_2=dvector(0,2);
					vector_q_1_G_3=dvector(0,2);
					vector_q_1_G_4=dvector(0,2);
					vector_q_2_G_1=dvector(0,2);
					vector_q_2_G_2=dvector(0,2);
					vector_q_2_G_3=dvector(0,2);
					vector_q_2_G_4=dvector(0,2);

					for(int i=0;i<3;i++)
					{
						vector_q_1_G_1[i]=q_1_G_1_rotation_matrix[i][2];
						vector_q_1_G_2[i]=q_1_G_2_rotation_matrix[i][2];
						vector_q_1_G_3[i]=q_1_G_3_rotation_matrix[i][2];
						vector_q_1_G_4[i]=q_1_G_4_rotation_matrix[i][2];
						vector_q_2_G_1[i]=q_2_G_1_rotation_matrix[i][2];
						vector_q_2_G_2[i]=q_2_G_2_rotation_matrix[i][2];
						vector_q_2_G_3[i]=q_2_G_3_rotation_matrix[i][2];
						vector_q_2_G_4[i]=q_2_G_4_rotation_matrix[i][2];

		//				cout<<vector_q_1_G_1[i]<<endl;  //for test 
		//				cout<<vector_q_1_G_2[i]<<endl;  //for test 
		//				cout<<vector_q_1_G_3[i]<<endl;  //for test 
		//				cout<<vector_q_1_G_4[i]<<endl;  //for test 
		//				cout<<vector_q_2_G_1[i]<<endl;  //for test 
		//				cout<<vector_q_2_G_2[i]<<endl;  //for test 
			//			cout<<vector_q_2_G_3[i]<<endl;  //for test 
		//				cout<<vector_q_2_G_4[i]<<endl;  //for test 
					}

					double * vector_q_1_G_1_2;
					double * vector_q_1_G_3_4;
					double * vector_q_1_G_1_2_3_4;
					double * vector_q_2_G_1_2;
					double * vector_q_2_G_3_4;
					double * vector_q_2_G_1_2_3_4;
					double * vector_orientation;

					 vector_q_1_G_1_2=dvector(0,2);
					 vector_q_1_G_3_4=dvector(0,2);
					 vector_q_1_G_1_2_3_4=dvector(0,2);
					 vector_q_2_G_1_2=dvector(0,2);
					 vector_q_2_G_3_4=dvector(0,2);
					 vector_q_2_G_1_2_3_4=dvector(0,2);
					 vector_orientation=dvector(0,2);

					rotate_2_vector(vector_q_1_G_1,vector_q_1_G_2, vector_q_1_G_1_2);
					// for test
			//		for(int i=0;i<3;i++)
		//			{
		//				cout<<vector_q_1_G_1_2[i]<<endl;
		//			}
					//
					rotate_2_vector(vector_q_1_G_3,vector_q_1_G_4,vector_q_1_G_3_4);

					rotate_2_vector(vector_q_2_G_1,vector_q_2_G_2,vector_q_2_G_1_2);
					rotate_2_vector(vector_q_2_G_3,vector_q_2_G_4,vector_q_2_G_3_4);

					rotate_2_vector(vector_q_1_G_1_2,vector_q_1_G_3_4,vector_q_1_G_1_2_3_4);
					rotate_2_vector(vector_q_2_G_1_2,vector_q_2_G_3_4,vector_q_2_G_1_2_3_4);

					rotate_2_vector(vector_q_1_G_1_2_3_4,vector_q_2_G_1_2_3_4,vector_orientation);
					//for test
					/*
					for(int i=0;i<3;i++)
					{
						cout<<vector_orientation[i]<<endl;
					}
					*/
					//

					//get the center position.
					double * center_position_vector;
					double * center_1;
					double * center_2;
					double * vector_2_ion;
					double * vector_1_2;
					center_position_vector=dvector(0,2);
					vector_2_ion=dvector(0,2);
					center_1=dvector(0,2);
					center_2=dvector(0,2);
					vector_1_2=dvector(0,2);

					for(int i=0;i<3;i++)
					{
						center_position_vector[i]=(origin_vector_q_1_G_1[i]+origin_vector_q_1_G_2[i]+origin_vector_q_1_G_3[i]+origin_vector_q_1_G_4[i]+origin_vector_q_2_G_1[i]+origin_vector_q_2_G_2[i]+origin_vector_q_2_G_3[i]+origin_vector_q_2_G_4[i])/8;
						center_1[i]=(origin_vector_q_1_G_1[i]+origin_vector_q_1_G_2[i]+origin_vector_q_1_G_3[i]+origin_vector_q_1_G_4[i])/4;
						center_2[i]=(origin_vector_q_2_G_1[i]+origin_vector_q_2_G_2[i]+origin_vector_q_2_G_3[i]+origin_vector_q_2_G_4[i])/4;
						vector_2_ion[i]=ion_coor[i]-center_position_vector[i];

						vector_1_2[i]=center_1[i]-center_2[i];
					}
					
					double length=0;
					double dist_z=0;
					double dist=0;
	
					length=dot_product_vector(vector_orientation,vector_2_ion,3); 
					dist_z=dot_product_vector(vector_orientation,vector_1_2,3);
					dist=sqrt(dot_product_vector(vector_1_2,vector_1_2,3));								
					 
					//
					out<<fixed<<showpoint;
					out<<time_temp<<"\t"<<setprecision(4)<<fabs(length)<<"\t"<<setprecision(4)<<dist<<"\t"<<setprecision(4)<<fabs(dist_z)<<endl;		


					free_dmatrix(q_1_G_1_matrix,0,2,0,8);
					free_dmatrix(q_1_G_2_matrix,0,2,0,8);
					free_dmatrix(q_1_G_3_matrix,0,2,0,8);
					free_dmatrix(q_1_G_4_matrix,0,2,0,8);
					free_dmatrix(q_2_G_1_matrix,0,2,0,8);
					free_dmatrix(q_2_G_2_matrix,0,2,0,8);
					free_dmatrix(q_2_G_3_matrix,0,2,0,8);
					free_dmatrix(q_2_G_4_matrix,0,2,0,8);

					free_dmatrix(q_1_G_1_rotation_matrix,0,2,0,2);
					free_dmatrix(q_1_G_2_rotation_matrix,0,2,0,2);
					free_dmatrix(q_1_G_3_rotation_matrix,0,2,0,2);
					free_dmatrix(q_1_G_4_rotation_matrix,0,2,0,2);
					free_dmatrix(q_2_G_1_rotation_matrix,0,2,0,2);
					free_dmatrix(q_2_G_2_rotation_matrix,0,2,0,2);
					free_dmatrix(q_2_G_3_rotation_matrix,0,2,0,2);
					free_dmatrix(q_2_G_4_rotation_matrix,0,2,0,2);

					free_dvector(vector_q_1_G_1,0,2);
					free_dvector(vector_q_1_G_2,0,2);
					free_dvector(vector_q_1_G_3,0,2);
					free_dvector(vector_q_1_G_4,0,2);
					free_dvector(vector_q_2_G_1,0,2);
					free_dvector(vector_q_2_G_2,0,2);
					free_dvector(vector_q_2_G_3,0,2);
					free_dvector(vector_q_2_G_4,0,2);

					free_dvector(origin_vector_q_1_G_1,0,2);
					free_dvector(origin_vector_q_1_G_2,0,2);
					free_dvector(origin_vector_q_1_G_3,0,2);
					free_dvector(origin_vector_q_1_G_4,0,2);
					free_dvector(origin_vector_q_2_G_1,0,2);
					free_dvector(origin_vector_q_2_G_2,0,2);
					free_dvector(origin_vector_q_2_G_3,0,2);
					free_dvector(origin_vector_q_2_G_4,0,2);

					 free_dvector(vector_q_1_G_1_2,0,2);
					 free_dvector(vector_q_1_G_3_4,0,2);
					 free_dvector(vector_q_1_G_1_2_3_4,0,2);
					 free_dvector(vector_q_2_G_1_2,0,2);
					 free_dvector(vector_q_2_G_3_4,0,2);
					 free_dvector( vector_q_2_G_1_2_3_4,0,2);
				     free_dvector( vector_orientation,0,2);
			}
        }
        xdrfile_close(xtc);
		out.close();
}
Ejemplo n.º 18
0
static void test_basic()
{
	float test_ii;      // 7 significant digits
	double test_jj;     // 16 significant digits
#define EPSILON_1 1e-7
#define EPSILON_2 1e-4
	
	printf("Testing basic xdrfile library:");
	for (test_ii = 1.0e1; test_ii < 1.0e2; (test_ii = test_ii + pow(M_PI, 0.00011)))
		{
		
#define BUFLEN 37
	XDRFILE *xfp;
	int i, j, k, len, ncoord = BUFLEN/3;
	char ptr[BUFLEN], *buf = "abcdefghijklmnopqrstuvwxyz";
	char *testfn = "test.xdr";
	unsigned char uptr[BUFLEN];
	short sptr[BUFLEN], sptr2[BUFLEN];
	unsigned short usptr[BUFLEN], usptr2[BUFLEN];
	int iptr[BUFLEN], iptr2[BUFLEN];
	unsigned int uiptr[BUFLEN], uiptr2[BUFLEN];
	float fptr[BUFLEN], fptr2[BUFLEN];
	double dptr[BUFLEN], dptr2[BUFLEN];
	char optr[BUFLEN], optr2[BUFLEN];
#define NPREC 1

	float  fprec[] = {234.45};
	double dprec[] = {234.45};

	/* Can not write a string that's on the stack since all data is
	   treated as variables.
	 */
	len = strlen(buf) + 1;
	if (len >= BUFLEN)
		die("Increase BUFLEN");
	strcpy(ptr, buf);
	strcpy((char *) uptr, buf);
	/* Initiate float arrays */
	for (i = 0; (i < BUFLEN); i++) 
	{
		fptr[i] = cos(i * 13.0 / M_PI);
		dptr[i] = sin(i * 13.0 / M_PI);
	}
	/* Initiate opaque array */
	memcpy(optr, dptr, BUFLEN);

	/*************************************/
	/*           WRITING BIT             */
	/*************************************/

	if ((xfp = xdrfile_open("test.xdr", "w")) == NULL)
		die("Can not open file for writing");

	if (xdrfile_write_char(ptr, len, xfp) != len)
		die("Writing char string");
	if (xdrfile_write_uchar(uptr, len, xfp) != len)
		die("Writing uchar string");
	if (xdrfile_write_short(sptr, BUFLEN,xfp) != BUFLEN)
		die("Writing short array");
	if (xdrfile_write_ushort(usptr, BUFLEN,xfp) != BUFLEN)
		die("Writing ushort array");
	if (xdrfile_write_int(iptr, BUFLEN,xfp) != BUFLEN)
		die("Writing int array");
	if (xdrfile_write_uint(uiptr, BUFLEN,xfp) != BUFLEN)
		die("Writing uint array");
	if (xdrfile_write_float(fptr, BUFLEN,xfp) != BUFLEN)
		die("Writing float array");
	if (xdrfile_write_double(dptr, BUFLEN,xfp) != BUFLEN)
		die("Writing double array");
	if (xdrfile_write_string(buf, xfp) != len)
		die("Writing string");
	if (xdrfile_write_opaque(optr, BUFLEN,xfp) != BUFLEN)
		die("Writing opaque");
	for (k = 0; (k < NPREC); k++) {
		if (xdrfile_compress_coord_float(fptr, ncoord, fprec[k], xfp)
				!= ncoord)
			die("Writing compress_coord_float");
		if (xdrfile_compress_coord_double(dptr, ncoord, dprec[k], xfp)
				!= ncoord)
			die("Writing compress_coord_double");
	}
	if (xdrfile_close(xfp) != 0)
		die("Can not close xdr file");

	/*************************************/
	/*          READING BIT              */
	/*************************************/
	if ((xfp = xdrfile_open(testfn, "r")) == NULL)
		die("Can not open file for reading");

	if ((xdrfile_read_char(ptr, len, xfp)) != len)
		die("Not the right number of chars read from string");
	if (strcmp(ptr, buf) != 0)
		printf("did not read the expected chars");
	if (xdrfile_read_uchar(uptr, len, xfp) != len)
		die("Not the right number of uchars read from string");
	if (strcmp((char *) uptr, buf) != 0)
		printf("did not read the expected uchars");
	if (xdrfile_read_short(sptr2, BUFLEN,xfp) != BUFLEN)
		die("Reading short array");
	for (i = 0; (i < BUFLEN); i++)
		if (sptr2[i] != sptr[i]) {
			fprintf(stderr, "i: %5d, wrote: %10d, read: %10d\n", i, sptr[i],
					sptr2[i]);
			die("Comparing short array");
		}
	if (xdrfile_read_ushort(usptr2, BUFLEN,xfp) != BUFLEN)
		die("Reading ushort array");
	for (i = 0; (i < BUFLEN); i++)
		if (usptr2[i] != usptr[i]) {
			fprintf(stderr, "i: %5d, wrote: %10d, read: %10d\n", i, usptr[i],
					usptr2[i]);
			die("Comparing ushort array");
		}
	if (xdrfile_read_int(iptr2, BUFLEN,xfp) != BUFLEN)
		die("Reading int array");
	for (i = 0; (i < BUFLEN); i++)
		if (iptr2[i] != iptr[i]) {
			fprintf(stderr, "i: %5d, wrote: %10d, read: %10d\n", i, iptr[i],
					iptr2[i]);
			die("Comparing int array");
		}
	if (xdrfile_read_uint(uiptr2, BUFLEN,xfp) != BUFLEN)
		die("Reading uint array");
	for (i = 0; (i < BUFLEN); i++)
		if (uiptr2[i] != uiptr[i]) {
			fprintf(stderr, "i: %5d, wrote: %10d, read: %10d\n", i, uiptr[i],
					uiptr2[i]);
			die("Comparing uint array");
		}
	if (xdrfile_read_float(fptr2, BUFLEN,xfp) != BUFLEN)
		die("Reading float array");
	for (i = 0; (i < BUFLEN); i++)
		if (fptr2[i] != fptr[i]) {
			fprintf(stderr, "i: %5d, wrote: %12g, read: %12g\n", i, fptr[i],
					fptr2[i]);
			die("Comparing float array");
		}
	if (xdrfile_read_double(dptr2, BUFLEN,xfp) != BUFLEN)
		die("Reading double array");
	for (i = 0; (i < BUFLEN); i++)
		if (dptr2[i] != dptr[i]) {
			fprintf(stderr, "i: %5d, wrote: %12g, read: %12g\n", i, dptr[i],
					dptr2[i]);
			die("Comparing double array");
		}
	if (xdrfile_read_string(ptr, BUFLEN,xfp) != len)
		die("Reading string");
	if (strcmp(ptr, buf) != 0)
		die("Comparing strings");
	if (xdrfile_read_opaque(optr2, BUFLEN,xfp) != BUFLEN)
		die("Reading opaque array");
	for (i = 0; (i < BUFLEN); i++)
		if (optr2[i] != optr[i]) {
			fprintf(stderr, "i: %5d, wrote: %2d, read: %2d\n", i, optr[i],
					optr2[i]);
			die("Comparing opaque array");
		}
	for (k = 0; (k < NPREC); k++) {
		float ff, fx;
		double dd, dx;
		int nc = ncoord;
		if (xdrfile_decompress_coord_float(fptr2, &nc, &ff, xfp) != ncoord)
			die("Reading compress_coord_float");
		if (fabs(ff - fprec[k]) > EPSILON_1) 
			{
				printf("Found precision %f, expected %f\n", ff, fprec[k]);
				die("Float precision");
			}
		if (ff <= 0)
			ff = 1000;


		for (i = 0; (i < ncoord); i++)
			for (j = 0; (j < 3); j++) {
				fx = rint(fptr[3 * i + j] * ff) / ff;
				if (fabs(fx - fptr2[3 * i + j]) > EPSILON_1) {
					printf(	"prec: %10g, i: %3d, j: %d, fx: %10g, fptr2: %12g, fptr: %12g\n",
							ff, i, j, fx, fptr2[3 * i + j], fptr[3 * i + j]);
					die("Reading decompressed float coordinates");
				}
			}
		if (xdrfile_decompress_coord_double(dptr2, &nc, &dd, xfp) != ncoord)
			die("Reading compress_coord_double");

		if (fabs(dd - dprec[k]) > EPSILON_2)
			die("Double precision");

		for (i = 0; (i < ncoord); i++)
			for (j = 0; (j < 3); j++) {
				dx = rint(dptr[3 * i + j] * dd) / dd;
				if (fabs(dx - dptr2[3 * i + j]) > EPSILON_2) {
					printf(	"prec: %10g, i: %3d, j: %d, dx: %10g, dptr2: %12g, dptr: %12g\n",
							dd, i, j, dx, dptr2[3 * i + j], dptr[3 * i + j]);
					die("Reading decompressed double coordinates");
				}
			}
	}

	if (xdrfile_close(xfp) != 0)
		die("Can not close xdr file");
		} 
#ifdef HAVE_UNISTD
	unlink(testfn);
#endif
	printf(" PASSED\n");
}