FML
Loading...
Searching...
No Matches
FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N > Class Template Reference

Class for holding the results after binning up a power-spectrum. More...

#include <PowerSpectrumBinning.h>

Public Types

enum  BinningType { LINEAR_SPACING , LOG_SPACING }
 

Public Member Functions

 PowerSpectrumBinning ()=default
 
 PowerSpectrumBinning (int n)
 
 PowerSpectrumBinning (double kmin, double kmax, int n, int bin_type)
 
PowerSpectrumBinning & operator+= (const PowerSpectrumBinning &rhs)
 Add two binnings together.
 
void reset ()
 Reset everything. Call before starting to bim.
 
void scale (const double boxsize)
 Scale to physical units: k *= 1/Box and pofk *= Box^N.
 
void info ()
 Print some info.
 
void combine (PowerSpectrumBinning &rhs)
 Combine with another binning (just for testing to make it easier to bin up over realisations)
 
void add_to_bin (double kvalue, double power, double weight=1.0)
 Add a new point to a bin.
 
void normalize ()
 Normalize (i.e. find mean in each bin) Do summation over MPI tasks.
 
int get_bin_index (double kvalue, double kmin, double kmax, int n, int bin_type)
 From k to the index of the bin.
 
double get_k_from_bin_index (int index, double kmin, double kmax, int n, int bin_type)
 From index of the bin get k.
 

Data Fields

int n {0}
 Number of bins.
 
int bin_type {LINEAR_SPACING}
 Bin spacing (LINEAR_SPACING or LOG_SPACING)
 
double kmin {0.0}
 The kmin value to bin from.
 
double kmax {0.0}
 The kmax value to bin up to.
 
bool subtract_shotnoise {true}
 Subtract shotnoise or not (used by algorithms)
 
std::vector< double > k
 The bin edges.
 
std::vector< double > kbin
 The mean k-value in the bin.
 
std::vector< double > count
 The bin count.
 
std::vector< double > pofk
 The power-spectrum in the bin.
 
std::vector< std::vector< double > > count_thread
 
std::vector< std::vector< double > > pofk_thread
 
std::vector< std::vector< double > > kbin_thread
 
int nbinnings {0}
 

Detailed Description

template<int N>
class FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >

Class for holding the results after binning up a power-spectrum.

This class is thread-safe, but cannot be created or normalized inside a OMP parallel region Summation over MPI tasks is done in normalize()

Definition at line 14 of file PowerSpectrumBinning.h.

Member Enumeration Documentation

◆ BinningType

Constructor & Destructor Documentation

◆ PowerSpectrumBinning() [1/3]

template<int N>
FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::PowerSpectrumBinning ( )
default

◆ PowerSpectrumBinning() [2/3]

template<int N>
FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::PowerSpectrumBinning ( int n)

Definition at line 110 of file PowerSpectrumBinning.h.

111 : PowerSpectrumBinning(2.0 * M_PI, 2.0 * M_PI * nbins, nbins, LINEAR_SPACING) {}

◆ PowerSpectrumBinning() [3/3]

template<int N>
FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::PowerSpectrumBinning ( double kmin,
double kmax,
int n,
int bin_type )

Definition at line 114 of file PowerSpectrumBinning.h.

115 : n(n), bin_type(bin_type), kmin(kmin), kmax(kmax), k(std::vector<double>(n, 0.0)),
116 kbin(std::vector<double>(n, 0.0)), count(std::vector<double>(n, 0.0)), pofk(std::vector<double>(n, 0.0)) {
117
118 for (int i = 0; i < n; i++)
120
121#ifdef USE_OMP
122 assert_mpi(omp_get_thread_num() == 0,
123 "[PowerSpectrumBinning] You cannot create a binning inside a parallel region\n");
124 count_thread.resize(NThreads);
125 pofk_thread.resize(NThreads);
126 kbin_thread.resize(NThreads);
127 for (int i = 0; i < NThreads; i++) {
128 count_thread[i] = std::vector<double>(n, 0.0);
129 pofk_thread[i] = std::vector<double>(n, 0.0);
130 kbin_thread[i] = std::vector<double>(n, 0.0);
131 }
132#endif
133 }
std::vector< std::vector< double > > count_thread
std::vector< double > kbin
The mean k-value in the bin.
double get_k_from_bin_index(int index, double kmin, double kmax, int n, int bin_type)
From index of the bin get k.
std::vector< std::vector< double > > kbin_thread
std::vector< double > pofk
The power-spectrum in the bin.
std::vector< std::vector< double > > pofk_thread
int bin_type
Bin spacing (LINEAR_SPACING or LOG_SPACING)
int NThreads
Total number of threads available.
Definition Global.cpp:41

Member Function Documentation

◆ add_to_bin()

template<int N>
void FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::add_to_bin ( double kvalue,
double power,
double weight = 1.0 )

Add a new point to a bin.

Definition at line 152 of file PowerSpectrumBinning.h.

