| [a9b86d] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
| [0aa122] | 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| [a9b86d] | 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 | */ | 
|---|
| [9758f7] | 37 | MatrixContainer::MatrixContainer() | 
|---|
| [a9b86d] | 38 | { | 
|---|
| [9758f7] | 39 | Header.resize(1); | 
|---|
|  | 40 | RowCounter.resize(1); | 
|---|
|  | 41 | ColumnCounter.resize(1); | 
|---|
| [a9b86d] | 42 | ColumnCounter[0] = -1; | 
|---|
| [9758f7] | 43 | MatrixCounter = 0; | 
|---|
| [a9b86d] | 44 | }; | 
|---|
|  | 45 |  | 
|---|
|  | 46 | /** Destructor of MatrixContainer class. | 
|---|
|  | 47 | */ | 
|---|
| [9758f7] | 48 | MatrixContainer::~MatrixContainer() | 
|---|
|  | 49 | {} | 
|---|
| [a9b86d] | 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 | */ | 
|---|
| [9758f7] | 56 | bool MatrixContainer::InitialiseIndices(class MatrixContainer *_container) | 
|---|
| [a9b86d] | 57 | { | 
|---|
| [9758f7] | 58 | if (_container == NULL) { | 
|---|
| [44f53e] | 59 | LOG(3, "INFO: Initialising indices with trivial mapping."); | 
|---|
| [9758f7] | 60 | Indices.resize(MatrixCounter + 1); | 
|---|
| [a9b86d] | 61 | for(int i=MatrixCounter+1;i--;) { | 
|---|
| [9758f7] | 62 | Indices[i].resize(RowCounter[i]); | 
|---|
| [a9b86d] | 63 | for(int j=RowCounter[i];j--;) | 
|---|
|  | 64 | Indices[i][j] = j; | 
|---|
|  | 65 | } | 
|---|
|  | 66 | } else { | 
|---|
| [47d041] | 67 | std::stringstream output; | 
|---|
| [9758f7] | 68 | if (MatrixCounter != _container->MatrixCounter) | 
|---|
| [a9b86d] | 69 | return false; | 
|---|
| [9758f7] | 70 | Indices.resize(MatrixCounter + 1); | 
|---|
| [a9b86d] | 71 | for(int i=MatrixCounter+1;i--;) { | 
|---|
| [9758f7] | 72 | if (RowCounter[i] != _container->RowCounter[i]) | 
|---|
| [a9b86d] | 73 | return false; | 
|---|
| [9758f7] | 74 | Indices[i].resize(_container->RowCounter[i]); | 
|---|
|  | 75 | for(int j=_container->RowCounter[i];j--;) { | 
|---|
|  | 76 | Indices[i][j] = _container->Indices[i][j]; | 
|---|
| [47d041] | 77 | output << Indices[i][j] << "\t"; | 
|---|
| [a9b86d] | 78 | } | 
|---|
|  | 79 | } | 
|---|
| [44f53e] | 80 | LOG(3, "INFO: Initialising indices from other MatrixContainer: " << output.str()); | 
|---|
| [a9b86d] | 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 | */ | 
|---|
| [9758f7] | 98 | bool MatrixContainer::ParseMatrix(std::istream &input, int skiplines, int skipcolumns, size_t MatrixNr) | 
|---|
| [a9b86d] | 99 | { | 
|---|
|  | 100 | stringstream line; | 
|---|
|  | 101 | string token; | 
|---|
|  | 102 | char filename[1023]; | 
|---|
|  | 103 |  | 
|---|
|  | 104 | if (input.fail()) { | 
|---|
| [47d041] | 105 | ELOG(1, endl << "MatrixContainer::ParseMatrix: Unable to parse istream."); | 
|---|
| [a9b86d] | 106 | //performCriticalExit(); | 
|---|
|  | 107 | return false; | 
|---|
|  | 108 | } | 
|---|
|  | 109 |  | 
|---|
|  | 110 | // parse header | 
|---|
| [9758f7] | 111 | if (Header.size() <= MatrixNr) | 
|---|
|  | 112 | Header.resize(MatrixNr); | 
|---|
|  | 113 | Header[MatrixNr] = std::string(""); | 
|---|
|  | 114 | char dummy[1024]; | 
|---|
| [a9b86d] | 115 | for (int m=skiplines+1;m--;) | 
|---|
| [9758f7] | 116 | input.getline(dummy, 1023); | 
|---|
|  | 117 | line.str(dummy); | 
|---|
| [a9b86d] | 118 | for(int k=skipcolumns;k--;) | 
|---|
|  | 119 | line >> Header[MatrixNr]; | 
|---|
| [44f53e] | 120 | LOG(3, "INFO: Header of Matrix " << MatrixNr << " :" << line.str()); | 
|---|
| [9758f7] | 121 |  | 
|---|
|  | 122 | // scan header for number of columns | 
|---|
|  | 123 | if (ColumnCounter.size() <= MatrixNr) | 
|---|
|  | 124 | ColumnCounter.resize(MatrixNr); | 
|---|
| [a9b86d] | 125 | ColumnCounter[MatrixNr]=0; | 
|---|
|  | 126 | while ( getline(line,token, '\t') ) { | 
|---|
|  | 127 | if (token.length() > 0) | 
|---|
|  | 128 | ColumnCounter[MatrixNr]++; | 
|---|
|  | 129 | } | 
|---|
| [44f53e] | 130 | LOG(3, "INFO: "+line.str()); | 
|---|
|  | 131 | LOG(4, "INFO: ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << "."); | 
|---|
| [a9b86d] | 132 | if (ColumnCounter[MatrixNr] == 0) { | 
|---|
| [47d041] | 133 | ELOG(0, "ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << " from ostream."); | 
|---|
| [a9b86d] | 134 | performCriticalExit(); | 
|---|
|  | 135 | } | 
|---|
|  | 136 |  | 
|---|
|  | 137 | // scan rest for number of rows/lines | 
|---|
| [9758f7] | 138 | if (RowCounter.size() <= MatrixNr) | 
|---|
|  | 139 | RowCounter.resize(MatrixNr); | 
|---|
| [a9b86d] | 140 | RowCounter[MatrixNr]=-1;    // counts one line too much | 
|---|
|  | 141 | while (!input.eof()) { | 
|---|
|  | 142 | input.getline(filename, 1023); | 
|---|
| [44f53e] | 143 | LOG(3, "INFO: Comparing: " << strncmp(filename,"MeanForce",9)); | 
|---|
| [a9b86d] | 144 | RowCounter[MatrixNr]++; // then line was not last MeanForce | 
|---|
|  | 145 | if (strncmp(filename,"MeanForce", 9) == 0) { | 
|---|
|  | 146 | break; | 
|---|
|  | 147 | } | 
|---|
|  | 148 | } | 
|---|
| [44f53e] | 149 | LOG(4, "INFO: RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from input stream."); | 
|---|
| [a9b86d] | 150 | if (RowCounter[MatrixNr] == 0) { | 
|---|
| [44f53e] | 151 | ELOG(0, "INFO: RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from input stream."); | 
|---|
| [a9b86d] | 152 | performCriticalExit(); | 
|---|
|  | 153 | } | 
|---|
|  | 154 |  | 
|---|
|  | 155 | // allocate matrix if it's not zero dimension in one direction | 
|---|
| [9758f7] | 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++) | 
|---|
| [a9b86d] | 165 | Matrix[MatrixNr][j][k] = 0; | 
|---|
|  | 166 | } | 
|---|
|  | 167 | } else { | 
|---|
| [9758f7] | 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); | 
|---|
| [44f53e] | 178 | LOG(3, "INFO: Header: " << line.str()); | 
|---|
| [9758f7] | 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); | 
|---|
| [47d041] | 184 | std::stringstream lines(filename); | 
|---|
|  | 185 | std::stringstream output; | 
|---|
|  | 186 | output << "INFO: Matrix at level " << j << ":";// << filename << endl; | 
|---|
| [9758f7] | 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]; | 
|---|
| [47d041] | 191 | output << " " << std::setprecision(2) << Matrix[MatrixNr][j][k] << endl; | 
|---|
| [9758f7] | 192 | } | 
|---|
| [44f53e] | 193 | LOG(3, output.str()); | 
|---|
| [a9b86d] | 194 | } | 
|---|
| [9758f7] | 195 |  | 
|---|
| [a9b86d] | 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 | */ | 
|---|
| [9758f7] | 217 | bool MatrixContainer::ParseFragmentMatrix(const std::string name, const std::string prefix, std::string suffix, int skiplines, int skipcolumns) | 
|---|
| [a9b86d] | 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()) { | 
|---|
| [44f53e] | 230 | ELOG(1, "MatrixContainer::ParseFragmentMatrix: Unable to open " << file.str() << ", is the directory correct?"); | 
|---|
| [a9b86d] | 231 | return false; | 
|---|
|  | 232 | } | 
|---|
|  | 233 | while (!input.eof()) { | 
|---|
|  | 234 | input.getline(filename, 1023); | 
|---|
|  | 235 | stringstream zeile(filename); | 
|---|
|  | 236 | MatrixCounter++; | 
|---|
|  | 237 | } | 
|---|
|  | 238 | input.close(); | 
|---|
| [44f53e] | 239 | LOG(2, "INFO: Determined " << MatrixCounter << " fragments."); | 
|---|
| [a9b86d] | 240 |  | 
|---|
| [44f53e] | 241 | LOG(1, "STATUS: Parsing through each fragment and retrieving " << prefix << suffix << "."); | 
|---|
| [9758f7] | 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); | 
|---|
| [a9b86d] | 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()); | 
|---|
| [44f53e] | 256 | LOG(2, "INFO: Opening " << file.str() << " ... "); | 
|---|
| [a9b86d] | 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 | */ | 
|---|
| [9758f7] | 274 | bool MatrixContainer::AllocateMatrix(StringVector GivenHeader, int MCounter, IntVector RCounter, IntVector CCounter) | 
|---|
| [a9b86d] | 275 | { | 
|---|
|  | 276 | MatrixCounter = MCounter; | 
|---|
| [9758f7] | 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); | 
|---|
| [a9b86d] | 281 | for(int i=MatrixCounter+1;i--;) { | 
|---|
| [9758f7] | 282 | Header[i] = GivenHeader[i]; | 
|---|
| [a9b86d] | 283 | RowCounter[i] = RCounter[i]; | 
|---|
|  | 284 | ColumnCounter[i] = CCounter[i]; | 
|---|
| [c9cafa] | 285 | if ((int)Matrix[i].size() <= RowCounter[i] + 2) | 
|---|
| [9758f7] | 286 | Matrix[i].resize(RowCounter[i] + 1); | 
|---|
|  | 287 | for(int j=0;j<=RowCounter[i];j++) | 
|---|
| [c9cafa] | 288 | if ((int)Matrix[i][j].size() <= ColumnCounter[i]+1) | 
|---|
| [9758f7] | 289 | Matrix[i][j].resize(ColumnCounter[i]); | 
|---|
|  | 290 | // allocation with 0 is guaranted by STL | 
|---|
| [a9b86d] | 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 | */ | 
|---|
| [9758f7] | 357 | bool MatrixContainer::SetLastMatrix(const MatrixArray &values, int skipcolumns) | 
|---|
| [a9b86d] | 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++) { | 
|---|
| [47d041] | 376 | //LOG(0, "Current Fragment is " << CurrentFragment << "/" << KeySets.OrderSet[Order][CurrentFragment] << "."); | 
|---|
| [a9b86d] | 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])) { | 
|---|
| [47d041] | 381 | //LOG(0, "Current other fragment is " << j << "/" << KeySets.OrderSet[SubOrder][j] << "."); | 
|---|
| [a9b86d] | 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]; | 
|---|
| [47d041] | 385 | //LOG(0, "Current index is " << k << "/" << m << "."); | 
|---|
| [a9b86d] | 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 | 
|---|
| [47d041] | 388 | //LOG(0, "Comparing " << m << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l] << "."); | 
|---|
| [a9b86d] | 389 | if (m == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l]) { | 
|---|
|  | 390 | m = l; | 
|---|
|  | 391 | break; | 
|---|
|  | 392 | } | 
|---|
|  | 393 | } | 
|---|
| [47d041] | 394 | //LOG(0, "Corresponding index in CurrentFragment is " << m << "."); | 
|---|
| [a9b86d] | 395 | if (m > RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) { | 
|---|
| [47d041] | 396 | ELOG(0, "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment]   << " current force index " << m << " is greater than " << RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!"); | 
|---|
| [a9b86d] | 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)) | 
|---|
| [47d041] | 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]); | 
|---|
| [a9b86d] | 410 | } | 
|---|
|  | 411 | } else { | 
|---|
| [47d041] | 412 | //LOG(0, "Fragment " << KeySets.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySets.OrderSet[Order][CurrentFragment] << "."); | 
|---|
| [a9b86d] | 413 | } | 
|---|
|  | 414 | } | 
|---|
|  | 415 | } | 
|---|
| [47d041] | 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]); | 
|---|
| [a9b86d] | 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 | */ | 
|---|
| [9758f7] | 427 | bool MatrixContainer::WriteTotalFragments(const std::string name, const std::string prefix) | 
|---|
| [a9b86d] | 428 | { | 
|---|
|  | 429 | ofstream output; | 
|---|
|  | 430 | char *FragmentNumber = NULL; | 
|---|
|  | 431 |  | 
|---|
| [44f53e] | 432 | LOG(1, "STATUS: Writing fragment files."); | 
|---|
| [a9b86d] | 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) { | 
|---|
| [47d041] | 440 | ELOG(0, "MatrixContainer::WriteTotalFragments: Unable to open output energy file " << line.str() << "!"); | 
|---|
| [a9b86d] | 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 | */ | 
|---|
| [9758f7] | 461 | bool MatrixContainer::WriteLastMatrix(const std::string name, const std::string prefix, const std::string suffix) | 
|---|
| [a9b86d] | 462 | { | 
|---|
|  | 463 | ofstream output; | 
|---|
|  | 464 | stringstream line; | 
|---|
|  | 465 |  | 
|---|
| [44f53e] | 466 | LOG(1, "STATUS: Writing matrix values of " << suffix << "."); | 
|---|
| [a9b86d] | 467 | line << name << prefix << suffix; | 
|---|
|  | 468 | output.open(line.str().c_str(), ios::out); | 
|---|
|  | 469 | if (output == NULL) { | 
|---|
| [47d041] | 470 | ELOG(0, "MatrixContainer::WriteLastMatrix: Unable to open output matrix file " << line.str() << "!"); | 
|---|
| [a9b86d] | 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 |  | 
|---|
| [1b145f] | 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 |  | 
|---|
| [a9b86d] | 512 | std::ostream & operator << (std::ostream &ost, const MatrixContainer &m) | 
|---|
|  | 513 | { | 
|---|
|  | 514 | for (int i=0;i<=m.MatrixCounter;++i) { | 
|---|
| [44f53e] | 515 | ost << "Matrix " << i << " in MatrixContainer:" << std::endl; | 
|---|
| [a9b86d] | 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 | } | 
|---|
| [1b145f] | 526 |  | 
|---|