File: SCF_restricted.cc

package info (click to toggle)
ergo 3.8.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 17,568 kB
  • sloc: cpp: 94,763; ansic: 17,785; sh: 10,701; makefile: 1,403; yacc: 127; lex: 116; awk: 23
file content (2000 lines) | stat: -rw-r--r-- 74,933 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
/* Ergo, version 3.8.2, a program for linear scaling electronic structure
 * calculations.
 * Copyright (C) 2023 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
 * and Anastasia Kruchinina.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Primary academic reference:
 * Ergo: An open-source program for linear-scaling electronic structure
 * calculations,
 * Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
 * Kruchinina,
 * SoftwareX 7, 107 (2018),
 * <http://dx.doi.org/10.1016/j.softx.2018.03.005>
 * 
 * For further information about Ergo, see <http://www.ergoscf.org>.
 */

/** @file SCF_restricted.cc

    @brief Class for self-consistent field (SCF) procedure;
    spin-restricted case.

    @author: Elias Rudberg <em>responsible</em>.
*/

#include <sstream>
#include "SCF_restricted.h"
#include "output.h"
#include "scf_utils.h"
#include "utilities.h"
#include "diis_restricted.h"
#include "density_projection.h"
#include "density_description_file.h"
#include "matrix_utilities.h"
#include "machine_epsilon.h"
#include "units.h"
#include "atom_labels.h"
#include "integral_matrix_wrappers.h"
#include "dipole_moment.h"


SCF_restricted::SCF_restricted(const Molecule&        molecule_,
                               const Molecule&        extraCharges_,
                               const BasisInfoStruct& basisInfo_,
                               const IntegralInfo&    integralInfo_,
                               const char             *guessDmatFileNamePtr,
                               const JK::Params&      J_K_paramsPtr,
                               const Dft::GridParams& gridParams_,
                               const SCF::Options&    scfopts,
                               const SCF::MatOptions& matOpts_,
                               ergo_real              threshold_integrals_1el_input)
   :   SCF_general(molecule_,
                   extraCharges_,
                   basisInfo_,
                   integralInfo_,
                   guessDmatFileNamePtr,
                   J_K_paramsPtr,
                   gridParams_,
                   scfopts,
                   matOpts_,
                   threshold_integrals_1el_input)
{
   DIIS = new DIISManagerRestricted;


   DensFromFock.do_restricted_calculations(); // set factor = 2
   DensFromFock.set_no_occupied_orbs(noOfElectrons / 2);
}


SCF_restricted::~SCF_restricted()
{
   delete ((DIISManagerRestricted *)DIIS);
}


void SCF_restricted::get_Fock_matrix(symmMatrix& FockMatrix_)
{
   FockMatrix.readFromFile();
   FockMatrix_ = FockMatrix;
   FockMatrix.writeToFile();
}


void SCF_restricted::get_density_matrix(symmMatrix& densityMatrix_)
{
   densityMatrix.readFromFile();
   densityMatrix_ = densityMatrix;
   densityMatrix.writeToFile();
}


void SCF_restricted::initialize_matrices()
{
   densityMatrix.resetSizesAndBlocks(matOpts.size_block_info,
                                     matOpts.size_block_info);

   densityMatrix_core.resetSizesAndBlocks(matOpts.size_block_info, matOpts.size_block_info);

   twoel_matrix_core.resetSizesAndBlocks(matOpts.size_block_info, matOpts.size_block_info);

   FockMatrix.resetSizesAndBlocks(matOpts.size_block_info,
                                  matOpts.size_block_info);
   Fprev.resetSizesAndBlocks(matOpts.size_block_info,
                             matOpts.size_block_info);
   Dprev.resetSizesAndBlocks(matOpts.size_block_info,
                             matOpts.size_block_info);
   F_ort_prev.resetSizesAndBlocks(matOpts.size_block_info,
                                  matOpts.size_block_info);
   // D_ort_prev.resetSizesAndBlocks(matOpts.size_block_info,
   //                                 matOpts.size_block_info);
   bestFockMatrixSoFar.resetSizesAndBlocks(matOpts.size_block_info,
                                           matOpts.size_block_info);
   bestFockMatrixSoFar2.resetSizesAndBlocks(matOpts.size_block_info,
                                            matOpts.size_block_info);
   ErrorMatrix.resetSizesAndBlocks(matOpts.size_block_info,
                                   matOpts.size_block_info);
}


void SCF_restricted::check_params()
{
   if (noOfElectrons % 2 != 0)
   {
      do_output(LOG_CAT_ERROR, LOG_AREA_SCF, "error: odd number of electrons in restricted calculation");
      throw "error: odd number of electrons in restricted calculation";
   }
}


void SCF_restricted::get_starting_guess_density()
{
   // set up starting guess

   int n = basisInfo.noOfBasisFuncs;

   DensFromFock.set_SCF_step(SCF_step);

   if (guessDmatFileName != NULL)
   {
      do_output(LOG_CAT_INFO, LOG_AREA_SCF, "getting starting guess density from file '%s'", guessDmatFileName);
      int        noOfDensityMatrices = 1;
      symmMatrix *matrixList[2];
      matrixList[0] = &densityMatrix;

      if (load_density_and_project_sparse(DensFromFock,
                                          guessDmatFileName,
                                          noOfDensityMatrices,
                                          &integralInfo,
                                          basisInfo,
                                          S_symm,
                                          matrixList,
                                          &noOfElectrons,
                                          matOpts.size_block_info,
                                          matOpts.permutationHML,
                                          matOpts.sparse_threshold) != 0)
      {
         do_output(LOG_CAT_ERROR, LOG_AREA_SCF, "error in load_density_and_project_sparse");
         throw "error in load_density_and_project_sparse";
      }
   }
   else
   {
      if (scfopts.use_simple_starting_guess == 1)
      {
         if (get_simple_starting_guess_sparse(n, noOfElectrons, densityMatrix) != 0)
         {
            throw "error in get_simple_starting_guess_sparse";
         }
         densityMatrix.writeToFile();
      }
      else if (scfopts.use_diag_guess_from_file == 1)
      {
         if (get_diag_matrix_from_file(n, densityMatrix, "diagdens.txt",
                                       matOpts.permutationHML) != 0)
         {
            throw "error in get_diag_matrix_from_file";
         }
         densityMatrix.writeToFile();
      }
      else
      {
         do_output(LOG_CAT_INFO, LOG_AREA_SCF,
                   "calling get_dens_from_fock to diagonalize H_core for starting guess, n = %i, sparse_threshold = %g",
                   n, (double)matOpts.sparse_threshold);

         symmMatrix F_ort_prev_dummy;
         F_ort_prev_dummy.resetSizesAndBlocks(matOpts.size_block_info,
                                              matOpts.size_block_info);
         F_ort_prev_dummy.writeToFile();
         densityMatrix.writeToFile();

         DensFromFock.clean_eigs_intervals();

         int use_diag_on_error = DensFromFock.get_use_diag_on_error();

         if (DensFromFock.get_use_diag_on_error_guess() == 1)
         {
            DensFromFock.set_use_diag_on_error();
         }


         if (DensFromFock.get_dens_from_fock(H_core_Matrix,
                                             densityMatrix,
                                             F_ort_prev_dummy) != 0)
         {
            throw "SCF_restricted::get_starting_guess_density: Error in get_dens_from_fock_general";
         }

         if (use_diag_on_error != 1)
         {
            DensFromFock.unset_use_diag_on_error();
         }
      }   // END ELSE use H_core
   }  // END ELSE no dmat given

   densityMatrix.readFromFile();
   output_sparsity_symm(n, densityMatrix, "starting guess density matrix");
   densityMatrix.writeToFile();

   densityMatrix_core.writeToFile(); // densityMatrix_core (if used) also needs to be written to file
}


void SCF_restricted::add_random_disturbance_to_starting_guess()
{
   if (scfopts.sg_disturb_specific_elements > SCF::DISTURB_ELEMENT_MAX_COUNT)
   {
      throw "Error in SCF_restricted::add_random_disturbance_to_starting_guess: (scfopts.sg_disturb_specific_elements > SCF::DISTURB_ELEMENT_MAX_COUNT)";
   }
   int n = basisInfo.noOfBasisFuncs;
   densityMatrix.readFromFile();
   add_disturbance_to_matrix(n,
                             densityMatrix,
                             scfopts.starting_guess_disturbance,
                             scfopts.sg_disturb_specific_elements,
                             scfopts.disturbedElementIndexVector,
                             matOpts.permutationHML);
   densityMatrix.writeToFile();
}


void SCF_restricted::initialize_homo_lumo_limits()
{
   intervalType hugeInterval(-1e22, 1e22);

   homoInterval_F_ort_prev  = hugeInterval;
   lumoInterval_F_ort_prev  = hugeInterval;
   homoInterval_Fprev       = hugeInterval;
   lumoInterval_Fprev       = hugeInterval;
}


void SCF_restricted::write_matrices_to_file()
{
   FockMatrix.writeToFile();
   Fprev.writeToFile();
   Dprev.writeToFile();
   bestFockMatrixSoFar.writeToFile();
   bestFockMatrixSoFar2.writeToFile();
   F_ort_prev.writeToFile();
}


