| 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(3, "INFO: 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(3, "INFO: 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 a temporary matrix
 | 
|---|
| 91 |  *    -# loop over found column and row counts and parse in each entry
 | 
|---|
| 92 |  *    -# use MatrixContainer::AddMatrix() to add the parsed matrix to the internal
 | 
|---|
| 93 |  * \param &input input stream
 | 
|---|
| 94 |  * \param skiplines number of inital lines to skip
 | 
|---|
| 95 |  * \param skiplines number of inital columns to skip
 | 
|---|
| 96 |  * \param MatrixNr index number in Matrix array to parse into
 | 
|---|
| 97 |  * \return parsing successful
 | 
|---|
| 98 |  */
 | 
|---|
| 99 | bool MatrixContainer::ParseMatrix(std::istream &input, int skiplines, int skipcolumns, size_t MatrixNr)
 | 
|---|
| 100 | {
 | 
|---|
| 101 |   stringstream line;
 | 
|---|
| 102 |   string token;
 | 
|---|
| 103 |   char filename[1023];
 | 
|---|
| 104 | 
 | 
|---|
| 105 |   if (input.fail()) {
 | 
|---|
| 106 |     ELOG(1, endl << "MatrixContainer::ParseMatrix: Unable to parse istream.");
 | 
|---|
| 107 |     //performCriticalExit();
 | 
|---|
| 108 |     return false;
 | 
|---|
| 109 |   }
 | 
|---|
| 110 | 
 | 
|---|
| 111 |   // parse header
 | 
|---|
| 112 |   std::string header;
 | 
|---|
| 113 |   char dummy[1024];
 | 
|---|
| 114 |   for (int m=skiplines+1;m--;)
 | 
|---|
| 115 |     input.getline(dummy, 1023);
 | 
|---|
| 116 |   line.str(dummy);
 | 
|---|
| 117 |   for(int k=skipcolumns;k--;)
 | 
|---|
| 118 |     line >> header;
 | 
|---|
| 119 |   LOG(3, "INFO: Header of Matrix " << MatrixNr << " :" << line.str());
 | 
|---|
| 120 | 
 | 
|---|
| 121 |   // scan header for number of columns
 | 
|---|
| 122 |   size_t ColumnCounter = 0;
 | 
|---|
| 123 |   while ( getline(line,token, '\t') ) {
 | 
|---|
| 124 |     if (token.length() > 0)
 | 
|---|
| 125 |       ColumnCounter++;
 | 
|---|
| 126 |   }
 | 
|---|
| 127 |   LOG(3, "INFO: "+line.str());
 | 
|---|
| 128 | 
 | 
|---|
| 129 |   // scan rest for number of rows/lines
 | 
|---|
| 130 |   size_t RowCounter = -1;
 | 
|---|
| 131 |   while (!input.eof()) {
 | 
|---|
| 132 |     input.getline(filename, 1023);
 | 
|---|
| 133 |     LOG(3, "INFO: Comparing: " << strncmp(filename,"MeanForce",9));
 | 
|---|
| 134 |     RowCounter++; // then line was not last MeanForce
 | 
|---|
| 135 |     if (strncmp(filename,"MeanForce", 9) == 0) {
 | 
|---|
| 136 |       break;
 | 
|---|
| 137 |     }
 | 
|---|
| 138 |   }
 | 
|---|
| 139 | 
 | 
|---|
| 140 |   // allocate temporary matrix
 | 
|---|
| 141 |   MatrixArray temp_matrix;
 | 
|---|
| 142 |   temp_matrix.resize(RowCounter);
 | 
|---|
| 143 |   for(MatrixArray::iterator iter = temp_matrix.begin(); iter != temp_matrix.end(); ++iter)
 | 
|---|
| 144 |     (*iter).resize(ColumnCounter);
 | 
|---|
| 145 | 
 | 
|---|
| 146 |   // parse in each entry for this matrix
 | 
|---|
| 147 |   input.clear();
 | 
|---|
| 148 |   input.seekg(ios::beg);
 | 
|---|
| 149 |   for (int m=skiplines+1;m--;)
 | 
|---|
| 150 |     input.getline(dummy, 1023);    // skip header
 | 
|---|
| 151 |   line.str(dummy);
 | 
|---|
| 152 |   LOG(3, "INFO: Header: " << line.str());
 | 
|---|
| 153 |   for(int k=skipcolumns;k--;)  // skip columns in header too
 | 
|---|
| 154 |     line >> filename;
 | 
|---|
| 155 |   header = line.str();
 | 
|---|
| 156 |   for(size_t j=0;j<RowCounter;j++) {
 | 
|---|
| 157 |     input.getline(filename, 1023);
 | 
|---|
| 158 |     std::stringstream lines(filename);
 | 
|---|
| 159 |     std::stringstream output;
 | 
|---|
| 160 |     output << "INFO: Matrix at level " << j << ":";// << filename << endl;
 | 
|---|
| 161 |     for(int k=skipcolumns;k--;)
 | 
|---|
| 162 |       lines >> filename;
 | 
|---|
| 163 |     for(size_t k=0;(k<ColumnCounter) && (!lines.eof());k++) {
 | 
|---|
| 164 |       lines >> temp_matrix[j][k];
 | 
|---|
| 165 |       output << " " << std::setprecision(2) << temp_matrix[j][k] << endl;
 | 
|---|
| 166 |     }
 | 
|---|
| 167 |     LOG(3, output.str());
 | 
|---|
| 168 |   }
 | 
|---|
| 169 | 
 | 
|---|
| 170 |   // finally add the matrix
 | 
|---|
| 171 |   return AddMatrix(header, temp_matrix, MatrixNr);
 | 
|---|
| 172 | }
 | 
|---|
| 173 | 
 | 
|---|
| 174 | /** Adds a matrix at position \a MatrixNr to MatrixContainer::Matrix.
 | 
|---|
| 175 |  *
 | 
|---|
| 176 |  * @param header header to add for this matrix
 | 
|---|
| 177 |  * @param matrix to add
 | 
|---|
| 178 |  * @param MatrixNr position in MatrixContainer::Matrix.
 | 
|---|
| 179 |  * @return true - insertion ok, false - invalid matrix
 | 
|---|
| 180 |  */
 | 
|---|
| 181 | bool MatrixContainer::AddMatrix(const std::string &header, const MatrixArray &matrix, size_t MatrixNr)
 | 
|---|
| 182 | {
 | 
|---|
| 183 |   // make some pre-checks
 | 
|---|
| 184 |   if (header.size() == 0)
 | 
|---|
| 185 |     ELOG(2, "The given header of the matrix to add is empty.");
 | 
|---|
| 186 |   if (matrix.size() == 0) {
 | 
|---|
| 187 |     ELOG(1, "RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from input stream.");
 | 
|---|
| 188 |     return false;
 | 
|---|
| 189 |   }
 | 
|---|
| 190 |   if (matrix[0].size() == 0) {
 | 
|---|
| 191 |     ELOG(1, "ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << " from ostream.");
 | 
|---|
| 192 |     return false;
 | 
|---|
| 193 |   }
 | 
|---|
| 194 | 
 | 
|---|
| 195 |   // add header
 | 
|---|
| 196 |   if (Header.size() <= MatrixNr)
 | 
|---|
| 197 |     Header.resize(MatrixNr+1);
 | 
|---|
| 198 |   Header[MatrixNr] = header;
 | 
|---|
| 199 | 
 | 
|---|
| 200 |   // row count
 | 
|---|
| 201 |   if (RowCounter.size() <= MatrixNr)
 | 
|---|
| 202 |     RowCounter.resize(MatrixNr+1);
 | 
|---|
| 203 |   RowCounter[MatrixNr] = matrix.size();
 | 
|---|
| 204 |   LOG(4, "INFO: RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from input stream.");
 | 
|---|
| 205 | 
 | 
|---|
| 206 |   // column count
 | 
|---|
| 207 |   if (ColumnCounter.size() <= MatrixNr)
 | 
|---|
| 208 |     ColumnCounter.resize(MatrixNr+1);
 | 
|---|
| 209 |   ColumnCounter[MatrixNr] = matrix[0].size();
 | 
|---|
| 210 |   LOG(4, "INFO: ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << ".");
 | 
|---|
| 211 | 
 | 
|---|
| 212 |   // allocate matrix ...
 | 
|---|
| 213 |   if (Matrix.size() <= MatrixNr)
 | 
|---|
| 214 |     Matrix.resize(MatrixNr+1);
 | 
|---|
| 215 |   MatrixCounter = Matrix.size()-1;
 | 
|---|
| 216 |   Matrix[MatrixNr].resize(RowCounter[MatrixNr] + 1);
 | 
|---|
| 217 |   for(int j=0;j<=RowCounter[MatrixNr];++j)
 | 
|---|
| 218 |     Matrix[MatrixNr][j].resize(ColumnCounter[MatrixNr]+1);
 | 
|---|
| 219 | 
 | 
|---|
| 220 |   // .. and copy values
 | 
|---|
| 221 |   for(int j=0;j<RowCounter[MatrixNr];++j)
 | 
|---|
| 222 |     for(int k=0;k<ColumnCounter[MatrixNr];++k)
 | 
|---|
| 223 |       Matrix[MatrixNr][j][k] = matrix[j][k];
 | 
|---|
| 224 |   // reset last column
 | 
|---|
| 225 |   for(int j=0;j<RowCounter[MatrixNr];++j)
 | 
|---|
| 226 |     Matrix[MatrixNr][j][ ColumnCounter[MatrixNr] ] = 0.;
 | 
|---|
| 227 |   // reset last row
 | 
|---|
| 228 |   for(int k=0;k<=ColumnCounter[MatrixNr];++k)
 | 
|---|
| 229 |     Matrix[MatrixNr][ RowCounter[MatrixNr] ][ k ] = 0.;
 | 
|---|
| 230 | 
 | 
|---|
| 231 |   return true;
 | 
|---|
| 232 | }
 | 
|---|
| 233 | 
 | 
|---|
| 234 | /** Parsing a number of matrices.
 | 
|---|
| 235 |  * -# First, count the number of matrices by counting lines in KEYSETFILE
 | 
|---|
| 236 |  * -# Then,
 | 
|---|
| 237 |  *    -# construct the fragment number
 | 
|---|
| 238 |  *    -# open the matrix file
 | 
|---|
| 239 |  *    -# skip some lines (\a skiplines)
 | 
|---|
| 240 |  *    -# scan header lines for number of columns
 | 
|---|
| 241 |  *    -# scan lines for number of rows
 | 
|---|
| 242 |  *    -# allocate matrix
 | 
|---|
| 243 |  *    -# loop over found column and row counts and parse in each entry
 | 
|---|
| 244 |  * -# Finally, allocate one additional matrix (\a MatrixCounter) containing combined or temporary values
 | 
|---|
| 245 |  * \param *name directory with files
 | 
|---|
| 246 |  * \param *prefix prefix of each matrix file
 | 
|---|
| 247 |  * \param *suffix suffix of each matrix file
 | 
|---|
| 248 |  * \param skiplines number of inital lines to skip
 | 
|---|
| 249 |  * \param skiplines number of inital columns to skip
 | 
|---|
| 250 |  * \return parsing successful
 | 
|---|
| 251 |  */
 | 
|---|
| 252 | bool MatrixContainer::ParseFragmentMatrix(const std::string name, const std::string prefix, std::string suffix, int skiplines, int skipcolumns)
 | 
|---|
| 253 | {
 | 
|---|
| 254 |   char filename[1023];
 | 
|---|
| 255 |   ifstream input;
 | 
|---|
| 256 |   char *FragmentNumber = NULL;
 | 
|---|
| 257 |   stringstream file;
 | 
|---|
| 258 |   string token;
 | 
|---|
| 259 | 
 | 
|---|
| 260 |   // count the number of matrices
 | 
|---|
| 261 |   MatrixCounter = -1; // we count one too much
 | 
|---|
| 262 |   file << name << FRAGMENTPREFIX << KEYSETFILE;
 | 
|---|
| 263 |   input.open(file.str().c_str(), ios::in);
 | 
|---|
| 264 |   if (input.bad()) {
 | 
|---|
| 265 |     ELOG(1, "MatrixContainer::ParseFragmentMatrix: Unable to open " << file.str() << ", is the directory correct?");
 | 
|---|
| 266 |     return false;
 | 
|---|
| 267 |   }
 | 
|---|
| 268 |   while (!input.eof()) {
 | 
|---|
| 269 |     input.getline(filename, 1023);
 | 
|---|
| 270 |     stringstream zeile(filename);
 | 
|---|
| 271 |     MatrixCounter++;
 | 
|---|
| 272 |   }
 | 
|---|
| 273 |   input.close();
 | 
|---|
| 274 |   LOG(2, "INFO: Determined " << MatrixCounter << " fragments.");
 | 
|---|
| 275 | 
 | 
|---|
| 276 |   LOG(1, "STATUS: Parsing through each fragment and retrieving " << prefix << suffix << ".");
 | 
|---|
| 277 |   Header.clear();
 | 
|---|
| 278 |   Matrix.clear();
 | 
|---|
| 279 |   RowCounter.clear();
 | 
|---|
| 280 |   ColumnCounter.clear();
 | 
|---|
| 281 |   Header.resize(MatrixCounter + 1); // one more each for the total molecule
 | 
|---|
| 282 |   Matrix.resize(MatrixCounter + 1); // one more each for the total molecule
 | 
|---|
| 283 |   RowCounter.resize(MatrixCounter + 1);
 | 
|---|
| 284 |   ColumnCounter.resize(MatrixCounter + 1);
 | 
|---|
| 285 |   for(int i=0; i < MatrixCounter;i++) {
 | 
|---|
| 286 |     // open matrix file
 | 
|---|
| 287 |     FragmentNumber = FixedDigitNumber(MatrixCounter, i);
 | 
|---|
| 288 |     file.str(" ");
 | 
|---|
| 289 |     file << name << FRAGMENTPREFIX << FragmentNumber << prefix << suffix;
 | 
|---|
| 290 |     std::ifstream input(file.str().c_str());
 | 
|---|
| 291 |     LOG(2, "INFO: Opening " << file.str() << " ... ");
 | 
|---|
| 292 |     if (!ParseMatrix(input, skiplines, skipcolumns, i)) {
 | 
|---|
| 293 |       input.close();
 | 
|---|
| 294 |       return false;
 | 
|---|
| 295 |     }
 | 
|---|
| 296 |     input.close();
 | 
|---|
| 297 |     delete[](FragmentNumber);
 | 
|---|
| 298 |   }
 | 
|---|
| 299 |   return true;
 | 
|---|
| 300 | };
 | 
|---|
| 301 | 
 | 
|---|
| 302 | /** Allocates and resets the memory for a number \a MCounter of matrices.
 | 
|---|
| 303 |  * \param **GivenHeader Header line for each matrix
 | 
|---|
| 304 |  * \param MCounter number of matrices
 | 
|---|
| 305 |  * \param *RCounter number of rows for each matrix
 | 
|---|
| 306 |  * \param *CCounter number of columns for each matrix
 | 
|---|
| 307 |  * \return Allocation successful
 | 
|---|
| 308 |  */
 | 
|---|
| 309 | bool MatrixContainer::AllocateMatrix(StringVector GivenHeader, int MCounter, IntVector RCounter, IntVector CCounter)
 | 
|---|
| 310 | {
 | 
|---|
| 311 |   MatrixCounter = MCounter;
 | 
|---|
| 312 |   Header.resize(MatrixCounter + 1);
 | 
|---|
| 313 |   Matrix.resize(MatrixCounter + 1); // one more each for the total molecule
 | 
|---|
| 314 |   RowCounter.resize(MatrixCounter + 1);
 | 
|---|
| 315 |   ColumnCounter.resize(MatrixCounter + 1);
 | 
|---|
| 316 |   for(int i=MatrixCounter+1;i--;) {
 | 
|---|
| 317 |     Header[i] = GivenHeader[i];
 | 
|---|
| 318 |     RowCounter[i] = RCounter[i];
 | 
|---|
| 319 |     ColumnCounter[i] = CCounter[i];
 | 
|---|
| 320 |     if ((int)Matrix[i].size() <= RowCounter[i] + 2)
 | 
|---|
| 321 |       Matrix[i].resize(RowCounter[i] + 1);
 | 
|---|
| 322 |     for(int j=0;j<=RowCounter[i];j++)
 | 
|---|
| 323 |       if ((int)Matrix[i][j].size() <= ColumnCounter[i]+1)
 | 
|---|
| 324 |         Matrix[i][j].resize(ColumnCounter[i]);
 | 
|---|
| 325 |     // allocation with 0 is guaranted by STL
 | 
|---|
| 326 |   }
 | 
|---|
| 327 |   return true;
 | 
|---|
| 328 | };
 | 
|---|
| 329 | 
 | 
|---|
| 330 | /** Resets all values in MatrixContainer::Matrix.
 | 
|---|
| 331 |  * \return true if successful
 | 
|---|
| 332 |  */
 | 
|---|
| 333 | bool MatrixContainer::ResetMatrix()
 | 
|---|
| 334 | {
 | 
|---|
| 335 |   for(int i=MatrixCounter+1;i--;)
 | 
|---|
| 336 |     for(int j=RowCounter[i]+1;j--;)
 | 
|---|
| 337 |       for(int k=ColumnCounter[i];k--;)
 | 
|---|
| 338 |         Matrix[i][j][k] = 0.;
 | 
|---|
| 339 |    return true;
 | 
|---|
| 340 | };
 | 
|---|
| 341 | 
 | 
|---|
| 342 | /** Scans all elements of MatrixContainer::Matrix for greatest absolute value.
 | 
|---|
| 343 |  * \return greatest value of MatrixContainer::Matrix
 | 
|---|
| 344 |  */
 | 
|---|
| 345 | double MatrixContainer::FindMaxValue()
 | 
|---|
| 346 | {
 | 
|---|
| 347 |   double max = Matrix[0][0][0];
 | 
|---|
| 348 |   for(int i=MatrixCounter+1;i--;)
 | 
|---|
| 349 |     for(int j=RowCounter[i]+1;j--;)
 | 
|---|
| 350 |       for(int k=ColumnCounter[i];k--;)
 | 
|---|
| 351 |         if (fabs(Matrix[i][j][k]) > max)
 | 
|---|
| 352 |           max = fabs(Matrix[i][j][k]);
 | 
|---|
| 353 |   if (fabs(max) < MYEPSILON)
 | 
|---|
| 354 |     max += MYEPSILON;
 | 
|---|
| 355 |  return max;
 | 
|---|
| 356 | };
 | 
|---|
| 357 | 
 | 
|---|
| 358 | /** Scans all elements of MatrixContainer::Matrix for smallest absolute value.
 | 
|---|
| 359 |  * \return smallest value of MatrixContainer::Matrix
 | 
|---|
| 360 |  */
 | 
|---|
| 361 | double MatrixContainer::FindMinValue()
 | 
|---|
| 362 | {
 | 
|---|
| 363 |   double min = Matrix[0][0][0];
 | 
|---|
| 364 |   for(int i=MatrixCounter+1;i--;)
 | 
|---|
| 365 |     for(int j=RowCounter[i]+1;j--;)
 | 
|---|
| 366 |       for(int k=ColumnCounter[i];k--;)
 | 
|---|
| 367 |         if (fabs(Matrix[i][j][k]) < min)
 | 
|---|
| 368 |           min = fabs(Matrix[i][j][k]);
 | 
|---|
| 369 |   if (fabs(min) < MYEPSILON)
 | 
|---|
| 370 |     min += MYEPSILON;
 | 
|---|
| 371 |   return min;
 | 
|---|
| 372 | };
 | 
|---|
| 373 | 
 | 
|---|
| 374 | /** Sets all values in the last of MatrixContainer::Matrix to \a value.
 | 
|---|
| 375 |  * \param value reset value
 | 
|---|
| 376 |  * \param skipcolumns skip initial columns
 | 
|---|
| 377 |  * \return true if successful
 | 
|---|
| 378 |  */
 | 
|---|
| 379 | bool MatrixContainer::SetLastMatrix(double value, int skipcolumns)
 | 
|---|
| 380 | {
 | 
|---|
| 381 |   for(int j=RowCounter[MatrixCounter]+1;j--;)
 | 
|---|
| 382 |     for(int k=skipcolumns;k<ColumnCounter[MatrixCounter];k++)
 | 
|---|
| 383 |       Matrix[MatrixCounter][j][k] = value;
 | 
|---|
| 384 |    return true;
 | 
|---|
| 385 | };
 | 
|---|
| 386 | 
 | 
|---|
| 387 | /** Sets all values in the last of MatrixContainer::Matrix to \a value.
 | 
|---|
| 388 |  * \param **values matrix with each value (must have at least same dimensions!)
 | 
|---|
| 389 |  * \param skipcolumns skip initial columns
 | 
|---|
| 390 |  * \return true if successful
 | 
|---|
| 391 |  */
 | 
|---|
| 392 | bool MatrixContainer::SetLastMatrix(const MatrixArray &values, int skipcolumns)
 | 
|---|
| 393 | {
 | 
|---|
| 394 |   for(int j=RowCounter[MatrixCounter]+1;j--;)
 | 
|---|
| 395 |     for(int k=skipcolumns;k<ColumnCounter[MatrixCounter];k++)
 | 
|---|
| 396 |       Matrix[MatrixCounter][j][k] = values[j][k];
 | 
|---|
| 397 |    return true;
 | 
|---|
| 398 | };
 | 
|---|
| 399 | 
 | 
|---|
| 400 | /** Sums the entries with each factor and put into last element of \a ***Matrix.
 | 
|---|
| 401 |  * Sums over "E"-terms to create the "F"-terms
 | 
|---|
| 402 |  * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
 | 
|---|
| 403 |  * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
 | 
|---|
| 404 |  * \param Order bond order
 | 
|---|
| 405 |  * \return true if summing was successful
 | 
|---|
| 406 |  */
 | 
|---|
| 407 | bool MatrixContainer::SumSubManyBodyTerms(class MatrixContainer &MatrixValues, class KeySetsContainer &KeySets, int Order)
 | 
|---|
| 408 | {
 | 
|---|
| 409 |   // go through each order
 | 
|---|
| 410 |   for (int CurrentFragment=0;CurrentFragment<KeySets.FragmentsPerOrder[Order];CurrentFragment++) {
 | 
|---|
| 411 |     //LOG(0, "Current Fragment is " << CurrentFragment << "/" << KeySets.OrderSet[Order][CurrentFragment] << ".");
 | 
|---|
| 412 |     // then go per order through each suborder and pick together all the terms that contain this fragment
 | 
|---|
| 413 |     for(int SubOrder=0;SubOrder<=Order;SubOrder++) { // go through all suborders up to the desired order
 | 
|---|
| 414 |       for (int j=0;j<KeySets.FragmentsPerOrder[SubOrder];j++) { // go through all possible fragments of size suborder
 | 
|---|
| 415 |         if (KeySets.Contains(KeySets.OrderSet[Order][CurrentFragment], KeySets.OrderSet[SubOrder][j])) {
 | 
|---|
| 416 |           //LOG(0, "Current other fragment is " << j << "/" << KeySets.OrderSet[SubOrder][j] << ".");
 | 
|---|
| 417 |           // if the fragment's indices are all in the current fragment
 | 
|---|
| 418 |           for(int k=0;k<RowCounter[ KeySets.OrderSet[SubOrder][j] ];k++) { // go through all atoms in this fragment
 | 
|---|
| 419 |             int m = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][k];
 | 
|---|
| 420 |             //LOG(0, "Current index is " << k << "/" << m << ".");
 | 
|---|
| 421 |             if (m != -1) { // if it's not an added hydrogen
 | 
|---|
| 422 |               for (int l=0;l<RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ];l++) { // look for the corresponding index in the current fragment
 | 
|---|
| 423 |                 //LOG(0, "Comparing " << m << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l] << ".");
 | 
|---|
| 424 |                 if (m == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l]) {
 | 
|---|
| 425 |                   m = l;
 | 
|---|
| 426 |                   break;
 | 
|---|
| 427 |                 }
 | 
|---|
| 428 |               }
 | 
|---|
| 429 |               //LOG(0, "Corresponding index in CurrentFragment is " << m << ".");
 | 
|---|
| 430 |               if (m > RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) {
 | 
|---|
| 431 |                 ELOG(0, "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment]   << " current force index " << m << " is greater than " << RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!");
 | 
|---|
| 432 |                 performCriticalExit();
 | 
|---|
| 433 |                 return false;
 | 
|---|
| 434 |               }
 | 
|---|
| 435 |               if (Order == SubOrder) { // equal order is always copy from Energies
 | 
|---|
| 436 |                 for(int l=ColumnCounter[ KeySets.OrderSet[SubOrder][j] ];l--;) // then adds/subtract each column
 | 
|---|
| 437 |                   Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][l] += MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
 | 
|---|
| 438 |               } else {
 | 
|---|
| 439 |                 for(int l=ColumnCounter[ KeySets.OrderSet[SubOrder][j] ];l--;)
 | 
|---|
| 440 |                   Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][l] -= Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
 | 
|---|
| 441 |               }
 | 
|---|
| 442 |             }
 | 
|---|
| 443 |             //if ((ColumnCounter[ KeySets.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1))
 | 
|---|
| 444 |              //LOG(0, "Fragments[ KeySets.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySets.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " <<  Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1]);
 | 
|---|
| 445 |           }
 | 
|---|
| 446 |         } else {
 | 
|---|
| 447 |           //LOG(0, "Fragment " << KeySets.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySets.OrderSet[Order][CurrentFragment] << ".");
 | 
|---|
| 448 |         }
 | 
|---|
| 449 |       }
 | 
|---|
| 450 |     }
 | 
