test_htgop_get_radiative_properties_bounds.h (7647B)
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 "htgop_c.h" 18 19 #if !defined(DATA) || !defined(DOMAIN) 20 #error "Missing the <DATA|DOMAIN> macro." 21 #endif 22 23 /* Helper macros */ 24 #define GET_NSPECINTS \ 25 CONCAT(CONCAT(htgop_get_, DOMAIN), _spectral_intervals_count) 26 #define GET_SPECINT \ 27 CONCAT(CONCAT(htgop_get_, DOMAIN), _spectral_interval) 28 29 #define LAY_SPECINT \ 30 CONCAT(CONCAT(htgop_layer_, DOMAIN),_spectral_interval) 31 #define LAY_SPECINT_TAB \ 32 CONCAT(CONCAT(htgop_layer_, DOMAIN),_spectral_interval_tab) 33 #define GET_LAY_SPECINT \ 34 CONCAT(CONCAT(htgop_layer_get_, DOMAIN), _spectral_interval) 35 #define GET_LAY_SPECINT_TAB \ 36 CONCAT(CONCAT(htgop_layer_, DOMAIN), _spectral_interval_get_tab) 37 38 #define LAY_SPECINT_QUADS_GET_BOUNDS \ 39 CONCAT(CONCAT(CONCAT(CONCAT( \ 40 htgop_layer_,DOMAIN),_spectral_interval_quadpoints_get_),DATA),_bounds) 41 #define LAY_SPECINT_TAB_GET_BOUNDS \ 42 CONCAT(CONCAT(CONCAT(CONCAT( \ 43 htgop_layer_,DOMAIN),_spectral_interval_tab_get_),DATA),_bounds) 44 45 #ifndef GET_K 46 #define GET_K(Tab, Id) ((Tab)->CONCAT(DATA,_tab)[Id]) 47 #endif 48 49 static void CONCAT(CONCAT(CONCAT(CONCAT( 50 check_layer_,DOMAIN),_),DATA),_bounds)(struct htgop* htgop) 51 { 52 struct htgop_layer lay; 53 struct LAY_SPECINT band; 54 struct LAY_SPECINT_TAB tab; 55 double x_h2o_range[2]; 56 double bounds[2]; 57 size_t quad_range[2]; 58 size_t ilay, iband, iquad, itab; 59 size_t nlays, nbands; 60 61 CHK(GET_NSPECINTS(htgop, &nbands) == RES_OK); 62 CHK(htgop_get_layers_count(htgop, &nlays) == RES_OK); 63 64 CHK(nlays && nbands); 65 66 CHK(htgop_get_layer(htgop, 0, &lay) == RES_OK); 67 CHK(GET_LAY_SPECINT(&lay, 0, &band) == RES_OK); 68 CHK(band.quadrature_length); 69 CHK(GET_LAY_SPECINT_TAB(&band, 0, &tab) == RES_OK); 70 71 quad_range[0] = 0; 72 quad_range[1] = 0; 73 x_h2o_range[0] = 0; 74 x_h2o_range[1] = 0; 75 CHK(LAY_SPECINT_QUADS_GET_BOUNDS 76 (NULL, quad_range, x_h2o_range, bounds) == RES_BAD_ARG); 77 CHK(LAY_SPECINT_QUADS_GET_BOUNDS 78 (&band, NULL, x_h2o_range, bounds) == RES_BAD_ARG); 79 CHK(LAY_SPECINT_QUADS_GET_BOUNDS 80 (&band, quad_range, NULL, bounds) == RES_BAD_ARG); 81 CHK(LAY_SPECINT_QUADS_GET_BOUNDS 82 (&band, quad_range, x_h2o_range, NULL) == RES_BAD_ARG); 83 x_h2o_range[0] = 1; 84 x_h2o_range[1] = 0; 85 CHK(LAY_SPECINT_QUADS_GET_BOUNDS 86 (&band, quad_range, x_h2o_range, bounds) == RES_BAD_ARG); 87 x_h2o_range[0] = 0; 88 x_h2o_range[1] = 1; 89 quad_range[0] = 0; 90 quad_range[1] = band.quadrature_length; 91 CHK(LAY_SPECINT_QUADS_GET_BOUNDS 92 (&band, quad_range, x_h2o_range, bounds) == RES_BAD_ARG); 93 94 x_h2o_range[0] = 0; 95 x_h2o_range[1] = 0; 96 CHK(LAY_SPECINT_TAB_GET_BOUNDS(NULL, x_h2o_range, bounds) == RES_BAD_ARG); 97 CHK(LAY_SPECINT_TAB_GET_BOUNDS(&tab, NULL, bounds) == RES_BAD_ARG); 98 CHK(LAY_SPECINT_TAB_GET_BOUNDS(&tab, x_h2o_range, NULL) == RES_BAD_ARG); 99 x_h2o_range[0] = 1; 100 x_h2o_range[1] = 0; 101 CHK(LAY_SPECINT_TAB_GET_BOUNDS(&tab, x_h2o_range, bounds) == RES_BAD_ARG); 102 103 FOR_EACH(ilay, 0, nlays) { 104 CHK(htgop_get_layer(htgop, ilay, &lay) == RES_OK); 105 106 FOR_EACH(iband, 0, nbands) { 107 double quad_bounds2[2] = {DBL_MAX, -DBL_MAX}; 108 CHK(GET_LAY_SPECINT(&lay, iband, &band) == RES_OK); 109 110 FOR_EACH(iquad, 0, band.quadrature_length) { 111 double quad_bounds[2] = {DBL_MAX, -DBL_MAX}; 112 double tab_bounds2[2] = {DBL_MAX, -DBL_MAX}; 113 size_t itest; 114 115 CHK(GET_LAY_SPECINT_TAB(&band, iquad, &tab) == RES_OK); 116 117 FOR_EACH(itab, 0, tab.tab_length) { 118 const double k = GET_K(&tab, itab); 119 double tab_bounds[2] = {DBL_MAX, -DBL_MAX}; 120 tab_bounds2[0] = MMIN(tab_bounds2[0], k); 121 tab_bounds2[1] = MMAX(tab_bounds2[1], k); 122 123 x_h2o_range[0] = tab.x_h2o_tab[0]; 124 x_h2o_range[1] = tab.x_h2o_tab[itab]; 125 CHK(LAY_SPECINT_TAB_GET_BOUNDS 126 (&tab, x_h2o_range, tab_bounds) == RES_OK); 127 CHK(eq_eps(tab_bounds2[0], tab_bounds[0], 1.e-6)); 128 CHK(eq_eps(tab_bounds2[1], tab_bounds[1], 1.e-6)); 129 } 130 131 FOR_EACH(itest, 0, 10) { 132 const double bias = tab.x_h2o_tab[0]; 133 const double scale = tab.x_h2o_tab[tab.tab_length-1] - bias; 134 const double r0 = rand_canonic(); 135 const double r1 = rand_canonic(); 136 double tab_bounds[2] = {DBL_MAX, -DBL_MAX}; 137 double tab_bounds3[2] = {DBL_MAX, -DBL_MAX}; 138 double k0, k1; 139 size_t tab_range[2]; 140 141 x_h2o_range[0] = r0*scale + bias; 142 x_h2o_range[1] = r1*scale + bias; 143 if(x_h2o_range[0] > x_h2o_range[1]) { 144 SWAP(double, x_h2o_range[0], x_h2o_range[1]); 145 } 146 HTGOP(CONCAT(CONCAT(CONCAT( 147 layer_,DOMAIN),_spectral_interval_tab_fetch_),DATA) 148 (&tab, x_h2o_range[0], &k0)); 149 HTGOP(CONCAT(CONCAT(CONCAT( 150 layer_,DOMAIN),_spectral_interval_tab_fetch_),DATA) 151 (&tab, x_h2o_range[1], &k1)); 152 153 tab_bounds[0] = MMIN(k0, k1); 154 tab_bounds[1] = MMAX(k0, k1); 155 156 tab_range[0] = SIZE_MAX; 157 tab_range[1] = SIZE_MAX; 158 FOR_EACH(itab, 0, tab.tab_length) { 159 if(tab_range[0]==SIZE_MAX && tab.x_h2o_tab[itab] > x_h2o_range[0]) 160 tab_range[0] = itab; 161 if(tab_range[1]==SIZE_MAX && tab.x_h2o_tab[itab] >= x_h2o_range[1]) 162 tab_range[1] = itab; 163 if(tab_range[0] != SIZE_MAX && tab_range[1] != SIZE_MAX) 164 break; 165 } 166 CHK(tab_range[0] != SIZE_MAX && tab_range[1] != SIZE_MAX); 167 168 FOR_EACH(itab, tab_range[0], tab_range[1]) { 169 tab_bounds[0] = MMIN(GET_K(&tab, itab), tab_bounds[0]); 170 tab_bounds[1] = MMAX(GET_K(&tab, itab), tab_bounds[1]); 171 } 172 173 CHK(LAY_SPECINT_TAB_GET_BOUNDS 174 (&tab, x_h2o_range, tab_bounds3) == RES_OK); 175 CHK(eq_eps(tab_bounds[0], tab_bounds3[0], 1.e-6)); 176 CHK(eq_eps(tab_bounds[1], tab_bounds3[1], 1.e-6)); 177 } 178 179 quad_range[0] = iquad; 180 quad_range[1] = iquad; 181 x_h2o_range[0] = lay.x_h2o_tab[0]; 182 x_h2o_range[1] = lay.x_h2o_tab[lay.tab_length-1]; 183 CHK(LAY_SPECINT_QUADS_GET_BOUNDS 184 (&band, quad_range, x_h2o_range, quad_bounds) == RES_OK); 185 CHK(eq_eps(quad_bounds[0], tab_bounds2[0], 1.e-6)); 186 CHK(eq_eps(quad_bounds[1], tab_bounds2[1], 1.e-6)); 187 188 quad_bounds2[0] = MMIN(quad_bounds[0], quad_bounds2[0]); 189 quad_bounds2[1] = MMAX(quad_bounds[1], quad_bounds2[1]); 190 191 quad_range[0] = 0; 192 quad_range[1] = iquad; 193 CHK(LAY_SPECINT_QUADS_GET_BOUNDS 194 (&band, quad_range, x_h2o_range, quad_bounds) == RES_OK); 195 CHK(eq_eps(quad_bounds2[0], quad_bounds[0], 1.e-6)); 196 CHK(eq_eps(quad_bounds2[1], quad_bounds[1], 1.e-6)); 197 } 198 } 199 } 200 } 201 202 #undef GET_NSPECINTS 203 #undef GET_SPECINT 204 #undef LAY_SPECINT_QUADS_GET_BOUNDS 205 #undef LAY_SPECINT_TAB_GET_BOUNDS 206 #undef LAY_SPECINT 207 #undef LAY_SPECINT_TAB 208 #undef GET_LAY_SPECINT 209 #undef GET_LAY_SPECINT_TAB 210 #undef GET_K 211 #undef DOMAIN 212 #undef DATA