OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
DataStorage.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the appropriate papers when you publish your
33 * work. Good starting points are:
34 *
35 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
36 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
37 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
38 * [4] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
40 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
41 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
42 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
43 */
44
45/**
46 * @file DataStorage.cpp
47 * @author tlin
48 * @date 10/26/2004
49 * @version 1.0
50 */
51
52#include "brains/DataStorage.hpp"
53using namespace std;
54namespace OpenMD {
55
56 DataStorage::DataStorage() : size_(0), storageLayout_(0) {}
57
58 DataStorage::DataStorage(std::size_t size, int storageLayout) : size_(size) {
59 setStorageLayout(storageLayout);
60 resize(size);
61 }
62
63 std::size_t DataStorage::getSize() {
64 if (storageLayout_ & dslPosition && position.size() != size_) {
65 // error
66 cerr << "size does not match" << endl;
67 }
68
69 if (storageLayout_ & dslVelocity && velocity.size() != size_) {
70 // error
71 cerr << "size does not match" << endl;
72 }
73
74 if (storageLayout_ & dslForce && force.size() != size_) {
75 // error
76 cerr << "size does not match" << endl;
77 }
78
79 if (storageLayout_ & dslAmat && aMat.size() != size_) {
80 // error
81 cerr << "size does not match" << endl;
82 }
83
84 if (storageLayout_ & dslAngularMomentum &&
85 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 &&
111 functionalDerivative.size() != size_) {
112 // error
113 cerr << "size does not match" << endl;
114 }
115
116 if (storageLayout_ & dslDipole && dipole.size() != size_) {
117 // error
118 cerr << "size does not match" << endl;
119 }
120
121 if (storageLayout_ & dslQuadrupole && quadrupole.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 if (storageLayout_ & dslSitePotential && sitePotential.size() != size_) {
152 // error
153 cerr << "size does not match" << endl;
154 }
155
156 return size_;
157 }
158
159 void DataStorage::resize(std::size_t newSize) {
160 if (storageLayout_ & dslPosition) { internalResize(position, newSize); }
161
162 if (storageLayout_ & dslVelocity) { internalResize(velocity, newSize); }
163
164 if (storageLayout_ & dslForce) { internalResize(force, newSize); }
165
166 if (storageLayout_ & dslAmat) { internalResize(aMat, newSize); }
167
168 if (storageLayout_ & dslAngularMomentum) {
169 internalResize(angularMomentum, newSize);
170 }
171
172 if (storageLayout_ & dslTorque) { internalResize(torque, newSize); }
173
174 if (storageLayout_ & dslParticlePot) {
175 internalResize(particlePot, newSize);
176 }
177
178 if (storageLayout_ & dslDensity) { internalResize(density, newSize); }
179
180 if (storageLayout_ & dslFunctional) { internalResize(functional, newSize); }
181
182 if (storageLayout_ & dslFunctionalDerivative) {
183 internalResize(functionalDerivative, newSize);
184 }
185
186 if (storageLayout_ & dslDipole) { internalResize(dipole, newSize); }
187
188 if (storageLayout_ & dslQuadrupole) { internalResize(quadrupole, newSize); }
189
190 if (storageLayout_ & dslElectricField) {
191 internalResize(electricField, newSize);
192 }
193
194 if (storageLayout_ & dslSkippedCharge) {
195 internalResize(skippedCharge, newSize);
196 }
197
198 if (storageLayout_ & dslFlucQPosition) {
199 internalResize(flucQPos, newSize);
200 }
201
202 if (storageLayout_ & dslFlucQVelocity) {
203 internalResize(flucQVel, newSize);
204 }
205
206 if (storageLayout_ & dslFlucQForce) { internalResize(flucQFrc, newSize); }
207
208 if (storageLayout_ & dslSitePotential) {
209 internalResize(sitePotential, newSize);
210 }
211
212 size_ = newSize;
213 }
214
215 void DataStorage::reserve(std::size_t size) {
216 if (storageLayout_ & dslPosition) { position.reserve(size); }
217
218 if (storageLayout_ & dslVelocity) { velocity.reserve(size); }
219
220 if (storageLayout_ & dslForce) { force.reserve(size); }
221
222 if (storageLayout_ & dslAmat) { aMat.reserve(size); }
223
224 if (storageLayout_ & dslAngularMomentum) { angularMomentum.reserve(size); }
225
226 if (storageLayout_ & dslTorque) { torque.reserve(size); }
227
228 if (storageLayout_ & dslParticlePot) { particlePot.reserve(size); }
229
230 if (storageLayout_ & dslDensity) { density.reserve(size); }
231
232 if (storageLayout_ & dslFunctional) { functional.reserve(size); }
233
234 if (storageLayout_ & dslFunctionalDerivative) {
235 functionalDerivative.reserve(size);
236 }
237
238 if (storageLayout_ & dslDipole) { dipole.reserve(size); }
239
240 if (storageLayout_ & dslQuadrupole) { quadrupole.reserve(size); }
241
242 if (storageLayout_ & dslElectricField) { electricField.reserve(size); }
243
244 if (storageLayout_ & dslSkippedCharge) { skippedCharge.reserve(size); }
245
246 if (storageLayout_ & dslFlucQPosition) { flucQPos.reserve(size); }
247
248 if (storageLayout_ & dslFlucQVelocity) { flucQVel.reserve(size); }
249
250 if (storageLayout_ & dslFlucQForce) { flucQFrc.reserve(size); }
251
252 if (storageLayout_ & dslSitePotential) { sitePotential.reserve(size); }
253 }
254
255 void DataStorage::copy(int source, std::size_t num, std::size_t target) {
256 if (num + target > size_) {
257 // error
258 }
259
260 if (storageLayout_ & dslPosition) {
261 internalCopy(position, source, num, target);
262 }
263
264 if (storageLayout_ & dslVelocity) {
265 internalCopy(velocity, source, num, target);
266 }
267
268 if (storageLayout_ & dslForce) { internalCopy(force, source, num, target); }
269
270 if (storageLayout_ & dslAmat) { internalCopy(aMat, source, num, target); }
271
272 if (storageLayout_ & dslAngularMomentum) {
273 internalCopy(angularMomentum, source, num, target);
274 }
275
276 if (storageLayout_ & dslTorque) {
277 internalCopy(torque, source, num, target);
278 }
279
280 if (storageLayout_ & dslParticlePot) {
281 internalCopy(particlePot, source, num, target);
282 }
283
284 if (storageLayout_ & dslDensity) {
285 internalCopy(density, source, num, target);
286 }
287
288 if (storageLayout_ & dslFunctional) {
289 internalCopy(functional, source, num, target);
290 }
291
292 if (storageLayout_ & dslFunctionalDerivative) {
293 internalCopy(functionalDerivative, source, num, target);
294 }
295
296 if (storageLayout_ & dslDipole) {
297 internalCopy(dipole, source, num, target);
298 }
299
300 if (storageLayout_ & dslQuadrupole) {
301 internalCopy(quadrupole, source, num, target);
302 }
303
304 if (storageLayout_ & dslElectricField) {
305 internalCopy(electricField, source, num, target);
306 }
307
308 if (storageLayout_ & dslSkippedCharge) {
309 internalCopy(skippedCharge, source, num, target);
310 }
311
312 if (storageLayout_ & dslFlucQPosition) {
313 internalCopy(flucQPos, source, num, target);
314 }
315
316 if (storageLayout_ & dslFlucQVelocity) {
317 internalCopy(flucQVel, source, num, target);
318 }
319 if (storageLayout_ & dslFlucQForce) {
320 internalCopy(flucQFrc, source, num, target);
321 }
322
323 if (storageLayout_ & dslSitePotential) {
324 internalCopy(sitePotential, source, num, target);
325 }
326 }
327
328 int DataStorage::getStorageLayout() { return storageLayout_; }
329
330 void DataStorage::setStorageLayout(int layout) {
331 storageLayout_ = layout;
332 resize(size_);
333 }
334
335 RealType* DataStorage::getArrayPointer(int whichArray) {
336 switch (whichArray) {
337 case dslPosition:
338 return internalGetArrayPointer(position);
339
340 case dslVelocity:
341 return internalGetArrayPointer(velocity);
342
343 case dslForce:
344 return internalGetArrayPointer(force);
345
346 case dslAmat:
347 return internalGetArrayPointer(aMat);
348
349 case dslAngularMomentum:
350 return internalGetArrayPointer(angularMomentum);
351
352 case dslTorque:
353 return internalGetArrayPointer(torque);
354
355 case dslParticlePot:
356 return internalGetArrayPointer(particlePot);
357
358 case dslDensity:
359 return internalGetArrayPointer(density);
360
361 case dslFunctional:
362 return internalGetArrayPointer(functional);
363
364 case dslFunctionalDerivative:
365 return internalGetArrayPointer(functionalDerivative);
366
367 case dslDipole:
368 return internalGetArrayPointer(dipole);
369
370 case dslQuadrupole:
371 return internalGetArrayPointer(quadrupole);
372
373 case dslElectricField:
374 return internalGetArrayPointer(electricField);
375
376 case dslSkippedCharge:
377 return internalGetArrayPointer(skippedCharge);
378
379 case dslFlucQPosition:
380 return internalGetArrayPointer(flucQPos);
381
382 case dslFlucQVelocity:
383 return internalGetArrayPointer(flucQVel);
384
385 case dslFlucQForce:
386 return internalGetArrayPointer(flucQFrc);
387
388 case dslSitePotential:
389 return internalGetArrayPointer(sitePotential);
390
391 default:
392 // error message
393 return NULL;
394 }
395 }
396
397 RealType* DataStorage::internalGetArrayPointer(std::vector<Vector3d>& v) {
398 if (v.empty()) {
399 return NULL;
400 } else {
401 return v[0].getArrayPointer();
402 }
403 }
404
405 RealType* DataStorage::internalGetArrayPointer(std::vector<Mat3x3d>& v) {
406 if (v.empty()) {
407 return NULL;
408 } else {
409 return v[0].getArrayPointer();
410 }
411 }
412
413 RealType* DataStorage::internalGetArrayPointer(std::vector<RealType>& v) {
414 if (v.empty()) {
415 return NULL;
416 } else {
417 return &(v[0]);
418 }
419 }
420
421 template<typename T>
422 void DataStorage::internalResize(std::vector<T>& v, std::size_t newSize) {
423 std::size_t oldSize = v.size();
424
425 if (oldSize == newSize) {
426 return;
427 } else if (oldSize < newSize) {
428 v.insert(v.end(), newSize - oldSize, T());
429 } else {
430 typename std::vector<T>::iterator i;
431 i = v.begin();
432 std::advance(i, newSize);
433 v.erase(i, v.end());
434 }
435 }
436
437 template<typename T>
438 void DataStorage::internalCopy(std::vector<T>& v, int source, std::size_t num,
439 std::size_t target) {
440 typename std::vector<T>::iterator first;
441 typename std::vector<T>::iterator last;
442 typename std::vector<T>::iterator result;
443
444 first = v.begin();
445 last = v.begin();
446 result = v.begin();
447
448 std::advance(first, source);
449 // STL algorithm use half opened range
450 std::advance(last, num + 1);
451 std::advance(result, target);
452
453 std::copy(first, last, result);
454 }
455
456 std::size_t DataStorage::getBytesPerStuntDouble(int layout) {
457 std::size_t bytes = 0;
458 if (layout & dslPosition) { bytes += sizeof(Vector3d); }
459 if (layout & dslVelocity) { bytes += sizeof(Vector3d); }
460 if (layout & dslForce) { bytes += sizeof(Vector3d); }
461 if (layout & dslAmat) { bytes += sizeof(RotMat3x3d); }
462 if (layout & dslAngularMomentum) { bytes += sizeof(Vector3d); }
463 if (layout & dslTorque) { bytes += sizeof(Vector3d); }
464 if (layout & dslParticlePot) { bytes += sizeof(RealType); }
465 if (layout & dslDensity) { bytes += sizeof(RealType); }
466 if (layout & dslFunctional) { bytes += sizeof(RealType); }
467 if (layout & dslFunctionalDerivative) { bytes += sizeof(RealType); }
468 if (layout & dslDipole) { bytes += sizeof(Vector3d); }
469 if (layout & dslQuadrupole) { bytes += sizeof(Mat3x3d); }
470 if (layout & dslElectricField) { bytes += sizeof(Vector3d); }
471 if (layout & dslSkippedCharge) { bytes += sizeof(RealType); }
472 if (layout & dslFlucQPosition) { bytes += sizeof(RealType); }
473 if (layout & dslFlucQVelocity) { bytes += sizeof(RealType); }
474 if (layout & dslFlucQForce) { bytes += sizeof(RealType); }
475 if (layout & dslSitePotential) { bytes += sizeof(RealType); }
476
477 return bytes;
478 }
479
480} // namespace OpenMD
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.