forked from subotnikgroup/AFSSH_modifications
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod_afssh.f90
1544 lines (1268 loc) · 38.5 KB
/
mod_afssh.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
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
Module mod_afssh
!! Hammes-Schiffer, Tully, JCP 101, 4657 (1994)
implicit none
real*8, parameter :: clight=2.99792458D10,av=6.0221367D23,hbar=1.05457266D-34
real*8, parameter :: kb=1.3806503d-23
real*8, parameter :: pascal_to_atm=9.86923267d-6,kcal_to_J=6.95d-21
real*8, parameter :: amu2kg=1.66053892d-27
real*8, parameter :: au2kg=9.10938291d-31,au2J=4.35974417d-18,au2m=5.2917721092d-11,au2s=2.418884326505d-17
real*8, parameter :: q_el=2.307075d-28
complex*16,parameter :: iota = (0.d0,1.d0)
real*8 pi,wave_to_J
!! Potential
integer nquant
real*8 g_coup,epsilon
real*8 V_exothermicity,Vc,omg_B,gamma_B,temperature
real*8 beta,gamma_D,lambda_D,V_reorg,V_barrier
real*8 s01,s02,x_cr
real*8,allocatable :: mass(:),omg(:),ck(:)
!! Input/Output
real*8 cnt_frust,cnt_collapse,cnt_init,cnt_term
real*8,allocatable :: pop(:,:),pop_surf(:,:),pop_amp(:,:)
!! Classical
integer nclass,idistribution
real*8,allocatable :: x(:),v(:),acc(:)
real*8,allocatable :: x_old(:),v_old(:),acc_old(:),x_hop(:)
real*8 tim_hop
integer iforward,flag_terminate,flag_frust,flag_reactant,flag_hop,flag_ortho
complex*16,allocatable :: delr(:,:),delp(:,:)
complex*16,allocatable :: delr_old(:,:),delp_old(:,:)
!! Quantum
integer state,nbasis,state_tentative
integer state_old
real*8,allocatable :: si_adiab(:,:),V_k(:),d_ij(:,:,:),vdotd(:,:),V_k_old(:)
real*8,allocatable :: Hamil_diab(:,:),delH_dels(:,:,:),delH_dels_ad(:,:,:)
real*8,allocatable :: pot(:,:),force(:,:),force_old(:,:)
complex*16,allocatable :: ci(:),ci_old(:)
real*8,allocatable :: si_adiab_prev(:,:)
complex*16,allocatable :: mat(:,:),mat_adiab(:,:)
real*8,allocatable :: hop_prob(:),W_overlap(:,:),hop_prob_net(:)
!! Evolution
integer n_traj,nsteps,nsteps_eq,nstep_write,iwrite,nstep_avg
real*8 dtc,total_time,curr_time,traj_num,tim_eq
real*8 energy,pot_en,KE_en,energy_cutoff,energy_old
real*8 ensq_avg,en_avg
integer nst_av
integer ihop,icollapse,iterminate,ifriction,iaverage
real*8 prob_tun
!! Parallelization
integer iparallel,iflow,iproc,iwait,ifolder
!! Misc
integer nold,cnt_rate
real*8 tim_tot,tim_ev_cl,tim_diag,tim_cl,tim_rattle,tim_pbc,tim_LJ_tot,tim_solv_solv,tim_check,tim_check2
real*8 tim_T_jk
integer,allocatable:: seed(:)
real*8,allocatable:: work(:)
complex*16,allocatable:: cwork(:)
integer,allocatable:: iwork(:),isuppz(:)
contains
!----------------------------------------------------------
!----------------------------------------------------------
subroutine setup
implicit none
character st_ch
integer i,size_seed,seed2(2)
real*8 rnd,c_0,c_e,kt
pi=dacos(-1.d0)
wave_to_J=2*pi*clight*hbar
nold=0
open(10,file="AFSSH.inp")
read(10,*) iflow
read(10,*) iproc
read(10,*) iparallel
read(10,*) iwait
read(10,*) N_traj
read(10,*) dtc
read(10,*) total_time
read(10,*) iwrite
read(10,*) nstep_write
read(10,*) nstep_avg
read(10,*) idistribution
read(10,*) flag_frust
read(10,*) flag_ortho
read(10,*) energy_cutoff
read(10,*) nclass
read(10,*) nquant
read(10,*) V_exothermicity
read(10,*) Vc
read(10,*) omg_B
read(10,*) V_barrier
read(10,*) gamma_B
read(10,*) temperature
read(10,*) iforward
read(10,*) icollapse
read(10,*) ifriction
read(10,*) seed2
read(10,*) st_ch
close(10)
!----------------------------------------------------------
if(st_ch.ne.'x') then
write(6,*) "problem in reading input file"
stop
endif
!----------------------------------------------------------
nbasis=nquant
energy_cutoff=energy_cutoff*wave_to_J
temperature=temperature*wave_to_J/kb
kt=kb*temperature
Vc=Vc*wave_to_J
V_exothermicity=V_exothermicity*wave_to_J
omg_B=omg_B*2*pi*clight
V_barrier=V_barrier*wave_to_J
gamma_B=gamma_B*omg_B
nsteps=nint(total_time/dtc)+1
beta=1.d0/(kb*temperature)
!gamma_D=omg_B**2/gamma_B
!lambda_D=V_reorg/4.d0
!-----------------------------------------------------------------
i=nsteps/nstep_avg+1
allocate(pop(nquant,i),pop_surf(nquant,i),pop_amp(nquant,i))
allocate(x(nclass),v(nclass),acc(nclass))
allocate(x_old(nclass),v_old(nclass),acc_old(nclass),x_hop(nclass))
allocate(mass(nclass),omg(nclass),ck(nclass))
allocate(delr(nquant,nclass),delp(nquant,nclass))
allocate(delr_old(nquant,nclass),delp_old(nquant,nclass))
allocate(si_adiab(nbasis,nquant),ci(nquant),V_k(nquant),V_k_old(nquant))
allocate(Hamil_diab(nbasis,nbasis),delH_dels(nbasis,nbasis,nclass),delH_dels_ad(nquant,nquant,nclass))
allocate(pot(nquant,nquant),force(nquant,nclass),force_old(nquant,nclass))
allocate(mat(nbasis,nbasis),mat_adiab(nquant,nquant))
allocate(d_ij(nquant,nquant,nclass),vdotd(nquant,nquant),hop_prob(nquant),W_overlap(nquant,nquant))
allocate(hop_prob_net(nquant))
allocate(ci_old(nquant),si_adiab_prev(nbasis,nquant))
call random_seed(size=size_seed)
allocate(seed(size_seed))
do i=1,size_seed/2
seed(i)=seed2(1)*(2*i+i*i-7)
enddo
do i=size_seed/2+1,size_seed
seed(i)=seed2(2)*(i/2+34-i**3)
enddo
call random_seed(put=seed)
call system_clock(count_rate=cnt_rate)
!-----------------------------------------------------------------
if(iflow==2) then
open(10,file="ifolder.inp")
read(10,*) ifolder
close(10)
N_traj=N_traj/iparallel
if(ifolder>1) then
do i=1,(ifolder-1)*N_traj
seed=seed+1
call init_cond
enddo
call random_seed(put=seed)
endif
else
ifolder=1
endif
tim_tot=0.d0
end subroutine setup
!----------------------------------------------------------
subroutine main
implicit none
integer i,j,k,n
real*8 t1,t2
call files(0)
call cpu_time(t1)
call setup_parameters
call initialize_averages
do i=1,N_traj
traj_num=i
call init_cond
call evolve(nsteps)
call average_end
enddo
call write_average
call cpu_time(t2);tim_tot=tim_tot+t2-t1
call files(1)
end subroutine main
!----------------------------------------------------------
subroutine files(flag)
implicit none
integer,intent(in)::flag
if(flag==0) then
open(10,file="output")
open(11,file="output_cl")
open(12,file="output_qm")
open(13,file="output_hop")
open(14,file="output_overlap")
open(15,file="output_dec")
open(100,file="pop.out")
open(101,file="cnts.out")
else
write(10,*)
write(10,*)"Total time=",tim_tot
close(10);close(11);close(12);close(13);close(14);close(15)
close(100);close(101)
endif
end subroutine files
!-----------------------------------------------------------------
subroutine initialize_averages
implicit none
cnt_frust=0.d0
cnt_collapse=0.d0
cnt_init=0.d0
cnt_term=0.d0
pop=0.d0
pop_surf=0.d0
pop_amp=0.d0
end subroutine initialize_averages
!-----------------------------------------------------------------
subroutine init_cond
implicit none
integer i
real*8 sig_x,sig_p,rnd,ak,su
real*8 energy_0
do i=1,nclass
!ak=2/(hbar*omg(i))*dtanh(beta*hbar*omg(i)/2.d0) !! Wigner
ak=beta !! Classical
sig_x=1.d0/dsqrt(ak*mass(i)*omg(i)**2)
sig_p=dsqrt(mass(i)/ak)
call gaussian_random_number(rnd)
x(i)=rnd*sig_x
call gaussian_random_number(rnd)
v(i)=(1.d0/mass(i)*(rnd*sig_p))
enddo
state=1
call evaluate_variables(0)
call evaluate_variables(1)
!! quantum state initialized on diabat 1
ci=si_adiab(state,:)
call random_number(rnd)
su=0.d0
do i=1,nquant
su=su+cdabs(ci(i))**2
if(rnd<su) then
state=i
exit
endif
enddo
delr=0.d0
delp=0.d0
ihop=1
iaverage=1
iterminate=0
flag_terminate=0
curr_time=0.d0
call evaluate_variables(0)
call evaluate_variables(1)
call compute_mat_diab
!! to compute the standard deviation of the energy of the trajectory
en_avg=0.d0;ensq_avg=0.d0
nst_av=0
end subroutine init_cond
!-----------------------------------------------------------------
subroutine evolve(nsteps)
implicit none
integer,intent(in) :: nsteps
integer i,j,nstep_sm,iflag_coll,i_do_something
real*8 t1,t2
integer iterm
!call cpu_time(t1)
call write_output(1,1)
do i=1,nsteps
call write_output(i,0)
call average(i)
call save_old_state
call evolve_classical(dtc)
i_do_something=0
if(ifriction==0) then
!! Do till energy is conserved
!! ifriction==1 --> Langevin equation (non-energy conserving calculations)
do while(dabs(energy-energy_old)>energy_cutoff.and.i>1)
i_do_something=i_do_something+1
call do_something(i_do_something)
enddo
endif
if(i_do_something==1) then
!! if_do_something==1 refers to energy conservation by changing adiabat.
!! This is treated as if a hop occured - hence reset moments
delr=0.d0
delp=0.d0
endif
if(i_do_something.ne.1)call evolve_quantum_small_dtq
!call evolve_quantum_small_dtq
if(i_do_something.ne.1.and.ihop==1)call hop
if(i_do_something.ne.1.and.icollapse==1)call collapse(dtc,iflag_coll)
if(flag_terminate==1) call traj_terminate(iterm)
if(iterm==1)exit
curr_time=curr_time+dtc
enddo
call write_output(1,1)
!call cpu_time(t2)
!tim_evolve=tim_evolve+t2-t1
end subroutine evolve
!-----------------------------------------------------------------
subroutine do_something(i_do_something)
!! subroutine to maintain energy conservation
implicit none
integer,intent(in)::i_do_something
real*8 acc_tent(nclass),dt,dtm(3)
integer i,nstep
if(i_do_something==1) then
!! On first pass, check if hop happens; if yes, check if evolution using the acceleration of hopped surface conservses energy
!! Useful for very sharp crossings
call evolve_quantum_small_dtq
if(flag_hop==1) then
state=state_tentative
call evaluate_variables(0)
v=v_old+0.5*(acc_old+acc)*dtc
call evaluate_variables(1)
endif
else
!! If the first pass did not work, reduce time-steps untill energy is conserved
dtm=1.d0 !! some large value
dtm(1)=0.1d0/maxval(vdotd)
dtm(2)=0.5*dtc*dsqrt(energy_cutoff/dabs(energy-energy_old))
dtm(3)=dtc
dt=minval(dtm)
dt=dt/real(i_do_something)
nstep=nint(dtc/dt)
dt=dtc/real(nstep)
call revert_state
do i=1,nstep
call evolve_classical(dt)
enddo
endif
end subroutine do_something
!-----------------------------------------------------------------
subroutine average(i)
implicit none
integer,intent(in) :: i
integer j,i1,j1
complex*16 ci_diab(nquant)
real*8 r_avg
integer if_reactant
real*8 t1,t2
!call cpu_time(t1)
if(iwrite==1) then
en_avg=en_avg+energy
ensq_avg=ensq_avg+energy*energy
nst_av=nst_av+1
endif
if(iaverage==1.and.(mod(i,nstep_avg)==1.or.nstep_avg==1)) then
if(nstep_avg==1) then
j=i
else
j=i/nstep_avg+1
endif
!! Diabatic population
!! J. Chem. Phys. 139, 211101 (2013)
pop(:,j)=pop(:,j)+si_adiab(:,state)**2
pop_surf(:,j)=pop_surf(:,j)+si_adiab(:,state)**2
ci_diab=matmul(si_adiab,ci)
pop_amp(:,j)=pop_amp(:,j)+cdabs(ci_diab)**2
do j1=2,nquant
do i1=1,j1-1
pop(:,j)=pop(:,j)+2*realpart(ci(i1)*dconjg(ci(j1)))*si_adiab(:,i1)*si_adiab(:,j1)
enddo
enddo
endif
!call cpu_time(t2)
!tim_coll=tim_coll+t2-t1
end subroutine average
!-----------------------------------------------------------------
subroutine average_end
implicit none
end subroutine average_end
!-----------------------------------------------------------------
subroutine save_old_state
implicit none
x_old=x
v_old=v
acc_old=acc
ci_old=ci
state_old=state
!ci2_old=ci2
si_adiab_prev=si_adiab
V_k_old=V_k
force_old=force
energy_old=energy
delr_old=delr
delp_old=delp
end subroutine save_old_state
!-----------------------------------------------------------------
subroutine revert_state
implicit none
x=x_old
v=v_old
state=state_old
ci=ci_old
delr=delr_old
delp=delp_old
force=force_old
!ci2=ci2_old
call evaluate_variables(0)
call evaluate_variables(1)
end subroutine revert_state
!-----------------------------------------------------------------
subroutine evolve_quantum_small_dtq
implicit none
integer i,nstep_qm
real*8 dtq,dtq1,dtq2
real*8 V_k_hold(nquant),dVk_dt(nquant)
real*8 dforce_dt(nquant,nclass)
complex*16 ci_prev(nquant),dci_dt(nquant)
call compute_vdotd
dVk_dt=(V_k-V_k_old)/dtc
if(icollapse==1) then
call compute_delH_dels_ad
endif
dtq1=0.02/maxval(vdotd)
dtq2=0.02*hbar/maxval(V_k-sum(V_k)/real(nquant))
dtq=dtq1
if(dtq>dtq2)dtq=dtq2
if(dtq>dtc)dtq=dtc
nstep_qm=nint(dtc/dtq)
dtq=dtc/real(nstep_qm)
hop_prob=0.d0
hop_prob_net=0.d0
V_k_hold=V_k
V_k=V_k_old
call compute_mat_adiab
flag_hop=0
do i=1,nstep_qm
call compute_hop_prob(dtq)
if(flag_hop==0)call check_hop(i*dtq)
call rk4(ci,dtq,dVk_dt)
enddo
if(icollapse==1) then
call verlet_decoherence(dtc,W_overlap,V_k_old,dvk_dt)
endif
do i=1,nquant
if(hop_prob_net(i)<0.d0)hop_prob_net=0.d0
hop_prob_net(i)=1.d0-dexp(-hop_prob_net(i))
enddo
end subroutine evolve_quantum_small_dtq
!-----------------------------------------------------------------
subroutine compute_hop_prob(dtq)
implicit none
real*8,intent(in)::dtq
integer i
real*8 pr
do i=1,nquant
if(i.ne.state) then
pr=-2*real(ci(i)*dconjg(ci(state)))*vdotd(i,state)
pr=pr*dtq/cdabs(ci(state))**2
if(pr<0.d0)pr=0.d0 !!!! CAUTION AMBER CHECK !!!!
hop_prob(i)=pr
hop_prob_net(i)=hop_prob_net(i)+pr
endif
enddo
end subroutine compute_hop_prob
!-----------------------------------------------------------------
subroutine check_hop(tim)
implicit none
real*8,intent(in)::tim
integer i
real*8 rnd,pr
call random_number(rnd)
pr=0.d0
flag_hop=0
do i=1,nquant
if(i.ne.state) then
pr=pr+hop_prob(i)
if(rnd<pr) then
state_tentative=i
flag_hop=1
exit
endif
endif
enddo
end subroutine check_hop
!-----------------------------------------------------------------
subroutine rk4(ci,dtq,dVk_dt)
implicit none
complex*16,intent(inout)::ci(nquant)
real*8,intent(in) :: dtq,dVk_dt(nquant)
complex*16,dimension(1:nquant):: k1,k2,k3,k4
k1=matmul(mat_adiab,ci)
V_k=V_k+dVk_dt*dtq/2.d0
call compute_mat_adiab
k2=matmul(mat_adiab,ci+0.5*dtq*k1)
k3=matmul(mat_adiab,ci+0.5*dtq*k2)
V_k=V_k+dVk_dt*dtq/2.d0
call compute_mat_adiab
k4=matmul(mat_adiab,ci+dtq*k3)
ci=ci+dtq/6.d0*(k1+2*k2+2*k3+k4)
end subroutine rk4
!-----------------------------------------------------------------
subroutine verlet_decoherence(dt,W_mat,V_k0,dvk_dt)
implicit none
real*8,intent(in):: dt,W_mat(nquant,nquant),V_k0(nquant),dvk_dt(nquant)
real*8 acc_dec(nquant,nclass),delf(nquant,nclass),temp(nclass)
complex*16 temp_delr(nquant,nclass),temp_delp(nquant,nclass)
!complex*16 ci_diab(nquant)
integer i,j,k
delF=force_old
temp=delF(state,:)
do i=1,nquant
delF(i,:)=delF(i,:)-temp
acc_dec(i,:)=delF(i,:)*cdabs(ci_old(i))**2/mass
enddo
do i=1,nquant
delr(i,:)=delr(i,:)+delp(i,:)/mass*dt+0.5*acc_dec(i,:)*dt**2
delp(i,:)=delp(i,:)+0.5*mass*acc_dec(i,:)*dt
enddo
!ci_diab=cdexp(iota*V_k0*dt/hbar)*cdexp(0.5*iota*dvk_dt*dt**2/hbar)*ci
!ci_diab=matmul_lap(W_mat,ci_diab)
delF=0.d0
do j=1,nquant
do k=1,nquant
delF(j,:)=delF(j,:)+dabs(W_mat(j,k)**2)*(force(k,:)-force(state,:))
enddo
enddo
!temp=delF(state,:)
do i=1,nquant
! delF(i,:)=delF(i,:)-temp
! !acc_dec(i,:)=delF(i,:)*cdabs(ci_diab(i))**2/mass
acc_dec(i,:)=delF(i,:)*cdabs(ci_old(i))**2/mass
enddo
do i=1,nquant
delp(i,:)=delp(i,:)+0.5*mass*acc_dec(i,:)*dt
enddo
temp_delr=0.d0;temp_delp=0.d0
do j=1,nquant
do k=1,nquant
temp_delr(j,:)=temp_delr(j,:)+dabs(W_mat(k,j)**2)*delr(k,:)
temp_delp(j,:)=temp_delp(j,:)+dabs(W_mat(k,j)**2)*delp(k,:)
enddo
enddo
delr=temp_delr
delp=temp_delp
!do i=1,nclass
! delr(:,i)=delr(:,i)-delr(state,i)
! delp(:,i)=delp(:,i)-delp(state,i)
!enddo
end subroutine verlet_decoherence
!-----------------------------------------------------------------
subroutine evolve_classical(dt)
!! Velocity Verlet
implicit none
real*8,intent(in) :: dt
real*8 gama_dt,c0,c1,c2
real*8 delta_r(nclass),delta_v(nclass),acc_sav(nclass)
real*8 t1,t2
!call cpu_time(t1)
if(ifriction==0) then
x=x+v*dt+0.5*acc*dt*dt
acc_sav=acc
call evaluate_variables(0)
v=v+0.5*(acc+acc_sav)*dt
call evaluate_variables(1)
endif
if(ifriction==1) then
gama_dt=gamma_B*dt
c0=dexp(-gama_dt)
c1=1.d0/gama_dt*(1.d0-c0)
c2=1.d0/gama_dt*(1.d0-c1)
call stochastic_force(delta_r,delta_v,dt)
x=x+c1*dt*v+c2*dt*dt*acc+delta_r
acc_sav=acc
call evaluate_variables(0)
v=c0*v+(c1-c2)*dt*acc_sav+c2*dt*acc+delta_v
call evaluate_variables(1)
endif
!call cpu_time(t2);tim_ev_cl=tim_ev_cl+t2-t1
end subroutine evolve_classical
!-----------------------------------------------------------------
subroutine deriv_xv(vec,acc,kk)
implicit none
real*8,intent(in)::vec(2*nclass),acc(nclass)
real*8,intent(out)::kk(2*nclass)
kk(1:nclass)=vec(nclass+1:2*nclass)
kk(nclass+1:2*nclass)=acc
end subroutine deriv_xv
!-----------------------------------------------------------------
subroutine traj_terminate(iterm)
implicit none
integer,intent(out) :: iterm
iterm=0
end subroutine traj_terminate
!-----------------------------------------------------------------
subroutine compute_mat_diab
implicit none
integer i,j
real*8 t1,t2
!call cpu_time(t1)
mat=0.d0
do i=1,nbasis
do j=1,nbasis
mat(i,j)=-iota/hbar*sum(si_adiab(i,:)*si_adiab(j,:)*V_k(1:nquant))
enddo
enddo
!call cpu_time(t2)
!tim_mat=tim_mat+t2-t1
end subroutine compute_mat_diab
!-----------------------------------------------------------------
subroutine compute_mat_adiab
implicit none
integer i,j
real*8 t1,t2
real*8 V_avg
!call cpu_time(t1)
mat_adiab=-vdotd
V_avg=sum(V_k)/real(nquant)
do i=1,nquant
mat_adiab(i,i)=mat_adiab(i,i)-iota/hbar*(V_k(i)-V_avg)
enddo
!call cpu_time(t2)
!tim_mat=tim_mat+t2-t1
end subroutine compute_mat_adiab
!-----------------------------------------------------------------
subroutine hop
implicit none
integer ifrust
if(flag_hop==1) then
call velocity_adjust(state_tentative,ifrust)
endif
end subroutine hop
!-----------------------------------------------------------------
subroutine velocity_adjust(state_tentative,ifrust)
implicit none
integer,intent(in)::state_tentative
integer,intent(out)::ifrust
real*8 gij,gama,aa,bb,cc,discr,dp(nclass),vd,f1,f2
integer i,j,k,kp
k=state;kp=state_tentative
cc=V_k(state)-V_k(state_tentative)
call compute_dij_2state(x,k,kp,dp)
dp=dp/dsqrt(sum(dp*dp))
aa=0.d0
bb=0.d0
do i=1,nclass
aa=aa+0.5/mass(i)*(dp(i)*dp(i))
bb=bb+(v(i)*dp(i))
enddo
discr=bb**2+4*aa*cc
if(discr<0.d0) then
ifrust=1
cnt_frust=cnt_frust+1.d0
if(flag_frust==0)then
gama=0.d0
call compute_delH_dels_ad
f1=sum(force(k,:)*dp)
f2=sum(force(kp,:)*dp)
vd=sum(v*dp)
!! reverse velocity based on Truhlar's ideas
if(f1*f2<0.d0.and.vd*f2<0.d0) then
!if(f1*f2<0.d0) then
gama=bb/aa
endif
endif
if(flag_frust>0)gama=0.d0
else
ifrust=0
if(bb>=0.d0) gama=(bb-dsqrt(discr))/(2*aa)
if(bb<0.d0) gama=(bb+dsqrt(discr))/(2*aa)
state=state_tentative
delr=0.d0
delp=0.d0
endif
do i=1,nclass
v(i)=v(i)-gama*dp(i)/mass(i)
enddo
!write(20,*)curr_time*1.d15,dp/dsqrt(sum(dp*dp)),x(1),ifrust
!write(21,*)curr_time*1.d15,k,kp,gama
call evaluate_variables(0)
call evaluate_variables(1)
end subroutine velocity_adjust
!-----------------------------------------------------------------
subroutine reverse_velocity
implicit none
end subroutine reverse_velocity
!-----------------------------------------------------------------
subroutine collapse(dt,iflag_coll)
implicit none
real*8,intent(in) :: dt
integer,intent(out) :: iflag_coll
real*8 rnd,gama_collapse,gama_reset
complex*16 su1
integer n,i,j
i=state
if(icollapse==1) then
iflag_coll=0
do n=1,nquant
if(n.ne.state) then
gama_reset=sum((force(n,:)-force(i,:))*dble(delr(n,:)-delr(state,:)))/(2*hbar)
!! CAUTION !! !! Assumes delr(n,n,:) is in direction of v(:) !!
su1=cdabs((V_k(i)-V_k(n))*vdotd(i,n)*sum((delr(n,:)-delr(state,:))*v))/sum(v*v)
gama_collapse=gama_reset-2/hbar*cdabs(su1)
gama_collapse=gama_collapse*dt
gama_reset=-gama_reset*dt
call random_number(rnd)
if(rnd<gama_collapse) then
iflag_coll=1
cnt_collapse=cnt_collapse+1
if(icollapse==1) then
!do j=1,nquant
! if(j.ne.n) ci(j)=ci(j)/dsqrt(1-cdabs(ci(n)**2))
!enddo
!! Erratum: Landry, Subotnik JCP 137, 229901 (2012)
ci(i)=ci(i)/cdabs(ci(i))*dsqrt(cdabs(ci(i))**2+cdabs(ci(n))**2)
ci(n)=0.d0
endif
endif
if(rnd<gama_collapse.or.rnd<gama_reset) then
if(icollapse==1) then
delr(n,:)=0.d0
delp(n,:)=0.d0
endif
endif
endif
enddo
endif
end subroutine collapse
!-----------------------------------------------------------------
subroutine write_output(n,nflag)
!! nflag=0: Writes various variables as a function of time
!! nflag=1: writes minimal useful information at the start and end of trajectory
implicit none
integer,intent(in)::nflag,n
integer i
real*8 t1,t2
real*8 phase
!call cpu_time(t1)
if(nflag==0) then
if(iwrite==1) then
if(mod(n,nstep_write)==1.or.nstep_write==1) then
write(10,'(4es17.7,i5)')curr_time*1.d15,energy/wave_to_J,sum(cdabs(ci)**2),temperature,state
write(11,'(es15.5$)')curr_time*1.d15
write(12,'(5f15.5)')curr_time*1.d15,cdabs(ci(1:2))**2,datan2(dimag(ci(1:2)),real(ci(1:2)))*180/pi
write(13,'(5es15.5)')curr_time*1.d15,vdotd(1,2),dasin(W_overlap(1,2))/dtc,hop_prob_net(3-state),state*1.d0
write(14,'(6f15.5)')curr_time*1.d15,W_overlap(1,1:2),W_overlap(2,1:2),determinant(W_overlap,nquant)
write(15,'(6es15.5)')curr_time*1.d15,delr(1,1)*1.d10,delr(2,1)*1.d10
do i=1,nclass
write(11,'(2es15.5$)')x(i)*1.d10,v(i)
enddo
write(11,*)
endif
endif
endif
if(nflag==1) then
if(iwrite==0)then
write(10,'(5es15.5)')traj_num,energy/wave_to_J,sum(cdabs(ci)**2),temperature
write(11,*) traj_num
write(11,'(es15.5$)')curr_time*1.d15
do i=1,nclass
write(11,'(2es15.5$)')x(i)*1.d10,v(i)
enddo
write(11,*)
write(11,*)
endif
if(iwrite==1) then
write(10,*)"traj num=",traj_num
write(10,*)"standard deviation=",dsqrt((ensq_avg-en_avg**2/dfloat(nst_av))/dfloat(nst_av))/wave_to_J
write(10,*)"ci**2=",sum(cdabs(ci)**2)
write(10,*);write(10,*)
write(11,*);write(11,*)
write(12,*);write(12,*)
write(13,*);write(13,*)
write(14,*);write(14,*)
write(15,*);write(15,*)
endif
endif
!call cpu_time(t2)
!tim_wr_out=tim_wr_out+t2-t1
end subroutine write_output
!-----------------------------------------------------------------
subroutine write_average
!! Writes the final useful output
implicit none
integer i,j
real*8 nf
nf=dfloat(n_traj)
cnt_frust=cnt_frust/nf
cnt_collapse=cnt_collapse/nf
pop=pop/nf
pop_surf=pop_surf/nf
pop_amp=pop_amp/nf
do i=1,nsteps/nstep_avg+1
write(100,'(16f15.7)')(i-1)*nstep_avg*dtc*1.d15,pop(:,i)
enddo
write(101,*) Vc/wave_to_J,cnt_frust,cnt_collapse
end subroutine write_average
!-----------------------------------------------------------------
subroutine evaluate_variables(flag)
implicit none
integer,intent(in):: flag
integer i,j
if(flag==0) then
!! position dependant variables only
call tise
endif
if(flag==1) then
KE_en=0.d0
do i=1,nclass
KE_en=KE_en+0.5*mass(i)*v(i)*v(i)
enddo
energy=pot_en+KE_en
!temperature=2*KE_en/(nclass*kb)
!vdotd=0.d0
!do i=1,nclass
! vdotd=vdotd+v(i)*d_ij(:,:,i)
!enddo
!call compute_vdotd
endif
end subroutine evaluate_variables
!-----------------------------------------------------------------
subroutine tise
!! time independent schrodinger equation
!! Output - pot_en,acc
!! Output - V_k,d_ij
implicit none
integer i,j,k
real*8 Hamil(nbasis,nbasis),ens(nbasis),vect(nbasis,nquant)
real*8 pot_cl,acc_cl(nclass),acc_qm(nclass),dpotcl_dx(nclass)
real*8 si_adiab_old(nquant,nbasis)
real*8 t1,t2
!call cpu_time(t1)
call compute_potential(Hamil,delH_dels)
Hamil_diab=Hamil
call diag(Hamil,nbasis,ens,vect,nquant)
do i=1,nquant
si_adiab(:,i)=vect(:,i)
if(sum(si_adiab(:,i)*si_adiab_prev(:,i))<0.d0)si_adiab(:,i)=-si_adiab(:,i)
enddo