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 |
#include <fstream> |
42 |
#include "integrators/LDForceManager.hpp" |
43 |
#include "math/CholeskyDecomposition.hpp" |
44 |
namespace oopse { |
45 |
|
46 |
LDForceManager::LDForceManager(SimInfo* info) : ForceManager(info){ |
47 |
Globals* simParams = info->getSimParams(); |
48 |
std::map<std::string, HydroProp> hydroPropMap; |
49 |
if (simParams->haveHydroPropFile()) { |
50 |
hydroPropMap = parseFrictionFile(simParams->getHydroPropFile()); |
51 |
} else { |
52 |
//error |
53 |
} |
54 |
|
55 |
SimInfo::MoleculeIterator i; |
56 |
Molecule::IntegrableObjectIterator j; |
57 |
Molecule* mol; |
58 |
StuntDouble* integrableObject; |
59 |
for (mol = info->beginMolecule(i); mol != NULL; mol = info->nextMolecule(i)) { |
60 |
for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL; |
61 |
integrableObject = mol->nextIntegrableObject(j)) { |
62 |
std::map<std::string, HydroProp>::iterator iter = hydroPropMap.find(integrableObject->getType()); |
63 |
if (iter != hydroPropMap.end()) { |
64 |
hydroProps_.push_back(iter->second); |
65 |
} else { |
66 |
//error |
67 |
} |
68 |
|
69 |
} |
70 |
} |
71 |
variance_ = 2.0*simParams->getDt(); |
72 |
} |
73 |
std::map<std::string, HydroProp> LDForceManager::parseFrictionFile(const std::string& filename) { |
74 |
std::map<std::string, HydroProp> props; |
75 |
std::ifstream ifs(filename.c_str()); |
76 |
if (ifs.is_open()) { |
77 |
|
78 |
} |
79 |
|
80 |
const unsigned int BufferSize = 65535; |
81 |
char buffer[BufferSize]; |
82 |
while (ifs.getline(buffer, BufferSize)) { |
83 |
StringTokenizer tokenizer(buffer); |
84 |
HydroProp currProp; |
85 |
if (tokenizer.countTokens() >= 67) { |
86 |
std::string atomName = tokenizer.nextToken(); |
87 |
currProp.cod[0] = tokenizer.nextTokenAsDouble(); |
88 |
currProp.cod[1] = tokenizer.nextTokenAsDouble(); |
89 |
currProp.cod[2] = tokenizer.nextTokenAsDouble(); |
90 |
|
91 |
currProp.Ddtt(0,0) = tokenizer.nextTokenAsDouble(); |
92 |
currProp.Ddtt(0,1) = tokenizer.nextTokenAsDouble(); |
93 |
currProp.Ddtt(0,2) = tokenizer.nextTokenAsDouble(); |
94 |
currProp.Ddtt(1,0) = tokenizer.nextTokenAsDouble(); |
95 |
currProp.Ddtt(1,1) = tokenizer.nextTokenAsDouble(); |
96 |
currProp.Ddtt(1,2) = tokenizer.nextTokenAsDouble(); |
97 |
currProp.Ddtt(2,0) = tokenizer.nextTokenAsDouble(); |
98 |
currProp.Ddtt(2,1) = tokenizer.nextTokenAsDouble(); |
99 |
currProp.Ddtt(2,2) = tokenizer.nextTokenAsDouble(); |
100 |
|
101 |
currProp.Ddtr(0,0) = tokenizer.nextTokenAsDouble(); |
102 |
currProp.Ddtr(0,1) = tokenizer.nextTokenAsDouble(); |
103 |
currProp.Ddtr(0,2) = tokenizer.nextTokenAsDouble(); |
104 |
currProp.Ddtr(1,0) = tokenizer.nextTokenAsDouble(); |
105 |
currProp.Ddtr(1,1) = tokenizer.nextTokenAsDouble(); |
106 |
currProp.Ddtr(1,2) = tokenizer.nextTokenAsDouble(); |
107 |
currProp.Ddtr(2,0) = tokenizer.nextTokenAsDouble(); |
108 |
currProp.Ddtr(2,1) = tokenizer.nextTokenAsDouble(); |
109 |
currProp.Ddtr(2,2) = tokenizer.nextTokenAsDouble(); |
110 |
|
111 |
currProp.Ddrr(0,0) = tokenizer.nextTokenAsDouble(); |
112 |
currProp.Ddrr(0,1) = tokenizer.nextTokenAsDouble(); |
113 |
currProp.Ddrr(0,2) = tokenizer.nextTokenAsDouble(); |
114 |
currProp.Ddrr(1,0) = tokenizer.nextTokenAsDouble(); |
115 |
currProp.Ddrr(1,1) = tokenizer.nextTokenAsDouble(); |
116 |
currProp.Ddrr(1,2) = tokenizer.nextTokenAsDouble(); |
117 |
currProp.Ddrr(2,0) = tokenizer.nextTokenAsDouble(); |
118 |
currProp.Ddrr(2,1) = tokenizer.nextTokenAsDouble(); |
119 |
currProp.Ddrr(2,2) = tokenizer.nextTokenAsDouble(); |
120 |
|
121 |
currProp.Xidtt(0,0) = tokenizer.nextTokenAsDouble(); |
122 |
currProp.Xidtt(0,1) = tokenizer.nextTokenAsDouble(); |
123 |
currProp.Xidtt(0,2) = tokenizer.nextTokenAsDouble(); |
124 |
currProp.Xidtt(1,0) = tokenizer.nextTokenAsDouble(); |
125 |
currProp.Xidtt(1,1) = tokenizer.nextTokenAsDouble(); |
126 |
currProp.Xidtt(1,2) = tokenizer.nextTokenAsDouble(); |
127 |
currProp.Xidtt(2,0) = tokenizer.nextTokenAsDouble(); |
128 |
currProp.Xidtt(2,1) = tokenizer.nextTokenAsDouble(); |
129 |
currProp.Xidtt(2,2) = tokenizer.nextTokenAsDouble(); |
130 |
|
131 |
currProp.Xidrt(0,0) = tokenizer.nextTokenAsDouble(); |
132 |
currProp.Xidrt(0,1) = tokenizer.nextTokenAsDouble(); |
133 |
currProp.Xidrt(0,2) = tokenizer.nextTokenAsDouble(); |
134 |
currProp.Xidrt(1,0) = tokenizer.nextTokenAsDouble(); |
135 |
currProp.Xidrt(1,1) = tokenizer.nextTokenAsDouble(); |
136 |
currProp.Xidrt(1,2) = tokenizer.nextTokenAsDouble(); |
137 |
currProp.Xidrt(2,0) = tokenizer.nextTokenAsDouble(); |
138 |
currProp.Xidrt(2,1) = tokenizer.nextTokenAsDouble(); |
139 |
currProp.Xidrt(2,2) = tokenizer.nextTokenAsDouble(); |
140 |
|
141 |
currProp.Xidtr(0,0) = tokenizer.nextTokenAsDouble(); |
142 |
currProp.Xidtr(0,1) = tokenizer.nextTokenAsDouble(); |
143 |
currProp.Xidtr(0,2) = tokenizer.nextTokenAsDouble(); |
144 |
currProp.Xidtr(1,0) = tokenizer.nextTokenAsDouble(); |
145 |
currProp.Xidtr(1,1) = tokenizer.nextTokenAsDouble(); |
146 |
currProp.Xidtr(1,2) = tokenizer.nextTokenAsDouble(); |
147 |
currProp.Xidtr(2,0) = tokenizer.nextTokenAsDouble(); |
148 |
currProp.Xidtr(2,1) = tokenizer.nextTokenAsDouble(); |
149 |
currProp.Xidtr(2,2) = tokenizer.nextTokenAsDouble(); |
150 |
|
151 |
currProp.Xidrr(0,0) = tokenizer.nextTokenAsDouble(); |
152 |
currProp.Xidrr(0,1) = tokenizer.nextTokenAsDouble(); |
153 |
currProp.Xidrr(0,2) = tokenizer.nextTokenAsDouble(); |
154 |
currProp.Xidrr(1,0) = tokenizer.nextTokenAsDouble(); |
155 |
currProp.Xidrr(1,1) = tokenizer.nextTokenAsDouble(); |
156 |
currProp.Xidrr(1,2) = tokenizer.nextTokenAsDouble(); |
157 |
currProp.Xidrr(2,0) = tokenizer.nextTokenAsDouble(); |
158 |
currProp.Xidrr(2,1) = tokenizer.nextTokenAsDouble(); |
159 |
currProp.Xidrr(2,2) = tokenizer.nextTokenAsDouble(); |
160 |
props.insert(std::map<std::string, HydroProp>::value_type(atomName, currProp)); |
161 |
} |
162 |
} |
163 |
|
164 |
return props; |
165 |
} |
166 |
|
167 |
void LDForceManager::postCalculation() { |
168 |
SimInfo::MoleculeIterator i; |
169 |
Molecule::IntegrableObjectIterator j; |
170 |
Molecule* mol; |
171 |
StuntDouble* integrableObject; |
172 |
Vector3d vel; |
173 |
Vector3d pos; |
174 |
Vector3d frc; |
175 |
Mat3x3d A; |
176 |
Vector3d Tb; |
177 |
Vector3d ji; |
178 |
double mass; |
179 |
unsigned int index = 0; |
180 |
for (mol = info_->beginMolecule(i); mol != NULL; mol = info_->nextMolecule(i)) { |
181 |
for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL; |
182 |
integrableObject = mol->nextIntegrableObject(j)) { |
183 |
|
184 |
vel =integrableObject->getVel(); |
185 |
if (integrableObject->isDirectional()){ |
186 |
//calculate angular velocity in lab frame |
187 |
Mat3x3d I = integrableObject->getI(); |
188 |
Vector3d angMom = integrableObject->getJ(); |
189 |
Vector3d omega; |
190 |
|
191 |
if (integrableObject->isLinear()) { |
192 |
int linearAxis = integrableObject->linearAxis(); |
193 |
int l = (linearAxis +1 )%3; |
194 |
int m = (linearAxis +2 )%3; |
195 |
omega[l] = angMom[l] /I(l, l); |
196 |
omega[m] = angMom[m] /I(m, m); |
197 |
|
198 |
} else { |
199 |
omega[0] = angMom[0] /I(0, 0); |
200 |
omega[1] = angMom[1] /I(1, 1); |
201 |
omega[2] = angMom[2] /I(2, 2); |
202 |
} |
203 |
|
204 |
//apply friction force and torque at center of diffusion |
205 |
A = integrableObject->getA(); |
206 |
Vector3d rcd = A.transpose() * hydroProps_[index].cod; |
207 |
Vector3d vcd = vel + cross(omega, rcd); |
208 |
Vector3d frictionForce = -(hydroProps_[index].Xidtt * vcd + hydroProps_[index].Xidrt * omega); |
209 |
integrableObject->addFrc(frictionForce); |
210 |
Vector3d frictionTorque = - (hydroProps_[index].Xidtr * vcd + hydroProps_[index].Xidrr * omega); |
211 |
integrableObject->addTrq(frictionTorque); |
212 |
|
213 |
//apply random force and torque at center of diffustion |
214 |
Vector3d randomForce; |
215 |
Vector3d randomTorque; |
216 |
genRandomForceAndTorque(randomForce, randomTorque, index, variance_); |
217 |
integrableObject->addFrc(randomForce); |
218 |
integrableObject->addTrq(randomTorque + cross(rcd, randomForce )); |
219 |
|
220 |
} else { |
221 |
//spheric atom |
222 |
Vector3d frictionForce = -(hydroProps_[index].Xidtt *vel); |
223 |
Vector3d randomForce; |
224 |
Vector3d randomTorque; |
225 |
genRandomForceAndTorque(randomForce, randomTorque, index, variance_); |
226 |
integrableObject->addFrc(frictionForce+randomForce); |
227 |
} |
228 |
|
229 |
++index; |
230 |
|
231 |
} |
232 |
} |
233 |
|
234 |
ForceManager::postCalculation(); |
235 |
|
236 |
|
237 |
|
238 |
} |
239 |
|
240 |
void LDForceManager::genRandomForceAndTorque(Vector3d& force, Vector3d& torque, unsigned int index, double variance) { |
241 |
SquareMatrix<double, 6> Dd; |
242 |
SquareMatrix<double, 6> S; |
243 |
Vector<double, 6> Z; |
244 |
Vector<double, 6> generalForce; |
245 |
Dd.setSubMatrix(0, 0, hydroProps_[index].Ddtt); |
246 |
Dd.setSubMatrix(0, 3, hydroProps_[index].Ddtr.transpose()); |
247 |
Dd.setSubMatrix(3, 0, hydroProps_[index].Ddtr); |
248 |
Dd.setSubMatrix(3, 3, hydroProps_[index].Ddrr); |
249 |
CholeskyDecomposition(Dd, S); |
250 |
|
251 |
Z[0] = randNumGen_.randNorm(0, variance); |
252 |
Z[1] = randNumGen_.randNorm(0, variance); |
253 |
Z[2] = randNumGen_.randNorm(0, variance); |
254 |
Z[3] = randNumGen_.randNorm(0, variance); |
255 |
Z[4] = randNumGen_.randNorm(0, variance); |
256 |
Z[5] = randNumGen_.randNorm(0, variance); |
257 |
|
258 |
generalForce = S*Z; |
259 |
force[0] = generalForce[0]; |
260 |
force[1] = generalForce[1]; |
261 |
force[2] = generalForce[2]; |
262 |
torque[0] = generalForce[3]; |
263 |
torque[1] = generalForce[4]; |
264 |
torque[2] = generalForce[5]; |
265 |
|
266 |
} |
267 |
|
268 |
} |