Beispiel #1
0
int main(void) {
    CHFL_TRAJECTORY* file = chfl_trajectory_open("tests/files/xyz/helium.xyz", 'r');
    CHFL_FRAME* frame = chfl_frame(0);
    unsigned* indexes = NULL;

    if (chfl_trajectory_read(file, frame) != CHFL_SUCCESS) {/*Handle error*/}

    size_t natoms = 0;
    float (*positions)[3] = NULL;
    chfl_frame_positions(frame, &positions, &natoms);
    indexes = malloc(natoms * sizeof(unsigned));
    if (indexes == NULL) {/*Handle error*/}

    for (unsigned i=0; i<natoms; i++) {
        indexes[i] = (unsigned)-1;
    }

    unsigned last_index = 0;
    for (unsigned i=0; i<natoms; i++) {
        if (positions[i][0] < 5) {
            indexes[last_index] = i;
            last_index++;
        }
    }

    printf("Atoms with x < 5:\n");
    unsigned i = 0;
    while(indexes[i] != (unsigned)-1 && i < natoms) {
        printf("  - %d\n", indexes[i]);
        i++;
    }
    printf("Number of atoms: %d\n", i);

    chfl_trajectory_close(file);
    chfl_frame_free(frame);
    free(indexes);
    return EXIT_SUCCESS;
}
Beispiel #2
0
int main() {
    CHFL_TRAJECTORY* traj = chfl_trajectory_open("filename.nc", "r");
    CHFL_FRAME* frame = chfl_frame(0);
    float (*positions)[3] = NULL;
    double* distances = NULL;

    if(traj == NULL || frame == NULL)
        goto error;

    size_t nsteps = 0;
    chfl_trajectory_nsteps(traj, &nsteps);

    distances = (double*)malloc(sizeof(double)*nsteps);
    if (distances == NULL)
        goto error;

    // Accumulate the distances to the origin of the 10th atom throughtout the
    // trajectory
    for (size_t i=0; i<nsteps; i++) {
        if(!chfl_trajectory_read(traj, frame))
            goto error;

        // Only allocate on the first iteration. This assume a constant number
        // of particles
        size_t natoms = 0;
        if (i == 0) {
            chfl_frame_atoms_count(frame, &natoms);
            positions = (float(*)[3])malloc(natoms*3*sizeof(float));
            if (positions == NULL)
                goto error;
        }

        // Position of the 10th atom
        chfl_frame_positions(frame, positions, natoms);
        double distance = sqrt(positions[9][0]*positions[9][0] +
                               positions[9][1]*positions[9][1] +
                               positions[9][2]*positions[9][2]);
        distances[i] = distance;
    }

    double mean = 0;
    for (size_t i=0; i<nsteps; i++) {
        mean += distances[i];
    }
    mean /= nsteps;

    double rmsd = 0.0;
    for (size_t i=0; i<nsteps; i++) {
        rmsd += (mean - distances[i])*(mean - distances[i]);
    }
    rmsd /= nsteps;
    rmsd = sqrt(rmsd);

    printf("Root-mean square displacement is: %f", rmsd);

    // Free the memory
    chfl_trajectory_close(traj);
    chfl_frame_free(frame);
    free(distances);
    free(positions);
    return 0;

error:
    printf("Error, cleaning up …\n");
    chfl_trajectory_close(traj);
    chfl_frame_free(frame);
    free(distances);
    free(positions);
    return 1;
}
Beispiel #3
0
int main() {
    CHFL_FRAME* frame = chfl_frame(5);

    size_t natoms=0, step=0;
    assert(!chfl_frame_atoms_count(frame, &natoms));
    assert(natoms == 5);

    assert(!chfl_frame_step(frame, &step));
    assert(step == 0);

    assert(!chfl_frame_set_step(frame, 42));
    assert(!chfl_frame_step(frame, &step));
    assert(step == 42);

    float data[4][3];
    float pos[4][3];
    for (unsigned i=0; i<4; i++)
        for (unsigned j=0; j<3; j++)
            data[i][j] = i*j;

    assert(!chfl_frame_set_positions(frame, data, 4));
    assert(!chfl_frame_atoms_count(frame, &natoms));
    assert(natoms == 4);

    assert(!chfl_frame_positions(frame, pos, 4));
    for (unsigned i=0; i<4; i++)
        for (unsigned j=0; j<3; j++)
            assert(fabs(data[i][j] - pos[i][j]) < 1e-9);


    bool has_vel = true;
    assert(!chfl_frame_has_velocities(frame, &has_vel));
    assert(has_vel == false);

    assert(!chfl_frame_set_velocities(frame, data, 4));
    float vel[4][3];
    assert(!chfl_frame_velocities(frame, vel, 4));
    for (unsigned i=0; i<4; i++)
        for (unsigned j=0; j<3; j++)
            assert(fabs(data[i][j] - vel[i][j]) < 1e-9);

    assert(!chfl_frame_has_velocities(frame, &has_vel));
    assert(has_vel == true);

    /*********************/
    CHFL_CELL* cell = chfl_cell(3, 4, 5);
    assert(!chfl_frame_set_cell(frame, cell));
    chfl_cell_free(cell);
    cell = chfl_cell_from_frame(frame);
    double a, b, c;
    assert(!chfl_cell_lengths(cell, &a, &b, &c));
    assert(fabs(a - 3.0) < 1e-9);
    assert(fabs(b - 4.0) < 1e-9);
    assert(fabs(c - 5.0) < 1e-9);
    chfl_cell_free(cell);
    /*********************/

    /*********************/
    CHFL_TOPOLOGY* top = chfl_topology();
    CHFL_ATOM* Zn = chfl_atom("Zn");
    CHFL_ATOM* Ar = chfl_atom("Ar");
    assert(!chfl_topology_append(top, Zn));
    assert(!chfl_topology_append(top, Ar));

    assert(!chfl_frame_set_topology(frame, top));
    chfl_topology_free(top);
    chfl_atom_free(Zn);
    chfl_atom_free(Ar);
    /*********************/

    top = chfl_topology_from_frame(frame);
    CHFL_ATOM* atom = chfl_atom_from_topology(top, 0);
    char name[32];
    assert(!chfl_atom_name(atom, name, sizeof(name)));
    assert(strcmp(name, "Zn") == 0);

    atom = chfl_atom_from_frame(frame, 1);
    assert(!chfl_atom_name(atom, name, sizeof(name)));
    assert(strcmp(name, "Ar") == 0);
    chfl_atom_free(atom);

    assert(!chfl_frame_free(frame));

    return EXIT_SUCCESS;
}