atrstm

Load and structure a combustion gas mixture
git clone git://git.meso-star.fr/atrstm.git
Log | Files | Refs | README | LICENSE

commit 69180011fa0340d48d68a520c853c1f2d9490db0
parent fcb825b1c37a6372f04e513140b8622b19b2b358
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Thu, 14 Jan 2021 18:12:19 +0100

Add the test_atrstm program

Diffstat:
Mcmake/CMakeLists.txt | 25++++++++++++-------------
Asrc/test_atrstm.c | 234+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 246 insertions(+), 13 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -95,19 +95,18 @@ rcmake_setup_devel(atrstm AtrGM ${VERSION} astoria/atrstm_version.h) # Add tests ################################################################################ if(NOT NO_TEST) - # function(build_test _name) - # add_executable(${_name} - # ${ATRSTM_SOURCE_DIR}/${_name}.c - # ${ATRSTM_SOURCE_DIR}/test_atrstm_utils.h) - # target_link_libraries(${_name} atrstm RSys ${ARGN}) - # endfunction() - # - # function(new_test _name) - # build_test(${_name} ${ARGN}) - # add_test(${_name} ${_name}) - # endfunction() - # - # new_test(test_atrstm) + function(build_test _name) + add_executable(${_name} + ${ATRSTM_SOURCE_DIR}/${_name}.c) + target_link_libraries(${_name} atrstm RSys ${ARGN}) + endfunction() + + function(new_test _name) + build_test(${_name} ${ARGN}) + add_test(${_name} ${_name}) + endfunction() + + build_test(test_atrstm) endif() ################################################################################ diff --git a/src/test_atrstm.c b/src/test_atrstm.c @@ -0,0 +1,234 @@ +/* Copyright (C) 2020 CNRS + * + * 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 "atrstm.h" + +#include <rsys/cstr.h> +#include <rsys/mem_allocator.h> +#include <rsys/rsys.h> + +#include <getopt.h> +#include <stdio.h> + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static void +print_help(const char* cmd) +{ + ASSERT(cmd); + printf( +"Usage: %s <option>...\n" +"Test the Astoria: Semi-Transparent Medium library.\n", + cmd); + printf("\n"); + printf( +" -f FRACTAL_DIM fractal dimension. Its default value is %g.\n", + ATRSTM_ARGS_DEFAULT.fractal_dimension); + printf( +" -g PREFACTOR gyration radius prefactor. Its default value is %g.\n", + ATRSTM_ARGS_DEFAULT.gyration_radius_prefactor); + printf( +" -h display this help and exit.\n"); + printf( +" -m TETRAHEDRA path toward the volumetric mesh.\n"); + printf( +" -N precompute the tetrahedra normals.\n"); + printf( +" -n NAME name of the medium. Default is \"%s\".\n", + ATRSTM_ARGS_DEFAULT.name); + printf( +" -p THERMOPROPS path toward the thermodynamic properties.\n"); + printf( +" -r REFRACT_ID path toward the per wavelength refractive\n" +" indices.\n"); + printf( +" -T THRESHOLD optical thickness used as threshold during the octree\n" +" building. By default its value is %g.\n", + ATRSTM_ARGS_DEFAULT.optical_thickness); + printf( +" -t NTHREADS hint on the number of threads to use. By default use\n" +" as many threads as CPU cores.\n"); + printf( +" -V X,Y,Z maximum definition of the acceleration grids\n" +" along the 3 axis. Its default value is [%u, %u, %u].\n", + SPLIT3(ATRSTM_ARGS_DEFAULT.grid_max_definition)); + printf( +" -v make the program verobse.\n"); + printf( +" -w WAVELENGTH shortwave wavelength to use, in nanometer.\n" +" By default it is set to %g nm\n", + ATRSTM_ARGS_DEFAULT.wlen_range[0]); + printf("\n"); + printf( +"Copyright (C) 2020 CNRS.\n" +"This is free software released under the GNU GPL license, version 3 or\n" +"later. You are free to change or redistribute it under certain\n" +"conditions <http://gnu.org.licenses/gpl.html>\n"); +} + +static res_T +parse_grid_definition(struct atrstm_args* args, const char* str) +{ + unsigned def[3]; + size_t len; + res_T res = RES_OK; + ASSERT(args && str); + + res = cstr_to_list_uint(str, ',', def, &len, 3); + if(res == RES_OK && len != 3) res = RES_BAD_ARG; + if(res != RES_OK) { + fprintf(stderr, "Invalid grid definition `%s'.\n", str); + goto error; + } + + if(!def[0] || !def[1] || !def[2]) { + fprintf(stderr, + "Invalid null grid definition [%u, %u, %u].\n", SPLIT3(def)); + res = RES_BAD_ARG; + goto error; + } + + args->grid_max_definition[0] = def[0]; + args->grid_max_definition[1] = def[1]; + args->grid_max_definition[2] = def[2]; + +exit: + return res; +error: + goto exit; +} + +static void +args_release(struct atrstm_args* args) +{ + ASSERT(args); + *args = ATRSTM_ARGS_DEFAULT; +} + +static res_T +args_init(struct atrstm_args* args, int argc, char** argv) +{ + res_T res = RES_OK; + int opt; + ASSERT(args && argc && argv); + + *args = ATRSTM_ARGS_DEFAULT; + + while((opt = getopt(argc, argv, "f:g:hm:Nn:p:T:t:r:vV:w:")) != -1) { + switch(opt) { + case 'f': + res = cstr_to_double(optarg, &args->fractal_dimension); + if(res == RES_OK && args->fractal_dimension <= 0) + res = RES_BAD_ARG; + break; + case 'g': + res = cstr_to_double(optarg, &args->gyration_radius_prefactor); + if(res == RES_OK && args->gyration_radius_prefactor <= 0) + res = RES_BAD_ARG; + break; + case 'h': + print_help(argv[0]); + args_release(args); + break; + case 'm': args->sth_filename = optarg; break; + case 'N': args->precompute_normals = 1; break; + case 'n': args->name = optarg; break; + case 'p': args->atrtp_filename = optarg; break; + case 'r': args->atrri_filename = optarg; break; + case 'T': + res = cstr_to_double(optarg, &args->optical_thickness); + if(res == RES_OK && args->optical_thickness < 0) res = RES_BAD_ARG; + break; + case 't': + res = cstr_to_uint(optarg, &args->nthreads); + if(res == RES_OK && !args->nthreads) res = RES_BAD_ARG; + break; + case 'V': res = parse_grid_definition(args, optarg); break; + case 'v': args->verbose = 1; break; + case 'w': + res = cstr_to_double(optarg, &args->wlen_range[0]); + if(res == RES_OK && args->wlen_range[0] < 0) res = RES_BAD_ARG; + args->wlen_range[1] = args->wlen_range[0]; + break; + default: res = RES_BAD_ARG; break; + } + if(res != RES_OK) { + if(optarg) { + fprintf(stderr, "%s: invalid option argument '%s' -- '%c'\n", + argv[0], optarg, opt); + } + goto error; + } + } + + /* Check parsed arguments */ + if(!args->sth_filename) { + fprintf(stderr, + "Missing the path toward the volumetric mesh -- option '-m'\n"); + res = RES_BAD_ARG; + goto error; + } + if(!args->atrtp_filename) { + fprintf(stderr, + "Missing the path of the thermodynamic properties -- option '-p'\n"); + res = RES_BAD_ARG; + goto error; + } + if(!args->atrri_filename) { + fprintf(stderr, + "Missing the path of the refractive indices -- option '-r'\n"); + res = RES_BAD_ARG; + goto error; + } + +exit: + return res; +error: + args_release(args); + goto exit; +} + +/******************************************************************************* + * Main function + ******************************************************************************/ +int +main(int argc, char** argv) +{ + struct atrstm_args args = ATRSTM_ARGS_DEFAULT; + struct atrstm* atrstm = NULL; + res_T res = RES_OK; + int err = 0; + + res = args_init(&args, argc, argv); + if(res != RES_OK) goto error; + if(!args.sth_filename) goto exit; /* Quit */ + + res = atrstm_create(NULL, &mem_default_allocator, &args, &atrstm); + if(res != RES_OK) goto error; + +exit: + args_release(&args); + if(atrstm) ATRSTM(ref_put(atrstm)); + if(MEM_ALLOCATED_SIZE(&mem_default_allocator) != 0) { + fprintf(stderr, "Memory leaks: %lu bytes\n", + (unsigned long)MEM_ALLOCATED_SIZE(&mem_default_allocator)); + err = -1; + } + return err; +error: + err = -1; + goto exit; +}