ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/brains/DataStorage.cpp
Revision: 1710
Committed: Fri May 18 21:44:02 2012 UTC (12 years, 11 months ago) by gezelter
File size: 14720 byte(s)
Log Message:
Added an adapter layer between the AtomType and the rest of the code to 
handle the bolt-on capabilities of new types. 

Fixed a long-standing bug in how storageLayout was being set to the maximum
possible value.

Started to add infrastructure for Polarizable and fluc-Q calculations.

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

Properties

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