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