sadist_lib_solid_fluid.c (6240B)
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 an interface between a solid and a fluid */ 24 struct solid_fluid { 25 double emissivity; 26 double specular_fraction; 27 double convection_coef; /* [W/K/m^2] */ 28 double tref; /* Reference temperature [K] */ 29 }; 30 #define SOLID_FLUID_NULL__ {0,0,0,0} 31 static const struct solid_fluid SOLID_FLUID_NULL = SOLID_FLUID_NULL__; 32 33 /****************************************************************************** 34 * Helper functions 35 ******************************************************************************/ 36 static void 37 print_usage(FILE* stream, const char* name) 38 { 39 ASSERT(name); 40 fprintf(stream, "usage: %s [-h] [-c convection_coef] [-e emissivity]\n" 41 "\t[-r reference_temperature] [-s specular_fraction]\n", name); 42 } 43 44 static res_T 45 parse_args 46 (const struct stardis_description_create_context* ctx, 47 struct solid_fluid* sf, 48 const int argc, 49 char* argv[]) 50 { 51 int opt = 0; 52 res_T res = RES_OK; 53 54 /* Check pre-conditions */ 55 ASSERT(ctx && sf); 56 57 optind = 1; 58 while((opt = getopt(argc, argv, "c:e:hr:s:")) != -1) { 59 switch(opt) { 60 case 'c': 61 res = cstr_to_double(optarg, &sf->convection_coef); 62 if(res == RES_OK && sf->convection_coef < 0) res = RES_BAD_ARG; 63 break; 64 case 'e': 65 res = cstr_to_double(optarg, &sf->emissivity); 66 if(res == RES_OK && (sf->emissivity < 0 || sf->emissivity > 1)) 67 res = RES_BAD_ARG; 68 break; 69 case 'h': 70 print_usage(stdout, ctx->name); 71 break; 72 case 'r': 73 res = cstr_to_double(optarg, &sf->tref); 74 if(res != RES_OK && sf->tref < 0) res = RES_BAD_ARG; 75 break; 76 case 's': 77 res = cstr_to_double(optarg, &sf->specular_fraction); 78 if(res == RES_OK 79 && (sf->specular_fraction < 0 || sf->specular_fraction > 1)) { 80 res = RES_BAD_ARG; 81 } 82 break; 83 default: res = RES_BAD_ARG; break; 84 } 85 if(res != RES_OK) { 86 if(optarg) { 87 fprintf(stderr, "%s: invalid option argument '%s' -- '%c'\n", 88 ctx->name, optarg, opt); 89 } 90 goto error; 91 } 92 } 93 exit: 94 return res; 95 error: 96 goto exit; 97 } 98 99 /****************************************************************************** 100 * Create data 101 ******************************************************************************/ 102 void* 103 stardis_create_data 104 (const struct stardis_description_create_context *ctx, 105 void* libdata, 106 size_t argc, 107 char* argv[]) 108 { 109 struct solid_fluid* solid_fluid = NULL; 110 res_T res = RES_OK; 111 (void)libdata; 112 113 solid_fluid = mem_alloc(sizeof(*solid_fluid)); 114 if(!solid_fluid) { 115 fprintf(stderr, "%s:%d: error allocating the solid fluid connection.\n", 116 __FILE__, __LINE__); 117 goto error; 118 } 119 *solid_fluid = SOLID_FLUID_NULL; 120 121 res = parse_args(ctx, solid_fluid, (int)argc, argv); 122 if(res != RES_OK) goto error; 123 124 exit: 125 return solid_fluid; 126 error: 127 if(solid_fluid) { 128 mem_rm(solid_fluid); 129 solid_fluid = NULL; 130 } 131 goto exit; 132 } 133 134 void 135 stardis_release_data(void* data) 136 { 137 ASSERT(data); 138 mem_rm(data); 139 } 140 141 /****************************************************************************** 142 * Solid fluid connection 143 ******************************************************************************/ 144 double 145 stardis_emissivity 146 (const struct stardis_interface_fragment* frag, 147 const unsigned source_id, 148 void* data) 149 { 150 const struct solid_fluid* solid_fluid = data; 151 ASSERT(solid_fluid); 152 (void)source_id, (void)frag; 153 return solid_fluid->emissivity; 154 } 155 156 double 157 stardis_specular_fraction 158 (const struct stardis_interface_fragment* frag, 159 const unsigned source_id, 160 void* data) 161 { 162 const struct solid_fluid* solid_fluid = data; 163 ASSERT(solid_fluid); 164 (void)source_id, (void)frag; 165 return solid_fluid->specular_fraction; 166 } 167 168 double 169 stardis_convection_coefficient 170 (const struct stardis_interface_fragment* frag, 171 void* data) 172 { 173 const struct solid_fluid* solid_fluid = data; 174 ASSERT(solid_fluid); 175 (void)frag; 176 return solid_fluid->convection_coef; 177 } 178 179 double 180 stardis_max_convection_coefficient(void* data) 181 { 182 const struct solid_fluid* solid_fluid = data; 183 ASSERT(solid_fluid); 184 return solid_fluid->convection_coef; 185 } 186 187 double 188 stardis_reference_temperature 189 (const struct stardis_interface_fragment* frag, 190 void* data) 191 { 192 const struct solid_fluid* solid_fluid = data; 193 ASSERT(solid_fluid); 194 (void)frag; 195 return solid_fluid->tref;; 196 } 197 198 double* 199 stardis_t_range(void* data, double range[2]) 200 { 201 const struct solid_fluid* solid_fluid = data; 202 ASSERT(solid_fluid); 203 range[0] = range[1] = solid_fluid->tref; 204 return range; 205 } 206 207 /******************************************************************************* 208 * Legal notices 209 ******************************************************************************/ 210 const char* 211 get_copyright_notice(void* data) 212 { 213 (void)data; /* Avoid "unused variable" warnings */ 214 return "Copyright (C) 2024 |Méso|Star> (contact@meso-star.com)"; 215 } 216 217 const char* 218 get_license_short(void* data) 219 { 220 (void)data; /* Avoid "unused variable" warnings */ 221 return "GNU GPL version 3 or later <http://www.gnu.org/licenses/>"; 222 } 223 224 const char* 225 get_license_text(void* data) 226 { 227 (void)data; /* Avoid "unused variable" warnings */ 228 return 229 "This is free software released under the GPL v3+ license: GNU GPL\n" 230 "version 3 or later. You are welcome to redistribute it under certain\n" 231 "conditions; refer to <http://www.gnu.org/licenses/> for details."; 232 }