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