FML
Loading...
Searching...
No Matches
Main.cpp File Reference

Go to the source code of this file.

Typedefs

template<class T >
using MPIParticles = FML::PARTICLE::MPIParticles<T>
 
template<int N>
using FFTWGrid = FML::GRID::FFTWGrid<N>
 
using Particle = SimpleParticle<Ndim>
 

Functions

double power_spectrum (double k)
 
void generate_delta (FFTWGrid< Ndim > &delta)
 
int main ()
 

Variables

const int Ndim = 3
 
const double box = 100.0
 

Typedef Documentation

◆ FFTWGrid

template<int N>
using FFTWGrid = FML::GRID::FFTWGrid<N>

Definition at line 53 of file Main.cpp.

◆ MPIParticles

template<class T >
using MPIParticles = FML::PARTICLE::MPIParticles<T>

Definition at line 51 of file Main.cpp.

◆ Particle

using Particle = SimpleParticle<Ndim>

Definition at line 54 of file Main.cpp.

Function Documentation

◆ generate_delta()

void generate_delta ( FFTWGrid< Ndim > & delta)

Definition at line 71 of file Main.cpp.

71 {
73 std::function<double(double)> Powspec;
74
75 // Fix amplitude (so only random phases) or do a normal GRF if false
76 const bool fix_amplitude = true;
77
78 // Function P(kBox / Box) / Volume that the method below requires
79 const double volume = std::pow(box, Ndim);
80 Powspec = [&](double kBox) -> double { return power_spectrum(kBox / box) / volume; };
81
82 // Make a random field in fourier space
84}
double power_spectrum(double k)
Definition Main.cpp:61
const int Ndim
Definition Main.cpp:44
const double box
Definition Main.cpp:45
const bool fix_amplitude
Definition Main.cpp:44
This is a class for having a unified interface for random numbers in the library.
void generate_gaussian_random_field_fourier(FFTWGrid< N > &grid, RandomGenerator *rng, std::function< double(double)> Pofk_of_kBox_over_volume, bool fix_amplitude)
std::mt19937_64 rng(1234)

◆ main()

int main ( )

Definition at line 86 of file Main.cpp.

