rnatm

Load and structure data describing an atmosphere
git clone git://git.meso-star.fr/rnatm.git
Log | Files | Refs | README | LICENSE

commit e9f545b525c8fbf0e5ddcf034339b311fbc512dc
parent 521d570cbebe738d902126f39134170f214577ae
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Mon, 18 Jul 2022 16:24:07 +0200

Begin the implementation of the atmosphere creation

Diffstat:
Acmake/CMakeLists.txt | 135+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/rnatm.c | 202+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/rnatm.h | 121+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/rnatm_c.h | 38++++++++++++++++++++++++++++++++++++++
Asrc/rnatm_log.c | 129+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/rnatm_log.h | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 698 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -0,0 +1,135 @@ +# Copyright (C) 2022 Centre National de la Recherche Scientifique +# Copyright (C) 2022 Institut de Physique du Globe de Paris +# Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) +# Copyright (C) 2022 Université de Reims Champagne-Ardenne +# Copyright (C) 2022 Université de Versaille Saint-Quentin +# Copyright (C) 2022 Université Paul Sabatier (contact@laplace.univ-tlse.fr) +# +# 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/>. + +cmake_minimum_required(VERSION 3.1) +project(rnatm C) +enable_testing() + +set(RNATM_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src) +option(NO_TEST "Do not build tests" OFF) + +################################################################################ +# Check dependencies +################################################################################ +find_package(RCMake 0.4 REQUIRED) +find_package(RNSF REQUIRED) +find_package(RSys 0.9 REQUIRED) +find_package(StarAerosol REQUIRED) +find_package(StarBuffer REQUIRED) +find_package(StarCK REQUIRED) +find_package(StarMesh REQUIRED) + +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${RCMAKE_SOURCE_DIR}) +include(rcmake) +include(rcmake_runtime) + +include_directories( + ${RNSF_INCLUDE_DIR} + ${RSys_INCLUDE_DIR} + ${StarAerosol_INCLUDE_DIR} + ${StarBuffer_INCLUDE_DIR} + ${StarCK_INCLUDE_DIR} + ${StarMesh_INCLUDE_DIR}) + +################################################################################ +# Configure and define targets +################################################################################ +set(VERSION_MAJOR 0) +set(VERSION_MINOR 0) +set(VERSION_PATCH 0) +set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) + +set(RNATM_FILES_SRC + rnatm.c + rnatm_log.c) +set(RNATM_FILES_INC + rnatm_c.h + rnatm_log.h) +set(RNATM_FILES_INC_API rnatm.h) +set(RNATM_FILES_DOC COPYING README.md) + +# Prepend each file in the `RNATM_FILES_<SRC|INC>' list by `RNATM_SOURCE_DIR' +rcmake_prepend_path(RNATM_FILES_SRC ${RNATM_SOURCE_DIR}) +rcmake_prepend_path(RNATM_FILES_INC ${RNATM_SOURCE_DIR}) +rcmake_prepend_path(RNATM_FILES_INC_API ${RNATM_SOURCE_DIR}) +rcmake_prepend_path(RNATM_FILES_DOC ${PROJECT_SOURCE_DIR}/../) + +add_library(rnatm SHARED ${RNATM_FILES_SRC} ${RNATM_FILES_INC} ${RNATM_FILES_INC_API}) +target_link_libraries(rnatm RSys) + +set_target_properties(rnatm PROPERTIES + DEFINE_SYMBOL RNATM_SHARED_BUILD + VERSION ${VERSION} + SOVERSION ${VERSION_MAJOR}) + +rcmake_setup_devel(rnatm RNATM ${VERSION} rad-net/rnatm_version.h) + +################################################################################ +# Add tests +################################################################################ +if(NOT NO_TEST) + function(build_test _name) + add_executable(${_name} + ${RNATM_SOURCE_DIR}/${_name}.c) + target_link_libraries(${_name} rnatm RSys) + endfunction() + + function(new_test _name) + build_test(${_name}) + add_test(${_name} ${_name}) + endfunction() + +endif() + +################################################################################ +# Man page +############################################################################### +find_program(SCDOC NAMES scdoc) +if(NOT SCDOC) + message(WARNING + "The `scdoc' program is missing. " + "The Rad Net ATMopshere man page cannot be generated.") +else() + set(_man_names ) # TODO + + foreach(_man IN LISTS _man_names) + set(_src ${PROJECT_SOURCE_DIR}/../doc/${_man}.scd) + add_custom_command( + OUTPUT ${_man} + COMMAND ${SCDOC} < ${_src} > ${_man} + DEPENDS ${_src} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMENT "Buid ROFF man page ${_man}" + VERBATIM) + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${_man} DESTINATION share/man/man5) + endforeach() + add_custom_target(man-roff ALL DEPENDS ${_man_names}) +endif() + + +################################################################################ +# Define output & install directories +################################################################################ +install(TARGETS rnatm + ARCHIVE DESTINATION bin + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin) +install(FILES ${RNATM_FILES_INC_API} DESTINATION include/rad-net) +install(FILES ${RNATM_FILES_DOC} DESTINATION share/doc/rnatm) diff --git a/src/rnatm.c b/src/rnatm.c @@ -0,0 +1,202 @@ +/* Copyright (C) 2022 Centre National de la Recherche Scientifique + * Copyright (C) 2022 Institut de Physique du Globe de Paris + * Copyright (C) 2022 |Méso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université de Reims Champagne-Ardenne + * Copyright (C) 2022 Université de Versaille Saint-Quentin + * Copyright (C) 2022 Université Paul Sabatier (contact@laplace.univ-tlse.fr) + * + * 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 "rnatm.h" +#include "rnatm_c.h" +#include "rnatm_log.h" + +#include <rsys/cstr.h> +#include <rsys/mem_allocator.h> + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static INLINE res_T +check_rnatm_gas_args(const struct rnatm_gas_args* args) +{ + if(!args) return RES_BAD_ARG; + + /* Filenames cannot be NULL */ + if(!args->smsh_filename + || !args->sck_filename + || !args->props_filename) + return RES_BAD_ARG; + + return RES_OK; +} + +static INLINE res_T +check_rnatm_aerosol_args(const struct rnatm_aerosol_args* args) +{ + if(!args) return RES_BAD_ARG; + + /* Filenames cannot be NULL */ + if(!args->smsh_filename + || !args->sars_filename + || !args->props_filename + || !args->phase_fn_lst_filename) + return RES_BAD_ARG; + + return RES_OK; +} + +static res_T +check_rnatm_create_args(const struct rnatm_create_args* args) +{ + size_t i; + res_T res = RES_OK; + + /* Invalid args */ + if(!args) return RES_BAD_ARG; + + /* Invalid gas */ + res = check_rnatm_gas_args(&args->gas); + if(res != RES_OK) return res; + + /* Invalid aerosols */ + FOR_EACH(i, 0, args->aerosols_count) { + res = check_rnatm_aerosol_args(args->aerosols+i); + if(res != RES_OK) return res; + } + + return RES_OK; +} + +static res_T +create_rnatm + (const struct rnatm_create_args* args, + struct rnatm** out_atm) +{ + struct rnatm* atm = NULL; + struct mem_allocator* allocator = NULL; + res_T res = RES_OK; + + if(!out_atm) { res = RES_BAD_ARG; goto error;} + res = check_rnatm_create_args(args); + if(res != RES_OK) goto error; + + allocator = args->allocator ? args->allocator : &mem_default_allocator; + atm = MEM_CALLOC(allocator, 1, sizeof(*atm)); + if(!atm) { + if(args->verbose) { + #define ERR_STR \ + "Could not allocate the device of the Rad-Net ATMosphere library" + if(args->logger) { + logger_print(args->logger, LOG_ERROR, ERR_STR); + } else { + fprintf(stderr, MSG_ERROR_PREFIX ERR_STR); + } + #undef ERR_STR + } + res = RES_MEM_ERR; + goto error; + } + ref_init(&atm->ref); + atm->allocator = allocator; + atm->verbose = args->verbose; + str_init(atm->allocator, &atm->name); + if(args->logger) { + atm->logger = args->logger; + } else { + res = setup_log_default(atm); + if(res != RES_OK) { + if(args->verbose) { + fprintf(stderr, MSG_ERROR_PREFIX + "Could not setup the default logger of the " + "Rad-Net ATMopshere library.\n"); + } + goto error; + } + } + + res = str_set(&atm->name, args->name); + if(res != RES_OK) { + log_err(atm, "Could not setup the atmosphere name to `%s' -- %s\n", + args->name, res_to_cstr(res)); + goto error; + } + +exit: + if(out_atm) *out_atm = atm; + return res; +error: + if(atm) { RNATM(ref_put(atm)); atm = NULL; } + goto exit; +} + +static void +release_rnatm(ref_T* ref) +{ + struct rnatm* atm = CONTAINER_OF(ref, struct rnatm, ref); + ASSERT(ref); + + if(atm->logger == &atm->logger__) logger_release(&atm->logger__); + str_release(&atm->name); + MEM_RM(atm->allocator, atm); +} + + +/******************************************************************************* + * Exported symbols + ******************************************************************************/ +res_T +rnatm_create + (const struct rnatm_create_args* args, + struct rnatm** out_atm) +{ + struct rnatm* atm = NULL; + res_T res = RES_OK; + + res = create_rnatm(args, &atm); + if(res != RES_OK) goto error; + + /* TODO + res = setup_meshes(atm, args); + if(res != RES_OK) goto error; + res = setup_octrees(atm, args); + if(res != RES_OK) goto error; + res = setup_properties(atm, args); + if(res != RES_OK) goto error; */ + +exit: + if(out_atm) *out_atm = atm; + return res; +error: + if(atm) { RNATM(ref_put(atm)); atm = NULL; } + goto exit; +} + +res_T +rnatm_ref_get(struct rnatm* atm) +{ + if(!atm) return RES_BAD_ARG; + ref_get(&atm->ref); + return RES_OK; +} + +res_T +rnatm_ref_put(struct rnatm* atm) +{ + if(!atm) return RES_BAD_ARG; + ref_put(&atm->ref, release_rnatm); + return RES_OK; +} + + diff --git a/src/rnatm.h b/src/rnatm.h @@ -0,0 +1,121 @@ +/* Copyright (C) 2022 Centre National de la Recherche Scientifique + * Copyright (C) 2022 Institut de Physique du Globe de Paris + * Copyright (C) 2022 |Méso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université de Reims Champagne-Ardenne + * Copyright (C) 2022 Université de Versaille Saint-Quentin + * Copyright (C) 2022 Université Paul Sabatier (contact@laplace.univ-tlse.fr) + * + * 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/>. */ + +#ifndef RNATM_H +#define RNATM_H + +#include <star/s3d.h> + +#include <rsys/rsys.h> + +/* Library symbol management */ +#if defined(RNATM_SHARED_BUILD) /* Build shared library */ + #define RNATM_API extern EXPORT_SYM +#elif defined(RNATM_STATIC) /* Use/build static library */ + #define RNATM_API extern LOCAL_SYM +#else /* Use shared library */ + #define RNATM_API extern IMPORT_SYM +#endif + +/* Helper macro that asserts if the invocation of the rnatm function `Func' + * returns an error. One should use this macro on suvm function calls for + * which no explicit error checking is performed */ +#ifndef NDEBUG + #define RNATM(Func) ASSERT(rnatm_ ## Func == RES_OK) +#else + #define RNATM(Func) rnatm_ ## Func +#endif + +/* Forward declaration of external data types */ +struct logger; +struct mem_allocator; + +struct rnatm_gas_args { + const char* smsh_filename; /* Geometry */ + const char* sck_filename; /* Radiative properties */ + const char* props_filename; /* Temperature */ +}; +#define RNATM_GAS_ARGS_NULL__ {NULL, NULL, NULL} +static const struct rnatm_gas_args RNATM_GAS_ARGS_NULL = RNATM_GAS_ARGS_NULL__; + +struct rnatm_aerosol_args { + const char* smsh_filename; /* Geometry */ + const char* sars_filename; /* Radiative properties */ + const char* props_filename; /* Indice de la fonction de phase */ + const char* phase_fn_lst_filename; /* List of phase functions */ +}; +#define RNATM_AEROSOL_ARGS_NULL__ {NULL, NULL, NULL, NULL} +static const struct rnatm_aerosol_args RNATM_AEROSOL_ARGS_NULL = + RNATM_AEROSOL_ARGS_NULL__; + +struct rnatm_create_args { + struct rnatm_gas_args gas; + const struct rnatm_aerosol_args* aerosols; + size_t aerosols_count; + + const char* name; /* Name of the atmosphere */ + + struct logger* logger; /* NULL <=> use default logger */ + struct mem_allocator* allocator; /* NULL <=> use default allocator */ + int verbose; /* Verbosity level */ +}; +#define RNATM_CREATE_ARGS_DEFAULT__ { \ + RNATM_GAS_ARGS_NULL__, /* Gas */ \ + NULL, /* Aerosols */ \ + 0, /* Number of aerosols */ \ + "atmosphere", /* Name */ \ + \ + NULL, /* Logger */ \ + NULL, /* Allocator */ \ + 0 /* Verbosity level */ \ +} +static const struct rnatm_create_args RNDGR_CREATE_ARGS_DEFAULT = + RNATM_CREATE_ARGS_DEFAULT__; + +/* Opaque data types */ +struct rnatm; + +BEGIN_DECLS + +/******************************************************************************* + * API of the Rad-Net GRounD library + ******************************************************************************/ +RNATM_API res_T +rnatm_create + (const struct rnatm_create_args* args, + struct rnatm** atm); + +RNATM_API res_T +rnatm_ref_get + (struct rnatm* atm); + +RNATM_API res_T +rnatm_ref_put + (struct rnatm* atm); + +/* Validates the atmosphere data. Data checks have already been done on load, + * but this function performs longer tests: for example, it iterates over all + * indices of the aerosol phase function check their validity against the mesh + * they are associated with and the phase function list loaded */ +RNATM_API res_T +rnatm_validate + (const struct rnatm* atm); + +#endif /* RNATM_H */ diff --git a/src/rnatm_c.h b/src/rnatm_c.h @@ -0,0 +1,38 @@ +/* Copyright (C) 2022 Centre National de la Recherche Scientifique + * Copyright (C) 2022 Institut de Physique du Globe de Paris + * Copyright (C) 2022 |Méso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université de Reims Champagne-Ardenne + * Copyright (C) 2022 Université de Versaille Saint-Quentin + * Copyright (C) 2022 Université Paul Sabatier (contact@laplace.univ-tlse.fr) + * + * 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/>. */ + +#ifndef RNATM_C_H +#define RNATM_C_H + +#include <rsys/logger.h> +#include <rsys/ref_count.h> +#include <rsys/str.h> + +struct rnatm { + struct str name; + + int verbose; + struct logger* logger; + struct logger logger__; + struct mem_allocator* allocator; + ref_T ref; +}; + +#endif /* RNATM_C_H */ diff --git a/src/rnatm_log.c b/src/rnatm_log.c @@ -0,0 +1,129 @@ +/* Copyright (C) 2022 Centre National de la Recherche Scientifique + * Copyright (C) 2022 Institut de Physique du Globe de Paris + * Copyright (C) 2022 |Méso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université de Reims Champagne-Ardenne + * Copyright (C) 2022 Université de Versaille Saint-Quentin + * Copyright (C) 2022 Université Paul Sabatier (contact@laplace.univ-tlse.fr) + * + * 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 "rnatm_c.h" +#include "rnatm_log.h" + +#include <rsys/cstr.h> +#include <rsys/logger.h> + +#include <stdarg.h> + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static INLINE void +log_msg + (const struct rnatm* atm, + const enum log_type stream, + const char* msg, + va_list vargs) +{ + ASSERT(atm && msg); + if(atm->verbose) { + res_T res; (void)res; + res = logger_vprint(atm->logger, stream, msg, vargs); + ASSERT(res == RES_OK); + } +} + +static void +print_info(const char* msg, void* ctx) +{ + (void)ctx; + fprintf(stderr, MSG_INFO_PREFIX"%s", msg); +} + +static void +print_err(const char* msg, void* ctx) +{ + (void)ctx; + fprintf(stderr, MSG_ERROR_PREFIX"%s", msg); +} + +static void +print_warn(const char* msg, void* ctx) +{ + (void)ctx; + fprintf(stderr, MSG_WARNING_PREFIX"%s", msg); +} + +/******************************************************************************* + * Local functions + ******************************************************************************/ +res_T +setup_log_default(struct rnatm* atm) +{ + res_T res = RES_OK; + ASSERT(atm); + + res = logger_init(atm->allocator, &atm->logger__); + if(res != RES_OK) { + if(atm->verbose) { + fprintf(stderr, + MSG_ERROR_PREFIX + "Could not setup the default logger -- %s.\n", + res_to_cstr(res)); + } + goto error; + } + logger_set_stream(&atm->logger__, LOG_OUTPUT, print_info, NULL); + logger_set_stream(&atm->logger__, LOG_ERROR, print_err, NULL); + logger_set_stream(&atm->logger__, LOG_WARNING, print_warn, NULL); + atm->logger = &atm->logger__; + +exit: + return res; +error: + goto exit; +} + +void +log_info(const struct rnatm* atm, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(atm && msg); + + va_start(vargs_list, msg); + log_msg(atm, LOG_OUTPUT, msg, vargs_list); + va_end(vargs_list); +} + +void +log_err(const struct rnatm* atm, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(atm && msg); + + va_start(vargs_list, msg); + log_msg(atm, LOG_ERROR, msg, vargs_list); + va_end(vargs_list); +} + +void +log_warn(const struct rnatm* atm, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(atm && msg); + + va_start(vargs_list, msg); + log_msg(atm, LOG_WARNING, msg, vargs_list); + va_end(vargs_list); +} diff --git a/src/rnatm_log.h b/src/rnatm_log.h @@ -0,0 +1,73 @@ +/* Copyright (C) 2022 Centre National de la Recherche Scientifique + * Copyright (C) 2022 Institut de Physique du Globe de Paris + * Copyright (C) 2022 |Méso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université de Reims Champagne-Ardenne + * Copyright (C) 2022 Université de Versaille Saint-Quentin + * Copyright (C) 2022 Université Paul Sabatier (contact@laplace.univ-tlse.fr) + * + * 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/>. */ + +#ifndef RNATM_LOG_H +#define RNATM_LOG_H + +#include <rsys/rsys.h> + +#define MSG_INFO_PREFIX "RNATM:\x1b[1m\x1b[32minfo\x1b[0m: " +#define MSG_ERROR_PREFIX "RNATM:\x1b[1m\x1b[31merror\x1b[0m: " +#define MSG_WARNING_PREFIX "RNATM:\x1b[1m\x1b[33mwarning\x1b[0m: " + +struct rnatm; +struct logger; + +extern LOCAL_SYM res_T +setup_log_default + (struct rnatm* rnatm); + +/* Conditionally log a message on the LOG_OUTPUT stream of the rnatm logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_info + (const struct rnatm* atm, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +/* Conditionally log a message on the LOG_ERROR stream of the rnatm logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_err + (const struct rnatm* atm, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +/* Conditionally log a message on the LOG_WARNING stream of the rnatm logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_warn + (const struct rnatm* atm, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +#endif /* RNATM_LOG_H */