-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSamplingModule.f90
More file actions
1459 lines (1250 loc) · 48.6 KB
/
Copy pathSamplingModule.f90
File metadata and controls
1459 lines (1250 loc) · 48.6 KB
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
! Copyright Todd J. Martinez and Raphael D. Levine, 1994
!>
!! @brief Ground state sampling routines, including sampling Wigner, Husimi and Boltzmann distributions.
!<
module SamplingModule
use GlobalModule
use TrajectoryModule
use QM_MM_Module, only: qcNumQM
use RandomModule, only: fms_ranb
implicit none
private
public :: FMS_InitialWigner, FMS_InitialHusimi, FMS_InitialConstT
public :: FMS_InitialSwarm, FMS_InitialQuasi, FMS_InitialColdWig
! Integer parameter definitions:
integer(kind=DefInt), public, parameter :: &
NOSAMPLE = 0, &
WIGNER = 1, &
QUASICLASS = 2, &
BOLTZ = 3, &
COLDWIG = 4, &
SWARM = 5, &
HUSIMI = 6
!> System temperature
real(kind=DefReal), public :: indTemperature
!> Determines ground state sampling method
integer(kind=DefInt), public :: inInitialCond
logical, public :: inzFirstGauss
!> For ConstT initial conditions, number of special modes
integer(kind=DefInt), public :: inNTemp
!> For ConstT initial conditions, index of each special mode
integer(kind=DefInt), public :: inIModeSharp(100)
!> For ConstT initial conditions, temperature for each of the special modes
real(kind=DefReal), public :: indFModeSharp(100)
!> For quasiclassical initial conditions, number of modes with extra quanta
integer(kind=DefInt), public :: inNAddQuanta
!> For quasiclassical initial conditions, modes with extra quanta
integer(kind=DefInt), public :: inIAddQuanta(100)
contains
!>
!! @brief This routine samples initial conditions from a Wigner Distribution.
!! For each Normal mode we sample from:
!! \f[
!! \rho(x,p)=\exp\left[ -2\alpha_x(x-x0)^2 -\frac{(p-p_0)^2}{2*\alpha_x} \right]
!! \f]
!! where x, and p are the normal mode position and momentum, respectively.
!! The width \f$\alpha_x\f$ is Mass*Freq/2 [Heller JCP vol 80 p 5038 (1984)]
!! But Normal modes have units of sqrt[mass]*Length and therefore
!! \f$\alpha_x=Freq/2\f$.
!! If indTemperature /=0 then the width is defined as
!! AlphaX=Freq/2*Tanh(Beta*Freq/2) where Beta=1/(KbT)
!! See Heller JCP, vol, 65, pp. 1289 (1976)
!! This is the result for a Wigner at finite temperature
!! @see FMS_InitialColdWig
!! @ingroup initial
!<
subroutine FMS_InitialWigner(T1, IDNum)
type(T_Trajectory), intent(inout) :: T1
integer(kind=DefInt), intent(in) :: IDNum
real(DefReal), dimension(qcNumQM) :: Mass
real(DefReal), dimension(3*qcNumQM) :: freq
real(DefReal), dimension(3*qcNumQM, 3*qcNumQM) :: Umat
! this are all back to front!
real(DefReal), dimension(qcNumQM, 3) :: atomcen, P, R, Vel
real(DefReal) :: Beta
real(DefReal) :: rsq, dx1, dx2, fac, x1, x2, alphax, sigp, sigx, ft
real(kind=DefReal) :: PosVec(3 * qcNumQM)
real(kind=DefReal) :: Momvec(3 * qcNumQM)
real(kind=DefReal) :: TotMass, rhototal, RCM(3), VelCM(3)
integer(kind=DefInt) :: I, J, K, L, natoms, nfreq, IDim, IParticle
real(kind=DefReal) :: ZPE, Ekin
! Beta is 1/KbT
beta = 0.0d0
if (indTemperature > d0) then
write (fmiOut, *) 'Sampling at finite T =', indTemperature
Beta = d1 / (indTemperature * BoltzK)
end if
! Read initial positions
do i = 1, qcNumQM
atomcen(i, 1:3) = T1%Particle(i)%get_pos()
end do
! now we can allocate memory
natoms = qcNumQM
do i = 1, natoms
Mass(i) = T1%Particle(i)%Mass
end do
call read_frequencies(natoms, Mass, nfreq, freq, Umat)
! Start sampling
Ekin = 0.0d0
! If this is the first trajectory and inzFirstGauss is true place the
! basis function at the origin. It will be shifted to atomcen/atvel below.
if (inzFirstGauss .and. IDNum == 1) then
PosVec = 0.0d0
Momvec = 0.0d0
else
! Sample from a Gaussian distribution
rhototal = 1.0d0
do I = 1, nfreq
Alphax = freq(i) / 2.0d0
if (indTemperature <= d0) then
ft = 1.0d0
else
ft = tanh(Beta * freq(i) * dp5)
end if
Sigx = sqrt(1.0d0 / (4.0d0 * Alphax * ft))
Sigp = sqrt(1.0d0 * Alphax / ft)
1 continue
dx1 = 2.d0 * FMS_ranb(i4zero) - 1.d0
dx2 = 2.d0 * FMS_ranb(i4zero) - 1.d0
rsq = dx1 * dx1 + dx2 * dx2
if (rsq >= 1.d0 .or. rsq == 0.d0) goto 1
fac = sqrt(-2.d0 * log(rsq) / rsq)
x1 = dx1 * fac
x2 = dx2 * fac
! Each Normal mode is in Equilibrium in both position and
! mometum, i.e., x0 and p0 are zero so we just need to "stretch"
! the random numbers using the proper Sigma.
! Note that p is the momentum but the normal modes have a unit mass
! so we do not need to divide by the mass when we go to velocity
PosVec(I) = Sigx * x1
Momvec(I) = Sigp * x2
! For debugging purpose calculate Kinetic energy
Ekin = Ekin + Momvec(i) * Momvec(I)
end do
end if
! Kinetic energy in Normal-Modes
Ekin = Ekin * dp5
! multiply by U (transform to mass-weighted Cartesian--position/momentum)
L = 0
do I = 1, natoms
do J = 1, 3
L = L + 1
R(I, J) = d0
P(I, J) = d0
do K = 1, nfreq
R(I, J) = R(I, J) + UMat(L, K) * PosVec(K)
P(I, J) = P(I, J) + UMat(L, K) * MomVec(K)
end do
end do
end do
! un-mass-weight -- Position and Momentum
do I = 1, natoms
do J = 1, 3
R(I, J) = R(I, J) / sqrt(T1%Particle(I)%Mass)
P(I, J) = P(I, J) * sqrt(T1%Particle(I)%Mass)
end do
end do
! Adding the displacments to the Equilibrium structure
! determine velocity using the momentum
do i = 1, natoms
do j = 1, 3
R(I, J) = atomcen(I, J) + R(I, J)
Vel(I, J) = P(I, J) / T1%Particle(I)%Mass
end do
end do
! make sure that COM is at origin and remove COM velocity
RCM = d0
VelCM = d0
TotMass = d0
! compute the c.o.m and its velocity
do i = 1, natoms
totMass = TotMass + T1%Particle(i)%Mass
do j = 1, 3
RCM(j) = RCM(j) + T1%Particle(i)%Mass * R(i, j)
VelCM(j) = VelCM(j) + T1%Particle(i)%Mass * Vel(i, j)
end do
end do
RCM = RCM / TotMass
VelCM = VelCM / TotMass
! Remove COM velocity
! amv: Turned off COM removal - it can interfere with QM/MM
! amv: momentum removal is still cool, though
! Use Velocity to determine Momentum
do i = 1, natoms
do j = 1, 3
! R(i,j)=R(i,j)-rcm(j)
Vel(i, j) = Vel(i, j) - velcm(j)
P(I, J) = Vel(I, J) * T1%Particle(I)%Mass
end do
end do
call write_initial_condition(T1, natoms, R, P)
! Assign position and momentum to trajctory
do IParticle = 1, qcNumQM
do IDim = 1, 3
call T1%set_pos(IParticle, IDim, R(IParticle, IDim))
call T1%set_mom(IParticle, IDim, P(IParticle, IDim))
end do
end do
! Calculate the kinetic energy and compare it to the ZPE.
! Should be about 1/2 the ZPE.
! We also compare the kinetic energy using Normal Modes and Cartesian Coord.
write (fmiOut, *) 'Ekin Normal Modes:', real(Ekin)
! TODO: Use a common function to calculate Ekin for a Trajectory
EKin = 0.0d0
do I = 1, natoms
do J = 1, 3
EKin = EKin + P(i, j) * P(i, j) / T1%Particle(I)%Mass
end do
end do
EKin = dp5 * EKin
ZPE = get_zpe(freq, nfreq)
write (fmiOut, *) 'ZPE is: ', real(ZPE), 'Half ZPE is: ', real(dp5 * ZPE)
write (fmiOut, *) 'Kinetic Energy is: ', real(Ekin)
flush (fmiOut)
end subroutine FMS_InitialWigner
!>
!! Samples initial conditions from a Boltzmann distribution at a constant Temperature
!! @ingroup initial
!<
subroutine FMS_InitialConstT(T1)
type(T_Trajectory), intent(inout) :: T1
real(kind=DefReal) :: atcen(qcNumQM, 3)
real(kind=DefReal), dimension(3*qcNumQM, 3*qcNumQM) :: Umat
real(kind=DefReal), dimension(3*qcNumQM) :: freq
real(kind=DefReal), dimension(qcNumQM) :: Mass
real(kind=DefReal) :: atmom(qcNumQM, 3)
real(kind=DefReal) :: PosVec(3 * qcNumQM)
real(kind=DefReal) :: VelVec(3 * qcNumQM)
real(kind=DefReal) :: R(qcNumQM, 3), Vel(qcNumQM, 3)
real(kind=DefReal) :: Ener, A, phi, TMode
real(kind=DefReal) :: VelCM(3), RCM(3), TotMass
real(kind=DefReal) :: ZPE, EKin, tSharp
integer(kind=DefInt) :: I, J, K, L, natoms, nfreq, IDim, IParticle
! Converting Temperature to AU
! The default is that all the modes are at temperature indTemperature
! But, the user can use the vector indFModeSharp to assign a different
! Temprature to certain modes. (The logic here is the same as in
! the quasi-classical action/angle variables.)
TSharp = indTemperature * DegToAu
indFModeSharp = indFModeSharp * DegToAu
! Read initial positions
do i = 1, qcNumQM
atcen(i, 1:3) = T1%Particle(i)%get_pos()
end do
! now we can allocate memory
natoms = qcNumQM
do i = 1, natoms
Mass(i) = T1%Particle(i)%Mass
end do
call read_frequencies(natoms, Mass, nfreq, freq, Umat)
! Finally select initial conditions
! selecting energy for each mode from a Boltzmann Distribution
! at a temperature tSharp. Note that we scan the vector IModeTemp
! to see if we want to have a different temperature in certain modes
do I = 1, nfreq
Tmode = tSharp
do J = 1, inNTemp
if (inIModeSharp(J) == I) TMode = indFModeSharp(J)
end do
Ener = -TMode * log(1.d0 - FMS_ranb(i4zero))
! add zpe
Ener = Ener + freq(I) * 0.5
phi = 2.d0 * Pi * FMS_ranb(i4zero)
A = 2.d0 * Ener / (freq(I) * freq(I))
PosVec(I) = -sqrt(A) * sin(phi) !Position
VelVec(I) = -freq(I) * sqrt(A) * cos(phi) !Velocity
end do
! Multiply by U (transform to mass-weighted cartesians)
L = 0
do I = 1, natoms
do J = 1, 3
L = L + 1
R(I, J) = d0
Vel(I, J) = d0
do K = 1, nfreq
R(I, J) = R(I, J) + Umat(L, K) * PosVec(K)
Vel(I, J) = Vel(I, J) + Umat(L, K) * VelVec(K)
end do
end do
end do
! un-mass-weight
do i = 1, natoms
do j = 1, 3
R(I, J) = R(I, J) / sqrt(T1%Particle(I)%Mass)
Vel(I, J) = Vel(I, J) / sqrt(T1%Particle(I)%Mass)
end do
end do
! Adding the displacments, R(i,j), to the Equilibrium structure
do i = 1, natoms
do j = 1, 3
R(i, j) = atcen(i, j) + R(i, j)
end do
end do
! make sure that COM is at origin and remove COM velocity
RCM = d0
VelCM = d0
TotMass = d0
! compute the c.o.m and its velocity
do i = 1, natoms
TotMass = TotMass + T1%Particle(i)%Mass
do j = 1, 3
RCM(j) = RCM(j) + T1%Particle(i)%Mass * R(i, j)
VelCM(j) = VelCM(j) + T1%Particle(i)%Mass * vel(i, j)
end do
end do
RCM = RCM / TotMass
VelCM = VelCM / TotMass
!amv: turned off COM removal
do i = 1, natoms
do j = 1, 3
! R(i,j)=R(i,j)-rcm(j)
vel(i, j) = vel(i, j) - velcm(j)
end do
end do
! Convert velocities to momenta
do i = 1, natoms
do j = 1, 3
atmom(i, j) = Vel(i, j) * T1%Particle(i)%Mass
end do
end do
call write_initial_condition(T1, natoms, R, atmom)
do IParticle = 1, natoms
do IDim = 1, 3
call T1%set_pos(IParticle, IDim, R(IParticle, IDim))
call T1%set_mom(IParticle, IDim, atmom(IParticle, IDim))
end do
end do
! Lets calculate the kinetic energy and compare it to the ZPE. Should be
! about 1/2 the ZPE.
ZPE = get_zpe(freq, nfreq)
EKin = 0.0d0
do I = 1, natoms
do J = 1, 3
EKin = EKin + vel(i, j) * vel(i, j) * T1%Particle(I)%Mass
end do
end do
EKin = dp5 * EKin
write (fmiOut, *) 'ZPE is : ', real(ZPE), 'Half ZPE is: ', real(dp5 * ZPE)
write (fmiOut, *) 'Kinetic Energy is: ', real(Ekin)
flush (fmiOut)
end subroutine FMS_InitialConstT
!>
!! The subrouinte is written as comparison to 'InitialWigner.f'
!! @brief This routine samples initial conditions from a Husimi Distribution.
!! For each Normal mode we sample from:
!! rho(x,p)=exp[ {- Freq^2*(x-x0)^2 - (p-p_0)^2 } / {2*alpha_x}]
!! where x, and p are the normal mode position and momentum, respectively.
!! The width alpha_x = Freq for zero temperature
!! Note that Normal modes have units of sqrt[mass]*Length
!! If indTemperature /=0 then the width is defined as
!! Alpha_X=Freq/2*[1+1/Tanh(Beta*Freq/2)] where Beta=1/(Kb*T)
!! See [ Liu and Miller, JCP vol 134, Article 104101 (2011) ]
!! This is the result for the Husimi distribution at finite temperature
!! For imaginary frequency problem occured in some molecules or more often
!! in clusters and solution, the code needs to be modified with the
!! local Gaussian approximation. See [Liu and Miller, JCP vol 131, 074113 (2009)]
!! @ingroup initial
!<
subroutine FMS_InitialHusimi(T1, IDNum)
type(T_Trajectory), intent(inout) :: T1
integer(kind=DefInt), intent(in) :: IDNum
real(DefReal), dimension(qcNumQM) :: Mass
real(DefReal), dimension(3*qcNumQM) :: freq
real(DefReal), dimension(3*qcNumQM, 3*qcNumQM) :: UMat
! this are all back to front!
real(DefReal), dimension(qcNumQM, 3) :: atomcen, P, R, Vel
real(DefReal) :: Beta
real(DefReal) :: rsq, dx1, dx2, fac, x1, x2, alphax, sigp, sigx
real(kind=DefReal) :: PosVec(3 * qcNumQM)
real(kind=DefReal) :: Momvec(3 * qcNumQM)
real(kind=DefReal) :: TotMass, rhototal, RCM(3), VelCM(3)
integer(kind=DefInt) :: I, J, K, L, natoms, nfreq, IDim, IParticle
real(kind=DefReal) :: ZPE, Ekin
! Read initial positions
do i = 1, qcNumQM
atomcen(i, 1:3) = T1%Particle(i)%get_pos()
end do
! now we can allocate memory
natoms = qcNumQM
do i = 1, natoms
Mass(i) = T1%Particle(i)%Mass
end do
call read_frequencies(natoms, Mass, nfreq, freq, Umat)
! Finally start sampling
Ekin = 0.0d0
! If this is the first trajectory and inzFirstGauss is true place the
! basis function at the origin. It will be shifted to atomcen/atvel below.
if (inzFirstGauss .and. IDNum == 1) then
PosVec = 0.0d0
Momvec = 0.0d0
else
! Sample from a Gaussian distribution
rhototal = 1.0d0
do I = 1, nfreq
Alphax = freq(i)
if (indTemperature > 0.0d0) then
write (fmiOut, *) 'Sampling at finite T =', indTemperature
Beta = 1.0d0 / (indTemperature * BoltzK)
Alphax = Alphax / 2.0d0 * (1.0d0 + 1.0d0 / tanh(Beta * freq(i) * dp5))
end if
Sigx = sqrt(Alphax) / freq(i)
Sigp = sqrt(Alphax)
1 continue
dx1 = 2.d0 * FMS_ranb(i4zero) - 1.d0
dx2 = 2.d0 * FMS_ranb(i4zero) - 1.d0
rsq = dx1 * dx1 + dx2 * dx2
if (rsq >= 1.d0 .or. rsq == 0.d0) goto 1
fac = sqrt(-2.d0 * log(rsq) / rsq)
x1 = dx1 * fac
x2 = dx2 * fac
! Each Normal mode is in Equilibrium in both position and
! mometum, i.e., x0 and p0 are zero so we just need to "stretch"
! the random numbers using the proper Sigma.
! Note that p is the momentum but the normal modes have a unit mass
! so we do not need to divide by the mass when we go to velocity
PosVec(I) = Sigx * x1
Momvec(I) = Sigp * x2
! For debugging purpose calculate Kinetic energy
Ekin = Ekin + Momvec(i) * Momvec(I)
end do
end if
! Kinetic energy in Normal-Modes
Ekin = Ekin * dp5
! multiply by U (transform to mass-weighted Cartesian--position/momentum)
L = 0
do I = 1, natoms
do J = 1, 3
L = L + 1
R(I, J) = d0
P(I, J) = d0
do K = 1, nfreq
R(I, J) = R(I, J) + UMat(L, K) * PosVec(K)
P(I, J) = P(I, J) + UMat(L, K) * MomVec(K)
end do
end do
end do
! un-mass-weight -- Position and Momentum
do I = 1, natoms
do J = 1, 3
R(I, J) = R(I, J) / sqrt(T1%Particle(I)%Mass)
P(I, J) = P(I, J) * sqrt(T1%Particle(I)%Mass)
end do
end do
! Adding the displacments to the Equilibrium structure
! determine velocity using the momentum
do i = 1, natoms
do j = 1, 3
R(I, J) = atomcen(I, J) + R(I, J)
Vel(I, J) = P(I, J) / T1%Particle(I)%Mass
end do
end do
! make sure that COM is at origin and remove COM velocity
RCM = d0
VelCM = d0
TotMass = d0
! compute the c.o.m and its velocity
do i = 1, natoms
TotMass = TotMass + T1%Particle(i)%Mass
do j = 1, 3
RCM(j) = RCM(j) + T1%Particle(i)%Mass * R(i, j)
VelCM(j) = VelCM(j) + T1%Particle(i)%Mass * Vel(i, j)
end do
end do
RCM = RCM / TotMass
VelCM = VelCM / TotMass
! Remove COM velocity
! amv: Turned off COM removal - it can interfere with QM/MM
! amv: momentum removal is still cool, though
! Use Velocity to determine Momentum
do i = 1, natoms
do j = 1, 3
! R(i,j)=R(i,j)-rcm(j)
Vel(i, j) = Vel(i, j) - velcm(j)
P(I, J) = Vel(I, J) * T1%Particle(I)%Mass
end do
end do
! Write initial conditions to a file
call write_initial_condition(T1, natoms, R, P)
! Assign position and momentum to trajecotry
do IParticle = 1, qcNumQM
do IDim = 1, 3
call T1%set_pos(IParticle, IDim, R(IParticle, IDim))
call T1%set_mom(IParticle, IDim, P(IParticle, IDim))
end do
end do
! Lets calculate the kinetic energy and compare it to the ZPE. Should be
! about 1/2 the ZPE.
! We also compare the kinetic energy using Normal Modes and Cartesian Coord.
ZPE = get_zpe(freq, nfreq)
write (fmiOut, *) 'Ekin Normal Modes:', real(Ekin)
EKin = 0.0d0
do I = 1, natoms
do J = 1, 3
EKin = EKin + P(i, j) * P(i, j) / T1%Particle(I)%Mass
end do
end do
EKin = dp5 * EKin
write (fmiOut, *) 'ZPE is : ', real(ZPE), 'Half ZPE is: ', real(dp5 * ZPE)
write (fmiOut, *) 'Kinetic Energy is: ', real(Ekin)
call flush (fmiOut)
end subroutine FMS_InitialHusimi
!>
!! Samples initial conditions from a Wigner distribution using a randomly chosen unit vector.
!! @note The resulting distribution seems to be too cold!
!! This version is general and allows for sampling at finite indTemp.
!! In this case the width of the Wigner distribution is given by:
!! \f[ \alpha_x = Freq/2*\tanh(\beta*Freq/2) \f] where \f$ \beta=1/(K_bT) \f$
!! See Heller JCP, vol, 65, pp. 1289 (1976)
!! If indTemperature=0 then \f$ \alpha_x=Freq/2\f$ which is the known result (Note
!! that the normal modes have unit mass)
!! \see FMS_InitialWigner
!! @ingroup initial
!<
subroutine FMS_InitialColdWig(T1, IDNum)
use EispackModule, only: FMS_ch
type(t_Trajectory), intent(inout) :: T1
integer(kind=DefInt), intent(in) :: IDNum
real(kind=DefReal) :: Beta
integer(kind=DefInt) :: I, J, l, IParticle, IDim
integer(kind=DefInt) :: natoms, natomsT6, NPSDim, MatZ
real(kind=DefReal) :: atcen(qcNumQM, 3), vel(qcNumQM, 3)
real(kind=DefReal), dimension(3*qcNumQM, 3*qcNumQM) :: Umat
real(kind=DefReal), dimension(3*qcNumQM) :: freq
real(kind=DefReal), dimension(qcNumQM) :: Mass
real(kind=DefReal), dimension(6*qcNumQM) :: fv1, fv2
real(kind=DefReal), dimension(6*qcNumQM, 6*qcNumQM) :: OReal, OImag
real(kind=DefReal) :: big, ysq, dx1, dx2, x1, x2, alphax, sigx, sigp, prob
real(kind=DefReal) :: ZPE, EKin
integer(kind=DefInt) :: nfreq, k, ione, ierr
real(kind=DefReal) :: PosVec(3 * qcNumQM), MomVec(3 * qcNumQM)
real(kind=DefReal) :: R(qcNumQM, 3), P(qcNumQM, 3)
real(kind=DefReal) :: RCM(3), VelCM(3), TotMass
real(kind=DefReal) :: UVec(6 * qcNumQM)
real(kind=DefReal) :: EValues(6 * qcNumQM)
real(kind=DefReal) :: EVReal(6 * qcNumQM, 6 * qcNumQM)
real(kind=DefReal) :: EVImag(6 * qcNumQM, 6 * qcNumQM)
real(kind=DefReal) :: FM1(18 * qcNumQM)
complex(kind=DefComp) :: CRMat(6 * qcNumQM, 6 * qcNumQM)
complex(kind=DefComp) :: Lambda(6 * qcNumQM, 6 * qcNumQM)
complex(kind=DefComp) :: CTmp(6 * qcNumQM, 6 * qcNumQM)
complex(kind=DefComp) :: CEvec(6 * qcNumQM, 6 * qcNumQM)
complex(kind=DefComp) :: CSTemp
! Beta is 1/kBT
Beta = d1 / (indTemperature * BoltzK)
! Read initial positions
do i = 1, qcNumQM
atcen(i, 1:3) = T1%Particle(i)%get_pos()
end do
natoms = qcNumQM
do i = 1, natoms
Mass(i) = T1%Particle(i)%Mass
end do
call read_frequencies(natoms, Mass, nfreq, freq, Umat)
NPSDim = nfreq * 2 !6D phase space
natomsT6 = natoms * 6
! Finally, select initial conditions
! If this is the first trajectory and inzFirstGauss is true the basis fxn
! is placed at the origin. Below it will be shifted to atcen
if (inzFirstGauss .and. IDNum == 1) then
PosVec = d0
MomVec = d0
else
! Need to choose a random vector on the 2N-dimensional unit hypersphere
! Construct a random purely imaginary Hermitian matrix.
! Then, exponentiate the matrix
! which is guaranteed to give an orthogonal matrix, i.e. a rotation matrix.
Big = 100000.0d0
do i = 1, NPSDim
do j = 1, NPSDim
Lambda(i, j) = (0.d0, 0.d0)
end do
end do
do i = 1, NPSDim
do J = i + 1, NPSDim
Lambda(i, j) = c1i * (Big - 2 * Big * FMS_ranb(i4zero))
Lambda(j, i) = -Lambda(i, j)
end do
end do
do i = 1, NPSDim
do j = 1, NPSDim
OReal(i, j) = real(Lambda(i, j))
OImag(i, j) = real(-c1i * Lambda(i, j))
end do
end do
matz = 1
call FMS_ch(natomsT6, NPSDim, OReal, OImag, EValues, &
MatZ, EVReal, EVImag, FV1, FV2, FM1, IErr)
do i = 1, NPSDim
do j = 1, NPSDim
CEvec(i, j) = EVReal(i, j) + c1i * EVImag(i, j)
end do
end do
do i = 1, NPSDim
CSTemp = exp(-c1i * EValues(i))
do j = 1, NPSDim
CTmp(i, j) = CSTemp * conjg(CEvec(j, i))
end do
end do
do i = 1, NPSDim
do j = 1, NPSDim
CRMat(i, j) = d0
do k = 1, NPSDim
CRMat(i, j) = CRMat(i, j) + CEvec(i, k) * CTmp(k, j)
end do
end do
end do
! Now CRMat should be a random rotation matrix in the NPSDim dimensional
! space. Furthermore, it should be purely real although we are
! storing it in a complex matrix (because it is built from complex
! matrix-matrix products)
do i = 1, NPSDim
do j = 1, NPSDim
if (abs(aimag(CRMat(i, j))) > 1.0d-5) then
write (fmiOut, *) 'Ouch ', i, j, CRMat(i, j)
end if
end do
end do
! Choose a random unit vector and rotate by the random rotation matrix
! make sure that IOne is in the correct range: 1-NPSDim
! we add 1.5 and not one in order to avoid a situation where IOne=NPSDim
! only when ranb=1.0d0. Once we have chosen a unit vector randomly, we
! rotate it with our random rotation matrix.
1 continue
IOne = int(dble(NPSDim - 1) * FMS_ranb(i4zero) + 1.50d0)
if (IOne > NPSDim .or. IOne == 0) goto 1
! Matrix-vector multiplication CRMat*UnitVector
do i = 1, NPSDim
UVec(i) = real(CRMat(i, IOne))
end do
! Check that we actually have a unit vector on the hypersurface
Ysq = d0
do i = 1, NPSDim
Ysq = Ysq + UVec(i) * UVec(i)
end do
if (abs(Ysq - d1) > 1.0d-04) then
write (fmiOut, *) 'Warning: Vector has non-unit norm?'
write (fmiOut, *) Ysq
end if
! Choose a random number from a Gaussian distribution we actually choose
! two and use only one. Scale the randomly oriented vector by a random
! number drawn from a standard normal deviate. We can do this because
! we will scale by frequency later.
dx1 = FMS_ranb(i4zero)
dx2 = FMS_ranb(i4zero)
x1 = sqrt(-2.d0 * log(dx1)) * cos(2.d0 * pi * dx2)
x2 = sqrt(-2.d0 * log(dx1)) * sin(2.d0 * pi * dx2)
do I = 1, NPSDim
UVec(i) = UVec(i) * x1
end do
! Wigner Distribution. For each Normal mode we sample from:
! rho(x,p)=exp[-2*AlphaX(x-x0)^2 -(p-p0)^2/(2*AlphaX)]
! note that p is momentum and therefore we divide by the mass
! of the normal mode to get the velocity
! The width AlphaX is Mass*Freq/2 [Heller JCP vol 80 p 5038 (1984)]
! But Normal modes have units of sqrt[mass]*Length and therefore
! alphax=Freq/2!
do I = 1, nfreq
Alphax = freq(i) / 2.d0
if (indTemperature /= d0) then
Alphax = Alphax * tanh(Beta * freq(i) * dp5)
end if
Sigx = sqrt(1.d0 / (4.d0 * Alphax))
Sigp = sqrt(1.d0 * Alphax)
! Each Normal mode is in Equilibrium in both position and
!! momentum, i.e., x0 and p0 are zero so we just need to "stretch"
! the random numbers using the proper Sigma.
! Note that p is the momentum but the normal modes have a unit mass
! so we do not need to divide by the mass when we go to velocity
PosVec(I) = Sigx * UVec(2 * I - 1)
MomVec(I) = Sigp * UVec(2 * I)
end do
end if
! Compute the probability of this Initial condition
Prob = d1
do I = 1, nfreq
Alphax = freq(i) / 2.d0
Prob = Prob * exp(-2.d0 * Alphax * PosVec(I) * PosVec(I) - &
MomVec(I) * MomVec(I) / (2.d0 * Alphax))
end do
write (fmiOut, *) 'Probability of These Initial Conditions: ', Prob
flush (fmiOut)
!! transform to mass weighted cartesian coordinates
L = 0
do I = 1, natoms
do J = 1, 3
L = L + 1
R(I, J) = d0
P(I, J) = d0
do K = 1, nfreq
R(I, J) = R(I, J) + UMat(L, K) * PosVec(K)
P(I, J) = P(I, J) + UMat(L, K) * MomVec(K)
end do
end do
end do
! un-mass weight, position and momentum
do i = 1, natoms
do j = 1, 3
r(i, j) = r(i, j) / sqrt(T1%Particle(I)%Mass)
p(i, j) = p(i, j) * sqrt(T1%Particle(I)%Mass)
end do
end do
! Adding the displacments to the Equilibrium structure
! Determine Velocity from Momentum
do i = 1, natoms
do j = 1, 3
R(I, J) = atcen(I, J) + R(I, J)
Vel(I, J) = P(I, J) / T1%Particle(I)%Mass
end do
end do
! make sure that COM is at origin and remove COM velocity
RCM = d0
VelCM = d0
TotMass = d0
! compute the c.o.m and its velocity
do i = 1, natoms
TotMass = TotMass + T1%Particle(i)%Mass
do j = 1, 3
RCM(j) = RCM(j) + T1%Particle(i)%Mass * atcen(i, j)
VelCM(j) = VelCM(j) + T1%Particle(i)%Mass * vel(i, j)
end do
end do
RCM = RCM / TotMass
VelCM = VelCM / TotMass
! amv: Do not remove COM - it interferes with QM/MM
do i = 1, natoms
do j = 1, 3
! R(i,j)=R(i,j)-rcm(j)
vel(i, j) = vel(i, j) - velcm(j)
P(i, j) = vel(i, j) * T1%Particle(i)%Mass
end do
end do
! Write initial conditions to a file
call write_initial_condition(T1, natoms, R, P)
do iParticle = 1, qcNumQM
do iDim = 1, 3
call T1%set_pos(iParticle, iDim, R(iParticle, IDim))
call T1%set_mom(iParticle, iDim, P(iParticle, IDim))
end do
end do
! Lets calculate the kinetic energy and compare it to the ZPE. Should be
! about 1/2 the ZPE.
ZPE = get_zpe(freq, nfreq)
EKin = 0.0d0
do I = 1, natoms
do J = 1, 3
EKin = EKin + P(i, j) * P(i, j) / T1%Particle(I)%Mass
end do
end do
EKin = dp5 * EKin
write (fmiOut, *) 'ZPE is : ', real(ZPE), 'Half ZPE is: ', real(dp5 * ZPE)
write (fmiOut, *) 'Kinetic Energy is: ', real(Ekin)
flush (fmiOut)
end subroutine FMS_InitialColdWig
!>
!! Samples initial conditions from a quasiclassical
!! distribution (I.e. using action-angle variables)
!! @ingroup initial
!<
subroutine FMS_InitialQuasi(T1)
type(T_Trajectory), intent(inout) :: T1
integer(kind=DefInt) :: natoms, nfreq, iGUnit
real(kind=DefReal), dimension(3*qcNumQM) :: freq, xmass
real(kind=DefReal) :: atcen(3 * qcNumQM)
real(kind=DefReal), dimension(3*qcNumQM, 3*qcNumQM) :: Umat
real(kind=DefReal), dimension(qcNumQM) :: Mass
real(kind=DefReal) :: atmom(3 * qcNumQM)
real(kind=DefReal) :: atvel(3 * qcNumQM)
real(kind=DefReal) :: qprime(3 * qcNumQM)
real(kind=DefReal) :: pprime(3 * qcNumQM)
real(kind=DefReal) :: qsave(3 * qcNumQM)
real(kind=DefReal) :: tmpq(3 * qcNumQM)
real(kind=DefReal) :: tmpp(3 * qcNumQM), tmpe, zpe, fac, targete
real(kind=DefReal) :: RCM(3), VelCM(3), TotMass
integer(kind=DefInt) :: i, j, k
real(kind=DefReal) :: x
character(len=:), allocatable :: output_file
! Read initial positions
do i = 1, qcNumQM
atcen((i - 1) * 3 + 1:i * 3) = T1%Particle(i)%get_pos()
end do
! now we can allocate memory
natoms = qcNumQM
do i = 1, natoms
Mass(i) = T1%Particle(i)%Mass
end do
call read_frequencies(natoms, Mass, nfreq, freq, Umat)
! Finally select initial conditions
! select random angle for each mode
do i = 1, nfreq
fac = d1
do j = 1, inNAddQuanta
if (i == inIAddQuanta(j)) fac = fac + 2.0d0
end do
fac = sqrt(fac)
x = FMS_ranb(i4zero) * d2 * pi
qprime(i) = cos(x) * fac
qsave(i) = qprime(i)
pprime(i) = -1.0d0 * sin(x) * fac
tmpq(i) = qprime(i) / sqrt(freq(i)) !Position
tmpp(i) = sqrt(freq(i)) * pprime(i) !Velocity
end do
! multiply by U (transform to mass-weighted cartesians)
do i = 1, natoms * 3
qprime(i) = d0
pprime(i) = d0
do j = 1, nfreq
qprime(i) = qprime(i) + UMat(i, j) * tmpq(j)
pprime(i) = pprime(i) + UMat(i, j) * tmpp(j)
end do
end do
xmass = 0.0d0
k = 1
do i = 1, natoms
do j = 1, 3
xmass(k) = sqrt(Mass(i))
k = k + 1
end do
end do
! un-mass-weight
do i = 1, natoms * 3
qprime(i) = qprime(i) / xmass(i)
atcen(i) = atcen(i) + qprime(i) !add to equil. position
atvel(i) = pprime(i) / xmass(i) !velocity
atmom(i) = pprime(i) * xmass(i) !momentum
end do
! make sure that COM is at origin and remove COM velocity
RCM = d0
VelCM = d0
TotMass = d0
! compute COM and its velocity
do i = 1, natoms
TotMass = TotMass + T1%Particle(i)%Mass
do j = 1, 3
RCM(j) = RCM(j) + T1%Particle(i)%Mass * atcen((i - 1) * 3 + j)
VelCM(j) = VelCM(j) + T1%Particle(i)%Mass * atvel((i - 1) * 3 + j)
end do
end do
RCM = RCM / TotMass
VelCM = VelCM / TotMass
! remove COM and its velocity
!amv turned off COM removal - it interferes with QM/MM
do i = 1, natoms
do j = 1, 3
! atcen((i-1)*3+j)=atcen((i-1)*3+j)-RCM(j)
atvel((i - 1) * 3 + j) = atvel((i - 1) * 3 + j) - VelCM(j)
atmom((i - 1) * 3 + j) = atmom((i - 1) * 3 + j) - VelCM(j) * T1%Particle(I)%Mass
end do
end do
! Write initial conditions to file
! TODO: call 'write_initial_condition' subroutine
output_file = trim(FMSWorkingDir)//'Out.xyz'
open (newunit=IGUnit, file=output_file, action='write')
write (IGUnit, '(A10)') 'UNITS=BOHR'
write (IGUnit, *) natoms
do I = 1, natoms
write (IGUnit, 1005) T1%Particle(I)%Elmnt, (atcen((I - 1) * 3 + J), J=1, 3)
end do
write (IGUnit, '(A9)') '# momenta'
do I = 1, natoms
write (IGUnit, 1006) (atmom((I - 1) * 3 + J), J=1, 3)
end do
close (IGUnit)
1005 format(A2, 3es24.15)
1006 format(3es24.15)
zpe = d0
tmpe = d0
do i = 1, nfreq
zpe = zpe + freq(i)
end do
do i = 1, natoms * 3
tmpe = tmpe + atmom(i) * atmom(i) / (xmass(i) * xmass(i))
end do
zpe = zpe * dp5
tmpe = tmpe * dp5
write (fmiOut, *) 'ZPE is: ', real(zpe), 'Half the ZPE is: ', real(dp5 * ZPE)
write (fmiOut, *) 'Kinetic Energy is:', real(tmpe)
do j = 1, nfreq
tmpq(j) = d0
tmpq(j + 1) = d0