86 {
87#ifdef MEMORY_LOGGING
88 auto * mem = FML::MemoryLog::get();
89#endif
90
91 //=====================================================
92 // Setting: what grid to use to generate the IC on and
93 // how many particles to generate
94 //=====================================================
95 const int Nmesh = 128;
96 const int Npart_1D = 128;
97 const double buffer_factor = 1.25;
98
99 //=====================================================
100 // Generate density field (the extra slices we use in this grid
101 // propagates to the grids we generate below)
102 // interpolation_method is how we interpolate from the grid
103 // to the particles (use NGP or CIC)
104 //=====================================================
105 std::string interpolation_method = "CIC";
107 FFTWGrid<Ndim> delta(Nmesh, nextra.first, nextra.second);
108 generate_delta(delta);
109
110 //=====================================================
111 // Generate the 1LPT potential phi_1LPT = delta(k)/k^2
112 //=====================================================
113 FFTWGrid<Ndim> phi_1LPT;
115
116 //=====================================================
117 // Generate the 2LPT potential phi_2LPT
118 // 7 FFTS
119 //=====================================================
120 FFTWGrid<Ndim> phi_2LPT;
122
123 //=====================================================
124 // Generate displacement field Psi = Dphi
125 // 3+3 FFTS
126 //=====================================================
127 std::array<FFTWGrid<Ndim>, Ndim> Psi_1LPT_vector;
129 phi_1LPT.free();
130
131 std::array<FFTWGrid<Ndim>, Ndim> Psi_2LPT_vector;
133 phi_2LPT.free();
134
135 //=====================================================
136 // Make a regular (Lagrangian) particle grid with
137 // Npart_1D^NDIM particles in total
138 //=====================================================
141 part.info();
142
143 //=====================================================
144 // Interpolate 1LPT displacement field to particle positions
145 // (Note: if Npart_1D = Nmesh then we can assign directly
146 // without interpolation using the disp fields as the
147 // cell is at the particle position and save time though
148 // we don't do this here)
149 //=====================================================
150 std::array<std::vector<FML::GRID::FloatType>, Ndim> displacements_1LPT;
152 part.get_particles_ptr(),
153 part.get_npart(),
154 displacements_1LPT,
156 for (int idim = 0; idim < Ndim; idim++) {
157 Psi_1LPT_vector[idim].free();
158 }
159
160 //=====================================================
161 // Interpolate 2LPT displacement field to particle positions
162 //=====================================================
163 std::array<std::vector<FML::GRID::FloatType>, Ndim> displacements_2LPT;
165 part.get_particles_ptr(),
166 part.get_npart(),
167 displacements_2LPT,
169 for (int idim = 0; idim < Ndim; idim++) {
170 Psi_2LPT_vector[idim].free();
171 }
172
173 //=====================================================
174 // Set the velocities to be the dimensionless v_code = a^2 dxdt / (H0 Box)
175 // For simplicity for the growth rate we just use the approx D1 = a => f1 = 1 and f2 = 2
176 // valid for OmegaM = 1
177 // (easy to compute the exact values by just solving the ODE or using the approx f1 ~ OmegaM(a)^0.55)
178 //=====================================================
179 const double a_ini = 1.0 / (1.0 + 50.0);
180 const double OmegaM = 1.0;
181 const double HoverH0 = std::sqrt(OmegaM / (a_ini * a_ini * a_ini) + 1.0 - OmegaM);
182 const double growth_rate1 = 1.0;
183 const double growth_rate2 = 2.0;
184 const double vfac_1LPT = a_ini * a_ini * HoverH0 * growth_rate1;
185 const double vfac_2LPT = a_ini * a_ini * HoverH0 * growth_rate2;
186
187 //=====================================================
188 // Add 1LPT displacement to particle position
189 // Add 2LPT displacement to particle position
190 //=====================================================
191
192 double max_disp_1LPT = 0.0;
193 double max_disp_2LPT = 0.0;
194 double max_vel_1LPT = 0.0;
195 double max_vel_2LPT = 0.0;
196
197 auto * part_ptr = part.get_particles_ptr();
198#ifdef USE_OMP
199#pragma omp parallel for reduction(max : max_disp_1LPT, max_disp_2LPT, max_vel_1LPT, max_vel_2LPT)
200#endif
201 for (size_t ind = 0; ind < part.get_npart(); ind++) {
202 auto * pos = part_ptr[ind].get_pos();
203 auto * vel = part_ptr[ind].get_vel();
204 for (int idim = Ndim - 1; idim >= 0; idim--) {
205 const double dpos_1LPT = displacements_1LPT[idim][ind];
206 const double dpos_2LPT = displacements_2LPT[idim][ind];
207 pos[idim] += dpos_1LPT + dpos_2LPT;
208 vel[idim] = vfac_1LPT * dpos_1LPT + vfac_2LPT * dpos_2LPT;
209 if (std::fabs(dpos_1LPT) > max_disp_1LPT)
210 max_disp_1LPT = std::fabs(dpos_1LPT);
211 if (std::fabs(dpos_2LPT) > max_disp_2LPT)
212 max_disp_2LPT = std::fabs(dpos_2LPT);
213 if (std::fabs(vfac_1LPT * dpos_1LPT) > max_vel_1LPT)
214 max_vel_1LPT = std::fabs(vfac_1LPT * dpos_1LPT);
215 if (std::fabs(vfac_2LPT * dpos_2LPT) > max_vel_2LPT)
216 max_vel_2LPT = std::fabs(vfac_2LPT * dpos_2LPT);
217 if (pos[idim] >= 1.0)
218 pos[idim] -= 1.0;
219 if (pos[idim] < 0.0)
220 pos[idim] += 1.0;
221 }
222 }
223
224 //=====================================================
225 // We no longer need Psi
226 //=====================================================
227 for (int idim = 0; idim < Ndim; idim++) {
228 displacements_1LPT[idim].clear();
229 displacements_1LPT[idim].shrink_to_fit();
230 displacements_2LPT[idim].clear();
231 displacements_2LPT[idim].shrink_to_fit();
232 }
233
234 //=====================================================
235 // Output the maximum displacement and velocity
236 //=====================================================
237 FML::MaxOverTasks(&max_disp_1LPT);
238 FML::MaxOverTasks(&max_disp_2LPT);
239 FML::MaxOverTasks(&max_vel_1LPT);
240 FML::MaxOverTasks(&max_vel_2LPT);
241 if (FML::ThisTask == 0)
242 std::cout << "Maximum displacements: " << max_disp_1LPT * Nmesh << " grid cells\n";
243 if (FML::ThisTask == 0)
244 std::cout << "Maximum displacements: " << max_disp_2LPT * Nmesh << " grid cells\n";
245 if (FML::ThisTask == 0)
246 std::cout << "Maximum velocity: " << max_vel_1LPT * 100.0 * box / a_ini << " km/s peculiar\n";
247 if (FML::ThisTask == 0)
248 std::cout << "Maximum velocity: " << max_vel_2LPT * 100.0 * box / a_ini << " km/s peculiar\n";
249
250 //=====================================================
251 // Communicate particles (they might have left the
252 // current task)
253 //=====================================================
255
256 //=====================================================
257 // Lets test that it works as expected
258 // From particles to density field. Compute P(k) and cross spectrum wrt the random field used
259 // to generate particles. This shows that with interlacing and a high order kernel (PQS is 5th order)
260 // we are able to reconstruct the initial density field from the particles to
261 // high precision
262 //=====================================================
263 const bool interlacing = true;
264 const std::string density_assignment_method = "PQS";
265 const auto nlr = FML::INTERPOLATION::get_extra_slices_needed_for_density_assignment(density_assignment_method);
266 FFTWGrid<Ndim> delta_from_part(Nmesh, nlr.first, nlr.second);
271 part.get_npart(),
272 part.get_npart_total(),
273 delta_from_part,
274 density_assignment_method,
275 interlacing);
276 FML::INTERPOLATION::deconvolve_window_function_fourier<Ndim>(delta_from_part, density_assignment_method);
277 FML::CORRELATIONFUNCTIONS::bin_up_power_spectrum(delta_from_part, pofk_part);
279 FML::CORRELATIONFUNCTIONS::bin_up_cross_power_spectrum(delta, delta_from_part, pofk_cross);
280
281 //=====================================================
282 // Convert P(k) to physical units: inherits the same
283 // units as the boxsize so h/Mpc for k and (Mpc/h)^NDIM for P(k)
284 //=====================================================
285 pofk_part.scale(box);
286 pofk_ini.scale(box);
287 pofk_cross.scale(box);
288
289 //=====================================================
290 // Output to screen
291 //=====================================================
292 if (FML::ThisTask == 0) {
293 std::cout << "\n# k (h/Mpc) CorrelationCoeff P_part / P P / P_analytic\n";
294 for (int i = 0; i < pofk_ini.n; i++) {
295 double k = pofk_ini.kbin[i];
296 double cross_corr_coeff = pofk_cross.pofk[i] / std::sqrt(pofk_part.pofk[i] * pofk_ini.pofk[i]);
297 std::cout << k << " " << cross_corr_coeff << " " << pofk_part.pofk[i] / pofk_ini.pofk[i] << " "
298 << pofk_ini.pofk[i] / power_spectrum(k) << "\n";
299 }
300 }
301
302 //=====================================================
303 // Free the last of the memory before printing the
304 // memory status (this is not needed)
305 //=====================================================
306 part.free();
307
308#ifdef MEMORY_LOGGING
309 // Print memory summary
310 mem->print();
311#endif
312}
void generate_delta(FFTWGrid< Ndim > &delta)
Definition Main.cpp:71
double HoverH0(double a)
Definition Main.cpp:76
const int Npart_1D
Definition Main.cpp:50
const double buffer_factor
Definition Main.cpp:51
const double OmegaM
Definition Main.cpp:65
const int Nmesh
Definition Main.cpp:48
const std::string interpolation_method
Definition Main.cpp:64
Class for holding the results after binning up a power-spectrum.
Class for holding grids and performing real-to-complex and complex-to-real FFTs using FFTW with MPI.
Definition FFTWGrid.h:98
void free()
The fftw_complex cast of get_fourier_gride.
Definition FFTWGrid.h:1453
static MemoryLog * get()
The (only) instance.
A container class for holding particles that are distributed across many CPUs and to easily deal with...
void create_particle_grid(int Npart_1D, double buffer_factor, double xmin_local, double xmax_local)
Create particles in a rectangular grid spread across all tasks buffer_factor is how much extra to al...
size_t get_npart_total() const
Total number of active particles across all tasks.
T * get_particles_ptr()
Get a pointer to the first particle.
void info()
Show some info about the class.
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 free()
Free all memory of the stored particles.
void bin_up_power_spectrum(const FFTWGrid< N > &fourier_grid, PowerSpectrumBinning< N > &pofk)
Compute the power-spectrum of a fourier grid.
void bin_up_cross_power_spectrum(const FFTWGrid< N > &fourier_grid_1, const FFTWGrid< N > &fourier_grid_2, PowerSpectrumBinning< N > &pofk)
Compute the cross power-spectrum of two fourier grids.
void compute_1LPT_potential_fourier(const FFTWGrid< N > &delta_fourier, FFTWGrid< N > &phi_1LPT_fourier)
Generate the 1LPT potential defined as and .
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.
void from_LPT_potential_to_displacement_vector(const FFTWGrid< N > &phi_fourier, std::array< FFTWGrid< N >, N > &psi_real, double DoverDini=1.0)
Generate the displaceement field from the LPT potential .
void particles_to_fourier_grid(const T *part, size_t NumPart, size_t NumPartTot, FFTWGrid< N > &density_grid_fourier, std::string density_assignment_method, bool interlacing)
Assign particles to grid to compute the over density.
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.
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 deconvolve_window_function_fourier(FFTWGrid< N > &fourier_grid, std::string density_assignment_method)
Deconvolves the density assignment kernel in Fourier space.
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
int ThisTask
The MPI task number.
Definition Global.cpp:39
double xmin_domain
The local extent of the domain (global domain goes from 0 to 1)
Definition Global.cpp:49

◆ power_spectrum()

double power_spectrum ( double k)

Definition at line 61 of file Main.cpp.

61 {
62 if (k == 0.0)
63 return 0.0;
64 return 1e-4 / std::pow(k, Ndim);
65}

Variable Documentation

◆ box

const double box = 100.0

Definition at line 45 of file Main.cpp.

◆ Ndim

const int Ndim = 3

Definition at line 44 of file Main.cpp.