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