OpenMD 3.0
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
Kirkwood.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 appropriate papers when you publish your
33 * work. Good starting points are:
34 *
35 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
36 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
37 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
38 * [4] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
40 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
41 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
42 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
43 */
44
45#include "applications/staticProps/Kirkwood.hpp"
46
47#include <algorithm>
48#include <fstream>
49#include <sstream>
50
51#include "types/MultipoleAdapter.hpp"
52#include "utils/Revision.hpp"
53#include "utils/simError.h"
54
55namespace OpenMD {
56
57 Kirkwood::Kirkwood(SimInfo* info, const std::string& filename,
58 const std::string& sele1, const std::string& sele2,
59 RealType len, int nrbins) :
60 RadialDistrFunc(info, filename, sele1, sele2, nrbins),
61 len_(len) {
62 setAnalysisType("Distance-dependent Kirkwood G-factor");
63 setOutputName(getPrefix(filename) + ".kirkwood");
64
65 deltaR_ = len_ / nBins_;
66
67 histogram_.resize(nBins_);
68 avgKirkwood_.resize(nBins_);
69 std::stringstream params;
70 params << " len = " << len_ << ", nrbins = " << nBins_;
71 const std::string paramString = params.str();
72 setParameterString(paramString);
73 }
74
75 void Kirkwood::preProcess() {
76 std::fill(avgKirkwood_.begin(), avgKirkwood_.end(), 0.0);
77 }
78
79 void Kirkwood::initializeHistogram() {
80 std::fill(histogram_.begin(), histogram_.end(), 0);
81 }
82
83 void Kirkwood::processHistogram() {
84 int nSelected1 = seleMan1_.getSelectionCount();
85 for (unsigned int i = 0; i < histogram_.size(); ++i) {
86 avgKirkwood_[i] += histogram_[i] / nSelected1;
87 }
88 }
89
90 void Kirkwood::collectHistogram(StuntDouble* sd1, StuntDouble* sd2) {
91 if (sd1 == sd2) { return; }
92 bool usePeriodicBoundaryConditions_ =
93 info_->getSimParams()->getUsePeriodicBoundaryConditions();
94
95 Vector3d pos1 = sd1->getPos();
96 Vector3d pos2 = sd2->getPos();
97 Vector3d r12 = pos2 - pos1;
98 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(r12);
99
100 RealType distance = r12.length();
101
102 AtomType* atype1 = static_cast<Atom*>(sd1)->getAtomType();
103 AtomType* atype2 = static_cast<Atom*>(sd2)->getAtomType();
104
105 MultipoleAdapter ma1 = MultipoleAdapter(atype1);
106 MultipoleAdapter ma2 = MultipoleAdapter(atype2);
107
108 Vector3d d1(0.0);
109 Vector3d d2(0.0);
110 RealType dotProduct(0.0);
111
112 if (ma1.isDipole()) {
113 d1 = sd1->getDipole();
114 d1.normalize();
115 if (ma2.isDipole()) {
116 d2 = sd2->getDipole();
117 d2.normalize();
118 dotProduct = dot(d1, d2);
119 }
120 }
121
122 if (distance < len_) {
123 int whichBin = int(distance / deltaR_);
124 // each dipole pair contributes to all of the radii that contain it.
125 for (unsigned int i = whichBin; i < nBins_; i++) {
126 histogram_[i] += dotProduct;
127 }
128 }
129 }
130
131 void Kirkwood::writeRdf() {
132 std::ofstream ofs(outputFilename_.c_str());
133 if (ofs.is_open()) {
134 Revision r;
135 ofs << "# " << getAnalysisType() << "\n";
136 ofs << "# OpenMD " << r.getFullRevision() << "\n";
137 ofs << "# " << r.getBuildDate() << "\n";
138 ofs << "# selection script1: \"" << selectionScript1_;
139 ofs << "\"\tselection script2: \"" << selectionScript2_ << "\"\n";
140 if (!paramString_.empty())
141 ofs << "# parameters: " << paramString_ << "\n";
142
143 ofs << "#r\tcorrValue\n";
144 for (unsigned int i = 0; i < avgKirkwood_.size(); ++i) {
145 RealType r = deltaR_ * (i + 0.5);
146 ofs << r << "\t" << avgKirkwood_[i] / nProcessed_ << "\n";
147 }
148 } else {
149 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
150 "Kirkwood: unable to open %s\n", outputFilename_.c_str());
151 painCave.isFatal = 1;
152 simError();
153 }
154 ofs.close();
155 }
156
157 KirkwoodQuadrupoles::KirkwoodQuadrupoles(SimInfo* info,
158 const std::string& filename,
159 const std::string& sele1,
160 const std::string& sele2,
161 RealType len, int nrbins) :
162 Kirkwood(info, filename, sele1, sele2, len, nrbins) {
163 setAnalysisType("Distance-dependent Kirkwood G-factor for quadrupoles");
164 setOutputName(getPrefix(filename) + ".kirkwoodQ");
165 }
166
167 void KirkwoodQuadrupoles::collectHistogram(StuntDouble* sd1,
168 StuntDouble* sd2) {
169 if (sd1 == sd2) { return; }
170 bool usePeriodicBoundaryConditions_ =
171 info_->getSimParams()->getUsePeriodicBoundaryConditions();
172
173 Vector3d pos1 = sd1->getPos();
174 Vector3d pos2 = sd2->getPos();
175 Vector3d r12 = pos2 - pos1;
176 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(r12);
177
178 RealType distance = r12.length();
179
180 AtomType* atype1 = static_cast<Atom*>(sd1)->getAtomType();
181 AtomType* atype2 = static_cast<Atom*>(sd2)->getAtomType();
182
183 MultipoleAdapter ma1 = MultipoleAdapter(atype1);
184 MultipoleAdapter ma2 = MultipoleAdapter(atype2);
185
186 Mat3x3d Q1(0.0);
187 Mat3x3d Q2(0.0);
188 RealType trQ1(0.0);
189 RealType trQ2(0.0);
190 RealType Q1dQ1(0.0);
191 RealType Q2dQ2(0.0);
192
193 RealType quadrupoleProduct(0.0);
194
195 // Similar to the dipole case, but the effective quadrupole moment
196 // is defined (in our electrostatics work) as:
197 // sqrt (3 Q:Q - Tr(Q)^2 )
198 // so normalization is a bit different. Here : denotes a
199 // contraction (double dot product) of the quadrupole tensor.
200
201 if (ma1.isQuadrupole()) {
202 Q1 = sd1->getQuadrupole();
203 trQ1 = Q1.trace();
204 Q1dQ1 = doubleDot(Q1, Q1);
205 Q1 /= sqrt(3.0 * Q1dQ1 - trQ1 * trQ1);
206 // recompute the trace after normalizing:
207 trQ1 = Q1.trace();
208
209 if (ma2.isQuadrupole()) {
210 Q2 = sd2->getQuadrupole();
211 trQ2 = Q2.trace();
212 Q2dQ2 = doubleDot(Q2, Q2);
213 Q2 /= sqrt(3.0 * Q2dQ2 - trQ2 * trQ2);
214 // recompute the trace after normalizing:
215 trQ2 = Q2.trace();
216
217 quadrupoleProduct = 3.0 * doubleDot(Q1, Q2) - trQ1 * trQ2;
218 }
219 }
220
221 if (distance < len_) {
222 int whichBin = int(distance / deltaR_);
223 // each dipole pair contributes to all of the radii that contain it.
224 for (unsigned int i = whichBin; i < nBins_; i++) {
225 histogram_[i] += quadrupoleProduct;
226 }
227 }
228 }
229} // namespace OpenMD
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
Real doubleDot(const RectMatrix< Real, Row, Col > &t1, const RectMatrix< Real, Row, Col > &t2)
Returns the tensor contraction (double dot product) of two rank 2 tensors (or Matrices)
Real dot(const DynamicVector< Real > &v1, const DynamicVector< Real > &v2)
Returns the dot product of two DynamicVectors.
std::string getPrefix(const std::string &str)
Real distance(const DynamicVector< Real > &v1, const DynamicVector< Real > &v2)
Returns the distance between two DynamicVectors.