CLASSpp Manual
Cosmology reference and developer manual
Loading...
Searching...
No Matches
perturbations_module.h
1#ifndef PERTURBATIONS_MODULE_H
2#define PERTURBATIONS_MODULE_H
3
4#include <mutex>
5#include <vector>
6
7#include "base_module.h"
8#include "evolver_options.h"
9#include "input_module.h"
10
11class PpfFluid;
12class ModifiedGravity;
14
15/* Resolved-once views into PerturbationsModule's frozen all_species_ collection.
16 The module owns several numerical approximation schemes (TCA/RSA/UFA/ncdmfa),
17 the Einstein/metric equations, and output conventions that legitimately
18 reference specific named species; these handles give that module-owned logic
19 O(1) access to the relevant species' data, replacing per-step/per-sample/per-k
20 O(n) string-keyed scans of all_species_ (frozen after construction) and the
21 per-step downcasts that accompanied them. Resolved by ResolveSpecies() in the
22 ctor. Defined at file scope (not nested in the class) so the cclassy.pxi
23 generator (generate_wrapper.py) does not mistake its closing brace for the end
24 of the PerturbationsModule class and truncate the harvested member list. */
25struct ResolvedSpecies {
26 const IDM_DR_IDR_Species* idm_dr_idr = nullptr; // TCA-idm_dr, RSA-idr
27 std::size_t idm_dr_idr_index = static_cast<std::size_t>(-1); // its species_layouts[] index
28 PpfFluid* ppf_fluid = nullptr; // PPF fluid (absorbs ppf_fluid_)
29 const ModifiedGravity* gravity = nullptr; // replaces GR's Einstein eqs
30 const BaseSpecies* lambda = nullptr; // excluded from rho_tot (delta_tot)
31 int idr_nature = idr_free_streaming; // absorbs idr_nature_
32 std::size_t cdm_index = static_cast<std::size_t>(-1); // h source (sync gauge)
33
34 std::vector<const BaseSpecies*> ur; // UR and its subclasses: UFA, RSA-ur, tensor rho
35};
36
37class PerturbationsModule : public BaseModule {
38 public:
39 // The single PPF fluid, or nullptr. PPF is a modified-gravity closure defined
40 // relative to the whole universe (intrinsically singular), so the module owns
41 // it; non-PPF fluids are ordinary species. Resolved once in the ctor.
42 PpfFluid* ppf_fluid() const {
43 return resolved_.ppf_fluid;
44 }
45
46 PerturbationsModule(InputModulePtr input_module,
47 BackgroundModulePtr background_module,
48 ThermodynamicsModulePtr thermodynamics_module);
49 ~PerturbationsModule();
50 void perturb_output_data(file_format output_format,
51 double z,
52 int number_of_titles,
53 double* data) const;
54 /* As above, but reading the stored table at a node of the late-time (ln_tau)
55 sampling instead of interpolating to a redshift. Exact, cheaper, and the
56 only way to reach the topmost node, whose round trip through z lands just
57 outside the tabulation range.
58 Design: docs/superpowers/specs/2026-09-16-classy-lss-methods-design.md */
59 void perturb_output_data_at_index_tau(file_format output_format,
60 int index_tau,
61 int number_of_titles,
62 double* data) const;
63 void perturb_output_titles(file_format output_format, std::string& titles) const;
64 void perturb_output_firstline_and_ic_suffix(int index_ic,
65 std::string& first_line,
66 std::string& ic_suffix) const;
67
69 const background* GetBackground() const noexcept {
70 return pba;
71 }
72 const perturbs* GetPerturbs() const noexcept {
73 return ppt;
74 }
75 const precision* GetPrecision() const noexcept {
76 return ppr;
77 }
78 /* Return by const reference: these are called from the perturbation RHS, and
79 returning the shared_ptr by value made every call a pair of atomic refcount
80 RMWs on one control block shared by all worker threads. That cache line
81 ping-ponged across cores (catastrophically across the P/E cluster boundary)
82 and cost up to 5.5x on the Perturbations stage at 11 threads. */
83 const BackgroundModulePtr& GetBackgroundModule() const {
84 return background_module_;
85 }
86 const ThermodynamicsModulePtr& GetThermodynamicsModule() const {
87 return thermodynamics_module_;
88 }
89
91 void SetSourceValue(
92 int index_md, int index_ic, int index_tp, int index_tau, int index_k, double value) {
93 sources_[index_md][index_ic * tp_size_[index_md] + index_tp]
94 [index_tau * k_size_[index_md] + index_k] = value;
95 }
96
99 int index_md_scalars_ = -1;
100 int index_md_tensors_ = -1;
101 int index_md_vectors_ = -1;
102 int md_size_ = 0;
106 int index_ic_ad_ = -1;
107 int index_ic_cdi_ = -1;
108 int index_ic_bi_ = -1;
109 int index_ic_nid_ = -1;
110 int index_ic_niv_ = -1;
111 int index_ic_ten_ = -1;
112 std::vector<int>
113 ic_size_;
118
119 /* remember that the temperature source function includes three
120 terms that we call 0,1,2 (since the strategy in class v > 1.7 is
121 to avoid the integration by part that would reduce the source to
122 a single term) */
123 int index_tp_t0_ = -1;
124 int index_tp_t1_ = -1;
125 int index_tp_t2_ = -1;
126 int index_tp_p_ = -1;
127 int index_tp_delta_tot_ = -1;
128 int index_tp_perturbed_recombination_delta_temp_ = -1;
129 int index_tp_perturbed_recombination_delta_chi_ = -1;
131 int index_tp_theta_m_ = -1;
132 int index_tp_theta_cb_ = -1;
133 int index_tp_theta_tot_ = -1;
135 int index_tp_phi_ = -1;
136 int index_tp_phi_prime_ = -1;
137 int index_tp_phi_plus_psi_ = -1;
138 int index_tp_psi_ = -1;
139 int index_tp_h_ = -1;
140 int index_tp_h_prime_ = -1;
141 int index_tp_eta_ = -1;
142 int index_tp_eta_prime_ = -1;
143 int index_tp_H_T_Nb_prime_ = -1;
144 int index_tp_k2gamma_Nb_ =
145 -1;
147 int index_tp_delta_m_ = -1;
148 int index_tp_delta_cb_ = -1;
149 std::vector<int>
150 tp_size_;
152 // Resolved-once views into the frozen all_species_ collection (ResolvedSpecies
153 // is defined at file scope above). Resolved by ResolveSpecies() in the ctor.
154 ResolvedSpecies resolved_;
155
156 void ResolveSpecies();
165 std::vector<char> species_emits_source_;
166
167 bool has_source_t_ = false;
168 bool has_source_p_ = false;
169 bool has_source_delta_m_ = false;
170 bool has_source_delta_cb_ =
171 false;
172 bool has_source_delta_tot_ = false;
173 bool has_source_theta_m_ = false;
174 bool has_source_theta_cb_ =
175 false;
176 bool has_source_theta_tot_ = false;
177 bool has_source_phi_ = false;
178 bool has_source_phi_prime_ = false;
179 bool has_source_phi_plus_psi_ = false;
180 bool has_source_psi_ = false;
181 bool has_source_h_ = false;
182 bool has_source_h_prime_ = false;
183 bool has_source_eta_ = false;
184 bool has_source_eta_prime_ = false;
185 bool has_source_H_T_Nb_prime_ = false;
186 bool has_source_k2gamma_Nb_ =
187 false;
191 std::vector<int>
192 index_k_output_values_;
195 std::string
196 scalar_titles_;
197 std::string
198 vector_titles_;
199 std::string
200 tensor_titles_;
202 std::vector<double> scalar_perturbations_data_
204 std::vector<double> vector_perturbations_data_
206 std::vector<double> tensor_perturbations_data_
209 int size_scalar_perturbation_data_
211 int size_vector_perturbation_data_
213 int size_tensor_perturbation_data_
216
220
221 std::vector<std::vector<std::vector<double>>> sources_;
229
230 std::vector<double>
231 ln_tau_;
233 int ln_tau_size_ = 0;
236
237 std::vector<double> tau_sampling_;
247 EvolverStats evolver_stats_;
248 ErkHistograms erk_histograms_;
249 std::mutex evolver_stats_mutex_;
250 int tau_size_ = 0;
252 std::vector<int> k_size_cl_;
255 std::vector<int> k_size_;
258 std::vector<std::vector<double>> k_;
259 double k_min_ = 0.0;
260 double k_max_ = 0.0;
262 private:
263 void perturb_store_columns(file_format output_format,
264 const std::vector<double>& tkfull,
265 int number_of_titles,
266 double* data) const;
267 void perturb_sources_at_tau(
268 int index_md, int index_ic, int index_tp, double tau, double* psource) const;
269 void perturb_init();
270 void perturb_indices_of_perturbs();
271 void perturb_timesampling_for_sources();
272 void perturb_get_k_list();
273 void perturb_workspace_init(int index_md, perturb_workspace* ppw);
274 void perturb_solve(int index_md, int index_ic, int index_k, perturb_workspace* ppw);
275 void perturb_prepare_k_output();
276 void perturb_find_approximation_number(int index_md,
277 double k,
279 double tau_ini,
280 double tau_end,
281 int* interval_number,
282 int* interval_number_of);
283 void perturb_find_approximation_switches(int index_md,
284 double k,
286 double tau_ini,
287 double tau_end,
288 double precision,
289 int interval_number,
290 const int* interval_number_of,
291 double* interval_limit,
292 int** interval_approx);
293 void perturb_vector_init(
294 int index_md, int index_ic, double k, double tau, perturb_workspace* ppw, const int* pa_old);
297 struct EarlyDensities {
298 double rho_m;
299 double rho_r;
300 double rho_nu;
301 };
302 EarlyDensities early_densities(const double* pvecback) const;
305 double tensor_ur_hierarchy_rho(const double* pvecback) const;
306 void perturb_initial_conditions(
307 int index_md, int index_ic, double k, double tau, perturb_workspace* ppw);
308 void perturb_approximations(int index_md, double k, double tau, perturb_workspace* ppw);
309 void perturb_einstein(int index_md, double k, double tau, double* y, perturb_workspace* ppw);
310 void perturb_total_stress_energy(int index_md, double k, double* y, perturb_workspace* ppw);
311 void perturb_sources(
312 double tau, double* y, double* dy, int index_tau, perturb_parameters_and_workspace* pppaw);
313 void perturb_print_variables(double tau,
314 double* y,
315 double* dy,
317 void perturb_derivs(double tau, double* y, double* dy, perturb_parameters_and_workspace* pppaw);
318 void perturb_derivs_diagonal(double tau,
319 double* y,
320 double* diag,
322 void perturb_tca_slip_and_shear(const double* y, perturb_parameters_and_workspace* pppaw);
323 void perturb_rsa_delta_and_theta(
324 double k, const double* y, double a_prime_over_a, double* pvecthermo, perturb_workspace* ppw);
325 void perturb_rsa_idr_delta_and_theta(
326 double k, const double* y, double a_prime_over_a, double* pvecthermo, perturb_workspace* ppw);
327
328 BackgroundModulePtr background_module_;
329 ThermodynamicsModulePtr thermodynamics_module_;
330 bool evolve_tensor_ur_ =
331 false;
332 bool evolve_tensor_ncdm_ =
333 false;
338
339 bool has_cmb_ = false;
340 bool has_lss_ = false;
343
344 std::vector<std::vector<double*>> late_sources_;
349 std::vector<std::vector<std::vector<double>>>
350 ddlate_sources_;
356 int number_of_scalar_titles_ =
357 0;
358 int number_of_vector_titles_ =
359 0;
360 int number_of_tensor_titles_ =
361 0;
366
367 std::vector<int> k_size_cmb_;
372};
373
381 perturb_parameters_and_workspace(PerturbationsModule* p_m) : perturbations_module(p_m) {}
382 PerturbationsModule* const perturbations_module;
383 int index_md = -1;
384 int index_ic = -1;
385 int index_k = -1;
386 double k = 0.0;
388};
389
390#endif //PERTURBATIONS_MODULE_H
Definition base_species.h:90
Definition idm_dr_idr_species.h:16
Definition modified_gravity.h:23
Definition ppf_fluid.h:11
#define _MAX_NUMBER_OF_K_FILES_
Definition perturbations.h:88
Definition perturbations.h:347
Definition perturbations.h:99
file_format
Definition precision.h:56
Definition evolver_erk.h:241
Definition perturbations_module.h:380
int index_k
Definition perturbations_module.h:385
perturb_workspace * ppw
Definition perturbations_module.h:387
int index_ic
Definition perturbations_module.h:384
double k
Definition perturbations_module.h:386
int index_md
Definition perturbations_module.h:383
Definition precision.h:79