| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * MatrixContainer.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: Sep 15, 2011 | 
|---|
| 12 | *      Author: heber | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | // include config.h | 
|---|
| 16 | #ifdef HAVE_CONFIG_H | 
|---|
| 17 | #include <config.h> | 
|---|
| 18 | #endif | 
|---|
| 19 |  | 
|---|
| 20 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 21 |  | 
|---|
| 22 | #include <cstring> | 
|---|
| 23 | #include <fstream> | 
|---|
| 24 | #include <iomanip> | 
|---|
| 25 |  | 
|---|
| 26 | #include "CodePatterns/Log.hpp" | 
|---|
| 27 | #include "KeySetsContainer.hpp" | 
|---|
| 28 |  | 
|---|
| 29 | #include "Fragmentation/helpers.hpp" | 
|---|
| 30 | #include "Helpers/defs.hpp" | 
|---|
| 31 | #include "Helpers/helpers.hpp" | 
|---|
| 32 |  | 
|---|
| 33 | #include "MatrixContainer.hpp" | 
|---|
| 34 |  | 
|---|
| 35 | /** Constructor of MatrixContainer class. | 
|---|
| 36 | */ | 
|---|
| 37 | MatrixContainer::MatrixContainer() | 
|---|
| 38 | { | 
|---|
| 39 | Header.resize(1); | 
|---|
| 40 | RowCounter.resize(1); | 
|---|
| 41 | ColumnCounter.resize(1); | 
|---|
| 42 | ColumnCounter[0] = -1; | 
|---|
| 43 | MatrixCounter = 0; | 
|---|
| 44 | }; | 
|---|
| 45 |  | 
|---|
| 46 | /** Destructor of MatrixContainer class. | 
|---|
| 47 | */ | 
|---|
| 48 | MatrixContainer::~MatrixContainer() | 
|---|
| 49 | {} | 
|---|
| 50 |  | 
|---|
| 51 | /** Either copies index matrix from another MatrixContainer or initialises with trivial mapping if NULL. | 
|---|
| 52 | * This either copies the index matrix or just maps 1 to 1, 2 to 2 and so on for all fragments. | 
|---|
| 53 | * \param *Matrix pointer to other MatrixContainer | 
|---|
| 54 | * \return true - copy/initialisation sucessful, false - dimension false for copying | 
|---|
| 55 | */ | 
|---|
| 56 | bool MatrixContainer::InitialiseIndices(class MatrixContainer *_container) | 
|---|
| 57 | { | 
|---|
| 58 | if (_container == NULL) { | 
|---|
| 59 | LOG(0, "Initialising indices with trivial mapping."); | 
|---|
| 60 | Indices.resize(MatrixCounter + 1); | 
|---|
| 61 | for(int i=MatrixCounter+1;i--;) { | 
|---|
| 62 | Indices[i].resize(RowCounter[i]); | 
|---|
| 63 | for(int j=RowCounter[i];j--;) | 
|---|
| 64 | Indices[i][j] = j; | 
|---|
| 65 | } | 
|---|
| 66 | } else { | 
|---|
| 67 | std::stringstream output; | 
|---|
| 68 | if (MatrixCounter != _container->MatrixCounter) | 
|---|
| 69 | return false; | 
|---|
| 70 | Indices.resize(MatrixCounter + 1); | 
|---|
| 71 | for(int i=MatrixCounter+1;i--;) { | 
|---|
| 72 | if (RowCounter[i] != _container->RowCounter[i]) | 
|---|
| 73 | return false; | 
|---|
| 74 | Indices[i].resize(_container->RowCounter[i]); | 
|---|
| 75 | for(int j=_container->RowCounter[i];j--;) { | 
|---|
| 76 | Indices[i][j] = _container->Indices[i][j]; | 
|---|
| 77 | output << Indices[i][j] << "\t"; | 
|---|
| 78 | } | 
|---|
| 79 | } | 
|---|
| 80 | LOG(0, "Initialising indices from other MatrixContainer: " << output.str()); | 
|---|
| 81 | } | 
|---|
| 82 | return true; | 
|---|
| 83 | }; | 
|---|
| 84 |  | 
|---|
| 85 | /** Parsing a number of matrices. | 
|---|
| 86 | *    -# open the matrix file | 
|---|
| 87 | *    -# skip some lines (\a skiplines) | 
|---|
| 88 | *    -# scan header lines for number of columns | 
|---|
| 89 | *    -# scan lines for number of rows | 
|---|
| 90 | *    -# allocate matrix | 
|---|
| 91 | *    -# loop over found column and row counts and parse in each entry | 
|---|
| 92 | * \param &input input stream | 
|---|
| 93 | * \param skiplines number of inital lines to skip | 
|---|
| 94 | * \param skiplines number of inital columns to skip | 
|---|
| 95 | * \param MatrixNr index number in Matrix array to parse into | 
|---|
| 96 | * \return parsing successful | 
|---|
| 97 | */ | 
|---|
| 98 | bool MatrixContainer::ParseMatrix(std::istream &input, int skiplines, int skipcolumns, size_t MatrixNr) | 
|---|
| 99 | { | 
|---|
| 100 | stringstream line; | 
|---|
| 101 | string token; | 
|---|
| 102 | char filename[1023]; | 
|---|
| 103 |  | 
|---|
| 104 | if (input.fail()) { | 
|---|
| 105 | ELOG(1, endl << "MatrixContainer::ParseMatrix: Unable to parse istream."); | 
|---|
| 106 | //performCriticalExit(); | 
|---|
| 107 | return false; | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | // parse header | 
|---|
| 111 | if (Header.size() <= MatrixNr) | 
|---|
| 112 | Header.resize(MatrixNr); | 
|---|
| 113 | Header[MatrixNr] = std::string(""); | 
|---|
| 114 | char dummy[1024]; | 
|---|
| 115 | for (int m=skiplines+1;m--;) | 
|---|
| 116 | input.getline(dummy, 1023); | 
|---|
| 117 | line.str(dummy); | 
|---|
| 118 | for(int k=skipcolumns;k--;) | 
|---|
| 119 | line >> Header[MatrixNr]; | 
|---|
| 120 | LOG(0, line.str()); | 
|---|
| 121 |  | 
|---|
| 122 | // scan header for number of columns | 
|---|
| 123 | if (ColumnCounter.size() <= MatrixNr) | 
|---|
| 124 | ColumnCounter.resize(MatrixNr); | 
|---|
| 125 | ColumnCounter[MatrixNr]=0; | 
|---|
| 126 | while ( getline(line,token, '\t') ) { | 
|---|
| 127 | if (token.length() > 0) | 
|---|
| 128 | ColumnCounter[MatrixNr]++; | 
|---|
| 129 | } | 
|---|
| 130 | LOG(0, line.str()); | 
|---|
| 131 | LOG(1, "ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << "."); | 
|---|
| 132 | if (ColumnCounter[MatrixNr] == 0) { | 
|---|
| 133 | ELOG(0, "ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << " from ostream."); | 
|---|
| 134 | performCriticalExit(); | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | // scan rest for number of rows/lines | 
|---|
| 138 | if (RowCounter.size() <= MatrixNr) | 
|---|
| 139 | RowCounter.resize(MatrixNr); | 
|---|
| 140 | RowCounter[MatrixNr]=-1;    // counts one line too much | 
|---|
| 141 | while (!input.eof()) { | 
|---|
| 142 | input.getline(filename, 1023); | 
|---|
| 143 | LOG(0, "Comparing: " << strncmp(filename,"MeanForce",9)); | 
|---|
| 144 | RowCounter[MatrixNr]++; // then line was not last MeanForce | 
|---|
| 145 | if (strncmp(filename,"MeanForce", 9) == 0) { | 
|---|
| 146 | break; | 
|---|
| 147 | } | 
|---|
| 148 | } | 
|---|
| 149 | LOG(1, "RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from input stream."); | 
|---|
| 150 | if (RowCounter[MatrixNr] == 0) { | 
|---|
| 151 | ELOG(0, "RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from input stream."); | 
|---|
| 152 | performCriticalExit(); | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | // allocate matrix if it's not zero dimension in one direction | 
|---|
| 156 | if (Matrix.size() <= MatrixNr) | 
|---|
| 157 | Matrix.resize(MatrixNr+1); | 
|---|
| 158 | if ((Matrix[MatrixNr].size() <= (size_t)RowCounter[MatrixNr] + 1) && (RowCounter[MatrixNr] > -1)) { | 
|---|
| 159 | Matrix[MatrixNr].resize(RowCounter[MatrixNr] + 2); | 
|---|
| 160 | for(int j=0;j<=RowCounter[MatrixNr];j++) { | 
|---|
| 161 | if ((Matrix[MatrixNr][j].size() <= (size_t)ColumnCounter[MatrixNr]) && (ColumnCounter[MatrixNr] > -1)) | 
|---|
| 162 | Matrix[MatrixNr][j].resize(ColumnCounter[MatrixNr]+1); | 
|---|
| 163 | // clear | 
|---|
| 164 | for(int k=0;k<=ColumnCounter[MatrixNr];k++) | 
|---|
| 165 | Matrix[MatrixNr][j][k] = 0; | 
|---|
| 166 | } | 
|---|
| 167 | } else { | 
|---|
| 168 | ELOG(1, "Matrix nr. " << MatrixNr << " has column and row count of (" << ColumnCounter[MatrixNr] << "," << RowCounter[MatrixNr] << "), could not allocate nor parse!"); | 
|---|
| 169 | return false; | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | // parse in each entry for this matrix | 
|---|
| 173 | input.clear(); | 
|---|
| 174 | input.seekg(ios::beg); | 
|---|
| 175 | for (int m=skiplines+1;m--;) | 
|---|
| 176 | input.getline(dummy, 1023);    // skip header | 
|---|
| 177 | line.str(dummy); | 
|---|
| 178 | LOG(0, "Header: " << line.str()); | 
|---|
| 179 | for(int k=skipcolumns;k--;)  // skip columns in header too | 
|---|
| 180 | line >> filename; | 
|---|
| 181 | Header[MatrixNr] = line.str(); | 
|---|
| 182 | for(int j=0;j<RowCounter[MatrixNr];j++) { | 
|---|
| 183 | input.getline(filename, 1023); | 
|---|
| 184 | std::stringstream lines(filename); | 
|---|
| 185 | std::stringstream output; | 
|---|
| 186 | output << "INFO: Matrix at level " << j << ":";// << filename << endl; | 
|---|
| 187 | for(int k=skipcolumns;k--;) | 
|---|
| 188 | lines >> filename; | 
|---|
| 189 | for(int k=0;(k<ColumnCounter[MatrixNr]) && (!lines.eof());k++) { | 
|---|
| 190 | lines >> Matrix[MatrixNr][j][k]; | 
|---|
| 191 | output << " " << std::setprecision(2) << Matrix[MatrixNr][j][k] << endl; | 
|---|
| 192 | } | 
|---|
| 193 | LOG(1, output.str()); | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | return true; | 
|---|
| 197 | }; | 
|---|
| 198 |  | 
|---|
| 199 | /** Parsing a number of matrices. | 
|---|
| 200 | * -# First, count the number of matrices by counting lines in KEYSETFILE | 
|---|
| 201 | * -# Then, | 
|---|
| 202 | *    -# construct the fragment number | 
|---|
| 203 | *    -# open the matrix file | 
|---|
| 204 | *    -# skip some lines (\a skiplines) | 
|---|
| 205 | *    -# scan header lines for number of columns | 
|---|
| 206 | *    -# scan lines for number of rows | 
|---|
| 207 | *    -# allocate matrix | 
|---|
| 208 | *    -# loop over found column and row counts and parse in each entry | 
|---|
| 209 | * -# Finally, allocate one additional matrix (\a MatrixCounter) containing combined or temporary values | 
|---|
| 210 | * \param *name directory with files | 
|---|
| 211 | * \param *prefix prefix of each matrix file | 
|---|
| 212 | * \param *suffix suffix of each matrix file | 
|---|
| 213 | * \param skiplines number of inital lines to skip | 
|---|
| 214 | * \param skiplines number of inital columns to skip | 
|---|
| 215 | * \return parsing successful | 
|---|
| 216 | */ | 
|---|
| 217 | bool MatrixContainer::ParseFragmentMatrix(const std::string name, const std::string prefix, std::string suffix, int skiplines, int skipcolumns) | 
|---|
| 218 | { | 
|---|
| 219 | char filename[1023]; | 
|---|
| 220 | ifstream input; | 
|---|
| 221 | char *FragmentNumber = NULL; | 
|---|
| 222 | stringstream file; | 
|---|
| 223 | string token; | 
|---|
| 224 |  | 
|---|
| 225 | // count the number of matrices | 
|---|
| 226 | MatrixCounter = -1; // we count one too much | 
|---|
| 227 | file << name << FRAGMENTPREFIX << KEYSETFILE; | 
|---|
| 228 | input.open(file.str().c_str(), ios::in); | 
|---|
| 229 | if (input.bad()) { | 
|---|
| 230 | LOG(0, endl << "MatrixContainer::ParseFragmentMatrix: Unable to open " << file.str() << ", is the directory correct?"); | 
|---|
| 231 | return false; | 
|---|
| 232 | } | 
|---|
| 233 | while (!input.eof()) { | 
|---|
| 234 | input.getline(filename, 1023); | 
|---|
| 235 | stringstream zeile(filename); | 
|---|
| 236 | MatrixCounter++; | 
|---|
| 237 | } | 
|---|
| 238 | input.close(); | 
|---|
| 239 | LOG(0, "Determined " << MatrixCounter << " fragments."); | 
|---|
| 240 |  | 
|---|
| 241 | LOG(0, "Parsing through each fragment and retrieving " << prefix << suffix << "."); | 
|---|
| 242 | Header.clear(); | 
|---|
| 243 | Matrix.clear(); | 
|---|
| 244 | RowCounter.clear(); | 
|---|
| 245 | ColumnCounter.clear(); | 
|---|
| 246 | Header.resize(MatrixCounter + 1); // one more each for the total molecule | 
|---|
| 247 | Matrix.resize(MatrixCounter + 1); // one more each for the total molecule | 
|---|
| 248 | RowCounter.resize(MatrixCounter + 1); | 
|---|
| 249 | ColumnCounter.resize(MatrixCounter + 1); | 
|---|
| 250 | for(int i=0; i < MatrixCounter;i++) { | 
|---|
| 251 | // open matrix file | 
|---|
| 252 | FragmentNumber = FixedDigitNumber(MatrixCounter, i); | 
|---|
| 253 | file.str(" "); | 
|---|
| 254 | file << name << FRAGMENTPREFIX << FragmentNumber << prefix << suffix; | 
|---|
| 255 | std::ifstream input(file.str().c_str()); | 
|---|
| 256 | LOG(1, "INFO: Opening " << file.str() << " ... "); | 
|---|
| 257 | if (!ParseMatrix(input, skiplines, skipcolumns, i)) { | 
|---|
| 258 | input.close(); | 
|---|
| 259 | return false; | 
|---|
| 260 | } | 
|---|
| 261 | input.close(); | 
|---|
| 262 | delete[](FragmentNumber); | 
|---|
| 263 | } | 
|---|
| 264 | return true; | 
|---|
| 265 | }; | 
|---|
| 266 |  | 
|---|
| 267 | /** Allocates and resets the memory for a number \a MCounter of matrices. | 
|---|
| 268 | * \param **GivenHeader Header line for each matrix | 
|---|
| 269 | * \param MCounter number of matrices | 
|---|
| 270 | * \param *RCounter number of rows for each matrix | 
|---|
| 271 | * \param *CCounter number of columns for each matrix | 
|---|
| 272 | * \return Allocation successful | 
|---|
| 273 | */ | 
|---|
| 274 | bool MatrixContainer::AllocateMatrix(StringVector GivenHeader, int MCounter, IntVector RCounter, IntVector CCounter) | 
|---|
| 275 | { | 
|---|
| 276 | MatrixCounter = MCounter; | 
|---|
| 277 | Header.resize(MatrixCounter + 1); | 
|---|
| 278 | Matrix.resize(MatrixCounter + 1); // one more each for the total molecule | 
|---|
| 279 | RowCounter.resize(MatrixCounter + 1); | 
|---|
| 280 | ColumnCounter.resize(MatrixCounter + 1); | 
|---|
| 281 | for(int i=MatrixCounter+1;i--;) { | 
|---|
| 282 | Header[i] = GivenHeader[i]; | 
|---|
| 283 | RowCounter[i] = RCounter[i]; | 
|---|
| 284 | ColumnCounter[i] = CCounter[i]; | 
|---|
| 285 | if ((int)Matrix[i].size() <= RowCounter[i] + 2) | 
|---|
| 286 | Matrix[i].resize(RowCounter[i] + 1); | 
|---|
| 287 | for(int j=0;j<=RowCounter[i];j++) | 
|---|
| 288 | if ((int)Matrix[i][j].size() <= ColumnCounter[i]+1) | 
|---|
| 289 | Matrix[i][j].resize(ColumnCounter[i]); | 
|---|
| 290 | // allocation with 0 is guaranted by STL | 
|---|
| 291 | } | 
|---|
| 292 | return true; | 
|---|
| 293 | }; | 
|---|
| 294 |  | 
|---|
| 295 | /** Resets all values in MatrixContainer::Matrix. | 
|---|
| 296 | * \return true if successful | 
|---|
| 297 | */ | 
|---|
| 298 | bool MatrixContainer::ResetMatrix() | 
|---|
| 299 | { | 
|---|
| 300 | for(int i=MatrixCounter+1;i--;) | 
|---|
| 301 | for(int j=RowCounter[i]+1;j--;) | 
|---|
| 302 | for(int k=ColumnCounter[i];k--;) | 
|---|
| 303 | Matrix[i][j][k] = 0.; | 
|---|
| 304 | return true; | 
|---|
| 305 | }; | 
|---|
| 306 |  | 
|---|
| 307 | /** Scans all elements of MatrixContainer::Matrix for greatest absolute value. | 
|---|
| 308 | * \return greatest value of MatrixContainer::Matrix | 
|---|
| 309 | */ | 
|---|
| 310 | double MatrixContainer::FindMaxValue() | 
|---|
| 311 | { | 
|---|
| 312 | double max = Matrix[0][0][0]; | 
|---|
| 313 | for(int i=MatrixCounter+1;i--;) | 
|---|
| 314 | for(int j=RowCounter[i]+1;j--;) | 
|---|
| 315 | for(int k=ColumnCounter[i];k--;) | 
|---|
| 316 | if (fabs(Matrix[i][j][k]) > max) | 
|---|
| 317 | max = fabs(Matrix[i][j][k]); | 
|---|
| 318 | if (fabs(max) < MYEPSILON) | 
|---|
| 319 | max += MYEPSILON; | 
|---|
| 320 | return max; | 
|---|
| 321 | }; | 
|---|
| 322 |  | 
|---|
| 323 | /** Scans all elements of MatrixContainer::Matrix for smallest absolute value. | 
|---|
| 324 | * \return smallest value of MatrixContainer::Matrix | 
|---|
| 325 | */ | 
|---|
| 326 | double MatrixContainer::FindMinValue() | 
|---|
| 327 | { | 
|---|
| 328 | double min = Matrix[0][0][0]; | 
|---|
| 329 | for(int i=MatrixCounter+1;i--;) | 
|---|
| 330 | for(int j=RowCounter[i]+1;j--;) | 
|---|
| 331 | for(int k=ColumnCounter[i];k--;) | 
|---|
| 332 | if (fabs(Matrix[i][j][k]) < min) | 
|---|
| 333 | min = fabs(Matrix[i][j][k]); | 
|---|
| 334 | if (fabs(min) < MYEPSILON) | 
|---|
| 335 | min += MYEPSILON; | 
|---|
| 336 | return min; | 
|---|
| 337 | }; | 
|---|
| 338 |  | 
|---|
| 339 | /** Sets all values in the last of MatrixContainer::Matrix to \a value. | 
|---|
| 340 | * \param value reset value | 
|---|
| 341 | * \param skipcolumns skip initial columns | 
|---|
| 342 | * \return true if successful | 
|---|
| 343 | */ | 
|---|
| 344 | bool MatrixContainer::SetLastMatrix(double value, int skipcolumns) | 
|---|
| 345 | { | 
|---|
| 346 | for(int j=RowCounter[MatrixCounter]+1;j--;) | 
|---|
| 347 | for(int k=skipcolumns;k<ColumnCounter[MatrixCounter];k++) | 
|---|
| 348 | Matrix[MatrixCounter][j][k] = value; | 
|---|
| 349 | return true; | 
|---|
| 350 | }; | 
|---|
| 351 |  | 
|---|
| 352 | /** Sets all values in the last of MatrixContainer::Matrix to \a value. | 
|---|
| 353 | * \param **values matrix with each value (must have at least same dimensions!) | 
|---|
| 354 | * \param skipcolumns skip initial columns | 
|---|
| 355 | * \return true if successful | 
|---|
| 356 | */ | 
|---|
| 357 | bool MatrixContainer::SetLastMatrix(const MatrixArray &values, int skipcolumns) | 
|---|
| 358 | { | 
|---|
| 359 | for(int j=RowCounter[MatrixCounter]+1;j--;) | 
|---|
| 360 | for(int k=skipcolumns;k<ColumnCounter[MatrixCounter];k++) | 
|---|
| 361 | Matrix[MatrixCounter][j][k] = values[j][k]; | 
|---|
| 362 | return true; | 
|---|
| 363 | }; | 
|---|
| 364 |  | 
|---|
| 365 | /** Sums the entries with each factor and put into last element of \a ***Matrix. | 
|---|
| 366 | * Sums over "E"-terms to create the "F"-terms | 
|---|
| 367 | * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies. | 
|---|
| 368 | * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order | 
|---|
| 369 | * \param Order bond order | 
|---|
| 370 | * \return true if summing was successful | 
|---|
| 371 | */ | 
|---|
| 372 | bool MatrixContainer::SumSubManyBodyTerms(class MatrixContainer &MatrixValues, class KeySetsContainer &KeySets, int Order) | 
|---|
| 373 | { | 
|---|
| 374 | // go through each order | 
|---|
| 375 | for (int CurrentFragment=0;CurrentFragment<KeySets.FragmentsPerOrder[Order];CurrentFragment++) { | 
|---|
| 376 | //LOG(0, "Current Fragment is " << CurrentFragment << "/" << KeySets.OrderSet[Order][CurrentFragment] << "."); | 
|---|
| 377 | // then go per order through each suborder and pick together all the terms that contain this fragment | 
|---|
| 378 | for(int SubOrder=0;SubOrder<=Order;SubOrder++) { // go through all suborders up to the desired order | 
|---|
| 379 | for (int j=0;j<KeySets.FragmentsPerOrder[SubOrder];j++) { // go through all possible fragments of size suborder | 
|---|
| 380 | if (KeySets.Contains(KeySets.OrderSet[Order][CurrentFragment], KeySets.OrderSet[SubOrder][j])) { | 
|---|
| 381 | //LOG(0, "Current other fragment is " << j << "/" << KeySets.OrderSet[SubOrder][j] << "."); | 
|---|
| 382 | // if the fragment's indices are all in the current fragment | 
|---|
| 383 | for(int k=0;k<RowCounter[ KeySets.OrderSet[SubOrder][j] ];k++) { // go through all atoms in this fragment | 
|---|
| 384 | int m = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][k]; | 
|---|
| 385 | //LOG(0, "Current index is " << k << "/" << m << "."); | 
|---|
| 386 | if (m != -1) { // if it's not an added hydrogen | 
|---|
| 387 | for (int l=0;l<RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ];l++) { // look for the corresponding index in the current fragment | 
|---|
| 388 | //LOG(0, "Comparing " << m << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l] << "."); | 
|---|
| 389 | if (m == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l]) { | 
|---|
| 390 | m = l; | 
|---|
| 391 | break; | 
|---|
| 392 | } | 
|---|
| 393 | } | 
|---|
| 394 | //LOG(0, "Corresponding index in CurrentFragment is " << m << "."); | 
|---|
| 395 | if (m > RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) { | 
|---|
| 396 | ELOG(0, "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment]   << " current force index " << m << " is greater than " << RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!"); | 
|---|
| 397 | performCriticalExit(); | 
|---|
| 398 | return false; | 
|---|
| 399 | } | 
|---|
| 400 | if (Order == SubOrder) { // equal order is always copy from Energies | 
|---|
| 401 | for(int l=ColumnCounter[ KeySets.OrderSet[SubOrder][j] ];l--;) // then adds/subtract each column | 
|---|
| 402 | Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][l] += MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l]; | 
|---|
| 403 | } else { | 
|---|
| 404 | for(int l=ColumnCounter[ KeySets.OrderSet[SubOrder][j] ];l--;) | 
|---|
| 405 | Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][l] -= Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l]; | 
|---|
| 406 | } | 
|---|
| 407 | } | 
|---|
| 408 | //if ((ColumnCounter[ KeySets.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1)) | 
|---|
| 409 | //LOG(0, "Fragments[ KeySets.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySets.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " <<  Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1]); | 
|---|
| 410 | } | 
|---|
| 411 | } else { | 
|---|
| 412 | //LOG(0, "Fragment " << KeySets.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySets.OrderSet[Order][CurrentFragment] << "."); | 
|---|
| 413 | } | 
|---|
| 414 | } | 
|---|
| 415 | } | 
|---|
| 416 | //LOG(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]); | 
|---|
| 417 | } | 
|---|
| 418 |  | 
|---|
| 419 | return true; | 
|---|
| 420 | }; | 
|---|
| 421 |  | 
|---|
| 422 | /** Writes the summed total fragment terms \f$F_{ij}\f$ to file. | 
|---|
| 423 | * \param *name inputdir | 
|---|
| 424 | * \param *prefix prefix before \a EnergySuffix | 
|---|
| 425 | * \return file was written | 
|---|
| 426 | */ | 
|---|
| 427 | bool MatrixContainer::WriteTotalFragments(const std::string name, const std::string prefix) | 
|---|
| 428 | { | 
|---|
| 429 | ofstream output; | 
|---|
| 430 | char *FragmentNumber = NULL; | 
|---|
| 431 |  | 
|---|
| 432 | LOG(0, "Writing fragment files."); | 
|---|
| 433 | for(int i=0;i<MatrixCounter;i++) { | 
|---|
| 434 | stringstream line; | 
|---|
| 435 | FragmentNumber = FixedDigitNumber(MatrixCounter, i); | 
|---|
| 436 | line << name << FRAGMENTPREFIX << FragmentNumber << "/" << prefix; | 
|---|
| 437 | delete[](FragmentNumber); | 
|---|
| 438 | output.open(line.str().c_str(), ios::out); | 
|---|
| 439 | if (output == NULL) { | 
|---|
| 440 | ELOG(0, "MatrixContainer::WriteTotalFragments: Unable to open output energy file " << line.str() << "!"); | 
|---|
| 441 | performCriticalExit(); | 
|---|
| 442 | return false; | 
|---|
| 443 | } | 
|---|
| 444 | output << Header[i] << endl; | 
|---|
| 445 | for(int j=0;j<RowCounter[i];j++) { | 
|---|
| 446 | for(int k=0;k<ColumnCounter[i];k++) | 
|---|
| 447 | output << scientific << Matrix[i][j][k] << "\t"; | 
|---|
| 448 | output << endl; | 
|---|
| 449 | } | 
|---|
| 450 | output.close(); | 
|---|
| 451 | } | 
|---|
| 452 | return true; | 
|---|
| 453 | }; | 
|---|
| 454 |  | 
|---|
| 455 | /** Writes the summed total values in the last matrix to file. | 
|---|
| 456 | * \param *name inputdir | 
|---|
| 457 | * \param *prefix prefix | 
|---|
| 458 | * \param *suffix suffix | 
|---|
| 459 | * \return file was written | 
|---|
| 460 | */ | 
|---|
| 461 | bool MatrixContainer::WriteLastMatrix(const std::string name, const std::string prefix, const std::string suffix) | 
|---|
| 462 | { | 
|---|
| 463 | ofstream output; | 
|---|
| 464 | stringstream line; | 
|---|
| 465 |  | 
|---|
| 466 | LOG(0, "Writing matrix values of " << suffix << "."); | 
|---|
| 467 | line << name << prefix << suffix; | 
|---|
| 468 | output.open(line.str().c_str(), ios::out); | 
|---|
| 469 | if (output == NULL) { | 
|---|
| 470 | ELOG(0, "MatrixContainer::WriteLastMatrix: Unable to open output matrix file " << line.str() << "!"); | 
|---|
| 471 | performCriticalExit(); | 
|---|
| 472 | return false; | 
|---|
| 473 | } | 
|---|
| 474 | output << Header[MatrixCounter] << endl; | 
|---|
| 475 | for(int j=0;j<RowCounter[MatrixCounter];j++) { | 
|---|
| 476 | for(int k=0;k<ColumnCounter[MatrixCounter];k++) | 
|---|
| 477 | output << scientific << Matrix[MatrixCounter][j][k] << "\t"; | 
|---|
| 478 | output << endl; | 
|---|
| 479 | } | 
|---|
| 480 | output.close(); | 
|---|
| 481 | return true; | 
|---|
| 482 | }; | 
|---|
| 483 |  | 
|---|
| 484 | /** Comparison operator for class MatrixContainer. | 
|---|
| 485 | * | 
|---|
| 486 | * @param other instance to compare to | 
|---|
| 487 | * @return true - both instances are the same in each member variable. | 
|---|
| 488 | */ | 
|---|
| 489 | bool MatrixContainer::operator==(const MatrixContainer &other) const | 
|---|
| 490 | { | 
|---|
| 491 | // compare matrices | 
|---|
| 492 | if (Matrix != other.Matrix) | 
|---|
| 493 | return false; | 
|---|
| 494 | // compare Indices | 
|---|
| 495 | if (Indices != other.Indices) | 
|---|
| 496 | return false; | 
|---|
| 497 | // compare Headers | 
|---|
| 498 | if (Header != other.Header) | 
|---|
| 499 | return false; | 
|---|
| 500 | // compare MatrixCounter | 
|---|
| 501 | if (MatrixCounter != other.MatrixCounter) | 
|---|
| 502 | return false; | 
|---|
| 503 | // compare RowCounter | 
|---|
| 504 | if (RowCounter != other.RowCounter) | 
|---|
| 505 | return false; | 
|---|
| 506 | // compare ColumnCounter | 
|---|
| 507 | if (ColumnCounter != other.ColumnCounter) | 
|---|
| 508 | return false; | 
|---|
| 509 | return true; | 
|---|
| 510 | } | 
|---|
| 511 |  | 
|---|
| 512 | std::ostream & operator << (std::ostream &ost, const MatrixContainer &m) | 
|---|
| 513 | { | 
|---|
| 514 | for (int i=0;i<=m.MatrixCounter;++i) { | 
|---|
| 515 | ost << "Printing matrix " << i << ":" << std::endl; | 
|---|
| 516 | for (int j=0;j<=m.RowCounter[i];++j) { | 
|---|
| 517 | for (int k=0;k<m.ColumnCounter[i];++k) { | 
|---|
| 518 | ost << m.Matrix[i][j][k] << " "; | 
|---|
| 519 | } | 
|---|
| 520 | ost << std::endl; | 
|---|
| 521 | } | 
|---|
| 522 | } | 
|---|
| 523 | ost << std::endl; | 
|---|
| 524 | return ost; | 
|---|
| 525 | } | 
|---|
| 526 |  | 
|---|