Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
fftlog.c File Reference
#include <stdlib.h>
#include <math.h>
#include <fftw3.h>
#include "fftlog.h"
Include dependency graph for fftlog.c:

Go to the source code of this file.

Functions

static double complex gamma_fftlog (double complex z)
 
static double complex polar (double r, double phi)
 
static double complex lngamma_fftlog (double complex z)
 
static void lngamma_4 (double x, double y, double *lnr, double *arg)
 
static double goodkr (int N, double mu, double q, double L, double kr)
 
void compute_u_coefficients (int N, double mu, double q, double L, double kcrc, double complex u[])
 
void fht (int N, const double r[], const double complex a[], double k[], double complex b[], double mu, double q, double kcrc, int noring, double complex *u)
 
void fftlog_ComputeXi2D (double bessel_order, int N, const double l[], const double cl[], double th[], double xi[])
 
void fftlog_ComputeXiLM (double l, double m, int N, const double k[], const double pk[], double r[], double xi[])
 
void pk2xi (int N, const double k[], const double pk[], double r[], double xi[])
 
void xi2pk (int N, const double r[], const double xi[], double k[], double pk[])
 

Function Documentation

void compute_u_coefficients ( int  N,
double  mu,
double  q,
double  L,
double  kcrc,
double complex  u[] 
)

Definition at line 73 of file fftlog.c.

References lngamma_4(), m, M_PI, polar(), and x.

Referenced by fht().

74 {
75  double y = M_PI/L;
76  double k0r0 = kcrc * exp(-L);
77  double t = -2*y*log(k0r0/2);
78 
79  if(q == 0) {
80  double x = (mu+1)/2;
81  double lnr, phi;
82  for(int m = 0; m <= N/2; m++) {
83  lngamma_4(x, m*y, &lnr, &phi);
84  u[m] = polar(1.0,m*t + 2*phi);
85  }
86  }
87  else {
88  double xp = (mu+1+q)/2;
89  double xm = (mu+1-q)/2;
90  double lnrp, phip, lnrm, phim;
91  for(int m = 0; m <= N/2; m++) {
92  lngamma_4(xp, m*y, &lnrp, &phip);
93  lngamma_4(xm, m*y, &lnrm, &phim);
94  u[m] = polar(exp(q*log(2) + lnrp - lnrm), m*t + phip - phim);
95  }
96  }
97 
98  for(int m = N/2+1; m < N; m++)
99  u[m] = conj(u[N-m]);
100  if((N % 2) == 0)
101  u[N/2] = (creal(u[N/2]) + I*0.0);
102 }
#define M_PI
Definition: ccl_constants.h:22
static void lngamma_4(double x, double y, double *lnr, double *arg)
Definition: fftlog.c:51
static CCL_BEGIN_DECLS double x[111][8]
static double complex polar(double r, double phi)
Definition: fftlog.c:40
static int m[2]
Definition: ccl_emu17.c:25
void fftlog_ComputeXi2D ( double  bessel_order,
int  N,
const double  l[],
const double  cl[],
double  th[],
double  xi[] 
)

Definition at line 144 of file fftlog.c.

References fht(), and M_PI.

Referenced by ccl_tracer_corr_fftlog().

146 {
147  double complex* a = malloc(sizeof(complex double)*N);
148  double complex* b = malloc(sizeof(complex double)*N);
149 
150  for(int i=0;i<N;i++)
151  a[i]=l[i]*cl[i];
152  fht(N,l,a,th,b,bessel_order,0,1,1,NULL);
153  for(int i=0;i<N;i++)
154  xi[i]=creal(b[i]/(2*M_PI*th[i]));
155 
156  free(a);
157  free(b);
158 }
#define M_PI
Definition: ccl_constants.h:22
void fht(int N, const double r[], const double complex a[], double k[], double complex b[], double mu, double q, double kcrc, int noring, double complex *u)
Definition: fftlog.c:104
void fftlog_ComputeXiLM ( double  l,
double  m,
int  N,
const double  k[],
const double  pk[],
double  r[],
double  xi[] 
)

