ABOUT LAMBDA

DR4 Delensing Likelihood presented in Han et al. 2020

Product Name
Delensing Likelihood Software
Mission
ACT

Description

This likelihood code has been tested using the November 2016 version of [cosmoMC](https://cosmologist.info/cosmomc/). 
The delensing part of the likelihood uses the python version of [CAMB](https://camb.readthedocs.io/en/latest/), 
and has been tested with both pyth
on 2.7 and python 3.7.


## Source files
- `ACTPol_Options.f90`: Contains settings for the likelihood that remain constant for all likelihood runs.
- `ACTPol_deep.f90`: Initializes the likelihood and does the main likelihood calculations.
- `ACTPol_like.f90`: A wrapper for `ACTPol_deep.f90`; this is the code that is actually called by cosmoMC.
- `Fg_subroutines.f90`: Used to calculate the foregrounds.
- `Foregrounds_loading.f90`: Called during initialization of the likelihood.

### Delensing
- `actpol_delensing.f90`: Calls the python function to calculate the delensed theory, via `actpol_delensing_c.c`.
- `actpol_delensing_c.c`: Used to call the python delensing function from the fortran likelihood, and return the delensed theory.
  - Note: If you are using python 2, you will need to replace `PyUnicode_FromString` with `PyString_FromString` in this file.
- `actpol_delensing_py.py`: Calculated the delensed theory, given unlensed theory.

### Files needed to use with cosmoMC
- `ACTPol.paramnames`: The `.paramnames` file for the foreground and nuisance parameters specific to the ACTPol likelihood.
- `Calculator_CAMB.f90`: This is a cosmoMC source file, which must be modified slightly to calculate the delensed theory within the likelihood. 
You can find the modification by searching for comments that begin with "ACTPol". In particular, within the subroutine `
CAMBCalc_SetPowersFromCAMB`, the line

  ```
  if (CosmoSettings%CMB_Lensing) then
  ```

  is changed to

  ```
  if (CosmoSettings%CMB_Lensing .and. (.not. CosmoSettings%delensing)) then
  ```

- `CosmologyTypes.f90`: This is another cosmoMC source file, that must also be modified to calculate the delensed theory. 
The modifications are indicated with comments beginning with "ACTPol".

  - Under `Type TCosmoTheoryParams`:
    - `lmax_computed_cl` is set to 12000 (increased from the default of 4500). This is recommended, 
    even if you do not want to calculate the delensing feature of the likelihood.
    - Add the line
      ```
      logical :: delensing = .false.
      ```
      so that you can add `delensing=T` in your cosmoMC `.ini` file to calculate the delensed theory in the likelihood.

  - In the subroutine `TCosmoTheorySettings_ReadParams`, add the line
    ```
    call Ini%Read('delensing', this%delensing)
    ```
    so cosmoMC will look for the `delensing` option in the `.ini` file.

- `actpol_likelihood.f90`: Used to get the parameters from cosmoMC and return the likelihood at each step in the MCMC chain.


## Input data
The likelihood needs you to provide bandpowers and a covariance matrix, along with three binning files (one each for TT, TE, EE). 
There are additional files listed below that are needed in specific circumstances. It also requires template files for the calculation
 of the foregrounds. Everything should be placed inside a directory, here called `actpol_data`. Within this directory should be two 
 subdirectories:

- The `Fg` directory contains the foreground template files, included here. The name of the `Fg` directory and the names of the template 
files should not be changed, as they are hard-coded in the likelihood.

- A directory, here named `data`, containing the bandpowers, covariance matrices, binning files, and any additional files needed. 
The name of this directory and the files within it must be specified in the cosmoMC `.ini` file.

The files contained in the `data` directory are:
- `lensed_dls.txt` and `delensed_dls.txt`: These contain the lensed and delensed bandpowers, in D_ell's: D_ell = ell(ell + 1) C_ell / (2 pi). 
The bandpowers must be in a single column, containing only the bandpowers to be used in the calculation, in the following
order:

  TT 90x90, TT 90x150, TT 150x150, TE 90x90, TE 90x150, TE 150x90, TE 150x150, EE 90x90, EE 90x150, EE 150x150

  where TT 90x90 refers to the D_l^TT 90 GHz bandpowers, etc. We use 47 bins for all TT bandpowers, and 49 bins for all TE and EE bandpowers.

- `lensed_cov.dat` and `delensed_cov.dat`: The covariance matrices for the lensed and delensed bandpowers, 
with blocks in the same order as the bandpowers and with the same number of bins.

- `leakage_template_te.txt`: This contains corrections that must be applied to the theory during the likelihood calculation if 
you are running with the data bandpowers above. It is not needed if you are not using data bandpowers (for example, it is not needed to c
alculate the likelihood using bandpowers from simulations). It should contain the same number of bins as are used for both TE and EE spectra; 
the code assumes that the same number of bins are used for TE and EE, and that the number of bins used for TT is less than
 or equal to the number of bins used for TE and EE.
  - Note we also include `leakage_template_tb.txt`, but it is not used in the likelihood.

- `recon_filter.txt`: The (unbinned) lensing reconstruction filter, used when calculating the delensed theory. 
This is only needed if you want to use the delensed bandpowers.

- The `binning` directory contains three binning files, returning binned spectra up to lmax = 7925 with 59 total bins:
  - `curved_full_BBL_DLTT.dat` to bin TT spectra
  - `curved_full_BBL_DLTP.dat` to bin TE spectra
  - `curved_full_BBL_DLPP.dat` to bin EE spectra.



## Instructions to use with cosmoMC
For the following steps, replace `$cosmomc` with the path to your own cosmoMC directory.

1. Place this directory, `actpol_delensing_likelihood`, under your `$cosmomc` directory. In other words, this directory 
should be `$cosmomc/actpol_delensing_likelihood`.

2. Copy all source files (all `.f90`, `.c`, and `.py` files listed above) into the `$cosmomc/source` directory.

3. Modify `$cosmomc/source/DataLikelihoods.f90`:
   - Under the line `subroutine SetDataLikelihoods(Ini)`, add a new line `use actpol_likelihood`.
   - Under the line `call CMBLikelihood_Add(DataLikelihoods, Ini)`, add a new line `call ACTPolLikelihood_Add(DataLikelihoods, Ini)`.

4. Modify `$cosmomc/Makefile` and `$cosmomc/source/Makefile`, following the instructions below.

5. `make clean` in the `$cosmomc` directory.

6. `make all` in the `$cosmomc` directory.

7. For python 2: Add the `$cosmomc/source` directory and the path to your python CAMB installation to your `PYTHONPATH`.

   For python 3: You must add the `$cosmomc/source` directory to your python `.pth` file. 
   See [here](https://docs.python.org/3/library/site.html) and [here](https://stackoverflow.com/questions/34066261/
   how-to-set-different-pythonpath-variables-for-python3-and-pyth
on2-respectively) for more information.

8. To use the likelihood, you must set `use_actpol=T` in your cosmoMC `.ini` file.

You can check that everything is working by running the provided `lensed_data.ini` and `delensed_data.ini` 
files (make sure `action=4` is set), and check if the likelihood calculated by cosmoMC matches the likelihood 
value under `expected_like` in the `.ini` files
. To use these files, move them into your main `$cosmomc` directory, and run cosmoMC using the commands:

```
./cosmomc lensed_data.ini
./cosmomc delensed_data.ini
```
For more information about the foreground and nuisance parameters, see the table in the image `fg_params.png`, provided here.


### cosmoMC makefiles
1. In `$cosmomc/Makefile`, change the line

   ```
   cosmomc: ./source/*.*90 ./camb/*.*90
   ```

   to

   ```
   cosmomc: ./source/*.*90 ./camb/*.*90 ./source/*.c
   ```

   This change is needed to use the delensing feature of the likelihood. To see an example of these changes, 
   look at the file `cosmomc_make`, and look for comments containing "ACTPol".

2. In `$cosmomc/source/Makefile`:

   - To compile with python:
     - Find your `PY_DIR` by using the command `which python`, and taking the path, excluding the last part of the path with `/bin/python`.
       - For example, on NERSC using python 3, the output of `which python` is `/usr/common/software/python/3.7-anaconda-2019.10/bin/python`, 
       so the `PY_DIR` is `/usr/common/software/python/3.7-anaconda-2019.10`.
     - After the line `PARAMETERIZATION = $(OUTPUT_DIR)/CosmologyParameterizations.o`, add the following: 
     (set your `PY_DIR` to the path you found above):
       ```
       PY_DIR = /path/to/python
       PY_VERSION = pythonX.Y
       INCLUDE += $(IFLAG)$(PY_DIR)/include/$(PY_VERSION) $(IFLAG)$(PY_DIR)/lib/$(PY_VERSION)/site-packages/numpy/core/include  
       $(IFLAG)$(PY_DIR)/include $(IFLAG). $(IFLAG). $(IFLAG)/Include
       FFLAGS += -fPIC -free
       GSLINC += -pthread -dynamic -qopenmp  -fno-strict-aliasing -g -O2 -DNDEBUG -fwrapv -O3 -Wall -Wextra -Wstrict-prototypes $(INCLUDE) 
       -fPIC -DPy_BUILD_CORE
       ```
       - You must set you `PY_DIR` to the path of your python installation (found above), and set your `PY_VERSION` to your python version 
       (for example, `PY_VERSION=python3.7`)
     - A few lines below, after the line `LINKFLAGS = -L../camb/$(OUTPUT_DIR) -lcamb_$(RECOMBINATION) $(LAPACKL) $(F90CRLINK) $(CLIKL)`, 
     add the following:
       ```
       PYCAMB_DIR = /path/to/pycamb
       LINKFLAGS += -L$(PY_DIR)/lib -Wl,-rpath=$(PY_DIR)/lib,--no-as-needed -L. -l$(PY_VERSION) -L$(PYCAMB_DIR)/camblib.
       so -lpthread -ldl  -lutil
       ```
       - You must set your `PYCAMB_DIR` to the path of your python CAMB installation.
     - Note: for python 3, you may need to make the following additional changes:
       - In the line beginning with `INCLUDE +=`, change `$(IFLAG)$(PY_DIR)/include/$(PY_VERSION)` 
       to `$(IFLAG)$(PY_DIR)/include/$(PY_VERSION)m` (add `m` to the end).
       - In the line beginning with `LINKFLAGS +=`, change `-l$(PY_VERSION)` to `-l$(PY_VERSION)m` (again, add `m` to the end).


   - After the line `DATAMODULES += $(OUTPUT_DIR)/wl.o`, add the following:

     ```
     DATAMODULES += $(OUTPUT_DIR)/actpol_delensing.o $(OUTPUT_DIR)/actpol_delensing_c.o
     DATAMODULES += $(OUTPUT_DIR)/ACTPol_like.o $(OUTPUT_DIR)/ACTPol_Options.o $(OUTPUT_DIR)/ACTPol_deep.o 
     $(OUTPUT_DIR)/Fg_subroutines.o $(OUTPUT_DIR)/Foregrounds_loading.o
     DATAMODULES += $(OUTPUT_DIR)/actpol_likelihood.o
     ```

   - After the line `$(OUTPUT_DIR)/wl.o: $(OUTPUT_DIR)/Likelihood_Cosmology.o`, add the following:

     ```
     $(OUTPUT_DIR)/actpol_delensing.o: $(OUTPUT_DIR)/actpol_delensing_c.o
     $(OUTPUT_DIR)/Foregrounds_loading.o : $(OUTPUT_DIR)/ACTPol_Options.o $(OUTPUT_DIR)/Fg_subroutines.o
     $(OUTPUT_DIR)/ACTPol_deep.o : $(OUTPUT_DIR)/ACTPol_Options.o $(OUTPUT_DIR)/Fg_subroutines.o $(OUTPUT_DIR)/Foregrounds_loading.o
     $(OUTPUT_DIR)/ACTPol_like.o : $(OUTPUT_DIR)/ACTPol_Options.o $(OUTPUT_DIR)/ACTPol_deep.o 
     $(OUTPUT_DIR)/Fg_subroutines.o $(OUTPUT_DIR)/Foregrounds_loading.o
     $(OUTPUT_DIR)/actpol_likelihood.o: $(OUTPUT_DIR)/Calculator_CAMB.o $(OUTPUT_DIR)/Likelihood_Cosmology.o 
     $(OUTPUT_DIR)/ACTPol_Options.o $(OUTPUT_DIR)/ACTPol_deep.o $(OUTPUT_DIR)/ACTPol_like.o 
     $(OUTPUT_DIR)/actpol_delensing.o $(OUTPUT_DIR)/actpol_delensing_c.o
     ```

   For the modifications needed to use python with the likelihood (for delensing), the correct compiler 
   flags can (probably) be found by following the instructions [here]
   (https://docs.python.org/2/extending/embedding.html#compiling-and-linking-under-unix-like-syst
ems).

   To see an example of these changes, look at the file `cosmomc_source_make`, and look for comments containing "ACTPol".

### Settings for the cosmoMC `.ini` file

Example cosmoMC `.ini` files are `lensed_data.ini` and `delensed_data.ini`. These can be run after following the instructions 
above to test if everything is working correctly. In both files, the expected likelihood calculated by cosmoMC is set under the option `ex
pected_like`. Many of the options below have a default, which is the setting that should be used with the data 
in the `actpol_data/data` directory.

The cosmoMC setting `action` in the `.ini` file determines what is run by cosmoMC. Setting `action=4` 
(the default for the `.ini` files included here) is used to do a single likelihood calculation. 
You can set `action=0` to run MCMC chains, or `action=2` to find t
he best-fit parameters. See the [cosmoMC readme](https://cosmologist.info/cosmomc/readme.html) for more information.

- `use_actpol`: Set `use_actpol=T` to use this likelihood.
- `data_dir_path`: The path to the directory containing all of the input to the likelihood. For example, to use the data given here, 
set `data_dir_path = $cosmomc/actpol_delensing_likelihood/actpol_data/`.
- `act_data_dir`: The name of the directory containing the bandpowers, covariance matrix, etc. 
within the directory given by `data_dir_path`. For example, if your data is in the directory 
`$cosmomc/actpol_delensing_likelihood/actpol_data/data`, set `act_data_dir =
 data/`.
- `spec_fname`: The filename of the file containing the data bandpowers, assumed to be in the directory set by `act_data_dir`.
- `cov_fname`: The filename of the file containing the covariance matrix, assumed to be in the directory set by `act_data_dir`.
- `bbltt_fname`: The filename of the binning matrix for TT spectra, assumed to be in the directory set by `act_data_dir`.
- `bblte_fname`: Same as above, but for TE.
- `bblee_fname`: Same as above, but for EE.
- `paramnames_path`: The full path to the `.paramnames` file.
- `tt_lmax`: When calculating the theory in the likelihood, only use the CMB theory up to ell of `tt_lmax`. Set to 5300 by default.
- `lmax_win`: The maximum ell value for the binning matrices. The foreground theory in the likelihood calculation is used up to ell of 
`lmax_win`. Set to 7925 by default.
- `nbintt`: The number of bins used for the TT bandpowers. Default is 47.
- `nbinte`: The number of bins used for the TE bandpowers. Default is 49.
- `nbinee`: The number of bins used for the EE bandpowers. Default is 49.
- `bmax0`: The total number of bins in the binning matrix. Default is 59.
- `b1tt`: The first bin number to use for TT spectra. Default is 13.
- `b1te`. The first bin number to use for TE spectra. Default is 11.
- `b1ee`. The first bin number to use for EE spectra. Default is 11.
- `use_cls`: Set to true (`use_cls = T`) if your data bandpowers are in C_ell's instead of D_ell's. Default is false.
- `use_tt`: Set to true to use the TT bandpowers in the likelihood calculation. Default is true.
- `use_te`: Same as above but for TE.
- `use_ee`: Same as above but for EE.
- `invcov`: Set to true if your covariance matrix is already inverted. Default is false.
- `data_run`: Set to true if you are using data bandpowers (rather than bandpowers from simulated spectra, for example). 
This should be set to true if you are using the data provided here. Default is true.
  - `leakage_correction_fname`: Filename of the leakage correction (provided here under `actpol_data/data/leakage_correction.txt`). 
  This must be specified if `data_run = T`.
- `delensing`: Set to true if you are using delensed bandpowers, so delensed theory will be used in the likelihood calculation. 
Default is false. If set to true, you must also set the following:
  - `filter_file`: The full path to the lensing reconstruction filter. It is provided here under `actpol_data/data/recon_filt.txt`.
  - `filt_lmax`: The maximum L value, to which the filter is calculated. Default is 9998.
  - `kappa_lmin` and `kappa_lmax`: The minimum and maximum L value used to calculate the filter.
- `save_test_output`: This can be set to true to save the CMB and foreground theory spectra used in the likelihood calculation. 
This should only be used for testing purposes (with `action=4`), as it saves the theory files every time the likelihood is calculated. D
efault is false. If it is set to true, you must also provide:
  - `test_save_dir`: The full path of the directory in which to save the output files.
  - `test_save_root`: All saved output will begin with `test_save_root_`.
  - `expected_like`: Set to an exact value to ensure you can reproduce your results using the same settings. Otherwise, 
  this can just be set to some reasonable value. If you run one of the provided `.ini` files, you should recover the likelihood value given 
  by `ex
pected_like`.
  - There are four files that will be saved:
    - `test_save_dir/test_save_root_CMB.txt`: The unbinned CMB lensed (if `delensing=F`) or delensed (if `delensing=T`) theory spectra, 
    with three columns in the order TT, TE, EE, beginning at ell = 2.
    - `test_save_dir/test_save_root_fgTT.txt`, `test_save_dir/test_save_root_fgTE.txt`, and `test_save_dir/test_save_root_fgEE.txt`: 
    The unbinned TT, TE, and EE foreground spectra, beginning at ell = 2. Each file has three columns for the three frequency combinati
ons, in the order 90x90 GHz, 90x150 GHz, and 150x150 GHz.

Back to Product Page

A service of the HEASARC and of the Astrophysics Science Division at NASA/GSFC
Goddard Space Flight Center, National Aeronautics and Space Administration
HEASARC Director: Dr. Alan P. Smale
LAMBDA Director: Dr. Eric R. Switzer
NASA Official: Dr. Eric R. Switzer
Web Curator: Mr. Michael R. Greason