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