Beispiel #1
0
FILE *
gmx_fio_fopen(const char *fn,const char *mode)
{
    FILE *fp;
    int   fd;

    fd = gmx_fio_open(fn,mode);

    return FIO[fd].fp;
}
Beispiel #2
0
FILE * gmx_fio_fopen(const char *fn, const char *mode)
{
    FILE     *fp, *ret;
    t_fileio *fio;

    fio = gmx_fio_open(fn, mode);
    gmx_fio_lock(fio);
    ret = fio->fp;
    gmx_fio_unlock(fio);

    return ret;
}
Beispiel #3
0
t_trxstatus *open_trx(const char *outfile,const char *filemode)
{
    t_trxstatus *stat;
    if (filemode[0]!='w' && filemode[0]!='a' && filemode[1]!='+')
        gmx_fatal(FARGS,"Sorry, write_trx can only write");

    snew(stat,1);
    status_init(stat);

    stat->fio=gmx_fio_open(outfile,filemode);
    return stat;
}
Beispiel #4
0
bool read_first_frame(const gmx_output_env_t *oenv, t_trxstatus **status,
                      const char *fn, t_trxframe *fr, int flags)
{
    t_fileio      *fio = nullptr;
    gmx_bool       bFirst, bOK;
    int            ftp   = fn2ftp(fn);

    clear_trxframe(fr, TRUE);

    bFirst = TRUE;

    snew((*status), 1);

    status_init( *status );
    initcount(*status);
    (*status)->flags = flags;

    if (efTNG == ftp)
    {
        /* Special treatment for TNG files */
        gmx_tng_open(fn, 'r', &(*status)->tng);
    }
    else
    {
        fio = (*status)->fio = gmx_fio_open(fn, "r");
    }
    switch (ftp)
    {
        case efTRR:
            break;
        case efCPT:
            read_checkpoint_trxframe(fio, fr);
            bFirst = FALSE;
            break;
        case efG96:
        {
            /* Can not rewind a compressed file, so open it twice */
            if (!(*status)->persistent_line)
            {
                /* allocate the persistent line */
                snew((*status)->persistent_line, STRLEN+1);
            }
            t_symtab *symtab = nullptr;
            read_g96_conf(gmx_fio_getfp(fio), fn, nullptr, fr, symtab, (*status)->persistent_line);
            gmx_fio_close(fio);
            clear_trxframe(fr, FALSE);
            if (flags & (TRX_READ_X | TRX_NEED_X))
            {
                snew(fr->x, fr->natoms);
            }
            if (flags & (TRX_READ_V | TRX_NEED_V))
            {
                snew(fr->v, fr->natoms);
            }
            (*status)->fio = gmx_fio_open(fn, "r");
            break;
        }
        case efXTC:
            if (read_first_xtc(fio, &fr->natoms, &fr->step, &fr->time, fr->box, &fr->x,
                               &fr->prec, &bOK) == 0)
            {
                GMX_RELEASE_ASSERT(!bOK, "Inconsistent results - OK status from read_first_xtc, but 0 atom coords read");
                fr->not_ok = DATA_NOT_OK;
            }
            if (fr->not_ok)
            {
                fr->natoms = 0;
                printincomp(*status, fr);
            }
            else
            {
                fr->bPrec = (fr->prec > 0);
                fr->bStep = TRUE;
                fr->bTime = TRUE;
                fr->bX    = TRUE;
                fr->bBox  = TRUE;
                printcount(*status, oenv, fr->time, FALSE);
            }
            bFirst = FALSE;
            break;
        case efTNG:
            fr->step = -1;
            if (!gmx_read_next_tng_frame((*status)->tng, fr, nullptr, 0))
            {
                fr->not_ok = DATA_NOT_OK;
                fr->natoms = 0;
                printincomp(*status, fr);
            }
            else
            {
                printcount(*status, oenv, fr->time, FALSE);
            }
            bFirst = FALSE;
            break;
        case efPDB:
            pdb_first_x(*status, gmx_fio_getfp(fio), fr);
            if (fr->natoms)
            {
                printcount(*status, oenv, fr->time, FALSE);
            }
            bFirst = FALSE;
            break;
        case efGRO:
            if (gro_first_x_or_v(gmx_fio_getfp(fio), fr))
            {
                printcount(*status, oenv, fr->time, FALSE);
            }
            bFirst = FALSE;
            break;
        default:
#if GMX_USE_PLUGINS
            fprintf(stderr, "The file format of %s is not a known trajectory format to GROMACS.\n"
                    "Please make sure that the file is a trajectory!\n"
                    "GROMACS will now assume it to be a trajectory and will try to open it using the VMD plug-ins.\n"
                    "This will only work in case the VMD plugins are found and it is a trajectory format supported by VMD.\n", fn);
            gmx_fio_fp_close(fio); /*only close the file without removing FIO entry*/
            if (!read_first_vmd_frame(fn, &(*status)->vmdplugin, fr))
            {
                gmx_fatal(FARGS, "Not supported in read_first_frame: %s", fn);
            }
#else
            gmx_fatal(FARGS, "Not supported in read_first_frame: %s. Please make sure that the file is a trajectory.\n"
                      "GROMACS is not compiled with plug-in support. Thus it cannot read non-GROMACS trajectory formats using the VMD plug-ins.\n"
                      "Please compile with plug-in support if you want to read non-GROMACS trajectory formats.\n", fn);
#endif
            break;
    }
    (*status)->tf = fr->time;

    /* Return FALSE if we read a frame that's past the set ending time. */
    if (!bFirst && (!(flags & TRX_DONT_SKIP) && check_times(fr->time) > 0))
    {
        (*status)->t0 = fr->time;
        return FALSE;
    }

    if (bFirst ||
        (!(flags & TRX_DONT_SKIP) && check_times(fr->time) < 0))
    {
        /* Read a frame when no frame was read or the first was skipped */
        if (!read_next_frame(oenv, *status, fr))
        {
            return FALSE;
        }
    }
    (*status)->t0 = fr->time;

    /* We need the number of atoms for random-access XTC searching, even when
     * we don't have access to the actual frame data.
     */
    (*status)->natoms = fr->natoms;

    return (fr->natoms > 0);
}
Beispiel #5
0
int open_enx(char *fn,char *mode)
{
  int        fp,nre,i;
  char       **nm=NULL;
  t_enxframe *fr;
  bool       bDum=TRUE;
  
  /* Energy files should always be opened as binary files,
   * but that is checked in gmx_fio_open.
   */

  if (mode[0]=='r') {
    fp=gmx_fio_open(fn,mode);
    gmx_fio_select(fp);
    gmx_fio_setprecision(fp,FALSE);
    do_enxnms(fp,&nre,&nm);
    snew(fr,1);
    do_eheader(fp,fr,&bDum);
    
    /* Now check whether this file is in single precision */
    if (((fr->e_size && (fr->nre == nre) && 
	  (nre*4*sizeof(float) == fr->e_size)) ||
	 (fr->d_size && 
	  (fr->ndisre*sizeof(float)*2+sizeof(int) == fr->d_size)))){
      fprintf(stderr,"Opened %s as single precision energy file\n",fn);
      for(i=0; (i<nre); i++)
	sfree(nm[i]);
      sfree(nm);
    }
    else {
      gmx_fio_rewind(fp);
      gmx_fio_select(fp);
      gmx_fio_setprecision(fp,TRUE);
      do_enxnms(fp,&nre,&nm);
      do_eheader(fp,fr,&bDum);
      if (((fr->e_size && (fr->nre == nre) && 
	    (nre*4*sizeof(double) == fr->e_size)) ||
	   (fr->d_size && 
	    (fr->ndisre*sizeof(double)*2+sizeof(int) == fr->d_size))))
	fprintf(stderr,"Opened %s as double precision energy file\n",fn);
      else {
	if (empty_file(fn))
	  fatal_error(0,"File %s is empty",fn);
	else
	  fatal_error(0,"Energy file %s not recognized, maybe different CPU?",
		      fn);
      }
      for(i=0; (i<nre); i++)
	  sfree(nm[i]);
      sfree(nm);
    }
    free_enxframe(fr);
    sfree(fr);
    gmx_fio_rewind(fp);
  }
  else 
    fp = gmx_fio_open(fn,mode);
    
  framenr=0;
    
  return fp;
}
Beispiel #6
0
t_fileio *open_xtc(const char *fn, const char *mode)
{
    return gmx_fio_open(fn, mode);
}
Beispiel #7
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;
}
Beispiel #8
0
void
gmx_mtxio_write(char *                   filename,
                int                      nrow,
                int                      ncol,
                real *                   full_matrix,
                gmx_sparsematrix_t *     sparse_matrix)
{
    int     fd;
    XDR *   xd;
    int     i,j,prec;
    bool    bDum = TRUE;
    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");
    }

    fd = gmx_fio_open(filename,"w");
    gmx_fio_select(fd);
    xd = gmx_fio_getxdr(fd);

    /* Write magic number */
    i = GMX_MTXIO_MAGIC_NUMBER;
    do_int(i);

    /* Write generating Gromacs version */
    do_string(GromacsVersion());

    /* Write 1 for double, 0 for single precision */
    if(sizeof(real)==sizeof(double))
        prec = 1;
    else
        prec = 0;
    do_int(prec);

    do_int(nrow);
    do_int(ncol);

    if(full_matrix!=NULL)
    {
        /* Full matrix storage format */
        i = GMX_MTXIO_FULL_MATRIX;
        do_int(i);
        sz = nrow*ncol;
        ndo_real(full_matrix,sz,bDum);
    }
    else
    {
        /* Sparse storage */
        i = GMX_MTXIO_SPARSE_MATRIX;
        do_int(i);

        do_int(sparse_matrix->compressed_symmetric);
        do_int(sparse_matrix->nrow);
        if(sparse_matrix->nrow != nrow)
        {
            gmx_fatal(FARGS,"Internal inconsistency in sparse matrix.\n");
        }
        ndo_int(sparse_matrix->ndata,sparse_matrix->nrow,bDum);
        for(i=0; i<sparse_matrix->nrow; i++)
        {
            for(j=0; j<sparse_matrix->ndata[i]; j++)
            {
                do_int(sparse_matrix->data[i][j].col);
                do_real(sparse_matrix->data[i][j].value);
            }
        }
    }
    gmx_fio_close(fd);
}
Beispiel #9
0
void
gmx_mtxio_read (char *                  filename,
                int *                   nrow,
                int *                   ncol,
                real **                 full_matrix,
                gmx_sparsematrix_t **   sparse_matrix)
{
    int     fd;
    XDR *   xd;
    int     i,j,prec;
    bool    bDum = TRUE;
    bool    bRead = TRUE;
    char    gmxver[256];
    size_t  sz;

    fd = gmx_fio_open(filename,"r");
    gmx_fio_select(fd);
    xd = gmx_fio_getxdr(fd);

    /* Read and check magic number */
    i = GMX_MTXIO_MAGIC_NUMBER;
    do_int(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 */
    do_string(gmxver);

    /* Write 1 for double, 0 for single precision */
    if(sizeof(real)==sizeof(double))
        prec = 1;
    else
        prec = 0;
    do_int(prec);

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

    do_int(i);
    *nrow=i;
    do_int(i);
    *ncol=i;

    do_int(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);
        ndo_real((*full_matrix),sz,bDum);
    }
    else
    {
        /* Sparse storage */
        printf("Sparse matrix storage format, nrow=%d, ncols=%d\n",*nrow,*ncol);

        snew((*sparse_matrix),1);
        do_int((*sparse_matrix)->compressed_symmetric);
        do_int((*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);
        ndo_int((*sparse_matrix)->ndata,(*sparse_matrix)->nrow,bDum);

        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++)
            {
                do_int((*sparse_matrix)->data[i][j].col);
                do_real((*sparse_matrix)->data[i][j].value);
            }
        }
    }
    gmx_fio_close(fd);
}
Beispiel #10
0
int read_first_frame(const output_env_t oenv, t_trxstatus **status,
                     const char *fn, t_trxframe *fr, int flags)
{
    t_fileio *fio;
    gmx_bool  bFirst, bOK;
    int       dummy = 0;

    clear_trxframe(fr, TRUE);
    fr->flags = flags;

    bFirst = TRUE;

    snew((*status), 1);

    status_init( *status );
    (*status)->nxframe = 1;
    initcount(*status);

    fio = (*status)->fio = gmx_fio_open(fn, "r");
    switch (gmx_fio_getftp(fio))
    {
    case efTRJ:
    case efTRR:
        break;
    case efCPT:
        read_checkpoint_trxframe(fio, fr);
        bFirst = FALSE;
        break;
    case efG96:
        /* Can not rewind a compressed file, so open it twice */
        if (!(*status)->persistent_line)
        {
            /* allocate the persistent line */
            snew((*status)->persistent_line, STRLEN+1);
        }
        read_g96_conf(gmx_fio_getfp(fio), fn, fr, (*status)->persistent_line);
        gmx_fio_close(fio);
        clear_trxframe(fr, FALSE);
        if (flags & (TRX_READ_X | TRX_NEED_X))
        {
            snew(fr->x, fr->natoms);
        }
        if (flags & (TRX_READ_V | TRX_NEED_V))
        {
            snew(fr->v, fr->natoms);
        }
        fio = (*status)->fio = gmx_fio_open(fn, "r");
        break;
    case efG87:
        fr->natoms = xyz_first_x(*status, gmx_fio_getfp(fio), oenv, &fr->time,
                                 &fr->x, fr->box);
        if (fr->natoms)
        {
            fr->bTime = TRUE;
            fr->bX    = TRUE;
            fr->bBox  = TRUE;
            printcount(*status, oenv, fr->time, FALSE);
        }
        bFirst = FALSE;
        break;
    case efXTC:
        if (read_first_xtc(fio, &fr->natoms, &fr->step, &fr->time, fr->box, &fr->x,
                           &fr->prec, &bOK) == 0)
        {
            if (bOK)
            {
                gmx_fatal(FARGS, "No XTC!\n");
            }
            else
            {
                fr->not_ok = DATA_NOT_OK;
            }
        }
        if (fr->not_ok)
        {
            fr->natoms = 0;
            printincomp(*status, fr);
        }
        else
        {
            fr->bPrec = (fr->prec > 0);
            fr->bStep = TRUE;
            fr->bTime = TRUE;
            fr->bX    = TRUE;
            fr->bBox  = TRUE;
            printcount(*status, oenv, fr->time, FALSE);
        }
        bFirst = FALSE;
        break;
    case efPDB:
        pdb_first_x(*status, gmx_fio_getfp(fio), fr);
        if (fr->natoms)
        {
            printcount(*status, oenv, fr->time, FALSE);
        }
        bFirst = FALSE;
        break;
    case efGRO:
        if (gro_first_x_or_v(gmx_fio_getfp(fio), fr))
        {
            printcount(*status, oenv, fr->time, FALSE);
        }
        bFirst = FALSE;
        break;
    default:
#ifdef GMX_USE_PLUGINS
        fprintf(stderr, "The file format of %s is not a known trajectory format to GROMACS.\n"
                "Please make sure that the file is a trajectory!\n"
                "GROMACS will now assume it to be a trajectory and will try to open it using the VMD plug-ins.\n"
                "This will only work in case the VMD plugins are found and it is a trajectory format supported by VMD.\n", fn);
        gmx_fio_fp_close(fio); /*only close the file without removing FIO entry*/
        if (!read_first_vmd_frame(fn, fr))
        {
            gmx_fatal(FARGS, "Not supported in read_first_frame: %s", fn);
        }
#else
        gmx_fatal(FARGS, "Not supported in read_first_frame: %s. Please make sure that the file is a trajectory.\n"
                  "GROMACS is not compiled with plug-in support. Thus it cannot read non-GROMACS trajectory formats using the VMD plug-ins.\n"
                  "Please compile with plug-in support if you want to read non-GROMACS trajectory formats.\n", fn);
#endif
        break;
    }

    /* Return FALSE if we read a frame that's past the set ending time. */
    if (!bFirst && (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) > 0))
    {
        fr->t0 = fr->time;
        return FALSE;
    }

    if (bFirst ||
            (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) < 0))
    {
        /* Read a frame when no frame was read or the first was skipped */
        if (!read_next_frame(oenv, *status, fr))
        {
            return FALSE;
        }
    }
    fr->t0 = fr->time;

    return (fr->natoms > 0);
}
Beispiel #11
0
/*! \brief Support handling restarts
 *
 * \todo Clean this up (next patch)
 *
 * Read just the simulation 'generation' and with bTryToAppendFiles check files.
 * This is is needed at the beginning of mdrun,
 * to be able to rename the logfile correctly.
 * When file appending is requested, checks which output files are present,
 * and returns TRUE/FALSE in bDoAppendFiles if all or none are present.
 * If only some output files are present, give a fatal error.
 * When bDoAppendFiles is TRUE upon return, bAddPart will tell whether the simulation part
 * needs to be added to the output file name.
 *
 * This routine cannot print tons of data, since it is called before
 * the log file is opened. */