static void output_diff_norm_values(symmMatrix const& F1,
                                    symmMatrix const& F2,
                                    ergo_real         acc,
                                    const char        *name)
{
   ergo_real E_norm_frob = symmMatrix::frob_diff(F1, F2);

   Util::TimeMeter timeMeterMixedDiff;
   ergo_real       E_norm_mixed = symmMatrix::mixed_diff(F1, F2, acc);
   timeMeterMixedDiff.print(LOG_AREA_DENSFROMF, "symmMatrix::mixed_diff");
   Util::TimeMeter timeMeterEuclDiff;
   ergo_real       E_norm_eucl = symmMatrix::eucl_diff(F1, F2, acc);
   timeMeterEuclDiff.print(LOG_AREA_DENSFROMF, "symmMatrix::eucl_diff ");
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "Nor of error matrix for '%s':", name);
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "frob :  %22.15f", (double)E_norm_frob);
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "mixed:  %22.15f", (double)E_norm_mixed);
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "eucl:   %22.15f", (double)E_norm_eucl);
}


static ergo_real get_eucl_diff_with_adapted_accuracy(int n,
						     const symmMatrix & F_w,
						     const symmMatrix & F_ort_prev_w,
						     ergo_real acc) {
  // The symmMatrix::eucl_diff() call may be slow, we use a maxIter param to detect if it is a difficult case, and in such cases use a larger acc value.
  int maxIterForEuclDiff = std::max(n / 10, 500);
  ergo_real maxEigValMovement_eucl = -1; // Value will be set in try/catch code below.
  try {
    Util::TimeMeter timeMeterEuclDiff;
    maxEigValMovement_eucl = symmMatrix::eucl_diff(F_w, F_ort_prev_w, acc, maxIterForEuclDiff) + acc;
    timeMeterEuclDiff.print(LOG_AREA_SCF, "symmMatrix::eucl_diff for maxEigValMovement_eucl ");
  }
  catch(...) {
    do_output(LOG_CAT_INFO, LOG_AREA_SCF, "symmMatrix::eucl_diff() for maxEigValMovement_eucl failed for maxIterForEuclDiff=%d. Calling eucl_diff() again with lower accuracy requirement sqrt(acc).",
	      maxIterForEuclDiff);
    ergo_real acc2 = template_blas_sqrt(acc);
    Util::TimeMeter timeMeterEuclDiff;
    maxEigValMovement_eucl = symmMatrix::eucl_diff(F_w, F_ort_prev_w, acc2) + acc2;
    timeMeterEuclDiff.print(LOG_AREA_SCF, "symmMatrix::eucl_diff for maxEigValMovement_eucl ");
  }
  return maxEigValMovement_eucl;
}


void SCF_restricted::get_2e_part_and_energy()
{
   densityMatrix.readFromFile();
   densityMatrix_core.readFromFile();

   bool scan_do_invcholfactor_transf = (bool)scfopts.scan_do_invcholfactor_transf;

   if (scfopts.do_acc_scan_J)
   {
      do_acc_scan_J(densityMatrix,
                    integralInfo,
                    basisInfo,
                    invCholFactor,
                    scan_do_invcholfactor_transf,
                    J_K_params,
                    matOpts.size_block_info,
                    matOpts.permutationHML,
                    scfopts.scan_no_of_steps,
                    scfopts.scan_start_thresh,
                    scfopts.scan_step_factor);
   }

   if (scfopts.do_acc_scan_K)
   {
      do_acc_scan_K(densityMatrix,
                    integralInfo,
                    basisInfo,
                    invCholFactor,
                    scan_do_invcholfactor_transf,
                    CAM_params,
                    J_K_params,
                    matOpts.size_block_info,
                    matOpts.permutationHML,
                    matOpts.inversePermutationHML,
                    scfopts.scan_no_of_steps,
                    scfopts.scan_start_thresh,
                    scfopts.scan_step_factor);
   }

   if (scfopts.do_acc_scan_Vxc)
   {
      do_acc_scan_Vxc(densityMatrix,
                      integralInfo,
                      basisInfo,
                      molecule,
                      gridParams,
                      noOfElectrons,
                      invCholFactor,
                      scan_do_invcholfactor_transf,
                      matOpts.size_block_info,
                      matOpts.permutationHML,
                      matOpts.inversePermutationHML,
                      scfopts.scan_no_of_steps,
                      scfopts.scan_start_thresh,
                      scfopts.scan_step_factor);
   }

   symmMatrix G;
   G.resetSizesAndBlocks(matOpts.size_block_info,
                         matOpts.size_block_info);

   if (get_2e_matrix_and_energy_sparse(basisInfo,
                                       molecule,
                                       integralInfo,
                                       G,
                                       densityMatrix,
                                       J_K_params,
                                       CAM_params,
                                       gridParams,
                                       scfopts.use_dft,
                                       &energy_2el, noOfElectrons,
                                       matOpts.size_block_info,
                                       matOpts.permutationHML,
                                       matOpts.inversePermutationHML,
                                       0,
                                       J_matrix,
                                       K_matrix,
                                       Fxc_matrix,
                                       *curr_cycle_stats) != 0)
   {
      do_output(LOG_CAT_ERROR, LOG_AREA_SCF, "error in get_2e_matrix_and_energy_sparse");
      throw "error in get_2e_matrix_and_energy_sparse";
   }

   if (scfopts.compute_core_density == 1)
   {
      // Get energy_2el_valence and H_matrix_core, but only if densityMatrix_core is nonzero (it is zero the first time, then we just skip).
      if (densityMatrix_core.frob() != 0)
      {
         // Get energy_2el_valence
         {
            symmMatrix densityMatrix_valence(densityMatrix);
            densityMatrix_valence += (ergo_real) - 1 * densityMatrix_core;
            symmMatrix G_valence;
            G_valence.resetSizesAndBlocks(matOpts.size_block_info, matOpts.size_block_info);
            symmMatrix     J_matrix_valence;
            symmMatrix     K_matrix_valence;
            symmMatrix     Fxc_matrix_valence;
            SCF_statistics curr_cycle_stats_dummy;
            int            no_of_valence_electrons = noOfElectrons - scfopts.no_of_core_electrons;
            if (get_2e_matrix_and_energy_sparse(basisInfo,
                                                molecule,
                                                integralInfo,
                                                G_valence,
                                                densityMatrix_valence,
                                                J_K_params,
                                                CAM_params,
                                                gridParams,
                                                scfopts.use_dft,
                                                &energy_2el_valence, no_of_valence_electrons,
                                                matOpts.size_block_info,
                                                matOpts.permutationHML,
                                                matOpts.inversePermutationHML,
                                                0,
                                                J_matrix_valence,
                                                K_matrix_valence,
                                                Fxc_matrix_valence,
                                                curr_cycle_stats_dummy) != 0)
            {
               do_output(LOG_CAT_ERROR, LOG_AREA_SCF, "Error in get_2e_matrix_and_energy_sparse for densityMatrix_valence.");
               throw "Error in get_2e_matrix_and_energy_sparse for densityMatrix_valence.";
            }
         }
         // Compute J_matrix_core
         {
            symmMatrix     J_matrix_core;
            symmMatrix     K_matrix_core;
            symmMatrix     Fxc_matrix_core;
            SCF_statistics curr_cycle_stats_dummy;
            energy_2el_core = 0;
            if (get_2e_matrix_and_energy_sparse(basisInfo,
                                                molecule,
                                                integralInfo,
                                                twoel_matrix_core,
                                                densityMatrix_core,
                                                J_K_params,
                                                CAM_params,
                                                gridParams,
                                                scfopts.use_dft,
                                                &energy_2el_core, scfopts.no_of_core_electrons,
                                                matOpts.size_block_info,
                                                matOpts.permutationHML,
                                                matOpts.inversePermutationHML,
                                                0,
                                                J_matrix_core,
                                                K_matrix_core,
                                                Fxc_matrix_core,
                                                curr_cycle_stats_dummy) != 0)
            {
               do_output(LOG_CAT_ERROR, LOG_AREA_SCF, "Error in get_2e_matrix_and_energy_sparse for densityMatrix_core.");
               throw "Error in get_2e_matrix_and_energy_sparse for densityMatrix_core.";
            }
         }
      }
   }

   densityMatrix.writeToFile();
   densityMatrix_core.writeToFile();

   // Check that G matrix is free from "inf", "nan" etc.
   if (check_if_matrix_contains_strange_elements(G, matOpts.inversePermutationHML))
   {
      throw std::runtime_error("error in SCF_restricted::get_2e_part_and_energy(): G matrix contains inf or nan.");
   }

   // calculate Fock matrix F = H_core + G
   H_core_Matrix.readFromFile();
   FockMatrix.readFromFile();
   FockMatrix = H_core_Matrix + G;
   H_core_Matrix.writeToFile();
   G.clear();

   // Save a copy of FockMatrix before truncation if verification requested.
   symmMatrix FockMatrixBeforeTruncation;
   if (scfopts.do_f_thresh_verification == 1)
   {
      FockMatrixBeforeTruncation = FockMatrix;
   }

   // Do truncation of FockMatrix taking into account gap of Fprev and norm of Z.
   ergo_real gapOfFprevMin = lumoInterval_Fprev.low() - homoInterval_Fprev.upp();
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "About to truncate FockMatrix, gap of Fprev >= %22.11f", (double)gapOfFprevMin);
   {
      // Compare FockMatrix to Fprev to check how far eigenvalues can have moved.
      Fprev.readFromFile();
      ergo_real       maxEigValMovement_frob = symmMatrix::frob_diff(FockMatrix, Fprev);
      ergo_real       acc = template_blas_sqrt(get_machine_epsilon());
      Util::TimeMeter timeMeterMixedDiff;
      ergo_real       maxEigValMovement_mixed = symmMatrix::mixed_diff(FockMatrix, Fprev, acc) + acc;
      timeMeterMixedDiff.print(LOG_AREA_SCF, "symmMatrix::mixed_diff for F vs Fprev maxEigValMovement_mixed");
      int n = basisInfo.noOfBasisFuncs;
      ergo_real maxEigValMovement_eucl = get_eucl_diff_with_adapted_accuracy(n, FockMatrix, Fprev, acc);
      Fprev.writeToFile();
      // Increase HOMO/LUMO intervals so that they for sure contain the HOMO and LUMO eigenvalues of F_ort
      intervalType homoInterval = homoInterval_Fprev;
      intervalType lumoInterval = lumoInterval_Fprev;
      homoInterval.increase(maxEigValMovement_eucl);
      lumoInterval.increase(maxEigValMovement_eucl);
      do_output(LOG_CAT_INFO, LOG_AREA_SCF, "maxEigValMovement_frob  = %22.11f", (double)maxEigValMovement_frob);
      do_output(LOG_CAT_INFO, LOG_AREA_SCF, "maxEigValMovement_mixed = %22.11f", (double)maxEigValMovement_mixed);
      do_output(LOG_CAT_INFO, LOG_AREA_SCF, "maxEigValMovement_eucl  = %22.11f", (double)maxEigValMovement_eucl);
      // Now we have homoInterval and lumoInterval valid for FockMatrix.
      ergo_real gapMin = lumoInterval.low() - homoInterval.upp();
      ergo_real gapMax = lumoInterval.upp() - homoInterval.low();
      ergo_real threshold_1;
      // Choose subspace error as for purification.
      ergo_real subspaceThr_1 = 0.1 * scfopts.purification_subspace_err_limit;
      // We consider the gap to be accurately known if the uncertainty is at most 10 %
      if ((gapMin > 0) && ((gapMax - gapMin) / gapMin < 0.1))
      {
         // Gap is accurately known: we use gapMin
         threshold_1 = subspaceThr_1 * gapMin / (1 + subspaceThr_1);
      }
      else
      {
         // Gap is not accurately known. To avoid choosing a very tight
         // threshold value due to a small lower bound for the gap, we
         // use the largest of 'gap_expected_lower_bound' and calculated
         // 'gapMin':
         threshold_1 = gapMin > scfopts.gap_expected_lower_bound ?
                       subspaceThr_1 * gapMin / (1 + subspaceThr_1) :
                       subspaceThr_1 * scfopts.gap_expected_lower_bound / (1 + subspaceThr_1);
      }

      /* Truncate matrix taking into account that we are in
       * 'non-orthogonal basis', passing invCholFactor to thresh */
      invCholFactor.readFromFile();
      Util::TimeMeter timeMeterEuclThresh;
      double          nnzF_S_before_trunc_pc = (double)FockMatrix.nnz() * 100 / ((double)n * n);
      ergo_real       truncError_1           = FockMatrix.eucl_thresh(threshold_1, &invCholFactor);
      double          nnzF_S_after_trunc_pc  = (double)FockMatrix.nnz() * 100 / ((double)n * n);
      invCholFactor.writeToFile();
      timeMeterEuclThresh.print(LOG_AREA_SCF, "FockMatrix.eucl_thresh() (with Z)");
      do_output(LOG_CAT_INFO, LOG_AREA_SCF,
                "Truncated FockMatrix (eucl with Z), selected threshold = %10.6g, returned error = %10.6g, nnz before = %3.4f %%, nnz after = %3.4f %%",
                (double)threshold_1, (double)truncError_1, nnzF_S_before_trunc_pc, nnzF_S_after_trunc_pc);
      this->curr_cycle_stats->add_value("investigation_nnz_percentage_F_S", nnzF_S_after_trunc_pc);
   }
   //  FockMatrix.frob_thresh(matOpts.sparse_threshold);

   if (scfopts.do_f_thresh_verification == 1)
   {
      Util::TimeMeter timeMeterThreshVerification;
      do_output(LOG_CAT_INFO, LOG_AREA_SCF, "do_f_thresh_verification requested, computing error matrices...");
      symmMatrix F1(FockMatrixBeforeTruncation);
      symmMatrix F2(FockMatrix);
      ergo_real  acc = template_blas_sqrt(get_machine_epsilon());
      // First get norm of error matrix in non-orthogonal basis.
      output_diff_norm_values(F1, F2, acc, "F in in non-orthogonal basis");
      // Now get norm of error matrix in non-orthogonal basis.
      do_output(LOG_CAT_INFO, LOG_AREA_SCF, "do_f_thresh_verification requested, doing Z multiplications...");
      invCholFactor.readFromFile();
      Util::TimeMeter timeMeterZFZ1;
      F1 = transpose(invCholFactor) * F1 * invCholFactor;
      timeMeterZFZ1.print(LOG_AREA_DENSFROMF, "transpose(invCholFactor) * F1 * invCholFactor");
      Util::TimeMeter timeMeterZFZ2;
      F2 = transpose(invCholFactor) * F2 * invCholFactor;
      timeMeterZFZ2.print(LOG_AREA_DENSFROMF, "transpose(invCholFactor) * F2 * invCholFactor");
      invCholFactor.writeToFile();
      // Now we have the matrix in orthogonal basis, before and after truncation.
      output_diff_norm_values(F1, F2, acc, "F in in orthogonal basis");
      timeMeterThreshVerification.print(LOG_AREA_SCF, "do_f_thresh_verification stuff");
   }

   FockMatrix.writeToFile();
}


