1#ifndef SMOOTHINGFOURIER_HEADER
2#define SMOOTHINGFOURIER_HEADER
39 assert_mpi(
Nmesh > 0,
"[shift_grid_fourier] Grid is not allocated\n");
42 std::array<std::vector<std::complex<double>>, N> factor;
43 for (
int idim = 0; idim < N; idim++) {
44 factor[idim].resize(
Nmesh + 1);
45 for (
int m = -
Nmesh / 2; m <=
Nmesh / 2; m++) {
46 const double phase = sign * 2.0 * M_PI * m * shift[idim];
47 const bool nyquist = (
Nmesh % 2 == 0) and (std::abs(m) ==
Nmesh / 2);
48 factor[idim][m +
Nmesh / 2] =
49 nyquist ? std::complex<double>(std::cos(phase), 0.0) : std::polar(1.0, phase);
54#pragma omp parallel for
56 for (
int islice = 0; islice < Local_nx; islice++) {
57 for (
auto && fourier_index : fourier_grid.
get_fourier_range(islice, islice + 1)) {
59 std::complex<double>
f = factor[0][kint[0] +
Nmesh / 2];
60 for (
int idim = 1; idim < N; idim++)
61 f *= factor[idim][kint[idim] +
Nmesh / 2];
76 template <
int N,
class F>
80#pragma omp parallel for
82 for (
int islice = 0; islice < Local_nx; islice++) {
84 std::array<double, N> kvec;
85 for (
auto && fourier_index : fourier_grid.
get_fourier_range(islice, islice + 1)) {
87 f(fourier_index, kmag);
93 template <
int N,
class F>
102 template <
int N,
class F>
111 assert_mpi(a.get_nmesh() == b.get_nmesh(),
"[linear_combination_fourier] The grids have different sizes\n");
114 const auto Local_nx = a.get_local_nx();
116#pragma omp parallel for
118 for (
int islice = 0; islice < Local_nx; islice++) {
119 for (
auto && fourier_index : a.get_fourier_range(islice, islice + 1)) {
120 a.set_fourier_from_index(fourier_index,
121 a.get_fourier_from_index(fourier_index) * fa +
122 b.get_fourier_from_index(fourier_index) * fb);
148#pragma omp parallel for
150 for (
int islice = 0; islice < Local_nx; islice++) {
151 [[maybe_unused]]
double kmag2;
152 [[maybe_unused]] std::array<double, N> kvec;
153 for (
auto && fourier_index : fourier_grid.
get_fourier_range(islice, islice + 1)) {
155 double norm = std::sqrt(std::norm(
value));
165 const std::function<
double(
double)> & custom_filter_of_kBox_squared) {
169#pragma omp parallel for
171 for (
int islice = 0; islice < Local_nx; islice++) {
172 [[maybe_unused]]
double kmag2;
173 [[maybe_unused]] std::array<double, N> kvec;
174 for (
auto && fourier_index : fourier_grid.
get_fourier_range(islice, islice + 1)) {
177 value *= custom_filter_of_kBox_squared(kmag2);
198 const double y = (x / 3.0) * (x / 3.0);
199 return x * (0.5 +
y * (-0.56249985 +
200 y * (0.21093573 +
y * (-0.03954289 +
201 y * (0.00443319 +
y * (-0.00031761 +
y * 0.00001109))))));
203 const double y = 3.0 / x;
207 y * (0.01659667 +
y * (0.00017105 +
y * (-0.00249511 +
y * (0.00113653 +
y * (-0.00020033))))));
208 const double theta1 =
211 y * (0.00005650 +
y * (-0.00637879 +
y * (0.00074348 +
y * (0.00079824 +
y * (-0.00029166))))));
212 return f1 * std::cos(theta1) / std::sqrt(x);
217 double smoothing_scale,
218 std::string smoothing_method) {
221 std::function<double(
double)> filter_sharpk = [=](
double k2) ->
double {
222 double kR2 = k2 * smoothing_scale * smoothing_scale;
228 std::function<double(
double)> filter_gaussian = [=](
double k2) ->
double {
229 double kR2 = k2 * smoothing_scale * smoothing_scale;
230 return std::exp(-0.5 * kR2);
234 std::function<double(
double)> filter_tophat_2D = [=](
double k2) ->
double {
235 double kR2 = k2 * smoothing_scale * smoothing_scale;
236 double kR = std::sqrt(kR2);
241 std::function<double(
double)> filter_tophat_3D = [=](
double k2) ->
double {
242 double kR2 = k2 * smoothing_scale * smoothing_scale;
243 double kR = std::sqrt(kR2);
246 return 3.0 * (std::sin(kR) - kR * std::cos(kR)) / (kR2 * kR);
250 std::function<double(
double)> filter;
251 if (smoothing_method ==
"sharpk") {
252 filter = filter_sharpk;
253 }
else if (smoothing_method ==
"gaussian") {
254 filter = filter_gaussian;
255 }
else if (smoothing_method ==
"tophat") {
256 assert_mpi(N == 2 or N == 3,
257 "[smoothing_filter_fourier_space] Tophat filter only implemented in 2D and 3D");
259 filter = filter_tophat_2D;
261 filter = filter_tophat_3D;
263 throw std::runtime_error(
"Unknown filter " + smoothing_method +
" Options: sharpk, gaussian, tophat");
269#pragma omp parallel for
271 for (
int islice = 0; islice < Local_nx; islice++) {
272 [[maybe_unused]]
double kmag2;
273 [[maybe_unused]] std::array<double, N> kvec;
274 for (
auto && fourier_index : fourier_grid.
get_fourier_range(islice, islice + 1)) {
277 value *= filter(kmag2);
300 bool f_and_g_are_the_same_grid = (&fourier_grid_f == &fourier_grid_g);
306 fourier_grid_result = fourier_grid_g;
307 fourier_grid_result.
add_memory_label(
"FFTWGrid::convolution_fourier_space::fourier_grid_result");
311 if (not f_and_g_are_the_same_grid) {
312 tmp = fourier_grid_f;
320#pragma omp parallel for
322 for (
int islice = 0; islice < Local_nx; islice++) {
323 for (
auto && real_index : fourier_grid_result.
get_real_range(islice, islice + 1)) {
325 auto f_real = g_real;
326 if (not f_and_g_are_the_same_grid)
356 bool f_and_g_are_the_same_grid = (&real_grid_f == &real_grid_g);
362 real_grid_result = real_grid_g;
363 real_grid_result.
add_memory_label(
"FFTWGrid::convolution_real_space::real_grid_result");
367 if (not f_and_g_are_the_same_grid) {
376#pragma omp parallel for
378 for (
int islice = 0; islice < Local_nx; islice++) {
379 for (
auto && fourier_index : real_grid_result.
get_fourier_range(islice, islice + 1)) {
381 auto f_fourier = g_fourier;
382 if (not f_and_g_are_the_same_grid)
413 double grid_min = std::numeric_limits<double>::max();
414 double grid_max = -grid_min;
416#pragma omp parallel for reduction(max : grid_max) reduction(min : grid_min)
418 for (
int islice = 0; islice < Local_nx; islice++) {
419 for (
auto && real_index : real_grid.
get_real_range(islice, islice + 1)) {
421 grid_min = std::min(grid_min,
value);
422 grid_max = std::max(grid_max,
value);
427 assert_mpi(nbins > 0,
"[compute_grid_PDF] Need nbins > 0");
430 if (grid_max == grid_min) {
437 pdf.assign(nbins, 0.0);
438 for (
int i = 0; i < nbins; i++) {
439 x[i] = grid_min + (grid_max - grid_min) /
double(nbins) * (i + 0.5);
445 nthreads = std::max(nthreads, omp_get_max_threads());
447 std::vector<std::vector<double>> pdfthreads(nthreads, std::vector<double>(nbins, 0.0));
450#pragma omp parallel for
452 for (
int islice = 0; islice < Local_nx; islice++) {
455 id = omp_get_thread_num();
457 for (
auto && real_index : real_grid.
get_real_range(islice, islice + 1)) {
459 int ibin = int((
value - grid_min) / (grid_max - grid_min) * nbins);
463 if (ibin >= 0 and ibin < nbins)
464 pdfthreads[id][ibin] += 1;
469 for (
int i = 0; i < nthreads; i++) {
470 for (
int j = 0; j < nbins; j++) {
471 pdf[j] += pdfthreads[i][j];
477 MPI_Allreduce(MPI_IN_PLACE, pdf.data(), nbins, MPI_DOUBLE, MPI_SUM,
FML::communicator);
481 const double dx = (grid_max - grid_min) /
double(nbins);
482 double integral = 0.0;
483 for (
int i = 0; i < nbins; i++) {
484 integral += pdf[i] * dx;
486 for (
int i = 0; i < nbins; i++) {
std::complex< FloatType > ComplexType
Class for holding grids and performing real-to-complex and complex-to-real FFTs using FFTW with MPI.
int get_nmesh() const
Number of grid-cells per dimension.
void fftw_c2r()
Perform complex-to-real fourier transform.
void set_real_from_index(const IndexIntType ind, const FloatType value)
Set value in grid from (local) index.
std::array< int, N > get_fourier_integer_wavevector_from_index(const IndexIntType index) const
void add_memory_label(std::string label)
For memory logging: add a label to the grid.
FloatType get_real_from_index(const IndexIntType index) const
Fetch value in grid by (local) index.
ptrdiff_t get_local_nx() const
Number of local x-slices in the real grid.
void set_fourier_from_index(const IndexIntType ind, const ComplexType value)
Set value of cell in fourier grid using (local) index.
ComplexType get_fourier_from_index(const IndexIntType index) const
Fetch value in fourier grid by (local) index.
FourierRange get_fourier_range(int islice_begin=0, int islice_end=0) const
Range iterator for going through all active cells in the main fourier grid by index [ e....
void fftw_r2c()
Perform real-to-complex fourier transform.
void get_fourier_wavevector_and_norm_by_index(const IndexIntType ind, std::array< double, N > &kvec, double &kmag) const
From index in the grid get the k-vector and the magnitude of it.
void get_fourier_wavevector_and_norm2_by_index(const IndexIntType ind, std::array< double, N > &kvec, double &kmag2) const
From index in the grid get the k-vector and the norm of it (square magnitude)
RealRange get_real_range(int islice_begin=0, int islice_end=0) const
Range iterator for going through all active cells in the main real real grid by index [ e....
void compute_grid_PDF(const FFTWGrid< N > &real_grid, int nbins, std::vector< double > &x, std::vector< double > &pdf)
This computes the PDF of whatever quantity is in the grid (e.g.
void convolution_real_space(const FFTWGrid< N > &real_grid_f, const FFTWGrid< N > &real_grid_g, FFTWGrid< N > &real_grid_result)
From two real grids, f and g, compute the convolution This is done via multiplication in fourier-spa...
void shift_grid_real(FFTWGrid< N > &real_grid, const std::array< double, N > &shift, double sign=+1.0)
Translate a real grid: using Fourier interpolation (an r2c, a phase factor and a c2r).
void custom_smoothing_filter_fourier_space(FFTWGrid< N > &fourier_grid, const std::function< double(double)> &custom_filter_of_kBox_squared)
void for_each_fourier_mode(const FFTWGrid< N > &fourier_grid, F &&f)
Call f(fourier_index, kmag) for every local mode of a grid in fourier space (OpenMP over the slices; ...
void convolution_fourier_space(const FFTWGrid< N > &fourier_grid_f, const FFTWGrid< N > &fourier_grid_g, FFTWGrid< N > &fourier_grid_result)
From two fourier grids, f and g, compute the convolution This is done via multiplication in reals-sp...
void multiply_fourier_by_function_of_k(FFTWGrid< N > &fourier_grid, F &&g)
Multiply every mode by g(kmag) (kmag in units of 1/boxsize)
void smoothing_filter_fourier_space(FFTWGrid< N > &fourier_grid, double smoothing_scale, std::string smoothing_method)
void shift_grid_fourier(FFTWGrid< N > &fourier_grid, const std::array< double, N > &shift, double sign=+1.0)
Translate a field given in Fourier space: , i.e.
void whitening_fourier_space(FFTWGrid< N > &fourier_grid)
Take a fourier grid and divide each mode by its norm: .
double bessel_J1(double x)
Low-pass filters (tophat, gaussian, sharpk)
void linear_combination_fourier(FFTWGrid< N > &a, const FFTWGrid< N > &b, double wa, double wb)
The linear combination a -> wa a + wb b of two grids in fourier space (same layout)
void transform_fourier_modes(FFTWGrid< N > &fourier_grid, F &&func)
Replace every mode by func(kmag, value) (kmag in units of 1/boxsize)
void MinOverTasks(T *value)
Inplace min over tasks of a single value.
int NThreads
Total number of threads available.
void MaxOverTasks(T *value)
Inplace max over tasks of a single value.
MPI_Comm communicator
The MPI communicator used for all communication in the library (fiducial MPI_COMM_WORLD).
ComplexType value(const std::array< int, N > &wave, int generation)