|---|
| 451 |    //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]);
 | 
|---|
| 452 |   }
 | 
|---|
| 453 | 
 | 
|---|
| 454 |   return true;
 | 
|---|
| 455 | };
 | 
|---|
| 456 | 
 | 
|---|
| 457 | /** Writes the summed total fragment terms \f$F_{ij}\f$ to file.
 | 
|---|
| 458 |  * \param *name inputdir
 | 
|---|
| 459 |  * \param *prefix prefix before \a EnergySuffix
 | 
|---|
| 460 |  * \return file was written
 | 
|---|
| 461 |  */
 | 
|---|
| 462 | bool MatrixContainer::WriteTotalFragments(const std::string name, const std::string prefix)
 | 
|---|
| 463 | {
 | 
|---|
| 464 |   ofstream output;
 | 
|---|
| 465 |   char *FragmentNumber = NULL;
 | 
|---|
| 466 | 
 | 
|---|
| 467 |   LOG(1, "STATUS: Writing fragment files.");
 | 
|---|
| 468 |   for(int i=0;i<MatrixCounter;i++) {
 | 
|---|
| 469 |     stringstream line;
 | 
|---|
| 470 |     FragmentNumber = FixedDigitNumber(MatrixCounter, i);
 | 
|---|
| 471 |     line << name << FRAGMENTPREFIX << FragmentNumber << "/" << prefix;
 | 
|---|
| 472 |     delete[](FragmentNumber);
 | 
|---|
| 473 |     output.open(line.str().c_str(), ios::out);
 | 
|---|
| 474 |     if (output == NULL) {
 | 
|---|
| 475 |       ELOG(0, "MatrixContainer::WriteTotalFragments: Unable to open output energy file " << line.str() << "!");
 | 
|---|
| 476 |       performCriticalExit();
 | 
|---|
| 477 |       return false;
 | 
|---|
| 478 |     }
 | 
|---|
| 479 |     output << Header[i] << endl;
 | 
|---|
| 480 |     for(int j=0;j<RowCounter[i];j++) {
 | 
|---|
| 481 |       for(int k=0;k<ColumnCounter[i];k++)
 | 
|---|
| 482 |         output << scientific << Matrix[i][j][k] << "\t";
 | 
|---|
| 483 |       output << endl;
 | 
|---|
| 484 |     }
 | 
|---|
| 485 |     output.close();
 | 
|---|
| 486 |   }
 | 
|---|
| 487 |   return true;
 | 
|---|
| 488 | };
 | 