void SCF_restricted::output_sparsity_S_F_D(SCF_statistics& stats)
{
   int n = basisInfo.noOfBasisFuncs;

   S_symm.readFromFile();
   output_sparsity_symm(n, S_symm, "S");
   stats.add_value("nnz_S", S_symm.nnz());
   S_symm.writeToFile();
   FockMatrix.readFromFile();
   output_sparsity_symm(n, FockMatrix, "F");
   stats.add_value("nnz_F", FockMatrix.nnz());
   FockMatrix.writeToFile();
   densityMatrix.readFromFile();
   output_sparsity_symm(n, densityMatrix, "D");
   stats.add_value("nnz_D", densityMatrix.nnz());
   densityMatrix.writeToFile();
}


void SCF_restricted::calculate_energy()
{
   // calculate energy
   H_core_Matrix.readFromFile();
   densityMatrix.readFromFile();
   energy = symmMatrix::trace_ab(densityMatrix, H_core_Matrix) + energy_2el;
   if (scfopts.compute_core_density == 1)
   {
      densityMatrix_core.readFromFile();
      symmMatrix densityMatrix_valence(densityMatrix);
      densityMatrix_valence += (ergo_real) - 1 * densityMatrix_core;
      energy_of_valence      = symmMatrix::trace_ab(densityMatrix_valence, H_core_Matrix) + symmMatrix::trace_ab(densityMatrix_valence, twoel_matrix_core) + energy_2el_valence;
      energy_reference       = symmMatrix::trace_ab(densityMatrix_core, H_core_Matrix) + energy_2el_core + nuclearEnergy;
      densityMatrix_core.writeToFile();
   }
   densityMatrix.writeToFile();
   H_core_Matrix.writeToFile();
   energy += nuclearEnergy;
}


void SCF_restricted::get_FDSminusSDF()
{
   int n = basisInfo.noOfBasisFuncs;

   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "calling compute_FDSminusSDF_sparse, n = %i", n);
   densityMatrix.readFromFile();
   S_symm.readFromFile();
   FockMatrix.readFromFile();
   compute_FDSminusSDF_sparse(n, FockMatrix, densityMatrix, S_symm,
                              ErrorMatrix, matOpts.sparse_threshold);
   S_symm.writeToFile();
   FockMatrix.writeToFile();
   densityMatrix.writeToFile();
   // write to file and read back again to reduce memory fragmentation.
   ErrorMatrix.writeToFile();
   ErrorMatrix.readFromFile();
   output_sparsity(n, ErrorMatrix, "FDS-SDF");
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "SCF_restricted::get_FDSminusSDF finished.");
}


void SCF_restricted::get_error_measure()
{
   ergo_real error_maxabs = compute_maxabs_sparse(ErrorMatrix);
   ergo_real error_frob   = ErrorMatrix.frob();

   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "maxabs FDS-SDF is %8.3g", (double)error_maxabs);
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "frob   FDS-SDF is %8.3g", (double)error_frob);
   errorMeasure = error_maxabs;
}


void SCF_restricted::add_to_DIIS_list()
{
   FockMatrix.readFromFile();
   if (((DIISManagerRestricted *)DIIS)->AddIterationToList(FockMatrix, ErrorMatrix) != 0)
   {
      do_output(LOG_CAT_ERROR, LOG_AREA_SCF, "error in DIIS AddIterationToList");
      throw "error in DIIS AddIterationToList";
   }
   FockMatrix.writeToFile();
}


void SCF_restricted::update_best_fock_so_far()
{
   Fprev.readFromFile();
   bestFockMatrixSoFar.readFromFile();
   bestFockMatrixSoFar = Fprev;
   Fprev.writeToFile();
   bestFockMatrixSoFar.writeToFile();
   FockMatrix.readFromFile();
   bestFockMatrixSoFar2.readFromFile();
   bestFockMatrixSoFar2 = FockMatrix;
   FockMatrix.writeToFile();
   bestFockMatrixSoFar2.writeToFile();
}


