-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTCL_prop.h
2189 lines (2015 loc) · 81.8 KB
/
TCL_prop.h
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
#ifndef TCLproph
#define TCLproph
#include <iostream>
#include <fstream>
#include <sstream>
#include <armadillo>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include "TCL.h"
#include "TCL_EE2.h"
#include "math.h"
#include "rks.h"
using namespace arma;
#define eVPerAu 27.2113
#define FsPerAu 0.0241888
inline bool FileExists(const char* name)
{
bool found= false;
try{
FILE* file;
found = (file = fopen(name, "r"));
}
catch(...)
{ }
return found;
}
class TDSCF
{
public:
int n_ao, n_mo;
int n_occ;
int n_aux;
int n, n_e;
int n_fock;
int loop, loop1, loop2, loop3;
int firstOrb, lastOrb;
int npol_col;
double ECore,Entropy,Energy; // Energy of core density.
double t;
long double wallt0;
rks* the_scf;
TCLMatrices* MyTCL;
EE2* ee2;
FieldMatrices* Mus;
TDSCF_Temporaries* Scratch;
std::map<std::string,double> params;
// For TDSSCF
vec f_diag;
cx_mat C;
cx_mat HJK; // For incfock.
mat P0;
vec f_diagb;
cx_mat V0,V; // (lowdinXFock change of basis.)
cx_mat Vs,Csub; // (lowdinXFock change of basis for a sub-matrix.)
// All these quantities are in the lowdin (x-basis) --------
cx_mat P0cx; // Initial core density matrix (x-basis)
cx_mat P_proj; // Projector onto 'core' initial density
cx_mat Q_proj; // Complement of 'core' initial density.
cx_mat Ps_proj; // Projector onto 'core' initial density (rectangular)
cx_mat Qs_proj; // Complement of 'core' initial density. (rectangular)
cx_mat sH,sHJK; // Small H and HJK matrices in case update fock is off.
cx_mat old_fock; // For incremental fock build
cx_mat old_p;
cx_mat Rho,NewRho,RhoM12;
mat Pol;
cx_vec LastFockPopulations;
vec LastFockEnergies;
arma::mat Pops; arma::mat FockEs;
// Parameters ---------
std::string logprefix;
// Parameters of any propagation. --------
bool RunDynamics;
double dt;
int MaxIter;
double RateBuildThresh;
bool ActiveSpace;
int ActiveFirst;
int ActiveLast;
double ActiveCutoff;
bool UpdateFockMatrix;
bool IncFock;
int UpdateEvery;
bool UpdateMarkovRates;
int Stabilize;
int InitialCondition; // Depreciate?
bool Restart;
bool ApplyImpulse;
bool ApplyNoOscField; // You can apply a static field.
bool ApplyCw;
double FieldFreq;
double FieldAmplitude;
vec FieldVector;
double Tau; // PulseLength
double tOn; // cental time of gaussian pulse.
// Relaxation Conditions ---------
double Temp;
bool TCLOn;
//--- Correlated Dynamics theory.
bool Corr;
bool RIFock;
bool Mp2NOGuess;
//--- Pump Probe.
int Stutter;
int StartFromSetman;
bool ApplyImpulse2; // These are unused normally owing to TDDFT's nonstationarity.
double FieldFreq2;
double FieldAmplitude2;
double Tau2;
double tOn2;
// Logging ---------
int SerialNumber;
int DipolesEvery;
int StatusEvery;
int SaveFockEvery;
int Print;
bool SaveDipoles;
bool SavePopulations;
bool SaveFockEnergies;
int WriteDensities;
int SaveEvery;
int FourierEvery;
double FourierZoom;
// Argument over-rides any defaults or params read from disk.
std::map<std::string,double> ReadParameters(std::map<std::string,double> ORParams_ = std::map<std::string,double>())
{
// Set defaults.
SerialNumber = 0;
Stutter = 0;
StartFromSetman = 0;
RunDynamics = true;
Corr = false;
RIFock = false;
Mp2NOGuess = true;
dt = 0.02;
MaxIter = 10000;
RateBuildThresh = 0.001;
ActiveSpace = false;
ActiveCutoff = 15.0/27.2113; // the tcl propagation is sometimes unstable with more than 30 electrons.
ActiveFirst = 0;
ActiveLast = 0;
UpdateFockMatrix = true; // Update the fock matrix during relaxation.
UpdateEvery = 1;
IncFock = false;
UpdateMarkovRates = true; // Update the markov rates when the eigenvalues change enough.
Stabilize = 2;
TCLOn = false;
// Initial Conditions.
Temp = 300.0;
InitialCondition = 0; // 0=Mean Field Solution. 1=Particle hole Excitation
Restart = false;
ApplyImpulse = true;
ApplyCw = false;
ApplyNoOscField = false;
FieldFreq = 1.1;
Tau = 0.07;
tOn = 7.0*Tau;
FieldAmplitude = 0.001;
FieldVector = vec(3); FieldVector.zeros(); FieldVector(1)=1.0;
// Second pulse.
ApplyImpulse2= false;
FieldFreq2 = 3.0/27.2113;
FieldAmplitude2 = 0.001;
Tau2 = 0.07;
tOn2 = tOn+10.0/0.024;
// Logging
DipolesEvery = 2;
StatusEvery = 15;
Print = 0;
SaveDipoles = true;
SavePopulations = false; // Save energy eigenbasis populations.
SaveFockEnergies = false; // Save energy eigenvalues during a relaxation process.
SaveFockEvery = 100;
SaveEvery = 200; // In units of dt
WriteDensities = 0; // 1=Write phaseless density (multiple times in one file for Jmol) 2=Write Attch/Detach (for gabedit)
FourierEvery = 5000;
FourierZoom = 0.269*(DipolesEvery*dt);
std::map<std::string,double> vals;
if (!FileExists("TDSCF.prm"))
{
cout << "===============================================" << endl;
cout << "Didn't find parameters file. Using defaults!!! " << endl;
cout << "===============================================" << endl;
}
else
{
ifstream prms("TDSCF.prm");
std::string key; double val;
prms.seekg(0);
if(prms.is_open())
{
while(!prms.eof())
{
prms >> key >> val;
vals[key]=val;
}
}
else
{
cout << "Couldn't open Parameters :( ... " << endl;
throw 1;
}
prms.close();
ORParams_.insert(vals.begin(), vals.end()); // Passed parameters over-ride defaults and TDSCF.prm.
std::swap(vals,ORParams_);
// The fact that this is the fewest-lines way to do this in C++ is a simple example of what a failure the language was before x11 which sadly qchem will not compile under.
Stutter = ((vals.find("Stutter") != vals.end())? vals.find("Stutter")->second : Stutter);
SerialNumber = ((vals.find("SerialNumber") != vals.end())? vals.find("SerialNumber")->second : SerialNumber);
StartFromSetman = ((vals.find("StartFromSetman") != vals.end())? vals.find("StartFromSetman")->second : StartFromSetman);
RunDynamics = ((vals.find("RunDynamics") != vals.end())? vals.find("RunDynamics")->second : RunDynamics);
Corr = ((vals.find("Corr") != vals.end())? vals.find("Corr")->second : Corr);
RIFock = ((vals.find("RIFock") != vals.end())? vals.find("RIFock")->second : RIFock);
if ((vals.find("GML") == vals.end()))
vals["GML"] = 0.0;
else
vals["GML"] = vals.find("GML")->second;
if ((vals.find("FD") == vals.end()))
vals["FD"] = 0.0;
else
vals["FD"] = vals.find("FD")->second;
if ((vals.find("DumpEE2") == vals.end()))
vals["DumpEE2"] = 0.0;
else
vals["DumpEE2"] = vals.find("FD")->second;
Mp2NOGuess = ((vals.find("Mp2NOGuess") != vals.end())? vals.find("Mp2NOGuess")->second : Mp2NOGuess);
dt = ((vals.find("dt") != vals.end())? vals.find("dt")->second : dt);
RateBuildThresh = ((vals.find("RateBuildThresh") != vals.end())? vals.find("RateBuildThresh")->second : RateBuildThresh);
ActiveSpace = ((vals.find("ActiveSpace") != vals.end())? vals.find("ActiveSpace")->second : ActiveSpace);
ActiveFirst = ((vals.find("ActiveFirst") != vals.end())? vals.find("ActiveFirst")->second : ActiveFirst);
ActiveLast = ((vals.find("ActiveLast") != vals.end())? vals.find("ActiveLast")->second : ActiveLast);
Restart = ((vals.find("Restart") != vals.end())? vals.find("Restart")->second : Restart);
if ((vals.find("CanRestart") == vals.end()))
vals["CanRestart"] = 0.0;
Stabilize = ((vals.find("Stabilize") != vals.end())? vals.find("Stabilize")->second : Stabilize);
ActiveCutoff = ((vals.find("ActiveCutoff") != vals.end())? vals.find("ActiveCutoff")->second : ActiveCutoff);
UpdateFockMatrix = ((vals.find("UpdateFockMatrix") != vals.end())? vals.find("UpdateFockMatrix")->second : UpdateFockMatrix);
UpdateEvery = ((vals.find("UpdateEvery") != vals.end())? vals.find("UpdateEvery")->second : UpdateEvery);
IncFock = ((vals.find("IncFock") != vals.end())? vals.find("IncFock")->second : IncFock);
UpdateMarkovRates = ((vals.find("UpdateMarkovRates") != vals.end())? vals.find("UpdateMarkovRates")->second : UpdateMarkovRates);
TCLOn = ((vals.find("TCLOn") != vals.end())? vals.find("TCLOn")->second : TCLOn);
Temp = ((vals.find("Temp") != vals.end())? vals.find("Temp")->second : Temp);
MaxIter = ((vals.find("MaxIter") != vals.end())? vals.find("MaxIter")->second : MaxIter);
InitialCondition = ((vals.find("InitialCondition") != vals.end())? vals.find("InitialCondition")->second : InitialCondition);
ApplyImpulse = ((vals.find("ApplyImpulse") != vals.end())? vals.find("ApplyImpulse")->second : ApplyImpulse);
ApplyCw = ((vals.find("ApplyCw") != vals.end())? vals.find("ApplyCw")->second : ApplyCw);
FieldFreq = ((vals.find("FieldFreq") != vals.end())? vals.find("FieldFreq")->second : FieldFreq);
Tau = ((vals.find("Tau") != vals.end())? vals.find("Tau")->second : Tau);
tOn = ((vals.find("tOn") != vals.end())? vals.find("tOn")->second : tOn);
FieldAmplitude = ((vals.find("FieldAmplitude") != vals.end())? vals.find("FieldAmplitude")->second : FieldAmplitude);
FieldVector(0) = ((vals.find("ExDir") != vals.end())? vals.find("ExDir")->second : FieldVector(0));
FieldVector(1) = ((vals.find("EyDir") != vals.end())? vals.find("EyDir")->second : FieldVector(1));
FieldVector(2) = ((vals.find("EzDir") != vals.end())? vals.find("EzDir")->second : FieldVector(2));
ApplyImpulse2 = ((vals.find("ApplyImpulse2") != vals.end())? vals.find("ApplyImpulse2")->second : ApplyImpulse2);
ApplyNoOscField = ((vals.find("ApplyNoOscField") != vals.end())? vals.find("ApplyNoOscField")->second : ApplyNoOscField);
FieldFreq2 = ((vals.find("FieldFreq2") != vals.end())? vals.find("FieldFreq2")->second : FieldFreq2);
FieldAmplitude2 = ((vals.find("FieldAmplitude2") != vals.end())? vals.find("FieldAmplitude2")->second : FieldAmplitude2);
Tau2 = ((vals.find("Tau2") != vals.end())? vals.find("Tau2")->second : Tau2);
tOn2 = ((vals.find("tOn2") != vals.end())? vals.find("tOn2")->second : FieldFreq);
StatusEvery = ((vals.find("StatusEvery") != vals.end())? vals.find("StatusEvery")->second : StatusEvery);
Print = ((vals.find("Print") != vals.end())? vals.find("Print")->second : Print);
SaveDipoles = ((vals.find("SaveDipoles") != vals.end())? vals.find("SaveDipoles")->second : SaveDipoles);
SavePopulations = ((vals.find("SavePopulations") != vals.end())? vals.find("SavePopulations")->second : SavePopulations);
if ((vals.find("PrntPopulationType") == vals.end()))
vals["PrntPopulationType"] = 0.0; // 0 = Populations of fock orbitals. 1= Natural orbital populations.
if ((vals.find("SavePopulationsEvery") == vals.end()))
vals["SavePopulationsEvery"] = 100.0;
if ((vals.find("TSample") == vals.end()))
vals["TSample"] = 1000.0;
if ((vals.find("TWarm") == vals.end()))
vals["TWarm"] = 1.0;
if ((vals.find("ExcitedFraction") == vals.end()))
vals["ExcitedFraction"] = 1.0;
SaveFockEnergies = ((vals.find("SaveFockEnergies") != vals.end())? vals.find("SaveFockEnergies")->second : SaveFockEnergies);
SaveFockEvery = ((vals.find("SaveFockEvery") != vals.end())? vals.find("SaveFockEvery")->second : SaveFockEvery);
WriteDensities = ((vals.find("WriteDensities") != vals.end())? vals.find("WriteDensities")->second : WriteDensities);
SaveEvery = ((vals.find("SaveEvery") != vals.end())? vals.find("SaveEvery")->second : SaveEvery);
DipolesEvery = ((vals.find("DipolesEvery") != vals.end())? vals.find("DipolesEvery")->second : DipolesEvery);
FourierEvery = ((vals.find("FourierEvery") != vals.end())? vals.find("FourierEvery")->second : FourierEvery);
FourierZoom = ((vals.find("FourierZoom") != vals.end())? vals.find("FourierZoom")->second :
0.134524*(DipolesEvery*dt));
}
// Set up separate logging prefixes if there is more than one propagation going on.
if (SerialNumber!=0)
{
char str[12];
sprintf(str, "%i", SerialNumber);
std::string suffix(str);
logprefix=logprefix+std::string("S")+suffix+std::string("_");
}
cout << "CONDITIONS:-----------" << endl;
cout << "RIFock: " << RIFock << endl;
if (Corr)
{
cout << "Corr: " << Corr << endl;
cout << "GML (Gell-Man Low): " << vals["GML"] << endl;
cout << "Mp2NOGuess: " << Mp2NOGuess << endl;
}
cout << "TCLOn: " << TCLOn << endl;
cout << "Temp: " << Temp << endl;
if (TCLOn)
{
cout << "UpdateMarkovRates: " << UpdateMarkovRates << endl;
cout << "RateBuildThresh: " << RateBuildThresh << endl;
}
cout << "InitialCondition: " << InitialCondition << endl;
cout << "StartFromSetman: " << StartFromSetman << endl;
cout << "MaxIter: " << MaxIter << endl;
cout << "Restart: " << Restart << endl;
cout << "FieldAmplitude: " << FieldAmplitude << endl;
cout << "ApplyImpulse: " << ApplyImpulse << endl;
cout << "ApplyCw: " << ApplyCw << endl;
cout << "FieldFreq: " << FieldFreq << endl;
cout << "Tau: " << Tau << endl;
cout << "tOn: " << tOn << endl;
FieldVector.st().print("FieldVector");
if (ApplyNoOscField)
{
cout << "ApplyNoOscField: " << ApplyNoOscField << endl;
cout << "Tau: " << Tau << endl;
cout << "tOn: " << tOn << endl;
}
if (ApplyImpulse2)
{
cout << "ApplyImpulse2: " << ApplyImpulse2 << endl;
cout << "FieldAmplitude2: " << FieldAmplitude2 << endl;
cout << "FieldFreq2: " << FieldFreq2 << endl;
cout << "Tau2: " << Tau2 << endl;
cout << "tOn2: " << tOn2 << endl;
}
cout << "THRESHOLDS: -----------" << endl;
cout << "dt: " << dt << endl;
cout << "ActiveSpace: " << ActiveSpace << endl;
if (ActiveLast!=0)
{
cout << "ActiveFirst: " << ActiveFirst << endl;
cout << "ActiveLast: " << ActiveLast << endl;
}
else
cout << "ActiveSpaceCutoff: " << ActiveCutoff << endl;
cout << "UpdateFockMatrix: " << UpdateFockMatrix << endl;
if (UpdateFockMatrix && UpdateEvery!=1)
cout << "UpdateFockMatrix at (n) Steps: " << UpdateEvery << endl;
cout << "IncFock : " << IncFock << endl;
cout << "Stabilize: " << Stabilize << endl;
cout << "LOGGING:--------------" << endl;
cout << "SerialNumber: " << SerialNumber << endl;
cout << "logprefix: " << logprefix << endl;
cout << "Stutter: " << Stutter << endl;
cout << "StatusEvery: " << StatusEvery << endl;
cout << "DipolesEvery: " << DipolesEvery << endl;
cout << "Print: " << Print << endl;
cout << "SaveDipoles: " << SaveDipoles << endl;
cout << "SaveEvery: " << SaveEvery << endl;
cout << "WriteDensities: " << WriteDensities << endl;
cout << "FourierEvery: " << FourierEvery << endl;
cout << "FourierZoom: " << FourierZoom << endl;
cout << "SavePopulations: " << SavePopulations << endl;
cout << "SavePopulationsEvery" << vals["SavePopulationsEvery"] << endl;
cout << "SaveFockEnergies: " << SaveFockEnergies << endl;
cout << "SaveFockEvery: " << SaveFockEvery << endl;
cout << "======================" << endl << endl;
return vals;
}
void SetupActiveSpace()
{
if (ActiveFirst==0)
{
for (; firstOrb<n; ++firstOrb)
if (abs(f_diagb(firstOrb)-f_diagb(n_occ-1))<ActiveCutoff)
break;
lastOrb = firstOrb+1;
for (; lastOrb<n; ++lastOrb)
if (abs(f_diagb(lastOrb)-f_diagb(n_occ-1))>ActiveCutoff)
break;
if (lastOrb < n_occ + 3)
lastOrb = min(n,lastOrb+8);
cout << "Propagating within the sub-matrix: " << firstOrb << " to " << lastOrb << " (inclusive)" << endl;
if (lastOrb == n)
lastOrb = n-1;
cout << "Propagating within the sub-matrix: " << f_diagb(firstOrb) << " (eH) to " << f_diagb(lastOrb) << " eH (inclusive)" << endl;
cout << "Propagating within the sub-matrix: " << 27.2113*f_diagb(firstOrb) << " (eV) to " << 27.2113*f_diagb(lastOrb) << " eV (inclusive)" << endl;
}
else
{
firstOrb = ActiveFirst;
lastOrb = ActiveLast;
}
int nact = lastOrb-firstOrb+1;
// Generate projectors these are in the Lowdin (X) basis.
cx_mat P_(n,n); P_.eye();
P0cx=P_;
P0cx.submat(firstOrb,firstOrb,n-1,n-1) *= 0;
//P0cx is the core density matrix in the lowdin basis.
P0cx = V*P0cx*V.t(); // Lowdin basis core density matrix.
P_.submat(firstOrb,firstOrb,lastOrb,lastOrb) *= 0.0;
P_proj = V*P_*V.t(); // Lowdin basis core inactive projector
Q_proj = eye<cx_mat>(n,n) - V*P_*V.t(); // Lowdin basis active projector
// Make the rectangular versions too:
Ps_proj.zeros(); Qs_proj.zeros();
Ps_proj.resize(n,n-nact); // projector onto inactive space (low X fock)
int j=0;
for (int i=0;i<n;++i)
{
if (i<firstOrb or i>lastOrb)
{
Ps_proj.col(j) = V.col(i);
++j;
}
}
Qs_proj.resize(n,nact); // projector onto active space (low X fock)
j=0;
for (int i=0;i<n;++i)
{
if (i>=firstOrb and i<=lastOrb)
{
Qs_proj.col(j) = V.col(i);
++j;
}
}
cout << "N Core St: " << trace(real(P_proj)) << " N Active St:" << trace(real(Q_proj)) << endl;
P_.eye(); P_.submat(the_scf->NOcc,the_scf->NOcc,n-1,n-1) *= 0.0; cx_mat Px = V*P_*V.t();
cout << "Tr(Ps Core-Space)" << trace(Ps_proj.t()*Px*Ps_proj) << " Tr(Ps Active Space)" << trace(Qs_proj.t()*Px*Qs_proj) << endl;
cout << " Tr(Pc Active Space) (should be zero) " << trace(Qs_proj.t()*P0cx*Qs_proj) << endl;
f_diag = f_diagb.subvec(firstOrb,lastOrb);
n = lastOrb-firstOrb+1;
n_e = n_occ - firstOrb;
Vs = Qs_proj; // LAO->current eigenbasis.
Csub = the_scf->X*Vs;
old_fock.zeros();
old_p.zeros();
}
void PlotFields()
{
cx_mat Field(n_mo,n_mo); Field.fill(0.0);
double x=10.0; bool y = false;
mat FieldR = real(Mus->mux);
//FieldR.print("Real Field");
vec eigval; mat Cu;
eig_sym(eigval,Cu,FieldR);//*the_scf->AOS);
//eigval.print("Eigval");
String fileName = String("./Perturbation.molden");
FILE *fp = fopen(fileName.c_str(),"w"); //QOpen(fileName,"w");
WriteMoldenATOMS_Repositioned(fp);
WriteMoldenGTO(fp);
vec dTs(n_mo); dTs.zeros();
dTs(0)=99.0;
WriteMoldenDiffDen(Cu.memptr(),Cu.memptr(),dTs.memptr(),dTs.memptr(),Cu.memptr(),Cu.memptr(),
the_scf->NOcc,the_scf->NOrb-the_scf->NOcc,the_scf->NOcc,the_scf->NOrb-the_scf->NOcc,fp);
fclose(fp);
if (ApplyImpulse) // Generate a power spectrum of the impulse for reference.
{
mat imp(10000,2);
for (int i=0; i<10000; i+=1)
{
imp(i,0)=dt*(double)i;
imp(i,1)=Mus->ImpulseAmp(dt*(double)i);
}
mat four=Fourier(imp,FourierZoom);
four.save(logprefix+"FourierImpulse"+".csv", raw_ascii);
}
}
void SetmanDensity()
{
int n_vir = n_mo - n_occ;
int StartState = max(rem_read(REM_RTPPSTATE),0);
cout << "Initializing from Linear Response State (starts from 1):" << StartState << endl;
// To read out of the files written by setman.
// Try Reading the RPA from setman.
vec Xv(n_occ*n_vir),Yv(n_occ*n_vir);
Xv.zeros(); Yv.zeros();
bool RPA = true;
FileMan(FM_READ,FILE_SET_TMP_RPA_X,FM_DP,n_vir*n_occ,0+n_vir*n_occ*(StartFromSetman-1),FM_BEG,Xv.memptr());
if(RPA)
FileMan(FM_READ,FILE_SET_TMP_RPA_Y,FM_DP,n_vir*n_occ,0+n_vir*n_occ*(StartFromSetman-1),FM_BEG,Yv.memptr());
mat X(n_occ,n_vir),Y(n_occ,n_vir);
cx_mat RhoRPA(n_mo,n_mo); RhoRPA.zeros();
for (int a=0; a<n_vir; ++a)
for (int i=0; i<n_occ; ++i)
{
X(i,a) = Xv(i*n_vir + a);
Y(i,a) = Yv(i*n_vir + a);
}
if (n_mo<12 or Print > 1)
{
X.print("X");
Y.print("Y");
}
for (int i=0; i<n_occ; ++i)
{
for (int j=0; j<n_occ; ++j)
{
for (int a=0; a<n_vir; ++a)
{
RhoRPA(i,j) -= X(i, a)*X(j, a);
RhoRPA(i,j) += Y(i, a)*Y(j, a);
}
}
}
for (int b=0; b<n_vir; ++b)
{
for (int a=0; a<n_vir; ++a)
{
for (int i=0; i<n_occ; ++i)
{
RhoRPA(a + n_occ,b+n_occ) += X(i,a)*X(i,b);
RhoRPA(a + n_occ,b+n_occ) -= Y(i,a)*Y(i,b);
}
}
}
/*
vec tvec = evecs_.col(StartState-1); // is an OxV mat.
// Add electron and subtract hole densities from the initial density.
for (int i=0; i<n_occ; ++i)
{
for (int j=0; j<n_occ; ++j)
{
for (int a=0; a<n_vir; ++a)
{
Rho(i,j) -= tvec(i*n_vir + a)*tvec(j*n_vir + a);
}
}
}
for (int b=0; b<n_vir; ++b)
{
for (int a=0; a<n_vir; ++a)
{
for (int i=0; i<n_occ; ++i)
Rho(a + n_occ,b+n_occ) += tvec(i*n_vir + a)*tvec(i*n_vir + b);
}
}
*/
// Add this to the orig density matrix in the active space.
Rho += (params["ExcitedFraction"])*RhoRPA.submat(firstOrb,firstOrb,lastOrb,lastOrb);
cout << "Constructed Particle-Hole Density" << endl;
cout << "Tr(Rho)" << trace(Rho) << endl;
cout << "Idempotency: " << accu(Rho-Rho*Rho) << endl;
Rho.diag().st().print("Raw Populations:");
cout << "Ensuring positivity." << endl;
Posify(Rho,n_e);
Rho.diag().st().print("Initial Populations:");
}
void ReadRestart()
{
if (!(FileExists((logprefix+"Rho_lastsave").c_str())))
{
cout << "Cannot Restart... missing data." << endl;
Pol.resize(5000,npol_col);
Pol.zeros(); // Polarization Energy, Trace, Gap, Entropy, homo-lumo Coherence.
}
else
{
try{
cout << "Reading old outputs to restart..." << endl;
// Read in the old Pol.
Pol.load(logprefix+"Pol.csv",raw_ascii);
int i=2;
for (; i<Pol.n_rows; ++i)
if (Pol(i,0) == 0.0)
break;
if (i == Pol.n_rows-1)
cout << "Trouble Reading Pol File... " << endl;
t = Pol(i-1,0);
loop=i*DipolesEvery;
loop2=i;
loop3=int(loop/SaveEvery);
cout << " *** Disabling save of fock energies and populations. *** " << endl;
SavePopulations = false;
SaveFockEnergies = false;
cout << "Reading Active space Rho and starting from there." << endl;
f_diag.load(logprefix+"f_diag_lastsave");
Rho.load(logprefix+"Rho_lastsave");
V.load(logprefix+"V_lastsave");
Mus->load(logprefix+"Mus_lastsave_");
Vs.load(logprefix+"Vs_lastsave");
if (UpdateFockMatrix)
{
HJK.load(logprefix+"HJK_lastsave");
RhoM12.load(logprefix+"RhoM12_lastsave");
}
}
catch(...)
{
cout << "Restart -------- FAILURE ------- " << endl;
throw;
}
}
}
void SplitLiouvillian(const arma::cx_mat& oldrho, arma::cx_mat& newrho, const double& tnow, bool& IsOn) const
{
if (TCLOn)
{
newrho.zeros();
MyTCL->ContractGammaRhoMThreads(newrho,oldrho);
}
}
void Split_RK4_Step(const arma::vec& f_diag, const arma::cx_mat& oldrho, arma::cx_mat& newrho, const double tnow, const double dt, bool& IsOn) const
{
if (!is_finite(oldrho) or !is_finite(f_diag))
{
cout << "Split_RK4_Step passed Garbage" << endl;
throw 1;
}
arma::cx_mat f_halfstep(newrho); f_halfstep.eye();
arma::cx_mat RhoHalfStepped(newrho); RhoHalfStepped.zeros();
cx_vec f_diagc(f_diag.n_elem); f_diagc.set_real(f_diag);
arma::cx_mat F = diagmat(f_diagc);
Mus->ApplyField(F,tnow,IsOn);
// Exponential propagator for Mu.
vec eigval;
cx_mat Cu;
eig_sym(eigval,Cu,F);
cx_mat Ud = exp(eigval*std::complex<double>(0.0,-0.5)*dt);
cx_mat U = Cu*diagmat(Ud)*Cu.t();
RhoHalfStepped = U*oldrho*U.t();
if (!is_finite(newrho))
{
cout << "Garbage output in RhoHalfStepped" << endl;
F.print("F");
oldrho.print("oldrho");
throw 1;
}
arma::cx_mat k1(newrho),k2(newrho),k3(newrho),k4(newrho),v2(newrho),v3(newrho),v4(newrho);
k1.zeros(); k2.zeros(); k3.zeros(); k4.zeros();
v2.zeros(); v3.zeros(); v4.zeros();
SplitLiouvillian( RhoHalfStepped, k1,tnow,IsOn);
v2 = (dt/2.0) * k1;
v2 += RhoHalfStepped;
SplitLiouvillian( RhoHalfStepped, k2,tnow+(dt/2.0),IsOn);
v3 = (dt/2.0) * k2;
v3 += RhoHalfStepped;
SplitLiouvillian( RhoHalfStepped, k3,tnow+(dt/2.0),IsOn);
v4 = (dt) * k3;
v4 += RhoHalfStepped;
SplitLiouvillian( RhoHalfStepped, k4,tnow+dt,IsOn);
newrho = RhoHalfStepped;
newrho += dt*(1.0/6.0)*k1;
newrho += dt*(2.0/6.0)*k2;
newrho += dt*(2.0/6.0)*k3;
newrho += dt*(1.0/6.0)*k4;
newrho = U*newrho*U.t();
}
void Split_RK4_Step_MMUT(const arma::vec& eigval, const arma::cx_mat& Cu , const arma::cx_mat& oldrho, arma::cx_mat& newrho, const double tnow, const double dt, bool& IsOn) const
{
cx_mat Ud = exp(eigval*std::complex<double>(0.0,-0.5)*dt);
cx_mat U = Cu*diagmat(Ud)*Cu.t();
cx_mat RhoHalfStepped = U*oldrho*U.t();
arma::cx_mat k1(newrho),k2(newrho),k3(newrho),k4(newrho),v2(newrho),v3(newrho),v4(newrho);
k1.zeros(); k2.zeros(); k3.zeros(); k4.zeros();
v2.zeros(); v3.zeros(); v4.zeros();
SplitLiouvillian( RhoHalfStepped, k1,tnow,IsOn);
v2 = (dt/2.0) * k1;
v2 += RhoHalfStepped;
SplitLiouvillian( RhoHalfStepped, k2,tnow+(dt/2.0),IsOn);
v3 = (dt/2.0) * k2;
v3 += RhoHalfStepped;
SplitLiouvillian( RhoHalfStepped, k3,tnow+(dt/2.0),IsOn);
v4 = (dt) * k3;
v4 += RhoHalfStepped;
SplitLiouvillian( RhoHalfStepped, k4,tnow+dt,IsOn);
newrho = RhoHalfStepped;
newrho += dt*(1.0/6.0)*k1;
newrho += dt*(2.0/6.0)*k2;
newrho += dt*(2.0/6.0)*k3;
newrho += dt*(1.0/6.0)*k4;
newrho = U*newrho*U.t();
}
void MMUTStep(arma::vec& f_diag, arma::cx_mat& HJK, arma::cx_mat& V, arma::cx_mat& Rho_, arma::cx_mat& RhoM12_, const double tnow , const double dt,bool& IsOn)
{
cx_mat CPrime(Rho_);
// Fock Rebuild Rho at this time.
CPrime = UpdateLiouvillian(f_diag,HJK,Rho_); // This rotates Rho, we must also rotate RhoM12!!!
RhoM12_ = CPrime.t()*RhoM12_*CPrime;
if (Stabilize)
Posify(RhoM12_, n_e);
// Make the ingredients for U.
arma::cx_mat F(Rho_); F.zeros(); F.set_real(diagmat(f_diag));
Mus->ApplyField(F,tnow,IsOn);
// Exponential propagator for Mu.
vec eigval; cx_mat Cu;
eig_sym(eigval,Cu,F);
// Full step RhoM12 to make new RhoM12.
cx_mat NewRhoM12(Rho_);
cx_mat NewRho(Rho_);
Split_RK4_Step_MMUT(eigval, Cu, RhoM12_, NewRhoM12, tnow, dt, IsOn);
// Half step that to make the new Rho.
Split_RK4_Step_MMUT(eigval, Cu, NewRhoM12, NewRho, tnow, dt/2.0, IsOn);
Rho_ = 0.5*(NewRho+NewRho.t());
RhoM12_ = 0.5*(NewRhoM12+NewRhoM12.t());
}
void InitializeLiouvillian(vec& f_diag_, cx_mat& V, cx_mat& mu_x_, cx_mat& mu_y_, cx_mat& mu_z_)
{
int n = the_scf->NOrb;
cx_mat Rhob(n,n); Rhob.zeros();
cout << "Initializing the Liouvillian for " << n << " orbitals " << endl;
the_scf->UpdateLiouvillian(Scratch, f_diag_,V,mu_x_,mu_y_,mu_z_,Rhob,old_fock,old_p,false);
}
// arg is in current Eigenbasis and mapped to Lowdin.
cx_mat FullRho(const cx_mat& arg) const
{
if (ActiveSpace)
{
// Enlarge the density into the whole x space.
cx_mat tmp = (Vs*arg*Vs.t());
if (Print)
cout << "FullRho: " << trace(tmp)<< trace(P0cx+tmp) << endl;
return (P0cx+tmp);
}
else
return (V*arg*V.t());
}
// Sync State members with another TDSCF object.
// Ie: after this routine the propagations are in the same state.
// Obviously between the Active space, incremental builds, etc. the number of state
// variables has gotten out of control, and needs to be objectified.
void Sync(const TDSCF* other)
{
cout << "P: " << SerialNumber << " Syncing from " << other->SerialNumber << endl;
Mus->Sync(other->Mus);
firstOrb = other->firstOrb;
lastOrb = other->lastOrb;
f_diag = other->f_diag;
C=other->C;
HJK=other->HJK;
P0=other->P0;
f_diagb=other->f_diagb;
V0=other->V0;
V=other->V;
Vs=other->Vs;
Csub=other->Csub;
P0cx=other->P0cx;
P_proj=other->P_proj;
Q_proj=other->Q_proj;
Ps_proj=other->Ps_proj;
Qs_proj=other->Qs_proj;
sH=other->sH;
sHJK=other->sHJK;
old_fock=other->old_fock;
old_p=other->old_p;
Rho=other->Rho;
NewRho=other->NewRho;
RhoM12=other->RhoM12;
Mus->InitializeExpectation(Rho);
cout << "Sync'd Mean-Field Energy: " << MeanFieldEnergy(Rho,V,HJK) << endl;
}
// P_ is in the current fock eigenbasis and will be updated.
// V (the lowdin to fock transformation) is updated.
// Fock is in the AO basis and will be updated.
// The matrix returned is the Rotation matrix into the new fock eigenbasis from the old fock eigenbasis.
cx_mat UpdateLiouvillian(vec& f_diag_, cx_mat& HJK, cx_mat& P_, bool Rebuild=true)
{
cx_mat Cprime(P_);
if (Print>3)
P_.print("P_ in update.");
int n = the_scf->NOrb;
int n_e = the_scf->NOcc;
cx_mat Rhob = FullRho(P_); // Makes an X-basis density
if (Print>3)
Rhob.print("Rhob");
if (Rebuild)
{
arma::cx_mat tmp;
if (!ActiveSpace)
Cprime = the_scf->UpdateLiouvillian(Scratch, f_diag_,V,HJK,tmp,tmp,Rhob,old_fock,old_p,IncFock && !(n_fock%100==0));
else
Cprime = the_scf->UpdateLiouvillian_ActiveSpace(Scratch,f_diag_,V,Vs,HJK,Rhob,P0cx,P_proj,Q_proj,Ps_proj,Qs_proj,old_fock,old_p,IncFock && !(n_fock%100==0));
if (Stabilize)
Posify(Rhob,the_scf->NOcc);
if (!ActiveSpace)
{
P_ = V.t()*Rhob*V;
Vs=V;
}
else
{
// Rebuild overall V-matrix.
P_ = Vs.t()*Rhob*Vs;
}
Csub=the_scf->X*Vs;
// Update Dipole Matrices Into current fock basis.
Mus->update(Csub);
// Update TCL.
if (TCLOn && MyTCL != NULL)
MyTCL->update(Csub,f_diag_);
}
else if (Stabilize)
Posify(Rhob,the_scf->NOcc);
if (!is_finite(Rhob))
{
cout << "Posify returned garbage... " << endl;
throw 1;
}
if (Print>3)
P_.print("P_");
return Cprime;
}
double VonNeumannEntropy(cx_mat& rho,bool print=false) const
{
vec eigval;
cx_mat Cprime;
if (!eig_sym(eigval, Cprime, rho, "dc"))
{
return 0.0;
}
if (print)
eigval.st().print("Natural Occs");
vec omp = 1.0-eigval;
vec olnp = log(omp);
vec lnp = log(eigval);
for (int i=0; i<eigval.n_elem; ++i)
{
if (eigval(i)<pow(10.0,-12.0))
lnp(i)=0.0;
if (omp(i)<pow(10.0,-12.0))
olnp(i)=0.0;
}
return -2.0*(dot(eigval,lnp)+dot(omp,olnp));
}
double MeanFieldEnergy(cx_mat& rho, cx_mat& V, cx_mat& HJK) const
{
cx_mat Plao;
if (!ActiveSpace)
{
Plao = V*rho*V.t();
arma::cx_mat Pao = the_scf->X.t()*Plao*the_scf->X;
double Etot = real(dot(Pao,the_scf->H)+ dot(Pao,HJK))+ the_scf->nuclear_energy()+the_scf->Ec+the_scf->Ex;
return Etot;
}
else
{
Plao = P0cx+Vs*rho*Vs.t(); // P0cx is in the x basis.
arma::cx_mat Pao = the_scf->X.t()*Plao*the_scf->X;
double Etot = real(dot(Pao,the_scf->H)+ dot(Pao,HJK))+ the_scf->nuclear_energy()+the_scf->Ec+the_scf->Ex;
return Etot;
}
}
// Ie: with Aufbau occupations.
double HundEnergy(cx_mat& rho, cx_mat& V, cx_mat& HJK) const
{
arma::cx_mat PTemp(the_scf->NOrb,the_scf->NOrb);
PTemp.eye();
PTemp.submat(the_scf->NOcc,the_scf->NOcc,the_scf->NOrb-1,the_scf->NOrb-1) *= 0.0;
arma::cx_mat Plao = V*PTemp*(V.t());
arma::cx_mat Pao = the_scf->X.t()*Plao*the_scf->X;
double Etot = real(dot(Pao,the_scf->H)+ dot(Pao,HJK))+ the_scf->nuclear_energy()+the_scf->Ec+the_scf->Ex;
return Etot;
}
// The other version of this routine can leave atoms too far away from the origin to actually be plotted. ;(
// This version cleans that up.
void WriteMoldenATOMS_Repositioned(FILE *fp) const
{
//
// Write the [Atoms] section containing the geometry.
//
if (!fp) fp = stdout;
// this will print ALL atoms (QM + MM) ... WAIT FUCK THAT... Gotta undo that bit.
int NAtoms = rem_read(REM_NATOMS);
int *AtNo;
double *Carts3,*xyz;
get_carts(NULL,&Carts3,&AtNo,NULL);
xyz = QAllocDouble(3*NAtoms);
VRcopy(xyz,Carts3,3*NAtoms);
bool useBohr = (rem_read(REM_INPUT_BOHR) == 1) ? true : false;
if (!useBohr) VRscale(xyz,3*NAtoms,ConvFac(BOHRS_TO_ANGSTROMS));
fprintf(fp,"[Molden Format]\n[Atoms] ");
if (useBohr)
fprintf(fp,"(AU)\n");
else
fprintf(fp,"(Angs)\n");
double xm=0;
double ym=0;
double zm=0;
for (int i = 0; i < NAtoms; i++){
xm += xyz[3*i];
ym += xyz[3*i+1];
zm += xyz[3*i+2];
}
for (int i = 0; i < NAtoms; i++){
String AtSymb = AtomicSymbol(AtNo[i]);
char *atsymb = (char*)AtSymb;
fprintf(fp,"%3s %6d %4d %15.8f %15.8f %15.8f\n",atsymb,
i+1,AtNo[i],xyz[3*i]-xm/NAtoms,xyz[3*i+1]-ym/NAtoms,xyz[3*i+2]-zm/NAtoms);
}
QFree(xyz);
}
// Allows for variable occupation numbers which are correctly plotted in GABEDIT.
void WriteMoldenDiffDen(double *jCA, double *jCB, double *jEA, double *jEB, double *occa, double* occb,
int NOccA, int NVirA, int NOccB, int NVirB, FILE *fp, bool Rest=false) const
{
int NOccAPrt=NOccA;
int NOccBPrt=NOccA;
int NVirAPrt=NVirA;
int NVirBPrt=NVirA;
if (Rest)
NOccBPrt = NVirBPrt = 0;
//
// Write the [MO] section using a "general" (i.e., user-specified)
// set of MO coefficients.
//
// fp = destination file (opened by the caller). Defaults
// to NULL for stdout.
//
// jCA,jCB = alpha/beta MO coefficients
// jEA,jEB = alpha/beta orbital energies
//
// NOccA,NVirA,NOccB,NVirB
// = no. of occupied/virtual alpha/beta orbitals that are
// contained in the input jC.
//
// NOccAPrt = no. alpha occupieds to output (HOMO, HOMO-1, HOMO-2, ...)
// NVirAPrt = no. alpha virtuals to output (LUMO, LUMO+1, LUMO+2, ...)
// NOccBPrt, NVirBPrt = no. beta occupieds/virtuals to output
//
if (!fp) fp = stdout;
int NBasis = bSetMgr.crntShlsStats(STAT_NBASIS);
int NBas6D = bSetMgr.crntShlsStats(STAT_NBAS6D);
int NAlpha = rem_read(REM_NALPHA);
int NBeta = rem_read(REM_NBETA);
BasisSet basis(DEF_ID);
LOGICAL *pureL = basis.deciPC();
bool pureD = pureL[2],
pureF = pureL[3],
pureG = pureL[4];