FML
Loading...
Searching...
No Matches
FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT > Class Template Reference

Dense results by default; COMPACT stores one slot per canonical tuple. More...

#include <PolyspectrumBinning.h>

Public Types

enum  BinningType { LINEAR_SPACING , LOG_SPACING }
 
using Configuration = std::array<int, ORDER>
 

Public Member Functions

void set_configurations (std::vector< Configuration > configurations)
 Empty restores all configurations. Inputs are canonicalized and deduplicated.
 
std::vector< Configuration > configurations ()
 
void prepare_storage ()
 Allocate compact results only when needed. No dense tensor is constructed.
 
const std::vector< Configuration > & stored_configurations () const
 
size_t result_storage_bytes () const
 
size_t metadata_storage_bytes () const
 
bool valid_storage () const
 
PolyspectrumBinning< N, ORDER, false > to_dense ()
 Explicit dense allocation and symmetry expansion; never used by estimators.
 
bool has_bincount (int nmesh)
 
void mark_bincount_computed (int nmesh)
 
 PolyspectrumBinning ()=default
 
 PolyspectrumBinning (double _kmin, double _kmax, int nbins, int bin_type=LINEAR_SPACING)
 
 PolyspectrumBinning (int nbins, int Nmesh, int bin_type=LINEAR_SPACING)
 
void scale (const double boxsize)
 To physical units: k *= 1/boxsize and P *= boxsize^((ORDER-1) NDIM)
 
void reset ()
 Clears the arrays.
 
void free ()
 Release result arrays/canonical slots, retaining shell metadata and selection.
 
double get_spectrum (const std::array< int, ORDER > &ik)
 Get the polyspectra at (k[ik1], k[ik2], ...)
 
double get_spectrum (int i, int j)
 For ORDER = 2 get the monospectum P(i,j) with i,j index of bin in [0,nbins)
 
double get_spectrum (int i, int j, int k)
 For ORDER = 3 get the bispectum B(i,j,k) with i,j,k index of bin in [0,nbins)
 
double get_spectrum (int i, int j, int k, int l)
 For ORDER = 4 get the trispectum T(i,j,k,l) with i,j,k,l index of bin in [0,nbins)
 
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 for ORDER=3,4.
 
double get_reduced_spectrum (int i, int j)
 
double get_reduced_spectrum (int i, int j, int k)
 
double get_reduced_spectrum (int i, int j, int k, int l)
 
double get_bincount (const std::array< int, ORDER > &ik)
 Returns the volume factor N123.
 
double get_bincount (int i, int j)
 
double get_bincount (int i, int j, int k)
 
double get_bincount (int i, int j, int k, int l)
 
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 same setup)
 
void set_bincount (const std::vector< double > &counts, int nmesh)
 Import with an explicit mesh identity (preferred to the legacy overload).
 
void combine (PolyspectrumBinning &rhs)
 This is just to make it easier to add binning of several spectra... just for testing.
 
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 determine how the data is stored and fetched.
 
size_t get_index_from_coord (const std::array< int, ORDER > &ik)
 
bool compute_this_configuration (const std::array< int, ORDER > &ik)
 Symmetry: we only need to compute ik1 <= ik2 <= ... This function tells algorithms which configurations to compute and which to set by using symmetry from the configs we have computed.
 

Static Public Member Functions

static size_t dense_entries (int nbins)
 

Data Fields

int n {0}
 Number of bins.
 
int bin_type {LINEAR_SPACING}
 Type of bins (LINEAR_SPACING or LOG_SPACING)
 
double kmin {0.0}
 The minimum k to bin from (should be 0!)
 
double kmax {0.0}
 The maximum k to bin to.
 
bool subtract_shotnoise {true}
 Subtract shotnoise? Algorithms (compute_polyspectrum) using this class checks this value.
 
std::vector< double > P123
 The polyspectrum.
 
std::vector< double > N123
 The volume (number of generalized triangles) of each bin.
 
std::vector< double > pofk
 The power-spectrum.
 
std::vector< double > kbin
 kbin is center of bin
 
std::vector< double > klow
 klow is lower bin edge
 
std::vector< double > khigh
 khigh is higher bin edge
 
std::vector< double > kmean
 kmean is the mean of wavenumbers added to the bin
 