Definition at line 160 of file fftlog.c.

References fht(), M_PI, and pow().

Referenced by pk2xi(), and xi2pk().

162 {
163  double complex* a = malloc(sizeof(complex double)*N);
164  double complex* b = malloc(sizeof(complex double)*N);
165 
166  for(int i = 0; i < N; i++)
167  a[i] = pow(k[i], m - 0.5) * pk[i];
168  fht(N, k, a, r, b, l + 0.5, 0, 1, 1, NULL);
169  for(int i = 0; i < N; i++)
170  xi[i] = creal(pow(2*M_PI*r[i], -(m-0.5)) * b[i]);
171 
172  free(a);
173  free(b);
174 }
#define M_PI
Definition: ccl_constants.h:22
void fht(int N, const double r[], const double complex a[], double k[], double complex b[], double mu, double q, double kcrc, int noring, double complex *u)
Definition: fftlog.c:104
float pow(float base, unsigned long int exp)
Definition: precision.hpp:39
static int m[2]
Definition: ccl_emu17.c:25
void fht ( int  N,
const double  r[],
const double complex  a[],
double  k[],
double complex  b[],
double  mu,
double  q,
double  kcrc,
int  noring,
double complex *  u 
)

Definition at line 104 of file fftlog.c.

References compute_u_coefficients(), goodkr(), and m.

Referenced by fftlog_ComputeXi2D(), and fftlog_ComputeXiLM().

106 {
107  double L = log(r[N-1]/r[0]) * N/(N-1.);
108  double complex* ulocal = NULL;
109  if(u == NULL) {
110  if(noring)
111  kcrc = goodkr(N, mu, q, L, kcrc);
112  ulocal = malloc (sizeof(complex double)*N);
113  compute_u_coefficients(N, mu, q, L, kcrc, ulocal);
114  u = ulocal;
115  }
116 
117  /* Compute the convolution b = a*u using FFTs */
118  fftw_plan forward_plan = fftw_plan_dft_1d(N, (fftw_complex*) a, (fftw_complex*) b, -1, FFTW_ESTIMATE);
119  fftw_plan reverse_plan = fftw_plan_dft_1d(N, (fftw_complex*) b, (fftw_complex*) b, +1, FFTW_ESTIMATE);
120  fftw_execute(forward_plan);
121  for(int m = 0; m < N; m++)
122  b[m] *= u[m] / (double)(N); // divide by N since FFTW doesn't normalize the inverse FFT
123  fftw_execute(reverse_plan);
124  fftw_destroy_plan(forward_plan);
125  fftw_destroy_plan(reverse_plan);
126 
127  /* Reverse b array */
128  double complex tmp;
129  for(int n = 0; n < N/2; n++) {
130  tmp = b[n];
131  b[n] = b[N-n-1];
132  b[N-n-1] = tmp;
133  }
134 
135  /* Compute k's corresponding to input r's */
136  double k0r0 = kcrc * exp(-L);
137  k[0] = k0r0/r[0];
138  for(int n = 1; n < N; n++)
139  k[n] = k[0] * exp(n*L/N);
140 
141  free(ulocal);
142 }
void compute_u_coefficients(int N, double mu, double q, double L, double kcrc, double complex u[])
Definition: fftlog.c:73
static double goodkr(int N, double mu, double q, double L, double kr)
Definition: fftlog.c:58
static int m[2]
Definition: ccl_emu17.c:25
static double complex gamma_fftlog ( double complex  z)
static

Definition at line 15 of file fftlog.c.

References M_PI, p, sqrt(), and x.

Referenced by lngamma_fftlog().

