| 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. Acknowledgement of the program authors must be made in any | 
| 10 | *    publication of scientific results based in part on use of the | 
| 11 | *    program.  An acceptable form of acknowledgement is citation of | 
| 12 | *    the article in which the program was described (Matthew | 
| 13 | *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher | 
| 14 | *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented | 
| 15 | *    Parallel Simulation Engine for Molecular Dynamics," | 
| 16 | *    J. Comput. Chem. 26, pp. 252-271 (2005)) | 
| 17 | * | 
| 18 | * 2. Redistributions of source code must retain the above copyright | 
| 19 | *    notice, this list of conditions and the following disclaimer. | 
| 20 | * | 
| 21 | * 3. Redistributions in binary form must reproduce the above copyright | 
| 22 | *    notice, this list of conditions and the following disclaimer in the | 
| 23 | *    documentation and/or other materials provided with the | 
| 24 | *    distribution. | 
| 25 | * | 
| 26 | * This software is provided "AS IS," without a warranty of any | 
| 27 | * kind. All express or implied conditions, representations and | 
| 28 | * warranties, including any implied warranty of merchantability, | 
| 29 | * fitness for a particular purpose or non-infringement, are hereby | 
| 30 | * excluded.  The University of Notre Dame and its licensors shall not | 
| 31 | * be liable for any damages suffered by licensee as a result of | 
| 32 | * using, modifying or distributing the software or its | 
| 33 | * derivatives. In no event will the University of Notre Dame or its | 
| 34 | * licensors be liable for any lost revenue, profit or data, or for | 
| 35 | * direct, indirect, special, consequential, incidental or punitive | 
| 36 | * damages, however caused and regardless of the theory of liability, | 
| 37 | * arising out of the use of or inability to use software, even if the | 
| 38 | * University of Notre Dame has been advised of the possibility of | 
| 39 | * such damages. | 
| 40 | */ | 
| 41 |  | 
| 42 | /** | 
| 43 | * @file DataStorage.cpp | 
| 44 | * @author tlin | 
| 45 | * @date 10/26/2004 | 
| 46 | * @time 11:56am | 
| 47 | * @version 1.0 | 
| 48 | */ | 
| 49 |  | 
| 50 | #include "brains/DataStorage.hpp" | 
| 51 |  | 
| 52 |  | 
| 53 | DataStorage::DataStorage() : size_(0), storageLayout_(0){ | 
| 54 |  | 
| 55 | } | 
| 56 |  | 
| 57 | DataStorage::DataStorage(int size, int storageLayout) : size_(size){ | 
| 58 | setStorageLayout(storageLayout); | 
| 59 | resize(size); | 
| 60 | } | 
| 61 |  | 
| 62 | int DataStorage::getSize() { | 
| 63 |  | 
| 64 | if (storageLayout_ & dslPosition && position.size() != size_) { | 
| 65 | //error | 
| 66 | std::cerr << "size does not match"<< std::endl; | 
| 67 | } | 
| 68 |  | 
| 69 | if (storageLayout_ & dslVelocity && velocity.size() != size_) { | 
| 70 | //error | 
| 71 | std::cerr << "size does not match"<< std::endl; | 
| 72 | } | 
| 73 |  | 
| 74 | if (storageLayout_ & dslAmat && aMat.size() != size_) { | 
| 75 | //error | 
| 76 | std::cerr << "size does not match"<< std::endl; | 
| 77 | } | 
| 78 |  | 
| 79 | if (storageLayout_ & dslAngularMomentum && angularMomentum.size() != size_) { | 
| 80 | //error | 
| 81 | std::cerr << "size does not match"<< std::endl; | 
| 82 | } | 
| 83 |  | 
| 84 | if (storageLayout_ & dslElectroFrame && electroFrame.size() != size_) { | 
| 85 | //error | 
| 86 | std::cerr << "size does not match"<< std::endl; | 
| 87 | } | 
| 88 |  | 
| 89 | if (storageLayout_ & dslZAngle && zAngle.size() != size_) { | 
| 90 | //error | 
| 91 | std::cerr << "size does not match"<< std::endl; | 
| 92 | } | 
| 93 |  | 
| 94 | if (storageLayout_ & dslForce && force.size() != size_) { | 
| 95 | //error | 
| 96 | std::cerr << "size does not match"<< std::endl; | 
| 97 | } | 
| 98 |  | 
| 99 | if (storageLayout_ & dslTorque && torque.size() != size_) { | 
| 100 | //error | 
| 101 | std::cerr << "size does not match"<< std::endl; | 
| 102 | } | 
| 103 |  | 
| 104 | return size_; | 
| 105 |  | 
| 106 | } | 
| 107 |  | 
| 108 | void DataStorage::resize(int newSize) { | 
| 109 |  | 
| 110 | if (storageLayout_ & dslPosition) { | 
| 111 | internalResize(position, newSize); | 
| 112 | } | 
| 113 |  | 
| 114 | if (storageLayout_ & dslVelocity) { | 
| 115 | internalResize(velocity, newSize); | 
| 116 | } | 
| 117 |  | 
| 118 | if (storageLayout_ & dslAmat) { | 
| 119 | internalResize(aMat, newSize); | 
| 120 | } | 
| 121 |  | 
| 122 | if (storageLayout_ & dslAngularMomentum) { | 
| 123 | internalResize(angularMomentum, newSize); | 
| 124 | } | 
| 125 |  | 
| 126 | if (storageLayout_ & dslElectroFrame) { | 
| 127 | internalResize(electroFrame, newSize); | 
| 128 | } | 
| 129 |  | 
| 130 | if (storageLayout_ & dslZAngle) { | 
| 131 | internalResize(zAngle, newSize); | 
| 132 | } | 
| 133 |  | 
| 134 | if (storageLayout_ & dslForce) { | 
| 135 | internalResize(force, newSize); | 
| 136 | } | 
| 137 |  | 
| 138 | if (storageLayout_ & dslTorque) { | 
| 139 | internalResize(torque, newSize); | 
| 140 | } | 
| 141 |  | 
| 142 | size_ = newSize; | 
| 143 | } | 
| 144 |  | 
| 145 | void DataStorage::reserve(int size) { | 
| 146 | if (storageLayout_ & dslPosition) { | 
| 147 | position.reserve(size); | 
| 148 | } | 
| 149 |  | 
| 150 | if (storageLayout_ & dslVelocity) { | 
| 151 | velocity.reserve(size); | 
| 152 | } | 
| 153 |  | 
| 154 | if (storageLayout_ & dslAmat) { | 
| 155 | aMat.reserve(size); | 
| 156 | } | 
| 157 |  | 
| 158 | if (storageLayout_ & dslAngularMomentum) { | 
| 159 | angularMomentum.reserve(size); | 
| 160 | } | 
| 161 |  | 
| 162 | if (storageLayout_ & dslElectroFrame) { | 
| 163 | electroFrame.reserve(size); | 
| 164 | } | 
| 165 |  | 
| 166 | if (storageLayout_ & dslZAngle) { | 
| 167 | zAngle.reserve(size); | 
| 168 | } | 
| 169 |  | 
| 170 | if (storageLayout_ & dslForce) { | 
| 171 | force.reserve(size); | 
| 172 | } | 
| 173 |  | 
| 174 | if (storageLayout_ & dslTorque) { | 
| 175 | torque.reserve(size); | 
| 176 | } | 
| 177 |  | 
| 178 | } | 
| 179 |  | 
| 180 | void DataStorage::copy(int source, int num, int target) { | 
| 181 | if (num + target > size_ ) { | 
| 182 | //error | 
| 183 | } | 
| 184 |  | 
| 185 | if (storageLayout_ & dslPosition) { | 
| 186 | internalCopy(position, source, num, target); | 
| 187 | } | 
| 188 |  | 
| 189 | if (storageLayout_ & dslVelocity) { | 
| 190 | internalCopy(velocity, source, num, target); | 
| 191 | } | 
| 192 |  | 
| 193 | if (storageLayout_ & dslAmat) { | 
| 194 | internalCopy(aMat, source, num, target); | 
| 195 | } | 
| 196 |  | 
| 197 | if (storageLayout_ & dslAngularMomentum) { | 
| 198 | internalCopy(angularMomentum, source, num, target); | 
| 199 | } | 
| 200 |  | 
| 201 | if (storageLayout_ & dslElectroFrame) { | 
| 202 | internalCopy(electroFrame, source, num, target); | 
| 203 | } | 
| 204 |  | 
| 205 | if (storageLayout_ & dslZAngle) { | 
| 206 | internalCopy(zAngle, source, num, target); | 
| 207 | } | 
| 208 |  | 
| 209 | if (storageLayout_ & dslForce) { | 
| 210 | internalCopy(force, source, num, target); | 
| 211 | } | 
| 212 |  | 
| 213 | if (storageLayout_ & dslTorque) { | 
| 214 | internalCopy(torque, source, num, target); | 
| 215 | } | 
| 216 |  | 
| 217 |  | 
| 218 | } | 
| 219 |  | 
| 220 | int DataStorage::getStorageLayout() { | 
| 221 | return storageLayout_; | 
| 222 | } | 
| 223 |  | 
| 224 | void DataStorage::setStorageLayout(int layout) { | 
| 225 | storageLayout_ = layout; | 
| 226 | resize(size_); | 
| 227 | } | 
| 228 |  | 
| 229 | double* DataStorage::getArrayPointer(int whichArray) { | 
| 230 |  | 
| 231 | switch (whichArray) { | 
| 232 | case dslPosition: | 
| 233 | return internalGetArrayPointer(position); | 
| 234 | break; | 
| 235 |  | 
| 236 | case dslVelocity: | 
| 237 | return internalGetArrayPointer(velocity); | 
| 238 | break; | 
| 239 |  | 
| 240 | case dslAmat: | 
| 241 | return internalGetArrayPointer(aMat); | 
| 242 | break; | 
| 243 |  | 
| 244 | case dslAngularMomentum: | 
| 245 | return internalGetArrayPointer(angularMomentum); | 
| 246 | break; | 
| 247 |  | 
| 248 | case dslElectroFrame: | 
| 249 | return internalGetArrayPointer(electroFrame); | 
| 250 | break; | 
| 251 |  | 
| 252 | case dslZAngle: | 
| 253 | return internalGetArrayPointer(zAngle); | 
| 254 | break; | 
| 255 |  | 
| 256 | case dslForce: | 
| 257 | return internalGetArrayPointer(force); | 
| 258 | break; | 
| 259 |  | 
| 260 | case dslTorque: | 
| 261 | return internalGetArrayPointer(torque); | 
| 262 | break; | 
| 263 |  | 
| 264 | default: | 
| 265 | //error message | 
| 266 | return NULL; | 
| 267 |  | 
| 268 | } | 
| 269 | } | 
| 270 |  | 
| 271 | double* DataStorage::internalGetArrayPointer(std::vector<Vector3d>& v) { | 
| 272 | if (v.size() == 0) { | 
| 273 | return NULL; | 
| 274 | } else { | 
| 275 | return v[0].getArrayPointer(); | 
| 276 | } | 
| 277 | } | 
| 278 |  | 
| 279 | double* DataStorage::internalGetArrayPointer(std::vector<RotMat3x3d>& v) { | 
| 280 | if (v.size() == 0) { | 
| 281 | return NULL; | 
| 282 | } else { | 
| 283 | return v[0].getArrayPointer(); | 
| 284 | } | 
| 285 |  | 
| 286 | } | 
| 287 |  | 
| 288 | double* DataStorage::internalGetArrayPointer(std::vector<double>& v) { | 
| 289 | if (v.size() == 0) { | 
| 290 | return NULL; | 
| 291 | } else { | 
| 292 | return &(v[0]); | 
| 293 | } | 
| 294 |  | 
| 295 | } | 
| 296 |  | 
| 297 | template<typename T> | 
| 298 | void DataStorage::internalResize(std::vector<T>& v, int newSize){ | 
| 299 | int oldSize = v.size(); | 
| 300 |  | 
| 301 | if (oldSize == newSize) { | 
| 302 | return; | 
| 303 | } else if (oldSize < newSize) { | 
| 304 | v.insert(v.end(), newSize-oldSize, T()); | 
| 305 | } else { | 
| 306 | typename std::vector<T>::iterator i; | 
| 307 | i = v.begin(); | 
| 308 | std::advance(i, newSize); | 
| 309 | v.erase(i, v.end()); | 
| 310 | } | 
| 311 | } | 
| 312 |  | 
| 313 | template<typename T> | 
| 314 | void DataStorage::internalCopy(std::vector<T>& v, int source,  int num, int target) { | 
| 315 | typename std::vector<T>::iterator first; | 
| 316 | typename std::vector<T>::iterator last; | 
| 317 | typename std::vector<T>::iterator result; | 
| 318 |  | 
| 319 | first = v.begin(); | 
| 320 | last = v.begin(); | 
| 321 | result = v.begin(); | 
| 322 |  | 
| 323 | std::advance(first, source); | 
| 324 | //STL algorithm use half opened range | 
| 325 | std::advance(last, num + 1); | 
| 326 | std::advance(result, target ); | 
| 327 |  | 
| 328 | std::copy(first, last, result); | 
| 329 | } |