[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 | * HessianMatrix.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 "HessianMatrix.hpp"
|
---|
| 33 |
|
---|
| 34 | /** Parsing force Indices of each fragment
|
---|
| 35 | * \param *name directory with \a ForcesFile
|
---|
| 36 | * \return parsing successful
|
---|
| 37 | */
|
---|
| 38 | bool HessianMatrix::ParseIndices(char *name)
|
---|
| 39 | {
|
---|
| 40 | std::ifstream input;
|
---|
| 41 | char *FragmentNumber = NULL;
|
---|
| 42 | char filename[1023];
|
---|
| 43 | stringstream line;
|
---|
| 44 |
|
---|
[47d041] | 45 | LOG(0, "Parsing hessian 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 << "HessianMatrix::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 hessian entries and puts into last element of \a HessianMatrix::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 HessianMatrix::SumSubHessians(class HessianMatrix &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 hessian index " << j << " is greater than " << RowCounter[MatrixCounter] << ", where i=" << i << ", Order=" << Order << ", l=" << l << " and FragmentNr=" << FragmentNr << "!");
|
---|
[a9b86d] | 96 | performCriticalExit();
|
---|
| 97 | return false;
|
---|
| 98 | }
|
---|
| 99 | if (j != -1) {
|
---|
| 100 | for(int m=0;m<ColumnCounter[ FragmentNr ];m++) {
|
---|
| 101 | int k = Indices[ FragmentNr ][m];
|
---|
| 102 | if (k > ColumnCounter[MatrixCounter]) {
|
---|
[47d041] | 103 | ELOG(0, "Current hessian index " << k << " is greater than " << ColumnCounter[MatrixCounter] << ", where m=" << m << ", j=" << j << ", i=" << i << ", Order=" << Order << ", l=" << l << " and FragmentNr=" << FragmentNr << "!");
|
---|
[a9b86d] | 104 | performCriticalExit();
|
---|
| 105 | return false;
|
---|
| 106 | }
|
---|
| 107 | if (k != -1) {
|
---|
[47d041] | 108 | //LOG(0, "Adding " << sign*Fragments.Matrix[ FragmentNr ][l][m] << " from [" << l << "][" << m << "] onto [" << j << "][" << k << "].");
|
---|
[a9b86d] | 109 | Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][m];
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | return true;
|
---|
| 116 | };
|
---|
| 117 |
|
---|
| 118 | /** Constructor for class HessianMatrix.
|
---|
| 119 | */
|
---|
| 120 | HessianMatrix::HessianMatrix() :
|
---|
| 121 | MatrixContainer(),
|
---|
| 122 | IsSymmetric(true)
|
---|
| 123 | {}
|
---|
| 124 |
|
---|
| 125 | /** Sums the hessian entries with each factor and put into last element of \a ***Matrix.
|
---|
| 126 | * Sums over "E"-terms to create the "F"-terms
|
---|
| 127 | * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
|
---|
| 128 | * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
|
---|
| 129 | * \param Order bond order
|
---|
| 130 | * \return true if summing was successful
|
---|
| 131 | */
|
---|
| 132 | bool HessianMatrix::SumSubManyBodyTerms(class MatrixContainer &MatrixValues, class KeySetsContainer &KeySets, int Order)
|
---|
| 133 | {
|
---|
| 134 | // go through each order
|
---|
| 135 | for (int CurrentFragment=0;CurrentFragment<KeySets.FragmentsPerOrder[Order];CurrentFragment++) {
|
---|
[47d041] | 136 | //LOG(0, "Current Fragment is " << CurrentFragment << "/" << KeySets.OrderSet[Order][CurrentFragment] << ".");
|
---|
[a9b86d] | 137 | // then go per order through each suborder and pick together all the terms that contain this fragment
|
---|
| 138 | for(int SubOrder=0;SubOrder<=Order;SubOrder++) { // go through all suborders up to the desired order
|
---|
| 139 | for (int j=0;j<KeySets.FragmentsPerOrder[SubOrder];j++) { // go through all possible fragments of size suborder
|
---|
| 140 | if (KeySets.Contains(KeySets.OrderSet[Order][CurrentFragment], KeySets.OrderSet[SubOrder][j])) {
|
---|
[47d041] | 141 | //LOG(0, "Current other fragment is " << j << "/" << KeySets.OrderSet[SubOrder][j] << ".");
|
---|
[a9b86d] | 142 | // if the fragment's indices are all in the current fragment
|
---|
| 143 | for(int k=0;k<RowCounter[ KeySets.OrderSet[SubOrder][j] ];k++) { // go through all atoms in this fragment
|
---|
| 144 | int m = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][k];
|
---|
[47d041] | 145 | //LOG(0, "Current row index is " << k << "/" << m << ".");
|
---|
[a9b86d] | 146 | if (m != -1) { // if it's not an added hydrogen
|
---|
| 147 | for (int l=0;l<RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ];l++) { // look for the corresponding index in the current fragment
|
---|
[47d041] | 148 | //LOG(0, "Comparing " << m << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l] << ".");
|
---|
[a9b86d] | 149 | if (m == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l]) {
|
---|
| 150 | m = l;
|
---|
| 151 | break;
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
[47d041] | 154 | //LOG(0, "Corresponding row index for " << k << " in CurrentFragment is " << m << ".");
|
---|
[a9b86d] | 155 | if (m > RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) {
|
---|
[47d041] | 156 | ELOG(0, "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment] << " current row index " << m << " is greater than " << RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!");
|
---|
[a9b86d] | 157 | performCriticalExit();
|
---|
| 158 | return false;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | for(int l=0;l<ColumnCounter[ KeySets.OrderSet[SubOrder][j] ];l++) {
|
---|
| 162 | int n = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][l];
|
---|
[47d041] | 163 | //LOG(0, "Current column index is " << l << "/" << n << ".");
|
---|
[a9b86d] | 164 | if (n != -1) { // if it's not an added hydrogen
|
---|
| 165 | for (int p=0;p<ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ];p++) { // look for the corresponding index in the current fragment
|
---|
[47d041] | 166 | //LOG(0, "Comparing " << n << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][p] << ".");
|
---|
[a9b86d] | 167 | if (n == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][p]) {
|
---|
| 168 | n = p;
|
---|
| 169 | break;
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
[47d041] | 172 | //LOG(0, "Corresponding column index for " << l << " in CurrentFragment is " << n << ".");
|
---|
[a9b86d] | 173 | if (n > ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) {
|
---|
[47d041] | 174 | ELOG(0, "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment] << " current column index " << n << " is greater than " << ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!");
|
---|
[a9b86d] | 175 | performCriticalExit();
|
---|
| 176 | return false;
|
---|
| 177 | }
|
---|
| 178 | if (Order == SubOrder) { // equal order is always copy from Energies
|
---|
[47d041] | 179 | //LOG(0, "Adding " << MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "].");
|
---|
[a9b86d] | 180 | Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][n] += MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
|
---|
| 181 | } else {
|
---|
[47d041] | 182 | //LOG(0, "Subtracting " << Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "].");
|
---|
[a9b86d] | 183 | Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][n] -= Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 | //if ((ColumnCounter[ KeySets.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1))
|
---|
[47d041] | 189 | //LOG(0, "Fragments[ KeySets.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySets.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1]);
|
---|
[a9b86d] | 190 | }
|
---|
| 191 | } else {
|
---|
[47d041] | 192 | //LOG(0, "Fragment " << KeySets.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySets.OrderSet[Order][CurrentFragment] << ".");
|
---|
[a9b86d] | 193 | }
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
[47d041] | 196 | //LOG(0, "Final Fragments[ KeySets.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySets.OrderSet[Order][CurrentFragment] << " ][" << KeySets.AtomCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][KeySets.AtomCounter[0]-1][1]);
|
---|
[a9b86d] | 197 | }
|
---|
| 198 |
|
---|
| 199 | return true;
|
---|
| 200 | };
|
---|
| 201 |
|
---|
| 202 | /** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
|
---|
| 203 | * \param *name directory with files
|
---|
| 204 | * \param *prefix prefix of each matrix file
|
---|
| 205 | * \param *suffix suffix of each matrix file
|
---|
| 206 | * \param skiplines number of inital lines to skip
|
---|
| 207 | * \param skiplines number of inital columns to skip
|
---|
| 208 | * \return parsing successful
|
---|
| 209 | */
|
---|
[955b91] | 210 | bool HessianMatrix::ParseFragmentMatrix(const char *name, const char *prefix, std::string suffix, int skiplines, int skipcolumns)
|
---|
[a9b86d] | 211 | {
|
---|
| 212 | char filename[1023];
|
---|
| 213 | std::ifstream input;
|
---|
| 214 | stringstream file;
|
---|
| 215 | int nr;
|
---|
| 216 | bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
|
---|
| 217 |
|
---|
| 218 | if (status) {
|
---|
| 219 | // count number of atoms for last plus one matrix
|
---|
| 220 | file << name << FRAGMENTPREFIX << KEYSETFILE;
|
---|
| 221 | input.open(file.str().c_str(), ios::in);
|
---|
| 222 | if (input.fail()) {
|
---|
[47d041] | 223 | LOG(0, endl << "HessianMatrix::ParseFragmentMatrix: Unable to open " << file.str() << ", is the directory correct?");
|
---|
[a9b86d] | 224 | return false;
|
---|
| 225 | }
|
---|
| 226 | RowCounter[MatrixCounter] = 0;
|
---|
| 227 | ColumnCounter[MatrixCounter] = 0;
|
---|
| 228 | while (!input.eof()) {
|
---|
| 229 | input.getline(filename, 1023);
|
---|
| 230 | stringstream zeile(filename);
|
---|
| 231 | while (!zeile.eof()) {
|
---|
| 232 | zeile >> nr;
|
---|
[47d041] | 233 | //LOG(0, "Current index: " << getNr() << ".");
|
---|
[a9b86d] | 234 | if (nr > RowCounter[MatrixCounter]) {
|
---|
| 235 | RowCounter[MatrixCounter] = nr;
|
---|
| 236 | ColumnCounter[MatrixCounter] = nr;
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 | }
|
---|
| 240 | RowCounter[MatrixCounter]++; // Nr start at 0, count starts at 1
|
---|
| 241 | ColumnCounter[MatrixCounter]++; // Nr start at 0, count starts at 1
|
---|
| 242 | input.close();
|
---|
| 243 |
|
---|
| 244 | // allocate last plus one matrix
|
---|
[47d041] | 245 | LOG(0, "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns.");
|
---|
[c9cafa] | 246 | if ((int)Matrix[MatrixCounter].size() <= RowCounter[MatrixCounter] + 2)
|
---|
[9758f7] | 247 | Matrix[MatrixCounter].resize(RowCounter[MatrixCounter] + 1);
|
---|
[a9b86d] | 248 | for(int j=0;j<=RowCounter[MatrixCounter];j++)
|
---|
[c9cafa] | 249 | if ((int)Matrix[MatrixCounter][j].size() <= ColumnCounter[MatrixCounter]+1)
|
---|
[9758f7] | 250 | Matrix[MatrixCounter][j].resize(ColumnCounter[MatrixCounter]);
|
---|
[a9b86d] | 251 |
|
---|
| 252 | // try independently to parse global forcesuffix file if present
|
---|
| 253 | strncpy(filename, name, 1023);
|
---|
| 254 | strncat(filename, prefix, 1023-strlen(filename));
|
---|
| 255 | strncat(filename, suffix.c_str(), 1023-strlen(filename));
|
---|
| 256 | std::ifstream input(filename);
|
---|
| 257 | ParseMatrix(input, skiplines, skipcolumns, MatrixCounter);
|
---|
| 258 | input.close();
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 |
|
---|
| 262 | return status;
|
---|
| 263 | };
|
---|