1 |
gezelter |
246 |
/* |
2 |
|
|
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
tim |
155 |
* |
4 |
gezelter |
246 |
* 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 |
tim |
155 |
*/ |
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 |
tim |
323 |
namespace oopse { |
53 |
tim |
155 |
|
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 |
|
|
std::cerr << "size does not match"<< std::endl; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
if (storageLayout_ & dslVelocity && velocity.size() != size_) { |
71 |
|
|
//error |
72 |
|
|
std::cerr << "size does not match"<< std::endl; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
if (storageLayout_ & dslAmat && aMat.size() != size_) { |
76 |
|
|
//error |
77 |
|
|
std::cerr << "size does not match"<< std::endl; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
if (storageLayout_ & dslAngularMomentum && angularMomentum.size() != size_) { |
81 |
|
|
//error |
82 |
|
|
std::cerr << "size does not match"<< std::endl; |
83 |
|
|
} |
84 |
|
|
|
85 |
gezelter |
246 |
if (storageLayout_ & dslElectroFrame && electroFrame.size() != size_) { |
86 |
tim |
155 |
//error |
87 |
|
|
std::cerr << "size does not match"<< std::endl; |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
if (storageLayout_ & dslZAngle && zAngle.size() != size_) { |
91 |
|
|
//error |
92 |
|
|
std::cerr << "size does not match"<< std::endl; |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
if (storageLayout_ & dslForce && force.size() != size_) { |
96 |
|
|
//error |
97 |
|
|
std::cerr << "size does not match"<< std::endl; |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
if (storageLayout_ & dslTorque && torque.size() != size_) { |
101 |
|
|
//error |
102 |
|
|
std::cerr << "size does not match"<< std::endl; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
return size_; |
106 |
|
|
|
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
void DataStorage::resize(int newSize) { |
110 |
|
|
|
111 |
|
|
if (storageLayout_ & dslPosition) { |
112 |
|
|
internalResize(position, newSize); |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
if (storageLayout_ & dslVelocity) { |
116 |
|
|
internalResize(velocity, newSize); |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
if (storageLayout_ & dslAmat) { |
120 |
|
|
internalResize(aMat, newSize); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
if (storageLayout_ & dslAngularMomentum) { |
124 |
|
|
internalResize(angularMomentum, newSize); |
125 |
|
|
} |
126 |
|
|
|
127 |
gezelter |
246 |
if (storageLayout_ & dslElectroFrame) { |
128 |
|
|
internalResize(electroFrame, newSize); |
129 |
tim |
155 |
} |
130 |
|
|
|
131 |
|
|
if (storageLayout_ & dslZAngle) { |
132 |
|
|
internalResize(zAngle, newSize); |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
if (storageLayout_ & dslForce) { |
136 |
|
|
internalResize(force, newSize); |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
if (storageLayout_ & dslTorque) { |
140 |
|
|
internalResize(torque, newSize); |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
size_ = newSize; |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
void DataStorage::reserve(int size) { |
147 |
|
|
if (storageLayout_ & dslPosition) { |
148 |
|
|
position.reserve(size); |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
if (storageLayout_ & dslVelocity) { |
152 |
|
|
velocity.reserve(size); |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
if (storageLayout_ & dslAmat) { |
156 |
|
|
aMat.reserve(size); |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
if (storageLayout_ & dslAngularMomentum) { |
160 |
|
|
angularMomentum.reserve(size); |
161 |
|
|
} |
162 |
|
|
|
163 |
gezelter |
246 |
if (storageLayout_ & dslElectroFrame) { |
164 |
|
|
electroFrame.reserve(size); |
165 |
tim |
155 |
} |
166 |
|
|
|
167 |
|
|
if (storageLayout_ & dslZAngle) { |
168 |
|
|
zAngle.reserve(size); |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
if (storageLayout_ & dslForce) { |
172 |
|
|
force.reserve(size); |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
if (storageLayout_ & dslTorque) { |
176 |
|
|
torque.reserve(size); |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
void DataStorage::copy(int source, int num, int target) { |
182 |
|
|
if (num + target > size_ ) { |
183 |
|
|
//error |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
if (storageLayout_ & dslPosition) { |
187 |
gezelter |
246 |
internalCopy(position, source, num, target); |
188 |
tim |
155 |
} |
189 |
|
|
|
190 |
|
|
if (storageLayout_ & dslVelocity) { |
191 |
gezelter |
246 |
internalCopy(velocity, source, num, target); |
192 |
tim |
155 |
} |
193 |
|
|
|
194 |
|
|
if (storageLayout_ & dslAmat) { |
195 |
gezelter |
246 |
internalCopy(aMat, source, num, target); |
196 |
tim |
155 |
} |
197 |
|
|
|
198 |
|
|
if (storageLayout_ & dslAngularMomentum) { |
199 |
gezelter |
246 |
internalCopy(angularMomentum, source, num, target); |
200 |
tim |
155 |
} |
201 |
|
|
|
202 |
gezelter |
246 |
if (storageLayout_ & dslElectroFrame) { |
203 |
|
|
internalCopy(electroFrame, source, num, target); |
204 |
tim |
155 |
} |
205 |
|
|
|
206 |
|
|
if (storageLayout_ & dslZAngle) { |
207 |
gezelter |
246 |
internalCopy(zAngle, source, num, target); |
208 |
tim |
155 |
} |
209 |
|
|
|
210 |
|
|
if (storageLayout_ & dslForce) { |
211 |
gezelter |
246 |
internalCopy(force, source, num, target); |
212 |
tim |
155 |
} |
213 |
|
|
|
214 |
|
|
if (storageLayout_ & dslTorque) { |
215 |
gezelter |
246 |
internalCopy(torque, source, num, target); |
216 |
tim |
155 |
} |
217 |
|
|
|
218 |
|
|
|
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
int DataStorage::getStorageLayout() { |
222 |
|
|
return storageLayout_; |
223 |
|
|
} |
224 |
|
|
|
225 |
|
|
void DataStorage::setStorageLayout(int layout) { |
226 |
|
|
storageLayout_ = layout; |
227 |
|
|
resize(size_); |
228 |
|
|
} |
229 |
|
|
|
230 |
|
|
double* DataStorage::getArrayPointer(int whichArray) { |
231 |
|
|
|
232 |
|
|
switch (whichArray) { |
233 |
|
|
case dslPosition: |
234 |
gezelter |
246 |
return internalGetArrayPointer(position); |
235 |
tim |
155 |
break; |
236 |
|
|
|
237 |
|
|
case dslVelocity: |
238 |
|
|
return internalGetArrayPointer(velocity); |
239 |
|
|
break; |
240 |
|
|
|
241 |
|
|
case dslAmat: |
242 |
|
|
return internalGetArrayPointer(aMat); |
243 |
|
|
break; |
244 |
|
|
|
245 |
|
|
case dslAngularMomentum: |
246 |
|
|
return internalGetArrayPointer(angularMomentum); |
247 |
|
|
break; |
248 |
|
|
|
249 |
gezelter |
246 |
case dslElectroFrame: |
250 |
|
|
return internalGetArrayPointer(electroFrame); |
251 |
tim |
155 |
break; |
252 |
|
|
|
253 |
|
|
case dslZAngle: |
254 |
|
|
return internalGetArrayPointer(zAngle); |
255 |
|
|
break; |
256 |
|
|
|
257 |
|
|
case dslForce: |
258 |
|
|
return internalGetArrayPointer(force); |
259 |
|
|
break; |
260 |
|
|
|
261 |
|
|
case dslTorque: |
262 |
|
|
return internalGetArrayPointer(torque); |
263 |
|
|
break; |
264 |
|
|
|
265 |
|
|
default: |
266 |
|
|
//error message |
267 |
|
|
return NULL; |
268 |
|
|
|
269 |
|
|
} |
270 |
|
|
} |
271 |
|
|
|
272 |
|
|
double* DataStorage::internalGetArrayPointer(std::vector<Vector3d>& v) { |
273 |
|
|
if (v.size() == 0) { |
274 |
|
|
return NULL; |
275 |
|
|
} else { |
276 |
|
|
return v[0].getArrayPointer(); |
277 |
|
|
} |
278 |
|
|
} |
279 |
|
|
|
280 |
|
|
double* DataStorage::internalGetArrayPointer(std::vector<RotMat3x3d>& v) { |
281 |
|
|
if (v.size() == 0) { |
282 |
|
|
return NULL; |
283 |
|
|
} else { |
284 |
|
|
return v[0].getArrayPointer(); |
285 |
|
|
} |
286 |
|
|
|
287 |
|
|
} |
288 |
|
|
|
289 |
|
|
double* DataStorage::internalGetArrayPointer(std::vector<double>& v) { |
290 |
|
|
if (v.size() == 0) { |
291 |
|
|
return NULL; |
292 |
|
|
} else { |
293 |
|
|
return &(v[0]); |
294 |
|
|
} |
295 |
|
|
|
296 |
|
|
} |
297 |
|
|
|
298 |
|
|
template<typename T> |
299 |
|
|
void DataStorage::internalResize(std::vector<T>& v, int newSize){ |
300 |
|
|
int oldSize = v.size(); |
301 |
|
|
|
302 |
|
|
if (oldSize == newSize) { |
303 |
|
|
return; |
304 |
|
|
} else if (oldSize < newSize) { |
305 |
|
|
v.insert(v.end(), newSize-oldSize, T()); |
306 |
|
|
} else { |
307 |
|
|
typename std::vector<T>::iterator i; |
308 |
|
|
i = v.begin(); |
309 |
|
|
std::advance(i, newSize); |
310 |
|
|
v.erase(i, v.end()); |
311 |
|
|
} |
312 |
|
|
} |
313 |
|
|
|
314 |
|
|
template<typename T> |
315 |
gezelter |
246 |
void DataStorage::internalCopy(std::vector<T>& v, int source, int num, int target) { |
316 |
tim |
155 |
typename std::vector<T>::iterator first; |
317 |
|
|
typename std::vector<T>::iterator last; |
318 |
|
|
typename std::vector<T>::iterator result; |
319 |
|
|
|
320 |
|
|
first = v.begin(); |
321 |
|
|
last = v.begin(); |
322 |
|
|
result = v.begin(); |
323 |
|
|
|
324 |
|
|
std::advance(first, source); |
325 |
|
|
//STL algorithm use half opened range |
326 |
|
|
std::advance(last, num + 1); |
327 |
|
|
std::advance(result, target ); |
328 |
|
|
|
329 |
|
|
std::copy(first, last, result); |
330 |
|
|
} |
331 |
tim |
323 |
|
332 |
|
|
int DataStorage::getBytesPerStuntDouble(int layout) { |
333 |
|
|
int bytes = 0; |
334 |
|
|
if (layout & dslPosition) { |
335 |
|
|
bytes += sizeof(Vector3d); |
336 |
|
|
} else if (layout & dslVelocity) { |
337 |
|
|
bytes += sizeof(Vector3d); |
338 |
|
|
} else if (layout & dslAmat) { |
339 |
|
|
bytes += sizeof(Mat3x3d); |
340 |
|
|
} else if (layout & dslAngularMomentum) { |
341 |
|
|
bytes += sizeof(Vector3d); |
342 |
|
|
} else if (layout & dslElectroFrame) { |
343 |
|
|
bytes += sizeof(Mat3x3d); |
344 |
|
|
} else if (layout & dslZAngle) { |
345 |
|
|
bytes += sizeof(Vector3d); |
346 |
|
|
} else if (layout & dslForce) { |
347 |
|
|
bytes += sizeof(Vector3d); |
348 |
|
|
} else if (layout & dslTorque) { |
349 |
|
|
bytes += sizeof(Vector3d); |
350 |
|
|
} |
351 |
|
|
return bytes; |
352 |
|
|
} |
353 |
|
|
|
354 |
|
|
} |