forked from pfhopkins/gizmo-public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriftfac.c
194 lines (149 loc) · 4.88 KB
/
driftfac.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#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 "allvars.h"
#include "proto.h"
/*
* This routine calculates the pre-factors and timesteps for cosmological
* simulations where the time-stepping is done in co-moving units and the
* time units are the scale factor. Basically the pre-factors that need to be
* combined and/or integrated are calculated here, to be combined with the
* appropriate time derivatives as calculated in code units elsewhere.
*/
/*
* This file was originally part of the GADGET3 code developed by
* Volker Springel. The code has been modified
* by Phil Hopkins ([email protected]) for GIZMO; the conventions for the
* timestep units are different so this is revised here. Computation uses
* different libraries now as well.
*/
static double logTimeBegin;
static double logTimeMax;
double drift_integ(double a, void *param)
{
double h;
h = hubble_function(a);
return 1 / (h * a * a * a);
}
double gravkick_integ(double a, void *param)
{
double h;
h = hubble_function(a);
return 1 / (h * a * a);
}
double growthfactor_integ(double a, void *param)
{
double s;
s = hubble_function(a) / All.Hubble_H0_CodeUnits * sqrt(a * a * a);
return pow(sqrt(a) / s, 3);
}
void init_drift_table(void)
{
#define WORKSIZE 100000
int i;
double result, abserr;
gsl_function F;
gsl_integration_workspace *workspace;
logTimeBegin = log(All.TimeBegin);
logTimeMax = log(All.TimeMax);
workspace = gsl_integration_workspace_alloc(WORKSIZE);
for(i = 0; i < DRIFT_TABLE_LENGTH; i++)
{
F.function = &drift_integ;
gsl_integration_qag(&F, exp(logTimeBegin),
exp(logTimeBegin + ((logTimeMax - logTimeBegin) / DRIFT_TABLE_LENGTH) * (i + 1)), 0,
1.0e-8, WORKSIZE, GSL_INTEG_GAUSS41, workspace, &result, &abserr);
DriftTable[i] = result;
F.function = &gravkick_integ;
gsl_integration_qag(&F, exp(logTimeBegin),
exp(logTimeBegin + ((logTimeMax - logTimeBegin) / DRIFT_TABLE_LENGTH) * (i + 1)), 0,
1.0e-8, WORKSIZE, GSL_INTEG_GAUSS41, workspace, &result, &abserr);
GravKickTable[i] = result;
}
gsl_integration_workspace_free(workspace);
}
/*! This function integrates the cosmological prefactor for a drift
* step between time0 and time1. The value returned is
* \f[ \int_{a_0}^{a_1} \frac{{\rm d}a}{H(a)}
* \f]
*
* A lookup-table is used for reasons of speed.
*/
double get_drift_factor(integertime time0, integertime time1)
{
double a1, a2, df1, df2, u1, u2;
int i1, i2;
static integertime last_time0 = -1, last_time1 = -1;
static double last_value;
if(time0 == last_time0 && time1 == last_time1)
return last_value;
/* note: will only be called for cosmological integration */
a1 = logTimeBegin + time0 * All.Timebase_interval;
a2 = logTimeBegin + time1 * All.Timebase_interval;
if(logTimeMax > logTimeBegin)
u1 = (a1 - logTimeBegin) / (logTimeMax - logTimeBegin) * DRIFT_TABLE_LENGTH;
else
u1 = 0;
i1 = (int) u1;
if(i1 >= DRIFT_TABLE_LENGTH)
i1 = DRIFT_TABLE_LENGTH - 1;
if(i1 <= 1)
df1 = u1 * DriftTable[0];
else
df1 = DriftTable[i1 - 1] + (DriftTable[i1] - DriftTable[i1 - 1]) * (u1 - i1);
if(logTimeMax > logTimeBegin)
u2 = (a2 - logTimeBegin) / (logTimeMax - logTimeBegin) * DRIFT_TABLE_LENGTH;
else
u2 = 0;
i2 = (int) u2;
if(i2 >= DRIFT_TABLE_LENGTH)
i2 = DRIFT_TABLE_LENGTH - 1;
if(i2 <= 1)
df2 = u2 * DriftTable[0];
else
df2 = DriftTable[i2 - 1] + (DriftTable[i2] - DriftTable[i2 - 1]) * (u2 - i2);
last_time0 = time0;
last_time1 = time1;
return last_value = (df2 - df1);
}
double get_gravkick_factor(integertime time0, integertime time1)
{
double a1, a2, df1, df2, u1, u2;
int i1, i2;
static integertime last_time0 = -1, last_time1 = -1;
static double last_value;
if(time0 == last_time0 && time1 == last_time1)
return last_value;
/* note: will only be called for cosmological integration */
a1 = logTimeBegin + time0 * All.Timebase_interval;
a2 = logTimeBegin + time1 * All.Timebase_interval;
if(logTimeMax > logTimeBegin)
u1 = (a1 - logTimeBegin) / (logTimeMax - logTimeBegin) * DRIFT_TABLE_LENGTH;
else
u1 = 0;
i1 = (int) u1;
if(i1 >= DRIFT_TABLE_LENGTH)
i1 = DRIFT_TABLE_LENGTH - 1;
if(i1 <= 1)
df1 = u1 * GravKickTable[0];
else
df1 = GravKickTable[i1 - 1] + (GravKickTable[i1] - GravKickTable[i1 - 1]) * (u1 - i1);
if(logTimeMax > logTimeBegin)
u2 = (a2 - logTimeBegin) / (logTimeMax - logTimeBegin) * DRIFT_TABLE_LENGTH;
else
u2 = 0;
i2 = (int) u2;
if(i2 >= DRIFT_TABLE_LENGTH)
i2 = DRIFT_TABLE_LENGTH - 1;
if(i2 <= 1)
df2 = u2 * GravKickTable[0];
else
df2 = GravKickTable[i2 - 1] + (GravKickTable[i2] - GravKickTable[i2 - 1]) * (u2 - i2);
last_time0 = time0;
last_time1 = time1;
return last_value = (df2 - df1);
}