[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
|
---|
[a2d653] | 90 | * -# allocate a temporary matrix
|
---|
[a9b86d] | 91 | * -# loop over found column and row counts and parse in each entry
|
---|
[a2d653] | 92 | * -# use MatrixContainer::AddMatrix() to add the parsed matrix to the internal
|
---|
[a9b86d] | 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 | */
|
---|
[9758f7] | 99 | bool MatrixContainer::ParseMatrix(std::istream &input, int skiplines, int skipcolumns, size_t MatrixNr)
|
---|
[a9b86d] | 100 | {
|
---|
| 101 | stringstream line;
|
---|
| 102 | string token;
|
---|
| 103 | char filename[1023];
|
---|
| 104 |
|
---|
| 105 | if (input.fail()) {
|
---|
[47d041] | 106 | ELOG(1, endl << "MatrixContainer::ParseMatrix: Unable to parse istream.");
|
---|
[a9b86d] | 107 | //performCriticalExit();
|
---|
| 108 | return false;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | // parse header
|
---|
[a2d653] | 112 | std::string header;
|
---|
[9758f7] | 113 | char dummy[1024];
|
---|
[a9b86d] | 114 | for (int m=skiplines+1;m--;)
|
---|
[9758f7] | 115 | input.getline(dummy, 1023);
|
---|
| 116 | line.str(dummy);
|
---|
[a9b86d] | 117 | for(int k=skipcolumns;k--;)
|
---|
[a2d653] | 118 | line >> header;
|
---|
[44f53e] | 119 | LOG(3, "INFO: Header of Matrix " << MatrixNr << " :" << line.str());
|
---|
[9758f7] | 120 |
|
---|
| 121 | // scan header for number of columns
|
---|
[a2d653] | 122 | size_t ColumnCounter = 0;
|
---|
[a9b86d] | 123 | while ( getline(line,token, '\t') ) {
|
---|
| 124 | if (token.length() > 0)
|
---|
[a2d653] | 125 | ColumnCounter++;
|
---|
[a9b86d] | 126 | }
|
---|
[44f53e] | 127 | LOG(3, "INFO: "+line.str());
|
---|
[a9b86d] | 128 |
|
---|
| 129 | // scan rest for number of rows/lines
|
---|
[a2d653] | 130 | size_t RowCounter = -1;
|
---|
[a9b86d] | 131 | while (!input.eof()) {
|
---|
| 132 | input.getline(filename, 1023);
|
---|
[44f53e] | 133 | LOG(3, "INFO: Comparing: " << strncmp(filename,"MeanForce",9));
|
---|
[a2d653] | 134 | RowCounter++; // then line was not last MeanForce
|
---|
[a9b86d] | 135 | if (strncmp(filename,"MeanForce", 9) == 0) {
|
---|
| 136 | break;
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[a2d653] | 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);
|
---|
[9758f7] | 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);
|
---|
[44f53e] | 152 | LOG(3, "INFO: Header: " << line.str());
|
---|
[9758f7] | 153 | for(int k=skipcolumns;k--;) // skip columns in header too
|
---|
| 154 | line >> filename;
|
---|
[a2d653] | 155 | header = line.str();
|
---|
| 156 | for(size_t j=0;j<RowCounter;j++) {
|
---|
[9758f7] | 157 | input.getline(filename, 1023);
|
---|
[47d041] | 158 | std::stringstream lines(filename);
|
---|
| 159 | std::stringstream output;
|
---|
| 160 | output << "INFO: Matrix at level " << j << ":";// << filename << endl;
|
---|
[9758f7] | 161 | for(int k=skipcolumns;k--;)
|
---|
| 162 | lines >> filename;
|
---|
[a2d653] | 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;
|
---|
[9758f7] | 166 | }
|
---|
[44f53e] | 167 | LOG(3, output.str());
|
---|
[a9b86d] | 168 | }
|
---|
[9758f7] | 169 |
|
---|
[a2d653] | 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 |
|
---|
[a9b86d] | 231 | return true;
|
---|
[a2d653] | 232 | }
|
---|
[a9b86d] | 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 | */
|
---|
[9758f7] | 252 | bool MatrixContainer::ParseFragmentMatrix(const std::string name, const std::string prefix, std::string suffix, int skiplines, int skipcolumns)
|
---|
[a9b86d] | 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()) {
|
---|
[44f53e] | 265 | ELOG(1, "MatrixContainer::ParseFragmentMatrix: Unable to open " << file.str() << ", is the directory correct?");
|
---|
[a9b86d] | 266 | return false;
|
---|
| 267 | }
|
---|
| 268 | while (!input.eof()) {
|
---|
| 269 | input.getline(filename, 1023);
|
---|
| 270 | stringstream zeile(filename);
|
---|
| 271 | MatrixCounter++;
|
---|
| 272 | }
|
---|
| 273 | input.close();
|
---|
[44f53e] | 274 | LOG(2, "INFO: Determined " << MatrixCounter << " fragments.");
|
---|
[a9b86d] | 275 |
|
---|
[44f53e] | 276 | LOG(1, "STATUS: Parsing through each fragment and retrieving " << prefix << suffix << ".");
|
---|
[9758f7] | 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);
|
---|
[a9b86d] | 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());
|
---|
[44f53e] | 291 | LOG(2, "INFO: Opening " << file.str() << " ... ");
|
---|
[a9b86d] | 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 | */
|
---|
[9758f7] | 309 | bool MatrixContainer::AllocateMatrix(StringVector GivenHeader, int MCounter, IntVector RCounter, IntVector CCounter)
|
---|
[a9b86d] | 310 | {
|
---|
| 311 | MatrixCounter = MCounter;
|
---|
[9758f7] | 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);
|
---|
[a9b86d] | 316 | for(int i=MatrixCounter+1;i--;) {
|
---|
[9758f7] | 317 | Header[i] = GivenHeader[i];
|
---|
[a9b86d] | 318 | RowCounter[i] = RCounter[i];
|
---|
| 319 | ColumnCounter[i] = CCounter[i];
|
---|
[c9cafa] | 320 | if ((int)Matrix[i].size() <= RowCounter[i] + 2)
|
---|
[9758f7] | 321 | Matrix[i].resize(RowCounter[i] + 1);
|
---|
| 322 | for(int j=0;j<=RowCounter[i];j++)
|
---|
[c9cafa] | 323 | if ((int)Matrix[i][j].size() <= ColumnCounter[i]+1)
|
---|
[9758f7] | 324 | Matrix[i][j].resize(ColumnCounter[i]);
|
---|
| 325 | // allocation with 0 is guaranted by STL
|
---|
[a9b86d] | 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 | */
|
---|
[9758f7] | 392 | bool MatrixContainer::SetLastMatrix(const MatrixArray &values, int skipcolumns)
|
---|
[a9b86d] | 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++) {
|
---|
[47d041] | 411 | //LOG(0, "Current Fragment is " << CurrentFragment << "/" << KeySets.OrderSet[Order][CurrentFragment] << ".");
|
---|
[a9b86d] | 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])) {
|
---|
[47d041] | 416 | //LOG(0, "Current other fragment is " << j << "/" << KeySets.OrderSet[SubOrder][j] << ".");
|
---|
[a9b86d] | 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];
|
---|
[47d041] | 420 | //LOG(0, "Current index is " << k << "/" << m << ".");
|
---|
[a9b86d] | 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
|
---|
[47d041] | 423 | //LOG(0, "Comparing " << m << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l] << ".");
|
---|
[a9b86d] | 424 | if (m == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l]) {
|
---|
| 425 | m = l;
|
---|
| 426 | break;
|
---|
| 427 | }
|
---|
| 428 | }
|
---|
[47d041] | 429 | //LOG(0, "Corresponding index in CurrentFragment is " << m << ".");
|
---|
[a9b86d] | 430 | if (m > RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) {
|
---|
[47d041] | 431 | ELOG(0, "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment] << " current force index " << m << " is greater than " << RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!");
|
---|
[a9b86d] | 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))
|
---|
[47d041] | 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]);
|
---|
[a9b86d] | 445 | }
|
---|
| 446 | } else {
|
---|
[47d041] | 447 | //LOG(0, "Fragment " << KeySets.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySets.OrderSet[Order][CurrentFragment] << ".");
|
---|
[a9b86d] | 448 | }
|
---|
| 449 | }
|
---|
| 450 | }
|
---|
[47d041] | 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]);
|
---|
[a9b86d] | 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 | */
|
---|
[9758f7] | 462 | bool MatrixContainer::WriteTotalFragments(const std::string name, const std::string prefix)
|
---|
[a9b86d] | 463 | {
|
---|
| 464 | ofstream output;
|
---|
| 465 | char *FragmentNumber = NULL;
|
---|
| 466 |
|
---|
[44f53e] | 467 | LOG(1, "STATUS: Writing fragment files.");
|
---|
[a9b86d] | 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) {
|
---|
[47d041] | 475 | ELOG(0, "MatrixContainer::WriteTotalFragments: Unable to open output energy file " << line.str() << "!");
|
---|
[a9b86d] | 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 | */
|
---|
[9758f7] | 496 | bool MatrixContainer::WriteLastMatrix(const std::string name, const std::string prefix, const std::string suffix)
|
---|
[a9b86d] | 497 | {
|
---|
| 498 | ofstream output;
|
---|
| 499 | stringstream line;
|
---|
| 500 |
|
---|
[44f53e] | 501 | LOG(1, "STATUS: Writing matrix values of " << suffix << ".");
|
---|
[a9b86d] | 502 | line << name << prefix << suffix;
|
---|
| 503 | output.open(line.str().c_str(), ios::out);
|
---|
| 504 | if (output == NULL) {
|
---|
[47d041] | 505 | ELOG(0, "MatrixContainer::WriteLastMatrix: Unable to open output matrix file " << line.str() << "!");
|
---|
[a9b86d] | 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 |
|
---|
[1b145f] | 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 |
|
---|
[a9b86d] | 547 | std::ostream & operator << (std::ostream &ost, const MatrixContainer &m)
|
---|
| 548 | {
|
---|
| 549 | for (int i=0;i<=m.MatrixCounter;++i) {
|
---|
[44f53e] | 550 | ost << "Matrix " << i << " in MatrixContainer:" << std::endl;
|
---|
[a9b86d] | 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 | }
|
---|
[1b145f] | 561 |
|
---|