-
Notifications
You must be signed in to change notification settings - Fork 3
/
MP2D.cpp
1236 lines (984 loc) · 46.4 KB
/
MP2D.cpp
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
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include"MP2D.h"
#include<iterator>
#include<unistd.h>
#include<string.h>
#include<iomanip>
#include<algorithm>
#include<functional>
//using namespace std::placeholders;
MP2D::MP2D() : Natoms_per_monomer(NULL), AtomicSymbols(NULL),
AtomicNumbers(NULL), xyz(NULL), symbols(NULL), RcovAB(NULL) // r0AB(NULL), RcovAB(NULL)
{}
void MP2D::Initialize(ifstream &infile, int argc, char *argv[]) {
AngToBohr = 1.8897259886;
HartreeToKcal = 627.5095;
// First read any parameters from the command line
GetUserParameters(argc,argv);
// Now read the default empirical parameters. If the user specified some parameters in the command line,
// these functionals will replace the default values with the user's values.
if (SCSMP2D_U == true) {
GetSCSMP2DParameters(infile);
}
else{
GetMP2DParameters(infile);
}
Ntot = BeforeGetTotalNumberOfAtoms(infile);
GetCoordinates(infile);
*GetAtomicNumber();
*GetMultipoleExpectationValue();
GetCovalentRadii();
GetC6Coefficients();
CoordinationNumber();
CoordinationNumberGradient();
ComputeEnergyandPrintResults();
}
//Destructor
MP2D::~MP2D() {
delete [] Natoms_per_monomer;
delete [] xyz;
}
// This function allows the user to specify parameter values for the calculations, and paths for the coefficients.
void MP2D::GetUserParameters(int argc, char* argv[]) {
// This loop matches the user specified inputs by their first several characters, and sets a few variables based on the matches that are made.
for (int i = 0; i<argc; i++) {
string str = argv[i];
// With this keyword you can set an alternative parameter file instead of using mp2d_parameter.dat
if (str.substr(0,7) == "--param") {
Param_U = true;
string PARAM_U = str.substr(8);
stringstream PARAM(PARAM_U);
PARAM >> Param;
cout << Param << endl;
}
// With this keyword you can set a path to where the UCHF coefficients are located
if (str.substr(0,6) == "--uchf") {
UCHF_U = true;
string Uchf_U = str.substr(7);
stringstream UCHF(Uchf_U);
UCHF >> UCHF_Path;
cout << UCHF_Path << endl;
}
// With this path you can set a path to where the CKS coefficients are located
else if (str.substr(0,5) == "--cks") {
// cout << "yay again";
CKS_U = true;
string Cks_U = str.substr(6);
stringstream CKS(Cks_U);
CKS >> CKS_Path;
//cout << CKS_Path << endl;
}
// This keyword lets you specify an alternative width for the double damping function
else if (str.substr(0,3) == "--w") {
w_U = true;
string W_U = str.substr(4);
stringstream WIDTH(W_U);
Width = 0.0;
WIDTH >> Width;
//cout << Width << endl;
}
// This keyword lets you specify an alternative cutoff radius for the double damping function
else if (str.substr(0,6) == "--rcut") {
rcut_U = true;
string Rcut_U = str.substr(7);
stringstream RCUT(Rcut_U);
Rcut = 0.0;
RCUT >> Rcut;
cout << Rcut << endl;
}
// This keyword lets you specify an alternative a1 parameter for the Tang-Toennies damping function
else if (str.substr(0,7) == "--TT_a1") {
TT_a1_U = true;
string TTa1_U = str.substr(8);
stringstream TTA1(TTa1_U);
TT_a1 = 0.0;
TTA1 >> TT_a1;
}
// This keyword lets you specify an altrernative a2 parameter for the Tang-Toennies damping function
else if (str.substr(0,7) == "--TT_a2") {
TT_a2_U = true;
string TTa2_U = str.substr(8);
stringstream TTA2(TTa2_U);
TT_a2 = 0.0;
TTA2 >> TT_a2;
//cout << TT_a2 << endl;
}
// This keyword lets you specify an alternative s8 parameter which compensates for the lack of higher order terms in the energy expansion
else if (str.substr(0,4) == "--s8") {
s8_U = true;
string S8_U = str.substr(5);
stringstream S8(S8_U);
s8 = 0.0;
S8 >> s8;
//cout << s8 << endl;
}
// This keyword lets you specify an alternative Css same-spin correlation scaling parameter
else if (str.substr(0,5) == "--css") {
Css_U = true;
string tmp_U = str.substr(6);
stringstream tmp(tmp_U);
userCss = 0.0;
tmp >> userCss;
//cout << "User requests Css = " << userCss << endl;
}
// This keyword lets you specify an alternative Cos opposite-spin correlation scaling parameter
else if (str.substr(0,5) == "--cos") {
Cos_U = true;
string tmp_U = str.substr(6);
stringstream tmp(tmp_U);
userCos = 0.0;
tmp >> userCos;
//cout << "User requests Css = " << userCss << endl;
}
// This keyword lets you specify a Hartree-Fock energy for SCS-MP2D
else if (str.substr(0,4) == "--hf") {
hf_U = true;
string hf_U = str.substr(5);
stringstream HF(hf_U);
hf = 0.0;
HF >> hf;
//cout << s8 << endl;
}
// This keyword lets you specify a mp2 os energy for SCS-MP2D
else if (str.substr(0,7) == "--mp2os") {
mp2os_U = true;
string mp2os_U = str.substr(8);
stringstream MP2OS(mp2os_U);
mp2_os = 0.0;
MP2OS >> mp2_os;
//cout << s8 << endl;
}
// This keyword lets you specify a mp2 ss energy for SCS-MP2D
else if (str.substr(0,7) == "--mp2ss") {
mp2ss_U = true;
string mp2ss_U = str.substr(8);
stringstream MP2SS(mp2ss_U);
mp2_ss = 0.0;
MP2SS >> mp2_ss;
//cout << s8 << endl;
}
// Specify that this is a SCS-MP2D calculation
else if (str.substr(0,9) == "--SCSMP2D" || str.substr(0,9) == "--scsmp2d") {
SCSMP2D_U =true;
}
// This parameter lets you specify whether or not you would like to print out the gradient when you run the calculation
else if (str.substr(0,10) == "--gradient") {
Gradient_U =true;
}
else {}
str.clear();
}
// Error handling for keywords
// 1) Only allow user-specified Cos/Css settings for SCS-MP2D
if ( (Cos_U || Css_U ) && !SCSMP2D_U) {
cerr << "ERROR: To use custom Cos / Css spin-component scaling coefficients, you must run an SCS-MP2D calculation." << endl << "To do so, you must specify the --SCSMP2D flags and give values for the --hf, --mp2os, and --mp2ss flags." << endl;
exit(1);
}
// 2) Gradients only for MP2D, not SCS-MP2D
if (SCSMP2D_U && Gradient_U) {
cerr << "ERROR: Gradients are only implemented for MP2D, not for SCS-MP2D." << endl;
exit(1);
}
// 3) General case where a keyword doesn't match a a known keyword.
if (argv[2] && Param_U==false && UCHF_U == false && CKS_U == false && w_U == false && rcut_U == false && TT_a1_U == false && TT_a2_U == false && s8_U == false && Cos_U == false && Css_U == false && hf_U == false && mp2os_U == false && mp2ss_U == false && SCSMP2D_U == false && Gradient_U == false ) {
cerr << "Calculation terminated, the provided input does not match any known keyword. Please refer to README.txt" << endl;
exit(1);
}
}
// Reads empirical MP2D parameters from file
void MP2D::GetSCSMP2DParameters(ifstream& infile) {
ifstream inFile;
string psiP;
// Set the path to the parameter file. The default is mp2d_parameters.dat
if (Param_U == true) {
psiP = Param;
}
// Check if MP2D_PARAM_PATH is set. If it is then set psiP. If not it
// will throw an error down the line.
else if (getenv("MP2D_PARAM_PATH")!=NULL) {
psiP = getenv("MP2D_PARAM_PATH");
}
else {}
if (psiP.empty()) {
cerr << "Calculation terminated, please add an MP2D_PARAM_PATH environment varaible. Refer to README.txt" << endl;
exit (1);
}
string file2 = "/scsmp2d_parameters.dat";
string path = psiP +file2;
inFile.open(path.c_str());
string line;
string aa;
string bb;
double cc;
if (!inFile) {
cerr << "Unable to open file scsmp2d_parameters.dat" << endl;
exit (1);
}
// loop through the parameter file to grab the numerical parameters for the damping functions, and the energy expression. Also, get the paths to the coefficient files.
while(getline(inFile,line)) {
istringstream iss(line);
iss >> aa >> bb >> cc;
if (aa =="a_one") {a_one = cc;}
if (aa == "a_two") {a_two = cc;}
if (aa == "rcut") {rcut = cc;}
if (aa == "width") {width = cc;}
if (aa == "s_8") {s_8 = cc;}
if (aa == "Cos") {Cos = cc;}
if (aa == "Css") {Css = cc;}
if (aa == "CKS_Path:") {GrimmePath = bb;}
if (aa == "UCHF_Path:") {UCHFPath = bb;}
}
inFile.close();
// If appropriate, replace the parameter file values with any specified command-line parameter values.
if (rcut_U == true) {rcut = Rcut;}
if (w_U == true) {width = Width;}
if (TT_a1_U == true) {a_one = TT_a1;}
if (TT_a2_U == true) {a_two = TT_a2;}
if (s8_U == true) {s_8 = s8;}
if (Cos_U == true) {Cos = userCos;}
if (Css_U == true) {Css = userCss;}
// Set HF, same-spin MP2 correlation, and opposite-spin MP2 correlation energies
HFT = hf;
MP2_os = mp2_os;
MP2_ss = mp2_ss;
if ( HFT == 0.0 || MP2_os == 0.0 || MP2_ss == 0.0 ) {
cerr << "ERROR: The HF and MP2 spin component energies must be supplied for SCS-MP2D." << endl << "Use the --hf, --mp2os, and --mp2ss flags to specify the values (in hartrees)." << endl;
exit (1);
}
}
// Reads empirical MP2D parameters from file
void MP2D::GetMP2DParameters(ifstream& infile) {
ifstream inFile;
string psiP;
// Set the path to the parameter file. The default is mp2d_parameters.dat
if (Param_U == true) {
psiP = Param;
}
// Check if MP2D_PARAM_PATH is set. If it is then set psiP. If not it
// will throw an error down the line.
else if (getenv("MP2D_PARAM_PATH")!=NULL) {
psiP = getenv("MP2D_PARAM_PATH");
}
else {}
if (psiP.empty()) {
cerr << "Calculation terminated, please add an MP2D_PARAM_PATH environment varaible. Refer to README.txt" << endl;
exit (1);
}
string file2 = "/mp2d_parameters.dat";
string path = psiP +file2;
inFile.open(path.c_str());
string line;
string aa;
string bb;
double cc;
if (!inFile) {
cerr << "Unable to open file mp2d_parameters.dat" << endl;
exit (1);
}
// loop through the parameter file to grab the numerical parameters for the damping functions, and the energy expression. Also, get the paths to the coefficient files.
while(getline(inFile,line)) {
istringstream iss(line);
iss >> aa >> bb >> cc;
if (aa =="a_one") {a_one = cc;}
if (aa == "a_two") {a_two = cc;}
if (aa == "rcut") {rcut = cc;}
if (aa == "width") {width = cc;}
if (aa == "s_8") {s_8 = cc;}
if (aa == "Cos") {Cos = cc;}
if (aa == "Css") {Css = cc;}
if (aa == "CKS_Path:") {GrimmePath = bb;}
if (aa == "UCHF_Path:") {UCHFPath = bb;}
}
inFile.close();
// If appropriate, replace the parameter file values with any specified command-line parameter values.
if (rcut_U == true) {rcut = Rcut;}
if (w_U == true) {width = Width;}
if (TT_a1_U == true) {a_one = TT_a1;}
if (TT_a2_U == true) {a_two = TT_a2;}
if (s8_U == true) {s_8 = s8;}
}
// Read the total number of atoms from the first line of the XYZ input file
int MP2D::BeforeGetTotalNumberOfAtoms(ifstream &infile) {
Rewind(infile);
infile >> NumberOfAtoms;
//printf("XYZ file contains %d atoms\n",NumberOfAtoms);
return NumberOfAtoms;
}
// Read atomic symbols and Cartesian coordinates
void MP2D::GetCoordinates(ifstream& infile) {
string line;
// Stores the atomic symbols
symbols = new string[Ntot];
// Stores the coordinates of the system
XYZ.resize(3*Ntot);
Rewind(infile);
int atom = 0;
// Skip the line for the number of atoms and the line for comments
getline(infile,line);
getline(infile,line);
// starting from the third line, loop through and store the coordinates
for (int i=0; i< NumberOfAtoms; i++) {
infile >> symbols[atom];
// Convert coordinates to Bohr as they are pulled in
infile >> XYZ[3*atom];
XYZ[3*atom] = XYZ[3*atom]*AngToBohr;
infile >> XYZ[3*atom+1];
XYZ[3*atom+1] = XYZ[3*atom+1]*AngToBohr;
infile >> XYZ[3*atom+2];
XYZ[3*atom+2] = XYZ[3*atom+2]*AngToBohr;
atom++;
}
}
int *MP2D::GetAtomicNumber() {
// atomic numbers of elements that we have UCHF coefficients for
// the index of a given element corresponds to its atomic number
string AtomicSymbols[36] = {
"XX",
"H","XX",
"XX","XX","B","C","N","O","F","Ne", // period 2
"XX","XX","XX","XX","P","S","Cl","Ar", // period 3
"XX","XX","XX","XX","XX","XX","XX","XX","XX","XX","XX","XX", // period 4
"XX","XX","XX","XX","Br"}; // period 4
int i, j, at_num = 0;
atomic_number = new int[Ntot];
// The array atomic_number is filled with the atomic numbers of each
// atom that appears in the input.
int counter = 0;
for (i=0;i<Ntot;i++) {
for (j=0;j<36;j++) {
if (symbols[i]==AtomicSymbols[j]) {
at_num = j;
atomic_number[i] = at_num;
counter++;
}
}
}
if (counter!=Ntot) {
cerr << "Calculation terminated, the input contains atoms that are not currently supported by MP2D. Please refer to README.txt." << endl;
exit(1);
}
// Here the returned argument is a pointer to the array atomic_number[]
return atomic_number;
}
double *MP2D::GetMultipoleExpectationValue() {
double MultipoleExpectationValues[36] = { 0.0, 8.0589, 0.0, 0.0, 0.0, 11.8799, 7.8715, 5.5588, 4.7566, 3.8025
,3.1036, 0.0, 0.0, 0.0, 0.0, 9.5361, 8.1652, 6.7463, 5.6004, 0.0 ,0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 7.1251};
int i, j;
r2_r4 = new double[Ntot];
for (int i=0;i<Ntot;i++) {
r2_r4[i] = MultipoleExpectationValues[atomic_number[i]];
}
return r2_r4;
}
void MP2D::GetCovalentRadii() {
// Covalent radii for H,B,C,N,O,F,Ne,P,S,Cl,Ar, and Br
// Each covalent radii is placed at the index corresponding to the element's
// atomic number
double CovalentRadii[36] = {
0.0,
0.32,0.0,
0.0,0.0,0.77,0.75,0.71,0.63,0.64,0.67, // period 2
0.0,0.0,0.0,0.0,1.1,1.02,0.99,0.96, // period 3
0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0, // period 4
0.0,0.0,0.0,0.0,1.14}; // period 4
double Rcov[Ntot];
for (int i=0; i<Ntot; i++ ) {
Rcov[i] = CovalentRadii[atomic_number[i]];
}
RcovAB = new double*[Ntot];
for(int i=0; i < Ntot; i++) {
RcovAB[i] = new double[Ntot];
}
for (int i=0; i<Ntot; i++) {
for (int j= 0; j<Ntot; j++) {
if (i==j) {
RcovAB[i][j] = 0.0;
}
else {
RcovAB[i][j] = (Rcov[i] + Rcov[j])*AngToBohr;
}
}
}
}
double MP2D::ComputeDistance(double a, double b, double c, double d, double e, double f) {
double R_AB = sqrt( pow(a - b, 2) + pow(c - d, 2) + pow(e - f, 2));
return R_AB;
}
void MP2D::GetC6Coefficients() {
C6_counter = 0;
ifstream inFile2;
string psiP;
if (CKS_U == true) {
psiP = CKS_Path;
}
else {
psiP = GrimmePath;
}
if (psiP.empty()) {
printf("Please set MP2D_PARAM_PATH shell environment variable to point directory containing mp2d_parameters.dat file\n");
}
string path = psiP;
CKSPATH = path;
inFile2.open(path.c_str());
string line2;
if (!inFile2) {
cerr << "unable to open file: " << path << endl;
exit (1);
}
while (getline(inFile2, line2)) {
C6_counter++;
}
inFile2.close();
// define matrix here with dimensions index x 5
matrix_Grimme.resize(C6_counter);
for (int i=0; i<C6_counter; i++) {
matrix_Grimme[i].resize(5);
}
inFile2.open(path.c_str());
string Line2;
if (!inFile2) {
cerr << "unable to open file: " << path << endl;
exit (1);
}
int counter_2 =0;
while (getline(inFile2, line2)) {
istringstream ss(line2);
double atomA, atomB, CN_A, CN_B, C6CKS;
ss >> atomA >> atomB >> CN_A >> CN_B >> C6CKS;
matrix_Grimme[counter_2][0] = atomA;
matrix_Grimme[counter_2][1] = atomB;
matrix_Grimme[counter_2][2] = CN_A;
matrix_Grimme[counter_2][3] = CN_B;
matrix_Grimme[counter_2][4] = C6CKS;
counter_2++;
}
inFile2.close();
// Where the UCHF coefficients are stored
UCHF_counter = 0;
ifstream inFile;
string psiP2;
if (UCHF_U == true) {
psiP2 = UCHF_Path;
}
else {
psiP2 = UCHFPath;
}
if (psiP2.empty()) {
printf("Please set MP2D_PARAM_PATH shell environment variable to point directory containing mp2d_parameters.dat file\n");
}
string path2 = psiP2;
UCHFPATH = path2;
inFile.open(path2.c_str());
string line;
if (!inFile) {
cerr << "Unable to open file: " << path2 << endl;
exit (1);
}
while (getline(inFile, line)) {
UCHF_counter++;
}
inFile.close();
matrix_UCHF.resize(UCHF_counter);
for (int i=0; i<UCHF_counter; i++) {
matrix_UCHF[i].resize(5);
}
inFile.open(path2.c_str());
if (!inFile) {
cerr << "unable to open file: " << path2 << endl;
exit (1);
}
counter_2 =0;
while (getline(inFile, line)) {
istringstream ss(line);
double atomA, atomB, CN_A, CN_B, UCHFCKS;
ss >> atomA >> atomB >> CN_A >> CN_B >> UCHFCKS;
matrix_UCHF[counter_2][0] = atomA;
matrix_UCHF[counter_2][1] = atomB;
matrix_UCHF[counter_2][2] = CN_A;
matrix_UCHF[counter_2][3] = CN_B;
matrix_UCHF[counter_2][4] = UCHFCKS;
counter_2++;
}
inFile.close();
}
double MP2D::Factorial(int n) {
if ( n>1 ) {
return n*Factorial(n-1);
}
else { return 1; }
}
double MP2D::DoubleDamping(double R0ab, double R, int atnumA, int atnumB ) {
double modR = 0.0;
if (R > R0ab*(rcut + width/2.0)) {
modR = R;
}
else if (R < R0ab*(rcut - width/2.0)) {
modR = rcut*R0ab;
}
else {
double r_prime = R0ab*rcut;
double w_prime = R0ab*width;
double x = (R - (r_prime - w_prime/2.0))/w_prime;
double interpolateR = (-2.5*pow(x,8) + 10*pow(x,7) - 14*pow(x,6) + 7*pow(x,5))*w_prime;
modR = rcut*R0ab + interpolateR;
}
return modR;
}
double MP2D::DoubleDampingDeriv(double R0ab, double R, int atnumA, int atnumB ) {
double modR_Deriv = 0.0;
if (R >= R0ab*(rcut + width/2.0)) {
modR_Deriv = 1.0;
}
else if (R <= R0ab*(rcut - width/2.0)) {
modR_Deriv = 0.0;
}
else {
double r_prime = R0ab*rcut;
double w_prime = R0ab*width;
double x = (R - (r_prime - w_prime/2.0))/w_prime;
double interpolateR_Deriv = (-20*pow(x,7) + 70*pow(x,6) - 84*pow(x,5) + 35*pow(x,4));
modR_Deriv = interpolateR_Deriv;
}
return modR_Deriv;
}
double MP2D::TangToennies(int n, double r0ab, double Rab) {
double damp_sum = 0.0;
double S = (r0ab*(a_one/pow(AngToBohr,2)) + a_two/AngToBohr)*Rab;
for (int i = 0; i < n+1; i++) {
damp_sum += pow(S,i)/Factorial(i);
}
return 1 - exp(-1*S)*damp_sum;
}
double MP2D::TangToenniesDamp(int n, double r0ab, double Rab) {
double damp_sum = 0.0;
double damp_sum_deriv = 0.0;
double S = (r0ab*(a_one/pow(AngToBohr,2)) + a_two/AngToBohr)*Rab;
double S_prime = (r0ab*(a_one/pow(AngToBohr,2)) + a_two/AngToBohr);
for (int i = 0; i < n+1; i++) {
damp_sum += pow(S,i)/Factorial(i);
damp_sum_deriv += ((pow(S_prime,i)*pow(Rab,i-1))/Factorial(i))*i;
}
double ttdamp = (S_prime*exp(-1*S)*damp_sum - exp(-1*S)*damp_sum_deriv)*1.8897;
return ttdamp;
}
void MP2D::CoordinationNumber() {
double function = 0.0;
double CN;
Coordination_Number.resize(Ntot);
for (int i=0; i < NumberOfAtoms; i++) {
CN = 0.0;
for (int j=0; j < NumberOfAtoms; j++ ) {
double R_AB = ComputeDistance(XYZ[3*i], XYZ[3*j], XYZ[3*i +1], XYZ[3*j +1], XYZ[3*i +2], XYZ[3*j + 2]);
if (i!=j) {
if (R_AB <= 0.95*RcovAB[i][j]) {
function = 1.0;
}
else if (R_AB >= 1.75*RcovAB[i][j]) {
function = 0.0;
}
else {
double numerator = R_AB - 0.95*RcovAB[i][j];
double xprime = numerator/(1.75*RcovAB[i][j] - 0.95*RcovAB[i][j]);
function = 1.0 - (-20*pow(xprime,7) + 70*pow(xprime, 6) - 84*pow(xprime,5) + 35*pow(xprime,4));
}
}
else {
function = 0.0;
}
CN = CN + function;
}
Coordination_Number[i] = CN;
}
}
void MP2D::CoordinationNumberGradient() {
double function_gradient = 0.0;
CNgrad.resize(Ntot);
dC6_CKSGrad.resize(Ntot);
dC6_UCHFGrad.resize(Ntot);
for (int i=0; i < Ntot; i++ ) {
CNgrad[i].resize(3*Ntot);
dC6_CKSGrad.resize(3*Ntot);
dC6_UCHFGrad.resize(3*Ntot);
}
// Initialize CNgrad with 0.0 elements
for (int i=0; i < NumberOfAtoms; i++) {
for (int j=0; j < NumberOfAtoms; j++ ) {
CNgrad[i][j] = 0.0;
}
}
for (int i=0; i < NumberOfAtoms; i++) {
for (int j=0; j < NumberOfAtoms; j++ ) {
double R_AB = ComputeDistance(XYZ[3*i], XYZ[3*j], XYZ[3*i +1], XYZ[3*j +1], XYZ[3*i +2], XYZ[3*j + 2]);
if (i!=j) {
if (R_AB <= 0.95*RcovAB[i][j]) {
function_gradient = 0.0;
}
else if ( R_AB >= 1.75*RcovAB[i][j]) {
function_gradient = 0.0;
}
else {
double x = (R_AB - 0.95*RcovAB[i][j])/(1.75*RcovAB[i][j] - 0.95*RcovAB[i][j]);
double x_grad = 1.25/RcovAB[i][j];
function_gradient = (140*pow(x,6) - 420*pow(x,5) + 420*pow(x,4) - 140*pow(x,3))*x_grad;
}
double dRdxi = function_gradient*(XYZ[3*i]-XYZ[3*j])/R_AB;
double dRdyi = function_gradient*(XYZ[3*i +1]-XYZ[3*j +1])/R_AB;
double dRdzi = function_gradient*(XYZ[3*i +2]-XYZ[3*j +2])/R_AB;
double dRdxj = -dRdxi;
double dRdyj = -dRdyi;
double dRdzj = -dRdzi;
CNgrad[i][3*i] += dRdxi;
CNgrad[i][3*i+1] += dRdyi;
CNgrad[i][3*i+2] += dRdzi;
CNgrad[i][3*j] += dRdxj;
CNgrad[i][3*j+1] += dRdyj;
CNgrad[i][3*j+2] += dRdzj;
}
}
}
}
double MP2D::ComputeC6_UCHF(int atnumA, int atnumB, double CN_A, double CN_B) {
double C6_UCHF = 0.0;
double sum_L_ij=0.0;
double sum_numerator=0.0;
double sum_C6ref_Lij =0.0;
for( int w=0; w<UCHF_counter-1; w++) {
double atA, atB, cnA, cnB, c6uchf;
atA = matrix_UCHF[w][0];
atB = matrix_UCHF[w][1];
cnA = matrix_UCHF[w][2];
cnB = matrix_UCHF[w][3];
c6uchf = matrix_UCHF[w][4];
if ((atA == atnumA && atB == atnumB) || (atA==atnumB && atB==atnumA)) {
if (atnumA==atnumB && cnA != cnB) {
L_ij = exp(-4*(((CN_A - cnA)*(CN_A - cnA)) + ((CN_B - cnB)*(CN_B - cnB)))) + exp(-4*(((CN_A - cnB)*(CN_A - cnB)) + ((CN_B - cnA)*(CN_B - cnA))));
}
else if (atA ==atnumA && atB ==atnumB) {
L_ij = exp(-4*(((CN_A - cnA)*(CN_A - cnA)) + ((CN_B - cnB)*(CN_B - cnB))));
}
else if (atA==atnumB && atB ==atnumA) {
L_ij = exp(-4*(((CN_A - cnB)*(CN_A - cnB)) + ((CN_B - cnA)*(CN_B - cnA))));
}
else {}
sum_L_ij += L_ij;
sum_C6ref_Lij += L_ij*c6uchf;
double numerator = L_ij*c6uchf;
sum_numerator += numerator;
C6_UCHF = sum_numerator/sum_L_ij;
}
}
return C6_UCHF;
}
double MP2D::ComputeC6_CKS(int atnumA, int atnumB, double CN_A, double CN_B) {
double C6_CKS = 0.0;
double sum_L_ij=0.0;
double sum_numerator=0.0;
double sum_C6ref_Lij =0.0;
for( int w=0; w<C6_counter-1; w++) {
double atA, atB, cnA, cnB, c6uchf;
atA = matrix_Grimme[w][0];
atB = matrix_Grimme[w][1];
cnA = matrix_Grimme[w][2];
cnB = matrix_Grimme[w][3];
c6uchf = matrix_Grimme[w][4];
if ((atA == atnumA && atB == atnumB) || (atA==atnumB && atB==atnumA)) {
// unless the coordination numbers are equal, the contribution needs to be counted twice.
if (atnumA==atnumB && cnA != cnB) {
L_ij = exp(-4*(((CN_A - cnA)*(CN_A - cnA)) + ((CN_B - cnB)*(CN_B - cnB)))) + exp(-4*(((CN_A - cnB)*(CN_A - cnB)) + ((CN_B - cnA)*(CN_B - cnA))));
}
else if (atA ==atnumA && atB ==atnumB) {
L_ij = exp(-4*(((CN_A - cnA)*(CN_A - cnA)) + ((CN_B - cnB)*(CN_B - cnB))));
}
else if (atA==atnumB && atB ==atnumA) {
L_ij = exp(-4*(((CN_A - cnB)*(CN_A - cnB)) + ((CN_B - cnA)*(CN_B - cnA))));
}
else {}
sum_L_ij += L_ij;
sum_C6ref_Lij += L_ij*c6uchf;
double numerator = L_ij*c6uchf;
sum_numerator += numerator;
C6_CKS = sum_numerator/sum_L_ij;
}
}
return C6_CKS;
}
valarray<double> MP2D::ComputeC6_UCHF_Gradient(int atnumA, int atnumB, double CN_A, double CN_B, valarray<double> vectA, valarray<double> vectB) {
double C6_UCHF = 0.0;
double C6_UCHF_Deriv = 0.0;
double sum_L_ij=0.0;
double sum_numerator=0.0;
double sum_C6ref_Lij =0.0;
valarray<double> L_ij_deriv;
L_ij_deriv.resize(3*Ntot);
valarray<double> sum_L_ij_deriv;
sum_L_ij_deriv.resize(3*Ntot);
valarray<double> sum_C6ref_Lij_deriv;
sum_C6ref_Lij_deriv.resize(3*Ntot);
valarray<double> C6_UCHFGrad;
C6_UCHFGrad.resize(3*Ntot);
for( int w=0; w<UCHF_counter-1; w++) {
double atA, atB, cnA, cnB, c6uchf;
atA = matrix_UCHF[w][0];
atB = matrix_UCHF[w][1];
cnA = matrix_UCHF[w][2];
cnB = matrix_UCHF[w][3];
c6uchf = matrix_UCHF[w][4];
if ((atA == atnumA && atB == atnumB) || (atA==atnumB && atB==atnumA)) {
if (atnumA==atnumB && cnA != cnB) {
L_ij = exp(-4*(((CN_A - cnA)*(CN_A - cnA)) + ((CN_B - cnB)*(CN_B - cnB)))) + exp(-4*(((CN_A - cnB)*(CN_A - cnB)) + ((CN_B - cnA)*(CN_B - cnA))));
L_ij_deriv = exp(-4*(((CN_A - cnA)*(CN_A - cnA)) + ((CN_B - cnB)*(CN_B - cnB))))*(-8*(((CN_A-cnA)*vectA)+((CN_B-cnB)*vectB))) + exp(-4*(((CN_A - cnB)*(CN_A - cnB)) + ((CN_B - cnA)*(CN_B - cnA))))*(-8*(((CN_A-cnB)*vectA)+((CN_B-cnA)*vectB)));
}
else if (atA ==atnumA && atB ==atnumB) {
L_ij = exp(-4*(((CN_A - cnA)*(CN_A - cnA)) + ((CN_B - cnB)*(CN_B - cnB))));
L_ij_deriv = exp(-4*(((CN_A - cnA)*(CN_A - cnA)) + ((CN_B - cnB)*(CN_B - cnB))))*(-8*(((CN_A-cnA)*vectA)+((CN_B-cnB)*vectB)));
}
else if (atA==atnumB && atB ==atnumA) {
L_ij = exp(-4*(((CN_A - cnB)*(CN_A - cnB)) + ((CN_B - cnA)*(CN_B - cnA))));
L_ij_deriv = exp(-4*(((CN_A - cnB)*(CN_A - cnB)) + ((CN_B - cnA)*(CN_B - cnA))))*(-8*(((CN_A-cnB)*vectA)+((CN_B-cnA)*vectB)));
}
else {}
sum_L_ij += L_ij;
sum_C6ref_Lij += L_ij*c6uchf;
sum_L_ij_deriv += L_ij_deriv;
sum_C6ref_Lij_deriv += L_ij_deriv*c6uchf;
//for (int i=0; i<Ntot; i++ ) {
//
//}
double numerator = L_ij*c6uchf;
sum_numerator += numerator;
C6_UCHF = sum_numerator/sum_L_ij;
}
}
C6_UCHFGrad = sum_C6ref_Lij_deriv/sum_L_ij - sum_C6ref_Lij*sum_L_ij_deriv/pow(sum_L_ij,2);
return C6_UCHFGrad;
}
valarray<double> MP2D::ComputeC6_CKS_Gradient(int atnumA, int atnumB, double CN_A, double CN_B, valarray<double> vectA, valarray<double> vectB) {
double C6_UCHF = 0.0;
double C6_UCHF_Deriv = 0.0;
double sum_L_ij=0.0;
double sum_numerator=0.0;
double sum_C6ref_Lij =0.0;
valarray<double> L_ij_deriv;
L_ij_deriv.resize(3*Ntot);
valarray<double> sum_L_ij_deriv;
sum_L_ij_deriv.resize(3*Ntot);
valarray<double> sum_C6ref_Lij_deriv;
sum_C6ref_Lij_deriv.resize(3*Ntot);
valarray<double> C6_UCHFGrad;
C6_UCHFGrad.resize(3*Ntot);
for( int w=0; w<C6_counter-1; w++) {
double atA, atB, cnA, cnB, c6uchf;
atA = matrix_Grimme[w][0];
atB = matrix_Grimme[w][1];
cnA = matrix_Grimme[w][2];
cnB = matrix_Grimme[w][3];
c6uchf = matrix_Grimme[w][4];
if ((atA == atnumA && atB == atnumB) || (atA==atnumB && atB==atnumA)) {
if (atnumA==atnumB && cnA != cnB) {
L_ij = exp(-4*(((CN_A - cnA)*(CN_A - cnA)) + ((CN_B - cnB)*(CN_B - cnB)))) + exp(-4*(((CN_A - cnB)*(CN_A - cnB)) + ((CN_B - cnA)*(CN_B - cnA))));
L_ij_deriv = exp(-4*(((CN_A - cnA)*(CN_A - cnA)) + ((CN_B - cnB)*(CN_B - cnB))))*(-8*(((CN_A-cnA)*vectA)+((CN_B-cnB)*vectB))) + exp(-4*(((CN_A - cnB)*(CN_A - cnB)) + ((CN_B - cnA)*(CN_B - cnA))))*(-8*(((CN_A-cnB)*vectA)+((CN_B-cnA)*vectB)));
}
else if (atA ==atnumA && atB ==atnumB) {
L_ij = exp(-4*(((CN_A - cnA)*(CN_A - cnA)) + ((CN_B - cnB)*(CN_B - cnB))));
L_ij_deriv = exp(-4*(((CN_A - cnA)*(CN_A - cnA)) + ((CN_B - cnB)*(CN_B - cnB))))*(-8*(((CN_A-cnA)*vectA)+((CN_B-cnB)*vectB)));
}
else if (atA==atnumB && atB ==atnumA) {
L_ij = exp(-4*(((CN_A - cnB)*(CN_A - cnB)) + ((CN_B - cnA)*(CN_B - cnA))));
L_ij_deriv = exp(-4*(((CN_A - cnB)*(CN_A - cnB)) + ((CN_B - cnA)*(CN_B - cnA))))*(-8*(((CN_A-cnB)*vectA)+((CN_B-cnA)*vectB)));
}
else {}
sum_L_ij += L_ij;
sum_C6ref_Lij += L_ij*c6uchf;
sum_L_ij_deriv += L_ij_deriv;