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