testRandom.c 7.06 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 34 35
double pearsonfunc(double mean1, double mean2, double total12, double var1, double var2, int counter) {
  
  const double mean12 = total12 / (double)counter;
  const double correlation = (mean12 - mean1 * mean2)/ pow(var1 * var2, .5f);
  return correlation; 
}

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
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);

  /* 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);
60
    const long long idoffset = id + 2;
61 62 63 64 65 66

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

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

67
    /* Pearson correlation variables for different times */
68 69
    double sum_previous_current = 0.;
    double previous = 0.;
70 71 72 73 74

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

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    /* Pearson correlation for different processes */
    double pearson_star_sf = 0.;
    double pearson_star_se = 0.;
    double pearson_star_bh = 0.;
    double pearson_sf_se = 0.;
    double pearson_sf_bh = 0.;
    double pearson_se_bh = 0.;

    /* Calculate the mean and <x^2> for these processes */
    double total_sf = 0.;
    double total_se = 0.;
    double total_bh = 0.;

    double total2_sf = 0.;
    double total2_se = 0.;
    double total2_bh = 0.;

93 94 95 96 97 98 99 100 101 102 103 104 105
    /* 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++;
106

107 108
      /* Calculate for correlation between time.
       * For this we use the pearson correlation of time i and i-1 */
109 110
      sum_previous_current += r * previous;
      previous = r;
111

112 113 114 115
      /* Calculate if there is a correlation between different ids */
      const double r_2ndid = random_unit_interval(idoffset, ti_current,
                                                  random_number_star_formation);

116 117 118 119
      /* Pearson correlation for small different IDs */
      pearsonIDs += r * r_2ndid;
      totalID += r_2ndid;
      total2ID += r_2ndid * r_2ndid;
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

      /* Calculate random numbers for the different processes and check
       * that they are uncorrelated */
      
      const double r_sf = 
          random_unit_interval(id, ti_current, random_number_stellar_feedback);

      const double r_se = 
          random_unit_interval(id, ti_current, random_number_stellar_enrichment);

      const double r_bh = 
          random_unit_interval(id, ti_current, random_number_BH_feedback);

      /* Calculate the correlation between the different processes */
      total_sf += r_sf;
      total_se += r_se;
      total_bh += r_bh;

      total2_sf += r_sf * r_sf;
      total2_se += r_se * r_se;
      total2_bh += r_bh * r_bh;

      pearson_star_sf += r * r_sf;
      pearson_star_se += r * r_se;
      pearson_star_bh += r * r_bh;
      pearson_sf_se += r_sf * r_se;
      pearson_sf_bh += r_sf * r_bh;
      pearson_se_bh += r_se * r_bh;
148 149 150 151 152
    }

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

153
    /* Pearson correlation calculation for different times */
Folkert Nobels's avatar
Folkert Nobels committed
154 155 156
    const double mean_xy = sum_previous_current / ((double)count - 1.f);
    const double correlation = (mean_xy - mean * mean) / var;

157
    /* Mean for different IDs */
158 159 160
    const double meanID = totalID / (double)count;
    const double varID = total2ID / (double)count - meanID * meanID;

161
    /* Pearson correlation between different IDs*/
162
    const double meanID_xy = pearsonIDs / (double)count;
Folkert Nobels's avatar
Folkert Nobels committed
163 164
    const double correlationID =
        (meanID_xy - mean * meanID) / pow(var * varID, .5f);
165

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    /* Mean and <x^2> for different processes */
    const double mean_sf = total_sf / (double)count;
    const double mean_se = total_se / (double)count;
    const double mean_bh = total_bh / (double)count;
    
    const double var_sf = total2_sf / (double)count - mean_sf * mean_sf;
    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 */
    const double corr_star_sf = pearsonfunc(mean,mean_sf,pearson_star_sf, var, var_sf, count);
    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);
    const double corr_sf_se = pearsonfunc(mean_sf,mean_se,pearson_sf_se, var_sf, var_se, count);
    const double corr_sf_bh = pearsonfunc(mean_sf,mean_bh,pearson_sf_bh, var_sf, var_bh, count);
    const double corr_se_bh = pearsonfunc(mean_se,mean_bh,pearson_se_bh, var_se, var_bh, count);
    message("%e %e %e %e %e %e",corr_star_sf, corr_star_se, corr_star_bh, corr_sf_se, corr_sf_bh, corr_se_bh);
    
184 185
    /* Verify that the mean and variance match the expected values for a uniform
     * distribution */
186 187
    if ((fabs(mean - 0.5) / 0.5 > 2e-4) ||
        (fabs(var - 1. / 12.) / (1. / 12.) > 1e-3) ||
Folkert Nobels's avatar
Folkert Nobels committed
188
        (fabs(correlation) > 3e-4) || (fabs(correlationID) > 3e-4)) {
189
      message("Test failed!");
Folkert Nobels's avatar
Folkert Nobels committed
190 191 192 193 194 195 196 197
      message(
          "Result:    count=%d mean=%f var=%f, correlation=%f ID "
          "correlation=%f",
          count, mean, var, correlation, correlationID);
      message(
          "Expected:  count=%d mean=%f var=%f, correlation=%f ID "
          "correlation=%f",
          count, 0.5f, 1. / 12., 0., 0.);
198 199 200 201 202 203
      return 1;
    }
  }

  return 0;
}