MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
Macros | Functions | Variables
lightcone.c File Reference
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_integration.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "utils.h"
#include "timefac.h"
#include "partmanager.h"
#include "cosmology.h"
#include "physconst.h"
Include dependency graph for lightcone.c:

Go to the source code of this file.

Macros

#define NENTRY   4096
 

Functions

static double lightcone_get_horizon (double a)
 
static void lightcone_cross (int p, double ddrift)
 
static void lightcone_set_time (double a, const double BoxSize)
 
static double kernel (double loga, void *params)
 
static void lightcone_init_entry (Cosmology *CP, int i, const double UnitLength_in_cm)
 
void lightcone_init (Cosmology *CP, double timeBegin, const double UnitLength_in_cm, const char *OutputDir)
 
static void update_replicas (double a, double BoxSize)
 
void lightcone_compute (double a, double BoxSize, Cosmology *CP, inttime_t ti_curr, inttime_t ti_next)
 

Variables

static double tab_loga [NENTRY]
 
static double dloga
 
static double tab_Dc [NENTRY]
 
static int Nreplica
 
static int BoxBoost = 20
 
static double Reps [8192][3]
 
static double HorizonDistance2
 
static double HorizonDistance
 
static double HorizonDistancePrev
 
static double HorizonDistance2Prev
 
static double HorizonDistanceRef
 
static double zmin = 0.1
 
static double zmax = 80.0
 
static double ReferenceRedshift = 2.0
 
static double SampleFraction
 
static FILE * fd_lightcone
 

Macro Definition Documentation

◆ NENTRY

#define NENTRY   4096

Definition at line 19 of file lightcone.c.

Function Documentation

◆ kernel()

static double kernel ( double  loga,
void *  params 
)
static

Definition at line 59 of file lightcone.c.

59  {
60  double a = exp(loga);
61  Cosmology * CP = (Cosmology *) params;
62  return 1 / hubble_function(CP, a) * CP->Hubble / a;
63 }
double hubble_function(const Cosmology *CP, double a)
Definition: cosmology.c:58
static Cosmology * CP
Definition: power.c:27
double Hubble
Definition: cosmology.h:21

References CP, Cosmology::Hubble, and hubble_function().

Referenced by density_kernel_desnumngb(), density_kernel_dW(), density_kernel_dwk(), density_kernel_init(), density_kernel_init_with_type(), density_kernel_volume(), density_kernel_wk(), density_ngbiter(), GetDensityKernelType(), lightcone_init_entry(), metal_return_ngbiter(), set_density_params(), and stellar_density_ngbiter().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lightcone_compute()

void lightcone_compute ( double  a,
double  BoxSize,
Cosmology CP,
inttime_t  ti_curr,
inttime_t  ti_next 
)

Definition at line 177 of file lightcone.c.

178 {
179  int i;
180  lightcone_set_time(a, BoxSize);
181  const double ddrift = get_exact_drift_factor(CP, ti_curr, ti_next);
182  #pragma omp parallel for
183  for(i = 0; i < PartManager->NumPart; i++)
184  {
185  lightcone_cross(i, ddrift);
186  }
187 }
static void lightcone_set_time(double a, const double BoxSize)
Definition: lightcone.c:189
static void lightcone_cross(int p, double ddrift)
Definition: lightcone.c:222
struct part_manager_type PartManager[1]
Definition: partmanager.c:11
double get_exact_drift_factor(Cosmology *CP, inttime_t ti0, inttime_t ti1)
Definition: timefac.c:64

References CP, get_exact_drift_factor(), lightcone_cross(), lightcone_set_time(), part_manager_type::NumPart, and PartManager.

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lightcone_cross()

static void lightcone_cross ( int  p,
double  ddrift 
)
static

Definition at line 222 of file lightcone.c.

