source: molecuilder/src/parser.cpp@ 247d49

Last change on this file since 247d49 was 59b70a, checked in by Frederik Heber <heber@…>, 16 years ago

FIX: MatrixContainer::ParseMatrix() should not call performCriticalExit(), when file opening fails.

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