-
Notifications
You must be signed in to change notification settings - Fork 1
/
emu.c
296 lines (226 loc) · 8.1 KB
/
emu.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <stdlib.h>
#include <stdio.h>
#include "emu.h"
#include "params.h"
#include <math.h>
int nk = 661; // Number of k bins
int numPC = 10; // Number of pricipal components
int nmodels = 100; // How many models to cover parameter space
int nmodels_0_4985 = 149;
int nparams = 5; // Number of cosmological parameters
int make_sigma_w(int nmodels, float designparams[][nparams], double *newparams, gsl_matrix *V11, gsl_matrix *V21, int PCnow, int numz);
int read_design(FILE *fp, int nmodels, float design[][nparams]);
int invert_matrix(int size, gsl_matrix *A, gsl_matrix *A_inv);
int emu(double *newparams, double outputredshift, double *output_pk)
{
int n;
int offset = 0;
char inputs[256];
float designparams[nmodels][nparams]; // this is nmodels x nparameters
float designparams_0_4985[nmodels_0_4985][nparams]; // this is nmodels x nparameters
//read in the design matrix
FILE *fpdesign = fopen("s-lhs.100.5_1", "r");
read_design(fpdesign, nmodels, designparams);
fclose(fpdesign);
fpdesign = fopen("s-lhs.149.5_1_combined", "r");
read_design(fpdesign, nmodels_0_4985, designparams_0_4985);
fclose(fpdesign);
// xstar contains the 5 emulator parameters plus the red shift.
double xstar[6], stuff[4];
xstar[0] = newparams[0];
xstar[1] = newparams[1];
xstar[2] = newparams[2];
xstar[3] = newparams[4];
xstar[4] = newparams[3];
xstar[5] = outputredshift;
//identify output redshift amongst input snapshots
double scalefactor[6] = {0.4985, 0.6086, 0.6974, 0.8051, 0.9083, 1.0};
double redshifts[6];
for (n = 0; n < 6; n++)
redshifts[n] = 1./scalefactor[n]-1;
double output_scalefactor = 1./(outputredshift+1.);
int numz_want = 0;
for (n=0; n < 6; n++)
{
if (output_scalefactor > scalefactor[n])
{
numz_want = n;
}
}
double wpred[numPC]; //predicted weights at new P(k) relation
int numz = 0; // redshift
double *pkpred = calloc(nk*6,sizeof(double));
for (numz = numz_want; numz < numz_want+2; numz++) // only do the redshifts needed
{
if (numz == 0)
{
gsl_matrix * V11 = gsl_matrix_alloc (nmodels_0_4985, nmodels_0_4985);
gsl_matrix * V11_inv = gsl_matrix_alloc (nmodels_0_4985, nmodels_0_4985);
gsl_matrix * V21 = gsl_matrix_alloc (1, nmodels_0_4985); // leave this as a matrix in case several interpolations are wanted at once;
for (n = 0; n < numPC; n++)
{
wpred[n] = 0;
make_sigma_w(nmodels_0_4985, designparams_0_4985, newparams, V11, V21,n, numz);
invert_matrix(nmodels_0_4985, V11, V11_inv);
int i, j;
double *dummyreslt = calloc(nmodels_0_4985,sizeof(double));
for (i = 0; i < nmodels_0_4985; i++)
{
for(j = 0; j < nmodels_0_4985; j++)
{
dummyreslt[i] += gsl_matrix_get(V11_inv, i, j)*what_0_4985[j+n*nmodels_0_4985];
}
// fprintf(stderr, "%f\n", dummyreslt[i]);
}
for (i = 0; i < nmodels_0_4985; i++)
wpred[n] += gsl_matrix_get(V21, 0, i)*dummyreslt[i];
free(dummyreslt);
// fprintf(stderr, "%f\n", wpred[n]);
}
gsl_matrix_free(V11);
gsl_matrix_free(V11_inv);
gsl_matrix_free(V21);
}
else
{
gsl_matrix * V11 = gsl_matrix_alloc (nmodels, nmodels);
gsl_matrix * V11_inv = gsl_matrix_alloc (nmodels, nmodels);
gsl_matrix * V21 = gsl_matrix_alloc (1, nmodels); // leave this as a matrix in case several interpolations are wanted at once;
for (n = 0; n < numPC; n++)
{
wpred[n] = 0;
make_sigma_w(nmodels, designparams, newparams, V11, V21,n, numz);
invert_matrix(nmodels, V11, V11_inv);
int i, j;
double *dummyreslt = calloc(nmodels,sizeof(double));
for (i = 0; i < nmodels; i++)
{
for(j = 0; j < nmodels; j++)
dummyreslt[i] += gsl_matrix_get(V11_inv, i, j)*what[j+n*nmodels][numz-1];
}
for (i = 0; i < nmodels; i++)
wpred[n] += gsl_matrix_get(V21, 0, i)*dummyreslt[i];
free(dummyreslt);
}
gsl_matrix_free(V11);
gsl_matrix_free(V11_inv);
gsl_matrix_free(V21);
}
int m;
for (n = 0; n < nk; n++)
{
if (logk[n] > -2) /* // P(k) emulator */
{
if (offset==0)
offset=n;
pkpred[6*n+numz] = ymean[n-offset][numz];
for (m = 0; m < numPC; m++)
{
pkpred[6*n+numz] += phi[n-offset][numz*numPC+m]*wpred[m]*ysimstd[n-offset][numz];
}
}
else
{
// large scale modes for xi calculation only
pkpred[6*n+numz] = ymean[0][numz];
for (m = 0; m < numPC; m++)
{
pkpred[6*n+numz] += phi[0][numz*numPC+m]*wpred[m]*ysimstd[0][numz];
}
}
}
}
gsl_spline *spline_bias = gsl_spline_alloc(gsl_interp_linear, 2);
gsl_interp_accel *acc_bias = gsl_interp_accel_alloc ();
gsl_spline *spline_pk = gsl_spline_alloc(gsl_interp_linear, 2);
gsl_interp_accel *acc_pk = gsl_interp_accel_alloc ();
// Convert back to P(k) from log10(bias/k)
for (n = 0; n < nk; n++)
{
gsl_spline_init (spline_bias, &(scalefactor[numz_want]), &(pkpred[6*n+numz_want]), 2);
gsl_spline_init (spline_pk, &(scalefactor[numz_want]), &(pk_m[n][numz_want]), 2);
double bias = gsl_spline_eval(spline_bias, output_scalefactor, acc_bias);
double k_unlogged;
if (logk[n] > -2)
k_unlogged = pow(10.,logk[n]);
else
k_unlogged = pow(10.,logk[offset]); // these modes are extrapolated with the linear bias (for xi only)
bias = pow(10.,bias);
output_pk[n] = bias*k_unlogged*gsl_spline_eval(spline_pk, output_scalefactor, acc_pk);
gsl_interp_accel_reset(acc_bias);
gsl_interp_accel_reset(acc_pk);
}
gsl_spline_free(spline_bias); gsl_spline_free(spline_pk);
gsl_interp_accel_free(acc_bias); gsl_interp_accel_free(acc_pk);
free(pkpred);
return(0);
}
int read_design(FILE *fp, int nmodels, float design[][nparams])
{
// this is the design matrix
int i, j, k;
for (i = 0; i < nmodels; i++)
for (j = 0; j < nparams;j++)
fscanf(fp, "%f", &(design[i][j]));
return(0);
}
int make_sigma_w(int nmodels, float designparams[][nparams], double *newparams, gsl_matrix *V11, gsl_matrix *V21, int PCnow, int numz)
{
// Do each PC individually.
int i, j, k;
for (i = 0; i < nmodels; i++)
{
for (j = 0; j < nmodels; j++)
/* for (j = 0; j < i; j++) */
{
double V11dummy = 1.;
for (k = 0; k < nparams; k++)
{
double distance = designparams[i][k]-designparams[j][k]; // always substract the same type of parameter from the same type i.e. Omegam_1 - Omegam_2 not Omegam_1-w
distance = 4.*distance*distance;
V11dummy *= pow(rho_w[numz*nparams+k][PCnow], distance);
}
V11dummy /= lambda_w[numz*numPC+PCnow];
if (i==j)
V11dummy += 1./lambdaP[numz];
gsl_matrix_set (V11, i, j, V11dummy);
}
}
for (j = 0; j < nmodels; j++)
{
double V21dummy = 1.;
for (k = 0; k < nparams; k++)
{
double distance = designparams[j][k]-newparams[k];
distance = 4.*distance*distance;
V21dummy *= pow(rho_w[numz*nparams+k][PCnow], distance);
}
V21dummy /= lambda_w[numz*numPC+PCnow];
gsl_matrix_set(V21, 0, j, V21dummy);
}
return(0);
}
int invert_matrix(int size, gsl_matrix *A, gsl_matrix *A_inv)
{
gsl_matrix *Adummy = gsl_matrix_alloc(size,size);
gsl_matrix *Adummy2 = gsl_matrix_alloc(size,size);
gsl_matrix_memcpy (Adummy, A);
/* gsl_linalg_cholesky_decomp (Adummy); // Adummy will be overwritten */
/* gsl_linalg_cholesky_invert (Adummy); // This is just the inverse of the lower half of the decomposed matrix */
gsl_vector *work = gsl_vector_alloc(size);
gsl_matrix *V = gsl_matrix_alloc(size,size); // V is untransposed
gsl_vector *Sdiag = gsl_vector_alloc(size);
gsl_matrix *S = gsl_matrix_alloc(size,size);
gsl_linalg_SV_decomp(Adummy, V, Sdiag, work); //A_inv is actually U: A = USV', Adummy becomes U on output;
int i,j;
double reslt = 0;
gsl_matrix_set_zero(S);
for (i=0; i < size; i++)
{
double reslt = gsl_vector_get(Sdiag,i);
gsl_matrix_set(S,i,i, 1./reslt);
}
gsl_blas_dgemm(CblasNoTrans, CblasTrans,1.0,S, Adummy, 0.0, Adummy2);
gsl_blas_dgemm(CblasNoTrans, CblasNoTrans,1.0,V, Adummy2, 0.0, A_inv);
return(0);
}