1 |
< |
/* |
1 |
> |
/* |
2 |
|
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
|
* |
4 |
|
* The University of Notre Dame grants you ("Licensee") a |
6 |
|
* redistribute this software in source and binary code form, provided |
7 |
|
* that the following conditions are met: |
8 |
|
* |
9 |
< |
* 1. Acknowledgement of the program authors must be made in any |
10 |
< |
* publication of scientific results based in part on use of the |
11 |
< |
* program. An acceptable form of acknowledgement is citation of |
12 |
< |
* the article in which the program was described (Matthew |
13 |
< |
* A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
14 |
< |
* J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
15 |
< |
* Parallel Simulation Engine for Molecular Dynamics," |
16 |
< |
* J. Comput. Chem. 26, pp. 252-271 (2005)) |
17 |
< |
* |
18 |
< |
* 2. Redistributions of source code must retain the above copyright |
9 |
> |
* 1. Redistributions of source code must retain the above copyright |
10 |
|
* notice, this list of conditions and the following disclaimer. |
11 |
|
* |
12 |
< |
* 3. Redistributions in binary form must reproduce the above copyright |
12 |
> |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
|
* notice, this list of conditions and the following disclaimer in the |
14 |
|
* documentation and/or other materials provided with the |
15 |
|
* distribution. |
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 |
+ |
* |
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 |
+ |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). |
39 |
+ |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
+ |
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
41 |
|
*/ |
42 |
|
|
43 |
|
/** |
55 |
|
#include <list> |
56 |
|
#include <utility> |
57 |
|
|
58 |
< |
namespace oopse { |
58 |
> |
namespace OpenMD { |
59 |
|
|
60 |
< |
/** |
61 |
< |
* @class IndexListContainer |
62 |
< |
* @brief |
63 |
< |
* @todo documentation |
64 |
< |
*/ |
65 |
< |
class IndexListContainer{ |
66 |
< |
public: |
67 |
< |
static const int MAX_INTEGER = 2147483647; |
68 |
< |
typedef std::list<std::pair<int, int> >::iterator IndexListContainerIterator; |
60 |
> |
/** |
61 |
> |
* @class IndexListContainer |
62 |
> |
* @brief |
63 |
> |
* @todo documentation |
64 |
> |
*/ |
65 |
> |
class IndexListContainer{ |
66 |
> |
public: |
67 |
> |
static const int MAX_INTEGER = 2147483647; |
68 |
> |
typedef std::list<std::pair<int, int> >::iterator IndexListContainerIterator; |
69 |
|
|
70 |
|
|
71 |
< |
IndexListContainer(int minIndex = 0, int maxIndex = MAX_INTEGER) |
72 |
< |
:minIndex_(minIndex), maxIndex_(maxIndex) { |
71 |
> |
IndexListContainer(int minIndex = 0, int maxIndex = MAX_INTEGER) |
72 |
> |
:minIndex_(minIndex), maxIndex_(maxIndex) { |
73 |
|
|
74 |
< |
indexContainer_.push_back(std::make_pair(minIndex, maxIndex)); |
75 |
< |
} |
74 |
> |
indexContainer_.push_back(std::make_pair(minIndex, maxIndex)); |
75 |
> |
} |
76 |
|
|
77 |
< |
int pop() { |
77 |
> |
int pop() { |
78 |
|
|
79 |
< |
if (indexContainer_.empty()) { |
80 |
< |
std::cerr << "" << std::endl; |
81 |
< |
} |
79 |
> |
if (indexContainer_.empty()) { |
80 |
> |
std::cerr << "" << std::endl; |
81 |
> |
} |
82 |
|
|
83 |
< |
IndexListContainerIterator i = indexContainer_.begin(); |
83 |
< |
int result; |
83 |
> |
int result; |
84 |
|
|
85 |
< |
result = indexContainer_.front().first; |
85 |
> |
result = indexContainer_.front().first; |
86 |
|
|
87 |
< |
if (indexContainer_.front().first == indexContainer_.front().second) { |
88 |
< |
indexContainer_.pop_front(); |
89 |
< |
} else if (indexContainer_.front().first < indexContainer_.front().second) { |
90 |
< |
++indexContainer_.front().first; |
91 |
< |
} else { |
92 |
< |
std::cerr << "" << std::endl; |
93 |
< |
} |
87 |
> |
if (indexContainer_.front().first == indexContainer_.front().second) { |
88 |
> |
indexContainer_.pop_front(); |
89 |
> |
} else if (indexContainer_.front().first < indexContainer_.front().second) { |
90 |
> |
++indexContainer_.front().first; |
91 |
> |
} else { |
92 |
> |
std::cerr << "" << std::endl; |
93 |
> |
} |
94 |
|
|
95 |
< |
return result; |
96 |
< |
} |
95 |
> |
return result; |
96 |
> |
} |
97 |
|
|
98 |
|
|
99 |
< |
/** |
100 |
< |
* |
101 |
< |
*/ |
102 |
< |
void insert(int index) { |
103 |
< |
IndexListContainerIterator insertPos = internalInsert(index, index); |
104 |
< |
merge(insertPos); |
105 |
< |
} |
99 |
> |
/** |
100 |
> |
* |
101 |
> |
*/ |
102 |
> |
void insert(int index) { |
103 |
> |
IndexListContainerIterator insertPos = internalInsert(index, index); |
104 |
> |
merge(insertPos); |
105 |
> |
} |
106 |
|
|
107 |
< |
/** |
108 |
< |
* Reclaims an index range |
109 |
< |
* @param beginIndex |
110 |
< |
* @param endIndex |
111 |
< |
*/ |
112 |
< |
void insert(int beginIndex, int endIndex) { |
113 |
< |
IndexListContainerIterator insertPos = internalInsert(beginIndex, endIndex); |
114 |
< |
merge(insertPos); |
115 |
< |
} |
107 |
> |
/** |
108 |
> |
* Reclaims an index range |
109 |
> |
* @param beginIndex |
110 |
> |
* @param endIndex |
111 |
> |
*/ |
112 |
> |
void insert(int beginIndex, int endIndex) { |
113 |
> |
IndexListContainerIterator insertPos = internalInsert(beginIndex, endIndex); |
114 |
> |
merge(insertPos); |
115 |
> |
} |
116 |
|
|
117 |
< |
/** |
118 |
< |
* Reclaims an index array. |
119 |
< |
* @param indices |
120 |
< |
*/ |
121 |
< |
void insert(std::vector<int>& indices){ |
117 |
> |
/** |
118 |
> |
* Reclaims an index array. |
119 |
> |
* @param indices |
120 |
> |
*/ |
121 |
> |
void insert(std::vector<int>& indices){ |
122 |
|
|
123 |
< |
if (indices.empty()) { |
124 |
< |
return; |
125 |
< |
} |
123 |
> |
if (indices.empty()) { |
124 |
> |
return; |
125 |
> |
} |
126 |
|
|
127 |
< |
std::sort(indices.begin(), indices.end()); |
128 |
< |
std::unique(indices.begin(), indices.end()); |
127 |
> |
std::sort(indices.begin(), indices.end()); |
128 |
> |
std::unique(indices.begin(), indices.end()); |
129 |
|
|
130 |
< |
std::vector<int>::iterator i; |
131 |
< |
IndexListContainerIterator insertPos; |
132 |
< |
int beginIndex; |
130 |
> |
std::vector<int>::iterator i; |
131 |
> |
int beginIndex; |
132 |
|
|
133 |
< |
beginIndex = indices[0]; |
133 |
> |
beginIndex = indices[0]; |
134 |
|
|
135 |
< |
for ( i = indices.begin() + 1 ; i != indices.end(); ++i) { |
136 |
< |
if (*i != *(i -1) + 1) { |
137 |
< |
insert(beginIndex, *(i-1)); |
138 |
< |
beginIndex = *i; |
139 |
< |
} |
140 |
< |
} |
141 |
< |
|
143 |
< |
|
144 |
< |
} |
135 |
> |
for ( i = indices.begin() + 1 ; i != indices.end(); ++i) { |
136 |
> |
if (*i != *(i -1) + 1) { |
137 |
> |
insert(beginIndex, *(i-1)); |
138 |
> |
beginIndex = *i; |
139 |
> |
} |
140 |
> |
} |
141 |
> |
} |
142 |
|
|
143 |
< |
std::vector<int> getIndicesBefore(int index) { |
144 |
< |
std::vector<int> result; |
145 |
< |
IndexListContainerIterator i; |
143 |
> |
std::vector<int> getIndicesBefore(int index) { |
144 |
> |
std::vector<int> result; |
145 |
> |
IndexListContainerIterator i; |
146 |
|
|
147 |
< |
for(i = indexContainer_.begin(); i != indexContainer_.end(); ++i) { |
148 |
< |
if ((*i).first > index) { |
149 |
< |
//we locate the node whose minimum index is greater that index |
150 |
< |
indexContainer_.erase(indexContainer_.begin(), i); |
151 |
< |
break; |
152 |
< |
} else if ((*i).second < index) { |
153 |
< |
//The biggest index current node hold is less than the index we want |
154 |
< |
for (int j = (*i).first; j <= (*i).second; ++j) { |
155 |
< |
result.push_back(j); |
156 |
< |
} |
157 |
< |
continue; |
158 |
< |
} else if ((*i).first == (*i).second) { |
159 |
< |
//the index happen to equal to a node which only contains one index |
160 |
< |
result.push_back((*i).first); |
161 |
< |
indexContainer_.erase(indexContainer_.begin(), i); |
162 |
< |
break; |
163 |
< |
} else { |
147 |
> |
for(i = indexContainer_.begin(); i != indexContainer_.end(); ++i) { |
148 |
> |
if ((*i).first > index) { |
149 |
> |
//we locate the node whose minimum index is greater that index |
150 |
> |
indexContainer_.erase(indexContainer_.begin(), i); |
151 |
> |
break; |
152 |
> |
} else if ((*i).second < index) { |
153 |
> |
//The biggest index current node hold is less than the index we want |
154 |
> |
for (int j = (*i).first; j <= (*i).second; ++j) { |
155 |
> |
result.push_back(j); |
156 |
> |
} |
157 |
> |
continue; |
158 |
> |
} else if ((*i).first == (*i).second) { |
159 |
> |
//the index happen to equal to a node which only contains one index |
160 |
> |
result.push_back((*i).first); |
161 |
> |
indexContainer_.erase(indexContainer_.begin(), i); |
162 |
> |
break; |
163 |
> |
} else { |
164 |
|
|
165 |
< |
for (int j = (*i).first; j < index; ++j) { |
166 |
< |
result.push_back(j); |
167 |
< |
} |
165 |
> |
for (int j = (*i).first; j < index; ++j) { |
166 |
> |
result.push_back(j); |
167 |
> |
} |
168 |
|
|
169 |
< |
(*i).first = index; |
170 |
< |
indexContainer_.erase(indexContainer_.begin(), i); |
171 |
< |
break; |
172 |
< |
} |
169 |
> |
(*i).first = index; |
170 |
> |
indexContainer_.erase(indexContainer_.begin(), i); |
171 |
> |
break; |
172 |
> |
} |
173 |
|
|
174 |
< |
} |
174 |
> |
} |
175 |
|
|
176 |
< |
return result; |
176 |
> |
return result; |
177 |
|
|
178 |
< |
} |
178 |
> |
} |
179 |
|
|
180 |
< |
int getMaxIndex() { |
181 |
< |
return maxIndex_; |
182 |
< |
} |
180 |
> |
int getMaxIndex() { |
181 |
> |
return maxIndex_; |
182 |
> |
} |
183 |
|
|
184 |
< |
private: |
184 |
> |
private: |
185 |
|
|
186 |
< |
IndexListContainerIterator internalInsert(int beginIndex, int endIndex) { |
186 |
> |
IndexListContainerIterator internalInsert(int beginIndex, int endIndex) { |
187 |
|
|
188 |
< |
if (beginIndex > endIndex) { |
189 |
< |
std::swap(beginIndex, endIndex); |
190 |
< |
std::cerr << "" << std::endl; |
191 |
< |
} |
195 |
< |
|
196 |
< |
if (endIndex > maxIndex_) { |
197 |
< |
std::cerr << "" << std::endl; |
198 |
< |
} |
188 |
> |
if (beginIndex > endIndex) { |
189 |
> |
std::swap(beginIndex, endIndex); |
190 |
> |
std::cerr << "" << std::endl; |
191 |
> |
} |
192 |
|
|
193 |
< |
|
194 |
< |
IndexListContainerIterator j; |
193 |
> |
if (endIndex > maxIndex_) { |
194 |
> |
std::cerr << "" << std::endl; |
195 |
> |
} |
196 |
> |
|
197 |
> |
IndexListContainerIterator i = indexContainer_.begin(); |
198 |
> |
for (; i != indexContainer_.end(); ++i) { |
199 |
> |
if ((*i).first > endIndex) { |
200 |
> |
indexContainer_.insert(i, std::make_pair(beginIndex, endIndex)); |
201 |
> |
return --i; |
202 |
> |
} else if ((*i).second < beginIndex) { |
203 |
> |
continue; |
204 |
> |
} else { |
205 |
> |
std::cerr << "" << std::endl; |
206 |
> |
} |
207 |
> |
} |
208 |
|
|
209 |
< |
IndexListContainerIterator i = indexContainer_.begin(); |
210 |
< |
for (; i != indexContainer_.end(); ++i) { |
205 |
< |
if ((*i).first > endIndex) { |
206 |
< |
indexContainer_.insert(i, std::make_pair(beginIndex, endIndex)); |
207 |
< |
return --i; |
208 |
< |
} else if ((*i).second < beginIndex) { |
209 |
< |
continue; |
210 |
< |
} else { |
211 |
< |
std::cerr << "" << std::endl; |
212 |
< |
} |
213 |
< |
} |
209 |
> |
indexContainer_.push_back(std::make_pair(beginIndex, endIndex)); |
210 |
> |
return --indexContainer_.end(); |
211 |
|
|
212 |
< |
indexContainer_.push_back(std::make_pair(beginIndex, endIndex)); |
216 |
< |
return --indexContainer_.end(); |
212 |
> |
} |
213 |
|
|
214 |
< |
} |
214 |
> |
void merge(IndexListContainerIterator i) { |
215 |
> |
IndexListContainerIterator j; |
216 |
|
|
217 |
< |
void merge(IndexListContainerIterator i) { |
218 |
< |
IndexListContainerIterator j; |
217 |
> |
//check whether current node can be merged with its previous node |
218 |
> |
if ( i != indexContainer_.begin()) { |
219 |
> |
j = i; |
220 |
> |
--j; |
221 |
> |
if (j != indexContainer_.begin() && (*j).second + 1 == (*i).first) { |
222 |
> |
(*i).first = (*j).first; |
223 |
> |
indexContainer_.erase(j); |
224 |
> |
} |
225 |
> |
} |
226 |
|
|
227 |
< |
//check whether current node can be merged with its previous node |
228 |
< |
if ( i != indexContainer_.begin()) { |
229 |
< |
j = i; |
230 |
< |
--j; |
227 |
< |
if (j != indexContainer_.begin() && (*j).second + 1 == (*i).first) { |
228 |
< |
(*i).first = (*j).first; |
229 |
< |
indexContainer_.erase(j); |
230 |
< |
} |
231 |
< |
} |
227 |
> |
//check whether current node can be merged with its next node |
228 |
> |
if ( i != indexContainer_.end()) { |
229 |
> |
j = i; |
230 |
> |
++j; |
231 |
|
|
232 |
< |
//check whether current node can be merged with its next node |
233 |
< |
if ( i != indexContainer_.end()) { |
234 |
< |
j = i; |
235 |
< |
++j; |
232 |
> |
if (j != indexContainer_.end() && (*i).second + 1 == (*j).first) { |
233 |
> |
(*i).second = (*j).second; |
234 |
> |
indexContainer_.erase(j); |
235 |
> |
} |
236 |
> |
} |
237 |
|
|
238 |
< |
if (j != indexContainer_.end() && (*i).second + 1 == (*j).first) { |
239 |
< |
(*i).second = (*j).second; |
240 |
< |
indexContainer_.erase(j); |
241 |
< |
} |
242 |
< |
} |
238 |
> |
} |
239 |
> |
int minIndex_; |
240 |
> |
int maxIndex_; |
241 |
> |
std::list<std::pair<int, int> > indexContainer_; |
242 |
> |
}; |
243 |
|
|
244 |
– |
} |
245 |
– |
int minIndex_; |
246 |
– |
int maxIndex_; |
247 |
– |
std::list<std::pair<int, int> > indexContainer_; |
248 |
– |
}; |
244 |
|
|
245 |
+ |
/** |
246 |
+ |
* @class LocalIndexManager LocalIndexManager.hpp "utils/LocalIndexManager.hpp" |
247 |
+ |
* @brief |
248 |
+ |
*/ |
249 |
+ |
class LocalIndexManager { |
250 |
+ |
public: |
251 |
+ |
|
252 |
+ |
int getNextAtomIndex() { |
253 |
+ |
return atomIndexContainer_.pop(); |
254 |
+ |
} |
255 |
|
|
256 |
< |
/** |
257 |
< |
* @class LocalIndexManager LocalIndexManager.hpp "utils/LocalIndexManager.hpp" |
258 |
< |
* @brief |
254 |
< |
*/ |
255 |
< |
class LocalIndexManager { |
256 |
< |
public: |
256 |
> |
std::vector<int> getAtomIndicesBefore(int index) { |
257 |
> |
return atomIndexContainer_.getIndicesBefore(index); |
258 |
> |
} |
259 |
|
|
260 |
< |
int getNextAtomIndex() { |
261 |
< |
return atomIndexContainer_.pop(); |
262 |
< |
} |
260 |
> |
void releaseAtomIndex(int index) { |
261 |
> |
atomIndexContainer_.insert(index); |
262 |
> |
} |
263 |
|
|
264 |
< |
std::vector<int> getAtomIndicesBefore(int index) { |
265 |
< |
return atomIndexContainer_.getIndicesBefore(index); |
266 |
< |
} |
264 |
> |
void releaseAtomIndex(int beginIndex, int endIndex) { |
265 |
> |
atomIndexContainer_.insert(beginIndex, endIndex); |
266 |
> |
} |
267 |
> |
|
268 |
> |
void releaseAtomIndex(std::vector<int> indices) { |
269 |
> |
atomIndexContainer_.insert(indices); |
270 |
> |
} |
271 |
> |
|
272 |
> |
int getNextRigidBodyIndex() { |
273 |
> |
return rigidBodyIndexContainer_.pop(); |
274 |
> |
} |
275 |
> |
|
276 |
> |
std::vector<int> getRigidBodyIndicesBefore(int index) { |
277 |
> |
return rigidBodyIndexContainer_.getIndicesBefore(index); |
278 |
> |
} |
279 |
|
|
280 |
< |
void releaseAtomIndex(int index) { |
281 |
< |
atomIndexContainer_.insert(index); |
282 |
< |
} |
280 |
> |
void releaseRigidBodyIndex(int index) { |
281 |
> |
rigidBodyIndexContainer_.insert(index); |
282 |
> |
} |
283 |
|
|
284 |
< |
void releaseAtomIndex(int beginIndex, int endIndex) { |
285 |
< |
atomIndexContainer_.insert(beginIndex, endIndex); |
286 |
< |
} |
284 |
> |
void releaseRigidBodyIndex(int beginIndex, int endIndex) { |
285 |
> |
rigidBodyIndexContainer_.insert(beginIndex, endIndex); |
286 |
> |
} |
287 |
|
|
288 |
< |
void releaseAtomIndex(std::vector<int> indices) { |
289 |
< |
atomIndexContainer_.insert(indices); |
290 |
< |
} |
288 |
> |
void releaseRigidBodyIndex(std::vector<int> indices) { |
289 |
> |
rigidBodyIndexContainer_.insert(indices); |
290 |
> |
} |
291 |
|
|
292 |
< |
int getNextRigidBodyIndex() { |
293 |
< |
return rigidBodyIndexContainer_.pop(); |
294 |
< |
} |
292 |
> |
int getNextCutoffGroupIndex() { |
293 |
> |
return cutoffGroupIndexContainer_.pop(); |
294 |
> |
} |
295 |
|
|
296 |
< |
std::vector<int> getRigidBodyIndicesBefore(int index) { |
297 |
< |
return rigidBodyIndexContainer_.getIndicesBefore(index); |
298 |
< |
} |
296 |
> |
std::vector<int> getCutoffGroupIndicesBefore(int index) { |
297 |
> |
return cutoffGroupIndexContainer_.getIndicesBefore(index); |
298 |
> |
} |
299 |
|
|
300 |
< |
void releaseRigidBodyIndex(int index) { |
301 |
< |
rigidBodyIndexContainer_.insert(index); |
302 |
< |
} |
300 |
> |
void releaseCutoffGroupIndex(int index) { |
301 |
> |
cutoffGroupIndexContainer_.insert(index); |
302 |
> |
} |
303 |
|
|
304 |
< |
void releaseRigidBodyIndex(int beginIndex, int endIndex) { |
305 |
< |
rigidBodyIndexContainer_.insert(beginIndex, endIndex); |
306 |
< |
} |
304 |
> |
void releaseCutoffGroupIndex(int beginIndex, int endIndex) { |
305 |
> |
cutoffGroupIndexContainer_.insert(beginIndex, endIndex); |
306 |
> |
} |
307 |
|
|
308 |
< |
void releaseRigidBodyIndex(std::vector<int> indices) { |
309 |
< |
rigidBodyIndexContainer_.insert(indices); |
310 |
< |
} |
308 |
> |
void releaseCutoffGroupIndex(std::vector<int> indices) { |
309 |
> |
cutoffGroupIndexContainer_.insert(indices); |
310 |
> |
} |
311 |
|
|
312 |
< |
private: |
312 |
> |
private: |
313 |
|
|
314 |
< |
IndexListContainer atomIndexContainer_; |
315 |
< |
IndexListContainer rigidBodyIndexContainer_; |
316 |
< |
}; |
314 |
> |
IndexListContainer atomIndexContainer_; |
315 |
> |
IndexListContainer rigidBodyIndexContainer_; |
316 |
> |
IndexListContainer cutoffGroupIndexContainer_; |
317 |
> |
}; |
318 |
|
|
319 |
< |
} //end namespace oopse |
319 |
> |
} //end namespace OpenMD |
320 |
|
#endif //UTILS_LOCALINDEXMANAGER_HPP |