| [a9b86d] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
|  | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | /* | 
|---|
|  | 9 | * MatrixContainer.cpp | 
|---|
|  | 10 | * | 
|---|
|  | 11 | *  Created on: Sep 15, 2011 | 
|---|
|  | 12 | *      Author: heber | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
|  | 15 | // include config.h | 
|---|
|  | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
|  | 19 |  | 
|---|
|  | 20 | #include "CodePatterns/MemDebug.hpp" | 
|---|
|  | 21 |  | 
|---|
|  | 22 | #include <cstring> | 
|---|
|  | 23 | #include <fstream> | 
|---|
|  | 24 | #include <iomanip> | 
|---|
|  | 25 |  | 
|---|
|  | 26 | #include "CodePatterns/Log.hpp" | 
|---|
|  | 27 | #include "KeySetsContainer.hpp" | 
|---|
|  | 28 |  | 
|---|
|  | 29 | #include "Fragmentation/helpers.hpp" | 
|---|
|  | 30 | #include "Helpers/defs.hpp" | 
|---|
|  | 31 | #include "Helpers/helpers.hpp" | 
|---|
|  | 32 |  | 
|---|
|  | 33 | #include "MatrixContainer.hpp" | 
|---|
|  | 34 |  | 
|---|
|  | 35 | /** Constructor of MatrixContainer class. | 
|---|
|  | 36 | */ | 
|---|
|  | 37 | MatrixContainer::MatrixContainer() : | 
|---|
|  | 38 | Indices(NULL) | 
|---|
|  | 39 | { | 
|---|
|  | 40 | Header = new char*[1]; | 
|---|
|  | 41 | Matrix = new double**[1]; // one more each for the total molecule | 
|---|
|  | 42 | RowCounter = new int[1]; | 
|---|
|  | 43 | ColumnCounter = new int[1]; | 
|---|
|  | 44 | Header[0] = NULL; | 
|---|
|  | 45 | Matrix[0] = NULL; | 
|---|
|  | 46 | RowCounter[0] = -1; | 
|---|
|  | 47 | MatrixCounter = 0; | 
|---|
|  | 48 | ColumnCounter[0] = -1; | 
|---|
|  | 49 | }; | 
|---|
|  | 50 |  | 
|---|
|  | 51 | /** Destructor of MatrixContainer class. | 
|---|
|  | 52 | */ | 
|---|
|  | 53 | MatrixContainer::~MatrixContainer() { | 
|---|
|  | 54 | if (Matrix != NULL) { | 
|---|
|  | 55 | // free Matrix[MatrixCounter] | 
|---|
|  | 56 | if ((ColumnCounter != NULL) && (RowCounter != NULL) && (Matrix[MatrixCounter] != NULL)) | 
|---|
|  | 57 | for(int j=RowCounter[MatrixCounter]+1;j--;) | 
|---|
|  | 58 | delete[](Matrix[MatrixCounter][j]); | 
|---|
|  | 59 | //if (MatrixCounter != 0) | 
|---|
|  | 60 | delete[](Matrix[MatrixCounter]); | 
|---|
|  | 61 | // free all matrices but ultimate one | 
|---|
|  | 62 | for(int i=MatrixCounter;i--;) { | 
|---|
|  | 63 | if ((ColumnCounter != NULL) && (RowCounter != NULL)) { | 
|---|
|  | 64 | for(int j=RowCounter[i]+1;j--;) | 
|---|
|  | 65 | delete[](Matrix[i][j]); | 
|---|
|  | 66 | delete[](Matrix[i]); | 
|---|
|  | 67 | } | 
|---|
|  | 68 | } | 
|---|
|  | 69 | delete[](Matrix); | 
|---|
|  | 70 | } | 
|---|
|  | 71 | // free indices | 
|---|
|  | 72 | if (Indices != NULL) | 
|---|
|  | 73 | for(int i=MatrixCounter+1;i--;) { | 
|---|
|  | 74 | delete[](Indices[i]); | 
|---|
|  | 75 | } | 
|---|
|  | 76 | delete[](Indices); | 
|---|
|  | 77 |  | 
|---|
|  | 78 | // free header and counters | 
|---|
|  | 79 | if (Header != NULL) | 
|---|
|  | 80 | for(int i=MatrixCounter+1;i--;) | 
|---|
|  | 81 | delete[](Header[i]); | 
|---|
|  | 82 | delete[](Header); | 
|---|
|  | 83 | delete[](RowCounter); | 
|---|
|  | 84 | delete[](ColumnCounter); | 
|---|
|  | 85 | }; | 
|---|
|  | 86 |  | 
|---|
|  | 87 | /** Either copies index matrix from another MatrixContainer or initialises with trivial mapping if NULL. | 
|---|
|  | 88 | * This either copies the index matrix or just maps 1 to 1, 2 to 2 and so on for all fragments. | 
|---|
|  | 89 | * \param *Matrix pointer to other MatrixContainer | 
|---|
|  | 90 | * \return true - copy/initialisation sucessful, false - dimension false for copying | 
|---|
|  | 91 | */ | 
|---|
|  | 92 | bool MatrixContainer::InitialiseIndices(class MatrixContainer *Matrix) | 
|---|
|  | 93 | { | 
|---|
|  | 94 | DoLog(0) && (Log() << Verbose(0) << "Initialising indices"); | 
|---|
|  | 95 | if (Matrix == NULL) { | 
|---|
|  | 96 | DoLog(0) && (Log() << Verbose(0) << " with trivial mapping." << endl); | 
|---|
|  | 97 | Indices = new int*[MatrixCounter + 1]; | 
|---|
|  | 98 | for(int i=MatrixCounter+1;i--;) { | 
|---|
|  | 99 | Indices[i] = new int[RowCounter[i]]; | 
|---|
|  | 100 | for(int j=RowCounter[i];j--;) | 
|---|
|  | 101 | Indices[i][j] = j; | 
|---|
|  | 102 | } | 
|---|
|  | 103 | } else { | 
|---|
|  | 104 | DoLog(0) && (Log() << Verbose(0) << " from other MatrixContainer." << endl); | 
|---|
|  | 105 | if (MatrixCounter != Matrix->MatrixCounter) | 
|---|
|  | 106 | return false; | 
|---|
|  | 107 | Indices = new int*[MatrixCounter + 1]; | 
|---|
|  | 108 | for(int i=MatrixCounter+1;i--;) { | 
|---|
|  | 109 | if (RowCounter[i] != Matrix->RowCounter[i]) | 
|---|
|  | 110 | return false; | 
|---|
|  | 111 | Indices[i] = new int[Matrix->RowCounter[i]]; | 
|---|
|  | 112 | for(int j=Matrix->RowCounter[i];j--;) { | 
|---|
|  | 113 | Indices[i][j] = Matrix->Indices[i][j]; | 
|---|
|  | 114 | //Log() << Verbose(0) << Indices[i][j] << "\t"; | 
|---|
|  | 115 | } | 
|---|
|  | 116 | //Log() << Verbose(0) << endl; | 
|---|
|  | 117 | } | 
|---|
|  | 118 | } | 
|---|
|  | 119 | return true; | 
|---|
|  | 120 | }; | 
|---|
|  | 121 |  | 
|---|
|  | 122 | /** Parsing a number of matrices. | 
|---|
|  | 123 | *    -# open the matrix file | 
|---|
|  | 124 | *    -# skip some lines (\a skiplines) | 
|---|
|  | 125 | *    -# scan header lines for number of columns | 
|---|
|  | 126 | *    -# scan lines for number of rows | 
|---|
|  | 127 | *    -# allocate matrix | 
|---|
|  | 128 | *    -# loop over found column and row counts and parse in each entry | 
|---|
|  | 129 | * \param &input input stream | 
|---|
|  | 130 | * \param skiplines number of inital lines to skip | 
|---|
|  | 131 | * \param skiplines number of inital columns to skip | 
|---|
|  | 132 | * \param MatrixNr index number in Matrix array to parse into | 
|---|
|  | 133 | * \return parsing successful | 
|---|
|  | 134 | */ | 
|---|
|  | 135 | bool MatrixContainer::ParseMatrix(std::istream &input, int skiplines, int skipcolumns, int MatrixNr) | 
|---|
|  | 136 | { | 
|---|
|  | 137 | stringstream line; | 
|---|
|  | 138 | string token; | 
|---|
|  | 139 | char filename[1023]; | 
|---|
|  | 140 |  | 
|---|
|  | 141 | if (input.fail()) { | 
|---|
|  | 142 | DoeLog(1) && (eLog()<< Verbose(1) << endl << "MatrixContainer::ParseMatrix: Unable to parse istream." << endl); | 
|---|
|  | 143 | //performCriticalExit(); | 
|---|
|  | 144 | return false; | 
|---|
|  | 145 | } | 
|---|
|  | 146 |  | 
|---|
|  | 147 | // parse header | 
|---|
|  | 148 | if (Header[MatrixNr] != NULL) | 
|---|
|  | 149 | delete[] Header[MatrixNr]; | 
|---|
|  | 150 | Header[MatrixNr] = new char[1024]; | 
|---|
|  | 151 | for (int m=skiplines+1;m--;) | 
|---|
|  | 152 | input.getline(Header[MatrixNr], 1023); | 
|---|
|  | 153 |  | 
|---|
|  | 154 | // scan header for number of columns | 
|---|
|  | 155 | line.str(Header[MatrixNr]); | 
|---|
|  | 156 | for(int k=skipcolumns;k--;) | 
|---|
|  | 157 | line >> Header[MatrixNr]; | 
|---|
|  | 158 | Log() << Verbose(0) << line.str() << endl; | 
|---|
|  | 159 | ColumnCounter[MatrixNr]=0; | 
|---|
|  | 160 | while ( getline(line,token, '\t') ) { | 
|---|
|  | 161 | if (token.length() > 0) | 
|---|
|  | 162 | ColumnCounter[MatrixNr]++; | 
|---|
|  | 163 | } | 
|---|
|  | 164 | Log() << Verbose(0) << line.str() << endl; | 
|---|
|  | 165 | Log() << Verbose(1) << "ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << "." << endl; | 
|---|
|  | 166 | if (ColumnCounter[MatrixNr] == 0) { | 
|---|
|  | 167 | DoeLog(0) && (eLog()<< Verbose(0) << "ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << " from ostream." << endl); | 
|---|
|  | 168 | performCriticalExit(); | 
|---|
|  | 169 | } | 
|---|
|  | 170 |  | 
|---|
|  | 171 | // scan rest for number of rows/lines | 
|---|
|  | 172 | RowCounter[MatrixNr]=-1;    // counts one line too much | 
|---|
|  | 173 | while (!input.eof()) { | 
|---|
|  | 174 | input.getline(filename, 1023); | 
|---|
|  | 175 | Log() << Verbose(0) << "Comparing: " << strncmp(filename,"MeanForce",9) << endl; | 
|---|
|  | 176 | RowCounter[MatrixNr]++; // then line was not last MeanForce | 
|---|
|  | 177 | if (strncmp(filename,"MeanForce", 9) == 0) { | 
|---|
|  | 178 | break; | 
|---|
|  | 179 | } | 
|---|
|  | 180 | } | 
|---|
|  | 181 | Log() << Verbose(1) << "RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from input stream." << endl; | 
|---|
|  | 182 | if (RowCounter[MatrixNr] == 0) { | 
|---|
|  | 183 | DoeLog(0) && (eLog()<< Verbose(0) << "RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from input stream." << endl); | 
|---|
|  | 184 | performCriticalExit(); | 
|---|
|  | 185 | } | 
|---|
|  | 186 |  | 
|---|
|  | 187 | // allocate matrix if it's not zero dimension in one direction | 
|---|
|  | 188 | if ((ColumnCounter[MatrixNr] > 0) && (RowCounter[MatrixNr] > -1)) { | 
|---|
|  | 189 | if (Matrix[MatrixNr] != NULL) | 
|---|
|  | 190 | delete[] Matrix[MatrixNr]; | 
|---|
|  | 191 | Matrix[MatrixNr] = new double*[RowCounter[MatrixNr] + 1]; | 
|---|
|  | 192 | for(int j=0;j<RowCounter[MatrixNr]+1;j++) | 
|---|
|  | 193 | Matrix[MatrixNr][j] = 0; | 
|---|
|  | 194 |  | 
|---|
|  | 195 | // parse in each entry for this matrix | 
|---|
|  | 196 | input.clear(); | 
|---|
|  | 197 | input.seekg(ios::beg); | 
|---|
|  | 198 | for (int m=skiplines+1;m--;) | 
|---|
|  | 199 | input.getline(Header[MatrixNr], 1023);    // skip header | 
|---|
|  | 200 | line.str(Header[MatrixNr]); | 
|---|
|  | 201 | Log() << Verbose(0) << "Header: " << line.str() << endl; | 
|---|
|  | 202 | for(int k=skipcolumns;k--;)  // skip columns in header too | 
|---|
|  | 203 | line >> filename; | 
|---|
|  | 204 | strncpy(Header[MatrixNr], line.str().c_str(), 1023); | 
|---|
|  | 205 | for(int j=0;j<RowCounter[MatrixNr];j++) { | 
|---|
|  | 206 | if (Matrix[MatrixNr][j] != NULL) | 
|---|
|  | 207 | delete[] Matrix[MatrixNr][j]; | 
|---|
|  | 208 | Matrix[MatrixNr][j] = new double[ColumnCounter[MatrixNr]]; | 
|---|
|  | 209 | for(int k=0;k<ColumnCounter[MatrixNr];k++) | 
|---|
|  | 210 | Matrix[MatrixNr][j][k] = 0; | 
|---|
|  | 211 |  | 
|---|
|  | 212 | input.getline(filename, 1023); | 
|---|
|  | 213 | stringstream lines(filename); | 
|---|
|  | 214 | Log() << Verbose(2) << "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 | Log() << Verbose(1) << " " << std::setprecision(2) << Matrix[MatrixNr][j][k] << endl; | 
|---|
|  | 220 | } | 
|---|
|  | 221 | if (Matrix[MatrixNr][ RowCounter[MatrixNr] ] != NULL) | 
|---|
|  | 222 | delete[] Matrix[MatrixNr][ RowCounter[MatrixNr] ]; | 
|---|
|  | 223 | Matrix[MatrixNr][ RowCounter[MatrixNr] ] = new double[ColumnCounter[MatrixNr]]; | 
|---|
|  | 224 | for(int j=ColumnCounter[MatrixNr];j--;) | 
|---|
|  | 225 | Matrix[MatrixNr][ RowCounter[MatrixNr] ][j] = 0.; | 
|---|
|  | 226 | } | 
|---|
|  | 227 | } else { | 
|---|
|  | 228 | DoeLog(1) && (eLog()<< Verbose(1) << "Matrix nr. " << MatrixNr << " has column and row count of (" << ColumnCounter[MatrixNr] << "," << RowCounter[MatrixNr] << "), could not allocate nor parse!" << endl); | 
|---|
|  | 229 | } | 
|---|
|  | 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.bad()) { | 
|---|
|  | 264 | DoLog(0) && (Log() << Verbose(0) << endl << "MatrixContainer::ParseFragmentMatrix: 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 | DoLog(0) && (Log() << Verbose(0) << "Determined " << MatrixCounter << " fragments." << endl); | 
|---|
|  | 274 |  | 
|---|
|  | 275 | DoLog(0) && (Log() << Verbose(0) << "Parsing through each fragment and retrieving " << prefix << suffix << "." << endl); | 
|---|
|  | 276 | delete[](Header); | 
|---|
|  | 277 | delete[](Matrix); | 
|---|
|  | 278 | delete[](RowCounter); | 
|---|
|  | 279 | delete[](ColumnCounter); | 
|---|
|  | 280 | Header = new char*[MatrixCounter + 1]; // one more each for the total molecule | 
|---|
|  | 281 | Matrix = new double**[MatrixCounter + 1]; // one more each for the total molecule | 
|---|
|  | 282 | RowCounter = new int[MatrixCounter + 1]; | 
|---|
|  | 283 | ColumnCounter = new int[MatrixCounter + 1]; | 
|---|
|  | 284 | for(int i=MatrixCounter+1;i--;) { | 
|---|
|  | 285 | Matrix[i] = NULL; | 
|---|
|  | 286 | Header[i] = NULL; | 
|---|
|  | 287 | RowCounter[i] = -1; | 
|---|
|  | 288 | ColumnCounter[i] = -1; | 
|---|
|  | 289 | } | 
|---|
|  | 290 | for(int i=0; i < MatrixCounter;i++) { | 
|---|
|  | 291 | // open matrix file | 
|---|
|  | 292 | FragmentNumber = FixedDigitNumber(MatrixCounter, i); | 
|---|
|  | 293 | file.str(" "); | 
|---|
|  | 294 | file << name << FRAGMENTPREFIX << FragmentNumber << prefix << suffix; | 
|---|
|  | 295 | std::ifstream input(file.str().c_str()); | 
|---|
|  | 296 | DoLog(0) &&( Log() << Verbose(0) << "Opening " << file.str() << " ... " << endl); | 
|---|
|  | 297 | if (!ParseMatrix(input, skiplines, skipcolumns, i)) { | 
|---|
|  | 298 | input.close(); | 
|---|
|  | 299 | return false; | 
|---|
|  | 300 | } | 
|---|
|  | 301 | input.close(); | 
|---|
|  | 302 | delete[](FragmentNumber); | 
|---|
|  | 303 | } | 
|---|
|  | 304 | return true; | 
|---|
|  | 305 | }; | 
|---|
|  | 306 |  | 
|---|
|  | 307 | /** Allocates and resets the memory for a number \a MCounter of matrices. | 
|---|
|  | 308 | * \param **GivenHeader Header line for each matrix | 
|---|
|  | 309 | * \param MCounter number of matrices | 
|---|
|  | 310 | * \param *RCounter number of rows for each matrix | 
|---|
|  | 311 | * \param *CCounter number of columns for each matrix | 
|---|
|  | 312 | * \return Allocation successful | 
|---|
|  | 313 | */ | 
|---|
|  | 314 | bool MatrixContainer::AllocateMatrix(char **GivenHeader, int MCounter, int *RCounter, int *CCounter) | 
|---|
|  | 315 | { | 
|---|
|  | 316 | MatrixCounter = MCounter; | 
|---|
|  | 317 | Header = new char*[MatrixCounter + 1]; | 
|---|
|  | 318 | Matrix = new double**[MatrixCounter + 1]; // one more each for the total molecule | 
|---|
|  | 319 | RowCounter = new int[MatrixCounter + 1]; | 
|---|
|  | 320 | ColumnCounter = new int[MatrixCounter + 1]; | 
|---|
|  | 321 | for(int i=MatrixCounter+1;i--;) { | 
|---|
|  | 322 | Header[i] = new char[1024]; | 
|---|
|  | 323 | strncpy(Header[i], GivenHeader[i], 1023); | 
|---|
|  | 324 | RowCounter[i] = RCounter[i]; | 
|---|
|  | 325 | ColumnCounter[i] = CCounter[i]; | 
|---|
|  | 326 | Matrix[i] = new double*[RowCounter[i] + 1]; | 
|---|
|  | 327 | for(int j=RowCounter[i]+1;j--;) { | 
|---|
|  | 328 | Matrix[i][j] = new double[ColumnCounter[i]]; | 
|---|
|  | 329 | for(int k=ColumnCounter[i];k--;) | 
|---|
|  | 330 | Matrix[i][j][k] = 0.; | 
|---|
|  | 331 | } | 
|---|
|  | 332 | } | 
|---|
|  | 333 | return true; | 
|---|
|  | 334 | }; | 
|---|
|  | 335 |  | 
|---|
|  | 336 | /** Resets all values in MatrixContainer::Matrix. | 
|---|
|  | 337 | * \return true if successful | 
|---|
|  | 338 | */ | 
|---|
|  | 339 | bool MatrixContainer::ResetMatrix() | 
|---|
|  | 340 | { | 
|---|
|  | 341 | for(int i=MatrixCounter+1;i--;) | 
|---|
|  | 342 | for(int j=RowCounter[i]+1;j--;) | 
|---|
|  | 343 | for(int k=ColumnCounter[i];k--;) | 
|---|
|  | 344 | Matrix[i][j][k] = 0.; | 
|---|
|  | 345 | return true; | 
|---|
|  | 346 | }; | 
|---|
|  | 347 |  | 
|---|
|  | 348 | /** Scans all elements of MatrixContainer::Matrix for greatest absolute value. | 
|---|
|  | 349 | * \return greatest value of MatrixContainer::Matrix | 
|---|
|  | 350 | */ | 
|---|
|  | 351 | double MatrixContainer::FindMaxValue() | 
|---|
|  | 352 | { | 
|---|
|  | 353 | double max = Matrix[0][0][0]; | 
|---|
|  | 354 | for(int i=MatrixCounter+1;i--;) | 
|---|
|  | 355 | for(int j=RowCounter[i]+1;j--;) | 
|---|
|  | 356 | for(int k=ColumnCounter[i];k--;) | 
|---|
|  | 357 | if (fabs(Matrix[i][j][k]) > max) | 
|---|
|  | 358 | max = fabs(Matrix[i][j][k]); | 
|---|
|  | 359 | if (fabs(max) < MYEPSILON) | 
|---|
|  | 360 | max += MYEPSILON; | 
|---|
|  | 361 | return max; | 
|---|
|  | 362 | }; | 
|---|
|  | 363 |  | 
|---|
|  | 364 | /** Scans all elements of MatrixContainer::Matrix for smallest absolute value. | 
|---|
|  | 365 | * \return smallest value of MatrixContainer::Matrix | 
|---|
|  | 366 | */ | 
|---|
|  | 367 | double MatrixContainer::FindMinValue() | 
|---|
|  | 368 | { | 
|---|
|  | 369 | double min = Matrix[0][0][0]; | 
|---|
|  | 370 | for(int i=MatrixCounter+1;i--;) | 
|---|
|  | 371 | for(int j=RowCounter[i]+1;j--;) | 
|---|
|  | 372 | for(int k=ColumnCounter[i];k--;) | 
|---|
|  | 373 | if (fabs(Matrix[i][j][k]) < min) | 
|---|
|  | 374 | min = fabs(Matrix[i][j][k]); | 
|---|
|  | 375 | if (fabs(min) < MYEPSILON) | 
|---|
|  | 376 | min += MYEPSILON; | 
|---|
|  | 377 | return min; | 
|---|
|  | 378 | }; | 
|---|
|  | 379 |  | 
|---|
|  | 380 | /** Sets all values in the last of MatrixContainer::Matrix to \a value. | 
|---|
|  | 381 | * \param value reset value | 
|---|
|  | 382 | * \param skipcolumns skip initial columns | 
|---|
|  | 383 | * \return true if successful | 
|---|
|  | 384 | */ | 
|---|
|  | 385 | bool MatrixContainer::SetLastMatrix(double value, int skipcolumns) | 
|---|
|  | 386 | { | 
|---|
|  | 387 | for(int j=RowCounter[MatrixCounter]+1;j--;) | 
|---|
|  | 388 | for(int k=skipcolumns;k<ColumnCounter[MatrixCounter];k++) | 
|---|
|  | 389 | Matrix[MatrixCounter][j][k] = value; | 
|---|
|  | 390 | return true; | 
|---|
|  | 391 | }; | 
|---|
|  | 392 |  | 
|---|
|  | 393 | /** Sets all values in the last of MatrixContainer::Matrix to \a value. | 
|---|
|  | 394 | * \param **values matrix with each value (must have at least same dimensions!) | 
|---|
|  | 395 | * \param skipcolumns skip initial columns | 
|---|
|  | 396 | * \return true if successful | 
|---|
|  | 397 | */ | 
|---|
|  | 398 | bool MatrixContainer::SetLastMatrix(double **values, int skipcolumns) | 
|---|
|  | 399 | { | 
|---|
|  | 400 | for(int j=RowCounter[MatrixCounter]+1;j--;) | 
|---|
|  | 401 | for(int k=skipcolumns;k<ColumnCounter[MatrixCounter];k++) | 
|---|
|  | 402 | Matrix[MatrixCounter][j][k] = values[j][k]; | 
|---|
|  | 403 | return true; | 
|---|
|  | 404 | }; | 
|---|
|  | 405 |  | 
|---|
|  | 406 | /** Sums the entries with each factor and put into last element of \a ***Matrix. | 
|---|
|  | 407 | * Sums over "E"-terms to create the "F"-terms | 
|---|
|  | 408 | * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies. | 
|---|
|  | 409 | * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order | 
|---|
|  | 410 | * \param Order bond order | 
|---|
|  | 411 | * \return true if summing was successful | 
|---|
|  | 412 | */ | 
|---|
|  | 413 | bool MatrixContainer::SumSubManyBodyTerms(class MatrixContainer &MatrixValues, class KeySetsContainer &KeySets, int Order) | 
|---|
|  | 414 | { | 
|---|
|  | 415 | // go through each order | 
|---|
|  | 416 | for (int CurrentFragment=0;CurrentFragment<KeySets.FragmentsPerOrder[Order];CurrentFragment++) { | 
|---|
|  | 417 | //Log() << Verbose(0) << "Current Fragment is " << CurrentFragment << "/" << KeySets.OrderSet[Order][CurrentFragment] << "." << endl; | 
|---|
|  | 418 | // then go per order through each suborder and pick together all the terms that contain this fragment | 
|---|
|  | 419 | for(int SubOrder=0;SubOrder<=Order;SubOrder++) { // go through all suborders up to the desired order | 
|---|
|  | 420 | for (int j=0;j<KeySets.FragmentsPerOrder[SubOrder];j++) { // go through all possible fragments of size suborder | 
|---|
|  | 421 | if (KeySets.Contains(KeySets.OrderSet[Order][CurrentFragment], KeySets.OrderSet[SubOrder][j])) { | 
|---|
|  | 422 | //Log() << Verbose(0) << "Current other fragment is " << j << "/" << KeySets.OrderSet[SubOrder][j] << "." << endl; | 
|---|
|  | 423 | // if the fragment's indices are all in the current fragment | 
|---|
|  | 424 | for(int k=0;k<RowCounter[ KeySets.OrderSet[SubOrder][j] ];k++) { // go through all atoms in this fragment | 
|---|
|  | 425 | int m = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][k]; | 
|---|
|  | 426 | //Log() << Verbose(0) << "Current index is " << k << "/" << m << "." << endl; | 
|---|
|  | 427 | if (m != -1) { // if it's not an added hydrogen | 
|---|
|  | 428 | for (int l=0;l<RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ];l++) { // look for the corresponding index in the current fragment | 
|---|
|  | 429 | //Log() << Verbose(0) << "Comparing " << m << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l] << "." << endl; | 
|---|
|  | 430 | if (m == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l]) { | 
|---|
|  | 431 | m = l; | 
|---|
|  | 432 | break; | 
|---|
|  | 433 | } | 
|---|
|  | 434 | } | 
|---|
|  | 435 | //Log() << Verbose(0) << "Corresponding index in CurrentFragment is " << m << "." << endl; | 
|---|
|  | 436 | if (m > RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) { | 
|---|
|  | 437 | DoeLog(0) && (eLog()<< Verbose(0) << "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment]   << " current force index " << m << " is greater than " << RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!" << endl); | 
|---|
|  | 438 | performCriticalExit(); | 
|---|
|  | 439 | return false; | 
|---|
|  | 440 | } | 
|---|
|  | 441 | if (Order == SubOrder) { // equal order is always copy from Energies | 
|---|
|  | 442 | for(int l=ColumnCounter[ KeySets.OrderSet[SubOrder][j] ];l--;) // then adds/subtract each column | 
|---|
|  | 443 | Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][l] += MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l]; | 
|---|
|  | 444 | } else { | 
|---|
|  | 445 | for(int l=ColumnCounter[ KeySets.OrderSet[SubOrder][j] ];l--;) | 
|---|
|  | 446 | Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][l] -= Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l]; | 
|---|
|  | 447 | } | 
|---|
|  | 448 | } | 
|---|
|  | 449 | //if ((ColumnCounter[ KeySets.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1)) | 
|---|
|  | 450 | //Log() << Verbose(0) << "Fragments[ KeySets.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySets.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " <<  Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1] << endl; | 
|---|
|  | 451 | } | 
|---|
|  | 452 | } else { | 
|---|
|  | 453 | //Log() << Verbose(0) << "Fragment " << KeySets.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySets.OrderSet[Order][CurrentFragment] << "." << endl; | 
|---|
|  | 454 | } | 
|---|
|  | 455 | } | 
|---|
|  | 456 | } | 
|---|
|  | 457 | //Log() << Verbose(0) << "Final Fragments[ KeySets.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySets.OrderSet[Order][CurrentFragment] << " ][" << KeySets.AtomCounter[0]-1 << "][" << 1 << "] = " <<  Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][KeySets.AtomCounter[0]-1][1] << endl; | 
|---|
|  | 458 | } | 
|---|
|  | 459 |  | 
|---|
|  | 460 | return true; | 
|---|
|  | 461 | }; | 
|---|
|  | 462 |  | 
|---|
|  | 463 | /** Writes the summed total fragment terms \f$F_{ij}\f$ to file. | 
|---|
|  | 464 | * \param *name inputdir | 
|---|
|  | 465 | * \param *prefix prefix before \a EnergySuffix | 
|---|
|  | 466 | * \return file was written | 
|---|
|  | 467 | */ | 
|---|
|  | 468 | bool MatrixContainer::WriteTotalFragments(const char *name, const char *prefix) | 
|---|
|  | 469 | { | 
|---|
|  | 470 | ofstream output; | 
|---|
|  | 471 | char *FragmentNumber = NULL; | 
|---|
|  | 472 |  | 
|---|
|  | 473 | DoLog(0) && (Log() << Verbose(0) << "Writing fragment files." << endl); | 
|---|
|  | 474 | for(int i=0;i<MatrixCounter;i++) { | 
|---|
|  | 475 | stringstream line; | 
|---|
|  | 476 | FragmentNumber = FixedDigitNumber(MatrixCounter, i); | 
|---|
|  | 477 | line << name << FRAGMENTPREFIX << FragmentNumber << "/" << prefix; | 
|---|
|  | 478 | delete[](FragmentNumber); | 
|---|
|  | 479 | output.open(line.str().c_str(), ios::out); | 
|---|
|  | 480 | if (output == NULL) { | 
|---|
|  | 481 | DoeLog(0) && (eLog()<< Verbose(0) << "MatrixContainer::WriteTotalFragments: Unable to open output energy file " << line.str() << "!" << endl); | 
|---|
|  | 482 | performCriticalExit(); | 
|---|
|  | 483 | return false; | 
|---|
|  | 484 | } | 
|---|
|  | 485 | output << Header[i] << endl; | 
|---|
|  | 486 | for(int j=0;j<RowCounter[i];j++) { | 
|---|
|  | 487 | for(int k=0;k<ColumnCounter[i];k++) | 
|---|
|  | 488 | output << scientific << Matrix[i][j][k] << "\t"; | 
|---|
|  | 489 | output << endl; | 
|---|
|  | 490 | } | 
|---|
|  | 491 | output.close(); | 
|---|
|  | 492 | } | 
|---|
|  | 493 | return true; | 
|---|
|  | 494 | }; | 
|---|
|  | 495 |  | 
|---|
|  | 496 | /** Writes the summed total values in the last matrix to file. | 
|---|
|  | 497 | * \param *name inputdir | 
|---|
|  | 498 | * \param *prefix prefix | 
|---|
|  | 499 | * \param *suffix suffix | 
|---|
|  | 500 | * \return file was written | 
|---|
|  | 501 | */ | 
|---|
|  | 502 | bool MatrixContainer::WriteLastMatrix(const char *name, const char *prefix, const char *suffix) | 
|---|
|  | 503 | { | 
|---|
|  | 504 | ofstream output; | 
|---|
|  | 505 | stringstream line; | 
|---|
|  | 506 |  | 
|---|
|  | 507 | DoLog(0) && (Log() << Verbose(0) << "Writing matrix values of " << suffix << "." << endl); | 
|---|
|  | 508 | line << name << prefix << suffix; | 
|---|
|  | 509 | output.open(line.str().c_str(), ios::out); | 
|---|
|  | 510 | if (output == NULL) { | 
|---|
|  | 511 | DoeLog(0) && (eLog()<< Verbose(0) << "MatrixContainer::WriteLastMatrix: Unable to open output matrix file " << line.str() << "!" << endl); | 
|---|
|  | 512 | performCriticalExit(); | 
|---|
|  | 513 | return false; | 
|---|
|  | 514 | } | 
|---|
|  | 515 | output << Header[MatrixCounter] << endl; | 
|---|
|  | 516 | for(int j=0;j<RowCounter[MatrixCounter];j++) { | 
|---|
|  | 517 | for(int k=0;k<ColumnCounter[MatrixCounter];k++) | 
|---|
|  | 518 | output << scientific << Matrix[MatrixCounter][j][k] << "\t"; | 
|---|
|  | 519 | output << endl; | 
|---|
|  | 520 | } | 
|---|
|  | 521 | output.close(); | 
|---|
|  | 522 | return true; | 
|---|
|  | 523 | }; | 
|---|
|  | 524 |  | 
|---|
|  | 525 | std::ostream & operator << (std::ostream &ost, const MatrixContainer &m) | 
|---|
|  | 526 | { | 
|---|
|  | 527 | for (int i=0;i<=m.MatrixCounter;++i) { | 
|---|
|  | 528 | ost << "Printing matrix " << i << ":" << std::endl; | 
|---|
|  | 529 | for (int j=0;j<=m.RowCounter[i];++j) { | 
|---|
|  | 530 | for (int k=0;k<m.ColumnCounter[i];++k) { | 
|---|
|  | 531 | ost << m.Matrix[i][j][k] << " "; | 
|---|
|  | 532 | } | 
|---|
|  | 533 | ost << std::endl; | 
|---|
|  | 534 | } | 
|---|
|  | 535 | } | 
|---|
|  | 536 | ost << std::endl; | 
|---|
|  | 537 | return ost; | 
|---|
|  | 538 | } | 
|---|