commit ce1c495672966812f52cda6da31a55f1b6d1a547
parent 30c8c29f5ec3274e34fa99319a6c035c7a640fea
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Fri, 13 Jul 2018 12:52:35 +0200
Test the "compute cross section <bounds|avg>" functions
Diffstat:
| M | src/test_htmie_load.c | | | 166 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- |
1 file changed, 162 insertions(+), 4 deletions(-)
diff --git a/src/test_htmie_load.c b/src/test_htmie_load.c
@@ -19,8 +19,20 @@
#include "test_htmie_utils.h"
#include <rsys/math.h>
+
+#include <stdlib.h>
#include <string.h>
+
+#define NEAREST HTMIE_FILTER_NEAREST
+#define LINEAR HTMIE_FILTER_LINEAR
+
+static INLINE double
+rand_canonic(void)
+{
+ return rand() / (double)(RAND_MAX - 1);
+}
+
static void
test_fetch(struct htmie* htmie)
{
@@ -34,8 +46,7 @@ test_fetch(struct htmie* htmie)
double xsca;
double u;
-#define NEAREST HTMIE_FILTER_NEAREST
-#define LINEAR HTMIE_FILTER_LINEAR
+ CHK(htmie);
nwlens = htmie_get_wavelengths_count(htmie);
CHK(nwlens > 1);
@@ -81,9 +92,154 @@ test_fetch(struct htmie* htmie)
CHK(eq_eps(xabs, htmie_fetch_xsection_absorption(htmie, wlen, LINEAR), 1.e-6));
CHK(eq_eps(xsca, htmie_fetch_xsection_scattering(htmie, wlen, LINEAR), 1.e-6));
}
+}
+
+static void
+test_bounds(struct htmie* htmie)
+{
+ double bounds[2];
+ double band[2];
+ double min_val = DBL_MAX;
+ double max_val =-DBL_MAX;
+ const double* wlens;
+ const size_t ntests = 128;
+ size_t ilow;
+ size_t iupp;
+ size_t nwlens;
+ size_t i;
+ size_t itest;
+ CHK(htmie);
+
+ wlens = htmie_get_wavelengths(htmie);
+ nwlens = htmie_get_wavelengths_count(htmie);
+
+ CHK(nwlens > 1);
+
+ FOR_EACH(itest, 0, ntests) {
+ do {
+ ilow = (size_t)(rand_canonic() * (double)nwlens);
+ iupp = (size_t)(rand_canonic() * (double)nwlens);
+ } while(ilow == iupp);
+
+ if(ilow > iupp) SWAP(size_t, ilow, iupp);
+ band[0] = wlens[ilow];
+ band[1] = wlens[iupp];
+
+ #define TEST(Name, Filter) { \
+ htmie_compute_xsection_## Name ## _bounds(htmie, band, Filter, bounds); \
+ min_val = htmie_fetch_xsection_ ## Name(htmie, band[0], Filter); \
+ max_val = htmie_fetch_xsection_ ## Name(htmie, band[1], Filter); \
+ if(min_val > max_val) SWAP(double, min_val, max_val); \
+ FOR_EACH(i, ilow+1, iupp) { \
+ double tmp = htmie_fetch_xsection_ ## Name(htmie, wlens[i], Filter); \
+ min_val = MMIN(min_val, tmp); \
+ max_val = MMAX(max_val, tmp); \
+ } \
+ CHK(eq_eps(bounds[0], min_val, 1.e-6)); \
+ CHK(eq_eps(bounds[1], max_val, 1.e-6)); \
+ } (void)0
+
+ TEST(absorption, NEAREST);
+ TEST(scattering, NEAREST);
+ TEST(absorption, LINEAR);
+ TEST(scattering, LINEAR);
+
+ band[0] = wlens[ilow] + 0.25*(wlens[ilow+1] - wlens[ilow+0]);
+ band[1] = wlens[iupp] - 0.25*(wlens[iupp+0] - wlens[iupp-1]);
+ CHK(band[0] < band[1]);
+
+ TEST(absorption, NEAREST);
+ TEST(scattering, NEAREST);
+ TEST(absorption, LINEAR);
+ TEST(scattering, LINEAR);
+
+ #undef TEST
+
+ band[0] = band[1];
+
+ htmie_compute_xsection_absorption_bounds(htmie, band, NEAREST, bounds);
+ min_val = htmie_fetch_xsection_absorption(htmie, band[0], NEAREST);
+ max_val = htmie_fetch_xsection_absorption(htmie, band[1], NEAREST);
+ CHK(eq_eps(bounds[0], min_val, 1.e-6));
+ CHK(eq_eps(bounds[1], max_val, 1.e-6));
+
+ htmie_compute_xsection_absorption_bounds(htmie, band, LINEAR, bounds);
+ min_val = htmie_fetch_xsection_absorption(htmie, band[0], LINEAR);
+ max_val = htmie_fetch_xsection_absorption(htmie, band[1], LINEAR);
+ CHK(eq_eps(bounds[0], min_val, 1.e-6));
+ CHK(eq_eps(bounds[1], max_val, 1.e-6));
+
+ htmie_compute_xsection_scattering_bounds(htmie, band, NEAREST, bounds);
+ min_val = htmie_fetch_xsection_scattering(htmie, band[0], NEAREST);
+ max_val = htmie_fetch_xsection_scattering(htmie, band[1], NEAREST);
+ CHK(eq_eps(bounds[0], min_val, 1.e-6));
+ CHK(eq_eps(bounds[1], max_val, 1.e-6));
+
+ htmie_compute_xsection_scattering_bounds(htmie, band, LINEAR, bounds);
+ min_val = htmie_fetch_xsection_scattering(htmie, band[0], LINEAR);
+ max_val = htmie_fetch_xsection_scattering(htmie, band[1], LINEAR);
+ CHK(eq_eps(bounds[0], min_val, 1.e-6));
+ CHK(eq_eps(bounds[1], max_val, 1.e-6));
+ }
+}
-#undef NEAREST
-#undef LINEAR
+static void
+test_avg(struct htmie* htmie)
+{
+ double band[2];
+ double avg;
+ double ref;
+ const double* wlens;
+ const size_t ntests = 128;
+ size_t itest;
+ size_t nwlens;
+ size_t ilow;
+ size_t iupp;
+ size_t i;
+ CHK(htmie);
+
+ wlens = htmie_get_wavelengths(htmie);
+ nwlens = htmie_get_wavelengths_count(htmie);
+
+ CHK(nwlens > 1);
+
+ FOR_EACH(itest, 0, ntests) {
+ do {
+ ilow = (size_t)(rand_canonic() * (double)nwlens);
+ iupp = (size_t)(rand_canonic() * (double)nwlens);
+ } while(ilow == iupp);
+
+ if(ilow > iupp) SWAP(size_t, ilow, iupp);
+ band[0] = wlens[ilow];
+ band[1] = wlens[iupp];
+
+ #define TEST(Name, Filter) { \
+ ref = htmie_fetch_xsection_ ## Name(htmie, band[0], Filter); \
+ ref += htmie_fetch_xsection_ ## Name(htmie, band[1], Filter); \
+ FOR_EACH(i, ilow+1, iupp) { \
+ ref += htmie_fetch_xsection_ ## Name(htmie, wlens[i], Filter); \
+ } \
+ ref /= (band[1] - band[0]); \
+ avg = htmie_compute_xsection_## Name ## _average(htmie, band, Filter); \
+ CHK(eq_eps(avg, ref, 1.e-6)); \
+ } (void)0
+
+ TEST(absorption, NEAREST);
+ TEST(scattering, NEAREST);
+ TEST(absorption, LINEAR);
+ TEST(scattering, LINEAR);
+
+ band[0] = wlens[ilow] + 0.25*(wlens[ilow+1] - wlens[ilow+0]);
+ band[1] = wlens[iupp] - 0.25*(wlens[iupp+0] - wlens[iupp-1]);
+ CHK(band[0] < band[1]);
+
+ TEST(absorption, NEAREST);
+ TEST(scattering, NEAREST);
+ TEST(absorption, LINEAR);
+ TEST(scattering, LINEAR);
+
+ #undef TEST
+ }
}
int
@@ -171,6 +327,8 @@ main(int argc, char** argv)
CHK(fclose(fp) == 0);
test_fetch(htmie);
+ test_bounds(htmie);
+ test_avg(htmie);
CHK(htmie_ref_put(htmie) == RES_OK);