|---|
| 489 | 
 | 
|---|
| 490 | /** Writes the summed total values in the last matrix to file.
 | 
|---|
| 491 |  * \param *name inputdir
 | 
|---|
| 492 |  * \param *prefix prefix
 | 
|---|
| 493 |  * \param *suffix suffix
 | 
|---|
| 494 |  * \return file was written
 | 
|---|
| 495 |  */
 | 
|---|
| 496 | bool MatrixContainer::WriteLastMatrix(const std::string name, const std::string prefix, const std::string suffix)
 | 
|---|
| 497 | {
 | 
|---|
| 498 |   ofstream output;
 | 
|---|
| 499 |   stringstream line;
 | 
|---|
| 500 | 
 | 
|---|
| 501 |   LOG(1, "STATUS: Writing matrix values of " << suffix << ".");
 | 
|---|
| 502 |   line << name << prefix << suffix;
 | 
|---|
| 503 |   output.open(line.str().c_str(), ios::out);
 | 
|---|
| 504 |   if (output == NULL) {
 | 
|---|
| 505 |     ELOG(0, "MatrixContainer::WriteLastMatrix: Unable to open output matrix file " << line.str() << "!");
 | 
|---|
| 506 |     performCriticalExit();
 | 
|---|
| 507 |     return false;
 | 
|---|
| 508 |   }
 | 
|---|
| 509 |   output << Header[MatrixCounter] << endl;
 | 
|---|
| 510 |   for(int j=0;j<RowCounter[MatrixCounter];j++) {
 | 
|---|
| 511 |     for(int k=0;k<ColumnCounter[MatrixCounter];k++)
 | 
|---|
| 512 |       output << scientific << Matrix[MatrixCounter][j][k] << "\t";
 | 
|---|
| 513 |     output << endl;
 | 
|---|
| 514 |   }
 | 
|---|
| 515 |   output.close();
 | 
|---|
| 516 |   return true;
 | 
|---|
| 517 | };
 | 
