-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathatom_property.c
170 lines (155 loc) · 6.36 KB
/
atom_property.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "atom_property.h"
//Make neighbourlist for the atoms
int makelinkedlist(a_coodrinates *input_atoms,int Natoms,float *cellsize,int *ng,long int *llst,long int ***lshd){
int igx,igy,igz;
for(int ii=0;ii<Natoms;ii++){
igx = (int)(input_atoms[ii].loc[0]/cellsize[0]);
igy = (int)(input_atoms[ii].loc[1]/cellsize[1]);
igz = (int)(input_atoms[ii].loc[2]/cellsize[2]);
if(igx >= ng[0] || igy >=ng[1] || igz >= ng[2]){
fprintf(stderr,"out of list: %d %d %d %d %d %d \n",igx,igy,igz,ng[0],ng[1],ng[2]);
exit(1);
}
llst[ii] = lshd[igx][igy][igz];
lshd[igx][igy][igz]=ii;
}
return(0);
}
int makeneighboutlist(a_coodrinates *input_atoms,int Natoms,float *boxmd,float *halfboxmd,float *cellsize,
int *ng,float rcutoffsq,long int *llst,long int ***lshd,int ***retval){
int igx,igy,igz;
int icx,icy,icz,jj,k;
int **nlist;
int min_range=-1,max_range=1;
double dr[3],rsq;
nlist=(int **)malloc(Natoms*sizeof(int *));
for(int ii=0;ii<Natoms;ii++){
igx = (int)(input_atoms[ii].loc[0]/cellsize[0]);
igy = (int)(input_atoms[ii].loc[1]/cellsize[1]);
igz = (int)(input_atoms[ii].loc[2]/cellsize[2]);
nlist[ii]=(int *)malloc((max_neighbour+1)*sizeof(int));
nlist[ii][0]=0;
for(int ix=min_range; ix<=max_range; ix++){
for(int iy=min_range; iy<=max_range; iy++){
for(int iz=min_range; iz<=max_range; iz++){
icx = (igx + ix + ng[0]) % ng[0];
icy = (igy + iy + ng[1]) % ng[1];
icz = (igz + iz + ng[2]) % ng[2];
jj = lshd[icx][icy][icz];
while(jj > 0){
rsq=0.0;
if(jj != ii){
dr[0]=input_atoms[ii].loc[0] - input_atoms[jj].loc[0];
dr[1]=input_atoms[ii].loc[1] - input_atoms[jj].loc[1];
dr[2]=input_atoms[ii].loc[2] - input_atoms[jj].loc[2];
for(k=0;k<3;k++){
if (dr[k] >= halfboxmd[k]) { dr[k] -= boxmd[k]; }
else if(dr[k] <= -halfboxmd[k]) { dr[k] += boxmd[k]; }
rsq += dr[k]*dr[k];
}
if (rsq <= rcutoffsq) {
nlist[ii][0] +=1;
nlist[ii][nlist[ii][0]]=jj;
}
}//end of (jj!=ii)
jj=llst[jj];
}
}
}
}
if( nlist[ii][0] > max_neighbour){
printf("max neighbour list is larger then %d. Its %d\n",max_neighbour,nlist[ii][0]);
exit(1);
}
}
*retval=nlist;
return(0);
}
int read_input(char *filename,a_systeminfo *mdatom_info,a_coodrinates **retval){
char buf[1024];
int atoms,count=-1;
a_coodrinates *input_atoms;
FILE *fp=fopen(filename,"r");
//read first line to find total number of atoms
if ( fgets(buf,sizeof(buf),fp) ){
atoms=atoi(buf);
mdatom_info->Natoms=atoms;
input_atoms = (a_coodrinates *)malloc(atoms*sizeof(a_coodrinates));
}else{
fprintf(stderr,"First line doesnot contain number of atoms");
fclose(fp);
exit(1);
}
//read box size
if ( fgets(buf,sizeof(buf),fp) ){
sscanf(buf,"%f %f %f",&(mdatom_info->boxmd[0]),&(mdatom_info->boxmd[1]),&(mdatom_info->boxmd[2]));
mdatom_info->halfboxmd[0] = 0.5*mdatom_info->boxmd[0];
mdatom_info->halfboxmd[1] = 0.5*mdatom_info->boxmd[1];
mdatom_info->halfboxmd[2] = 0.5*mdatom_info->boxmd[2];
}else{
fprintf(stderr,"Box size not defined");
fclose(fp);
exit(1);
}
//read atom coordinates and its properties
while(fgets(buf,sizeof(buf),fp)){
if(buf[strlen(buf)-1] !='\n'){
fprintf(stderr, "Length of the input string is too large\n");
exit(1);
}
count++;
sscanf(buf,"%s %f %f %f %f",input_atoms[count].aname,&(input_atoms[count].loc[0]),
&(input_atoms[count].loc[1]),&(input_atoms[count].loc[2]),&(input_atoms[count].property));
if (input_atoms[count].property <= 4.0) input_atoms[count].label = 0;
else if (input_atoms[count].property > 4.0 && input_atoms[count].property < 10.0) input_atoms[count].label = 1;
else input_atoms[count].label = 2;
}
*retval = input_atoms;
setsystemparameter(mdatom_info);
return(0);
}
int write_coordinate(a_coodrinates *input_atoms,char *filename,int Natoms,float *box,int **c_number){
FILE *fp=fopen(filename,"w");
fprintf(fp,"%d \n",Natoms);
fprintf(fp,"%12.6f %12.6f %12.6f \n",box[0],box[1],box[2]);
for(int i=0;i<Natoms;i++){
fprintf(fp,"%s \t %12.6f \t %12.6f \t %12.6f \t %10d %10d\n",input_atoms[i].aname,
input_atoms[i].loc[0],input_atoms[i].loc[1],input_atoms[i].loc[2],input_atoms[i].label,c_number[i][0]);
}
return(0);
}
int setsystemparameter(a_systeminfo *mdatom_info){
mdatom_info->binsize=BINSIZE_DEFALUT;
mdatom_info->rcutoff=RCUTOFF_DEFAULT;
mdatom_info->rcutoffsq=mdatom_info->rcutoff*mdatom_info->rcutoff;
int ix,iy,iz;
for(int ii=0;ii<3;ii++){
mdatom_info->cellsize[ii]=mdatom_info->binsize;
mdatom_info->ng[ii]=(int) (mdatom_info->boxmd[ii]/mdatom_info->cellsize[ii]);
mdatom_info->cellsize[ii] = mdatom_info->boxmd[ii]/mdatom_info->ng[ii];
}
//create memory for linked list
mdatom_info->llst=(long int*)malloc(mdatom_info->Natoms*sizeof(long int));
//cread memory for 3d lshd
ix=mdatom_info->ng[0];
iy=mdatom_info->ng[1];
iz=mdatom_info->ng[2];
mdatom_info->lshd=(long int***)malloc(ix*sizeof(long int**));
for(int ii=0;ii<ix;ii++){
mdatom_info->lshd[ii]=(long int**)malloc(iy*sizeof(long int*));
for(int jj=0;jj<iy;jj++){
mdatom_info->lshd[ii][jj]=(long int*)malloc(iz*sizeof(long int));
}
}
//initilze all variable of lshd to -1
for(int ii=0;ii<ix;ii++)
for(int jj=0;jj<iy;jj++)
for(int kk=0;kk<iz;kk++){
mdatom_info->lshd[ii][jj][kk]=-1;
}
return(0);
}