1 |
/* |
2 |
* Copyright (c) 2008, 2009 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 |
#include <fstream> |
42 |
#include <iostream> |
43 |
#include "integrators/SMIPDForceManager.hpp" |
44 |
#include "utils/PhysicalConstants.hpp" |
45 |
#include "math/ConvexHull.hpp" |
46 |
#include "math/Triangle.hpp" |
47 |
#include "math/CholeskyDecomposition.hpp" |
48 |
|
49 |
namespace OpenMD { |
50 |
|
51 |
SMIPDForceManager::SMIPDForceManager(SimInfo* info) : ForceManager(info) { |
52 |
|
53 |
simParams = info->getSimParams(); |
54 |
veloMunge = new Velocitizer(info); |
55 |
|
56 |
// Create Hull, Convex Hull for now, other options later. |
57 |
|
58 |
surfaceMesh_ = new ConvexHull(); |
59 |
|
60 |
/* Check that the simulation has target pressure and target |
61 |
temperature set */ |
62 |
|
63 |
if (!simParams->haveTargetTemp()) { |
64 |
sprintf(painCave.errMsg, |
65 |
"SMIPDynamics error: You can't use the SMIPD integrator\n" |
66 |
"\twithout a targetTemp (K)!\n"); |
67 |
painCave.isFatal = 1; |
68 |
painCave.severity = OPENMD_ERROR; |
69 |
simError(); |
70 |
} else { |
71 |
targetTemp_ = simParams->getTargetTemp(); |
72 |
} |
73 |
|
74 |
if (!simParams->haveTargetPressure()) { |
75 |
sprintf(painCave.errMsg, |
76 |
"SMIPDynamics error: You can't use the SMIPD integrator\n" |
77 |
"\twithout a targetPressure (atm)!\n"); |
78 |
painCave.isFatal = 1; |
79 |
simError(); |
80 |
} else { |
81 |
// Convert pressure from atm -> amu/(fs^2*Ang) |
82 |
targetPressure_ = simParams->getTargetPressure() / |
83 |
PhysicalConstants::pressureConvert; |
84 |
} |
85 |
|
86 |
if (simParams->getUsePeriodicBoundaryConditions()) { |
87 |
sprintf(painCave.errMsg, |
88 |
"SMIPDynamics error: You can't use the SMIPD integrator\n" |
89 |
"\twith periodic boundary conditions!\n"); |
90 |
painCave.isFatal = 1; |
91 |
simError(); |
92 |
} |
93 |
|
94 |
if (!simParams->haveViscosity()) { |
95 |
sprintf(painCave.errMsg, |
96 |
"SMIPDynamics error: You can't use the SMIPD integrator\n" |
97 |
"\twithout a viscosity!\n"); |
98 |
painCave.isFatal = 1; |
99 |
painCave.severity = OPENMD_ERROR; |
100 |
simError(); |
101 |
}else{ |
102 |
viscosity_ = simParams->getViscosity(); |
103 |
} |
104 |
|
105 |
dt_ = simParams->getDt(); |
106 |
|
107 |
variance_ = 2.0 * PhysicalConstants::kb * targetTemp_ / dt_; |
108 |
|
109 |
// Build a vector of integrable objects to determine if the are |
110 |
// surface atoms |
111 |
Molecule* mol; |
112 |
StuntDouble* integrableObject; |
113 |
SimInfo::MoleculeIterator i; |
114 |
Molecule::IntegrableObjectIterator j; |
115 |
|
116 |
for (mol = info_->beginMolecule(i); mol != NULL; |
117 |
mol = info_->nextMolecule(i)) { |
118 |
for (integrableObject = mol->beginIntegrableObject(j); |
119 |
integrableObject != NULL; |
120 |
integrableObject = mol->nextIntegrableObject(j)) { |
121 |
localSites_.push_back(integrableObject); |
122 |
} |
123 |
} |
124 |
} |
125 |
|
126 |
void SMIPDForceManager::postCalculation(bool needStress){ |
127 |
SimInfo::MoleculeIterator i; |
128 |
Molecule::IntegrableObjectIterator j; |
129 |
Molecule* mol; |
130 |
StuntDouble* integrableObject; |
131 |
|
132 |
// Compute surface Mesh |
133 |
surfaceMesh_->computeHull(localSites_); |
134 |
|
135 |
// Get total area and number of surface stunt doubles |
136 |
RealType area = surfaceMesh_->getArea(); |
137 |
std::vector<Triangle> sMesh = surfaceMesh_->getMesh(); |
138 |
int nTriangles = sMesh.size(); |
139 |
|
140 |
// Generate all of the necessary random forces |
141 |
std::vector<Vector3d> randNums = genTriangleForces(nTriangles, variance_); |
142 |
|
143 |
// Loop over the mesh faces and apply external pressure to each |
144 |
// of the faces |
145 |
std::vector<Triangle>::iterator face; |
146 |
std::vector<StuntDouble*>::iterator vertex; |
147 |
int thisFacet = 0; |
148 |
for (face = sMesh.begin(); face != sMesh.end(); ++face){ |
149 |
Triangle thisTriangle = *face; |
150 |
std::vector<StuntDouble*> vertexSDs = thisTriangle.getVertices(); |
151 |
RealType thisArea = thisTriangle.getArea(); |
152 |
Vector3d unitNormal = thisTriangle.getNormal(); |
153 |
unitNormal.normalize(); |
154 |
Vector3d centroid = thisTriangle.getCentroid(); |
155 |
Vector3d facetVel = thisTriangle.getFacetVelocity(); |
156 |
RealType thisMass = thisTriangle.getFacetMass(); |
157 |
Mat3x3d hydroTensor = thisTriangle.computeHydrodynamicTensor(viscosity_); |
158 |
|
159 |
hydroTensor *= PhysicalConstants::viscoConvert; |
160 |
Mat3x3d S; |
161 |
CholeskyDecomposition(hydroTensor, S); |
162 |
|
163 |
Vector3d extPressure = -unitNormal * (targetPressure_ * thisArea) / |
164 |
PhysicalConstants::energyConvert; |
165 |
|
166 |
Vector3d randomForce = S * randNums[thisFacet++]; |
167 |
Vector3d dragForce = -hydroTensor * facetVel; |
168 |
|
169 |
Vector3d langevinForce = (extPressure + randomForce + dragForce); |
170 |
|
171 |
// Apply triangle force to stuntdouble vertices |
172 |
for (vertex = vertexSDs.begin(); vertex != vertexSDs.end(); ++vertex){ |
173 |
if ((*vertex) != NULL){ |
174 |
Vector3d vertexForce = langevinForce / 3.0; |
175 |
(*vertex)->addFrc(vertexForce); |
176 |
} |
177 |
} |
178 |
} |
179 |
|
180 |
veloMunge->removeComDrift(); |
181 |
veloMunge->removeAngularDrift(); |
182 |
|
183 |
Snapshot* currSnapshot = info_->getSnapshotManager()->getCurrentSnapshot(); |
184 |
currSnapshot->setVolume(surfaceMesh_->getVolume()); |
185 |
ForceManager::postCalculation(needStress); |
186 |
} |
187 |
|
188 |
|
189 |
std::vector<Vector3d> SMIPDForceManager::genTriangleForces(int nTriangles, |
190 |
RealType variance) |
191 |
{ |
192 |
|
193 |
// zero fill the random vector before starting: |
194 |
std::vector<Vector3d> gaussRand; |
195 |
gaussRand.resize(nTriangles); |
196 |
std::fill(gaussRand.begin(), gaussRand.end(), V3Zero); |
197 |
|
198 |
#ifdef IS_MPI |
199 |
if (worldRank == 0) { |
200 |
#endif |
201 |
for (int i = 0; i < nTriangles; i++) { |
202 |
gaussRand[i][0] = randNumGen_.randNorm(0.0, variance); |
203 |
gaussRand[i][1] = randNumGen_.randNorm(0.0, variance); |
204 |
gaussRand[i][2] = randNumGen_.randNorm(0.0, variance); |
205 |
} |
206 |
#ifdef IS_MPI |
207 |
} |
208 |
#endif |
209 |
|
210 |
// push these out to the other processors |
211 |
|
212 |
#ifdef IS_MPI |
213 |
if (worldRank == 0) { |
214 |
MPI::COMM_WORLD.Bcast(&gaussRand[0], nTriangles*3, MPI::REALTYPE, 0); |
215 |
} else { |
216 |
MPI::COMM_WORLD.Bcast(&gaussRand[0], nTriangles*3, MPI::REALTYPE, 0); |
217 |
} |
218 |
#endif |
219 |
|
220 |
return gaussRand; |
221 |
} |
222 |
} |