void SCF_restricted::combine_old_fock_matrices(ergo_real stepLength)
{
   bestFockMatrixSoFar.readFromFile();
   bestFockMatrixSoFar2.readFromFile();
   FockMatrix.readFromFile();
   FockMatrix  = 0;
   FockMatrix += stepLength * bestFockMatrixSoFar2;
   FockMatrix += (1 - stepLength) * bestFockMatrixSoFar;
   FockMatrix.writeToFile();
   bestFockMatrixSoFar.writeToFile();
   bestFockMatrixSoFar2.writeToFile();
}


void SCF_restricted::use_diis_to_get_new_fock_matrix()
{
   symmMatrix newFsymm;

   if (((DIISManagerRestricted *)DIIS)->GetCombinedFockMatrix(newFsymm) != 0)
   {
      do_output(LOG_CAT_ERROR, LOG_AREA_SCF, "error in DIIS.GetCombinedFockMatrix");
      throw "error in DIIS.GetCombinedFockMatrix";
   }
   FockMatrix.readFromFile();
   FockMatrix = newFsymm;
   FockMatrix.writeToFile();
}


void SCF_restricted::clear_diis_list()
{
   ((DIISManagerRestricted *)DIIS)->ClearList();
}


void SCF_restricted::clear_error_matrices()
{
   ErrorMatrix.clear();
}


void SCF_restricted::save_current_fock_as_fprev()
{
   // save current Fock matrix as Fprev
   FockMatrix.readFromFile();
   Fprev.readFromFile();
   Fprev = FockMatrix;
   FockMatrix.writeToFile();
   Fprev.writeToFile();
}


void SCF_restricted::get_new_density_matrix()
{
   int n = basisInfo.noOfBasisFuncs;

   DensFromFock.set_SCF_step(SCF_step);

   // As input to the density matrix construction routine, the default
   // is to use FockMatrix. However, if shift_using_prev_density_matrix
   // we use a modified matrix instead.
   symmMatrix *F_effective = &FockMatrix;
   symmMatrix F_modified;
   if (scfopts.shift_using_prev_density_matrix != 0)
   {
      do_output(LOG_CAT_INFO, LOG_AREA_SCF, "Using shift_using_prev_density_matrix, shifting by %9.5f a.u. = %9.5f eV",
                (double)scfopts.shift_using_prev_density_matrix, (double)scfopts.shift_using_prev_density_matrix / UNIT_one_eV);
      F_modified = FockMatrix;
      F_modified.readFromFile();
      // Get matrix SDS
      symmMatrix SDS(densityMatrix);
      SDS.readFromFile();
      transform_with_S(SDS);
      // Use factor 0.5 since this is restricted case.
      F_modified += (ergo_real) - 0.5 * scfopts.shift_using_prev_density_matrix * SDS;
      F_modified.writeToFile();
      F_effective = &F_modified;
   }

   DensFromFock.set_eigs_F_ort_prev(homoInterval_F_ort_prev, lumoInterval_F_ort_prev);
   DensFromFock.clean_puri_stats();

   DensFromFock.set_generate_figures();

   // if eigenvectors are needed, set params
   int use_init_guess = 0;
   if ((scfopts.use_prev_vector_as_initial_guess == 1) &&
       (scfopts.min_number_of_iterations-1 <= SCF_step) &&
       (SCF_step > 1) &&
       !eigVecUNOCC.empty() && !eigVecOCC.empty()) // ensure that we computed vectors in previous cycle
   {
      use_init_guess = 1;
   }

   // if we use new purification
   if ((SCF_step > 1) &&
       (scfopts.min_number_of_iterations-1 <= SCF_step) &&
       (DensFromFock.get_use_purification() == 1) &&
       (DensFromFock.get_output_homo_and_lumo_eigenvectors() == 1))
   {
      DensFromFock.compute_eigenvectors(scfopts.eigenvectors_method,
                                        scfopts.eigenvectors_iterative_method,
                                        scfopts.eigensolver_accuracy,
                                        scfopts.eigensolver_maxiter,
                                        use_init_guess,
                                        scfopts.try_eigv_on_next_iteration_if_fail);

      DensFromFock.compute_eigenvectors_extra(scfopts.puri_compute_eigv_in_each_iteration, scfopts.run_shift_and_square_method_on_F);
   }

   if (scfopts.create_checkpoints == 1)
   {
      Util::TimeMeter timeMeter;
      generalVector * eigVecLUMO = NULL;
      generalVector * eigVecHOMO = NULL;
      if(eigVecOCC.size() >= 1)   eigVecHOMO = &eigVecOCC[0];
      if(eigVecUNOCC.size() >= 1) eigVecLUMO = &eigVecUNOCC[0];
      DensFromFock.create_checkpoint(*F_effective, // should normally be same as Fprev now
                                     F_ort_prev,
                                     eigVecLUMO,
                                     eigVecHOMO,
                                     scfopts.checkpoint_IDstr);
      timeMeter.print(LOG_AREA_SCF, "in SCF_restricted DensFromFock::create_checkpoint took");
   }

   if (DensFromFock.get_dens_from_fock(*F_effective, // should normally be same as Fprev now
                                       densityMatrix,
                                       F_ort_prev) != 0)
   {
      throw "SCF_restricted::get_new_density_matrix: Error in get_dens_from_fock";
   }

   // return empty vectors in case nothing is computed
   DensFromFock.get_computed_eigenpairs(eigVecUNOCC, eigVecOCC, eigValUNOCC, eigValOCC);

   DensFromFock.get_eigs_F_ort_prev(homoInterval_F_ort_prev, lumoInterval_F_ort_prev);
   DensFromFock.get_eigs_Fprev(homoInterval_Fprev, lumoInterval_Fprev);

   std::map<std::string, double> puri_stats;
   DensFromFock.get_puri_stats(puri_stats);
   this->curr_cycle_stats->add_values(puri_stats);

   DensFromFock.unset_generate_figures();
   electronicEntropyTerm = DensFromFock.get_result_entropy_term();

   if (scfopts.compute_core_density == 1)
   {
      DensFromFock.clean_eigs_intervals();
      if (DensFromFock.get_dens_from_fock(*F_effective,
                                          densityMatrix_core,
                                          F_ort_prev) != 0)
      {
         throw "SCF_restricted::get_new_density_matrix: Error in get_dens_from_fock for core density matrix.";
      }
   }

   // Report sparsity of D and trace(DS)
   S_symm.readFromFile();
   densityMatrix.readFromFile();
   output_sparsity_symm(n, densityMatrix, "new density matrix");
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "Tr( D * S ) = %22.11f", (double)symmMatrix::trace_ab(densityMatrix, S_symm));
   densityMatrix.writeToFile();
   S_symm.writeToFile();
}


static int write_matrix_to_file(symmMatrix& M, const std::vector<int>& inversePermutationHML, const BasisInfoStruct& basisInfo, const char *fileName)
{
   M.readFromFile();
   matrix_description_struct matrixList[2];
   int              nvalues = M.nvalues();
   std::vector<int> rowind;
   rowind.reserve(nvalues);
   std::vector<int> colind;
   colind.reserve(nvalues);
   std::vector<ergo_real> values;
   values.reserve(nvalues);
   M.get_all_values(rowind, colind, values, inversePermutationHML, inversePermutationHML);
   M.writeToFile();
   matrixList[0].nvalues = nvalues;
   matrixList[0].rowind  = &rowind[0];
   matrixList[0].colind  = &colind[0];
   matrixList[0].values  = &values[0];
   if (ddf_writeShellListAndDensityMatricesToFile_sparse(basisInfo, 1, matrixList, fileName) != 0)
   {
      do_output(LOG_CAT_ERROR, LOG_AREA_SCF, "Error in ddf_writeShellListAndDensityMatricesToFile_sparse.");
      return -1;
   }
   return 0;
}


void SCF_restricted::write_density_to_file()
{
   if (write_matrix_to_file(densityMatrix, matOpts.inversePermutationHML, basisInfo, "density.bin") != 0)
   {
      throw "error in SCF_restricted::write_density_to_file(): write_matrix_to_file failed for densityMatrix.";
   }
   if (scfopts.compute_core_density == 1)
   {
      if (write_matrix_to_file(densityMatrix_core, matOpts.inversePermutationHML, basisInfo, "density_core.bin") != 0)
      {
         throw "error in SCF_restricted::write_density_to_file(): write_matrix_to_file failed for densityMatrix_core.";
      }
   }
}


void SCF_restricted::save_final_potential()
{
   if (save_symmetric_matrix(FockMatrix, basisInfo, "potential.bin",
                             matOpts.inversePermutationHML) != 0)
   {
      do_output(LOG_CAT_ERROR, LOG_AREA_SCF,
                "error in ddf_writeShellListAndDensityMatricesToFile");
      throw "error in ddf_writeShellListAndDensityMatricesToFile";
   }
}


void SCF_restricted::save_full_matrices_for_matlab()
{
   int n = basisInfo.noOfBasisFuncs;

   FockMatrix.readFromFile();
   write_full_matrix(n, FockMatrix, "matrix_F",
                     matOpts.inversePermutationHML);
   FockMatrix.writeToFile();

   S_symm.readFromFile();
   write_full_matrix(n, S_symm, "matrix_S",
                     matOpts.inversePermutationHML);
   S_symm.writeToFile();

   densityMatrix.readFromFile();
   write_full_matrix(n, densityMatrix, "matrix_D",
                     matOpts.inversePermutationHML);
   densityMatrix.writeToFile();
}


