test_htgop_sample.c (3397B)
1 /* Copyright (C) 2018-2021, 2023 |Méso|Star> (contact@meso-star.com) 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include "htgop.h" 17 #include "test_htgop_utils.h" 18 19 #include <rsys/math.h> 20 #include <string.h> 21 22 #define N 100000 23 24 static void 25 check_sample_quadrature 26 (struct htgop* htgop, 27 res_T (*get_nspecints)(const struct htgop*, size_t*), 28 res_T (*get_specint) 29 (const struct htgop*, const size_t, struct htgop_spectral_interval*)) 30 { 31 struct htgop_spectral_interval specint; 32 size_t nspecints; 33 size_t iquadpt; 34 size_t i; 35 36 CHK(htgop && get_specint); 37 38 CHK(get_nspecints(htgop, &nspecints) == RES_OK); 39 CHK(nspecints); 40 41 CHK(get_specint(htgop, 0, &specint) == RES_OK); 42 CHK(htgop_spectral_interval_sample_quadrature(NULL, 0, &iquadpt) == RES_BAD_ARG); 43 CHK(htgop_spectral_interval_sample_quadrature(&specint, 1, &iquadpt) == RES_BAD_ARG); 44 CHK(htgop_spectral_interval_sample_quadrature(&specint, 1, NULL) == RES_BAD_ARG); 45 CHK(htgop_spectral_interval_sample_quadrature(&specint, 1, NULL) == RES_BAD_ARG); 46 47 FOR_EACH(i, 0, 10) { 48 int* hist; 49 size_t ispecint; 50 size_t j; 51 52 ispecint = (size_t)(rand_canonic() * (double)nspecints); 53 CHK(get_specint(htgop, ispecint, &specint) == RES_OK); 54 55 CHK(hist = mem_calloc(specint.quadrature_length, sizeof(*hist))); 56 FOR_EACH(j, 0, N) { 57 const double r = rand_canonic(); 58 CHK(htgop_spectral_interval_sample_quadrature(&specint, r, &iquadpt) == RES_OK); 59 CHK(iquadpt < specint.quadrature_length); 60 CHK(specint.quadrature_cdf[iquadpt] > r); 61 CHK(!iquadpt || specint.quadrature_cdf[iquadpt-1] <= r); 62 63 hist[iquadpt] += 1; 64 } 65 66 FOR_EACH(iquadpt, 0, specint.quadrature_length) { 67 if(!eq_eps(specint.quadrature_pdf[iquadpt], 0, 1.e-4)) 68 CHK(hist[iquadpt] > 0); 69 } 70 71 mem_rm(hist); 72 } 73 } 74 75 int 76 main(int argc, char** argv) 77 { 78 struct mem_allocator allocator; 79 struct htgop* htgop; 80 const double* wnums; 81 size_t nspecints; 82 83 if(argc < 2) { 84 fprintf(stderr, "Usage: %s FILENAME\n", argv[0]); 85 return 1; 86 } 87 88 CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); 89 90 CHK(htgop_create(NULL, &allocator, 1, &htgop) == RES_OK); 91 CHK(htgop_load(htgop, argv[1]) == RES_OK); 92 CHK(htgop_get_sw_spectral_intervals_count(htgop, &nspecints) == RES_OK); 93 CHK(nspecints > 0); 94 95 CHK(htgop_get_sw_spectral_intervals_wave_numbers(htgop, &wnums) == RES_OK); 96 97 check_sample_quadrature(htgop, htgop_get_sw_spectral_intervals_count, 98 htgop_get_sw_spectral_interval); 99 check_sample_quadrature(htgop, htgop_get_lw_spectral_intervals_count, 100 htgop_get_lw_spectral_interval); 101 102 CHK(htgop_ref_put(htgop) == RES_OK); 103 check_memory_allocator(&allocator); 104 mem_shutdown_proxy_allocator(&allocator); 105 CHK(mem_allocated_size() == 0); 106 return 0; 107 }