testRandom.c 3.44 KB
Newer Older
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
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (C) 2019 Matthieu Schaller (schaller@strw.leidenuniv.nl)
 *
 * 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"

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);

44 45
  

46 47 48 49 50 51 52 53 54 55
  /* Time-step size */
  const int time_bin = 29;

  /* Try a few different values for the ID */
  for (int i = 0; i < 20; ++i) {

    const long long id = rand() * (1LL << 31) + rand();
    const integertime_t increment = (1LL << time_bin);

    message("Testing id=%lld time_bin=%d", id, time_bin);
56 57 58 59 60
    char buffer[32];
    snprintf(buffer, sizeof(char)*32, "fileII%i.txt", i);

    FILE *fp;
    fp = fopen(buffer,"w");
61 62 63 64

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

65 66 67 68 69 70
    /* Pearson correlation variables */
    double sum_previous_current = 0.;
    double previous = 0.;
    
    message("Max nr timesteps = %lld",max_nr_timesteps);

71 72 73 74 75 76 77 78 79 80 81 82 83
    /* 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);

      total += r;
      total2 += r * r;
      count++;
84 85 86 87 88 89
      const unsigned int test = 127LL*(ti_current - 1LL) + 124429LL;
      fprintf(fp, "%f   %lld    %lld\n", r, (test) % 1514917LL, ti_current );

      /* For the pearson correlation */
      sum_previous_current += r * previous;
      previous = r;
90 91
    }

92
    fclose(fp);
93 94 95
    const double mean = total / (double)count;
    const double var = total2 / (double)count - mean * mean;

96 97 98 99
    double mean_xy = sum_previous_current / ( (double)count -1.f);
    double correlation = (mean_xy-mean*mean)/var;
    message("Correlation = %f", correlation);

100 101
    /* Verify that the mean and variance match the expected values for a uniform
     * distribution */
102 103 104
    if ((fabs(mean - 0.5) / 0.5 > 2e-4) ||
        (fabs(var - 1. / 12.) / (1. / 12.) > 1e-3) ||
        (fabs(correlation) > 3e-4)) {
105
      message("Test failed!");
106 107
      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.);
108 109 110 111 112 113
      return 1;
    }
  }

  return 0;
}