FML
Loading...
Searching...
No Matches
PowerSpectrumBinning.h
Go to the documentation of this file.
1#ifndef POWERSPECTRUMBINNING_HEADER
2#define POWERSPECTRUMBINNING_HEADER
3#include <FML/Global/Global.h>
4#include <cmath>
5#include <vector>
6
7namespace FML {
8 namespace CORRELATIONFUNCTIONS {
9
10 /// Class for holding the results after binning up a power-spectrum.
11 /// This class is thread-safe, but cannot be created or normalized inside a OMP parallel region
12 /// Summation over MPI tasks is done in normalize()
13 template <int N>
15 public:
17
18 /// Number of bins
19 int n{0};
20 /// Bin spacing (LINEAR_SPACING or LOG_SPACING)
22 /// The kmin value to bin from
23 double kmin{0.0};
24 /// The kmax value to bin up to
25 double kmax{0.0};
26 /// Subtract shotnoise or not (used by algorithms)
28
29 /// The bin edges
30 std::vector<double> k;
31 /// The mean k-value in the bin
32 std::vector<double> kbin;
33 /// The bin count
34 std::vector<double> count;
35 /// The power-spectrum in the bin
36 std::vector<double> pofk;
37
38#ifdef USE_OMP
39 // Temporary storage for OpenMP parallelization
40 std::vector<std::vector<double>> count_thread;
41 std::vector<std::vector<double>> pofk_thread;
42 std::vector<std::vector<double>> kbin_thread;
43#endif
44
47 PowerSpectrumBinning(double kmin, double kmax, int n, int bin_type);
48
49 /// Add two binnings together
51
52 /// Reset everything. Call before starting to bim
53 void reset();
54
55 /// Scale to physical units: k *= 1/Box and pofk *= Box^N
56 void scale(const double boxsize);
57
58 /// Print some info
59 void info();
60
61 /// Combine with another binning (just for testing to make it easier to bin up over realisations)
62 void combine(PowerSpectrumBinning & rhs);
63 int nbinnings{0};
64
65 /// Add a new point to a bin
66 void add_to_bin(double kvalue, double power, double weight = 1.0);
67
68 /// Normalize (i.e. find mean in each bin) Do summation over MPI tasks
69 void normalize();
70
71 /// From k to the index of the bin
72 int get_bin_index(double kvalue, double kmin, double kmax, int n, int bin_type);
73
74 /// From index of the bin get k
75 double get_k_from_bin_index(int index, double kmin, double kmax, int n, int bin_type);
76 };
77
78 template <int N>
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 }
91
92 template <int N>
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 }
108
109 template <int N>
111 : PowerSpectrumBinning(2.0 * M_PI, 2.0 * M_PI * nbins, nbins, LINEAR_SPACING) {}
112
113 template <int N>
114 PowerSpectrumBinning<N>::PowerSpectrumBinning(double kmin, double kmax, int n, int bin_type)
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 }
134
135 template <int N>
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 }
150
151 template <int N>
152 void PowerSpectrumBinning<N>::add_to_bin(double kvalue, double power, double weight) {
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 }
173
174 template <int N>
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 }
187
188 template <int N>
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 }
216
217 template <int N>
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 }
225
226 // This method can return values out of bounds (-1 for k below the first bin: floor, not truncation towards 0)
227 template <int N>
228 int PowerSpectrumBinning<N>::get_bin_index(double kvalue, double kmin, double kmax, int n, int bin_type) {
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 }
241
242 template <int N>
243 double PowerSpectrumBinning<N>::get_k_from_bin_index(int index, double kmin, double kmax, int n, int bin_type) {
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 }
254 } // namespace CORRELATIONFUNCTIONS
255} // namespace FML
256#endif
const double boxsize
Definition TestHDF5.cpp:47
Class for holding the results after binning up a power-spectrum.
bool subtract_shotnoise
Subtract shotnoise or not (used by algorithms)
void reset()
Reset everything. Call before starting to bim.
void add_to_bin(double kvalue, double power, double weight=1.0)
Add a new point to a bin.
std::vector< std::vector< double > > count_thread
std::vector< double > kbin
The mean k-value in the bin.
void scale(const double boxsize)
Scale to physical units: k *= 1/Box and pofk *= Box^N.
void combine(PowerSpectrumBinning &rhs)
Combine with another binning (just for testing to make it easier to bin up over realisations)
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
PowerSpectrumBinning & operator+=(const PowerSpectrumBinning &rhs)
Add two binnings together.
int get_bin_index(double kvalue, double kmin, double kmax, int n, int bin_type)
From k to the index of the bin.
void normalize()
Normalize (i.e. find mean in each bin) Do summation over MPI tasks.
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
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
int ThisTask
The MPI task number.
Definition Global.cpp:39
MPI_Comm communicator
The MPI communicator used for all communication in the library (fiducial MPI_COMM_WORLD).
Definition Global.cpp:26
const double kmax
Definition test_fnl.cpp:43