source: molecuilder/src/parser.cpp@ 2c69a9

Last change on this file since 2c69a9 was e6fe8a, checked in by Frederik Heber <heber@…>, 16 years ago

cstring header was missing in files, supplying definition of strlen, strcpy, and so on.

This was noted on laptop with gcc 4.1 (on workstation we have gcc 4.2)

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