test_ssol_by_receiver_integration.c (7211B)
1 /* Copyright (C) 2018-2026 |Meso|Star> (contact@meso-star.com) 2 * Copyright (C) 2016, 2018 CNRS 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 16 17 #include "ssol.h" 18 #include "test_ssol_utils.h" 19 #include "test_ssol_materials.h" 20 21 #define PLANE_NAME SQUARE 22 #define HALF_X 1 23 #define HALF_Y 1 24 #include "test_ssol_rect_geometry.h" 25 26 #define POLYGON_NAME POLY 27 #define HALF_X 10 28 #define HALF_Y 10 29 #include "test_ssol_rect2D_geometry.h" 30 31 #include <rsys/double33.h> 32 33 #include <star/s3d.h> 34 #include <star/ssp.h> 35 36 static void 37 get_wlen(const size_t i, double* wlen, double* data, void* ctx) 38 { 39 double wavelengths[3] = { 1, 2, 3 }; 40 double intensities[3] = { 1, 0.8, 1 }; 41 CHK(i < 3); 42 (void)ctx; 43 *wlen = wavelengths[i]; 44 *data = intensities[i]; 45 } 46 47 int 48 main(int argc, char** argv) 49 { 50 struct mem_allocator allocator; 51 struct ssol_device* dev; 52 struct ssp_rng* rng; 53 struct ssol_scene* scene; 54 struct ssol_shape* square; 55 struct ssol_vertex_data attribs[1] = { SSOL_VERTEX_DATA_NULL__ }; 56 struct ssol_shape* quad_square; 57 struct ssol_carving carving = SSOL_CARVING_NULL; 58 struct ssol_quadric quadric = SSOL_QUADRIC_DEFAULT; 59 struct ssol_punched_surface punched = SSOL_PUNCHED_SURFACE_NULL; 60 struct ssol_material* m_mtl; 61 struct ssol_material* v_mtl; 62 struct ssol_mirror_shader shader = SSOL_MIRROR_SHADER_NULL; 63 struct ssol_object* m_object; 64 struct ssol_object* t_object; 65 struct ssol_instance* heliostat; 66 struct ssol_instance* target; 67 struct ssol_sun* sun; 68 struct ssol_spectrum* spectrum; 69 struct ssol_estimator *estimator1, *estimator2; 70 struct ssol_mc_receiver mc_rcv; 71 double dir[3]; 72 double transform[12]; /* 3x4 column major matrix */ 73 74 (void) argc, (void) argv; 75 76 d3_splat(transform + 9, 0); 77 d33_rotation_pitch(transform, PI); /* flip faces: invert normal */ 78 transform[11] = 2; /* +2 offset along Z axis */ 79 80 mem_init_proxy_allocator(&allocator, &mem_default_allocator); 81 82 CHK(ssol_device_create 83 (NULL, &allocator, SSOL_NTHREADS_DEFAULT, 0, &dev) == RES_OK); 84 85 CHK(ssp_rng_create(&allocator, SSP_RNG_THREEFRY, &rng) == RES_OK); 86 CHK(ssol_spectrum_create(dev, &spectrum) == RES_OK); 87 CHK(ssol_spectrum_setup(spectrum, get_wlen, 3, NULL) == RES_OK); 88 CHK(ssol_sun_create_directional(dev, &sun) == RES_OK); 89 CHK(ssol_sun_set_direction(sun, d3(dir, 1, 0, -1)) == RES_OK); 90 CHK(ssol_sun_set_spectrum(sun, spectrum) == RES_OK); 91 CHK(ssol_sun_set_dni(sun, 1000) == RES_OK); 92 CHK(ssol_scene_create(dev, &scene) == RES_OK); 93 CHK(ssol_scene_attach_sun(scene, sun) == RES_OK); 94 95 /* Create scene content */ 96 97 CHK(ssol_shape_create_mesh(dev, &square) == RES_OK); 98 attribs[0].usage = SSOL_POSITION; 99 attribs[0].get = get_position; 100 CHK(ssol_mesh_setup(square, SQUARE_NTRIS__, get_ids, 101 SQUARE_NVERTS__, attribs, 1, (void*) &SQUARE_DESC__) == RES_OK); 102 103 CHK(ssol_shape_create_punched_surface(dev, &quad_square) == RES_OK); 104 carving.get = get_polygon_vertices; 105 carving.operation = SSOL_AND; 106 carving.nb_vertices = POLY_NVERTS__; 107 carving.context = &POLY_EDGES__; 108 quadric.type = SSOL_QUADRIC_PLANE; 109 punched.nb_carvings = 1; 110 punched.quadric = &quadric; 111 punched.carvings = &carving; 112 CHK(ssol_punched_surface_setup(quad_square, &punched) == RES_OK); 113 114 CHK(ssol_material_create_mirror(dev, &m_mtl) == RES_OK); 115 shader.normal = get_shader_normal; 116 shader.reflectivity = get_shader_reflectivity; 117 shader.roughness = get_shader_roughness; 118 CHK(ssol_mirror_setup(m_mtl, &shader, SSOL_MICROFACET_BECKMANN) == RES_OK); 119 CHK(ssol_material_create_virtual(dev, &v_mtl) == RES_OK); 120 121 CHK(ssol_object_create(dev, &m_object) == RES_OK); 122 CHK(ssol_object_add_shaded_shape(m_object, quad_square, m_mtl, m_mtl) == RES_OK); 123 CHK(ssol_object_instantiate(m_object, &heliostat) == RES_OK); 124 CHK(ssol_scene_attach_instance(scene, heliostat) == RES_OK); 125 126 CHK(ssol_object_create(dev, &t_object) == RES_OK); 127 CHK(ssol_object_add_shaded_shape(t_object, square, v_mtl, v_mtl) == RES_OK); 128 CHK(ssol_object_instantiate(t_object, &target) == RES_OK); 129 CHK(ssol_instance_set_transform(target, transform) == RES_OK); 130 CHK(ssol_instance_set_receiver(target, SSOL_FRONT, 0) == RES_OK); 131 CHK(ssol_instance_sample(target, 0) == RES_OK); 132 CHK(ssol_scene_attach_instance(scene, target) == RES_OK); 133 134 #define N__ 10000 135 #define S_DNI_cos (4 * 1000 * cos(PI / 4)) 136 #define GET_MC_RCV ssol_estimator_get_mc_receiver 137 #define GET_MC_SAMP_X_RCV ssol_estimator_get_mc_sampled_x_receiver 138 CHK(ssol_solve(scene, rng, N__, 0, NULL, &estimator1) == RES_OK); 139 CHK(GET_MC_RCV(estimator1, target, SSOL_FRONT, &mc_rcv) == RES_OK); 140 printf("Ir(target) = %g +/- %g\n", 141 mc_rcv.incoming_flux.E, mc_rcv.incoming_flux.SE); 142 CHK(ssol_instance_set_receiver(heliostat, SSOL_FRONT, 0) == RES_OK); 143 CHK(eq_eps 144 (mc_rcv.incoming_flux.E, S_DNI_cos, 145 mc_rcv.incoming_flux.SE*3)); 146 CHK(ssol_solve(scene, rng, 8 * N__, 0, NULL, &estimator2) == RES_OK); 147 CHK(GET_MC_RCV(estimator2, target, SSOL_FRONT, &mc_rcv) == RES_OK); 148 printf("Ir(target) = %g +/- %g\n", 149 mc_rcv.incoming_flux.E, mc_rcv.incoming_flux.SE); 150 CHK(eq_eps(mc_rcv.incoming_flux.E, S_DNI_cos, mc_rcv.incoming_flux.SE*3) == 1); 151 CHK(ssol_estimator_ref_put(estimator1) == RES_OK); 152 CHK(ssol_solve(scene, rng, 3 * N__, 0, NULL, &estimator1) == RES_OK); 153 CHK(GET_MC_RCV(estimator1, target, SSOL_FRONT, &mc_rcv) == RES_OK); 154 printf("Ir(target) = %g +/- %g\n", 155 mc_rcv.incoming_flux.E, mc_rcv.incoming_flux.SE); 156 CHK(eq_eps 157 (mc_rcv.incoming_flux.E, S_DNI_cos, 158 mc_rcv.incoming_flux.SE*3)); 159 CHK(GET_MC_SAMP_X_RCV(estimator1, heliostat, target, SSOL_FRONT, &mc_rcv) == RES_OK); 160 printf("Ir(heliostat=>target) = %g +/- %g\n", 161 mc_rcv.incoming_flux.E, mc_rcv.incoming_flux.SE); 162 CHK(eq_eps(mc_rcv.incoming_flux.E, S_DNI_cos, mc_rcv.incoming_flux.SE*3) == 1); 163 164 /* Free data */ 165 CHK(ssol_instance_ref_put(heliostat) == RES_OK); 166 CHK(ssol_instance_ref_put(target) == RES_OK); 167 CHK(ssol_object_ref_put(m_object) == RES_OK); 168 CHK(ssol_object_ref_put(t_object) == RES_OK); 169 CHK(ssol_shape_ref_put(square) == RES_OK); 170 CHK(ssol_shape_ref_put(quad_square) == RES_OK); 171 CHK(ssol_material_ref_put(m_mtl) == RES_OK); 172 CHK(ssol_material_ref_put(v_mtl) == RES_OK); 173 CHK(ssol_device_ref_put(dev) == RES_OK); 174 CHK(ssol_estimator_ref_put(estimator1) == RES_OK); 175 CHK(ssol_estimator_ref_put(estimator2) == RES_OK); 176 CHK(ssol_scene_ref_put(scene) == RES_OK); 177 CHK(ssp_rng_ref_put(rng) == RES_OK); 178 CHK(ssol_spectrum_ref_put(spectrum) == RES_OK); 179 CHK(ssol_sun_ref_put(sun) == RES_OK); 180 181 check_memory_allocator(&allocator); 182 mem_shutdown_proxy_allocator(&allocator); 183 CHK(mem_allocated_size() == 0); 184 185 return 0; 186 }