Exemplo n.º 1
0
int read_next_xtc(int fp,
		  int natoms,int *step,real *time,
		  matrix box,rvec *x,real *prec,bool *bOK)
{
  int magic;
  int n;
  XDR *xd;

  *bOK=TRUE;
  xd = gmx_fio_getxdr(fp);
  
  /* read header */
  if (!xtc_header(xd,&magic,&n,step,time,TRUE,bOK))
    return 0;

  /* Check magic number */
  check_xtc_magic(magic);

  if (n > natoms) {
    gmx_fatal(FARGS, "Frame contains more atoms (%d) than expected (%d)", 
	      n, natoms);
  }

  *bOK=xtc_coord(xd,&natoms,box,x,prec,TRUE);

  return *bOK;
}
Exemplo n.º 2
0
int read_next_xtc(t_fileio* fio,
                  int natoms, int64_t *step, real *time,
                  matrix box, rvec *x, real *prec, gmx_bool *bOK)
{
    int  magic;
    int  n;
    XDR *xd;

    *bOK = TRUE;
    xd   = gmx_fio_getxdr(fio);

    /* read header */
    if (!xtc_header(xd, &magic, &n, step, time, TRUE, bOK))
    {
        return 0;
    }

    /* Check magic number */
    check_xtc_magic(magic);

    if (n > natoms)
    {
        gmx_fatal(FARGS, "Frame contains more atoms (%d) than expected (%d)",
                  n, natoms);
    }

    *bOK = (xtc_coord(xd, &natoms, box, x, prec, TRUE) != 0);

    return static_cast<int>(*bOK);
}
Exemplo n.º 3
0
int write_xtc(int fp,
	      int natoms,int step,real time,
	      matrix box,rvec *x,real prec)
{
  int magic_number = XTC_MAGIC;
  XDR *xd;
  bool bDum;
  int bOK;
	
  xd = gmx_fio_getxdr(fp);
  /* write magic number and xtc identidier */
  if (xtc_header(xd,&magic_number,&natoms,&step,&time,FALSE,&bDum) == 0)
  {
	  return 0;
  }
    
  /* write data */
  bOK = xtc_coord(xd,&natoms,box,x,&prec,FALSE); /* bOK will be 1 if writing went well */

  if(bOK)
  {
	  if(gmx_fio_flush(fp) !=0)
	  {
		  bOK = 0;
	  }
  }
  return bOK;  /* 0 if bad, 1 if writing went well */
}
Exemplo n.º 4
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;
}
Exemplo n.º 5
0
int write_xtc(XDRFILE *xd,
			  int natoms,int step,float time,
			  matrix box,rvec *x,float prec)
/* Write a frame to xtc file */
{
	int result;
  
	if ((result = xtc_header(xd,&natoms,&step,&time,FALSE)) != exdrOK)
		return result;

	if ((result = xtc_coord(xd,&natoms,box,x,&prec,0)) != exdrOK)
		return result;
  
	return exdrOK;
}
Exemplo n.º 6
0
int read_xtc(XDRFILE *xd,
			 int natoms,int *step,float *time,
			 matrix box,rvec *x,float *prec)
/* Read subsequent frames */
{
	int result;
  
	if ((result = xtc_header(xd,&natoms,step,time,TRUE)) != exdrOK)
		return result;
	  
	if ((result = xtc_coord(xd,&natoms,box,x,prec,1)) != exdrOK)
		return result;
  
	return exdrOK;
}
Exemplo n.º 7
0
int read_first_xtc(int fp,int *natoms,int *step,real *time,
		   matrix box,rvec **x,real *prec,bool *bOK)
{
  int magic;
  XDR *xd;
  
  *bOK=TRUE;
  xd = gmx_fio_getxdr(fp);
  
  /* read header and malloc x */
  if ( !xtc_header(xd,&magic,natoms,step,time,TRUE,bOK))
    return 0;
    
  /* Check magic number */
  check_xtc_magic(magic);
  
  snew(*x,*natoms);

  *bOK=xtc_coord(xd,natoms,box,*x,prec,TRUE);
  
  return *bOK;
}
Exemplo n.º 8
0
int read_first_xtc(t_fileio *fio, int *natoms, int64_t *step, real *time,
                   matrix box, rvec **x, real *prec, gmx_bool *bOK)
{
    int  magic;
    XDR *xd;

    *bOK = TRUE;
    xd   = gmx_fio_getxdr(fio);

    /* read header and malloc x */
    if (!xtc_header(xd, &magic, natoms, step, time, TRUE, bOK))
    {
        return 0;
    }

    /* Check magic number */
    check_xtc_magic(magic);

    snew(*x, *natoms);

    *bOK = (xtc_coord(xd, natoms, box, *x, prec, TRUE) != 0);

    return static_cast<int>(*bOK);
}
Exemplo n.º 9
0
int write_xtc(t_fileio *fio,
              int natoms, int64_t step, real time,
              const rvec *box, const rvec *x, real prec)
{
    int      magic_number = XTC_MAGIC;
    XDR     *xd;
    gmx_bool bDum;
    int      bOK;

    if (!fio)
    {
        /* This means the fio object is not being used, e.g. because
           we are actually writing TNG output. We still have to return
           a pseudo-success value, to keep some callers happy. */
        return 1;
    }

    xd = gmx_fio_getxdr(fio);
    /* write magic number and xtc identidier */
    if (xtc_header(xd, &magic_number, &natoms, &step, &time, FALSE, &bDum) == 0)
    {
        return 0;
    }

    /* write data */
    bOK = xtc_coord(xd, &natoms, const_cast<rvec *>(box), const_cast<rvec *>(x), &prec, FALSE); /* bOK will be 1 if writing went well */

    if (bOK)
    {
        if (gmx_fio_flush(fio) != 0)
        {
            bOK = 0;
        }
    }
    return bOK; /* 0 if bad, 1 if writing went well */
}
Exemplo 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;
    }
}