| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * | 
|---|
| 6 | * | 
|---|
| 7 | *   This file is part of MoleCuilder. | 
|---|
| 8 | * | 
|---|
| 9 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
| 10 | *    it under the terms of the GNU General Public License as published by | 
|---|
| 11 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
| 12 | *    (at your option) any later version. | 
|---|
| 13 | * | 
|---|
| 14 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
| 15 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 17 | *    GNU General Public License for more details. | 
|---|
| 18 | * | 
|---|
| 19 | *    You should have received a copy of the GNU General Public License | 
|---|
| 20 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | /** \file joiner.cpp | 
|---|
| 24 | * | 
|---|
| 25 | * Takes evaluated fragments (energy and forces) and by reading the factors files determines total energy | 
|---|
| 26 | * and each force for the whole molecule. | 
|---|
| 27 | * | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | //============================ INCLUDES =========================== | 
|---|
| 31 |  | 
|---|
| 32 | // include config.h | 
|---|
| 33 | #ifdef HAVE_CONFIG_H | 
|---|
| 34 | #include <config.h> | 
|---|
| 35 | #endif | 
|---|
| 36 |  | 
|---|
| 37 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 38 |  | 
|---|
| 39 | #include <cstring> | 
|---|
| 40 | #include <cmath> | 
|---|
| 41 |  | 
|---|
| 42 | #include "datacreator.hpp" | 
|---|
| 43 | #include "Element/periodentafel.hpp" | 
|---|
| 44 | #include "Fragmentation/defs.hpp" | 
|---|
| 45 | #include "Fragmentation/EnergyMatrix.hpp" | 
|---|
| 46 | #include "Fragmentation/ForceMatrix.hpp" | 
|---|
| 47 | #include "Fragmentation/helpers.hpp" | 
|---|
| 48 | #include "Fragmentation/HessianMatrix.hpp" | 
|---|
| 49 | #include "Fragmentation/KeySetsContainer.hpp" | 
|---|
| 50 | #include "CodePatterns/Log.hpp" | 
|---|
| 51 | #include "CodePatterns/Verbose.hpp" | 
|---|
| 52 |  | 
|---|
| 53 | //============================== MAIN ============================= | 
|---|
| 54 |  | 
|---|
| 55 | int main(int argc, char **argv) | 
|---|
| 56 | { | 
|---|
| 57 | periodentafel *periode = NULL; // and a period table of all elements | 
|---|
| 58 | EnergyMatrix Energy; | 
|---|
| 59 | EnergyMatrix EnergyFragments; | 
|---|
| 60 |  | 
|---|
| 61 | EnergyMatrix Hcorrection; | 
|---|
| 62 | EnergyMatrix HcorrectionFragments; | 
|---|
| 63 |  | 
|---|
| 64 | ForceMatrix Force; | 
|---|
| 65 | ForceMatrix ForceFragments; | 
|---|
| 66 |  | 
|---|
| 67 | HessianMatrix Hessian; | 
|---|
| 68 | HessianMatrix HessianFragments; | 
|---|
| 69 |  | 
|---|
| 70 | ForceMatrix Shielding; | 
|---|
| 71 | ForceMatrix ShieldingPAS; | 
|---|
| 72 | ForceMatrix ShieldingFragments; | 
|---|
| 73 | ForceMatrix ShieldingPASFragments; | 
|---|
| 74 | ForceMatrix Chi; | 
|---|
| 75 | ForceMatrix ChiPAS; | 
|---|
| 76 | ForceMatrix ChiFragments; | 
|---|
| 77 | ForceMatrix ChiPASFragments; | 
|---|
| 78 | KeySetsContainer KeySet; | 
|---|
| 79 | stringstream prefix; | 
|---|
| 80 | char *dir = NULL; | 
|---|
| 81 | bool NoHCorrection = false; | 
|---|
| 82 | bool NoHessian = false; | 
|---|
| 83 |  | 
|---|
| 84 | LOG(0, "Joiner"); | 
|---|
| 85 | LOG(0, "======"); | 
|---|
| 86 |  | 
|---|
| 87 | // Get the command line options | 
|---|
| 88 | if (argc < 3) { | 
|---|
| 89 | LOG(0, "Usage: " << argv[0] << " <inputdir> <prefix> [elementsdb]"); | 
|---|
| 90 | LOG(0, "<inputdir>\ttherein the output of a molecuilder fragmentation is expected, each fragment with a subdir containing an energy.all and a forces.all file."); | 
|---|
| 91 | LOG(0, "<prefix>\tprefix of energy and forces file."); | 
|---|
| 92 | LOG(0, "[elementsdb]\tpath to elements database, needed for shieldings."); | 
|---|
| 93 | return 1; | 
|---|
| 94 | } else { | 
|---|
| 95 | dir = new char[strlen(argv[2]) + 2]; | 
|---|
| 96 | strcpy(dir, "/"); | 
|---|
| 97 | strcat(dir, argv[2]); | 
|---|
| 98 | } | 
|---|
| 99 | if (argc > 3) { | 
|---|
| 100 | periode = new periodentafel; | 
|---|
| 101 | periode->LoadPeriodentafel(argv[3]); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | // Test the given directory | 
|---|
| 105 | if (!TestParams(argc, argv)) | 
|---|
| 106 | return 1; | 
|---|
| 107 |  | 
|---|
| 108 | // +++++++++++++++++ PARSING +++++++++++++++++++++++++++++++ | 
|---|
| 109 |  | 
|---|
| 110 | // ------------- Parse through all Fragment subdirs -------- | 
|---|
| 111 | if (!Energy.ParseFragmentMatrix(argv[1], dir, EnergySuffix, 0,0)) return 1; | 
|---|
| 112 | if (!Hcorrection.ParseFragmentMatrix(argv[1], "", HCORRECTIONSUFFIX, 0,0)) { | 
|---|
| 113 | NoHCorrection = true; | 
|---|
| 114 | LOG(0, "No HCorrection matrices found, skipping these."); | 
|---|
| 115 | } | 
|---|
| 116 | if (!Force.ParseFragmentMatrix(argv[1], dir, ForcesSuffix, 0,0)) return 1; | 
|---|
| 117 | if (!Hessian.ParseFragmentMatrix(argv[1], dir, HessianSuffix, 0,0)) { | 
|---|
| 118 | NoHessian = true; | 
|---|
| 119 | LOG(0, "No hessian matrices found, skipping these."); | 
|---|
| 120 | } | 
|---|
| 121 | if (periode != NULL) { // also look for PAS values | 
|---|
| 122 | if (!Shielding.ParseFragmentMatrix(argv[1], dir, ShieldingSuffix, 1, 0)) return 1; | 
|---|
| 123 | if (!ShieldingPAS.ParseFragmentMatrix(argv[1], dir, ShieldingPASSuffix, 1, 0)) return 1; | 
|---|
| 124 | if (!Chi.ParseFragmentMatrix(argv[1], dir, ChiSuffix, 1, 0)) return 1; | 
|---|
| 125 | if (!ChiPAS.ParseFragmentMatrix(argv[1], dir, ChiPASSuffix, 1, 0)) return 1; | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | // ---------- Parse the TE Factors into an array ----------------- | 
|---|
| 129 | if (!Energy.InitialiseIndices()) return 1; | 
|---|
| 130 | if (!NoHCorrection) | 
|---|
| 131 | Hcorrection.InitialiseIndices(); | 
|---|
| 132 |  | 
|---|
| 133 | // ---------- Parse the Force indices into an array --------------- | 
|---|
| 134 | if (!Force.ParseIndices(argv[1])) return 1; | 
|---|
| 135 |  | 
|---|
| 136 | // ---------- Parse the Hessian (=force) indices into an array --------------- | 
|---|
| 137 | if (!NoHessian) | 
|---|
| 138 | if (!Hessian.InitialiseIndices((class MatrixContainer *)&Force)) return 1; | 
|---|
| 139 |  | 
|---|
| 140 | // ---------- Parse the shielding indices into an array --------------- | 
|---|
| 141 | if (periode != NULL) { // also look for PAS values | 
|---|
| 142 | if(!Shielding.ParseIndices(argv[1])) return 1; | 
|---|
| 143 | if(!ShieldingPAS.ParseIndices(argv[1])) return 1; | 
|---|
| 144 | if(!Chi.ParseIndices(argv[1])) return 1; | 
|---|
| 145 | if(!ChiPAS.ParseIndices(argv[1])) return 1; | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | // ---------- Parse the KeySets into an array --------------- | 
|---|
| 149 | { | 
|---|
| 150 | std::stringstream filename; | 
|---|
| 151 | filename << FRAGMENTPREFIX << KEYSETFILE; | 
|---|
| 152 | if (!KeySet.ParseKeySets(argv[1], filename.str(), Force.MatrixCounter)) return 1; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | if (!KeySet.ParseManyBodyTerms()) return 1; | 
|---|
| 156 |  | 
|---|
| 157 | if (!EnergyFragments.AllocateMatrix(Energy.Header, Energy.MatrixCounter, Energy.RowCounter, Energy.ColumnCounter)) return 1; | 
|---|
| 158 | if (!NoHCorrection) | 
|---|
| 159 | HcorrectionFragments.AllocateMatrix(Hcorrection.Header, Hcorrection.MatrixCounter, Hcorrection.RowCounter, Hcorrection.ColumnCounter); | 
|---|
| 160 | if (!ForceFragments.AllocateMatrix(Force.Header, Force.MatrixCounter, Force.RowCounter, Force.ColumnCounter)) return 1; | 
|---|
| 161 | if (!NoHessian) | 
|---|
| 162 | if (!HessianFragments.AllocateMatrix(Hessian.Header, Hessian.MatrixCounter, Hessian.RowCounter, Hessian.ColumnCounter)) return 1; | 
|---|
| 163 | if (periode != NULL) { // also look for PAS values | 
|---|
| 164 | if (!ShieldingFragments.AllocateMatrix(Shielding.Header, Shielding.MatrixCounter, Shielding.RowCounter, Shielding.ColumnCounter)) return 1; | 
|---|
| 165 | if (!ShieldingPASFragments.AllocateMatrix(ShieldingPAS.Header, ShieldingPAS.MatrixCounter, ShieldingPAS.RowCounter, ShieldingPAS.ColumnCounter)) return 1; | 
|---|
| 166 | if (!ChiFragments.AllocateMatrix(Chi.Header, Chi.MatrixCounter, Chi.RowCounter, Chi.ColumnCounter)) return 1; | 
|---|
| 167 | if (!ChiPASFragments.AllocateMatrix(ChiPAS.Header, ChiPAS.MatrixCounter, ChiPAS.RowCounter, ChiPAS.ColumnCounter)) return 1; | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | // ----------- Resetting last matrices (where full QM values are stored right now) | 
|---|
| 171 | if(!Energy.SetLastMatrix(0., 0)) return 1; | 
|---|
| 172 | if(!Force.SetLastMatrix(0., 2)) return 1; | 
|---|
| 173 | if (!NoHessian) | 
|---|
| 174 | if (!Hessian.SetLastMatrix(0., 0)) return 1; | 
|---|
| 175 | if (periode != NULL) { // also look for PAS values | 
|---|
| 176 | if(!Shielding.SetLastMatrix(0., 2)) return 1; | 
|---|
| 177 | if(!ShieldingPAS.SetLastMatrix(0., 2)) return 1; | 
|---|
| 178 | if(!Chi.SetLastMatrix(0., 2)) return 1; | 
|---|
| 179 | if(!ChiPAS.SetLastMatrix(0., 2)) return 1; | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | // +++++++++++++++++ SUMMING +++++++++++++++++++++++++++++++ | 
|---|
| 183 |  | 
|---|
| 184 | // --------- sum up and write for each order---------------- | 
|---|
| 185 | for (int BondOrder=0;BondOrder<KeySet.Order;BondOrder++) { | 
|---|
| 186 | // --------- sum up energy -------------------- | 
|---|
| 187 | LOG(0, "Summing energy of order " << BondOrder+1 << " ..."); | 
|---|
| 188 | if (!EnergyFragments.SumSubManyBodyTerms(Energy, KeySet, BondOrder)) return 1; | 
|---|
| 189 | if (!NoHCorrection) { | 
|---|
| 190 | HcorrectionFragments.SumSubManyBodyTerms(Hcorrection, KeySet, BondOrder); | 
|---|
| 191 | if (!Energy.SumSubEnergy(EnergyFragments, &HcorrectionFragments, KeySet, BondOrder, 1.)) return 1; | 
|---|
| 192 | Hcorrection.SumSubEnergy(HcorrectionFragments, NULL, KeySet, BondOrder, 1.); | 
|---|
| 193 | } else | 
|---|
| 194 | if (!Energy.SumSubEnergy(EnergyFragments, NULL, KeySet, BondOrder, 1.)) return 1; | 
|---|
| 195 | // --------- sum up Forces -------------------- | 
|---|
| 196 | LOG(0, "Summing forces of order " << BondOrder+1 << " ..."); | 
|---|
| 197 | if (!ForceFragments.SumSubManyBodyTerms(Force, KeySet, BondOrder)) return 1; | 
|---|
| 198 | if (!Force.SumSubForces(ForceFragments, KeySet, BondOrder, 1.)) return 1; | 
|---|
| 199 | // --------- sum up Hessian -------------------- | 
|---|
| 200 | if (!NoHessian) { | 
|---|
| 201 | LOG(0, "Summing Hessian of order " << BondOrder+1 << " ..."); | 
|---|
| 202 | if (!HessianFragments.SumSubManyBodyTerms(Hessian, KeySet, BondOrder)) return 1; | 
|---|
| 203 | if (!Hessian.SumSubHessians(HessianFragments, KeySet, BondOrder, 1.)) return 1; | 
|---|
| 204 | } | 
|---|
| 205 | if (periode != NULL) { // also look for PAS values | 
|---|
| 206 | LOG(0, "Summing shieldings and susceptibilities of order " << BondOrder+1 << " ..."); | 
|---|
| 207 | if (!ShieldingFragments.SumSubManyBodyTerms(Shielding, KeySet, BondOrder)) return 1; | 
|---|
| 208 | if (!Shielding.SumSubForces(ShieldingFragments, KeySet, BondOrder, 1.)) return 1; | 
|---|
| 209 | if (!ShieldingPASFragments.SumSubManyBodyTerms(ShieldingPAS, KeySet, BondOrder)) return 1; | 
|---|
| 210 | if (!ShieldingPAS.SumSubForces(ShieldingPASFragments, KeySet, BondOrder, 1.)) return 1; | 
|---|
| 211 | if (!ChiFragments.SumSubManyBodyTerms(Chi, KeySet, BondOrder)) return 1; | 
|---|
| 212 | if (!Chi.SumSubForces(ChiFragments, KeySet, BondOrder, 1.)) return 1; | 
|---|
| 213 | if (!ChiPASFragments.SumSubManyBodyTerms(ChiPAS, KeySet, BondOrder)) return 1; | 
|---|
| 214 | if (!ChiPAS.SumSubForces(ChiPASFragments, KeySet, BondOrder, 1.)) return 1; | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | // --------- write the energy and forces file -------------------- | 
|---|
| 218 | prefix.str(" "); | 
|---|
| 219 | prefix << dir << OrderSuffix << (BondOrder+1); | 
|---|
| 220 | LOG(0, "Writing files " << argv[1] << prefix.str() << ". ..."); | 
|---|
| 221 | // energy | 
|---|
| 222 | if (!Energy.WriteLastMatrix(argv[1], (prefix.str()).c_str(), EnergySuffix)) return 1; | 
|---|
| 223 | // forces | 
|---|
| 224 | if (!Force.WriteLastMatrix(argv[1], (prefix.str()).c_str(), ForcesSuffix)) return 1; | 
|---|
| 225 | // hessian | 
|---|
| 226 | if (!NoHessian) | 
|---|
| 227 | if (!Hessian.WriteLastMatrix(argv[1], (prefix.str()).c_str(), HessianSuffix)) return 1; | 
|---|
| 228 | // shieldings | 
|---|
| 229 | if (periode != NULL) { // also look for PAS values | 
|---|
| 230 | if (!Shielding.WriteLastMatrix(argv[1], (prefix.str()).c_str(), ShieldingSuffix)) return 1; | 
|---|
| 231 | if (!ShieldingPAS.WriteLastMatrix(argv[1], (prefix.str()).c_str(), ShieldingPASSuffix)) return 1; | 
|---|
| 232 | if (!Chi.WriteLastMatrix(argv[1], (prefix.str()).c_str(), ChiSuffix)) return 1; | 
|---|
| 233 | if (!ChiPAS.WriteLastMatrix(argv[1], (prefix.str()).c_str(), ChiPASSuffix)) return 1; | 
|---|
| 234 | } | 
|---|
| 235 | } | 
|---|
| 236 | // fragments | 
|---|
| 237 | prefix.str(" "); | 
|---|
| 238 | prefix << dir << EnergyFragmentSuffix; | 
|---|
| 239 | if (!EnergyFragments.WriteTotalFragments(argv[1], (prefix.str()).c_str())) return 1; | 
|---|
| 240 | if (!NoHCorrection) { | 
|---|
| 241 | prefix.str(" "); | 
|---|
| 242 | prefix << dir << HcorrectionFragmentSuffix; | 
|---|
| 243 | if (!HcorrectionFragments.WriteTotalFragments(argv[1], (prefix.str()).c_str())) return 1; | 
|---|
| 244 | } | 
|---|
| 245 | prefix.str(" "); | 
|---|
| 246 | prefix << dir << ForceFragmentSuffix; | 
|---|
| 247 | if (!ForceFragments.WriteTotalFragments(argv[1], (prefix.str()).c_str())) return 1; | 
|---|
| 248 | { | 
|---|
| 249 | std::string fileprefix(FRAGMENTPREFIX); | 
|---|
| 250 | fileprefix += ENERGYPERFRAGMENT; | 
|---|
| 251 | if (!CreateDataFragment(EnergyFragments, KeySet, argv[1], fileprefix.c_str(), "fragment energy versus the Fragment No", "today", CreateEnergy)) return 1; | 
|---|
| 252 | } | 
|---|
| 253 | if (!NoHessian) { | 
|---|
| 254 | prefix.str(" "); | 
|---|
| 255 | prefix << dir << HessianFragmentSuffix; | 
|---|
| 256 | if (!HessianFragments.WriteTotalFragments(argv[1], (prefix.str()).c_str())) return 1; | 
|---|
| 257 | } | 
|---|
| 258 | if (periode != NULL) { // also look for PAS values | 
|---|
| 259 | prefix.str(" "); | 
|---|
| 260 | prefix << dir << ShieldingFragmentSuffix; | 
|---|
| 261 | if (!ShieldingFragments.WriteTotalFragments(argv[1], (prefix.str()).c_str())) return 1; | 
|---|
| 262 | prefix.str(" "); | 
|---|
| 263 | prefix << dir << ShieldingPASFragmentSuffix; | 
|---|
| 264 | if (!ShieldingPASFragments.WriteTotalFragments(argv[1], (prefix.str()).c_str())) return 1; | 
|---|
| 265 | prefix.str(" "); | 
|---|
| 266 | prefix << dir << ChiFragmentSuffix; | 
|---|
| 267 | if (!ChiFragments.WriteTotalFragments(argv[1], (prefix.str()).c_str())) return 1; | 
|---|
| 268 | prefix.str(" "); | 
|---|
| 269 | prefix << dir << ChiPASFragmentSuffix; | 
|---|
| 270 | if (!ChiPASFragments.WriteTotalFragments(argv[1], (prefix.str()).c_str())) return 1; | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | // write last matrices as fragments into central dir (not subdir as above), for analyzer to know index bounds | 
|---|
| 274 | if (!Energy.WriteLastMatrix(argv[1], dir, EnergyFragmentSuffix)) return 1; | 
|---|
| 275 | if (!NoHCorrection) Hcorrection.WriteLastMatrix(argv[1], dir, HcorrectionFragmentSuffix); | 
|---|
| 276 | if (!Force.WriteLastMatrix(argv[1], dir, ForceFragmentSuffix)) return 1; | 
|---|
| 277 | if (!NoHessian) | 
|---|
| 278 | if (!Hessian.WriteLastMatrix(argv[1], dir, HessianFragmentSuffix)) return 1; | 
|---|
| 279 | if (periode != NULL) { // also look for PAS values | 
|---|
| 280 | if (!Shielding.WriteLastMatrix(argv[1], dir, ShieldingFragmentSuffix)) return 1; | 
|---|
| 281 | if (!ShieldingPAS.WriteLastMatrix(argv[1], dir, ShieldingPASFragmentSuffix)) return 1; | 
|---|
| 282 | if (!Chi.WriteLastMatrix(argv[1], dir, ChiFragmentSuffix)) return 1; | 
|---|
| 283 | if (!ChiPAS.WriteLastMatrix(argv[1], dir, ChiPASFragmentSuffix)) return 1; | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | // exit | 
|---|
| 287 | delete(periode); | 
|---|
| 288 | delete[](dir); | 
|---|
| 289 | LOG(0, "done."); | 
|---|
| 290 | return 0; | 
|---|
| 291 | }; | 
|---|
| 292 |  | 
|---|
| 293 | //============================ END =========================== | 
|---|