1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
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/>.
|
---|
21 | */
|
---|
22 |
|
---|
23 | /*
|
---|
24 | * KeySetsContainer.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 <fstream>
|
---|
38 | #include <sstream>
|
---|
39 |
|
---|
40 | #include <boost/lexical_cast.hpp>
|
---|
41 | #include <boost/tokenizer.hpp>
|
---|
42 |
|
---|
43 | #include "CodePatterns/Log.hpp"
|
---|
44 |
|
---|
45 | #include "Fragmentation/defs.hpp"
|
---|
46 | #include "Fragmentation/helpers.hpp"
|
---|
47 | #include "Helpers/defs.hpp"
|
---|
48 | #include "KeySetsContainer.hpp"
|
---|
49 |
|
---|
50 | /** Constructor of KeySetsContainer class.
|
---|
51 | */
|
---|
52 | KeySetsContainer::KeySetsContainer() :
|
---|
53 | FragmentCounter(0),
|
---|
54 | Order(0),
|
---|
55 | FragmentsPerOrder(0)
|
---|
56 | {};
|
---|
57 |
|
---|
58 | /** Destructor of KeySetsContainer class.
|
---|
59 | */
|
---|
60 | KeySetsContainer::~KeySetsContainer() {
|
---|
61 | // for(int i=FragmentCounter;i--;)
|
---|
62 | // delete[](KeySets[i]);
|
---|
63 | // for(int i=Order;i--;)
|
---|
64 | // delete[](OrderSet[i]);
|
---|
65 | // delete[](KeySets);
|
---|
66 | // delete[](OrderSet);
|
---|
67 | // delete[](AtomCounter);
|
---|
68 | // delete[](FragmentsPerOrder);
|
---|
69 | };
|
---|
70 |
|
---|
71 | /** Parsing KeySets into array.
|
---|
72 | * \param *name directory with keyset file
|
---|
73 | * \param *ACounter number of atoms per fragment
|
---|
74 | * \param FCounter number of fragments
|
---|
75 | * \return parsing succesful
|
---|
76 | */
|
---|
77 | bool KeySetsContainer::ParseKeySets(const std::string path, const std::string name, const int FCounter) {
|
---|
78 | ifstream input;
|
---|
79 | //char *FragmentNumber = NULL;
|
---|
80 | stringstream file;
|
---|
81 | char filename[1023];
|
---|
82 |
|
---|
83 | FragmentCounter = FCounter;
|
---|
84 | KeySets.resize(FragmentCounter);
|
---|
85 | AtomCounter.resize(FragmentCounter);
|
---|
86 |
|
---|
87 | // open file
|
---|
88 | file << path << "/" << name;
|
---|
89 | input.open(file.str().c_str(), ios::in);
|
---|
90 | if (input.fail()) {
|
---|
91 | LOG(0, endl << "KeySetsContainer::ParseKeySets: Unable to open " << file.str() << ", is the directory correct?");
|
---|
92 | return false;
|
---|
93 | }
|
---|
94 |
|
---|
95 | // parse each line, i.e. get the index set per fragment
|
---|
96 | LOG(0, "Parsing key sets.");
|
---|
97 | for(int i=0;(i<FragmentCounter) && (!input.eof());i++) {
|
---|
98 | input.getline(filename, 1023);
|
---|
99 | string line(filename);
|
---|
100 | // LOG(2, "DEBUG: line is " << line << ".");
|
---|
101 | std::stringstream set_output;
|
---|
102 | typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
|
---|
103 | boost::char_separator<char> sep(" \t");
|
---|
104 | tokenizer tok(line, sep);
|
---|
105 | for(tokenizer::iterator beg=tok.begin();
|
---|
106 | beg != tok.end();++beg){
|
---|
107 | const int tempvalue = boost::lexical_cast<int>(*beg);
|
---|
108 | KeySets[i].push_back(tempvalue);
|
---|
109 | // set_output << " " << KeySets[i].back();
|
---|
110 | }
|
---|
111 | // LOG(2, "DEBUG: Scanned keys are '" << set_output.str() << "'.");
|
---|
112 | AtomCounter[i] = KeySets[i].size();
|
---|
113 | // {
|
---|
114 | // std::stringstream output;
|
---|
115 | // FragmentNumber = FixedDigitNumber(FragmentCounter, i);
|
---|
116 | // output << FRAGMENTPREFIX << FragmentNumber << "[" << AtomCounter[i] << "]:" << set_output.str();
|
---|
117 | // delete[](FragmentNumber);
|
---|
118 | // LOG(0, output.str());
|
---|
119 | // }
|
---|
120 | }
|
---|
121 | input.close();
|
---|
122 | return true;
|
---|
123 | };
|
---|
124 |
|
---|
125 | /** Parse many body terms, associating each fragment to a certain bond order.
|
---|
126 | * \return parsing succesful
|
---|
127 | */
|
---|
128 | bool KeySetsContainer::ParseManyBodyTerms()
|
---|
129 | {
|
---|
130 | int Counter;
|
---|
131 |
|
---|
132 | LOG(0, "Creating Fragment terms.");
|
---|
133 | // scan through all to determine maximum order
|
---|
134 | Order=0;
|
---|
135 | for(int i=FragmentCounter;i--;) {
|
---|
136 | Counter=0;
|
---|
137 | for(int j=AtomCounter[i];j--;)
|
---|
138 | if (KeySets[i][j] != -1)
|
---|
139 | Counter++;
|
---|
140 | if (Counter > Order)
|
---|
141 | Order = Counter;
|
---|
142 | }
|
---|
143 | LOG(0, "Found Order is " << Order << ".");
|
---|
144 |
|
---|
145 | // scan through all to determine fragments per order
|
---|
146 | FragmentsPerOrder.resize(Order);
|
---|
147 | // for(int i=Order;i--;)
|
---|
148 | // FragmentsPerOrder[i] = 0;
|
---|
149 | for(int i=FragmentCounter;i--;) {
|
---|
150 | Counter=0;
|
---|
151 | for(int j=AtomCounter[i];j--;)
|
---|
152 | if (KeySets[i][j] != -1)
|
---|
153 | Counter++;
|
---|
154 | FragmentsPerOrder[Counter-1]++;
|
---|
155 | }
|
---|
156 | for(int i=0;i<Order;i++)
|
---|
157 | LOG(0, "Found No. of Fragments of Order " << i+1 << " is " << FragmentsPerOrder[i] << ".");
|
---|
158 |
|
---|
159 | // scan through all to gather indices to each order set
|
---|
160 | OrderSet.resize(Order);
|
---|
161 | for(int i=Order;i--;)
|
---|
162 | OrderSet[i].resize(FragmentsPerOrder[i]);
|
---|
163 | for(int i=Order;i--;)
|
---|
164 | FragmentsPerOrder[i] = 0;
|
---|
165 | for(int i=FragmentCounter;i--;) {
|
---|
166 | Counter=0;
|
---|
167 | for(int j=AtomCounter[i];j--;)
|
---|
168 | if (KeySets[i][j] != -1)
|
---|
169 | Counter++;
|
---|
170 | OrderSet[Counter-1][FragmentsPerOrder[Counter-1]] = i;
|
---|
171 | FragmentsPerOrder[Counter-1]++;
|
---|
172 | }
|
---|
173 | std::stringstream output;
|
---|
174 | output << "Printing OrderSet: " << std::endl;
|
---|
175 | for(int i=0;i<Order;i++) {
|
---|
176 | for (int j=0;j<FragmentsPerOrder[i];j++) {
|
---|
177 | output << " " << OrderSet[i][j];
|
---|
178 | }
|
---|
179 | output << std::endl;
|
---|
180 | }
|
---|
181 | LOG(0, output.str());
|
---|
182 |
|
---|
183 | return true;
|
---|
184 | };
|
---|
185 |
|
---|
186 | /** Compares each entry in \a *SmallerSet if it is containted in \a *GreaterSet.
|
---|
187 | * \param GreaterSet index to greater set
|
---|
188 | * \param SmallerSet index to smaller set
|
---|
189 | * \return true if all keys of SmallerSet contained in GreaterSet
|
---|
190 | */
|
---|
191 | bool KeySetsContainer::Contains(const int GreaterSet, const int SmallerSet)
|
---|
192 | {
|
---|
193 | bool result = true;
|
---|
194 | bool intermediate;
|
---|
195 | if ((GreaterSet < 0) || (SmallerSet < 0) || (GreaterSet > FragmentCounter) || (SmallerSet > FragmentCounter)) // index out of bounds
|
---|
196 | return false;
|
---|
197 | for(int i=AtomCounter[SmallerSet];i--;) {
|
---|
198 | intermediate = false;
|
---|
199 | for (int j=AtomCounter[GreaterSet];j--;)
|
---|
200 | intermediate = (intermediate || ((KeySets[SmallerSet][i] == KeySets[GreaterSet][j]) || (KeySets[SmallerSet][i] == -1)));
|
---|
201 | result = result && intermediate;
|
---|
202 | }
|
---|
203 |
|
---|
204 | return result;
|
---|
205 | };
|
---|
206 |
|
---|
207 | /** Comparison operator for class KeySetsContainer.
|
---|
208 | *
|
---|
209 | * @param other instance to compare to
|
---|
210 | * @return true - both instances are the same in each member variable.
|
---|
211 | */
|
---|
212 | bool KeySetsContainer::operator==(const KeySetsContainer &other) const
|
---|
213 | {
|
---|
214 | // compare KeySets
|
---|
215 | if (KeySets != other.KeySets)
|
---|
216 | return false;
|
---|
217 | // compare OrderSet
|
---|
218 | if (OrderSet != other.OrderSet)
|
---|
219 | return false;
|
---|
220 | // compare AtomCounter
|
---|
221 | if (AtomCounter != other.AtomCounter)
|
---|
222 | return false;
|
---|
223 | // compare FragmentCounter
|
---|
224 | if (FragmentCounter != other.FragmentCounter)
|
---|
225 | return false;
|
---|
226 | // compare Order
|
---|
227 | if (Order != other.Order)
|
---|
228 | return false;
|
---|
229 | // compare FragmentsPerOrder
|
---|
230 | if (FragmentsPerOrder != other.FragmentsPerOrder)
|
---|
231 | return false;
|
---|
232 |
|
---|
233 | return true;
|
---|
234 | }
|
---|
235 |
|
---|
236 | void KeySetsContainer::clear()
|
---|
237 | {
|
---|
238 | KeySets.clear();
|
---|
239 | OrderSet.clear();
|
---|
240 | AtomCounter.clear();
|
---|
241 | FragmentCounter = 0;
|
---|
242 | Order = 0;
|
---|
243 | FragmentsPerOrder.clear();
|
---|
244 | }
|
---|
245 |
|
---|
246 | void KeySetsContainer::insert(const KeySetsContainer &other)
|
---|
247 | {
|
---|
248 | KeySets.reserve(KeySets.size()+other.KeySets.size());
|
---|
249 | AtomCounter.reserve(AtomCounter.size()+other.AtomCounter.size());
|
---|
250 | for (ArrayOfIntVectors::const_iterator iter = other.KeySets.begin();
|
---|
251 | iter != other.KeySets.end(); ++iter)
|
---|
252 | insert(*iter, iter->size());
|
---|
253 | }
|
---|
254 |
|
---|
255 | void KeySetsContainer::insert(const IntVector &indices, const size_t order)
|
---|
256 | {
|
---|
257 | KeySets.push_back(indices);
|
---|
258 | AtomCounter.push_back(indices.size());
|
---|
259 | if (order > OrderSet.size() )
|
---|
260 | OrderSet.resize(order);
|
---|
261 | OrderSet[order-1].push_back(FragmentCounter);
|
---|
262 | if (order > FragmentsPerOrder.size())
|
---|
263 | FragmentsPerOrder.resize(order);
|
---|
264 | ++(FragmentsPerOrder[order-1]);
|
---|
265 | ++FragmentCounter;
|
---|
266 | }
|
---|