| 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 | 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 |  | 
| 385 | case dslVelocity: | 
| 386 | return internalGetArrayPointer(velocity); | 
| 387 |  | 
| 388 | case dslForce: | 
| 389 | return internalGetArrayPointer(force); | 
| 390 |  | 
| 391 | case dslAmat: | 
| 392 | return internalGetArrayPointer(aMat); | 
| 393 |  | 
| 394 | case dslAngularMomentum: | 
| 395 | return internalGetArrayPointer(angularMomentum); | 
| 396 |  | 
| 397 | case dslTorque: | 
| 398 | return internalGetArrayPointer(torque); | 
| 399 |  | 
| 400 | case dslParticlePot: | 
| 401 | return internalGetArrayPointer(particlePot); | 
| 402 |  | 
| 403 | case dslDensity: | 
| 404 | return internalGetArrayPointer(density); | 
| 405 |  | 
| 406 | case dslFunctional: | 
| 407 | return internalGetArrayPointer(functional); | 
| 408 |  | 
| 409 | case dslFunctionalDerivative: | 
| 410 | return internalGetArrayPointer(functionalDerivative); | 
| 411 |  | 
| 412 | case dslDipole: | 
| 413 | return internalGetArrayPointer(dipole); | 
| 414 |  | 
| 415 | case dslQuadrupole: | 
| 416 | return internalGetArrayPointer(quadrupole); | 
| 417 |  | 
| 418 | case dslElectricField: | 
| 419 | return internalGetArrayPointer(electricField); | 
| 420 |  | 
| 421 | case dslSkippedCharge: | 
| 422 | return internalGetArrayPointer(skippedCharge); | 
| 423 |  | 
| 424 | case dslFlucQPosition: | 
| 425 | return internalGetArrayPointer(flucQPos); | 
| 426 |  | 
| 427 | case dslFlucQVelocity: | 
| 428 | return internalGetArrayPointer(flucQVel); | 
| 429 |  | 
| 430 | case dslFlucQForce: | 
| 431 | return internalGetArrayPointer(flucQFrc); | 
| 432 |  | 
| 433 | default: | 
| 434 | //error message | 
| 435 | return NULL; | 
| 436 | } | 
| 437 | } | 
| 438 |  | 
| 439 | RealType* DataStorage::internalGetArrayPointer(std::vector<Vector3d>& v) { | 
| 440 | if (v.empty()) { | 
| 441 | return NULL; | 
| 442 | } else { | 
| 443 | return v[0].getArrayPointer(); | 
| 444 | } | 
| 445 | } | 
| 446 |  | 
| 447 | RealType* DataStorage::internalGetArrayPointer(std::vector<Mat3x3d>& v) { | 
| 448 | if (v.empty()) { | 
| 449 | return NULL; | 
| 450 | } else { | 
| 451 | return v[0].getArrayPointer(); | 
| 452 | } | 
| 453 |  | 
| 454 | } | 
| 455 |  | 
| 456 | RealType* DataStorage::internalGetArrayPointer(std::vector<RealType>& v) { | 
| 457 | if (v.empty()) { | 
| 458 | return NULL; | 
| 459 | } else { | 
| 460 | return &(v[0]); | 
| 461 | } | 
| 462 |  | 
| 463 | } | 
| 464 |  | 
| 465 | template<typename T> | 
| 466 | void DataStorage::internalResize(std::vector<T>& v, int newSize){ | 
| 467 | int oldSize = v.size(); | 
| 468 |  | 
| 469 | if (oldSize == newSize) { | 
| 470 | return; | 
| 471 | } else if (oldSize < newSize) { | 
| 472 | v.insert(v.end(), newSize-oldSize, T()); | 
| 473 | } else { | 
| 474 | typename std::vector<T>::iterator i; | 
| 475 | i = v.begin(); | 
| 476 | std::advance(i, newSize); | 
| 477 | v.erase(i, v.end()); | 
| 478 | } | 
| 479 | } | 
| 480 |  | 
| 481 | template<typename T> | 
| 482 | void DataStorage::internalCopy(std::vector<T>& v, int source,  int num, int target) { | 
| 483 | typename std::vector<T>::iterator first; | 
| 484 | typename std::vector<T>::iterator last; | 
| 485 | typename std::vector<T>::iterator result; | 
| 486 |  | 
| 487 | first = v.begin(); | 
| 488 | last = v.begin(); | 
| 489 | result = v.begin(); | 
| 490 |  | 
| 491 | std::advance(first, source); | 
| 492 | //STL algorithm use half opened range | 
| 493 | std::advance(last, num + 1); | 
| 494 | std::advance(result, target ); | 
| 495 |  | 
| 496 | std::copy(first, last, result); | 
| 497 | } | 
| 498 |  | 
| 499 | int DataStorage::getBytesPerStuntDouble(int layout) { | 
| 500 | int bytes = 0; | 
| 501 | if (layout & dslPosition) { | 
| 502 | bytes += sizeof(Vector3d); | 
| 503 | } | 
| 504 | if (layout & dslVelocity) { | 
| 505 | bytes += sizeof(Vector3d); | 
| 506 | } | 
| 507 | if (layout & dslForce) { | 
| 508 | bytes += sizeof(Vector3d); | 
| 509 | } | 
| 510 | if (layout & dslAmat) { | 
| 511 | bytes += sizeof(RotMat3x3d); | 
| 512 | } | 
| 513 | if (layout & dslAngularMomentum) { | 
| 514 | bytes += sizeof(Vector3d); | 
| 515 | } | 
| 516 | if (layout & dslTorque) { | 
| 517 | bytes += sizeof(Vector3d); | 
| 518 | } | 
| 519 | if (layout & dslParticlePot) { | 
| 520 | bytes += sizeof(RealType); | 
| 521 | } | 
| 522 | if (layout & dslDensity) { | 
| 523 | bytes += sizeof(RealType); | 
| 524 | } | 
| 525 | if (layout & dslFunctional) { | 
| 526 | bytes += sizeof(RealType); | 
| 527 | } | 
| 528 | if (layout & dslFunctionalDerivative) { | 
| 529 | bytes += sizeof(RealType); | 
| 530 | } | 
| 531 | if (layout & dslDipole) { | 
| 532 | bytes += sizeof(Vector3d); | 
| 533 | } | 
| 534 | if (layout & dslQuadrupole) { | 
| 535 | bytes += sizeof(Mat3x3d); | 
| 536 | } | 
| 537 | if (layout & dslElectricField) { | 
| 538 | bytes += sizeof(Vector3d); | 
| 539 | } | 
| 540 | if (layout & dslSkippedCharge) { | 
| 541 | bytes += sizeof(RealType); | 
| 542 | } | 
| 543 | if (layout & dslFlucQPosition) { | 
| 544 | bytes += sizeof(RealType); | 
| 545 | } | 
| 546 | if (layout & dslFlucQVelocity) { | 
| 547 | bytes += sizeof(RealType); | 
| 548 | } | 
| 549 | if (layout & dslFlucQForce) { | 
| 550 | bytes += sizeof(RealType); | 
| 551 | } | 
| 552 |  | 
| 553 | return bytes; | 
| 554 | } | 
| 555 |  | 
| 556 | } |