|---|
| 518 | 
 | 
|---|
| 519 | /** Comparison operator for class MatrixContainer.
 | 
|---|
| 520 |  *
 | 
|---|
| 521 |  * @param other instance to compare to
 | 
|---|
| 522 |  * @return true - both instances are the same in each member variable.
 | 
|---|
| 523 |  */
 | 
|---|
| 524 | bool MatrixContainer::operator==(const MatrixContainer &other) const
 | 
|---|
| 525 | {
 | 
|---|
| 526 |   // compare matrices
 | 
|---|
| 527 |   if (Matrix != other.Matrix)
 | 
|---|
| 528 |     return false;
 | 
|---|
| 529 |   // compare Indices
 | 
|---|
| 530 |   if (Indices != other.Indices)
 | 
|---|
| 531 |     return false;
 | 
|---|
| 532 |   // compare Headers
 | 
|---|
| 533 |   if (Header != other.Header)
 | 
|---|
| 534 |     return false;
 | 
|---|
| 535 |   // compare MatrixCounter
 | 
|---|
| 536 |   if (MatrixCounter != other.MatrixCounter)
 | 
|---|
| 537 |     return false;
 | 
|---|
| 538 |   // compare RowCounter
 | 
|---|
| 539 |   if (RowCounter != other.RowCounter)
 | 
|---|
| 540 |     return false;
 | 
|---|
| 541 |   // compare ColumnCounter
 | 
|---|
| 542 |   if (ColumnCounter != other.ColumnCounter)
 | 
|---|
| 543 |     return false;
 | 
|---|
| 544 |   return true;
 | 
|---|
| 545 | }
 | 
|---|
| 546 | 
 | 
|---|
| 547 | std::ostream & operator << (std::ostream &ost, const MatrixContainer &m)
 | 
|---|
| 548 | {
 | 
|---|
| 549 |   for (int i=0;i<=m.MatrixCounter;++i) {
 | 
|---|
| 550 |     ost << "Matrix " << i << " in MatrixContainer:" << std::endl;
 | 
|---|
| 551 |     for (int j=0;j<=m.RowCounter[i];++j) {
 | 
|---|
| 552 |       for (int k=0;k<m.ColumnCounter[i];++k) {
 | 
|---|
| 553 |         ost << m.Matrix[i][j][k] << " ";
 | 
|---|
| 554 |       }
 | 
|---|
| 555 |       ost << std::endl;
 | 
|---|
| 556 |     }
 | 
|---|
| 557 |   }
 | 
|---|
| 558 |   ost << std::endl;
 | 
|---|
| 559 |   return ost;
 | 
|---|
| 560 | }
 | 
|---|
| 561 | 
 | 
|---|