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, 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 |
/** |
44 |
* @file DataStorage.cpp |
45 |
* @author tlin |
46 |
* @date 10/26/2004 |
47 |
* @version 1.0 |
48 |
*/ |
49 |
|
50 |
#include "brains/DataStorage.hpp" |
51 |
using namespace std; |
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 |
cerr << "size does not match"<< endl; |
68 |
} |
69 |
|
70 |
if (storageLayout_ & dslVelocity && velocity.size() != size_) { |
71 |
//error |
72 |
cerr << "size does not match"<< endl; |
73 |
} |
74 |
|
75 |
if (storageLayout_ & dslForce && force.size() != size_) { |
76 |
//error |
77 |
cerr << "size does not match"<< endl; |
78 |
} |
79 |
|
80 |
if (storageLayout_ & dslAmat && aMat.size() != size_) { |
81 |
//error |
82 |
cerr << "size does not match"<< endl; |
83 |
} |
84 |
|
85 |
if (storageLayout_ & dslAngularMomentum && angularMomentum.size() != size_) { |
86 |
//error |
87 |
cerr << "size does not match"<< endl; |
88 |
} |
89 |
|
90 |
if (storageLayout_ & dslTorque && torque.size() != size_) { |
91 |
//error |
92 |
cerr << "size does not match"<< endl; |
93 |
} |
94 |
|
95 |
if (storageLayout_ & dslParticlePot && particlePot.size() != size_) { |
96 |
//error |
97 |
cerr << "size does not match"<< endl; |
98 |
} |
99 |
|
100 |
if (storageLayout_ & dslDensity && density.size() != size_) { |
101 |
//error |
102 |
cerr << "size does not match"<< endl; |
103 |
} |
104 |
|
105 |
if (storageLayout_ & dslFunctional && functional.size() != size_) { |
106 |
//error |
107 |
cerr << "size does not match"<< endl; |
108 |
} |
109 |
|
110 |
if (storageLayout_ & dslFunctionalDerivative && functionalDerivative.size() != size_) { |
111 |
//error |
112 |
cerr << "size does not match"<< endl; |
113 |
} |
114 |
|
115 |
if (storageLayout_ & dslDipole && dipole.size() != size_) { |
116 |
//error |
117 |
cerr << "size does not match"<< endl; |
118 |
} |
119 |
|
120 |
if (storageLayout_ & dslQuadrupole && quadrupole.size() != size_) { |
121 |
//error |
122 |
cerr << "size does not match"<< endl; |
123 |
} |
124 |
|
125 |
if (storageLayout_ & dslElectricField && electricField.size() != size_) { |
126 |
//error |
127 |
cerr << "size does not match"<< endl; |
128 |
} |
129 |
|
130 |
if (storageLayout_ & dslSkippedCharge && skippedCharge.size() != size_) { |
131 |
//error |
132 |
cerr << "size does not match"<< endl; |
133 |
} |
134 |
|
135 |
if (storageLayout_ & dslFlucQPosition && flucQPos.size() != size_) { |
136 |
//error |
137 |
cerr << "size does not match"<< endl; |
138 |
} |
139 |
|
140 |
if (storageLayout_ & dslFlucQVelocity && flucQVel.size() != size_) { |
141 |
//error |
142 |
cerr << "size does not match"<< endl; |
143 |
} |
144 |
|
145 |
if (storageLayout_ & dslFlucQForce && flucQFrc.size() != size_) { |
146 |
//error |
147 |
cerr << "size does not match"<< endl; |
148 |
} |
149 |
|
150 |
if (storageLayout_ & dslSitePotential && sitePotential.size() != size_) { |
151 |
//error |
152 |
cerr << "size does not match"<< endl; |
153 |
} |
154 |
|
155 |
return size_; |
156 |
|
157 |
} |
158 |
|
159 |
void DataStorage::resize(int newSize) { |
160 |
|
161 |
if (storageLayout_ & dslPosition) { |
162 |
internalResize(position, newSize); |
163 |
} |
164 |
|
165 |
if (storageLayout_ & dslVelocity) { |
166 |
internalResize(velocity, newSize); |
167 |
} |
168 |
|
169 |
if (storageLayout_ & dslForce) { |
170 |
internalResize(force, newSize); |
171 |
} |
172 |
|
173 |
if (storageLayout_ & dslAmat) { |
174 |
internalResize(aMat, newSize); |
175 |
} |
176 |
|
177 |
if (storageLayout_ & dslAngularMomentum) { |
178 |
internalResize(angularMomentum, newSize); |
179 |
} |
180 |
|
181 |
if (storageLayout_ & dslTorque) { |
182 |
internalResize(torque, newSize); |
183 |
} |
184 |
|
185 |
if (storageLayout_ & dslParticlePot) { |
186 |
internalResize(particlePot, newSize); |
187 |
} |
188 |
|
189 |
if (storageLayout_ & dslDensity) { |
190 |
internalResize(density, newSize); |
191 |
} |
192 |
|
193 |
if (storageLayout_ & dslFunctional) { |
194 |
internalResize(functional, newSize); |
195 |
} |
196 |
|
197 |
if (storageLayout_ & dslFunctionalDerivative) { |
198 |
internalResize(functionalDerivative, newSize); |
199 |
} |
200 |
|
201 |
if (storageLayout_ & dslDipole) { |
202 |
internalResize(dipole, newSize); |
203 |
} |
204 |
|
205 |
if (storageLayout_ & dslQuadrupole) { |
206 |
internalResize(quadrupole, newSize); |
207 |
} |
208 |
|
209 |
if (storageLayout_ & dslElectricField) { |
210 |
internalResize(electricField, newSize); |
211 |
} |
212 |
|
213 |
if (storageLayout_ & dslSkippedCharge) { |
214 |
internalResize(skippedCharge, newSize); |
215 |
} |
216 |
|
217 |
if (storageLayout_ & dslFlucQPosition) { |
218 |
internalResize(flucQPos, newSize); |
219 |
} |
220 |
|
221 |
if (storageLayout_ & dslFlucQVelocity) { |
222 |
internalResize(flucQVel, newSize); |
223 |
} |
224 |
|
225 |
if (storageLayout_ & dslFlucQForce) { |
226 |
internalResize(flucQFrc, newSize); |
227 |
} |
228 |
|
229 |
if (storageLayout_ & dslSitePotential) { |
230 |
internalResize(sitePotential, newSize); |
231 |
} |
232 |
|
233 |
size_ = newSize; |
234 |
} |
235 |
|
236 |
void DataStorage::reserve(int size) { |
237 |
if (storageLayout_ & dslPosition) { |
238 |
position.reserve(size); |
239 |
} |
240 |
|
241 |
if (storageLayout_ & dslVelocity) { |
242 |
velocity.reserve(size); |
243 |
} |
244 |
|
245 |
if (storageLayout_ & dslForce) { |
246 |
force.reserve(size); |
247 |
} |
248 |
|
249 |
if (storageLayout_ & dslAmat) { |
250 |
aMat.reserve(size); |
251 |
} |
252 |
|
253 |
if (storageLayout_ & dslAngularMomentum) { |
254 |
angularMomentum.reserve(size); |
255 |
} |
256 |
|
257 |
if (storageLayout_ & dslTorque) { |
258 |
torque.reserve(size); |
259 |
} |
260 |
|
261 |
if (storageLayout_ & dslParticlePot) { |
262 |
particlePot.reserve(size); |
263 |
} |
264 |
|
265 |
if (storageLayout_ & dslDensity) { |
266 |
density.reserve(size); |
267 |
} |
268 |
|
269 |
if (storageLayout_ & dslFunctional) { |
270 |
functional.reserve(size); |
271 |
} |
272 |
|
273 |
if (storageLayout_ & dslFunctionalDerivative) { |
274 |
functionalDerivative.reserve(size); |
275 |
} |
276 |
|
277 |
if (storageLayout_ & dslDipole) { |
278 |
dipole.reserve(size); |
279 |
} |
280 |
|
281 |
if (storageLayout_ & dslQuadrupole) { |
282 |
quadrupole.reserve(size); |
283 |
} |
284 |
|
285 |
if (storageLayout_ & dslElectricField) { |
286 |
electricField.reserve(size); |
287 |
} |
288 |
|
289 |
if (storageLayout_ & dslSkippedCharge) { |
290 |
skippedCharge.reserve(size); |
291 |
} |
292 |
|
293 |
if (storageLayout_ & dslFlucQPosition) { |
294 |
flucQPos.reserve(size); |
295 |
} |
296 |
|
297 |
if (storageLayout_ & dslFlucQVelocity) { |
298 |
flucQVel.reserve(size); |
299 |
} |
300 |
|
301 |
if (storageLayout_ & dslFlucQForce) { |
302 |
flucQFrc.reserve(size); |
303 |
} |
304 |
|
305 |
if (storageLayout_ & dslSitePotential) { |
306 |
sitePotential.reserve(size); |
307 |
} |
308 |
} |
309 |
|
310 |
void DataStorage::copy(int source, int num, int target) { |
311 |
if (num + target > size_ ) { |
312 |
//error |
313 |
} |
314 |
|
315 |
if (storageLayout_ & dslPosition) { |
316 |
internalCopy(position, source, num, target); |
317 |
} |
318 |
|
319 |
if (storageLayout_ & dslVelocity) { |
320 |
internalCopy(velocity, source, num, target); |
321 |
} |
322 |
|
323 |
if (storageLayout_ & dslForce) { |
324 |
internalCopy(force, source, num, target); |
325 |
} |
326 |
|
327 |
if (storageLayout_ & dslAmat) { |
328 |
internalCopy(aMat, source, num, target); |
329 |
} |
330 |
|
331 |
if (storageLayout_ & dslAngularMomentum) { |
332 |
internalCopy(angularMomentum, source, num, target); |
333 |
} |
334 |
|
335 |
if (storageLayout_ & dslTorque) { |
336 |
internalCopy(torque, source, num, target); |
337 |
} |
338 |
|
339 |
if (storageLayout_ & dslParticlePot) { |
340 |
internalCopy(particlePot, source, num, target); |
341 |
} |
342 |
|
343 |
if (storageLayout_ & dslDensity) { |
344 |
internalCopy(density, source, num, target); |
345 |
} |
346 |
|
347 |
if (storageLayout_ & dslFunctional) { |
348 |
internalCopy(functional, source, num, target); |
349 |
} |
350 |
|
351 |
if (storageLayout_ & dslFunctionalDerivative) { |
352 |
internalCopy(functionalDerivative, source, num, target); |
353 |
} |
354 |
|
355 |
if (storageLayout_ & dslDipole) { |
356 |
internalCopy(dipole, source, num, target); |
357 |
} |
358 |
|
359 |
if (storageLayout_ & dslQuadrupole) { |
360 |
internalCopy(quadrupole, source, num, target); |
361 |
} |
362 |
|
363 |
if (storageLayout_ & dslElectricField) { |
364 |
internalCopy(electricField, source, num, target); |
365 |
} |
366 |
|
367 |
if (storageLayout_ & dslSkippedCharge) { |
368 |
internalCopy(skippedCharge, source, num, target); |
369 |
} |
370 |
|
371 |
if (storageLayout_ & dslFlucQPosition) { |
372 |
internalCopy(flucQPos, source, num, target); |
373 |
} |
374 |
|
375 |
if (storageLayout_ & dslFlucQVelocity) { |
376 |
internalCopy(flucQVel, source, num, target); |
377 |
} |
378 |
if (storageLayout_ & dslFlucQForce) { |
379 |
internalCopy(flucQFrc, source, num, target); |
380 |
} |
381 |
|
382 |
if (storageLayout_ & dslSitePotential) { |
383 |
internalCopy(sitePotential, source, num, target); |
384 |
} |
385 |
} |
386 |
|
387 |
int DataStorage::getStorageLayout() { |
388 |
return storageLayout_; |
389 |
} |
390 |
|
391 |
void DataStorage::setStorageLayout(int layout) { |
392 |
storageLayout_ = layout; |
393 |
resize(size_); |
394 |
} |
395 |
|
396 |
RealType* DataStorage::getArrayPointer(int whichArray) { |
397 |
|
398 |
switch (whichArray) { |
399 |
case dslPosition: |
400 |
return internalGetArrayPointer(position); |
401 |
|
402 |
case dslVelocity: |
403 |
return internalGetArrayPointer(velocity); |
404 |
|
405 |
case dslForce: |
406 |
return internalGetArrayPointer(force); |
407 |
|
408 |
case dslAmat: |
409 |
return internalGetArrayPointer(aMat); |
410 |
|
411 |
case dslAngularMomentum: |
412 |
return internalGetArrayPointer(angularMomentum); |
413 |
|
414 |
case dslTorque: |
415 |
return internalGetArrayPointer(torque); |
416 |
|
417 |
case dslParticlePot: |
418 |
return internalGetArrayPointer(particlePot); |
419 |
|
420 |
case dslDensity: |
421 |
return internalGetArrayPointer(density); |
422 |
|
423 |
case dslFunctional: |
424 |
return internalGetArrayPointer(functional); |
425 |
|
426 |
case dslFunctionalDerivative: |
427 |
return internalGetArrayPointer(functionalDerivative); |
428 |
|
429 |
case dslDipole: |
430 |
return internalGetArrayPointer(dipole); |
431 |
|
432 |
case dslQuadrupole: |
433 |
return internalGetArrayPointer(quadrupole); |
434 |
|
435 |
case dslElectricField: |
436 |
return internalGetArrayPointer(electricField); |
437 |
|
438 |
case dslSkippedCharge: |
439 |
return internalGetArrayPointer(skippedCharge); |
440 |
|
441 |
case dslFlucQPosition: |
442 |
return internalGetArrayPointer(flucQPos); |
443 |
|
444 |
case dslFlucQVelocity: |
445 |
return internalGetArrayPointer(flucQVel); |
446 |
|
447 |
case dslFlucQForce: |
448 |
return internalGetArrayPointer(flucQFrc); |
449 |
|
450 |
case dslSitePotential: |
451 |
return internalGetArrayPointer(sitePotential); |
452 |
|
453 |
default: |
454 |
//error message |
455 |
return NULL; |
456 |
} |
457 |
} |
458 |
|
459 |
RealType* DataStorage::internalGetArrayPointer(std::vector<Vector3d>& v) { |
460 |
if (v.empty()) { |
461 |
return NULL; |
462 |
} else { |
463 |
return v[0].getArrayPointer(); |
464 |
} |
465 |
} |
466 |
|
467 |
RealType* DataStorage::internalGetArrayPointer(std::vector<Mat3x3d>& v) { |
468 |
if (v.empty()) { |
469 |
return NULL; |
470 |
} else { |
471 |
return v[0].getArrayPointer(); |
472 |
} |
473 |
|
474 |
} |
475 |
|
476 |
RealType* DataStorage::internalGetArrayPointer(std::vector<RealType>& v) { |
477 |
if (v.empty()) { |
478 |
return NULL; |
479 |
} else { |
480 |
return &(v[0]); |
481 |
} |
482 |
|
483 |
} |
484 |
|
485 |
template<typename T> |
486 |
void DataStorage::internalResize(std::vector<T>& v, int newSize){ |
487 |
int oldSize = v.size(); |
488 |
|
489 |
if (oldSize == newSize) { |
490 |
return; |
491 |
} else if (oldSize < newSize) { |
492 |
v.insert(v.end(), newSize-oldSize, T()); |
493 |
} else { |
494 |
typename std::vector<T>::iterator i; |
495 |
i = v.begin(); |
496 |
std::advance(i, newSize); |
497 |
v.erase(i, v.end()); |
498 |
} |
499 |
} |
500 |
|
501 |
template<typename T> |
502 |
void DataStorage::internalCopy(std::vector<T>& v, int source, int num, int target) { |
503 |
typename std::vector<T>::iterator first; |
504 |
typename std::vector<T>::iterator last; |
505 |
typename std::vector<T>::iterator result; |
506 |
|
507 |
first = v.begin(); |
508 |
last = v.begin(); |
509 |
result = v.begin(); |
510 |
|
511 |
std::advance(first, source); |
512 |
//STL algorithm use half opened range |
513 |
std::advance(last, num + 1); |
514 |
std::advance(result, target ); |
515 |
|
516 |
std::copy(first, last, result); |
517 |
} |
518 |
|
519 |
int DataStorage::getBytesPerStuntDouble(int layout) { |
520 |
int bytes = 0; |
521 |
if (layout & dslPosition) { |
522 |
bytes += sizeof(Vector3d); |
523 |
} |
524 |
if (layout & dslVelocity) { |
525 |
bytes += sizeof(Vector3d); |
526 |
} |
527 |
if (layout & dslForce) { |
528 |
bytes += sizeof(Vector3d); |
529 |
} |
530 |
if (layout & dslAmat) { |
531 |
bytes += sizeof(RotMat3x3d); |
532 |
} |
533 |
if (layout & dslAngularMomentum) { |
534 |
bytes += sizeof(Vector3d); |
535 |
} |
536 |
if (layout & dslTorque) { |
537 |
bytes += sizeof(Vector3d); |
538 |
} |
539 |
if (layout & dslParticlePot) { |
540 |
bytes += sizeof(RealType); |
541 |
} |
542 |
if (layout & dslDensity) { |
543 |
bytes += sizeof(RealType); |
544 |
} |
545 |
if (layout & dslFunctional) { |
546 |
bytes += sizeof(RealType); |
547 |
} |
548 |
if (layout & dslFunctionalDerivative) { |
549 |
bytes += sizeof(RealType); |
550 |
} |
551 |
if (layout & dslDipole) { |
552 |
bytes += sizeof(Vector3d); |
553 |
} |
554 |
if (layout & dslQuadrupole) { |
555 |
bytes += sizeof(Mat3x3d); |
556 |
} |
557 |
if (layout & dslElectricField) { |
558 |
bytes += sizeof(Vector3d); |
559 |
} |
560 |
if (layout & dslSkippedCharge) { |
561 |
bytes += sizeof(RealType); |
562 |
} |
563 |
if (layout & dslFlucQPosition) { |
564 |
bytes += sizeof(RealType); |
565 |
} |
566 |
if (layout & dslFlucQVelocity) { |
567 |
bytes += sizeof(RealType); |
568 |
} |
569 |
if (layout & dslFlucQForce) { |
570 |
bytes += sizeof(RealType); |
571 |
} |
572 |
if (layout & dslSitePotential) { |
573 |
bytes += sizeof(RealType); |
574 |
} |
575 |
|
576 |
return bytes; |
577 |
} |
578 |
|
579 |
} |