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