-
Notifications
You must be signed in to change notification settings - Fork 12
/
differencial_evolution_1.F90
executable file
·326 lines (315 loc) · 11.8 KB
/
differencial_evolution_1.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
!
! File: differencial_evolution.F90
! Author: zyy
!
! Created on 2013年4月23日, 下午9:47
!
module differencial_evolution
contains
subroutine run_de_lammps(step)
use kinds
use lattice_mod, only : init_lat_nosym
use init_struct, only : fill_atom
use parameters, only : population, pstruct, pool, atom_dis, de_ratio
use sort_atom_idx, only : kmatch
use init_struct
implicit none
integer(i4b), intent(in) :: step
integer(i4b) :: i,j,k
real(dp) :: tmp, cr = 0.9, f = 0.5
integer(i4b) :: a,b,c, a1, a2, n
real(dp) :: p1(3), p2(3), p3(3), y(3), lat(3,3), dis
logical :: f1
do i = 1, population
if(i > floor(population * de_ratio)) then
call init_lat_nosym(i)
call fill_atom(i)
cycle
end if
do
call random_number(tmp)
a = floor(tmp * population * de_ratio + 1.0)
if(a /= i) exit
end do
do
call random_number(tmp)
b = floor(tmp * population * de_ratio + 1.0)
if(b /= i .and. b /= a) exit
end do
do
call random_number(tmp)
c = floor(tmp * population * de_ratio + 1.0)
if(c /= i .and. c /= a .and. c /= b) exit
end do
call kmatch(pstruct(i), pool(a))
call kmatch(pstruct(i), pool(b))
call kmatch(pstruct(i), pool(c))
n = pstruct(i) % natom
lat = pstruct(i) % lat
do j = 1, n
p1(:) = pool(a) % pos(:, j)
p2(:) = pool(b) % pos(:, j)
p3(:) = pool(c) % pos(:, j)
y(:) = p1(:) + f * (p2(:) - p3(:))
do k = 1, 3
y(k) = y(k) - lat(k, k) * real(floor(y(k) / lat(k, k)))
end do
f1 = .true.
do k = 1, n
if(k == j) cycle
call find_dis(lat, pstruct(i) % pos(:, k), y(:), dis)
a1 = pstruct(i) % ptype(k)
a2 = pstruct(i) % ptype(j)
if(dis < atom_dis(a1, a2)) then
f1 = .false.
end if
end do
if(f1) then
pstruct(i) % pos(:, j) = y(:)
end if
end do
end do
end subroutine run_de_lammps
subroutine run_de_vasp(step)
use kinds
use constants
use parameters, only : pstruct, pool, population, de_ratio
use parameters, only : Q2D, vacuum_layer, cluster
use parameters, only : cluster_substrate, num_species, num_ele, SubstrateElements
use parameters, only : fix_atom
use sort, only : sort_results
use init_struct_all, only : init_struct_tot
use sort_atom_idx, only : kmatch
implicit none
integer(i4b), intent(in) :: step
integer(i4b) :: i,j,k, ratio_cut, a,b, i1
real(dp) :: tmp, y(3), p1(3), p2(3), p3(3), pbest(3)
real(dp) :: lambd = 0.2, F = 0.2
real(dp) :: c1, c2, c3, ratio, max_z, min_z
logical :: flag
logical :: is_substrate(max_atom * max_type)
write(*, *) "start de_operation"
do i = 1, population
write(*, *) "energy now : old", pstruct(i) % energy, pool(i) % energy
if (pstruct(i) % energy < pool(i) % energy) then
write(*, *) "renew: ", i
pool(i) = pstruct(i)
end if
end do
call sort_results()
ratio_cut = floor(de_ratio * population)
do i = 1, population
write(*, *) "de_struct: ", i
write(*, *) "atom_type: ", (pstruct(i) % ptype(k), k = 1, pstruct(i) % natom)
if(i > ratio_cut) then
call init_struct_tot(i)
cycle
end if
do
call random_number(tmp)
a = floor(tmp * ratio_cut + 1)
if(a /= i) exit
end do
do
call random_number(tmp)
b = floor(tmp * ratio_cut + 1)
if(b /= i .and. b /= a) exit
end do
!call kmatch(pool(1), pstruct(i))
!call kmatch(pool(1), pool(a))
!call kmatch(pool(1), pool(b))
!atoms of the substrate do not perform the de operation
if(cluster_substrate) then
i1 = 0
do j = 1, num_species
do k = 1, num_ele(j)
i1 = i1 + 1
if(k <= SubstrateElements(j)) then
is_substrate(i1) = .true.
else
is_substrate(i1) = .false.
end if
end do
end do
end if
do j = 1, pstruct(i) % natom
pbest(:) = pool(1) % pos(:, j)
p1(:) = pool(i) % pos(:, j)
p2(:) = pool(a) % pos(:, j)
p3(:) = pool(b) % pos(:, j)
y(:) = lambd * pbest(:) + (1-lambd) * p1(:) + F * (p2(:) - p3(:))
y(:) = y(:) - floor(y(:))
if(cluster_substrate) then
if(.not. is_substrate(j)) then
pstruct(i) % pos(:, j) = y(:)
end if
else
pstruct(i) % pos(:, j) = y(:)
end if
end do
call check(i, flag)
if (flag .eqv. .false.) then
call init_struct_tot(i)
end if
! for quasi-2D case
! to keep the vacuum_layer after DE operations
if(Q2D) then
min_z = 1.0
max_z = 0.0
do j = 1, pstruct(i) % natom
if(min_z > pstruct(i) % pos(3, j)) min_z = pstruct(i) % pos(3, j)
if(max_z < pstruct(i) % pos(3, j)) max_z = pstruct(i) % pos(3, j)
end do
c1 = (1.0 - (max_z - min_z)) * pstruct(i) % lat(3, 3)
c2 = (max_z - min_z) * pstruct(i) % lat(3, 3) + vacuum_layer
ratio = c1 / c2
do j = 1, pstruct(i) % natom
pstruct(i) % pos(3, j) = pstruct(i) % pos(3, j) * ratio
end do
pstruct(i) % lat(3, 3) = c2
end if
end do
end subroutine run_de_vasp
subroutine check(tag, flag)
use kinds
use parameters, only : pstruct, atom_dis
use parameters, only : model_gb, bottom_height, gb_height, top_height
use init_struct, only : dir2car, find_dis
implicit none
integer(i4b), intent(in) :: tag
logical, intent(out) :: flag
integer(i4b) :: i,j,k,a1,a2,n
real(dp) :: dir0(3), dir1(3), dir2(3), car1(3), car2(3), lattice(3,3)
real(dp) :: distance
integer(i4b) :: dpos(3, 27), dnum
n = pstruct(tag) % natom
lattice = pstruct(tag) % lat
dnum = 0
do i = -1, 1
do j = -1, 1
do k = -1, 1
dnum = dnum + 1
dpos(1,dnum) = i
dpos(2,dnum) = j
dpos(3,dnum) = k
end do
end do
end do
flag = .true.
do i = 1, n
do j = 1, i - 1
dir0 = pstruct(tag) % pos(:, i)
dir2 = pstruct(tag) % pos(:, j)
do k = 1, 27
dir1(:) = dpos(:, k) + dir0(:)
call dir2car(lattice, dir1, car1)
call dir2car(lattice, dir2, car2)
call find_dis(lattice, car1, car2, distance)
a1 = pstruct(tag) % ptype(i)
a2 = pstruct(tag) % ptype(j)
if(distance < atom_dis(a1, a2) * 0.9) then
flag = .false.
return
end if
end do
end do
end do
! for grain boundary case
! if atom is not in the gb region, return false
if(model_gb) then
do i = 1, n
dir0 = pstruct(tag) % pos(:, i)
if(dir0(3) < (bottom_height - 0.2 * gb_height) / lattice(3, 3) .or. &
& dir0(3) > (1 - (top_height - 0.2 * gb_height)/ lattice(3, 3))) then
flag = .false.
end if
end do
end if
end subroutine check
subroutine run_mode_vasp(step)
use kinds
use parameters, only : pstruct, pool, population, de_ratio
use sort, only : sort_results_mode
use init_struct_spg, only : init_struct_sym
implicit none
integer(i4b), intent(in) :: step
integer(i4b) :: i,j,k,ratio_cut, a, b, c, nf
real(dp) :: tmp, y(3), p1(3), p2(3), p3(3), pbest(3)
real(dp) :: lambd = 0.1, F = 0.2
logical :: flag
do i = 1, population
if(pstruct(i) % energy < pool(i) % energy .and. pstruct(i) % hardness < pool(i) % hardness) then
pool(i) = pstruct(i)
end if
call random_number(tmp)
if(pstruct(i) % energy < pool(i) % energy .or. pstruct(i) % hardness < pool(i) % hardness) then
if(tmp < 0.5) then
pool(i) = pstruct(i)
end if
end if
end do
write(*, *) "start sort"
call sort_results_mode()
write(*, *) "end sort"
ratio_cut = floor(de_ratio * population)
nf = 0
do i = 1, population
if(pool(i) % frontier == 1) then
nf = nf + 1
end if
end do
write(*, *) "number of struct in 1st front: ", nf
do i = 1, population
write(*, *) "de on: ", i
if(pool(i) % energy > 100 .or. pool(i) % hardness > 100) then
write(*, *) "de new struct1: ", i
call init_struct_sym(i)
cycle
end if
if(i > ratio_cut) then
write(*, *) "de new struct2: ", i
call init_struct_sym(i)
cycle
end if
write(*, *) "de operation: ", i
do
call random_number(tmp)
a = floor(tmp * ratio_cut + 1)
if(a /= i) exit
end do
do
call random_number(tmp)
b = floor(tmp * ratio_cut + 1)
if(b /= i .and. b /= a) exit
end do
if(pool(i) % frontier /= 1) then
call random_number(tmp)
c = floor(tmp * nf + 1)
end if
if(pool(i) % frontier == 1) then
do j = 1, pstruct(i) % natom
p1(:) = pool(i) % pos(:, j)
p2(:) = pool(a) % pos(:, j)
p3(:) = pool(b) % pos(:, j)
y(:) = p1(:) + F * (p2(:) - p3(:))
y(:) = y(:) - floor(y(:))
pstruct(i) % pos(:, j) = y(:)
end do
else
do j = 1, pstruct(i) % natom
pbest(:) = pool(c) % pos(:, j)
p1(:) = pool(i) % pos(:, j)
p2(:) = pool(a) % pos(:, j)
p3(:) = pool(b) % pos(:, j)
y(:) = lambd * pbest(:) + (1 - lambd) * p1(:) + F * (p2(:) - p3(:))
y(:) = y(:) - floor(y(:))
pstruct(i) % pos(:, j) = y(:)
end do
end if
call check(i,flag)
if(flag .eqv. .false.) then
call init_struct_sym(i)
end if
end do
end subroutine run_mode_vasp
end module differencial_evolution