-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSCF_main.f90
2011 lines (1591 loc) · 74.5 KB
/
SCF_main.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
! This code is the fussion of a lot of codes that will be used to carry out
! an SCF of a HHe^+ diatomic.
!
program SCF
implicit none
! +++++++++++++++++++++++++++++ Interface +++++++++++++++++++++++++++++++++
interface
subroutine jacobi_diagonalization(incoming_matrix, m1, diagonalized_mat, &
total_jcbi)
real(kind=8), dimension(:,:), intent(in) :: incoming_matrix
real(kind=8), dimension(:,:), intent(out) :: diagonalized_mat, &
total_jcbi
integer, intent(in) :: m1
end subroutine jacobi_diagonalization
subroutine inverse_sqrt(diagonal_overlap, m1, inverse_diagonal_overlap)
real(kind=8), dimension(:,:), intent(in) :: diagonal_overlap
real(kind=8), dimension(:,:), intent(out) :: inverse_diagonal_overlap
integer, intent(in) :: m1
end subroutine inverse_sqrt
subroutine similarity_transformation(incoming_matrix, transforming_mat, &
m1, transformed_mat)
real(kind=8), dimension(:,:), intent(in) :: incoming_matrix, &
transforming_mat
real(kind=8), dimension(:,:), intent(out) :: transformed_mat
integer, intent(in) :: m1
end subroutine similarity_transformation
subroutine set_up_calculation(full_basis, system_coord, nuclear_repulsion)
real(kind=8), dimension(3,2,2), intent(out) :: full_basis
real(kind=8), dimension(3,2), intent(out) :: system_coord
real(kind=8), intent(out) :: nuclear_repulsion
end subroutine set_up_calculation
subroutine calculate_overlap_matrix(basis_info, coord_array, ovrlpmat)
real(kind=8), dimension(:,:,:), intent(in) :: basis_info
real(kind=8), dimension(:,:), intent(in) :: coord_array
real(kind=8), dimension(:,:), intent(out) :: ovrlpmat
end subroutine calculate_overlap_matrix
subroutine overlap_integral(expA, expB, coeffA, coeffB, basis_distance, &
intgrl)
real(kind=8), intent(in) :: expA, expB, coeffA, coeffB, basis_distance
real(kind=8), intent(out) :: intgrl
end subroutine overlap_integral
subroutine calculate_T_matrix(basis_info, coord_array, kin_engy_mat)
real(kind=8), dimension(:,:,:), intent(in) :: basis_info
real(kind=8), dimension(:,:), intent(in) :: coord_array
real(kind=8), dimension(:,:), intent(out) :: kin_engy_mat
end subroutine calculate_T_matrix
subroutine kinetic_integral(expA, expB, coeffA, coeffB, basis_distance, &
intgrl)
real(kind=8), intent(in) :: expA, expB, coeffA, coeffB, basis_distance
real(kind=8), intent(out) :: intgrl
end subroutine kinetic_integral
subroutine attr_integral(expA, expB, coeffA, coeffB, basis_distance, &
nuclear_coord, pointP_coord, nuclear_charge, intgrl)
real(kind=8), dimension(3), intent(in) :: nuclear_coord, pointP_coord
real(kind=8), intent(in) :: expA, expB, coeffA, coeffB, basis_distance, &
nuclear_charge
real(kind=8), intent(out) :: intgrl
end subroutine attr_integral
subroutine calculate_V_matrices(basis_info, coord_array, V1_matrix, &
V2_matrix, Vtot_matrix)
real(kind=8), dimension(:,:,:), intent(in) :: basis_info
real(kind=8), dimension(:,:), intent(in) :: coord_array
real(kind=8), dimension(:,:), intent(out) :: V1_matrix, V2_matrix, &
Vtot_matrix
end subroutine calculate_V_matrices
subroutine calculate_two_elec_ints(basis_info, coord_array, two_e_ints)
real(kind=8), dimension(:,:,:), intent(in) :: basis_info
real(kind=8), dimension(:,:), intent(in) :: coord_array
real(kind=8), dimension(:,:,:,:), intent(out) :: two_e_ints
end subroutine calculate_two_elec_ints
subroutine repulsion_integral(expA, expB, expC, expD, coeffA, coeffB, &
coeffC, coeffD, ab_dist, cd_dist, pq_dist, intgrl)
real(kind=8), intent(in) :: expA, expB, expC, expD, coeffA, coeffB, &
coeffC, coeffD, ab_dist, cd_dist, pq_dist
real(kind=8), intent(out) :: intgrl
end subroutine repulsion_integral
subroutine generate_G_matrix(two_e_ints, density_mat, gmat)
real(kind=8), dimension(:,:,:,:), intent(in) :: two_e_ints
real(kind=8), dimension(:,:), intent(in) :: density_mat
real(kind=8), dimension(:,:), intent(out) :: gmat
end subroutine generate_G_matrix
subroutine generate_Hcore_matrix(kin_engy_mat, Vtot_matrix, H_core_matrix)
real(kind=8), dimension(:,:), intent(in) :: kin_engy_mat, Vtot_matrix
real(kind=8), dimension(:,:), intent(out) :: H_core_matrix
end subroutine generate_Hcore_matrix
subroutine generate_Fock_matrix(H_core_matrix, gmat, Fock_mat)
real(kind=8), dimension(:,:), intent(in) :: H_core_matrix, gmat
real(kind=8), dimension(:,:), intent(out) :: Fock_mat
end subroutine generate_Fock_matrix
subroutine eigenvalue_vector_ordering(diagonal_mat, eigenvector_mat)
real(kind=8), dimension(:,:), intent(inout) :: diagonal_mat, &
eigenvector_mat
end subroutine eigenvalue_vector_ordering
subroutine matrix_multiplication(matrixA, matrixB, resulting_matrix)
real(kind=8), dimension(:,:), intent(in) :: matrixA, matrixB
real(kind=8), dimension(:,:), intent(out) :: resulting_matrix
end subroutine matrix_multiplication
subroutine generate_new_density_mat(coeff_mat, new_density_mat)
real(kind=8), dimension(:,:), intent(in) :: coeff_mat
real(kind=8), dimension(:,:), intent(out) :: new_density_mat
end subroutine generate_new_density_mat
subroutine determine_convergence(old_density_mat, new_density_mat, &
convergence)
real(kind=8), dimension(:,:), intent(in) :: old_density_mat, &
new_density_mat
logical, intent(out) :: convergence
end subroutine determine_convergence
subroutine calculate_electronic_energy(density_mat, Hcore_mat, fock_mat, &
total_E)
real(kind=8), dimension(:,:), intent(in) :: density_mat, Hcore_mat, &
fock_mat
real(kind=8), intent(out) :: total_E
end subroutine calculate_electronic_energy
end interface
! +++++++++++++++++++++++++ End of Interface ++++++++++++++++++++++++++++++
!
!
!
! +++++++++++++++++++++++++++ Actual Program ++++++++++++++++++++++++++++++
!
! --------------------- Declare stuff used in the main program ------------
real(kind=8), dimension(:,:,:,:), allocatable :: two_e_intgrls
real(kind=8), dimension(:,:,:), allocatable :: basis_set
real(kind=8), dimension(:,:), allocatable :: s, diagonal_s, inv_sqrt_diag_s, &
u_diagonalizer, final_transformed_s, coordinates, T_matrix, V_1_Matrix, &
V_2_Matrix, V_tot_Matrix, G_matrix, density_matrix, Hcore_matrix, &
Fock_matrix, transformed_Fock_matrix, diagonalized_Fock_mat, &
diagonal_trans_Fock_matrix, c_prime_mat, c_matrix, old_density_matrix
real(kind=8) :: total_energy, nuclear_rep
integer :: m, x, y, i, j, iteration_counter
logical :: is_converged = .false.
! ---------- Allocate arrays containing basis set and coordinates ---------
allocate(basis_set(3,2,2))
allocate(coordinates(3,2))
! ----------- Call subroutine to get HHe^+ system's information -----------
call set_up_calculation(basis_set, coordinates, nuclear_rep)
! ********************** A tool for debugging ***********************
! write(*,*)"The H basis information is: "
! do i = lbound(basis_set,1), ubound(basis_set,1)
! write(*,*) (basis_set(i, j, 1), j=lbound(basis_set,2), ubound(basis_set,2))
! end do
! write(*,*)" "
! write(*,*)"The He basis information is: "
! do i = lbound(basis_set,1), ubound(basis_set,1)
! write(*,*) (basis_set(i, j, 2), j=lbound(basis_set,2), ubound(basis_set,2))
! end do
! write(*,*)" "
! write(*,*)"The systems's coordinates are:"
! write(*,*)"H coordinates:"
! do i = 1, 3
! write(*,*) coordinates(i,1)
! end do
! write(*,*)"He coordinates"
! do i = 1, 3
! write(*,*) coordinates(i, 2)
! end do
! *******************************************************************
! -------------- Allocate a bunch of matrices (S, T, V, etc.) -------------
allocate(s(2,2))
allocate(diagonal_s(2,2))
allocate(inv_sqrt_diag_s(2,2))
allocate(u_diagonalizer(2,2))
allocate(final_transformed_s(2,2))
allocate(T_matrix(2,2))
allocate(V_1_Matrix(2,2))
allocate(V_2_Matrix(2,2))
allocate(V_tot_Matrix(2,2))
allocate(two_e_intgrls(2,2,2,2))
allocate(density_matrix(2,2))
allocate(G_matrix(2,2))
allocate(Hcore_matrix(2,2))
allocate(Fock_matrix(2,2))
allocate(transformed_Fock_matrix(2,2))
allocate(diagonalized_Fock_mat(2,2))
allocate(diagonal_trans_Fock_matrix(2,2))
allocate(c_prime_mat(2,2))
allocate(c_matrix(2,2))
allocate(old_density_matrix(2,2))
m = 2 !<--- Useful to allocate intermediates in matrix manipulation
! subroutines
! -------------------- Initialize iteration counter ---------------------
iteration_counter = 0 !<--- counter to keep track of iterations
! -- Call subroutine to calculate kinetic energy integrals, get T matrix --
call calculate_T_matrix(basis_set, coordinates, T_matrix)
! ********************** A tool for debugging ***********************
! write(*,*)"The T matrix is"
! do i = lbound(T_matrix, 1), ubound(T_matrix,1)
! write(*,*) (T_matrix(i, j), j=lbound(T_matrix,2), ubound(T_matrix,2))
! end do
! write(*,*)" "
! *******************************************************************
! --- Call subroutine calculate electron-nuclear integrals, get V matrix --
call calculate_V_matrices(basis_set, coordinates, V_1_Matrix, V_2_Matrix, &
V_tot_Matrix)
! ********************** A tool for debugging ***********************
! write(*,*) "The V1 Matrix is:"
! write(*,*) " "
! do i = lbound(V_1_Matrix,1), ubound(V_1_Matrix,1)
! write(*,*) (V_1_Matrix(i, j), j=lbound(V_1_Matrix,2), &
! ubound(V_1_Matrix,2))
! end do
! write(*,*) " "
! write(*,*) "The V2 Matrix is:"
! write(*,*) " "
! do i = lbound(V_2_Matrix,1), ubound(V_2_Matrix,1)
! write(*,*) (V_2_Matrix(i, j), j=lbound(V_2_Matrix,2), &
! ubound(V_2_Matrix,2))
! end do
! write(*,*) " "
! write(*,*) "The Vtotal Matrix is:"
! write(*,*) " "
! do i = lbound(V_tot_Matrix,1), ubound(V_tot_Matrix,1)
! write(*,*) (V_tot_Matrix(i, j), j=lbound(V_tot_Matrix,2), &
! ubound(V_tot_Matrix,2))
! end do
! *******************************************************************
! --------------- Call subroutine to generate H_core matrix ---------------
call generate_Hcore_matrix(T_matrix, V_tot_Matrix, Hcore_matrix)
! ********************** A tool for debugging ***********************
! write(*,*) "The H_core Matrix is:"
! write(*,*) " "
! do i = lbound(Hcore_matrix,1), ubound(Hcore_matrix,1)
! write(*,*) (Hcore_matrix(i, j), j=lbound(Hcore_matrix,2), &
! ubound(Hcore_matrix,2))
! end do
! *******************************************************************
! -------- Call subroutine to calculate all 2 electron integrals ----------
call calculate_two_elec_ints(basis_set, coordinates, two_e_intgrls)
! ********************** A tool for debugging ***********************
! write(*,*)"The following integrals should be te same: "
! write(*,*)" "
! write(*,*) two_e_intgrls(1,2,1,2) ! 1,2,3,4
! write(*,*)" "
! write(*,*) two_e_intgrls(1,2,1,2) ! 3,2,1,4
! write(*,*)" "
! write(*,*) two_e_intgrls(1,2,1,2) ! 1,4,3,2
! write(*,*)" "
! write(*,*) two_e_intgrls(1,2,1,2) ! 3,4,1,2
! write(*,*)" "
! write(*,*) two_e_intgrls(2,1,2,1) ! 2,1,4,3
! write(*,*)" "
! write(*,*) two_e_intgrls(2,1,2,1) ! 4,1,2,3
! write(*,*)" "
! write(*,*) two_e_intgrls(2,1,2,1) ! 2,3,4,1
! write(*,*)" "
! write(*,*) two_e_intgrls(2,1,2,1) ! 4,3,2,1
! *******************************************************************
! ------------- Call subroutine to calculate overlap matrix ---------------
call calculate_overlap_matrix(basis_set, coordinates, s)
! ********************** A tool for debugging ***********************
! write(*,*)"The overlap matrix is"
! do i = lbound(s, 1), ubound(s,1)
! write(*,*) (s(i, j), j=lbound(s,2), ubound(s,2))
! end do
! write(*,*)" "
! *******************************************************************
! ------------------ Call subroutine for diagonalization ------------------
call jacobi_diagonalization(s, m, diagonal_s, u_diagonalizer)
! ********************** A tool for debugging ***********************
! WRITE(*,*)"The diagonalization gives:"
! do i = lbound(diagonal_s,1), ubound(diagonal_s,1)
! WRITE(*,*) (diagonal_s(i,y), y = lbound(diagonal_s,1), ubound(diagonal_s,1))
! end do
! WRITE(*,*)" "
! *******************************************************************
! ********************** A tool for debugging ***********************
! WRITE(*,*)"Final eigenvector matrix" ! Print out final Jtot matrix
! do i = lbound(u_diagonalizer,1), ubound(u_diagonalizer,1)
! WRITE(*,*) (u_diagonalizer(i,y), y = lbound(u_diagonalizer,1), &
! &ubound(u_diagonalizer,1))
! end do
! WRITE(*,*)" "
! *******************************************************************
! --- Call subroutine to take inverse square roots of diagonal elements ---
call inverse_sqrt(diagonal_s, m, inv_sqrt_diag_s)
! ********************** A tool for debugging ***********************
! WRITE(*,*)"Inverse square root values:"
! do i = lbound(inv_sqrt_diag_s,1), ubound(inv_sqrt_diag_s,1)
! WRITE(*,*) (inv_sqrt_diag_s(i,y), y = lbound(inv_sqrt_diag_s,1), &
! &ubound(diagonal_s,1))
! end do
! WRITE(*,*)" "
! *******************************************************************
! ----------- Call subroutine to perform U·s^(-1/2)·U^t -------------------
call similarity_transformation(inv_sqrt_diag_s, u_diagonalizer, m, &
final_transformed_s)
! ********************** A tool for debugging ***********************
! WRITE(*,*)"Final S to the -1/2:"
! do i = lbound(final_transformed_s,1), ubound(final_transformed_s,1)
! WRITE(*,*) (final_transformed_s(i,y), y = lbound(final_transformed_s,1), &
! &ubound(final_transformed_s,1))
! end do
! WRITE(*,*)" "
! *******************************************************************
! -- Set initial density matrix to the null matrix (so that F = H_core) ---
density_matrix = 0.0
! ------------------------- Perform iterations ----------------------------
do while (.not.is_converged)
old_density_matrix = density_matrix
! --------------- Call subroutine to generate the G matrix ----------------
call generate_G_matrix(two_e_intgrls, density_matrix, G_matrix)
! ********************** A tool for debugging ***********************
! write(*,*)"The G matrix is: "
! write(*,*)" "
! do i = lbound(G_matrix, 1), ubound(G_matrix, 1)
! write(*,*) (G_matrix(i, j), j=lbound(G_matrix,2), &
! ubound(G_matrix,2))
! end do
! *******************************************************************
! ---------------- Call subroutine to generate Fock matrix ----------------
call generate_Fock_matrix(Hcore_matrix, G_matrix, Fock_matrix)
! ********************** A tool for debugging ***********************
! write(*,*)"The Fock matrix is: "
! write(*,*)" "
! do i = lbound(Fock_matrix, 1), ubound(Fock_matrix, 1)
! write(*,*) (Fock_matrix(i, j), j=lbound(Fock_matrix,2), &
! ubound(Fock_matrix,2))
! end do
! write(*,*)" "
! *******************************************************************
! ------------ Call subroutine to calculate electronic energy -------------
call calculate_electronic_energy(density_matrix, Hcore_matrix, &
Fock_matrix, total_energy)
write(*,*)"---------- Electronic energy:", total_energy, "------------------"
write(*,*)" "
write(*,*)" "
write(*,*)"********* Iteration:", iteration_counter, "*********"
! ---------------- Call subroutine to transform Fock matrix ---------------
call similarity_transformation(Fock_matrix, final_transformed_s, m, &
transformed_Fock_matrix)
! ********************** A tool for debugging ***********************
! write(*,*)"The transformed Fock matrix is: "
! write(*,*)" "
! do i = lbound(transformed_Fock_matrix, 1), ubound(transformed_Fock_matrix, 1)
! write(*,*) (transformed_Fock_matrix(i, j), &
! j=lbound(transformed_Fock_matrix,2), &
! ubound(transformed_Fock_matrix,2))
! end do
! write(*,*)" "
! *******************************************************************
! ------ Call subroutine to diagonalize the transformed Fock matrix -------
call jacobi_diagonalization(transformed_Fock_matrix, m, &
diagonal_trans_Fock_matrix, c_prime_mat)
! ********************** A tool for debugging ***********************
! write(*,*)"The diagonalized transformed Fock matrix is: "
! write(*,*)" "
! do i = lbound(diagonal_trans_Fock_matrix, 1), &
! ubound(diagonal_trans_Fock_matrix, 1)
! write(*,*) (diagonal_trans_Fock_matrix(i, j), &
! j=lbound(diagonal_trans_Fock_matrix,2), &
! ubound(diagonal_trans_Fock_matrix,2))
! end do
! write(*,*)" "
! write(*,*)"The unordered ordered C' matrix is: "
! write(*,*)" "
! do i = lbound(c_prime_mat,1), ubound(c_prime_mat,1)
! write(*,*) (c_prime_mat(i,j), j=lbound(c_prime_mat,2), &
! ubound(c_prime_mat,2))
! end do
! write(*,*)" "
! *******************************************************************
! - Call subroutine to order eigenvalues/vectors in diagonal Fock Matrix --
call eigenvalue_vector_ordering(diagonal_trans_Fock_matrix, c_prime_mat)
! ********************** A tool for debugging ***********************
! write(*,*)"The ordered transformed Fock matrix is: "
! write(*,*)" "
! do i = lbound(diagonal_trans_Fock_matrix, 1), &
! ubound(diagonal_trans_Fock_matrix, 1)
! write(*,*) (diagonal_trans_Fock_matrix(i, j), &
! j=lbound(diagonal_trans_Fock_matrix,2), &
! ubound(diagonal_trans_Fock_matrix,2))
! end do
! write(*,*)" "
! write(*,*)"The ordered C' matrix is: "
! write(*,*)" "
! do i = lbound(c_prime_mat,1), ubound(c_prime_mat,1)
! write(*,*) (c_prime_mat(i,j), j=lbound(c_prime_mat,2), &
! ubound(c_prime_mat,2))
! end do
! write(*,*)" "
! *******************************************************************
! -------------- Call subroutine to obtain C from S^-1/2*C'----------------
call matrix_multiplication(final_transformed_s, c_prime_mat, c_matrix)
! ********************** A tool for debugging ***********************
! write(*,*)"The C matrix is: "
! write(*,*)" "
! do i = lbound(c_matrix,1), ubound(c_matrix,1)
! write(*,*) (c_matrix(i,j), j=lbound(c_matrix,2), &
! ubound(c_matrix,2))
! end do
! write(*,*)" "
! *******************************************************************
! -------- Call subroutine to obtain new density matrix from C ------------
call generate_new_density_mat(c_matrix, density_matrix)
! ********************** A tool for debugging ***********************
write(*,*)"The new density matrix is: "
write(*,*)" "
do i = lbound(density_matrix,1), ubound(density_matrix,1)
write(*,*) (density_matrix(i,j), j=lbound(density_matrix,2), &
ubound(density_matrix,2))
end do
write(*,*)" "
! *******************************************************************
! -------------- Call subroutine to evaluate convergence ------------------
call determine_convergence(old_density_matrix, density_matrix, is_converged)
! ********************** A tool for debugging ***********************
! write(*,*)"Is converged: "
! write(*,*) is_converged
! *******************************************************************
iteration_counter = iteration_counter + 1
end do
write(*,*) " "
write(*,*) "************** Density converged **************"
write(*,*) " "
write(*,*) "+++++++++++++++++++++++++++++++++"
write(*,*) "+ Final data +"
write(*,*) "+++++++++++++++++++++++++++++++++"
write(*,*) " "
write(*,*)"Orbitals:"
write(*,*)" "
do i = lbound(c_matrix,1), ubound(c_matrix,1)
write(*,*) (c_matrix(i,j), j=lbound(c_matrix,2), &
ubound(c_matrix,2))
end do
write(*,*)" "
write(*,*) "Orbital energies:"
write(*,*)" "
do i = lbound(diagonal_trans_Fock_matrix, 1), &
ubound(diagonal_trans_Fock_matrix, 1)
write(*,*) (diagonal_trans_Fock_matrix(i, j), &
j=lbound(diagonal_trans_Fock_matrix,2), &
ubound(diagonal_trans_Fock_matrix,2))
end do
write(*,*)" "
write(*,*)"Final electronic energy:"
write(*,*) total_energy
write(*,*)" "
write(*,*)"Final system energy:"
write(*,*) total_energy + nuclear_rep
end program SCF
!
!
! It's not a phase mom, this is who I really am
!
!
subroutine jacobi_diagonalization(incoming_matrix, m1, diagonalized_mat, &
total_jcbi)
! ------------------- Declare stuff coming in and out ---------------------
real(kind=8), dimension(:,:), intent(in) :: incoming_matrix
real(kind=8), dimension(:,:), intent(out) :: diagonalized_mat, &
total_jcbi
integer, intent(in) :: m1
!
!
! ------------------ Declare stuff used in the subroutine -----------------
real(kind=8), dimension(:,:), allocatable :: J, Jt, C, E, Jtot, Temp, A
real(kind=8), dimension(:), allocatable :: VA, VE, DV, EIG, Tem1
integer :: i, j2, k, l, x, y, z, deltacount
real(kind=8) :: pp, pq, qp, qq, phi, jpp, jpq, jqp, product, delta, aux1
! ---------------- Allocate all the intermediates to use ------------------
allocate (A(m1,m1))
allocate (J(m1,m1))
allocate (Jt(m1,m1))
allocate (C(m1,m1))
allocate (E(m1,m1))
allocate (Jtot(m1,m1))
allocate (Temp(m1,m1))
allocate (VA(m1))
allocate (VE(m1))
allocate (DV(m1))
allocate (EIG(m1))
allocate (Tem1(m1))
A = incoming_matrix !<- Put incoming matrix into A
! -------------------- Begin diagonalization process -----------------------
! Initialize Jtot (Eigenvector matrix) as an identity matrix
! so that the first time it is multiplied it does not alter
! the incoming matrix
do x = lbound(Jtot,1), ubound(Jtot,1) !<- Row counter
do y = lbound(Jtot,2), ubound(Jtot,2) !<- Column counter
if (x == y) then !<- Elements in the diagonal are 1
Jtot(x,y) = 1
else !<- Elements anywhere else are 0
Jtot(x,y) = 0
end if
end do
end do
deltacount = 0 ! Counter for convergence
! When there is virutally no change in the elemtns of
! the rotated matrix, and the previous one in 5 consecutive
! loops, the code will stop and give the resulting matrix back
do while (deltacount < 5)
! ------------------ Begin sweep thorugh the matrix ------------------------
!
! Since we only want to zero the off-diagonal elements of matrix A, counter k
! will go from i+1 to the end, ensuring that no element in the diagonal will
! be zeroed.
do i = lbound(A,1), ubound(A,1) - 1 !<- Row counter - p
do k = i+1, ubound(A,2) !<- Column counter - q
! ********************** A tool for debugging ***********************
! WRITE(*,*)"Matrix A to use now"
! do x = lbound(A,1), ubound(A,1)
! WRITE(*,*) (A(x,y), y = lbound(A,2), ubound(A,2))
! end do
! WRITE(*,*)" "
! *******************************************************************
pp = A(i, i) !<- Element pp
pq = A(i, k) !<- Element pq
qp = A(k, i) !<- Element qp
qq = A(k, k) !<- Element qq
! ********************** A tool for debugging ***********************
! WRITE(*,*)"Coefficients to use"
! WRITE(*,*) pp, pq
! WRITE(*,*) qp, qq
! WRITE(*,*)" "
! *******************************************************************
! We have extracted pp, pq, qp, qq
! Now calculate phi
phi = 0 !<- Clean phi
phi = (0.5) * (atan((2*pq)/(qq-pp))) ! Compute phi
! ********************** A tool for debugging ***********************
! WRITE(*,*)"Phi to use", phi
! WRITE(*,*)" "
! *******************************************************************
! Now create J
! J is an indentity matrix first
do x = lbound(J,1), ubound(J,1) !<- Row counter
do y = lbound(J,2), ubound(J,2) !<- Column counter
if (x == y) then !<- Elements in the diagonal are 1
J(x,y) = 1
else ! Elements anywhere else are 0
J(x,y) = 0
end if
end do
end do
! Now plug in the sines and cosines in J
J(i,i) = cos(phi) !<- Same as pp
J(i,k) = sin(phi) !<- pq
J(k,i) = -1 * sin(phi) !<- qp
J(k,k) = cos(phi) !<- and qq
! ********************** A tool for debugging ***********************
! WRITE(*,*)"Matrix J"
! do x = lbound(J,1), ubound(J,1)
! WRITE(*,*) (J(x,y), y = lbound(J,2), ubound(J,2))
! end do
!WRITE(*,*)"One do"
! *******************************************************************
! Begin Jtot * J multiplication
product = 0
do z = lbound(Jtot,1), ubound(Jtot,1) !<- This counter is the "slowest", it will allow us to move the rows in Jtot and Temp
do x = lbound(J,2), ubound(J,2) !<- This counter sets the column in matrix J
do y = lbound(Jtot,2), ubound(Jtot,2) !<- This counter is the "fastest", it sets the column in Jtot and row in J
product = Jtot(z,y)*J(y,x) + product !<- Multiply each element and add
end do
Temp(z,x) = product !<- Set the result in C
product = 0 !<- Clear variable value
end do
end do
! We now have matrix Jtot(old)*J in Temp,
! now set Jtot(new) = temp
! for next iteration
Jtot = Temp
! We now have matrix Jtot(old)*J in Temp,
! now set Jtot(new) = temp, for next iteration
! Jtot = Temp
! End Jtot * J multiplication
! Generate J (transpose)
do x = lbound(J,1), ubound(J,1)
do y = lbound(J,1), ubound(J,1)
Jt(y,x) = J(x,y)
end do
end do
! ********************** A tool for debugging ***********************
! WRITE(*,*)"Matrix Jt"
! do x = lbound(Jt,1), ubound(Jt,1)
! WRITE(*,*) (Jt(x,y), y = lbound(Jt,2), ubound(Jt,2))
! end do
! *******************************************************************
product = 0 !<- Clear variable
! Obtain matrix C, which is the product of A·J
do z = lbound(A,1), ubound(A,1) !<- This counter is the "slowest", it will allow us to move the rows in A and C
do x = lbound(J,2), ubound(J,2) !<- This counter sets the column in matrix B
do y = lbound(A,2), ubound(A,2) !<- This counter is the "fastest", it sets the column in A and row in B
product = A(z,y)*J(y,x) + product !<- Multiply each element and add
end do
C(z,x) = product !<- Set the result in C
product = 0 !<- Clear variable value
end do
end do
! Once we have C, multiply it by J (tranpose)
! ********************** A tool for debugging ***********************
! WRITE(*,*)"A·J"
! do x = lbound(C,1), ubound(C,1)
! WRITE(*,*) (C(x,y), y = lbound(C,2), ubound(C,2))
! end do
! WRITE(*,*)" "
! *******************************************************************
product = 0 !<- Clear variable
! Perform J·C to complete the rotation/similarity transformation
do z = lbound(Jt,1), ubound(Jt,1) !<- This counter is the "slowest", it will allow us to move the rows in Pt and B
do x = lbound(C,2), ubound(C,2) !<- This counter sets the column in matrix C
do y = lbound(Jt,2), ubound(Jt,2) !<- This counter is the "fastest", it sets the column in Pt and row in C
product = Jt(z,y)*C(y,x) + product !<- Multiply each element and add
end do
E(z,x) = product !<- Set the result in C
product = 0 !<- Clear variable value
end do
end do
! ********************** A tool for debugging ***********************
! WRITE(*,*)"Jt·A·J"
! do x = lbound(E,1), ubound(E,1)
! WRITE(*,*) (E(x,y), y = lbound(E,2), ubound(E,2))
! end do
! WRITE(*,*)" "
! *******************************************************************
do x = lbound(A,1), ubound(A,1) !<- Extract diagonal values of A
VA(x) = A(x,x)
end do
do x = lbound(E,1), ubound(E,1) !<- Extract diagonal values of E
VE(x) = E(x,x)
end do
DV = ABS(VE)-ABS(VA) ! Take the difference in the norm of the diagonal elements of A and B
delta = SUM(DV) ! Sum all elements in DV. If all differences were small, delta should be small
! ********************** A tool for debugging ***********************
! WRITE(*,*) delta
! *******************************************************************
if (delta < 1E-10) then !<- When delta is less than E-10, add 1 to the counter
deltacount = deltacount + 1
else if (delta > 1E-10) then !<- When delta is bigger than E-10, clean counter
deltacount = 0 !<- This ensures convergence through multiple consecutive identical results
end if
A = E !<- Take the rotated matrix and redo
end do
end do !<- End sweep
end do !<- End do while
diagonalized_mat = A
total_jcbi = Jtot
end subroutine jacobi_diagonalization
!
!
! Dad, why do I look like the mail man?
!
!
subroutine inverse_sqrt(diagonal_overlap, m1, inverse_diagonal_overlap)
! ------------------- Declare stuff coming in and out ---------------------
real(kind=8), dimension(:,:), intent(in) :: diagonal_overlap
real(kind=8), dimension(:,:), intent(out) :: inverse_diagonal_overlap
integer, intent(in) :: m1
!
!
!
! ------------------ Declare stuff used in the subroutine -----------------
integer :: i, j
! Take inverse square root of diagonal elements in diagonalized overlap matrix
do i = lbound(diagonal_overlap, 1), ubound(diagonal_overlap, 1)
do j = lbound(diagonal_overlap, 2), ubound(diagonal_overlap, 2)
if (i == j) then
inverse_diagonal_overlap(i, j) = 1/sqrt(diagonal_overlap(i,j))
else
inverse_diagonal_overlap(i,j) = 0.0
end if
end do
end do
end subroutine inverse_sqrt
!
!
! Does this look infected?
!
!
subroutine similarity_transformation(incoming_matrix, transforming_mat, m1, &
transformed_mat)
! ------------------- Declare stuff coming in and out ---------------------
real(kind=8), dimension(:,:), intent(in) :: incoming_matrix, &
transforming_mat
real(kind=8), dimension(:,:), intent(out) :: transformed_mat
integer, intent(in) :: m1
! ------------------ Declare stuff used in the subroutine -----------------
real(kind=8), dimension(:,:), allocatable :: A, P, C, Pt, B
real(kind=8) :: product
integer :: x, y, i, j
! ---------------- Allocate all the intermediates to use ------------------
allocate (A(m1,m1))
allocate (P(m1,m1))
allocate (C(m1,m1))
allocate (Pt(m1,m1))
allocate (B(m1,m1))
! ---------- Set variables so that I don't have to rewrite this code ------
! This code performs P(trans)*A*P
A = incoming_matrix
P = transforming_mat
! --------------- Begin similarity transformation process -----------------
! Generate P transpose
do x = lbound(P,1), ubound(P,1)
do y = lbound(P,2), ubound(P,2)
Pt(y,x) = P(x,y)
end do
end do
product = 0
! Obtain matrix C, which is the product of A·Pt
do i = lbound(A,1), ubound(A,1) !<- This counter is the "slowest", it will allow us to move the rows in A and C
do x = lbound(P,2), ubound(P,2) !<- This counter sets the column in matrix B
do y = lbound(A,2), ubound(A,2) !<- This counter is the "fastest", it sets the column in A and row in B
product = A(i,y)*Pt(y,x) + product !<- Multiply each element and add
end do
C(i,x) = product !<- Set the result in C
product = 0 !<- Clear variable value
end do
end do
! Once we have matrix C, multiply P by it
do i = lbound(P,1), ubound(P,1) !<- This counter is the "slowest", it will allow us to move the rows in P and C
do x = lbound(C,2), ubound(C,2) !<- This counter sets the column in matrix C
do y = lbound(P,2), ubound(P,2) !<- This counter is the "fastest", it sets the column in P and row in C
product = P(i,y)*C(y,x) + product !<- Multiply each element and add
end do
B(i,x) = product !<- Set the result in C
product = 0 !<- Clear variable value
end do
end do
transformed_mat = B
end subroutine similarity_transformation
!
!
! You should respect my opinion! *their opinion: 2+3 = 7*
!
!
subroutine set_up_calculation(full_basis, system_coord, nuclear_repulsion)
! ------------------- Declare stuff coming in and out ---------------------
real(kind=8), dimension(3,2,2), intent(out) :: full_basis
real(kind=8), dimension(3,2), intent(out) :: system_coord
real(kind=8), intent(out) :: nuclear_repulsion
! ------------------ Declare stuff used in the subroutine -----------------
! The ranks of these arrays are specific for STO-3G
real(kind=8), DIMENSION(3,2) :: basis_H, basis_He
REAL(kind=8), DIMENSION(3) :: coordinates_H, coordinates_He
real(kind=8) :: internuclear_dist
INTEGER :: i, j, k, contraction_H, contraction_He
! ------------------ Setting up the system: Basis set ---------------------
! Basis function info is stored in a 2D array. Column 1 contains exponents,
! column 2 contains the corresponding contraction coefficients.
! ------------------------ BSE STO-3G basis set ---------------------------
! basis_H = reshape((/0.3425250914E+01, 0.1543289673E+00, 0.6239137298E+00, &
! 0.5353281423E+00, 0.1688554040E+00, 0.4446345422E+00/), (/3,2/), ORDER=(/2,1/))
! basis_He = reshape((/0.6362421394E+01, 0.1543289673E+00, 0.1158922999E+01, &
! 0.5353281423E+00, 0.3136497915E+00, 0.4446345422E+00/), (/3,2/), ORDER=(/2,1/))
! ---------------------- Szabo & Ostlund basis set ------------------------
basis_H = reshape((/0.168856157, 0.444635, 0.62391349, 0.535328, &
3.425250016, 0.154329/), (/3,2/), ORDER=(/2,1/))
basis_He = reshape((/0.48084429, 0.444635, 1.776691148, 0.535328, &
9.753934616, 0.154329/), (/3,2/), ORDER=(/2,1/))
! Set up the array that packs the basis set info
do i = lbound(full_basis,3), ubound(full_basis,3)
do j = lbound(full_basis,1), ubound(full_basis,1)
do k = lbound(full_basis,2), ubound(full_basis,2)
if (i==1) then
full_basis(j,k,i) = basis_H(j,k)
elseif (i==2) then
full_basis(j,k,i) = basis_He(j,k)
end if
end do
end do
end do
! ----------------- Setting up the system: Coordinates --------------------
!
! Array contains coordinates at x, y, z in positions 1, 2, 3 respectively.
coordinates_H(1) = 0.7316
coordinates_H(2) = 0.0
coordinates_H(3) = 0.0
coordinates_He(1) = -0.7316
coordinates_He(2) = 0.0
coordinates_He(3) = 0.0
! Setting the coordinates of the diatomic in an array
do i = 1, 2 !<--- Two atoms, two columns
do j = 1, 3 !<--- Three coordinates
if (i == 1) then
system_coord(j,i) = coordinates_H(j)
elseif (i==2) then
system_coord(j,i) = coordinates_He(j)
end if
end do
end do
internuclear_dist = 0.0
do i = 1, 3 !<--- Calculate internuclear distance
internuclear_dist = internuclear_dist + &
((coordinates_H(i)-coordinates_He(i))*&
(coordinates_H(i)-coordinates_He(i)))
end do
internuclear_dist = sqrt(internuclear_dist)
nuclear_repulsion = 2/internuclear_dist
end subroutine set_up_calculation
!
!
! When life gives you lemons... you start doing tequila shots
!
!
subroutine calculate_overlap_matrix(basis_info, coord_array, ovrlpmat)
! ------------------- Declare stuff coming in and out ---------------------
real(kind=8), dimension(:,:,:), intent(in) :: basis_info
real(kind=8), dimension(:,:), intent(in) :: coord_array
real(kind=8), dimension(:,:), intent(out) :: ovrlpmat