Commit 8874d70f authored by Folkert Nobels's avatar Folkert Nobels
Browse files

Format random.h and testRandom.c

parent e4779020
...@@ -30,14 +30,14 @@ ...@@ -30,14 +30,14 @@
* @brief The categories of random number generated. * @brief The categories of random number generated.
* *
* The values of the fields are carefully chose numbers * The values of the fields are carefully chose numbers
* These are values adviced by NR to use on page * These are values adviced by NR to use on page
* 348 in the first table. We used selected 4 numbers * 348 in the first table. We used selected 4 numbers
* and know that they produce no correlation at all * and know that they produce no correlation at all
* for the 4 different processes. * for the 4 different processes.
* Only change when you know what you are doing, changing * Only change when you know what you are doing, changing
* the numbers to bad values will break the random number * the numbers to bad values will break the random number
* generator. * generator.
* In case new numbers need to be added other possible * In case new numbers need to be added other possible
* numbers could be: * numbers could be:
* 4162943475, 3874257210, 2654432763 * 4162943475, 3874257210, 2654432763
*/ */
...@@ -96,7 +96,8 @@ INLINE static double random_unit_interval(const long long int id, ...@@ -96,7 +96,8 @@ INLINE static double random_unit_interval(const long long int id,
number ^= number >> 35; number ^= number >> 35;
number ^= number << 4; number ^= number << 4;
/* Nonlinear congruential generator */ /* Nonlinear congruential generator */
const unsigned long long idpart = 3457LL * id + 593LL * id * ti_current + 5417LL * id * id; const unsigned long long idpart =
3457LL * id + 593LL * id * ti_current + 5417LL * id * id;
unsigned int seed = unsigned int seed =
(937LL * number + 5171LL * number * number + idpart + 1109LL) % (937LL * number + 5171LL * number * number + idpart + 1109LL) %
9996361LL % seed_range; 9996361LL % seed_range;
......
...@@ -26,19 +26,19 @@ ...@@ -26,19 +26,19 @@
/* Local headers. */ /* Local headers. */
#include "swift.h" #include "swift.h"
/* /*
* @brief Compute the Pearson correlation coefficient for two sets of numbers * @brief Compute the Pearson correlation coefficient for two sets of numbers
* *
* The pearson correlation coefficient between two sets of numbers can be * The pearson correlation coefficient between two sets of numbers can be
* calculated as: * calculated as:
* *
* <x*y> - <x>*<y> * <x*y> - <x>*<y>
* r_xy = ---------------------- * r_xy = ----------------------
* (var(x) * var(y))^.5 * (var(x) * var(y))^.5
* *
* In the case that both sets are purely uncorrelated the value of the * 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 * 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 * there is positive correlation r_xy > 0 and in the case of negative
* correlation, the function has r_xy < 0. * correlation, the function has r_xy < 0.
* *
* @param mean1 average of first series of numbers * @param mean1 average of first series of numbers
...@@ -49,11 +49,12 @@ ...@@ -49,11 +49,12 @@
* @param number of elements in both series * @param number of elements in both series
* @return the Pearson correlation coefficient * @return the Pearson correlation coefficient
* */ * */
double pearsonfunc(double mean1, double mean2, double total12, double var1, double var2, int counter) { double pearsonfunc(double mean1, double mean2, double total12, double var1,
double var2, int counter) {
const double mean12 = total12 / (double)counter; const double mean12 = total12 / (double)counter;
const double correlation = (mean12 - mean1 * mean2)/ sqrt(var1 * var2); const double correlation = (mean12 - mean1 * mean2) / sqrt(var1 * var2);
return fabs(correlation); return fabs(correlation);
} }
/** /**
...@@ -62,21 +63,21 @@ double pearsonfunc(double mean1, double mean2, double total12, double var1, doub ...@@ -62,21 +63,21 @@ double pearsonfunc(double mean1, double mean2, double total12, double var1, doub
* *
* The test initializes with the current time and than creates 20 ID numbers * 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 * it runs the test using these 20 ID numbers. Using these 20 ID numbers it
* Checks 4 different things: * Checks 4 different things:
* 1. The mean and variance are correct for random numbers generated by this * 1. The mean and variance are correct for random numbers generated by this
* ID number. * ID number.
* 2. The random numbers from this ID number do not cause correlation in time. * 2. The random numbers from this ID number do not cause correlation in time.
* Correlation is checked using the Pearson correlation coefficient which * Correlation is checked using the Pearson correlation coefficient which
* should be sufficiently close to zero. * should be sufficiently close to zero.
* 3. A small offset in ID number of 2, doesn't cause correlation between * 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 * the two sets of random numbers (again with the Pearson correlation
* coefficient) and the mean and variance of this set is * coefficient) and the mean and variance of this set is
* also correct. * also correct.
* 4. Different physical processes in random.h are also uncorrelated and * 4. Different physical processes in random.h are also uncorrelated and
* produce the correct mean and variance as expected. Again the correlation * produce the correct mean and variance as expected. Again the correlation
* is calculated using the Pearson correlation coefficient. * is calculated using the Pearson correlation coefficient.
* *
* More information about the Pearson correlation coefficient can be found in * More information about the Pearson correlation coefficient can be found in
* the function pearsonfunc above this function. * the function pearsonfunc above this function.
* *
* @param none * @param none
...@@ -169,14 +170,14 @@ int main(int argc, char* argv[]) { ...@@ -169,14 +170,14 @@ int main(int argc, char* argv[]) {
/* Calculate random numbers for the different processes and check /* Calculate random numbers for the different processes and check
* that they are uncorrelated */ * that they are uncorrelated */
const double r_sf = const double r_sf =
random_unit_interval(id, ti_current, random_number_stellar_feedback); random_unit_interval(id, ti_current, random_number_stellar_feedback);
const double r_se = const double r_se = random_unit_interval(
random_unit_interval(id, ti_current, random_number_stellar_enrichment); id, ti_current, random_number_stellar_enrichment);
const double r_bh = const double r_bh =
random_unit_interval(id, ti_current, random_number_BH_feedback); random_unit_interval(id, ti_current, random_number_BH_feedback);
/* Calculate the correlation between the different processes */ /* Calculate the correlation between the different processes */
...@@ -200,34 +201,42 @@ int main(int argc, char* argv[]) { ...@@ -200,34 +201,42 @@ int main(int argc, char* argv[]) {
const double var = total2 / (double)count - mean * mean; const double var = total2 / (double)count - mean * mean;
/* Pearson correlation calculation for different times */ /* Pearson correlation calculation for different times */
//const double mean_xy = sum_previous_current / ((double)count - 1.f); // const double mean_xy = sum_previous_current / ((double)count - 1.f);
//const double correlation = (mean_xy - mean * mean) / var; // const double correlation = (mean_xy - mean * mean) / var;
const double correlation = pearsonfunc(mean,mean, sum_previous_current, var, var, count-1); const double correlation =
pearsonfunc(mean, mean, sum_previous_current, var, var, count - 1);
/* Mean for different IDs */ /* Mean for different IDs */
const double meanID = totalID / (double)count; const double meanID = totalID / (double)count;
const double varID = total2ID / (double)count - meanID * meanID; const double varID = total2ID / (double)count - meanID * meanID;
/* Pearson correlation between different IDs*/ /* Pearson correlation between different IDs*/
const double correlationID = pearsonfunc(mean, meanID, pearsonIDs, var, varID, count); const double correlationID =
pearsonfunc(mean, meanID, pearsonIDs, var, varID, count);
/* Mean and <x^2> for different processes */ /* Mean and <x^2> for different processes */
const double mean_sf = total_sf / (double)count; const double mean_sf = total_sf / (double)count;
const double mean_se = total_se / (double)count; const double mean_se = total_se / (double)count;
const double mean_bh = total_bh / (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_sf = total2_sf / (double)count - mean_sf * mean_sf;
const double var_se = total2_se / (double)count - mean_se * mean_se; const double var_se = total2_se / (double)count - mean_se * mean_se;
const double var_bh = total2_bh / (double)count - mean_bh * mean_bh; const double var_bh = total2_bh / (double)count - mean_bh * mean_bh;
/* Correlation between different processes */ /* Correlation between different processes */
const double corr_star_sf = pearsonfunc(mean,mean_sf,pearson_star_sf, var, var_sf, count); const double corr_star_sf =
const double corr_star_se = pearsonfunc(mean,mean_se,pearson_star_se, var, var_se, count); pearsonfunc(mean, mean_sf, pearson_star_sf, var, var_sf, count);
const double corr_star_bh = pearsonfunc(mean,mean_bh,pearson_star_bh, var, var_bh, count); const double corr_star_se =
const double corr_sf_se = pearsonfunc(mean_sf,mean_se,pearson_sf_se, var_sf, var_se, count); pearsonfunc(mean, mean_se, pearson_star_se, var, var_se, count);
const double corr_sf_bh = pearsonfunc(mean_sf,mean_bh,pearson_sf_bh, var_sf, var_bh, count); const double corr_star_bh =
const double corr_se_bh = pearsonfunc(mean_se,mean_bh,pearson_se_bh, var_se, var_bh, count); 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);
/* Verify that the mean and variance match the expected values for a uniform /* Verify that the mean and variance match the expected values for a uniform
* distribution */ * distribution */
const double tolmean = 2e-4; const double tolmean = 2e-4;
...@@ -238,46 +247,57 @@ int main(int argc, char* argv[]) { ...@@ -238,46 +247,57 @@ int main(int argc, char* argv[]) {
(fabs(var - 1. / 12.) / (1. / 12.) > tolvar) || (fabs(var - 1. / 12.) / (1. / 12.) > tolvar) ||
(correlation > tolcorr) || (correlationID > tolcorr) || (correlation > tolcorr) || (correlationID > tolcorr) ||
(fabs(meanID - 0.5) / 0.5 > tolmean) || (fabs(meanID - 0.5) / 0.5 > tolmean) ||
(fabs(varID - 1. / 12.) / (1. / 12.) > tolvar) || (fabs(varID - 1. / 12.) / (1. / 12.) > tolvar) ||
(corr_star_sf > tolcorr) || (corr_star_se > tolcorr) || (corr_star_sf > tolcorr) || (corr_star_se > tolcorr) ||
(corr_star_bh > tolcorr) || (corr_sf_se > tolcorr) || (corr_star_bh > tolcorr) || (corr_sf_se > tolcorr) ||
(corr_sf_bh > tolcorr) || (corr_se_bh > tolcorr) || (corr_sf_bh > tolcorr) || (corr_se_bh > tolcorr) ||
(fabs(mean_sf - 0.5) / 0.5 > tolmean) || (fabs(mean_sf - 0.5) / 0.5 > tolmean) ||
(fabs(mean_se - 0.5) / 0.5 > tolmean) || (fabs(mean_se - 0.5) / 0.5 > tolmean) ||
(fabs(mean_bh - 0.5) / 0.5 > tolmean) || (fabs(mean_bh - 0.5) / 0.5 > tolmean) ||
(fabs(var_sf - 1. / 12.) / (1. / 12.) > tolvar) || (fabs(var_sf - 1. / 12.) / (1. / 12.) > tolvar) ||
(fabs(var_se - 1. / 12.) / (1. / 12.) > tolvar) || (fabs(var_se - 1. / 12.) / (1. / 12.) > tolvar) ||
(fabs(var_bh - 1. / 12.) / (1. / 12.) > tolvar)) { (fabs(var_bh - 1. / 12.) / (1. / 12.) > tolvar)) {
message("Test failed!"); message("Test failed!");
message("Global result:"); message("Global result:");
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.);
message("ID part");
message( message(
"Result: count=%d mean=%f var=%f, correlation=%f", "Result: count%d mean=%f var=%f"
count, mean, var, correlation); " correlation=%f",
count, meanID, varID, correlationID);
message( message(
"Expected: count=%d mean=%f var=%f, correlation=%f", "Expected: count%d mean=%f var=%f"
count, 0.5f, 1. / 12., 0.); " correlation=%f",
message("ID part"); count, .5f, 1. / 12., 0.);
message("Result: count%d mean=%f var=%f"
" correlation=%f", count, meanID, varID, correlationID);
message("Expected: count%d mean=%f var=%f"
" correlation=%f", count, .5f, 1. / 12., 0.);
message("Different physical processes:"); message("Different physical processes:");
message("Means: stars=%f stellar feedback=%f stellar " message(
" enrichement=%f black holes=%f", mean, mean_sf, mean_se, "Means: stars=%f stellar feedback=%f stellar "
mean_bh); " enrichement=%f black holes=%f",
message("Expected: stars=%f stellar feedback=%f stellar " mean, mean_sf, mean_se, mean_bh);
" enrichement=%f black holes=%f", .5f, .5f, .5f, .5f); message(
message("Var: stars=%f stellar feedback=%f stellar " "Expected: stars=%f stellar feedback=%f stellar "
" enrichement=%f black holes=%f", var, var_sf, var_se, " enrichement=%f black holes=%f",
var_bh); .5f, .5f, .5f, .5f);
message("Expected: stars=%f stellar feedback=%f stellar " message(
" enrichement=%f black holes=%f", 1./12., 1./12., 1/12., "Var: stars=%f stellar feedback=%f stellar "
1./12.); " enrichement=%f black holes=%f",
message("Correlation: stars-sf=%f stars-se=%f stars-bh=%f" var, var_sf, var_se, var_bh);
"sf-se=%f sf-bh=%f se-bh=%f", corr_star_sf, corr_star_se, message(
corr_star_bh, corr_sf_se, corr_sf_bh, corr_se_bh); "Expected: stars=%f stellar feedback=%f stellar "
message("Expected: stars-sf=%f stars-se=%f stars-bh=%f" " enrichement=%f black holes=%f",
"sf-se=%f sf-bh=%f se-bh=%f", 0., 0., 0., 0., 0., 0.); 1. / 12., 1. / 12., 1 / 12., 1. / 12.);
message(
"Correlation: stars-sf=%f stars-se=%f stars-bh=%f"
"sf-se=%f sf-bh=%f se-bh=%f",
corr_star_sf, corr_star_se, corr_star_bh, corr_sf_se, corr_sf_bh,
corr_se_bh);
message(
"Expected: stars-sf=%f stars-se=%f stars-bh=%f"
"sf-se=%f sf-bh=%f se-bh=%f",
0., 0., 0., 0., 0., 0.);
return 1; return 1;
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment