MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
Classes | Functions | Variables
timebinmgr.c File Reference
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "timebinmgr.h"
#include "utils.h"
Include dependency graph for timebinmgr.c:

Go to the source code of this file.

Classes

struct  sync_params
 

Functions

int cmp_double (const void *a, const void *b)
 
int OutputListAction (ParameterSet *ps, const char *name, void *data)
 
void set_sync_params (int OutputListLength, double *OutputListTimes)
 
void setup_sync_points (double TimeIC, double TimeMax, double no_snapshot_until_time, int SnapshotWithFOF)
 
SyncPointfind_next_sync_point (inttime_t ti)
 
SyncPointfind_current_sync_point (inttime_t ti)
 
static double Dloga_interval_ti (inttime_t ti)
 
double loga_from_ti (inttime_t ti)
 
inttime_t ti_from_loga (double loga)
 
double dloga_from_dti (inttime_t dti, const inttime_t Ti_Current)
 
inttime_t dti_from_dloga (double loga, const inttime_t Ti_Current)
 
double get_dloga_for_bin (int timebin, const inttime_t Ti_Current)
 
inttime_t round_down_power_of_two (inttime_t dti)
 

Variables

static SyncPointSyncPoints
 
static int NSyncPoints
 
static struct sync_params Sync
 

Function Documentation

◆ cmp_double()

int cmp_double ( const void *  a,
const void *  b 
)

Definition at line 17 of file timebinmgr.c.

18 {
19  return ( *(double*)a - *(double*)b );
20 }

Referenced by setup_sync_points().

Here is the caller graph for this function:

◆ dloga_from_dti()

double dloga_from_dti ( inttime_t  dti,
const inttime_t  Ti_Current 
)

Definition at line 256 of file timebinmgr.c.

257 {
258  double Dloga = Dloga_interval_ti(Ti_Current);
259  int sign = 1;
260  if(dti < 0) {
261  dti = -dti;
262  sign = -1;
263  }
264  if((unsigned int) dti > TIMEBASE) {
265  endrun(1, "Requesting dti %d larger than TIMEBASE %u\n", sign*dti, TIMEBASE);
266  }
267  return Dloga * dti * sign;
268 }
void endrun(int where, const char *fmt,...)
Definition: endrun.c:147
static double Dloga_interval_ti(inttime_t ti)
Definition: timebinmgr.c:207
#define TIMEBASE
Definition: timebinmgr.h:14

References Dloga_interval_ti(), endrun(), and TIMEBASE.

Referenced by apply_half_kick(), density(), density_ngbiter(), find_timesteps(), and hydro_ngbiter().

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

◆ Dloga_interval_ti()

static double Dloga_interval_ti ( inttime_t  ti)
static

Definition at line 207 of file timebinmgr.c.

208 {
209  /* FIXME: This uses the bit tricks because it has to be fast
210  * -- till we clean up the calls to loga_from_ti; then we can avoid bit tricks. */
211 
212  inttime_t lastsnap = ti >> TIMEBINS;
213 
214  if(lastsnap >= NSyncPoints - 1) {
215  /* stop advancing loga after the last sync point. */
216  return 0;
217  }
218  double lastoutput = SyncPoints[lastsnap].loga;
219  return (SyncPoints[lastsnap+1].loga - lastoutput)/TIMEBASE;
220 }
double loga
Definition: timebinmgr.h:25
static int NSyncPoints
Definition: timebinmgr.c:10
static SyncPoint * SyncPoints
Definition: timebinmgr.c:9
#define TIMEBINS
Definition: timebinmgr.h:13
int32_t inttime_t
Definition: types.h:8

References SyncPoint::loga, NSyncPoints, SyncPoints, TIMEBASE, and TIMEBINS.

Referenced by dloga_from_dti(), get_dloga_for_bin(), and loga_from_ti().

Here is the caller graph for this function:

◆ dti_from_dloga()

inttime_t dti_from_dloga ( double  loga,
const inttime_t  Ti_Current 
)

Definition at line 271 of file timebinmgr.c.

