FML
Loading...
Searching...
No Matches
FML::COSMOLOGY::LPT Namespace Reference

This namespace contains things related to Lagrangian Perturbation Theory (displacement fields, reconstruction, initial conditions etc.) More...

Typedefs

template<int N>
using FFTWGrid = FML::GRID::FFTWGrid<N>
 
template<class T >
using MPIParticles = FML::PARTICLE::MPIParticles<T>
 
using FloatType = FML::GRID::FloatType
 

Enumerations

enum class  Dealiasing { None , Padding }
 Padding evaluates nonlinear products on a larger mesh and crops the result. More...
 

Functions

template<int N>
void from_LPT_potential_to_displacement_vector (const FFTWGrid< N > &phi, std::array< FFTWGrid< N >, N > &psi, double DoverDini)
 Generate the displaceement field \( \Psi = \nabla \phi \) from the LPT potential \( \phi \).
 
template<int N>
void compute_1LPT_potential_fourier (const FFTWGrid< N > &delta_fourier, FFTWGrid< N > &phi_1LPT_fourier)
 Generate the 1LPT potential defined as \( \Psi^{\rm 1LPT} = \nabla \phi^{\rm 1LPT} \) and \( \nabla^2 \phi^{\rm 1LPT} = -\delta \).
 
template<int N>
void compute_2LPT_potential_fourier (const FFTWGrid< N > &delta_fourier, FFTWGrid< N > &phi_2LPT_fourier, Dealiasing dealiasing=Dealiasing::None)
 Optional Padding uses a >=3N/2 work mesh for the quadratic source.
 
template<int N>
void compute_3LPT_potential_fourier (const FFTWGrid< N > &delta_fourier, FFTWGrid< N > &phi_1LPT_fourier, FFTWGrid< N > &phi_2LPT_fourier, FFTWGrid< N > &phi_3LPT_a_fourier, FFTWGrid< N > &phi_3LPT_b_fourier, std::array< FFTWGrid< N >, N > &phi_3LPT_Avec_fourier, bool ignore_curl_term, Dealiasing dealiasing=Dealiasing::None)
 Optional Padding uses a >=2N work mesh for all nonlinear sources, retaining the full intermediate second-order bandwidth until cropping.
 
template<int N>
void compute_ALPT_potential_fourier (const FFTWGrid< N > &delta_fourier, const FFTWGrid< N > &phi_2LPT_fourier, FFTWGrid< N > &phi_ALPT_fourier, double smoothing_scale, std::string smoothing_method, double DoverDini_1LPT, double DoverDini_2LPT)
 Generate the APT potential defined as \( \Psi = \Psi^L + \Psi^S \) where the long range is just a smoothed 1LPT+2LPT and the short range is the spherical collapse approximation.
 
template<int N>
void compute_spherical_collapse_potential (const FFTWGrid< N > &delta_fourier, FFTWGrid< N > &phi_sc_fourier, double DoverDini_1LPT)
 Take in an initial density field (generated at a redshift zini) and the corresponding ratio of growth factors D(z)/D(zini) produces the approximate spherical collapse potential defined via \( \nabla^2 \phi_{\rm SC} = 3(\sqrt{\max(1 - \frac{2}{3}\delta(x,z), 0)} - 1) \) at the redshift z with \( \delta(x,z) = \frac{D(z)}{D(z_{\rm ini})}\delta_{\rm ini}(x) \).
 
template<int N>
void from_LPT_potential_to_displacement_vector_scaledependent (const FFTWGrid< N > &phi, std::array< FFTWGrid< N >, N > &psi, std::function< double(double)> DoverDini_of_k)
 Function is the ratio of the scale-dependent growth-factor at the time we want to generate particles to the time where phi was generated at as function of k.
 
template<int N>
void compute_3LPT_displacement_field (const FFTWGrid< N > &delta_fourier, std::array< FFTWGrid< N >, N > &Psi, std::array< FFTWGrid< N >, N > &dPsidt, double dlogDdt, bool ignore_curl_term, Dealiasing dealiasing)
 The potential construction retains the first-order Hessian and streams the second-order Hessian in two batches through N work grids.
 
template<int N, class T >
void assign_displacement_fields_scaledependent (MPIParticles< T > &part, FFTWGrid< N > &LPT_potential_fourier, std::function< double(double)> DoverDini_of_k, std::string LPT_potential_order)
 This method assigns the displacement fields to particles for the case where we have scaledependent growth.
 
void compute_LPT_growth_factors_GeffGLCDM (double OmegaM, double zini, std::function< double(double)> HoverH0_of_a, std::function< double(double)> GeffOverG_of_a, FML::INTERPOLATION::SPLINE::Spline &D_1LPT_of_loga, FML::INTERPOLATION::SPLINE::Spline &D_2LPT_of_loga, FML::INTERPOLATION::SPLINE::Spline &D_3LPTa_of_loga, FML::INTERPOLATION::SPLINE::Spline &D_3LPTb_of_loga)
 Compute 1,2,3LPT growth-factors in simple modified gravity models that have a GeffG(a) and spline them The growth factors are normalized such that D1LPT = 1 at zini We assume initial conditions as in EdS: D1LPT=1, D2LPT = -3/7, D3LPTa = -1/3 and D3LPTb = 5/21 NB: There should be a modified 2LPT+ kernel factor in general which we don't include here (model dependent)
 
void compute_LPT_growth_factors_LCDM (double OmegaM, double zini, std::function< double(double)> HoverH0_of_a, FML::INTERPOLATION::SPLINE::Spline &D_1LPT_of_loga, FML::INTERPOLATION::SPLINE::Spline &D_2LPT_of_loga, FML::INTERPOLATION::SPLINE::Spline &D_3LPTa_of_loga, FML::INTERPOLATION::SPLINE::Spline &D_3LPTb_of_loga)
 Compute 1,2,3LPT growth-factors in LCDM and spline them The growth factors are normalized such that D1LPT = 1 at zini We assume initial conditions as in EdS: D1LPT=1, D2LPT = -3/7, D3LPTa = -1/3 and D3LPTb = 5/21.
 
