OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
GCNSeq.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/GCNSeq.hpp"
49
50#include <algorithm>
51#include <fstream>
52#include <sstream>
53
54#include "io/DumpReader.hpp"
55#include "utils/Revision.hpp"
56#include "utils/simError.h"
57
58namespace OpenMD {
59
60 GCNSeq::GCNSeq(SimInfo* info, const std::string& filename,
61 const std::string& sele1, const std::string& sele2,
62 RealType rCut, int bins) :
63 SequentialAnalyzer(info, filename, sele1, sele2),
64 rCut_(rCut), bins_(bins) {
65 setSequenceType("Generalized Coordination Number Distribution");
66 setOutputName(getPrefix(filename) + ".gcnSeq");
67
68 nnMax_ = 12;
69 RealType binMax_ = nnMax_ * 1.5;
70 delta_ = binMax_ / bins_;
71 usePBC_ = info->getSimParams()->getUsePeriodicBoundaryConditions();
72
73 std::stringstream params;
74 params << " rcut = " << rCut_ << ", nbins = " << bins_
75 << ", max neighbors = " << nnMax_;
76 const std::string paramString = params.str();
77 setParameterString(paramString);
78 }
79
80 void GCNSeq::doFrame(int istep) {
81 SelectionManager common(info_);
82
83 std::vector<std::vector<int>> listNN;
84 std::vector<int> globalToLocal;
85
86 StuntDouble* sd1;
87 StuntDouble* sd2;
88
89 int iterator1;
90 int iterator2;
91 unsigned int mapIndex1(0);
92 unsigned int mapIndex2(0);
93 unsigned int tempIndex(0);
94 unsigned int whichBin(0);
95 RealType gcn(0.0);
96 Vector3d pos1;
97 Vector3d pos2;
98 Vector3d diff;
99 RealType distance;
100
101 // First have to calculate lists of nearest neighbors (listNN_):
102
103 selectionCount1_ = seleMan1_.getSelectionCount();
104 selectionCount2_ = seleMan2_.getSelectionCount();
105
106 // We need a common selection set:
107 common = seleMan1_ | seleMan2_;
108 int commonCount = common.getSelectionCount();
109
110 globalToLocal.clear();
111 globalToLocal.resize(
112 info_->getNGlobalAtoms() + info_->getNGlobalRigidBodies(), -1);
113 for (unsigned int i = 0; i < listNN.size(); i++)
114 listNN.at(i).clear();
115 listNN.clear();
116 listNN.resize(commonCount);
117 std::vector<RealType> histo;
118 histo.resize(bins_, 0.0);
119
120 mapIndex1 = 0;
121 for (sd1 = common.beginSelected(iterator1); sd1 != NULL;
122 sd1 = common.nextSelected(iterator1)) {
123 globalToLocal.at(sd1->getGlobalIndex()) = mapIndex1;
124
125 pos1 = sd1->getPos();
126
127 mapIndex2 = 0;
128 for (sd2 = common.beginSelected(iterator2); sd2 != NULL;
129 sd2 = common.nextSelected(iterator2)) {
130 if (mapIndex1 < mapIndex2) {
131 pos2 = sd2->getPos();
132 diff = pos2 - pos1;
133 if (usePBC_) currentSnapshot_->wrapVector(diff);
134 distance = diff.length();
135 if (distance < rCut_) {
136 listNN.at(mapIndex1).push_back(mapIndex2);
137 listNN.at(mapIndex2).push_back(mapIndex1);
138 }
139 }
140 mapIndex2++;
141 }
142 mapIndex1++;
143 }
144
145 // Fill up the histogram with gcn values
146 for (sd1 = seleMan1_.beginSelected(iterator1); sd1 != NULL;
147 sd1 = seleMan1_.nextSelected(iterator1)) {
148 mapIndex1 = globalToLocal.at(sd1->getGlobalIndex());
149 gcn = 0.0;
150 for (unsigned int i = 0; i < listNN.at(mapIndex1).size(); i++) {
151 // tempIndex is the index of one of i's nearest neighbors
152 tempIndex = listNN.at(mapIndex1).at(i);
153 gcn += listNN.at(tempIndex).size();
154 }
155
156 gcn = gcn / nnMax_;
157 whichBin = int(gcn / delta_);
158 if (whichBin < histo.size()) {
159 histo[whichBin] += 1;
160 } else {
161 cerr << "In frame " << istep << ", object " << sd1->getGlobalIndex()
162 << " has GCN value = " << gcn << "\n";
163 }
164 }
165
166 for (unsigned int n = 0; n < histo.size(); n++) {
167 if (selectionCount1_ > 0)
168 histo[n] /= RealType(selectionCount1_);
169 else
170 histo[n] = 0.0;
171 }
172
173 count_.push_back(selectionCount1_);
174 histogram_.push_back(histo);
175 }
176
177 void GCNSeq::writeSequence() {
178 std::ofstream ofs(outputFilename_.c_str(), std::ios::binary);
179
180 if (ofs.is_open()) {
181 Revision r;
182 RealType binValue(0.0);
183
184 ofs << "# " << getSequenceType() << "\n";
185 ofs << "# OpenMD " << r.getFullRevision() << "\n";
186 ofs << "# " << r.getBuildDate() << "\n";
187 ofs << "# selection script1: \"" << selectionScript1_;
188 ofs << "\"\tselection script2: \"" << selectionScript2_ << "\"\n";
189 if (!paramString_.empty())
190 ofs << "# parameters: " << paramString_ << "\n";
191
192 ofs << "#time\tvalue\n";
193
194 for (unsigned int i = 0; i < times_.size(); ++i) {
195 ofs << "#Frame " << i << "\n";
196 ofs << "#Selection 1 Count: " << count_[i] << "\n";
197
198 for (unsigned int n = 0; n < histogram_[i].size(); n++) {
199 binValue = n * delta_;
200 ofs << binValue << "\t" << histogram_[i][n] << "\n";
201 }
202 }
203 } else {
204 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
205 "GCN::writeSequence Error: failed to open %s\n",
206 outputFilename_.c_str());
207 painCave.isFatal = 1;
208 simError();
209 }
210
211 ofs.close();
212 }
213} // namespace OpenMD
"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)
Real distance(const DynamicVector< Real > &v1, const DynamicVector< Real > &v2)
Returns the distance between two DynamicVectors.