testRandom.c 15 KB
Newer Older
1
2
3
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (C) 2019 Matthieu Schaller (schaller@strw.leidenuniv.nl)
Folkert Nobels's avatar
Folkert Nobels committed
4
 *               2019 Folkert Nobels    (nobels@strw.leidenuniv.nl)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 ******************************************************************************/

/* Config parameters. */
#include "../config.h"

#include <fenv.h>

/* Local headers. */
#include "swift.h"

29
/**
30
31
32
33
 * @brief Compute the Pearson correlation coefficient for two sets of numbers
 *
 * The pearson correlation coefficient between two sets of numbers can be
 * calculated as:
34
 *
35
36
37
38
 *           <x*y> - <x>*<y>
 * r_xy = ----------------------
 *         (var(x) * var(y))^.5
 *
39
40
41
 * In the case that both sets are purely uncorrelated the value of the
 * Pearson correlation function is expected to be close to 0. In the case that
 * there is positive correlation r_xy > 0 and in the case of negative
42
43
44
45
46
47
48
 * correlation, the function has r_xy < 0.
 *
 * @param mean1 average of first series of numbers
 * @param mean2 average of second series of numbers
 * @param total12 sum of x_i * y_i of both series of numbers
 * @param var1 variance of the first series of numbers
 * @param var2 variance of the second series of numbers
49
 * @param counter number of elements in both series
50
 * @return the Pearson correlation coefficient
51
 * */
52
53
54
double pearsonfunc(double mean1, double mean2, double total12, double var1,
                   double var2, int counter) {

55
  const double mean12 = total12 / (double)counter;
56
57
  const double correlation = (mean12 - mean1 * mean2) / sqrt(var1 * var2);
  return fabs(correlation);
58
59
}

