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