Triumvirate C++ API 0.5.0.post1.dev301+g026f21751
Three-point clustering measurements in large-scale structure analyses.
Loading...
Searching...
No Matches
field.hpp
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
38
39#ifndef TRIUMVIRATE_INCLUDE_FIELD_HPP_INCLUDED_
40#define TRIUMVIRATE_INCLUDE_FIELD_HPP_INCLUDED_
41
42#if defined(TRV_USE_HIP)
43#include <hipfft/hipfftXt.h>
44#elif defined(TRV_USE_CUDA) // !TRV_USE_HIP && TRV_USE_CUDA
45#include <cufftXt.h>
46#endif // TRV_USE_HIP
47#include <fftw3.h>
48
49#include <chrono>
50#include <cmath>
51#include <complex>
52#include <functional>
53#include <vector>
54
55#include "arrayops.hpp"
56#include "monitor.hpp"
57#include "parameters.hpp"
58#include "maths.hpp"
59#include "dataobjs.hpp"
60#include "io.hpp"
61#include "particles.hpp"
62
64#ifdef __GNUC__
65#define PURE __attribute__((pure))
66#else
67#define PURE
68#endif
70
71namespace trvm = trv::maths;
72
73namespace trv {
74
75// ***********************************************************************
76// Mesh field
77// ***********************************************************************
78
83class MeshField {
84 public:
86 std::string name;
87 fftw_complex* field;
88 double dr[3];
89 double dk[3];
90 double vol;
91 double vol_cell;
92
93 // ---------------------------------------------------------------------
94 // Life cycle
95 // ---------------------------------------------------------------------
96
105 explicit MeshField(
107 bool plan_ini = true,
108 const std::string& name = "mesh-field"
109 );
110
124 explicit MeshField(
126 fftw_plan& transform, fftw_plan& inv_transform,
127 const std::string& name = "mesh-field"
128 );
129
133 ~MeshField();
134
141 void reset_density_field();
142
143 // ---------------------------------------------------------------------
144 // Operators & reserved methods
145 // ---------------------------------------------------------------------
146
153 PURE const fftw_complex& operator[](long long gid);
154
155 // ---------------------------------------------------------------------
156 // Mesh assignment
157 // ---------------------------------------------------------------------
158
166 ParticleCatalogue& particles, fftw_complex* weights
167 );
168
169 // ---------------------------------------------------------------------
170 // Field computations
171 // ---------------------------------------------------------------------
172
184
202 ParticleCatalogue& particles
203 );
204
238 ParticleCatalogue& particles_data, ParticleCatalogue& particles_rand,
239 LineOfSight* los_data, LineOfSight* los_rand,
240 double alpha, int ell, int m
241 );
242
256 ParticleCatalogue& particles, LineOfSight* los,
257 double alpha, int ell, int m
258 );
259
292 ParticleCatalogue& particles_data, ParticleCatalogue& particles_rand,
293 LineOfSight* los_data, LineOfSight* los_rand,
294 double alpha, int ell, int m
295 );
296
310 ParticleCatalogue& particles, LineOfSight* los,
311 double alpha, int ell, int m
312 );
313
314 // ---------------------------------------------------------------------
315 // Field transforms
316 // ---------------------------------------------------------------------
317
326 void fourier_transform();
327
332
333 // ---------------------------------------------------------------------
334 // Field operations
335 // ---------------------------------------------------------------------
336
344
350
351 // ---------------------------------------------------------------------
352 // One-point statistics
353 // ---------------------------------------------------------------------
354
379 MeshField& field_fourier, std::vector< std::complex<double> >& ylm,
380 double k_band, double dk_band,
381 double& k_eff, int& nmodes
382 );
383
405 MeshField& field_fourier,
406 std::vector< std::complex<double> >& ylm,
408 double r
409 );
410
411 // ---------------------------------------------------------------------
412 // Misc
413 // ---------------------------------------------------------------------
414
426 double calc_grid_based_powlaw_norm(ParticleCatalogue& particles, int order);
427
428 private:
430 std::vector<double> window;
432 int window_assign_order = -1;
433
435 fftw_complex* field_s = nullptr;
436
438 fftw_plan transform{};
439 fftw_plan transform_s{};
440 fftw_plan inv_transform{};
441#if defined(TRV_USE_HIP) || defined(TRV_USE_CUDA)
442 fftHandle transform_gpu{};
443#endif // TRV_USE_HIP || TRV_USE_CUDA
444
446#if defined(TRV_USE_HIP) || defined(TRV_USE_CUDA)
447 fft_double_complex* d_field = nullptr;
448#endif // TRV_USE_HIP || TRV_USE_CUDA
449#ifdef TRV_USE_CUDA
451 cudaLibXtDesc* field_desc = nullptr;
452#endif // TRV_USE_CUDA
453
454#ifdef _CUDA_STREAM
456 cudaStream_t custream{};
457#endif // _CUDA_STREAM
458
459 bool plan_ini = false;
460 bool plan_ext = false;
461
462 friend class FieldStats;
463
464 // ---------------------------------------------------------------------
465 // Mesh grid properties
466 // ---------------------------------------------------------------------
467
474 PURE long long ret_grid_index(int i, int j, int k);
475
481 void shift_grid_indices_fourier(int& i, int& j, int& k);
482
489 void get_grid_pos_vector(int i, int j, int k, double rvec[3]);
490
497 void get_grid_wavevector(int i, int j, int k, double kvec[3]);
498
499 // ---------------------------------------------------------------------
500 // Mesh assignment
501 // ---------------------------------------------------------------------
502
510 void assign_weighted_field_to_mesh_ngp(
511 ParticleCatalogue& particles, fftw_complex* weight
512 );
513
521 void assign_weighted_field_to_mesh_cic(
522 ParticleCatalogue& particles, fftw_complex* weight
523 );
524
532 void assign_weighted_field_to_mesh_tsc(
533 ParticleCatalogue& particles, fftw_complex* weight
534 );
535
543 void assign_weighted_field_to_mesh_pcs(
544 ParticleCatalogue& particles, fftw_complex* weight
545 );
546
556 double calc_assignment_window_in_fourier(int i, int j, int k, int order = 0);
557
566 void compute_assignment_window_in_fourier(int order = 0);
567};
568
569
570// ***********************************************************************
571// Field statistics
572// ***********************************************************************
573
583 public:
584 std::vector<int> nmodes;
585 std::vector<int> npairs;
586 std::vector<double> k;
587 std::vector<double> r;
589 std::vector< std::complex<double> > sn;
591 std::vector< std::complex<double> > pk;
593 std::vector< std::complex<double> > xi;
594
595 // ---------------------------------------------------------------------
596 // Life cycle
597 // ---------------------------------------------------------------------
598
606 explicit FieldStats(trv::ParameterSet& params, bool plan_ini = true);
607
611 ~FieldStats();
612
616 void reset_stats();
617
618 // ---------------------------------------------------------------------
619 // Binned statistics
620 // ---------------------------------------------------------------------
621
653 MeshField& field_a, MeshField& field_b, std::complex<double> shotnoise_amp,
654 int ell, int m, trv::Binning& kbinning
655 );
656
675 MeshField& field_a, MeshField& field_b, std::complex<double> shotnoise_amp,
676 int ell, int m, trv::Binning& rbinning
677 );
678
712 MeshField& field_a, MeshField& field_b,
713 std::vector< std::complex<double> >& ylm_a,
714 std::vector< std::complex<double> >& ylm_b,
715 std::complex<double> shotnoise_amp,
716 trv::Binning& rbinning
717 );
718
757 MeshField& field_a, MeshField& field_b,
758 std::vector< std::complex<double> >& ylm_a,
759 std::vector< std::complex<double> >& ylm_b,
762 std::complex<double> shotnoise_amp,
763 double k_a, double k_b
764 );
765
773 trv::Binning& binning, const std::string& save_file
774 );
775
776 private:
777 trv::ParameterSet params;
778 double dr[3];
779 double dk[3];
780 double vol;
781 double vol_cell;
782
784 fftw_plan inv_transform{};
785#if defined(TRV_USE_HIP) || defined(TRV_USE_CUDA)
786 fftHandle inv_transform_gpu{};
787#endif // TRV_USE_HIP || TRV_USE_CUDA
788
790 fftw_complex* twopt_3d = nullptr;
791#if defined(TRV_USE_HIP) || defined(TRV_USE_CUDA)
792 fft_double_complex* d_twopt_3d = nullptr;
793#endif // TRV_USE_HIP || TRV_USE_CUDA
794#ifdef TRV_USE_CUDA
796 cudaLibXtDesc* twopt_3d_desc = nullptr;
797#endif // TRV_USE_CUDA
798
799#ifdef _CUDA_STREAM
801 cudaStream_t custream{};
802#endif // _CUDA_STREAM
803
805 bool plan_ini = false;
806
808 std::vector<double> alias_sn;
809
811 bool alias_ini = false;
812
813 // ---------------------------------------------------------------------
814 // Utilities
815 // ---------------------------------------------------------------------
816
824 PURE bool if_fields_compatible(MeshField& field_a, MeshField& field_b);
825
831 void resize_stats(int num_bins);
832
841 PURE long long ret_grid_index(int i, int j, int k);
842
850 void shift_grid_indices_fourier(int& i, int& j, int& k);
851
852 // ---------------------------------------------------------------------
853 // Sampling corrections
854 // ---------------------------------------------------------------------
855
867 std::function<double(int, int, int)> ret_calc_shotnoise_aliasing();
868
876 void get_shotnoise_aliasing_sin2(
877 int i, int j, int k, double& sx2, double& sy2, double& sz2
878 );
879
887 void get_shotnoise_aliasing_sin2_cos2_isoapprox(
888 int i, int j, int k, double& s2h, double& c2h
889 );
890
898 double calc_shotnoise_aliasing_ngp(int i, int j, int k);
899
907 double calc_shotnoise_aliasing_cic(int i, int j, int k);
908
916 double calc_shotnoise_aliasing_tsc(int i, int j, int k);
917
925 double calc_shotnoise_aliasing_pcs(int i, int j, int k);
926
935 double calc_shotnoise_aliasing_interlaced_isoapprox(
936 int i, int j, int k, int order
937 );
938
943 void compute_shotnoise_aliasing();
944};
945
946} // namespace trv
947
948#endif // !TRIUMVIRATE_INCLUDE_FIELD_HPP_INCLUDED_
Array operations.
Isotropic coordinate binning.
Definition dataobjs.hpp:58
std::vector< std::complex< double > > sn
shot-noise power in bins
Definition field.hpp:589
void reset_stats()
Reset two-point statistics.
Definition field.cpp:2248
std::vector< std::complex< double > > pk
pseudo power spectrum in bins
Definition field.hpp:591
std::vector< double > r
Definition field.hpp:587
void compute_uncoupled_shotnoise_for_3pcf(MeshField &field_a, MeshField &field_b, std::vector< std::complex< double > > &ylm_a, std::vector< std::complex< double > > &ylm_b, std::complex< double > shotnoise_amp, trv::Binning &rbinning)
Compute binned uncoupled three-point correlation function shot noise.
Definition field.cpp:2947
std::complex< double > compute_uncoupled_shotnoise_for_bispec_per_bin(MeshField &field_a, MeshField &field_b, std::vector< std::complex< double > > &ylm_a, std::vector< std::complex< double > > &ylm_b, trvm::SphericalBesselCalculator &sj_a, trvm::SphericalBesselCalculator &sj_b, std::complex< double > shotnoise_amp, double k_a, double k_b)
Compute unbinned uncoupled bispectrum shot noise on the FFT mesh grid.
Definition field.cpp:3199
~FieldStats()
Destruct two-point statistics.
Definition field.cpp:2193
std::vector< int > nmodes
number of wavevector modes in bins
Definition field.hpp:584
FieldStats(trv::ParameterSet &params, bool plan_ini=true)
Construct pseudo two-point statistics.
Definition field.cpp:2076
std::vector< double > k
average wavenumber in bins
Definition field.hpp:586
std::vector< std::complex< double > > xi
pseudo two-point correlation function in bins
Definition field.hpp:593
std::vector< int > npairs
number of separation pairs in bins
Definition field.hpp:585
void compute_ylm_wgtd_2pt_stats_in_fourier(MeshField &field_a, MeshField &field_b, std::complex< double > shotnoise_amp, int ell, int m, trv::Binning &kbinning)
Compute binned two-point statistics in Fourier space.
Definition field.cpp:2511
trv::BinnedVectors record_binned_vectors(trv::Binning &binning, const std::string &save_file)
Record binned vectors given a binning scheme.
Definition field.cpp:2317
void compute_ylm_wgtd_2pt_stats_in_config(MeshField &field_a, MeshField &field_b, std::complex< double > shotnoise_amp, int ell, int m, trv::Binning &rbinning)
Compute binned two-point statistics in configuration space.
Definition field.cpp:2705
Discretely sampled field on a mesh grid from particle catalogues.
Definition field.hpp:83
trv::ParameterSet params
parameter set
Definition field.hpp:85
void inv_fourier_transform()
Inverse Fourier transform the (FFT-transformed) field.
Definition field.cpp:1657
friend class FieldStats
Definition field.hpp:462
void inv_fourier_transform_sjl_ylm_wgtd_field(MeshField &field_fourier, std::vector< std::complex< double > > &ylm, trvm::SphericalBesselCalculator &sjl, double r)
Inverse Fourier transform a field weighted by the spherical Bessel function and reduced spherical ha...
Definition field.cpp:1908
std::string name
field name
Definition field.hpp:86
void assign_weighted_field_to_mesh(ParticleCatalogue &particles, fftw_complex *weights)
Assign a weighted field to a mesh by interpolation scheme.
Definition field.cpp:569
double calc_grid_based_powlaw_norm(ParticleCatalogue &particles, int order)
Definition field.cpp:2017
void reset_density_field()
(Re-)initialise the complex field (and its shadow) on mesh.
Definition field.cpp:500
void inv_fourier_transform_ylm_wgtd_field_band_limited(MeshField &field_fourier, std::vector< std::complex< double > > &ylm, double k_band, double dk_band, double &k_eff, int &nmodes)
Inverse Fourier transform a field weighted by the reduced spherical harmonics restricted to a wavenu...
Definition field.cpp:1792
double dr[3]
grid size in each dimension
Definition field.hpp:88
void fourier_transform()
Fourier transform the field.
Definition field.cpp:1496
void compute_ylm_wgtd_field(ParticleCatalogue &particles_data, ParticleCatalogue &particles_rand, LineOfSight *los_data, LineOfSight *los_rand, double alpha, int ell, int m)
Compute the weighted field (fluctuations) further weighted by the reduced spherical harmonics.
Definition field.cpp:1246
void compute_unweighted_field(ParticleCatalogue &particles)
Compute the unweighted field.
Definition field.cpp:1208
void apply_wide_angle_pow_law_kernel()
Apply wide-angle correction kernel in configuration space.
Definition field.cpp:1727
void compute_ylm_wgtd_quad_field(ParticleCatalogue &particles_data, ParticleCatalogue &particles_rand, LineOfSight *los_data, LineOfSight *los_rand, double alpha, int ell, int m)
Compute the quadratic weighted field (fluctuations) further weighted by the reduced spherical harmoni...
Definition field.cpp:1364
fftw_complex * field
complex field on mesh
Definition field.hpp:87
MeshField(trv::ParameterSet &params, bool plan_ini=true, const std::string &name="mesh-field")
Construct the mesh field.
Definition field.cpp:45
void compute_unweighted_field_fluctuations_insitu(ParticleCatalogue &particles)
Compute the unweighted field fluctuations.
Definition field.cpp:1229
double dk[3]
fundamental wavenumber in each dimension
Definition field.hpp:89
double vol
mesh volume
Definition field.hpp:90
~MeshField()
Destruct the mesh field.
Definition field.cpp:434
double vol_cell
mesh grid cell volume
Definition field.hpp:91
void apply_assignment_compensation()
Apply compensation needed after Fourier transform for assignment schemes.
Definition field.cpp:1764
const fftw_complex & operator[](long long gid)
Return mesh field grid cell value.
Definition field.cpp:524
Parameter set.
Particle catalogue.
Definition particles.hpp:78
Interpolated spherical Bessel function of the first kind.
Definition maths.hpp:268
Clustering measurement data objects.
I/O support including custom exceptions and utility functions.
Mathematical calculations.
Provide tracking of program resources and exceptions.
Program parameter configuration.
Particle containers with I/O methods and operations.
Binned vectors.
Definition dataobjs.hpp:166
Line-of-sight vector.
Definition dataobjs.hpp:186