60
61
62
63
64
65
/**
 * @brief Test to check that the pseodo-random numbers in SWIFT are random
 * enough for our purpose.
 *
 * The test initializes with the current time and than creates 20 ID numbers
 * it runs the test using these 20 ID numbers. Using these 20 ID numbers it
66
 * Checks 4 different things:
67
68
69
 * 1. The mean and variance are correct for random numbers generated by this
 *    ID number.
 * 2. The random numbers from this ID number do not cause correlation in time.
70
71
 *    Correlation is checked using the Pearson correlation coefficient which
 *    should be sufficiently close to zero.
72
73
74
 * 3. A small offset in ID number of 2, doesn't cause correlation between
 *    the two sets of random numbers (again with the Pearson correlation
 *    coefficient) and the mean and variance of this set is
75
 *    also correct.
76
 * 4. Different physical processes in random.h are also uncorrelated and
77
 *    produce the correct mean and variance as expected. Again the correlation
78
 *    is calculated using the Pearson correlation coefficient.
79
 *
80
 * More information about the Pearson correlation coefficient can be found in
81
 * the function pearsonfunc above this function.
82
 *
83
84
 * @param argc Unused
 * @param argv Unused
85
 * @return 0 if everything is fine, 1 if random numbers are not random enough.
86
 */
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
int main(int argc, char* argv[]) {

  /* Initialize CPU frequency, this also starts time. */
  unsigned long long cpufreq = 0;
  clocks_set_cpufreq(cpufreq);

/* Choke on FPEs */
#ifdef HAVE_FE_ENABLE_EXCEPT
  feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
#endif

  /* Get some randomness going */
  const int seed = time(NULL);
  message("Seed = %d", seed);
  srand(seed);

Loic Hausammann's avatar
Loic Hausammann committed
103
104
105
  /* Log the swift random seed */
  message("SWIFT random seed = %d", SWIFT_RANDOM_SEED_XOR);

106
  /* Time-step size */
107
  const int time_bin = 33;
108
109

  /* Try a few different values for the ID */
110
  for (int i = 0; i < 10; ++i) {
111
112
113

    const long long id = rand() * (1LL << 31) + rand();
    const integertime_t increment = (1LL << time_bin);
114
    const long long idoffset = id + 2;
115
116
117
118
119
120

    message("Testing id=%lld time_bin=%d", id, time_bin);

    double total = 0., total2 = 0.;
    int count = 0;

121
    /* Pearson correlation variables for different times */
122
123
    double sum_previous_current = 0.;
    double previous = 0.;
124
125
126
127
128

    /* Pearson correlation for two different IDs */
    double pearsonIDs = 0.;
    double totalID = 0.;
    double total2ID = 0.;
129

130
    /* Pearson correlation for different processes */
131
132
    double pearson_star_sf_1 = 0.;
    double pearson_star_sf_2 = 0.;
133
134
    double pearson_star_se = 0.;
    double pearson_star_bh = 0.;
135
136
    double pearson_sf_1_se = 0.;
    double pearson_sf_1_bh = 0.;
137
138
139
    double pearson_se_bh = 0.;

    /* Calculate the mean and <x^2> for these processes */
140
141
    double total_sf_1 = 0.;
    double total_sf_2 = 0.;
142
143
144
    double total_se = 0.;
    double total_bh = 0.;

145
146
    double total2_sf_1 = 0.;
    double total2_sf_2 = 0.;
147
148
149
    double total2_se = 0.;
    double total2_bh = 0.;

150
151
152
153
154
155
156
157
158
    /* Check that the numbers are uniform over the full-range of useful
     * time-steps */
    for (integertime_t ti_current = 0LL; ti_current < max_nr_timesteps;
         ti_current += increment) {

      ti_current += increment;

      const double r =
          random_unit_interval(id, ti_current, random_number_star_formation);
Pedro Gonnet's avatar
Pedro Gonnet committed
159
      if (r < 0.0 || r >= 1.0) {
160
        error("Generated random value %f is not in [0, 1).", r);
Pedro Gonnet's avatar
Pedro Gonnet committed
161
      }
162
163
164
165

      total += r;
      total2 += r * r;
      count++;
166

167
168
      /* Calculate for correlation between time.
       * For this we use the pearson correlation of time i and i-1 */
169
170
      sum_previous_current += r * previous;
      previous = r;
171

172
173
174
      /* Calculate if there is a correlation between different ids */
      const double r_2ndid = random_unit_interval(idoffset, ti_current,
                                                  random_number_star_formation);
Pedro Gonnet's avatar
Pedro Gonnet committed
175
      if (r_2ndid < 0.0 || r_2ndid >= 1.0) {
176
        error("Generated random value %f is not in [0, 1).", r_2ndid);
Pedro Gonnet's avatar
Pedro Gonnet committed
177
      }
178

179
180
181
182
      /* Pearson correlation for small different IDs */
      pearsonIDs += r * r_2ndid;
      totalID += r_2ndid;
      total2ID += r_2ndid * r_2ndid;
183
184
185

      /* Calculate random numbers for the different processes and check
       * that they are uncorrelated */
186

187
188
189
      const double r_sf_1 = random_unit_interval(
          id, ti_current, random_number_stellar_feedback_1);
      if (r_sf_1 < 0.0 || r_sf_1 >= 1.0) {
190
        error("Generated random value %f is not in [0, 1).", r_sf_1);
191
192
193
194
195
      }

      const double r_sf_2 = random_unit_interval(
          id, ti_current, random_number_stellar_feedback_2);
      if (r_sf_2 < 0.0 || r_sf_2 >= 1.0) {
196
        error("Generated random value %f is not in [0, 1).", r_sf_2);
Pedro Gonnet's avatar
Pedro Gonnet committed
197
      }
198

199
200
      const double r_se = random_unit_interval(
          id, ti_current, random_number_stellar_enrichment);
Pedro Gonnet's avatar
Pedro Gonnet committed
201
      if (r_se < 0.0 || r_se >= 1.0) {
202
        error("Generated random value %f is not in [0, 1).", r_se);
Pedro Gonnet's avatar
Pedro Gonnet committed
203
      }
204

205
      const double r_bh =
206
          random_unit_interval(id, ti_current, random_number_BH_feedback);
Pedro Gonnet's avatar
Pedro Gonnet committed
207
      if (r_bh < 0.0 || r_bh >= 1.0) {
208
        error("Generated random value %f is not in [0, 1).", r_bh);
Pedro Gonnet's avatar
Pedro Gonnet committed
209
      }
210
211

      /* Calculate the correlation between the different processes */
212
213
      total_sf_1 += r_sf_1;
      total_sf_2 += r_sf_2;
214
215
216
      total_se += r_se;
      total_bh += r_bh;

217
218
      total2_sf_1 += r_sf_1 * r_sf_1;
      total2_sf_2 += r_sf_2 * r_sf_2;
219
220
221
      total2_se += r_se * r_se;
      total2_bh += r_bh * r_bh;

222
223
      pearson_star_sf_1 += r * r_sf_1;
      pearson_star_sf_2 += r * r_sf_2;
224
225
      pearson_star_se += r * r_se;
      pearson_star_bh += r * r_bh;
226
227
      pearson_sf_1_se += r_sf_1 * r_se;
      pearson_sf_1_bh += r_sf_1 * r_bh;
228
      pearson_se_bh += r_se * r_bh;
229
230
231
232
233
    }

    const double mean = total / (double)count;
    const double var = total2 / (double)count - mean * mean;

234
    /* Pearson correlation calculation for different times */
235
236
237
238
    // const double mean_xy = sum_previous_current / ((double)count - 1.f);
    // const double correlation = (mean_xy - mean * mean) / var;
    const double correlation =
        pearsonfunc(mean, mean, sum_previous_current, var, var, count - 1);
Folkert Nobels's avatar
Folkert Nobels committed
239

240
    /* Mean for different IDs */
241
242
243
    const double meanID = totalID / (double)count;
    const double varID = total2ID / (double)count - meanID * meanID;

244
    /* Pearson correlation between different IDs*/
245
246
    const double correlationID =
        pearsonfunc(mean, meanID, pearsonIDs, var, varID, count);
247

248
    /* Mean and <x^2> for different processes */
249
250
    const double mean_sf_1 = total_sf_1 / (double)count;
    const double mean_sf_2 = total_sf_2 / (double)count;
251
252
    const double mean_se = total_se / (double)count;
    const double mean_bh = total_bh / (double)count;
253

254
255
    const double var_sf_1 = total2_sf_1 / (double)count - mean_sf_1 * mean_sf_1;
    const double var_sf_2 = total2_sf_2 / (double)count - mean_sf_2 * mean_sf_2;
256
257
258
259
    const double var_se = total2_se / (double)count - mean_se * mean_se;
    const double var_bh = total2_bh / (double)count - mean_bh * mean_bh;

    /* Correlation between different processes */
260
261
262
263
    const double corr_star_sf_1 =
        pearsonfunc(mean, mean_sf_1, pearson_star_sf_1, var, var_sf_1, count);
    const double corr_star_sf_2 =
        pearsonfunc(mean, mean_sf_2, pearson_star_sf_2, var, var_sf_2, count);
264
265
266
267
    const double corr_star_se =
        pearsonfunc(mean, mean_se, pearson_star_se, var, var_se, count);
    const double corr_star_bh =
        pearsonfunc(mean, mean_bh, pearson_star_bh, var, var_bh, count);
268
269
270
271
    const double corr_sf_1_se = pearsonfunc(mean_sf_1, mean_se, pearson_sf_1_se,
                                            var_sf_1, var_se, count);
    const double corr_sf_1_bh = pearsonfunc(mean_sf_1, mean_bh, pearson_sf_1_bh,
                                            var_sf_1, var_bh, count);
272
273
274
    const double corr_se_bh =
        pearsonfunc(mean_se, mean_bh, pearson_se_bh, var_se, var_bh, count);

275
276
    /* Verify that the mean and variance match the expected values for a uniform
     * distribution */
277
278
279
280
281

    /* Set the allowed standard deviation */
    const double std_check = 5.;

    /* The mean is expected to deviate a maximum of std_check * std / sqrt(N) */
Folkert Nobels's avatar
Folkert Nobels committed
282
    const double tolmean = std_check / sqrtf(12.f * count);
283

Folkert Nobels's avatar
Folkert Nobels committed
284
    /* the variance is expected to deviate a maximum of std_check * variance
285
     * * sqrt(2/(n-1)) */
Folkert Nobels's avatar
Folkert Nobels committed
286
287
    const double tolvar =
        std_check * sqrtf(2.f / (12.f * ((double)count - 1.f)));
288

Folkert Nobels's avatar
Folkert Nobels committed
289
    /* The correlation coefficient is expected to deviate sqrt(1-R^2)
290
     * / sqrt(n-2), in our case <R> = 0, so we get 1/sqrt(n-2) */
Folkert Nobels's avatar
Folkert Nobels committed
291
    const double tolcorr = std_check / sqrtf((double)count - 2.);
292

Folkert Nobels's avatar
Folkert Nobels committed
293
    if ((fabs(mean - 0.5) > tolmean) || (fabs(var - 1. / 12.) > tolvar) ||
294
        (fabs(correlation) > tolcorr) || (fabs(correlationID) > tolcorr) ||
Folkert Nobels's avatar
Folkert Nobels committed
295
        (fabs(meanID - 0.5) > tolmean) || (fabs(varID - 1. / 12.) > tolvar) ||
296
297
298
299
300
301
302
303
        (fabs(corr_star_sf_1) > tolcorr) || (fabs(corr_star_sf_2) > tolcorr) ||
        (fabs(corr_star_se) > tolcorr) || (fabs(corr_star_bh) > tolcorr) ||
        (fabs(corr_sf_1_se) > tolcorr) || (fabs(corr_sf_1_bh) > tolcorr) ||
        (fabs(corr_se_bh) > tolcorr) || (fabs(mean_sf_1 - 0.5) > tolmean) ||
        (fabs(mean_sf_2 - 0.5) > tolmean) || (fabs(mean_se - 0.5) > tolmean) ||
        (fabs(mean_bh - 0.5) > tolmean) ||
        (fabs(var_sf_1 - 1. / 12.) > tolvar) ||
        (fabs(var_sf_2 - 1. / 12.) > tolvar) ||
304
305
        (fabs(var_se - 1. / 12.) > tolvar) ||
        (fabs(var_bh - 1. / 12.) > tolvar)) {
306
      message("Test failed!");
307
      message("Global result:");
308
309
310
311
      message("Result:    count=%d mean=%f var=%f, correlation=%f", count, mean,
              var, correlation);
      message("Expected:  count=%d mean=%f var=%f, correlation=%f", count, 0.5f,
              1. / 12., 0.);
Folkert Nobels's avatar
Folkert Nobels committed
312
      message("Max difference:           mean=%f var=%f, correlation=%f",
313
              tolmean, tolvar, tolcorr);
Folkert Nobels's avatar
Folkert Nobels committed
314
315
      message("Difference:               mean=%f var=%f, correlation=%f",
              fabs(mean - 0.5f), fabs(var - 1. / 12.), fabs(correlation));
316
      message("ID part");
Folkert Nobels's avatar
Folkert Nobels committed
317
      message(
Pedro Gonnet's avatar
Pedro Gonnet committed
318
          "Result:    count=%d mean=%f var=%f"
319
320
          " correlation=%f",
          count, meanID, varID, correlationID);
Folkert Nobels's avatar
Folkert Nobels committed
321
      message(
Pedro Gonnet's avatar
Pedro Gonnet committed
322
          "Expected:  count=%d mean=%f var=%f"
323
324
          " correlation=%f",
          count, .5f, 1. / 12., 0.);
325
326
327
      message("Max difference:           mean=%f var=%f, correlation=%f",
              tolmean, tolvar, tolcorr);
      message("Difference:               mean=%f var=%f, correlation=%f",
Folkert Nobels's avatar
Folkert Nobels committed
328
              fabs(meanID - 0.5f), fabs(varID - 1. / 12.), fabs(correlation));
329
      message("Different physical processes:");
330
331
      message(
          "Means:    stars=%f stellar feedback=%f stellar "
332
          " enrichment=%f black holes=%f",
333
          mean, mean_sf_1, mean_se, mean_bh);
334
335
      message(
          "Expected: stars=%f stellar feedback=%f stellar "
336
          " enrichment=%f black holes=%f",
337
          .5f, .5f, .5f, .5f);
338
339
      message(
          "Max diff: stars=%f stellar feedback=%f stellar "
Folkert Nobels's avatar
Folkert Nobels committed
340
341
          " enrichment=%f black holes=%f",
          tolmean, tolmean, tolmean, tolmean);
342
343
      message(
          "Diff:     stars=%f stellar feedback=%f stellar "
Folkert Nobels's avatar
Folkert Nobels committed
344
          " enrichment=%f black holes=%f",
345
          fabs(mean - .5f), fabs(mean_sf_1 - .5f), fabs(mean_se - .5f),
Folkert Nobels's avatar
Folkert Nobels committed
346
          fabs(mean_bh - .5f));
347
      message(" ");
348
349
      message(
          "Var:      stars=%f stellar feedback=%f stellar "
350
          " enrichment=%f black holes=%f",
351
          var, var_sf_1, var_se, var_bh);
352
353
      message(
          "Expected: stars=%f stellar feedback=%f stellar "
354
          " enrichment=%f black holes=%f",
355
          1. / 12., 1. / 12., 1 / 12., 1. / 12.);
356
357
      message(
          "Max diff: stars=%f stellar feedback=%f stellar "
Folkert Nobels's avatar
Folkert Nobels committed
358
359
          " enrichment=%f black holes=%f",
          tolvar, tolvar, tolvar, tolvar);
360
361
      message(
          "Diff:     stars=%f stellar feedback=%f stellar "
Folkert Nobels's avatar
Folkert Nobels committed
362
          " enrichment=%f black holes=%f",
363
          fabs(var - 1. / 12.), fabs(var_sf_1 - 1. / 12.),
Folkert Nobels's avatar
Folkert Nobels committed
364
          fabs(var_se - 1. / 12.), fabs(var_bh - 1. / 12.));
365
      message(" ");
366
      message(
Pedro Gonnet's avatar
Pedro Gonnet committed
367
          "Correlation: stars-sf=%f stars-se=%f stars-bh=%f "
368
          "sf-se=%f sf-bh=%f se-bh=%f",
369
370
          corr_star_sf_1, corr_star_se, corr_star_bh, corr_sf_1_se,
          corr_sf_1_bh, corr_se_bh);
371
      message(
Pedro Gonnet's avatar
Pedro Gonnet committed
372
          "Expected:    stars-sf=%f stars-se=%f stars-bh=%f "
373
374
          "sf-se=%f sf-bh=%f se-bh=%f",
          0., 0., 0., 0., 0., 0.);
375
376
      message(
          "Max diff:    stars-sf=%f stars-se=%f stars-bh=%f "
Folkert Nobels's avatar
Folkert Nobels committed
377
378
          "sf-se=%f sf-bh=%f se-bh=%f",
          tolcorr, tolcorr, tolcorr, tolcorr, tolcorr, tolcorr);
379
      message(
380
          "Diff:        stars-sf1=%f stars-sf2=%f stars-se=%f stars-bh=%f "
Folkert Nobels's avatar
Folkert Nobels committed
381
          "sf-se=%f sf-bh=%f se-bh=%f",
382
383
384
          fabs(corr_star_sf_1), fabs(corr_star_sf_2), fabs(corr_star_se),
          fabs(corr_star_bh), fabs(corr_sf_1_se), fabs(corr_sf_1_bh),
          fabs(corr_se_bh));
385
386
387
388
389
390
      return 1;
    }
  }

  return 0;
}