Example #1
0
int get_tpr_version(const char *infile)
{
	char  	buf[STRLEN];
	gmx_bool  	bDouble;
	int 	precision,fver;
        t_fileio *fio;

	fio = open_tpx(infile,"r");
	gmx_fio_checktype(fio);

	precision = sizeof(real);

	gmx_fio_do_string(fio,buf);
	if (strncmp(buf,"VERSION",7))
		gmx_fatal(FARGS,"Can not read file %s,\n"
				"             this file is from a Gromacs version which is older than 2.0\n"
				"             Make a new one with grompp or use a gro or pdb file, if possible",
				gmx_fio_getname(fio));
	gmx_fio_do_int(fio,precision);
	bDouble = (precision == sizeof(double));
	if ((precision != sizeof(float)) && !bDouble)
		gmx_fatal(FARGS,"Unknown precision in file %s: real is %d bytes "
				"instead of %d or %d",
				gmx_fio_getname(fio),precision,sizeof(float),sizeof(double));
	gmx_fio_setprecision(fio,bDouble);
	fprintf(stderr,"Reading file %s, %s (%s precision)\n",
			gmx_fio_getname(fio),buf,bDouble ? "double" : "single");

	gmx_fio_do_int(fio,fver);

	close_tpx(fio);

	return fver;
}
Example #2
0
static gmx_bool do_trnheader(t_fileio *fio, gmx_bool bRead, t_trnheader *sh, gmx_bool *bOK)
{
    int             magic  = GROMACS_MAGIC;
    static gmx_bool bFirst = TRUE;
    char            buf[256];

    *bOK = TRUE;

    gmx_fio_checktype(fio);

    if (!gmx_fio_do_int(fio, magic) || magic != GROMACS_MAGIC)
    {
        return FALSE;
    }

    if (bRead)
    {
        *bOK = *bOK && gmx_fio_do_string(fio, buf);
        if (bFirst)
        {
            fprintf(stderr, "trn version: %s ", buf);
        }
    }
    else
    {
        sprintf(buf, "GMX_trn_file");
        *bOK = *bOK && gmx_fio_do_string(fio, buf);
    }
    *bOK = *bOK && gmx_fio_do_int(fio, sh->ir_size);
    *bOK = *bOK && gmx_fio_do_int(fio, sh->e_size);
    *bOK = *bOK && gmx_fio_do_int(fio, sh->box_size);
    *bOK = *bOK && gmx_fio_do_int(fio, sh->vir_size);
    *bOK = *bOK && gmx_fio_do_int(fio, sh->pres_size);
    *bOK = *bOK && gmx_fio_do_int(fio, sh->top_size);
    *bOK = *bOK && gmx_fio_do_int(fio, sh->sym_size);
    *bOK = *bOK && gmx_fio_do_int(fio, sh->x_size);
    *bOK = *bOK && gmx_fio_do_int(fio, sh->v_size);
    *bOK = *bOK && gmx_fio_do_int(fio, sh->f_size);
    *bOK = *bOK && gmx_fio_do_int(fio, sh->natoms);

    if (!*bOK)
    {
        return *bOK;
    }
    sh->bDouble = (nFloatSize(sh) == sizeof(double));
    gmx_fio_setprecision(fio, sh->bDouble);

    if (bRead && bFirst)
    {
        fprintf(stderr, "(%s precision)\n", sh->bDouble ? "double" : "single");
        bFirst = FALSE;
    }

    *bOK = *bOK && gmx_fio_do_int(fio, sh->step);
    *bOK = *bOK && gmx_fio_do_int(fio, sh->nre);
    *bOK = *bOK && gmx_fio_do_real(fio, sh->t);
    *bOK = *bOK && gmx_fio_do_real(fio, sh->lambda);

    return *bOK;
}
Example #3
0
void do_enxnms(ener_file_t ef,int *nre,gmx_enxnm_t **nms)
{
    int  magic=-55555;
    XDR  *xdr;
    gmx_bool bRead = gmx_fio_getread(ef->fio);
    int  file_version;
    int  i;
   
    gmx_fio_checktype(ef->fio); 

    xdr = gmx_fio_getxdr(ef->fio);
    
    if (!xdr_int(xdr,&magic))
    {
        if(!bRead)
        {
            gmx_file("Cannot write energy names to file; maybe you are out of quota?");
        }
        *nre=0;
        return;
    }
    if (magic > 0)
    {
        /* Assume this is an old edr format */
        file_version = 1;
        *nre = magic;
        ef->eo.bOldFileOpen = TRUE;
        ef->eo.bReadFirstStep = FALSE;
        srenew(ef->eo.ener_prev,*nre);
    }
    else
    {
        ef->eo.bOldFileOpen=FALSE;

        if (magic != -55555)
        {
            gmx_fatal(FARGS,"Energy names magic number mismatch, this is not a GROMACS edr file");
        }
        file_version = enx_version;
        xdr_int(xdr,&file_version);
        if (file_version > enx_version)
        {
            gmx_fatal(FARGS,"reading tpx file (%s) version %d with version %d program",gmx_fio_getname(ef->fio),file_version,enx_version);
        }
        xdr_int(xdr,nre);
    }
    if (file_version != enx_version)
    {
        fprintf(stderr,"Note: enx file_version %d, software version %d\n",
                file_version,enx_version);
    }

    edr_strings(xdr,bRead,file_version,*nre,nms);
}
Example #4
0
gmx_bool do_enx(ener_file_t ef,t_enxframe *fr)
{
    int       file_version=-1;
    int       i,b;
    gmx_bool      bRead,bOK,bOK1,bSane;
    real      tmp1,tmp2,rdum;
    char      buf[22];
    /*int       d_size;*/
    
    bOK = TRUE;
    bRead = gmx_fio_getread(ef->fio);
    if (!bRead)
    {  
        fr->e_size = fr->nre*sizeof(fr->ener[0].e)*4;
        /*d_size = fr->ndisre*(sizeof(real)*2);*/
    }
    gmx_fio_checktype(ef->fio);

    if (!do_eheader(ef,&file_version,fr,-1,NULL,&bOK))
    {
        if (bRead)
        {
            fprintf(stderr,"\rLast energy frame read %d time %8.3f         ",
                    ef->framenr-1,ef->frametime);
            if (!bOK)
            {
                fprintf(stderr,
                        "\nWARNING: Incomplete energy frame: nr %d time %8.3f\n",
                        ef->framenr,fr->t);
            }
        }
        else
        {
            gmx_file("Cannot write energy file header; maybe you are out of quota?");
        }
        return FALSE;
    }
    if (bRead)
    {
        if ((ef->framenr <   20 || ef->framenr %   10 == 0) &&
            (ef->framenr <  200 || ef->framenr %  100 == 0) &&
            (ef->framenr < 2000 || ef->framenr % 1000 == 0))
        {
            fprintf(stderr,"\rReading energy frame %6d time %8.3f         ",
                    ef->framenr,fr->t);
        }
        ef->framenr++;
        ef->frametime = fr->t;
    }
    /* Check sanity of this header */
    bSane = fr->nre > 0 ;
    for(b=0; b<fr->nblock; b++)
    {
        bSane = bSane || (fr->block[b].nsub > 0);
    }
    if (!((fr->step >= 0) && bSane))
    {
        fprintf(stderr,"\nWARNING: there may be something wrong with energy file %s\n",
                gmx_fio_getname(ef->fio));
        fprintf(stderr,"Found: step=%s, nre=%d, nblock=%d, time=%g.\n"
                "Trying to skip frame expect a crash though\n",
                gmx_step_str(fr->step,buf),fr->nre,fr->nblock,fr->t);
    }
    if (bRead && fr->nre > fr->e_alloc)
    {
        srenew(fr->ener,fr->nre);
        for(i=fr->e_alloc; (i<fr->nre); i++)
        {
            fr->ener[i].e    = 0;
            fr->ener[i].eav  = 0;
            fr->ener[i].esum = 0;
        }
        fr->e_alloc = fr->nre;
    }
    
    for(i=0; i<fr->nre; i++)
    {
        bOK = bOK && gmx_fio_do_real(ef->fio, fr->ener[i].e);
        
        /* Do not store sums of length 1,
         * since this does not add information.
         */
        if (file_version == 1 ||
            (bRead && fr->nsum > 0) || fr->nsum > 1)
        {
            tmp1 = fr->ener[i].eav;
            bOK = bOK && gmx_fio_do_real(ef->fio, tmp1);
            if (bRead)
                fr->ener[i].eav = tmp1;
            
            /* This is to save only in single precision (unless compiled in DP) */
            tmp2 = fr->ener[i].esum;
            bOK = bOK && gmx_fio_do_real(ef->fio, tmp2);
            if (bRead)
                fr->ener[i].esum = tmp2;
            
            if (file_version == 1)
            {
                /* Old, unused real */
                rdum = 0;
                bOK = bOK && gmx_fio_do_real(ef->fio, rdum);
            }
        }
    }
    
    /* Here we can not check for file_version==1, since one could have
     * continued an old format simulation with a new one with mdrun -append.
     */
    if (bRead && ef->eo.bOldFileOpen)
    {
        /* Convert old full simulation sums to sums between energy frames */
        convert_full_sums(&(ef->eo),fr);
    }
    /* read the blocks */
    for(b=0; b<fr->nblock; b++)
    {
        /* now read the subblocks. */
        int nsub=fr->block[b].nsub; /* shortcut */
        int i;

        for(i=0;i<nsub;i++)
        {
            t_enxsubblock *sub=&(fr->block[b].sub[i]); /* shortcut */

            if (bRead)
            {
                enxsubblock_alloc(sub);
            }

            /* read/write data */
            bOK1=TRUE;
            switch (sub->type)
            {
                case xdr_datatype_float:
                    bOK1=gmx_fio_ndo_float(ef->fio, sub->fval, sub->nr); 
                    break;
                case xdr_datatype_double:
                    bOK1=gmx_fio_ndo_double(ef->fio, sub->dval, sub->nr); 
                    break;
                case xdr_datatype_int:
                    bOK1=gmx_fio_ndo_int(ef->fio, sub->ival, sub->nr);
                    break;
                case xdr_datatype_large_int:
                    bOK1=gmx_fio_ndo_gmx_large_int(ef->fio, sub->lval, sub->nr);
                    break;
                case xdr_datatype_char:
                    bOK1=gmx_fio_ndo_uchar(ef->fio, sub->cval, sub->nr);
                    break;
                case xdr_datatype_string:
                    bOK1=gmx_fio_ndo_string(ef->fio, sub->sval, sub->nr);
                    break;
                default:
                    gmx_incons("Reading unknown block data type: this file is corrupted or from the future");
            }
            bOK = bOK && bOK1;
        }
    }
    
    if(!bRead)
    {
        if( gmx_fio_flush(ef->fio) != 0)
        {
            gmx_file("Cannot write energy file; maybe you are out of quota?");
        }
    }
    
    if (!bOK)
    {
        if (bRead)
        {
            fprintf(stderr,"\nLast energy frame read %d",
                    ef->framenr-1);
            fprintf(stderr,"\nWARNING: Incomplete energy frame: nr %d time %8.3f\n",
                    ef->framenr,fr->t);
        }
        else
        {
            gmx_fatal(FARGS,"could not write energies");
        }
        return FALSE; 
    }
    
    return TRUE;
}
Example #5
0
ener_file_t open_enx(const char *fn,const char *mode)
{
    int        nre,i;
    gmx_enxnm_t *nms=NULL;
    int        file_version=-1;
    t_enxframe *fr;
    gmx_bool       bWrongPrecision,bOK=TRUE;
    struct ener_file *ef;

    snew(ef,1);

    if (mode[0]=='r') {
        ef->fio=gmx_fio_open(fn,mode);
        gmx_fio_checktype(ef->fio);
        gmx_fio_setprecision(ef->fio,FALSE);
        do_enxnms(ef,&nre,&nms);
        snew(fr,1);
        do_eheader(ef,&file_version,fr,nre,&bWrongPrecision,&bOK);
        if(!bOK)
        {
            gmx_file("Cannot read energy file header. Corrupt file?");
        }

        /* Now check whether this file is in single precision */
        if (!bWrongPrecision &&
            ((fr->e_size && (fr->nre == nre) && 
              (nre*4*(long int)sizeof(float) == fr->e_size)) ) )
        {
            fprintf(stderr,"Opened %s as single precision energy file\n",fn);
            free_enxnms(nre,nms);
        }
        else
        {
            gmx_fio_rewind(ef->fio);
            gmx_fio_checktype(ef->fio);
            gmx_fio_setprecision(ef->fio,TRUE);
            do_enxnms(ef,&nre,&nms);
            do_eheader(ef,&file_version,fr,nre,&bWrongPrecision,&bOK);
            if(!bOK)
            {
                gmx_file("Cannot write energy file header; maybe you are out of quota?");
            }

            if (((fr->e_size && (fr->nre == nre) && 
                            (nre*4*(long int)sizeof(double) == fr->e_size)) ))
                fprintf(stderr,"Opened %s as double precision energy file\n",
                        fn);
            else {
                if (empty_file(fn))
                    gmx_fatal(FARGS,"File %s is empty",fn);
                else
                    gmx_fatal(FARGS,"Energy file %s not recognized, maybe different CPU?",
                              fn);
            }
            free_enxnms(nre,nms);
        }
        free_enxframe(fr);
        sfree(fr);
        gmx_fio_rewind(ef->fio);
    }
    else 
        ef->fio = gmx_fio_open(fn,mode);

    ef->framenr=0;
    ef->frametime=0;
    return ef;
}
Example #6
0
void gmx_mtxio_write(const char *             filename,
                     int                      nrow,
                     int                      ncol,
                     real *                   full_matrix,
                     gmx_sparsematrix_t *     sparse_matrix)
{
    t_fileio *fio;
    XDR *   xd;
    int     i,j,prec;
    gmx_bool    bDum = TRUE;
    gmx_bool    bRead = FALSE;
    size_t  sz;
    
    if(full_matrix!=NULL && sparse_matrix!=NULL)
    {
        gmx_fatal(FARGS,"Both full AND sparse matrix specified to gmx_mtxio_write().\n");
    }
    
    fio = gmx_fio_open(filename,"w");
    gmx_fio_checktype(fio);
    xd = gmx_fio_getxdr(fio);
    
    /* Write magic number */
    i = GMX_MTXIO_MAGIC_NUMBER;
    gmx_fio_do_int(fio, i);
    
    /* Write generating Gromacs version */
    gmx_fio_write_string(fio, GromacsVersion());
    
    /* Write 1 for double, 0 for single precision */
    if(sizeof(real)==sizeof(double))
        prec = 1;
    else
        prec = 0;
    gmx_fio_do_int(fio, prec);
    
    gmx_fio_do_int(fio, nrow);
    gmx_fio_do_int(fio, ncol);
    
    if(full_matrix!=NULL)
    {
        /* Full matrix storage format */
        i = GMX_MTXIO_FULL_MATRIX;
        gmx_fio_do_int(fio, i);
        sz = nrow*ncol;        
        bDum=gmx_fio_ndo_real(fio, full_matrix,sz);
    }
    else
    {
        /* Sparse storage */
        i = GMX_MTXIO_SPARSE_MATRIX;
        gmx_fio_do_int(fio, i);

        gmx_fio_do_gmx_bool(fio, sparse_matrix->compressed_symmetric);
        gmx_fio_do_int(fio, sparse_matrix->nrow);
        if(sparse_matrix->nrow != nrow)
        {
            gmx_fatal(FARGS,"Internal inconsistency in sparse matrix.\n");
        }
        bDum=gmx_fio_ndo_int(fio, sparse_matrix->ndata,sparse_matrix->nrow);
        for(i=0;i<sparse_matrix->nrow;i++)
        {
            for(j=0;j<sparse_matrix->ndata[i];j++)
            {
                gmx_fio_do_int(fio, sparse_matrix->data[i][j].col);
                gmx_fio_do_real(fio, sparse_matrix->data[i][j].value);
            }
        }
    }
    gmx_fio_close(fio);
}
Example #7
0
void
gmx_mtxio_read (const char *            filename,
                int *                   nrow,
                int *                   ncol,
                real **                 full_matrix,
                gmx_sparsematrix_t **   sparse_matrix)
{
    t_fileio  *fio;
    XDR *   xd;
    int     i,j,prec;
    gmx_bool    bDum = TRUE;
    gmx_bool    bRead = TRUE;
    char    gmxver[256];
    size_t  sz;
    
    fio = gmx_fio_open(filename,"r");
    gmx_fio_checktype(fio);
    xd = gmx_fio_getxdr(fio);
    
    /* Read and check magic number */
    i = GMX_MTXIO_MAGIC_NUMBER;
    gmx_fio_do_int(fio, i);

    if(i!=GMX_MTXIO_MAGIC_NUMBER)
    {
        gmx_fatal(FARGS,
                  "No matrix data found in file. Note that the Hessian matrix format changed\n"
                  "in Gromacs 3.3 to enable portable files and sparse matrix storage.\n");
    }
    
    /* Read generating Gromacs version */
    gmx_fio_do_string(fio, gmxver);
    
    /* Write 1 for double, 0 for single precision */
    if(sizeof(real)==sizeof(double))
        prec = 1;
    else
        prec = 0;
    gmx_fio_do_int(fio, prec);

    fprintf(stderr,"Reading %s precision matrix generated by Gromacs %s\n",
            (prec == 1) ? "double" : "single",gmxver);
    
    gmx_fio_do_int(fio, i);
    *nrow=i;
    gmx_fio_do_int(fio, i);
    *ncol=i;

    gmx_fio_do_int(fio, i);
    
    if(i==GMX_MTXIO_FULL_MATRIX)
    {
        printf("Full matrix storage format, nrow=%d, ncols=%d\n",*nrow,*ncol);

        sz = (*nrow) * (*ncol);
        snew((*full_matrix),sz);
        bDum=gmx_fio_ndo_real(fio, (*full_matrix),sz);
    }
    else
    {
        /* Sparse storage */
        printf("Sparse matrix storage format, nrow=%d, ncols=%d\n",*nrow,*ncol);

        snew((*sparse_matrix),1);
        gmx_fio_do_gmx_bool(fio, (*sparse_matrix)->compressed_symmetric);
        gmx_fio_do_int(fio, (*sparse_matrix)->nrow);        
        if((*sparse_matrix)->nrow != *nrow)
        {
            gmx_fatal(FARGS,"Internal inconsistency in sparse matrix.\n");
        }
        snew((*sparse_matrix)->ndata,(*sparse_matrix)->nrow);
        snew((*sparse_matrix)->nalloc,(*sparse_matrix)->nrow);
        snew((*sparse_matrix)->data,(*sparse_matrix)->nrow);
        bDum=gmx_fio_ndo_int(fio, (*sparse_matrix)->ndata,
                             (*sparse_matrix)->nrow);

        for(i=0;i<(*sparse_matrix)->nrow;i++)
        {
            (*sparse_matrix)->nalloc[i] = (*sparse_matrix)->ndata[i] + 10;
            snew(((*sparse_matrix)->data[i]),(*sparse_matrix)->nalloc[i]);
            
            for(j=0;j<(*sparse_matrix)->ndata[i];j++)
            {
                gmx_fio_do_int(fio, (*sparse_matrix)->data[i][j].col);
                gmx_fio_do_real(fio, (*sparse_matrix)->data[i][j].value);
            }
        }
    }
    gmx_fio_close(fio);
}