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
int
gmx_fio_close(int fio)
{
    int rc = 0;

    gmx_fio_check(fio);

    if (in_ftpset(FIO[fio].iFTP,asize(ftpXDR),ftpXDR)) {
        rc = !xdrclose(FIO[fio].xdr); /* xdrclose returns 1 if happy, negate it */
        sfree(FIO[fio].xdr);
    }
    else {
        /* Don't close stdin and stdout! */
        if (!FIO[fio].bStdio)
            rc = fclose(FIO[fio].fp); /* fclose returns 0 if happy */
    }

    sfree(FIO[fio].fn);
    FIO[fio].bOpen = FALSE;
    do_read  = do_dummy;
    do_write = do_dummy;

    return rc;
}
Ejemplo n.º 3
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;
}