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