1 |
gezelter |
507 |
/* |
2 |
gezelter |
246 |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
|
|
* |
4 |
|
|
* The University of Notre Dame grants you ("Licensee") a |
5 |
|
|
* non-exclusive, royalty free, license to use, modify and |
6 |
|
|
* redistribute this software in source and binary code form, provided |
7 |
|
|
* that the following conditions are met: |
8 |
|
|
* |
9 |
gezelter |
1390 |
* 1. Redistributions of source code must retain the above copyright |
10 |
gezelter |
246 |
* notice, this list of conditions and the following disclaimer. |
11 |
|
|
* |
12 |
gezelter |
1390 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
gezelter |
246 |
* notice, this list of conditions and the following disclaimer in the |
14 |
|
|
* documentation and/or other materials provided with the |
15 |
|
|
* distribution. |
16 |
|
|
* |
17 |
|
|
* This software is provided "AS IS," without a warranty of any |
18 |
|
|
* kind. All express or implied conditions, representations and |
19 |
|
|
* warranties, including any implied warranty of merchantability, |
20 |
|
|
* fitness for a particular purpose or non-infringement, are hereby |
21 |
|
|
* excluded. The University of Notre Dame and its licensors shall not |
22 |
|
|
* be liable for any damages suffered by licensee as a result of |
23 |
|
|
* using, modifying or distributing the software or its |
24 |
|
|
* derivatives. In no event will the University of Notre Dame or its |
25 |
|
|
* licensors be liable for any lost revenue, profit or data, or for |
26 |
|
|
* direct, indirect, special, consequential, incidental or punitive |
27 |
|
|
* damages, however caused and regardless of the theory of liability, |
28 |
|
|
* arising out of the use of or inability to use software, even if the |
29 |
|
|
* University of Notre Dame has been advised of the possibility of |
30 |
|
|
* such damages. |
31 |
gezelter |
1390 |
* |
32 |
|
|
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
33 |
|
|
* research, please cite the appropriate papers when you publish your |
34 |
|
|
* work. Good starting points are: |
35 |
|
|
* |
36 |
|
|
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
37 |
|
|
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
38 |
gezelter |
1879 |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). |
39 |
gezelter |
1782 |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
|
|
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
41 |
gezelter |
246 |
*/ |
42 |
|
|
|
43 |
|
|
/** |
44 |
|
|
* @file LocalIndexManager.hpp |
45 |
|
|
* @author tlin |
46 |
|
|
* @date 11/02/2004 |
47 |
|
|
* @version 1.0 |
48 |
|
|
*/ |
49 |
|
|
|
50 |
|
|
#ifndef UTILS_LOCALINDEXMANAGER_HPP |
51 |
|
|
#define UTILS_LOCALINDEXMANAGER_HPP |
52 |
|
|
#include <algorithm> |
53 |
|
|
#include <iostream> |
54 |
|
|
#include <cassert> |
55 |
|
|
#include <list> |
56 |
|
|
#include <utility> |
57 |
|
|
|
58 |
gezelter |
1390 |
namespace OpenMD { |
59 |
gezelter |
246 |
|
60 |
gezelter |
507 |
/** |
61 |
|
|
* @class IndexListContainer |
62 |
|
|
* @brief |
63 |
|
|
* @todo documentation |
64 |
|
|
*/ |
65 |
|
|
class IndexListContainer{ |
66 |
|
|
public: |
67 |
|
|
static const int MAX_INTEGER = 2147483647; |
68 |
gezelter |
1953 |
typedef std::list<std::pair<int, int> >::iterator IndexListContainerIterator; |
69 |
gezelter |
246 |
|
70 |
gezelter |
507 |
IndexListContainer(int minIndex = 0, int maxIndex = MAX_INTEGER) |
71 |
|
|
:minIndex_(minIndex), maxIndex_(maxIndex) { |
72 |
gezelter |
1953 |
indexContainer_.push_back(std::make_pair(minIndex, maxIndex)); |
73 |
|
|
} |
74 |
gezelter |
246 |
|
75 |
gezelter |
1953 |
int pop() { |
76 |
gezelter |
507 |
if (indexContainer_.empty()) { |
77 |
|
|
std::cerr << "" << std::endl; |
78 |
|
|
} |
79 |
gezelter |
246 |
|
80 |
gezelter |
507 |
int result; |
81 |
gezelter |
246 |
|
82 |
gezelter |
507 |
result = indexContainer_.front().first; |
83 |
gezelter |
246 |
|
84 |
gezelter |
507 |
if (indexContainer_.front().first == indexContainer_.front().second) { |
85 |
|
|
indexContainer_.pop_front(); |
86 |
gezelter |
1953 |
} else if (indexContainer_.front().first < |
87 |
|
|
indexContainer_.front().second) { |
88 |
gezelter |
507 |
++indexContainer_.front().first; |
89 |
|
|
} else { |
90 |
|
|
std::cerr << "" << std::endl; |
91 |
|
|
} |
92 |
gezelter |
1953 |
|
93 |
gezelter |
507 |
return result; |
94 |
|
|
} |
95 |
gezelter |
246 |
|
96 |
|
|
|
97 |
gezelter |
507 |
/** |
98 |
|
|
* |
99 |
|
|
*/ |
100 |
|
|
void insert(int index) { |
101 |
gezelter |
1953 |
IndexListContainerIterator insertPos = internalInsert(index, index); |
102 |
gezelter |
507 |
merge(insertPos); |
103 |
|
|
} |
104 |
gezelter |
246 |
|
105 |
gezelter |
507 |
/** |
106 |
|
|
* Reclaims an index range |
107 |
|
|
* @param beginIndex |
108 |
|
|
* @param endIndex |
109 |
|
|
*/ |
110 |
|
|
void insert(int beginIndex, int endIndex) { |
111 |
gezelter |
1953 |
IndexListContainerIterator insertPos = internalInsert(beginIndex, |
112 |
|
|
endIndex); |
113 |
gezelter |
507 |
merge(insertPos); |
114 |
|
|
} |
115 |
gezelter |
246 |
|
116 |
gezelter |
507 |
/** |
117 |
|
|
* Reclaims an index array. |
118 |
|
|
* @param indices |
119 |
|
|
*/ |
120 |
|
|
void insert(std::vector<int>& indices){ |
121 |
gezelter |
246 |
|
122 |
gezelter |
507 |
if (indices.empty()) { |
123 |
|
|
return; |
124 |
|
|
} |
125 |
gezelter |
246 |
|
126 |
gezelter |
507 |
std::sort(indices.begin(), indices.end()); |
127 |
|
|
std::unique(indices.begin(), indices.end()); |
128 |
gezelter |
246 |
|
129 |
gezelter |
507 |
std::vector<int>::iterator i; |
130 |
|
|
int beginIndex; |
131 |
gezelter |
246 |
|
132 |
gezelter |
507 |
beginIndex = indices[0]; |
133 |
gezelter |
246 |
|
134 |
gezelter |
507 |
for ( i = indices.begin() + 1 ; i != indices.end(); ++i) { |
135 |
|
|
if (*i != *(i -1) + 1) { |
136 |
|
|
insert(beginIndex, *(i-1)); |
137 |
|
|
beginIndex = *i; |
138 |
|
|
} |
139 |
|
|
} |
140 |
|
|
} |
141 |
gezelter |
246 |
|
142 |
gezelter |
507 |
std::vector<int> getIndicesBefore(int index) { |
143 |
|
|
std::vector<int> result; |
144 |
|
|
IndexListContainerIterator i; |
145 |
gezelter |
246 |
|
146 |
gezelter |
507 |
for(i = indexContainer_.begin(); i != indexContainer_.end(); ++i) { |
147 |
|
|
if ((*i).first > index) { |
148 |
|
|
//we locate the node whose minimum index is greater that index |
149 |
|
|
indexContainer_.erase(indexContainer_.begin(), i); |
150 |
|
|
break; |
151 |
|
|
} else if ((*i).second < index) { |
152 |
|
|
//The biggest index current node hold is less than the index we want |
153 |
|
|
for (int j = (*i).first; j <= (*i).second; ++j) { |
154 |
|
|
result.push_back(j); |
155 |
|
|
} |
156 |
|
|
continue; |
157 |
|
|
} else if ((*i).first == (*i).second) { |
158 |
|
|
//the index happen to equal to a node which only contains one index |
159 |
|
|
result.push_back((*i).first); |
160 |
|
|
indexContainer_.erase(indexContainer_.begin(), i); |
161 |
|
|
break; |
162 |
|
|
} else { |
163 |
|
|
for (int j = (*i).first; j < index; ++j) { |
164 |
|
|
result.push_back(j); |
165 |
gezelter |
1953 |
} |
166 |
gezelter |
507 |
(*i).first = index; |
167 |
|
|
indexContainer_.erase(indexContainer_.begin(), i); |
168 |
|
|
break; |
169 |
|
|
} |
170 |
|
|
} |
171 |
|
|
return result; |
172 |
|
|
} |
173 |
gezelter |
246 |
|
174 |
gezelter |
507 |
int getMaxIndex() { |
175 |
|
|
return maxIndex_; |
176 |
|
|
} |
177 |
gezelter |
246 |
|
178 |
gezelter |
507 |
private: |
179 |
gezelter |
246 |
|
180 |
gezelter |
507 |
IndexListContainerIterator internalInsert(int beginIndex, int endIndex) { |
181 |
gezelter |
246 |
|
182 |
gezelter |
507 |
if (beginIndex > endIndex) { |
183 |
|
|
std::swap(beginIndex, endIndex); |
184 |
|
|
std::cerr << "" << std::endl; |
185 |
|
|
} |
186 |
gezelter |
246 |
|
187 |
gezelter |
507 |
if (endIndex > maxIndex_) { |
188 |
|
|
std::cerr << "" << std::endl; |
189 |
|
|
} |
190 |
gezelter |
1879 |
|
191 |
gezelter |
507 |
IndexListContainerIterator i = indexContainer_.begin(); |
192 |
|
|
for (; i != indexContainer_.end(); ++i) { |
193 |
|
|
if ((*i).first > endIndex) { |
194 |
|
|
indexContainer_.insert(i, std::make_pair(beginIndex, endIndex)); |
195 |
|
|
return --i; |
196 |
|
|
} else if ((*i).second < beginIndex) { |
197 |
|
|
continue; |
198 |
|
|
} else { |
199 |
|
|
std::cerr << "" << std::endl; |
200 |
|
|
} |
201 |
|
|
} |
202 |
gezelter |
246 |
|
203 |
gezelter |
507 |
indexContainer_.push_back(std::make_pair(beginIndex, endIndex)); |
204 |
|
|
return --indexContainer_.end(); |
205 |
gezelter |
246 |
|
206 |
gezelter |
507 |
} |
207 |
gezelter |
246 |
|
208 |
gezelter |
507 |
void merge(IndexListContainerIterator i) { |
209 |
|
|
IndexListContainerIterator j; |
210 |
gezelter |
246 |
|
211 |
gezelter |
507 |
//check whether current node can be merged with its previous node |
212 |
|
|
if ( i != indexContainer_.begin()) { |
213 |
|
|
j = i; |
214 |
|
|
--j; |
215 |
|
|
if (j != indexContainer_.begin() && (*j).second + 1 == (*i).first) { |
216 |
|
|
(*i).first = (*j).first; |
217 |
|
|
indexContainer_.erase(j); |
218 |
|
|
} |
219 |
|
|
} |
220 |
gezelter |
246 |
|
221 |
gezelter |
507 |
//check whether current node can be merged with its next node |
222 |
|
|
if ( i != indexContainer_.end()) { |
223 |
|
|
j = i; |
224 |
|
|
++j; |
225 |
gezelter |
246 |
|
226 |
gezelter |
507 |
if (j != indexContainer_.end() && (*i).second + 1 == (*j).first) { |
227 |
|
|
(*i).second = (*j).second; |
228 |
|
|
indexContainer_.erase(j); |
229 |
|
|
} |
230 |
|
|
} |
231 |
gezelter |
246 |
|
232 |
gezelter |
507 |
} |
233 |
|
|
int minIndex_; |
234 |
|
|
int maxIndex_; |
235 |
|
|
std::list<std::pair<int, int> > indexContainer_; |
236 |
|
|
}; |
237 |
gezelter |
246 |
|
238 |
|
|
|
239 |
gezelter |
507 |
/** |
240 |
|
|
* @class LocalIndexManager LocalIndexManager.hpp "utils/LocalIndexManager.hpp" |
241 |
|
|
* @brief |
242 |
|
|
*/ |
243 |
|
|
class LocalIndexManager { |
244 |
|
|
public: |
245 |
gezelter |
246 |
|
246 |
gezelter |
507 |
int getNextAtomIndex() { |
247 |
|
|
return atomIndexContainer_.pop(); |
248 |
|
|
} |
249 |
gezelter |
246 |
|
250 |
gezelter |
507 |
std::vector<int> getAtomIndicesBefore(int index) { |
251 |
|
|
return atomIndexContainer_.getIndicesBefore(index); |
252 |
|
|
} |
253 |
gezelter |
246 |
|
254 |
gezelter |
507 |
void releaseAtomIndex(int index) { |
255 |
|
|
atomIndexContainer_.insert(index); |
256 |
|
|
} |
257 |
gezelter |
246 |
|
258 |
gezelter |
507 |
void releaseAtomIndex(int beginIndex, int endIndex) { |
259 |
|
|
atomIndexContainer_.insert(beginIndex, endIndex); |
260 |
|
|
} |
261 |
gezelter |
246 |
|
262 |
gezelter |
507 |
void releaseAtomIndex(std::vector<int> indices) { |
263 |
|
|
atomIndexContainer_.insert(indices); |
264 |
|
|
} |
265 |
gezelter |
246 |
|
266 |
gezelter |
507 |
int getNextRigidBodyIndex() { |
267 |
|
|
return rigidBodyIndexContainer_.pop(); |
268 |
|
|
} |
269 |
gezelter |
246 |
|
270 |
gezelter |
507 |
std::vector<int> getRigidBodyIndicesBefore(int index) { |
271 |
|
|
return rigidBodyIndexContainer_.getIndicesBefore(index); |
272 |
|
|
} |
273 |
gezelter |
246 |
|
274 |
gezelter |
507 |
void releaseRigidBodyIndex(int index) { |
275 |
|
|
rigidBodyIndexContainer_.insert(index); |
276 |
|
|
} |
277 |
gezelter |
246 |
|
278 |
gezelter |
507 |
void releaseRigidBodyIndex(int beginIndex, int endIndex) { |
279 |
|
|
rigidBodyIndexContainer_.insert(beginIndex, endIndex); |
280 |
|
|
} |
281 |
gezelter |
246 |
|
282 |
gezelter |
507 |
void releaseRigidBodyIndex(std::vector<int> indices) { |
283 |
|
|
rigidBodyIndexContainer_.insert(indices); |
284 |
|
|
} |
285 |
gezelter |
1782 |
|
286 |
|
|
int getNextCutoffGroupIndex() { |
287 |
|
|
return cutoffGroupIndexContainer_.pop(); |
288 |
|
|
} |
289 |
|
|
|
290 |
|
|
std::vector<int> getCutoffGroupIndicesBefore(int index) { |
291 |
|
|
return cutoffGroupIndexContainer_.getIndicesBefore(index); |
292 |
|
|
} |
293 |
|
|
|
294 |
|
|
void releaseCutoffGroupIndex(int index) { |
295 |
|
|
cutoffGroupIndexContainer_.insert(index); |
296 |
|
|
} |
297 |
|
|
|
298 |
|
|
void releaseCutoffGroupIndex(int beginIndex, int endIndex) { |
299 |
|
|
cutoffGroupIndexContainer_.insert(beginIndex, endIndex); |
300 |
|
|
} |
301 |
|
|
|
302 |
|
|
void releaseCutoffGroupIndex(std::vector<int> indices) { |
303 |
|
|
cutoffGroupIndexContainer_.insert(indices); |
304 |
|
|
} |
305 |
gezelter |
1953 |
|
306 |
|
|
int getNextBondIndex() { |
307 |
|
|
return bondIndexContainer_.pop(); |
308 |
|
|
} |
309 |
|
|
|
310 |
|
|
std::vector<int> getBondIndicesBefore(int index) { |
311 |
|
|
return bondIndexContainer_.getIndicesBefore(index); |
312 |
|
|
} |
313 |
|
|
|
314 |
|
|
void releaseBondIndex(int index) { |
315 |
|
|
bondIndexContainer_.insert(index); |
316 |
|
|
} |
317 |
|
|
|
318 |
|
|
void releaseBondIndex(int beginIndex, int endIndex) { |
319 |
|
|
bondIndexContainer_.insert(beginIndex, endIndex); |
320 |
|
|
} |
321 |
|
|
|
322 |
|
|
void releaseBondIndex(std::vector<int> indices) { |
323 |
|
|
bondIndexContainer_.insert(indices); |
324 |
|
|
} |
325 |
|
|
|
326 |
|
|
int getNextBendIndex() { |
327 |
|
|
return bendIndexContainer_.pop(); |
328 |
|
|
} |
329 |
|
|
|
330 |
|
|
std::vector<int> getBendIndicesBefore(int index) { |
331 |
|
|
return bendIndexContainer_.getIndicesBefore(index); |
332 |
|
|
} |
333 |
|
|
|
334 |
|
|
void releaseBendIndex(int index) { |
335 |
|
|
bendIndexContainer_.insert(index); |
336 |
|
|
} |
337 |
|
|
|
338 |
|
|
void releaseBendIndex(int beginIndex, int endIndex) { |
339 |
|
|
bendIndexContainer_.insert(beginIndex, endIndex); |
340 |
|
|
} |
341 |
|
|
|
342 |
|
|
void releaseBendIndex(std::vector<int> indices) { |
343 |
|
|
bendIndexContainer_.insert(indices); |
344 |
|
|
} |
345 |
|
|
|
346 |
|
|
int getNextTorsionIndex() { |
347 |
|
|
return torsionIndexContainer_.pop(); |
348 |
|
|
} |
349 |
|
|
|
350 |
|
|
std::vector<int> getTorsionIndicesBefore(int index) { |
351 |
|
|
return torsionIndexContainer_.getIndicesBefore(index); |
352 |
|
|
} |
353 |
|
|
|
354 |
|
|
void releaseTorsionIndex(int index) { |
355 |
|
|
torsionIndexContainer_.insert(index); |
356 |
|
|
} |
357 |
|
|
|
358 |
|
|
void releaseTorsionIndex(int beginIndex, int endIndex) { |
359 |
|
|
torsionIndexContainer_.insert(beginIndex, endIndex); |
360 |
|
|
} |
361 |
|
|
|
362 |
|
|
void releaseTorsionIndex(std::vector<int> indices) { |
363 |
|
|
torsionIndexContainer_.insert(indices); |
364 |
|
|
} |
365 |
|
|
|
366 |
|
|
int getNextInversionIndex() { |
367 |
|
|
return inversionIndexContainer_.pop(); |
368 |
|
|
} |
369 |
|
|
|
370 |
|
|
std::vector<int> getInversionIndicesBefore(int index) { |
371 |
|
|
return inversionIndexContainer_.getIndicesBefore(index); |
372 |
|
|
} |
373 |
|
|
|
374 |
|
|
void releaseInversionIndex(int index) { |
375 |
|
|
inversionIndexContainer_.insert(index); |
376 |
|
|
} |
377 |
|
|
|
378 |
|
|
void releaseInversionIndex(int beginIndex, int endIndex) { |
379 |
|
|
inversionIndexContainer_.insert(beginIndex, endIndex); |
380 |
|
|
} |
381 |
|
|
|
382 |
|
|
void releaseInversionIndex(std::vector<int> indices) { |
383 |
|
|
inversionIndexContainer_.insert(indices); |
384 |
|
|
} |
385 |
|
|
|
386 |
gezelter |
507 |
private: |
387 |
gezelter |
246 |
|
388 |
gezelter |
507 |
IndexListContainer atomIndexContainer_; |
389 |
|
|
IndexListContainer rigidBodyIndexContainer_; |
390 |
gezelter |
1782 |
IndexListContainer cutoffGroupIndexContainer_; |
391 |
gezelter |
1953 |
IndexListContainer bondIndexContainer_; |
392 |
|
|
IndexListContainer bendIndexContainer_; |
393 |
|
|
IndexListContainer torsionIndexContainer_; |
394 |
|
|
IndexListContainer inversionIndexContainer_; |
395 |
gezelter |
507 |
}; |
396 |
gezelter |
246 |
|
397 |
gezelter |
1390 |
} //end namespace OpenMD |
398 |
gezelter |
246 |
#endif //UTILS_LOCALINDEXMANAGER_HPP |