272 {
273  inttime_t ti = ti_from_loga(loga_from_ti(Ti_Current));
274  inttime_t tip = ti_from_loga(loga+loga_from_ti(Ti_Current));
275  return tip - ti;
276 }
inttime_t ti_from_loga(double loga)
Definition: timebinmgr.c:236
double loga_from_ti(inttime_t ti)
Definition: timebinmgr.c:223

References loga_from_ti(), and ti_from_loga().

Referenced by get_PM_timestep_ti(), and get_timestep_ti().

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

◆ find_current_sync_point()

SyncPoint* find_current_sync_point ( inttime_t  ti)

Definition at line 188 of file timebinmgr.c.

189 {
190  int i;
191  for(i = 0; i < NSyncPoints; i ++) {
192  if(SyncPoints[i].ti == ti) {
193  return &SyncPoints[i];
194  }
195  }
196  return NULL;
197 }

References NSyncPoints, and SyncPoints.

Referenced by run(), test_conversions(), and test_skip_first().

Here is the caller graph for this function:

◆ find_next_sync_point()

SyncPoint* find_next_sync_point ( inttime_t  ti)

this function returns the next output time that is in the future of ti_curr; if none is find it return NULL, indication the run shall terminate.

Definition at line 174 of file timebinmgr.c.

175 {
176  int i;
177  for(i = 0; i < NSyncPoints; i ++) {
178  if(SyncPoints[i].ti > ti) {
179  return &SyncPoints[i];
180  }
181  }
182  return NULL;
183 }

References NSyncPoints, and SyncPoints.

Referenced by get_PM_timestep_ti(), run(), and test_conversions().

Here is the caller graph for this function:

◆ get_dloga_for_bin()

double get_dloga_for_bin ( int  timebin,
const inttime_t  Ti_Current 
)

Definition at line 279 of file timebinmgr.c.

280 {
281  double logDTime = Dloga_interval_ti(Ti_Current);
282  return (timebin > 0 ? (1u << (unsigned) timebin) : 0 ) * logDTime;
283 }

References Dloga_interval_ti().

Referenced by blackhole_accretion_postprocess(), blackhole_feedback_copy(), cooling_direct(), get_helium_neutral_fraction_sfreff(), get_neutral_fraction_sfreff(), get_sfr_eeqos(), get_timestep_dloga(), hydro_copy(), hydro_ngbiter(), print_timebin_statistics(), run(), sfr_wind_copy(), test_dloga(), and winds_evolve().

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

◆ loga_from_ti()

double loga_from_ti ( inttime_t  ti)

Definition at line 223 of file timebinmgr.c.

224 {
225  inttime_t lastsnap = ti >> TIMEBINS;
226  if(lastsnap > NSyncPoints) {
227  endrun(1, "Requesting snap %d, from ti %d, beyond last sync point %d\n", lastsnap, ti, NSyncPoints);
228  }
229  double last = SyncPoints[lastsnap].loga;
230  inttime_t dti = ti & (TIMEBASE - 1);
231  double logDTime = Dloga_interval_ti(ti);
232  return last + dti * logDTime;
233 }

References Dloga_interval_ti(), endrun(), SyncPoint::loga, NSyncPoints, SyncPoints, TIMEBASE, and TIMEBINS.

Referenced by dti_from_dloga().

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

◆ OutputListAction()

int OutputListAction ( ParameterSet ps,
const char *  name,
void *  data 
)

This function parses a string containing a comma-separated list of variables, each of which is interpreted as a double. The purpose is to read an array of output times into the code. So specifying the output list now looks like: OutputList 0.1,0.3,0.5,1.0

We sort the input after reading it, so that the initial list need not be sorted. This function could be repurposed for reading generic arrays in future.

Definition at line 31 of file timebinmgr.c.

