| 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, 234107 (2008). |
| 39 |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
| 40 |
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
| 41 |
*/ |
| 42 |
|
| 43 |
#include <algorithm> |
| 44 |
#include <functional> |
| 45 |
#include "applications/sequentialProps/ContactAngle2.hpp" |
| 46 |
#include "utils/simError.h" |
| 47 |
#include "io/DumpReader.hpp" |
| 48 |
#include "primitives/Molecule.hpp" |
| 49 |
#include "utils/NumericConstant.hpp" |
| 50 |
#include "utils/PhysicalConstants.hpp" |
| 51 |
#include "math/Eigenvalue.hpp" |
| 52 |
|
| 53 |
namespace OpenMD { |
| 54 |
|
| 55 |
ContactAngle2::ContactAngle2(SimInfo* info, const std::string& filename, |
| 56 |
const std::string& sele, RealType solidZ, |
| 57 |
RealType threshDens, int nrbins, int nzbins) |
| 58 |
: SequentialAnalyzer(info, filename), selectionScript_(sele), |
| 59 |
evaluator_(info), seleMan_(info), solidZ_(solidZ), |
| 60 |
threshDens_(threshDens), nRBins_(nrbins), nZBins_(nzbins) { |
| 61 |
|
| 62 |
setOutputName(getPrefix(filename) + ".ca2"); |
| 63 |
|
| 64 |
evaluator_.loadScriptString(sele); |
| 65 |
|
| 66 |
if (!evaluator_.isDynamic()) { |
| 67 |
seleMan_.setSelectionSet(evaluator_.evaluate()); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
void ContactAngle2::doFrame() { |
| 72 |
StuntDouble* sd; |
| 73 |
int i; |
| 74 |
|
| 75 |
// set up the bins for density analysis |
| 76 |
|
| 77 |
Mat3x3d hmat = info_->getSnapshotManager()->getCurrentSnapshot()->getHmat(); |
| 78 |
RealType len = std::min(hmat(0, 0), hmat(1, 1)); |
| 79 |
RealType zLen = hmat(2,2); |
| 80 |
|
| 81 |
RealType dr = len / (RealType) nRBins_; |
| 82 |
RealType dz = zLen / (RealType) nZBins_; |
| 83 |
|
| 84 |
std::vector<std::vector<RealType> > histo; |
| 85 |
histo.resize(nRBins_); |
| 86 |
for (unsigned int i = 0; i < histo.size(); ++i){ |
| 87 |
histo[i].resize(nZBins_); |
| 88 |
std::fill(histo[i].begin(), histo[i].end(), 0.0); |
| 89 |
} |
| 90 |
|
| 91 |
if (evaluator_.isDynamic()) { |
| 92 |
seleMan_.setSelectionSet(evaluator_.evaluate()); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
RealType mtot = 0.0; |
| 97 |
Vector3d com(V3Zero); |
| 98 |
RealType mass; |
| 99 |
|
| 100 |
for (sd = seleMan_.beginSelected(i); sd != NULL; |
| 101 |
sd = seleMan_.nextSelected(i)) { |
| 102 |
mass = sd->getMass(); |
| 103 |
mtot += mass; |
| 104 |
com += sd->getPos() * mass; |
| 105 |
} |
| 106 |
|
| 107 |
com /= mtot; |
| 108 |
|
| 109 |
// now that we have the centroid, we can make cylindrical density maps |
| 110 |
Vector3d pos; |
| 111 |
RealType r; |
| 112 |
RealType z; |
| 113 |
|
| 114 |
for (sd = seleMan_.beginSelected(i); sd != NULL; |
| 115 |
sd = seleMan_.nextSelected(i)) { |
| 116 |
pos = sd->getPos() - com; |
| 117 |
|
| 118 |
// r goes from zero upwards |
| 119 |
r = sqrt(pow(pos.x(), 2) + pow(pos.y(), 2)); |
| 120 |
// z is possibly symmetric around 0 |
| 121 |
z = pos.z(); |
| 122 |
|
| 123 |
int whichRBin = int(r / dr); |
| 124 |
int whichZBin = int( (zLen/2.0 + z) / dz); |
| 125 |
|
| 126 |
if ((whichRBin < nRBins_) && (whichZBin >= 0) && (whichZBin < nZBins_)) |
| 127 |
histo[whichRBin][whichZBin] += sd->getMass(); |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
for(unsigned int i = 0 ; i < histo.size(); ++i){ |
| 132 |
|
| 133 |
RealType rL = i * dr; |
| 134 |
RealType rU = rL + dr; |
| 135 |
RealType volSlice = NumericConstant::PI * dz * (( rU*rU ) - ( rL*rL )); |
| 136 |
|
| 137 |
for (unsigned int j = 0; j < histo[i].size(); ++j) { |
| 138 |
histo[i][j] *= PhysicalConstants::densityConvert / volSlice; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
std::vector<Vector<RealType, 2> > points; |
| 143 |
points.clear(); |
| 144 |
|
| 145 |
for (unsigned int j = 0; j < nZBins_; ++j) { |
| 146 |
|
| 147 |
// The z coordinates were measured relative to the selection |
| 148 |
// center of mass. However, we're interested in the elevation |
| 149 |
// above the solid surface. Also, the binning was done around |
| 150 |
// zero with enough bins to cover the zLength of the box: |
| 151 |
|
| 152 |
RealType thez = com.z() - solidZ_ - zLen/2.0 + dz * (j + 0.5); |
| 153 |
bool aboveThresh = false; |
| 154 |
bool foundThresh = false; |
| 155 |
int rloc = 0; |
| 156 |
|
| 157 |
for (unsigned int i = 0; i < nRBins_; ++i) { |
| 158 |
RealType ther = dr * (i + 0.5); |
| 159 |
if (histo[i][j] >= threshDens_) aboveThresh = true; |
| 160 |
|
| 161 |
if (aboveThresh && (histo[i][j] <= threshDens_)) { |
| 162 |
rloc = i; |
| 163 |
foundThresh = true; |
| 164 |
aboveThresh = false; |
| 165 |
} |
| 166 |
|
| 167 |
} |
| 168 |
if (foundThresh) { |
| 169 |
Vector<RealType,2> point; |
| 170 |
point[0] = dr*(rloc+0.5); |
| 171 |
point[1] = thez; |
| 172 |
points.push_back( point ); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
int numPoints = points.size(); |
| 177 |
|
| 178 |
// Compute the average of the data points. |
| 179 |
Vector<RealType, 2> average = points[0]; |
| 180 |
int i0; |
| 181 |
for (i0 = 1; i0 < numPoints; ++i0) { |
| 182 |
average += points[i0]; |
| 183 |
} |
| 184 |
RealType invNumPoints = ((RealType)1)/(RealType)numPoints; |
| 185 |
average *= invNumPoints; |
| 186 |
|
| 187 |
DynamicRectMatrix<RealType> mat(4, 4); |
| 188 |
int row, col; |
| 189 |
for (row = 0; row < 4; ++row) { |
| 190 |
for (col = 0; col < 4; ++col){ |
| 191 |
mat(row,col) = 0.0; |
| 192 |
} |
| 193 |
} |
| 194 |
for (int i = 0; i < numPoints; ++i) { |
| 195 |
RealType x = points[i][0]; |
| 196 |
RealType y = points[i][1]; |
| 197 |
RealType x2 = x*x; |
| 198 |
RealType y2 = y*y; |
| 199 |
RealType xy = x*y; |
| 200 |
RealType r2 = x2+y2; |
| 201 |
RealType xr2 = x*r2; |
| 202 |
RealType yr2 = y*r2; |
| 203 |
RealType r4 = r2*r2; |
| 204 |
|
| 205 |
mat(0,1) += x; |
| 206 |
mat(0,2) += y; |
| 207 |
mat(0,3) += r2; |
| 208 |
mat(1,1) += x2; |
| 209 |
mat(1,2) += xy; |
| 210 |
mat(1,3) += xr2; |
| 211 |
mat(2,2) += y2; |
| 212 |
mat(2,3) += yr2; |
| 213 |
mat(3,3) += r4; |
| 214 |
} |
| 215 |
mat(0,0) = (RealType)numPoints; |
| 216 |
|
| 217 |
for (row = 0; row < 4; ++row) { |
| 218 |
for (col = 0; col < row; ++col) { |
| 219 |
mat(row,col) = mat(col,row); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
for (row = 0; row < 4; ++row) { |
| 224 |
for (col = 0; col < 4; ++col) { |
| 225 |
mat(row,col) *= invNumPoints; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
JAMA::Eigenvalue<RealType> eigensystem(mat); |
| 230 |
DynamicRectMatrix<RealType> evects(4, 4); |
| 231 |
DynamicVector<RealType> evals(4); |
| 232 |
|
| 233 |
eigensystem.getRealEigenvalues(evals); |
| 234 |
eigensystem.getV(evects); |
| 235 |
|
| 236 |
DynamicVector<RealType> evector = evects.getColumn(0); |
| 237 |
RealType inv = ((RealType)1)/evector[3]; // beware zero divide |
| 238 |
RealType coeff[3]; |
| 239 |
for (row = 0; row < 3; ++row) { |
| 240 |
coeff[row] = inv*evector[row]; |
| 241 |
} |
| 242 |
|
| 243 |
Vector<RealType, 2> center; |
| 244 |
|
| 245 |
center[0] = -((RealType)0.5)*coeff[1]; |
| 246 |
center[1] = -((RealType)0.5)*coeff[2]; |
| 247 |
RealType radius = sqrt(fabs(center[0]*center[0] + center[1]*center[1] |
| 248 |
- coeff[0])); |
| 249 |
RealType ev0 = fabs(evals[0]); |
| 250 |
|
| 251 |
int i1; |
| 252 |
for (i1 = 0; i1 < 100; ++i1) { |
| 253 |
// Update the iterates. |
| 254 |
Vector<RealType, 2> current = center; |
| 255 |
|
| 256 |
// Compute average L, dL/da, dL/db. |
| 257 |
RealType lenAverage = (RealType)0; |
| 258 |
Vector<RealType, 2> derLenAverage = Vector<RealType, 2>(0.0); |
| 259 |
for (i0 = 0; i0 < numPoints; ++i0) { |
| 260 |
Vector<RealType, 2> diff = points[i0] - center; |
| 261 |
RealType length = diff.length(); |
| 262 |
if (length > 1e-6) { |
| 263 |
lenAverage += length; |
| 264 |
RealType invLength = ((RealType)1)/length; |
| 265 |
derLenAverage -= invLength*diff; |
| 266 |
} |
| 267 |
} |
| 268 |
lenAverage *= invNumPoints; |
| 269 |
derLenAverage *= invNumPoints; |
| 270 |
|
| 271 |
center = average + lenAverage*derLenAverage; |
| 272 |
radius = lenAverage; |
| 273 |
|
| 274 |
Vector<RealType, 2> diff = center - current; |
| 275 |
if (fabs(diff[0]) <= 1e-6 && fabs(diff[1]) <= 1e-6) { |
| 276 |
break; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
RealType zCen = center[1]; |
| 281 |
RealType rDrop = radius; |
| 282 |
RealType ca; |
| 283 |
|
| 284 |
if (fabs(zCen) > rDrop) { |
| 285 |
ca = 180.0; |
| 286 |
} else { |
| 287 |
|
| 288 |
if (zCen >= 0.0) { |
| 289 |
ca = 90.0 + asin(zCen/rDrop)*(180.0/M_PI); |
| 290 |
} else { |
| 291 |
ca = 90 - asin(zCen/rDrop)*(180.0/M_PI); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
values_.push_back( ca ); |
| 296 |
|
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
|