| [14de469] | 1 | /** \file joiner.cpp | 
|---|
|  | 2 | * | 
|---|
|  | 3 | * Takes evaluated fragments (energy and forces) and by reading the factors files determines total energy | 
|---|
| [6ac7ee] | 4 | * and each force for the whole molecule. | 
|---|
|  | 5 | * | 
|---|
| [14de469] | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | //============================ INCLUDES =========================== | 
|---|
|  | 9 |  | 
|---|
| [6ac7ee] | 10 | #include "datacreator.hpp" | 
|---|
|  | 11 | #include "helpers.hpp" | 
|---|
|  | 12 | #include "parser.hpp" | 
|---|
|  | 13 | #include "periodentafel.hpp" | 
|---|
| [14de469] | 14 |  | 
|---|
|  | 15 | //============================== MAIN ============================= | 
|---|
|  | 16 |  | 
|---|
|  | 17 | int main(int argc, char **argv) | 
|---|
|  | 18 | { | 
|---|
| [042f82] | 19 | periodentafel *periode = NULL; // and a period table of all elements | 
|---|
|  | 20 | EnergyMatrix Energy; | 
|---|
|  | 21 | EnergyMatrix EnergyFragments; | 
|---|
| [85d278] | 22 |  | 
|---|
|  | 23 | EnergyMatrix Hcorrection; | 
|---|
| [042f82] | 24 | EnergyMatrix HcorrectionFragments; | 
|---|
| [85d278] | 25 |  | 
|---|
|  | 26 | ForceMatrix Force; | 
|---|
| [042f82] | 27 | ForceMatrix ForceFragments; | 
|---|
| [85d278] | 28 |  | 
|---|
|  | 29 | HessianMatrix Hessian; | 
|---|
|  | 30 | HessianMatrix HessianFragments; | 
|---|
|  | 31 |  | 
|---|
| [042f82] | 32 | ForceMatrix Shielding; | 
|---|
|  | 33 | ForceMatrix ShieldingPAS; | 
|---|
|  | 34 | ForceMatrix ShieldingFragments; | 
|---|
|  | 35 | ForceMatrix ShieldingPASFragments; | 
|---|
| [71e7c7] | 36 | ForceMatrix Chi; | 
|---|
|  | 37 | ForceMatrix ChiPAS; | 
|---|
|  | 38 | ForceMatrix ChiFragments; | 
|---|
|  | 39 | ForceMatrix ChiPASFragments; | 
|---|
| [14de469] | 40 | KeySetsContainer KeySet; | 
|---|
| [042f82] | 41 | stringstream prefix; | 
|---|
|  | 42 | char *dir = NULL; | 
|---|
| [b12a35] | 43 | bool NoHCorrection = false; | 
|---|
| [85d278] | 44 | bool NoHessian = false; | 
|---|
| [042f82] | 45 |  | 
|---|
|  | 46 | cout << "Joiner" << endl; | 
|---|
|  | 47 | cout << "======" << endl; | 
|---|
|  | 48 |  | 
|---|
|  | 49 | // Get the command line options | 
|---|
|  | 50 | if (argc < 3) { | 
|---|
|  | 51 | cout << "Usage: " << argv[0] << " <inputdir> <prefix> [elementsdb]" << endl; | 
|---|
|  | 52 | cout << "<inputdir>\ttherein the output of a molecuilder fragmentation is expected, each fragment with a subdir containing an energy.all and a forces.all file." << endl; | 
|---|
|  | 53 | cout << "<prefix>\tprefix of energy and forces file." << endl; | 
|---|
|  | 54 | cout << "[elementsdb]\tpath to elements database, needed for shieldings." << endl; | 
|---|
|  | 55 | return 1; | 
|---|
|  | 56 | } else { | 
|---|
|  | 57 | dir = (char *) Malloc(sizeof(char)*(strlen(argv[2])+2), "main: *dir"); | 
|---|
|  | 58 | strcpy(dir, "/"); | 
|---|
|  | 59 | strcat(dir, argv[2]); | 
|---|
|  | 60 | } | 
|---|
|  | 61 | if (argc > 3) { | 
|---|
|  | 62 | periode = new periodentafel; | 
|---|
|  | 63 | periode->LoadPeriodentafel(argv[3]); | 
|---|
|  | 64 | } | 
|---|
|  | 65 |  | 
|---|
|  | 66 | // Test the given directory | 
|---|
|  | 67 | if (!TestParams(argc, argv)) | 
|---|
|  | 68 | return 1; | 
|---|
|  | 69 |  | 
|---|
|  | 70 | // +++++++++++++++++ PARSING +++++++++++++++++++++++++++++++ | 
|---|
|  | 71 |  | 
|---|
|  | 72 | // ------------- Parse through all Fragment subdirs -------- | 
|---|
|  | 73 | if (!Energy.ParseFragmentMatrix(argv[1], dir, EnergySuffix, 0,0)) return 1; | 
|---|
| [b12a35] | 74 | if (!Hcorrection.ParseFragmentMatrix(argv[1], "", HCORRECTIONSUFFIX, 0,0)) { | 
|---|
|  | 75 | NoHCorrection = true; | 
|---|
|  | 76 | cout << "No HCorrection matrices found, skipping these." << endl; | 
|---|
|  | 77 | } | 
|---|
| [042f82] | 78 | if (!Force.ParseFragmentMatrix(argv[1], dir, ForcesSuffix, 0,0)) return 1; | 
|---|
| [85d278] | 79 | if (!Hessian.ParseFragmentMatrix(argv[1], dir, HessianSuffix, 0,0)) { | 
|---|
|  | 80 | NoHessian = true; | 
|---|
| [b12a35] | 81 | cout << "No hessian matrices found, skipping these." << endl; | 
|---|
| [85d278] | 82 | } | 
|---|
| [042f82] | 83 | if (periode != NULL) { // also look for PAS values | 
|---|
|  | 84 | if (!Shielding.ParseFragmentMatrix(argv[1], dir, ShieldingSuffix, 1, 0)) return 1; | 
|---|
|  | 85 | if (!ShieldingPAS.ParseFragmentMatrix(argv[1], dir, ShieldingPASSuffix, 1, 0)) return 1; | 
|---|
| [674220] | 86 | if (!Chi.ParseFragmentMatrix(argv[1], dir, ChiSuffix, 1, 0)) return 1; | 
|---|
|  | 87 | if (!ChiPAS.ParseFragmentMatrix(argv[1], dir, ChiPASSuffix, 1, 0)) return 1; | 
|---|
| [042f82] | 88 | } | 
|---|
|  | 89 |  | 
|---|
|  | 90 | // ---------- Parse the TE Factors into an array ----------------- | 
|---|
| [f731ae] | 91 | if (!Energy.InitialiseIndices()) return 1; | 
|---|
| [b12a35] | 92 | if (!NoHCorrection) | 
|---|
|  | 93 | Hcorrection.InitialiseIndices(); | 
|---|
| [14de469] | 94 |  | 
|---|
| [042f82] | 95 | // ---------- Parse the Force indices into an array --------------- | 
|---|
|  | 96 | if (!Force.ParseIndices(argv[1])) return 1; | 
|---|
|  | 97 |  | 
|---|
| [85d278] | 98 | // ---------- Parse the Hessian (=force) indices into an array --------------- | 
|---|
| [b12a35] | 99 | if (!NoHessian) | 
|---|
|  | 100 | if (!Hessian.InitialiseIndices((class MatrixContainer *)&Force)) return 1; | 
|---|
| [85d278] | 101 |  | 
|---|
| [042f82] | 102 | // ---------- Parse the shielding indices into an array --------------- | 
|---|
|  | 103 | if (periode != NULL) { // also look for PAS values | 
|---|
|  | 104 | if(!Shielding.ParseIndices(argv[1])) return 1; | 
|---|
|  | 105 | if(!ShieldingPAS.ParseIndices(argv[1])) return 1; | 
|---|
| [234af2] | 106 | if(!Chi.ParseIndices(argv[1])) return 1; | 
|---|
|  | 107 | if(!ChiPAS.ParseIndices(argv[1])) return 1; | 
|---|
| [042f82] | 108 | } | 
|---|
|  | 109 |  | 
|---|
|  | 110 | // ---------- Parse the KeySets into an array --------------- | 
|---|
|  | 111 | if (!KeySet.ParseKeySets(argv[1], Force.RowCounter, Force.MatrixCounter)) return 1; | 
|---|
|  | 112 |  | 
|---|
|  | 113 | if (!KeySet.ParseManyBodyTerms()) return 1; | 
|---|
| [674220] | 114 |  | 
|---|
| [042f82] | 115 | if (!EnergyFragments.AllocateMatrix(Energy.Header, Energy.MatrixCounter, Energy.RowCounter, Energy.ColumnCounter)) return 1; | 
|---|
| [b12a35] | 116 | if (!NoHCorrection) | 
|---|
|  | 117 | HcorrectionFragments.AllocateMatrix(Hcorrection.Header, Hcorrection.MatrixCounter, Hcorrection.RowCounter, Hcorrection.ColumnCounter); | 
|---|
| [042f82] | 118 | if (!ForceFragments.AllocateMatrix(Force.Header, Force.MatrixCounter, Force.RowCounter, Force.ColumnCounter)) return 1; | 
|---|
| [85d278] | 119 | if (!NoHessian) | 
|---|
|  | 120 | if (!HessianFragments.AllocateMatrix(Hessian.Header, Hessian.MatrixCounter, Hessian.RowCounter, Hessian.ColumnCounter)) return 1; | 
|---|
| [042f82] | 121 | if (periode != NULL) { // also look for PAS values | 
|---|
|  | 122 | if (!ShieldingFragments.AllocateMatrix(Shielding.Header, Shielding.MatrixCounter, Shielding.RowCounter, Shielding.ColumnCounter)) return 1; | 
|---|
|  | 123 | if (!ShieldingPASFragments.AllocateMatrix(ShieldingPAS.Header, ShieldingPAS.MatrixCounter, ShieldingPAS.RowCounter, ShieldingPAS.ColumnCounter)) return 1; | 
|---|
| [674220] | 124 | if (!ChiFragments.AllocateMatrix(Chi.Header, Chi.MatrixCounter, Chi.RowCounter, Chi.ColumnCounter)) return 1; | 
|---|
|  | 125 | if (!ChiPASFragments.AllocateMatrix(ChiPAS.Header, ChiPAS.MatrixCounter, ChiPAS.RowCounter, ChiPAS.ColumnCounter)) return 1; | 
|---|
| [042f82] | 126 | } | 
|---|
|  | 127 |  | 
|---|
|  | 128 | // ----------- Resetting last matrices (where full QM values are stored right now) | 
|---|
|  | 129 | if(!Energy.SetLastMatrix(0., 0)) return 1; | 
|---|
|  | 130 | if(!Force.SetLastMatrix(0., 2)) return 1; | 
|---|
| [85d278] | 131 | if (!NoHessian) | 
|---|
|  | 132 | if (!Hessian.SetLastMatrix(0., 0)) return 1; | 
|---|
| [042f82] | 133 | if (periode != NULL) { // also look for PAS values | 
|---|
|  | 134 | if(!Shielding.SetLastMatrix(0., 2)) return 1; | 
|---|
|  | 135 | if(!ShieldingPAS.SetLastMatrix(0., 2)) return 1; | 
|---|
| [674220] | 136 | if(!Chi.SetLastMatrix(0., 2)) return 1; | 
|---|
|  | 137 | if(!ChiPAS.SetLastMatrix(0., 2)) return 1; | 
|---|
| [042f82] | 138 | } | 
|---|
|  | 139 |  | 
|---|
|  | 140 | // +++++++++++++++++ SUMMING +++++++++++++++++++++++++++++++ | 
|---|
|  | 141 |  | 
|---|
|  | 142 | // --------- sum up and write for each order---------------- | 
|---|
|  | 143 | for (int BondOrder=0;BondOrder<KeySet.Order;BondOrder++) { | 
|---|
|  | 144 | // --------- sum up energy -------------------- | 
|---|
|  | 145 | cout << "Summing energy of order " << BondOrder+1 << " ..." << endl; | 
|---|
|  | 146 | if (!EnergyFragments.SumSubManyBodyTerms(Energy, KeySet, BondOrder)) return 1; | 
|---|
| [b12a35] | 147 | if (!NoHCorrection) { | 
|---|
| [042f82] | 148 | HcorrectionFragments.SumSubManyBodyTerms(Hcorrection, KeySet, BondOrder); | 
|---|
|  | 149 | if (!Energy.SumSubEnergy(EnergyFragments, &HcorrectionFragments, KeySet, BondOrder, 1.)) return 1; | 
|---|
| [b12a35] | 150 | Hcorrection.SumSubEnergy(HcorrectionFragments, NULL, KeySet, BondOrder, 1.); | 
|---|
| [390248] | 151 | } else | 
|---|
| [042f82] | 152 | if (!Energy.SumSubEnergy(EnergyFragments, NULL, KeySet, BondOrder, 1.)) return 1; | 
|---|
|  | 153 | // --------- sum up Forces -------------------- | 
|---|
|  | 154 | cout << "Summing forces of order " << BondOrder+1 << " ..." << endl; | 
|---|
|  | 155 | if (!ForceFragments.SumSubManyBodyTerms(Force, KeySet, BondOrder)) return 1; | 
|---|
|  | 156 | if (!Force.SumSubForces(ForceFragments, KeySet, BondOrder, 1.)) return 1; | 
|---|
| [85d278] | 157 | // --------- sum up Hessian -------------------- | 
|---|
|  | 158 | if (!NoHessian) { | 
|---|
|  | 159 | cout << "Summing Hessian of order " << BondOrder+1 << " ..." << endl; | 
|---|
|  | 160 | if (!HessianFragments.SumSubManyBodyTerms(Hessian, KeySet, BondOrder)) return 1; | 
|---|
|  | 161 | if (!Hessian.SumSubHessians(HessianFragments, KeySet, BondOrder, 1.)) return 1; | 
|---|
|  | 162 | } | 
|---|
| [042f82] | 163 | if (periode != NULL) { // also look for PAS values | 
|---|
| [674220] | 164 | cout << "Summing shieldings and susceptibilities of order " << BondOrder+1 << " ..." << endl; | 
|---|
| [042f82] | 165 | if (!ShieldingFragments.SumSubManyBodyTerms(Shielding, KeySet, BondOrder)) return 1; | 
|---|
|  | 166 | if (!Shielding.SumSubForces(ShieldingFragments, KeySet, BondOrder, 1.)) return 1; | 
|---|
|  | 167 | if (!ShieldingPASFragments.SumSubManyBodyTerms(ShieldingPAS, KeySet, BondOrder)) return 1; | 
|---|
|  | 168 | if (!ShieldingPAS.SumSubForces(ShieldingPASFragments, KeySet, BondOrder, 1.)) return 1; | 
|---|
| [674220] | 169 | if (!ChiFragments.SumSubManyBodyTerms(Chi, KeySet, BondOrder)) return 1; | 
|---|
| [234af2] | 170 | if (!Chi.SumSubForces(ChiFragments, KeySet, BondOrder, 1.)) return 1; | 
|---|
| [674220] | 171 | if (!ChiPASFragments.SumSubManyBodyTerms(ChiPAS, KeySet, BondOrder)) return 1; | 
|---|
| [234af2] | 172 | if (!ChiPAS.SumSubForces(ChiPASFragments, KeySet, BondOrder, 1.)) return 1; | 
|---|
| [042f82] | 173 | } | 
|---|
|  | 174 |  | 
|---|
|  | 175 | // --------- write the energy and forces file -------------------- | 
|---|
|  | 176 | prefix.str(" "); | 
|---|
|  | 177 | prefix << dir << OrderSuffix << (BondOrder+1); | 
|---|
|  | 178 | cout << "Writing files " << argv[1] << prefix.str() << ". ..." << endl; | 
|---|
|  | 179 | // energy | 
|---|
|  | 180 | if (!Energy.WriteLastMatrix(argv[1], (prefix.str()).c_str(), EnergySuffix)) return 1; | 
|---|
|  | 181 | // forces | 
|---|
|  | 182 | if (!Force.WriteLastMatrix(argv[1], (prefix.str()).c_str(), ForcesSuffix)) return 1; | 
|---|
| [85d278] | 183 | // hessian | 
|---|
| [b12a35] | 184 | if (!NoHessian) | 
|---|
|  | 185 | if (!Hessian.WriteLastMatrix(argv[1], (prefix.str()).c_str(), HessianSuffix)) return 1; | 
|---|
| [042f82] | 186 | // shieldings | 
|---|
|  | 187 | if (periode != NULL) { // also look for PAS values | 
|---|
|  | 188 | if (!Shielding.WriteLastMatrix(argv[1], (prefix.str()).c_str(), ShieldingSuffix)) return 1; | 
|---|
|  | 189 | if (!ShieldingPAS.WriteLastMatrix(argv[1], (prefix.str()).c_str(), ShieldingPASSuffix)) return 1; | 
|---|
| [674220] | 190 | if (!Chi.WriteLastMatrix(argv[1], (prefix.str()).c_str(), ChiSuffix)) return 1; | 
|---|
|  | 191 | if (!ChiPAS.WriteLastMatrix(argv[1], (prefix.str()).c_str(), ChiPASSuffix)) return 1; | 
|---|
| [042f82] | 192 | } | 
|---|
|  | 193 | } | 
|---|
|  | 194 | // fragments | 
|---|
|  | 195 | prefix.str(" "); | 
|---|
|  | 196 | prefix << dir << EnergyFragmentSuffix; | 
|---|
|  | 197 | if (!EnergyFragments.WriteTotalFragments(argv[1], (prefix.str()).c_str())) return 1; | 
|---|
| [b12a35] | 198 | if (!NoHCorrection) { | 
|---|
| [042f82] | 199 | prefix.str(" "); | 
|---|
|  | 200 | prefix << dir << HcorrectionFragmentSuffix; | 
|---|
|  | 201 | if (!HcorrectionFragments.WriteTotalFragments(argv[1], (prefix.str()).c_str())) return 1; | 
|---|
|  | 202 | } | 
|---|
|  | 203 | prefix.str(" "); | 
|---|
|  | 204 | prefix << dir << ForceFragmentSuffix; | 
|---|
|  | 205 | if (!ForceFragments.WriteTotalFragments(argv[1], (prefix.str()).c_str())) return 1; | 
|---|
|  | 206 | if (!CreateDataFragment(EnergyFragments, KeySet, argv[1], FRAGMENTPREFIX ENERGYPERFRAGMENT, "fragment energy versus the Fragment No", "today", CreateEnergy)) return 1; | 
|---|
| [85d278] | 207 | if (!NoHessian) { | 
|---|
|  | 208 | prefix.str(" "); | 
|---|
|  | 209 | prefix << dir << HessianFragmentSuffix; | 
|---|
|  | 210 | if (!HessianFragments.WriteTotalFragments(argv[1], (prefix.str()).c_str())) return 1; | 
|---|
|  | 211 | } | 
|---|
| [042f82] | 212 | if (periode != NULL) { // also look for PAS values | 
|---|
|  | 213 | prefix.str(" "); | 
|---|
|  | 214 | prefix << dir << ShieldingFragmentSuffix; | 
|---|
|  | 215 | if (!ShieldingFragments.WriteTotalFragments(argv[1], (prefix.str()).c_str())) return 1; | 
|---|
|  | 216 | prefix.str(" "); | 
|---|
|  | 217 | prefix << dir << ShieldingPASFragmentSuffix; | 
|---|
|  | 218 | if (!ShieldingPASFragments.WriteTotalFragments(argv[1], (prefix.str()).c_str())) return 1; | 
|---|
| [674220] | 219 | prefix.str(" "); | 
|---|
|  | 220 | prefix << dir << ChiFragmentSuffix; | 
|---|
|  | 221 | if (!ChiFragments.WriteTotalFragments(argv[1], (prefix.str()).c_str())) return 1; | 
|---|
|  | 222 | prefix.str(" "); | 
|---|
|  | 223 | prefix << dir << ChiPASFragmentSuffix; | 
|---|
|  | 224 | if (!ChiPASFragments.WriteTotalFragments(argv[1], (prefix.str()).c_str())) return 1; | 
|---|
| [042f82] | 225 | } | 
|---|
|  | 226 |  | 
|---|
|  | 227 | // write last matrices as fragments into central dir (not subdir as above), for analyzer to know index bounds | 
|---|
|  | 228 | if (!Energy.WriteLastMatrix(argv[1], dir, EnergyFragmentSuffix)) return 1; | 
|---|
| [b12a35] | 229 | if (!NoHCorrection) Hcorrection.WriteLastMatrix(argv[1], dir, HcorrectionFragmentSuffix); | 
|---|
| [042f82] | 230 | if (!Force.WriteLastMatrix(argv[1], dir, ForceFragmentSuffix)) return 1; | 
|---|
| [85d278] | 231 | if (!NoHessian) | 
|---|
|  | 232 | if (!Hessian.WriteLastMatrix(argv[1], dir, HessianFragmentSuffix)) return 1; | 
|---|
| [042f82] | 233 | if (periode != NULL) { // also look for PAS values | 
|---|
|  | 234 | if (!Shielding.WriteLastMatrix(argv[1], dir, ShieldingFragmentSuffix)) return 1; | 
|---|
|  | 235 | if (!ShieldingPAS.WriteLastMatrix(argv[1], dir, ShieldingPASFragmentSuffix)) return 1; | 
|---|
| [674220] | 236 | if (!Chi.WriteLastMatrix(argv[1], dir, ChiFragmentSuffix)) return 1; | 
|---|
|  | 237 | if (!ChiPAS.WriteLastMatrix(argv[1], dir, ChiPASFragmentSuffix)) return 1; | 
|---|
| [042f82] | 238 | } | 
|---|
|  | 239 |  | 
|---|
|  | 240 | // exit | 
|---|
|  | 241 | delete(periode); | 
|---|
|  | 242 | Free((void **)&dir, "main: *dir"); | 
|---|
|  | 243 | cout << "done." << endl; | 
|---|
|  | 244 | return 0; | 
|---|
| [14de469] | 245 | }; | 
|---|
|  | 246 |  | 
|---|
|  | 247 | //============================ END =========================== | 
|---|