bool bincount_is_set {false}
 If we have precomputed the volume factor (N123) or not and have it available for algorithms to use.
 
bool dealias {true}
 True integer-wavevector closure.
 
PolyspectrumOptions options
 
PolyspectrumStatistics statistics
 
int nbinnings {0}
 

Static Public Attributes

static constexpr bool compact_storage = COMPACT
 

Friends

template<int , int , bool >
class PolyspectrumBinning
 

Detailed Description

template<int N, int ORDER, bool COMPACT = false>
class FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >

Dense results by default; COMPACT stores one slot per canonical tuple.

Definition at line 44 of file PolyspectrumBinning.h.

Member Typedef Documentation

◆ Configuration

template<int N, int ORDER, bool COMPACT = false>
using FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::Configuration = std::array<int, ORDER>

Definition at line 84 of file PolyspectrumBinning.h.

Member Enumeration Documentation

◆ BinningType

template<int N, int ORDER, bool COMPACT = false>
enum FML::CORRELATIONFUNCTIONS::PolyspectrumBinning::BinningType

Constructor & Destructor Documentation

◆ PolyspectrumBinning() [1/3]

template<int N, int ORDER, bool COMPACT = false>
FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::PolyspectrumBinning ( )
default

◆ PolyspectrumBinning() [2/3]

template<int N, int ORDER, bool COMPACT>
FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::PolyspectrumBinning ( double _kmin,
double _kmax,
int nbins,
int bin_type = LINEAR_SPACING )

Definition at line 283 of file PolyspectrumBinning.h.

286 {
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 }
std::vector< double > pofk
The power-spectrum.
std::vector< double > khigh
khigh is higher bin edge
double kmin
The minimum k to bin from (should be 0!)
std::vector< double > P123
The polyspectrum.
std::vector< double > kmean
kmean is the mean of wavenumbers added to the bin
std::vector< double > kbin
kbin is center of bin
std::vector< double > N123
The volume (number of generalized triangles) of each bin.
std::vector< double > klow
klow is lower bin edge
int bin_type
Type of bins (LINEAR_SPACING or LOG_SPACING)

◆ PolyspectrumBinning() [3/3]

template<int N, int ORDER, bool COMPACT>
FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::PolyspectrumBinning ( int nbins,
int Nmesh,
int bin_type = LINEAR_SPACING )

Definition at line 279 of file PolyspectrumBinning.h.

280 : PolyspectrumBinning(0.0, 2.0 * M_PI * Nmesh / 2.0, nbins, bin_type){};
const int Nmesh
Definition Main.cpp:48

Member Function Documentation

◆ combine()

template<int N, int ORDER, bool COMPACT>
void FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::combine ( PolyspectrumBinning< N, ORDER, COMPACT > & rhs)

This is just to make it easier to add binning of several spectra... just for testing.

Definition at line 576 of file PolyspectrumBinning.h.

576 {
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 }
void prepare_storage()
Allocate compact results only when needed. No dense tensor is constructed.
bool dealias
True integer-wavevector closure.
bool bincount_is_set
If we have precomputed the volume factor (N123) or not and have it available for algorithms to use.
rhs(lna, y)

◆ compute_this_configuration()

template<int N, int ORDER, bool COMPACT>
bool FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::compute_this_configuration ( const std::array< int, ORDER > & ik)

Symmetry: we only need to compute ik1 <= ik2 <= ... This function tells algorithms which configurations to compute and which to set by using symmetry from the configs we have computed.

Definition at line 366 of file PolyspectrumBinning.h.

366 {
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 }
list ORDER
Definition cases.py:640
constexpr int ii[6]

◆ configurations()

template<int N, int ORDER, bool COMPACT = false>
std::vector< Configuration > FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::configurations ( )
inline

Definition at line 107 of file PolyspectrumBinning.h.

107 {
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 }
bool compute_this_configuration(const std::array< int, ORDER > &ik)
Symmetry: we only need to compute ik1 <= ik2 <= ... This function tells algorithms which configuratio...

◆ dense_entries()

template<int N, int ORDER, bool COMPACT = false>
static size_t FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::dense_entries ( int nbins)
inlinestatic