static void
read_checkpoint_data(const char *filename, int *simulation_part,
                     t_commrec *cr,
                     gmx_bool bTryToAppendFiles,
                     int nfile, const t_filenm fnm[],
                     const char *part_suffix,
                     gmx_bool *bAddPart,
                     gmx_bool *bDoAppendFiles)
{
    t_fileio            *fp;
    int                  nfiles;
    gmx_file_position_t *outputfiles;
    int                  nexist, f;
    char                *fn, suf_up[STRLEN];

    *bDoAppendFiles = FALSE;

    if (SIMMASTER(cr))
    {
        if (!gmx_fexist(filename) || (!(fp = gmx_fio_open(filename, "r")) ))
        {
            *simulation_part = 0;
        }
        else
        {
            read_checkpoint_simulation_part_and_filenames(fp,
                                                          simulation_part,
                                                          &nfiles,
                                                          &outputfiles);

            if (bTryToAppendFiles)
            {
                nexist = 0;
                for (f = 0; f < nfiles; f++)
                {
                    if (exist_output_file(outputfiles[f].filename, nfile, fnm))
                    {
                        nexist++;
                    }
                }
                if (nexist == nfiles)
                {
                    *bDoAppendFiles = bTryToAppendFiles;
                }
                else if (nexist > 0)
                {
                    fprintf(stderr,
                            "Output file appending has been requested,\n"
                            "but some output files listed in the checkpoint file %s\n"
                            "are not present or are named differently by the current program:\n",
                            filename);
                    fprintf(stderr, "output files present:");
                    for (f = 0; f < nfiles; f++)
                    {
                        if (exist_output_file(outputfiles[f].filename,
                                              nfile, fnm))
                        {
                            fprintf(stderr, " %s", outputfiles[f].filename);
                        }
                    }
                    fprintf(stderr, "\n");
                    fprintf(stderr, "output files not present or named differently:");
                    for (f = 0; f < nfiles; f++)
                    {
                        if (!exist_output_file(outputfiles[f].filename,
                                               nfile, fnm))
                        {
                            fprintf(stderr, " %s", outputfiles[f].filename);
                        }
                    }
                    fprintf(stderr, "\n");

                    gmx_fatal(FARGS, "File appending requested, but %d of the %d output files are not present or are named differently", nfiles-nexist, nfiles);
                }
            }

            if (*bDoAppendFiles)
            {
                if (nfiles == 0)
                {
                    gmx_fatal(FARGS, "File appending requested, but no output file information is stored in the checkpoint file");
                }
                fn = outputfiles[0].filename;
                if (strlen(fn) < 4 ||
                    gmx_strcasecmp(fn+strlen(fn)-4, ftp2ext(efLOG)) == 0)
                {
                    gmx_fatal(FARGS, "File appending requested, but the log file is not the first file listed in the checkpoint file");
                }
                /* Set bAddPart to whether the suffix string '.part' is present
                 * in the log file name.
                 */
                strcpy(suf_up, part_suffix);
                upstring(suf_up);
                *bAddPart = (strstr(fn, part_suffix) != NULL ||
                             strstr(fn, suf_up) != NULL);
            }

            sfree(outputfiles);
        }
    }
    if (PAR(cr))
    {
        gmx_bcast(sizeof(*simulation_part), simulation_part, cr);

        if (*simulation_part > 0 && bTryToAppendFiles)
        {
            gmx_bcast(sizeof(*bDoAppendFiles), bDoAppendFiles, cr);
            gmx_bcast(sizeof(*bAddPart), bAddPart, cr);
        }
    }
}
Beispiel #12
0
t_fileio *gmx_trr_open(const char *fn, const char *mode)
{
    return gmx_fio_open(fn, mode);
}
Beispiel #13
0
void gmx_mtxio_write(const char *             filename,
                     int                      nrow,
                     int                      ncol,
                     real *                   full_matrix,
                     gmx_sparsematrix_t *     sparse_matrix)
{
    t_fileio   *fio;
    int         i, j, prec;
    size_t      sz;

    if (full_matrix != nullptr && sparse_matrix != nullptr)
    {
        gmx_fatal(FARGS, "Both full AND sparse matrix specified to gmx_mtxio_write().\n");
    }

    fio = gmx_fio_open(filename, "w");

    /* Write magic number */
    i = GMX_MTXIO_MAGIC_NUMBER;
    gmx_fio_do_int(fio, i);

    /* Write generating Gromacs version */
    gmx_fio_write_string(fio, gmx_version());

    /* 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 != nullptr)
    {
        /* Full matrix storage format */
        i = GMX_MTXIO_FULL_MATRIX;
        gmx_fio_do_int(fio, i);
        sz   = nrow*ncol;
        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");
        }
        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);
}
Beispiel #14
0
int open_enx(const char *fn,const char *mode)
{
  int        fp,nre,i;
  gmx_enxnm_t *nms=NULL;
  int        file_version=-1;
  t_enxframe *fr;
  bool       bDum=TRUE;

  if (mode[0]=='r') {
    fp=gmx_fio_open(fn,mode);
    gmx_fio_select(fp);
    gmx_fio_setprecision(fp,FALSE);
    do_enxnms(fp,&nre,&nms);
    snew(fr,1);
    do_eheader(fp,&file_version,fr,TRUE,&bDum);
	if(!bDum)
	{
		gmx_file("Cannot read energy file header. Corrupt file?");
	}
	  
    /* Now check whether this file is in single precision */
    if (((fr->e_size && (fr->nre == nre) && 
	  (nre*4*sizeof(float) == fr->e_size)) ||
	 (fr->d_size && 
	  (fr->ndisre*sizeof(float)*2+sizeof(int) == fr->d_size)))){
      fprintf(stderr,"Opened %s as single precision energy file\n",fn);
      free_enxnms(nre,nms);
    }
    else {
      gmx_fio_rewind(fp);
      gmx_fio_select(fp);
      gmx_fio_setprecision(fp,TRUE);
      do_enxnms(fp,&nre,&nms);
      do_eheader(fp,&file_version,fr,TRUE,&bDum);
  	  if(!bDum)
	  {
		  gmx_file("Cannot write energy file header; maybe you are out of quota?");
	  }
		
      if (((fr->e_size && (fr->nre == nre) && 
	    (nre*4*sizeof(double) == fr->e_size)) ||
	   (fr->d_size && 
	    (fr->ndisre*sizeof(double)*2+sizeof(int) == fr->d_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(fp);
  }
  else 
    fp = gmx_fio_open(fn,mode);
    
  framenr=0;
  frametime=0;

  return fp;
}
Beispiel #15
0
int read_first_frame(const output_env_t oenv,t_trxstatus **status,
                     const char *fn,t_trxframe *fr,int flags)
{
  t_fileio *fio;
  gmx_bool bFirst,bOK;
  int dummy=0;

  clear_trxframe(fr,TRUE);
  fr->flags = flags;

  bFirst = TRUE;

  snew((*status), 1);

  status_init( *status );
  (*status)->nxframe=1;
  initcount(*status);
  
  fio = (*status)->fio =gmx_fio_open(fn,"r");
  switch (gmx_fio_getftp(fio)) 
  {
  case efTRJ:
  case efTRR:
    break;
  case efCPT:
    read_checkpoint_trxframe(fio,fr);
    bFirst = FALSE;
    break;
  case efG96:
    /* Can not rewind a compressed file, so open it twice */
    read_g96_conf(gmx_fio_getfp(fio),fn,fr);
    gmx_fio_close(fio);
    clear_trxframe(fr,FALSE);
    if (flags & (TRX_READ_X | TRX_NEED_X))
      snew(fr->x,fr->natoms);
    if (flags & (TRX_READ_V | TRX_NEED_V))
      snew(fr->v,fr->natoms);
    fio = (*status)->fio =gmx_fio_open(fn,"r");
    break;
  case efG87:
    fr->natoms=xyz_first_x(*status, gmx_fio_getfp(fio),oenv,&fr->time,
                           &fr->x,fr->box);
    if (fr->natoms) {
      fr->bTime = TRUE;
      fr->bX    = TRUE;
      fr->bBox  = TRUE;
      printcount(*status,oenv,fr->time,FALSE);
    }
    bFirst = FALSE;
    break;
  case efXTC:
    if (read_first_xtc(fio,&fr->natoms,&fr->step,&fr->time,fr->box,&fr->x,
		       &fr->prec,&bOK) == 0) {
      if (bOK) {
	gmx_fatal(FARGS,"No XTC!\n");
      } else {
	fr->not_ok = DATA_NOT_OK;
      }
    }
    if (fr->not_ok) {
      fr->natoms = 0;
      printincomp(*status,fr);
    } else {
      fr->bPrec = (fr->prec > 0);
      fr->bStep = TRUE;
      fr->bTime = TRUE;
      fr->bX    = TRUE;
      fr->bBox  = TRUE;
      printcount(*status,oenv,fr->time,FALSE);
    }
    bFirst = FALSE;
    break;
  case efPDB:
    pdb_first_x(*status, gmx_fio_getfp(fio),fr);
    if (fr->natoms)
      printcount(*status,oenv,fr->time,FALSE);
    bFirst = FALSE;
    break;
  case efGRO:
    if (gro_first_x_or_v(gmx_fio_getfp(fio),fr))
      printcount(*status,oenv,fr->time,FALSE);
    bFirst = FALSE;
    break;
  default:
#ifdef GMX_DLOPEN
      gmx_fio_fp_close(fio); /*only close the file without removing FIO entry*/
      if (!read_first_vmd_frame(&dummy,fn,fr,flags))
      {
	  gmx_fatal(FARGS,"Not supported in read_first_frame: %s",fn);
      }
#else
      gmx_fatal(FARGS,"Not supported in read_first_frame: %s",fn);
#endif
      break;
  }

  /* Return FALSE if we read a frame that's past the set ending time. */
  if (!bFirst && (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) > 0)) {
    fr->t0 = fr->time;
    return FALSE;
  }
  
  if (bFirst || 
      (!(fr->flags & TRX_DONT_SKIP) && check_times(fr->time) < 0))
    /* Read a frame when no frame was read or the first was skipped */
    if (!read_next_frame(oenv,*status,fr))
      return FALSE;
  fr->t0 = fr->time;
  
  return (fr->natoms > 0);
}
Beispiel #16
0
int open_trn(char *fn,char *mode)
{
  return gmx_fio_open(fn,mode);
}