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