test_sphin_load_source.c (11048B)
1 /* Copyright (C) 2024-2026 Centre National de la Recherche Scientifique 2 * Copyright (C) 2024-2026 Clermont Auvergne INP 3 * Copyright (C) 2024-2026 INSA Lyon 4 * Copyright (C) 2024-2026 Institut Mines Télécom Albi-Carmaux 5 * Copyright (C) 2024-2026 Institut National Polytechnique de Toulouse 6 * Copyright (C) 2024-2026 |Méso|Star> (contact@meso-star.com) 7 * Copyright (C) 2024-2026 PhotonLyX (info@photonlyx.com) 8 * Copyright (C) 2024-2026 Université de Lorraine 9 * Copyright (C) 2024-2026 Université Paul Sabatier 10 * Copyright (C) 2024-2026 Université Toulouse - Jean Jaurès 11 * 12 * This program is free software: you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation, either version 3 of the License, or 15 * (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 * 22 * You should have received a copy of the GNU General Public License 23 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 24 25 #include "sphin.h" 26 27 #include <rsys/math.h> 28 #include <rsys/mem_allocator.h> 29 #include <rsys/rsys.h> 30 31 #include <stdio.h> 32 #include <string.h> 33 34 struct sphin; 35 36 static void 37 write_stl_test_files 38 (void) 39 { 40 FILE* file; 41 static const char* test0 = 42 "solid\n" 43 " facet normal 0.0 -1.0 0.0\n" 44 " outer loop\n" 45 " vertex 0.0 0.0 0.0\n" 46 " vertex 1.0 0.0 0.0\n" 47 " vertex 0.0 0.0 1.0\n" 48 " endloop\n" 49 " endfacet\n" 50 "endsolid"; 51 file = fopen("test_0.stl", "w"); 52 CHK(file != NULL); 53 fwrite(test0, sizeof(char), strlen(test0), file); 54 fclose(file); 55 } 56 57 static void 58 write_emission_spectrum_test_files 59 (void) 60 { 61 FILE* file; 62 static const char* test0 = 63 "#wv,abs_cross_sec\n" 64 "100 1\n" 65 "200 2\n" 66 "300 3\n" 67 "400 4\n"; 68 file = fopen("source_spec.txt", "w"); 69 CHK(file != NULL); 70 fwrite(test0, sizeof(char), strlen(test0), file); 71 fclose(file); 72 } 73 74 static void 75 test_source_api 76 (struct sphin* sphin) 77 { 78 const char* path = "file.txt"; 79 double power; 80 double value; 81 size_t nsurfaces, ngeometries; 82 struct sphin_config* config = NULL; 83 struct sphin_surface* surface = NULL; 84 struct sphin_source_surface* source = NULL; 85 struct sphin_source_surface_direction_distribution dir_dist = 86 SPHIN_SOURCE_SURFACE_DIRECTION_DISTRIBUTION_NULL; 87 struct sphin_source_surface_flux_density flux_density = 88 SPHIN_SOURCE_SURFACE_FLUX_DENSITY_NULL; 89 struct sphin_spectral_property_descriptor emission_desc = 90 SPHIN_SPECTRAL_PROPERTY_DESCRIPTOR_NULL; 91 92 FILE* fp = NULL; 93 94 CHK(fp = fopen(path, "w+")); 95 fprintf(fp, "\t\t surface : \"surface name\"\n"); /* Source 0 */ 96 fprintf(fp, "\tgeometry: FRONT test_0.stl\t\n"); 97 fprintf(fp, "\tsource:#commentaire\n"); 98 fprintf(fp, "\tflux_density: 200e-6 mol/m^2/s source_spec.txt nm nm^-1 \t\n"); 99 fprintf(fp, "\tdirection: LAMBERT\n"); 100 fprintf(fp, "\t\t surface : \"surface name 2\"\n"); /* Source 1 */ 101 fprintf(fp, "\tgeometry: BACK test_0.stl\t\n"); 102 fprintf(fp, "\tsource:\t #commentaire\n"); 103 fprintf(fp, "\tflux_density: 1e3 umol/m^2/s source_spec.txt nm nm^-1\t\n"); 104 fprintf(fp, "\tdirection: COLLIM NORMAL\n"); 105 fprintf(fp, "\t\t surface : \"surface name 3\"\n"); /* Source 2 */ 106 fprintf(fp, "\tgeometry: BACK test_0.stl\t\n"); 107 fprintf(fp, "\tsource:\t #commentaire\n"); 108 fprintf(fp, "\tflux_density: 200e3 mW/m^2 source_spec.txt nm nm^-1\n"); 109 fprintf(fp, "\tdirection: COS_POW_N 1\n"); 110 fclose(fp); 111 112 CHK(sphin_load(sphin, path, &config) == RES_OK); 113 CHK(sphin_config_get_surface_count(config, &nsurfaces) == RES_OK); 114 CHK(nsurfaces == 3); 115 116 CHK(sphin_config_get_surface(config, 0, &surface) == RES_OK); 117 CHK(sphin_surface_get_geometry_count(NULL, &ngeometries) == RES_BAD_ARG); 118 CHK(sphin_surface_get_geometry_count(surface, &ngeometries) == RES_OK); 119 CHK(sphin_surface_get_source(surface, &source) == RES_OK); 120 CHK(sphin_source_surface_get_direction_distribution 121 (NULL, &dir_dist) == RES_BAD_ARG); 122 CHK(sphin_source_surface_get_direction_distribution 123 (source, NULL) == RES_BAD_ARG); 124 CHK(sphin_source_surface_get_direction_distribution 125 (source, &dir_dist) == RES_OK); 126 CHK(dir_dist.type == SPHIN_SOURCE_DIRECTION_ISOTROPIC); 127 CHK(dir_dist.collim.direction[0] == 128 SPHIN_SOURCE_SURFACE_DIRECTION_DISTRIBUTION_COLLIM_NULL.direction[0]); 129 CHK(dir_dist.collim.direction[1] == 130 SPHIN_SOURCE_SURFACE_DIRECTION_DISTRIBUTION_COLLIM_NULL.direction[1]); 131 CHK(dir_dist.collim.direction[2] == 132 SPHIN_SOURCE_SURFACE_DIRECTION_DISTRIBUTION_COLLIM_NULL.direction[2]); 133 CHK(sphin_source_surface_get_flux_density 134 (NULL, &flux_density) == RES_BAD_ARG); 135 CHK(sphin_source_surface_get_flux_density(source, NULL) == RES_BAD_ARG); 136 CHK(sphin_source_surface_get_flux_density(source, &flux_density) == RES_OK); 137 CHK(eq_eps(flux_density.flux_density, 200, 1e-15)); 138 CHK(flux_density.unit == SPHIN_PHOTON_UNIT_MOL); 139 CHK(sphin_source_surface_ref_get(NULL) == RES_BAD_ARG); 140 CHK(sphin_source_surface_ref_put(NULL) == RES_BAD_ARG); 141 CHK(sphin_source_surface_ref_get(source) == RES_OK); 142 CHK(sphin_source_surface_ref_put(source) == RES_OK); 143 144 CHK(sphin_config_get_surface(config, 1, &surface) == RES_OK); 145 CHK(sphin_surface_get_source(surface, &source) == RES_OK); 146 CHK(sphin_source_surface_get_direction_distribution 147 (source, &dir_dist) == RES_OK); 148 CHK(dir_dist.type == SPHIN_SOURCE_DIRECTION_COLLIM); 149 CHK(dir_dist.collim.direction[0] == 0); 150 CHK(dir_dist.collim.direction[1] == 0); 151 CHK(dir_dist.collim.direction[2] == 0); 152 CHK(sphin_source_surface_get_flux_density(source, &flux_density) == RES_OK); 153 CHK(eq_eps(flux_density.flux_density, 1000, 1e-15)); 154 CHK(flux_density.unit == SPHIN_PHOTON_UNIT_MOL); 155 CHK(sphin_spectral_property_ref_get 156 (flux_density.emission_spectrum) == RES_OK); 157 CHK(sphin_spectral_property_ref_put 158 (flux_density.emission_spectrum) == RES_OK); 159 CHK(sphin_spectral_property_get_desc 160 (flux_density.emission_spectrum, &emission_desc) == RES_OK); 161 CHK(sphin_spectral_property_interpolate_at_wavelength 162 (flux_density.emission_spectrum, 163 100, 164 SPHIN_INTERPOLATION_LINEAR, 165 &value) == RES_OK); 166 CHK(eq_eps(value, 1./750, 1e-15)); 167 CHK(sphin_spectral_property_interpolate_at_wavelength 168 (flux_density.emission_spectrum, 169 175, 170 SPHIN_INTERPOLATION_LINEAR, 171 &value) == RES_OK); 172 CHK(eq_eps(value, 1.75/750, 1e-15)); 173 CHK(eq_eps(emission_desc.wavelengths[2], 300, 1e-15)); 174 CHK(eq_eps(emission_desc.values[2], 3./750, 1e-15)); 175 CHK(emission_desc.data_count == 4); 176 177 CHK(sphin_config_get_surface(config, 2, &surface) == RES_OK); 178 CHK(sphin_surface_get_source(surface, &source) == RES_OK); 179 CHK(sphin_source_surface_get_direction_distribution 180 (source, &dir_dist) == RES_OK); 181 CHK(dir_dist.type == SPHIN_SOURCE_DIRECTION_COS_POW_N); 182 CHK(eq_eps(dir_dist.cos_pow_n.collimation_degree, 1, 1e-15)); 183 CHK(dir_dist.collim.direction[0] == 184 SPHIN_SOURCE_SURFACE_DIRECTION_DISTRIBUTION_COLLIM_NULL.direction[0]); 185 CHK(dir_dist.collim.direction[1] == 186 SPHIN_SOURCE_SURFACE_DIRECTION_DISTRIBUTION_COLLIM_NULL.direction[1]); 187 CHK(dir_dist.collim.direction[2] == 188 SPHIN_SOURCE_SURFACE_DIRECTION_DISTRIBUTION_COLLIM_NULL.direction[2]); 189 CHK(sphin_source_surface_get_flux_density(source, NULL) == RES_BAD_ARG); 190 CHK(sphin_source_surface_get_flux_density 191 (NULL, &flux_density) == RES_BAD_ARG); 192 CHK(sphin_source_surface_get_flux_density(source, &flux_density) == RES_OK); 193 CHK(eq_eps(flux_density.flux_density, 200, 1e-15)); 194 CHK(sphin_surface_source_get_power(surface, &power) == RES_OK); 195 CHK(eq_eps(power, 100, 1e-15)); 196 CHK(flux_density.unit == SPHIN_PHOTON_UNIT_JOULE); 197 198 CHK(sphin_config_ref_put(config) == RES_OK); 199 } 200 201 static void 202 test_source_api_bad_flux_density 203 (struct sphin* sphin) 204 { 205 const char* path = "file.txt"; 206 struct sphin_config* config = NULL; 207 208 FILE* fp = NULL; 209 210 CHK(fp = fopen(path, "w+")); 211 fprintf(fp, "\t\t surface : \"surface name\"\n"); 212 fprintf(fp, "\tgeometry: FRONT test_0.stl\t\n"); 213 fprintf(fp, "\tsource:\t #commentaire\n"); 214 fprintf(fp, "\tflux_density: 200e-6 mol /m^2/s\t\n"); 215 fprintf(fp, "\tdirection: LAMBERT\n"); 216 fclose(fp); 217 218 CHK(sphin_load(sphin, path, &config) == RES_BAD_ARG); 219 } 220 221 static void 222 test_source_api_bad_flux_density_value 223 (struct sphin* sphin) 224 { 225 const char* path = "file.txt"; 226 struct sphin_config* config = NULL; 227 228 FILE* fp = NULL; 229 230 CHK(fp = fopen(path, "w+")); 231 fprintf(fp, "\t\t surface : \"surface name\"\n"); 232 fprintf(fp, "\tgeometry: FRONT test_0.stl\t\n"); 233 fprintf(fp, "\tsource:\t #commentaire\n"); 234 fprintf(fp, "\tflux_density: -200e-6 mol/m^2/s\t\n"); 235 fprintf(fp, "\tdirection: LAMBERT\n"); 236 fclose(fp); 237 238 CHK(sphin_load(sphin, path, &config) == RES_BAD_ARG); 239 } 240 241 static void 242 test_source_api_bad_direction 243 (struct sphin* sphin) 244 { 245 const char* path = "file.txt"; 246 struct sphin_config* config = NULL; 247 248 FILE* fp = NULL; 249 250 CHK(fp = fopen(path, "w+")); 251 fprintf(fp, "\t\t surface : \"surface name\"\n"); 252 fprintf(fp, "\tgeometry: FRONT test_0.stl\t\n"); 253 fprintf(fp, "\tsource:\t #commentaire\n"); 254 fprintf(fp, "\tflux_density: 200e-6 mol/m^2/s\t\n"); 255 fprintf(fp, "\tdirection: LAMBRET\n"); 256 fclose(fp); 257 258 CHK(sphin_load(sphin, path, &config) == RES_BAD_ARG); 259 } 260 261 static void 262 test_source_api_bad_collimation_degree 263 (struct sphin* sphin) 264 { 265 const char* path = "file.txt"; 266 struct sphin_config* config = NULL; 267 268 FILE* fp = NULL; 269 270 CHK(fp = fopen(path, "w+")); 271 fprintf(fp, "\t\t surface : \"surface name\"\n"); 272 fprintf(fp, "\tgeometry: FRONT test_0.stl\t\n"); 273 fprintf(fp, "\tsource:\t #commentaire\n"); 274 fprintf(fp, "\tflux_density: 200e-6 mol/m^2/s\t\n"); 275 fprintf(fp, "\tdirection: COS_POW_N -100\n"); 276 fclose(fp); 277 278 CHK(sphin_load(sphin, path, &config) == RES_BAD_ARG); 279 } 280 281 static void 282 test_source_api_bad_name 283 (struct sphin* sphin) 284 { 285 const char* path = "file.txt"; 286 struct sphin_config* config = NULL; 287 288 FILE* fp = NULL; 289 290 CHK(fp = fopen(path, "w+")); 291 fprintf(fp, "\t\t surface : \"surface name\"\n"); 292 fprintf(fp, "\tgeometry: FRONT test_0.stl\t\n"); 293 fprintf(fp, "\tsource:\t \"blabla\" #commentaire\n"); 294 fclose(fp); 295 296 CHK(sphin_load(sphin, path, &config) == RES_BAD_ARG); 297 } 298 int 299 main(int argc, char** argv) 300 { 301 struct sphin* sphin = NULL; 302 struct sphin_create_args args = SPHIN_CREATE_ARGS_DEFAULT; 303 304 (void)argc; 305 (void)argv; 306 307 args.verbose = 1; 308 sphin_create(&args, &sphin); 309 310 write_stl_test_files(); 311 write_emission_spectrum_test_files(); 312 test_source_api(sphin); 313 test_source_api_bad_flux_density(sphin); 314 test_source_api_bad_flux_density_value(sphin); 315 test_source_api_bad_collimation_degree(sphin); 316 test_source_api_bad_direction(sphin); 317 test_source_api_bad_name(sphin); 318 319 CHK(sphin_ref_put(sphin) == RES_OK); 320 321 CHK(mem_allocated_size() == 0); 322 return 0; 323 }