star-gs

Literate program for a geometric sensitivity calculation
git clone git://git.meso-star.fr/star-gs.git
Log | Files | Refs | README | LICENSE

sgs.c (5077B)


      1 /* Copyright (C) 2021-2023 Centre National de la Recherche Scientifique
      2  * Copyright (C) 2021-2023 INSA Lyon
      3  * Copyright (C) 2021-2023 Institut Mines Télécom Albi-Carmaux
      4  * Copyright (C) 2021-2023 |Méso|Star> (contact@meso-star.com)
      5  * Copyright (C) 2021-2023 Institut Pascal
      6  * Copyright (C) 2021-2023 PhotonLyX (info@photonlyx.com)
      7  * Copyright (C) 2021-2023 Université de Lorraine
      8  * Copyright (C) 2021-2023 Université Paul Sabatier
      9  * Copyright (C) 2021-2023 Université Toulouse - Jean Jaurès
     10  *
     11  * This program is free software: you can redistribute it and/or modify
     12  * it under the terms of the GNU General Public License as published by
     13  * the Free Software Foundation, either version 3 of the License, or
     14  * (at your option) any later version.
     15  *
     16  * This program is distributed in the hope that it will be useful,
     17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     19  * GNU General Public License for more details.
     20  *
     21  * You should have received a copy of the GNU General Public License
     22  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     23 
     24 #include "sgs.h"
     25 #include "sgs_args.h"
     26 #include "sgs_c.h"
     27 #include "sgs_log.h"
     28 
     29 #include <star/s3d.h>
     30 
     31 #include <rsys/cstr.h>
     32 #include <rsys/double3.h>
     33 #include <rsys/mem_allocator.h>
     34 
     35 /*******************************************************************************
     36  * Helper functions
     37  ******************************************************************************/
     38 static res_T
     39 setup_scene(struct sgs* sgs, const struct sgs_args* args)
     40 {
     41   res_T res = RES_OK;
     42 
     43   res = sgs_geometry_box_create(sgs, &args->geom, &sgs->scene.geom);
     44   if(res != RES_OK) goto error;
     45 
     46   d3_sub(sgs->scene.D, args->geom.upper, args->geom.lower);
     47 
     48   /* Setup the 2D lower/upper bounds of the receiver */
     49   sgs->scene.recv_min[0] = sgs->scene.D[0]*1.0/8.0 + args->geom.lower[0];
     50   sgs->scene.recv_min[1] = sgs->scene.D[1]*3.0/8.0 + args->geom.lower[1];
     51   sgs->scene.recv_max[0] = sgs->scene.D[0]*3.0/8.0 + args->geom.lower[0];
     52   sgs->scene.recv_max[1] = sgs->scene.D[1]*5.0/8.0 + args->geom.lower[1];
     53 
     54 exit:
     55   return res;
     56 error:
     57   goto exit;
     58 }
     59 
     60 /*******************************************************************************
     61  * sgs API
     62  ******************************************************************************/
     63 res_T
     64 sgs_create
     65   (struct mem_allocator* mem_allocator,
     66    const struct sgs_args* args,
     67    struct sgs** out_sgs)
     68 {
     69   struct sgs* sgs = NULL;
     70   struct mem_allocator* allocator = NULL;
     71   res_T res = RES_OK;
     72   ASSERT(args && out_sgs);
     73 
     74   allocator = mem_allocator ? mem_allocator : &mem_default_allocator;
     75   sgs = MEM_CALLOC(allocator, 1, sizeof(struct sgs));
     76   if(!sgs) {
     77     fprintf(stderr, SGS_LOG_ERROR_PREFIX
     78       "Could not allocate the sgs data structure.\n");
     79     res = RES_MEM_ERR;
     80     goto error;
     81   }
     82   sgs->allocator = allocator;
     83   sgs->nrealisations = (size_t)args->nrealisations;
     84   sgs->nthreads = args->nthreads;
     85   sgs->dump_geometry = args->dump_geometry;
     86 
     87   setup_logger(sgs);
     88 
     89   res = s3d_device_create(&sgs->logger, sgs->allocator, 0, &sgs->s3d);
     90   if(res != RES_OK) {
     91     sgs_log_err(sgs, "Error creating the Star-3D device -- %s.\n",
     92       res_to_cstr(res));
     93     goto error;
     94   }
     95 
     96   res = setup_scene(sgs, args);
     97   if(res != RES_OK) goto error;
     98 
     99 exit:
    100   *out_sgs = sgs;
    101   return res;
    102 error:
    103   if(sgs) {
    104     sgs_release(sgs);
    105     sgs = NULL;
    106   }
    107   goto exit;
    108 }
    109 
    110 void
    111 sgs_release(struct sgs* sgs)
    112 {
    113   ASSERT(sgs);
    114 
    115   if(sgs->s3d) S3D(device_ref_put(sgs->s3d));
    116   if(sgs->scene.geom) sgs_geometry_ref_put(sgs->scene.geom);
    117   logger_release(&sgs->logger);
    118   MEM_RM(sgs->allocator, sgs);
    119 }
    120 
    121 struct mem_allocator*
    122 sgs_get_allocator(struct sgs* sgs)
    123 {
    124   ASSERT(sgs);
    125   return sgs->allocator;
    126 }
    127 
    128 struct s3d_device*
    129 sgs_get_s3d_device(struct sgs* sgs)
    130 {
    131   ASSERT(sgs);
    132   return sgs->s3d;
    133 }
    134 
    135 void
    136 sgs_fprint_copyright(FILE* stream)
    137 {
    138   ASSERT(stream);
    139   fprintf(stream,
    140 "Copyright (C) 2021-2023 Centre National de la Recherche Scientifique\n"
    141 "Copyright (C) 2021-2023 INSA Lyon\n"
    142 "Copyright (C) 2021-2023 Institut Mines Télécom Albi-Carmaux\n"
    143 "Copyright (C) 2021-2023 |Méso|Star> (contact@meso-star.com)\n"
    144 "Copyright (C) 2021-2023 Institut Pascal\n"
    145 "Copyright (C) 2021-2023 PhotonLyX (info@photonlyx.com)\n"
    146 "Copyright (C) 2021-2023 Université de Lorraine\n"
    147 "Copyright (C) 2021-2023 Université Paul Sabatier\n"
    148 "Copyright (C) 2021-2023 Université Toulouse - Jean Jaurès\n");
    149 }
    150 
    151 void
    152 sgs_fprint_license(FILE* stream)
    153 {
    154   ASSERT(stream);
    155   fprintf(stream,
    156 "Star Geometric Sensitivity is free software released under the GNU GPL\n"
    157 "license, version 3 or later. You are free to change or redistribute it\n"
    158 "under certain conditions <http://gnu.org/licenses/gpl.html>.\n");
    159 }
    160 
    161 res_T
    162 sgs_run(struct sgs* sgs)
    163 {
    164   res_T res = RES_OK;
    165   ASSERT(sgs);
    166 
    167   if(sgs->dump_geometry) {
    168     res = sgs_geometry_dump_vtk(sgs->scene.geom, stdout);
    169     if(res != RES_OK) goto error;
    170   } else {
    171     res = compute_sensitivity_translation(sgs);
    172     if(res != RES_OK) goto error;
    173   }
    174 
    175 exit:
    176   return res;
    177 error:
    178   goto exit;
    179 }
    180