-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm_metropolis.f90
557 lines (495 loc) · 15.1 KB
/
m_metropolis.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
!----------------------------------------------------------------------
! MELQUIADES: Metropolis Monte Carlo Program
!----------------------------------------------------------------------
!bop
! !Module: m_metropolis
!
! !Description: This module is the core of program. Here the Metropolis Monte Carlo algorithm is
!implemented.
!
!A general scheme in the calculation routines is:
!
!\begin{center}
!\begin{verbatim}
!Inital position in 'Markov chain' {Loop in total number of step}
! call translation components (atoms - molecules - groups)
! Evaluate energy
! call rotation components
! Evaluate energy
! call volume modification
! Evaluate energy
! call exchage components
! Evaluate energy
! call internal degree evaluation
! Evaluate energy
! ....
! call other process
! Evaluate energy
! End position in 'Markov chain' {Loop in number step}
!\end{verbatim}
!\end{center}
!\\
!\\
! !Interface:
!
module m_metropolis
!
! !Uses:
!
use m_kind
use m_unit
use m_simtype
use m_boxtype
use m_random
use m_rotation
use m_pbc
use m_zeros
use m_interaction
use m_uplist
use m_averages
use m_sampler
use m_store
use m_isobaric
use m_plots
use m_configuration
use m_correlation
use m_shift
use m_error
implicit none
!
! !Public member functions:
!
private
!
public :: r_markov, r_metropolis
!
! !Revision history:
! 06Aug 2015 Asdrubal Lozada
!
! !Remarks:
! It's advised to avoid the writting of formatted files during run time.
!
!eop
!----------------------------------------------------------
contains
!
!bop
!
! !Iroutine: r_metropolis
!
! !Description: In this routine a Markov chain is builded.
!\\
!\\
! !Interface:
subroutine r_metropolis( edge, engconf, virconf, t, y, x )
!
implicit none
! !Input parameters:
type(simulation), intent(inout) :: y
type(box), pointer :: x
type(temporary), intent(inout) :: t
real(rkind), dimension(:), intent(inout) :: edge
real(rkind), intent(inout) :: engconf
real(rkind), intent(inout) :: virconf
!
! !Revision history:
! 10Aug 2015 Asdrubal Lozada
!
!eop
!----------------------------------------------------------------------
integer :: naccn, attempt, step
integer :: ncycl, nmoves, nsamp
integer, parameter :: iseed = 86456
integer :: acm ! accumulate
real(rkind) :: enp, press
real(rkind) :: rho
real(rkind) :: apv
real(rkind) :: start_time, stop_time
integer :: ifreq, jfreq, iter, icfreq, iter0
!---------
! Verify variables
integer :: istate
integer :: var
real(rkind) :: tol_loc
!---------
istate = 0
iter = 0
iter0 = 11
naccn = 0
attempt = 0
nmoves = 1
nsamp = y%m_mxmol
acm = 0
var = 2
tol_loc = 0.001_rkind
!--------------------
! Frequeny for screening
if( y%m_nsteps > 10 ) then
ifreq = int(y%m_nsteps / 10)
else
ifreq = 5
end if
! Frequency for writting data
if( y%m_nsteps > 5 .and. y%m_nsteps < 100 ) then
jfreq = int(y%m_nsteps / 10)
elseif(y%m_nsteps > 100) then
jfreq = int(y%m_nsteps / 1000)
else
jfreq = 5
end if
! Frequency to finite difference
if( y%m_nsteps > 5 .and. y%m_nsteps < 100 ) then
icfreq = int(y%m_nsteps / 11)
elseif(y%m_nsteps > 100) then
icfreq = int(y%m_nsteps / 11)
else
icfreq = 5
end if
! Temporary files
open(intemp,status='scratch')
open(ixyz,file='file.xyz',status='unknown')
write(*,*)'--------------------------------------------------------------------------------'
write(*,*)' Start of Markov Chain '
if(y%m_averag) then
write(*,*)' Sampling '
write(*,*)' Cycle Energy per molecule[kcal/mol] Density[g/cm³] '
else
write(*,*)' Thermalization '
write(*,*)' Cycle Energy per molecule[kcal/mol] Density[g/cm³] '
end if
write(*,*)'--------------------------------------------------------------------------------'
ncycl = y%m_nsteps
call cpu_time(start_time)
do step = 1, ncycl ! Start Markov chain
call r_markov(edge, engconf, virconf, apv, step, t, y, x)
!*! [without save averages]
if( .not.y%m_averag ) then
call r_samples(intemp, step, iter, engconf, virconf, enp, rho, press, apv, t ,y )
!*
if( mod(step,ifreq) == 0 ) then
write(*,'(1x,i10,a8,i10,a7,2e20.9)')step, ' out of ', ncycl, ' : ', enp, rho
end if
end if
! Sampling for averages
if( y%m_averag ) then
call r_samples(intemp, step, iter, engconf, virconf, enp, rho, press, apv, t, y )
if( mod(step,ifreq) == 0 ) then
write(*,'(1x,i10,a8,i10,a7,2e20.9)')step, ' out of ', ncycl, ' : ', enp, rho
end if
if( mod(step,jfreq) == 0 ) then
call r_saves(engconf, virconf,y,x)
end if
end if
!!! call r_saves(engconf,y,x)
!-----------------------------------------
! Shift volume
if( y%m_ensbls=='nvt' .and. y%m_corr) then
if( y%m_varold < tol_loc) then
if( mod(step,icfreq) == 0) then
call r_shift( edge, iter0, y, x)
end if
end if
end if
!------------------------------------------
end do ! End Markov chain
if( mod(step,jfreq) == 0 ) then
call r_saves(engconf, virconf,y,x)
end if
call cpu_time(stop_time)
write(*,*)'--------------------------------------------------------------------------'
write(*,*)' End of Markov Chain '
write(*,*)'--------------------------------------------------------------------------'
write(*,5000) (stop_time - start_time)
write(*,*)'--------------------------------------------------------------------------'
write(*,*)'--------------------------------------------------------------------------'
! if( y%m_corr .and. .not.y%m_averag) then
call r_corr( y, var, iter, 1 )
! end if
!! if( y%m_averag ) then
call r_averages(t, y, x)
!! end if
call r_saves( engconf, virconf, y, x )
call r_newbox( y, x )
if(y%m_plot) then
call r_plot( y )
end if
5000 format(' Total time in Markov chain : ',e20.10,' seconds')
end subroutine r_metropolis
!
!bop
!
! !Iroutine: r_markov
!
! !Description: This routine.
!\\
!\\
! !Interface:
subroutine r_markov( edge, engconf, virconf, apv, steps, t, y, x )
!
implicit none
!
! !Input parameters:
type(simulation), intent(inout) :: y
type(box), pointer :: x
type(temporary), intent(inout) :: t
real(rkind), dimension(:), intent(in) :: edge
real(rkind), intent(inout) :: engconf
real(rkind), intent(inout) :: virconf
real(rkind), intent(out) :: apv
integer, intent(in) :: steps
!
! !Revision history:
! 10Aug 2015 Asdrubal Lozada
!
!eop
!---------------------------------------------------------------------
! Local variables
integer :: i_d, i
real(rkind), dimension(3) :: com_old
real(rkind), dimension(3) :: com_new
real(rkind) :: e_old, e_new
real(rkind) :: v_old, v_new
integer :: ido, idn
real(rkind) :: deltv, deltvb
real(rkind) :: deltw
integer :: accept
integer :: jb
logical :: logic, logic2
integer :: l
real(rkind) :: eng_temp
real(rkind) :: xi
!--------------------------------------------------
deltv = 0.0_rkind
deltvb = 0.0_rkind
deltw = 0.0_rkind
apv = 0.0_rkind
ido = 0
idn = 1
jb = 1
if(.not.y%m_solute) then
xi = m_rand()
!100 i_d = int(y%m_mxmol * m_rand()) + 1
100 i_d = int(y%m_mxmol * xi) + 1
if(i_d > y%m_mxmol) goto 100
accept = 0
!--------------------------------------------------
com_old(1) = x%m_cmass(1,i_d)
com_old(2) = x%m_cmass(2,i_d)
com_old(3) = x%m_cmass(3,i_d)
!---------------------------------------------------
! Backup com position
x%m_rcom(1,i_d) = x%m_cmass(1,i_d)
x%m_rcom(2,i_d) = x%m_cmass(2,i_d)
x%m_rcom(3,i_d) = x%m_cmass(3,i_d)
!----------------------------------------------------
! Start target site
do i = 1, x%m_ns(i_d)
x%m_rot(1,i) = x%m_site(1,i,i_d)
x%m_rot(2,i) = x%m_site(2,i,i_d)
x%m_rot(3,i) = x%m_site(3,i,i_d)
end do
!----------------------
! Backup site position
do i = 1, x%m_ns(i_d)
x%m_rsite(1,i) = x%m_site(1,i,i_d)
x%m_rsite(2,i) = x%m_site(2,i,i_d)
x%m_rsite(3,i) = x%m_site(3,i,i_d)
end do
!-------------------------
call r_interactions( com_old, edge, i_d, jb, ido, e_old, v_old, t, y, x )
!---------------------------------------------------
! Transformation matrix in R3 apply
!-------------------------------------------------------------------------------
com_new(1) = (com_old(1) + (2.0d0 * m_rand() - 1.0d0) * y%m_transl)
com_new(2) = (com_old(2) + (2.0d0 * m_rand() - 1.0d0) * y%m_transl)
com_new(3) = (com_old(3) + (2.0d0 * m_rand() - 1.0d0) * y%m_transl)
!------------------------------------------------------------------------------
! Pick up central image
if( y%m_pbcs ) then
call r_pbc(com_new, edge)
end if
!---------------------------------------------------------------------
call r_euler(i_d,y,x)
!--------------------------------------------------------------------
call r_interactions( com_new, edge, i_d, jb, idn, e_new, v_new, t, y, x )
!-------------------------------------------------------------
!boc
deltv = e_new - e_old ! Energy variation
deltw = v_new - v_old ! Virial variation
deltvb = deltv * y%m_beta
!-----------------------------
if(deltv < 10.0d0) then
if(deltv <= 0.0d0) then
accept = 1
t%m_move = t%m_move + 1
else if(exp(-deltvb) > m_rand()) then
accept = 1
t%m_move = t%m_move + 1
end if
end if
!eoc
if(accept == 1) then
engconf = engconf + deltv
virconf = virconf + deltw
!-----------------
end if
t%m_imove = t%m_imove + accept
t%m_amv = t%m_amv + 1
!-------------------------------------------------------------
call r_update(com_new, i_d, accept, y, x)
!-------------------------------------------------------------
if(y%m_ensbls == 'npt') then
if( mod (steps, y%m_ifvol) == 0 ) then
call r_npt(engconf, virconf, apv, t, y, x)
end if
end if
! End conventional calculation
!--------------------------------------------------------------
!* else
!---
!*200 i_d = int(y%m_mxmol * m_rand()) + 1
!* if(i_d > y%m_mxmol) goto 200
!*
!* logic2 = .false.
!--------------------------------------------------
!* do l = 1, y%m_iexcl
!* if(x%m_idtype(i_d) == x%m_extype(l)) then
!* logic2 = .true.
!* exit
!* end if
!* end do ! l
!---------------------------------------------------
!* if(logic2) then
!* com_old(1) = x%m_cmass(1,i_d)
!* com_old(2) = x%m_cmass(2,i_d)
!* com_old(3) = x%m_cmass(3,i_d)
!---------------------------------------------------
! Backup com position
!* x%m_rcom(1,i_d) = x%m_cmass(1,i_d)
!* x%m_rcom(2,i_d) = x%m_cmass(2,i_d)
!* x%m_rcom(3,i_d) = x%m_cmass(3,i_d)
!----------------------------------------------------
!* do i = 1, x%m_ns(i_d)
!* x%m_rot(1,i) = x%m_site(1,i,i_d)
!* x%m_rot(2,i) = x%m_site(2,i,i_d)
!* x%m_rot(3,i) = x%m_site(3,i,i_d)
!* end do ! i
!----------------------
! Backup site position
!* do i = 1, x%m_ns(i_d)
!* x%m_rsite(1,i) = x%m_site(1,i,i_d)
!* x%m_rsite(2,i) = x%m_site(2,i,i_d)
!* x%m_rsite(3,i) = x%m_site(3,i,i_d)
!* end do
!-------------------------
!* call interactions(com_old, edge, i_d, jb, ido, e_old, t, y, x)
!----
! Transformation matrix in R3 apply
!* com_new(1) = (com_old(1) + (2.0d0 * m_rand() - 1.0d0) * y%m_transl)
!* com_new(2) = (com_old(2) + (2.0d0 * m_rand() - 1.0d0) * y%m_transl)
!* com_new(3) = (com_old(3) + (2.0d0 * m_rand() - 1.0d0) * y%m_transl)
!-----------
! Pick up central image
!* call pbc(com_new, edge)
!------
!* call rotation(i_d, y, x)
!------
!* call interactions(com_new, edge, i_d, jb, idn,e_new, t, y, x)
!---------
!* deltv = e_new(1) - e_old(1) ! Energy variation
!* deltvb = deltv * y%m_beta
!-------
!* if(deltv < 75.0d0) then
!* if(deltv <= 0.0d0) then
!* accept = 1
!* else if(exp(-deltvb) > m_rand()) then
!* accept = 1
!* end if
!* end if
!* if(accept == 1) then
!* engconf = engconf + deltv
!* enthalpy = enthalpy + (e_new(1) + y%m_ppcon * y%m_vol)-(e_old(1) + y%m_ppcon * y%m_vol)
! y%m_virial = y%m_virial + (vir_new-vir_old)
!* end if
!* call update3(com_new, i_d, accept, y, x)
!* end if ! logic
!----------------------
! -----------------------
! Non-exclude types
!--------------------------------------
!* logic = .false.
!--------------------------------------------------
!* do l = 1, y%m_iexcl
!* if(x%m_idtype(i_d) == x%m_extype(l)) then
!* logic = .true.
!* exit
!* end if
!* end do ! l
!---------------------------------------------------
!* if(.not.logic) then
!* y%m_solute = .false.
!* com_old(1) = x%m_cmass(1,i_d)
!* com_old(2) = x%m_cmass(2,i_d)
!* com_old(3) = x%m_cmass(3,i_d)
!---------------------------------------------------
! Backup com position
!* x%m_rcom(1,i_d) = x%m_cmass(1,i_d)
!* x%m_rcom(2,i_d) = x%m_cmass(2,i_d)
!* x%m_rcom(3,i_d) = x%m_cmass(3,i_d)
!----------------------------------------------------
!* do i = 1, x%m_ns(i_d)
!* x%m_rot(1,i) = x%m_site(1,i,i_d)
!* x%m_rot(2,i) = x%m_site(2,i,i_d)
!* x%m_rot(3,i) = x%m_site(3,i,i_d)
!* end do ! i
!----------------------
! Backup site position
!* do i = 1, x%m_ns(i_d)
!* x%m_rsite(1,i) = x%m_site(1,i,i_d)
!* x%m_rsite(2,i) = x%m_site(2,i,i_d)
!* x%m_rsite(3,i) = x%m_site(3,i,i_d)
!* end do
!-------------------------
!* call interactions(com_old, edge, i_d, jb, ido, e_old, t, y, x)
!----
! Transformation matrix in R3 apply
!* com_new(1) = (com_old(1) + (2.0d0 * m_rand() - 1.0d0) * y%m_transl)
!* com_new(2) = (com_old(2) + (2.0d0 * m_rand() - 1.0d0) * y%m_transl)
!* com_new(3) = (com_old(3) + (2.0d0 * m_rand() - 1.0d0) * y%m_transl)
!-----------
! Pick up central image
!* call pbc(com_new, edge)
!------
!* call rotation(i_d, y, x)
!------
!* call interactions(com_new, edge, i_d, jb, idn, e_new, t, y, x)
!---------
!* deltv = e_new(1) - e_old(1) ! Energy variation
!* deltvb = deltv * y%m_beta
!-------
!* if(deltv < 75.0d0) then
!* if(deltv <= 0.0d0) then
!* accept = 1
!* else if(exp(-deltvb) > m_rand()) then
!* accept = 1
!* end if
!* end if
!* if(accept == 1) then
!* engconf = engconf + deltv
!* enthalpy = enthalpy + (e_new(1) + y%m_ppcon * y%m_vol)-(e_old(1) + y%m_ppcon * y%m_vol)
! y%m_virial = y%m_virial + (vir_new-vir_old)
!* end if
!* call update2(com_new, i_d, accept, y, x)
!* end if ! logic
!-------------------------------------------------------------
!* if(y%m_ensbls == 'npt') then
!* if( mod (steps, y%m_ifvol) == 0 ) then
!* call npt(engconf, enthalpy, t, y, x)
!* end if
!* end if
!----------------------------------------------------------
!* y%m_solute = .true.
end if ! Active
!------------------------------------------------------------
end subroutine r_markov
end module m_metropolis