1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
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>
|
---|
23 | #include <sstream>
|
---|
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),
|
---|
37 | FragmentsPerOrder(0)
|
---|
38 | {};
|
---|
39 |
|
---|
40 | /** Destructor of KeySetsContainer class.
|
---|
41 | */
|
---|
42 | KeySetsContainer::~KeySetsContainer() {
|
---|
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);
|
---|
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 | */
|
---|
59 | bool KeySetsContainer::ParseKeySets(const std::string name, const IntVector ACounter, const int FCounter) {
|
---|
60 | ifstream input;
|
---|
61 | char *FragmentNumber = NULL;
|
---|
62 | stringstream file;
|
---|
63 | char filename[1023];
|
---|
64 |
|
---|
65 | FragmentCounter = FCounter;
|
---|
66 | LOG(0, "Parsing key sets.");
|
---|
67 | KeySets.resize(FragmentCounter);
|
---|
68 | file << name << FRAGMENTPREFIX << KEYSETFILE;
|
---|
69 | input.open(file.str().c_str(), ios::in);
|
---|
70 | if (input.fail()) {
|
---|
71 | LOG(0, endl << "KeySetsContainer::ParseKeySets: Unable to open " << file.str() << ", is the directory correct?");
|
---|
72 | return false;
|
---|
73 | }
|
---|
74 |
|
---|
75 | AtomCounter.resize(FragmentCounter);
|
---|
76 | for(int i=0;(i<FragmentCounter) && (!input.eof());i++) {
|
---|
77 | stringstream line;
|
---|
78 | AtomCounter[i] = ACounter[i];
|
---|
79 | // parse the values
|
---|
80 | KeySets[i].resize(AtomCounter[i]);
|
---|
81 | for(int j=AtomCounter[i];j--;)
|
---|
82 | KeySets[i][j] = -1;
|
---|
83 | FragmentNumber = FixedDigitNumber(FragmentCounter, i);
|
---|
84 | //std::stringstream output;
|
---|
85 | //output << FRAGMENTPREFIX << FragmentNumber << "[" << AtomCounter[i] << "]:";
|
---|
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];
|
---|
91 | //output << " " << KeySets[i][j];
|
---|
92 | }
|
---|
93 | //LOG(0, output.str());
|
---|
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 |
|
---|
106 | LOG(0, "Creating Fragment terms.");
|
---|
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 | }
|
---|
117 | LOG(0, "Found Order is " << Order << ".");
|
---|
118 |
|
---|
119 | // scan through all to determine fragments per order
|
---|
120 | FragmentsPerOrder.resize(Order);
|
---|
121 | // for(int i=Order;i--;)
|
---|
122 | // FragmentsPerOrder[i] = 0;
|
---|
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++)
|
---|
131 | LOG(0, "Found No. of Fragments of Order " << i+1 << " is " << FragmentsPerOrder[i] << ".");
|
---|
132 |
|
---|
133 | // scan through all to gather indices to each order set
|
---|
134 | OrderSet.resize(Order);
|
---|
135 | for(int i=Order;i--;)
|
---|
136 | OrderSet[i].resize(FragmentsPerOrder[i]);
|
---|
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 | }
|
---|
147 | std::stringstream output;
|
---|
148 | output << "Printing OrderSet: " << std::endl;
|
---|
149 | for(int i=0;i<Order;i++) {
|
---|
150 | for (int j=0;j<FragmentsPerOrder[i];j++) {
|
---|
151 | output << " " << OrderSet[i][j];
|
---|
152 | }
|
---|
153 | output << std::endl;
|
---|
154 | }
|
---|
155 | LOG(0, output.str());
|
---|
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 |
|
---|
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 | }
|
---|