ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/brains/DataStorage.cpp
Revision: 1808
Committed: Mon Oct 22 20:42:10 2012 UTC (12 years, 6 months ago) by gezelter
File size: 14658 byte(s)
Log Message:
A bug fix in the electric field for the new electrostatic code.  Also comment fixes for Doxygen 

File Contents

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

Properties

Name Value
svn:executable *
svn:keywords Author Id Revision Date