stardis

Perform coupled heat transfer calculations
git clone git://git.meso-star.fr/stardis.git
Log | Files | Refs | README | LICENSE

stardis-solid.c (6517B)


      1 /* Copyright (C) 2018-2025 |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-solid.h"
     17 #include "stardis-app.h"
     18 
     19 #include <sdis.h>
     20 
     21 #include <rsys/mem_allocator.h>
     22 
     23 #include <limits.h>
     24 
     25 /*******************************************************************************
     26  * Local Functions
     27  ******************************************************************************/
     28 
     29 static double
     30 solid_get_calorific_capacity
     31   (const struct sdis_rwalk_vertex* vtx,
     32    struct sdis_data* data)
     33 {
     34   const struct solid* const* solid_props = sdis_data_cget(data);
     35   (void)vtx;
     36   return (*solid_props)->cp;
     37 }
     38 
     39 static double
     40 solid_get_thermal_conductivity
     41   (const struct sdis_rwalk_vertex* vtx,
     42    struct sdis_data* data)
     43 {
     44   const struct solid* const* solid_props = sdis_data_cget(data);
     45   (void)vtx;
     46   return (*solid_props)->lambda;
     47 }
     48 
     49 static double
     50 solid_get_volumic_mass
     51   (const struct sdis_rwalk_vertex* vtx,
     52    struct sdis_data* data)
     53 {
     54   const struct solid* const* solid_props = sdis_data_cget(data);
     55   (void)vtx;
     56   return (*solid_props)->rho;
     57 }
     58 
     59 static double
     60 solid_get_delta
     61   (const struct sdis_rwalk_vertex* vtx,
     62    struct sdis_data* data)
     63 {
     64   const struct solid* const* solid_props = sdis_data_cget(data);
     65   (void)vtx;
     66   return (*solid_props)->delta;
     67 }
     68 
     69 #if Stardis_VERSION_MINOR == 3
     70 static double
     71 solid_get_delta_boundary
     72   (const struct sdis_rwalk_vertex* vtx,
     73    struct sdis_data* data)
     74 {
     75   const struct solid* const* solid_props = sdis_data_cget(data);
     76   return (*solid_props)->delta;
     77 }
     78 #endif
     79 
     80 static double
     81 solid_get_temperature
     82   (const struct sdis_rwalk_vertex* vtx,
     83    struct sdis_data* data)
     84 {
     85   const struct solid* const* solid_props = sdis_data_cget(data);
     86   if(SDIS_TEMPERATURE_IS_KNOWN((*solid_props)->imposed_temperature))
     87     /* If there is an imposed temp, it is imposed regardless of time */
     88     return (*solid_props)->imposed_temperature;
     89   if(vtx->time <= (*solid_props)->t0) {
     90     /* If time is <= t0: use tinit */
     91     if(SDIS_TEMPERATURE_IS_UNKNOWN((*solid_props)->tinit)) {
     92       if(str_is_empty(&(*solid_props)->name)) {
     93         FATAL("solid_get_temperature: getting undefined Tinit\n");
     94       } else {
     95         VFATAL("solid_get_temperature: getting undefined Tinit (solid '%s')\n",
     96           ARG1(str_cget(&(*solid_props)->name)));
     97       }
     98     }
     99     return (*solid_props)->tinit;
    100   }
    101   return SDIS_TEMPERATURE_NONE;
    102 }
    103 
    104 static double
    105 solid_get_power
    106   (const struct sdis_rwalk_vertex* vtx,
    107    struct sdis_data* data)
    108 {
    109   const struct solid* const* solid_props = sdis_data_cget(data);
    110   (void)vtx;
    111   return (*solid_props)->vpower;
    112 }
    113 
    114 /*******************************************************************************
    115  * Public Functions
    116  ******************************************************************************/
    117 
    118 res_T
    119 create_solver_solid
    120   (struct stardis* stardis,
    121    const struct solid* solid_props)
    122 {
    123   res_T res = RES_OK;
    124   struct sdis_solid_shader solid_shader = SDIS_SOLID_SHADER_NULL;
    125   struct sdis_data* data = NULL;
    126   const struct solid** props;
    127   /* Could be less restrictive if green output included positions/dates */
    128 
    129   ASSERT(stardis && solid_props);
    130   solid_shader.calorific_capacity = solid_get_calorific_capacity;
    131   solid_shader.thermal_conductivity = solid_get_thermal_conductivity;
    132   solid_shader.volumic_mass = solid_get_volumic_mass;
    133   solid_shader.delta = solid_get_delta;
    134 #if Stardis_VERSION_MINOR == 3
    135   solid_shader.delta_boundary = solid_get_delta_boundary;
    136 #endif
    137   solid_shader.temperature = solid_get_temperature;
    138   solid_shader.t0 = stardis->initial_time;
    139   ERR(sdis_data_create(stardis->dev, sizeof(struct solid*), ALIGNOF(struct solid*),
    140     NULL, &data));
    141 
    142   props = sdis_data_get(data); /* Fetch the allocated memory space */
    143   *props = solid_props;
    144   if(solid_props->vpower != 0) solid_shader.volumic_power = solid_get_power;
    145   if(solid_props->solid_id >= darray_media_ptr_size_get(&stardis->media)) {
    146     ERR(darray_media_ptr_resize(&stardis->media, solid_props->solid_id + 1));
    147   }
    148   ASSERT(!darray_media_ptr_data_get(&stardis->media)[solid_props->solid_id]);
    149   ERR(sdis_solid_create(stardis->dev, &solid_shader, data,
    150     darray_media_ptr_data_get(&stardis->media) + solid_props->solid_id));
    151 
    152 end:
    153   if(data) SDIS(data_ref_put(data));
    154   return res;
    155 error:
    156   goto end;
    157 }
    158 
    159 res_T
    160 init_solid(struct mem_allocator* allocator, struct solid** dst)
    161 {
    162   res_T res = RES_OK;
    163   int str_initialized = 0;
    164   ASSERT(allocator && dst && *dst == NULL);
    165   *dst = MEM_ALLOC(allocator, sizeof(struct solid));
    166   if(! *dst) {
    167     res = RES_MEM_ERR;
    168     goto error;
    169   }
    170   str_init(allocator, &(*dst)->name);
    171   (*dst)->lambda = 1;
    172   (*dst)->rho = 1;
    173   (*dst)->cp = 1;
    174   (*dst)->delta = 1;
    175   (*dst)->tinit = SDIS_TEMPERATURE_NONE;
    176   (*dst)->imposed_temperature = SDIS_TEMPERATURE_NONE;
    177   (*dst)->vpower = SDIS_VOLUMIC_POWER_NONE;
    178   (*dst)->t0 = 0;
    179   (*dst)->is_outside = 0;
    180   (*dst)->is_green = 0;
    181   (*dst)->desc_id = UINT_MAX;
    182   (*dst)->solid_id = UINT_MAX;
    183 end:
    184   return res;
    185 error:
    186   if(str_initialized) str_release(&(*dst)->name);
    187   if(*dst) MEM_RM(allocator, *dst);
    188   goto end;
    189 }
    190 
    191 void
    192 release_solid
    193   (struct solid* solid,
    194    struct mem_allocator* allocator)
    195 {
    196   ASSERT(solid && allocator);
    197   str_release(&solid->name);
    198   MEM_RM(allocator, solid);
    199 }
    200 
    201 res_T
    202 str_print_solid(struct str* str, const struct solid* s)
    203 {
    204   res_T res = RES_OK;
    205   ASSERT(str && s);
    206   ERR(str_append_printf(str, "Solid '%s': lambda=%g rho=%g cp=%g delta=%g",
    207     str_cget(&s->name), s->lambda, s->rho, s->cp, s->delta));
    208   if(s->vpower != 0) {
    209     ERR(str_append_printf(str, " VPower=%g", s->vpower));
    210   }
    211   if(SDIS_TEMPERATURE_IS_KNOWN(s->tinit)) {
    212     ERR(str_append_printf(str, " Tinit=%g", s->tinit));
    213   }
    214   if(SDIS_TEMPERATURE_IS_KNOWN(s->imposed_temperature)) {
    215     ERR(str_append_printf(str, " Temp=%g", s->imposed_temperature));
    216   }
    217   ERR(str_append_printf(str, " (it is medium %u)", s->solid_id));
    218 end:
    219   return res;
    220 error:
    221   goto end;
    222 }