solstice-solver

Solver library of the solstice app
git clone git://git.meso-star.com/solstice-solver.git
Log | Files | Refs | README | LICENSE

test_ssol_solver9.c (6565B)


      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 #include <rsys/math.h>
     22 
     23 #define TGT_X 6
     24 #define TGT_Y 10
     25 #define PLANE_NAME TARGET
     26 #define HALF_X (TGT_X / 2)
     27 #define HALF_Y (TGT_Y / 2)
     28 STATIC_ASSERT((HALF_X * 2 == TGT_X), ONLY_ENVEN_VALUES_FOR_TGT_X);
     29 STATIC_ASSERT((HALF_Y * 2 == TGT_Y), ONLY_ENVEN_VALUES_FOR_TGT_Y);
     30 #include "test_ssol_rect_geometry.h"
     31 
     32 #define SZ MMAX(TGT_X, TGT_Y)
     33 #define CUBE_NAME CUBE
     34 #define HALF_X (SZ / 2)
     35 #define HALF_Y (SZ / 2)
     36 #define HALF_Z (SZ / 2)
     37 STATIC_ASSERT((HALF_X * 2 == SZ), ONLY_ENVEN_VALUES_FOR_SZ);
     38 #include "test_ssol_cube_geometry.h"
     39 
     40 #include <rsys/double33.h>
     41 
     42 #include <star/s3d.h>
     43 #include <star/ssp.h>
     44 
     45 static void
     46 get_wlen(const size_t i, double* wlen, double* data, void* ctx)
     47 {
     48   double wavelengths[3] = { 1, 2, 3 };
     49   double intensities[3] = { 1, 0.8, 1 };
     50   CHK(i < 3);
     51   (void) ctx;
     52   *wlen = wavelengths[i];
     53   *data = intensities[i];
     54 }
     55 
     56 int
     57 main(int argc, char** argv)
     58 {
     59   struct mem_allocator allocator;
     60   struct ssol_device* dev;
     61   struct ssp_rng* rng;
     62   struct ssol_scene* scene;
     63   struct ssol_shape* square;
     64   struct ssol_vertex_data attribs[1] = { SSOL_VERTEX_DATA_NULL__ };
     65   struct ssol_shape* cube;
     66   struct ssol_material* m_mtl;
     67   struct ssol_material* v_mtl;
     68   struct ssol_mirror_shader shader = SSOL_MIRROR_SHADER_NULL;
     69   struct ssol_object* m_object;
     70   struct ssol_object* t_object;
     71   struct ssol_instance* heliostat;
     72   struct ssol_instance* target;
     73   struct ssol_sun* sun;
     74   struct ssol_spectrum* spectrum;
     75   struct ssol_estimator* estimator;
     76   struct ssol_mc_global mc_global;
     77   struct ssol_mc_receiver mc_rcv;
     78   double dir[3];
     79   double transform[12]; /* 3x4 column major matrix */
     80   size_t count;
     81 
     82   (void) argc, (void) argv;
     83   d3_splat(transform + 9, 0);
     84   d33_rotation_pitch(transform, PI); /* flip faces: invert normal */
     85   transform[11] = 6; /* set it above the cube */
     86 
     87   mem_init_proxy_allocator(&allocator, &mem_default_allocator);
     88 
     89   CHK(ssol_device_create
     90     (NULL, &allocator, SSOL_NTHREADS_DEFAULT, 0, &dev) == RES_OK);
     91 
     92 #define DNI 1000
     93   CHK(ssp_rng_create(&allocator, SSP_RNG_THREEFRY, &rng) == RES_OK);
     94   CHK(ssol_spectrum_create(dev, &spectrum) == RES_OK);
     95   CHK(ssol_spectrum_setup(spectrum, get_wlen, 3, NULL) == RES_OK);
     96   CHK(ssol_sun_create_directional(dev, &sun) == RES_OK);
     97   CHK(ssol_sun_set_direction(sun, d3(dir, 0, 0, -1)) == RES_OK);
     98   CHK(ssol_sun_set_spectrum(sun, spectrum) == RES_OK);
     99   CHK(ssol_sun_set_dni(sun, DNI) == RES_OK);
    100   CHK(ssol_scene_create(dev, &scene) == RES_OK);
    101   CHK(ssol_scene_attach_sun(scene, sun) == RES_OK);
    102 
    103   /* Create scene content */
    104 
    105   CHK(ssol_shape_create_mesh(dev, &square) == RES_OK);
    106   attribs[0].usage = SSOL_POSITION;
    107   attribs[0].get = get_position;
    108   CHK(ssol_mesh_setup(square, TARGET_NTRIS__, get_ids,
    109     TARGET_NVERTS__, attribs, 1, (void*) &TARGET_DESC__) == RES_OK);
    110 
    111   CHK(ssol_shape_create_mesh(dev, &cube) == RES_OK);
    112   CHK(ssol_mesh_setup(cube, CUBE_NTRIS__, get_ids,
    113     CUBE_NVERTS__, attribs, 1, (void*) &CUBE_DESC__) == RES_OK);
    114 
    115   CHK(ssol_material_create_mirror(dev, &m_mtl) == RES_OK);
    116   shader.normal = get_shader_normal;
    117   shader.reflectivity = get_shader_reflectivity;
    118   shader.roughness = get_shader_roughness;
    119   CHK(ssol_mirror_setup(m_mtl, &shader, SSOL_MICROFACET_BECKMANN) == RES_OK);
    120   CHK(ssol_material_create_virtual(dev, &v_mtl) == RES_OK);
    121 
    122   CHK(ssol_object_create(dev, &m_object) == RES_OK);
    123   CHK(ssol_object_add_shaded_shape(m_object, cube, m_mtl, m_mtl) == RES_OK);
    124   CHK(ssol_object_instantiate(m_object, &heliostat) == RES_OK);
    125   CHK(ssol_scene_attach_instance(scene, heliostat) == RES_OK);
    126 
    127   CHK(ssol_object_create(dev, &t_object) == RES_OK);
    128   CHK(ssol_object_add_shaded_shape(t_object, square, v_mtl, v_mtl) == RES_OK);
    129   CHK(ssol_object_instantiate(t_object, &target) == RES_OK);
    130   CHK(ssol_instance_set_transform(target, transform) == RES_OK);
    131   CHK(ssol_instance_set_receiver(target, SSOL_FRONT, 0) == RES_OK);
    132   CHK(ssol_instance_sample(target, 0) == RES_OK);
    133   CHK(ssol_scene_attach_instance(scene, target) == RES_OK);
    134 
    135 #define N__ 100000
    136 #define GET_MC_RCV ssol_estimator_get_mc_receiver
    137   CHK(ssol_solve(scene, rng, N__, 0, NULL, &estimator) == RES_OK);
    138   CHK(ssol_estimator_get_realisation_count(estimator, &count) == RES_OK);
    139   CHK(count == N__);
    140   CHK(ssol_estimator_get_failed_count(estimator, &count) == RES_OK);
    141   CHK(count == 0);
    142 #define DNI_TGT_S (DNI * TGT_X * TGT_Y)
    143 #define DNI_S (DNI * SZ * SZ)
    144   CHK(ssol_estimator_get_mc_global(estimator, &mc_global) == RES_OK);
    145   print_global(&mc_global);
    146   CHK(eq_eps(mc_global.cos_factor.E, 1./3., 3 * mc_global.cos_factor.SE));
    147   CHK(eq_eps(mc_global.shadowed.E, DNI_S, 3 * mc_global.shadowed.SE));
    148   CHK(eq_eps(mc_global.missing.E, MMAX(DNI_S, DNI_TGT_S),
    149     3 * mc_global.missing.SE));
    150   CHK(GET_MC_RCV(estimator, target, SSOL_FRONT, &mc_rcv) == RES_OK);
    151   printf("Ir(target1) = %g +/- %g\n",
    152     mc_rcv.incoming_flux.E, mc_rcv.incoming_flux.SE);
    153   CHK(eq_eps(mc_rcv.incoming_flux.E, MMIN(DNI_S, DNI_TGT_S),
    154     3 * mc_rcv.incoming_flux.SE));
    155 
    156   /* Free data */
    157   CHK(ssol_instance_ref_put(heliostat) == RES_OK);
    158   CHK(ssol_instance_ref_put(target) == RES_OK);
    159   CHK(ssol_object_ref_put(m_object) == RES_OK);
    160   CHK(ssol_object_ref_put(t_object) == RES_OK);
    161   CHK(ssol_shape_ref_put(square) == RES_OK);
    162   CHK(ssol_shape_ref_put(cube) == RES_OK);
    163   CHK(ssol_material_ref_put(m_mtl) == RES_OK);
    164   CHK(ssol_material_ref_put(v_mtl) == RES_OK);
    165   CHK(ssol_estimator_ref_put(estimator) == RES_OK);
    166   CHK(ssol_device_ref_put(dev) == RES_OK);
    167   CHK(ssol_scene_ref_put(scene) == RES_OK);
    168   CHK(ssp_rng_ref_put(rng) == RES_OK);
    169   CHK(ssol_spectrum_ref_put(spectrum) == RES_OK);
    170   CHK(ssol_sun_ref_put(sun) == RES_OK);
    171 
    172   check_memory_allocator(&allocator);
    173   mem_shutdown_proxy_allocator(&allocator);
    174   CHK(mem_allocated_size() == 0);
    175 
    176   return 0;
    177 }