OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
ContactAngle2.cpp
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the following paper when you publish your work:
33 *
34 * [1] Drisko et al., J. Open Source Softw. 9, 7004 (2024).
35 *
36 * Good starting points for code and simulation methodology are:
37 *
38 * [2] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
39 * [3] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
40 * [4] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
41 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
42 * [6] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
43 * [7] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
44 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
45 * [9] Drisko & Gezelter, J. Chem. Theory Comput. 20, 4986-4997 (2024).
46 */
47
48#include "applications/sequentialProps/ContactAngle2.hpp"
49
50#include <algorithm>
51#include <functional>
52#include <sstream>
53
54#include "io/DumpReader.hpp"
55#include "math/Eigenvalue.hpp"
57#include "utils/Constants.hpp"
58#include "utils/simError.h"
59
60namespace OpenMD {
61
62 ContactAngle2::ContactAngle2(SimInfo* info, const std::string& filename,
63 const std::string& sele1,
64 const std::string& sele2, RealType solidZ,
65 RealType centroidX, RealType centroidY,
66 RealType threshDens, RealType bufferLength,
67 int nrbins, int nzbins) :
68 SequentialAnalyzer(info, filename, sele1, sele2),
69 solidZ_(solidZ), centroidX_(centroidX), centroidY_(centroidY),
70 threshDens_(threshDens), bufferLength_(bufferLength), nRBins_(nrbins),
71 nZBins_(nzbins) {
72 setOutputName(getPrefix(filename) + ".ca2");
73
74 std::stringstream params;
75 params << " referenceZ = " << solidZ_ << ", centroid = (" << centroidX_
76 << ", " << centroidY_ << ")"
77 << ", threshDens = " << threshDens_
78 << ", bufferLength = " << bufferLength_ << ", nbins = " << nRBins_
79 << ", nbins_z = " << nZBins_;
80
81 const std::string paramString = params.str();
82 setParameterString(paramString);
83 }
84
85 void ContactAngle2::doFrame(int) {
86 StuntDouble* sd;
87 int i;
88
89 // set up the bins for density analysis
90
91 Mat3x3d hmat = info_->getSnapshotManager()->getCurrentSnapshot()->getHmat();
92 RealType len = std::min(hmat(0, 0), hmat(1, 1));
93 RealType zLen = hmat(2, 2);
94
95 RealType dr = len / (RealType)nRBins_;
96 RealType dz = zLen / (RealType)nZBins_;
97
98 std::vector<std::vector<RealType>> histo;
99 histo.resize(nRBins_);
100 for (unsigned int i = 0; i < histo.size(); ++i) {
101 histo[i].resize(nZBins_);
102 std::fill(histo[i].begin(), histo[i].end(), 0.0);
103 }
104
105 if (evaluator1_.isDynamic()) {
106 seleMan1_.setSelectionSet(evaluator1_.evaluate());
107 }
108
109 Vector3d com(centroidX_, centroidY_, solidZ_);
110
111 // now that we have the centroid, we can make cylindrical density maps
112 Vector3d pos;
113 RealType r;
114 RealType z;
115
116 for (sd = seleMan1_.beginSelected(i); sd != NULL;
117 sd = seleMan1_.nextSelected(i)) {
118 pos = sd->getPos() - com;
119
120 // r goes from zero upwards
121 r = std::sqrt(std::pow(pos.x(), 2) + std::pow(pos.y(), 2));
122 // z is possibly symmetric around 0
123 z = pos.z();
124
125 int whichRBin = int(r / dr);
126 int whichZBin = int((zLen / 2.0 + z) / dz);
127
128 if ((whichRBin < int(nRBins_)) && (whichZBin >= 0) &&
129 (whichZBin < int(nZBins_))) {
130 histo[whichRBin][whichZBin] += sd->getMass();
131 }
132 }
133
134 for (unsigned int i = 0; i < histo.size(); ++i) {
135 RealType rL = i * dr;
136 RealType rU = rL + dr;
137 RealType volSlice = Constants::PI * dz * ((rU * rU) - (rL * rL));
138
139 for (unsigned int j = 0; j < histo[i].size(); ++j) {
140 histo[i][j] *= Constants::densityConvert / volSlice;
141 }
142 }
143
144 std::vector<Vector<RealType, 2>> points;
145 points.clear();
146
147 for (unsigned int j = 0; j < nZBins_; ++j) {
148 // The z coordinates were measured relative to the selection
149 // center of mass. However, we're interested in the elevation
150 // above the solid surface. Also, the binning was done around
151 // zero with enough bins to cover the zLength of the box:
152
153 RealType thez = com.z() - solidZ_ - zLen / 2.0 + dz * (j + 0.5);
154 bool aboveThresh = false;
155 bool foundThresh = false;
156 int rloc = 0;
157
158 for (std::size_t i = 0; i < nRBins_; ++i) {
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 if (foundThresh) {
168 Vector<RealType, 2> point;
169 point[0] = dr * (rloc + 0.5);
170 point[1] = thez;
171
172 if (thez > bufferLength_) { 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
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);
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 =
248 std::sqrt(std::abs(center[0] * center[0] +
249 center[1] * center[1] - coeff[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 (std::abs(diff[0]) <= 1e-6 && std::abs(diff[1]) <= 1e-6) { break; }
276 }
277
278 RealType zCen = center[1];
279 RealType rDrop = radius;
280 RealType ca;
281
282 if (std::abs(zCen) > rDrop) {
283 ca = 180.0;
284 } else {
285 ca = 90.0 + std::asin(zCen / rDrop) * (180.0 / Constants::PI);
286 }
287
288 values_.push_back(ca);
289 }
290} // namespace OpenMD
Computes eigenvalues and eigenvectors of a real (non-complex) matrix.
Rectangular matrix class with contiguous flat storage.
Dynamically-sized vector class.
"applications/sequentialProps/SequentialAnalyzer"
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
Definition SimInfo.hpp:96
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
std::string getPrefix(const std::string &str)