template<int N, class T >
void RSDReconstructionFourierMethod (MPIParticles< T > &part, std::string density_assignment_method, std::vector< double > los_direction, int Nmesh, int niterations, double beta, std::pair< std::string, double > smoothing_options, bool survey_data)
 RSD removal for particles in a periodic box We use a fixed line of sight direction los_direction, typically coordinate axes (e.g.
 

Detailed Description

This namespace contains things related to Lagrangian Perturbation Theory (displacement fields, reconstruction, initial conditions etc.)

Typedef Documentation

◆ FFTWGrid

Definition at line 34 of file DisplacementFields.h.

◆ FloatType

using FML::COSMOLOGY::LPT::FloatType = FML::GRID::FloatType

Definition at line 27 of file Reconstruction.h.

◆ MPIParticles

Definition at line 36 of file DisplacementFields.h.

Enumeration Type Documentation

◆ Dealiasing

Padding evaluates nonlinear products on a larger mesh and crops the result.

None preserves the original, aliased calculation. Padding excludes input and output Nyquist planes (for even meshes); all strictly sub-Nyquist modes remain.

Enumerator
None 
Padding 

Definition at line 12 of file Dealiasing.h.

Function Documentation

◆ assign_displacement_fields_scaledependent()

template<int N, class T >
void FML::COSMOLOGY::LPT::assign_displacement_fields_scaledependent ( MPIParticles< T > & part,
FFTWGrid< N > & LPT_potential_fourier,
std::function< double(double)> DoverDini_of_k,
std::string LPT_potential_order )

This method assigns the displacement fields to particles for the case where we have scaledependent growth.

This is involved: we take phi_iLPT(zini,k) and use growth factors to go to phi_iLPT(z,k) and FFT to get phi_iLPT(z,q). Then we transport the particles back to their original CPU given by the particles Lagrangian position q and assign the displacement field before we move them back to their eulerian positions. This can be done more efficiently (we don't need to do this much communication) but this will come at the expense of a lot more code so fuck it. In practice to be efficient we should ideally do all orders at the same time otherwise we need to swap and do communication many times. This is how its done in our COLA code. So this method mainly shows how to do it.

Template Parameters
NThe dimension we are working in
TThe particle class. Must have a get_D_* with * being one or more of 1LPT, 2LPT, 3LPTa, 3LPTb to use this method.
Parameters
[out]partThe particle container. We assign the displacement fields to the particles we get in.
[in]LPT_potential_fourierThe LPT potential phi(zini,k) at the initial redshift.
[in]DoverDini_of_kRatio of the growth factors at the current time to that of the time of LPT_potential_fourier.
[in]LPT_potential_order1LPT, 2LPT, 3LPTa or 3LPTb. Tells us what field in the particles we should store the data in

Definition at line 1489 of file DisplacementFields.h.

1492 {
1493 enum Order { _1LPT, _2LPT, _3LPTA, _3LPTB };
1494 const std::string interpolation_method = "CIC";
1495 int LPT_order{};
1496
1497 // Sanity checks
1498 assert_mpi(
1499 FML::PARTICLE::has_get_q<T>(),
1500 "[assign_displacement_fields_scaledependent] Particle must have Lagrangian position to use this");
1501 if (LPT_potential_order == "1LPT") {
1502 LPT_order = _1LPT;
1503 assert_mpi(FML::PARTICLE::has_get_D_1LPT<T>(),
1504 "[assign_displacement_fields_scaledependent] Particle must have D_1LPT to use this");
1505 } else if (LPT_potential_order == "2LPT") {
1506 LPT_order = _2LPT;
1507 assert_mpi(FML::PARTICLE::has_get_D_2LPT<T>(),
1508 "[assign_displacement_fields_scaledependent] Particle must have D_2LPT to use this");
1509 } else if (LPT_potential_order == "3LPTa") {
1510 LPT_order = _3LPTA;
1511 assert_mpi(FML::PARTICLE::has_get_D_3LPTa<T>(),
1512 "[assign_displacement_fields_scaledependent] Particle must have D_3LPTa to use this");
1513 } else if (LPT_potential_order == "3LPTb") {
1514 LPT_order = _3LPTB;
1515 assert_mpi(FML::PARTICLE::has_get_D_3LPTb<T>(),
1516 "[assign_displacement_fields_scaledependent] Particle must have D_3LPTb to use this");
1517 } else {
1518 assert_mpi(false,
1519 "[assign_displacement_fields_scaledependent] Unknown LPT_potential, options "
1520 "1LPT,2LPT,3LPTa,3LPTb");
1521 }
1522
1523 // Swap positions with Lagrangian position
1524 // Bring particles to the task they started off from
1526 part.communicate_particles();
1527
1528 // Use initial LPT potential + growth factor to compute displacement field at present time
1529 std::array<FFTWGrid<N>, N> psi_LPT_vector;
1531 psi_LPT_vector,
1532 DoverDini_of_k);
1533 for (int idim = 0; idim < N; idim++) {
1534 psi_LPT_vector[idim].communicate_boundaries();
1535 }
1536 // Interpolate to particle positions after which we have Psi(q,t) in displacements
1537 std::array<std::vector<FML::GRID::FloatType>, N> displacements;
1539 part.get_particles_ptr(),
1540 part.get_npart(),
1541 displacements,
1543 for (int idim = 0; idim < N; idim++) {
1544 psi_LPT_vector[idim].free();
1545 }
1546
1547 // Assign displacement field to particles
1548 auto np = part.get_npart();
1549#ifdef USE_OMP
1550#pragma omp parallel for
1551#endif
1552 for (size_t ind = 0; ind < np; ind++) {
1553 if (LPT_order == _1LPT) {
1554 auto * D = FML::PARTICLE::GetD_1LPT(part[ind]);
1555 for (int idim = 0; idim < N; idim++)
1556 D[idim] = displacements[idim][ind];
1557 } else if (LPT_order == _2LPT) {
1558 auto * D = FML::PARTICLE::GetD_2LPT(part[ind]);
1559 for (int idim = 0; idim < N; idim++)
1560 D[idim] = displacements[idim][ind];
1561 } else if (LPT_order == _3LPTA) {
1562 auto * D = FML::PARTICLE::GetD_3LPTa(part[ind]);
1563 for (int idim = 0; idim < N; idim++)
1564 D[idim] = displacements[idim][ind];
1565 } else if (LPT_order == _3LPTB) {
1566 auto * D = FML::PARTICLE::GetD_3LPTb(part[ind]);
1567 for (int idim = 0; idim < N; idim++)
1568 D[idim] = displacements[idim][ind];
1569 }
1570 }
1571
1572 // Swap back positions
1573 // Bring particles back to the task the eulerian position belongs to
1574 // The particles are now as they were just with the displacement field assigned
1576 part.communicate_particles();
1577 }
const std::string interpolation_method
Definition Main.cpp:64
T * get_particles_ptr()
Get a pointer to the first particle.
void communicate_particles(size_t max_message_bytes=std::numeric_limits< int >::max(), bool force_general=false)
Communicate particles across CPU boundaries.
size_t get_npart() const
Number of active particles on the local task.
void from_LPT_potential_to_displacement_vector_scaledependent(const FFTWGrid< N > &phi_fourier, std::array< FFTWGrid< N >, N > &psi_real, std::function< double(double)> growth_function_ratio)
Function is the ratio of the scale-dependent growth-factor at the time we want to generate particles ...
void interpolate_grid_vector_to_particle_positions(const std::array< FFTWGrid< N >, N > &grid_vec, const T *part, size_t NumPart, std::array< std::vector< FloatType >, N > &interpolated_values_vec)
Interpolate a vector of grids (e.g.
constexpr double * GetD_3LPTa(...)
constexpr double * GetD_3LPTb(...)
constexpr double * GetD_2LPT(...)
void swap_eulerian_and_lagrangian_positions(T *part, size_t NumPart)

◆ compute_1LPT_potential_fourier()

template<int N>
void FML::COSMOLOGY::LPT::compute_1LPT_potential_fourier ( const FFTWGrid< N > & delta_fourier,
FFTWGrid< N > & phi_1LPT_fourier )

Generate the 1LPT potential defined as \( \Psi^{\rm 1LPT} = \nabla \phi^{\rm 1LPT} \) and \( \nabla^2 \phi^{\rm 1LPT} = -\delta \).

Returns it in Fourier space.

Template Parameters
NThe dimension of the grid
Parameters
[in]delta_fourierThe density contrast in fourier space
[out]phi_1LPT_fourierThe LPT potential in fourier space

Definition at line 300 of file DisplacementFields.h.

300 {
301
302 // We require delta to exist and if phi_1LPT is allocated it must have the same size as delta
303 assert_mpi(delta_fourier.get_nmesh() > 0,
304 "[compute_1LPT_potential_fourier] delta grid has to be already allocated!");
305
306#ifdef DEBUG_LPT
307 if (FML::ThisTask == 0)
308 std::cout << "Compute 1LPT potential\n";
309#endif
310
311 auto Nmesh_phi = phi_1LPT_fourier.get_nmesh();
312
313 auto nleft = delta_fourier.get_n_extra_slices_left();
314 auto nright = delta_fourier.get_n_extra_slices_right();
315 auto Nmesh = delta_fourier.get_nmesh();
316 auto Local_nx = delta_fourier.get_local_nx();
317 auto Local_x_start = delta_fourier.get_local_x_start();
318
319 // Create 1LPT grid
320 if (Nmesh_phi == 0) {
321 phi_1LPT_fourier = FFTWGrid<N>(Nmesh, nleft, nright);
322 phi_1LPT_fourier.add_memory_label("FFTWGrid::compute_1LPT_potential_fourier::phi_1LPT_fourier");
323 phi_1LPT_fourier.set_grid_status_real(false);
324 }
325
326 // Divide grid by k^2. Assuming delta was created in fourier-space so no FFTW normalization needed
327#ifdef USE_OMP
328#pragma omp parallel for
329#endif
330 for (int islice = 0; islice < Local_nx; islice++) {
331 [[maybe_unused]] double kmag2;
332 [[maybe_unused]] std::array<double, N> kvec;
333 for (auto && fourier_index : phi_1LPT_fourier.get_fourier_range(islice, islice + 1)) {
334 if (Local_x_start == 0 and fourier_index == 0)
335 continue; // DC mode (k=0)
336
337 // Get wavevector and magnitude
338 phi_1LPT_fourier.get_fourier_wavevector_and_norm2_by_index(fourier_index, kvec, kmag2);
339
340 // D^2 Phi_1LPT = -delta => F[Phi_1LPT] = F[delta] / k^2
341 auto value = delta_fourier.get_fourier_from_index(fourier_index) / FML::GRID::FloatType(kmag2);
342 phi_1LPT_fourier.set_fourier_from_index(fourier_index, value);
343 }
344 }
345
346 // Deal with DC mode
347 if (Local_x_start == 0)
348 phi_1LPT_fourier.set_fourier_from_index(0, 0.0);
349 }
FML::GRID::FFTWGrid< N > FFTWGrid
Definition Main.cpp:13
const int Nmesh
Definition Main.cpp:48
int get_nmesh() const
Number of grid-cells per dimension.
Definition FFTWGrid.h:1230
void add_memory_label(std::string label)
For memory logging: add a label to the grid.
Definition FFTWGrid.h:404
ptrdiff_t get_local_x_start() const
The x-slice the first local slice has in the global grid.
Definition FFTWGrid.h:1260
ptrdiff_t get_local_nx() const
Number of local x-slices in the real grid.
Definition FFTWGrid.h:1255
void set_fourier_from_index(const IndexIntType ind, const ComplexType value)
Set value of cell in fourier grid using (local) index.
Definition FFTWGrid.h:1212
ComplexType get_fourier_from_index(const IndexIntType index) const
Fetch value in fourier grid by (local) index.
Definition FFTWGrid.h:1207
void set_grid_status_real(bool grid_is_a_real_grid)
Set the status of the grid: is it currently a real grid or a fourier grid?
Definition FFTWGrid.h:556
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)
Definition FFTWGrid.h:1299
int get_n_extra_slices_right() const
How many extra slices we have allocated to the right.
Definition FFTWGrid.h:1473
int get_n_extra_slices_left() const
How many extra slices we have allocated to the left.
Definition FFTWGrid.h:1468
int ThisTask
The MPI task number.
Definition Global.cpp:39
ComplexType value(const std::array< int, N > &wave, int generation)

◆ compute_2LPT_potential_fourier()

template<int N>
void FML::COSMOLOGY::LPT::compute_2LPT_potential_fourier ( const FFTWGrid< N > & delta,
FFTWGrid< N > & phi_2LPT,
Dealiasing dealiasing = Dealiasing::None )

Optional Padding uses a >=3N/2 work mesh for the quadratic source.

Generate the 2LPT potential defined as \( \Psi^{\rm 2LPT} = \nabla \phi^{\rm 2LPT} \) and \( \nabla^2 \phi^{\rm 2LPT} = \ldots \).

See LPT/README.md for Nyquist conventions, memory cost and examples.

Returns the grid in Fourier space.

Template Parameters
NThe dimension of the grid
Parameters
[in]deltaThe density contrast in fourier space
[out]phi_2LPTThe LPT potential in fourier space

Definition at line 363 of file DisplacementFields.h.

363 {
364 if (dealiasing == Dealiasing::Padding) {
365 auto padded = FML::GRID::resize_fourier_grid(delta, detail::padded_lpt_mesh(delta.get_nmesh(), 2));
369 delta.get_nmesh(),
372 return;
373 }
374
375 // We require delta to exist and if phi_2LPT is allocated it must have the same size as delta
376 assert_mpi(delta.get_nmesh() > 0,
377 "[compute_2LPT_potential_fourier] delta grid has to be already allocated!");
378
379 // This is the -3/7 factor coming from D2 = -3/7 D1^2 for the growing mode in Einstein-deSitter
380 constexpr double prefactor_2LPT = -3.0 / 7.0;
381
382#ifdef DEBUG_LPT
383 if (FML::ThisTask == 0)
384 std::cout << "Compute 2LPT potential (require N(N-1)/2 = 3 for N = 3 temporary grids)\n";
385#endif
386
387 auto nleft = delta.get_n_extra_slices_left();
388 auto nright = delta.get_n_extra_slices_right();
389 auto Nmesh = delta.get_nmesh();
390 auto Local_nx = delta.get_local_nx();
391 auto Local_x_start = delta.get_local_x_start();
392
393 // Create grids
394 FFTWGrid<N> phi_1LPT_ii[N];
395 for (int i = 0; i < N; i++) {
396 phi_1LPT_ii[i] = FFTWGrid<N>(Nmesh, nleft, nright);
397 phi_1LPT_ii[i].add_memory_label("FFTWGrid::compute_2LPT_potential_fourier::phi_1LPT_ii_" +
398 std::to_string(i));
399 phi_1LPT_ii[i].set_grid_status_real(false);
400 }
401#ifdef DEBUG_LPT
402 if (FML::ThisTask == 0) {
403 std::cout << "Compute [DiDi phi_1LPT] in fourier space\n";
404 }
405#endif
406
407 // Compute phi_xx, phi_yy, ...
408#ifdef USE_OMP
409#pragma omp parallel for
410#endif
411 for (int islice = 0; islice < Local_nx; islice++) {
412 [[maybe_unused]] double kmag2;
413 [[maybe_unused]] std::array<double, N> kvec;
414 for (auto && fourier_index : phi_1LPT_ii[0].get_fourier_range(islice, islice + 1)) {
415 if (Local_x_start == 0 and fourier_index == 0)
416 continue; // DC mode (k=0)
417
418 // Get wavevector and magnitude
419 delta.get_fourier_wavevector_and_norm2_by_index(fourier_index, kvec, kmag2);
420
421 // D^2Phi = -delta => F[DiDj Phi] = F[delta] kikj/k^2
422 auto value = delta.get_fourier_from_index(fourier_index) / FML::GRID::FloatType(kmag2);
423
424 for (int idim = 0; idim < N; idim++) {
425 phi_1LPT_ii[idim].set_fourier_from_index(fourier_index,
426 value *
427 FML::GRID::FloatType(kvec[idim] * kvec[idim]));
428 }
429 }
430 }
431
432 // Deal with DC mode
433 if (Local_x_start == 0)
434 for (int idim = 0; idim < N; idim++)
435 phi_1LPT_ii[idim].set_fourier_from_index(0, 0.0);
436
437 // Fourier transform
438 for (int idim = 0; idim < N; idim++) {
439#ifdef DEBUG_LPT
440 if (FML::ThisTask == 0)
441 std::cout << "Fourier transform [DiDi phi_1LPT] to real space: " << idim + 1 << " / " << N
442 << "\n";
443#endif
444 phi_1LPT_ii[idim].fftw_c2r();
445 }
446
447 // Crete output grid
448 phi_2LPT = FFTWGrid<N>(Nmesh, nleft, nright);
449 phi_2LPT.add_memory_label("FFTWGrid::compute_2LPT_potential_fourier::phi_2LPT_fourier");
450 phi_2LPT.set_grid_status_real(true);
451
452 // Copy over source
453#ifdef DEBUG_LPT
454 if (FML::ThisTask == 0)
455 std::cout << "Add 0.5[(D^2 phi_1LPT)^2 - DiDi phi_1LPT^2] to real space grid containing "
456 "(D^2phi_2LPT) \n";
457#endif
458
459#ifdef USE_OMP
460#pragma omp parallel for
461#endif
462 for (int islice = 0; islice < Local_nx; islice++) {
463 for (auto && real_index : phi_2LPT.get_real_range(islice, islice + 1)) {
464 auto laplacian = 0.0, sum_squared = 0.0;
465 for (int idim = 0; idim < N; idim++) {
466 auto curpsi = phi_1LPT_ii[idim].get_real_from_index(real_index);
467 laplacian += curpsi;
468 sum_squared += curpsi * curpsi;
469 }
470 auto value = 0.5 * (laplacian * laplacian - sum_squared);
471 phi_2LPT.set_real_from_index(real_index, value);
472 }
473 }
474
475 // Free memory
476 for (int idim = 0; idim < N; idim++)
477 phi_1LPT_ii[idim].free();
478
479 // Create grids
480 const int num_pairs = (N * (N - 1)) / 2;
481 FFTWGrid<N> phi_1LPT_ij[num_pairs];
482 for (int i = 0; i < num_pairs; i++) {
483 phi_1LPT_ij[i] = FFTWGrid<N>(Nmesh, nleft, nright);
484 phi_1LPT_ij[i].add_memory_label("FFTWGrid::compute_2LPT_potential_fourier::phi_1LPT_ij_" +
485 std::to_string(i));
486 phi_1LPT_ij[i].set_grid_status_real(false);
487 }
488
489 // Compute phi_xixj for all pairs of i,j
490#ifdef DEBUG_LPT
491 if (FML::ThisTask == 0)
492 std::cout << "Compute [DiDj phi_1LPT] in fourier space\n";
493#endif
494
495#ifdef USE_OMP
496#pragma omp parallel for
497#endif
498 for (int islice = 0; islice < Local_nx; islice++) {
499 [[maybe_unused]] double kmag2;
500 [[maybe_unused]] std::array<double, N> kvec;
501 for (auto && fourier_index : phi_1LPT_ij[0].get_fourier_range(islice, islice + 1)) {
502 if (Local_x_start == 0 and fourier_index == 0)
503 continue; // DC mode (k=0)
504
505 // Get wavevector and magnitude
506 delta.get_fourier_wavevector_and_norm2_by_index(fourier_index, kvec, kmag2);
507 // The mixed derivatives of the Nyquist modes are zero
508 const auto kodd = detail::odd_derivative_wavevector<N>(kvec, Nmesh);
509
510 auto value = delta.get_fourier_from_index(fourier_index) / FML::GRID::FloatType(kmag2);
511
512 int pair = 0;
513 for (int idim1 = 0; idim1 < N; idim1++) {
514 for (int idim2 = idim1 + 1; idim2 < N; idim2++) {
515 phi_1LPT_ij[pair++].set_fourier_from_index(
516 fourier_index,
517 FML::GRID::FloatType(kodd[idim1] * kodd[idim2]) * value);
518 }
519 }
520 }
521 }
522
523 // Deal with DC mode
524 if (Local_x_start == 0)
525 for (auto & g : phi_1LPT_ij)
526 g.set_fourier_from_index(0, 0.0);
527
528 // Fourier transform
529 for (int pair = 0; pair < num_pairs; pair++) {
530#ifdef DEBUG_LPT
531 if (FML::ThisTask == 0)
532 std::cout << "Fourier transform [DiDj phi_1LPT] to real space: " << pair + 1 << " / "
533 << num_pairs << "\n";
534#endif
535 phi_1LPT_ij[pair].fftw_c2r();
536 }
537
538 // Copy over source
539#ifdef DEBUG_LPT
540 if (FML::ThisTask == 0)
541 std::cout << "Add [-DiDjphi_1LPT^2] to real space grid containing (D^2phi_2LPT) \n";
542#endif
543
544#ifdef USE_OMP
545#pragma omp parallel for
546#endif
547 for (int islice = 0; islice < Local_nx; islice++) {
548 for (auto && real_index : phi_2LPT.get_real_range(islice, islice + 1)) {
549 auto sum_squared = 0.0;
550 for (int pair = 0; pair < num_pairs; pair++) {
551 auto curpsi = phi_1LPT_ij[pair].get_real_from_index(real_index);
552 sum_squared += curpsi * curpsi;
553 }
554 auto value = (phi_2LPT.get_real_from_index(real_index) - sum_squared);
555
556 phi_2LPT.set_real_from_index(real_index, value);
557 }
558 }
559
560 // Free memory
561 for (int pair = 0; pair < num_pairs; pair++) {
562 phi_1LPT_ij[pair].free();
563 }
564
565 // Fourier transform source
566#ifdef DEBUG_LPT
567 if (FML::ThisTask == 0)
568 std::cout << "Fourier transform [D^2phi_2LPT] to fourier space\n";
569#endif
570 phi_2LPT.fftw_r2c();
571
572 // Divide by -k^2 and normalize
573#ifdef DEBUG_LPT
574 if (FML::ThisTask == 0)
575 std::cout << "Computing phi_2LPT in fourier space\n";
576#endif
577
578#ifdef USE_OMP
579#pragma omp parallel for
580#endif
581 for (int islice = 0; islice < Local_nx; islice++) {
582 [[maybe_unused]] double kmag2;
583 [[maybe_unused]] std::array<double, N> kvec;
584 for (auto && fourier_index : phi_2LPT.get_fourier_range(islice, islice + 1)) {
585 if (Local_x_start == 0 and fourier_index == 0)
586 continue; // DC mode (k=0)
587
588 // Get wavevector and magnitude
589 phi_2LPT.get_fourier_wavevector_and_norm2_by_index(fourier_index, kvec, kmag2);
590
591 // Add in the -3/7 factor
592 auto value = phi_2LPT.get_fourier_from_index(fourier_index);
593 value *= -prefactor_2LPT / kmag2;
594
595 phi_2LPT.set_fourier_from_index(fourier_index, value);
596 }
597 }
598
599 // Deal with DC mode
600 if (Local_x_start == 0)
601 phi_2LPT.set_fourier_from_index(0, 0.0);
602 }
void free(HaloModel **hm)
Definition Wrapper.cpp:34
Class for holding grids and performing real-to-complex and complex-to-real FFTs using FFTW with MPI.
Definition FFTWGrid.h:98
void fftw_c2r()
Perform complex-to-real fourier transform.
Definition FFTWGrid.h:1132
void set_real_from_index(const IndexIntType ind, const FloatType value)
Set value in grid from (local) index.
Definition FFTWGrid.h:1196
void free()
The fftw_complex cast of get_fourier_gride.
Definition FFTWGrid.h:1453
FloatType get_real_from_index(const IndexIntType index) const
Fetch value in grid by (local) index.
Definition FFTWGrid.h:1461
void fftw_r2c()
Perform real-to-complex fourier transform.
Definition FFTWGrid.h:1077
void compute_2LPT_potential_fourier(const FFTWGrid< N > &delta_fourier, FFTWGrid< N > &phi_2LPT_fourier, Dealiasing dealiasing=Dealiasing::None)
Optional Padding uses a >=3N/2 work mesh for the quadratic source.
FFTWGrid< N > resize_fourier_grid(const FFTWGrid< N > &input, int nmesh, int nleft=0, int nright=0)
Copy normalized Fourier coefficients; excluded modes are zero, with no volume factor.
void potential()

◆ compute_3LPT_displacement_field()

template<int N>
void FML::COSMOLOGY::LPT::compute_3LPT_displacement_field ( const FFTWGrid< N > & delta_fourier,
std::array< FFTWGrid< N >, N > & Psi,
std::array< FFTWGrid< N >, N > & dPsidt,
double dlogDdt,
bool ignore_curl_term,
Dealiasing dealiasing )

The potential construction retains the first-order Hessian and streams the second-order Hessian in two batches through N work grids.

With all 3D potentials and curl requested, its peak is about 17 full grids including the input, before caller-owned outputs and FFT workspace overhead. The units of the dlogDdt term is what sets the units of dPsidt In this methods the displacement field is assumed to be on the EdS/LCDM form Psi = D Psi1LPT + D^2 Psi2LPT + D^3 Psi3LPT i.e. each term multiplied with powers of D so we only have a single growth-factor D_nLPT = const * (D_1LPT)^n with the constants being 1, -3/7, 1/3, -10/21, ... (these are already included in the potentials returned by compute_3LPT_potential_fourier). The fields are returned at the time of delta_fourier: Psi = Psi1LPT + Psi2LPT + Psi3LPT and dPsidt = dlogDdt * (Psi1LPT + 2 Psi2LPT + 3 Psi3LPT).

Template Parameters
NDimensions we are working in (only 2 or 3)
Parameters
[in]delta_fourierA realisation of the density field.
[out]PsiThe displacement field
[out]dPsidtThe derivative of the displacement field (units is that of the next factor)
[in]dlogDdtThe logarithmic derivative of D at the time we want Psi in whatever units you want it to be.
[in]ignore_curl_termDon't include the transverse (curl) 3LPT term

Definition at line 627 of file DisplacementFields.h.

632 {
633
634 // We require delta to exist
635 assert_mpi(delta_fourier.get_nmesh() > 0,
636 "[compute_3LPT_displacement_field] delta grid has to be already allocated!");
637
638 auto nleft = delta_fourier.get_n_extra_slices_left();
639 auto nright = delta_fourier.get_n_extra_slices_right();
640 auto Nmesh = delta_fourier.get_nmesh();
641 auto Local_nx = delta_fourier.get_local_nx();
642 auto Local_x_start = delta_fourier.get_local_x_start();
643
644 std::array<FFTWGrid<N>, N> phi_3LPT_Avec_fourier;
645 FFTWGrid<N> phi_1LPT_fourier;
646 FFTWGrid<N> phi_2LPT_fourier;
647 FFTWGrid<N> phi_3LPT_a_fourier;
648 FFTWGrid<N> phi_3LPT_b_fourier;
649 compute_3LPT_potential_fourier<N>(delta_fourier,
650 phi_1LPT_fourier,
651 phi_2LPT_fourier,
652 phi_3LPT_a_fourier,
653 phi_3LPT_b_fourier,
654 phi_3LPT_Avec_fourier,
655 ignore_curl_term,
656 dealiasing);
657
658 // Make the displacement field
659 for (int idim = 0; idim < N; idim++) {
660 Psi[idim] = FFTWGrid<N>(Nmesh, nleft, nright);
661 Psi[idim].add_memory_label("FFTWGrid::compute_1LPT_2LPT_3LPT_displacement_field::Psi" +
662 std::to_string(idim));
663 dPsidt[idim] = FFTWGrid<N>(Nmesh, nleft, nright);
664 dPsidt[idim].add_memory_label("FFTWGrid::compute_1LPT_2LPT_3LPT_displacement_field::dPsidt" +
665 std::to_string(idim));
666 Psi[idim].set_grid_status_real(false);
667 dPsidt[idim].set_grid_status_real(false);
668 }
669
670 // The potentials from compute_3LPT_potential_fourier are already scaled (with the EdS factors
671 // -1, -3/7, 1/3, -10/21 and 1/7 and the -1/k^2 of the inverse Laplacian) such that at the initial time
672 // Psi = D(phi_1LPT + phi_2LPT + phi_3LPT_a + phi_3LPT_b) + D x A_3LPT
673 // With Psi_nLPT propto D^n we have dPsi/dt = dlogDdt * (Psi_1LPT + 2 Psi_2LPT + 3 Psi_3LPT)
674 using ComplexType = std::complex<FML::GRID::FloatType>;
675#ifdef USE_OMP
676#pragma omp parallel for
677#endif
678 for (int islice = 0; islice < Local_nx; islice++) {
679 [[maybe_unused]] double kmag2;
680 [[maybe_unused]] std::array<double, N> kvec;
681 const ComplexType I(0, 1);
682 for (auto && fourier_index : phi_1LPT_fourier.get_fourier_range(islice, islice + 1)) {
683 if (Local_x_start == 0 and fourier_index == 0)
684 continue; // DC mode (k=0)
685
686 // Get wavevector and magnitude
687 phi_1LPT_fourier.get_fourier_wavevector_and_norm2_by_index(fourier_index, kvec, kmag2);
688 // Only first derivatives here: zero for the Nyquist components of k_vec
689 const auto kodd = detail::odd_derivative_wavevector<N>(kvec, Nmesh);
690 std::array<FML::GRID::FloatType, N> k;
691 for (int idim = 0; idim < N; idim++)
692 k[idim] = FML::GRID::FloatType(kodd[idim]);
693
694 const ComplexType value_1 = phi_1LPT_fourier.get_fourier_from_index(fourier_index);
695 const ComplexType value_2 = phi_2LPT_fourier.get_fourier_from_index(fourier_index);
696 const ComplexType value_3 = phi_3LPT_a_fourier.get_fourier_from_index(fourier_index) +
697 phi_3LPT_b_fourier.get_fourier_from_index(fourier_index);
698 const ComplexType value = value_1 + value_2 + value_3;
699 const ComplexType dvaluedt =
700 FML::GRID::FloatType(dlogDdt) *
701 (value_1 + FML::GRID::FloatType(2.0) * value_2 + FML::GRID::FloatType(3.0) * value_3);
702 const FML::GRID::FloatType dfac3 = FML::GRID::FloatType(3.0 * dlogDdt);
703
704 if constexpr (N == 2) {
705 // In 2D the vector potential only has a 'z' component and curl(A z) = (d_y A, -d_x A)
706 ComplexType Az = 0.0;
707 if (not ignore_curl_term)
708 Az = phi_3LPT_Avec_fourier[0].get_fourier_from_index(fourier_index);
709 const std::array<ComplexType, N> curlA{I * k[1] * Az, -I * k[0] * Az};
710 for (int idim = 0; idim < N; idim++) {
711 Psi[idim].set_fourier_from_index(fourier_index, I * k[idim] * value + curlA[idim]);
712 dPsidt[idim].set_fourier_from_index(fourier_index,
713 I * k[idim] * dvaluedt + dfac3 * curlA[idim]);
714 }
715 } else if constexpr (N == 3) {
716 std::array<ComplexType, N> A;
717 A.fill(0.0);
718 if (not ignore_curl_term) {
719 for (int idim = 0; idim < N; idim++)
720 A[idim] = phi_3LPT_Avec_fourier[idim].get_fourier_from_index(fourier_index);
721 }
722 const std::array<ComplexType, N> curlA{I * (k[1] * A[2] - k[2] * A[1]),
723 I * (k[2] * A[0] - k[0] * A[2]),
724 I * (k[0] * A[1] - k[1] * A[0])};
725 for (int idim = 0; idim < N; idim++) {
726 Psi[idim].set_fourier_from_index(fourier_index, I * k[idim] * value + curlA[idim]);
727 dPsidt[idim].set_fourier_from_index(fourier_index,
728 I * k[idim] * dvaluedt + dfac3 * curlA[idim]);
729 }
730 }
731 }
732 }
733
734 // Deal with DC mode
735 if (Local_x_start == 0) {
736 for (auto & g : Psi)
737 g.set_fourier_from_index(0, 0.0);
738 for (auto & g : dPsidt)
739 g.set_fourier_from_index(0, 0.0);
740 }
741
742 // Fourier transform to real space and we are done
743 for (int idim = 0; idim < N; idim++) {
744 Psi[idim].fftw_c2r();
745 dPsidt[idim].fftw_c2r();
746 }
747 }
std::complex< FloatType > ComplexType
Definition FFTWGlobal.h:37
This namespace deals with interpolation and density assignment.

◆ compute_3LPT_potential_fourier()

template<int N>
void FML::COSMOLOGY::LPT::compute_3LPT_potential_fourier ( const FFTWGrid< N > & delta_fourier,
FFTWGrid< N > & phi_1LPT_fourier,
FFTWGrid< N > & phi_2LPT_fourier,
FFTWGrid< N > & phi_3LPT_a_fourier,
FFTWGrid< N > & phi_3LPT_b_fourier,
std::array< FFTWGrid< N >, N > & phi_3LPT_Avec_fourier,
bool ignore_curl_term,
Dealiasing dealiasing )

Optional Padding uses a >=2N work mesh for all nonlinear sources, retaining the full intermediate second-order bandwidth until cropping.

The potential construction retains the first-order Hessian and streams the second-order Hessian in two batches through N work grids.

With all 3D potentials and curl requested, its peak is about 17 full grids including the input, before caller-owned outputs and FFT workspace overhead. The potentials we output are normalized such that we can get the displacement field as Psi = D phi_1LPT + D phi_2LPT + D phi_3LPT_a + D phi_3PT_b + D x A_3LPT

Template Parameters
NDimensions we are working in (only 2 or 3)
Parameters
[in]delta_fourierA realisation of the density field.
[out]phi_1LPT_fourierThe 1LPT displacement potential
[out]phi_2LPT_fourierThe 2LPT displacement potential
[out]phi_3LPT_a_fourierThe A 3LPT displacement potential
[out]phi_3LPT_b_fourierThe B 3LPT displacement potential
[out]phi_3LPT_Avec_fourierThe 3LPT displacement vector potential
[in]ignore_curl_termDon't compute the vector potential

Definition at line 768 of file DisplacementFields.h.

775 {
776
777 // Only works for N = 2 and N = 3
778 static_assert(N == 2 or N == 3);
779 if (dealiasing == Dealiasing::Padding) {
780 // Keep the full second-order bandwidth until the third-order
781 // products are complete. Cropping phi2 early loses real UV couplings.
782 auto padded = FML::GRID::resize_fourier_grid(delta_fourier,
783 detail::padded_lpt_mesh(delta_fourier.get_nmesh(), 3));
784 FFTWGrid<N> p1, p2, pa, pb;
785 std::array<FFTWGrid<N>, N> avec;
786 compute_3LPT_potential_fourier<N>(padded, p1, p2, pa, pb, avec, ignore_curl_term);
787 FML::GRID::FourierResamplingPlan<N> crop_plan(p1, delta_fourier);
788 auto crop = [&](FFTWGrid<N> & field) {
789 FFTWGrid<N> result(delta_fourier.get_nmesh(),
790 delta_fourier.get_n_extra_slices_left(),
791 delta_fourier.get_n_extra_slices_right());
792 crop_plan.apply(field, result);
793 field.free();
794 return result;
795 };
796 phi_1LPT_fourier = crop(p1);
797 phi_2LPT_fourier = crop(p2);
798 phi_3LPT_a_fourier = crop(pa);
799 phi_3LPT_b_fourier = crop(pb);
800 if (!ignore_curl_term)
801 for (int d = 0; d < (N == 2 ? 1 : N); d++)
802 phi_3LPT_Avec_fourier[d] = crop(avec[d]);
803 return;
804 }
805
806 // We require delta to exist
807 assert_mpi(delta_fourier.get_nmesh() > 0,
808 "[compute_3LPT_displacement_field] delta grid has to be already allocated!");
809
810 // Factor to scale displacement potentials such that Psi = Dphi_1LPT + Dphi_2LPT + ... + D x Avec_3LPT
811 constexpr FML::GRID::FloatType prefactor_1LPT = -1.0;
812 constexpr FML::GRID::FloatType prefactor_2LPT = -3.0 / 7.0;
813 constexpr FML::GRID::FloatType prefactor_3LPT_a = 1.0 / 3.0;
814 constexpr FML::GRID::FloatType prefactor_3LPT_b = -10.0 / 21.0;
815 constexpr FML::GRID::FloatType prefactor_3LPT_Avec = 1.0 / 7.0;
816
817 auto nleft = delta_fourier.get_n_extra_slices_left();
818 auto nright = delta_fourier.get_n_extra_slices_right();
819 auto Nmesh = delta_fourier.get_nmesh();
820 auto Local_nx = delta_fourier.get_local_nx();
821 auto Local_x_start = delta_fourier.get_local_x_start();
822
823 // Store -k^2phi_1LPT
824 phi_1LPT_fourier = delta_fourier;
825 phi_1LPT_fourier.add_memory_label("FFTWGrid::compute_3LPT_potential_fourier::phi_1LPT_fourier");
826
827 // Compute all terms phi_1LPT_ij. These are absolutely needed
828 const int num_pairs = (N * (N + 1)) / 2;
829 FFTWGrid<N> phi_1LPT_ij[num_pairs];
830 for (int i = 0; i < num_pairs; i++) {
831 phi_1LPT_ij[i] = FFTWGrid<N>(Nmesh, nleft, nright);
832 phi_1LPT_ij[i].add_memory_label("FFTWGrid::compute_3LPT_potential_fourier::phi_1LPT_ij_" +
833 std::to_string(i));
834 }
835
836 if (FML::ThisTask == 0)
837 std::cout << "Computing phi_1LPT_ij for all i,j...\n";
838#ifdef USE_OMP
839#pragma omp parallel for
840#endif
841 for (int islice = 0; islice < Local_nx; islice++) {
842 [[maybe_unused]] double kmag2;
843 [[maybe_unused]] std::array<double, N> kvec;
844 for (auto && fourier_index : phi_1LPT_fourier.get_fourier_range(islice, islice + 1)) {
845 if (Local_x_start == 0 and fourier_index == 0)
846 continue; // DC mode (k=0)
847
848 // Get wavevector and magnitude
849 phi_1LPT_fourier.get_fourier_wavevector_and_norm2_by_index(fourier_index, kvec, kmag2);
850 // The mixed derivatives of the Nyquist modes are zero
851 const auto kodd = detail::odd_derivative_wavevector<N>(kvec, Nmesh);
852
853 // phi_1LPT_fourier is delta so transform to LPT potential D^2 phi_1LPT = -delta
854 auto value =
855 -phi_1LPT_fourier.get_fourier_from_index(fourier_index) / FML::GRID::FloatType(kmag2);
856
857 int pair = 0;
858 for (int idim1 = 0; idim1 < N; idim1++) {
859 for (int idim2 = idim1; idim2 < N; idim2++) {
860 const double kk =
861 idim1 == idim2 ? kvec[idim1] * kvec[idim1] : kodd[idim1] * kodd[idim2];
862 phi_1LPT_ij[pair++].set_fourier_from_index(fourier_index,
863 -value * FML::GRID::FloatType(kk));
864 }
865 }
866 }
867 }
868
869 // Deal with DC mode
870 if (Local_x_start == 0)
871 for (auto & g : phi_1LPT_ij)
872 g.set_fourier_from_index(0, 0.0);
873
874 // Fourier transform it all to real-space
875 for (int i = 0; i < num_pairs; i++)
876 phi_1LPT_ij[i].fftw_c2r();
877
878 // Compute 2LPT
879 phi_2LPT_fourier = FFTWGrid<N>(Nmesh, nleft, nright);
880 phi_2LPT_fourier.add_memory_label("FFTWGrid::compute_3LPT_potential_fourier::phi_2LPT_fourier");
881
882 if (FML::ThisTask == 0)
883 std::cout << "Computing phi_2LPT...\n";
884#ifdef USE_OMP
885#pragma omp parallel for
886#endif
887 for (int islice = 0; islice < Local_nx; islice++) {
888 for (auto && real_index : phi_2LPT_fourier.get_real_range(islice, islice + 1)) {
889 // Compute laplacian and sum of squares to get Sum_i,j Phi_iiPhi_jj Phi_ij^2
890 double laplacian = 0.0;
891 double sum_squared = 0.0;
892 int pair = 0;
893 for (int idim1 = 0; idim1 < N; idim1++) {
894 auto phi_ij = phi_1LPT_ij[pair++].get_real_from_index(real_index);
895 laplacian += phi_ij;
896 sum_squared += phi_ij * phi_ij;
897 for (int idim2 = idim1 + 1; idim2 < N; idim2++) {
898 phi_ij = phi_1LPT_ij[pair++].get_real_from_index(real_index);
899 sum_squared += 2.0 * phi_ij * phi_ij;
900 }
901 }
902 phi_2LPT_fourier.set_real_from_index(real_index, 0.5 * (laplacian * laplacian - sum_squared));
903 }
904 }
905
906 // Back to fourier space: We now have -k^2 phi_2LPT in this grid
907 phi_2LPT_fourier.fftw_r2c();
908
909 // The 3a source only needs the first-order Hessian. Form it before the second-order
910 // derivatives, then reuse a reduced batch of work grids for the mixed sources.
911 phi_3LPT_a_fourier = FFTWGrid<N>(Nmesh, nleft, nright);
912 phi_3LPT_a_fourier.add_memory_label("FFTWGrid::compute_3LPT_potential_fourier::phi_3LPT_a_fourier");
913#ifdef USE_OMP
914#pragma omp parallel for
915#endif
916 for (int islice = 0; islice < Local_nx; islice++) {
917 for (auto && index : phi_3LPT_a_fourier.get_real_range(islice, islice + 1)) {
918 if constexpr (N == 2) {
919 const auto xx = phi_1LPT_ij[0].get_real_from_index(index);
920 const auto xy = phi_1LPT_ij[1].get_real_from_index(index);
921 const auto yy = phi_1LPT_ij[2].get_real_from_index(index);
922 phi_3LPT_a_fourier.set_real_from_index(index, xx * yy - xy * xy);
923 } else {
924 const auto xx = phi_1LPT_ij[0].get_real_from_index(index);
925 const auto xy = phi_1LPT_ij[1].get_real_from_index(index);
926 const auto zx = phi_1LPT_ij[2].get_real_from_index(index);
927 const auto yy = phi_1LPT_ij[3].get_real_from_index(index);
928 const auto yz = phi_1LPT_ij[4].get_real_from_index(index);
929 const auto zz = phi_1LPT_ij[5].get_real_from_index(index);
930 auto value = xx * yy * zz;
931 value += 2.0 * xy * yz * zx;
932 value += -xx * yz * yz;
933 value += -yy * zx * zx;
934 value += -zz * xy * xy;
935 phi_3LPT_a_fourier.set_real_from_index(index, value);
936 }
937 }
938 }
939
940 phi_3LPT_b_fourier = FFTWGrid<N>(Nmesh, nleft, nright);
941 phi_3LPT_b_fourier.add_memory_label("FFTWGrid::compute_3LPT_potential_fourier::phi_3LPT_b_fourier");
942 if (not ignore_curl_term) {
943 constexpr int num_Avec = N == 2 ? 1 : N;
944 for (int dimension = 0; dimension < num_Avec; dimension++) {
945 phi_3LPT_Avec_fourier[dimension] = FFTWGrid<N>(Nmesh, nleft, nright);
946 phi_3LPT_Avec_fourier[dimension].add_memory_label(
947 "FFTWGrid::compute_3LPT_potential_fourier::phi_3LPT_Avec_fourier" +
948 std::to_string(dimension));
949 }
950 }
951
952 // Keep just one batch of H2 derivatives: diagonals first, then off-diagonals.
953 // Cache the diagonal contributions in the output grids before reusing the work grids.
954 // This avoids retaining all six H2 meshes and avoids six separate Fourier/source passes.
955 std::array<FFTWGrid<N>, N> hessian2;
956 for (int component = 0; component < N; component++) {
957 hessian2[component] = FFTWGrid<N>(Nmesh, nleft, nright);
958 hessian2[component].add_memory_label("FFTWGrid::compute_3LPT_potential_fourier::work_phi_2LPT_ij_" +
959 std::to_string(component));
960 }
961 auto make_hessian_batch = [&](auto diagonal_tag) {
962 constexpr bool diagonal = decltype(diagonal_tag)::value;
963 constexpr int count = diagonal ? N : N * (N - 1) / 2;
964 for (int component = 0; component < count; component++)
965 hessian2[component].set_grid_status_real(false);
966#ifdef USE_OMP
967#pragma omp parallel for
968#endif
969 for (int islice = 0; islice < Local_nx; islice++) {
970 double kmag2;
971 std::array<double, N> kvec;
972 for (auto && index : phi_2LPT_fourier.get_fourier_range(islice, islice + 1)) {
973 if (Local_x_start == 0 and index == 0) {
974 for (int component = 0; component < count; component++)
975 hessian2[component].set_fourier_from_index(index, 0.0);
976 continue;
977 }
978 phi_2LPT_fourier.get_fourier_wavevector_and_norm2_by_index(index, kvec, kmag2);
979 const auto value =
980 -phi_2LPT_fourier.get_fourier_from_index(index) / FML::GRID::FloatType(kmag2);
981 if constexpr (diagonal) {
982 for (int row = 0; row < N; row++)
983 hessian2[row].set_fourier_from_index(
984 index,
985 -value * FML::GRID::FloatType(kvec[row] * kvec[row]));
986 } else {
987 // The mixed derivatives of the Nyquist modes are zero
988 const auto kodd = detail::odd_derivative_wavevector<N>(kvec, Nmesh);
989 int component = 0;
990 for (int row = 0; row < N; row++)
991 for (int col = row + 1; col < N; col++)
992 hessian2[component++].set_fourier_from_index(
993 index,
994 -value * FML::GRID::FloatType(kodd[row] * kodd[col]));
995 }
996 }
997 }
998 for (int component = 0; component < count; component++)
999 hessian2[component].fftw_c2r();
1000 };
1001 make_hessian_batch(std::true_type{});
1002#ifdef USE_OMP
1003#pragma omp parallel for
1004#endif
1005 for (int islice = 0; islice < Local_nx; islice++) {
1006 for (auto && index : phi_3LPT_b_fourier.get_real_range(islice, islice + 1)) {
1007 const auto h2xx = hessian2[0].get_real_from_index(index);
1008 const auto h2yy = hessian2[1].get_real_from_index(index);
1009 if constexpr (N == 2) {
1010 const auto h1xx = phi_1LPT_ij[0].get_real_from_index(index);
1011 phi_3LPT_b_fourier.set_real_from_index(index, h1xx * h2yy);
1012 if (not ignore_curl_term)
1013 phi_3LPT_Avec_fourier[0].set_real_from_index(index, h2yy - h2xx);
1014 } else {
1015 const auto h1xx = phi_1LPT_ij[0].get_real_from_index(index);
1016 const auto h1yy = phi_1LPT_ij[3].get_real_from_index(index);
1017 const auto h1zz = phi_1LPT_ij[5].get_real_from_index(index);
1018 const auto h2zz = hessian2[2].get_real_from_index(index);
1019 auto value = 0.5 * h1xx * (h2yy + h2zz);
1020 value += 0.5 * h1yy * (h2zz + h2xx);
1021 value += 0.5 * h1zz * (h2xx + h2yy);
1022 phi_3LPT_b_fourier.set_real_from_index(index, value);
1023 // These differences retain the grid precision of the original curl expression.
1024 if (not ignore_curl_term) {
1025 phi_3LPT_Avec_fourier[0].set_real_from_index(index, h2yy - h2zz);
1026 phi_3LPT_Avec_fourier[1].set_real_from_index(index, h2zz - h2xx);
1027 phi_3LPT_Avec_fourier[2].set_real_from_index(index, h2xx - h2yy);
1028 }
1029 }
1030 }
1031 }
1032
1033 make_hessian_batch(std::false_type{});
1034#ifdef USE_OMP
1035#pragma omp parallel for
1036#endif
1037 for (int islice = 0; islice < Local_nx; islice++) {
1038 for (auto && index : phi_3LPT_b_fourier.get_real_range(islice, islice + 1)) {
1039 const auto h2xy = hessian2[0].get_real_from_index(index);
1040 const auto h1xx = phi_1LPT_ij[0].get_real_from_index(index);
1041 const auto h1xy = phi_1LPT_ij[1].get_real_from_index(index);
1042 if constexpr (N == 2) {
1043 phi_3LPT_b_fourier.set_real_from_index(index,
1044 phi_3LPT_b_fourier.get_real_from_index(index) -
1045 h1xy * h2xy);
1046 if (not ignore_curl_term) {
1047 const auto h1yy = phi_1LPT_ij[2].get_real_from_index(index);
1048 const auto difference = phi_3LPT_Avec_fourier[0].get_real_from_index(index);
1049 phi_3LPT_Avec_fourier[0].set_real_from_index(index,
1050 h2xy * (h1yy - h1xx) - h1xy * difference);
1051 }
1052 } else {
1053 const auto h1xz = phi_1LPT_ij[2].get_real_from_index(index);
1054 const auto h1yy = phi_1LPT_ij[3].get_real_from_index(index);
1055 const auto h1yz = phi_1LPT_ij[4].get_real_from_index(index);
1056 const auto h1zz = phi_1LPT_ij[5].get_real_from_index(index);
1057 const auto h2xz = hessian2[1].get_real_from_index(index);
1058 const auto h2yz = hessian2[2].get_real_from_index(index);
1059 // Preserve the original accumulator type: at least double because of the factor 0.5.
1060 using SourceType = decltype(0.5 * FML::GRID::FloatType{});
1061 SourceType value = phi_3LPT_b_fourier.get_real_from_index(index);
1062 value += -h1xy * h2xy - h1yz * h2yz - h1xz * h2xz;
1063 phi_3LPT_b_fourier.set_real_from_index(index, value);
1064 if (not ignore_curl_term) {
1065 auto Ax = h1xz * h2xy - h2xz * h1xy;
1066 Ax += h1yz * phi_3LPT_Avec_fourier[0].get_real_from_index(index) - h2yz * (h1yy - h1zz);
1067 auto Ay = h1xy * h2yz - h2xy * h1yz;
1068 Ay += h1xz * phi_3LPT_Avec_fourier[1].get_real_from_index(index) - h2xz * (h1zz - h1xx);
1069 auto Az = h1yz * h2xz - h2yz * h1xz;
1070 Az += h1xy * phi_3LPT_Avec_fourier[2].get_real_from_index(index) - h2xy * (h1xx - h1yy);
1071 phi_3LPT_Avec_fourier[0].set_real_from_index(index, Ax);
1072 phi_3LPT_Avec_fourier[1].set_real_from_index(index, Ay);
1073 phi_3LPT_Avec_fourier[2].set_real_from_index(index, Az);
1074 }
1075 }
1076 }
1077 }
1078 for (auto & grid : hessian2)
1079 grid.free();
1080 for (auto & grid : phi_1LPT_ij)
1081 grid.free();
1082
1083 // Fourier transform and voila we have -k^2phi_3LPT_a stored in phi_3LPT_a
1084 phi_3LPT_a_fourier.fftw_r2c();
1085
1086 // Fourier transform and voila we have -k^2phi_3LPT_b stored in phi_3LPT_b
1087 phi_3LPT_b_fourier.fftw_r2c();
1088
1089 // Fourier transform and voila we have -k^2phi_3LPT_Avec stored in phi_3LPT_Avec
1090 // (for N=2 only the 'z' component, phi_3LPT_Avec_fourier[0], is allocated and used)
1091 if (not ignore_curl_term) {
1092 constexpr int num_Avec = N == 2 ? 1 : N;
1093 for (int idim = 0; idim < num_Avec; idim++)
1094 phi_3LPT_Avec_fourier[idim].fftw_r2c();
1095 }
1096
1097 // Divide by -1/k^2 and multiply by factor to make Psi = Dphi^1LPT + Dphi^2LPT + Dphi^3LPT + D x
1098 // Avec^3LPT
1099#ifdef USE_OMP
1100#pragma omp parallel for
1101#endif
1102 for (int islice = 0; islice < Local_nx; islice++) {
1103 [[maybe_unused]] double kmag2;
1104 [[maybe_unused]] std::array<double, N> kvec;
1105 for (auto && fourier_index : phi_1LPT_fourier.get_fourier_range(islice, islice + 1)) {
1106 if (Local_x_start == 0 and fourier_index == 0)
1107 continue; // DC mode (k=0)
1108
1109 // Get wavevector and magnitude
1110 phi_1LPT_fourier.get_fourier_wavevector_and_norm2_by_index(fourier_index, kvec, kmag2);
1111 const FML::GRID::FloatType fac = -1.0 / kmag2;
1112
1113 // Fetch and rescale
1114 auto value_1LPT = phi_1LPT_fourier.get_fourier_from_index(fourier_index);
1115 auto value_2LPT = phi_2LPT_fourier.get_fourier_from_index(fourier_index);
1116 auto value_3LPT_a = phi_3LPT_a_fourier.get_fourier_from_index(fourier_index);
1117 auto value_3LPT_b = phi_3LPT_b_fourier.get_fourier_from_index(fourier_index);
1118 phi_1LPT_fourier.set_fourier_from_index(fourier_index, prefactor_1LPT * value_1LPT * fac);
1119 phi_2LPT_fourier.set_fourier_from_index(fourier_index, prefactor_2LPT * value_2LPT * fac);
1120 phi_3LPT_a_fourier.set_fourier_from_index(fourier_index, prefactor_3LPT_a * value_3LPT_a * fac);
1121 phi_3LPT_b_fourier.set_fourier_from_index(fourier_index, prefactor_3LPT_b * value_3LPT_b * fac);
1122
1123 // The vector potential A
1124 if (not ignore_curl_term) {
1125 if constexpr (N == 2) {
1126 // For N=2 the curl is a scalar and we only store the "z" component
1127 auto value_Az = phi_3LPT_Avec_fourier[0].get_fourier_from_index(fourier_index);
1128 phi_3LPT_Avec_fourier[0].set_fourier_from_index(fourier_index,
1129 prefactor_3LPT_Avec * value_Az * fac);
1130 }
1131 if constexpr (N == 3) {
1132 auto value_Ax = phi_3LPT_Avec_fourier[0].get_fourier_from_index(fourier_index);
1133 auto value_Ay = phi_3LPT_Avec_fourier[1].get_fourier_from_index(fourier_index);
1134 auto value_Az = phi_3LPT_Avec_fourier[2].get_fourier_from_index(fourier_index);
1135 phi_3LPT_Avec_fourier[0].set_fourier_from_index(fourier_index,
1136 prefactor_3LPT_Avec * value_Ax * fac);
1137 phi_3LPT_Avec_fourier[1].set_fourier_from_index(fourier_index,
1138 prefactor_3LPT_Avec * value_Ay * fac);
1139 phi_3LPT_Avec_fourier[2].set_fourier_from_index(fourier_index,
1140 prefactor_3LPT_Avec * value_Az * fac);
1141 }
1142 }
1143 }
1144 }
1145 }
Reusable geometry for normalized, non-transposed FFTWGrid slab layouts.
void fftw_c2r(FFTWGrid< N > &in_grid, FFTWGrid< N > &out_grid)
Definition FFTWGrid.h:1431
void fftw_r2c(FFTWGrid< N > &in_grid, FFTWGrid< N > &out_grid)
Definition FFTWGrid.h:1442
Vector grid(size_t n)
double difference(const Grid< N > &a, const Grid< N > &b)

◆ compute_ALPT_potential_fourier()

template<int N>
void FML::COSMOLOGY::LPT::compute_ALPT_potential_fourier ( const FFTWGrid< N > & delta_fourier,
const FFTWGrid< N > & phi_2LPT_fourier,
FFTWGrid< N > & phi_ALPT_fourier,
double smoothing_scale,
std::string smoothing_method,
double DoverDini_1LPT,
double DoverDini_2LPT )

Generate the APT potential defined as \( \Psi = \Psi^L + \Psi^S \) where the long range is just a smoothed 1LPT+2LPT and the short range is the spherical collapse approximation.

Template Parameters
NThe dimension of the grid
Parameters
[in]delta_fourierThe density contrast in fourier space
[in]phi_2LPT_fourierThe 2LPT potential in fourier space
[out]phi_ALPT_fourierThe ALPT potential in fourier space
[in]smoothing_scaleThe smoothing scale in the unit of the boxsize
[in]smoothing_methodThe smoothing filter (gaussian, tophat, sharpk)
[in]DoverDini_1LPTThe 1LPT growth-factor at the time we want phi_ALPT_fourier divide by the growth factor at the redshift delta_fourier is at
[in]DoverDini_2LPTThe 2LPT growth-factor at the time we want phi_ALPT_fourier divide by the growth factor at the redshift delta_fourier is at

Definition at line 1232 of file DisplacementFields.h.

1238 {
1239
1240 assert_mpi(delta_fourier.get_nmesh() > 0,
1241 "[compute_ALPT_potential_fourier] delta_fourier grid has to be already allocated");
1242 assert_mpi(phi_2LPT_fourier.get_nmesh() > 0,
1243 "[compute_ALPT_potential_fourier] phi_2LPT_fourier grid has to be already allocated");
1244 assert_mpi(phi_2LPT_fourier.get_nmesh() == delta_fourier.get_nmesh(),
1245 "[compute_ALPT_potential_fourier] phi_2LPT_fourier grid and delta_fourier must have the "
1246 "same size");
1247
1248 [[maybe_unused]] auto Nmesh = delta_fourier.get_nmesh();
1249 auto Local_nx = delta_fourier.get_local_nx();
1250 auto Local_x_start = delta_fourier.get_local_x_start();
1251
1252 // Compute the short range spherical collapse approximation
1253 // D*Psi_SC = 3( sqrt(1 - 2/3 * D/Dini delta_ini) - 1 )
1254 FFTWGrid<N> phi_sc_fourier;
1255 compute_spherical_collapse_potential(delta_fourier, phi_sc_fourier, DoverDini_1LPT);
1256
1257 // Copy over the long range 2LPT approximation
1258 phi_ALPT_fourier = phi_2LPT_fourier;
1259 phi_ALPT_fourier.add_memory_label("FFTWGrid::compute_ALPT_potential_fourier::phi_ALPT_fourier");
1260 phi_ALPT_fourier.set_grid_status_real(false);
1261
1262 // Compute (phi_1LPT + phi_2LPT - phi_SC)
1263#ifdef USE_OMP
1264#pragma omp parallel for
1265#endif
1266 for (int islice = 0; islice < Local_nx; islice++) {
1267 [[maybe_unused]] double kmag2;
1268 [[maybe_unused]] std::array<double, N> kvec;
1269 for (auto && fourier_index : phi_ALPT_fourier.get_fourier_range(islice, islice + 1)) {
1270 if (Local_x_start == 0 and fourier_index == 0)
1271 continue; // DC mode (k=0)
1272 // Get wavevector and magnitude
1273 phi_ALPT_fourier.get_fourier_wavevector_and_norm2_by_index(fourier_index, kvec, kmag2);
1274
1275 auto short_range = phi_sc_fourier.get_fourier_from_index(fourier_index);
1276 // phi_1LPT = delta / k^2 (D^2 phi_1LPT = -delta)
1277 auto p1LPT = delta_fourier.get_fourier_from_index(fourier_index) / FML::GRID::FloatType(kmag2);
1278 auto p2LPT = phi_ALPT_fourier.get_fourier_from_index(fourier_index);
1279 auto long_range = p1LPT * DoverDini_1LPT + p2LPT * DoverDini_2LPT;
1280 phi_ALPT_fourier.set_fourier_from_index(fourier_index, long_range - short_range);
1281 }
1282 }
1283
1284 // Smooth (phi_1LPT + phi_2LPT - phi_SC)
1285 FML::GRID::smoothing_filter_fourier_space(phi_ALPT_fourier, smoothing_scale, smoothing_method);
1286
1287 // Set phi_ALPT = phi_SC + Smooth(phi_1LPT + phi_2LPT - phi_SC)
1288#ifdef USE_OMP
1289#pragma omp parallel for
1290#endif
1291 for (int islice = 0; islice < Local_nx; islice++) {
1292 for (auto && fourier_index : phi_ALPT_fourier.get_fourier_range(islice, islice + 1)) {
1293 auto short_range = phi_sc_fourier.get_fourier_from_index(fourier_index);
1294 auto smoothed = phi_ALPT_fourier.get_fourier_from_index(fourier_index);
1295 phi_ALPT_fourier.set_fourier_from_index(fourier_index, short_range + smoothed);
1296 }
1297 }
1298 }
void compute_spherical_collapse_potential(const FFTWGrid< N > &delta_fourier, FFTWGrid< N > &phi_sc_fourier, double DoverDini=1.0)
Take in an initial density field (generated at a redshift zini) and the corresponding ratio of growth...
void smoothing_filter_fourier_space(FFTWGrid< N > &fourier_grid, double smoothing_scale, std::string smoothing_method)

◆ compute_LPT_growth_factors_GeffGLCDM()

void FML::COSMOLOGY::LPT::compute_LPT_growth_factors_GeffGLCDM ( double OmegaM,
double zini,
std::function< double(double)> HoverH0_of_a,
std::function< double(double)> GeffOverG_of_a,
FML::INTERPOLATION::SPLINE::Spline & D_1LPT_of_loga,
FML::INTERPOLATION::SPLINE::Spline & D_2LPT_of_loga,
FML::INTERPOLATION::SPLINE::Spline & D_3LPTa_of_loga,
FML::INTERPOLATION::SPLINE::Spline & D_3LPTb_of_loga )
inline

Compute 1,2,3LPT growth-factors in simple modified gravity models that have a GeffG(a) and spline them The growth factors are normalized such that D1LPT = 1 at zini We assume initial conditions as in EdS: D1LPT=1, D2LPT = -3/7, D3LPTa = -1/3 and D3LPTb = 5/21 NB: There should be a modified 2LPT+ kernel factor in general which we don't include here (model dependent)

Parameters
[in]OmegaMMatter density parameter at z=0
[in]ziniThe redshift we want D1LPT = 1
[in]HoverH0_of_aThe hubble function H(a)/H0
[in]GeffOverG_of_aNewtons constant Geff/G as function of a
[out]D_1LPT_of_logaSpline of the 1LPT growth factor
[out]D_2LPT_of_logaSpline of the 2LPT growth factor
[out]D_3LPTa_of_logaSpline of the 3LPTa growth factor
[out]D_3LPTb_of_logaSpline of the 3LPTb growth factor

The friction term dlogH/dloga is computed from HoverH0_of_a by a (central) numerical derivative so any background H(a) can be used (not only flat LCDM). The splines cover 1/1000 <= a <= 1.5.

Definition at line 1322 of file DisplacementFields.h.

1329 {
1330
1331 using DVector = std::vector<double>;
1332
1333 // Start at z = 1000 assuming no radiation, but this is fine because we want the solution to follow
1334 // the matter attractor
1335 // We integrate a bit beyond a = 1 so that the splines cover a = 1 (and slightly later times)
1336 const int npts = 2000;
1337 const double aini = 1.0 / 1000.0;
1338 const double aend = 1.5;
1339
1340 // dlogH/dloga computed numerically from the H(a) we are given (central difference in loga)
1341 const double dx_deriv = 1e-4;
1342 auto dlogHdx_of_x = [&](double x) {
1343 return (std::log(HoverH0_of_a(std::exp(x + dx_deriv))) -
1344 std::log(HoverH0_of_a(std::exp(x - dx_deriv)))) /
1345 (2.0 * dx_deriv);
1346 };
1347
1348 FML::SOLVERS::ODESOLVER::ODEFunction deriv = [&](double x, const double * y, double * dydx) {
1349 const double a = std::exp(x);
1350 const double H = HoverH0_of_a(a);
1351 const double dlogHdx = dlogHdx_of_x(x);
1352 const double factor = 1.5 * OmegaM * GeffOverG_of_a(a) / (H * H * a * a * a);
1353 const double D1 = y[0];
1354 const double dD1dx = y[1];
1355 const double D2 = y[2];
1356 const double dD2dx = y[3];
1357 const double D3a = y[4];
1358 const double dD3adx = y[5];
1359 const double D3b = y[6];
1360 const double dD3bdx = y[7];
1361 dydx[0] = dD1dx;
1362 dydx[1] = factor * D1 - (2.0 + dlogHdx) * dD1dx;
1363 dydx[2] = dD2dx;
1364 dydx[3] = factor * (D2 - D1 * D1) - (2.0 + dlogHdx) * dD2dx;
1365 dydx[4] = dD3adx;
1366 dydx[5] = factor * (D3a - 2.0 * D1 * D1 * D1) - (2.0 + dlogHdx) * dD3adx;
1367 dydx[6] = dD3bdx;
1368 dydx[7] = factor * (D3b + D1 * D1 * D1 - D2 * D1) - (2.0 + dlogHdx) * dD3bdx;
1369 return GSL_SUCCESS;
1370 };
1371
1372 const double D1_ini = 1.0;
1373 const double dD1dx_ini = 1.0 * 1.0;
1374 const double D2_ini = -3.0 / 7.0;
1375 const double dD2dx_ini = -3.0 / 7.0 * 2.0;
1376 const double D3a_ini = -1.0 / 3.0;
1377 const double dD3adx_ini = -1.0 / 3.0 * 3.0;
1378 const double D3b_ini = 5.0 / 21.0;
1379 const double dD3bdx_ini = 5.0 / 21.0 * 3.0;
1380
1381 // The initial conditions
1382 // D1 = a/aini, D2 = -3/7 D1^2, D3a = -1/3 D1^3 and D3b = 5/21 D1^3 for growing mode in EdS
1383 DVector yini{D1_ini, dD1dx_ini, D2_ini, dD2dx_ini, D3a_ini, dD3adx_ini, D3b_ini, dD3bdx_ini};
1384 DVector xarr(npts);
1385 for (int i = 0; i < npts; i++)
1386 xarr[i] = std::log(aini) + std::log(aend / aini) * i / double(npts - 1);
1387
1388 // Solve the ODE
1390 ode.solve(deriv, xarr, yini);
1391 auto D1 = ode.get_data_by_component(0);
1392 auto D2 = ode.get_data_by_component(2);
1393 auto D3a = ode.get_data_by_component(4);
1394 auto D3b = ode.get_data_by_component(6);
1395
1397 tmp.create(xarr, D1, "D1(loga) Spline");
1398 const double D = tmp(std::log(1.0 / (1.0 + zini)));
1399
1400 // Normalize such that D1LPT = 1 at zini
1401 for (int i = 0; i < npts; i++) {
1402 D1[i] /= D;
1403 D2[i] /= D * D;
1404 D3a[i] /= D * D * D;
1405 D3b[i] /= D * D * D;
1406 }
1407
1408 // Print some values
1409 if (FML::ThisTask == 0) {
1410 std::cout << "[compute_LPT_growth_factors_LCDM] Scalefactor Growth factors (D1LPT / a, "
1411 "D2LPT / D1LPT^2, D3LPTa / D1LPT^3, D3LPTb / "
1412 "D1LPT^3)\n";
1413 for (int i = 0; i < npts; i += npts / 15)
1414 std::cout << std::exp(xarr[i]) << " " << D1[i] / (1.0 + zini) / std::exp(xarr[i]) << " "
1415 << D2[i] / (D1[i] * D1[i]) << " " << D3a[i] / (D1[i] * D1[i] * D1[i]) << " "
1416 << D3b[i] / (D1[i] * D1[i] * D1[i]) << " "
1417 << "\n";
1418 }
1419
1420 // Spline it up
1421 D_1LPT_of_loga.create(xarr, D1, "D1(loga) Spline");
1422 D_2LPT_of_loga.create(xarr, D2, "D2(loga) Spline");
1423 D_3LPTa_of_loga.create(xarr, D3a, "D3a(loga) Spline");
1424 D_3LPTb_of_loga.create(xarr, D3b, "D3b(loga) Spline");
1425 }
FML::SOLVERS::FFTLog::DVector DVector
Definition Main.cpp:4
FML::INTERPOLATION::SPLINE::Spline D_3LPTa_of_loga
Definition Main.cpp:73
FML::INTERPOLATION::SPLINE::Spline D_2LPT_of_loga
Definition Main.cpp:72
const double aend
Definition Main.cpp:42
const double OmegaM
Definition Main.cpp:65
FML::INTERPOLATION::SPLINE::Spline D_3LPTb_of_loga
Definition Main.cpp:74
const double zini
Definition Main.cpp:40
FML::INTERPOLATION::SPLINE::Spline D_1LPT_of_loga
Definition Main.cpp:71
const double aini
Definition Main.cpp:41
This is a wrapper class for easy use of GSL splines It is (OpenMP) thread safe as long as splines are...
Definition Spline.h:106
void create(const double *x, const double *y, int nx, std::string splinename="NoName", const gsl_interp_type *interpoltype=SPLINE_FIDUCIAL_INTERPOL_TYPE)
Create a spline from pointers to x and y. Both must have nx elements.
Definition Spline.cpp:159
This is a wrapper around the GSL library to easily solve ODEs and return the data in whatever format ...
Definition ODESolver.h:97
DVector get_data_by_component(int icomponent) const
Get the data for a particular component y_i(xarr)
void solve(ODEFunctionPointer ode_equation, void *parameters, const DVector &xarr, const DVector &yinitial, const gsl_odeiv2_step_type *stepper=ODESOLVER_FIDUCIAL_STEPPER, ODEFunctionPointerJacobian jacobian=nullptr)
std::vector< double > DVector
std::function< int(double, const double *, double *)> ODEFunction
Definition ODESolver.h:33

◆ compute_LPT_growth_factors_LCDM()

void FML::COSMOLOGY::LPT::compute_LPT_growth_factors_LCDM ( double OmegaM,
double zini,
std::function< double(double)> HoverH0_of_a,
FML::INTERPOLATION::SPLINE::Spline & D_1LPT_of_loga,
FML::INTERPOLATION::SPLINE::Spline & D_2LPT_of_loga,
FML::INTERPOLATION::SPLINE::Spline & D_3LPTa_of_loga,
FML::INTERPOLATION::SPLINE::Spline & D_3LPTb_of_loga )
inline

Compute 1,2,3LPT growth-factors in LCDM and spline them The growth factors are normalized such that D1LPT = 1 at zini We assume initial conditions as in EdS: D1LPT=1, D2LPT = -3/7, D3LPTa = -1/3 and D3LPTb = 5/21.

Parameters
[in]OmegaMMatter density parameter at z=0
[in]ziniThe redshift we want D1LPT = 1
[in]HoverH0_of_aThe hubble function H(a)/H0
[out]D_1LPT_of_logaSpline of the 1LPT growth factor
[out]D_2LPT_of_logaSpline of the 2LPT growth factor
[out]D_3LPTa_of_logaSpline of the 3LPTa growth factor
[out]D_3LPTb_of_logaSpline of the 3LPTb growth factor

The friction term dlogH/dloga is computed from HoverH0_of_a (numerical derivative) so this also works for e.g. wCDM backgrounds (OmegaM only enters the source term). The splines cover 1/1000 <= a <= 1.5.

Definition at line 1444 of file DisplacementFields.h.

1450 {
1451
1452 auto GeffOverG_of_a = []([[maybe_unused]] double a) { return 1.0; };
1453 compute_LPT_growth_factors_GeffGLCDM(OmegaM,
1454 zini,
1455 HoverH0_of_a,
1456 GeffOverG_of_a,
1461 }

◆ compute_spherical_collapse_potential()

template<int N>
void FML::COSMOLOGY::LPT::compute_spherical_collapse_potential ( const FFTWGrid< N > & delta_fourier,
FFTWGrid< N > & phi_sc_fourier,
double DoverDini_1LPT )

Take in an initial density field (generated at a redshift zini) and the corresponding ratio of growth factors D(z)/D(zini) produces the approximate spherical collapse potential defined via \( \nabla^2 \phi_{\rm SC} = 3(\sqrt{\max(1 - \frac{2}{3}\delta(x,z), 0)} - 1) \) at the redshift z with \( \delta(x,z) = \frac{D(z)}{D(z_{\rm ini})}\delta_{\rm ini}(x) \).

The displacement field is \( \Psi_{\rm SC} = \nabla\phi_{\rm SC} \), i.e. we return \( \phi_{\rm SC}(k) = -{\rm source}(k)/k^2 \) (with the k=0 mode set to zero). To linear order \( \phi_{\rm SC} = \phi_{\rm 1LPT} \).

Template Parameters
NThe dimension of the grid
Parameters
[in]delta_fourierThe density contrast in fourier space
[out]phi_sc_fourierThe spherical collapse approximation potential in fourier space
[in]DoverDini_1LPTThe 1LPT growth-factor at the time we want phi_ALPT_fourier divide by the growth factor at the redshift delta_fourier is at

Definition at line 1164 of file DisplacementFields.h.

1166 {
1167
1168 assert_mpi(delta_fourier.get_nmesh() > 0,
1169 "[compute_spherical_collapse_potential] delta_fourier grid has to be already allocated");
1170
1171 [[maybe_unused]] auto Nmesh = delta_fourier.get_nmesh();
1172 auto Local_nx = delta_fourier.get_local_nx();
1173 auto Local_x_start = delta_fourier.get_local_x_start();
1174
1175 phi_sc_fourier = delta_fourier;
1176 phi_sc_fourier.add_memory_label("FFTWGrid::compute_spherical_collapse_potential::phi_sc_fourier");
1177 phi_sc_fourier.set_grid_status_real(false);
1178 phi_sc_fourier.fftw_c2r();
1179#ifdef USE_OMP
1180#pragma omp parallel for
1181#endif
1182 for (int islice = 0; islice < Local_nx; islice++) {
1183 for (auto && real_index : phi_sc_fourier.get_real_range(islice, islice + 1)) {
1184 auto delta_ini = phi_sc_fourier.get_real_from_index(real_index);
1185 double value = 1.0 - 2.0 / 3.0 * delta_ini * DoverDini_1LPT;
1186 value = std::max(value, 0.0);
1187 phi_sc_fourier.set_real_from_index(real_index, 3.0 * (std::sqrt(value) - 1.0));
1188 }
1189 }
1190 phi_sc_fourier.fftw_r2c();
1191
1192 // We now have the source D^2 phi_SC in the grid. Solve for the potential: phi_SC(k) = -source(k) / k^2
1193#ifdef USE_OMP
1194#pragma omp parallel for
1195#endif
1196 for (int islice = 0; islice < Local_nx; islice++) {
1197 [[maybe_unused]] double kmag2;
1198 [[maybe_unused]] std::array<double, N> kvec;
1199 for (auto && fourier_index : phi_sc_fourier.get_fourier_range(islice, islice + 1)) {
1200 if (Local_x_start == 0 and fourier_index == 0)
1201 continue; // DC mode (k=0)
1202 phi_sc_fourier.get_fourier_wavevector_and_norm2_by_index(fourier_index, kvec, kmag2);
1203 auto value = phi_sc_fourier.get_fourier_from_index(fourier_index);
1204 phi_sc_fourier.set_fourier_from_index(fourier_index, -value / FML::GRID::FloatType(kmag2));
1205 }
1206 }
1207
1208 // Set the DC mode
1209 if (Local_x_start == 0)
1210 phi_sc_fourier.set_fourier_from_index(0, 0.0);
1211 }

◆ from_LPT_potential_to_displacement_vector()

template<int N>
void FML::COSMOLOGY::LPT::from_LPT_potential_to_displacement_vector ( const FFTWGrid< N > & phi,
std::array< FFTWGrid< N >, N > & psi,
double DoverDini )

Generate the displaceement field \( \Psi = \nabla \phi \) from the LPT potential \( \phi \).

Template Parameters
NThe dimension of the grid
Parameters
[in]phiThe LPT potential in fourier space
[out]psiThe displacement vector in real space
[in]DoverDiniThe growth factor at the time you want the displacement field to the growth factor at which phi is at

Definition at line 221 of file DisplacementFields.h.

223 {
224
225 // We require phi to exist and if psi exists it must have the same size as phi
226 assert_mpi(phi.get_nmesh() > 0,
227 "[from_LPT_potential_to_displacement_vector] Grid has to be already allocated!");
228
229#ifdef DEBUG_LPT
230 if (FML::ThisTask == 0)
231 std::cout << "From LPT potential to displaceent vector\n";
232#endif
233
234 auto nleft = phi.get_n_extra_slices_left();
235 auto nright = phi.get_n_extra_slices_right();
236 auto Nmesh = phi.get_nmesh();
237 auto Local_nx = phi.get_local_nx();
238 auto Local_x_start = phi.get_local_x_start();
239
240 // Create the output grids if they don't exist already
241 for (int idim = 0; idim < N; idim++) {
242 if (psi[idim].get_nmesh() == 0) {
243 psi[idim] = FFTWGrid<N>(Nmesh, nleft, nright);
244 psi[idim].add_memory_label("FFTWGrid::from_LPT_potential_to_displacement_vector::Psi_" +
245 std::to_string(idim));
246 psi[idim].set_grid_status_real(false);
247 }
248 }
249
250#ifdef USE_OMP
251#pragma omp parallel for
252#endif
253 for (int islice = 0; islice < Local_nx; islice++) {
254 [[maybe_unused]] double kmag;
255 [[maybe_unused]] std::array<double, N> kvec;
256 std::complex<FML::GRID::FloatType> I(0, 1);
257 for (auto && fourier_index : psi[0].get_fourier_range(islice, islice + 1)) {
258 if (Local_x_start == 0 and fourier_index == 0)
259 continue; // DC mode (k=0)
260
261 // Get wavevector and magnitude
262 phi.get_fourier_wavevector_and_norm_by_index(fourier_index, kvec, kmag);
263 const auto kodd = detail::odd_derivative_wavevector<N>(kvec, Nmesh);
264
265 // Psi_vec = D Phi => F[Psi_vec] = ik_vec F[Phi] (zero for Nyquist components of k_vec)
266 auto value = phi.get_fourier_from_index(fourier_index) * FML::GRID::FloatType(DoverDini);
267 for (int idim = 0; idim < N; idim++) {
268 psi[idim].set_fourier_from_index(fourier_index,
269 I * value * FML::GRID::FloatType(kodd[idim]));
270 }
271 }
272 }
273
274 // Deal with DC mode
275 if (Local_x_start == 0)
276 for (int idim = 0; idim < N; idim++)
277 psi[idim].set_fourier_from_index(0, 0.0);
278
279 // Fourier transform Psi
280 for (int idim = 0; idim < N; idim++) {
281#ifdef DEBUG_LPT
282 if (FML::ThisTask == 0)
283 std::cout << "Fourier transforming Dphi to real space: " << idim + 1 << " / " << N << "\n";
284#endif
285 psi[idim].fftw_c2r();
286 }
287 }

◆ from_LPT_potential_to_displacement_vector_scaledependent()

template<int N>
void FML::COSMOLOGY::LPT::from_LPT_potential_to_displacement_vector_scaledependent ( const FFTWGrid< N > & phi,
std::array< FFTWGrid< N >, N > & psi,
std::function< double(double)> DoverDini_of_k )

Function is the ratio of the scale-dependent growth-factor at the time we want to generate particles to the time where phi was generated at as function of k.

Template Parameters
NThe dimension of the grid
Parameters
[in]phiThe LPT potential in fourier space
[out]psiThe displacement vector in real space
[in]DoverDini_of_kThe function D(k,z)/D(k,zini) as function of k.

Definition at line 135 of file DisplacementFields.h.

137 {
138
139 // We require phi to exist and if psi exists it must have the same size as phi
140 assert_mpi(phi.get_nmesh() > 0,
141 "[from_LPT_potential_to_displacement_vector_scaledependent] phi grid has to be already "
142 "allocated");
143
144#ifdef DEBUG_LPT
145 if (FML::ThisTask == 0)
146 std::cout << "From LPT potential to displacement field scaledependent\n";
147#endif
148
149 auto nleft = phi.get_n_extra_slices_left();
150 auto nright = phi.get_n_extra_slices_right();
151 auto Nmesh = phi.get_nmesh();
152 auto Local_nx = phi.get_local_nx();
153 auto Local_x_start = phi.get_local_x_start();
154
155 // Create the output grids if they don't exist already
156 for (int idim = 0; idim < N; idim++) {
157 if (psi[idim].get_nmesh() == 0) {
158 psi[idim] = FFTWGrid<N>(Nmesh, nleft, nright);
159 psi[idim].add_memory_label(
160 "FFTWGrid::from_LPT_potential_to_displacement_vector_scaledependent::Psi_" +
161 std::to_string(idim));
162 psi[idim].set_grid_status_real(false);
163 }
164 }
165
166 // Make a spline of the function (faster) if we have GSL otherwise this is
167 // just a copy of the function itself
168 auto DoverDini_of_k_spline = phi.make_fourier_spline(DoverDini_of_k, "D(k)/Dini(k)");
169#ifdef USE_OMP
170#pragma omp parallel for
171#endif
172 for (int islice = 0; islice < Local_nx; islice++) {
173 [[maybe_unused]] double kmag;
174 [[maybe_unused]] std::array<double, N> kvec;
175 std::complex<FML::GRID::FloatType> I(0, 1);
176 for (auto && fourier_index : phi.get_fourier_range(islice, islice + 1)) {
177 if (Local_x_start == 0 and fourier_index == 0)
178 continue; // DC mode (k=0)
179
180 // Get wavevector and magnitude
181 phi.get_fourier_wavevector_and_norm_by_index(fourier_index, kvec, kmag);
182 const auto kodd = detail::odd_derivative_wavevector<N>(kvec, Nmesh);
183
184 // Psi_vec = D Phi => F[Psi_vec] = ik_vec F[Phi] (zero for Nyquist components of k_vec)
185 auto value = phi.get_fourier_from_index(fourier_index) * I *
186 FML::GRID::FloatType(DoverDini_of_k_spline(kmag));
187
188 for (int idim = 0; idim < N; idim++) {
189 psi[idim].set_fourier_from_index(fourier_index, value * FML::GRID::FloatType(kodd[idim]));
190 }
191 }
192 }
193
194 // Deal with DC mode
195 if (Local_x_start == 0)
196 for (int idim = 0; idim < N; idim++)
197 psi[idim].set_fourier_from_index(0, 0.0);
198
199 // Fourier transform Psi
200 for (int idim = 0; idim < N; idim++) {
201#ifdef DEBUG_LPT
202 if (FML::ThisTask == 0)
203 std::cout << "Fourier transforming Dphi to real space: " << idim + 1 << " / " << N << "\n";
204#endif
205 psi[idim].fftw_c2r();
206 }
207 }

◆ RSDReconstructionFourierMethod()

template<int N, class T >
void FML::COSMOLOGY::LPT::RSDReconstructionFourierMethod ( MPIParticles< T > & part,
std::string density_assignment_method,
std::vector< double > los_direction,
int Nmesh,
int niterations,
double beta,
std::pair< std::string, double > smoothing_options,
bool survey_data )

RSD removal for particles in a periodic box We use a fixed line of sight direction los_direction, typically coordinate axes (e.g.

los_direction = {0,0,1} ) We are solving the equation LPT equation \( \nabla(b\Psi) + \beta \nabla((b\Psi\cdot r)r) = -\delta_{\rm tracer} \) and assuming zero-curl (which is not perfectly true) of the second term so we can use Fourier methods. Then we subtract the RSD which is given by \( \Psi_{\rm rsd} = \beta (b\Psi\cdot r)r) \) Here \( \beta = f / b \) growth-rate over galaxy bias. The smoothing filter is a filter we apply to the density field in k-space. It is always applied (also when R is smaller than the grid spacing: e.g. a gaussian filter still damps the modes by exp(-k^2R^2/2)). Use R = 0 for no smoothing.

NB: the window of the density assignment (particles to grid) and of the interpolation of the displacement field back to the particles (CIC) are not deconvolved.

The iterative scheme of Burden et al. 2015 (arXiv:1504.02591): with \( s \) the observed (redshift-space) positions and \( x_0 = s \), iteration n computes \( b\Psi_n \) from the density of the positions \( x_{n-1} \) and sets \( x_n = s - \beta (b\Psi_n(s)\cdot r)r \). The first iteration overestimates the shift (the density is in redshift space); the iterations converge for \( \beta < 1 \) (3-5 iterations are enough).

Only a periodic box with a fixed line of sight is supported (survey_data = true, a line of sight towards an observer, is not implemented and aborts).

This method assumes scale-independent growth factor, but that is easily changed

Template Parameters
NThe dimension of the grid
TThe particle class
Parameters
[out]partMPIParticles. Particles gets updated.
[in]density_assignment_methodThe density assignment method (NGP, CIC, TSC, PCS, PQS)
[in]los_directionThe fixed line of sight direction, e.g. (0,0,1) for the z-axis
[in]NmeshThe size of the grid we use
[in]niterationsHow many iterations to do (not too many and not too few, 3 is good)
[in]betaThis is beta = f/b the growth-rate over the bias
[in]smoothing_optionsThe smoothing filter (gaussian, tophat, sharpk) and the smothing scale (in units of the boxsize)
[in]survey_dataMust be false (survey data is not supported)

Definition at line 84 of file Reconstruction.h.

91 {
92
93 static_assert(FML::PARTICLE::has_get_pos<T>(),
94 "[RSDReconstructionFourierMethod] Particle must have a get_pos method");
95 assert_mpi(not survey_data,
96 "[RSDReconstructionFourierMethod] Survey data (line of sight towards an observer, no "
97 "periodic box) is not supported\n");
98 assert_mpi(niterations >= 1, "[RSDReconstructionFourierMethod] Need at least one iteration\n");
99
100 // Use N-linear interpolation
101 const std::string interpolation_method = "CIC";
102
103 // The particles stay at the observed positions s until the end
104 const size_t NumPart = part.get_npart();
105 auto * p = part.get_particles_ptr();
106
107 // Normalize the los_direction to a unit vector
108 assert_mpi(los_direction.size() == N,
109 "[RSDReconstructionFourierMethod] Line of sight direction has wrong dimension\n");
110 double norm = 0.0;
111 for (int idim = 0; idim < N; idim++) {
112 norm += los_direction[idim] * los_direction[idim];
113 }
114 assert_mpi(norm > 0.0,
115 "[RSDReconstructionFourierMethod] Line of sight vector cannot be the zero vector\n");
116 norm = 1.0 / std::sqrt(norm);
117 for (int idim = 0; idim < N; idim++) {
118 los_direction[idim] *= norm;
119 }
120
121 // The RSD shift beta (bPsi*r)r of each particle (from the last iteration)
122 std::vector<double> shift(NumPart, 0.0);
123
124 auto wrap = [](double x) {
125 x -= std::floor(x);
126 return x >= 1.0 ? 0.0 : x;
127 };
128
129 auto nleftright =
131
132 for (int i = 0; i < niterations; i++) {
133 if (FML::ThisTask == 0)
134 std::cout << "[RSDReconstructionFourierMethod] Iteration: " << i + 1 << " / " << niterations
135 << "\n";
136
137 // The density field of the current estimate of the real-space positions x = s - shift
138 // (the observed positions in the first iteration)
139 FFTWGrid<N> density(Nmesh, nleftright.first, nleftright.second);
140 density.add_memory_label("FFTWGrid::RSDReconstructionFourierMethod::density");
141 density.set_grid_status_real(true);
142 if (i == 0) {
144 NumPart,
145 part.get_npart_total(),
146 density,
147 density_assignment_method);
148 } else {
149 // Copies at the shifted positions (they can be on another task)
150 using Helper = detail::ReconstructionParticle<N>;
151 std::vector<Helper> shifted(NumPart);
152#ifdef USE_OMP
153#pragma omp parallel for
154#endif
155 for (size_t j = 0; j < NumPart; j++) {
156 const auto * pos = FML::PARTICLE::GetPos(p[j]);
157 for (int idim = 0; idim < N; idim++)
158 shifted[j].pos[idim] = wrap(pos[idim] - shift[j] * los_direction[idim]);
159 }
160 long long int nmax = NumPart;
161 FML::MaxOverTasks(&nmax);
163 shifted_part.create(shifted.data(),
164 NumPart,
165 size_t(2 * nmax + 1000),
168 false);
169 std::vector<Helper>().swap(shifted);
171 shifted_part.get_npart(),
172 shifted_part.get_npart_total(),
173 density,
174 density_assignment_method);
175 }
176
177 density.fftw_r2c();
178
179 // Perform a smoothing
181 smoothing_options.second,
182 smoothing_options.first);
183
184 // The 1LPT potential
185 FFTWGrid<N> phi_1LPT;
186 compute_1LPT_potential_fourier(density, phi_1LPT);
187
188 // Free some memory
189 density.free();
190
191 // The 1LPT displacement field
192 std::array<FFTWGrid<N>, N> Psi;
193 from_LPT_potential_to_displacement_vector<N>(phi_1LPT, Psi);
194 for (int idim = 0; idim < N; idim++) {
195 Psi[idim].communicate_boundaries();
196 }
197
198 // Free some memory
199 phi_1LPT.free();
200
201 // Interpolate Psi to the observed particle positions
202 std::array<std::vector<FloatType>, N> Psi_particle_positions;
204 p,
205 NumPart,
206 Psi_particle_positions,
208 for (int idim = 0; idim < N; idim++) {
209 Psi[idim].free();
210 }
211
212 // The RSD shift beta (bPsi*r)r
213#ifdef USE_OMP
214#pragma omp parallel for
215#endif
216 for (size_t j = 0; j < NumPart; j++) {
217 double Psidotr = 0.0;
218 for (int idim = 0; idim < N; idim++)
219 Psidotr += los_direction[idim] * Psi_particle_positions[idim][j];
220 shift[j] = beta * Psidotr;
221 }
222 }
223
224 // Subtract the RSD shift, do a periodic wrap and communicate the particles in case they have left the
225 // current domain
226 double max_shift = 0.0;
227#ifdef USE_OMP
228#pragma omp parallel for reduction(max : max_shift)
229#endif
230 for (size_t j = 0; j < NumPart; j++) {
231 auto * pos = FML::PARTICLE::GetPos(p[j]);
232 for (int idim = 0; idim < N; idim++)
233 pos[idim] = wrap(pos[idim] - shift[j] * los_direction[idim]);
234 max_shift = std::max(max_shift, std::abs(shift[j]));
235 }
237
238 // Show maximum shift
239 FML::MaxOverTasks(&max_shift);
240 if (FML::ThisTask == 0)
241 std::cout << "[RSDReconstructionFourierMethod] Maximum shift (boxsize units): " << max_shift
242 << "\n";
243 }
A container class for holding particles that are distributed across many CPUs and to easily deal with...
size_t get_npart_total() const
Total number of active particles across all tasks.
void create(T *part, size_t NumParts, size_t nallocate, double xmin_local, double xmax_local, bool all_tasks_has_the_same_particles)
Create MPIParticles from a set of particles.
void compute_1LPT_potential_fourier(const FFTWGrid< N > &delta_fourier, FFTWGrid< N > &phi_1LPT_fourier)
Generate the 1LPT potential defined as and .
std::pair< int, int > get_extra_slices_needed_for_density_assignment(std::string density_assignment_method)
Compute how many extra slices we need in the FFTWGrid for a given density assignment / interpolation ...
void particles_to_grid(const T *part, size_t NumPart, size_t NumPartTot, FFTWGrid< N > &density, std::string density_assignment_method)
Assign particles to a grid to compute the over density field delta.
get_vel constexpr double * GetPos(...)
FML_UNITS_INLINE constexpr double density
Definition Units.h:49
double xmax_domain
The local extent of the domain (global domain goes from 0 to 1)
Definition Global.cpp:50
void MaxOverTasks(T *value)
Inplace max over tasks of a single value.
Definition Global.h:189
double xmin_domain
The local extent of the domain (global domain goes from 0 to 1)
Definition Global.cpp:49
double wrap(double x)
Definition test_fof.cpp:43
double beta(V3 a, V3 b)
double norm(Vector a)