Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
run_pk_param_space.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 """
3 Generate a set of CLASS power spectra across a set of sample points in
4 cosmological parameter space, and compare with CCL.
5 """
6 from param_space import *
7 import os, sys
8 
9 # Need to specify directory containing 'class' executable
10 CLASS_ROOT = None
11 if len(sys.argv) > 1: CLASS_ROOT = sys.argv[1]
12 assert CLASS_ROOT is not None, \
13  "Must specify CLASS_ROOT (as argument or in source file)."
14 
15 PREFIX = "std" # Prefix to use for this run
16 NSAMP = 100 # No. of sample points in parameter space
17 SEED = 10 # Random seed to use for sampling
18 ZVALS = np.arange(0., 3., 0.5) # Redshifts to evaluate P(k) at
19 
20 # Define parameter space to sample over
21 param_dict = {
22  'h': (0.55, 0.8),
23  'Omega_cdm': (0.15, 0.35),
24  'Omega_b': (0.018, 0.052),
25  'A_s': (1.5e-9, 2.5e-9),
26  'n_s': (0.94, 0.98)
27 }
28 
29 # Check that expected output data directories exist
30 class_datadir = "%s/data/class" % os.path.abspath(".")
31 ccl_datadir = "%s/data/ccl" % os.path.abspath(".")
32 if not os.path.exists(class_datadir): os.makedirs(class_datadir)
33 if not os.path.exists(ccl_datadir): os.makedirs(ccl_datadir)
34 
35 # Get root filename for CLASS and CCL filenames
36 root = "%s/%s" % (class_datadir, PREFIX)
37 ccl_root = "%s/%s" % (ccl_datadir, PREFIX)
38 
39 # Generate sample points on Latin hypercube
40 sample_points = generate_latin_hypercube( samples=NSAMP, param_dict=param_dict,
41  class_root=CLASS_ROOT, seed=SEED )
42 save_hypercube("%s_params.dat" % root, sample_points)
43 
44 
45 # Generate CLASS .ini files
46 print("Writing CLASS linear .ini files")
47 generate_class_ini(sample_points, root="%s_lin_std" % root,
48  nonlinear=False, redshifts=ZVALS)
49 generate_class_ini(sample_points, root="%s_lin_pre" % root,
50  nonlinear=False, redshifts=ZVALS)
51 
52 print("Writing CLASS nonlinear .ini files")
53 generate_class_ini(sample_points, root="%s_nl_std" % root,
54  nonlinear=True, redshifts=ZVALS)
55 generate_class_ini(sample_points, root="%s_nl_pre" % root,
56  nonlinear=True, redshifts=ZVALS)
57 
58 
59 # Run CLASS on generated .ini files
60 print("Running CLASS on .ini files")
61 run_class(fname_pattern="%s_lin_std_?????.ini" % root,
62  class_root=CLASS_ROOT, precision=False)
63 run_class(fname_pattern="%s_lin_pre_?????.ini" % root,
64  class_root=CLASS_ROOT, precision=True)
65 run_class(fname_pattern="%s_nl_std_?????.ini" % root,
66  class_root=CLASS_ROOT, precision=False)
67 run_class(fname_pattern="%s_nl_pre_?????.ini" % root,
68  class_root=CLASS_ROOT, precision=True)
69 
70 
71 # Run CCL for the same sets of parameters
72 generate_ccl_pspec(sample_points, ccl_root,
73  class_data_root="%s_lin_std" % root,
74  zvals=ZVALS, default_params={'mnu': 0.}, mode='std')
75 
76 generate_ccl_pspec(sample_points, ccl_root,
77  class_data_root="%s_lin_pre" % root,
78  zvals=ZVALS, default_params={'mnu': 0.}, mode='pre')
79 
80 generate_ccl_pspec(sample_points, ccl_root,
81  class_data_root="%s_nl_std" % root,
82  zvals=ZVALS, default_params={'mnu': 0.},
83  nonlin=True, mode='std')
84 
85 generate_ccl_pspec(sample_points, ccl_root,
86  class_data_root="%s_nl_pre" % root,
87  zvals=ZVALS, default_params={'mnu': 0.},
88  nonlin=True, mode='pre')
89 
def generate_latin_hypercube(samples, param_dict, class_root, seed=10)
Definition: param_space.py:81
def save_hypercube(fname, sample_points)
Definition: param_space.py:26
def generate_class_ini(sample_points, root, nonlinear=False, mnu=False, redshifts=np.arange(0., 3., 0.5))
Definition: param_space.py:230
def run_class(fname_pattern, class_root, precision=False)
Definition: param_space.py:350
def generate_ccl_pspec(sample_points, root, class_data_root, zvals, default_params=None, nonlin=False, mode='std')
Definition: param_space.py:137