32 {
33  char * outputlist = param_get_string(ps, name);
34  char * strtmp = fastpm_strdup(outputlist);
35  char * token;
36  int count;
37 
38  /* Note TimeInit and TimeMax not yet initialised here*/
39 
40  /*First parse the string to get the number of outputs*/
41  for(count=0, token=strtok(strtmp,","); token; count++, token=strtok(NULL, ","))
42  {}
43 /* message(1, "Found %d times in output list.\n", count); */
44 
45  /*Allocate enough memory*/
46  Sync.OutputListLength = count;
47  int maxcount = sizeof(Sync.OutputListTimes) / sizeof(Sync.OutputListTimes[0]);
48  if(maxcount > (int) MAXSNAPSHOTS)
49  maxcount = MAXSNAPSHOTS;
50  if(Sync.OutputListLength > maxcount) {
51  message(1, "Too many entries (%d) in the OutputList, can take no more than %d.\n", Sync.OutputListLength, maxcount);
52  return 1;
53  }
54  /*Now read in the values*/
55  for(count=0,token=strtok(outputlist,","); count < Sync.OutputListLength && token; count++, token=strtok(NULL,","))
56  {
57  /* Skip a leading quote if one exists.
58  * Extra characters are ignored by atof, so
59  * no need to skip matching char.*/
60  if(token[0] == '"')
61  token+=1;
62 
63  double a = atof(token);
64 
65  if(a < 0.0) {
66  endrun(1, "Requesting a negative output scaling factor a = %g\n", a);
67  }
68  Sync.OutputListTimes[count] = a;
69 /* message(1, "Output at: %g\n", Sync.OutputListTimes[count]); */
70  }
71  myfree(strtmp);
72  return 0;
73 }
const char * name
Definition: densitykernel.c:93
void message(int where, const char *fmt,...)
Definition: endrun.c:175
#define myfree(x)
Definition: mymalloc.h:19
char * param_get_string(ParameterSet *ps, const char *name)
Definition: paramset.c:346
char * fastpm_strdup(const char *str)
Definition: string.c:31
int OutputListLength
Definition: timebinmgr.c:13
double OutputListTimes[1024]
Definition: timebinmgr.c:14
static struct sync_params Sync
#define MAXSNAPSHOTS
Definition: timebinmgr.h:15

References endrun(), fastpm_strdup(), MAXSNAPSHOTS, message(), myfree, name, sync_params::OutputListLength, sync_params::OutputListTimes, param_get_string(), and Sync.

Referenced by create_gadget_parameter_set().

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

◆ round_down_power_of_two()

inttime_t round_down_power_of_two ( inttime_t  dti)

Definition at line 286 of file timebinmgr.c.

287 {
288  /* make dti a power 2 subdivision */
289  inttime_t ti_min = TIMEBASE;
290  int sign = 1;
291  if(dti < 0) {
292  dti = -dti;
293  sign = -1;
294  }
295  while(ti_min > dti)
296  ti_min >>= 1;
297  return ti_min * sign;
298 }

References TIMEBASE.

Referenced by find_timesteps(), get_PM_timestep_ti(), init_timeline(), and test_dloga().

Here is the caller graph for this function:

◆ set_sync_params()

void set_sync_params ( int  OutputListLength,
double *  OutputListTimes 
)

Definition at line 76 of file timebinmgr.c.

77 {
78  int i;
80  for(i = 0; i < OutputListLength; i++)
81  Sync.OutputListTimes[i] = OutputListTimes[i];
82 }
#define OutputListLength

References OutputListLength, sync_params::OutputListLength, sync_params::OutputListTimes, and Sync.

Referenced by setup().

Here is the caller graph for this function:

◆ setup_sync_points()

void setup_sync_points ( double  TimeIC,
double  TimeMax,
double  no_snapshot_until_time,
int  SnapshotWithFOF 
)

Definition at line 97 of file timebinmgr.c.

