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, 24107 (2008). |
39 |
* [4] Vardeman & Gezelter, in progress (2009). |
40 |
*/ |
41 |
|
42 |
/** |
43 |
* @file ForceManager.cpp |
44 |
* @author tlin |
45 |
* @date 11/09/2004 |
46 |
* @time 10:39am |
47 |
* @version 1.0 |
48 |
*/ |
49 |
|
50 |
#include "brains/ForceManager.hpp" |
51 |
#include "primitives/Molecule.hpp" |
52 |
#include "UseTheForce/doForces_interface.h" |
53 |
#define __OPENMD_C |
54 |
#include "UseTheForce/DarkSide/fInteractionMap.h" |
55 |
#include "utils/simError.h" |
56 |
#include "primitives/Bond.hpp" |
57 |
#include "primitives/Bend.hpp" |
58 |
#include "primitives/Torsion.hpp" |
59 |
#include "primitives/Inversion.hpp" |
60 |
|
61 |
namespace OpenMD { |
62 |
|
63 |
ForceManager::ForceManager(SimInfo * info) : info_(info), |
64 |
NBforcesInitialized_(false) { |
65 |
std::cerr << __PRETTY_FUNCTION__ << "\n"; |
66 |
lj_ = new LJ(info_->getForceField()); |
67 |
} |
68 |
|
69 |
void ForceManager::calcForces() { |
70 |
|
71 |
std::cerr << __PRETTY_FUNCTION__ << "\n"; |
72 |
if (!info_->isFortranInitialized()) { |
73 |
info_->update(); |
74 |
} |
75 |
|
76 |
if (!NBforcesInitialized_) { |
77 |
lj_->initialize(); |
78 |
} |
79 |
|
80 |
preCalculation(); |
81 |
|
82 |
calcShortRangeInteraction(); |
83 |
|
84 |
calcLongRangeInteraction(); |
85 |
|
86 |
postCalculation(); |
87 |
|
88 |
} |
89 |
|
90 |
void ForceManager::preCalculation() { |
91 |
SimInfo::MoleculeIterator mi; |
92 |
Molecule* mol; |
93 |
Molecule::AtomIterator ai; |
94 |
Atom* atom; |
95 |
Molecule::RigidBodyIterator rbIter; |
96 |
RigidBody* rb; |
97 |
|
98 |
// forces are zeroed here, before any are accumulated. |
99 |
// NOTE: do not rezero the forces in Fortran. |
100 |
|
101 |
for (mol = info_->beginMolecule(mi); mol != NULL; |
102 |
mol = info_->nextMolecule(mi)) { |
103 |
for(atom = mol->beginAtom(ai); atom != NULL; atom = mol->nextAtom(ai)) { |
104 |
atom->zeroForcesAndTorques(); |
105 |
} |
106 |
|
107 |
//change the positions of atoms which belong to the rigidbodies |
108 |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; |
109 |
rb = mol->nextRigidBody(rbIter)) { |
110 |
rb->zeroForcesAndTorques(); |
111 |
} |
112 |
|
113 |
} |
114 |
|
115 |
// Zero out the stress tensor |
116 |
tau *= 0.0; |
117 |
|
118 |
} |
119 |
|
120 |
void ForceManager::calcShortRangeInteraction() { |
121 |
Molecule* mol; |
122 |
RigidBody* rb; |
123 |
Bond* bond; |
124 |
Bend* bend; |
125 |
Torsion* torsion; |
126 |
Inversion* inversion; |
127 |
SimInfo::MoleculeIterator mi; |
128 |
Molecule::RigidBodyIterator rbIter; |
129 |
Molecule::BondIterator bondIter;; |
130 |
Molecule::BendIterator bendIter; |
131 |
Molecule::TorsionIterator torsionIter; |
132 |
Molecule::InversionIterator inversionIter; |
133 |
RealType bondPotential = 0.0; |
134 |
RealType bendPotential = 0.0; |
135 |
RealType torsionPotential = 0.0; |
136 |
RealType inversionPotential = 0.0; |
137 |
|
138 |
//calculate short range interactions |
139 |
for (mol = info_->beginMolecule(mi); mol != NULL; |
140 |
mol = info_->nextMolecule(mi)) { |
141 |
|
142 |
//change the positions of atoms which belong to the rigidbodies |
143 |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; |
144 |
rb = mol->nextRigidBody(rbIter)) { |
145 |
rb->updateAtoms(); |
146 |
} |
147 |
|
148 |
for (bond = mol->beginBond(bondIter); bond != NULL; |
149 |
bond = mol->nextBond(bondIter)) { |
150 |
bond->calcForce(); |
151 |
bondPotential += bond->getPotential(); |
152 |
} |
153 |
|
154 |
for (bend = mol->beginBend(bendIter); bend != NULL; |
155 |
bend = mol->nextBend(bendIter)) { |
156 |
|
157 |
RealType angle; |
158 |
bend->calcForce(angle); |
159 |
RealType currBendPot = bend->getPotential(); |
160 |
|
161 |
bendPotential += bend->getPotential(); |
162 |
std::map<Bend*, BendDataSet>::iterator i = bendDataSets.find(bend); |
163 |
if (i == bendDataSets.end()) { |
164 |
BendDataSet dataSet; |
165 |
dataSet.prev.angle = dataSet.curr.angle = angle; |
166 |
dataSet.prev.potential = dataSet.curr.potential = currBendPot; |
167 |
dataSet.deltaV = 0.0; |
168 |
bendDataSets.insert(std::map<Bend*, BendDataSet>::value_type(bend, dataSet)); |
169 |
}else { |
170 |
i->second.prev.angle = i->second.curr.angle; |
171 |
i->second.prev.potential = i->second.curr.potential; |
172 |
i->second.curr.angle = angle; |
173 |
i->second.curr.potential = currBendPot; |
174 |
i->second.deltaV = fabs(i->second.curr.potential - |
175 |
i->second.prev.potential); |
176 |
} |
177 |
} |
178 |
|
179 |
for (torsion = mol->beginTorsion(torsionIter); torsion != NULL; |
180 |
torsion = mol->nextTorsion(torsionIter)) { |
181 |
RealType angle; |
182 |
torsion->calcForce(angle); |
183 |
RealType currTorsionPot = torsion->getPotential(); |
184 |
torsionPotential += torsion->getPotential(); |
185 |
std::map<Torsion*, TorsionDataSet>::iterator i = torsionDataSets.find(torsion); |
186 |
if (i == torsionDataSets.end()) { |
187 |
TorsionDataSet dataSet; |
188 |
dataSet.prev.angle = dataSet.curr.angle = angle; |
189 |
dataSet.prev.potential = dataSet.curr.potential = currTorsionPot; |
190 |
dataSet.deltaV = 0.0; |
191 |
torsionDataSets.insert(std::map<Torsion*, TorsionDataSet>::value_type(torsion, dataSet)); |
192 |
}else { |
193 |
i->second.prev.angle = i->second.curr.angle; |
194 |
i->second.prev.potential = i->second.curr.potential; |
195 |
i->second.curr.angle = angle; |
196 |
i->second.curr.potential = currTorsionPot; |
197 |
i->second.deltaV = fabs(i->second.curr.potential - |
198 |
i->second.prev.potential); |
199 |
} |
200 |
} |
201 |
|
202 |
for (inversion = mol->beginInversion(inversionIter); |
203 |
inversion != NULL; |
204 |
inversion = mol->nextInversion(inversionIter)) { |
205 |
RealType angle; |
206 |
inversion->calcForce(angle); |
207 |
RealType currInversionPot = inversion->getPotential(); |
208 |
inversionPotential += inversion->getPotential(); |
209 |
std::map<Inversion*, InversionDataSet>::iterator i = inversionDataSets.find(inversion); |
210 |
if (i == inversionDataSets.end()) { |
211 |
InversionDataSet dataSet; |
212 |
dataSet.prev.angle = dataSet.curr.angle = angle; |
213 |
dataSet.prev.potential = dataSet.curr.potential = currInversionPot; |
214 |
dataSet.deltaV = 0.0; |
215 |
inversionDataSets.insert(std::map<Inversion*, InversionDataSet>::value_type(inversion, dataSet)); |
216 |
}else { |
217 |
i->second.prev.angle = i->second.curr.angle; |
218 |
i->second.prev.potential = i->second.curr.potential; |
219 |
i->second.curr.angle = angle; |
220 |
i->second.curr.potential = currInversionPot; |
221 |
i->second.deltaV = fabs(i->second.curr.potential - |
222 |
i->second.prev.potential); |
223 |
} |
224 |
} |
225 |
} |
226 |
|
227 |
RealType shortRangePotential = bondPotential + bendPotential + |
228 |
torsionPotential + inversionPotential; |
229 |
Snapshot* curSnapshot = info_->getSnapshotManager()->getCurrentSnapshot(); |
230 |
curSnapshot->statData[Stats::SHORT_RANGE_POTENTIAL] = shortRangePotential; |
231 |
curSnapshot->statData[Stats::BOND_POTENTIAL] = bondPotential; |
232 |
curSnapshot->statData[Stats::BEND_POTENTIAL] = bendPotential; |
233 |
curSnapshot->statData[Stats::DIHEDRAL_POTENTIAL] = torsionPotential; |
234 |
curSnapshot->statData[Stats::INVERSION_POTENTIAL] = inversionPotential; |
235 |
|
236 |
} |
237 |
|
238 |
void ForceManager::calcLongRangeInteraction() { |
239 |
Snapshot* curSnapshot; |
240 |
DataStorage* config; |
241 |
RealType* frc; |
242 |
RealType* pos; |
243 |
RealType* trq; |
244 |
RealType* A; |
245 |
RealType* electroFrame; |
246 |
RealType* rc; |
247 |
RealType* particlePot; |
248 |
|
249 |
//get current snapshot from SimInfo |
250 |
curSnapshot = info_->getSnapshotManager()->getCurrentSnapshot(); |
251 |
|
252 |
//get array pointers |
253 |
config = &(curSnapshot->atomData); |
254 |
frc = config->getArrayPointer(DataStorage::dslForce); |
255 |
pos = config->getArrayPointer(DataStorage::dslPosition); |
256 |
trq = config->getArrayPointer(DataStorage::dslTorque); |
257 |
A = config->getArrayPointer(DataStorage::dslAmat); |
258 |
electroFrame = config->getArrayPointer(DataStorage::dslElectroFrame); |
259 |
particlePot = config->getArrayPointer(DataStorage::dslParticlePot); |
260 |
|
261 |
//calculate the center of mass of cutoff group |
262 |
SimInfo::MoleculeIterator mi; |
263 |
Molecule* mol; |
264 |
Molecule::CutoffGroupIterator ci; |
265 |
CutoffGroup* cg; |
266 |
Vector3d com; |
267 |
std::vector<Vector3d> rcGroup; |
268 |
|
269 |
if(info_->getNCutoffGroups() > 0){ |
270 |
|
271 |
for (mol = info_->beginMolecule(mi); mol != NULL; |
272 |
mol = info_->nextMolecule(mi)) { |
273 |
for(cg = mol->beginCutoffGroup(ci); cg != NULL; |
274 |
cg = mol->nextCutoffGroup(ci)) { |
275 |
cg->getCOM(com); |
276 |
rcGroup.push_back(com); |
277 |
} |
278 |
}// end for (mol) |
279 |
|
280 |
rc = rcGroup[0].getArrayPointer(); |
281 |
} else { |
282 |
// center of mass of the group is the same as position of the atom |
283 |
// if cutoff group does not exist |
284 |
rc = pos; |
285 |
} |
286 |
|
287 |
//initialize data before passing to fortran |
288 |
RealType longRangePotential[LR_POT_TYPES]; |
289 |
RealType lrPot = 0.0; |
290 |
Vector3d totalDipole; |
291 |
int isError = 0; |
292 |
|
293 |
for (int i=0; i<LR_POT_TYPES;i++){ |
294 |
longRangePotential[i]=0.0; //Initialize array |
295 |
} |
296 |
|
297 |
doForceLoop(pos, |
298 |
rc, |
299 |
A, |
300 |
electroFrame, |
301 |
frc, |
302 |
trq, |
303 |
tau.getArrayPointer(), |
304 |
longRangePotential, |
305 |
particlePot, |
306 |
&isError ); |
307 |
|
308 |
if( isError ){ |
309 |
sprintf( painCave.errMsg, |
310 |
"Error returned from the fortran force calculation.\n" ); |
311 |
painCave.isFatal = 1; |
312 |
simError(); |
313 |
} |
314 |
for (int i=0; i<LR_POT_TYPES;i++){ |
315 |
lrPot += longRangePotential[i]; //Quick hack |
316 |
} |
317 |
|
318 |
// grab the simulation box dipole moment if specified |
319 |
if (info_->getCalcBoxDipole()){ |
320 |
getAccumulatedBoxDipole(totalDipole.getArrayPointer()); |
321 |
|
322 |
curSnapshot->statData[Stats::BOX_DIPOLE_X] = totalDipole(0); |
323 |
curSnapshot->statData[Stats::BOX_DIPOLE_Y] = totalDipole(1); |
324 |
curSnapshot->statData[Stats::BOX_DIPOLE_Z] = totalDipole(2); |
325 |
} |
326 |
|
327 |
//store the tau and long range potential |
328 |
curSnapshot->statData[Stats::LONG_RANGE_POTENTIAL] = lrPot; |
329 |
curSnapshot->statData[Stats::VANDERWAALS_POTENTIAL] = longRangePotential[VDW_POT]; |
330 |
curSnapshot->statData[Stats::ELECTROSTATIC_POTENTIAL] = longRangePotential[ELECTROSTATIC_POT]; |
331 |
} |
332 |
|
333 |
|
334 |
void ForceManager::postCalculation() { |
335 |
SimInfo::MoleculeIterator mi; |
336 |
Molecule* mol; |
337 |
Molecule::RigidBodyIterator rbIter; |
338 |
RigidBody* rb; |
339 |
Snapshot* curSnapshot = info_->getSnapshotManager()->getCurrentSnapshot(); |
340 |
|
341 |
// collect the atomic forces onto rigid bodies |
342 |
|
343 |
for (mol = info_->beginMolecule(mi); mol != NULL; |
344 |
mol = info_->nextMolecule(mi)) { |
345 |
for (rb = mol->beginRigidBody(rbIter); rb != NULL; |
346 |
rb = mol->nextRigidBody(rbIter)) { |
347 |
Mat3x3d rbTau = rb->calcForcesAndTorquesAndVirial(); |
348 |
tau += rbTau; |
349 |
} |
350 |
} |
351 |
|
352 |
#ifdef IS_MPI |
353 |
Mat3x3d tmpTau(tau); |
354 |
MPI_Allreduce(tmpTau.getArrayPointer(), tau.getArrayPointer(), |
355 |
9, MPI_REALTYPE, MPI_SUM, MPI_COMM_WORLD); |
356 |
#endif |
357 |
curSnapshot->statData.setTau(tau); |
358 |
} |
359 |
|
360 |
} //end namespace OpenMD |