stardis-test

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

commit 0d75c96055b25f1ed7d8eb2ef242cabc97c9c4af
parent afedf40670f072b16a614169c9247140a1b3e913
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Sun, 14 Apr 2024 10:06:46 +0200

Finalize conducto-radiation test

Add a programmable radiation environment whose temperature depends on
the direction of the ray along the X axis. Use it to describe the
radiative environment of the conducto-radiative test. The test then
calculates the steady-state temperature of a given position in the solid
that exchanges with this radiative environment.

Diffstat:
MMakefile | 16++++++++++------
Msrc/sadist_conducto_radiative.c | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
Asrc/sadist_lib_radenv_1d.c | 209+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/sadist_lib_spherical_source.c | 6++----
4 files changed, 307 insertions(+), 16 deletions(-)

diff --git a/Makefile b/Makefile @@ -25,6 +25,7 @@ TESTS =\ sadist_unsteady LIBS =\ + libsadist_radenv_1d.so\ libsadist_spherical_source.so\ libsadist_trilinear_profile.so\ libsadist_unsteady_profile.so\ @@ -56,6 +57,13 @@ test: $(TESTS) $(LIBS) ################################################################################ # Libraries ################################################################################ +src/sadist_lib_radenv_1d.o: config.mk src/sadist_lib_radenv_1d.c + $(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -c $(@:.o=.c) -o $@ + +libsadist_radenv_1d.so: config.mk src/sadist_lib_radenv_1d.o + $(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -o $@ src/sadist_lib_radenv_1d.o \ + $(LDFLAGS_SO) $(RSYS_LIBS) + src/sadist_lib_spherical_source.o: config.mk src/sadist_lib_spherical_source.c $(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -c $(@:.o=.c) -o $@ @@ -64,9 +72,7 @@ libsadist_spherical_source.so: config.mk src/sadist_lib_spherical_source.o $(LDFLAGS_SO) $(RSYS_LIBS) src/sadist_lib_trilinear_profile.o:\ - config.mk\ - src/sadist.h\ - src/sadist_lib_trilinear_profile.c + config.mk src/sadist.h src/sadist_lib_trilinear_profile.c $(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -c $(@:.o=.c) -o $@ libsadist_trilinear_profile.so: src/sadist_lib_trilinear_profile.o @@ -74,9 +80,7 @@ libsadist_trilinear_profile.so: src/sadist_lib_trilinear_profile.o $(LDFLAGS_SO) $(RSYS_LIBS) src/sadist_lib_unsteady_profile.o:\ - config.mk\ - src/sadist.h\ - src/sadist_lib_unsteady_profile.c + config.mk src/sadist.h src/sadist_lib_unsteady_profile.c $(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -c $(@:.o=.c) -o $@ libsadist_unsteady_profile.so: src/sadist_lib_unsteady_profile.o diff --git a/src/sadist_conducto_radiative.c b/src/sadist_conducto_radiative.c @@ -18,6 +18,7 @@ #include "sadist.h" #include <rsys/rsys.h> +#include <rsys/math.h> #include <errno.h> #include <stdio.h> @@ -27,7 +28,7 @@ /* * The scene is a solid cube whose +/-X faces exchange with the radiative * environment. All other faces are adiabatic. The radiative environment is set - * at 300K along the -X direction and 310K along the +X direction. + * at T0 along the -X direction and T1 along the +X direction. * * This test calculates the steady temperature at a given position of the probe * in the solid and validates it against its analytical solution. @@ -35,7 +36,7 @@ * //////(1,1,1) * +-------+ * ---> /' /| <--- - * 300 K ---> +-------+ | E=1 <--- 310 K + * T0 K ---> +-------+ | E=1 <--- T1 K * Y ---> E=1 | +.....|.+ <--- * | |, |/ * o--X +-------+ @@ -48,8 +49,24 @@ #define FILENAME_RADIATIVE "radiative_boundary.stl" #define FILENAME_SCENE "scene.txt" +#define EMISSIVITY 1.0 +#define TREF 300.0 /* [K] */ +#define T0 280.0 /* [K] */ +#define T1 350.0 /* [K] */ #define LAMBDA 0.1 +#define BOLTZMANN_CONSTANT 5.6696e-8 /* [W/m^2/K^4] */ +#define HR (4.0*BOLTZMANN_CONSTANT * TREF*TREF*TREF * EMISSIVITY) + +/* Probe position */ +#define PX 0.2 +#define PY 0.4 +#define PZ 0.6 + +/* The commands to be executed */ +#define COMMAND1 "stardis -V3 -p "STR(PX)","STR(PY)","STR(PZ)" -M "FILENAME_SCENE +#define COMMAND2 COMMAND1 " -a wos" + static const double vertices[] = { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, @@ -79,10 +96,16 @@ static void setup_scene(FILE* fp) { ASSERT(fp); - fprintf(fp, "SOLID cube "STR(LAMBDA)" 1 1 0.05 0 UNKNOWN 0 FRONT "FILENAME_CUBE"\n"); - fprintf(fp, "SOLID_FLUID_CONNECTION radiative 300 1 0 0 "FILENAME_RADIATIVE"\n"); - fprintf(fp, "SOLID_FLUID_CONNECTION adiabatic 0 0 0 0 "FILENAME_ADIABATIC"\n"); - fprintf(fp, "TRAD 310 300\n"); + fprintf(fp, "PROGRAM radenv_1d libsadist_radenv_1d.so\n"); + fprintf(fp, "FLUID environment 1 1 0 UNKNOWN BACK %s\n", FILENAME_CUBE); + fprintf(fp, "SOLID cube %g 1 1 0.05 0 UNKNOWN 0 FRONT %s\n", + LAMBDA, FILENAME_CUBE); + fprintf(fp, "SOLID_FLUID_CONNECTION radiative 300 %g 0 0 %s\n", + EMISSIVITY, FILENAME_RADIATIVE); + fprintf(fp, "SOLID_FLUID_CONNECTION adiabatic 0 0 0 0 %s\n", + FILENAME_ADIABATIC); + fprintf(fp, "TRAD_PROG radenv_1d PROG_PARAMS dummy -r %g,%g -t %g,%g\n", + TREF, TREF, T0, T1); } static int @@ -125,6 +148,61 @@ error: goto exit; } +static double +analatical_solution(void) +{ + const double tmp = LAMBDA / (2*LAMBDA + HR) * (T1 - T0); + const double Ts0 = T0 + tmp; + const double Ts1 = T1 - tmp; + const double ref = PX*Ts1 + (1 - PX)*Ts0; + return ref; +} + +static int +run(const char* command) +{ + FILE* output = NULL; + double E = 0; + double SE = 0; + double ref = 0; + int n = 0; + int err = 0; + int status = 0; + + printf("%s\n", command); + + if(!(output = popen(command, "r"))) { + fprintf(stderr, "Error executing stardis -- %s\n", strerror(errno)); + err = errno; + goto error; + } + + if((n = fscanf(output, "%lf %lf %*d %*d", &E, &SE)), n != 2 && n != EOF) { + fprintf(stderr, "Error reading the output stream -- %s\n", strerror(errno)); + err = errno; + goto error; + } + + /* Check command exit status */ + if((status=pclose(output)), output=NULL, status) { + err = WIFEXITED(status) ? WEXITSTATUS(status) : errno; + goto error; + } + + ref = analatical_solution(); + printf("T = %g ~ %g +/- %g\n", ref, E, SE); + if(!eq_eps(ref, E, SE*3)) { + err = 1; + goto error; + } + +exit: + if(output) pclose(output); + return err; +error: + goto exit; +} + /******************************************************************************* * The test ******************************************************************************/ @@ -135,6 +213,8 @@ main(int argc, char** argv) (void)argc, (void)argv; if((err = init())) goto error; + if((err = run(COMMAND1))) goto error; + if((err = run(COMMAND2))) goto error; exit: return err; diff --git a/src/sadist_lib_radenv_1d.c b/src/sadist_lib_radenv_1d.c @@ -0,0 +1,209 @@ +/* Copyright (C) 2024 |Méso|Star> (contact@meso-star.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include <stardis/stardis-prog-properties.h> + +#include <rsys/cstr.h> +#include <rsys/mem_allocator.h> + +#include <getopt.h> + +/* Define a radiative environment whose temperature depend on the X direction */ +struct radenv { + double temperatures[2]; /* [K] */ + double reference_temperatures[2]; /* [K] */ +}; +#define RADENV_NULL__ {{0,0}, {0,0}} +static const struct radenv RADENV_NULL = RADENV_NULL__; + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static void +print_usage(FILE* stream, const char* name) +{ + ASSERT(name); + fprintf(stream, "usage: %s [-h] [-r t0,t1] [-t t0,t1]\n", name); +} + +static res_T +parse_temps(const char* str, double temps[2]) +{ + size_t len = 0; + res_T res = RES_OK; + ASSERT(str && temps); + + res = cstr_to_list_double(str, ',', temps, &len, 2); + if(res != RES_OK && len != 2) res = RES_BAD_ARG; + if(res != RES_OK) goto error; + +exit: + return res; +error: + goto exit; +} + +static res_T +parse_trefs(const char* str, double trefs[2]) +{ + size_t len = 0; + res_T res = RES_OK; + ASSERT(str && trefs); + + res = cstr_to_list_double(str, ',', trefs, &len, 2); + if(res != RES_OK && len != 2) res = RES_BAD_ARG; + if(res != RES_OK) goto error; + + if(trefs[0] < 0 || trefs[1] < 0) { + fprintf(stderr, "A reference temperature cannot be negative.\n"); + res = RES_BAD_ARG; + goto error; + } + +exit: + return res; +error: + goto exit; +} + +static res_T +parse_args + (const struct stardis_description_create_context* ctx, + struct radenv* radenv, + const int argc, + char* argv[]) +{ + int opt = 0; + res_T res = RES_OK; + + /* Check pre-conditions */ + ASSERT(ctx && radenv); + + optind = 1;; + while((opt = getopt(argc, argv, "t:r:")) != -1) { + switch(opt) { + case 'h': + print_usage(stdout, ctx->name); + break; + case 'r': + res = parse_trefs(optarg, radenv->reference_temperatures); + break; + case 't': + res = parse_temps(optarg, radenv->temperatures); + break; + default: res = RES_BAD_ARG; break; + } + if(res != RES_OK) { + if(optarg) { + fprintf(stderr, "%s: invalid option argument '%s' -- '%c'\n", + ctx->name, optarg, opt); + } + goto error; + } + } + +exit: + return res; +error: + goto exit; +} + +/******************************************************************************* + * Create data + ******************************************************************************/ +void* +stardis_create_data + (const struct stardis_description_create_context *ctx, + void* libdata, + size_t argc, + char* argv[]) +{ + struct radenv* radenv = NULL; + res_T res = RES_OK; + (void)libdata; + + radenv = mem_alloc(sizeof(*radenv)); + if(!radenv) { + fprintf(stderr, "%s:%d: error allocating the radiative environment.\n", + __FILE__, __LINE__); + goto error; + } + *radenv = RADENV_NULL; + + res = parse_args(ctx, radenv, (int)argc, argv); + if(res != RES_OK) goto error; + +exit: + return radenv; +error: + if(radenv) { + mem_rm(radenv); + radenv = NULL; + } + goto exit; +} + +void +stardis_release_data(void* data) +{ + ASSERT(data); + mem_rm(data); +} + +/******************************************************************************* + * External source + ******************************************************************************/ +double /* [K] */ +stardis_radiative_env_temperature(const double dir[3], void* data) +{ + struct radenv* radenv = data; + ASSERT(radenv); + return radenv->temperatures[dir[0] > 0]; /* [K] */ +} + +double /* [K] */ +stardis_radiative_env_reference_temperature(const double dir[3], void* data) +{ + struct radenv* radenv = data; + ASSERT(radenv); + return radenv->reference_temperatures[dir[0] > 0]; /* [K] */ +} + +/******************************************************************************* + * Legal notices + ******************************************************************************/ +const char* +get_copyright_notice(void* data) +{ + (void)data; /* Avoid "unused variable" warnings */ + return "Copyright (C) 2024 |Méso|Star> (contact@meso-star.com)"; +} + +const char* +get_license_short(void* data) +{ + (void)data; /* Avoid "unused variable" warnings */ + return "GNU GPL version 3 or later <http://www.gnu.org/licenses/>"; +} + +const char* +get_license_text(void* data) +{ + (void)data; /* Avoid "unused variable" warnings */ + return + "This is free software released under the GPL v3+ license: GNU GPL\n" + "version 3 or later. You are welcome to redistribute it under certain\n" + "conditions; refer to <http://www.gnu.org/licenses/> for details."; +} diff --git a/src/sadist_lib_spherical_source.c b/src/sadist_lib_spherical_source.c @@ -15,12 +15,10 @@ #include <stardis/stardis-prog-properties.h> -#include <stdarg.h> /* va_list */ -#include <getopt.h> - #include <rsys/cstr.h> #include <rsys/mem_allocator.h> +#include <stdarg.h> /* va_list */ #include <getopt.h> struct source { @@ -66,7 +64,7 @@ parse_args res = cstr_to_double(optarg, &source->power); if(res == RES_OK && source->power < 0) res = RES_BAD_ARG; break; - default: res = RES_BAD_ARG; + default: res = RES_BAD_ARG; break; } if(res != RES_OK) { if(optarg) {