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