void SCF_restricted::output_expected_values_pos_operator()
{
  if(eigVecOCC.size() == 1)
    get_expected_values_pos_operator(eigVecOCC[0], "HOMO");
  if(eigVecUNOCC.size() == 1)  
    get_expected_values_pos_operator(eigVecUNOCC[0], "LUMO");
  
  if(eigVecOCC.size() > 1)  
    for (size_t i = 1; i < eigVecOCC.size(); i++) {
      std::ostringstream name;
      name << "OCCUPIED " << i;
      get_expected_values_pos_operator(eigVecOCC[i], name.str().c_str());
    }
  if(eigVecUNOCC.size() > 1)  
    for (size_t i = 1; i < eigVecUNOCC.size(); i++) {
        std::ostringstream name;
        name << "UNOCCUPIED " << i;
        get_expected_values_pos_operator(eigVecUNOCC[i], name.str().c_str());
      }    
}


// expected value of a measurement of the position of the particle
void SCF_restricted::get_expected_values_pos_operator(generalVector& eigVec, const char *vector_name)
{
   Util::TimeMeter timeMeter;

   int n = basisInfo.noOfBasisFuncs;

   if (eigVec.is_empty())
   {
      do_output(LOG_CAT_WARNING, LOG_AREA_SCF, "Failed to output expected value of a position operator for the %s eigenvector.", vector_name);
      return;
   }
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "Computing expected value of a position operator for the %s eigenvector.", vector_name);

   std::vector<ergo_real> vec(n);
   eigVec.fullvector(vec);

  // get density matrix corresponding to the eigenvector
   std::vector<int> rows(n), cols(n);
   std::vector<ergo_real> vals(n);
   ergo_real tmp;
   size_t count = 0;
   for (int i = 0; i < n; ++i)
      for (int j = i; j < n; ++j)
      {
          tmp = vec[i] * vec[j];
          if(template_blas_fabs(tmp) < 1e-5)
            continue;
          rows[count] = i;
          cols[count] = j;
          vals[count] = tmp;
          count++;
          if(count % n == 0)
            {
                rows.resize(count+n);
                cols.resize(count+n);
                vals.resize(count+n);
            }
     }
   rows.resize(count);
   cols.resize(count);
   vals.resize(count);

   symmMatrix densityMatrix;
   densityMatrix.resetSizesAndBlocks(matOpts.size_block_info, matOpts.size_block_info);
   densityMatrix.assign_from_sparse(rows, cols, vals);

   std::vector<ergo_real> mean;
   std::vector<ergo_real> std;
   get_exp_value_pos_operator(basisInfo,
                              molecule,
                              densityMatrix,
                              matOpts.size_block_info,
                              matOpts.permutationHML,
                              mean,
                              std);


   if ((mean.size() != 3) || (std.size() != 3))
   {
      throw "Error in output_expected_values_pos_operator: wrong size of a vector.";
   }

   ergo_real conv_const = UNIT_one_Angstrom; // convert a.u. to Angstrom
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "Expected value of position operator:   \n  (%.12lf, %.12lf, %.12lf) a.u. = (%.12lf, %.12lf, %.12lf) A",
             mean[0], mean[1], mean[2],
             mean[0] / conv_const, mean[1] / conv_const, mean[2] / conv_const);
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "Standard deviation of position operator:  \n  (%.12lf, %.12lf, %.12lf) a.u. = (%.12lf, %.12lf, %.12lf) A",
             std[0], std[1], std[2],
             std[0] / conv_const, std[1] / conv_const, std[2] / conv_const);

   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "SCF_restricted::output_expected_values_pos_operator finished OK.");
   timeMeter.print(LOG_AREA_SCF, "SCF_restricted::output_expected_values_pos_operator");
}


void SCF_restricted::output_density_images()
{
   Util::TimeMeter timeMeter;

   int n = basisInfo.noOfBasisFuncs;

   ergo_real *densityMatrixFull_tot  = new ergo_real[n * n];
   ergo_real *densityMatrixFull_spin = new ergo_real[n * n];

   // Get full matrix version of density matrix, and empty spin density matrix.
   {
      std::vector<ergo_real> densityMatrixFull(n *n);

      densityMatrix.readFromFile();
      densityMatrix.fullMatrix(densityMatrixFull,
                               matOpts.inversePermutationHML,
                               matOpts.inversePermutationHML);
      densityMatrix.writeToFile();

      for (int i = 0; i < n * n; i++)
      {
         densityMatrixFull_tot [i] = densityMatrixFull[i];
         densityMatrixFull_spin[i] = 0;
      }
   }

   do_density_images(basisInfo,
                     molecule,
                     densityMatrixFull_tot,
                     densityMatrixFull_spin,
                     scfopts.output_density_images_boxwidth);

   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "SCF_restricted::output_density_images finished OK.");
   timeMeter.print(LOG_AREA_SCF, "SCF_restricted::output_density_images");
}


void SCF_restricted::output_density_images_orbital(generalVector& eigVec, const std::string& filename_id)
{
   Util::TimeMeter timeMeter;

   int n = basisInfo.noOfBasisFuncs;

   ergo_real *densityMatrixFull_tot  = new ergo_real[n * n];
   ergo_real *densityMatrixFull_spin = new ergo_real[n * n];

   if (eigVec.is_empty())
   {
      do_output(LOG_CAT_WARNING, LOG_AREA_SCF, "Failed to output density image for the eigenvector.");
      return;
   }
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "Creating density image for the eigenvector.");
   std::vector<ergo_real> vec_perm(n);
   eigVec.fullvector(vec_perm);
   // now we have the permuted vector
   std::vector<ergo_real> vec(n);
   for (int ind = 0; ind < n; ind++)
   {
      vec[ind] = vec_perm[matOpts.inversePermutationHML[ind]];
   }

   // create density matrix corresponding to the given orbital
   std::vector<ergo_real> densityMatrixFull(n *n);

   for (int j = 0; j < n; ++j)
   {
      for (int i = 0; i < n; ++i)
      {
         densityMatrixFull[i + j * n] = vec[i] * vec[j];
      }
   }

   // Get full matrix version of density matrix, and empty spin density matrix.
   {
      for (int i = 0; i < n * n; i++)
      {
         densityMatrixFull_tot [i] = densityMatrixFull[i];
         densityMatrixFull_spin[i] = 0;
      }
   }

   do_density_images(basisInfo,
                     molecule,
                     densityMatrixFull_tot,
                     densityMatrixFull_spin,
                     scfopts.output_density_images_boxwidth,
                     filename_id); // add filename id

   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "SCF_restricted::output_density_images_orbital finished OK.");
   timeMeter.print(LOG_AREA_SCF, "SCF_restricted::output_density_images_orbital");
}


void SCF_restricted::write_diag_dens_to_file()
{
   int n = basisInfo.noOfBasisFuncs;

   densityMatrix.readFromFile();
   write_diag_elements_to_file(n, densityMatrix, "diagdens.txt",
                               matOpts.permutationHML);
   densityMatrix.writeToFile();
}


void SCF_restricted::report_final_results()
{
}


void SCF_restricted::do_spin_flip(int atomCount)
{
   throw "error: SCF_restricted::do_spin_flip does not make sense, should not have been called.";
}


void SCF_restricted::save_density_as_prevdens()
{
   densityMatrix.readFromFile();
   Dprev.readFromFile();
   Dprev = densityMatrix;
   densityMatrix.writeToFile();
   Dprev.writeToFile();
}


void SCF_restricted::report_density_difference()
{
   if (scfopts.do_report_density_diff == 0)
   {
      do_output(LOG_CAT_INFO, LOG_AREA_SCF,
                "SCF_restricted::report_density_difference() skipping: (scfopts.do_report_density_diff == 0).");
      return;
   }
   Util::TimeMeter tm;
   densityMatrix.readFromFile();
   Dprev.readFromFile();
   symmMatrix diff(densityMatrix);
   diff += (ergo_real) - 1.0 * Dprev;
   ergo_real diff_eucl = GetEuclideanNormOfMatrix(diff);
   densityMatrix.writeToFile();
   Dprev.writeToFile();
   do_output(LOG_CAT_INFO, LOG_AREA_SCF,
             "SCF_restricted::report_density_difference, diff_eucl = %22.11f", (double)diff_eucl);
   tm.print(LOG_AREA_SCF, "SCF_restricted::report_density_difference");
}


void SCF_restricted::compute_dipole_moment()
{
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "SCF_restricted::compute_dipole_moment");
   densityMatrix.readFromFile();
   get_dipole_moment(densityMatrix, basisInfo, matOpts.size_block_info, matOpts.permutationHML, molecule, LOG_AREA_SCF, "SCF");
   densityMatrix.writeToFile();
}


void SCF_restricted::do_mulliken_pop_stuff()
{
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "SCF_restricted::do_mulliken_pop_stuff");
   densityMatrix.readFromFile();
   S_symm.readFromFile();
   do_mulliken_atomic_charges(densityMatrix,
                              S_symm,
                              basisInfo,
                              matOpts.size_block_info,
                              matOpts.permutationHML,
                              matOpts.inversePermutationHML,
                              molecule);
   densityMatrix.writeToFile();
   S_symm.writeToFile();
}


