Correlation 4.10.0
An Analysis Tool for Liquids and for Amorphous Solids
Loading...
Searching...
No Matches
PerFrameDistributionStore.hpp
Go to the documentation of this file.
1// Correlation - Liquid and Amorphous Solid Analysis Tool
2// Copyright © 2013-2026 Isaías Rodríguez (isurwars@gmail.com)
3// SPDX-License-Identifier: AGPL-3.0-only
4// Full license: https://github.com/Isurwars/Correlation/blob/main/LICENSE
5
6#pragma once
7
8#include "math/Precision.hpp"
9
10#include <array>
11#include <cstddef>
12#include <cstdint>
13#include <functional>
14#include <mutex>
15#include <shared_mutex>
16#include <string>
17#include <vector>
18
19namespace correlation::analysis {
20
24enum class DistributionMetric : uint8_t {
25 RDF = 0,
26 PAD = 1,
27 SQ = 2,
28 XRD = 3
29};
30
35 std::vector<real_t> bins;
36 std::vector<real_t> mean_curve;
37 std::vector<std::vector<real_t>> frame_curves;
38 std::string name;
39 std::string title;
40 std::string x_unit;
41 std::string y_unit;
42};
43
51public:
52 static constexpr std::size_t METRIC_COUNT = 4;
53 using FrameCurveArray = std::array<std::vector<real_t>, METRIC_COUNT>;
54 using FrameCurveGenerator = std::function<FrameCurveArray(std::size_t)>;
55
57 std::size_t start_frame{0};
58 std::size_t sample_count{0};
59 std::size_t stride{1};
60 };
61
66 explicit PerFrameDistributionStore(std::size_t total_frames);
67
73 void setBins(DistributionMetric metric, std::vector<real_t> bins);
74
80 void setMeanCurve(DistributionMetric metric, std::vector<real_t> mean_curve);
81
88 void setFrameCurve(DistributionMetric metric, std::size_t frame_idx, std::vector<real_t> curve);
89
94 [[nodiscard]] std::size_t getTotalFrames() const noexcept;
95
101 [[nodiscard]] bool hasData(DistributionMetric metric) const noexcept;
102
109 [[nodiscard]] bool hasFrame(DistributionMetric metric, std::size_t frame_idx) const noexcept;
110
116 [[nodiscard]] const std::vector<real_t> &getBins(DistributionMetric metric) const;
117
123 [[nodiscard]] const std::vector<real_t> &getMeanCurve(DistributionMetric metric) const;
124
131 [[nodiscard]] const std::vector<real_t> &getFrameCurve(DistributionMetric metric,
132 std::size_t frame_idx) const;
133
139 [[nodiscard]] const MetricCurveData &getMetricData(DistributionMetric metric) const;
140
146 [[nodiscard]] MetricCurveData &getMetricData(DistributionMetric metric);
147
152 void configureMeanFrames(MeanFrameWindow window);
153
159 void markFrameInMean(std::size_t frame_idx, bool in_mean = true);
160
166 [[nodiscard]] bool isFrameInMean(std::size_t frame_idx) const noexcept;
167
173
177 void clearFrameGenerator() noexcept;
178
184 [[nodiscard]] bool needsFrameGeneration(std::size_t frame_idx) const noexcept;
185
190 void ensureFrame(std::size_t frame_idx) const;
191
192private:
193 [[nodiscard]] static std::size_t toIndex(DistributionMetric metric) noexcept;
194 [[nodiscard]] bool needsFrameGenerationUnlocked(std::size_t frame_idx) const noexcept;
195
196 mutable std::shared_mutex mutex_;
197 mutable std::mutex generation_mutex_;
198 std::size_t total_frames_{0};
199 mutable std::array<MetricCurveData, METRIC_COUNT> metrics_;
201 std::vector<uint8_t> in_mean_mask_;
203 static const std::vector<real_t> EMPTY_CURVE;
204};
205
206} // namespace correlation::analysis
Floating point precision type definitions for Correlation.
const std::vector< real_t > & getMeanCurve(DistributionMetric metric) const
Retrieves the ensemble mean curve for a metric.
Definition PerFrameDistributionStore.cpp:112
const std::vector< real_t > & getBins(DistributionMetric metric) const
Retrieves the shared bin coordinates for a metric.
Definition PerFrameDistributionStore.cpp:106
std::array< MetricCurveData, METRIC_COUNT > metrics_
Definition PerFrameDistributionStore.hpp:199
void markFrameInMean(std::size_t frame_idx, bool in_mean=true)
Explicitly marks whether a single frame contributed to the ensemble mean.
Definition PerFrameDistributionStore.cpp:150
std::mutex generation_mutex_
Definition PerFrameDistributionStore.hpp:197
std::vector< uint8_t > in_mean_mask_
Definition PerFrameDistributionStore.hpp:201
bool has_explicit_mean_mask_
Definition PerFrameDistributionStore.hpp:200
bool isFrameInMean(std::size_t frame_idx) const noexcept
Queries whether a trajectory frame contributed to the ensemble mean.
Definition PerFrameDistributionStore.cpp:162
std::function< FrameCurveArray(std::size_t)> FrameCurveGenerator
Definition PerFrameDistributionStore.hpp:54
static std::size_t toIndex(DistributionMetric metric) noexcept
Definition PerFrameDistributionStore.cpp:60
static const std::vector< real_t > EMPTY_CURVE
Definition PerFrameDistributionStore.hpp:203
bool hasData(DistributionMetric metric) const noexcept
Checks if bins and curves are available for a given metric.
Definition PerFrameDistributionStore.cpp:90
std::array< std::vector< real_t >, METRIC_COUNT > FrameCurveArray
Definition PerFrameDistributionStore.hpp:53
void ensureFrame(std::size_t frame_idx) const
Lazily computes and caches missing 1D curves for frame_idx if a generator is attached.
Definition PerFrameDistributionStore.cpp:198
void clearFrameGenerator() noexcept
Detaches any active lazy frame generator.
Definition PerFrameDistributionStore.cpp:178
bool hasFrame(DistributionMetric metric, std::size_t frame_idx) const noexcept
Checks if curve data is populated for a specific frame index.
Definition PerFrameDistributionStore.cpp:96
std::size_t total_frames_
Definition PerFrameDistributionStore.hpp:198
static constexpr std::size_t METRIC_COUNT
Definition PerFrameDistributionStore.hpp:52
const MetricCurveData & getMetricData(DistributionMetric metric) const
Retrieves const reference to the full metric curve dataset.
Definition PerFrameDistributionStore.cpp:127
FrameCurveGenerator frame_generator_
Definition PerFrameDistributionStore.hpp:202
const std::vector< real_t > & getFrameCurve(DistributionMetric metric, std::size_t frame_idx) const
Retrieves the curve for a specific frame index.
Definition PerFrameDistributionStore.cpp:117
std::shared_mutex mutex_
Definition PerFrameDistributionStore.hpp:196
bool needsFrameGenerationUnlocked(std::size_t frame_idx) const noexcept
Definition PerFrameDistributionStore.cpp:183
void setBins(DistributionMetric metric, std::vector< real_t > bins)
Sets the shared bin coordinates for a metric.
Definition PerFrameDistributionStore.cpp:64
std::size_t getTotalFrames() const noexcept
Returns the total trajectory frame count allocated in this store.
Definition PerFrameDistributionStore.cpp:88
void configureMeanFrames(MeanFrameWindow window)
Records the strided [start_frame, start_frame + sample_count * stride) mean window.
Definition PerFrameDistributionStore.cpp:137
bool needsFrameGeneration(std::size_t frame_idx) const noexcept
Checks whether any metric with a populated mean curve is missing a curve for frame_idx.
Definition PerFrameDistributionStore.cpp:193
void setFrameCurve(DistributionMetric metric, std::size_t frame_idx, std::vector< real_t > curve)
Sets the 1D distribution curve for a specific trajectory frame.
Definition PerFrameDistributionStore.cpp:75
void setFrameGenerator(FrameCurveGenerator generator)
Installs an on-demand 1D curve generator for frames outside the initial mean range.
Definition PerFrameDistributionStore.cpp:173
PerFrameDistributionStore(std::size_t total_frames)
Constructs a store pre-allocated for a specific total frame count.
Definition PerFrameDistributionStore.cpp:16
void setMeanCurve(DistributionMetric metric, std::vector< real_t > mean_curve)
Sets the ensemble mean curve for a metric.
Definition PerFrameDistributionStore.cpp:69
Definition AnalysisTypes.hpp:11
DistributionMetric
Identifiers for 1D distributions tracked on a per-frame basis.
Definition PerFrameDistributionStore.hpp:24
@ RDF
Radial Distribution Function g(r).
Definition PerFrameDistributionStore.hpp:25
@ SQ
Structure Factor S(Q).
Definition PerFrameDistributionStore.hpp:27
@ XRD
X-Ray Diffraction Pattern I(2θ).
Definition PerFrameDistributionStore.hpp:28
@ PAD
Planar Angle Distribution θ
Definition PerFrameDistributionStore.hpp:26
double real_t
Definition Precision.hpp:18
Holds 1D curve data for a specific distribution across an entire trajectory.
Definition PerFrameDistributionStore.hpp:34
std::string y_unit
Physical unit for Y axis.
Definition PerFrameDistributionStore.hpp:41
std::string name
Short metric identifier ("g(r)", "PAD", "S(Q)").
Definition PerFrameDistributionStore.hpp:38
std::string x_unit
Physical unit for X axis ("Å", "deg", "Å⁻¹").
Definition PerFrameDistributionStore.hpp:40
std::vector< real_t > bins
X-axis bin positions (shared across frames).
Definition PerFrameDistributionStore.hpp:35
std::vector< real_t > mean_curve
Y-axis ensemble mean curve (from [min_frame, max_frame]).
Definition PerFrameDistributionStore.hpp:36
std::vector< std::vector< real_t > > frame_curves
Y-axis per-frame curves (indexed by frame_idx).
Definition PerFrameDistributionStore.hpp:37
std::string title
Full display title.
Definition PerFrameDistributionStore.hpp:39
std::size_t sample_count
Definition PerFrameDistributionStore.hpp:58
std::size_t start_frame
Definition PerFrameDistributionStore.hpp:57
std::size_t stride
Definition PerFrameDistributionStore.hpp:59