tutil.c: add residual and random number functions - granular - granular dynamics simulation
(HTM) git clone git://src.adamsgaard.dk/granular
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 0ced4ee84ae554d75646ebbaa76cc31c2397cfb3
(DIR) parent b6248f108d84c1068e84381d5a06a34522ebdc82
(HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Thu, 18 Mar 2021 08:55:40 +0100
util.c: add residual and random number functions
Diffstat:
M util.c | 26 ++++++++++++++++++++++++++
M util.h | 6 ++++++
2 files changed, 32 insertions(+), 0 deletions(-)
---
(DIR) diff --git a/util.c b/util.c
t@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <stdlib.h>
#include <math.h>
#include "granular.h"
t@@ -78,3 +79,28 @@ check_int_non_negative(const char name[], const int value, int *status)
*status = 1;
}
}
+
+double
+residual(const double new_val, const double old_val)
+{
+ return (new_val - old_val) / (old_val + 1e-16);
+}
+
+double
+random(void)
+{
+ return (double)rand() / RAND_MAX;
+}
+
+double
+random_value_uniform(double min, double max)
+{
+ return random() * (max - min) + min;
+}
+
+double
+random_value_powerlaw(double power, double min, double max)
+{
+ return pow((pow(max, power + 1.0) - pow(min, power + 1.0)) * random()
+ + pow(min, power + 1.0), 1.0 / (power + 1.0));
+}
(DIR) diff --git a/util.h b/util.h
t@@ -7,4 +7,10 @@ void check_float_non_negative(const char name[], const double value, int *status
void check_float_positive(const char name[], const double value, int *status);
void check_bool(const char name[], const double value, int *status);
void check_int_non_negative(const char name[], const int value, int *status);
+
+double residual(const double new_val, const double old_val);
+
+double random_value_uniform(double min, double max);
+double random_value_powerlaw(double power, double min, double max);
+
#endif