stardis-test

Test Stardis behaviors
git clone git://git.meso-star.fr/stardis-test.git
Log | Files | Refs | README | LICENSE

sadist_lib_radenv_1d.c (5736B)


      1 /* Copyright (C) 2024 |Méso|Star> (contact@meso-star.com)
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     15 
     16 #include <stardis/stardis-prog-properties.h>
     17 
     18 #include <rsys/cstr.h>
     19 #include <rsys/mem_allocator.h>
     20 
     21 #include <getopt.h>
     22 
     23 /* Define a radiative environment whose temperature depend on the X direction */
     24 struct radenv {
     25   double temperatures[2]; /* [K] */
     26   double reference_temperatures[2]; /* [K] */
     27 };
     28 #define RADENV_NULL__ {{0,0}, {0,0}}
     29 static const struct radenv RADENV_NULL = RADENV_NULL__;
     30 
     31 /*******************************************************************************
     32  * Helper functions
     33  ******************************************************************************/
     34 static void
     35 print_usage(FILE* stream, const char* name)
     36 {
     37   ASSERT(name);
     38   fprintf(stream, "usage: %s [-h] [-r t0,t1] [-t t0,t1]\n", name);
     39 }
     40 
     41 static res_T
     42 parse_temps(const char* str, double temps[2])
     43 {
     44   size_t len = 0;
     45   res_T res = RES_OK;
     46   ASSERT(str && temps);
     47 
     48   res = cstr_to_list_double(str, ',', temps, &len, 2);
     49   if(res != RES_OK && len != 2) res = RES_BAD_ARG;
     50   if(res != RES_OK) goto error;
     51 
     52 exit:
     53   return res;
     54 error:
     55   goto exit;
     56 }
     57 
     58 static res_T
     59 parse_trefs(const char* str, double trefs[2])
     60 {
     61   size_t len = 0;
     62   res_T res = RES_OK;
     63   ASSERT(str && trefs);
     64 
     65   res = cstr_to_list_double(str, ',', trefs, &len, 2);
     66   if(res != RES_OK && len != 2) res = RES_BAD_ARG;
     67   if(res != RES_OK) goto error;
     68 
     69   if(trefs[0] < 0 || trefs[1] < 0) {
     70     fprintf(stderr, "A reference temperature cannot be negative.\n");
     71     res = RES_BAD_ARG;
     72     goto error;
     73   }
     74 
     75 exit:
     76   return res;
     77 error:
     78   goto exit;
     79 }
     80 
     81 static res_T
     82 parse_args
     83   (const struct stardis_description_create_context* ctx,
     84    struct radenv* radenv,
     85    const int argc,
     86    char* argv[])
     87 {
     88   int opt = 0;
     89   res_T res = RES_OK;
     90 
     91   /* Check pre-conditions */
     92   ASSERT(ctx && radenv);
     93 
     94   optind = 1;
     95   while((opt = getopt(argc, argv, "ht:r:")) != -1) {
     96     switch(opt) {
     97       case 'h':
     98         print_usage(stdout, ctx->name);
     99         break;
    100       case 'r':
    101         res = parse_trefs(optarg, radenv->reference_temperatures);
    102         break;
    103       case 't':
    104         res = parse_temps(optarg, radenv->temperatures);
    105         break;
    106       default: res = RES_BAD_ARG; break;
    107     }
    108     if(res != RES_OK) {
    109       if(optarg) {
    110         fprintf(stderr, "%s: invalid option argument '%s' -- '%c'\n",
    111           ctx->name, optarg, opt);
    112       }
    113       goto error;
    114     }
    115   }
    116 
    117 exit:
    118   return res;
    119 error:
    120   goto exit;
    121 }
    122 
    123 /*******************************************************************************
    124  * Create data
    125  ******************************************************************************/
    126 void*
    127 stardis_create_data
    128   (const struct stardis_description_create_context *ctx,
    129    void* libdata,
    130    size_t argc,
    131    char* argv[])
    132 {
    133   struct radenv* radenv = NULL;
    134   res_T res = RES_OK;
    135   (void)libdata;
    136 
    137   radenv = mem_alloc(sizeof(*radenv));
    138   if(!radenv) {
    139     fprintf(stderr, "%s:%d: error allocating the radiative environment.\n",
    140       __FILE__, __LINE__);
    141     goto error;
    142   }
    143   *radenv = RADENV_NULL;
    144 
    145   res = parse_args(ctx, radenv, (int)argc, argv);
    146   if(res != RES_OK) goto error;
    147 
    148 exit:
    149   return radenv;
    150 error:
    151   if(radenv) {
    152     mem_rm(radenv);
    153     radenv = NULL;
    154   }
    155   goto exit;
    156 }
    157 
    158 void
    159 stardis_release_data(void* data)
    160 {
    161   ASSERT(data);
    162   mem_rm(data);
    163 }
    164 
    165 /*******************************************************************************
    166  * Radiative environment
    167  ******************************************************************************/
    168 double /* [K] */
    169 stardis_radiative_env_temperature
    170   (const double time, /* [s] */
    171    const double dir[3],
    172    void* data)
    173 {
    174   struct radenv* radenv = data;
    175   (void)time;
    176   ASSERT(radenv);
    177   return radenv->temperatures[dir[0] > 0]; /* [K] */
    178 }
    179 
    180 double /* [K] */
    181 stardis_radiative_env_reference_temperature
    182   (const double time, /* [s] */
    183    const double dir[3],
    184    void* data)
    185 {
    186   struct radenv* radenv = data;
    187   (void)time;
    188   ASSERT(radenv);
    189   return radenv->reference_temperatures[dir[0] > 0]; /* [K] */
    190 }
    191 
    192 double*
    193 stardis_t_range(void* data, double t_range[2])
    194 {
    195   struct radenv* radenv = data;
    196   const double* trefs = NULL;
    197   ASSERT(radenv);
    198 
    199   trefs = radenv->reference_temperatures;
    200   t_range[0] = MMIN(trefs[0], trefs[1]);
    201   t_range[1] = MMAX(trefs[0], trefs[1]);
    202   return t_range;
    203 }
    204 
    205 /*******************************************************************************
    206  * Legal notices
    207  ******************************************************************************/
    208 const char*
    209 get_copyright_notice(void* data)
    210 {
    211   (void)data; /* Avoid "unused variable" warnings */
    212   return "Copyright (C) 2024 |Méso|Star> (contact@meso-star.com)";
    213 }
    214 
    215 const char*
    216 get_license_short(void* data)
    217 {
    218   (void)data; /* Avoid "unused variable" warnings */
    219   return "GNU GPL version 3 or later <http://www.gnu.org/licenses/>";
    220 }
    221 
    222 const char*
    223 get_license_text(void* data)
    224 {
    225   (void)data; /* Avoid "unused variable" warnings */
    226   return
    227     "This is free software released under the GPL v3+ license: GNU GPL\n"
    228     "version 3 or later. You are welcome to redistribute it under certain\n"
    229     "conditions; refer to <http://www.gnu.org/licenses/> for details.";
    230 }