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