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_solparser3.c (12944B)


      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 const char* input[] = {
     21   "- material: &lambertian\n",
     22   "    matte: { reflectivity: 0.5 }\n",
     23   "- geometry: &cylinder\n",
     24   "    - cylinder: { radius: 1, height: 10, slices: 128 }\n",
     25   "      material: *lambertian\n",
     26   "- geometry: &cylinder2\n",
     27   "    - cylinder: { radius: 1, height: 10 }\n",
     28   "      material: *lambertian\n",
     29   "- geometry: &hyperbol1\n",
     30   "    - hyperbol:\n",
     31   "        focals: &hyp1_focals { real: 4, image: 1 }\n",
     32   "        clip:\n",
     33   "        - operation : AND\n",
     34   "          vertices : [[1, 2],[3, 4],[6, 7]]\n",
     35   "      material: *lambertian\n",
     36   "- geometry: &hemisphere1\n",
     37   "    - hemisphere:\n",
     38   "        radius: 100\n",
     39   "        clip:\n",
     40   "        - operation : AND\n",
     41   "          circle: { radius: 4 }\n",
     42   "      material: *lambertian\n",
     43   "- sun: \n",
     44   "    dni: 1\n",
     45   "    spectrum: [{wavelength: 1, data: 1}]\n",
     46   "- atmosphere:\n",
     47   "    extinction: [{wavelength: 1, data: 1}]\n",
     48   "- entity:\n",
     49   "    name: entity0\n",
     50   "    primary: 0\n",
     51   "    geometry: *cylinder\n",
     52   "    anchors:\n",
     53   "      - name: anchor0\n",
     54   "        position: [1, 2, 3]\n",
     55   "      - name: anchor1\n",
     56   "        position: [4, 5, 6]\n",
     57   "      - name: anchor2\n",
     58   "        hyperboloid_image_focals: *hyp1_focals\n",
     59   "    children:\n",
     60   "      - name: entity0a\n",
     61   "        primary: 1\n",
     62   "        geometry: *cylinder2\n",
     63   "      - name: entity0b\n",
     64   "        primary: 1\n",
     65   "        geometry: *cylinder\n",
     66   "        anchors:\n\n",
     67   "          - name: anchor0\n",
     68   "            position: [4, 5, 6]\n",
     69   "          - name: entity0b\n",
     70   "            position: [7, 8, 9]\n",
     71   "      - name: entity0c\n",
     72   "        primary: 0\n",
     73   "        geometry: *hyperbol1\n",
     74   "      - name: entity0d\n",
     75   "        primary: 0\n",
     76   "        geometry: *hemisphere1\n",
     77   "- entity:\n",
     78   "    name: entity1\n",
     79   "    x_pivot:\n",
     80   "      ref_point: [1, 2, 3]\n",
     81   "      target: { anchor: \"entity0.entity0b.anchor0\" }\n",
     82   "- entity:\n",
     83   "    name: entity2\n",
     84   "    zx_pivot:\n",
     85   "      spacing: 1\n",
     86   "      ref_point: [1, 2, 3]\n",
     87   "      target: { anchor: \"entity0.entity0b.anchor0\" }\n",
     88   "- entity:\n",
     89   "    name: entity3\n",
     90   "    x_pivot:\n",
     91   "      ref_point: [4, 2, 3]\n",
     92   "      target: { anchor: \"entity0.anchor2\" }\n",
     93   NULL
     94 };
     95 
     96 const struct solparser_anchor* entity0_anchor0;
     97 const struct solparser_anchor* entity0_anchor1;
     98 const struct solparser_anchor* entity0_entity0b_anchor0;
     99 const struct solparser_anchor* entity0_entity0b_entity0b;
    100 const struct solparser_geometry* geom;
    101 const struct solparser_x_pivot* x_pivot;
    102 const struct solparser_zx_pivot* zx_pivot;
    103 
    104 static void
    105 check_entity0
    106   (struct solparser* parser, const struct solparser_entity* entity0)
    107 {
    108   struct solparser_anchor_id anchor_id;
    109   struct solparser_entity_id entity_id;
    110   struct solparser_object_id obj_id;
    111   const struct solparser_entity *entity0a, *entity0b, *entity0c, *entity0d;
    112   const struct solparser_material_matte* matte;
    113   const struct solparser_material* mtl;
    114   const struct solparser_material_double_sided* mtl2;
    115   const struct solparser_object* obj;
    116   const struct solparser_shape* shape;
    117   const struct solparser_shape_cylinder* cylinder;
    118   double tmp[3];
    119 
    120   CHK(parser != NULL);
    121   CHK(entity0 != NULL);
    122 
    123   CHK(strcmp(str_cget(&entity0->name), "entity0") == 0);
    124   CHK(d3_eq(entity0->rotation, d3_splat(tmp, 0)) == 1);
    125   CHK(d3_eq(entity0->translation, d3_splat(tmp, 0)) == 1);
    126   CHK(entity0->type == SOLPARSER_ENTITY_GEOMETRY);
    127 
    128   geom = solparser_get_geometry(parser, entity0->data.geometry);
    129   CHK(solparser_geometry_get_objects_count(geom) == 1);
    130 
    131   obj_id = solparser_geometry_get_object(geom, 0);
    132   obj = solparser_get_object(parser, obj_id);
    133   CHK(d3_eq(obj->rotation, d3_splat(tmp, 0)) == 1);
    134   CHK(d3_eq(obj->translation, d3_splat(tmp, 0)) == 1);
    135 
    136   shape = solparser_get_shape(parser, obj->shape);
    137   CHK(shape->type == SOLPARSER_SHAPE_CYLINDER);
    138 
    139   cylinder = solparser_get_shape_cylinder(parser, shape->data.cylinder);
    140   CHK(cylinder->height == 10);
    141   CHK(cylinder->radius == 1);
    142   CHK(cylinder->nslices == 128);
    143 
    144   mtl2 = solparser_get_material_double_sided(parser, obj->mtl2);
    145   CHK(mtl2->front.i == mtl2->back.i);
    146 
    147   mtl = solparser_get_material(parser, mtl2->front);
    148   CHK(mtl->type == SOLPARSER_MATERIAL_MATTE);
    149 
    150   matte = solparser_get_material_matte(parser, mtl->data.matte);
    151   CHK(matte->reflectivity.type == SOLPARSER_MTL_DATA_REAL);
    152   CHK(matte->reflectivity.value.real == 0.5);
    153 
    154   CHK(solparser_entity_get_children_count(entity0) == 4);
    155   CHK(solparser_entity_get_anchors_count(entity0) == 3);
    156 
    157   anchor_id = solparser_entity_get_anchor(entity0, 0);
    158   entity0_anchor0 = solparser_get_anchor(parser, anchor_id);
    159   CHK(strcmp(str_cget(&entity0_anchor0->name), "anchor0") == 0);
    160   CHK(d3_eq(entity0_anchor0->position, d3(tmp, 1, 2, 3)) == 1);
    161 
    162   anchor_id = solparser_entity_get_anchor(entity0, 1);
    163   entity0_anchor1 = solparser_get_anchor(parser, anchor_id);
    164   CHK(strcmp(str_cget(&entity0_anchor1->name), "anchor1") == 0);
    165   CHK(d3_eq(entity0_anchor1->position, d3(tmp, 4, 5, 6)) == 1);
    166 
    167   entity_id = solparser_entity_get_child(entity0, 0);
    168   entity0a = solparser_get_entity(parser, entity_id);
    169   CHK(strcmp(str_cget(&entity0a->name), "entity0a") == 0);
    170   CHK(entity0a->type == SOLPARSER_ENTITY_GEOMETRY);
    171   CHK(entity0->data.geometry.i != entity0a->data.geometry.i);
    172   CHK(solparser_entity_get_anchors_count(entity0a) == 0);
    173   CHK(solparser_entity_get_children_count(entity0a) == 0);
    174 
    175   geom = solparser_get_geometry(parser, entity0a->data.geometry);
    176   CHK(solparser_geometry_get_objects_count(geom) == 1);
    177   obj_id = solparser_geometry_get_object(geom, 0);
    178   obj = solparser_get_object(parser, obj_id);
    179   shape = solparser_get_shape(parser, obj->shape);
    180   CHK(shape->type == SOLPARSER_SHAPE_CYLINDER);
    181   cylinder = solparser_get_shape_cylinder(parser, shape->data.cylinder);
    182   CHK(cylinder->height == 10);
    183   CHK(cylinder->radius == 1);
    184   CHK(cylinder->nslices == 16);
    185 
    186   entity_id = solparser_entity_get_child(entity0, 1);
    187   entity0b = solparser_get_entity(parser, entity_id);
    188   CHK(strcmp(str_cget(&entity0b->name), "entity0b") == 0);
    189   CHK(entity0b->type == SOLPARSER_ENTITY_GEOMETRY);
    190   CHK(entity0->data.geometry.i == entity0b->data.geometry.i);
    191   CHK(solparser_entity_get_anchors_count(entity0b) == 2);
    192   CHK(solparser_entity_get_children_count(entity0b) == 0);
    193 
    194   anchor_id = solparser_entity_get_anchor(entity0b, 0);
    195   entity0_entity0b_anchor0 = solparser_get_anchor(parser, anchor_id);
    196   CHK(strcmp(str_cget(&entity0_entity0b_anchor0->name), "anchor0") == 0);
    197   CHK(d3_eq(entity0_entity0b_anchor0->position, d3(tmp, 4, 5, 6)) == 1);
    198 
    199   anchor_id = solparser_entity_get_anchor(entity0b, 1);
    200   entity0_entity0b_entity0b = solparser_get_anchor(parser, anchor_id);
    201   CHK(strcmp(str_cget(&entity0_entity0b_entity0b->name), "entity0b") == 0);
    202   CHK(d3_eq(entity0_entity0b_entity0b->position, d3(tmp, 7, 8, 9)) == 1);
    203 
    204   entity_id = solparser_entity_get_child(entity0, 2);
    205   entity0c = solparser_get_entity(parser, entity_id);
    206   CHK(strcmp(str_cget(&entity0c->name), "entity0c") == 0);
    207   CHK(entity0c->type == SOLPARSER_ENTITY_GEOMETRY);
    208   CHK(entity0->data.geometry.i != entity0c->data.geometry.i);
    209   CHK(solparser_entity_get_anchors_count(entity0c) == 0);
    210   CHK(solparser_entity_get_children_count(entity0c) == 0);
    211 
    212   entity_id = solparser_entity_get_child(entity0, 3);
    213   entity0d = solparser_get_entity(parser, entity_id);
    214   CHK(strcmp(str_cget(&entity0d->name), "entity0d") == 0);
    215   CHK(entity0d->type == SOLPARSER_ENTITY_GEOMETRY);
    216   CHK(entity0->data.geometry.i != entity0d->data.geometry.i);
    217   CHK(solparser_entity_get_anchors_count(entity0d) == 0);
    218   CHK(solparser_entity_get_children_count(entity0d) == 0);
    219 }
    220 
    221 static void
    222 check_entity1
    223   (struct solparser* parser, const struct solparser_entity* entity1)
    224 {
    225   double tmp[3];
    226 
    227   CHK(parser != NULL);
    228   CHK(entity1 != NULL);
    229 
    230   CHK(strcmp(str_cget(&entity1->name), "entity1") == 0);
    231   CHK(entity1->type == SOLPARSER_ENTITY_X_PIVOT);
    232   CHK(solparser_entity_get_anchors_count(entity1) == 0);
    233   CHK(solparser_entity_get_children_count(entity1) == 0);
    234 
    235   x_pivot = solparser_get_x_pivot(parser, entity1->data.x_pivot);
    236   CHK(x_pivot != NULL);
    237   CHK(d3_eq(x_pivot->ref_point, d3(tmp, 1, 2, 3)) == 1);
    238   CHK(x_pivot->target.type == SOLPARSER_TARGET_ANCHOR);
    239 }
    240 
    241 static void
    242 check_entity2
    243   (struct solparser* parser, const struct solparser_entity* entity2)
    244 {
    245   double tmp[3];
    246 
    247   CHK(parser != NULL);
    248   CHK(entity2 != NULL);
    249 
    250   CHK(strcmp(str_cget(&entity2->name), "entity2") == 0);
    251   CHK(entity2->type == SOLPARSER_ENTITY_ZX_PIVOT);
    252   CHK(solparser_entity_get_anchors_count(entity2) == 0);
    253   CHK(solparser_entity_get_children_count(entity2) == 0);
    254 
    255   zx_pivot = solparser_get_zx_pivot(parser, entity2->data.zx_pivot);
    256   CHK(zx_pivot != NULL);
    257   CHK(zx_pivot->spacing == 1);
    258   CHK(d3_eq(zx_pivot->ref_point, d3(tmp, 1, 2, 3)) == 1);
    259   CHK(zx_pivot->target.type == SOLPARSER_TARGET_ANCHOR);
    260 }
    261 
    262 static void
    263 check_entity3
    264   (struct solparser* parser, const struct solparser_entity* entity3)
    265 {
    266   double tmp[3];
    267 
    268   CHK(parser != NULL);
    269   CHK(entity3 != NULL);
    270 
    271   CHK(strcmp(str_cget(&entity3->name), "entity3") == 0);
    272   CHK(entity3->type == SOLPARSER_ENTITY_X_PIVOT);
    273   CHK(solparser_entity_get_anchors_count(entity3) == 0);
    274   CHK(solparser_entity_get_children_count(entity3) == 0);
    275 
    276   x_pivot = solparser_get_x_pivot(parser, entity3->data.x_pivot);
    277   CHK(x_pivot != NULL);
    278   CHK(d3_eq(x_pivot->ref_point, d3(tmp, 4, 2, 3)) == 1);
    279   CHK(x_pivot->target.type == SOLPARSER_TARGET_ANCHOR);
    280 }
    281 
    282 int
    283 main(int argc, char** argv)
    284 {
    285   struct mem_allocator allocator;
    286   struct solparser* parser;
    287   struct solparser_entity_iterator it, it_end;
    288   const struct solparser_anchor* anchor;
    289   FILE* stream;
    290   size_t i;
    291   (void)argc, (void)argv;
    292 
    293   CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK);
    294   solparser_create(&allocator, &parser);
    295 
    296   stream = tmpfile();
    297   CHK(stream != NULL);
    298   i = 0;
    299   while(input[i]) {
    300     const size_t len = strlen(input[i]);
    301     CHK(fwrite(input[i], 1, len, stream) == len);
    302     ++i;
    303   }
    304   rewind(stream);
    305 
    306   CHK(solparser_setup(parser, NULL, stream) == RES_OK);
    307   CHK(solparser_load(parser) == RES_OK);
    308 
    309   solparser_entity_iterator_begin(parser, &it);
    310   solparser_entity_iterator_end(parser, &it_end);
    311   CHK(solparser_entity_iterator_eq(&it, &it_end) == 0);
    312 
    313   while(!solparser_entity_iterator_eq(&it, &it_end)) {
    314     struct solparser_entity_id entity_id;
    315     const struct solparser_entity* entity;
    316 
    317     entity_id = solparser_entity_iterator_get(&it);
    318     entity = solparser_get_entity(parser, entity_id);
    319 
    320     if(!strcmp(str_cget(&entity->name), "entity0")) {
    321       check_entity0(parser, entity);
    322     }
    323     else if (!strcmp(str_cget(&entity->name), "entity1")) {
    324       check_entity1(parser, entity);
    325     }
    326     else if(!strcmp(str_cget(&entity->name), "entity2")) {
    327       check_entity2(parser, entity);
    328     }
    329     else if (!strcmp(str_cget(&entity->name), "entity3")) {
    330       check_entity3(parser, entity);
    331     } else {
    332       FATAL("Unexpected entity name.\n");
    333     }
    334 
    335     solparser_entity_iterator_next(&it);
    336   }
    337 
    338   anchor = solparser_get_anchor(parser, x_pivot->target.data.anchor);
    339   CHK(anchor == entity0_entity0b_anchor0);
    340 
    341   anchor = solparser_find_anchor(parser, "entity0");
    342   CHK(anchor == NULL);
    343   anchor = solparser_find_anchor(parser, "entity0.anchor0");
    344   CHK(anchor == entity0_anchor0);
    345   anchor = solparser_find_anchor(parser, "entity0.anchor1");
    346   CHK(anchor == entity0_anchor1);
    347   anchor = solparser_find_anchor(parser, "entity0.entity0a.anchor0");
    348   CHK(anchor == NULL);
    349   anchor = solparser_find_anchor(parser, "entity0.entity0b.anchor0");
    350   CHK(anchor == entity0_entity0b_anchor0);
    351   anchor = solparser_find_anchor(parser, "entity0.entity0b.entity0b");
    352   CHK(anchor == entity0_entity0b_entity0b);
    353   anchor = solparser_find_anchor(parser, "entity1.entity0b.anchor1");
    354   CHK(anchor == NULL);
    355 
    356   CHK(solparser_load(parser) == RES_BAD_OP);
    357   solparser_ref_put(parser);
    358   fclose(stream);
    359 
    360   check_memory_allocator(&allocator);
    361   mem_shutdown_proxy_allocator(&allocator);
    362   CHK(mem_allocated_size() == 0);
    363   return 0;
    364 }