OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
TimeCorrFunc.hpp
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#ifndef APPLICATIONS_DYNAMICPROPS_TIMECORRFUNC_HPP
49#define APPLICATIONS_DYNAMICPROPS_TIMECORRFUNC_HPP
50
51#include <string>
52#include <vector>
53
54#include "applications/dynamicProps/DynamicProperty.hpp"
55#include "brains/SimInfo.hpp"
56#include "io/DumpReader.hpp"
58#include "selection/SelectionEvaluator.hpp"
59#include "selection/SelectionManager.hpp"
60#include "utils/ProgressBar.hpp"
61
62namespace OpenMD {
63
64 //! Computes a correlation function by scanning a trajectory once to
65 //! precompute quantities to be correlated
66
67 // There are two modes for carrying out time correleation
68 // functions. The default mode allows overlapping origins of the
69 // sampling windows.
70 //
71 // origin 0: frames 0,1,2,...,nFrames-1
72 // origin 1: frames 1,2,3,...,nFrames-1
73 // origin 2: frames 2,3,4,...,nFrames-1
74 // ...
75 // This gives many samples for short lag times and fewer for long
76 // lag times, and we normalize using count_[timeBin]
77 //
78 // The second approach uses non-overlapping windows:
79 // |--ncor--|--nsep--|--ncor--|--nsep--|--ncor--|
80 // avg1 avg2 avg3
81 // We accomplish this behavior by setting:
82 //
83 // nStart_ = frames to skip at beginning (for equilibration)
84 // ncor = frames per window (nTimeBins_)
85 // nStride_ = frames between window starts (ncor + nsep)
86 //
87 // This means that the sampling windows look like:
88 // origin 0: frames [nStart_, nStart_ + ncor)
89 // origin 1: frames [nStart_ + nStride_, nStart_ + nStride_ + ncor)
90 // origin 2: frames [nStart_ + 2*nStride_, nStart_ + 2*nStride_ + ncor)
91 //
92 // nStride_ is the key here:
93 // nStride_ = 1 = all overlapping origins
94 // nStride_ = nTimeBins_ = tight non-overlapping windows (nsep=0)
95 // nStride_ > nTimeBins_ = sampling windows with a decorrelation gap
96 // 1 < nStride_ < nTimeBins_ = overlapping fixed-length windows
97 // (pass tsep_fs < 0 to setWindowingParameters;
98 // |tsep_fs| is the overlap duration in fs)
99
100 template<typename T>
101 class TimeCorrFunc : public DynamicProperty {
102 public:
103 TimeCorrFunc(SimInfo* info, const std::string& filename,
104 const std::string& sele1, const std::string& sele2);
105
106 virtual ~TimeCorrFunc() { delete reader_; }
107
108 // Setters — call before doCorrelate()
109 void setWindowingParameters(RealType tcorr_fs, int nStart, RealType tsep_fs);
110
111 virtual void doCorrelate();
112
113 const std::string& getCorrFuncType() const { return corrFuncType_; }
114
115 void setCorrFuncType(const std::string& type) { corrFuncType_ = type; }
116
117 void setParameterString(const std::string& params) {
118 paramString_ = params;
119 }
120
121 void setLabelString(const std::string& label) { labelString_ = label; }
122
123 protected:
124 virtual void preCorrelate();
125 virtual void correlation();
126 virtual void postCorrelate();
127 virtual void computeFrame(int frame);
128 virtual void validateSelection(SelectionManager& seleMan);
129 virtual void correlateFrames(int frame1, int frame2, int timeBin);
130 virtual void writeCorrelate();
131
132 // The pure virtual functions that must be implemented.
133
134 // For System Properties:
135 virtual void computeProperty1(int frame) = 0;
136 virtual void computeProperty2(int frame) = 0;
137 virtual T calcCorrVal(int frame1, int frame2) = 0;
138 // For Molecular Properties
139 virtual int computeProperty1(int frame, Molecule* mol) = 0;
140 virtual int computeProperty2(int frame, Molecule* mol) = 0;
141 // For Object
142 virtual int computeProperty1(int frame, StuntDouble* sd) = 0;
143 virtual int computeProperty2(int frame, StuntDouble* sd) = 0;
144 // For Bond properties
145 virtual int computeProperty1(int frame, Bond* b) = 0;
146 virtual int computeProperty2(int frame, Bond* b) = 0;
147 // For everything except System properties:
148 virtual T calcCorrVal(int frame1, int frame2, int id1, int id2) = 0;
149
150 RealType deltaTime_;
151 unsigned int nTimeBins_;
152 int nFrames_;
153 std::vector<T> histogram_;
154 std::vector<int> count_;
155 std::vector<RealType> times_;
156 RealType dtMean_ {};
157 RealType dtSigma_ {};
158
159 SimInfo* info_ {nullptr};
160 DumpReader* reader_;
161 std::string dumpFilename_;
162 SelectionManager seleMan1_;
163 SelectionManager seleMan2_;
164
165 Snapshot* currentSnapshot_;
166
167 std::string selectionScript1_;
168 std::string selectionScript2_;
169
170 SelectionEvaluator evaluator1_;
171 SelectionEvaluator evaluator2_;
172
173 bool uniqueSelections_;
174 bool autoCorrFunc_;
175 bool doSystemProperties_;
176 bool doMolecularProperties_;
177 bool doObjectProperties_;
178 bool doAtomicProperties_;
179 bool doBondProperties_;
180 bool allowTimeFuzz_;
181
182 std::string corrFuncType_;
183 std::string paramString_;
184 std::string labelString_;
185
186 std::vector<std::vector<int>> sele1ToIndex_;
187 std::vector<std::vector<int>> sele2ToIndex_;
188 std::vector<std::vector<int>> GIDtoSele1_;
189 std::vector<std::vector<int>> GIDtoSele2_;
190 std::vector<std::vector<int>> selection1StartFrame_;
191 std::vector<std::vector<int>> selection2StartFrame_;
192
193 ProgressBarPtr progressBar_;
194 // --- Windowing parameters -------------------------------------------
195 //
196 // nStart_ : number of frames to skip at the start of the trajectory
197 // (equilibration period). Default 0.
198 //
199 // nStride_ : number of frames between the start of successive
200 // correlation windows.
201 //
202 // nStride_ = 1 → original overlapping-origin behavior
203 // (all possible time origins used)
204 //
205 // nStride_ = nTimeBins_ → non-overlapping windows, no gap
206 // (MultiSpec ncor mode)
207 //
208 // nStride_ = nTimeBins_ + nSep_
209 // → non-overlapping windows with a gap
210 // between them (MultiSpec ncor+nsep mode)
211 //
212 // nSep_ : gap in frames between end of one window and start of
213 // the next. Only meaningful when nStride_ > nTimeBins_.
214 // Default 0.
215 //
216 // nTimeBins_ already controls the correlation window length (ncor).
217 // It is set from simParams->getSampleTime() and nFrames_ in the
218 // existing constructor, but can be overridden by setSampleSize().
219 //
220 // navg_ : number of windows that fit in the trajectory given
221 // nStart_, nStride_, nTimeBins_, nFrames_.
222 // Computed in preCorrelate(), read-only after that.
223 //
224 // useWindowing_ : true if nStride_ > 1 or nStart_ > 0.
225 // Controls which correlation() path is taken.
226 // --------------------------------------------------------------------
227
228 int nStart_ {0}; // frames to skip at trajectory start
229 int nSep_ {0}; // signed offset: >0 gap, <0 overlap (frames)
230 int nStride_ {1}; // frames between window origins
231 int navg_ {0}; // number of windows (computed)
232 bool useWindowing_ {false};
233
234 };
235
236 template<typename T>
237 class AutoCorrFunc : public TimeCorrFunc<T> {
238 public:
239 AutoCorrFunc(SimInfo* info, const std::string& filename,
240 const std::string& sele1, const std::string& sele2);
241
242 protected:
243 virtual void computeProperty1(int frame) = 0;
244 virtual int computeProperty1(int frame, Molecule* mol) = 0;
245 virtual int computeProperty1(int frame, StuntDouble* sd) = 0;
246 virtual int computeProperty1(int frame, Bond* bond) = 0;
247
248 virtual void computeProperty2(int) {}
249 virtual int computeProperty2(int, Molecule*) { return -1; }
250 virtual int computeProperty2(int, StuntDouble*) { return -1; }
251 virtual int computeProperty2(int, Bond*) { return -1; }
252 };
253
254 template<typename T>
255 class CrossCorrFunc : public TimeCorrFunc<T> {
256 public:
257 CrossCorrFunc(SimInfo* info, const std::string& filename,
258 const std::string& sele1, const std::string& sele2);
259
260 protected:
261 virtual void computeProperty1(int frame) = 0;
262 virtual int computeProperty1(int frame, Molecule* mol) = 0;
263 virtual int computeProperty1(int frame, StuntDouble* sd) = 0;
264 virtual int computeProperty1(int frame, Bond* bond) = 0;
265 virtual void computeProperty2(int frame) = 0;
266 virtual int computeProperty2(int frame, Molecule* mol) = 0;
267 virtual int computeProperty2(int frame, StuntDouble* sd) = 0;
268 virtual int computeProperty2(int frame, Bond* bond) = 0;
269 };
270
271 template<typename T>
272 class SystemACF : public AutoCorrFunc<T> {
273 public:
274 SystemACF(SimInfo* info, const std::string& filename,
275 const std::string& sele1, const std::string& sele2);
276
277 protected:
278 virtual void computeProperty1(int frame) = 0;
279 virtual T calcCorrVal(int frame1, int frame2) = 0;
280
281 virtual int computeProperty1(int, Molecule*) { return -1; }
282 virtual int computeProperty1(int, StuntDouble*) { return -1; }
283 virtual int computeProperty1(int, Bond*) { return -1; }
284
285 virtual void computeProperty2(int) {}
286 virtual int computeProperty2(int, Molecule*) { return -1; }
287 virtual int computeProperty2(int, StuntDouble*) { return -1; }
288 virtual int computeProperty2(int, Bond*) { return -1; }
289
290 T calcCorrVal(int, int, int, int) { return T(0.0); }
291 };
292
293 template<typename T>
294 class SystemCCF : public CrossCorrFunc<T> {
295 public:
296 SystemCCF(SimInfo* info, const std::string& filename,
297 const std::string& sele1, const std::string& sele2);
298
299 protected:
300 virtual void computeProperty1(int frame) = 0;
301 virtual void computeProperty2(int frame) = 0;
302 virtual T calcCorrVal(int frame1, int frame2) = 0;
303
304 virtual int computeProperty1(int, Molecule*) { return -1; }
305 virtual int computeProperty1(int, StuntDouble*) { return -1; }
306 virtual int computeProperty1(int, Bond*) { return -1; }
307
308 virtual int computeProperty2(int, Molecule*) { return -1; }
309 virtual int computeProperty2(int, StuntDouble*) { return -1; }
310 virtual int computeProperty2(int, Bond*) { return -1; }
311
312 T calcCorrVal(int, int, int, int) { return T(0.0); }
313 };
314
315 template<typename T>
316 class ObjectACF : public AutoCorrFunc<T> {
317 public:
318 ObjectACF(SimInfo* info, const std::string& filename,
319 const std::string& sele1, const std::string& sele2);
320
321 protected:
322 virtual int computeProperty1(int frame, StuntDouble* sd) = 0;
323 virtual T calcCorrVal(int frame1, int frame2, int id1, int id2) = 0;
324
325 virtual void computeProperty1(int) { return; }
326 virtual int computeProperty1(int, Molecule*) { return -1; }
327 virtual int computeProperty1(int, Bond*) { return -1; }
328
329 virtual void computeProperty2(int) { return; }
330 virtual int computeProperty2(int, Molecule*) { return -1; }
331 virtual int computeProperty2(int, StuntDouble*) { return -1; }
332 virtual int computeProperty2(int, Bond*) { return -1; }
333
334 virtual T calcCorrVal(int, int) { return T(0.0); }
335 };
336
337 template<typename T>
338 class ObjectCCF : public CrossCorrFunc<T> {
339 public:
340 ObjectCCF(SimInfo* info, const std::string& filename,
341 const std::string& sele1, const std::string& sele2);
342
343 protected:
344 virtual int computeProperty1(int frame, StuntDouble* sd) = 0;
345 virtual int computeProperty2(int frame, StuntDouble* sd) = 0;
346 virtual T calcCorrVal(int frame1, int frame2, int id1, int id2) = 0;
347
348 virtual void computeProperty1(int) { return; }
349 virtual int computeProperty1(int, Molecule*) { return -1; }
350 virtual int computeProperty1(int, Bond*) { return -1; }
351
352 virtual void computeProperty2(int) {}
353 virtual int computeProperty2(int, Molecule*) { return -1; }
354 virtual int computeProperty2(int, Bond*) { return -1; }
355
356 virtual T calcCorrVal(int, int) { return T(0.0); }
357 };
358
359 template<typename T>
360 class MoleculeACF : public AutoCorrFunc<T> {
361 public:
362 MoleculeACF(SimInfo* info, const std::string& filename,
363 const std::string& sele1, const std::string& sele2);
364
365 protected:
366 virtual int computeProperty1(int frame, Molecule* mol) = 0;
367 virtual T calcCorrVal(int frame1, int frame2, int id1, int id2) = 0;
368
369 virtual void computeProperty1(int) { return; }
370 virtual int computeProperty1(int, StuntDouble*) { return -1; }
371 virtual int computeProperty1(int, Bond*) { return -1; }
372
373 virtual void computeProperty2(int) { return; }
374 virtual int computeProperty2(int, Molecule*) { return -1; }
375 virtual int computeProperty2(int, StuntDouble*) { return -1; }
376 virtual int computeProperty2(int, Bond*) { return -1; }
377
378 virtual T calcCorrVal(int, int) { return T(0.0); }
379 };
380
381 template<typename T>
382 class MoleculeCCF : public CrossCorrFunc<T> {
383 public:
384 MoleculeCCF(SimInfo* info, const std::string& filename,
385 const std::string& sele1, const std::string& sele2);
386
387 protected:
388 virtual int computeProperty1(int frame, Molecule* mol) = 0;
389 virtual int computeProperty2(int frame, Molecule* mol) = 0;
390 virtual T calcCorrVal(int frame1, int frame2, int id1, int id2) = 0;
391
392 virtual void computeProperty1(int) { return; }
393 virtual int computeProperty1(int, StuntDouble*) { return -1; }
394 virtual int computeProperty1(int, Bond*) { return -1; }
395
396 virtual void computeProperty2(int) {}
397 virtual int computeProperty2(int, StuntDouble*) { return -1; }
398 virtual int computeProperty2(int, Bond*) { return -1; }
399
400 virtual T calcCorrVal(int, int) { return T(0.0); }
401 };
402} // namespace OpenMD
403
404#endif
"applications/dynamicProps/DynamicProperty"
Computes a correlation function by scanning a trajectory once to precompute quantities to be correlat...
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.