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