| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * EnergyMatrix.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 "EnergyMatrix.hpp" | 
|---|
| 29 |  | 
|---|
| 30 | /** Create a trivial energy index mapping. | 
|---|
| 31 | * This just maps 1 to 1, 2 to 2 and so on for all fragments. | 
|---|
| 32 | * \return creation sucessful | 
|---|
| 33 | */ | 
|---|
| 34 | bool EnergyMatrix::ParseIndices() | 
|---|
| 35 | { | 
|---|
| 36 | LOG(0, "Parsing energy indices."); | 
|---|
| 37 | Indices.resize(MatrixCounter + 1); | 
|---|
| 38 | for(int i=MatrixCounter+1;i--;) { | 
|---|
| 39 | Indices[i].resize(RowCounter[i]); | 
|---|
| 40 | for(int j=RowCounter[i];j--;) | 
|---|
| 41 | Indices[i][j] = j; | 
|---|
| 42 | } | 
|---|
| 43 | return true; | 
|---|
| 44 | }; | 
|---|
| 45 |  | 
|---|
| 46 | /** Sums the energy with each factor and put into last element of \a EnergyMatrix::Matrix. | 
|---|
| 47 | * Sums over the "F"-terms in ANOVA decomposition. | 
|---|
| 48 | * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies. | 
|---|
| 49 | * \param CorrectionFragments MatrixContainer with hydrogen saturation correction per fragments | 
|---|
| 50 | * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order | 
|---|
| 51 | * \param Order bond order | 
|---|
| 52 | * \parsm sign +1 or -1 | 
|---|
| 53 | * \return true if summing was successful | 
|---|
| 54 | */ | 
|---|
| 55 | bool EnergyMatrix::SumSubEnergy(class EnergyMatrix &Fragments, class EnergyMatrix *CorrectionFragments, class KeySetsContainer &KeySets, int Order, double sign) | 
|---|
| 56 | { | 
|---|
| 57 | // sum energy | 
|---|
| 58 | if (CorrectionFragments == NULL) | 
|---|
| 59 | for(int i=KeySets.FragmentsPerOrder[Order];i--;) | 
|---|
| 60 | for(int j=RowCounter[ KeySets.OrderSet[Order][i] ];j--;) | 
|---|
| 61 | for(int k=ColumnCounter[ KeySets.OrderSet[Order][i] ];k--;) | 
|---|
| 62 | Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ KeySets.OrderSet[Order][i] ][j][k]; | 
|---|
| 63 | else | 
|---|
| 64 | for(int i=KeySets.FragmentsPerOrder[Order];i--;) | 
|---|
| 65 | for(int j=RowCounter[ KeySets.OrderSet[Order][i] ];j--;) | 
|---|
| 66 | for(int k=ColumnCounter[ KeySets.OrderSet[Order][i] ];k--;) | 
|---|
| 67 | Matrix[MatrixCounter][j][k] += sign*(Fragments.Matrix[ KeySets.OrderSet[Order][i] ][j][k] + CorrectionFragments->Matrix[ KeySets.OrderSet[Order][i] ][j][k]); | 
|---|
| 68 | return true; | 
|---|
| 69 | }; | 
|---|
| 70 |  | 
|---|
| 71 | /** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix. | 
|---|
| 72 | * \param *name directory with files | 
|---|
| 73 | * \param *prefix prefix of each matrix file | 
|---|
| 74 | * \param *suffix suffix of each matrix file | 
|---|
| 75 | * \param skiplines number of inital lines to skip | 
|---|
| 76 | * \param skiplines number of inital columns to skip | 
|---|
| 77 | * \return parsing successful | 
|---|
| 78 | */ | 
|---|
| 79 | bool EnergyMatrix::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns) | 
|---|
| 80 | { | 
|---|
| 81 | char filename[1024]; | 
|---|
| 82 | bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns); | 
|---|
| 83 |  | 
|---|
| 84 | if (status) { | 
|---|
| 85 | // count maximum of columns | 
|---|
| 86 | RowCounter[MatrixCounter] = 0; | 
|---|
| 87 | ColumnCounter[MatrixCounter] = 0; | 
|---|
| 88 | for(int j=0; j < MatrixCounter;j++) { // (energy matrix might be bigger than number of atoms in terms of rows) | 
|---|
| 89 | if (RowCounter[j] > RowCounter[MatrixCounter]) | 
|---|
| 90 | RowCounter[MatrixCounter] = RowCounter[j]; | 
|---|
| 91 | if (ColumnCounter[j] > ColumnCounter[MatrixCounter])  // take maximum of all for last matrix | 
|---|
| 92 | ColumnCounter[MatrixCounter] = ColumnCounter[j]; | 
|---|
| 93 | } | 
|---|
| 94 | // allocate last plus one matrix | 
|---|
| 95 | if (Matrix[MatrixCounter].size() <= RowCounter[MatrixCounter] + 2) | 
|---|
| 96 | Matrix[MatrixCounter].resize(RowCounter[MatrixCounter] + 1); | 
|---|
| 97 | for(int j=0;j<=RowCounter[MatrixCounter];j++) | 
|---|
| 98 | if (Matrix[MatrixCounter][j].size() <= ColumnCounter[MatrixCounter]+1) | 
|---|
| 99 | Matrix[MatrixCounter][j].resize(ColumnCounter[MatrixCounter]); | 
|---|
| 100 |  | 
|---|
| 101 | // try independently to parse global energysuffix file if present | 
|---|
| 102 | strncpy(filename, name, 1023); | 
|---|
| 103 | strncat(filename, prefix, 1023-strlen(filename)); | 
|---|
| 104 | strncat(filename, suffix.c_str(), 1023-strlen(filename)); | 
|---|
| 105 | std::ifstream input(filename); | 
|---|
| 106 | ParseMatrix(input, skiplines, skipcolumns, MatrixCounter); | 
|---|
| 107 | input.close(); | 
|---|
| 108 | } | 
|---|
| 109 | return status; | 
|---|
| 110 | }; | 
|---|