222  {
223  if(SampleFraction <= 0.0) return;
224  int i;
225  int k;
226  /* DM only */
227  if(P[p].Type != 1) return;
228 
229  for(i = 0; i < Nreplica; i++) {
230  double r = get_random_number(P[p].ID + i);
231  if(r > SampleFraction) continue;
232 
233  double pnew[3];
234  double pold[3];
235  double p3[4];
236  double dnew = 0, dold = 0;
237  for(k = 0; k < 3; k ++) {
238  pold[k] = P[p].Pos[k] + Reps[i][k] - PartManager->CurrentParticleOffset[k];
239  pnew[k] = P[p].Pos[k] + P[i].Vel[k] * ddrift - PartManager->CurrentParticleOffset[k];
240  dnew += pnew[k] * pnew[k];
241  dold += pold[k] * pold[k];
242  }
243  if(
244  (dold <= HorizonDistance2Prev && dnew >= HorizonDistance2)
245  ) {
246  double u1, u2;
247  if(dold != dnew) {
248  double cnew, cold;
249  dnew = sqrt(dnew);
250  dold = sqrt(dold);
251  cnew = dnew - HorizonDistance;
252  cold = dold - HorizonDistancePrev;
253  u1 = -cold / (cnew - cold);
254  u2 = cnew / (cnew - cold);
255  } else {
256  /* really should write all particles along the line:
257  * this partilce is moving along the horizon! */
258  u1 = u2 = 0.5;
259  }
260 
261  /* write particle position */
262  for(k = 0; k < 3; k ++) {
263  p3[k] = pold[k] * u2 + pnew[k] * u1;
264  }
265  p3[3] = SampleFraction;
266  fwrite(p3, sizeof(double), 4, fd_lightcone);
267  }
268  }
269 }
static double SampleFraction
Definition: lightcone.c:45
static int Nreplica
Definition: lightcone.c:34
static double HorizonDistance
Definition: lightcone.c:38
static FILE * fd_lightcone
Definition: lightcone.c:46
static double HorizonDistance2
Definition: lightcone.c:37
static double HorizonDistancePrev
Definition: lightcone.c:39
static double Reps[8192][3]
Definition: lightcone.c:36
#define P
Definition: partmanager.h:88
double CurrentParticleOffset[3]
Definition: partmanager.h:82
double get_random_number(uint64_t id)
Definition: system.c:60

References part_manager_type::CurrentParticleOffset, fd_lightcone, get_random_number(), HorizonDistance, HorizonDistance2, HorizonDistancePrev, Nreplica, P, PartManager, Reps, and SampleFraction.

Referenced by lightcone_compute().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lightcone_get_horizon()

static double lightcone_get_horizon ( double  a)
static

Definition at line 119 of file lightcone.c.

119  {
120  double loga = log(a);
121  int bin = (log(a) -tab_loga[0]) / dloga;
122  if (bin < 0) {
123  return tab_Dc[0];
124  }
125  if (bin >= NENTRY - 1) {
126  return tab_Dc[NENTRY - 1];
127  }
128  double u1 = loga - tab_loga[bin];
129  double u2 = tab_loga[bin + 1] - loga;
130  u1 /= (tab_loga[bin + 1] - tab_loga[bin]);
131  u2 /= (tab_loga[bin + 1] - tab_loga[bin]);
132  return tab_Dc[bin] * u2 + tab_Dc[bin + 1] * u1;
133 }
static double tab_loga[NENTRY]
Definition: lightcone.c:20
static double tab_Dc[NENTRY]
Definition: lightcone.c:22
#define NENTRY
Definition: lightcone.c:19
static double dloga
Definition: lightcone.c:21

References dloga, NENTRY, tab_Dc, and tab_loga.

Referenced by lightcone_init(), and lightcone_set_time().

Here is the caller graph for this function:

◆ lightcone_init()

void lightcone_init ( Cosmology CP,
double  timeBegin,
const double  UnitLength_in_cm,
const char *  OutputDir 
)

Definition at line 91 of file lightcone.c.

