seams-core v2.14.2
libyodaLib, the C++ engine of d-SEAMS
Loading...
Searching...
No Matches
trajectory.hpp
Go to the documentation of this file.
1//-----------------------------------------------------------------------------------
2// d-SEAMS - Deferred Structural Elucidation Analysis for Molecular Simulations
3//
4// Copyright (c) d-SEAMS developers
5// SPDX-License-Identifier: MIT
6//
7// This program is free software: you can redistribute it and/or modify
8// it under the terms of the MIT License as published by
9// the Open Source Initiative.
10//
11// A copy of the MIT License is included in the LICENSE file of this repository.
12// You should have received a copy of the MIT License along with this program.
13// If not, see <https://opensource.org/licenses/MIT>.
14//-----------------------------------------------------------------------------------
15
16#ifndef SEAMS_TRAJECTORY_HPP_
17#define SEAMS_TRAJECTORY_HPP_
18
19#include <mol_sys.hpp>
20
21#include <array>
22#include <cstddef>
23#include <cstdint>
24#include <deque>
25#include <limits>
26#include <vector>
27
32namespace traj {
33
38inline constexpr std::size_t kMaxTriples =
39 static_cast<std::size_t>(std::numeric_limits<std::ptrdiff_t>::max()) /
40 (3 * sizeof(double));
41
46inline constexpr std::size_t kMaxBoxes =
47 static_cast<std::size_t>(std::numeric_limits<std::ptrdiff_t>::max()) /
48 (9 * sizeof(double));
49
54inline constexpr std::size_t kMaxLag =
55 static_cast<std::size_t>(std::numeric_limits<std::ptrdiff_t>::max()) /
56 sizeof(double) -
57 1;
58
62[[nodiscard]] inline bool checkedMul(std::size_t a, std::size_t b,
63 std::size_t &out) noexcept {
64 if (a != 0 && b > std::numeric_limits<std::size_t>::max() / a) {
65 return false;
66 }
67 out = a * b;
68 return true;
69}
70
77struct BoxCell {
78 double ox = 0.0;
79 double oy = 0.0;
80 double oz = 0.0;
81 double lx = 0.0;
82 double ly = 0.0;
83 double lz = 0.0;
84 double xy = 0.0;
85 double xz = 0.0;
86 double yz = 0.0;
87 double halfMin = 0.0;
88 bool triclinic = false;
89 bool ok = false;
90
91 static BoxCell fromBox9(const double box9[9]) noexcept;
93 const molSys::PointCloud<molSys::Point<double>, double> &cloud) noexcept;
94 void toBox9(double out[9]) const noexcept;
95
104 [[nodiscard]] std::array<double, 3> micDelta(double dx, double dy,
105 double dz) const noexcept;
106
111 [[nodiscard]] std::array<double, 3> micDeltaSearch(double dx, double dy,
112 double dz) const noexcept;
113};
114
119public:
120 explicit TrajectoryUnwrapper(std::size_t nParticles = 0);
121
124 void reset(std::size_t nParticles = 0);
125
126 [[nodiscard]] std::size_t numParticles() const noexcept { return nParticles_; }
127 [[nodiscard]] std::size_t framesSeen() const noexcept { return framesSeen_; }
128
129 bool pushFrame(const double *xyz, std::size_t nParticles, const BoxCell &cell,
130 double *unwrappedOut);
131
132private:
133 std::size_t nParticles_ = 0;
134 std::size_t framesSeen_ = 0;
135 std::vector<double> currentUnwrapped_;
136};
137
143bool unwrapPositions(const double *xyzFrames, std::size_t nFrames,
144 std::size_t nAtoms, const double *boxes,
145 std::size_t nBoxes, double *unwrappedOut);
146
153bool moleculeCentres(const double *xyz, std::size_t nAtoms, const BoxCell &cell,
154 const int *molIds, const double *masses,
155 std::vector<int> &uniqueMolIds,
156 std::vector<double> &centresOut);
157
162 const molSys::PointCloud<molSys::Point<double>, double> &cloud,
163 const double *masses, std::vector<int> &uniqueMolIds,
164 std::vector<double> &centresOut);
165
169bool unwrapMoleculeCentres(const double *xyzFrames, std::size_t nFrames,
170 std::size_t nAtoms, const double *boxes,
171 std::size_t nBoxes, const int *molIds,
172 const double *masses,
173 std::vector<int> &uniqueMolIds,
174 std::vector<double> &unwrappedCentresOut);
175
180 std::vector<int> molIds;
181 std::vector<int> oxygenIndices;
182 std::vector<double> ohMidVectors;
183 std::vector<double> bisectorUnit;
184 std::vector<double> hhVectors;
185 std::vector<double> hhUnit;
186};
187
197 const molSys::PointCloud<molSys::Point<double>, double> &cloud,
198 int oxygenType, int hydrogenType, WaterVectorsResult &out,
199 double covalentCutoff = 1.35);
200
205 std::vector<int> molIds;
206 std::vector<double> dipoles;
207 std::vector<double> unitVectors;
208 std::vector<double> magnitudes;
209};
210
219bool moleculeDipoleVectors(const double *xyz, std::size_t nAtoms,
220 const BoxCell &cell, const int *molIds,
221 const double *charges, DipoleVectorsResult &out);
222
224 const molSys::PointCloud<molSys::Point<double>, double> &cloud,
225 const int *molIdsOverride, const double *charges, DipoleVectorsResult &out);
226
231 std::vector<double> cosTheta;
232 std::vector<double> p2;
233 double meanP1 = 0.0;
234 double meanP2 = 0.0;
235 std::size_t nValid = 0;
236};
237
238bool axisOrderParameters(const double *vectors, std::size_t nVectors,
239 const double axis[3], OrientationOrderResult &out);
240
245 double dTotal = 0.0;
246 double dAxial = 0.0;
247 double dLateral = 0.0;
250 double alpha = std::numeric_limits<double>::quiet_NaN();
251 bool alphaFitted = false;
252 double r2Total = 0.0;
253};
254
259 double tauIntegral = 0.0;
260 double tauExp = 0.0;
261 double amplitude = 1.0;
262 double r2 = 0.0;
263};
264
265bool fitDiffusionCurve(const std::vector<double> &lagTime,
266 const std::vector<double> &msdTotal,
267 const std::vector<double> &msdAxial,
268 const std::vector<double> &msdLateral, double tStart,
269 double tEnd, DiffusionFitResult &out);
270
271bool fitCorrelationDecay(const std::vector<double> &lagTime,
272 const std::vector<double> &corr, double tStart,
273 double tEnd, DecayFitResult &out);
274
282public:
283 MsdAccumulator(std::size_t nParticles, std::size_t maxLag,
284 std::size_t originStride, double dt, const double axis[3]);
285
286 [[nodiscard]] bool valid() const noexcept { return valid_; }
287 [[nodiscard]] std::size_t numParticles() const noexcept { return nParticles_; }
288 [[nodiscard]] std::size_t framesSeen() const noexcept { return framesSeen_; }
289 [[nodiscard]] std::size_t numLags() const noexcept;
290
298 bool push(const double *xyz, std::size_t nParticles,
299 const double *box9OrNull = nullptr);
300
301 void curve(std::vector<double> &lagTime, std::vector<double> &total,
302 std::vector<double> &axial, std::vector<double> &lateral,
303 std::vector<std::size_t> &nOrigins) const;
304
305 bool fit(double tStart, double tEnd, DiffusionFitResult &out) const;
306
307private:
308 struct OriginSlot {
309 std::size_t frameIndex = 0;
310 std::vector<double> coords;
311 };
312
313 std::size_t nParticles_ = 0;
314 std::size_t maxLag_ = 0;
315 std::size_t originStride_ = 1;
316 double dt_ = 1.0;
317 std::array<double, 3> axisUnit_{0.0, 0.0, 1.0};
318 bool valid_ = false;
319
320 std::size_t framesSeen_ = 0;
321 bool usedBox_ = false;
322 TrajectoryUnwrapper unwrapper_;
323 std::vector<double> scratchUnwrapped_;
324 std::deque<OriginSlot> activeOrigins_;
325
326 std::vector<double> sumTotal_;
327 std::vector<double> sumAxial_;
328 std::vector<double> sumLateral_;
329 std::vector<std::size_t> originCounts_;
330};
331
336public:
337 HbondLifetimeAccumulator(std::size_t maxLag, std::size_t originStride,
338 double dt, bool directed = true);
339
340 [[nodiscard]] bool valid() const noexcept { return valid_; }
341 [[nodiscard]] std::size_t framesSeen() const noexcept { return framesSeen_; }
342 [[nodiscard]] std::size_t numLags() const noexcept;
343
344 bool push(const int *donors, const int *acceptors, std::size_t nEdges);
345
346 void curve(std::vector<double> &lagTime, std::vector<double> &cIntermittent,
347 std::vector<double> &sContinuous,
348 std::vector<std::size_t> &nOrigins) const;
349
350 bool fit(double tStart, double tEnd, DecayFitResult &intermittentFit,
351 DecayFitResult &continuousFit) const;
352
353private:
354 struct OriginSlot {
355 std::size_t frameIndex = 0;
356 std::vector<std::uint64_t> initialEdges;
357 std::vector<std::uint64_t> survivingEdges;
358 };
359
360 std::size_t maxLag_ = 0;
361 std::size_t originStride_ = 1;
362 double dt_ = 1.0;
363 bool directed_ = true;
364 bool valid_ = false;
365
366 std::size_t framesSeen_ = 0;
367 std::deque<OriginSlot> activeOrigins_;
368
369 std::vector<double> intermittentNum_;
370 std::vector<double> continuousNum_;
371 std::vector<double> denom_;
372 std::vector<std::size_t> originCounts_;
373};
374
380public:
381 ReorientationAccumulator(std::size_t nVectors, std::size_t maxLag,
382 std::size_t originStride, double dt);
383
384 [[nodiscard]] bool valid() const noexcept { return valid_; }
385 [[nodiscard]] std::size_t numVectors() const noexcept { return nVectors_; }
386 [[nodiscard]] std::size_t framesSeen() const noexcept { return framesSeen_; }
387 [[nodiscard]] std::size_t numLags() const noexcept;
388
389 bool push(const double *vectors, std::size_t nVectors);
390
391 void curve(std::vector<double> &lagTime, std::vector<double> &c1,
392 std::vector<double> &c2,
393 std::vector<std::size_t> &nOrigins) const;
394
395 bool fit(double tStart, double tEnd, DecayFitResult &c1Fit,
396 DecayFitResult &c2Fit) const;
397
398private:
399 struct OriginSlot {
400 std::size_t frameIndex = 0;
401 std::vector<double> unitVectors;
402 std::vector<std::uint8_t> mask;
403 };
404
405 std::size_t nVectors_ = 0;
406 std::size_t maxLag_ = 0;
407 std::size_t originStride_ = 1;
408 double dt_ = 1.0;
409 bool valid_ = false;
410
411 std::size_t framesSeen_ = 0;
412 std::deque<OriginSlot> activeOrigins_;
413
414 std::vector<double> sumC1_;
415 std::vector<double> sumC2_;
416 std::vector<std::size_t> pairCounts_;
417 std::vector<std::size_t> originCounts_;
418};
419
420} // namespace traj
421
422#endif // SEAMS_TRAJECTORY_HPP_
bool push(const int *donors, const int *acceptors, std::size_t nEdges)
bool valid() const noexcept
std::size_t numLags() const noexcept
std::size_t framesSeen() const noexcept
bool fit(double tStart, double tEnd, DecayFitResult &intermittentFit, DecayFitResult &continuousFit) const
HbondLifetimeAccumulator(std::size_t maxLag, std::size_t originStride, double dt, bool directed=true)
void curve(std::vector< double > &lagTime, std::vector< double > &cIntermittent, std::vector< double > &sContinuous, std::vector< std::size_t > &nOrigins) const
bool valid() const noexcept
std::size_t numLags() const noexcept
bool push(const double *xyz, std::size_t nParticles, const double *box9OrNull=nullptr)
Push one frame of coordinates.
void curve(std::vector< double > &lagTime, std::vector< double > &total, std::vector< double > &axial, std::vector< double > &lateral, std::vector< std::size_t > &nOrigins) const
MsdAccumulator(std::size_t nParticles, std::size_t maxLag, std::size_t originStride, double dt, const double axis[3])
std::size_t framesSeen() const noexcept
std::size_t numParticles() const noexcept
bool fit(double tStart, double tEnd, DiffusionFitResult &out) const
bool fit(double tStart, double tEnd, DecayFitResult &c1Fit, DecayFitResult &c2Fit) const
bool valid() const noexcept
bool push(const double *vectors, std::size_t nVectors)
std::size_t numVectors() const noexcept
void curve(std::vector< double > &lagTime, std::vector< double > &c1, std::vector< double > &c2, std::vector< std::size_t > &nOrigins) const
std::size_t framesSeen() const noexcept
ReorientationAccumulator(std::size_t nVectors, std::size_t maxLag, std::size_t originStride, double dt)
std::size_t numLags() const noexcept
std::size_t framesSeen() const noexcept
std::size_t numParticles() const noexcept
void reset(std::size_t nParticles=0)
A count above kMaxTriples leaves the unwrapper empty, and pushFrame refuses such a count.
bool pushFrame(const double *xyz, std::size_t nParticles, const BoxCell &cell, double *unwrappedOut)
TrajectoryUnwrapper(std::size_t nParticles=0)
The main molecular system handler.
Multi-frame PBC unwrapping, molecular orientation/dipole vectors, and streaming time-correlation accu...
bool moleculeDipoleVectors(const double *xyz, std::size_t nAtoms, const BoxCell &cell, const int *molIds, const double *charges, DipoleVectorsResult &out)
Compute per-molecule dipole or geometric orientation vectors.
bool checkedMul(std::size_t a, std::size_t b, std::size_t &out) noexcept
out = a * b; false, leaving out alone, when that overflows size_t.
bool waterOrientationVectors(const molSys::PointCloud< molSys::Point< double >, double > &cloud, int oxygenType, int hydrogenType, WaterVectorsResult &out, double covalentCutoff=1.35)
Extract per-water O->midpoint(H1,H2) and H1->H2 vectors under MIC.
bool unwrapMoleculeCentres(const double *xyzFrames, std::size_t nFrames, std::size_t nAtoms, const double *boxes, std::size_t nBoxes, const int *molIds, const double *masses, std::vector< int > &uniqueMolIds, std::vector< double > &unwrappedCentresOut)
Compute and unwrap molecule centres across nFrames frames.
constexpr std::size_t kMaxLag
Largest max_lag whose max_lag + 1 lag bins one std::vector can hold.
constexpr std::size_t kMaxBoxes
Largest box count n whose 9 * n doubles fit in a single array.
bool moleculeCentres(const double *xyz, std::size_t nAtoms, const BoxCell &cell, const int *molIds, const double *masses, std::vector< int > &uniqueMolIds, std::vector< double > &centresOut)
Compute intact PBC-assembled per-molecule centres for one frame.
bool axisOrderParameters(const double *vectors, std::size_t nVectors, const double axis[3], OrientationOrderResult &out)
bool fitDiffusionCurve(const std::vector< double > &lagTime, const std::vector< double > &msdTotal, const std::vector< double > &msdAxial, const std::vector< double > &msdLateral, double tStart, double tEnd, DiffusionFitResult &out)
bool fitCorrelationDecay(const std::vector< double > &lagTime, const std::vector< double > &corr, double tStart, double tEnd, DecayFitResult &out)
bool unwrapPositions(const double *xyzFrames, std::size_t nFrames, std::size_t nAtoms, const double *boxes, std::size_t nBoxes, double *unwrappedOut)
Unwrap a multi-frame coordinate array [nFrames * 3 * nAtoms].
constexpr std::size_t kMaxTriples
Largest particle or vector count n whose 3 * n doubles one std::vector can hold.
This contains a collection of points; contains information for a particular frame.
Definition mol_sys.hpp:173
This contains per-particle information.
Definition mol_sys.hpp:152
Restricted-triclinic periodic simulation cell.
std::array< double, 3 > micDelta(double dx, double dy, double dz) const noexcept
Euclidean minimum-image Cartesian displacement for raw delta (dx, dy, dz).
static BoxCell fromCloud(const molSys::PointCloud< molSys::Point< double >, double > &cloud) noexcept
static BoxCell fromBox9(const double box9[9]) noexcept
std::array< double, 3 > micDeltaSearch(double dx, double dy, double dz) const noexcept
Euclidean minimum image by exhaustive search over the lattice shifts that can beat the fractional ima...
void toBox9(double out[9]) const noexcept
Integrated and single-exponential relaxation time summary.
Linear Einstein fit of total, 1D axial, and 2D lateral MSD.
double alpha
NaN unless alphaFitted: the window needs two lags with t > 0 and MSD_total > 1e-20 for the log-log fi...
Generic per-molecule dipole / orientation vectors.
std::vector< int > molIds
std::vector< double > magnitudes
std::vector< double > dipoles
std::vector< double > unitVectors
Axis-projected orientational order parameters P1 and P2.
std::vector< double > cosTheta
std::vector< double > p2
Per-water orientation vectors (O->H midpoint and H->H).
std::vector< double > bisectorUnit
std::vector< int > oxygenIndices
std::vector< double > hhUnit
std::vector< int > molIds
std::vector< double > hhVectors
std::vector< double > ohMidVectors