void SCF_restricted::create_mtx_files_F(int const scfIter)
{
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "Creating mtx file for Fock matrix");
   std::stringstream ss_fileName;
   ss_fileName << "F_matrix_" << scfIter;
   std::stringstream ss_id;
   ss_id << scfopts.calculation_identifier << " - effective Hamiltonian matrix, SCF cycle " << scfIter;
   FockMatrix.readFromFile();
   write_matrix_in_matrix_market_format(FockMatrix, matOpts.inversePermutationHML, ss_fileName.str(),
                                        ss_id.str(), scfopts.method_and_basis_set);
   FockMatrix.writeToFile();
}


void SCF_restricted::create_mtx_files_D(int const scfIter)
{
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "Creating mtx file for density matrix");
   std::stringstream ss_fileName;
   ss_fileName << "D_matrix_" << scfIter;
   std::stringstream ss_id;
   ss_id << scfopts.calculation_identifier << " - density matrix, SCF cycle " << scfIter;
   densityMatrix.readFromFile();
   write_matrix_in_matrix_market_format(densityMatrix, matOpts.inversePermutationHML, ss_fileName.str(),
                                        ss_id.str(), scfopts.method_and_basis_set);
   densityMatrix.writeToFile();
}


void SCF_restricted::create_eigenvalues_files() const
{
  if(! eigValOCC.empty() )
  {
    std::ofstream ff("occupied_spectrum.txt");
    for (size_t ind = 0; ind < eigValOCC.size(); ind++)
    {
       ff << (double)eigValOCC[ind] << std::endl;
    }
    ff.close();
  }
  
  if(! eigValUNOCC.empty() )
  {
    std::ofstream ff("unoccupied_spectrum.txt");
    for (size_t ind = 0; ind < eigValUNOCC.size(); ind++)
    {
       ff << (double)eigValUNOCC[ind] << std::endl;
    }
    ff.close();
  }
}


void SCF_restricted::create_eigenvectors_files() const
{
  create_eigenvalues_files();
  
  
  if(! eigVecOCC.empty() )
  {
    create_eigvec_file(eigVecOCC[0],
                      "HOMO",
                      "homo_coefficient_vec");
    for (size_t i = 1; i < eigVecOCC.size(); i++) 
    {
      std::stringstream ss_name, ss_filename;
      ss_name << "HOMO-" << i;
      ss_filename << "occ_" << i << "_coefficient_vec";
      create_eigvec_file(eigVecOCC[i],
                        ss_name.str().c_str(),
                        ss_filename.str().c_str());
    }
  }
  
  if(! eigVecUNOCC.empty())
  {
    create_eigvec_file(eigVecUNOCC[0],
                       "LUMO",
                       "lumo_coefficient_vec");
    for (size_t i = 1; i < eigVecUNOCC.size(); i++) 
    {
      std::stringstream ss_name, ss_filename;
      ss_name << "LUMO-" << i;
      ss_filename << "unocc_" << i << "_coefficient_vec";
      create_eigvec_file(eigVecUNOCC[i],
                        ss_name.str().c_str(),
                        ss_filename.str().c_str());
    }
  }
}


void SCF_restricted::create_eigvec_file(const generalVector& eigVec,
                                        const char           *vector_name,
                                        const char           *filename_id) const
{
   if (eigVec.is_empty())
   {
      do_output(LOG_CAT_WARNING, LOG_AREA_SCF, "Failed to output %s to file. No %s eigenvector stored.", vector_name, vector_name);
      return;
   }
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "Storing %s eigenvector to file %s.", vector_name, filename_id);
   int n = basisInfo.noOfBasisFuncs;
   std::vector<ergo_real> vec_perm(n);
   eigVec.fullvector(vec_perm);
   // now we have the permuted vector
   std::vector<ergo_real> vec(n);
   for (int ind = 0; ind < n; ind++)
   {
      vec[matOpts.inversePermutationHML[ind]] = vec_perm[ind];
   }
   char ffname[888];
   sprintf(ffname, "%s.txt", filename_id);
   std::ofstream ff(ffname);
   for (int ind = 0; ind < n; ind++)
   {
      ff << (double)vec[ind] << std::endl;
   }
   ff.close();
}


static void
output_orbital_coeffs_in_gabedit_order(const BasisInfoStruct&        basisInfo,
                                       std::vector<int> const&       shellIdxList,
                                       std::ofstream&                ff,
                                       std::vector<ergo_real> const& orbital_vec)
{
   int count = 0;

   for (int i = 0; i < basisInfo.noOfShells; i++)
   {
      int k        = shellIdxList[i];
      int startIdx = basisInfo.shellList[k].startIndexInMatrix;
      switch (basisInfo.shellList[k].shellType)
      {
      case 0: // s-type shell
         ff << count + 1 << "   " << (double)orbital_vec[startIdx] << std::endl;
         count++;
         break;

      case 1: // p-type shell
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 2] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 0] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 1] << std::endl;
         count++;
         break;

      case 2: // d-type shell
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 2] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 3] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 1] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 4] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 0] << std::endl;
         count++;
         break;

      case 3: // f-type shell
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 3] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 4] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 2] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 5] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 1] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 6] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 0] << std::endl;
         count++;
         break;

      case 4: // g-type shell
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 4] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 5] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 3] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 6] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 2] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 7] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 1] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 8] << std::endl;
         count++;
         ff << count + 1 << "   " << (double)orbital_vec[startIdx + 0] << std::endl;
         count++;
         break;

      default:
         throw "error in output_orbital_coeffs_in_gabedit_order: shell types beyond g not implemented!";
      }
   }
   if (count != basisInfo.noOfBasisFuncs)
   {
      throw "error in output_orbital_coeffs_in_gabedit_order: (count != basisInfo.noOfBasisFuncs)";
   }
}


void SCF_restricted::create_gabedit_file() const
{
   if (eigVecOCC.empty() || eigVecUNOCC.empty())
   {
      do_output(LOG_CAT_WARNING, LOG_AREA_SCF, "Failed to output eigenvectors to gabedit file; no eigenvectors info available.");
      return;
   }
   if (basisInfo.use_6_d_funcs == 1)
   {
      do_output(LOG_CAT_WARNING, LOG_AREA_SCF, "Failed to output eigenvectors to gabedit file; not implemented for use_6_d_funcs case.");
      return;
   }
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "Creating Gabedit file with eigenvector info.");
   int n = basisInfo.noOfBasisFuncs;

   // Create Gabedit file.
   const char    fileName [] = "gabeditfile.gab";
   std::ofstream ff(fileName);

   /* FIXME: check if we should use "Cart" or "Sphe" here. That is,
    * should we use use_6_d_funcs? */
   int use_6_d_funcs = 0;
   ff << "[Gabedit Format] Sphe" << std::endl;
   ff << "[Atoms] Angs" << std::endl;
   for (int i = 0; i < molecule.getNoOfAtoms(); i++)
   {
      char atomLabelString[4];
      get_atom_label_from_charge_int(molecule.getAtom(i).charge, atomLabelString, 4);
      ff << atomLabelString << " " << i + 1 << " " << (double)molecule.getAtom(i).charge
         << "   " << (double)(molecule.getAtom(i).coords[0] / UNIT_one_Angstrom)
         << "   " << (double)(molecule.getAtom(i).coords[1] / UNIT_one_Angstrom)
         << "   " << (double)(molecule.getAtom(i).coords[2] / UNIT_one_Angstrom)
         << std::endl;
   }
   std::vector<int>     shellIdxList(basisInfo.noOfShells);
   int                  shellIdxCounter = 0;
   SquareFuncIntegrator sfi;
   ff << "[Basis]" << std::endl;
   for (int i = 0; i < molecule.getNoOfAtoms(); i++)
   {
      ff << i + 1 << " 0" << std::endl;
      // Now output info about shells for this atom.
      for (int k = 0; k < basisInfo.noOfShells; k++)
      {
         // Check if this shell belongs to the current atom.
         ergo_real absdx     = template_blas_fabs(basisInfo.shellList[k].centerCoords[0] - molecule.getAtom(i).coords[0]);
         ergo_real absdy     = template_blas_fabs(basisInfo.shellList[k].centerCoords[1] - molecule.getAtom(i).coords[1]);
         ergo_real absdz     = template_blas_fabs(basisInfo.shellList[k].centerCoords[2] - molecule.getAtom(i).coords[2]);
         ergo_real distlimit = 0.01;
         if ((absdx > distlimit) || (absdy > distlimit) || (absdz > distlimit))
         {
            continue;
         }
         // OK, now we know this shell is at least very near the current atom.
         shellIdxList[shellIdxCounter] = k;
         shellIdxCounter++;
         char shellChar = 'x';
         int  shellType = basisInfo.shellList[k].shellType;
         switch (shellType)
         {
         case 0:
            shellChar = 's';
            break;

         case 1:
            shellChar = 'p';
            break;

         case 2:
            shellChar = 'd';
            break;

         case 3:
            shellChar = 'f';
            break;

         case 4:
            shellChar = 'g';
            break;

         default:
            throw "SCF_restricted::create_gabedit_file error: shell types beyond g not implemented!";
         }
         ff << shellChar << " " << basisInfo.shellList[k].noOfContr << " 1.00" << std::endl;
         for (int contridx = 0; contridx < basisInfo.shellList[k].noOfContr; contridx++)
         {
            ergo_real exponent    = basisInfo.shellList[k].exponentList[contridx];
            ergo_real shellFactor = sfi.getShellFactor(integralInfo, exponent, shellType, use_6_d_funcs);
            ergo_real coeff       = basisInfo.shellList[k].coeffList[contridx] / shellFactor;
            ff << (double)exponent << "   " << (double)coeff << std::endl;
         }
      }
      // Blank line before shells for next atom.
      ff << std::endl;
   }
   if (shellIdxCounter != basisInfo.noOfShells)
   {
      throw "Error: (shellIdxCounter != basisInfo.noOfShells)";
   }
   // MO section.
   ff << "[MO]" << std::endl;
   
   
   // // HOMO
   // ff << "Spin=Alpha" << std::endl;
   // ff << "Occup=   2.000000" << std::endl;
   // output_orbital_coeffs_in_gabedit_order(basisInfo, shellIdxList, ff, homo_vec);
   // // LUMO
   // ff << "Spin=Alpha" << std::endl;
   // ff << "Occup=   0.000000" << std::endl;
   // output_orbital_coeffs_in_gabedit_order(basisInfo, shellIdxList, ff, lumo_vec);
   
   
   
   // Occupied orbitals 
   for (int orb = (int)eigVecOCC.size()-1; orb >= 0; orb--)  // note that orb can be negative
   {
     std::vector<ergo_real> orb_vec_perm(n);
     eigVecOCC[orb].fullvector(orb_vec_perm);
     // now we have the permuted vector
     std::vector<ergo_real> orb_vec(n);
     for (int ind = 0; ind < n; ind++)
     {
        orb_vec[matOpts.inversePermutationHML[ind]] = orb_vec_perm[ind];
     }
     ff << "Spin=Alpha" << std::endl;
     if((int)eigValOCC.size() > orb) // in case we do not have eigenvalues
      ff << "Energy= " << (double)eigValOCC[orb] << std::endl;
     ff << "Occup=   2.000000" << std::endl;
     output_orbital_coeffs_in_gabedit_order(basisInfo, shellIdxList, ff, orb_vec);
   }
   

   // Unoccupied orbitals 
   for (size_t orb = 0; orb < eigVecUNOCC.size(); orb++) 
   {
     std::vector<ergo_real> orb_vec_perm(n);
     eigVecUNOCC[orb].fullvector(orb_vec_perm);
     // now we have the permuted vector
     std::vector<ergo_real> orb_vec(n);
     for (int ind = 0; ind < n; ind++)
     {
        orb_vec[matOpts.inversePermutationHML[ind]] = orb_vec_perm[ind];
     }
     ff << "Spin=Alpha" << std::endl;
     if(eigValUNOCC.size() > orb) // in case we do not have eigenvalues
       ff << "Energy= " << (double)eigValUNOCC[orb] << std::endl;
     ff << "Occup=   0.000000" << std::endl;
     output_orbital_coeffs_in_gabedit_order(basisInfo, shellIdxList, ff, orb_vec);
   }
   
      
   // Blank line before end of file.
   ff << std::endl;
   // Close file.
   ff.close();
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "Gabedit file '%s' with eigenvectors info created OK.", fileName);
}


