1#ifndef POWERSPECTRUMBINNING_HEADER
2#define POWERSPECTRUMBINNING_HEADER
8 namespace CORRELATIONFUNCTIONS {
30 std::vector<double>
k;
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);
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];
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)) {
118 for (
int i = 0; i <
n; i++)
122 assert_mpi(omp_get_thread_num() == 0,
123 "[PowerSpectrumBinning] You cannot create a binning inside a parallel region\n");
127 for (
int i = 0; i <
NThreads; i++) {
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);
145 for (
int i = 0; i < n; i++)
146 printf(
"%10.5e %10.5e\n", k[i], pofk[i]);
147 printf(
"==================================\n");
157 const int index = get_bin_index(kvalue, kmin,
kmax, n, bin_type);
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;
166 if (0 <= index and index < n) {
167 count[index] += weight;
168 pofk[index] +=
power * weight;
169 kbin[index] += kvalue * weight;
176 for (
int i = 0; i < n; i++) {
177 count[i] = pofk[i] = kbin[i] = 0.0;
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;
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];
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);
209 for (
int i = 0; i < n; i++) {
219 for (
int i = 0; i < n; i++) {
222 pofk[i] *= std::pow(
boxsize, N);
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) {
235 index = int(std::floor(std::log(kvalue / kmin) / std::log(
kmax / kmin) * (n - 1) + 0.5));
237 assert_mpi(
false,
"[PowerSpectrumBinning::get_bin_index] Unknown binning type\n");
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);
250 assert_mpi(
false,
"[PowerSpectrumBinning::get_k_from_bin_index] Unknown binning type\n");
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
PowerSpectrumBinning()=default
std::vector< double > kbin
The mean k-value in the bin.
void info()
Print some info.
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 kmax
The kmax value to bin up to.
double kmin
The kmin value to bin from.
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< double > k
The bin edges.
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
std::vector< double > count
The bin count.
int bin_type
Bin spacing (LINEAR_SPACING or LOG_SPACING)
int NThreads
Total number of threads available.
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...
int ThisTask
The MPI task number.
MPI_Comm communicator
The MPI communicator used for all communication in the library (fiducial MPI_COMM_WORLD).