98 {
99  int i;
100 
102 
103  if(NSyncPoints > 0)
105  SyncPoints = (SyncPoint *) mymalloc("SyncPoints", sizeof(SyncPoint) * (Sync.OutputListLength+2));
106 
107  /* Set up first and last entry to SyncPoints; TODO we can insert many more! */
108 
109  SyncPoints[0].a = TimeIC;
110  SyncPoints[0].loga = log(TimeIC);
111  SyncPoints[0].write_snapshot = 0; /* by default no output here. */
112  SyncPoints[0].write_fof = 0;
113  SyncPoints[1].a = TimeMax;
114  SyncPoints[1].loga = log(TimeMax);
115  SyncPoints[1].write_snapshot = 1;
116  if(SnapshotWithFOF)
117  SyncPoints[1].write_fof = 1;
118  else
119  SyncPoints[1].write_fof = 0;
120  NSyncPoints = 2;
121 
122  /* we do an insertion sort here. A heap is faster but who cares the speed for this? */
123  for(i = 0; i < Sync.OutputListLength; i ++) {
124  int j = 0;
125  double a = Sync.OutputListTimes[i];
126  double loga = log(a);
127 
128  for(j = 0; j < NSyncPoints; j ++) {
129  if(a <= SyncPoints[j].a) {
130  break;
131  }
132  }
133  if(j == NSyncPoints) {
134  /* beyond TimeMax, skip */
135  continue;
136  }
137  /* found, so loga >= SyncPoints[j].loga */
138  if(a == SyncPoints[j].a) {
139  /* requesting output on an existing entry, e.g. TimeInit or duplicated entry */
140  } else {
141  /* insert the item; */
142  memmove(&SyncPoints[j + 1], &SyncPoints[j],
143  sizeof(SyncPoints[0]) * (NSyncPoints - j));
144  SyncPoints[j].a = a;
145  SyncPoints[j].loga = loga;
146  NSyncPoints ++;
147  }
148  if(SyncPoints[j].a > no_snapshot_until_time) {
149  SyncPoints[j].write_snapshot = 1;
150  if(SnapshotWithFOF) {
151  SyncPoints[j].write_fof = 1;
152  }
153  else
154  SyncPoints[j].write_fof = 0;
155  } else {
156  SyncPoints[j].write_snapshot = 0;
157  SyncPoints[j].write_fof = 0;
158  }
159  }
160 
161  for(i = 0; i < NSyncPoints; i++) {
162  SyncPoints[i].ti = (i * 1L) << (TIMEBINS);
163  }
164 
165 /* for(i = 0; i < NSyncPoints; i++) { */
166 /* message(1,"Out: %g %ld\n", exp(SyncPoints[i].loga), SyncPoints[i].ti); */
167 /* } */
168 }
#define mymalloc(name, size)
Definition: mymalloc.h:15
inttime_t ti
Definition: timebinmgr.h:28
double a
Definition: timebinmgr.h:24
int write_fof
Definition: timebinmgr.h:27
int write_snapshot
Definition: timebinmgr.h:26
#define qsort_openmp
Definition: test_exchange.c:14
int cmp_double(const void *a, const void *b)
Definition: timebinmgr.c:17

References SyncPoint::a, cmp_double(), SyncPoint::loga, myfree, mymalloc, NSyncPoints, sync_params::OutputListLength, sync_params::OutputListTimes, qsort_openmp, Sync, SyncPoints, SyncPoint::ti, TIMEBINS, SyncPoint::write_fof, and SyncPoint::write_snapshot.

Referenced by init_timeline(), setup_density(), test_conversions(), test_dloga(), and test_skip_first().

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

◆ ti_from_loga()

inttime_t ti_from_loga ( double  loga)

Definition at line 236 of file timebinmgr.c.

237 {
238  int i;
239  int ti;
240  /* First syncpoint is simulation start*/
241  for(i = 1; i < NSyncPoints - 1; i++)
242  {
243  if(SyncPoints[i].loga > loga)
244  break;
245  }
246  /*If loop didn't trigger, i == All.NSyncPointTimes-1*/
247  double logDTime = (SyncPoints[i].loga - SyncPoints[i-1].loga)/TIMEBASE;
248  ti = (i-1) << TIMEBINS;
249  /* Note this means if we overrun the end of the timeline,
250  * we still get something reasonable*/
251  ti += (loga - SyncPoints[i-1].loga)/logDTime;
252  return ti;
253 }

References SyncPoint::loga, NSyncPoints, SyncPoints, TIMEBASE, and TIMEBINS.

Referenced by dti_from_dloga(), init_timebins(), init_timeline(), test_conversions(), and test_dloga().

Here is the caller graph for this function:

Variable Documentation

◆ NSyncPoints

int NSyncPoints
static

◆ Sync

struct sync_params Sync
static

◆ SyncPoints

SyncPoint* SyncPoints
static

table with desired sync points. All forces and phase space variables are synchonized to the same order.

Definition at line 9 of file timebinmgr.c.

Referenced by Dloga_interval_ti(), find_current_sync_point(), find_next_sync_point(), loga_from_ti(), setup_sync_points(), and ti_from_loga().