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