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