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; }
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; }
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); }