solstice

Compute collected power and efficiencies of a solar plant
git clone git://git.meso-star.com/solstice.git
Log | Files | Refs | README | LICENSE

test_solparser_mirror.c (5052B)


      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 "solparser.h"
     18 #include "test_solstice_utils.h"
     19 
     20 static void
     21 check_object
     22   (struct solparser* parser,
     23    const struct solparser_object* obj,
     24    const double reflectivity,
     25    const double slope_error,
     26    const enum solparser_microfacet_distribution distrib)
     27 {
     28   const struct solparser_shape* shape;
     29   const struct solparser_shape_sphere* sphere;
     30   const struct solparser_material_double_sided* mtl2;
     31   const struct solparser_material* mtl;
     32   const struct solparser_material_mirror* mirror;
     33 
     34   CHK(obj != NULL);
     35 
     36   shape = solparser_get_shape(parser, obj->shape);
     37   CHK(shape->type == SOLPARSER_SHAPE_SPHERE);
     38   sphere = solparser_get_shape_sphere(parser, shape->data.sphere);
     39   CHK(sphere->radius == 1);
     40 
     41   mtl2 = solparser_get_material_double_sided(parser, obj->mtl2);
     42   CHK(mtl2->front.i == mtl2->back.i);
     43   mtl = solparser_get_material(parser, mtl2->front);
     44   CHK(mtl->type == SOLPARSER_MATERIAL_MIRROR);
     45   mirror = solparser_get_material_mirror(parser, mtl->data.mirror);
     46   CHK(mirror->reflectivity.type == SOLPARSER_MTL_DATA_REAL);
     47   CHK(mirror->reflectivity.value.real == reflectivity);
     48   CHK(mirror->slope_error.type == SOLPARSER_MTL_DATA_REAL);
     49   CHK(mirror->slope_error.value.real == slope_error);
     50   CHK(mirror->ufacet_distrib == distrib);
     51 }
     52 
     53 int
     54 main(int argc, char** argv)
     55 {
     56   struct mem_allocator allocator;
     57   struct solparser* parser;
     58   struct solparser_entity_iterator it, it_end;
     59   struct solparser_entity_id entity_id;
     60   const struct solparser_entity* entity;
     61   const struct solparser_geometry* geom;
     62   const struct solparser_object* obj[3];
     63   FILE* stream;
     64   (void)argc, (void)argv;
     65 
     66   CHK((stream = tmpfile()) != NULL);
     67   fprintf(stream, "- sun: {dni: 1}\n");
     68   fprintf(stream, "- material: &specular\n");
     69   fprintf(stream, "    mirror:\n");
     70   fprintf(stream, "      reflectivity: 1\n");
     71   fprintf(stream, "      slope_error: 0\n");
     72   fprintf(stream, "- material: &beckmann\n");
     73   fprintf(stream, "    mirror:\n");
     74   fprintf(stream, "      reflectivity: 0.5\n");
     75   fprintf(stream, "      slope_error: 0.5\n");
     76   fprintf(stream, "      microfacet: BECKMANN\n");
     77   fprintf(stream, "- material: &pillbox\n");
     78   fprintf(stream, "    mirror:\n");
     79   fprintf(stream, "      reflectivity: 0.2\n");
     80   fprintf(stream, "      slope_error: 0.2\n");
     81   fprintf(stream, "      microfacet: PILLBOX\n");
     82   fprintf(stream, "\n");
     83   fprintf(stream, "- entity:\n");
     84   fprintf(stream, "    name: entity\n");
     85   fprintf(stream, "    primary: 1\n");
     86   fprintf(stream, "    geometry: \n");
     87   fprintf(stream, "    - sphere: {radius: 1}\n");
     88   fprintf(stream, "      material: *specular\n");
     89   fprintf(stream, "    - sphere: {radius: 1}\n");
     90   fprintf(stream, "      material: *beckmann\n");
     91   fprintf(stream, "    - sphere: {radius: 1}\n");
     92   fprintf(stream, "      material: *pillbox\n");
     93   rewind(stream);
     94 
     95   CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK);
     96   solparser_create(&allocator, &parser);
     97   CHK(solparser_setup(parser, NULL, stream) == RES_OK);
     98   CHK(solparser_load(parser) == RES_OK);
     99 
    100   solparser_entity_iterator_begin(parser, &it);
    101   solparser_entity_iterator_end(parser, &it_end);
    102   CHK(!solparser_entity_iterator_eq(&it, &it_end));
    103 
    104   entity_id = solparser_entity_iterator_get(&it);
    105   entity = solparser_get_entity(parser, entity_id);
    106   CHK(!strcmp(str_cget(&entity->name), "entity"));
    107   CHK(entity->primary == 1);
    108   CHK(entity->type == SOLPARSER_ENTITY_GEOMETRY);
    109   
    110   geom = solparser_get_geometry(parser, entity->data.geometry);
    111   CHK(solparser_geometry_get_objects_count(geom) == 3);
    112 
    113   obj[0] = solparser_get_object(parser, solparser_geometry_get_object(geom,0));
    114   obj[1] = solparser_get_object(parser, solparser_geometry_get_object(geom,1));
    115   obj[2] = solparser_get_object(parser, solparser_geometry_get_object(geom,2));
    116   check_object(parser, obj[0], 1.0, 0.0, SOLPARSER_MICROFACET_BECKMANN);
    117   check_object(parser, obj[1], 0.5, 0.5, SOLPARSER_MICROFACET_BECKMANN);
    118   check_object(parser, obj[2], 0.2, 0.2, SOLPARSER_MICROFACET_PILLBOX);
    119 
    120   solparser_entity_iterator_next(&it);
    121   CHK(solparser_entity_iterator_eq(&it, &it_end));
    122   CHK(solparser_load(parser) == RES_BAD_OP);
    123   solparser_ref_put(parser);
    124 
    125   fclose(stream);
    126   check_memory_allocator(&allocator);
    127   mem_shutdown_proxy_allocator(&allocator);
    128   CHK(mem_allocated_size() == 0);
    129   return 0;
    130 }