sadist_lib_unsteady_profile.c (6945B)
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 "sadist.h" 17 18 #include <stardis/stardis-prog-properties.h> 19 20 #include <rsys/cstr.h> 21 #include <rsys/rsys.h> 22 #include <rsys/mem_allocator.h> 23 24 #include <getopt.h> 25 26 /******************************************************************************* 27 * Helper functions 28 ******************************************************************************/ 29 static void 30 print_usage(FILE* stream, const char* name) 31 { 32 fprintf(stream, 33 "usage: %s [-h] [-k kx,ky,kz] [-m lambda,rho,cp] [-p A,B1,B2]\n", name); 34 } 35 36 static res_T 37 parse_periodicities(struct sadist_unsteady_profile* profile, const char* str) 38 { 39 double vec[3] = {0, 0, 0}; 40 size_t len = 0; 41 res_T res = RES_OK; 42 ASSERT(profile && str); 43 44 res = cstr_to_list_double(str, ',', vec, &len, 3); 45 if(res == RES_OK && len != 3) res = RES_BAD_ARG; 46 if(res != RES_OK) { 47 fprintf(stderr, 48 "%s:%d: unable to parse the periodicity parmeters `%s' -- %s\n", 49 __FILE__, __LINE__, str, res_to_cstr(res)); 50 goto error; 51 } 52 53 profile->kx = vec[0]; 54 profile->ky = vec[1]; 55 profile->kz = vec[2]; 56 57 exit: 58 return res; 59 error: 60 goto exit; 61 } 62 63 static res_T 64 parse_medium_properties 65 (struct sadist_unsteady_profile* profile, 66 const char* str) 67 { 68 double vec[3] = {0, 0, 0}; 69 size_t len = 0; 70 res_T res = RES_OK; 71 ASSERT(profile && str); 72 73 res = cstr_to_list_double(str, ',', vec, &len, 3); 74 if(res == RES_OK && len != 3) res = RES_BAD_ARG; 75 if(res != RES_OK) { 76 fprintf(stderr, 77 "%s:%d: unable to parse the medium properties `%s' -- %s\n", 78 __FILE__, __LINE__, str, res_to_cstr(res)); 79 goto error; 80 } 81 82 if(vec[0] <= 0) { /* Lambda */ 83 fprintf(stderr, 84 "%s:%d: invalid thermal conductivity %g. It cannot be negative or null\n", 85 __FILE__, __LINE__, vec[0]); 86 res = RES_BAD_ARG; 87 goto error; 88 } 89 90 if(vec[1] <= 0) { /* rho */ 91 fprintf(stderr, 92 "%s:%d: invalid volumic mass %g. It cannot be negative or null\n", 93 __FILE__, __LINE__, vec[1]); 94 res = RES_BAD_ARG; 95 goto error; 96 } 97 98 if(vec[2] <= 0) { /* cp */ 99 fprintf(stderr, 100 "%s:%d: invalid calorific capacity%g. It cannot be negative or null\n", 101 __FILE__, __LINE__, vec[2]); 102 res = RES_BAD_ARG; 103 goto error; 104 } 105 106 profile->lambda = vec[0]; 107 profile->rho = vec[1]; 108 profile->cp = vec[2]; 109 110 exit: 111 return res; 112 error: 113 goto exit; 114 } 115 116 static res_T 117 parse_scale_factors 118 (struct sadist_unsteady_profile* profile, 119 const char* str) 120 { 121 double vec[3] = {0, 0, 0}; 122 size_t len = 0; 123 res_T res = RES_OK; 124 ASSERT(profile && str); 125 126 res = cstr_to_list_double(str, ',', vec, &len, 3); 127 if(res == RES_OK && len != 3) res = RES_BAD_ARG; 128 if(res != RES_OK) { 129 fprintf(stderr, 130 "%s:%d: unable to parse the scale factors `%s' -- %s\n", 131 __FILE__, __LINE__, str, res_to_cstr(res)); 132 goto error; 133 } 134 135 if(vec[0] < 0 || vec[1] < 0 || vec[2] < 0) { 136 fprintf(stderr, 137 "%s:%d: invalid scale factors `%s'. They cannot be negative\n", 138 __FILE__, __LINE__, str); 139 res = RES_BAD_ARG; 140 goto error; 141 } 142 143 profile->A = vec[0]; 144 profile->B1 = vec[1]; 145 profile->B2 = vec[2]; 146 147 exit: 148 return res; 149 error: 150 goto exit; 151 } 152 153 static res_T 154 parse_args 155 (const struct stardis_description_create_context* ctx, 156 struct sadist_unsteady_profile* profile, 157 int argc, 158 char* argv[]) 159 { 160 int opt; 161 res_T res = RES_OK; 162 ASSERT(ctx && profile); 163 164 optind = 1; 165 while((opt = getopt(argc, argv, "hk:m:p:")) != -1) { 166 switch(opt) { 167 case 'h': 168 print_usage(stdout, ctx->name); 169 break; 170 case 'k': 171 res = parse_periodicities(profile, optarg); 172 if(res != RES_OK) goto error; 173 break; 174 case 'm': 175 res = parse_medium_properties(profile, optarg); 176 if(res != RES_OK) goto error; 177 break; 178 case 'p': 179 res = parse_scale_factors(profile, optarg); 180 if(res != RES_OK) goto error; 181 break; 182 default: res = RES_BAD_ARG; break; 183 } 184 } 185 186 exit: 187 return res; 188 error: 189 goto exit; 190 } 191 192 /******************************************************************************* 193 * Legal notices 194 ******************************************************************************/ 195 const char* 196 get_copyright_notice(void* data) 197 { 198 (void)data; /* Avoid "unused variable" warnings */ 199 return "Copyright (C) 2024 |Méso|Star> (contact@meso-star.com)"; 200 } 201 202 const char* 203 get_license_short(void* data) 204 { 205 (void)data; /* Avoid "unused variable" warnings */ 206 return "GNU GPL version 3 or later <http://www.gnu.org/licenses/>"; 207 } 208 209 const char* 210 get_license_text(void* data) 211 { 212 (void)data; /* Avoid "unused variable" warnings */ 213 return 214 "This is free software released under the GPL v3+ license: GNU GPL\n" 215 "version 3 or later. You are welcome to redistribute it under certain\n" 216 "conditions; refer to <http://www.gnu.org/licenses/> for details."; 217 } 218 219 220 /******************************************************************************* 221 * Create data 222 ******************************************************************************/ 223 void* 224 stardis_create_data 225 (const struct stardis_description_create_context* ctx, 226 void* libdata, 227 size_t argc, 228 char* argv[]) 229 { 230 struct sadist_unsteady_profile* profile = NULL; 231 res_T res = RES_OK; 232 (void)libdata; 233 234 profile = mem_alloc(sizeof(*profile)); 235 if(!profile) { 236 fprintf(stderr, "%s:%d: error allocating the unsteady profile.\n", 237 __FILE__, __LINE__); 238 goto error; 239 } 240 241 *profile = SADIST_UNSTEADY_PROFILE_NULL; 242 243 res = parse_args(ctx, profile, (int)argc, argv); 244 if(res != RES_OK) goto error; 245 246 exit: 247 return profile; 248 error: 249 if(profile) { 250 mem_rm(profile); 251 profile = NULL; 252 } 253 goto exit; 254 } 255 256 void 257 stardis_release_data(void* data) 258 { 259 ASSERT(data); 260 mem_rm(data); 261 } 262 263 /******************************************************************************* 264 * Boundary condition 265 ******************************************************************************/ 266 double 267 stardis_boundary_temperature 268 (const struct stardis_interface_fragment* frag, 269 void* data) 270 { 271 ASSERT(frag && data); 272 return sadist_unsteady_profile_temperature(data, frag->P, frag->time); 273 } 274 275 double* 276 stardis_t_range(void* data, double range[2]) 277 { 278 ASSERT(data && range); 279 (void)data; 280 range[0] = 0; 281 range[1] = INF; 282 return range; 283 }