[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 |
|
---|
| 557 | // ======================================= CLASS EnergyMatrix =============================
|
---|
| 558 |
|
---|
| 559 | /** Create a trivial energy index mapping.
|
---|
| 560 | * This just maps 1 to 1, 2 to 2 and so on for all fragments.
|
---|
| 561 | * \return creation sucessful
|
---|
| 562 | */
|
---|
[6ac7ee] | 563 | bool EnergyMatrix::ParseIndices()
|
---|
[14de469] | 564 | {
|
---|
[a67d19] | 565 | DoLog(0) && (Log() << Verbose(0) << "Parsing energy indices." << endl);
|
---|
[920c70] | 566 | Indices = new int*[MatrixCounter + 1];
|
---|
[042f82] | 567 | for(int i=MatrixCounter+1;i--;) {
|
---|
[920c70] | 568 | Indices[i] = new int[RowCounter[i]];
|
---|
[042f82] | 569 | for(int j=RowCounter[i];j--;)
|
---|
| 570 | Indices[i][j] = j;
|
---|
| 571 | }
|
---|
| 572 | return true;
|
---|
[14de469] | 573 | };
|
---|
| 574 |
|
---|
| 575 | /** Sums the energy with each factor and put into last element of \a EnergyMatrix::Matrix.
|
---|
[6f6a8e] | 576 | * Sums over the "F"-terms in ANOVA decomposition.
|
---|
[f731ae] | 577 | * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
|
---|
[390248] | 578 | * \param CorrectionFragments MatrixContainer with hydrogen saturation correction per fragments
|
---|
[f66195] | 579 | * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
|
---|
[14de469] | 580 | * \param Order bond order
|
---|
[390248] | 581 | * \parsm sign +1 or -1
|
---|
[14de469] | 582 | * \return true if summing was successful
|
---|
| 583 | */
|
---|
[f66195] | 584 | bool EnergyMatrix::SumSubEnergy(class EnergyMatrix &Fragments, class EnergyMatrix *CorrectionFragments, class KeySetsContainer &KeySets, int Order, double sign)
|
---|
[14de469] | 585 | {
|
---|
[042f82] | 586 | // sum energy
|
---|
| 587 | if (CorrectionFragments == NULL)
|
---|
[f66195] | 588 | for(int i=KeySets.FragmentsPerOrder[Order];i--;)
|
---|
| 589 | for(int j=RowCounter[ KeySets.OrderSet[Order][i] ];j--;)
|
---|
| 590 | for(int k=ColumnCounter[ KeySets.OrderSet[Order][i] ];k--;)
|
---|
| 591 | Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ KeySets.OrderSet[Order][i] ][j][k];
|
---|
[042f82] | 592 | else
|
---|
[f66195] | 593 | for(int i=KeySets.FragmentsPerOrder[Order];i--;)
|
---|
| 594 | for(int j=RowCounter[ KeySets.OrderSet[Order][i] ];j--;)
|
---|
| 595 | for(int k=ColumnCounter[ KeySets.OrderSet[Order][i] ];k--;)
|
---|
| 596 | Matrix[MatrixCounter][j][k] += sign*(Fragments.Matrix[ KeySets.OrderSet[Order][i] ][j][k] + CorrectionFragments->Matrix[ KeySets.OrderSet[Order][i] ][j][k]);
|
---|
[042f82] | 597 | return true;
|
---|
[14de469] | 598 | };
|
---|
| 599 |
|
---|
[8f019c] | 600 | /** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
|
---|
| 601 | * \param *name directory with files
|
---|
| 602 | * \param *prefix prefix of each matrix file
|
---|
| 603 | * \param *suffix suffix of each matrix file
|
---|
| 604 | * \param skiplines number of inital lines to skip
|
---|
| 605 | * \param skiplines number of inital columns to skip
|
---|
| 606 | * \return parsing successful
|
---|
[6ac7ee] | 607 | */
|
---|
[1c6081] | 608 | bool EnergyMatrix::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns)
|
---|
[8f019c] | 609 | {
|
---|
[042f82] | 610 | char filename[1024];
|
---|
| 611 | bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
|
---|
| 612 |
|
---|
| 613 | if (status) {
|
---|
| 614 | // count maximum of columns
|
---|
| 615 | RowCounter[MatrixCounter] = 0;
|
---|
[f731ae] | 616 | ColumnCounter[MatrixCounter] = 0;
|
---|
| 617 | for(int j=0; j < MatrixCounter;j++) { // (energy matrix might be bigger than number of atoms in terms of rows)
|
---|
[042f82] | 618 | if (RowCounter[j] > RowCounter[MatrixCounter])
|
---|
| 619 | RowCounter[MatrixCounter] = RowCounter[j];
|
---|
[f731ae] | 620 | if (ColumnCounter[j] > ColumnCounter[MatrixCounter]) // take maximum of all for last matrix
|
---|
| 621 | ColumnCounter[MatrixCounter] = ColumnCounter[j];
|
---|
| 622 | }
|
---|
[042f82] | 623 | // allocate last plus one matrix
|
---|
[a67d19] | 624 | DoLog(0) && (Log() << Verbose(0) << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl);
|
---|
[920c70] | 625 | Matrix[MatrixCounter] = new double*[RowCounter[MatrixCounter] + 1];
|
---|
[042f82] | 626 | for(int j=0;j<=RowCounter[MatrixCounter];j++)
|
---|
[920c70] | 627 | Matrix[MatrixCounter][j] = new double[ColumnCounter[MatrixCounter]];
|
---|
[8f019c] | 628 |
|
---|
[042f82] | 629 | // try independently to parse global energysuffix file if present
|
---|
| 630 | strncpy(filename, name, 1023);
|
---|
| 631 | strncat(filename, prefix, 1023-strlen(filename));
|
---|
| 632 | strncat(filename, suffix.c_str(), 1023-strlen(filename));
|
---|
[4e855e] | 633 | std::ifstream input(filename);
|
---|
| 634 | ParseMatrix(input, skiplines, skipcolumns, MatrixCounter);
|
---|
| 635 | input.close();
|
---|
[042f82] | 636 | }
|
---|
| 637 | return status;
|
---|
[8f019c] | 638 | };
|
---|
| 639 |
|
---|
[14de469] | 640 | // ======================================= CLASS ForceMatrix =============================
|
---|
| 641 |
|
---|
| 642 | /** Parsing force Indices of each fragment
|
---|
| 643 | * \param *name directory with \a ForcesFile
|
---|
[6ac7ee] | 644 | * \return parsing successful
|
---|
[14de469] | 645 | */
|
---|
[1c6081] | 646 | bool ForceMatrix::ParseIndices(const char *name)
|
---|
[14de469] | 647 | {
|
---|
[042f82] | 648 | ifstream input;
|
---|
| 649 | char *FragmentNumber = NULL;
|
---|
| 650 | char filename[1023];
|
---|
| 651 | stringstream line;
|
---|
| 652 |
|
---|
[a67d19] | 653 | DoLog(0) && (Log() << Verbose(0) << "Parsing force indices for " << MatrixCounter << " matrices." << endl);
|
---|
[920c70] | 654 | Indices = new int*[MatrixCounter + 1];
|
---|
[042f82] | 655 | line << name << FRAGMENTPREFIX << FORCESFILE;
|
---|
| 656 | input.open(line.str().c_str(), ios::in);
|
---|
[e138de] | 657 | //Log() << Verbose(0) << "Opening " << line.str() << " ... " << input << endl;
|
---|
[042f82] | 658 | if (input == NULL) {
|
---|
[68f03d] | 659 | DoLog(0) && (Log() << Verbose(0) << endl << "ForceMatrix::ParseIndices: Unable to open " << line.str() << ", is the directory correct?" << endl);
|
---|
[042f82] | 660 | return false;
|
---|
| 661 | }
|
---|
| 662 | for (int i=0;(i<MatrixCounter) && (!input.eof());i++) {
|
---|
| 663 | // get the number of atoms for this fragment
|
---|
| 664 | input.getline(filename, 1023);
|
---|
| 665 | line.str(filename);
|
---|
| 666 | // parse the values
|
---|
[920c70] | 667 | Indices[i] = new int[RowCounter[i]];
|
---|
[042f82] | 668 | FragmentNumber = FixedDigitNumber(MatrixCounter, i);
|
---|
[e138de] | 669 | //Log() << Verbose(0) << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:";
|
---|
[920c70] | 670 | delete[](FragmentNumber);
|
---|
[042f82] | 671 | for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) {
|
---|
| 672 | line >> Indices[i][j];
|
---|
[e138de] | 673 | //Log() << Verbose(0) << " " << Indices[i][j];
|
---|
[042f82] | 674 | }
|
---|
[e138de] | 675 | //Log() << Verbose(0) << endl;
|
---|
[042f82] | 676 | }
|
---|
[920c70] | 677 | Indices[MatrixCounter] = new int[RowCounter[MatrixCounter]];
|
---|
[042f82] | 678 | for(int j=RowCounter[MatrixCounter];j--;) {
|
---|
| 679 | Indices[MatrixCounter][j] = j;
|
---|
| 680 | }
|
---|
| 681 | input.close();
|
---|
| 682 | return true;
|
---|
[14de469] | 683 | };
|
---|
| 684 |
|
---|
| 685 |
|
---|
| 686 | /** Sums the forces and puts into last element of \a ForceMatrix::Matrix.
|
---|
[f731ae] | 687 | * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
|
---|
[f66195] | 688 | * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
|
---|
[14de469] | 689 | * \param Order bond order
|
---|
[042f82] | 690 | * \param sign +1 or -1
|
---|
[14de469] | 691 | * \return true if summing was successful
|
---|
| 692 | */
|
---|
[f66195] | 693 | bool ForceMatrix::SumSubForces(class ForceMatrix &Fragments, class KeySetsContainer &KeySets, int Order, double sign)
|
---|
[14de469] | 694 | {
|
---|
[042f82] | 695 | int FragmentNr;
|
---|
| 696 | // sum forces
|
---|
[f66195] | 697 | for(int i=0;i<KeySets.FragmentsPerOrder[Order];i++) {
|
---|
| 698 | FragmentNr = KeySets.OrderSet[Order][i];
|
---|
[042f82] | 699 | for(int l=0;l<RowCounter[ FragmentNr ];l++) {
|
---|
| 700 | int j = Indices[ FragmentNr ][l];
|
---|
| 701 | if (j > RowCounter[MatrixCounter]) {
|
---|
[58ed4a] | 702 | DoeLog(0) && (eLog()<< Verbose(0) << "Current force index " << j << " is greater than " << RowCounter[MatrixCounter] << "!" << endl);
|
---|
[e359a8] | 703 | performCriticalExit();
|
---|
[042f82] | 704 | return false;
|
---|
| 705 | }
|
---|
| 706 | if (j != -1) {
|
---|
[e138de] | 707 | //if (j == 0) Log() << Verbose(0) << "Summing onto ion 0, type 0 from fragment " << FragmentNr << ", ion " << l << "." << endl;
|
---|
[f731ae] | 708 | for(int k=2;k<ColumnCounter[MatrixCounter];k++)
|
---|
[042f82] | 709 | Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][k];
|
---|
| 710 | }
|
---|
| 711 | }
|
---|
| 712 | }
|
---|
| 713 | return true;
|
---|
[14de469] | 714 | };
|
---|
| 715 |
|
---|
| 716 |
|
---|
[8f019c] | 717 | /** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
|
---|
| 718 | * \param *name directory with files
|
---|
| 719 | * \param *prefix prefix of each matrix file
|
---|
| 720 | * \param *suffix suffix of each matrix file
|
---|
| 721 | * \param skiplines number of inital lines to skip
|
---|
| 722 | * \param skiplines number of inital columns to skip
|
---|
| 723 | * \return parsing successful
|
---|
[6ac7ee] | 724 | */
|
---|
[1c6081] | 725 | bool ForceMatrix::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns)
|
---|
[8f019c] | 726 | {
|
---|
[042f82] | 727 | char filename[1023];
|
---|
| 728 | ifstream input;
|
---|
| 729 | stringstream file;
|
---|
| 730 | int nr;
|
---|
| 731 | bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
|
---|
| 732 |
|
---|
| 733 | if (status) {
|
---|
| 734 | // count number of atoms for last plus one matrix
|
---|
| 735 | file << name << FRAGMENTPREFIX << KEYSETFILE;
|
---|
| 736 | input.open(file.str().c_str(), ios::in);
|
---|
| 737 | if (input == NULL) {
|
---|
[68f03d] | 738 | DoLog(0) && (Log() << Verbose(0) << endl << "ForceMatrix::ParseFragmentMatrix: Unable to open " << file.str() << ", is the directory correct?" << endl);
|
---|
[042f82] | 739 | return false;
|
---|
| 740 | }
|
---|
| 741 | RowCounter[MatrixCounter] = 0;
|
---|
| 742 | while (!input.eof()) {
|
---|
| 743 | input.getline(filename, 1023);
|
---|
| 744 | stringstream zeile(filename);
|
---|
| 745 | while (!zeile.eof()) {
|
---|
| 746 | zeile >> nr;
|
---|
[e138de] | 747 | //Log() << Verbose(0) << "Current index: " << nr << "." << endl;
|
---|
[042f82] | 748 | if (nr > RowCounter[MatrixCounter])
|
---|
| 749 | RowCounter[MatrixCounter] = nr;
|
---|
| 750 | }
|
---|
| 751 | }
|
---|
| 752 | RowCounter[MatrixCounter]++; // nr start at 0, count starts at 1
|
---|
| 753 | input.close();
|
---|
| 754 |
|
---|
[f731ae] | 755 | ColumnCounter[MatrixCounter] = 0;
|
---|
| 756 | for(int j=0; j < MatrixCounter;j++) { // (energy matrix might be bigger than number of atoms in terms of rows)
|
---|
| 757 | if (ColumnCounter[j] > ColumnCounter[MatrixCounter]) // take maximum of all for last matrix
|
---|
| 758 | ColumnCounter[MatrixCounter] = ColumnCounter[j];
|
---|
| 759 | }
|
---|
[85d278] | 760 |
|
---|
| 761 | // allocate last plus one matrix
|
---|
[a67d19] | 762 | DoLog(0) && (Log() << Verbose(0) << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl);
|
---|
[920c70] | 763 | Matrix[MatrixCounter] = new double*[RowCounter[MatrixCounter] + 1];
|
---|
[85d278] | 764 | for(int j=0;j<=RowCounter[MatrixCounter];j++)
|
---|
[920c70] | 765 | Matrix[MatrixCounter][j] = new double[ColumnCounter[MatrixCounter]];
|
---|
[85d278] | 766 |
|
---|
| 767 | // try independently to parse global forcesuffix file if present
|
---|
| 768 | strncpy(filename, name, 1023);
|
---|
| 769 | strncat(filename, prefix, 1023-strlen(filename));
|
---|
| 770 | strncat(filename, suffix.c_str(), 1023-strlen(filename));
|
---|
[4e855e] | 771 | std::ifstream input(filename);
|
---|
| 772 | ParseMatrix(input, skiplines, skipcolumns, MatrixCounter);
|
---|
| 773 | input.close();
|
---|
[85d278] | 774 | }
|
---|
| 775 |
|
---|
| 776 |
|
---|
| 777 | return status;
|
---|
| 778 | };
|
---|
| 779 |
|
---|
| 780 | // ======================================= CLASS HessianMatrix =============================
|
---|
| 781 |
|
---|
| 782 | /** Parsing force Indices of each fragment
|
---|
| 783 | * \param *name directory with \a ForcesFile
|
---|
| 784 | * \return parsing successful
|
---|
| 785 | */
|
---|
| 786 | bool HessianMatrix::ParseIndices(char *name)
|
---|
| 787 | {
|
---|
| 788 | ifstream input;
|
---|
| 789 | char *FragmentNumber = NULL;
|
---|
| 790 | char filename[1023];
|
---|
| 791 | stringstream line;
|
---|
| 792 |
|
---|
[a67d19] | 793 | DoLog(0) && (Log() << Verbose(0) << "Parsing hessian indices for " << MatrixCounter << " matrices." << endl);
|
---|
[920c70] | 794 | Indices = new int*[MatrixCounter + 1];
|
---|
[85d278] | 795 | line << name << FRAGMENTPREFIX << FORCESFILE;
|
---|
| 796 | input.open(line.str().c_str(), ios::in);
|
---|
[e138de] | 797 | //Log() << Verbose(0) << "Opening " << line.str() << " ... " << input << endl;
|
---|
[85d278] | 798 | if (input == NULL) {
|
---|
[68f03d] | 799 | DoLog(0) && (Log() << Verbose(0) << endl << "HessianMatrix::ParseIndices: Unable to open " << line.str() << ", is the directory correct?" << endl);
|
---|
[85d278] | 800 | return false;
|
---|
| 801 | }
|
---|
| 802 | for (int i=0;(i<MatrixCounter) && (!input.eof());i++) {
|
---|
| 803 | // get the number of atoms for this fragment
|
---|
| 804 | input.getline(filename, 1023);
|
---|
| 805 | line.str(filename);
|
---|
| 806 | // parse the values
|
---|
[920c70] | 807 | Indices[i] = new int[RowCounter[i]];
|
---|
[85d278] | 808 | FragmentNumber = FixedDigitNumber(MatrixCounter, i);
|
---|
[e138de] | 809 | //Log() << Verbose(0) << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:";
|
---|
[920c70] | 810 | delete[](FragmentNumber);
|
---|
[85d278] | 811 | for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) {
|
---|
| 812 | line >> Indices[i][j];
|
---|
[e138de] | 813 | //Log() << Verbose(0) << " " << Indices[i][j];
|
---|
[85d278] | 814 | }
|
---|
[e138de] | 815 | //Log() << Verbose(0) << endl;
|
---|
[85d278] | 816 | }
|
---|
[920c70] | 817 | Indices[MatrixCounter] = new int[RowCounter[MatrixCounter]];
|
---|
[85d278] | 818 | for(int j=RowCounter[MatrixCounter];j--;) {
|
---|
| 819 | Indices[MatrixCounter][j] = j;
|
---|
| 820 | }
|
---|
| 821 | input.close();
|
---|
| 822 | return true;
|
---|
| 823 | };
|
---|
| 824 |
|
---|
| 825 |
|
---|
| 826 | /** Sums the hessian entries and puts into last element of \a HessianMatrix::Matrix.
|
---|
[f731ae] | 827 | * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
|
---|
[f66195] | 828 | * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
|
---|
[85d278] | 829 | * \param Order bond order
|
---|
| 830 | * \param sign +1 or -1
|
---|
| 831 | * \return true if summing was successful
|
---|
| 832 | */
|
---|
[f66195] | 833 | bool HessianMatrix::SumSubHessians(class HessianMatrix &Fragments, class KeySetsContainer &KeySets, int Order, double sign)
|
---|
[85d278] | 834 | {
|
---|
| 835 | int FragmentNr;
|
---|
| 836 | // sum forces
|
---|
[f66195] | 837 | for(int i=0;i<KeySets.FragmentsPerOrder[Order];i++) {
|
---|
| 838 | FragmentNr = KeySets.OrderSet[Order][i];
|
---|
[85d278] | 839 | for(int l=0;l<RowCounter[ FragmentNr ];l++) {
|
---|
| 840 | int j = Indices[ FragmentNr ][l];
|
---|
| 841 | if (j > RowCounter[MatrixCounter]) {
|
---|
[58ed4a] | 842 | 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] | 843 | performCriticalExit();
|
---|
[85d278] | 844 | return false;
|
---|
| 845 | }
|
---|
| 846 | if (j != -1) {
|
---|
[f731ae] | 847 | for(int m=0;m<ColumnCounter[ FragmentNr ];m++) {
|
---|
[85d278] | 848 | int k = Indices[ FragmentNr ][m];
|
---|
[f731ae] | 849 | if (k > ColumnCounter[MatrixCounter]) {
|
---|
[58ed4a] | 850 | 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] | 851 | performCriticalExit();
|
---|
[85d278] | 852 | return false;
|
---|
| 853 | }
|
---|
| 854 | if (k != -1) {
|
---|
[e138de] | 855 | //Log() << Verbose(0) << "Adding " << sign*Fragments.Matrix[ FragmentNr ][l][m] << " from [" << l << "][" << m << "] onto [" << j << "][" << k << "]." << endl;
|
---|
[85d278] | 856 | Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][m];
|
---|
| 857 | }
|
---|
| 858 | }
|
---|
| 859 | }
|
---|
| 860 | }
|
---|
| 861 | }
|
---|
| 862 | return true;
|
---|
| 863 | };
|
---|
| 864 |
|
---|
[b12a35] | 865 | /** Constructor for class HessianMatrix.
|
---|
| 866 | */
|
---|
[97b825] | 867 | HessianMatrix::HessianMatrix() :
|
---|
| 868 | MatrixContainer(),
|
---|
| 869 | IsSymmetric(true)
|
---|
| 870 | {}
|
---|
[b12a35] | 871 |
|
---|
| 872 | /** Sums the hessian entries with each factor and put into last element of \a ***Matrix.
|
---|
| 873 | * Sums over "E"-terms to create the "F"-terms
|
---|
| 874 | * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
|
---|
[f66195] | 875 | * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
|
---|
[b12a35] | 876 | * \param Order bond order
|
---|
| 877 | * \return true if summing was successful
|
---|
| 878 | */
|
---|
[f66195] | 879 | bool HessianMatrix::SumSubManyBodyTerms(class MatrixContainer &MatrixValues, class KeySetsContainer &KeySets, int Order)
|
---|
[b12a35] | 880 | {
|
---|
| 881 | // go through each order
|
---|
[f66195] | 882 | for (int CurrentFragment=0;CurrentFragment<KeySets.FragmentsPerOrder[Order];CurrentFragment++) {
|
---|
[e138de] | 883 | //Log() << Verbose(0) << "Current Fragment is " << CurrentFragment << "/" << KeySets.OrderSet[Order][CurrentFragment] << "." << endl;
|
---|
[b12a35] | 884 | // then go per order through each suborder and pick together all the terms that contain this fragment
|
---|
| 885 | for(int SubOrder=0;SubOrder<=Order;SubOrder++) { // go through all suborders up to the desired order
|
---|
[f66195] | 886 | for (int j=0;j<KeySets.FragmentsPerOrder[SubOrder];j++) { // go through all possible fragments of size suborder
|
---|
| 887 | if (KeySets.Contains(KeySets.OrderSet[Order][CurrentFragment], KeySets.OrderSet[SubOrder][j])) {
|
---|
[e138de] | 888 | //Log() << Verbose(0) << "Current other fragment is " << j << "/" << KeySets.OrderSet[SubOrder][j] << "." << endl;
|
---|
[b12a35] | 889 | // if the fragment's indices are all in the current fragment
|
---|
[f66195] | 890 | for(int k=0;k<RowCounter[ KeySets.OrderSet[SubOrder][j] ];k++) { // go through all atoms in this fragment
|
---|
| 891 | int m = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][k];
|
---|
[e138de] | 892 | //Log() << Verbose(0) << "Current row index is " << k << "/" << m << "." << endl;
|
---|
[b12a35] | 893 | if (m != -1) { // if it's not an added hydrogen
|
---|
[f66195] | 894 | for (int l=0;l<RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ];l++) { // look for the corresponding index in the current fragment
|
---|
[e138de] | 895 | //Log() << Verbose(0) << "Comparing " << m << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l] << "." << endl;
|
---|
[f66195] | 896 | if (m == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l]) {
|
---|
[b12a35] | 897 | m = l;
|
---|
| 898 | break;
|
---|
| 899 | }
|
---|
| 900 | }
|
---|
[e138de] | 901 | //Log() << Verbose(0) << "Corresponding row index for " << k << " in CurrentFragment is " << m << "." << endl;
|
---|
[f66195] | 902 | if (m > RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) {
|
---|
[58ed4a] | 903 | 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] | 904 | performCriticalExit();
|
---|
[b12a35] | 905 | return false;
|
---|
| 906 | }
|
---|
| 907 |
|
---|
[f66195] | 908 | for(int l=0;l<ColumnCounter[ KeySets.OrderSet[SubOrder][j] ];l++) {
|
---|
| 909 | int n = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][l];
|
---|
[e138de] | 910 | //Log() << Verbose(0) << "Current column index is " << l << "/" << n << "." << endl;
|
---|
[b12a35] | 911 | if (n != -1) { // if it's not an added hydrogen
|
---|
[f66195] | 912 | for (int p=0;p<ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ];p++) { // look for the corresponding index in the current fragment
|
---|
[e138de] | 913 | //Log() << Verbose(0) << "Comparing " << n << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][p] << "." << endl;
|
---|
[f66195] | 914 | if (n == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][p]) {
|
---|
[b12a35] | 915 | n = p;
|
---|
| 916 | break;
|
---|
| 917 | }
|
---|
| 918 | }
|
---|
[e138de] | 919 | //Log() << Verbose(0) << "Corresponding column index for " << l << " in CurrentFragment is " << n << "." << endl;
|
---|
[f66195] | 920 | if (n > ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) {
|
---|
[58ed4a] | 921 | 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] | 922 | performCriticalExit();
|
---|
[b12a35] | 923 | return false;
|
---|
| 924 | }
|
---|
| 925 | if (Order == SubOrder) { // equal order is always copy from Energies
|
---|
[e138de] | 926 | //Log() << Verbose(0) << "Adding " << MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "]." << endl;
|
---|
[f66195] | 927 | Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][n] += MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
|
---|
[b12a35] | 928 | } else {
|
---|
[e138de] | 929 | //Log() << Verbose(0) << "Subtracting " << Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "]." << endl;
|
---|
[f66195] | 930 | Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][n] -= Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
|
---|
[b12a35] | 931 | }
|
---|
| 932 | }
|
---|
| 933 | }
|
---|
| 934 | }
|
---|
[f66195] | 935 | //if ((ColumnCounter[ KeySets.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1))
|
---|
[e138de] | 936 | //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] | 937 | }
|
---|
| 938 | } else {
|
---|
[e138de] | 939 | //Log() << Verbose(0) << "Fragment " << KeySets.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySets.OrderSet[Order][CurrentFragment] << "." << endl;
|
---|
[b12a35] | 940 | }
|
---|
| 941 | }
|
---|
| 942 | }
|
---|
[e138de] | 943 | //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] | 944 | }
|
---|
| 945 |
|
---|
| 946 | return true;
|
---|
| 947 | };
|
---|
[85d278] | 948 |
|
---|
| 949 | /** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
|
---|
| 950 | * \param *name directory with files
|
---|
| 951 | * \param *prefix prefix of each matrix file
|
---|
| 952 | * \param *suffix suffix of each matrix file
|
---|
| 953 | * \param skiplines number of inital lines to skip
|
---|
| 954 | * \param skiplines number of inital columns to skip
|
---|
| 955 | * \return parsing successful
|
---|
| 956 | */
|
---|
[36ec71] | 957 | bool HessianMatrix::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns)
|
---|
[8f019c] | 958 | {
|
---|
| 959 | char filename[1023];
|
---|
| 960 | ifstream input;
|
---|
| 961 | stringstream file;
|
---|
| 962 | int nr;
|
---|
| 963 | bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
|
---|
| 964 |
|
---|
| 965 | if (status) {
|
---|
| 966 | // count number of atoms for last plus one matrix
|
---|
| 967 | file << name << FRAGMENTPREFIX << KEYSETFILE;
|
---|
| 968 | input.open(file.str().c_str(), ios::in);
|
---|
| 969 | if (input == NULL) {
|
---|
[68f03d] | 970 | DoLog(0) && (Log() << Verbose(0) << endl << "HessianMatrix::ParseFragmentMatrix: Unable to open " << file.str() << ", is the directory correct?" << endl);
|
---|
[8f019c] | 971 | return false;
|
---|
| 972 | }
|
---|
| 973 | RowCounter[MatrixCounter] = 0;
|
---|
[f731ae] | 974 | ColumnCounter[MatrixCounter] = 0;
|
---|
[8f019c] | 975 | while (!input.eof()) {
|
---|
| 976 | input.getline(filename, 1023);
|
---|
| 977 | stringstream zeile(filename);
|
---|
| 978 | while (!zeile.eof()) {
|
---|
| 979 | zeile >> nr;
|
---|
[e138de] | 980 | //Log() << Verbose(0) << "Current index: " << nr << "." << endl;
|
---|
[f731ae] | 981 | if (nr > RowCounter[MatrixCounter]) {
|
---|
[8f019c] | 982 | RowCounter[MatrixCounter] = nr;
|
---|
[f731ae] | 983 | ColumnCounter[MatrixCounter] = nr;
|
---|
| 984 | }
|
---|
[8f019c] | 985 | }
|
---|
| 986 | }
|
---|
| 987 | RowCounter[MatrixCounter]++; // nr start at 0, count starts at 1
|
---|
[f731ae] | 988 | ColumnCounter[MatrixCounter]++; // nr start at 0, count starts at 1
|
---|
[8f019c] | 989 | input.close();
|
---|
| 990 |
|
---|
[042f82] | 991 | // allocate last plus one matrix
|
---|
[a67d19] | 992 | DoLog(0) && (Log() << Verbose(0) << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl);
|
---|
[920c70] | 993 | Matrix[MatrixCounter] = new double*[RowCounter[MatrixCounter] + 1];
|
---|
[042f82] | 994 | for(int j=0;j<=RowCounter[MatrixCounter];j++)
|
---|
[920c70] | 995 | Matrix[MatrixCounter][j] = new double[ColumnCounter[MatrixCounter]];
|
---|
[042f82] | 996 |
|
---|
| 997 | // try independently to parse global forcesuffix file if present
|
---|
| 998 | strncpy(filename, name, 1023);
|
---|
| 999 | strncat(filename, prefix, 1023-strlen(filename));
|
---|
| 1000 | strncat(filename, suffix.c_str(), 1023-strlen(filename));
|
---|
[4e855e] | 1001 | std::ifstream input(filename);
|
---|
| 1002 | ParseMatrix(input, skiplines, skipcolumns, MatrixCounter);
|
---|
| 1003 | input.close();
|
---|
[042f82] | 1004 | }
|
---|
| 1005 |
|
---|
| 1006 |
|
---|
| 1007 | return status;
|
---|
[8f019c] | 1008 | };
|
---|
| 1009 |
|
---|
[14de469] | 1010 | // ======================================= CLASS KeySetsContainer =============================
|
---|
| 1011 |
|
---|
| 1012 | /** Constructor of KeySetsContainer class.
|
---|
| 1013 | */
|
---|
[97b825] | 1014 | KeySetsContainer::KeySetsContainer() :
|
---|
| 1015 | KeySets(NULL),
|
---|
| 1016 | AtomCounter(NULL),
|
---|
| 1017 | FragmentCounter(0),
|
---|
| 1018 | Order(0),
|
---|
| 1019 | FragmentsPerOrder(0),
|
---|
| 1020 | OrderSet(NULL)
|
---|
| 1021 | {};
|
---|
[14de469] | 1022 |
|
---|
| 1023 | /** Destructor of KeySetsContainer class.
|
---|
| 1024 | */
|
---|
| 1025 | KeySetsContainer::~KeySetsContainer() {
|
---|
[042f82] | 1026 | for(int i=FragmentCounter;i--;)
|
---|
[920c70] | 1027 | delete[](KeySets[i]);
|
---|
[042f82] | 1028 | for(int i=Order;i--;)
|
---|
[920c70] | 1029 | delete[](OrderSet[i]);
|
---|
| 1030 | delete[](KeySets);
|
---|
| 1031 | delete[](OrderSet);
|
---|
| 1032 | delete[](AtomCounter);
|
---|
| 1033 | delete[](FragmentsPerOrder);
|
---|
[14de469] | 1034 | };
|
---|
| 1035 |
|
---|
| 1036 | /** Parsing KeySets into array.
|
---|
| 1037 | * \param *name directory with keyset file
|
---|
| 1038 | * \param *ACounter number of atoms per fragment
|
---|
| 1039 | * \param FCounter number of fragments
|
---|
| 1040 | * \return parsing succesful
|
---|
| 1041 | */
|
---|
| 1042 | bool KeySetsContainer::ParseKeySets(const char *name, const int *ACounter, const int FCounter) {
|
---|
[042f82] | 1043 | ifstream input;
|
---|
| 1044 | char *FragmentNumber = NULL;
|
---|
| 1045 | stringstream file;
|
---|
| 1046 | char filename[1023];
|
---|
| 1047 |
|
---|
| 1048 | FragmentCounter = FCounter;
|
---|
[a67d19] | 1049 | DoLog(0) && (Log() << Verbose(0) << "Parsing key sets." << endl);
|
---|
[920c70] | 1050 | KeySets = new int*[FragmentCounter];
|
---|
[042f82] | 1051 | for(int i=FragmentCounter;i--;)
|
---|
| 1052 | KeySets[i] = NULL;
|
---|
| 1053 | file << name << FRAGMENTPREFIX << KEYSETFILE;
|
---|
| 1054 | input.open(file.str().c_str(), ios::in);
|
---|
| 1055 | if (input == NULL) {
|
---|
[68f03d] | 1056 | DoLog(0) && (Log() << Verbose(0) << endl << "KeySetsContainer::ParseKeySets: Unable to open " << file.str() << ", is the directory correct?" << endl);
|
---|
[042f82] | 1057 | return false;
|
---|
| 1058 | }
|
---|
| 1059 |
|
---|
[920c70] | 1060 | AtomCounter = new int[FragmentCounter];
|
---|
[042f82] | 1061 | for(int i=0;(i<FragmentCounter) && (!input.eof());i++) {
|
---|
| 1062 | stringstream line;
|
---|
| 1063 | AtomCounter[i] = ACounter[i];
|
---|
| 1064 | // parse the values
|
---|
[920c70] | 1065 | KeySets[i] = new int[AtomCounter[i]];
|
---|
[042f82] | 1066 | for(int j=AtomCounter[i];j--;)
|
---|
| 1067 | KeySets[i][j] = -1;
|
---|
| 1068 | FragmentNumber = FixedDigitNumber(FragmentCounter, i);
|
---|
[e138de] | 1069 | //Log() << Verbose(0) << FRAGMENTPREFIX << FragmentNumber << "[" << AtomCounter[i] << "]:";
|
---|
[920c70] | 1070 | delete[](FragmentNumber);
|
---|
[042f82] | 1071 | input.getline(filename, 1023);
|
---|
| 1072 | line.str(filename);
|
---|
| 1073 | for(int j=0;(j<AtomCounter[i]) && (!line.eof());j++) {
|
---|
| 1074 | line >> KeySets[i][j];
|
---|
[e138de] | 1075 | //Log() << Verbose(0) << " " << KeySets[i][j];
|
---|
[042f82] | 1076 | }
|
---|
[e138de] | 1077 | //Log() << Verbose(0) << endl;
|
---|
[042f82] | 1078 | }
|
---|
| 1079 | input.close();
|
---|
| 1080 | return true;
|
---|
[14de469] | 1081 | };
|
---|
| 1082 |
|
---|
| 1083 | /** Parse many body terms, associating each fragment to a certain bond order.
|
---|
| 1084 | * \return parsing succesful
|
---|
| 1085 | */
|
---|
| 1086 | bool KeySetsContainer::ParseManyBodyTerms()
|
---|
| 1087 | {
|
---|
[042f82] | 1088 | int Counter;
|
---|
| 1089 |
|
---|
[a67d19] | 1090 | DoLog(0) && (Log() << Verbose(0) << "Creating Fragment terms." << endl);
|
---|
[042f82] | 1091 | // scan through all to determine maximum order
|
---|
| 1092 | Order=0;
|
---|
| 1093 | for(int i=FragmentCounter;i--;) {
|
---|
| 1094 | Counter=0;
|
---|
| 1095 | for(int j=AtomCounter[i];j--;)
|
---|
| 1096 | if (KeySets[i][j] != -1)
|
---|
| 1097 | Counter++;
|
---|
| 1098 | if (Counter > Order)
|
---|
| 1099 | Order = Counter;
|
---|
| 1100 | }
|
---|
[a67d19] | 1101 | DoLog(0) && (Log() << Verbose(0) << "Found Order is " << Order << "." << endl);
|
---|
[042f82] | 1102 |
|
---|
| 1103 | // scan through all to determine fragments per order
|
---|
[920c70] | 1104 | FragmentsPerOrder = new int[Order];
|
---|
[042f82] | 1105 | for(int i=Order;i--;)
|
---|
| 1106 | FragmentsPerOrder[i] = 0;
|
---|
| 1107 | for(int i=FragmentCounter;i--;) {
|
---|
| 1108 | Counter=0;
|
---|
| 1109 | for(int j=AtomCounter[i];j--;)
|
---|
| 1110 | if (KeySets[i][j] != -1)
|
---|
| 1111 | Counter++;
|
---|
| 1112 | FragmentsPerOrder[Counter-1]++;
|
---|
| 1113 | }
|
---|
| 1114 | for(int i=0;i<Order;i++)
|
---|
[a67d19] | 1115 | DoLog(0) && (Log() << Verbose(0) << "Found No. of Fragments of Order " << i+1 << " is " << FragmentsPerOrder[i] << "." << endl);
|
---|
[042f82] | 1116 |
|
---|
| 1117 | // scan through all to gather indices to each order set
|
---|
[920c70] | 1118 | OrderSet = new int*[Order];
|
---|
[042f82] | 1119 | for(int i=Order;i--;)
|
---|
[920c70] | 1120 | OrderSet[i] = new int[FragmentsPerOrder[i]];
|
---|
[042f82] | 1121 | for(int i=Order;i--;)
|
---|
| 1122 | FragmentsPerOrder[i] = 0;
|
---|
| 1123 | for(int i=FragmentCounter;i--;) {
|
---|
| 1124 | Counter=0;
|
---|
| 1125 | for(int j=AtomCounter[i];j--;)
|
---|
| 1126 | if (KeySets[i][j] != -1)
|
---|
| 1127 | Counter++;
|
---|
| 1128 | OrderSet[Counter-1][FragmentsPerOrder[Counter-1]] = i;
|
---|
| 1129 | FragmentsPerOrder[Counter-1]++;
|
---|
| 1130 | }
|
---|
[a67d19] | 1131 | DoLog(0) && (Log() << Verbose(0) << "Printing OrderSet." << endl);
|
---|
[042f82] | 1132 | for(int i=0;i<Order;i++) {
|
---|
| 1133 | for (int j=0;j<FragmentsPerOrder[i];j++) {
|
---|
[a67d19] | 1134 | DoLog(0) && (Log() << Verbose(0) << " " << OrderSet[i][j]);
|
---|
[042f82] | 1135 | }
|
---|
[a67d19] | 1136 | DoLog(0) && (Log() << Verbose(0) << endl);
|
---|
[042f82] | 1137 | }
|
---|
[a67d19] | 1138 | DoLog(0) && (Log() << Verbose(0) << endl);
|
---|
[042f82] | 1139 |
|
---|
| 1140 |
|
---|
| 1141 | return true;
|
---|
[14de469] | 1142 | };
|
---|
| 1143 |
|
---|
| 1144 | /** Compares each entry in \a *SmallerSet if it is containted in \a *GreaterSet.
|
---|
| 1145 | * \param GreaterSet index to greater set
|
---|
| 1146 | * \param SmallerSet index to smaller set
|
---|
| 1147 | * \return true if all keys of SmallerSet contained in GreaterSet
|
---|
| 1148 | */
|
---|
| 1149 | bool KeySetsContainer::Contains(const int GreaterSet, const int SmallerSet)
|
---|
| 1150 | {
|
---|
[042f82] | 1151 | bool result = true;
|
---|
| 1152 | bool intermediate;
|
---|
| 1153 | if ((GreaterSet < 0) || (SmallerSet < 0) || (GreaterSet > FragmentCounter) || (SmallerSet > FragmentCounter)) // index out of bounds
|
---|
| 1154 | return false;
|
---|
| 1155 | for(int i=AtomCounter[SmallerSet];i--;) {
|
---|
| 1156 | intermediate = false;
|
---|
| 1157 | for (int j=AtomCounter[GreaterSet];j--;)
|
---|
| 1158 | intermediate = (intermediate || ((KeySets[SmallerSet][i] == KeySets[GreaterSet][j]) || (KeySets[SmallerSet][i] == -1)));
|
---|
| 1159 | result = result && intermediate;
|
---|
| 1160 | }
|
---|
| 1161 |
|
---|
| 1162 | return result;
|
---|
[14de469] | 1163 | };
|
---|
| 1164 |
|
---|
| 1165 |
|
---|
| 1166 | // ======================================= END =============================================
|
---|