| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * 
 | 
|---|
| 6 |  *
 | 
|---|
| 7 |  *   This file is part of MoleCuilder.
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 12 |  *    (at your option) any later version.
 | 
|---|
| 13 |  *
 | 
|---|
| 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 17 |  *    GNU General Public License for more details.
 | 
|---|
| 18 |  *
 | 
|---|
| 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * ForceMatrix.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Sep 15, 2011
 | 
|---|
| 27 |  *      Author: heber
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // include config.h
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <config.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include <cstring>
 | 
|---|
| 38 | #include <fstream>
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 41 | #include "KeySetsContainer.hpp"
 | 
|---|
| 42 | 
 | 
|---|
| 43 | #include "Fragmentation/helpers.hpp"
 | 
|---|
| 44 | #include "Helpers/defs.hpp"
 | 
|---|
| 45 | #include "Helpers/helpers.hpp"
 | 
|---|
| 46 | 
 | 
|---|
| 47 | #include "ForceMatrix.hpp"
 | 
|---|
| 48 | 
 | 
|---|
| 49 | /** Parsing force Indices of each fragment
 | 
|---|
| 50 |  * \param *name directory with \a ForcesFile
 | 
|---|
| 51 |  * \return parsing successful
 | 
|---|
| 52 |  */
 | 
|---|
| 53 | bool ForceMatrix::ParseIndices(const char *name)
 | 
|---|
| 54 | {
 | 
|---|
| 55 |   ifstream input;
 | 
|---|
| 56 |   char *FragmentNumber = NULL;
 | 
|---|
| 57 |   char filename[1023];
 | 
|---|
| 58 |   stringstream line;
 | 
|---|
| 59 | 
 | 
|---|
| 60 |   LOG(0, "Parsing force indices for " << MatrixCounter << " matrices.");
 | 
|---|
| 61 |   Indices.resize(MatrixCounter + 1);
 | 
|---|
| 62 |   line << name << FRAGMENTPREFIX << FORCESFILE;
 | 
|---|
| 63 |   input.open(line.str().c_str(), ios::in);
 | 
|---|
| 64 |   //LOG(0, "Opening " << line.str() << " ... "  << input);
 | 
|---|
| 65 |   if (input.fail()) {
 | 
|---|
| 66 |     LOG(0, endl << "ForceMatrix::ParseIndices: Unable to open " << line.str() << ", is the directory correct?");
 | 
|---|
| 67 |     return false;
 | 
|---|
| 68 |   }
 | 
|---|
| 69 |   for (int i=0;(i<MatrixCounter) && (!input.eof());i++) {
 | 
|---|
| 70 |     // get the number of atoms for this fragment
 | 
|---|
| 71 |     input.getline(filename, 1023);
 | 
|---|
| 72 |     line.str(filename);
 | 
|---|
| 73 |     // parse the values
 | 
|---|
| 74 |     Indices[i].resize(RowCounter[i]);
 | 
|---|
| 75 |     FragmentNumber = FixedDigitNumber(MatrixCounter, i);
 | 
|---|
| 76 |     //std::stringstream output;
 | 
|---|
| 77 |     //output << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:";
 | 
|---|
| 78 |     delete[](FragmentNumber);
 | 
|---|
| 79 |     for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) {
 | 
|---|
| 80 |       line >> Indices[i][j];
 | 
|---|
| 81 |       //output << " " << Indices[i][j];
 | 
|---|
| 82 |     }
 | 
|---|
| 83 |     //LOG(0, output.str());
 | 
|---|
| 84 |   }
 | 
|---|
| 85 |   Indices[MatrixCounter].resize(RowCounter[MatrixCounter]);
 | 
|---|
| 86 |   for(int j=RowCounter[MatrixCounter];j--;) {
 | 
|---|
| 87 |     Indices[MatrixCounter][j] = j;
 | 
|---|
| 88 |   }
 | 
|---|
| 89 |   input.close();
 | 
|---|
| 90 |   return true;
 | 
|---|
| 91 | };
 | 
|---|
| 92 | 
 | 
|---|
| 93 | /** Parsing force Indices of each fragment
 | 
|---|
| 94 |  * \param *name directory with \a ForcesFile
 | 
|---|
| 95 |  * \return parsing successful
 | 
|---|
| 96 |  */
 | 
|---|
| 97 | bool ForceMatrix::ParseIndices(const KeySetsContainer &ForceKeySet)
 | 
|---|
| 98 | {
 | 
|---|
| 99 |   Indices = ForceKeySet.KeySets;
 | 
|---|
| 100 |   return true;
 | 
|---|
| 101 | }
 | 
|---|
| 102 | 
 | 
|---|
| 103 | /** Sums the forces and puts into last element of \a ForceMatrix::Matrix.
 | 
|---|
| 104 |  * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
 | 
|---|
| 105 |  * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
 | 
|---|
| 106 |  * \param Order bond order
 | 
|---|
| 107 |  *  \param sign +1 or -1
 | 
|---|
| 108 |  * \return true if summing was successful
 | 
|---|
| 109 |  */
 | 
|---|
| 110 | bool ForceMatrix::SumSubForces(class ForceMatrix &Fragments, class KeySetsContainer &KeySets, int Order, double sign)
 | 
