Example #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;
}
Example #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;
}