16 {
17  /* Lanczos coefficients for g = 7 */
18  static double p[] = {
19  0.99999999999980993227684700473478,
20  676.520368121885098567009190444019,
21  -1259.13921672240287047156078755283,
22  771.3234287776530788486528258894,
23  -176.61502916214059906584551354,
24  12.507343278686904814458936853,
25  -0.13857109526572011689554707,
26  9.984369578019570859563e-6,
27  1.50563273514931155834e-7
28  };
29 
30  if(creal(z) < 0.5)
31  return M_PI / (sin(M_PI*z)*gamma_fftlog(1. - z));
32  z -= 1;
33  double complex x = p[0];
34  for(int n = 1; n < 9; n++)
35  x += p[n] / (z + (double)(n));
36  double complex t = z + 7.5;
37  return sqrt(2*M_PI) * cpow(t, z+0.5) * cexp(-t) * x;
38 }
static double complex gamma_fftlog(double complex z)
Definition: fftlog.c:15
#define M_PI
Definition: ccl_constants.h:22
Grid< NDIM, T > sqrt(Grid< NDIM, T > lhs)
Definition: grid.h:231
static CCL_BEGIN_DECLS double x[111][8]
static double z[8]
static int p
Definition: ccl_emu17.c:25
static double goodkr ( int  N,
double  mu,
double  q,
double  L,
double  kr 
)
static

Definition at line 58 of file fftlog.c.

References lngamma_4(), and M_PI.

Referenced by fht().

59 {
60  double xp = (mu+1+q)/2;
61  double xm = (mu+1-q)/2;
62  double y = M_PI*N/(2*L);
63  double lnr, argm, argp;
64  lngamma_4(xp, y, &lnr, &argp);
65  lngamma_4(xm, y, &lnr, &argm);
66  double arg = log(2/kr) * N/L + (argp + argm)/M_PI;
67  double iarg = round(arg);
68  if(arg != iarg)
69  kr *= exp((arg - iarg)*L/N);
70  return kr;
71 }
#define M_PI
Definition: ccl_constants.h:22
static void lngamma_4(double x, double y, double *lnr, double *arg)
Definition: fftlog.c:51
static void lngamma_4 ( double  x,
double  y,
double lnr,
double arg 
)
static

Definition at line 51 of file fftlog.c.

References lngamma_fftlog(), and w.

Referenced by compute_u_coefficients(), and goodkr().

52 {
53  double complex w = lngamma_fftlog(x+y*I);
54  if(lnr) *lnr = creal(w);
55  if(arg) *arg = cimag(w);
56 }
static double complex lngamma_fftlog(double complex z)
Definition: fftlog.c:45
static CCL_BEGIN_DECLS double x[111][8]
static double w[2][28][111]
Definition: ccl_emu17.c:33
static double complex lngamma_fftlog ( double complex  z)
static

Definition at line 45 of file fftlog.c.

References Catch::clog(), and gamma_fftlog().

Referenced by lngamma_4().

46 {
47  return clog(gamma_fftlog(z));
48 }
static double complex gamma_fftlog(double complex z)
Definition: fftlog.c:15
std::ostream & clog()
static double z[8]
void pk2xi ( int  N,
const double  k[],
const double  pk[],
double  r[],
double  xi[] 
)

Definition at line 176 of file fftlog.c.

References fftlog_ComputeXiLM().

Referenced by ccl_correlation_3d().

177 {
178  fftlog_ComputeXiLM(0, 2, N, k, pk, r, xi);
179 }
void fftlog_ComputeXiLM(double l, double m, int N, const double k[], const double pk[], double r[], double xi[])
Definition: fftlog.c:160
static double complex polar ( double  r,
double  phi 
)
static

Definition at line 40 of file fftlog.c.

Referenced by compute_u_coefficients().

41 {
42  return (r*cos(phi) +I*(r*sin(phi)));
43 }
void xi2pk ( int  N,
const double  r[],
const double  xi[],
double  k[],
double  pk[] 
)

Definition at line 181 of file fftlog.c.

References fftlog_ComputeXiLM(), and M_PI.

182 {
183  static const double TwoPiCubed = 8*M_PI*M_PI*M_PI;
184  fftlog_ComputeXiLM(0, 2, N, r, xi, k, pk);
185  for(int j = 0; j < N; j++)
186  pk[j] *= TwoPiCubed;
187 }
void fftlog_ComputeXiLM(double l, double m, int N, const double k[], const double pk[], double r[], double xi[])
Definition: fftlog.c:160
#define M_PI
Definition: ccl_constants.h:22