OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
MultiComponentRDF.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 "MultiComponentRDF.hpp"
46
47#include <algorithm>
48
49#include "io/DumpReader.hpp"
51
52namespace OpenMD {
53
54 MultiComponentRDF::MultiComponentRDF(SimInfo* info,
55 const std::string& filename,
56 const std::string& sele1,
57 const std::string& sele2,
58 unsigned int nbins) :
59 StaticAnalyser(info, filename, nbins),
60 selectionScript1_(sele1), selectionScript2_(sele2), evaluator1_(info),
61 evaluator2_(info), seleMan1_(info), seleMan2_(info),
62 sele1_minus_common_(info), sele2_minus_common_(info), common_(info) {
63 nPairs_.resize(MaxPairs);
64
65 evaluator1_.loadScriptString(sele1);
66 evaluator2_.loadScriptString(sele2);
67
68 if (!evaluator1_.isDynamic()) {
69 seleMan1_.setSelectionSet(evaluator1_.evaluate());
70 validateSelection1(seleMan1_);
71 }
72 if (!evaluator2_.isDynamic()) {
73 seleMan2_.setSelectionSet(evaluator2_.evaluate());
74 validateSelection2(seleMan2_);
75 }
76
77 // If all selections are static, we can precompute the number
78 // of real pairs.
79 if (!evaluator1_.isDynamic() && !evaluator2_.isDynamic()) { calcNPairs(); }
80 }
81
82 void MultiComponentRDF::calcNPairs() {
83 common_ = seleMan1_ & seleMan2_;
84 sele1_minus_common_ = seleMan1_ - common_;
85 sele2_minus_common_ = seleMan2_ - common_;
86 nSelected1_ = seleMan1_.getSelectionCount();
87 nSelected2_ = seleMan2_.getSelectionCount();
88 int nIntersect = common_.getSelectionCount();
89
90 nPairs_[OneOne] = nSelected1_ * (nSelected1_ - 1) / 2;
91 nPairs_[OneTwo] =
92 nSelected1_ * nSelected2_ - (nIntersect + 1) * nIntersect / 2;
93 nPairs_[TwoTwo] = nSelected2_ * (nSelected2_ - 1) / 2;
94 }
95
96 void MultiComponentRDF::process() {
97 preProcess();
98
99 DumpReader reader(info_, dumpFilename_);
100 int nFrames = reader.getNFrames();
101 nProcessed_ = nFrames / step_;
102
103 for (int i = 0; i < nFrames; i += step_) {
104 reader.readFrame(i);
105 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
106
107 initializeHistograms();
108
109 if (evaluator1_.isDynamic()) {
110 seleMan1_.setSelectionSet(evaluator1_.evaluate());
111 validateSelection1(seleMan1_);
112 }
113 if (evaluator2_.isDynamic()) {
114 seleMan2_.setSelectionSet(evaluator2_.evaluate());
115 validateSelection2(seleMan2_);
116 }
117
118 // Selections may overlap, and we need a bit of logic to deal
119 // with this.
120 //
121 // | s1 |
122 // | s1 -c | c |
123 // | c | s2 - c |
124 // | s2 |
125 //
126 // s1 : Set of StuntDoubles in selection1
127 // s2 : Set of StuntDoubles in selection2
128 // c : Intersection of selection1 and selection2
129 //
130 // When we loop over the pairs, we can divide the looping into 3
131 // stages:
132 //
133 // Stage 1 : [s1-c] [s2]
134 // Stage 2 : [c] [s2 - c]
135 // Stage 3 : [c] [c]
136 // Stages 1 and 2 are completely non-overlapping.
137 // Stage 3 is completely overlapping.
138
139 if (evaluator1_.isDynamic() || evaluator2_.isDynamic()) { calcNPairs(); }
140
141 processNonOverlapping(sele1_minus_common_, seleMan2_, OneTwo);
142 processNonOverlapping(common_, sele2_minus_common_, OneTwo);
143
144 processOverlapping(common_, OneTwo);
145 processOverlapping(seleMan1_, OneOne);
146 processOverlapping(seleMan2_, TwoTwo);
147
148 processHistograms();
149 }
150
151 postProcess();
152
153 writeRdf();
154 }
155
156 void MultiComponentRDF::processNonOverlapping(SelectionManager& sman1,
157 SelectionManager& sman2,
158 int pairIndex) {
159 StuntDouble* sd1;
160 StuntDouble* sd2;
161 int i;
162 int j;
163
164 // This is the same as a non-overlapping pairwise loop structure:
165 // for (int i = 0; i < ni ; ++i ) {
166 // for (int j = 0; j < nj; ++j) {}
167 // }
168
169 for (sd1 = sman1.beginSelected(i); sd1 != NULL;
170 sd1 = sman1.nextSelected(i)) {
171 for (sd2 = sman2.beginSelected(j); sd2 != NULL;
172 sd2 = sman2.nextSelected(j)) {
173 collectHistograms(sd1, sd2, pairIndex);
174 }
175 }
176 }
177
178 void MultiComponentRDF::processOverlapping(SelectionManager& sman,
179 int pairIndex) {
180 StuntDouble* sd1;
181 StuntDouble* sd2;
182 int i;
183 int j;
184
185 // This is the same as a pairwise loop structure:
186 // for (int i = 0; i < n-1 ; ++i ) {
187 // for (int j = i + 1; j < n; ++j) {}
188 // }
189
190 for (sd1 = sman.beginSelected(i); sd1 != NULL; sd1 = sman.nextSelected(i)) {
191 for (j = i, sd2 = sman.nextSelected(j); sd2 != NULL;
192 sd2 = sman.nextSelected(j)) {
193 collectHistograms(sd1, sd2, pairIndex);
194 }
195 }
196 }
197} // namespace OpenMD
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.