-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdmain.f90
675 lines (525 loc) · 15.9 KB
/
mdmain.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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
subroutine qc_md_run (irst)
use qc_system
use qc_mpi
use qc_bim
implicit none
integer, intent(in) :: irst
real*8 :: temp, umon, udim, ulr
integer :: istep, istart0
if (sys_master) then
open (45, file='trajectory.xyz', access='append', status='unknown')
end if
call md_nose_init
istart0 = 0
istep = 0
if (irst .eq. 0) then
call read_system
call md_vel_init
else
call md_restart (istart0)
end if
!-- Parrallel JOB
call qc_mpi_barrier
call qc_mpi_real_bcast (gm_pos, 3*natom) ! makes sense if we're doing restart
if (irst .eq. 0) then
! Obtain Gradient
call qc_bim_pot (umon, udim, ulr) ! If RESPA is used ==> umon and udim correspond to RHF
istep = 0
!if (sys_master) then
! call md_kinen
! call md_report (istep+istart0, umon, udim, ulr, temp)
!end if
end if
!--- begin MD procedure
do istep = 1, nprod
if ( .not. use_respa ) then
call md_integrate (umon, udim, ulr)
else
call md_integrate_respa (umon, udim, ulr)
end if
if (sys_master) then
call md_report (istep+istart0, umon, udim, ulr, temp)
if (mod(istep+istart0, nsave) .eq. 0) &
call md_save (istep+istart0)
if (mod(istep+istart0,5) .eq. 0) then
call md_trajectory (istep+istart0, umon+udim+ulr, temp)
end if
end if
end do
if (sys_master) close (45)
end subroutine qc_md_run
subroutine md_integrate (Umon, Udim, Ulr)
use qc_system
use consts
use qc_mpi
use qc_bim
implicit none
real*8, intent(out) :: Umon, Udim, Ulr
integer :: iat
real*8 :: dt2
real*8, dimension(3) :: arr, vrr
dt2 = 0.5d0*dt
if (sys_master) then
call md_nose_chain (dt) ! integrates from t = 0 to t = dt / 2. !
do iat = 1, natom
arr(1:3) = -d_rr(1:3,iat) / at_mass(iat)
vrr(1:3) = vel(1:3,iat) + arr(1:3)*dt2
gm_pos(1:3,iat) = gm_pos(1:3,iat) + dt*vrr(1:3)*bohr2ang
vel(1:3,iat)= vrr(1:3)
end do
call md_pbc
end if
!--- Velocity Verlet algorithm
call qc_mpi_barrier ()
call qc_mpi_real_bcast (gm_pos, 3*natom)
call qc_bim_pot (Umon, Udim, Ulr)
if (sys_master) then
do iat = 1, natom
arr(1:3) = -d_rr(1:3,iat)/at_mass(iat)
vel(1:3,iat) = vel(1:3,iat) + dt2*arr(1:3)
end do
call md_nose_chain (dt)
end if
end subroutine md_integrate
subroutine md_integrate_respa (Umon, Udim, Ulr)
use qc_system
use consts
use qc_mpi
use qc_bim
implicit none
real*8, intent(out) :: Umon, Udim, Ulr
integer :: iat, istep
real*8 :: dt2, dti, dti2
real*8, dimension(3) :: arr, vrr
dt2 = 0.5d0*dt
dti = dt / nresp
dti2 = 0.5d0 * dti
if (sys_master .and. xorespa) call md_nose_chain (dt)
do istep = 1, nresp
if (sys_master) then
if (xirespa) call md_nose_chain (dti)
if (istep .eq. 1) then
use_respa = .false.
theory='scf' ! should save the old theory string somewhere
do iat = 1, natom
arr(1:3) = -corr_d_rr(1:3,iat) / at_mass(iat)
vrr(1:3) = vel(1:3,iat) + arr(1:3)*dt2
vel(1:3,iat)= vrr(1:3)
end do
end if
do iat = 1, natom
arr(1:3) = -d_rr(1:3,iat) / at_mass(iat)
vrr(1:3) = vel(1:3,iat) + arr(1:3)*dti2
gm_pos(1:3,iat) = gm_pos(1:3,iat) + dti*vrr(1:3)*bohr2ang
vel(1:3,iat)= vrr(1:3)
end do
call md_pbc
if (istep .eq. nresp) then
use_respa = .true.
theory='mp2'
end if
end if
call qc_mpi_barrier ()
call qc_mpi_real_bcast (gm_pos, 3*natom)
call qc_mpi_logical_bcast1 (use_respa)
call qc_mpi_char_bcast (theory)
call qc_bim_pot (Umon, Udim, Ulr)
if (sys_master) then
do iat = 1, natom
arr(1:3) = -d_rr(1:3,iat) / at_mass(iat)
vrr(1:3) = vel(1:3,iat) + arr(1:3)*dti2
vel(1:3,iat)= vrr(1:3)
end do
if (istep .eq. nresp) then
do iat = 1, natom
arr(1:3) = -corr_d_rr(1:3,iat) / at_mass(iat)
vrr(1:3) = vel(1:3,iat) + arr(1:3)*dt2
vel(1:3,iat)= vrr(1:3)
end do
end if
if (xirespa) call md_nose_chain(dti)
end if
end do
if (sys_master .and. xorespa) call md_nose_chain (dt)
end subroutine md_integrate_respa
subroutine md_nose_init
use qc_system
use consts !
implicit none
real(8) :: omega_system, omega2
integer :: i
allocate(qmass(nnos))
allocate(xnh(nnos))
allocate(vnh(nnos))
allocate(gnh(nnos))
Ekin0 = temp0*boltz ! K-> A.U.
gfree0 = 3.0d0 * natom
xnh = 0.0d0
vnh = 0.0d0
!time_system = 8.881d0/au_time ! fs --> A.U.
time_system = 20.000d0/au_time ! fs --> A.U. -- why? AK
omega_system = 2.0d0*pi/time_system
omega2 = omega_system**2
qmass(1) = gfree0 * Ekin0 / omega2
qmass(2:nnos) = Ekin0 / omega2
gnh = -1.0d0 * Ekin0 / qmass
glogv = 0.0d0
vlogv = 0.0d0
xlogv = 0.0d0
pmass = (gfree0 + 3.0d0) * Ekin0 * 0.1d0 !* 100.0d0
xirespa = .false.
xorespa = .true.
return
end subroutine md_nose_init
subroutine md_nose_chain (tau)
! Propagates Nose - Hoover chain from t = 0 to t = ___tau/2___
use qc_system
use consts
implicit none
real*8, intent(in) :: tau
integer :: iat, iresn, iyosh, inos
real*8 :: scale, aa
wdti2 = tau / 2.0d0 / nresn * wyosh
wdti4 = wdti2 / 2.0d0
wdti8 = wdti4 / 2.0d0
call md_kinen ! resets pmom2 and en_kin ==> why do we need pmom2 update at the end of the subroutine?
scale = 1.0D0
gnh(1) = (2.0d0*en_kin - gfree0*Ekin0)/qmass(1)
if (ensemble_nvt) then
do iresn = 1, nresn
do iyosh = 1, nyosh
vnh(nnos) = vnh(nnos) + gnh(nnos) * wdti4(iyosh)
! Update thermostat velocities
do inos = nnos - 1, 1, -1
aa = dexp(-wdti8(iyosh) * vnh(inos + 1))
vnh(inos) = vnh(inos) * aa * aa + wdti4(iyosh) * gnh(inos) * aa
end do
! Update thermostat coordinates
do inos = 1, nnos
xnh(inos) = xnh(inos) + vnh(inos) * wdti2(iyosh)
end do
scale = scale * dexp(-wdti2(iyosh) * vnh(1))
gnh(1) = (2.0d0*en_kin*scale*scale - gfree0*Ekin0)/qmass(1)
do inos = 1, nnos - 1
aa = dexp(-wdti8(iyosh) * vnh(inos + 1))
vnh(inos) = vnh(inos) * aa * aa + wdti4(iyosh) * gnh(inos) * aa
gnh(inos + 1) = (qmass(inos) * vnh(inos) * vnh(inos) - Ekin0)/qmass(inos + 1)
end do
vnh(nnos) = vnh(nnos) + gnh(nnos) * wdti4(iyosh)
end do
end do
end if
do iat = 1, natom
vel(1:3,iat) = vel(1:3, iat)*scale
end do
pmom2 = pmom2 * scale*scale
return
end subroutine md_nose_chain
subroutine md_kinen
use qc_system
use consts
implicit none
integer :: iat
integer :: i, j
pmom2 = 0.0d0
do iat = 1, natom
do j = 1,3
do i = 1,3
pmom2(i,j) = pmom2(i,j) + at_mass(iat)*vel(i,iat)*vel(j,iat)
end do
end do
end do
en_kin = 0.5d0 * (pmom2(1,1) + pmom2(2,2) + pmom2(3,3))
return
end subroutine md_kinen
subroutine md_save (istep)
use qc_system
use qc_mpi
implicit none
integer, intent(in) :: istep
integer :: iat, i
if (sys_master) then
open (10, file='mdrr.sav', status='unknown')
open (11, file='mdrr.xyz', status='unknown')
write (10, *) natom
write (10, '(i9)') istep
write (11, *) natom
write (11, '(i9)') istep
! Coordinate
do iat = 1, natom
write (10, *) at_atnm(iat), gm_pos(1:3,iat)
write (11, *) at_atnm(iat), gm_pos(1:3,iat)
end do
! Velocity
do iat = 1, natom
write (10, *) vel(1:3,iat)
end do
! Gradient
do iat = 1, natom
write (10, *) d_rr(1:3,iat)
end do
!do iat = 1, natom
! write (10, *) at_chg(iat)
!end do
! Nose-Hoover Temperature
do i = 1, nnos
write (10, *) xnh(i), gnh(i), vnh(i), qmass(i)
end do
! Pressure
write (10, *) xlogv, glogv, vlogv, pmass
close(10)
close(11)
end if
end subroutine md_save
subroutine md_trajectory (istep,Upot,temp)
use qc_system
use consts
use qc_mpi
implicit none
integer, intent(in) :: istep
real*8, intent(in) :: Upot, temp
real*8 :: time_ps
integer :: iat
if (sys_master) then
time_ps = istep*dt*au_time/1.0e3 ! ps
write (45, *) natom
write (45, '(A,f14.6,A,f10.2,A,f9.3)') &
' Upot ', Upot, ' Temp ', temp, &
' Time(ps) ', time_ps
! Coordinate
do iat = 1, natom
write (45, '(A,3f16.8)') at_atnm(iat), gm_pos(1:3,iat)
end do
call flush(45)
end if
end subroutine md_trajectory
subroutine md_restart (istart)
use qc_system
use qc_mpi
implicit none
integer, intent(out) :: istart
integer :: iat, i
if (sys_master) then
open (10, file='./mdrr.sav', status='old')
read (10, *) !natom
read (10, *) istart
! Coordinate
do iat = 1, natom
read (10, *) at_atnm(iat), gm_pos(1:3,iat)
end do
do iat = 1, natom
read (10, *) vel(1:3, iat)
end do
do iat = 1, natom
read (10, *) d_rr(1:3, iat)
end do
!do iat = 1, natom
! read (10, *) at_chg(iat)
!end do
do i = 1, nnos
read (10, *) xnh(i), gnh(i), vnh(i), qmass(i)
end do
! Pressure
read (10, *) xlogv, glogv, vlogv, pmass
close(10)
call md_kinen
end if
call qc_mpi_barrier
call qc_mpi_int_bcast1 (istart)
!call qc_mpi_real_bcast (at_chg, natom)
call qc_mpi_real_bcast (d_rr, 3*natom) ! ?? Propagation is always performed by master thread
end subroutine md_restart
subroutine md_vel_init
use qc_system
use consts
use qc_mpi
implicit none
integer :: iat
real*8 :: vxx, vyy, vzz
real*8 :: rand_f
real*8 :: tmp, scale
real*8, dimension(3) :: pmom
if (sys_master) then
rand_f = 3.0d0**15
scale = sqrt(Ekin0)
pmom = 0.0d0
do iat = 1, natom
call random (vxx, vyy, vzz,rand_f)
vel(1,iat) = vxx*scale
vel(2,iat) = vyy*scale
vel(3,iat) = vzz*scale
pmom = pmom + at_mass(iat)*vel(1:3,iat)
end do
pmom = pmom / natom
! correct velocities
do iat = 1, natom
vel(1:3,iat) = vel(1:3,iat) - pmom(1:3)/at_mass(iat)
end do
call md_kinen
tmp = 2.0D0*en_kin/(boltz*gfree0)
scale = sqrt (temp0/tmp)
do iat = 1, natom
vel(1:3,iat) = vel(1:3,iat)*scale
end do
end if
end subroutine md_vel_init
subroutine random (vxx, vyy, vzz, rand_f)
implicit none
real*8 :: vxx, vyy, vzz
real*8 :: rand_b, rand_f, rand_r, rand_ri
real*8 :: ra, re, rg, pid
rand_b = 5.0d0**3
rand_r = 2.0d0**24
rand_ri = 1.0d0/rand_r
pid = 6.28318530717958647692528677d0
ra = rand_b * rand_f
rand_f=ra-dint(ra*rand_ri)*rand_r
re=rand_f*rand_ri
ra=rand_b*rand_f
rand_f=ra-dint(ra*rand_ri)*rand_r
rg=rand_f*rand_ri
vxx=sqrt(-2.0d0*log(re))*sin(pid*rg)
ra=rand_b*rand_f
rand_f=ra-dint(ra*rand_ri)*rand_r
re=rand_f*rand_ri
ra=rand_b*rand_f
rand_f=ra-dint(ra*rand_ri)*rand_r
rg=rand_f*rand_ri
vyy=sqrt(-2.0d0*log(re))*sin(pid*rg)
ra=rand_b*rand_f
rand_f=ra-dint(ra*rand_ri)*rand_r
re=rand_f*rand_ri
ra=rand_b*rand_f
rand_f=ra-dint(ra*rand_ri)*rand_r
rg=rand_f*rand_ri
vzz=sqrt(-2.0d0*log(re))*sin(pid*rg)
end subroutine random
subroutine md_report (istep, Umon, Udim, Ulr, temp)
use qc_system
use qc_lattice
use consts
use qc_mpi
implicit none
integer, intent(in) :: istep
real*8, intent(in) :: Umon, Udim, Ulr
real*8, intent(out):: temp
real*8 :: Upot, Ten, scale
real*8 :: time_ps, press
real*8 :: vol, qkin, qpot ! kinetic and potential energy of therm
integer :: i, j
if (sys_master) then
Upot = Umon + Udim + Ulr
Ten = Upot + en_kin
temp = 2.0d0*en_kin/(boltz*gfree0)
! Ang --> Bohr
vol = volume(lat)*ang2bohr**3 !M!
do i = 1,3
do j = 1,3
Pr(i,j) = (pmom2(i,j) + virt(i,j))/vol
end do
end do
press = (Pr(1,1)+Pr(2,2)+Pr(3,3))/3.0d0 * au_bar ! E_h/bohr**3 --> Bar
time_ps = istep*dt*au_time/1.0e3 ! ps
if (ensemble_nvt) then
qkin = 0.5d0 * sum(qmass * vnh * vnh)
qpot = gfree0* Ekin0 * xnh(1) + Ekin0 * sum(xnh(2:nnos))
Ten = Ten + qkin + qpot ! Conserved quantity for N-H dynamics
end if
!write (6,'(I8,F10.3,5F14.6,F10.2)') &
write (6,'(I8,F12.5,5F14.6,F10.2,F12.2)') & ! AK : modified format
istep, time_ps,Upot/Nmol, Umon/Nmol, Udim/Nmol, Ulr/Nmol, &
Ten/Nmol, temp, press
! end if
if (is_temp_tol) then ! Berendsen thermostat -- shouldn't be used except for equilibration runs
!if (.false.) then
if (temp .lt. temp0 - 50.0d0 .or. temp .gt. temp0 + 50.0d0) then
scale = sqrt(temp0/temp)
vel = vel * scale
end if
end if
call flush(6)
end if
end subroutine md_report
subroutine md_pbc
use qc_system
implicit none
integer :: iat, im, nsite, is
!real*8 :: tr
gm_pos = matmul(lati, gm_pos) ! scale coordinates
do im = 1, nmol ! fold molecules back into central cell
iat = mol_iatom(im)
nsite = mol_nsite(im)
! X
!if (gm_pos(1,iat+1) .ge. 1.0d0) then
! tr = 1.0d0 * int(gm_pos(1,iat+1))
! do is = 1, nsite
! gm_pos(1,iat+is) = gm_pos(1,iat+is) - tr
! end do
!end if
!if (gm_pos(1,iat+1) .lt. 0.0d0) then
! tr = 1.0d0 * int(gm_pos(1,iat+1)) + 1.0
! do is = 1, nsite
! gm_pos(1,iat+is) = gm_pos(1,iat+is) - tr
! end do
!end if
!
! Y
!if (gm_pos(2,iat+1) .lt. 0.0d0 .or. gm_pos(2,iat+1) .ge. 1.0d0) then
! tr = 1.0d0 * int(gm_pos(2,iat+1))
! do is = 1, nsite
! gm_pos(2,iat+is) = gm_pos(2,iat+is) - tr
! end do
!end if
!if (gm_pos(2,iat+1) .lt. 0.0d0) then
! tr = 1.0d0 * int(gm_pos(2,iat+1)) + 1.0
! do is = 1, nsite
! gm_pos(2,iat+is) = gm_pos(2,iat+is) - tr
! end do
!end if
!
! Z
!if (gm_pos(3,iat+1) .lt. 0.0d0 .or. gm_pos(3,iat+1) .ge. 1.0d0) then
! tr = 1.0d0 * int(gm_pos(3,iat+1))
! do is = 1, nsite
! gm_pos(3,iat+is) = gm_pos(3,iat+is) - tr
! end do
!end if
!if (gm_pos(3,iat+1) .lt. 0.0d0) then
! tr = 1.0d0 * int(gm_pos(3,iat+1)) + 1.0
! do is = 1, nsite
! gm_pos(3,iat+is) = gm_pos(3,iat+is) - tr
! end do
!end if
! X
if (gm_pos(1,iat+1) .lt. 0.0d0) then
do is = 1, nsite
gm_pos(1,iat+is) = gm_pos(1,iat+is) + 1.0d0
end do
else if (gm_pos(1,iat+1) .ge. 1.0d0) then
do is = 1, nsite
gm_pos(1,iat+is) = gm_pos(1,iat+is) - 1.0d0
end do
end if
! Y
if (gm_pos(2,iat+1) .lt. 0.0d0) then
do is = 1, nsite
gm_pos(2,iat+is) = gm_pos(2,iat+is) + 1.0d0
end do
else if (gm_pos(2,iat+1) .ge. 1.0d0) then
do is = 1, nsite
gm_pos(2,iat+is) = gm_pos(2,iat+is) - 1.0d0
end do
end if
! Z
if (gm_pos(3,iat+1) .lt. 0.0d0) then
do is = 1, nsite
gm_pos(3,iat+is) = gm_pos(3,iat+is) + 1.0d0
end do
else if (gm_pos(3,iat+1) .ge. 1.0d0) then
do is = 1, nsite
gm_pos(3,iat+is) = gm_pos(3,iat+is) - 1.0d0
end do
end if
end do
gm_pos = matmul(lat, gm_pos) ! unscale coordinates
end subroutine md_pbc