void SCF_restricted::update_subspace_diff()
{
   densityMatrix.readFromFile();
   Dprev.readFromFile();
   ergo_real acc = template_blas_sqrt(get_machine_epsilon());

   symmMatrix diff(densityMatrix);
   diff += (ergo_real) - 1.0 * Dprev;

   transform_with_S(diff);
   transform_with_invChol(diff);

   // Compensate for factor 2 (restricted case)
   diff *= (ergo_real)0.5;

   ergo_real diff_eucl = diff.eucl(acc);

   densityMatrix.writeToFile();
   Dprev.writeToFile();

   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "SCF_restricted::update_subspace_diff, diff_eucl = %22.11f", (double)diff_eucl);
   curr_subspace_diff = diff_eucl;
}


struct RandomNumber
{
   ergo_real accumulate(ergo_real& a, int const dummy1, int const dummy2)
   {
      a = rand() / (ergo_real)RAND_MAX;
      return 0;
   }
};


/** Transform matrix A to S*A*S */
void SCF_restricted::transform_with_S(symmMatrix& A)
{
   S_symm.readFromFile();

   normalMatrix S_norm(S_symm);
   normalMatrix A_norm(A);

   normalMatrix SA(S_symm);
   SA = (ergo_real)1.0 * S_norm * A_norm;
   normalMatrix SAS(S_symm);
   SAS = (ergo_real)1.0 * SA * S_norm;

   A = SAS;

   S_symm.writeToFile();
}


/** Transform matrix A to invCholT*A*invChol */
void SCF_restricted::transform_with_invChol(symmMatrix& A)
{
   invCholFactor.readFromFile();
   A = transpose(invCholFactor) * A * invCholFactor;
   invCholFactor.writeToFile();
}


void SCF_restricted::get_non_ort_err_mat_normalized_in_ort_basis(symmMatrix& randomMatrix, int transform_with_S_also)
{
   symmMatrix randomMatrix1;

   randomMatrix1.resetSizesAndBlocks(matOpts.size_block_info,
                                     matOpts.size_block_info);
   symmMatrix randomMatrix2;
   randomMatrix2.resetSizesAndBlocks(matOpts.size_block_info,
                                     matOpts.size_block_info);
   randomMatrix1.random();
   randomMatrix2.random();
   randomMatrix  = 0;
   randomMatrix += (ergo_real)1.0 * randomMatrix1;
   randomMatrix += (ergo_real) - 1.0 * randomMatrix2;

   symmMatrix randomMatrix_ort(randomMatrix);

   if (transform_with_S_also)
   {
      transform_with_S(randomMatrix_ort);
   }

   transform_with_invChol(randomMatrix_ort);

   ergo_real acc = template_blas_sqrt(get_machine_epsilon());
   ergo_real randomMatrix_Norm     = randomMatrix.eucl(acc);
   ergo_real randomMatrix_ort_Norm = randomMatrix_ort.eucl(acc);

   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "norms of randomMatrix and randomMatrix_ort : %22.11f %22.11f",
             (double)randomMatrix_Norm, (double)randomMatrix_ort_Norm);

   // Normalize randomMatrix so that randomMatrix_ort would have norm 1.
   randomMatrix *= (ergo_real)(1.0 / randomMatrix_ort_Norm);

   ergo_real randomMatrixNormAfterNormalization = randomMatrix.eucl(acc);
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "randomMatrixNormAfterNormalization = %22.11f", (double)randomMatrixNormAfterNormalization);
}


void SCF_restricted::disturb_dens_matrix(ergo_real subspaceError)
{
   ergo_real gap = 2;
   ergo_real desiredErrorNorm = gap * subspaceError / (1 + subspaceError);

   symmMatrix randomMatrix;

   randomMatrix.resetSizesAndBlocks(matOpts.size_block_info,
                                    matOpts.size_block_info);
   get_non_ort_err_mat_normalized_in_ort_basis(randomMatrix, 1);

   densityMatrix.readFromFile();

   densityMatrix += desiredErrorNorm * randomMatrix;

   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "Disturbed density matrix with desiredErrorNorm = %22.11f  (gap = %8.3f)",
             (double)desiredErrorNorm, (double)gap);

   densityMatrix.writeToFile();
}


void SCF_restricted::disturb_dens_matrix_exact_try(const symmMatrix& randomMatrix,
                                                   const symmMatrix& orgDensMatrix,
                                                   ergo_real         disturbanceFactor,
                                                   ergo_real&        resultSinTheta,
                                                   symmMatrix&       resultDensMatrix)
{
   symmMatrix D(orgDensMatrix);

   D += (ergo_real)1.0 * disturbanceFactor * randomMatrix;

   symmMatrix SDS_symm(D);
   transform_with_S(SDS_symm);
   SDS_symm *= (ergo_real) - 1.0;

   symmMatrix F_ort_prev_dummy;
   F_ort_prev_dummy.resetSizesAndBlocks(matOpts.size_block_info,
                                        matOpts.size_block_info);
   F_ort_prev_dummy.writeToFile();

   resultDensMatrix.writeToFile();
   SDS_symm.writeToFile();


   int use_diag          = DensFromFock.get_use_diagonalization();
   int use_diag_on_error = DensFromFock.get_use_diag_on_error();

   DensFromFock.unset_use_diagonalization();
   DensFromFock.unset_use_diag_on_error();

   DensFromFock.clean_eigs_intervals();

   if (DensFromFock.get_dens_from_fock(SDS_symm,
                                       resultDensMatrix,
                                       F_ort_prev_dummy) != 0)
   {
      throw "SCF_restricted::disturb_dens_matrix_exact_try: Error in get_dens_from_fock";
   }

   if (use_diag == 1)
   {
      DensFromFock.set_use_diagonalization();
   }
   if (use_diag_on_error == 1)
   {
      DensFromFock.set_use_diag_on_error();
   }


   // OK, now we have computed D_Pure which is the purified version of SDS_symm.
   // But D_Pure is not in orthogonal basis.

   resultDensMatrix.readFromFile();

   symmMatrix diff(resultDensMatrix);
   diff += (ergo_real) - 1.0 * orgDensMatrix;

   transform_with_S(diff);
   transform_with_invChol(diff);

   // Compensate for factor 2 (restricted case)
   diff *= (ergo_real)0.5;

   ergo_real acc = template_blas_sqrt(get_machine_epsilon());

   resultSinTheta = diff.eucl(acc);
}