Definition at line 155 of file PolyspectrumBinning.h.

155 {
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 }

◆ free()

template<int N, int ORDER, bool COMPACT>
void FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::free ( )

Release result arrays/canonical slots, retaining shell metadata and selection.

Definition at line 421 of file PolyspectrumBinning.h.

421 {
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 }

◆ get_bincount() [1/4]

template<int N, int ORDER, bool COMPACT>
double FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::get_bincount ( const std::array< int, ORDER > & ik)

Returns the volume factor N123.

Definition at line 545 of file PolyspectrumBinning.h.

545 {
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 }
size_t get_index_from_coord(const std::array< int, ORDER > &ik)

◆ get_bincount() [2/4]

template<int N, int ORDER, bool COMPACT>
double FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::get_bincount ( int i,
int j )

Definition at line 555 of file PolyspectrumBinning.h.

555 {
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 }
double get_bincount(const std::array< int, ORDER > &ik)
Returns the volume factor N123.

◆ get_bincount() [3/4]

template<int N, int ORDER, bool COMPACT>
double FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::get_bincount ( int i,
int j,
int k )

Definition at line 562 of file PolyspectrumBinning.h.

562 {
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 }

◆ get_bincount() [4/4]

template<int N, int ORDER, bool COMPACT>
double FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::get_bincount ( int i,
int j,
int k,
int l )

Definition at line 569 of file PolyspectrumBinning.h.

569 {
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 }

◆ get_coord_from_index()

template<int N, int ORDER, bool COMPACT>
std::array< int, ORDER > FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::get_coord_from_index ( size_t index)

The coord (k1,k2,...) corresponding to a given index in the P123 and N123 array These methods determine how the data is stored and fetched.

Definition at line 339 of file PolyspectrumBinning.h.

339 {
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 }

◆ get_index_from_coord()

template<int N, int ORDER, bool COMPACT>
size_t FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::get_index_from_coord ( const std::array< int, ORDER > & ik)

Definition at line 351 of file PolyspectrumBinning.h.

351 {
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 }

◆ get_reduced_spectrum() [1/4]

template<int N, int ORDER, bool COMPACT>
double FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::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 for ORDER=3,4.

Definition at line 514 of file PolyspectrumBinning.h.

514 {
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 }
double get_spectrum(const std::array< int, ORDER > &ik)
Get the polyspectra at (k[ik1], k[ik2], ...)

◆ get_reduced_spectrum() [2/4]

template<int N, int ORDER, bool COMPACT>
double FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::get_reduced_spectrum ( int i,
int j )

Definition at line 490 of file PolyspectrumBinning.h.

490 {
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 }
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...

◆ get_reduced_spectrum() [3/4]

template<int N, int ORDER, bool COMPACT>
double FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::get_reduced_spectrum ( int i,
int j,
int k )

Definition at line 498 of file PolyspectrumBinning.h.

498 {
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 }

◆ get_reduced_spectrum() [4/4]

template<int N, int ORDER, bool COMPACT>
double FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::get_reduced_spectrum ( int i,
int j,
int k,
int l )

Definition at line 506 of file PolyspectrumBinning.h.

506 {
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 }

◆ get_spectrum() [1/4]

template<int N, int ORDER, bool COMPACT>
double FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::get_spectrum ( const std::array< int, ORDER > & ik)

Get the polyspectra at (k[ik1], k[ik2], ...)

Definition at line 480 of file PolyspectrumBinning.h.

480 {
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 }

◆ get_spectrum() [2/4]

template<int N, int ORDER, bool COMPACT>
double FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::get_spectrum ( int i,
int j )

For ORDER = 2 get the monospectum P(i,j) with i,j index of bin in [0,nbins)

Definition at line 459 of file PolyspectrumBinning.h.

459 {
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 }

◆ get_spectrum() [3/4]

template<int N, int ORDER, bool COMPACT>
double FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::get_spectrum ( int i,
int j,
int k )

For ORDER = 3 get the bispectum B(i,j,k) with i,j,k index of bin in [0,nbins)

Definition at line 466 of file PolyspectrumBinning.h.

466 {
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 }

◆ get_spectrum() [4/4]

template<int N, int ORDER, bool COMPACT>
double FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::get_spectrum ( int i,
int j,
int k,
int l )

