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