|---|
| 111 | {
 | 
|---|
| 112 |   int FragmentNr;
 | 
|---|
| 113 |   // sum forces
 | 
|---|
| 114 |   for(int i=0;i<KeySets.FragmentsPerOrder[Order];i++) {
 | 
|---|
| 115 |     FragmentNr = KeySets.OrderSet[Order][i];
 | 
|---|
| 116 |     for(int l=0;l<RowCounter[ FragmentNr ];l++) {
 | 
|---|
| 117 |       int j = Indices[ FragmentNr ][l];
 | 
|---|
| 118 |       if (j > RowCounter[MatrixCounter]) {
 | 
|---|
| 119 |         ELOG(0, "Current force index " << j << " is greater than " << RowCounter[MatrixCounter] << "!");
 | 
|---|
| 120 |         performCriticalExit();
 | 
|---|
| 121 |         return false;
 | 
|---|
| 122 |       }
 | 
|---|
| 123 |       if (j != -1) {
 | 
|---|
| 124 |         //if (j == 0) LOG(0, "Summing onto ion 0, type 0 from fragment " << FragmentNr << ", ion " << l << ".");
 | 
|---|
| 125 |         for(int k=2;k<ColumnCounter[MatrixCounter];k++)
 | 
|---|
| 126 |           Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][k];
 | 
|---|
| 127 |       }
 | 
|---|
| 128 |     }
 | 
|---|
| 129 |   }
 | 
|---|
| 130 |   return true;
 | 
|---|
| 131 | };
 | 
|---|
| 132 | 
 | 
|---|
| 133 | 
 | 
|---|
| 134 | /** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
 | 
|---|
| 135 |  * \param *name directory with files
 | 
|---|
| 136 |  * \param *prefix prefix of each matrix file
 | 
|---|
| 137 |  * \param *suffix suffix of each matrix file
 | 
|---|
| 138 |  * \param skiplines number of inital lines to skip
 | 
|---|
| 139 |  * \param skiplines number of inital columns to skip
 | 
|---|
| 140 |  * \return parsing successful
 | 
|---|
| 141 |  */
 | 
|---|
| 142 | bool ForceMatrix::ParseFragmentMatrix(const char *name, const char *prefix, std::string suffix, int skiplines, int skipcolumns)
 | 
|---|
| 143 | {
 | 
|---|
| 144 |   char filename[1023];
 | 
|---|
| 145 |   ifstream input;
 | 
|---|
| 146 |   stringstream file;
 | 
|---|
| 147 |   int nr;
 | 
|---|
| 148 |   bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
 | 
|---|
| 149 | 
 | 
|---|
| 150 |   if (status) {
 | 
|---|
| 151 |     // count number of atoms for last plus one matrix
 | 
|---|
| 152 |     file << name << FRAGMENTPREFIX << KEYSETFILE;
 | 
|---|
| 153 |     input.open(file.str().c_str(), ios::in);
 | 
|---|
| 154 |     if (input.fail()) {
 | 
|---|
| 155 |       LOG(0, endl << "ForceMatrix::ParseFragmentMatrix: Unable to open " << file.str() << ", is the directory correct?");
 | 
|---|
| 156 |       return false;
 | 
|---|
| 157 |     }
 | 
|---|
| 158 |     RowCounter[MatrixCounter] = 0;
 | 
|---|
| 159 |     while (!input.eof()) {
 | 
|---|
| 160 |       input.getline(filename, 1023);
 | 
|---|
| 161 |       stringstream zeile(filename);
 | 
|---|
| 162 |       while (!zeile.eof()) {
 | 
|---|
| 163 |         zeile >> nr;
 | 
|---|
| 164 |         //LOG(0, "Current index: " << getNr() << ".");
 | 
|---|
| 165 |         if (nr > RowCounter[MatrixCounter])
 | 
|---|
| 166 |           RowCounter[MatrixCounter] = nr;
 | 
|---|
| 167 |       }
 | 
|---|
| 168 |     }
 | 
|---|
| 169 |     RowCounter[MatrixCounter]++;    // Nr start at 0, count starts at 1
 | 
|---|
| 170 |     input.close();
 | 
|---|
| 171 | 
 | 
|---|
| 172 |     ColumnCounter[MatrixCounter] = 0;
 | 
|---|
| 173 |     for(int j=0; j < MatrixCounter;j++) { // (energy matrix might be bigger than number of atoms in terms of rows)
 | 
|---|
| 174 |       if (ColumnCounter[j] > ColumnCounter[MatrixCounter])  // take maximum of all for last matrix
 | 
|---|
| 175 |         ColumnCounter[MatrixCounter] = ColumnCounter[j];
 | 
|---|
| 176 |     }
 | 
|---|
| 177 | 
 | 
|---|
| 178 |     // allocate last plus one matrix
 | 
|---|
| 179 |     if ((int)Matrix[MatrixCounter].size() <= RowCounter[MatrixCounter] + 2)
 | 
|---|
| 180 |       Matrix[MatrixCounter].resize(RowCounter[MatrixCounter] + 1);
 | 
|---|
| 181 |     for(int j=0;j<=RowCounter[MatrixCounter];j++)
 | 
|---|
| 182 |       if ((int)Matrix[MatrixCounter][j].size() <= ColumnCounter[MatrixCounter]+1)
 | 
|---|
| 183 |         Matrix[MatrixCounter][j].resize(ColumnCounter[MatrixCounter]);
 | 
|---|
| 184 | 
 | 
|---|
| 185 |     // try independently to parse global forcesuffix file if present
 | 
|---|
| 186 |     strncpy(filename, name, 1023);
 | 
|---|
| 187 |     strncat(filename, prefix, 1023-strlen(filename));
 | 
|---|
| 188 |     strncat(filename, suffix.c_str(), 1023-strlen(filename));
 | 
|---|
| 189 |     std::ifstream input(filename);
 | 
|---|
| 190 |     ParseMatrix(input, skiplines, skipcolumns, MatrixCounter);
 | 
|---|
| 191 |     input.close();
 | 
|---|
| 192 |   }
 | 
|---|
| 193 | 
 | 
|---|
| 194 | 
 | 
|---|
| 195 |   return status;
 | 
|---|
| 196 | };
 | 
|---|