OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
GofXyz.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/GofXyz.hpp"
46
47#include <algorithm>
48#include <fstream>
49
51#include "types/MultipoleAdapter.hpp"
52#include "utils/simError.h"
53
54namespace OpenMD {
55
56 GofXyz::GofXyz(SimInfo* info, const std::string& filename,
57 const std::string& sele1, const std::string& sele2,
58 const std::string& sele3, RealType len, int nrbins) :
59 RadialDistrFunc(info, filename, sele1, sele2, nrbins),
60 len_(len), halfLen_(len / 2), evaluator3_(info), seleMan3_(info) {
61 setOutputName(getPrefix(filename) + ".gxyz");
62
63 evaluator3_.loadScriptString(sele3);
64 if (!evaluator3_.isDynamic()) {
65 seleMan3_.setSelectionSet(evaluator3_.evaluate());
66 }
67
68 deltaR_ = len_ / nBins_;
69
70 histogram_.resize(nBins_);
71 for (unsigned int i = 0; i < nBins_; ++i) {
72 histogram_[i].resize(nBins_);
73 for (unsigned int j = 0; j < nBins_; ++j) {
74 histogram_[i][j].resize(nBins_);
75 }
76 }
77 }
78
79 void GofXyz::preProcess() {
80 for (unsigned int i = 0; i < nBins_; ++i) {
81 histogram_[i].resize(nBins_);
82 for (unsigned int j = 0; j < nBins_; ++j) {
83 std::fill(histogram_[i][j].begin(), histogram_[i][j].end(), 0);
84 }
85 }
86 }
87
88 void GofXyz::initializeHistogram() {
89 // Calculate the center of mass of the molecule of selected
90 // StuntDouble in selection1
91
92 if (!evaluator3_.isDynamic()) {
93 seleMan3_.setSelectionSet(evaluator3_.evaluate());
94 }
95
96 assert(seleMan1_.getSelectionCount() == seleMan3_.getSelectionCount());
97 bool usePeriodicBoundaryConditions_ =
98 info_->getSimParams()->getUsePeriodicBoundaryConditions();
99
100 // The Dipole direction of selection3 and position of selection3 will
101 // be used to determine the y-z plane
102 // v1 = s3 -s1,
103 // z = origin.dipole
104 // x = v1 X z
105 // y = z X x
106 rotMats_.clear();
107
108 int i;
109 int j;
110 StuntDouble* sd1;
111 StuntDouble* sd3;
112
113 for (sd1 = seleMan1_.beginSelected(i), sd3 = seleMan3_.beginSelected(j);
114 sd1 != NULL || sd3 != NULL;
115 sd1 = seleMan1_.nextSelected(i), sd3 = seleMan3_.nextSelected(j)) {
116 Vector3d r3 = sd3->getPos();
117 Vector3d r1 = sd1->getPos();
118 Vector3d v1 = r3 - r1;
119 if (usePeriodicBoundaryConditions_)
120 info_->getSnapshotManager()->getCurrentSnapshot()->wrapVector(v1);
121
122 AtomType* atype1 = static_cast<Atom*>(sd1)->getAtomType();
123 MultipoleAdapter ma1 = MultipoleAdapter(atype1);
124
125 Vector3d zaxis;
126 if (ma1.isDipole())
127 zaxis = sd1->getDipole();
128 else
129 zaxis = sd1->getA().transpose() * V3Z;
130
131 Vector3d xaxis = cross(v1, zaxis);
132 Vector3d yaxis = cross(zaxis, xaxis);
133
134 xaxis.normalize();
135 yaxis.normalize();
136 zaxis.normalize();
137
138 RotMat3x3d rotMat;
139 rotMat.setRow(0, xaxis);
140 rotMat.setRow(1, yaxis);
141 rotMat.setRow(2, zaxis);
142
143 rotMats_.insert(
144 std::map<int, RotMat3x3d>::value_type(sd1->getGlobalIndex(), rotMat));
145 }
146 }
147
148 void GofXyz::collectHistogram(StuntDouble* sd1, StuntDouble* sd2) {
149 bool usePeriodicBoundaryConditions_ =
150 info_->getSimParams()->getUsePeriodicBoundaryConditions();
151
152 Vector3d pos1 = sd1->getPos();
153 Vector3d pos2 = sd2->getPos();
154 Vector3d r12 = pos2 - pos1;
155 if (usePeriodicBoundaryConditions_) currentSnapshot_->wrapVector(r12);
156
157 std::map<int, RotMat3x3d>::iterator i =
158 rotMats_.find(sd1->getGlobalIndex());
159 assert(i != rotMats_.end());
160
161 Vector3d newR12 = i->second * r12;
162 // x, y and z's possible values range -halfLen_ to halfLen_
163 int xbin = int((newR12.x() + halfLen_) / deltaR_);
164 int ybin = int((newR12.y() + halfLen_) / deltaR_);
165 int zbin = int((newR12.z() + halfLen_) / deltaR_);
166
167 if (xbin < int(nBins_) && xbin >= 0 && ybin < int(nBins_) && ybin >= 0 &&
168 zbin < int(nBins_) && zbin >= 0) {
169 ++histogram_[xbin][ybin][zbin];
170 }
171 }
172
173 void GofXyz::writeRdf() {
174 std::ofstream rdfStream(outputFilename_.c_str(), std::ios::binary);
175 if (rdfStream.is_open()) {
176 // rdfStream << "#g(x, y, z)\n";
177 // rdfStream << "#selection1: (" << selectionScript1_ << ")\t";
178 // rdfStream << "selection2: (" << selectionScript2_ << ")\n";
179 // rdfStream << "#nRBins = " << nBins_ << "\t maxLen = "
180 // << len_ << "deltaR = " << deltaR_ <<"\n";
181 for (unsigned int i = 0; i < histogram_.size(); ++i) {
182 for (unsigned int j = 0; j < histogram_[i].size(); ++j) {
183 for (unsigned int k = 0; k < histogram_[i][j].size(); ++k) {
184 rdfStream.write(reinterpret_cast<char*>(&histogram_[i][j][k]),
185 sizeof(histogram_[i][j][k]));
186 }
187 }
188 }
189
190 } else {
191 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
192 "GofXyz: unable to open %s\n", outputFilename_.c_str());
193 painCave.isFatal = 1;
194 simError();
195 }
196
197 rdfStream.close();
198 }
199
200} // namespace OpenMD
void setRow(unsigned int row, const Vector< Real, Row > &v)
Sets a row of this matrix.
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
Vector3< Real > cross(const Vector3< Real > &v1, const Vector3< Real > &v2)
Returns the cross product of two Vectors.
Definition Vector3.hpp:136
std::string getPrefix(const std::string &str)