-
Notifications
You must be signed in to change notification settings - Fork 0
/
13-static.f90
411 lines (354 loc) · 13.5 KB
/
13-static.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
module mo_static
use mo_syst
use mo_config
use mo_math
implicit none
type tpset_fourier
character(250) :: file_sq = "short_sq.dat" ! file for the structure factor s(|q|)
logical :: calc_flag = .true. ! to calculate or not, the condition is assigned in program main
! control parameters for the output type, one could also adjust them in program main
real(8) :: cutoff = 3.d0 ! the maximal qsc in the calculation and the output
real(8) :: bin = 0.5d0 ! the bin of qsc for average
end type
type(tpset_fourier) :: set_fourier
type tpfourier
! output: dimensionless qualities, more qualities in q space can be added here
real(8), allocatable, dimension(:) :: qsc
real(8), allocatable, dimension(:) :: sqsc ! the structure factor s(|q|)
! output: control parameter
integer, allocatable, dimension(:) :: qord ! the ordered index of qsc
logical, allocatable, dimension(:) :: q_flag ! for cutoff
! poilot process: control parameters, do not attach values here!
integer :: ave_times ! to record the number of effective call for calculation
integer :: ave_num ! ave_num = ave_times * natom
! poilot process: wave vector, being calculated in the init_fourier
logical :: bin_flag = .false.
logical :: cut_flag = .false.
real(8) :: unit_d, cutoff, bin
real(8) :: kve(free) ! kve(i) = 2.0*pi / la(i), dimensional quality, la(i) is the box length
integer :: nve ! nve = int(dble(natom)**(1.d0/dble(free))), natom is the number of particles
integer :: dimq ! the len of qsc/sqsc/qord (it is according to the set of cutoff, if cutoff presents)
contains
! the initial procedure
! procedure :: init_fourier => init_fourier
! calculation
! procedure :: calc_fourier => calc_fourier
procedure :: calc_unit_length => calc_unit_length
! the output procedure for the structure factor s(|q|)
! procedure :: outp_fourier => outp_fourier
end type
type(tpfourier) :: fourier
contains
! free == 2
subroutine init_fourier( tfourier, tcon, tcutoff, tbin )
!
! initiate fourier, allocate memory of fourier
!
implicit none
! para list
type(tpfourier), intent(inout) :: tfourier
type(con_t), intent(in) :: tcon
real(8), intent(in), optional :: tcutoff
real(8), intent(in), optional :: tbin
! local
real(8), allocatable, dimension(:) :: qtest
real(8) :: temp_qsc
integer :: dimqtest
integer :: nn(free)
integer :: i, j, countn, counti
if(free .ne. 2) then
write(*,*) "the module is only for 2d, modify it, if you need 3d version"
stop
end if
associate( &
kve => tfourier%kve, &
nve => tfourier%nve, &
dimq => tfourier%dimq, &
unit_d => tfourier%unit_d, &
cutoff => tfourier%cutoff, &
cut_flag => tfourier%cut_flag, &
bin => tfourier%bin, &
bin_flag => tfourier%bin_flag, &
ave_times => tfourier%ave_times, &
ave_num => tfourier%ave_num, &
natom => tcon%natom, &
la => tcon%la &
)
kve = 2.d0*pi / la
nve = int( dble(natom)**( 1.d0/dble(free) ) )
unit_d = tfourier%calc_unit_length( tcon )
dimqtest = 1
do i = 1, free
dimqtest = dimqtest * ( nve-i+1 )
end do
dimqtest = dimqtest / int(gamma( dble(free)+1.d0 ))
if ( present( tbin ) ) then
bin_flag = .true.
bin = tbin
end if
if ( present( tcutoff ) ) then
cut_flag = .true.
cutoff = tcutoff
allocate( qtest(dimqtest) )
countn = 0
nn(1) = 0
do while (nn(1) < nve)
nn(1) = nn(1) + 1
nn(2) = nn(1)
do while (nn(2) < nve)
nn(2) = nn(2) + 1
countn = countn + 1
qtest(countn) = dsqrt( sum( ( dble(nn)/la )**2 ) ) * 2.d0*pi * unit_d
end do
end do
call qsort(qtest)
loop1: do i = 1, dimqtest
if(qtest(i) >= cutoff) then
dimq = i - 1
exit loop1
end if
end do loop1
deallocate( qtest )
else
dimq = dimqtest
end if
if(dimq < 1) then
write(*,*) "the cutoff is too larger, please reenter"
stop
end if
allocate( tfourier%qsc (dimq) )
allocate( tfourier%sqsc(dimq) )
allocate( tfourier%qord(dimq) )
allocate( tfourier%q_flag (dimqtest) )
associate( &
qsc => tfourier%qsc, &
sqsc => tfourier%sqsc, &
qord => tfourier%qord, &
q_flag => tfourier%q_flag &
)
qsc = 0.d0
sqsc = 0.d0
qord = 0
q_flag = .false.
if( present( tcutoff ) ) then
countn = 0
counti = 0
nn(1) = 0
loop1: do while (nn(1) < nve)
nn(1) = nn(1) + 1
nn(2) = nn(1)
loop2: do while (nn(2) < nve)
nn(2) = nn(2) + 1
counti = counti + 1
temp_qsc = dsqrt( sum( ( dble(nn)/la )**2 ) ) * 2.d0*pi * unit_d
if(temp_qsc >= cutoff) cycle
countn = countn + 1
if (countn > dimq) exit loop1
qsc(countn) = temp_qsc
q_flag(counti) = .true.
end do loop2
end do loop1
else
countn = 0
nn(1) = 0
do while (nn(1) < nve)
nn(1) = nn(1) + 1
nn(2) = nn(1)
do while (nn(2) < nve)
nn(2) = nn(2) + 1
countn = countn + 1
qsc(countn) = dsqrt( sum( ( dble(nn)/la )**2 ) ) * 2.d0*pi * unit_d
end do
end do
end if
ave_num = natom
ave_times = 0
qord = sortperm(dimq, qsc)
! do i = 1, dimq
! write(*,*) i, qsc(i), qord(i)
! end do
end associate
end associate
end subroutine
subroutine calc_fourier( tfourier, tcon, tcalc_flag )
!
! if tcalc_flag is true, calculate fourier
!
implicit none
! para list
type(tpfourier), intent(inout) :: tfourier
type(con_t), intent(in) :: tcon
logical, intent(in) :: tcalc_flag
! local
integer :: nn(free)
real(8) :: kk(free)
integer :: i, countn, counti
real(8) :: cosqra, sinqra, qra, temp
real(8) :: rsq, isq
if(free .ne. 2) then
write(*,*) "the module is only for 2d, modify it, if you need 3d version"
stop
end if
if(tcalc_flag) then
associate( &
q_flag => tfourier%q_flag, &
sqsc => tfourier%sqsc, &
kve => tfourier%kve, &
nve => tfourier%nve, &
dimq => tfourier%dimq, &
unit_d => tfourier%unit_d, &
cutoff => tfourier%cutoff, &
cut_flag => tfourier%cut_flag, &
ave_times => tfourier%ave_times, &
natom => tcon%natom, &
la => tcon%la, &
ra => tcon%ra &
)
if(cut_flag) then
counti = 0
countn = 0
nn(1) = 0
loop1: do while (nn(1) < nve)
nn(1) = nn(1) + 1
nn(2) = nn(1)
loop2: do while (nn(2) < nve)
nn(2) = nn(2) + 1
counti = counti + 1
if(q_flag(counti)) then
countn = countn + 1
if(countn > dimq) exit loop1
rsq = 0.d0
isq = 0.d0
kk = dble(nn) * kve
do i = 1, natom
qra = sum(kk * ra(:,i))
cosqra = cos(qra)
sinqra = sin(qra)
rsq = rsq + cosqra
isq = isq + sinqra
end do
sqsc(countn) = sqsc(countn) + rsq**2 + isq**2
end if
end do loop2
end do loop1
else
countn = 0
nn(1) = 0
do while (nn(1) < nve)
nn(1) = nn(1) + 1
nn(2) = nn(1)
do while (nn(2) < nve)
nn(2) = nn(2) + 1
countn = countn + 1
rsq = 0.d0
isq = 0.d0
kk = dble(nn) * kve
do i = 1, natom
qra = sum(kk * ra(:,i))
cosqra = cos(qra)
sinqra = sin(qra)
rsq = rsq + cosqra
isq = isq + sinqra
end do
sqsc(countn) = sqsc(countn) + rsq**2 + isq**2
end do
end do
end if
ave_times = ave_times + 1
end associate
end if
end subroutine
subroutine outp_fourier( tfourier, tfile_sq )
!
! output s(|q|), if tbin presents, average s(|q|) according to tbin.
!
implicit none
! para list
type(tpfourier), intent(inout) :: tfourier
character(250), intent(inout) :: tfile_sq
! local
real(8), allocatable, dimension(:) :: out_qsc
real(8), allocatable, dimension(:) :: out_sqsc ! the structure factor s(|q|)
integer :: i, counti
real(8) :: maxqsc, temp_sqsc, temp_qsc
associate( &
qsc => tfourier%qsc, &
sqsc => tfourier%sqsc, &
qord => tfourier%qord, &
nve => tfourier%nve, &
dimq => tfourier%dimq, &
bin => tfourier%bin, &
bin_flag => tfourier%bin_flag, &
ave_times => tfourier%ave_times, &
ave_num => tfourier%ave_num &
)
if(ave_times < 1) then
print *, "fail to output, no calculation of fourier is done"
return
end if
ave_num = ave_num * ave_times
sqsc = sqsc / dble(ave_num)
allocate( out_qsc(dimq) )
allocate( out_sqsc(dimq) )
out_qsc = 0.d0
out_sqsc = 0.d0
do i = 1, dimq
out_qsc (i) = qsc( qord(i) )
out_sqsc(i) = sqsc( qord(i) )
end do
if ( bin_flag ) then
maxqsc = out_qsc(1) * 0.9d0
counti = 0
temp_qsc = 0.d0
temp_sqsc = 0.d0
open(2, file = tfile_sq)
do i = 1, dimq
if(out_qsc(i) > maxqsc) then
if(i > 1) then
temp_qsc = temp_qsc / dble(counti)
temp_sqsc = temp_sqsc / dble(counti)
write(2,*) temp_qsc, temp_sqsc
end if
counti = 0
temp_qsc = 0.d0
temp_sqsc = 0.d0
maxqsc = maxqsc + bin
end if
temp_qsc = temp_qsc + out_qsc(i)
temp_sqsc = temp_sqsc + out_sqsc(i)
counti = counti + 1
end do
close(2)
else
open(1, file = tfile_sq)
do i = 1, dimq
write(1,*) out_qsc(i), out_sqsc(i)
end do
close(1)
end if
deallocate( out_qsc )
deallocate( out_sqsc )
end associate
end subroutine
pure function calc_unit_length( this, tcon ) result(unit_d)
!
! unit_d is the average diameter, the unit of length
!
implicit none
! para list
class(tpfourier), intent(in) :: this
type(con_t), intent(in) :: tcon
! result
real(8) :: unit_d
! local
real(8) :: sdisk
associate( &
r => tcon%r, &
natom => tcon%natom &
)
sdisk = sum((2.d0*r)**free)
sdisk = sdisk / dble(natom)
unit_d = sdisk ** (1.d0 / dble(free))
end associate
end function
end module