-
Notifications
You must be signed in to change notification settings - Fork 2
/
read_xdatcar.f90
239 lines (206 loc) · 12.1 KB
/
read_xdatcar.f90
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
subroutine calc_vectors(lattice_11, lattice_12, lattice_13, lattice_21, lattice_22, lattice_23, lattice_31, lattice_32, lattice_33, aa, bb, cc, alpha, beta, gamma, calpha, cbeta,cgamma)
implicit none
real(kind=8), Intent(In) :: lattice_11, lattice_12, lattice_13,lattice_21, lattice_22, lattice_23, lattice_31, lattice_32, lattice_33 !These are the element of the cell matrix a11, a12, etc etc, be carefull sometimes the matrix is rotated.
real(kind=8), Intent(Out) :: aa, bb, cc ! lenght of the vectors of the cell
real(kind=8), Intent(Out) :: alpha, beta, gamma, calpha, cbeta, cgamma
real(kind=8) :: vol
aa = lattice_11 !In principle the formula is more complex, this is the shortcutbecause we have a triangular matrix
bb = sqrt(lattice_21*lattice_21 + lattice_22*lattice_22)
cc = sqrt(lattice_31*lattice_31 + lattice_32*lattice_32+lattice_33*lattice_33)
vol = (lattice_31*(lattice_12*lattice_23-lattice_13*lattice_22) + lattice_32*(lattice_13*lattice_21-lattice_11*lattice_23) + lattice_33*(lattice_11*lattice_22-lattice_12*lattice_21))
alpha = acos((lattice_21*lattice_31 + lattice_22*lattice_32 + lattice_23*lattice_33)/(bb*cc))
beta = acos((lattice_11*lattice_31 + lattice_12*lattice_32 + lattice_13*lattice_33)/(aa*cc))
gamma = acos((lattice_11*lattice_21 + lattice_12*lattice_22 + lattice_13*lattice_23)/(bb*aa))
calpha = cos(alpha)
cbeta = cos(beta)
cgamma = cos(gamma)
end subroutine calc_vectors
!!!!
!
!!!!
subroutine read_structure(UnitNum,FileName,num_atom, atom_position)
implicit none
integer, Intent(In) :: UnitNum !file name number
character (len=*), Intent(In) :: FileName !name of the output
integer(kind=8) :: i
integer(kind=8), Intent(In) :: num_atom !number of atoms per tructures
real(kind=8), DIMENSION(num_atom, 3), Intent(InOut) :: atom_position !atomic coordinates, matrix 3xN basically the poscar
!write(*,*)UnitNum, FileName
open(unit=UnitNum, file=FileName, status='old', action='read' )
atom_position = -1
read(1,*)! SKIP !read position of first specie
do i=1, num_atom
read(1,*)atom_position(i,1), atom_position(i,2), atom_position(i,3)
end do
return
end subroutine read_structure
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine rdf(vector_rdf, lung, number_of_couples, couple)
implicit none
integer(kind=8), Intent(InOut) :: vector_rdf(number_of_couples,600) !prints up to 60 Angstrom this is the output file
real(kind=8), Intent(In) :: lung ! lenght of the bond
integer(kind=8),Intent(In) :: number_of_couples, couple !number of couple MUST be atmost 29. This is due to the output.
real :: bin
integer :: position
!write(*,*)lung, "lung"
bin = 0.1
position = INT(lung/bin) + 1
!write(*,*) "chiamata a rdf: position", position, "bin", bin, "lung", lung
vector_rdf(couple, position)=vector_rdf(couple, position) + 1
RETURN
end subroutine rdf
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
subroutine setupvector(vector_rdf,number_of_couples)
implicit none
integer(kind=8), Intent(InOut) :: vector_rdf(number_of_couples,600)
integer(kind=8),Intent(In) :: number_of_couples
vector_rdf=0
RETURN
end subroutine setupvector
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
program read_xdatcar
implicit none
integer(kind=8) :: num_structu !number of MD steps
integer(kind=8) :: num_atom ! number of atom per cell
integer(kind=8) :: num_species, atoma, atomb !number of atomic species, number of first atom for the distance, second atom for the distance
integer(kind=8), DIMENSION(:), ALLOCATABLE :: atom_a, atom_b !Coordinates of atom 1 and atom 2 whree you calculate the distance (For example distance Oxygen (N.5) and Hydrogen (n.7). atoma =5. atomb = 7. Atom_a = (O5_x, O5_y, O5_z), etc etc
integer(kind=8) :: number_of_couples !MAX 29!!!!!!!!!
real(kind=8), DIMENSION(:, :), ALLOCATABLE :: atom_position
integer(kind=8), DIMENSION(:), ALLOCATABLE :: element_quantity
integer(kind=8), DIMENSION(3) :: per ! To generate the 27 Next Neighbour
real(kind=8) :: lattice_11, lattice_12, lattice_13
real(kind=8) :: lattice_21, lattice_22, lattice_23
real(kind=8) :: lattice_31, lattice_32, lattice_33
real(kind=8) :: distance, min_distance, bond
real(kind=8) :: aa,bb,cc
real(kind=8) :: alpha, beta, gamma
real(kind=8) :: calpha, cbeta, cgamma
integer(kind=8) :: i , ii, jj, kk, jkl, write_i
integer(kind=8) :: unit_output ! The distance of couple number N is output on fort.N
!fort.(N+10) contains the closest atom B to atom A.
! I also generate fort.(N+30) This contains the distances between A and all the 27 NN B atoms.
! N is the number of couples. SO.... IF N= 32. File fort.32 will cointan the disstance of couple N=32, BUT also the distance of all the NN
! atoms of couple 2. Because those results are written in fort. (2+30), which is the same file!
!!!!!!! CHECK if N+30 or N+40. In case MAX couples 29 or 19
integer(kind=8), DIMENSION(:, :), ALLOCATABLE :: rdf_minimo, rdf_all, vector_rdf ! RDF radial distribution function
!integer(kind=8), DIMENSION(600) :: rdf_minimo, rdf_all, vector_rdf
real(kind=8) :: x_axis
! DEFINE FILES
open (unit = 1, file="ONE_XDATCAR", status='old', action='read') ! trajectory as in HEADER + ALL THE CONFIGURATIONS
open (unit = 2, file="INPUT", status='old', action='read') ! trajectory as in HEADER + ALL THE CONFIGURATIONS
! Necessary to generate next cells.
per(1) = -1
per(2) = 0
per(3) = 1
!
read(1,*) !num_structu, num_species
read(1,*) !skip
read(1,*) lattice_11, lattice_12, lattice_13
read(1,*) lattice_21, lattice_22, lattice_23
read(1,*) lattice_31, lattice_32, lattice_33
read(1,*) ! skip elements name
!READ INPUT
read(2,*) num_structu, num_species
read(2,*)number_of_couples
ALLOCATE(rdf_minimo(number_of_couples,600)) !!!! We do not know how many atoms, we have. We read the number, THEN WE CREATE VECTOR: allocate
ALLOCATE(rdf_all(number_of_couples,600))
ALLOCATE(vector_rdf(number_of_couples,600))
ALLOCATE(atom_a(number_of_couples))
ALLOCATE(atom_b(number_of_couples))
atom_a = 0
atom_b = 0
do jkl=1, number_of_couples ! AGAIN CHECK LESS 29 or 19
read(2,*)atom_a(jkl),atom_b(jkl)
end do
rdf_minimo = 0
rdf_all = 0
!RESET VECTOR RDF
call setupvector(vector_rdf,number_of_couples)
call calc_vectors(lattice_11, lattice_12, lattice_13, lattice_21, lattice_22, lattice_23, lattice_31, lattice_32, lattice_33, aa, bb, cc, alpha, beta, gamma, calpha, cbeta,cgamma)
! THIS SUB calculates the lenght of the cell and the angles.
!write(*,*)calpha
ALLOCATE(element_quantity(num_species))
element_quantity = 0
SELECT CASE (num_species) ! I don't know how many elements I do have.
CASE (1)
read(1,*) element_quantity(1)
CASE (2)
read(1,*) element_quantity(1), element_quantity(2)
CASE (3)
read(1,*) element_quantity(1), element_quantity(2), element_quantity(3)
CASE (4)
read(1,*) element_quantity(1), element_quantity(2), element_quantity(3), element_quantity(4)
CASE (5)
read(1,*) element_quantity(1), element_quantity(2), element_quantity(3), element_quantity(4), element_quantity(5)
CASE (6)
read(1,*) element_quantity(1), element_quantity(2), element_quantity(3), element_quantity(4), element_quantity(5), element_quantity(6)
CASE (7)
read(1,*) element_quantity(1), element_quantity(2), element_quantity(3), element_quantity(4), element_quantity(5), element_quantity(6), element_quantity(7)
CASE (8)
read(1,*) element_quantity(1), element_quantity(2), element_quantity(3), element_quantity(4), element_quantity(5), element_quantity(6), element_quantity(7), element_quantity(8)
END SELECT
!write(*,*)'num species', num_species
num_atom = 0 ! RESET variable
do i=1,num_species
num_atom = num_atom + element_quantity(i)
end do
!write(*,*)"num atom", num_atom
ALLOCATE(atom_position(num_atom,3) )
atom_position = 0
!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!
!!! START !!!
!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!
do i=1,num_structu
call read_structure(1,"ONE_XDATCAR",num_atom, atom_position) ! 1 is the file unit... !ONE_XDATCAR is the file name
!write(*,*)distance(1,2, aa, bb, cc, calpha, cbeta, cgamma, atom_position, num_atom)
! After one structure is read, calculate d, print it.
do jkl=1,number_of_couples
atoma = atom_a(jkl)
atomb = atom_b(jkl)
min_distance = 10000.45 !Necessary to void random number
do ii=1,3
do jj=1,3
do kk=1,3
atom_position(atomb,1) = (atom_position(atomb,1) + per(ii)) ! generate all the 27 possible distance. Not very clever.
atom_position(atomb,2) = (atom_position(atomb,2) + per(jj))
atom_position(atomb,3) = (atom_position(atomb,3) + per(kk))
bond=distance(atoma, atomb, aa, bb, cc, calpha, cbeta, cgamma, atom_position, num_atom)
if ( (bond.lt.min_distance) ) then !.and.(pbclung.gt.0)) then !write(*,*)"MIN", bond, min_distance
min_distance = bond
end if !write(*,*)"this is bond, which is passed as lung", bond
!subroutine rdf(vector_rdf, lung, number_of_couples, couple)
call rdf(rdf_all, bond, number_of_couples, jkl) !write(*,*)"b", bond
atom_position(atomb,1) = (atom_position(atomb,1) - per(ii)) ! remove all the 27 possible distance. Not very clever.
atom_position(atomb,2) = (atom_position(atomb,2) - per(jj))
atom_position(atomb,3) = (atom_position(atomb,3) - per(kk))
end do !ii
end do !! jj
end do !! kk !write(*,*)"minima",min_distance
call rdf(rdf_minimo, min_distance, number_of_couples, jkl) !write(*,*)"this is min_distance, which is passed as lung", min_distance
end do ! jkl
end do ! num_structure
do jkl=1, number_of_couples
unit_output = 10 + jkl
x_axis = 0.0
do write_i=1,600 ! fort.unit_output +10
write(unit_output,*)x_axis, rdf_minimo(jkl,write_i)
write(unit_output+30,*)x_axis, rdf_all(jkl,write_i)
x_axis = x_axis + 0.1
end do ! jkl writing files
end do ! i writing files
end program read_xdatcar
!!!!!!!!!BEGIN FUNCTION
real(kind=8) function distance(atom1, atom2, aa, bb, cc, calpha, cbeta, cgamma, atom_position, num_atom)
implicit none
integer(kind=8), intent(in) :: atom1, atom2, num_atom
real(kind=8), intent(in) :: aa, bb, cc, calpha, cbeta, cgamma
real(kind=8), DIMENSION(num_atom,3), Intent(In) :: atom_position
real(kind=8) :: dfx, dfy, dfz
dfx = (atom_position(atom1,1) - atom_position(atom2,1))
dfy = (atom_position(atom1,2) - atom_position(atom2,2))
dfz = (atom_position(atom1,3) - atom_position(atom2,3))
distance = sqrt(aa*aa*dfx*dfx + bb*bb*dfy*dfy + cc*cc*dfz*dfz + 2*bb*cc*calpha*dfy*dfz + 2*cc*aa*cbeta*dfz*dfx + 2*aa*bb*cgamma*dfx*dfy)
RETURN !no need of return if contains is used
end function distance