For ORDER = 4 get the trispectum T(i,j,k,l) with i,j,k,l index of bin in [0,nbins)

Definition at line 473 of file PolyspectrumBinning.h.

473 {
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 }

◆ has_bincount()

template<int N, int ORDER, bool COMPACT = false>
bool FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::has_bincount ( int nmesh)
inline

Definition at line 174 of file PolyspectrumBinning.h.

174 {
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 }

◆ mark_bincount_computed()

template<int N, int ORDER, bool COMPACT = false>
void FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::mark_bincount_computed ( int nmesh)
inline

Definition at line 182 of file PolyspectrumBinning.h.

182 {
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 }

◆ metadata_storage_bytes()

template<int N, int ORDER, bool COMPACT = false>
size_t FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::metadata_storage_bytes ( ) const
inline

Definition at line 146 of file PolyspectrumBinning.h.

146 {
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 }

◆ prepare_storage()

template<int N, int ORDER, bool COMPACT = false>
void FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::prepare_storage ( )
inline

Allocate compact results only when needed. No dense tensor is constructed.

Definition at line 132 of file PolyspectrumBinning.h.

132 {
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 }

◆ reset()

template<int N, int ORDER, bool COMPACT>
void FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::reset ( )

Clears the arrays.

Definition at line 410 of file PolyspectrumBinning.h.

410 {
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 }

◆ result_storage_bytes()

template<int N, int ORDER, bool COMPACT = false>
size_t FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::result_storage_bytes ( ) const
inline

Definition at line 145 of file PolyspectrumBinning.h.

145{ return (P123.capacity() + N123.capacity()) * sizeof(double); }

◆ scale()

template<int N, int ORDER, bool COMPACT>
void FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::scale ( const double boxsize)

To physical units: k *= 1/boxsize and P *= boxsize^((ORDER-1) NDIM)

Definition at line 386 of file PolyspectrumBinning.h.

386 {
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 }
const double boxsize
Definition TestHDF5.cpp:47
void scale(const double boxsize)
To physical units: k *= 1/boxsize and P *= boxsize^((ORDER-1) NDIM)

◆ set_bincount() [1/2]

template<int N, int ORDER, bool COMPACT = false>
void FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::set_bincount ( const std::vector< double > & counts,
int nmesh )
inline

Import with an explicit mesh identity (preferred to the legacy overload).

Definition at line 258 of file PolyspectrumBinning.h.

258 {
259 set_bincount(counts);
261 }
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...

◆ set_bincount() [2/2]

template<int N, int ORDER, bool COMPACT>
void FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::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 same setup)

Definition at line 600 of file PolyspectrumBinning.h.

600 {
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 {
614 } while (std::next_permutation(cfg.begin(), cfg.end()));
615 }
616 }
618 }

◆ set_configurations()

template<int N, int ORDER, bool COMPACT = false>
void FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::set_configurations ( std::vector< Configuration > configurations)
inline

Empty restores all configurations. Inputs are canonicalized and deduplicated.

Definition at line 87 of file PolyspectrumBinning.h.

87 {
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 }

◆ stored_configurations()

template<int N, int ORDER, bool COMPACT = false>
const std::vector< Configuration > & FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::stored_configurations ( ) const
inline

Definition at line 144 of file PolyspectrumBinning.h.

144{ return compact_configs; }

◆ to_dense()

template<int N, int ORDER, bool COMPACT>
PolyspectrumBinning< N, ORDER, false > FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::to_dense ( )

Explicit dense allocation and symmetry expansion; never used by estimators.

Definition at line 621 of file PolyspectrumBinning.h.

621 {
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");
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 }
bool subtract_shotnoise
Subtract shotnoise? Algorithms (compute_polyspectrum) using this class checks this value.

◆ valid_storage()

template<int N, int ORDER, bool COMPACT = false>
bool FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::valid_storage ( ) const
inline

Definition at line 167 of file PolyspectrumBinning.h.

167 {
168 const size_t entries = COMPACT ? compact_configs.size() : dense_entries(n);
169 return P123.size() == entries && N123.size() == entries;
170 }

Friends And Related Symbol Documentation

◆ PolyspectrumBinning

