Ejemplo n.º 1
0
void gmx_fio_rewind(int fio)
{
    gmx_fio_check(fio);
    if (FIO[fio].xdr) {
        xdrclose(FIO[fio].xdr);
        /* File is always opened as binary by xdropen */
        xdropen(FIO[fio].xdr,FIO[fio].fn,FIO[fio].bRead ? "r" : "w");
    }
    else
        frewind(FIO[fio].fp);
}
Ejemplo n.º 2
0
bool XtcFileFormat::read(std::istream &input, chemkit::TrajectoryFile *file)
{
    // read data into temporary file
    QTemporaryFile dataFile;
    dataFile.open();

    unsigned int dataSize = 0;
    for(;;){
        char c = input.get();
        if(input.eof()){
            break;
        }

        dataFile.write(&c, 1);
        dataSize++;
    }

    dataFile.close();

    XDR xdrs;
    xdropen(&xdrs, dataFile.fileName().toAscii().constData(), "r");

    chemkit::Trajectory *trajectory = new chemkit::Trajectory;

    while(xdr_getpos(&xdrs) < dataSize){
        // read magic (should be '1995')
        int magic = 0;
        xdr_int(&xdrs, &magic);
        if(magic != 1995){
            break;
        }

        // create new frame
        chemkit::TrajectoryFrame *frame = trajectory->addFrame();

        // read atom count
        int atomCount = 0;
        xdr_int(&xdrs, &atomCount);

        // read frame number
        int frameNumber = 0;
        xdr_int(&xdrs, &frameNumber);

        // read time
        float time = 0;
        xdr_float(&xdrs, &time);

        // read unit cell
        float box[3][3];
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                xdr_float(&xdrs, &box[i][j]);
            }
        }

        chemkit::Vector3 x(box[0][0], box[0][1], box[0][2]);
        chemkit::Vector3 y(box[1][0], box[1][1], box[1][2]);
        chemkit::Vector3 z(box[2][0], box[2][1], box[2][2]);

        frame->setUnitCell(new chemkit::UnitCell(x * 10, y * 10, z * 10));

        // read coordinates
        float coordinateData[3 * atomCount];
        float precision = 1000.0f;
        xdr3dfcoord(&xdrs, coordinateData, &atomCount, &precision);

        chemkit::Coordinates coordinates(atomCount);

        for(int i = 0; i < atomCount; i++){
            // multiply each coordinate by 10 to convert
            // from nanometers to angstroms
            chemkit::Point3 position(coordinateData[i*3+0] * 10,
                                     coordinateData[i*3+1] * 10,
                                     coordinateData[i*3+2] * 10);

            coordinates.setPosition(i, position);
        }

        frame->setCoordinates(&coordinates);
    }

    xdrclose(&xdrs);

    if(trajectory->isEmpty()){
        delete trajectory;
        return false;
    }

    file->setTrajectory(trajectory);

    return true;
}
Ejemplo n.º 3
0
/*****************************************************************
 *
 *                     EXPORTED SECTION
 *
 *****************************************************************/
int gmx_fio_open(const char *fn,const char *mode)
{
    t_fileio *fio=NULL;
    int      i,nfio=0;
    char     *bf,newmode[5];
    bool     bRead;
    int      xdrid;

    if (fn2ftp(fn)==efTPA)
    {
        strcpy(newmode,mode);
    }
    else
    {
        if (mode[0]=='r')
        {
            strcpy(newmode,"r");
        }
        else if (mode[0]=='w')
        {
            strcpy(newmode,"w");
        }
        else if (mode[0]=='a')
        {
            strcpy(newmode,"a");
        }
        else
        {
            gmx_fatal(FARGS,"DEATH HORROR in gmx_fio_open, mode is '%s'",mode);
        }
    }

    /* Check if it should be opened as a binary file */
    if (strncmp(ftp2ftype(fn2ftp(fn)),"ASCII",5))
    {
        /* Not ascii, add b to file mode */
        if ((strchr(newmode,'b')==NULL) && (strchr(newmode,'B')==NULL))
        {
            strcat(newmode,"b");
        }
    }

    /* Determine whether we have to make a new one */
    for(i=0; (i<nFIO); i++)
    {
        if (!FIO[i].bOpen)
        {
            fio  = &(FIO[i]);
            nfio = i;
            break;
        }
    }

    if (i == nFIO)
    {
        nFIO++;
        srenew(FIO,nFIO);
        fio  = &(FIO[nFIO-1]);
        nfio = nFIO-1;
    }
    bRead = (newmode[0]=='r');
    fio->fp  = NULL;
    fio->xdr = NULL;
    if (fn)
    {
        fio->iFTP   = fn2ftp(fn);
        fio->fn     = strdup(fn);
        fio->bStdio = FALSE;

        /* If this file type is in the list of XDR files, open it like that */
        if (in_ftpset(fio->iFTP,asize(ftpXDR),ftpXDR))
        {
            /* First check whether we have to make a backup,
             * only for writing, not for read or append.
             */
            if (newmode[0]=='w')
            {
#ifndef GMX_FAHCORE
                /* only make backups for normal gromacs */
                if (gmx_fexist(fn))
                {
                    bf=(char *)backup_fn(fn);
                    if (rename(fn,bf) == 0)
                    {
                        fprintf(stderr,"\nBack Off! I just backed up %s to %s\n",fn,bf);
                    }
                    else
                    {
                        fprintf(stderr,"Sorry, I couldn't backup %s to %s\n",fn,bf);
                    }
                }
#endif
            }
            else
            {
                /* Check whether file exists */
                if (!gmx_fexist(fn))
                {
                    gmx_open(fn);
                }
            }
            snew(fio->xdr,1);
            xdrid = xdropen(fio->xdr,fn,newmode);
            if (xdrid == 0)
            {
                if(newmode[0]=='r')
                    gmx_fatal(FARGS,"Cannot open file %s for reading\nCheck permissions if it exists.",fn);
                else
                    gmx_fatal(FARGS,"Cannot open file %s for writing.\nCheck your permissions, disk space and/or quota.",fn);
            }
            fio->fp = xdr_get_fp(xdrid);
        }
        else
        {
            /* If it is not, open it as a regular file */
            fio->fp = ffopen(fn,newmode);
        }
    }
    else
    {
        /* Use stdin/stdout for I/O */
        fio->iFTP   = efTPA;
        fio->fp     = bRead ? stdin : stdout;
        fio->fn     = strdup("STDIO");
        fio->bStdio = TRUE;
    }
    fio->bRead  = bRead;
    fio->bDouble= (sizeof(real) == sizeof(double));
    fio->bDebug = FALSE;
    fio->bOpen  = TRUE;

    return nfio;
}