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