Aug 19, 2014

[C/C++] read xyz file and convert element symbol into atomic number

// This program reads a text file containing molecular configuration (element symbol, and xyz coordinates), and convert element symbol into atomic number.
 
#include <stdio.h>
 
#define max 1000
 
    int element[max], t_element=0, c_element[max];
    float x[max], y[max], z[max], t_x, t_y, t_z, c_x[max], c_y[max], c_z[max];
    int total_n=0;
    int count_atom=0;
    void list_atoms(int count_atom, int t_element, float *t_x, float *t_y, float *t_z);
 
 
void main(void)
{
FILE *xyzfile = fopen("example.txt", "r");
    if(xyzfile==NULL) {
        printf("\nError: Unable to open file.\n");
        exit(1);
    } else {
        fscanf(xyzfile, "%d\n", &total_n);
//       printf("\n\tTotal number of atoms : %d\n", total_n);
        char description[100];
        fgets(description, 100, xyzfile);
//       printf("\nDescription : %s\n", description);
 
        char e_type[2];
        int i=1;           // [1] is for first atom (third line)
        while(fscanf(xyzfile, "%s %f %f %f\n", e_type, &x[i], &y[i], &z[i]) != EOF){       // loop through and store elemental symbol, x, y, and z values into the array
 
            if (strcmp(e_type, "H")==0 || strcmp(e_type, "h")==0) {                        // convert elemental symbol into elemetal number
                element[i] = 1;
                t_element=element[i];
                t_x=x[i]; t_y=y[i]; t_z=z[i];
                count_atom++;
                list_atoms(count_atom, t_element, &t_x, &t_y, &t_z);
                i++;
            } else if(strcmp(e_type, "C")==0 || strcmp(e_type, "c")==0) {
                element[i] = 6;
                t_element=element[i];
                t_x=x[i]; t_y=y[i]; t_z=z[i];
                count_atom++;
                list_atoms(count_atom, t_element, &t_x, &t_y, &t_z);
                i++;
            } else if(strcmp(e_type, "N")==0 || strcmp(e_type, "n")==0) {
                element[i] = 7;
                t_element=element[i];
                t_x=x[i]; t_y=y[i]; t_z=z[i];
                count_atom++;
                list_atoms(count_atom, t_element, &t_x, &t_y, &t_z);
                i++;
            } else if(strcmp(e_type, "O")==0 || strcmp(e_type, "o")==0) {
                element[i] = 8;
                t_element=element[i];
                t_x=x[i]; t_y=y[i]; t_z=z[i];
                count_atom++;
                list_atoms(count_atom, t_element, &t_x, &t_y, &t_z);
                i++;
            } else if(strcmp(e_type, "Na")==0 || strcmp(e_type, "na")==0 || strcmp(e_type, "NA")==0) {
                element[i] = 11;
                t_element=element[i];
                t_x=x[i]; t_y=y[i]; t_z=z[i];
                count_atom++;
                list_atoms(count_atom, t_element, &t_x, &t_y, &t_z);
                i++;
            } else if(strcmp(e_type, "Mg")==0 || strcmp(e_type, "mg")==0 || strcmp(e_type, "MG")==0) {
                element[i] = 12;
                t_element=element[i];
                t_x=x[i]; t_y=y[i]; t_z=z[i];
                count_atom++;
                list_atoms(count_atom, t_element, &t_x, &t_y, &t_z);
                i++;
            } else if(strcmp(e_type, "Ca")==0 || strcmp(e_type, "ca")==0 || strcmp(e_type, "CA")==0) {
                element[i] = 20;
                t_element=element[i];
                t_x=x[i]; t_y=y[i]; t_z=z[i];
                count_atom++;
                list_atoms(count_atom, t_element, &t_x, &t_y, &t_z);
                i++;
            } else {
                element[i] = 0;
            }
    }
    fclose(xyzfile);
 
    printf("\nNumber of atoms : %d", count_atom);
}
}
 
 
void list_atoms(int count_atom, int t_element, float *t_x, float *t_y, float *t_z)
{
    int k = count_atom;
 
    c_element[k] = t_element;
    c_x[k] = *t_x;
    c_y[k] = *t_y;
    c_z[k] = *t_z;
    printf("\nafter function, converted element %-3d = %-2d %.5f %.5f %.5f", count_atom, c_element[k], c_x[k], c_y[k], c_z[k]);
}
 

No comments:

Post a Comment