152 {
153
154 // Do not include zero-mode
155 if (kvalue == 0.0)
156 return;
157 const int index = get_bin_index(kvalue, kmin, kmax, n, bin_type);
158#ifdef USE_OMP
159 const int myid = NThreads == 1 ? 0 : omp_get_thread_num();
160 if (0 <= index and index < n) {
161 count_thread[myid][index] += weight;
162 pofk_thread[myid][index] += power * weight;
163 kbin_thread[myid][index] += kvalue * weight;
164 }
165#else
166 if (0 <= index and index < n) {
167 count[index] += weight;
168 pofk[index] += power * weight;
169 kbin[index] += kvalue * weight;
170 }
171#endif
172 }
int get_bin_index(double kvalue, double kmin, double kmax, int n, int bin_type)
From k to the index of the bin.
constexpr long long int power(int base, int exponent)
Simple integer a^b power-function by squaring (b >= 0; returns 0 for b < 0 instead of recursing forev...
Definition Global.h:287

◆ combine()

template<int N>
void FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::combine ( PowerSpectrumBinning< N > & rhs)

Combine with another binning (just for testing to make it easier to bin up over realisations)

Definition at line 79 of file PowerSpectrumBinning.h.

79 {
80 if (nbinnings == 0) {
81 count = rhs.count;
82 pofk = rhs.pofk;
83 } else {
84 for (int i = 0; i < n; i++)
85 count[i] += rhs.count[i];
86 for (int i = 0; i < n; i++)
87 pofk[i] = (pofk[i] * nbinnings + rhs.pofk[i]) / (nbinnings + 1.0);
88 }
89 nbinnings++;
90 }
rhs(lna, y)

◆ get_bin_index()

template<int N>
int FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::get_bin_index ( double kvalue,
double kmin,
double kmax,
int n,
int bin_type )

From k to the index of the bin.

Definition at line 228 of file PowerSpectrumBinning.h.

228 {
229 int index = -1;
230 if (bin_type == LINEAR_SPACING) {
231 index = int(std::floor((kvalue - kmin) / (kmax - kmin) * (n - 1) + 0.5));
232 } else if (bin_type == LOG_SPACING) {
233 if (kvalue <= 0.0)
234 return -1;
235 index = int(std::floor(std::log(kvalue / kmin) / std::log(kmax / kmin) * (n - 1) + 0.5));
236 } else {
237 assert_mpi(false, "[PowerSpectrumBinning::get_bin_index] Unknown binning type\n");
238 }
239 return index;
240 }

◆ get_k_from_bin_index()

template<int N>
double FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::get_k_from_bin_index ( int index,
double kmin,
double kmax,
int n,
int bin_type )

From index of the bin get k.

Definition at line 243 of file PowerSpectrumBinning.h.

243 {
244 double kvalue = 0.0;
245 if (bin_type == LINEAR_SPACING) {
246 kvalue = kmin + (kmax - kmin) / double(n - 1) * index;
247 } else if (bin_type == LOG_SPACING) {
248 kvalue = std::exp(std::log(kmin) + std::log(kmax / kmin) / double(n - 1) * index);
249 } else {
250 assert_mpi(false, "[PowerSpectrumBinning::get_k_from_bin_index] Unknown binning type\n");
251 }
252 return kvalue;
253 }

◆ info()

template<int N>
void FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::info ( )

Print some info.

Definition at line 136 of file PowerSpectrumBinning.h.

136 {
137 if (ThisTask == 0) {
138 printf("\n==================================\n");
139 printf("PowerSpectrumBinning Info:\n");
140 printf("nbins: %i\n", n);
141 printf("kmin: %10.5e\n", kmin);
142 printf("kmax: %10.5e\n", kmax);
143 printf("BinType %i\n", bin_type);
144 printf("Data: \n");
145 for (int i = 0; i < n; i++)
146 printf("%10.5e %10.5e\n", k[i], pofk[i]);
147 printf("==================================\n");
148 }
149 }
int ThisTask
The MPI task number.
Definition Global.cpp:39

◆ normalize()

template<int N>
void FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::normalize ( )

Normalize (i.e. find mean in each bin) Do summation over MPI tasks.

Definition at line 189 of file PowerSpectrumBinning.h.

189 {
190
191#ifdef USE_OMP
192 assert_mpi(omp_get_thread_num() == 0,
193 "[PowerSpectrumBinning::normalize] This method can only be run by the main thread\n");
194 for (int id = 0; id < NThreads; id++) {
195 for (int i = 0; i < n; i++) {
196 count[i] += count_thread[id][i];
197 pofk[i] += pofk_thread[id][i];
198 kbin[i] += kbin_thread[id][i];
199 }
200 }
201#endif
202
203#ifdef USE_MPI
204 MPI_Allreduce(MPI_IN_PLACE, pofk.data(), n, MPI_DOUBLE, MPI_SUM, FML::communicator);
205 MPI_Allreduce(MPI_IN_PLACE, count.data(), n, MPI_DOUBLE, MPI_SUM, FML::communicator);
206 MPI_Allreduce(MPI_IN_PLACE, kbin.data(), n, MPI_DOUBLE, MPI_SUM, FML::communicator);
207#endif
208
209 for (int i = 0; i < n; i++) {
210 if (count[i] > 0) {
211 pofk[i] /= count[i];
212 kbin[i] /= count[i];
213 }
214 }
215 }
MPI_Comm communicator
The MPI communicator used for all communication in the library (fiducial MPI_COMM_WORLD).
Definition Global.cpp:26

◆ operator+=()

template<int N>
PowerSpectrumBinning< N > & FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::operator+= ( const PowerSpectrumBinning< N > & rhs)

Add two binnings together.

Definition at line 93 of file PowerSpectrumBinning.h.

93 {
94 assert_mpi(k.size() == rhs.k.size(), "[PowerSpectrumBinning::operator +=] k has wrong size\n");
95 assert_mpi(count.size() == rhs.count.size(), "[PowerSpectrumBinning::operator +=] count has wrong size\n");
96 assert_mpi(pofk.size() == rhs.pofk.size(), "[PowerSpectrumBinning::operator +=] pofk has wrong size\n");
97 assert_mpi(kbin.size() == rhs.kbin.size(), "[PowerSpectrumBinning::operator +=] kbin has wrong size\n");
98 assert_mpi(bin_type == rhs.bin_type, "[PowerSpectrumBinning::operator +=] bin_type differs\n");
99 assert_mpi(kmin == rhs.kmin, "[PowerSpectrumBinning::operator +=] kmin differs\n");
100 assert_mpi(kmax == rhs.kmax, "[PowerSpectrumBinning::operator +=] kmax differs\n");
101 for (int i = 0; i < n; i++) {
102 count[i] += rhs.count[i];
103 pofk[i] += rhs.pofk[i];
104 kbin[i] += rhs.kbin[i];
105 }
106 return *this;
107 }

◆ reset()

template<int N>
void FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::reset ( )

Reset everything. Call before starting to bim.

Definition at line 175 of file PowerSpectrumBinning.h.

175 {
176 for (int i = 0; i < n; i++) {
177 count[i] = pofk[i] = kbin[i] = 0.0;
178 }
179#ifdef USE_OMP
180 for (int id = 0; id < NThreads; id++) {
181 for (int i = 0; i < n; i++) {
182 count_thread[id][i] = pofk_thread[id][i] = kbin_thread[id][i] = 0.0;
183 }
184 }
185#endif
186 }

◆ scale()

template<int N>
void FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::scale ( const double boxsize)

Scale to physical units: k *= 1/Box and pofk *= Box^N.

Definition at line 218 of file PowerSpectrumBinning.h.

218 {
219 for (int i = 0; i < n; i++) {
220 k[i] *= 1.0 / boxsize;
221 kbin[i] *= 1.0 / boxsize;
222 pofk[i] *= std::pow(boxsize, N);
223 }
224 }
const double boxsize
Definition TestHDF5.cpp:47

Field Documentation

◆ bin_type

Bin spacing (LINEAR_SPACING or LOG_SPACING)

Definition at line 21 of file PowerSpectrumBinning.h.

◆ count

template<int N>
std::vector<double> FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::count

The bin count.

Definition at line 34 of file PowerSpectrumBinning.h.

◆ count_thread

template<int N>
std::vector<std::vector<double> > FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::count_thread

Definition at line 40 of file PowerSpectrumBinning.h.

◆ k

template<int N>
std::vector<double> FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::k

The bin edges.

Definition at line 30 of file PowerSpectrumBinning.h.

◆ kbin

template<int N>
std::vector<double> FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::kbin

The mean k-value in the bin.

Definition at line 32 of file PowerSpectrumBinning.h.

◆ kbin_thread

template<int N>
std::vector<std::vector<double> > FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::kbin_thread

Definition at line 42 of file PowerSpectrumBinning.h.

◆ kmax

template<int N>
double FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::kmax {0.0}

The kmax value to bin up to.

Definition at line 25 of file PowerSpectrumBinning.h.

25{0.0};

◆ kmin

template<int N>
double FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::kmin {0.0}

The kmin value to bin from.

Definition at line 23 of file PowerSpectrumBinning.h.

23{0.0};

◆ n

template<int N>
int FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::n {0}

Number of bins.

Definition at line 19 of file PowerSpectrumBinning.h.

19{0};

◆ nbinnings

template<int N>
int FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::nbinnings {0}

Definition at line 63 of file PowerSpectrumBinning.h.

63{0};

◆ pofk

template<int N>
std::vector<double> FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::pofk

The power-spectrum in the bin.

Definition at line 36 of file PowerSpectrumBinning.h.

◆ pofk_thread

template<int N>
std::vector<std::vector<double> > FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::pofk_thread

Definition at line 41 of file PowerSpectrumBinning.h.

◆ subtract_shotnoise

template<int N>
bool FML::CORRELATIONFUNCTIONS::PowerSpectrumBinning< N >::subtract_shotnoise {true}

Subtract shotnoise or not (used by algorithms)

Definition at line 27 of file PowerSpectrumBinning.h.

27{true};

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