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