1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /** \file parsing.cpp
|
---|
9 | *
|
---|
10 | * Declarations of various class functions for the parsing of value files.
|
---|
11 | *
|
---|
12 | */
|
---|
13 |
|
---|
14 | // ======================================= INCLUDES ==========================================
|
---|
15 |
|
---|
16 | // include config.h
|
---|
17 | #ifdef HAVE_CONFIG_H
|
---|
18 | #include <config.h>
|
---|
19 | #endif
|
---|
20 |
|
---|
21 | #include "CodePatterns/MemDebug.hpp"
|
---|
22 |
|
---|
23 | #include <cstring>
|
---|
24 |
|
---|
25 | #include "Helpers/helpers.hpp"
|
---|
26 | #include "parser.hpp"
|
---|
27 | #include "CodePatterns/Verbose.hpp"
|
---|
28 |
|
---|
29 | // ======================================= FUNCTIONS ==========================================
|
---|
30 |
|
---|
31 | /** Test if given filename can be opened.
|
---|
32 | * \param filename name of file
|
---|
33 | * \param test true - no error message, false - print error
|
---|
34 | * \return given file exists
|
---|
35 | */
|
---|
36 | bool FilePresent(const char *filename, bool test)
|
---|
37 | {
|
---|
38 | ifstream input;
|
---|
39 |
|
---|
40 | input.open(filename, ios::in);
|
---|
41 | if (input == NULL) {
|
---|
42 | if (!test)
|
---|
43 | DoLog(0) && (Log() << Verbose(0) << endl << "FilePresent: Unable to open " << filename << ", is the directory correct?" << endl);
|
---|
44 | return false;
|
---|
45 | }
|
---|
46 | input.close();
|
---|
47 | return true;
|
---|
48 | };
|
---|
49 |
|
---|
50 | /** Test the given parameters.
|
---|
51 | * \param argc argument count
|
---|
52 | * \param **argv arguments array
|
---|
53 | * \return given inputdir is valid
|
---|
54 | */
|
---|
55 | bool TestParams(int argc, char **argv)
|
---|
56 | {
|
---|
57 | ifstream input;
|
---|
58 | stringstream line;
|
---|
59 |
|
---|
60 | line << argv[1] << FRAGMENTPREFIX << KEYSETFILE;
|
---|
61 | return FilePresent(line.str().c_str(), false);
|
---|
62 | };
|
---|
63 |
|
---|
64 | // ======================================= CLASS MatrixContainer =============================
|
---|
65 |
|
---|
66 | /** Constructor of MatrixContainer class.
|
---|
67 | */
|
---|
68 | MatrixContainer::MatrixContainer() :
|
---|
69 | Indices(NULL)
|
---|
70 | {
|
---|
71 | Header = new char*[1];
|
---|
72 | Matrix = new double**[1]; // one more each for the total molecule
|
---|
73 | RowCounter = new int[1];
|
---|
74 | ColumnCounter = new int[1];
|
---|
75 | Header[0] = NULL;
|
---|
76 | Matrix[0] = NULL;
|
---|
77 | RowCounter[0] = -1;
|
---|
78 | MatrixCounter = 0;
|
---|
79 | ColumnCounter[0] = -1;
|
---|
80 | };
|
---|
81 |
|
---|
82 | /** Destructor of MatrixContainer class.
|
---|
83 | */
|
---|
84 | MatrixContainer::~MatrixContainer() {
|
---|
85 | if (Matrix != NULL) {
|
---|
86 | // free Matrix[MatrixCounter]
|
---|
87 | if ((ColumnCounter != NULL) && (RowCounter != NULL) && (Matrix[MatrixCounter] != NULL))
|
---|
88 | for(int j=RowCounter[MatrixCounter]+1;j--;)
|
---|
89 | delete[](Matrix[MatrixCounter][j]);
|
---|
90 | //if (MatrixCounter != 0)
|
---|
91 | delete[](Matrix[MatrixCounter]);
|
---|
92 | // free all matrices but ultimate one
|
---|
93 | for(int i=MatrixCounter;i--;) {
|
---|
94 | if ((ColumnCounter != NULL) && (RowCounter != NULL)) {
|
---|
95 | for(int j=RowCounter[i]+1;j--;)
|
---|
96 | delete[](Matrix[i][j]);
|
---|
97 | delete[](Matrix[i]);
|
---|
98 | }
|
---|
99 | }
|
---|
100 | delete[](Matrix);
|
---|
101 | }
|
---|
102 | // free indices
|
---|
103 | if (Indices != NULL)
|
---|
104 | for(int i=MatrixCounter+1;i--;) {
|
---|
105 | delete[](Indices[i]);
|
---|
106 | }
|
---|
107 | delete[](Indices);
|
---|
108 |
|
---|
109 | // free header and counters
|
---|
110 | if (Header != NULL)
|
---|
111 | for(int i=MatrixCounter+1;i--;)
|
---|
112 | delete[](Header[i]);
|
---|
113 | delete[](Header);
|
---|
114 | delete[](RowCounter);
|
---|
115 | delete[](ColumnCounter);
|
---|
116 | };
|
---|
117 |
|
---|
118 | /** Either copies index matrix from another MatrixContainer or initialises with trivial mapping if NULL.
|
---|
119 | * This either copies the index matrix or just maps 1 to 1, 2 to 2 and so on for all fragments.
|
---|
120 | * \param *Matrix pointer to other MatrixContainer
|
---|
121 | * \return true - copy/initialisation sucessful, false - dimension false for copying
|
---|
122 | */
|
---|
123 | bool MatrixContainer::InitialiseIndices(class MatrixContainer *Matrix)
|
---|
124 | {
|
---|
125 | DoLog(0) && (Log() << Verbose(0) << "Initialising indices");
|
---|
126 | if (Matrix == NULL) {
|
---|
127 | DoLog(0) && (Log() << Verbose(0) << " with trivial mapping." << endl);
|
---|
128 | Indices = new int*[MatrixCounter + 1];
|
---|
129 | for(int i=MatrixCounter+1;i--;) {
|
---|
130 | Indices[i] = new int[RowCounter[i]];
|
---|
131 | for(int j=RowCounter[i];j--;)
|
---|
132 | Indices[i][j] = j;
|
---|
133 | }
|
---|
134 | } else {
|
---|
135 | DoLog(0) && (Log() << Verbose(0) << " from other MatrixContainer." << endl);
|
---|
136 | if (MatrixCounter != Matrix->MatrixCounter)
|
---|
137 | return false;
|
---|
138 | Indices = new int*[MatrixCounter + 1];
|
---|
139 | for(int i=MatrixCounter+1;i--;) {
|
---|
140 | if (RowCounter[i] != Matrix->RowCounter[i])
|
---|
141 | return false;
|
---|
142 | Indices[i] = new int[Matrix->RowCounter[i]];
|
---|
143 | for(int j=Matrix->RowCounter[i];j--;) {
|
---|
144 | Indices[i][j] = Matrix->Indices[i][j];
|
---|
145 | //Log() << Verbose(0) << Indices[i][j] << "\t";
|
---|
146 | }
|
---|
147 | //Log() << Verbose(0) << endl;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | return true;
|
---|
151 | };
|
---|
152 |
|
---|
153 | /** Parsing a number of matrices.
|
---|
154 | * -# open the matrix file
|
---|
155 | * -# skip some lines (\a skiplines)
|
---|
156 | * -# scan header lines for number of columns
|
---|
157 | * -# scan lines for number of rows
|
---|
158 | * -# allocate matrix
|
---|
159 | * -# loop over found column and row counts and parse in each entry
|
---|
160 | * \param *name directory with files
|
---|
161 | * \param skiplines number of inital lines to skip
|
---|
162 | * \param skiplines number of inital columns to skip
|
---|
163 | * \param MatrixNr index number in Matrix array to parse into
|
---|
164 | * \return parsing successful
|
---|
165 | */
|
---|
166 | bool MatrixContainer::ParseMatrix(const char *name, int skiplines, int skipcolumns, int MatrixNr)
|
---|
167 | {
|
---|
168 | ifstream input;
|
---|
169 | stringstream line;
|
---|
170 | string token;
|
---|
171 | char filename[1023];
|
---|
172 |
|
---|
173 | input.open(name, ios::in);
|
---|
174 | //Log() << Verbose(1) << "Opening " << name << " ... " << input << endl;
|
---|
175 | if (input == NULL) {
|
---|
176 | DoeLog(1) && (eLog()<< Verbose(1) << endl << "MatrixContainer::ParseMatrix: Unable to open " << name << ", is the directory correct?" << endl);
|
---|
177 | //performCriticalExit();
|
---|
178 | return false;
|
---|
179 | }
|
---|
180 |
|
---|
181 | // parse header
|
---|
182 | if (Header[MatrixNr] != NULL)
|
---|
183 | delete[] Header[MatrixNr];
|
---|
184 | Header[MatrixNr] = new char[1024];
|
---|
185 | for (int m=skiplines+1;m--;)
|
---|
186 | input.getline(Header[MatrixNr], 1023);
|
---|
187 |
|
---|
188 | // scan header for number of columns
|
---|
189 | line.str(Header[MatrixNr]);
|
---|
190 | for(int k=skipcolumns;k--;)
|
---|
191 | line >> Header[MatrixNr];
|
---|
192 | //Log() << Verbose(0) << line.str() << endl;
|
---|
193 | ColumnCounter[MatrixNr]=0;
|
---|
194 | while ( getline(line,token, '\t') ) {
|
---|
195 | if (token.length() > 0)
|
---|
196 | ColumnCounter[MatrixNr]++;
|
---|
197 | }
|
---|
198 | //Log() << Verbose(0) << line.str() << endl;
|
---|
199 | //Log() << Verbose(1) << "ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << "." << endl;
|
---|
200 | if (ColumnCounter[MatrixNr] == 0) {
|
---|
201 | DoeLog(0) && (eLog()<< Verbose(0) << "ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << " from file " << name << ", this is probably an error!" << endl);
|
---|
202 | performCriticalExit();
|
---|
203 | }
|
---|
204 |
|
---|
205 | // scan rest for number of rows/lines
|
---|
206 | RowCounter[MatrixNr]=-1; // counts one line too much
|
---|
207 | while (!input.eof()) {
|
---|
208 | input.getline(filename, 1023);
|
---|
209 | //Log() << Verbose(0) << "Comparing: " << strncmp(filename,"MeanForce",9) << endl;
|
---|
210 | RowCounter[MatrixNr]++; // then line was not last MeanForce
|
---|
211 | if (strncmp(filename,"MeanForce", 9) == 0) {
|
---|
212 | break;
|
---|
213 | }
|
---|
214 | }
|
---|
215 | //Log() << Verbose(1) << "RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from file " << name << "." << endl;
|
---|
216 | if (RowCounter[MatrixNr] == 0) {
|
---|
217 | DoeLog(0) && (eLog()<< Verbose(0) << "RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from file " << name << ", this is probably an error!" << endl);
|
---|
218 | performCriticalExit();
|
---|
219 | }
|
---|
220 |
|
---|
221 | // allocate matrix if it's not zero dimension in one direction
|
---|
222 | if ((ColumnCounter[MatrixNr] > 0) && (RowCounter[MatrixNr] > -1)) {
|
---|
223 | if (Matrix[MatrixNr] != NULL)
|
---|
224 | delete[] Matrix[MatrixNr];
|
---|
225 | Matrix[MatrixNr] = new double*[RowCounter[MatrixNr] + 1];
|
---|
226 | for(int j=0;j<RowCounter[MatrixNr]+1;j++)
|
---|
227 | Matrix[MatrixNr][j] = 0;
|
---|
228 |
|
---|
229 | // parse in each entry for this matrix
|
---|
230 | input.clear();
|
---|
231 | input.seekg(ios::beg);
|
---|
232 | for (int m=skiplines+1;m--;)
|
---|
233 | input.getline(Header[MatrixNr], 1023); // skip header
|
---|
234 | line.str(Header[MatrixNr]);
|
---|
235 | for(int k=skipcolumns;k--;) // skip columns in header too
|
---|
236 | line >> filename;
|
---|
237 | strncpy(Header[MatrixNr], line.str().c_str(), 1023);
|
---|
238 | for(int j=0;j<RowCounter[MatrixNr];j++) {
|
---|
239 | if (Matrix[MatrixNr][j] != NULL)
|
---|
240 | delete[] Matrix[MatrixNr][j];
|
---|
241 | Matrix[MatrixNr][j] = new double[ColumnCounter[MatrixNr]];
|
---|
242 | for(int k=0;k<ColumnCounter[MatrixNr];k++)
|
---|
243 | Matrix[MatrixNr][j][k] = 0;
|
---|
244 |
|
---|
245 | input.getline(filename, 1023);
|
---|
246 | stringstream lines(filename);
|
---|
247 | //Log() << Verbose(2) << "Matrix at level " << j << ":";// << filename << endl;
|
---|
248 | for(int k=skipcolumns;k--;)
|
---|
249 | lines >> filename;
|
---|
250 | for(int k=0;(k<ColumnCounter[MatrixNr]) && (!lines.eof());k++) {
|
---|
251 | lines >> Matrix[MatrixNr][j][k];
|
---|
252 | //Log() << Verbose(1) << " " << setprecision(2) << Matrix[MatrixNr][j][k] << endl;
|
---|
253 | }
|
---|
254 | if (Matrix[MatrixNr][ RowCounter[MatrixNr] ] != NULL)
|
---|
255 | delete[] Matrix[MatrixNr][ RowCounter[MatrixNr] ];
|
---|
256 | Matrix[MatrixNr][ RowCounter[MatrixNr] ] = new double[ColumnCounter[MatrixNr]];
|
---|
257 | for(int j=ColumnCounter[MatrixNr];j--;)
|
---|
258 | Matrix[MatrixNr][ RowCounter[MatrixNr] ][j] = 0.;
|
---|
259 | }
|
---|
260 | } else {
|
---|
261 | DoeLog(1) && (eLog()<< Verbose(1) << "Matrix nr. " << MatrixNr << " has column and row count of (" << ColumnCounter[MatrixNr] << "," << RowCounter[MatrixNr] << "), could not allocate nor parse!" << endl);
|
---|
262 | }
|
---|
263 | input.close();
|
---|
264 | return true;
|
---|
265 | };
|
---|
266 |
|
---|
267 | /** Parsing a number of matrices.
|
---|
268 | * -# First, count the number of matrices by counting lines in KEYSETFILE
|
---|
269 | * -# Then,
|
---|
270 | * -# construct the fragment number
|
---|
271 | * -# open the matrix file
|
---|
272 | * -# skip some lines (\a skiplines)
|
---|
273 | * -# scan header lines for number of columns
|
---|
274 | * -# scan lines for number of rows
|
---|
275 | * -# allocate matrix
|
---|
276 | * -# loop over found column and row counts and parse in each entry
|
---|
277 | * -# Finally, allocate one additional matrix (\a MatrixCounter) containing combined or temporary values
|
---|
278 | * \param *name directory with files
|
---|
279 | * \param *prefix prefix of each matrix file
|
---|
280 | * \param *suffix suffix of each matrix file
|
---|
281 | * \param skiplines number of inital lines to skip
|
---|
282 | * \param skiplines number of inital columns to skip
|
---|
283 | * \return parsing successful
|
---|
284 | */
|
---|
285 | bool MatrixContainer::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns)
|
---|
286 | {
|
---|
287 | char filename[1023];
|
---|
288 | ifstream input;
|
---|
289 | char *FragmentNumber = NULL;
|
---|
290 | stringstream file;
|
---|
291 | string token;
|
---|
292 |
|
---|
293 | // count the number of matrices
|
---|
294 | MatrixCounter = -1; // we count one too much
|
---|
295 | file << name << FRAGMENTPREFIX << KEYSETFILE;
|
---|
296 | input.open(file.str().c_str(), ios::in);
|
---|
297 | if (input == NULL) {
|
---|
298 | DoLog(0) && (Log() << Verbose(0) << endl << "MatrixContainer::ParseFragmentMatrix: Unable to open " << file.str() << ", is the directory correct?" << endl);
|
---|
299 | return false;
|
---|
300 | }
|
---|
301 | while (!input.eof()) {
|
---|
302 | input.getline(filename, 1023);
|
---|
303 | stringstream zeile(filename);
|
---|
304 | MatrixCounter++;
|
---|
305 | }
|
---|
306 | input.close();
|
---|
307 | DoLog(0) && (Log() << Verbose(0) << "Determined " << MatrixCounter << " fragments." << endl);
|
---|
308 |
|
---|
309 | DoLog(0) && (Log() << Verbose(0) << "Parsing through each fragment and retrieving " << prefix << suffix << "." << endl);
|
---|
310 | delete[](Header);
|
---|
311 | delete[](Matrix);
|
---|
312 | delete[](RowCounter);
|
---|
313 | delete[](ColumnCounter);
|
---|
314 | Header = new char*[MatrixCounter + 1]; // one more each for the total molecule
|
---|
315 | Matrix = new double**[MatrixCounter + 1]; // one more each for the total molecule
|
---|
316 | RowCounter = new int[MatrixCounter + 1];
|
---|
317 | ColumnCounter = new int[MatrixCounter + 1];
|
---|
318 | for(int i=MatrixCounter+1;i--;) {
|
---|
319 | Matrix[i] = NULL;
|
---|
320 | Header[i] = NULL;
|
---|
321 | RowCounter[i] = -1;
|
---|
322 | ColumnCounter[i] = -1;
|
---|
323 | }
|
---|
324 | for(int i=0; i < MatrixCounter;i++) {
|
---|
325 | // open matrix file
|
---|
326 | FragmentNumber = FixedDigitNumber(MatrixCounter, i);
|
---|
327 | file.str(" ");
|
---|
328 | file << name << FRAGMENTPREFIX << FragmentNumber << prefix << suffix;
|
---|
329 | if (!ParseMatrix(file.str().c_str(), skiplines, skipcolumns, i))
|
---|
330 | return false;
|
---|
331 | delete[](FragmentNumber);
|
---|
332 | }
|
---|
333 | return true;
|
---|
334 | };
|
---|
335 |
|
---|
336 | /** Allocates and resets the memory for a number \a MCounter of matrices.
|
---|
337 | * \param **GivenHeader Header line for each matrix
|
---|
338 | * \param MCounter number of matrices
|
---|
339 | * \param *RCounter number of rows for each matrix
|
---|
340 | * \param *CCounter number of columns for each matrix
|
---|
341 | * \return Allocation successful
|
---|
342 | */
|
---|
343 | bool MatrixContainer::AllocateMatrix(char **GivenHeader, int MCounter, int *RCounter, int *CCounter)
|
---|
344 | {
|
---|
345 | MatrixCounter = MCounter;
|
---|
346 | Header = new char*[MatrixCounter + 1];
|
---|
347 | Matrix = new double**[MatrixCounter + 1]; // one more each for the total molecule
|
---|
348 | RowCounter = new int[MatrixCounter + 1];
|
---|
349 | ColumnCounter = new int[MatrixCounter + 1];
|
---|
350 | for(int i=MatrixCounter+1;i--;) {
|
---|
351 | Header[i] = new char[1024];
|
---|
352 | strncpy(Header[i], GivenHeader[i], 1023);
|
---|
353 | RowCounter[i] = RCounter[i];
|
---|
354 | ColumnCounter[i] = CCounter[i];
|
---|
355 | Matrix[i] = new double*[RowCounter[i] + 1];
|
---|
356 | for(int j=RowCounter[i]+1;j--;) {
|
---|
357 | Matrix[i][j] = new double[ColumnCounter[i]];
|
---|
358 | for(int k=ColumnCounter[i];k--;)
|
---|
359 | Matrix[i][j][k] = 0.;
|
---|
360 | }
|
---|
361 | }
|
---|
362 | return true;
|
---|
363 | };
|
---|
364 |
|
---|
365 | /** Resets all values in MatrixContainer::Matrix.
|
---|
366 | * \return true if successful
|
---|
367 | */
|
---|
368 | bool MatrixContainer::ResetMatrix()
|
---|
369 | {
|
---|
370 | for(int i=MatrixCounter+1;i--;)
|
---|
371 | for(int j=RowCounter[i]+1;j--;)
|
---|
372 | for(int k=ColumnCounter[i];k--;)
|
---|
373 | Matrix[i][j][k] = 0.;
|
---|
374 | return true;
|
---|
375 | };
|
---|
376 |
|
---|
377 | /** Scans all elements of MatrixContainer::Matrix for greatest absolute value.
|
---|
378 | * \return greatest value of MatrixContainer::Matrix
|
---|
379 | */
|
---|
380 | double MatrixContainer::FindMaxValue()
|
---|
381 | {
|
---|
382 | double max = Matrix[0][0][0];
|
---|
383 | for(int i=MatrixCounter+1;i--;)
|
---|
384 | for(int j=RowCounter[i]+1;j--;)
|
---|
385 | for(int k=ColumnCounter[i];k--;)
|
---|
386 | if (fabs(Matrix[i][j][k]) > max)
|
---|
387 | max = fabs(Matrix[i][j][k]);
|
---|
388 | if (fabs(max) < MYEPSILON)
|
---|
389 | max += MYEPSILON;
|
---|
390 | return max;
|
---|
391 | };
|
---|
392 |
|
---|
393 | /** Scans all elements of MatrixContainer::Matrix for smallest absolute value.
|
---|
394 | * \return smallest value of MatrixContainer::Matrix
|
---|
395 | */
|
---|
396 | double MatrixContainer::FindMinValue()
|
---|
397 | {
|
---|
398 | double min = Matrix[0][0][0];
|
---|
399 | for(int i=MatrixCounter+1;i--;)
|
---|
400 | for(int j=RowCounter[i]+1;j--;)
|
---|
401 | for(int k=ColumnCounter[i];k--;)
|
---|
402 | if (fabs(Matrix[i][j][k]) < min)
|
---|
403 | min = fabs(Matrix[i][j][k]);
|
---|
404 | if (fabs(min) < MYEPSILON)
|
---|
405 | min += MYEPSILON;
|
---|
406 | return min;
|
---|
407 | };
|
---|
408 |
|
---|
409 | /** Sets all values in the last of MatrixContainer::Matrix to \a value.
|
---|
410 | * \param value reset value
|
---|
411 | * \param skipcolumns skip initial columns
|
---|
412 | * \return true if successful
|
---|
413 | */
|
---|
414 | bool MatrixContainer::SetLastMatrix(double value, int skipcolumns)
|
---|
415 | {
|
---|
416 | for(int j=RowCounter[MatrixCounter]+1;j--;)
|
---|
417 | for(int k=skipcolumns;k<ColumnCounter[MatrixCounter];k++)
|
---|
418 | Matrix[MatrixCounter][j][k] = value;
|
---|
419 | return true;
|
---|
420 | };
|
---|
421 |
|
---|
422 | /** Sets all values in the last of MatrixContainer::Matrix to \a value.
|
---|
423 | * \param **values matrix with each value (must have at least same dimensions!)
|
---|
424 | * \param skipcolumns skip initial columns
|
---|
425 | * \return true if successful
|
---|
426 | */
|
---|
427 | bool MatrixContainer::SetLastMatrix(double **values, int skipcolumns)
|
---|
428 | {
|
---|
429 | for(int j=RowCounter[MatrixCounter]+1;j--;)
|
---|
430 | for(int k=skipcolumns;k<ColumnCounter[MatrixCounter];k++)
|
---|
431 | Matrix[MatrixCounter][j][k] = values[j][k];
|
---|
432 | return true;
|
---|
433 | };
|
---|
434 |
|
---|
435 | /** Sums the entries with each factor and put into last element of \a ***Matrix.
|
---|
436 | * Sums over "E"-terms to create the "F"-terms
|
---|
437 | * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
|
---|
438 | * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
|
---|
439 | * \param Order bond order
|
---|
440 | * \return true if summing was successful
|
---|
441 | */
|
---|
442 | bool MatrixContainer::SumSubManyBodyTerms(class MatrixContainer &MatrixValues, class KeySetsContainer &KeySets, int Order)
|
---|
443 | {
|
---|
444 | // go through each order
|
---|
445 | for (int CurrentFragment=0;CurrentFragment<KeySets.FragmentsPerOrder[Order];CurrentFragment++) {
|
---|
446 | //Log() << Verbose(0) << "Current Fragment is " << CurrentFragment << "/" << KeySets.OrderSet[Order][CurrentFragment] << "." << endl;
|
---|
447 | // then go per order through each suborder and pick together all the terms that contain this fragment
|
---|
448 | for(int SubOrder=0;SubOrder<=Order;SubOrder++) { // go through all suborders up to the desired order
|
---|
449 | for (int j=0;j<KeySets.FragmentsPerOrder[SubOrder];j++) { // go through all possible fragments of size suborder
|
---|
450 | if (KeySets.Contains(KeySets.OrderSet[Order][CurrentFragment], KeySets.OrderSet[SubOrder][j])) {
|
---|
451 | //Log() << Verbose(0) << "Current other fragment is " << j << "/" << KeySets.OrderSet[SubOrder][j] << "." << endl;
|
---|
452 | // if the fragment's indices are all in the current fragment
|
---|
453 | for(int k=0;k<RowCounter[ KeySets.OrderSet[SubOrder][j] ];k++) { // go through all atoms in this fragment
|
---|
454 | int m = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][k];
|
---|
455 | //Log() << Verbose(0) << "Current index is " << k << "/" << m << "." << endl;
|
---|
456 | if (m != -1) { // if it's not an added hydrogen
|
---|
457 | for (int l=0;l<RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ];l++) { // look for the corresponding index in the current fragment
|
---|
458 | //Log() << Verbose(0) << "Comparing " << m << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l] << "." << endl;
|
---|
459 | if (m == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l]) {
|
---|
460 | m = l;
|
---|
461 | break;
|
---|
462 | }
|
---|
463 | }
|
---|
464 | //Log() << Verbose(0) << "Corresponding index in CurrentFragment is " << m << "." << endl;
|
---|
465 | if (m > RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) {
|
---|
466 | DoeLog(0) && (eLog()<< Verbose(0) << "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment] << " current force index " << m << " is greater than " << RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!" << endl);
|
---|
467 | performCriticalExit();
|
---|
468 | return false;
|
---|
469 | }
|
---|
470 | if (Order == SubOrder) { // equal order is always copy from Energies
|
---|
471 | for(int l=ColumnCounter[ KeySets.OrderSet[SubOrder][j] ];l--;) // then adds/subtract each column
|
---|
472 | Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][l] += MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
|
---|
473 | } else {
|
---|
474 | for(int l=ColumnCounter[ KeySets.OrderSet[SubOrder][j] ];l--;)
|
---|
475 | Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][l] -= Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
|
---|
476 | }
|
---|
477 | }
|
---|
478 | //if ((ColumnCounter[ KeySets.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1))
|
---|
479 | //Log() << Verbose(0) << "Fragments[ KeySets.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySets.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1] << endl;
|
---|
480 | }
|
---|
481 | } else {
|
---|
482 | //Log() << Verbose(0) << "Fragment " << KeySets.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySets.OrderSet[Order][CurrentFragment] << "." << endl;
|
---|
483 | }
|
---|
484 | }
|
---|
485 | }
|
---|
486 | //Log() << Verbose(0) << "Final Fragments[ KeySets.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySets.OrderSet[Order][CurrentFragment] << " ][" << KeySets.AtomCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][KeySets.AtomCounter[0]-1][1] << endl;
|
---|
487 | }
|
---|
488 |
|
---|
489 | return true;
|
---|
490 | };
|
---|
491 |
|
---|
492 | /** Writes the summed total fragment terms \f$F_{ij}\f$ to file.
|
---|
493 | * \param *name inputdir
|
---|
494 | * \param *prefix prefix before \a EnergySuffix
|
---|
495 | * \return file was written
|
---|
496 | */
|
---|
497 | bool MatrixContainer::WriteTotalFragments(const char *name, const char *prefix)
|
---|
498 | {
|
---|
499 | ofstream output;
|
---|
500 | char *FragmentNumber = NULL;
|
---|
501 |
|
---|
502 | DoLog(0) && (Log() << Verbose(0) << "Writing fragment files." << endl);
|
---|
503 | for(int i=0;i<MatrixCounter;i++) {
|
---|
504 | stringstream line;
|
---|
505 | FragmentNumber = FixedDigitNumber(MatrixCounter, i);
|
---|
506 | line << name << FRAGMENTPREFIX << FragmentNumber << "/" << prefix;
|
---|
507 | delete[](FragmentNumber);
|
---|
508 | output.open(line.str().c_str(), ios::out);
|
---|
509 | if (output == NULL) {
|
---|
510 | DoeLog(0) && (eLog()<< Verbose(0) << "MatrixContainer::WriteTotalFragments: Unable to open output energy file " << line.str() << "!" << endl);
|
---|
511 | performCriticalExit();
|
---|
512 | return false;
|
---|
513 | }
|
---|
514 | output << Header[i] << endl;
|
---|
515 | for(int j=0;j<RowCounter[i];j++) {
|
---|
516 | for(int k=0;k<ColumnCounter[i];k++)
|
---|
517 | output << scientific << Matrix[i][j][k] << "\t";
|
---|
518 | output << endl;
|
---|
519 | }
|
---|
520 | output.close();
|
---|
521 | }
|
---|
522 | return true;
|
---|
523 | };
|
---|
524 |
|
---|
525 | /** Writes the summed total values in the last matrix to file.
|
---|
526 | * \param *name inputdir
|
---|
527 | * \param *prefix prefix
|
---|
528 | * \param *suffix suffix
|
---|
529 | * \return file was written
|
---|
530 | */
|
---|
531 | bool MatrixContainer::WriteLastMatrix(const char *name, const char *prefix, const char *suffix)
|
---|
532 | {
|
---|
533 | ofstream output;
|
---|
534 | stringstream line;
|
---|
535 |
|
---|
536 | DoLog(0) && (Log() << Verbose(0) << "Writing matrix values of " << suffix << "." << endl);
|
---|
537 | line << name << prefix << suffix;
|
---|
538 | output.open(line.str().c_str(), ios::out);
|
---|
539 | if (output == NULL) {
|
---|
540 | DoeLog(0) && (eLog()<< Verbose(0) << "MatrixContainer::WriteLastMatrix: Unable to open output matrix file " << line.str() << "!" << endl);
|
---|
541 | performCriticalExit();
|
---|
542 | return false;
|
---|
543 | }
|
---|
544 | output << Header[MatrixCounter] << endl;
|
---|
545 | for(int j=0;j<RowCounter[MatrixCounter];j++) {
|
---|
546 | for(int k=0;k<ColumnCounter[MatrixCounter];k++)
|
---|
547 | output << scientific << Matrix[MatrixCounter][j][k] << "\t";
|
---|
548 | output << endl;
|
---|
549 | }
|
---|
550 | output.close();
|
---|
551 | return true;
|
---|
552 | };
|
---|
553 |
|
---|
554 | // ======================================= CLASS EnergyMatrix =============================
|
---|
555 |
|
---|
556 | /** Create a trivial energy index mapping.
|
---|
557 | * This just maps 1 to 1, 2 to 2 and so on for all fragments.
|
---|
558 | * \return creation sucessful
|
---|
559 | */
|
---|
560 | bool EnergyMatrix::ParseIndices()
|
---|
561 | {
|
---|
562 | DoLog(0) && (Log() << Verbose(0) << "Parsing energy indices." << endl);
|
---|
563 | Indices = new int*[MatrixCounter + 1];
|
---|
564 | for(int i=MatrixCounter+1;i--;) {
|
---|
565 | Indices[i] = new int[RowCounter[i]];
|
---|
566 | for(int j=RowCounter[i];j--;)
|
---|
567 | Indices[i][j] = j;
|
---|
568 | }
|
---|
569 | return true;
|
---|
570 | };
|
---|
571 |
|
---|
572 | /** Sums the energy with each factor and put into last element of \a EnergyMatrix::Matrix.
|
---|
573 | * Sums over the "F"-terms in ANOVA decomposition.
|
---|
574 | * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
|
---|
575 | * \param CorrectionFragments MatrixContainer with hydrogen saturation correction per fragments
|
---|
576 | * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
|
---|
577 | * \param Order bond order
|
---|
578 | * \parsm sign +1 or -1
|
---|
579 | * \return true if summing was successful
|
---|
580 | */
|
---|
581 | bool EnergyMatrix::SumSubEnergy(class EnergyMatrix &Fragments, class EnergyMatrix *CorrectionFragments, class KeySetsContainer &KeySets, int Order, double sign)
|
---|
582 | {
|
---|
583 | // sum energy
|
---|
584 | if (CorrectionFragments == NULL)
|
---|
585 | for(int i=KeySets.FragmentsPerOrder[Order];i--;)
|
---|
586 | for(int j=RowCounter[ KeySets.OrderSet[Order][i] ];j--;)
|
---|
587 | for(int k=ColumnCounter[ KeySets.OrderSet[Order][i] ];k--;)
|
---|
588 | Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ KeySets.OrderSet[Order][i] ][j][k];
|
---|
589 | else
|
---|
590 | for(int i=KeySets.FragmentsPerOrder[Order];i--;)
|
---|
591 | for(int j=RowCounter[ KeySets.OrderSet[Order][i] ];j--;)
|
---|
592 | for(int k=ColumnCounter[ KeySets.OrderSet[Order][i] ];k--;)
|
---|
593 | Matrix[MatrixCounter][j][k] += sign*(Fragments.Matrix[ KeySets.OrderSet[Order][i] ][j][k] + CorrectionFragments->Matrix[ KeySets.OrderSet[Order][i] ][j][k]);
|
---|
594 | return true;
|
---|
595 | };
|
---|
596 |
|
---|
597 | /** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
|
---|
598 | * \param *name directory with files
|
---|
599 | * \param *prefix prefix of each matrix file
|
---|
600 | * \param *suffix suffix of each matrix file
|
---|
601 | * \param skiplines number of inital lines to skip
|
---|
602 | * \param skiplines number of inital columns to skip
|
---|
603 | * \return parsing successful
|
---|
604 | */
|
---|
605 | bool EnergyMatrix::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns)
|
---|
606 | {
|
---|
607 | char filename[1024];
|
---|
608 | bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
|
---|
609 |
|
---|
610 | if (status) {
|
---|
611 | // count maximum of columns
|
---|
612 | RowCounter[MatrixCounter] = 0;
|
---|
613 | ColumnCounter[MatrixCounter] = 0;
|
---|
614 | for(int j=0; j < MatrixCounter;j++) { // (energy matrix might be bigger than number of atoms in terms of rows)
|
---|
615 | if (RowCounter[j] > RowCounter[MatrixCounter])
|
---|
616 | RowCounter[MatrixCounter] = RowCounter[j];
|
---|
617 | if (ColumnCounter[j] > ColumnCounter[MatrixCounter]) // take maximum of all for last matrix
|
---|
618 | ColumnCounter[MatrixCounter] = ColumnCounter[j];
|
---|
619 | }
|
---|
620 | // allocate last plus one matrix
|
---|
621 | DoLog(0) && (Log() << Verbose(0) << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl);
|
---|
622 | Matrix[MatrixCounter] = new double*[RowCounter[MatrixCounter] + 1];
|
---|
623 | for(int j=0;j<=RowCounter[MatrixCounter];j++)
|
---|
624 | Matrix[MatrixCounter][j] = new double[ColumnCounter[MatrixCounter]];
|
---|
625 |
|
---|
626 | // try independently to parse global energysuffix file if present
|
---|
627 | strncpy(filename, name, 1023);
|
---|
628 | strncat(filename, prefix, 1023-strlen(filename));
|
---|
629 | strncat(filename, suffix.c_str(), 1023-strlen(filename));
|
---|
630 | ParseMatrix(filename, skiplines, skipcolumns, MatrixCounter);
|
---|
631 | }
|
---|
632 | return status;
|
---|
633 | };
|
---|
634 |
|
---|
635 | // ======================================= CLASS ForceMatrix =============================
|
---|
636 |
|
---|
637 | /** Parsing force Indices of each fragment
|
---|
638 | * \param *name directory with \a ForcesFile
|
---|
639 | * \return parsing successful
|
---|
640 | */
|
---|
641 | bool ForceMatrix::ParseIndices(const char *name)
|
---|
642 | {
|
---|
643 | ifstream input;
|
---|
644 | char *FragmentNumber = NULL;
|
---|
645 | char filename[1023];
|
---|
646 | stringstream line;
|
---|
647 |
|
---|
648 | DoLog(0) && (Log() << Verbose(0) << "Parsing force indices for " << MatrixCounter << " matrices." << endl);
|
---|
649 | Indices = new int*[MatrixCounter + 1];
|
---|
650 | line << name << FRAGMENTPREFIX << FORCESFILE;
|
---|
651 | input.open(line.str().c_str(), ios::in);
|
---|
652 | //Log() << Verbose(0) << "Opening " << line.str() << " ... " << input << endl;
|
---|
653 | if (input == NULL) {
|
---|
654 | DoLog(0) && (Log() << Verbose(0) << endl << "ForceMatrix::ParseIndices: Unable to open " << line.str() << ", is the directory correct?" << endl);
|
---|
655 | return false;
|
---|
656 | }
|
---|
657 | for (int i=0;(i<MatrixCounter) && (!input.eof());i++) {
|
---|
658 | // get the number of atoms for this fragment
|
---|
659 | input.getline(filename, 1023);
|
---|
660 | line.str(filename);
|
---|
661 | // parse the values
|
---|
662 | Indices[i] = new int[RowCounter[i]];
|
---|
663 | FragmentNumber = FixedDigitNumber(MatrixCounter, i);
|
---|
664 | //Log() << Verbose(0) << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:";
|
---|
665 | delete[](FragmentNumber);
|
---|
666 | for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) {
|
---|
667 | line >> Indices[i][j];
|
---|
668 | //Log() << Verbose(0) << " " << Indices[i][j];
|
---|
669 | }
|
---|
670 | //Log() << Verbose(0) << endl;
|
---|
671 | }
|
---|
672 | Indices[MatrixCounter] = new int[RowCounter[MatrixCounter]];
|
---|
673 | for(int j=RowCounter[MatrixCounter];j--;) {
|
---|
674 | Indices[MatrixCounter][j] = j;
|
---|
675 | }
|
---|
676 | input.close();
|
---|
677 | return true;
|
---|
678 | };
|
---|
679 |
|
---|
680 |
|
---|
681 | /** Sums the forces and puts into last element of \a ForceMatrix::Matrix.
|
---|
682 | * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
|
---|
683 | * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
|
---|
684 | * \param Order bond order
|
---|
685 | * \param sign +1 or -1
|
---|
686 | * \return true if summing was successful
|
---|
687 | */
|
---|
688 | bool ForceMatrix::SumSubForces(class ForceMatrix &Fragments, class KeySetsContainer &KeySets, int Order, double sign)
|
---|
689 | {
|
---|
690 | int FragmentNr;
|
---|
691 | // sum forces
|
---|
692 | for(int i=0;i<KeySets.FragmentsPerOrder[Order];i++) {
|
---|
693 | FragmentNr = KeySets.OrderSet[Order][i];
|
---|
694 | for(int l=0;l<RowCounter[ FragmentNr ];l++) {
|
---|
695 | int j = Indices[ FragmentNr ][l];
|
---|
696 | if (j > RowCounter[MatrixCounter]) {
|
---|
697 | DoeLog(0) && (eLog()<< Verbose(0) << "Current force index " << j << " is greater than " << RowCounter[MatrixCounter] << "!" << endl);
|
---|
698 | performCriticalExit();
|
---|
699 | return false;
|
---|
700 | }
|
---|
701 | if (j != -1) {
|
---|
702 | //if (j == 0) Log() << Verbose(0) << "Summing onto ion 0, type 0 from fragment " << FragmentNr << ", ion " << l << "." << endl;
|
---|
703 | for(int k=2;k<ColumnCounter[MatrixCounter];k++)
|
---|
704 | Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][k];
|
---|
705 | }
|
---|
706 | }
|
---|
707 | }
|
---|
708 | return true;
|
---|
709 | };
|
---|
710 |
|
---|
711 |
|
---|
712 | /** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
|
---|
713 | * \param *name directory with files
|
---|
714 | * \param *prefix prefix of each matrix file
|
---|
715 | * \param *suffix suffix of each matrix file
|
---|
716 | * \param skiplines number of inital lines to skip
|
---|
717 | * \param skiplines number of inital columns to skip
|
---|
718 | * \return parsing successful
|
---|
719 | */
|
---|
720 | bool ForceMatrix::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns)
|
---|
721 | {
|
---|
722 | char filename[1023];
|
---|
723 | ifstream input;
|
---|
724 | stringstream file;
|
---|
725 | int nr;
|
---|
726 | bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
|
---|
727 |
|
---|
728 | if (status) {
|
---|
729 | // count number of atoms for last plus one matrix
|
---|
730 | file << name << FRAGMENTPREFIX << KEYSETFILE;
|
---|
731 | input.open(file.str().c_str(), ios::in);
|
---|
732 | if (input == NULL) {
|
---|
733 | DoLog(0) && (Log() << Verbose(0) << endl << "ForceMatrix::ParseFragmentMatrix: Unable to open " << file.str() << ", is the directory correct?" << endl);
|
---|
734 | return false;
|
---|
735 | }
|
---|
736 | RowCounter[MatrixCounter] = 0;
|
---|
737 | while (!input.eof()) {
|
---|
738 | input.getline(filename, 1023);
|
---|
739 | stringstream zeile(filename);
|
---|
740 | while (!zeile.eof()) {
|
---|
741 | zeile >> nr;
|
---|
742 | //Log() << Verbose(0) << "Current index: " << nr << "." << endl;
|
---|
743 | if (nr > RowCounter[MatrixCounter])
|
---|
744 | RowCounter[MatrixCounter] = nr;
|
---|
745 | }
|
---|
746 | }
|
---|
747 | RowCounter[MatrixCounter]++; // nr start at 0, count starts at 1
|
---|
748 | input.close();
|
---|
749 |
|
---|
750 | ColumnCounter[MatrixCounter] = 0;
|
---|
751 | for(int j=0; j < MatrixCounter;j++) { // (energy matrix might be bigger than number of atoms in terms of rows)
|
---|
752 | if (ColumnCounter[j] > ColumnCounter[MatrixCounter]) // take maximum of all for last matrix
|
---|
753 | ColumnCounter[MatrixCounter] = ColumnCounter[j];
|
---|
754 | }
|
---|
755 |
|
---|
756 | // allocate last plus one matrix
|
---|
757 | DoLog(0) && (Log() << Verbose(0) << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl);
|
---|
758 | Matrix[MatrixCounter] = new double*[RowCounter[MatrixCounter] + 1];
|
---|
759 | for(int j=0;j<=RowCounter[MatrixCounter];j++)
|
---|
760 | Matrix[MatrixCounter][j] = new double[ColumnCounter[MatrixCounter]];
|
---|
761 |
|
---|
762 | // try independently to parse global forcesuffix file if present
|
---|
763 | strncpy(filename, name, 1023);
|
---|
764 | strncat(filename, prefix, 1023-strlen(filename));
|
---|
765 | strncat(filename, suffix.c_str(), 1023-strlen(filename));
|
---|
766 | ParseMatrix(filename, skiplines, skipcolumns, MatrixCounter);
|
---|
767 | }
|
---|
768 |
|
---|
769 |
|
---|
770 | return status;
|
---|
771 | };
|
---|
772 |
|
---|
773 | // ======================================= CLASS HessianMatrix =============================
|
---|
774 |
|
---|
775 | /** Parsing force Indices of each fragment
|
---|
776 | * \param *name directory with \a ForcesFile
|
---|
777 | * \return parsing successful
|
---|
778 | */
|
---|
779 | bool HessianMatrix::ParseIndices(char *name)
|
---|
780 | {
|
---|
781 | ifstream input;
|
---|
782 | char *FragmentNumber = NULL;
|
---|
783 | char filename[1023];
|
---|
784 | stringstream line;
|
---|
785 |
|
---|
786 | DoLog(0) && (Log() << Verbose(0) << "Parsing hessian indices for " << MatrixCounter << " matrices." << endl);
|
---|
787 | Indices = new int*[MatrixCounter + 1];
|
---|
788 | line << name << FRAGMENTPREFIX << FORCESFILE;
|
---|
789 | input.open(line.str().c_str(), ios::in);
|
---|
790 | //Log() << Verbose(0) << "Opening " << line.str() << " ... " << input << endl;
|
---|
791 | if (input == NULL) {
|
---|
792 | DoLog(0) && (Log() << Verbose(0) << endl << "HessianMatrix::ParseIndices: Unable to open " << line.str() << ", is the directory correct?" << endl);
|
---|
793 | return false;
|
---|
794 | }
|
---|
795 | for (int i=0;(i<MatrixCounter) && (!input.eof());i++) {
|
---|
796 | // get the number of atoms for this fragment
|
---|
797 | input.getline(filename, 1023);
|
---|
798 | line.str(filename);
|
---|
799 | // parse the values
|
---|
800 | Indices[i] = new int[RowCounter[i]];
|
---|
801 | FragmentNumber = FixedDigitNumber(MatrixCounter, i);
|
---|
802 | //Log() << Verbose(0) << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:";
|
---|
803 | delete[](FragmentNumber);
|
---|
804 | for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) {
|
---|
805 | line >> Indices[i][j];
|
---|
806 | //Log() << Verbose(0) << " " << Indices[i][j];
|
---|
807 | }
|
---|
808 | //Log() << Verbose(0) << endl;
|
---|
809 | }
|
---|
810 | Indices[MatrixCounter] = new int[RowCounter[MatrixCounter]];
|
---|
811 | for(int j=RowCounter[MatrixCounter];j--;) {
|
---|
812 | Indices[MatrixCounter][j] = j;
|
---|
813 | }
|
---|
814 | input.close();
|
---|
815 | return true;
|
---|
816 | };
|
---|
817 |
|
---|
818 |
|
---|
819 | /** Sums the hessian entries and puts into last element of \a HessianMatrix::Matrix.
|
---|
820 | * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
|
---|
821 | * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
|
---|
822 | * \param Order bond order
|
---|
823 | * \param sign +1 or -1
|
---|
824 | * \return true if summing was successful
|
---|
825 | */
|
---|
826 | bool HessianMatrix::SumSubHessians(class HessianMatrix &Fragments, class KeySetsContainer &KeySets, int Order, double sign)
|
---|
827 | {
|
---|
828 | int FragmentNr;
|
---|
829 | // sum forces
|
---|
830 | for(int i=0;i<KeySets.FragmentsPerOrder[Order];i++) {
|
---|
831 | FragmentNr = KeySets.OrderSet[Order][i];
|
---|
832 | for(int l=0;l<RowCounter[ FragmentNr ];l++) {
|
---|
833 | int j = Indices[ FragmentNr ][l];
|
---|
834 | if (j > RowCounter[MatrixCounter]) {
|
---|
835 | DoeLog(0) && (eLog()<< Verbose(0) << "Current hessian index " << j << " is greater than " << RowCounter[MatrixCounter] << ", where i=" << i << ", Order=" << Order << ", l=" << l << " and FragmentNr=" << FragmentNr << "!" << endl);
|
---|
836 | performCriticalExit();
|
---|
837 | return false;
|
---|
838 | }
|
---|
839 | if (j != -1) {
|
---|
840 | for(int m=0;m<ColumnCounter[ FragmentNr ];m++) {
|
---|
841 | int k = Indices[ FragmentNr ][m];
|
---|
842 | if (k > ColumnCounter[MatrixCounter]) {
|
---|
843 | DoeLog(0) && (eLog()<< Verbose(0) << "Current hessian index " << k << " is greater than " << ColumnCounter[MatrixCounter] << ", where m=" << m << ", j=" << j << ", i=" << i << ", Order=" << Order << ", l=" << l << " and FragmentNr=" << FragmentNr << "!" << endl);
|
---|
844 | performCriticalExit();
|
---|
845 | return false;
|
---|
846 | }
|
---|
847 | if (k != -1) {
|
---|
848 | //Log() << Verbose(0) << "Adding " << sign*Fragments.Matrix[ FragmentNr ][l][m] << " from [" << l << "][" << m << "] onto [" << j << "][" << k << "]." << endl;
|
---|
849 | Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][m];
|
---|
850 | }
|
---|
851 | }
|
---|
852 | }
|
---|
853 | }
|
---|
854 | }
|
---|
855 | return true;
|
---|
856 | };
|
---|
857 |
|
---|
858 | /** Constructor for class HessianMatrix.
|
---|
859 | */
|
---|
860 | HessianMatrix::HessianMatrix() :
|
---|
861 | MatrixContainer(),
|
---|
862 | IsSymmetric(true)
|
---|
863 | {}
|
---|
864 |
|
---|
865 | /** Sums the hessian entries with each factor and put into last element of \a ***Matrix.
|
---|
866 | * Sums over "E"-terms to create the "F"-terms
|
---|
867 | * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
|
---|
868 | * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
|
---|
869 | * \param Order bond order
|
---|
870 | * \return true if summing was successful
|
---|
871 | */
|
---|
872 | bool HessianMatrix::SumSubManyBodyTerms(class MatrixContainer &MatrixValues, class KeySetsContainer &KeySets, int Order)
|
---|
873 | {
|
---|
874 | // go through each order
|
---|
875 | for (int CurrentFragment=0;CurrentFragment<KeySets.FragmentsPerOrder[Order];CurrentFragment++) {
|
---|
876 | //Log() << Verbose(0) << "Current Fragment is " << CurrentFragment << "/" << KeySets.OrderSet[Order][CurrentFragment] << "." << endl;
|
---|
877 | // then go per order through each suborder and pick together all the terms that contain this fragment
|
---|
878 | for(int SubOrder=0;SubOrder<=Order;SubOrder++) { // go through all suborders up to the desired order
|
---|
879 | for (int j=0;j<KeySets.FragmentsPerOrder[SubOrder];j++) { // go through all possible fragments of size suborder
|
---|
880 | if (KeySets.Contains(KeySets.OrderSet[Order][CurrentFragment], KeySets.OrderSet[SubOrder][j])) {
|
---|
881 | //Log() << Verbose(0) << "Current other fragment is " << j << "/" << KeySets.OrderSet[SubOrder][j] << "." << endl;
|
---|
882 | // if the fragment's indices are all in the current fragment
|
---|
883 | for(int k=0;k<RowCounter[ KeySets.OrderSet[SubOrder][j] ];k++) { // go through all atoms in this fragment
|
---|
884 | int m = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][k];
|
---|
885 | //Log() << Verbose(0) << "Current row index is " << k << "/" << m << "." << endl;
|
---|
886 | if (m != -1) { // if it's not an added hydrogen
|
---|
887 | for (int l=0;l<RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ];l++) { // look for the corresponding index in the current fragment
|
---|
888 | //Log() << Verbose(0) << "Comparing " << m << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l] << "." << endl;
|
---|
889 | if (m == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l]) {
|
---|
890 | m = l;
|
---|
891 | break;
|
---|
892 | }
|
---|
893 | }
|
---|
894 | //Log() << Verbose(0) << "Corresponding row index for " << k << " in CurrentFragment is " << m << "." << endl;
|
---|
895 | if (m > RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) {
|
---|
896 | DoeLog(0) && (eLog()<< Verbose(0) << "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment] << " current row index " << m << " is greater than " << RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!" << endl);
|
---|
897 | performCriticalExit();
|
---|
898 | return false;
|
---|
899 | }
|
---|
900 |
|
---|
901 | for(int l=0;l<ColumnCounter[ KeySets.OrderSet[SubOrder][j] ];l++) {
|
---|
902 | int n = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][l];
|
---|
903 | //Log() << Verbose(0) << "Current column index is " << l << "/" << n << "." << endl;
|
---|
904 | if (n != -1) { // if it's not an added hydrogen
|
---|
905 | for (int p=0;p<ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ];p++) { // look for the corresponding index in the current fragment
|
---|
906 | //Log() << Verbose(0) << "Comparing " << n << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][p] << "." << endl;
|
---|
907 | if (n == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][p]) {
|
---|
908 | n = p;
|
---|
909 | break;
|
---|
910 | }
|
---|
911 | }
|
---|
912 | //Log() << Verbose(0) << "Corresponding column index for " << l << " in CurrentFragment is " << n << "." << endl;
|
---|
913 | if (n > ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) {
|
---|
914 | DoeLog(0) && (eLog()<< Verbose(0) << "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment] << " current column index " << n << " is greater than " << ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!" << endl);
|
---|
915 | performCriticalExit();
|
---|
916 | return false;
|
---|
917 | }
|
---|
918 | if (Order == SubOrder) { // equal order is always copy from Energies
|
---|
919 | //Log() << Verbose(0) << "Adding " << MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "]." << endl;
|
---|
920 | Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][n] += MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
|
---|
921 | } else {
|
---|
922 | //Log() << Verbose(0) << "Subtracting " << Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "]." << endl;
|
---|
923 | Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][n] -= Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
|
---|
924 | }
|
---|
925 | }
|
---|
926 | }
|
---|
927 | }
|
---|
928 | //if ((ColumnCounter[ KeySets.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1))
|
---|
929 | //Log() << Verbose(0) << "Fragments[ KeySets.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySets.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1] << endl;
|
---|
930 | }
|
---|
931 | } else {
|
---|
932 | //Log() << Verbose(0) << "Fragment " << KeySets.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySets.OrderSet[Order][CurrentFragment] << "." << endl;
|
---|
933 | }
|
---|
934 | }
|
---|
935 | }
|
---|
936 | //Log() << Verbose(0) << "Final Fragments[ KeySets.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySets.OrderSet[Order][CurrentFragment] << " ][" << KeySets.AtomCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][KeySets.AtomCounter[0]-1][1] << endl;
|
---|
937 | }
|
---|
938 |
|
---|
939 | return true;
|
---|
940 | };
|
---|
941 |
|
---|
942 | /** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
|
---|
943 | * \param *name directory with files
|
---|
944 | * \param *prefix prefix of each matrix file
|
---|
945 | * \param *suffix suffix of each matrix file
|
---|
946 | * \param skiplines number of inital lines to skip
|
---|
947 | * \param skiplines number of inital columns to skip
|
---|
948 | * \return parsing successful
|
---|
949 | */
|
---|
950 | bool HessianMatrix::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns)
|
---|
951 | {
|
---|
952 | char filename[1023];
|
---|
953 | ifstream input;
|
---|
954 | stringstream file;
|
---|
955 | int nr;
|
---|
956 | bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
|
---|
957 |
|
---|
958 | if (status) {
|
---|
959 | // count number of atoms for last plus one matrix
|
---|
960 | file << name << FRAGMENTPREFIX << KEYSETFILE;
|
---|
961 | input.open(file.str().c_str(), ios::in);
|
---|
962 | if (input == NULL) {
|
---|
963 | DoLog(0) && (Log() << Verbose(0) << endl << "HessianMatrix::ParseFragmentMatrix: Unable to open " << file.str() << ", is the directory correct?" << endl);
|
---|
964 | return false;
|
---|
965 | }
|
---|
966 | RowCounter[MatrixCounter] = 0;
|
---|
967 | ColumnCounter[MatrixCounter] = 0;
|
---|
968 | while (!input.eof()) {
|
---|
969 | input.getline(filename, 1023);
|
---|
970 | stringstream zeile(filename);
|
---|
971 | while (!zeile.eof()) {
|
---|
972 | zeile >> nr;
|
---|
973 | //Log() << Verbose(0) << "Current index: " << nr << "." << endl;
|
---|
974 | if (nr > RowCounter[MatrixCounter]) {
|
---|
975 | RowCounter[MatrixCounter] = nr;
|
---|
976 | ColumnCounter[MatrixCounter] = nr;
|
---|
977 | }
|
---|
978 | }
|
---|
979 | }
|
---|
980 | RowCounter[MatrixCounter]++; // nr start at 0, count starts at 1
|
---|
981 | ColumnCounter[MatrixCounter]++; // nr start at 0, count starts at 1
|
---|
982 | input.close();
|
---|
983 |
|
---|
984 | // allocate last plus one matrix
|
---|
985 | DoLog(0) && (Log() << Verbose(0) << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl);
|
---|
986 | Matrix[MatrixCounter] = new double*[RowCounter[MatrixCounter] + 1];
|
---|
987 | for(int j=0;j<=RowCounter[MatrixCounter];j++)
|
---|
988 | Matrix[MatrixCounter][j] = new double[ColumnCounter[MatrixCounter]];
|
---|
989 |
|
---|
990 | // try independently to parse global forcesuffix file if present
|
---|
991 | strncpy(filename, name, 1023);
|
---|
992 | strncat(filename, prefix, 1023-strlen(filename));
|
---|
993 | strncat(filename, suffix.c_str(), 1023-strlen(filename));
|
---|
994 | ParseMatrix(filename, skiplines, skipcolumns, MatrixCounter);
|
---|
995 | }
|
---|
996 |
|
---|
997 |
|
---|
998 | return status;
|
---|
999 | };
|
---|
1000 |
|
---|
1001 | // ======================================= CLASS KeySetsContainer =============================
|
---|
1002 |
|
---|
1003 | /** Constructor of KeySetsContainer class.
|
---|
1004 | */
|
---|
1005 | KeySetsContainer::KeySetsContainer() :
|
---|
1006 | KeySets(NULL),
|
---|
1007 | AtomCounter(NULL),
|
---|
1008 | FragmentCounter(0),
|
---|
1009 | Order(0),
|
---|
1010 | FragmentsPerOrder(0),
|
---|
1011 | OrderSet(NULL)
|
---|
1012 | {};
|
---|
1013 |
|
---|
1014 | /** Destructor of KeySetsContainer class.
|
---|
1015 | */
|
---|
1016 | KeySetsContainer::~KeySetsContainer() {
|
---|
1017 | for(int i=FragmentCounter;i--;)
|
---|
1018 | delete[](KeySets[i]);
|
---|
1019 | for(int i=Order;i--;)
|
---|
1020 | delete[](OrderSet[i]);
|
---|
1021 | delete[](KeySets);
|
---|
1022 | delete[](OrderSet);
|
---|
1023 | delete[](AtomCounter);
|
---|
1024 | delete[](FragmentsPerOrder);
|
---|
1025 | };
|
---|
1026 |
|
---|
1027 | /** Parsing KeySets into array.
|
---|
1028 | * \param *name directory with keyset file
|
---|
1029 | * \param *ACounter number of atoms per fragment
|
---|
1030 | * \param FCounter number of fragments
|
---|
1031 | * \return parsing succesful
|
---|
1032 | */
|
---|
1033 | bool KeySetsContainer::ParseKeySets(const char *name, const int *ACounter, const int FCounter) {
|
---|
1034 | ifstream input;
|
---|
1035 | char *FragmentNumber = NULL;
|
---|
1036 | stringstream file;
|
---|
1037 | char filename[1023];
|
---|
1038 |
|
---|
1039 | FragmentCounter = FCounter;
|
---|
1040 | DoLog(0) && (Log() << Verbose(0) << "Parsing key sets." << endl);
|
---|
1041 | KeySets = new int*[FragmentCounter];
|
---|
1042 | for(int i=FragmentCounter;i--;)
|
---|
1043 | KeySets[i] = NULL;
|
---|
1044 | file << name << FRAGMENTPREFIX << KEYSETFILE;
|
---|
1045 | input.open(file.str().c_str(), ios::in);
|
---|
1046 | if (input == NULL) {
|
---|
1047 | DoLog(0) && (Log() << Verbose(0) << endl << "KeySetsContainer::ParseKeySets: Unable to open " << file.str() << ", is the directory correct?" << endl);
|
---|
1048 | return false;
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | AtomCounter = new int[FragmentCounter];
|
---|
1052 | for(int i=0;(i<FragmentCounter) && (!input.eof());i++) {
|
---|
1053 | stringstream line;
|
---|
1054 | AtomCounter[i] = ACounter[i];
|
---|
1055 | // parse the values
|
---|
1056 | KeySets[i] = new int[AtomCounter[i]];
|
---|
1057 | for(int j=AtomCounter[i];j--;)
|
---|
1058 | KeySets[i][j] = -1;
|
---|
1059 | FragmentNumber = FixedDigitNumber(FragmentCounter, i);
|
---|
1060 | //Log() << Verbose(0) << FRAGMENTPREFIX << FragmentNumber << "[" << AtomCounter[i] << "]:";
|
---|
1061 | delete[](FragmentNumber);
|
---|
1062 | input.getline(filename, 1023);
|
---|
1063 | line.str(filename);
|
---|
1064 | for(int j=0;(j<AtomCounter[i]) && (!line.eof());j++) {
|
---|
1065 | line >> KeySets[i][j];
|
---|
1066 | //Log() << Verbose(0) << " " << KeySets[i][j];
|
---|
1067 | }
|
---|
1068 | //Log() << Verbose(0) << endl;
|
---|
1069 | }
|
---|
1070 | input.close();
|
---|
1071 | return true;
|
---|
1072 | };
|
---|
1073 |
|
---|
1074 | /** Parse many body terms, associating each fragment to a certain bond order.
|
---|
1075 | * \return parsing succesful
|
---|
1076 | */
|
---|
1077 | bool KeySetsContainer::ParseManyBodyTerms()
|
---|
1078 | {
|
---|
1079 | int Counter;
|
---|
1080 |
|
---|
1081 | DoLog(0) && (Log() << Verbose(0) << "Creating Fragment terms." << endl);
|
---|
1082 | // scan through all to determine maximum order
|
---|
1083 | Order=0;
|
---|
1084 | for(int i=FragmentCounter;i--;) {
|
---|
1085 | Counter=0;
|
---|
1086 | for(int j=AtomCounter[i];j--;)
|
---|
1087 | if (KeySets[i][j] != -1)
|
---|
1088 | Counter++;
|
---|
1089 | if (Counter > Order)
|
---|
1090 | Order = Counter;
|
---|
1091 | }
|
---|
1092 | DoLog(0) && (Log() << Verbose(0) << "Found Order is " << Order << "." << endl);
|
---|
1093 |
|
---|
1094 | // scan through all to determine fragments per order
|
---|
1095 | FragmentsPerOrder = new int[Order];
|
---|
1096 | for(int i=Order;i--;)
|
---|
1097 | FragmentsPerOrder[i] = 0;
|
---|
1098 | for(int i=FragmentCounter;i--;) {
|
---|
1099 | Counter=0;
|
---|
1100 | for(int j=AtomCounter[i];j--;)
|
---|
1101 | if (KeySets[i][j] != -1)
|
---|
1102 | Counter++;
|
---|
1103 | FragmentsPerOrder[Counter-1]++;
|
---|
1104 | }
|
---|
1105 | for(int i=0;i<Order;i++)
|
---|
1106 | DoLog(0) && (Log() << Verbose(0) << "Found No. of Fragments of Order " << i+1 << " is " << FragmentsPerOrder[i] << "." << endl);
|
---|
1107 |
|
---|
1108 | // scan through all to gather indices to each order set
|
---|
1109 | OrderSet = new int*[Order];
|
---|
1110 | for(int i=Order;i--;)
|
---|
1111 | OrderSet[i] = new int[FragmentsPerOrder[i]];
|
---|
1112 | for(int i=Order;i--;)
|
---|
1113 | FragmentsPerOrder[i] = 0;
|
---|
1114 | for(int i=FragmentCounter;i--;) {
|
---|
1115 | Counter=0;
|
---|
1116 | for(int j=AtomCounter[i];j--;)
|
---|
1117 | if (KeySets[i][j] != -1)
|
---|
1118 | Counter++;
|
---|
1119 | OrderSet[Counter-1][FragmentsPerOrder[Counter-1]] = i;
|
---|
1120 | FragmentsPerOrder[Counter-1]++;
|
---|
1121 | }
|
---|
1122 | DoLog(0) && (Log() << Verbose(0) << "Printing OrderSet." << endl);
|
---|
1123 | for(int i=0;i<Order;i++) {
|
---|
1124 | for (int j=0;j<FragmentsPerOrder[i];j++) {
|
---|
1125 | DoLog(0) && (Log() << Verbose(0) << " " << OrderSet[i][j]);
|
---|
1126 | }
|
---|
1127 | DoLog(0) && (Log() << Verbose(0) << endl);
|
---|
1128 | }
|
---|
1129 | DoLog(0) && (Log() << Verbose(0) << endl);
|
---|
1130 |
|
---|
1131 |
|
---|
1132 | return true;
|
---|
1133 | };
|
---|
1134 |
|
---|
1135 | /** Compares each entry in \a *SmallerSet if it is containted in \a *GreaterSet.
|
---|
1136 | * \param GreaterSet index to greater set
|
---|
1137 | * \param SmallerSet index to smaller set
|
---|
1138 | * \return true if all keys of SmallerSet contained in GreaterSet
|
---|
1139 | */
|
---|
1140 | bool KeySetsContainer::Contains(const int GreaterSet, const int SmallerSet)
|
---|
1141 | {
|
---|
1142 | bool result = true;
|
---|
1143 | bool intermediate;
|
---|
1144 | if ((GreaterSet < 0) || (SmallerSet < 0) || (GreaterSet > FragmentCounter) || (SmallerSet > FragmentCounter)) // index out of bounds
|
---|
1145 | return false;
|
---|
1146 | for(int i=AtomCounter[SmallerSet];i--;) {
|
---|
1147 | intermediate = false;
|
---|
1148 | for (int j=AtomCounter[GreaterSet];j--;)
|
---|
1149 | intermediate = (intermediate || ((KeySets[SmallerSet][i] == KeySets[GreaterSet][j]) || (KeySets[SmallerSet][i] == -1)));
|
---|
1150 | result = result && intermediate;
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | return result;
|
---|
1154 | };
|
---|
1155 |
|
---|
1156 |
|
---|
1157 | // ======================================= END =============================================
|
---|