| [a9b86d] | 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 |  * ForceMatrix.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 | 
 | 
|---|
 | 25 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 26 | #include "KeySetsContainer.hpp"
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 | #include "Fragmentation/helpers.hpp"
 | 
|---|
 | 29 | #include "Helpers/defs.hpp"
 | 
|---|
 | 30 | #include "Helpers/helpers.hpp"
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | #include "ForceMatrix.hpp"
 | 
|---|
 | 33 | 
 | 
|---|
 | 34 | /** Parsing force Indices of each fragment
 | 
|---|
 | 35 |  * \param *name directory with \a ForcesFile
 | 
|---|
 | 36 |  * \return parsing successful
 | 
|---|
 | 37 |  */
 | 
|---|
 | 38 | bool ForceMatrix::ParseIndices(const char *name)
 | 
|---|
 | 39 | {
 | 
|---|
 | 40 |   ifstream input;
 | 
|---|
 | 41 |   char *FragmentNumber = NULL;
 | 
|---|
 | 42 |   char filename[1023];
 | 
|---|
 | 43 |   stringstream line;
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 |   DoLog(0) && (Log() << Verbose(0) << "Parsing force indices for " << MatrixCounter << " matrices." << endl);
 | 
|---|
| [9758f7] | 46 |   Indices.resize(MatrixCounter + 1);
 | 
|---|
| [a9b86d] | 47 |   line << name << FRAGMENTPREFIX << FORCESFILE;
 | 
|---|
 | 48 |   input.open(line.str().c_str(), ios::in);
 | 
|---|
 | 49 |   //Log() << Verbose(0) << "Opening " << line.str() << " ... "  << input << endl;
 | 
|---|
 | 50 |   if (input.fail()) {
 | 
|---|
 | 51 |     DoLog(0) && (Log() << Verbose(0) << endl << "ForceMatrix::ParseIndices: Unable to open " << line.str() << ", is the directory correct?" << endl);
 | 
|---|
 | 52 |     return false;
 | 
|---|
 | 53 |   }
 | 
|---|
 | 54 |   for (int i=0;(i<MatrixCounter) && (!input.eof());i++) {
 | 
|---|
 | 55 |     // get the number of atoms for this fragment
 | 
|---|
 | 56 |     input.getline(filename, 1023);
 | 
|---|
 | 57 |     line.str(filename);
 | 
|---|
 | 58 |     // parse the values
 | 
|---|
| [9758f7] | 59 |     Indices[i].resize(RowCounter[i]);
 | 
|---|
| [a9b86d] | 60 |     FragmentNumber = FixedDigitNumber(MatrixCounter, i);
 | 
|---|
 | 61 |     //Log() << Verbose(0) << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:";
 | 
|---|
 | 62 |     delete[](FragmentNumber);
 | 
|---|
 | 63 |     for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) {
 | 
|---|
 | 64 |       line >> Indices[i][j];
 | 
|---|
 | 65 |       //Log() << Verbose(0) << " " << Indices[i][j];
 | 
|---|
 | 66 |     }
 | 
|---|
 | 67 |     //Log() << Verbose(0) << endl;
 | 
|---|
 | 68 |   }
 | 
|---|
| [9758f7] | 69 |   Indices[MatrixCounter].resize(RowCounter[MatrixCounter]);
 | 
|---|
| [a9b86d] | 70 |   for(int j=RowCounter[MatrixCounter];j--;) {
 | 
|---|
 | 71 |     Indices[MatrixCounter][j] = j;
 | 
|---|
 | 72 |   }
 | 
|---|
 | 73 |   input.close();
 | 
|---|
 | 74 |   return true;
 | 
|---|
 | 75 | };
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 | 
 | 
|---|
 | 78 | /** Sums the forces and puts into last element of \a ForceMatrix::Matrix.
 | 
|---|
 | 79 |  * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
 | 
|---|
 | 80 |  * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
 | 
|---|
 | 81 |  * \param Order bond order
 | 
|---|
 | 82 |  *  \param sign +1 or -1
 | 
|---|
 | 83 |  * \return true if summing was successful
 | 
|---|
 | 84 |  */
 | 
|---|
 | 85 | bool ForceMatrix::SumSubForces(class ForceMatrix &Fragments, class KeySetsContainer &KeySets, int Order, double sign)
 | 
|---|
 | 86 | {
 | 
|---|
 | 87 |   int FragmentNr;
 | 
|---|
 | 88 |   // sum forces
 | 
|---|
 | 89 |   for(int i=0;i<KeySets.FragmentsPerOrder[Order];i++) {
 | 
|---|
 | 90 |     FragmentNr = KeySets.OrderSet[Order][i];
 | 
|---|
 | 91 |     for(int l=0;l<RowCounter[ FragmentNr ];l++) {
 | 
|---|
 | 92 |       int j = Indices[ FragmentNr ][l];
 | 
|---|
 | 93 |       if (j > RowCounter[MatrixCounter]) {
 | 
|---|
 | 94 |         DoeLog(0) && (eLog()<< Verbose(0) << "Current force index " << j << " is greater than " << RowCounter[MatrixCounter] << "!" << endl);
 | 
|---|
 | 95 |         performCriticalExit();
 | 
|---|
 | 96 |         return false;
 | 
|---|
 | 97 |       }
 | 
|---|
 | 98 |       if (j != -1) {
 | 
|---|
 | 99 |         //if (j == 0) Log() << Verbose(0) << "Summing onto ion 0, type 0 from fragment " << FragmentNr << ", ion " << l << "." << endl;
 | 
|---|
 | 100 |         for(int k=2;k<ColumnCounter[MatrixCounter];k++)
 | 
|---|
 | 101 |           Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][k];
 | 
|---|
 | 102 |       }
 | 
|---|
 | 103 |     }
 | 
|---|
 | 104 |   }
 | 
