-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomfield.h
342 lines (260 loc) · 10.4 KB
/
randomfield.h
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#pragma once
#include "main.h"
#include <vector>
#include <complex>
#include <cmath>
#include <memory>
#include <iostream>
#include <limits>
#include "fftw3.h"
#include "ExactSum.h"
class RandomField {
public:
RandomField(Long n) : nGrid(n) {
/* allocate memory */
field.reserve(nGrid);
modes.reserve(nGrid);
/* these will be deleted at the end of generate! */
Long nModes = nGrid/2+1;
out_ = (double*) fftw_malloc(sizeof(double) * nGrid);
in_ = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * nModes);
/* planner is not thread save. We assume that RandomFields are always only constructed from a single thread
* (the plan could also be global and passed as it should be possible to use the same plan in parallel, but this is ugly - I'd like to contain the FFTW stuff in here */
p_ = fftw_plan_dft_c2r_1d(nGrid, in_, out_, FFTW_ESTIMATE);
//p = fftw_plan_dft_1d(nGrid, reinterpret_cast<fftw_complex*>(&modes[0]), reinterpret_cast<fftw_complex*>(&field[0]), FFTW_BACKWARD, FFTW_ESTIMATE);
/* this plan will not be deleted in generate, again, it is not clear this is thread-safe */
}
fftw_complex *in_;
double *out_;
fftw_plan p_;
RandomField(RandomField&& other) : RandomField(other.nGrid) {
field = std::move(other.field);
modes = std::move(other.modes);
in_ = other.in_;
other.in_ = nullptr;
out_ = other.out_;
other.out_ = nullptr;
p_ = other.p_;
other.p_ = nullptr;
}
RandomField(const RandomField& other) = delete;
~RandomField() {
if (p_ != nullptr)
fftw_destroy_plan(p_);
}
/* this parallelizes but grid size has to be known */
void generate(pcg32& pcg, PowerSpectrum* spec) {
Long nModes = nGrid/2+1;
modes.clear();
modes.resize(nModes);
for (int i = 1; i < nModes; ++i) {
Float u1 = pcg.nextFloat();
Float u2 = pcg.nextFloat();
while (u1 <= std::numeric_limits<Float>::min())
u1 = pcg.nextFloat();
Float s, c;
c = cospi(2*u2);
Float normal = std::sqrt(-2 * std::log(u1)) * c;
std::complex<double> mode;
sincospi(pcg.nextFloat()*2, &s, &c);
mode.real(s);
mode.imag(c);
Float k = 2*pi*i;
modes[i] = mode*std::sqrt(spec->eval_dimless(i))*normal/**std::exp(-k*k*1e-8);*/;
}
modes[0] = 0;
// to be absolutely sure it is safe we avoid reinterpret_cast<fftw_complex*>(&mode[0])
for (int i = 1; i < nModes; ++i) {
Float k = 2*pi*i;
/* already divide by k here to get the displacement field */
in_[i][0] = -modes[i].imag()/k;
in_[i][1] = modes[i].real()/k;
}
in_[0][0] = 0;
in_[0][1] = 0;
/* let's not put stuff in nyquist */
in_[nModes-1][1] = 0;
in_[nModes-1][0] = 0;
fftw_execute(p_);
displacementfield.clear();
displacementfield.resize(nGrid);
for (int i = 0; i < nGrid; ++i) {
displacementfield[i] = out_[i];
}
/* c2r in FFTW destroys input array even for out-of-place trafo */
for (int i = 1; i < nModes; ++i) {
in_[i][0] = modes[i].real();
in_[i][1] = modes[i].imag();
}
in_[0][0] = 0;
in_[0][1] = 0;
/* let's not put stuff in nyquist */
in_[nModes-1][1] = 0;
in_[nModes-1][0] = 0;
fftw_execute(p_);
field.clear();
field.resize(nGrid);
for (int i = 0; i < nGrid; ++i) {
field[i] = out_[i];
}
//fftw_destroy_plan(p_);
fftw_free(in_);
fftw_free(out_);
//std::cout << "field0 " << field[0] << " field1 " << field[1] << std::endl;
}
Float get_field_at(Float x) const {
assert(x >= 0);
Long idx = x*nGrid;
idx = std::max(long(0), idx);
idx = std::min(idx, nGrid-1);
return field[idx];
//Sum s;
//for (int i = 1; i < modes.size(); ++i) {
//double sn, cs;
//sincospi(2.0*i*x, &sn, &cs);
//s += 2*(sn*modes[i].imag() + cs*modes[i].real());
//}
//return s;
//Sum r;
/* assume nGrid is divisible by 2 */
//for (int i = idx-nGrid/2; i < idx+nGrid/2; ++i) {
//Float sincarg = x*nGrid-i;
//Float sinc = 1;
//if (std::fabs(sincarg) > 0.0000001)
//sinc = sinpi(sincarg)/(pi*sincarg);
//else
//sinc = 1 - pi*pi*sincarg*sincarg/6;
//int j = i;
//if (j < 0) j = j + nGrid;
//if (j >= nGrid) j = j - nGrid;
//r += field[j]*sinc;
//}
//return r;
}
/* this displacement still needs to be multiplied by L to be correct */
Float get_displacement_at(Float x) const {
assert(x >= 0);
Long idx = x*nGrid;
//Sum s;
//for (int i = 1; i < modes.size(); ++i) {
//double sn, cs;
//sincospi(2*i*x, &sn, &cs);
//Float k = 2*pi*i;
//s += 2*(-cs*modes[i].imag() + sn*modes[i].real())/k;
//}
//return s;
//if (std::fabs(x*nGrid-std::round(x*nGrid)) < 1e-4) {
idx = std::max(long(0), idx);
idx = std::min(idx, nGrid-1);
return displacementfield[idx];
//}
/* else, interpolate with Whittaker-Shannon: this does not include higher frequency components but gives the unique
* band-limited signal of highest band-limit that can exactly be represented by some discrete samples */
//Sum r;
//[> assume nGrid is divisible by 2 <]
//for (int i = idx-nGrid/2; i < idx+nGrid/2; ++i) {
//Float sincarg = x*nGrid-i;
//Float sinc = 1;
//if (std::fabs(sincarg) > 0.0000001)
//sinc = sinpi(sincarg)/(pi*sincarg);
//else
//sinc = 1 - pi*pi*sincarg*sincarg/6;
//int j = i;
//if (j < 0) j = j + nGrid;
//if (j >= nGrid) j = j - nGrid;
//r += displacementfield[j]*sinc;
//}
//return r;
}
/* does not parallelize! */
std::vector<Float> get_field(int res, int bandlimit = 0) const {
if (bandlimit == 0)
bandlimit = res;
std::vector<Float> ret;
ret.resize(res);
/* return native field if limit exceeds it */
if (bandlimit >= nGrid) {
for (int i = 0; i < res; ++i)
ret[i] = get_field_at(Float(i)/Float(res));
return ret;
}
/* else, we create an aliasing problem when simply subsampling the field. the best solution is to recompute the FFT with less modes */
double * out = (double*) fftw_malloc(sizeof(double) * res);
Long nModes = res/2+1;
fftw_complex * in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * nModes);
fftw_plan pl;
pl = fftw_plan_dft_c2r_1d(res, in, out, FFTW_ESTIMATE);
for (int i = 1; i < nModes; ++i) {
Float k = 2*pi*i;
if (i < bandlimit/2+1) {
in[i][0] = modes[i].real();
in[i][1] = modes[i].imag();
}
else {
in[i][0] = 0;
in[i][1] = 0;
}
}
in[0][0] = 0;
in[0][1] = 0;
//[> let's not put stuff in nyquist <]
in[nModes-1][1] = 0;
in[nModes-1][0] = 0;
fftw_execute(pl);
for (int i = 0; i < res; ++i) {
ret[i] = out[i];
}
fftw_destroy_plan(pl);
fftw_free(in);
fftw_free(out);
return ret;
}
/* does not parallelize! */
std::vector<Float> get_displacement(int res, int bandlimit = 0) const {
if (bandlimit == 0)
bandlimit = res;
std::vector<Float> ret;
ret.resize(res);
/* return native field if limit exceeds it */
if (bandlimit >= nGrid) {
for (int i = 0; i < res; ++i)
ret[i] = get_displacement_at(Float(i)/Float(res));
return ret;
}
/* else, we have an aliasing problem simply subsampling the field. the best solution is to recompute the FFT with less modes */
double * out = (double*) fftw_malloc(sizeof(double) * res);
Long nModes = res/2+1;
fftw_complex * in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * nModes);
fftw_plan pl = fftw_plan_dft_c2r_1d(res, in, out, FFTW_ESTIMATE);
for (int i = 1; i < nModes; ++i) {
Float k = 2*pi*i;
if (i < bandlimit/2+1) {
in[i][0] = -modes[i].imag()/k;
in[i][1] = modes[i].real()/k;
}
else {
in[i][0] = 0;
in[i][1] = 0;
}
}
in[0][0] = 0;
in[0][1] = 0;
/* let's not put stuff in nyquist */
in[nModes-1][1] = 0;
in[nModes-1][0] = 0;
fftw_execute(pl);
for (int i = 0; i < res; ++i) {
ret[i] = out[i];
}
fftw_destroy_plan(pl);
fftw_free(in);
fftw_free(out);
return ret;
}
/* Assuming to be in the linear regime, the density is related to the displacement of an Eulerian grid in a simple way. Similarly, this allows us to get the velocities from the time derivative. The result will be rather uniform, which is also good, as averaging different nonlinear realizations is not a good strategy (this Monte Carlo strategy averaging results for delta-function ICs that average to the right ones makes sense only for linear evolution). Zeldovich holds also at later times in 1D, but in the nonlinear regime it becomes more difficult to get the right initial displacements that give a given nonlinear density. One could find any other method to put particles according to the random field at any time and get the displacements from comparing to the nearest free Eulerian grid position... but all of it is simple in the linear regime. */
private:
std::vector<double> field;
std::vector<double> displacementfield;
std::vector<std::complex<double>> modes;
const Long nGrid;
};