[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 | * KeySetsContainer.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 <fstream>
|
---|
[9758f7] | 23 | #include <sstream>
|
---|
[a9b86d] | 24 |
|
---|
| 25 | #include "CodePatterns/Log.hpp"
|
---|
| 26 |
|
---|
| 27 | #include "Fragmentation/defs.hpp"
|
---|
| 28 | #include "Fragmentation/helpers.hpp"
|
---|
| 29 | #include "Helpers/defs.hpp"
|
---|
| 30 | #include "KeySetsContainer.hpp"
|
---|
| 31 |
|
---|
| 32 | /** Constructor of KeySetsContainer class.
|
---|
| 33 | */
|
---|
| 34 | KeySetsContainer::KeySetsContainer() :
|
---|
| 35 | FragmentCounter(0),
|
---|
| 36 | Order(0),
|
---|
[9758f7] | 37 | FragmentsPerOrder(0)
|
---|
[a9b86d] | 38 | {};
|
---|
| 39 |
|
---|
| 40 | /** Destructor of KeySetsContainer class.
|
---|
| 41 | */
|
---|
| 42 | KeySetsContainer::~KeySetsContainer() {
|
---|
[9758f7] | 43 | // for(int i=FragmentCounter;i--;)
|
---|
| 44 | // delete[](KeySets[i]);
|
---|
| 45 | // for(int i=Order;i--;)
|
---|
| 46 | // delete[](OrderSet[i]);
|
---|
| 47 | // delete[](KeySets);
|
---|
| 48 | // delete[](OrderSet);
|
---|
| 49 | // delete[](AtomCounter);
|
---|
| 50 | // delete[](FragmentsPerOrder);
|
---|
[a9b86d] | 51 | };
|
---|
| 52 |
|
---|
| 53 | /** Parsing KeySets into array.
|
---|
| 54 | * \param *name directory with keyset file
|
---|
| 55 | * \param *ACounter number of atoms per fragment
|
---|
| 56 | * \param FCounter number of fragments
|
---|
| 57 | * \return parsing succesful
|
---|
| 58 | */
|
---|
[9758f7] | 59 | bool KeySetsContainer::ParseKeySets(const std::string name, const IntVector ACounter, const int FCounter) {
|
---|
[a9b86d] | 60 | ifstream input;
|
---|
| 61 | char *FragmentNumber = NULL;
|
---|
| 62 | stringstream file;
|
---|
| 63 | char filename[1023];
|
---|
| 64 |
|
---|
| 65 | FragmentCounter = FCounter;
|
---|
[47d041] | 66 | LOG(0, "Parsing key sets.");
|
---|
[9758f7] | 67 | KeySets.resize(FragmentCounter);
|
---|
[a9b86d] | 68 | file << name << FRAGMENTPREFIX << KEYSETFILE;
|
---|
| 69 | input.open(file.str().c_str(), ios::in);
|
---|
| 70 | if (input.fail()) {
|
---|
[47d041] | 71 | LOG(0, endl << "KeySetsContainer::ParseKeySets: Unable to open " << file.str() << ", is the directory correct?");
|
---|
[a9b86d] | 72 | return false;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[9758f7] | 75 | AtomCounter.resize(FragmentCounter);
|
---|
[a9b86d] | 76 | for(int i=0;(i<FragmentCounter) && (!input.eof());i++) {
|
---|
| 77 | stringstream line;
|
---|
| 78 | AtomCounter[i] = ACounter[i];
|
---|
| 79 | // parse the values
|
---|
[9758f7] | 80 | KeySets[i].resize(AtomCounter[i]);
|
---|
[a9b86d] | 81 | for(int j=AtomCounter[i];j--;)
|
---|
| 82 | KeySets[i][j] = -1;
|
---|
| 83 | FragmentNumber = FixedDigitNumber(FragmentCounter, i);
|
---|
[47d041] | 84 | //std::stringstream output;
|
---|
| 85 | //output << FRAGMENTPREFIX << FragmentNumber << "[" << AtomCounter[i] << "]:";
|
---|
[a9b86d] | 86 | delete[](FragmentNumber);
|
---|
| 87 | input.getline(filename, 1023);
|
---|
| 88 | line.str(filename);
|
---|
| 89 | for(int j=0;(j<AtomCounter[i]) && (!line.eof());j++) {
|
---|
| 90 | line >> KeySets[i][j];
|
---|
[47d041] | 91 | //output << " " << KeySets[i][j];
|
---|
[a9b86d] | 92 | }
|
---|
[47d041] | 93 | //LOG(0, output.str());
|
---|
[a9b86d] | 94 | }
|
---|
| 95 | input.close();
|
---|
| 96 | return true;
|
---|
| 97 | };
|
---|
| 98 |
|
---|
| 99 | /** Parse many body terms, associating each fragment to a certain bond order.
|
---|
| 100 | * \return parsing succesful
|
---|
| 101 | */
|
---|
| 102 | bool KeySetsContainer::ParseManyBodyTerms()
|
---|
| 103 | {
|
---|
| 104 | int Counter;
|
---|
| 105 |
|
---|
[47d041] | 106 | LOG(0, "Creating Fragment terms.");
|
---|
[a9b86d] | 107 | // scan through all to determine maximum order
|
---|
| 108 | Order=0;
|
---|
| 109 | for(int i=FragmentCounter;i--;) {
|
---|
| 110 | Counter=0;
|
---|
| 111 | for(int j=AtomCounter[i];j--;)
|
---|
| 112 | if (KeySets[i][j] != -1)
|
---|
| 113 | Counter++;
|
---|
| 114 | if (Counter > Order)
|
---|
| 115 | Order = Counter;
|
---|
| 116 | }
|
---|
[47d041] | 117 | LOG(0, "Found Order is " << Order << ".");
|
---|
[a9b86d] | 118 |
|
---|
| 119 | // scan through all to determine fragments per order
|
---|
[9758f7] | 120 | FragmentsPerOrder.resize(Order);
|
---|
| 121 | // for(int i=Order;i--;)
|
---|
| 122 | // FragmentsPerOrder[i] = 0;
|
---|
[a9b86d] | 123 | for(int i=FragmentCounter;i--;) {
|
---|
| 124 | Counter=0;
|
---|
| 125 | for(int j=AtomCounter[i];j--;)
|
---|
| 126 | if (KeySets[i][j] != -1)
|
---|
| 127 | Counter++;
|
---|
| 128 | FragmentsPerOrder[Counter-1]++;
|
---|
| 129 | }
|
---|
| 130 | for(int i=0;i<Order;i++)
|
---|
[47d041] | 131 | LOG(0, "Found No. of Fragments of Order " << i+1 << " is " << FragmentsPerOrder[i] << ".");
|
---|
[a9b86d] | 132 |
|
---|
| 133 | // scan through all to gather indices to each order set
|
---|
[9758f7] | 134 | OrderSet.resize(Order);
|
---|
[a9b86d] | 135 | for(int i=Order;i--;)
|
---|
[9758f7] | 136 | OrderSet[i].resize(FragmentsPerOrder[i]);
|
---|
[a9b86d] | 137 | for(int i=Order;i--;)
|
---|
| 138 | FragmentsPerOrder[i] = 0;
|
---|
| 139 | for(int i=FragmentCounter;i--;) {
|
---|
| 140 | Counter=0;
|
---|
| 141 | for(int j=AtomCounter[i];j--;)
|
---|
| 142 | if (KeySets[i][j] != -1)
|
---|
| 143 | Counter++;
|
---|
| 144 | OrderSet[Counter-1][FragmentsPerOrder[Counter-1]] = i;
|
---|
| 145 | FragmentsPerOrder[Counter-1]++;
|
---|
| 146 | }
|
---|
[47d041] | 147 | std::stringstream output;
|
---|
| 148 | output << "Printing OrderSet: " << std::endl;
|
---|
[a9b86d] | 149 | for(int i=0;i<Order;i++) {
|
---|
| 150 | for (int j=0;j<FragmentsPerOrder[i];j++) {
|
---|
[47d041] | 151 | output << " " << OrderSet[i][j];
|
---|
[a9b86d] | 152 | }
|
---|
[47d041] | 153 | output << std::endl;
|
---|
[a9b86d] | 154 | }
|
---|
[47d041] | 155 | LOG(0, output.str());
|
---|
[a9b86d] | 156 |
|
---|
| 157 | return true;
|
---|
| 158 | };
|
---|
| 159 |
|
---|
| 160 | /** Compares each entry in \a *SmallerSet if it is containted in \a *GreaterSet.
|
---|
| 161 | * \param GreaterSet index to greater set
|
---|
| 162 | * \param SmallerSet index to smaller set
|
---|
| 163 | * \return true if all keys of SmallerSet contained in GreaterSet
|
---|
| 164 | */
|
---|
| 165 | bool KeySetsContainer::Contains(const int GreaterSet, const int SmallerSet)
|
---|
| 166 | {
|
---|
| 167 | bool result = true;
|
---|
| 168 | bool intermediate;
|
---|
| 169 | if ((GreaterSet < 0) || (SmallerSet < 0) || (GreaterSet > FragmentCounter) || (SmallerSet > FragmentCounter)) // index out of bounds
|
---|
| 170 | return false;
|
---|
| 171 | for(int i=AtomCounter[SmallerSet];i--;) {
|
---|
| 172 | intermediate = false;
|
---|
| 173 | for (int j=AtomCounter[GreaterSet];j--;)
|
---|
| 174 | intermediate = (intermediate || ((KeySets[SmallerSet][i] == KeySets[GreaterSet][j]) || (KeySets[SmallerSet][i] == -1)));
|
---|
| 175 | result = result && intermediate;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | return result;
|
---|
| 179 | };
|
---|
| 180 |
|
---|
[1b145f] | 181 | /** Comparison operator for class KeySetsContainer.
|
---|
| 182 | *
|
---|
| 183 | * @param other instance to compare to
|
---|
| 184 | * @return true - both instances are the same in each member variable.
|
---|
| 185 | */
|
---|
| 186 | bool KeySetsContainer::operator==(const KeySetsContainer &other) const
|
---|
| 187 | {
|
---|
| 188 | // compare KeySets
|
---|
| 189 | if (KeySets != other.KeySets)
|
---|
| 190 | return false;
|
---|
| 191 | // compare OrderSet
|
---|
| 192 | if (OrderSet != other.OrderSet)
|
---|
| 193 | return false;
|
---|
| 194 | // compare AtomCounter
|
---|
| 195 | if (AtomCounter != other.AtomCounter)
|
---|
| 196 | return false;
|
---|
| 197 | // compare FragmentCounter
|
---|
| 198 | if (FragmentCounter != other.FragmentCounter)
|
---|
| 199 | return false;
|
---|
| 200 | // compare Order
|
---|
| 201 | if (Order != other.Order)
|
---|
| 202 | return false;
|
---|
| 203 | // compare FragmentsPerOrder
|
---|
| 204 | if (FragmentsPerOrder != other.FragmentsPerOrder)
|
---|
| 205 | return false;
|
---|
| 206 |
|
---|
| 207 | return true;
|
---|
| 208 | }
|
---|