|---|
 | 105 |   return true;
 | 
|---|
 | 106 | };
 | 
|---|
 | 107 | 
 | 
|---|
 | 108 | 
 | 
|---|
 | 109 | /** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
 | 
|---|
 | 110 |  * \param *name directory with files
 | 
|---|
 | 111 |  * \param *prefix prefix of each matrix file
 | 
|---|
 | 112 |  * \param *suffix suffix of each matrix file
 | 
|---|
 | 113 |  * \param skiplines number of inital lines to skip
 | 
|---|
 | 114 |  * \param skiplines number of inital columns to skip
 | 
|---|
 | 115 |  * \return parsing successful
 | 
|---|
 | 116 |  */
 | 
|---|
 | 117 | bool ForceMatrix::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns)
 | 
|---|
 | 118 | {
 | 
|---|
 | 119 |   char filename[1023];
 | 
|---|
 | 120 |   ifstream input;
 | 
|---|
 | 121 |   stringstream file;
 | 
|---|
 | 122 |   int nr;
 | 
|---|
 | 123 |   bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 |   if (status) {
 | 
|---|
 | 126 |     // count number of atoms for last plus one matrix
 | 
|---|
 | 127 |     file << name << FRAGMENTPREFIX << KEYSETFILE;
 | 
|---|
 | 128 |     input.open(file.str().c_str(), ios::in);
 | 
|---|
 | 129 |     if (input.fail()) {
 | 
|---|
 | 130 |       DoLog(0) && (Log() << Verbose(0) << endl << "ForceMatrix::ParseFragmentMatrix: Unable to open " << file.str() << ", is the directory correct?" << endl);
 | 
|---|
 | 131 |       return false;
 | 
|---|
 | 132 |     }
 | 
|---|
 | 133 |     RowCounter[MatrixCounter] = 0;
 | 
|---|
 | 134 |     while (!input.eof()) {
 | 
|---|
 | 135 |       input.getline(filename, 1023);
 | 
|---|
 | 136 |       stringstream zeile(filename);
 | 
|---|
 | 137 |       while (!zeile.eof()) {
 | 
|---|
 | 138 |         zeile >> nr;
 | 
|---|
 | 139 |         //Log() << Verbose(0) << "Current index: " << getNr() << "." << endl;
 | 
|---|
 | 140 |         if (nr > RowCounter[MatrixCounter])
 | 
|---|
 | 141 |           RowCounter[MatrixCounter] = nr;
 | 
|---|
 | 142 |       }
 | 
|---|
 | 143 |     }
 | 
|---|
 | 144 |     RowCounter[MatrixCounter]++;    // Nr start at 0, count starts at 1
 | 
|---|
 | 145 |     input.close();
 | 
|---|
 | 146 | 
 | 
|---|
 | 147 |     ColumnCounter[MatrixCounter] = 0;
 | 
|---|
 | 148 |     for(int j=0; j < MatrixCounter;j++) { // (energy matrix might be bigger than number of atoms in terms of rows)
 | 
|---|
 | 149 |       if (ColumnCounter[j] > ColumnCounter[MatrixCounter])  // take maximum of all for last matrix
 | 
|---|
 | 150 |         ColumnCounter[MatrixCounter] = ColumnCounter[j];
 | 
|---|
 | 151 |     }
 | 
|---|
 | 152 | 
 | 
|---|
 | 153 |     // allocate last plus one matrix
 | 
|---|
| [9758f7] | 154 |     if (Matrix[MatrixCounter].size() <= RowCounter[MatrixCounter] + 2)
 | 
|---|
 | 155 |       Matrix[MatrixCounter].resize(RowCounter[MatrixCounter] + 1);
 | 
|---|
| [a9b86d] | 156 |     for(int j=0;j<=RowCounter[MatrixCounter];j++)
 | 
|---|
| [9758f7] | 157 |       if (Matrix[MatrixCounter][j].size() <= ColumnCounter[MatrixCounter]+1)
 | 
|---|
 | 158 |         Matrix[MatrixCounter][j].resize(ColumnCounter[MatrixCounter]);
 | 
|---|
| [a9b86d] | 159 | 
 | 
|---|
 | 160 |     // try independently to parse global forcesuffix file if present
 | 
|---|
 | 161 |     strncpy(filename, name, 1023);
 | 
|---|
 | 162 |     strncat(filename, prefix, 1023-strlen(filename));
 | 
|---|
 | 163 |     strncat(filename, suffix.c_str(), 1023-strlen(filename));
 | 
|---|
 | 164 |     std::ifstream input(filename);
 | 
|---|
 | 165 |     ParseMatrix(input, skiplines, skipcolumns, MatrixCounter);
 | 
|---|
 | 166 |     input.close();
 | 
|---|
 | 167 |   }
 | 
|---|
 | 168 | 
 | 
|---|
 | 169 | 
 | 
|---|
 | 170 |   return status;
 | 
|---|
 | 171 | };
 | 
|---|