Triumvirate C++ API 0.5.0
Three-point clustering measurements in large-scale structure analyses.
Loading...
Searching...
No Matches
dataobjs.cpp
Go to the documentation of this file.
1// Copyright (C) [GPLv3 Licence]
2//
3// This file is part of the Triumvirate program. See the COPYRIGHT
4// and LICENCE files at the top-level directory of this distribution
5// for details of copyright and licensing.
6//
7// This program is free software: you can redistribute it and/or modify it
8// under the terms of the GNU General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15// See the GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <https://www.gnu.org/licenses/>.
19
27#include "dataobjs.hpp"
28
29namespace trvs = trv::sys;
30
31namespace trv {
32
33// ***********************************************************************
34// Binning schemes
35// ***********************************************************************
36
37Binning::Binning(std::string space, std::string scheme) :
38 space(space), scheme(scheme) {}
39
41 params.space, params.binning
42) {
43 this->bin_min = params.bin_min;
44 this->bin_max = params.bin_max;
45 this->num_bins = params.num_bins;
46
47 // Change default padding to grid scale.
48 this->dbin_pad_config = (1 + 5.e-3)
49 * *std::max_element(params.boxsize, params.boxsize + 3)
50 / *std::min_element(params.ngrid, params.ngrid + 3);
51 this->dbin_pad_fourier = (1 + 5.e-3)
52 * (2 * M_PI)
53 / *std::max_element(params.boxsize, params.boxsize + 3);
54}
55
56void Binning::set_bins(double coord_min, double coord_max, int nbin) {
57 if (coord_min < 0.) {
59 "Bin range must be non-negative."
60 );
61 }
62 if (nbin <= 0) {
64 "Number of bins must be positive."
65 );
66 }
67
68 this->bin_min = coord_min;
69 this->bin_max = coord_max;
70 this->num_bins = nbin;
71
72 this->set_bins();
73}
74
76 // Reset vector attributes.
77 this->bin_edges.clear();
78 this->bin_centres.clear();
79 this->bin_widths.clear();
80
81 this->compute_binning();
82}
83
84void Binning::set_bins(double boxsize_max, int ngrid_min) {
85 this->scheme = "lin";
86
87 this->bin_min = 0.;
88
89 double bin_width = 0.;
90 if (this->space == "config") {
91 bin_width = boxsize_max / double(ngrid_min);
92 this->bin_max = boxsize_max / 2.;
93 } else
94 if (this->space == "fourier") {
95 bin_width = 2*M_PI / boxsize_max;
96 this->bin_max = M_PI * double(ngrid_min) / boxsize_max;
97 }
98 this->bin_max += bin_width / 2.;
99
100 this->num_bins = ngrid_min / 2;
101
102 // Reset vector attributes.
103 this->bin_edges.clear();
104 this->bin_centres.clear();
105 this->bin_widths.clear();
106
107 this->compute_binning();
108}
109
110void Binning::set_bins(std::vector<double> bin_edges) {
111 this->bin_min = bin_edges.front();
112 this->bin_max = bin_edges.back();
113 this->num_bins = bin_edges.size() - 1;
114
115 // Reset attributes.
116 this->bin_edges.clear();
117 this->bin_centres.clear();
118 this->bin_widths.clear();
119 this->scheme = "custom";
120
121 this->bin_edges = bin_edges;
122
123 for (int ibin = 0; ibin < this->num_bins; ibin++) {
124 double centre = (this->bin_edges[ibin] + this->bin_edges[ibin + 1]) / 2.;
125 double width = this->bin_edges[ibin + 1] - this->bin_edges[ibin];
126
127 this->bin_centres.push_back(centre);
128 this->bin_widths.push_back(width);
129 }
130}
131
132void Binning::compute_binning() {
133 // Set up padding parameters.
134 double dbin_pad;
135 if (this->space == "fourier") {
136 dbin_pad = this->dbin_pad_fourier;
137 } else
138 if (this->space == "config") {
139 dbin_pad = this->dbin_pad_config;
140 }
141
142 // Implement binning scheme.
143
144 // ---------------------------------------------------------------------
145 // Linear binning
146 // ---------------------------------------------------------------------
147 if (scheme == "lin") {
148 double dbin = (this->bin_max - this->bin_min) / double(this->num_bins);
149
150 for (int ibin = 0; ibin < this->num_bins; ibin++) {
151 double edge_left = this->bin_min + dbin * ibin;
152 double centre = edge_left + dbin / 2.;
153
154 this->bin_edges.push_back(edge_left);
155 this->bin_centres.push_back(centre);
156 this->bin_widths.push_back(dbin);
157 }
158 this->bin_edges.push_back(this->bin_max);
159 } else
160 // ---------------------------------------------------------------------
161 // Logarithmic binning
162 // ---------------------------------------------------------------------
163 if (scheme == "log") {
164 if (this->bin_min == 0.) {
166 "Cannot use logarithmic binning when the lowest edge is zero."
167 );
168 }
169
170 double dlnbin = (std::log(this->bin_max) - std::log(this->bin_min))
171 / double(this->num_bins);
172
173 for (int ibin = 0; ibin < this->num_bins; ibin++) {
174 double edge_left = this->bin_min * std::exp(dlnbin * ibin);
175 double edge_right = this->bin_min * std::exp(dlnbin * (ibin + 1));
176 double centre = (edge_left + edge_right) / 2.;
177
178 this->bin_edges.push_back(edge_left);
179 this->bin_centres.push_back(centre);
180 this->bin_widths.push_back(edge_right - edge_left);
181 }
182 this->bin_edges.push_back(this->bin_max);
183 } else
184 // ---------------------------------------------------------------------
185 // Padded linear binning
186 // ---------------------------------------------------------------------
187 if (scheme == "linpad") {
188 for (int ibin = 0; ibin < this->nbin_pad; ibin++) {
189 double edge_left = dbin_pad * ibin;
190 double centre = edge_left + dbin_pad / 2.;
191
192 this->bin_edges.push_back(edge_left);
193 this->bin_centres.push_back(centre);
194 this->bin_widths.push_back(dbin_pad);
195 }
196
197 double bin_min = dbin_pad * this->nbin_pad;
198
199 double dbin = (this->bin_max - bin_min)
200 / double(this->num_bins - this->nbin_pad);
201
202 for (int ibin = this->nbin_pad; ibin < this->num_bins; ibin++) {
203 double edge_left = bin_min + dbin * (ibin - this->nbin_pad);
204 double centre = edge_left + dbin / 2.;
205
206 this->bin_edges.push_back(edge_left);
207 this->bin_centres.push_back(centre);
208 this->bin_widths.push_back(dbin);
209 }
210 this->bin_edges.push_back(this->bin_max);
211 } else
212 // ---------------------------------------------------------------------
213 // Padded logarithmic binning
214 // ---------------------------------------------------------------------
215 if (scheme == "logpad") {
216 for (int ibin = 0; ibin < this->nbin_pad; ibin++) {
217 double edge_left = dbin_pad * ibin;
218 double centre = edge_left + dbin_pad / 2.;
219
220 this->bin_edges.push_back(edge_left);
221 this->bin_centres.push_back(centre);
222 this->bin_widths.push_back(dbin_pad);
223 }
224
225 double bin_min = dbin_pad * this->nbin_pad;
226
227 double dlnbin = (std::log(this->bin_max) - std::log(bin_min))
228 / double(this->num_bins - this->nbin_pad);
229
230 for (int ibin = this->nbin_pad; ibin < this->num_bins; ibin++) {
231 double edge_left =
232 bin_min * std::exp(dlnbin * (ibin - this->nbin_pad));
233 double edge_right =
234 bin_min * std::exp(dlnbin * (ibin - this->nbin_pad + 1));
235 double centre = (edge_left + edge_right) / 2.;
236
237 this->bin_edges.push_back(edge_left);
238 this->bin_centres.push_back(centre);
239 this->bin_widths.push_back(edge_right - edge_left);
240 }
241 this->bin_edges.push_back(this->bin_max);
242 } else {
244 "Unrecognised/unsupported binning `scheme`: %s.", scheme.c_str()
245 );
246 }
247}
248
249
250// ***********************************************************************
251// Mesh grids
252// ***********************************************************************
253
254
255// ***********************************************************************
256// Line of sight
257// ***********************************************************************
258
259
260// ***********************************************************************
261// Clustering statistics
262// ***********************************************************************
263
264} // namespace trv
Isotropic coordinate binning.
Definition dataobjs.hpp:56
std::vector< double > bin_widths
bin widths
Definition dataobjs.hpp:65
std::vector< double > bin_edges
bin edges
Definition dataobjs.hpp:63
Binning(std::string space, std::string scheme)
Construct binnng from bin specification.
Definition dataobjs.cpp:37
std::vector< double > bin_centres
bin centres
Definition dataobjs.hpp:64
std::string scheme
binning scheme
Definition dataobjs.hpp:59
double bin_min
lowest bin edge
Definition dataobjs.hpp:60
std::string space
coordinate space
Definition dataobjs.hpp:58
double bin_max
highest bin edge
Definition dataobjs.hpp:61
void set_bins()
Set bins.
Definition dataobjs.cpp:75
int num_bins
number of bins
Definition dataobjs.hpp:62
Parameter set.
double bin_min
measurement range minimum (in Mpc/h or h/Mpc)
int num_bins
number of measurement bins
int ngrid[3]
grid number in each dimension
double boxsize[3]
box size (in Mpc/h) in each dimension
double bin_max
measurement range maximum (in Mpc/h or h/Mpc)
Exception raised when parameters are invalid.
Definition monitor.hpp:432
Clustering measurement data objects.