template<int N, int ORDER, bool COMPACT = false>
template<int , int , bool >
friend class PolyspectrumBinning
friend

Definition at line 193 of file PolyspectrumBinning.h.

Field Documentation

◆ bin_type

template<int N, int ORDER, bool COMPACT = false>
int FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::bin_type {LINEAR_SPACING}

Type of bins (LINEAR_SPACING or LOG_SPACING)

Definition at line 52 of file PolyspectrumBinning.h.

◆ bincount_is_set

template<int N, int ORDER, bool COMPACT = false>
bool FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::bincount_is_set {false}

If we have precomputed the volume factor (N123) or not and have it available for algorithms to use.

Definition at line 78 of file PolyspectrumBinning.h.

78{false};

◆ compact_storage

template<int N, int ORDER, bool COMPACT = false>
bool FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::compact_storage = COMPACT
staticconstexpr

Definition at line 46 of file PolyspectrumBinning.h.

◆ dealias

template<int N, int ORDER, bool COMPACT = false>
bool FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::dealias {true}

True integer-wavevector closure.

False retains legacy modulo-grid closure. Nonlinear estimates exclude even-mesh Nyquist planes when enabled.

Definition at line 81 of file PolyspectrumBinning.h.

81{true};

◆ kbin

template<int N, int ORDER, bool COMPACT = false>
std::vector<double> FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::kbin

kbin is center of bin

Definition at line 69 of file PolyspectrumBinning.h.

◆ khigh

template<int N, int ORDER, bool COMPACT = false>
std::vector<double> FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::khigh

khigh is higher bin edge

Definition at line 73 of file PolyspectrumBinning.h.

◆ klow

template<int N, int ORDER, bool COMPACT = false>
std::vector<double> FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::klow

klow is lower bin edge

Definition at line 71 of file PolyspectrumBinning.h.

◆ kmax

template<int N, int ORDER, bool COMPACT = false>
double FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::kmax {0.0}

The maximum k to bin to.

Definition at line 56 of file PolyspectrumBinning.h.

56{0.0};

◆ kmean

template<int N, int ORDER, bool COMPACT = false>
std::vector<double> FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::kmean

kmean is the mean of wavenumbers added to the bin

Definition at line 75 of file PolyspectrumBinning.h.

◆ kmin

template<int N, int ORDER, bool COMPACT = false>
double FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::kmin {0.0}

The minimum k to bin from (should be 0!)

Definition at line 54 of file PolyspectrumBinning.h.

54{0.0};

◆ n

template<int N, int ORDER, bool COMPACT = false>
int FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::n {0}

Number of bins.

Definition at line 50 of file PolyspectrumBinning.h.

50{0};

◆ N123

template<int N, int ORDER, bool COMPACT = false>
std::vector<double> FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::N123

The volume (number of generalized triangles) of each bin.

Definition at line 64 of file PolyspectrumBinning.h.

◆ nbinnings

template<int N, int ORDER, bool COMPACT = false>
int FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::nbinnings {0}

Definition at line 265 of file PolyspectrumBinning.h.

265{0};

◆ options

template<int N, int ORDER, bool COMPACT = false>
PolyspectrumOptions FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::options

Definition at line 82 of file PolyspectrumBinning.h.

◆ P123

template<int N, int ORDER, bool COMPACT = false>
std::vector<double> FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::P123

The polyspectrum.

Definition at line 62 of file PolyspectrumBinning.h.

◆ pofk

template<int N, int ORDER, bool COMPACT = false>
std::vector<double> FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::pofk

The power-spectrum.

Definition at line 66 of file PolyspectrumBinning.h.

◆ statistics

template<int N, int ORDER, bool COMPACT = false>
PolyspectrumStatistics FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::statistics

Definition at line 83 of file PolyspectrumBinning.h.

◆ subtract_shotnoise

template<int N, int ORDER, bool COMPACT = false>
bool FML::CORRELATIONFUNCTIONS::PolyspectrumBinning< N, ORDER, COMPACT >::subtract_shotnoise {true}

Subtract shotnoise? Algorithms (compute_polyspectrum) using this class checks this value.

Definition at line 59 of file PolyspectrumBinning.h.

59{true};

The documentation for this class was generated from the following files: