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

Format random.h and testRandom.c

parent e4779020
......@@ -96,7 +96,8 @@ INLINE static double random_unit_interval(const long long int id,
number ^= number >> 35;
number ^= number << 4;
/* 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 =
(937LL * number + 5171LL * number * number + idpart + 1109LL) %
9996361LL % seed_range;
......
......@@ -49,10 +49,11 @@
* @param number of elements in both series
* @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 correlation = (mean12 - mean1 * mean2)/ sqrt(var1 * var2);
const double correlation = (mean12 - mean1 * mean2) / sqrt(var1 * var2);
return fabs(correlation);
}
......@@ -173,8 +174,8 @@ int main(int argc, char* argv[]) {
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_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);
......@@ -200,16 +201,18 @@ int main(int argc, char* argv[]) {
const double var = total2 / (double)count - mean * mean;
/* Pearson correlation calculation for different times */
//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);
// 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);
/* Mean for different IDs */
const double meanID = totalID / (double)count;
const double varID = total2ID / (double)count - meanID * meanID;
/* 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 */
const double mean_sf = total_sf / (double)count;
......@@ -221,12 +224,18 @@ int main(int argc, char* argv[]) {
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);
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);
/* Verify that the mean and variance match the expected values for a uniform
* distribution */
......@@ -250,34 +259,45 @@ int main(int argc, char* argv[]) {
(fabs(var_bh - 1. / 12.) / (1. / 12.) > tolvar)) {
message("Test failed!");
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(
"Result: count=%d mean=%f var=%f, correlation=%f",
count, mean, var, correlation);
"Result: count%d mean=%f var=%f"
" correlation=%f",
count, meanID, varID, correlationID);
message(
"Expected: count=%d mean=%f var=%f, correlation=%f",
count, 0.5f, 1. / 12., 0.);
message("ID part");
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.);
"Expected: count%d mean=%f var=%f"
" correlation=%f",
count, .5f, 1. / 12., 0.);
message("Different physical processes:");
message("Means: stars=%f stellar feedback=%f stellar "
" enrichement=%f black holes=%f", mean, mean_sf, mean_se,
mean_bh);
message("Expected: stars=%f stellar feedback=%f stellar "
" enrichement=%f black holes=%f", .5f, .5f, .5f, .5f);
message("Var: stars=%f stellar feedback=%f stellar "
" enrichement=%f black holes=%f", var, var_sf, var_se,
var_bh);
message("Expected: stars=%f stellar feedback=%f stellar "
" enrichement=%f black holes=%f", 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.);
message(
"Means: stars=%f stellar feedback=%f stellar "
" enrichement=%f black holes=%f",
mean, mean_sf, mean_se, mean_bh);
message(
"Expected: stars=%f stellar feedback=%f stellar "
" enrichement=%f black holes=%f",
.5f, .5f, .5f, .5f);
message(
"Var: stars=%f stellar feedback=%f stellar "
" enrichement=%f black holes=%f",
var, var_sf, var_se, var_bh);
message(
"Expected: stars=%f stellar feedback=%f stellar "
" enrichement=%f black holes=%f",
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;
}
}
......
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