92 {
93  int i;
94  dloga = (0.0 - log(timeBegin)) / (NENTRY - 1);
95  for(i = 0; i < NENTRY; i ++) {
97  };
98  char buf[1024];
99  int chunk = 100;
100  int ThisTask;
101  MPI_Comm_rank(MPI_COMM_WORLD, &ThisTask);
102 
103  sprintf(buf, "%s/lightcone/", OutputDir);
104  mkdir(buf, 02755);
105  sprintf(buf, "%s/lightcone/%03d/", OutputDir, (int)(ThisTask / chunk));
106  mkdir(buf, 02755);
107  sprintf(buf, "%s/lightcone/%03d/lightcone-%05d.raw", OutputDir, (int)(ThisTask / chunk), ThisTask);
108 
109  fd_lightcone = fopen(buf, "a+");
110  if(fd_lightcone == NULL) {
111  endrun(1, "failed to open %s\n", buf);
112  }
114  printf("lightcone reference redshift = %g distance = %g\n",
116 }
void endrun(int where, const char *fmt,...)
Definition: endrun.c:147
static double HorizonDistanceRef
Definition: lightcone.c:41
static void lightcone_init_entry(Cosmology *CP, int i, const double UnitLength_in_cm)
Definition: lightcone.c:65
static double ReferenceRedshift
Definition: lightcone.c:44
static double lightcone_get_horizon(double a)
Definition: lightcone.c:119
static double UnitLength_in_cm
Definition: power.c:26
int ThisTask
Definition: test_exchange.c:23

References CP, dloga, endrun(), fd_lightcone, HorizonDistanceRef, lightcone_get_horizon(), lightcone_init_entry(), NENTRY, ReferenceRedshift, ThisTask, and UnitLength_in_cm.

Referenced by begrun().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lightcone_init_entry()

static void lightcone_init_entry ( Cosmology CP,
int  i,
const double  UnitLength_in_cm 
)
static

Definition at line 65 of file lightcone.c.

65  {
66  tab_loga[i] = - dloga * (NENTRY - i - 1);
67 
68  gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);
69 
70  double result, error;
71 
72  gsl_function F;
73  F.function = &kernel;
74  F.params = CP;
75  gsl_integration_qags (&F, tab_loga[i], 0, 0, 1e-7, 1000,
76  w, &result, &error);
77 
78  /* result is in DH, hubble distance */
79  /* convert to cm / h */
80  result *= LIGHTCGS / HUBBLE;
81  /* convert to Kpc/h or internal units */
82  result /= UnitLength_in_cm;
83 
84  gsl_integration_workspace_free (w);
85  tab_Dc[i] = result;
86 // double a = exp(tab_loga[i]);
87 // double z = 1 / a - 1;
88 // printf("a = %g z = %g Dc = %g\n", a, z, result);
89 }
static double kernel(double loga, void *params)
Definition: lightcone.c:59
#define HUBBLE
Definition: physconst.h:25
#define LIGHTCGS
Definition: physconst.h:18

References CP, dloga, HUBBLE, kernel(), LIGHTCGS, NENTRY, tab_Dc, tab_loga, and UnitLength_in_cm.

Referenced by lightcone_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lightcone_set_time()

void lightcone_set_time ( double  a,
const double  BoxSize 
)
static

Definition at line 189 of file lightcone.c.

189  {
190  double z = 1 / a - 1;
191  if(z > zmin && z < zmax) {
196  update_replicas(a, BoxSize);
197  fflush(fd_lightcone);
198  if (z < ReferenceRedshift) {
199  SampleFraction = 1.0;
200  } else {
201  /* write a smaller fraction of the points at high redshift
202  */
203  /* This is the angular resolution rule */
207  /* This is the luminosity resolution rule */
208 #if 0
210  SampleFraction *= (1 + ReferenceRedshift) / (1 + z);
212 
213 #endif
214  }
215  message(0,"RefRedeshit=%g, SampleFraction=%g HorizonDistance=%g\n", ReferenceRedshift, SampleFraction, HorizonDistance);
216  } else {
217  SampleFraction = 0;
218  }
219 }
void message(int where, const char *fmt,...)
Definition: endrun.c:175
static double HorizonDistance2Prev
Definition: lightcone.c:40
static double zmin
Definition: lightcone.c:42
static void update_replicas(double a, double BoxSize)
Definition: lightcone.c:136
static double zmax
Definition: lightcone.c:43

References fd_lightcone, HorizonDistance, HorizonDistance2, HorizonDistance2Prev, HorizonDistancePrev, HorizonDistanceRef, lightcone_get_horizon(), message(), ReferenceRedshift, SampleFraction, update_replicas(), zmax, and zmin.

Referenced by lightcone_compute().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ update_replicas()

static void update_replicas ( double  a,
double  BoxSize 
)
static

Definition at line 136 of file lightcone.c.

136  {
137  int Nmax = BoxBoost * BoxBoost * BoxBoost;
138  int i;
139  int rx, ry, rz;
140  rx = ry = rz = 0;
141  Nreplica = 0;
142 
143  for(i = 0; i < Nmax; i ++) {
144  double dx = BoxSize * rx;
145  double dy = BoxSize * ry;
146  double dz = BoxSize * rz;
147  double d1, d2;
148  d1 = dx * dx + dy * dy + dz * dz;
149  dx += BoxSize;
150  dy += BoxSize;
151  dz += BoxSize;
152  d2 = dx * dx + dy * dy + dz * dz;
153  if(d1 <= HorizonDistance2 && d2 >= HorizonDistance2) {
154  Reps[Nreplica][0] = rx * BoxSize;
155  Reps[Nreplica][1] = ry * BoxSize;
156  Reps[Nreplica][2] = rz * BoxSize;
157  Nreplica ++;
158  if(Nreplica > 1000) {
159  endrun(951234, "too many replica");
160  }
161  }
162  rz ++;
163  if(rz == BoxBoost) {
164  rz = 0;
165  ry ++;
166  }
167  if(ry == BoxBoost) {
168  ry = 0;
169  rx ++;
170  }
171  }
172 }
static int BoxBoost
Definition: lightcone.c:35

References BoxBoost, endrun(), HorizonDistance2, Nreplica, and Reps.

Referenced by lightcone_set_time().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ BoxBoost

int BoxBoost = 20
static

Definition at line 35 of file lightcone.c.

Referenced by update_replicas().

◆ dloga

double dloga
static

◆ fd_lightcone

FILE* fd_lightcone
static

Definition at line 46 of file lightcone.c.

Referenced by lightcone_cross(), lightcone_init(), and lightcone_set_time().

◆ HorizonDistance

double HorizonDistance
static

Definition at line 38 of file lightcone.c.

Referenced by lightcone_cross(), and lightcone_set_time().

◆ HorizonDistance2

double HorizonDistance2
static

Definition at line 37 of file lightcone.c.

Referenced by lightcone_cross(), lightcone_set_time(), and update_replicas().

◆ HorizonDistance2Prev

double HorizonDistance2Prev
static

Definition at line 40 of file lightcone.c.

Referenced by lightcone_set_time().

◆ HorizonDistancePrev

double HorizonDistancePrev
static

Definition at line 39 of file lightcone.c.

Referenced by lightcone_cross(), and lightcone_set_time().

◆ HorizonDistanceRef

double HorizonDistanceRef
static

Definition at line 41 of file lightcone.c.

Referenced by lightcone_init(), and lightcone_set_time().

◆ Nreplica

int Nreplica
static

Definition at line 34 of file lightcone.c.

Referenced by lightcone_cross(), and update_replicas().

◆ ReferenceRedshift

double ReferenceRedshift = 2.0
static

Definition at line 44 of file lightcone.c.

Referenced by lightcone_init(), and lightcone_set_time().

◆ Reps

double Reps[8192][3]
static

Definition at line 36 of file lightcone.c.

Referenced by lightcone_cross(), and update_replicas().

◆ SampleFraction

double SampleFraction
static

Definition at line 45 of file lightcone.c.

Referenced by lightcone_cross(), and lightcone_set_time().

◆ tab_Dc

double tab_Dc[NENTRY]
static

Definition at line 22 of file lightcone.c.

Referenced by lightcone_get_horizon(), and lightcone_init_entry().

◆ tab_loga

double tab_loga[NENTRY]
static

Definition at line 20 of file lightcone.c.

Referenced by lightcone_get_horizon(), and lightcone_init_entry().

◆ zmax

double zmax = 80.0
static

Definition at line 43 of file lightcone.c.

Referenced by lightcone_set_time().

◆ zmin

double zmin = 0.1
static

Definition at line 42 of file lightcone.c.

Referenced by lightcone_set_time().