-
Notifications
You must be signed in to change notification settings - Fork 1
/
angles.f90
446 lines (331 loc) · 13 KB
/
angles.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
module mod_angles
implicit none
contains
subroutine compare(ref, vec, out)
implicit none
integer, intent(in) :: ref(:), vec(:)
logical, intent(out) :: out
integer :: i, j, N, cont
N = size(vec)
cont = 0
do i = 1, size(vec)
do j = 1, size(ref)
if (vec(i) == ref(j)) then
cont = cont + 1
exit
endif
enddo
enddo
if (cont == N) then
out = .true.
else
out = .false.
endif
end subroutine compare
subroutine get_neighbor_list( dist_matrix, dist_atoms, N_atoms, N_species, atomtype, &
N_neighbor, neighbor_list )
implicit none
integer, intent(in) :: N_atoms, N_species, dist_atoms(:,:), atomtype(:)
real*8, intent(in) :: dist_matrix(:,:)
integer, intent(out) :: N_neighbor(N_atoms, N_species)
integer, allocatable, intent(out) :: neighbor_list(:,:,:,:)
integer :: aux_list(N_atoms, N_species, size(dist_matrix,1),2)
real*8 :: aux_dist(N_atoms, size(dist_matrix,1))
integer :: i, iat, iesp, size_neigh, ins(2)
real*8 :: d
aux_list = 0
N_neighbor = 0
do i = 1, size(dist_matrix,1)
iat = dist_atoms(i,1)
iesp = atomtype( dist_atoms(i,2) )
d = norm2( dist_matrix(i,:) )
! Insertion sort
ins = (/i,dist_atoms(i,2)/)
!call insert( aux_dist(iat,:), aux_list(iat,:), N_neighbor(iat), d, i )
call insert2( aux_dist(iat,:), aux_list(iat,iesp,:,:), N_neighbor(iat,iesp), d, ins )
enddo
size_neigh = maxval(N_neighbor)
allocate(neighbor_list(N_atoms, N_species, size_neigh,2))
neighbor_list = aux_list(:,:,:size_neigh,:)
end subroutine get_neighbor_list
! Solo el numero de vecinos que hay hasta el primer minimo
subroutine get_neighbor_list2( ext, dist_matrix, dist_atoms, N_atoms, N_species, atomtype, &
mat_neighbor,N_neighbor, neighbor_list )
implicit none
integer, intent(in) :: N_atoms, N_species, dist_atoms(:,:), atomtype(:), &
mat_neighbor(:,:), ext
real*8, intent(in) :: dist_matrix(:,:)
integer, intent(out) :: N_neighbor(N_atoms, N_species)
integer, allocatable, intent(out) :: neighbor_list(:,:,:,:)
integer :: aux_list(N_atoms, N_species, size(dist_matrix,1),2)
real*8 :: aux_dist(N_atoms, N_species, size(dist_matrix,1))
integer :: i, iat, iesp, size_neigh, ins(2)
real*8 :: d
aux_list = 0
N_neighbor = 0
do i = 1, size(dist_matrix,1)
iat = dist_atoms(i,1)
iesp = atomtype( dist_atoms(i,2) )
d = norm2( dist_matrix(i,:) )
! Insertion sort
ins = (/i,dist_atoms(i,2)/)
!call insert( aux_dist(iat,:), aux_list(iat,:), N_neighbor(iat), d, i )
call insert2( aux_dist(iat,iesp,:), aux_list(iat,iesp,:,:), N_neighbor(iat,iesp), d, ins )
enddo
size_neigh = maxval(N_neighbor)+ext
allocate(neighbor_list(N_atoms, N_species, size_neigh,2))
neighbor_list = aux_list(:,:,:size_neigh,:)
! N_neighbor = 0
! do iat = 1, N_atoms
! do iesp = 1, N_species
! do i = 1, mat_neighbor(atomtype(iat),iesp)
! neighbor_list(iat,iesp,i,:) = aux_list(iat,iesp,i,:)
! N_neighbor(iat,iesp) = N_neighbor(iat,iesp) + 1
! enddo
! enddo
! enddo
end subroutine get_neighbor_list2
subroutine insert(V_dist, W_list, N, x_d, y_n)
implicit none
real*8, intent(inout) :: V_dist(:)
integer, intent(inout) :: N, W_list(:)
real*8, intent(in) :: x_d
integer, intent(in) :: y_n
integer :: j
N = N + 1
V_dist(N) = x_d
W_list(N) = y_n
do j = N, 2 , -1
if ( V_dist(j-1) < V_dist(j) ) then
exit
else
V_dist(j) = V_dist(j-1)
V_dist(j-1) = x_d
W_list(j) = W_list(j-1)
W_list(j-1) = y_n
endif
enddo
end subroutine insert
subroutine insert2(V_dist, W_list, N, x_d, y_n)
implicit none
real*8, intent(inout) :: V_dist(:)
integer, intent(inout) :: N, W_list(:,:)
real*8, intent(in) :: x_d
integer, intent(in) :: y_n(:)
integer :: j
N = N + 1
V_dist(N) = x_d
W_list(N,:) = y_n
do j = N, 2 , -1
if ( V_dist(j-1) < V_dist(j) ) then
exit
else
V_dist(j) = V_dist(j-1)
V_dist(j-1) = x_d
W_list(j,:) = W_list(j-1,:)
W_list(j-1,:) = y_n(:)
endif
enddo
end subroutine insert2
subroutine find_pair_index( ext, tag1, tag2, neighbor_order_list, N_neighbor, ind, ierr )
implicit none
integer, intent(in) :: ext, tag1, tag2, neighbor_order_list(:), N_neighbor
integer, intent(out) :: ind, ierr
integer :: i, j, ind1, ind2, found1, found2
found1 = 0
found2 = 0
ind1 = -1
ind2 = -1
do i = 1, N_neighbor + ext
if (found1 == 1 .and. found2 == 1) then
exit
endif
if ( neighbor_order_list(i) == tag1 ) then
ind1 = i
found1 = found1 + 1
endif
if ( neighbor_order_list(i) == tag2 ) then
ind2 = i
found2 = found2 + 1
endif
enddo
if (ind1 < 0 .or. ind2 < 0) then
! print*, "ERROR: ", ind1, ind2
! print*, tag1, tag2
! print*, neighbor_order_list(:)
ierr = -1
!STOP
else
ierr = 0
endif
i = min(ind1,ind2)
j = max(ind1,ind2)
ind = i + (j-1)*(j-2)/2
end subroutine find_pair_index
subroutine update_angle_distr(ext, neighbor_order_list, atomtype, mat_neighbor, &
dist_matrix, neighbor_list, angle_distr, cont_adf )
implicit none
integer, intent(in) :: ext, neighbor_order_list(:,:,:), atomtype(:), &
neighbor_list(:,:,:,:), mat_neighbor(:,:)
real*8, intent(in) :: dist_matrix(:,:)
real*8, intent(inout) :: angle_distr(:,:,:,:)
integer, intent(inout) :: cont_adf(:,:,:)
integer :: N_atoms, N_species, max_pair, bins, N_pair, N_neigh
integer :: iat, iesp, ipair, n1, n2, tag1, tag2, ind, ihist, ierr
real*8 :: rj(3), rk(3), pi, c, theta, x
pi = acos(-1.0d0)
c = 0.03d0
N_atoms = size(angle_distr,1)
N_species = size(angle_distr,2)
max_pair = size(angle_distr,3)
bins = size(angle_distr,4)
! Loop in atoms
do iat = 1, N_atoms
! Loop in species
do iesp = 1, N_species
if ( atomtype(iat) == iesp ) cycle
N_neigh = mat_neighbor(atomtype(iat),iesp)
N_pair = N_neigh*(N_neigh-1)/2
! Loop in all posible pairs
do n1 = 1, mat_neighbor(atomtype(iat),iesp)
tag1 = neighbor_list(iat,iesp,n1,2)
rj = dist_matrix( neighbor_list(iat,iesp,n1,1), : )
do n2 = n1+1, mat_neighbor(atomtype(iat),iesp)
tag2 = neighbor_list(iat,iesp,n2,2)
rk = dist_matrix( neighbor_list(iat,iesp,n2,1), : )
! Find index in the angle_distr corresponding to the pair (n1,n2)
call find_pair_index( ext, tag1, tag2, neighbor_order_list(iat,iesp,:), &
N_neigh, ind, ierr )
! if (ierr == -1) then
! print*, iat, iesp
! write(*,"(100i5)") neighbor_list(iat,iesp,:N_neigh,2)
! write(*,"(100i5)") neighbor_order_list(iat,iesp,:N_neigh)
! STOP
! endif
if (ierr == 0) then
! Update histogram
theta = ( sum(rj*rk) / (norm2(rj)*norm2(rk)) )
if (abs(theta) + 1.0D-8 < 1.0d0) then
cont_adf(iat,iesp,ind) = cont_adf(iat,iesp,ind) + 1
theta = acos(theta)
ihist = int(theta/pi*bins)
angle_distr(iat,iesp,ind,ihist) = angle_distr(iat,iesp,ind,ihist) + 1.0d0
! do ihist=1, bins
! x=pi/bins*ihist
! angle_distr(iat,iesp,ind,ihist) = angle_distr(iat,iesp,ind,ihist) + &
! exp(-(x-theta)**2/(2.0d0*c**2))/(c*sqrt(2.0d0*pi))
! enddo
endif
endif
enddo
enddo
enddo
enddo
end subroutine update_angle_distr
subroutine apply_smearing_ang(histogram)
implicit none
real*8, intent(inout) :: histogram(:)
integer :: bins, ihist, jhist, N, i
real*8 :: copy(size(histogram)), theta, x, pi, c, dx, norm
pi = acos(-1.0d0)
c = 0.05d0
bins = size(histogram)
copy = 0.0d0
do ihist = 1, bins
if (histogram(ihist) < 1.0D-8) cycle
theta = (pi/bins)*(ihist+0.5d0)
do jhist = 1, bins
x = (pi/bins)*jhist
copy(jhist) = copy(jhist) + histogram(ihist)*exp(-(x-theta)**2/(2.0d0*c**2))/(c*sqrt(2.0d0*pi))
enddo
enddo
dx = pi/bins
norm = sum(copy)*dx
do i = 1, bins
if (copy(i) > 1.0d-14) then
histogram(i) = copy(i)/norm
else
histogram(i) = copy(i)
endif
enddo
end subroutine apply_smearing_ang
! angle_distr :: Distribution of the distance between each couple of atoms
! lenght(natoms,N_species,max_neigh,bins)
! contribution_adf :: Decomposition of the total_pdf into the distributions of the first neighbors
! lenght(N_species,N_species,max_neigh,bins)
! total_adf :: total pdf for each pair of species
! lenght(N_species,N_species,bins)
subroutine total_adf( ext, angle_distr, mat_neighbor, neighbor_list, atomtype, numions, contribution_adf, tot_adf, cont_adf)
implicit none
real*8, intent(inout) :: angle_distr(:,:,:,:)
integer, intent(in) :: ext, neighbor_list(:,:,:,:), mat_neighbor(:,:), atomtype(:), numions(:), cont_adf(:,:,:)
real*8, intent(out) :: tot_adf(:,:,:), contribution_adf(:,:,:,:)
integer :: N_atoms, N_species, max_neigh, bins, iat, iesp, n1, ihist, jesp, N_neigh
integer, allocatable :: cont(:,:,:)
N_atoms = size(angle_distr,1)
N_species = size(angle_distr,2)
max_neigh = size(angle_distr,3)
bins = size(angle_distr,4)
tot_adf = 0.0d0
contribution_adf = 0.0d0
allocate(cont(N_species, N_species, max_neigh))
cont = 0
! Loop in atoms
do iat = 1, N_atoms
! Loop in species
do iesp = 1, N_species
if ( atomtype(iat) == iesp ) cycle
N_neigh = mat_neighbor(atomtype(iat),iesp)*(mat_neighbor(atomtype(iat),iesp)-1)/2! + ext
do n1 = 1, N_neigh
!call apply_smearing_ang( angle_distr(iat,iesp,n1,:) )
cont(atomtype(iat),iesp,n1) = cont(atomtype(iat),iesp,n1) + 1
do ihist = 1, bins
!if ( angle_distr(iat,iesp,n1,ihist) > 1.0d-8 ) then
!print*, iat, iesp, n1, sum(angle_distr(iat,iesp,n1,:))*acos(-1.0d0)/bins
contribution_adf(atomtype(iat),iesp,n1,ihist) = contribution_adf(atomtype(iat),iesp,n1,ihist) + &
angle_distr(iat,iesp,n1,ihist)!/cont_adf(iat,iesp,n1)! / &
!N_neigh
!endif
enddo
enddo
enddo
enddo
do iat = 1, N_species
do iesp = 1, N_species
if(iat == iesp) cycle
N_neigh = mat_neighbor(iat,iesp)*(mat_neighbor(iat,iesp)-1)/2! + ext
do n1 = 1, N_neigh
contribution_adf(iat,iesp,n1,:) = contribution_adf(iat,iesp,n1,:)/cont(iat,iesp,n1)
!print*, iat, iesp, n1, sum(contribution_adf(iat,iesp,n1,:))*acos(-1.0d0)/bins
enddo
enddo
enddo
!tot_adf = sum(contribution_adf,dim=3)
end subroutine total_adf
subroutine get_mean_sigma_angle( N_pair, angle_distr, mean, sigma )
implicit none
integer, intent(in) :: N_pair
real*8, intent(inout) :: angle_distr(:,:)
real*8, intent(out) :: mean(:), sigma(:)
real*8, parameter :: pi = acos(-1.0d0)
real*8 :: x, dx, norm
integer :: ipair, i, N
N = size(angle_distr,2)
dx = pi/N
mean = 0.0d0
sigma = 0.0d0
do ipair = 1, N_pair
do i = 1, N
x = (i-0.5)*dx
mean(ipair) = mean(ipair) + x * angle_distr(ipair,i) * dx
enddo
do i = 1, N
x = (i-0.5)*dx
sigma(ipair) = sigma(ipair) + ( x-mean(ipair) )**2 * angle_distr(ipair,i) * dx
enddo
!print*, sum(angle_distr(ipair,:))*dx
enddo
mean = mean*180/pi
sigma = sqrt(sigma)*180/pi
end subroutine get_mean_sigma_angle
end module mod_angles