FML
Loading...
Searching...
No Matches
PolyspectrumBinning.h
Go to the documentation of this file.
1#ifndef POLYSPECTRUMBINNING_HEADER
2#define POLYSPECTRUMBINNING_HEADER
3#include <FML/Global/Global.h>
4#include <algorithm>
5#include <array>
6#include <cmath>
7#include <functional>
8#include <optional>
9#include <stdexcept>
10#include <vector>
11
12namespace FML {
13 namespace CORRELATIONFUNCTIONS {
14
16 /// Zero retains all needed shells. Otherwise cap the resident shell count.
18 /// Per-rank cache budget; zero is unlimited. Excludes the input, one
19 /// source-resolution padding scratch grid, MPI remapping buffers, FFT workspace and result arrays.
21 /// Canonical configurations per collective; zero requests one batch (up to INT_MAX).
23 /// Optional caller estimate for opaque FFT plans/workspace, in bytes per rank.
24 std::optional<size_t> fft_workspace_allowance_bytes;
25 };
27 // Component maxima over the latest geometry/field evaluation phases and all ranks.
32 // Absent unless every rank supplies an FFT allowance. Neither number is measured process RSS.
33 std::optional<size_t> estimated_peak_bytes;
34 };
41
42 /// Dense results by default; COMPACT stores one slot per canonical tuple.
43 template <int N, int ORDER, bool COMPACT = false>
45 public:
46 static constexpr bool compact_storage = COMPACT;
48
49 /// Number of bins
50 int n{0};
51 /// Type of bins (LINEAR_SPACING or LOG_SPACING)
53 /// The minimum k to bin from (should be 0!)
54 double kmin{0.0};
55 /// The maximum k to bin to
56 double kmax{0.0};
57
58 /// Subtract shotnoise? Algorithms (compute_polyspectrum) using this class checks this value
60
61 /// The polyspectrum
62 std::vector<double> P123;
63 /// The volume (number of generalized triangles) of each bin
64 std::vector<double> N123;
65 /// The power-spectrum
66 std::vector<double> pofk;
67
68 /// kbin is center of bin
69 std::vector<double> kbin;
70 /// klow is lower bin edge
71 std::vector<double> klow;
72 /// khigh is higher bin edge
73 std::vector<double> khigh;
74 /// kmean is the mean of wavenumbers added to the bin
75 std::vector<double> kmean;
76
77 /// If we have precomputed the volume factor (N123) or not and have it available for algorithms to use
78 bool bincount_is_set{false};
79 /// True integer-wavevector closure. False retains legacy modulo-grid closure.
80 /// Nonlinear estimates exclude even-mesh Nyquist planes when enabled.
81 bool dealias{true};
84 using Configuration = std::array<int, ORDER>;
85
86 /// Empty restores all configurations. Inputs are canonicalized and deduplicated.
87 void set_configurations(std::vector<Configuration> configurations) {
88 for (auto & cfg : configurations) {
89 for (auto bin : cfg)
90 if (bin < 0 || bin >= n)
91 throw std::invalid_argument("[PolyspectrumBinning] Selected bin is out of range");
92 std::sort(cfg.begin(), cfg.end());
93 }
94 std::sort(configurations.begin(), configurations.end());
95 configurations.erase(std::unique(configurations.begin(), configurations.end()), configurations.end());
96 if (selected == configurations)
97 return;
98 selected = std::move(configurations);
99 bincount_is_set = false;
100 if constexpr (COMPACT) {
101 std::vector<Configuration>().swap(compact_configs);
102 std::vector<Configuration>().swap(cached_selected);
103 std::vector<double>().swap(P123);
104 std::vector<double>().swap(N123);
105 }
106 }
107 std::vector<Configuration> configurations() {
108 std::vector<Configuration> result;
109 if (!selected.empty()) {
110 for (const auto & cfg : selected)
112 result.push_back(cfg);
113 return result;
114 }
115 Configuration cfg{};
116 std::function<void(int, int)> enumerate = [&](int depth, int first) {
117 if (depth == ORDER) {
119 result.push_back(cfg);
120 return;
121 }
122 for (int bin = first; bin < n; bin++) {
123 cfg[depth] = bin;
124 enumerate(depth + 1, bin);
125 }
126 };
127 enumerate(0, 0);
128 return result;
129 }
130
131 /// Allocate compact results only when needed. No dense tensor is constructed.
133 if constexpr (COMPACT) {
134 auto configs = configurations();
135 if (configs != compact_configs || P123.size() != configs.size() || N123.size() != configs.size()) {
136 std::vector<double> powers(configs.size(), 0.0), counts(configs.size(), 0.0);
137 compact_configs = std::move(configs);
138 P123 = std::move(powers);
139 N123 = std::move(counts);
140 bincount_is_set = false;
141 }
142 }
143 }
144 const std::vector<Configuration> & stored_configurations() const { return compact_configs; }
145 size_t result_storage_bytes() const { return (P123.capacity() + N123.capacity()) * sizeof(double); }
146 size_t metadata_storage_bytes() const {
147 return sizeof(*this) +
148 (pofk.capacity() + kbin.capacity() + klow.capacity() + khigh.capacity() + kmean.capacity() +
149 cached_low.capacity() + cached_high.capacity()) *
150 sizeof(double) +
151 (compact_configs.capacity() + selected.capacity() + cached_selected.capacity()) *
152 sizeof(Configuration);
153 }
154
155 static size_t dense_entries(int nbins) {
156 if (nbins < 2)
157 throw std::invalid_argument("[PolyspectrumBinning] At least two bins are required");
158 size_t entries = 1;
159 const size_t limit = std::vector<double>().max_size();
160 for (int d = 0; d < ORDER; ++d) {
161 if (entries > limit / size_t(nbins))
162 throw std::length_error("[PolyspectrumBinning] Dense result exceeds addressable storage");
163 entries *= size_t(nbins);
164 }
165 return entries;
166 }
167 bool valid_storage() const {
168 const size_t entries = COMPACT ? compact_configs.size() : dense_entries(n);
169 return P123.size() == entries && N123.size() == entries;
170 }
171 /// Explicit dense allocation and symmetry expansion; never used by estimators.
173
174 bool has_bincount(int nmesh) {
175 if (!bincount_is_set || cached_low != klow || cached_high != khigh || cached_dealias != dealias ||
176 cached_selected != selected)
177 return false;
178 if (cached_mesh == 0)
179 cached_mesh = nmesh; // legacy explicitly imported counts
180 return cached_mesh == nmesh;
181 }
182 void mark_bincount_computed(int nmesh) {
183 cached_mesh = nmesh;
184 cached_low = klow;
185 cached_high = khigh;
186 cached_dealias = dealias;
187 cached_selected = selected;
188 bincount_is_set = true;
189 }
190
191 private:
192 template <int, int, bool>
194 std::vector<Configuration> compact_configs;
195 size_t compact_index(Configuration cfg) const {
196 for (auto bin : cfg)
197 if (bin < 0 || bin >= n)
198 throw std::out_of_range("[PolyspectrumBinning] Bin is out of range");
199 std::sort(cfg.begin(), cfg.end());
200 auto found = std::lower_bound(compact_configs.begin(), compact_configs.end(), cfg);
201 return found != compact_configs.end() && *found == cfg ? size_t(found - compact_configs.begin()) :
202 compact_configs.size();
203 }
204 std::vector<Configuration> selected, cached_selected;
205 int cached_mesh{-1};
206 std::vector<double> cached_low, cached_high;
207 bool cached_dealias{true};
208
209 public:
211 PolyspectrumBinning(double _kmin, double _kmax, int nbins, int bin_type = LINEAR_SPACING);
212 PolyspectrumBinning(int nbins, int Nmesh, int bin_type = LINEAR_SPACING);
213
214 /// To physical units: k *= 1/boxsize and P *= boxsize^((ORDER-1) NDIM)
215 void scale(const double boxsize);
216
217 /// Clears the arrays
218 void reset();
219
220 /// Release result arrays/canonical slots, retaining shell metadata and selection.
221 void free();
222
223 /// Get the polyspectra at (k[ik1], k[ik2], ...)
224 double get_spectrum(const std::array<int, ORDER> & ik);
225 /// For ORDER = 2 get the monospectum P(i,j) with i,j index of bin in [0,nbins)
226 double get_spectrum(int i, int j);
227 /// For ORDER = 3 get the bispectum B(i,j,k) with i,j,k index of bin in [0,nbins)
228 double get_spectrum(int i, int j, int k);
229 /// For ORDER = 4 get the trispectum T(i,j,k,l) with i,j,k,l index of bin in [0,nbins)
230 double get_spectrum(int i, int j, int k, int l);
231
232 /// Get the reduced polyspectrum: P / fac
233 /// where fac = sqrt(P(k1)P(k2)) for order=2 and P(k1)P(k2) + cyc for ORDER=3,4
234 double get_reduced_spectrum(const std::array<int, ORDER> & ik);
235 double get_reduced_spectrum(int i, int j);
236 double get_reduced_spectrum(int i, int j, int k);
237 double get_reduced_spectrum(int i, int j, int k, int l);
238
239 /// Returns the volume factor N123
240 double get_bincount(const std::array<int, ORDER> & ik);
241 double get_bincount(int i, int j);
242 double get_bincount(int i, int j, int k);
243 double get_bincount(int i, int j, int k, int l);
244
245 /* Simpler ways of doing the methods above
246 template <typename... Ints>
247 double get_spectrum(Ints... is);
248 template <typename... Ints>
249 double get_reduced_spectrum(Ints... is);
250 template <typename... Ints>
251 double get_bincount(Ints... is);
252 */
253
254 /// Copy over the volume factor N123 (useful to save computations if we do many calculations with
255 /// the same setup)
256 void set_bincount(const std::vector<double> & N123_external);
257 /// Import with an explicit mesh identity (preferred to the legacy overload).
258 void set_bincount(const std::vector<double> & counts, int nmesh) {
259 set_bincount(counts);
261 }
262
263 /// This is just to make it easier to add binning of several spectra... just for testing
264 void combine(PolyspectrumBinning & rhs);
265 int nbinnings{0};
266
267 /// The coord (k1,k2,...) corresponding to a given index in the P123 and N123 array
268 /// These methods determine how the data is stored and fetched
269 std::array<int, ORDER> get_coord_from_index(size_t index);
270 size_t get_index_from_coord(const std::array<int, ORDER> & ik);
271
272 /// Symmetry: we only need to compute ik1 <= ik2 <= ...
273 /// This function tells algorithms which configurations to compute and
274 /// which to set by using symmetry from the configs we have computed
275 bool compute_this_configuration(const std::array<int, ORDER> & ik);
276 };
277
278 template <int N, int ORDER, bool COMPACT>
280 : PolyspectrumBinning(0.0, 2.0 * M_PI * Nmesh / 2.0, nbins, bin_type){};
281
282 template <int N, int ORDER, bool COMPACT>
284 double _kmax,
285 int nbins,
286 int bin_type) {
287 assert_mpi(nbins >= 2, "[PolyspectrumBinning] We need at least 2 bins");
288 this->bin_type = bin_type;
289 n = nbins;
290 kmin = _kmin;
291 kmax = _kmax;
292 if constexpr (!COMPACT) {
293 const size_t ntot = dense_entries(n);
294 P123.resize(ntot, 0.0);
295 N123.resize(ntot, 0.0);
296 }
297 pofk.resize(n, 0.0);
298 kbin.resize(n, 0.0);
299 klow.resize(n, 0.0);
300 khigh.resize(n, 0.0);
301 kmean.resize(n, 0.0);
302
303 // k[0] is leftmost bin edge and k[n-1] is rightmost bin edge and otherwise center of bin
304 // Used to set kbin below
305 std::vector<double> k(n);
306 for (int i = 0; i < n; i++) {
307 if (bin_type == LINEAR_SPACING) {
308 k[i] = kmin + (kmax - kmin) * i / double(n - 1);
309 } else if (bin_type == LOG_SPACING) {
310 assert_mpi(kmin > 0.0, "[PolyspectrumBinning] For log spacing we cannot have kmin = 0.0");
311 k[i] = std::exp(std::log(kmin) + std::log(kmax / kmin) * i / double(n - 1));
312 } else {
313 // Unknown binning type
314 assert_mpi(false, "[PolyspectrumBinning] Unknown bintype (not LINEAR_SPACING or LOG_SPACING)");
315 }
316 }
317
318 // We require that klow = 0 in the smallest bin
319 for (int i = 0; i < n; i++) {
320 if (i == 0) {
321 klow[i] = std::min(k[0], 0.0);
322 khigh[i] = k[0] + (k[1] - k[0]) / 2.0;
323 if (bin_type == LOG_SPACING)
324 khigh[i] = std::exp(std::log(k[0]) + std::log(k[1] / k[0]) / 2.0);
325 } else if (i < n - 1) {
326 klow[i] = khigh[i - 1];
327 khigh[i] = k[i] + (k[i + 1] - k[i]) / 2.0;
328 if (bin_type == LOG_SPACING)
329 khigh[i] = std::exp(std::log(k[i]) + std::log(k[i + 1] / k[i]) / 2.0);
330 } else {
331 klow[i] = khigh[i - 1];
332 khigh[i] = k[nbins - 1];
333 }
334 kbin[i] = (klow[i] + khigh[i]) / 2.0;
335 }
336 }
337
338 template <int N, int ORDER, bool COMPACT>
340 if constexpr (COMPACT)
341 return compact_configs.at(index);
342 std::array<int, ORDER> ik;
343 for (int ii = ORDER - 1; ii >= 0; ii--) {
344 ik[ii] = int(index % n);
345 index /= size_t(n);
346 }
347 return ik;
348 }
349
350 template <int N, int ORDER, bool COMPACT>
351 size_t PolyspectrumBinning<N, ORDER, COMPACT>::get_index_from_coord(const std::array<int, ORDER> & ik) {
352 if constexpr (COMPACT) {
353 const size_t index = compact_index(ik);
354 if (index == compact_configs.size())
355 throw std::out_of_range("[PolyspectrumBinning] Configuration has no stored slot");
356 return index;
357 }
358 size_t index = 0;
359 for (int i = 0; i < ORDER; i++)
360 index = index * n + ik[i];
361 return index;
362 }
363
364 // Symmetry: we only need to compute ik1 <= ik2 <= ...
365 template <int N, int ORDER, bool COMPACT>
367 if (!selected.empty() && !std::binary_search(selected.begin(), selected.end(), ik))
368 return false;
369 double ksum = 0.0;
370 for (int ii = 1; ii < ORDER; ii++) {
371 if (ik[ii - 1] > ik[ii]) {
372 return false;
373 }
374 ksum += khigh[ik[ii - 1]];
375 }
376
377 // No valid 'triangles' if k1+k2+... < kN so can just set too zero right away
378 // Saves a bit of time computing, but not significantly so its fine to remove this just to be sure
379 if (ksum < klow[ik[ORDER - 1]] and ORDER > 2)
380 return false;
381
382 return true;
383 }
384
385 template <int N, int ORDER, bool COMPACT>
387 for (int i = 0; i < n; i++) {
388 kbin[i] *= 1.0 / boxsize;
389 klow[i] *= 1.0 / boxsize;
390 khigh[i] *= 1.0 / boxsize;
391 kmean[i] *= 1.0 / boxsize;
392 }
393
394 // Scale polyspectrum
395 double scale = std::pow(boxsize, N * (ORDER - 1));
396 for (auto & p : P123)
397 p *= scale;
398
399 // Scale power-spectrum
400 scale = std::pow(boxsize, N);
401 for (auto & p : pofk)
402 p *= scale;
403
404 // In principle we should scale N123 by 1/boxsize^ORDER
405 // however this is only used internally and then in
406 // dimensionless units so we omit this here
407 }
408
409 template <int N, int ORDER, bool COMPACT>
411 std::fill(P123.begin(), P123.end(), 0.0);
412 std::fill(N123.begin(), N123.end(), 0.0);
413 std::fill(pofk.begin(), pofk.end(), 0.0);
414 std::fill(kmean.begin(), kmean.end(), 0.0);
415 nbinnings = 0;
416 // N123 is zeroed so it must be computed again
417 bincount_is_set = false;
418 }
419
420 template <int N, int ORDER, bool COMPACT>
422 // We only bother to free the things that take up memory
423 bincount_is_set = false;
424 P123.clear();
425 P123.shrink_to_fit();
426 N123.clear();
427 N123.shrink_to_fit();
428 if constexpr (COMPACT) {
429 std::vector<Configuration>().swap(compact_configs);
430 std::vector<Configuration>().swap(cached_selected);
431 }
432 }
433
434 /* Simpler way of doing the methods below
435 template <int N, int ORDER, bool COMPACT>
436 template<typename... Ints>
437 double PolyspectrumBinning<N, ORDER, COMPACT>::get_spectrum(Ints... is) {
438 static_assert(sizeof...(is) == ORDER,
439 "Calling get_spectrum(i1, i2, ..., in) requires n == ORDER (e.g. get_spetrum(i,j,k) can only be called
440 when ORDER=3"); std::array<int, ORDER> ik{is...}; return get_spectrum(ik);
441 }
442 template <int N, int ORDER, bool COMPACT>
443 template<typename... Ints>
444 double PolyspectrumBinning<N, ORDER, COMPACT>::get_reduced_spectrum(Ints... is) {
445 static_assert(sizeof...(is) == ORDER,
446 "Calling get_reduced_spectrum(i1, i2, ..., in) requires n == ORDER (e.g. get_reduced_spetrum(i,j,k)
447 can only be called when ORDER=3"); std::array<int, ORDER> ik{is...}; return get_reduced_spectrum(ik);
448 }
449 template <int N, int ORDER, bool COMPACT>
450 template<typename... Ints>
451 double PolyspectrumBinning<N, ORDER, COMPACT>::get_bincount(Ints... is) {
452 static_assert(sizeof...(is) == ORDER,
453 "Calling get_bincount(i1, i2, ..., in) requires n == ORDER (e.g. get_bincount(i,j,k) can only be
454 called when ORDER=3"); std::array<int, ORDER> ik{is...}; return get_bincount(ik);
455 }
456 */
457
458 template <int N, int ORDER, bool COMPACT>
460 assert_mpi(ORDER == 2, "[PolyspectrumBinning::get_spectrum] This method can only be called for ORDER=2\n");
461 std::array<int, 2> ik{i, j};
462 return get_spectrum(ik);
463 }
464
465 template <int N, int ORDER, bool COMPACT>
467 assert_mpi(ORDER == 3, "[PolyspectrumBinning::get_spectrum] This method can only be called for ORDER=3\n");
468 std::array<int, 3> ik{i, j, k};
469 return get_spectrum(ik);
470 }
471
472 template <int N, int ORDER, bool COMPACT>
473 double PolyspectrumBinning<N, ORDER, COMPACT>::get_spectrum(int i, int j, int k, int l) {
474 assert_mpi(ORDER == 4, "[PolyspectrumBinning::get_spectrum] This method can only be called for ORDER=4\n");
475 std::array<int, 4> ik{i, j, k, l};
476 return get_spectrum(ik);
477 }
478
479 template <int N, int ORDER, bool COMPACT>
480 double PolyspectrumBinning<N, ORDER, COMPACT>::get_spectrum(const std::array<int, ORDER> & ik) {
481 if constexpr (COMPACT) {
482 const size_t index = compact_index(ik);
483 return index < P123.size() ? P123[index] : 0.0;
484 }
485 assert_mpi(ik.size() == ORDER, "[PolyspectrumBinning::get_spectrum] ik != ORDER has the wrong size\n");
486 return P123[get_index_from_coord(ik)];
487 }
488
489 template <int N, int ORDER, bool COMPACT>
491 assert_mpi(ORDER == 2,
492 "[PolyspectrumBinning::get_reduced_spectrum] This method can only be called for ORDER=2\n");
493 std::array<int, 2> ik{i, j};
494 return get_reduced_spectrum(ik);
495 }
496
497 template <int N, int ORDER, bool COMPACT>
499 assert_mpi(ORDER == 3,
500 "[PolyspectrumBinning::get_reduced_spectrum] This method can only be called for ORDER=3\n");
501 std::array<int, 3> ik{i, j, k};
502 return get_reduced_spectrum(ik);
503 }
504
505 template <int N, int ORDER, bool COMPACT>
507 assert_mpi(ORDER == 4,
508 "[PolyspectrumBinning::get_reduced_spectrum] This method can only be called for ORDER=4\n");
509 std::array<int, 4> ik{i, j, k, l};
510 return get_reduced_spectrum(ik);
511 }
512
513 template <int N, int ORDER, bool COMPACT>
514 double PolyspectrumBinning<N, ORDER, COMPACT>::get_reduced_spectrum(const std::array<int, ORDER> & ik) {
515 const double P = get_spectrum(ik);
516 // An empty bin (or a configuration that is not a triangle) has P = 0 and maybe zero power below
517 if (P == 0.0)
518 return 0.0;
519 std::vector<double> pi(ORDER);
520
521 for (int i = 0; i < ORDER; i++)
522 pi[i] = pofk[ik[i]];
523
524 if (ORDER == 2) {
525 // For gaussian delta(i,j) for ORDER = 2
526 return P / std::sqrt(pofk[ik[0]] * pofk[ik[1]]);
527 } else if (ORDER == 3 or ORDER == 4) {
528 // P(k1,..) / (P1P2 + cyc) for ORDER = 3 or 4
529 // So = 0 and = 1 if gaussian
530 double pipj = pi[ORDER - 1] * pi[0];
531 for (int i = 0; i < ORDER - 1; i++)
532 pipj += pi[i] * pi[i + 1];
533 return P / pipj;
534 }
535
536 // The general case
537 // <d1d2...d2n> / (Pi1Pi2...Pin + cyc) ~ 1 if gaussian
538 // This will never be used so we don't implement it
539 assert_mpi(false, "[PolyspectrumBinning::get_reduced_spectrum] Not implemented\n");
540
541 return 0.0;
542 }
543
544 template <int N, int ORDER, bool COMPACT>
545 double PolyspectrumBinning<N, ORDER, COMPACT>::get_bincount(const std::array<int, ORDER> & ik) {
546 if constexpr (COMPACT) {
547 const size_t index = compact_index(ik);
548 return index < N123.size() ? N123[index] : 0.0;
549 }
550 assert_mpi(ik.size() == ORDER, "[PolyspectrumBinning::get_bincount] ik != ORDER has the wrong size\n");
551 return N123[get_index_from_coord(ik)];
552 }
553
554 template <int N, int ORDER, bool COMPACT>
556 assert_mpi(ORDER == 2, "[PolyspectrumBinning::get_bincount] This method can only be called for ORDER=2\n");
557 std::array<int, 2> ik{i, j};
558 return get_bincount(ik);
559 }
560
561 template <int N, int ORDER, bool COMPACT>
563 assert_mpi(ORDER == 3, "[PolyspectrumBinning::get_bincount] This method can only be called for ORDER=3\n");
564 std::array<int, 3> ik{i, j, k};
565 return get_bincount(ik);
566 }
567
568 template <int N, int ORDER, bool COMPACT>
569 double PolyspectrumBinning<N, ORDER, COMPACT>::get_bincount(int i, int j, int k, int l) {
570 assert_mpi(ORDER == 4, "[PolyspectrumBinning::get_bincount] This method can only be called for ORDER=4\n");
571 std::array<int, 4> ik{i, j, k, l};
572 return get_bincount(ik);
573 }
574
575 template <int N, int ORDER, bool COMPACT>
577 prepare_storage();
578 rhs.prepare_storage();
579 assert_mpi(n == rhs.n and bin_type == rhs.bin_type && klow == rhs.klow && khigh == rhs.khigh &&
580 dealias == rhs.dealias && selected == rhs.selected && compact_configs == rhs.compact_configs,
581 "[PolyspectrumBinning::combine] Incompatible binnings\n");
582 if (nbinnings == 0) {
583 P123 = rhs.P123;
584 N123 = rhs.N123;
585 pofk = rhs.pofk;
586 kmean = rhs.kmean;
587 } else {
588 for (size_t i = 0; i < P123.size(); i++)
589 P123[i] = (P123[i] * nbinnings + rhs.P123[i]) / (nbinnings + 1.0);
590 for (size_t i = 0; i < N123.size(); i++)
591 N123[i] += rhs.N123[i];
592 for (size_t i = 0; i < pofk.size(); i++)
593 pofk[i] = (pofk[i] * nbinnings + rhs.pofk[i]) / (nbinnings + 1.0);
594 }
595 nbinnings++;
596 bincount_is_set = false;
597 }
598
599 template <int N, int ORDER, bool COMPACT>
600 void PolyspectrumBinning<N, ORDER, COMPACT>::set_bincount(const std::vector<double> & N123_external) {
601 prepare_storage();
602 assert_mpi(N123_external.size() == N123.size(), "[set_bincount] Incompatible size of N123 array");
603 N123 = N123_external;
604 if (!selected.empty() && !COMPACT) {
605 const auto requested = configurations();
606 std::vector<double> values;
607 for (const auto & cfg : requested)
608 values.push_back(N123[get_index_from_coord(cfg)]);
609 std::fill(N123.begin(), N123.end(), 0.0);
610 for (size_t i = 0; i < requested.size(); i++) {
611 auto cfg = requested[i];
612 do {
613 N123[get_index_from_coord(cfg)] = values[i];
614 } while (std::next_permutation(cfg.begin(), cfg.end()));
615 }
616 }
617 mark_bincount_computed(0);
618 }
619
620 template <int N, int ORDER, bool COMPACT>
622 if constexpr (!COMPACT) {
623 return *this;
624 } else {
625 if (P123.size() != compact_configs.size() || N123.size() != compact_configs.size())
626 throw std::invalid_argument("[PolyspectrumBinning] Inconsistent compact result storage");
627 PolyspectrumBinning<N, ORDER, false> dense(kmin, kmax, n, bin_type);
628 dense.subtract_shotnoise = subtract_shotnoise;
629 dense.dealias = dealias;
630 dense.options = options;
631 dense.statistics = statistics;
632 dense.pofk = pofk;
633 dense.kbin = kbin;
634 dense.klow = klow;
635 dense.khigh = khigh;
636 dense.kmean = kmean;
637 dense.nbinnings = nbinnings;
638 dense.selected = selected;
639 for (size_t i = 0; i < compact_configs.size(); ++i) {
640 auto cfg = compact_configs[i];
641 do {
642 const auto index = dense.get_index_from_coord(cfg);
643 dense.P123[index] = P123[i];
644 dense.N123[index] = N123[i];
645 } while (std::next_permutation(cfg.begin(), cfg.end()));
646 }
647 dense.bincount_is_set = bincount_is_set;
648 dense.cached_mesh = cached_mesh;
649 dense.cached_low = cached_low;
650 dense.cached_high = cached_high;
651 dense.cached_dealias = cached_dealias;
652 dense.cached_selected = cached_selected;
653 return dense;
654 }
655 }
656
657 template <int N, int ORDER>
659 } // namespace CORRELATIONFUNCTIONS
660} // namespace FML
661
662#endif
const int Nmesh
Definition Main.cpp:48
const double boxsize
Definition TestHDF5.cpp:47
Dense results by default; COMPACT stores one slot per canonical tuple.
void set_bincount(const std::vector< double > &counts, int nmesh)
Import with an explicit mesh identity (preferred to the legacy overload).
std::vector< double > pofk
The power-spectrum.
std::array< int, ORDER > get_coord_from_index(size_t index)
The coord (k1,k2,...) corresponding to a given index in the P123 and N123 array These methods determi...
size_t get_index_from_coord(const std::array< int, ORDER > &ik)
PolyspectrumBinning< N, ORDER, false > to_dense()
Explicit dense allocation and symmetry expansion; never used by estimators.
std::vector< double > khigh
khigh is higher bin edge
double kmin
The minimum k to bin from (should be 0!)
double get_bincount(const std::array< int, ORDER > &ik)
Returns the volume factor N123.
std::vector< double > P123
The polyspectrum.
void free()
Release result arrays/canonical slots, retaining shell metadata and selection.
std::vector< double > kmean
kmean is the mean of wavenumbers added to the bin
void set_configurations(std::vector< Configuration > configurations)
Empty restores all configurations. Inputs are canonicalized and deduplicated.
void combine(PolyspectrumBinning &rhs)
This is just to make it easier to add binning of several spectra... just for testing.
std::vector< double > kbin
kbin is center of bin
std::vector< double > N123
The volume (number of generalized triangles) of each bin.
const std::vector< Configuration > & stored_configurations() const
void scale(const double boxsize)
To physical units: k *= 1/boxsize and P *= boxsize^((ORDER-1) NDIM)
double get_spectrum(const std::array< int, ORDER > &ik)
Get the polyspectra at (k[ik1], k[ik2], ...)
void prepare_storage()
Allocate compact results only when needed. No dense tensor is constructed.
std::vector< double > klow
klow is lower bin edge
int bin_type
Type of bins (LINEAR_SPACING or LOG_SPACING)
void set_bincount(const std::vector< double > &N123_external)
Copy over the volume factor N123 (useful to save computations if we do many calculations with the sam...
bool dealias
True integer-wavevector closure.
bool compute_this_configuration(const std::array< int, ORDER > &ik)
Symmetry: we only need to compute ik1 <= ik2 <= ... This function tells algorithms which configuratio...
bool subtract_shotnoise
Subtract shotnoise? Algorithms (compute_polyspectrum) using this class checks this value.
bool bincount_is_set
If we have precomputed the volume factor (N123) or not and have it available for algorithms to use.
double get_reduced_spectrum(const std::array< int, ORDER > &ik)
Get the reduced polyspectrum: P / fac where fac = sqrt(P(k1)P(k2)) for order=2 and P(k1)P(k2) + cyc f...
size_t max_cached_shells
Zero retains all needed shells. Otherwise cap the resident shell count.
size_t reduction_batch_size
Canonical configurations per collective; zero requests one batch (up to INT_MAX).
size_t shell_memory_budget_bytes
Per-rank cache budget; zero is unlimited.
std::optional< size_t > fft_workspace_allowance_bytes
Optional caller estimate for opaque FFT plans/workspace, in bytes per rank.
constexpr int ii[6]
const double kmax
Definition test_fnl.cpp:43
Options options()