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; }
static gmx_bool do_eheader(ener_file_t ef,int *file_version,t_enxframe *fr, int nre_test,gmx_bool *bWrongPrecision,gmx_bool *bOK) { int magic=-7777777; real first_real_to_check; int b,i,zero=0,dum=0; gmx_bool bRead = gmx_fio_getread(ef->fio); int tempfix_nr=0; int ndisre=0; int startb=0; #ifndef GMX_DOUBLE xdr_datatype dtreal=xdr_datatype_float; #else xdr_datatype dtreal=xdr_datatype_double; #endif if (nre_test >= 0) { *bWrongPrecision = FALSE; } *bOK=TRUE; /* The original energy frame started with a real, * so we have to use a real for compatibility. * This is VERY DIRTY code, since do_eheader can be called * with the wrong precision set and then we could read r > -1e10, * while actually the intention was r < -1e10. * When nre_test >= 0, do_eheader should therefore terminate * before the number of i/o calls starts depending on what has been read * (which is the case for for instance the block sizes for variable * number of blocks, where this number is read before). */ first_real_to_check = -2e10; if (!gmx_fio_do_real(ef->fio, first_real_to_check)) { return FALSE; } if (first_real_to_check > -1e10) { /* Assume we are reading an old format */ *file_version = 1; fr->t = first_real_to_check; if (!gmx_fio_do_int(ef->fio, dum)) *bOK = FALSE; fr->step = dum; } else { if (!gmx_fio_do_int(ef->fio, magic)) *bOK = FALSE; if (magic != -7777777) { enx_warning("Energy header magic number mismatch, this is not a GROMACS edr file"); *bOK=FALSE; return FALSE; } *file_version = enx_version; if (!gmx_fio_do_int(ef->fio, *file_version)) *bOK = FALSE; if (*bOK && *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); } if (!gmx_fio_do_double(ef->fio, fr->t)) *bOK = FALSE; if (!gmx_fio_do_gmx_large_int(ef->fio, fr->step)) *bOK = FALSE; if (!bRead && fr->nsum == 1) { /* Do not store sums of length 1, * since this does not add information. */ if (!gmx_fio_do_int(ef->fio, zero)) *bOK = FALSE; } else { if (!gmx_fio_do_int(ef->fio, fr->nsum)) *bOK = FALSE; } if (*file_version >= 3) { if (!gmx_fio_do_gmx_large_int(ef->fio, fr->nsteps)) *bOK = FALSE; } else { fr->nsteps = max(1,fr->nsum); } if (*file_version >= 5) { if (!gmx_fio_do_double(ef->fio, fr->dt)) *bOK = FALSE; } else { fr->dt = 0; } } if (!gmx_fio_do_int(ef->fio, fr->nre)) *bOK = FALSE; if (*file_version < 4) { if (!gmx_fio_do_int(ef->fio, ndisre)) *bOK = FALSE; } else { /* now reserved for possible future use */ if (!gmx_fio_do_int(ef->fio, dum)) *bOK = FALSE; } if (!gmx_fio_do_int(ef->fio, fr->nblock)) *bOK = FALSE; if (fr->nblock < 0) *bOK=FALSE; if (ndisre!=0) { if (*file_version >= 4) { enx_warning("Distance restraint blocks in old style in new style file"); *bOK=FALSE; return FALSE; } fr->nblock+=1; } /* Frames could have nre=0, so we can not rely only on the fr->nre check */ if (bRead && nre_test >= 0 && ((fr->nre > 0 && fr->nre != nre_test) || fr->nre < 0 || ndisre < 0 || fr->nblock < 0)) { *bWrongPrecision = TRUE; return *bOK; } /* we now know what these should be, or we've already bailed out because of wrong precision */ if ( *file_version==1 && (fr->t < 0 || fr->t > 1e20 || fr->step < 0 ) ) { enx_warning("edr file with negative step number or unreasonable time (and without version number)."); *bOK=FALSE; return FALSE; } if (*bOK && bRead) { add_blocks_enxframe(fr, fr->nblock); } startb=0; if (ndisre>0) { /* sub[0] is the instantaneous data, sub[1] is time averaged */ add_subblocks_enxblock(&(fr->block[0]), 2); fr->block[0].id=enxDISRE; fr->block[0].sub[0].nr=ndisre; fr->block[0].sub[1].nr=ndisre; fr->block[0].sub[0].type=dtreal; fr->block[0].sub[1].type=dtreal; startb++; } /* read block header info */ for(b=startb; b<fr->nblock; b++) { if (*file_version<4) { /* blocks in old version files always have 1 subblock that consists of reals. */ int nrint; if (bRead) { add_subblocks_enxblock(&(fr->block[b]), 1); } else { if (fr->block[b].nsub != 1) { gmx_incons("Writing an old version .edr file with too many subblocks"); } if (fr->block[b].sub[0].type != dtreal) { gmx_incons("Writing an old version .edr file the wrong subblock type"); } } nrint = fr->block[b].sub[0].nr; if (!gmx_fio_do_int(ef->fio, nrint)) { *bOK = FALSE; } fr->block[b].id = b - startb; fr->block[b].sub[0].nr = nrint; fr->block[b].sub[0].type = dtreal; } else { int i; /* in the new version files, the block header only contains the ID and the number of subblocks */ int nsub=fr->block[b].nsub; *bOK = *bOK && gmx_fio_do_int(ef->fio, fr->block[b].id); *bOK = *bOK && gmx_fio_do_int(ef->fio, nsub); fr->block[b].nsub=nsub; if (bRead) add_subblocks_enxblock(&(fr->block[b]), nsub); /* read/write type & size for each subblock */ for(i=0;i<nsub;i++) { t_enxsubblock *sub=&(fr->block[b].sub[i]); /* shortcut */ int typenr=sub->type; *bOK=*bOK && gmx_fio_do_int(ef->fio, typenr); *bOK=*bOK && gmx_fio_do_int(ef->fio, sub->nr); sub->type = (xdr_datatype)typenr; } } } if (!gmx_fio_do_int(ef->fio, fr->e_size)) *bOK = FALSE; /* now reserved for possible future use */ if (!gmx_fio_do_int(ef->fio, dum)) *bOK = FALSE; /* Do a dummy int to keep the format compatible with the old code */ if (!gmx_fio_do_int(ef->fio, dum)) *bOK = FALSE; if (*bOK && *file_version == 1 && nre_test < 0) { #if 0 if (fp >= ener_old_nalloc) { gmx_incons("Problem with reading old format energy files"); } #endif if (!ef->eo.bReadFirstStep) { ef->eo.bReadFirstStep = TRUE; ef->eo.first_step = fr->step; ef->eo.step_prev = fr->step; ef->eo.nsum_prev = 0; } fr->nsum = fr->step - ef->eo.first_step + 1; fr->nsteps = fr->step - ef->eo.step_prev; fr->dt = 0; } return *bOK; }
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); }
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); }
static gmx_bool do_trr_frame_header(t_fileio *fio, bool bRead, gmx_trr_header_t *sh, gmx_bool *bOK) { int magic = GROMACS_MAGIC; static gmx_bool bFirst = TRUE; char buf[256]; *bOK = TRUE; 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, "trr 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; }