void SCF_restricted::disturb_dens_matrix_exact(ergo_real subspaceError)
{
   //ergo_real gap = 2;
   //ergo_real desiredErrorNorm = gap * subspaceError / (1 + subspaceError);

   symmMatrix randomMatrix;

   randomMatrix.resetSizesAndBlocks(matOpts.size_block_info,
                                    matOpts.size_block_info);
   get_non_ort_err_mat_normalized_in_ort_basis(randomMatrix, 1);

   symmMatrix newDensMatrix;
   newDensMatrix.resetSizesAndBlocks(matOpts.size_block_info,
                                     matOpts.size_block_info);

   densityMatrix.readFromFile();

   ergo_real currSinTheta;
   ergo_real disturbanceFactor_min = 0;
   ergo_real disturbanceFactor_max = 5;

   int iterCount = 0;
   do
   {
      iterCount++;
      if (iterCount > 44)
      {
         throw "error in SCF_restricted::disturb_dens_matrix_exact, iterCount esceeded limit.";
      }
      ergo_real disturbanceFactor = 0.5 * (disturbanceFactor_min + disturbanceFactor_max);
      disturb_dens_matrix_exact_try(randomMatrix,
                                    densityMatrix,
                                    disturbanceFactor,
                                    currSinTheta,
                                    newDensMatrix);
      if (currSinTheta < subspaceError)
      {
         disturbanceFactor_min = disturbanceFactor;
      }
      else
      {
         disturbanceFactor_max = disturbanceFactor;
      }
   } while (template_blas_fabs(currSinTheta - subspaceError) > 0.001 * subspaceError);

   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "SCF_restricted::disturb_dens_matrix_exact done, iterCount = %2i", iterCount);

   densityMatrix = newDensMatrix;

   densityMatrix.writeToFile();
}


void SCF_restricted::disturb_fock_matrix(ergo_real subspaceError)
{
   symmMatrix F_ort_prev_dummy;

   F_ort_prev_dummy.resetSizesAndBlocks(matOpts.size_block_info,
                                        matOpts.size_block_info);
   F_ort_prev_dummy.writeToFile();

   symmMatrix densityMatrix_dummy;
   densityMatrix_dummy.resetSizesAndBlocks(matOpts.size_block_info,
                                           matOpts.size_block_info);
   densityMatrix_dummy.writeToFile();



   int use_diag          = DensFromFock.get_use_diagonalization();
   int use_diag_on_error = DensFromFock.get_use_diag_on_error();

   DensFromFock.unset_use_diagonalization();
   DensFromFock.unset_use_diag_on_error();

   DensFromFock.clean_eigs_intervals();

   if (DensFromFock.get_dens_from_fock(FockMatrix,
                                       densityMatrix_dummy,
                                       F_ort_prev_dummy) != 0)
   {
      throw "SCF_restricted::disturb_fock_matrix: Error in get_dens_from_fock";
   }

   if (use_diag == 1)
   {
      DensFromFock.set_use_diagonalization();
   }
   if (use_diag_on_error == 1)
   {
      DensFromFock.set_use_diag_on_error();
   }

   intervalType homoInterval_tmp2;
   intervalType lumoInterval_tmp2;
   DensFromFock.get_eigs_F_ort_prev(homoInterval_tmp2, lumoInterval_tmp2);


   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "SCF_restricted::disturb_fock_matrix, interval sizes: %22.11f %22.11f",
             (double)(homoInterval_tmp2.upp() - homoInterval_tmp2.low()), (double)(lumoInterval_tmp2.upp() - lumoInterval_tmp2.low()));

   ergo_real gap = lumoInterval_tmp2.low() - homoInterval_tmp2.upp();

   ergo_real desiredErrorNorm = gap * subspaceError / (1 + subspaceError);

   symmMatrix randomMatrix;
   randomMatrix.resetSizesAndBlocks(matOpts.size_block_info,
                                    matOpts.size_block_info);
   get_non_ort_err_mat_normalized_in_ort_basis(randomMatrix, 0);

   FockMatrix.readFromFile();

   FockMatrix += desiredErrorNorm * randomMatrix;

   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "Disturbed Fock matrix with desiredErrorNorm = %22.11f  (gap = %8.3f)",
             (double)desiredErrorNorm, (double)gap);

   FockMatrix.writeToFile();
}


static ergo_real get_nucl_energy_for_given_mol_and_dens(const IntegralInfo&        integralInfo,
                                                        const Molecule&            molecule,
                                                        const BasisInfoStruct&     basisInfo,
                                                        const symmMatrix&          D,
                                                        ergo_real                  threshold_integrals_1el,
                                                        mat::SizesAndBlocks const& matrix_size_block_info,
                                                        std::vector<int> const&    permutationHML)
{
   ergo_real nuclearRepulsionEnergy = molecule.getNuclearRepulsionEnergyQuadratic();
   ergo_real elecNuclEnergy         = get_electron_nuclear_attraction_energy(integralInfo,
                                                                             molecule,
                                                                             basisInfo,
                                                                             D,
                                                                             threshold_integrals_1el,
                                                                             matrix_size_block_info,
                                                                             permutationHML);

   return nuclearRepulsionEnergy + elecNuclEnergy;
}


/* Compute gradient of energy with respect to nuclear positions, for
 * fixed electron density. */
void
SCF_restricted::compute_gradient_fixeddens()
{
   // Since we here regard the electron density as fixed, there are
   // only two terms in the energy which give nonzero contributions:
   // the nuclear-electron interaction term, and the nuclear-nuclear
   // interaction term.
   densityMatrix.readFromFile();
   int nAtoms = molecule.getNoOfAtoms();
   std::vector<ergo_real> gradient(nAtoms * 3);
   get_gradient_for_given_mol_and_dens(integralInfo,
                                       molecule,
                                       basisInfo,
                                       densityMatrix,
                                       threshold_integrals_1el,
                                       matOpts.size_block_info,
                                       matOpts.permutationHML,
                                       &gradient[0]);
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "Gradient of energy with respect to nuclear positions, "
	     "for fixed electron density (no Pulay correction, only Hellmann-Feynman forces):");
   for (int i = 0; i < nAtoms; i++)
   {
      do_output(LOG_CAT_INFO, LOG_AREA_SCF, "Atom %6d: %22.11f %22.11f %22.11f",
                i,
                (double)gradient[i * 3 + 0],
                (double)gradient[i * 3 + 1],
                (double)gradient[i * 3 + 2]);
   }
   do_output(LOG_CAT_INFO, LOG_AREA_SCF, "(End of gradient)");

   if (scfopts.verify_gradient_fixeddens == 1)
   {
      std::vector<ergo_real> gradient_for_verification(nAtoms * 3);
      for (int i = 0; i < nAtoms; i++)
      {
         for (int coordIdx = 0; coordIdx < 3; coordIdx++)
         {
            const ergo_real h           = 1e-3;
            Molecule        moleculeTmp = molecule;
            Atom            atomTmp     = molecule.getAtom(i);
            atomTmp.coords[coordIdx] += h;
            moleculeTmp.replaceAtom(i, atomTmp);
            ergo_real E1 = get_nucl_energy_for_given_mol_and_dens(integralInfo,
                                                                  moleculeTmp,
                                                                  basisInfo,
                                                                  densityMatrix,
                                                                  threshold_integrals_1el,
                                                                  matOpts.size_block_info,
                                                                  matOpts.permutationHML);
            moleculeTmp = molecule;
            atomTmp     = molecule.getAtom(i);
            atomTmp.coords[coordIdx] -= h;
            moleculeTmp.replaceAtom(i, atomTmp);
            ergo_real E2 = get_nucl_energy_for_given_mol_and_dens(integralInfo,
                                                                  moleculeTmp,
                                                                  basisInfo,
                                                                  densityMatrix,
                                                                  threshold_integrals_1el,
                                                                  matOpts.size_block_info,
                                                                  matOpts.permutationHML);
            ergo_real gradientComponent = (E1 - E2) / (2 * h);
            gradient_for_verification[i * 3 + coordIdx] = gradientComponent;
         } // END FOR coordIdx
      }    // END FOR i
      do_output(LOG_CAT_INFO, LOG_AREA_SCF, "Gradient of energy with respect to nuclear positions, for fixed electron density (for verification, computed using finite differences):");
      for (int i = 0; i < nAtoms; i++)
      {
         do_output(LOG_CAT_INFO, LOG_AREA_SCF, "Atom %6d: %22.11f %22.11f %22.11f",
                   i,
                   (double)gradient_for_verification[i * 3 + 0],
                   (double)gradient_for_verification[i * 3 + 1],
                   (double)gradient_for_verification[i * 3 + 2]);
      }
      do_output(LOG_CAT_INFO, LOG_AREA_SCF, "(End of gradient)");
      ergo_real maxAbsDiff     = -1;
      int       atomIdx_saved  = -1;
      int       coordIdx_saved = -1;
      for (int atomIdx = 0; atomIdx < nAtoms; atomIdx++)
      {
         for (int coordIdx = 0; coordIdx < 3; coordIdx++)
         {
            ergo_real absdiff = template_blas_fabs(gradient[atomIdx * 3 + coordIdx] - gradient_for_verification[atomIdx * 3 + coordIdx]);
            if (absdiff > maxAbsDiff)
            {
               maxAbsDiff     = absdiff;
               atomIdx_saved  = atomIdx;
               coordIdx_saved = coordIdx;
            }
         }
      }
      do_output(LOG_CAT_INFO, LOG_AREA_SCF, "maxAbsDiff %22.11f = %9.4g, for atomIdx %d and coordIdx %d", maxAbsDiff, maxAbsDiff, atomIdx_saved, coordIdx_saved);
   }

   densityMatrix.writeToFile();
}