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