1 |
/* |
2 |
* 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 |
* 1. Redistributions of source code must retain the above copyright |
10 |
* notice, this list of conditions and the following disclaimer. |
11 |
* |
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. |
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 |
* |
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, 24107 (2008). |
39 |
* [4] Vardeman & Gezelter, in progress (2009). |
40 |
*/ |
41 |
|
42 |
/** |
43 |
* @file DataStorage.cpp |
44 |
* @author tlin |
45 |
* @date 10/26/2004 |
46 |
* @time 11:56am |
47 |
* @version 1.0 |
48 |
*/ |
49 |
|
50 |
#include "brains/DataStorage.hpp" |
51 |
|
52 |
namespace OpenMD { |
53 |
|
54 |
DataStorage::DataStorage() : size_(0), storageLayout_(0){ |
55 |
|
56 |
} |
57 |
|
58 |
DataStorage::DataStorage(int size, int storageLayout) : size_(size){ |
59 |
setStorageLayout(storageLayout); |
60 |
resize(size); |
61 |
} |
62 |
|
63 |
int DataStorage::getSize() { |
64 |
|
65 |
if (storageLayout_ & dslPosition && position.size() != size_) { |
66 |
//error |
67 |
std::cerr << "size does not match"<< std::endl; |
68 |
} |
69 |
|
70 |
if (storageLayout_ & dslVelocity && velocity.size() != size_) { |
71 |
//error |
72 |
std::cerr << "size does not match"<< std::endl; |
73 |
} |
74 |
|
75 |
if (storageLayout_ & dslAmat && aMat.size() != size_) { |
76 |
//error |
77 |
std::cerr << "size does not match"<< std::endl; |
78 |
} |
79 |
|
80 |
if (storageLayout_ & dslAngularMomentum && angularMomentum.size() != size_) { |
81 |
//error |
82 |
std::cerr << "size does not match"<< std::endl; |
83 |
} |
84 |
|
85 |
if (storageLayout_ & dslElectroFrame && electroFrame.size() != size_) { |
86 |
//error |
87 |
std::cerr << "size does not match"<< std::endl; |
88 |
} |
89 |
|
90 |
if (storageLayout_ & dslZAngle && zAngle.size() != size_) { |
91 |
//error |
92 |
std::cerr << "size does not match"<< std::endl; |
93 |
} |
94 |
|
95 |
if (storageLayout_ & dslForce && force.size() != size_) { |
96 |
//error |
97 |
std::cerr << "size does not match"<< std::endl; |
98 |
} |
99 |
|
100 |
if (storageLayout_ & dslTorque && torque.size() != size_) { |
101 |
//error |
102 |
std::cerr << "size does not match"<< std::endl; |
103 |
} |
104 |
|
105 |
if (storageLayout_ & dslParticlePot && particlePot.size() != size_) { |
106 |
//error |
107 |
std::cerr << "size does not match"<< std::endl; |
108 |
} |
109 |
|
110 |
if (storageLayout_ & dslDensity && density.size() != size_) { |
111 |
//error |
112 |
std::cerr << "size does not match"<< std::endl; |
113 |
} |
114 |
|
115 |
if (storageLayout_ & dslFunctional && functional.size() != size_) { |
116 |
//error |
117 |
std::cerr << "size does not match"<< std::endl; |
118 |
} |
119 |
|
120 |
if (storageLayout_ & dslFunctionalDerivative && functionalDerivative.size() != size_) { |
121 |
//error |
122 |
std::cerr << "size does not match"<< std::endl; |
123 |
} |
124 |
|
125 |
if (storageLayout_ & dslElectricField && electricField.size() != size_) { |
126 |
//error |
127 |
std::cerr << "size does not match"<< std::endl; |
128 |
} |
129 |
|
130 |
return size_; |
131 |
|
132 |
} |
133 |
|
134 |
void DataStorage::resize(int newSize) { |
135 |
|
136 |
if (storageLayout_ & dslPosition) { |
137 |
internalResize(position, newSize); |
138 |
} |
139 |
|
140 |
if (storageLayout_ & dslVelocity) { |
141 |
internalResize(velocity, newSize); |
142 |
} |
143 |
|
144 |
if (storageLayout_ & dslAmat) { |
145 |
internalResize(aMat, newSize); |
146 |
} |
147 |
|
148 |
if (storageLayout_ & dslAngularMomentum) { |
149 |
internalResize(angularMomentum, newSize); |
150 |
} |
151 |
|
152 |
if (storageLayout_ & dslElectroFrame) { |
153 |
internalResize(electroFrame, newSize); |
154 |
} |
155 |
|
156 |
if (storageLayout_ & dslZAngle) { |
157 |
internalResize(zAngle, newSize); |
158 |
} |
159 |
|
160 |
if (storageLayout_ & dslForce) { |
161 |
internalResize(force, newSize); |
162 |
} |
163 |
|
164 |
if (storageLayout_ & dslTorque) { |
165 |
internalResize(torque, newSize); |
166 |
} |
167 |
|
168 |
if (storageLayout_ & dslParticlePot) { |
169 |
internalResize(particlePot, newSize); |
170 |
} |
171 |
|
172 |
if (storageLayout_ & dslDensity) { |
173 |
internalResize(density, newSize); |
174 |
} |
175 |
|
176 |
if (storageLayout_ & dslFunctional) { |
177 |
internalResize(functional, newSize); |
178 |
} |
179 |
|
180 |
if (storageLayout_ & dslFunctionalDerivative) { |
181 |
internalResize(functionalDerivative, newSize); |
182 |
} |
183 |
|
184 |
if (storageLayout_ & dslElectricField) { |
185 |
internalResize(electricField, newSize); |
186 |
} |
187 |
|
188 |
size_ = newSize; |
189 |
} |
190 |
|
191 |
void DataStorage::reserve(int size) { |
192 |
if (storageLayout_ & dslPosition) { |
193 |
position.reserve(size); |
194 |
} |
195 |
|
196 |
if (storageLayout_ & dslVelocity) { |
197 |
velocity.reserve(size); |
198 |
} |
199 |
|
200 |
if (storageLayout_ & dslAmat) { |
201 |
aMat.reserve(size); |
202 |
} |
203 |
|
204 |
if (storageLayout_ & dslAngularMomentum) { |
205 |
angularMomentum.reserve(size); |
206 |
} |
207 |
|
208 |
if (storageLayout_ & dslElectroFrame) { |
209 |
electroFrame.reserve(size); |
210 |
} |
211 |
|
212 |
if (storageLayout_ & dslZAngle) { |
213 |
zAngle.reserve(size); |
214 |
} |
215 |
|
216 |
if (storageLayout_ & dslForce) { |
217 |
force.reserve(size); |
218 |
} |
219 |
|
220 |
if (storageLayout_ & dslTorque) { |
221 |
torque.reserve(size); |
222 |
} |
223 |
|
224 |
if (storageLayout_ & dslParticlePot) { |
225 |
particlePot.reserve(size); |
226 |
} |
227 |
|
228 |
if (storageLayout_ & dslDensity) { |
229 |
density.reserve(size); |
230 |
} |
231 |
|
232 |
if (storageLayout_ & dslFunctional) { |
233 |
functional.reserve(size); |
234 |
} |
235 |
|
236 |
if (storageLayout_ & dslFunctionalDerivative) { |
237 |
functionalDerivative.reserve(size); |
238 |
} |
239 |
|
240 |
if (storageLayout_ & dslElectricField) { |
241 |
electricField.reserve(size); |
242 |
} |
243 |
|
244 |
} |
245 |
|
246 |
void DataStorage::copy(int source, int num, int target) { |
247 |
if (num + target > size_ ) { |
248 |
//error |
249 |
} |
250 |
|
251 |
if (storageLayout_ & dslPosition) { |
252 |
internalCopy(position, source, num, target); |
253 |
} |
254 |
|
255 |
if (storageLayout_ & dslVelocity) { |
256 |
internalCopy(velocity, source, num, target); |
257 |
} |
258 |
|
259 |
if (storageLayout_ & dslAmat) { |
260 |
internalCopy(aMat, source, num, target); |
261 |
} |
262 |
|
263 |
if (storageLayout_ & dslAngularMomentum) { |
264 |
internalCopy(angularMomentum, source, num, target); |
265 |
} |
266 |
|
267 |
if (storageLayout_ & dslElectroFrame) { |
268 |
internalCopy(electroFrame, source, num, target); |
269 |
} |
270 |
|
271 |
if (storageLayout_ & dslZAngle) { |
272 |
internalCopy(zAngle, source, num, target); |
273 |
} |
274 |
|
275 |
if (storageLayout_ & dslForce) { |
276 |
internalCopy(force, source, num, target); |
277 |
} |
278 |
|
279 |
if (storageLayout_ & dslTorque) { |
280 |
internalCopy(torque, source, num, target); |
281 |
} |
282 |
|
283 |
if (storageLayout_ & dslParticlePot) { |
284 |
internalCopy(particlePot, source, num, target); |
285 |
} |
286 |
|
287 |
if (storageLayout_ & dslDensity) { |
288 |
internalCopy(density, source, num, target); |
289 |
} |
290 |
|
291 |
if (storageLayout_ & dslFunctional) { |
292 |
internalCopy(functional, source, num, target); |
293 |
} |
294 |
|
295 |
if (storageLayout_ & dslFunctionalDerivative) { |
296 |
internalCopy(functionalDerivative, source, num, target); |
297 |
} |
298 |
|
299 |
if (storageLayout_ & dslElectricField) { |
300 |
internalCopy(electricField, source, num, target); |
301 |
} |
302 |
} |
303 |
|
304 |
int DataStorage::getStorageLayout() { |
305 |
return storageLayout_; |
306 |
} |
307 |
|
308 |
void DataStorage::setStorageLayout(int layout) { |
309 |
storageLayout_ = layout; |
310 |
resize(size_); |
311 |
} |
312 |
|
313 |
RealType* DataStorage::getArrayPointer(int whichArray) { |
314 |
|
315 |
switch (whichArray) { |
316 |
case dslPosition: |
317 |
return internalGetArrayPointer(position); |
318 |
break; |
319 |
|
320 |
case dslVelocity: |
321 |
return internalGetArrayPointer(velocity); |
322 |
break; |
323 |
|
324 |
case dslAmat: |
325 |
return internalGetArrayPointer(aMat); |
326 |
break; |
327 |
|
328 |
case dslAngularMomentum: |
329 |
return internalGetArrayPointer(angularMomentum); |
330 |
break; |
331 |
|
332 |
case dslElectroFrame: |
333 |
return internalGetArrayPointer(electroFrame); |
334 |
break; |
335 |
|
336 |
case dslZAngle: |
337 |
return internalGetArrayPointer(zAngle); |
338 |
break; |
339 |
|
340 |
case dslForce: |
341 |
return internalGetArrayPointer(force); |
342 |
break; |
343 |
|
344 |
case dslTorque: |
345 |
return internalGetArrayPointer(torque); |
346 |
break; |
347 |
|
348 |
case dslParticlePot: |
349 |
return internalGetArrayPointer(particlePot); |
350 |
break; |
351 |
|
352 |
case dslDensity: |
353 |
return internalGetArrayPointer(density); |
354 |
break; |
355 |
|
356 |
case dslFunctional: |
357 |
return internalGetArrayPointer(functional); |
358 |
break; |
359 |
|
360 |
case dslFunctionalDerivative: |
361 |
return internalGetArrayPointer(functionalDerivative); |
362 |
break; |
363 |
|
364 |
case dslElectricField: |
365 |
return internalGetArrayPointer(electricField); |
366 |
break; |
367 |
|
368 |
default: |
369 |
//error message |
370 |
return NULL; |
371 |
|
372 |
} |
373 |
} |
374 |
|
375 |
RealType* DataStorage::internalGetArrayPointer(std::vector<Vector3d>& v) { |
376 |
if (v.size() == 0) { |
377 |
return NULL; |
378 |
} else { |
379 |
return v[0].getArrayPointer(); |
380 |
} |
381 |
} |
382 |
|
383 |
RealType* DataStorage::internalGetArrayPointer(std::vector<RotMat3x3d>& v) { |
384 |
if (v.size() == 0) { |
385 |
return NULL; |
386 |
} else { |
387 |
return v[0].getArrayPointer(); |
388 |
} |
389 |
|
390 |
} |
391 |
|
392 |
RealType* DataStorage::internalGetArrayPointer(std::vector<RealType>& v) { |
393 |
if (v.size() == 0) { |
394 |
return NULL; |
395 |
} else { |
396 |
return &(v[0]); |
397 |
} |
398 |
|
399 |
} |
400 |
|
401 |
template<typename T> |
402 |
void DataStorage::internalResize(std::vector<T>& v, int newSize){ |
403 |
int oldSize = v.size(); |
404 |
|
405 |
if (oldSize == newSize) { |
406 |
return; |
407 |
} else if (oldSize < newSize) { |
408 |
v.insert(v.end(), newSize-oldSize, T()); |
409 |
} else { |
410 |
typename std::vector<T>::iterator i; |
411 |
i = v.begin(); |
412 |
std::advance(i, newSize); |
413 |
v.erase(i, v.end()); |
414 |
} |
415 |
} |
416 |
|
417 |
template<typename T> |
418 |
void DataStorage::internalCopy(std::vector<T>& v, int source, int num, int target) { |
419 |
typename std::vector<T>::iterator first; |
420 |
typename std::vector<T>::iterator last; |
421 |
typename std::vector<T>::iterator result; |
422 |
|
423 |
first = v.begin(); |
424 |
last = v.begin(); |
425 |
result = v.begin(); |
426 |
|
427 |
std::advance(first, source); |
428 |
//STL algorithm use half opened range |
429 |
std::advance(last, num + 1); |
430 |
std::advance(result, target ); |
431 |
|
432 |
std::copy(first, last, result); |
433 |
} |
434 |
|
435 |
int DataStorage::getBytesPerStuntDouble(int layout) { |
436 |
int bytes = 0; |
437 |
if (layout & dslPosition) { |
438 |
bytes += sizeof(Vector3d); |
439 |
} |
440 |
if (layout & dslVelocity) { |
441 |
bytes += sizeof(Vector3d); |
442 |
} |
443 |
if (layout & dslAmat) { |
444 |
bytes += sizeof(RotMat3x3d); |
445 |
} |
446 |
if (layout & dslAngularMomentum) { |
447 |
bytes += sizeof(Vector3d); |
448 |
} |
449 |
if (layout & dslElectroFrame) { |
450 |
bytes += sizeof(Mat3x3d); |
451 |
} |
452 |
if (layout & dslZAngle) { |
453 |
bytes += sizeof(RealType); |
454 |
} |
455 |
if (layout & dslForce) { |
456 |
bytes += sizeof(Vector3d); |
457 |
} |
458 |
if (layout & dslTorque) { |
459 |
bytes += sizeof(Vector3d); |
460 |
} |
461 |
if (layout & dslParticlePot) { |
462 |
bytes += sizeof(RealType); |
463 |
} |
464 |
if (layout & dslDensity) { |
465 |
bytes += sizeof(RealType); |
466 |
} |
467 |
if (layout & dslFunctional) { |
468 |
bytes += sizeof(RealType); |
469 |
} |
470 |
if (layout & dslFunctionalDerivative) { |
471 |
bytes += sizeof(RealType); |
472 |
} |
473 |
if (layout & dslElectricField) { |
474 |
bytes += sizeof(Vector3d); |
475 |
} |
476 |
|
477 |
return bytes; |
478 |
} |
479 |
|
480 |
} |