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