star-hitran

Load line-by-line data from the HITRAN database
git clone git://git.meso-star.fr/star-hitran.git
Log | Files | Refs | README | LICENSE

commit 7bcf9f7e2cfb3264d20b295e4b31e826e0bda4c0
parent 002644fc1e4e3f88c2001877b1f45d5978f4c2d8
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed,  9 Feb 2022 10:01:46 +0100

Revert sht prefix in shitran

We already use the sht prefix for Star-HeaT

Diffstat:
Mcmake/CMakeLists.txt | 67++++++++++++++++++++++++++++++++++---------------------------------
Asrc/shitran.c | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/shitran.h | 145+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/shitran_c.h | 35+++++++++++++++++++++++++++++++++++
Asrc/shitran_isotope_metadata.c | 705+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/shitran_log.c | 127+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/shitran_log.h | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dsrc/sht.c | 101-------------------------------------------------------------------------------
Dsrc/sht.h | 142-------------------------------------------------------------------------------
Dsrc/sht_c.h | 35-----------------------------------
Dsrc/sht_isotope_metadata.c | 704-------------------------------------------------------------------------------
Dsrc/sht_log.c | 127-------------------------------------------------------------------------------
Dsrc/sht_log.h | 73-------------------------------------------------------------------------
Asrc/test_shitran.c | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/test_shitran_isotope_metadata.c | 179+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dsrc/test_sht.c | 74--------------------------------------------------------------------------
Dsrc/test_sht_isotope_metadata.c | 179-------------------------------------------------------------------------------
17 files changed, 1473 insertions(+), 1468 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -17,10 +17,10 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. cmake_minimum_required(VERSION 3.1) -project(sht C) +project(shitran C) enable_testing() -set(SHT_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src) +set(SHITRAN_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src) option(NO_TEST "Do not build tests" OFF) ################################################################################ @@ -43,41 +43,42 @@ set(VERSION_MINOR 0) set(VERSION_PATCH 0) set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) -set(SHT_FILES_SRC - sht.c - sht_log.c - sht_isotope_metadata.c) -set(SHT_FILES_INC - sht_c.h - sht_log.h) -set(SHT_FILES_INC_API - sht.h) - -set(SHT_FILES_DOC COPYING README.md) - -# Prepend each file in the `SHT_FILES_<SRC|INC>' list by `SHT_SOURCE_DIR' -rcmake_prepend_path(SHT_FILES_SRC ${SHT_SOURCE_DIR}) -rcmake_prepend_path(SHT_FILES_INC ${SHT_SOURCE_DIR}) -rcmake_prepend_path(SHT_FILES_INC_API ${SHT_SOURCE_DIR}) -rcmake_prepend_path(SHT_FILES_DOC ${PROJECT_SOURCE_DIR}/../) - -add_library(sht SHARED ${SHT_FILES_SRC} ${SHT_FILES_INC} ${SHT_FILES_INC_API}) -target_link_libraries(sht RSys) - -set_target_properties(sht PROPERTIES - DEFINE_SYMBOL SHT_SHARED_BUILD +set(SHITRAN_FILES_SRC + shitran.c + shitran_log.c + shitran_isotope_metadata.c) +set(SHITRAN_FILES_INC + shitran_c.h + shitran_log.h) +set(SHITRAN_FILES_INC_API + shitran.h) + +set(SHITRAN_FILES_DOC COPYING README.md) + +# Prepend each file in the `SHITRAN_FILES_<SRC|INC>' list by `SHITRAN_SOURCE_DIR' +rcmake_prepend_path(SHITRAN_FILES_SRC ${SHITRAN_SOURCE_DIR}) +rcmake_prepend_path(SHITRAN_FILES_INC ${SHITRAN_SOURCE_DIR}) +rcmake_prepend_path(SHITRAN_FILES_INC_API ${SHITRAN_SOURCE_DIR}) +rcmake_prepend_path(SHITRAN_FILES_DOC ${PROJECT_SOURCE_DIR}/../) + +add_library(shitran SHARED + ${SHITRAN_FILES_SRC} ${SHITRAN_FILES_INC} ${SHITRAN_FILES_INC_API}) +target_link_libraries(shitran RSys) + +set_target_properties(shitran PROPERTIES + DEFINE_SYMBOL SHITRAN_SHARED_BUILD VERSION ${VERSION} SOVERSION ${VERSION_MAJOR}) -rcmake_setup_devel(sht StarHITRAN ${VERSION} star/sht_version.h) +rcmake_setup_devel(shitran StarHITRAN ${VERSION} star/shitran_version.h) ################################################################################ # Add tests ################################################################################ if(NOT NO_TEST) function(build_test _name) - add_executable(${_name} ${SHT_SOURCE_DIR}/${_name}.c) - target_link_libraries(${_name} sht RSys ${ARGN}) + add_executable(${_name} ${SHITRAN_SOURCE_DIR}/${_name}.c) + target_link_libraries(${_name} shitran RSys ${ARGN}) endfunction() function(new_test _name) @@ -85,17 +86,17 @@ if(NOT NO_TEST) add_test(${_name} ${_name}) endfunction() - new_test(test_sht) - new_test(test_sht_isotope_metadata) + new_test(test_shitran) + new_test(test_shitran_isotope_metadata) endif() ################################################################################ # Define output & install directories ################################################################################ -install(TARGETS sht +install(TARGETS shitran ARCHIVE DESTINATION bin LIBRARY DESTINATION lib RUNTIME DESTINATION bin) -install(FILES ${SHT_FILES_INC_API} DESTINATION include/star) -install(FILES ${SHT_FILES_DOC} DESTINATION share/doc/star-hitran) +install(FILES ${SHITRAN_FILES_INC_API} DESTINATION include/star) +install(FILES ${SHITRAN_FILES_DOC} DESTINATION share/doc/star-hitran) diff --git a/src/shitran.c b/src/shitran.c @@ -0,0 +1,101 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT + * Copyright (C) 2022 Université Paul Sabatier - Laplace + * + * 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 "shitran.h" +#include "shitran_c.h" +#include "shitran_log.h" + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static INLINE res_T +check_shitran_create_args(const struct shitran_create_args* args) +{ + return args ? RES_OK : RES_BAD_ARG; +} + +static void +release_shitran(ref_T* ref) +{ + struct shitran* shitran = CONTAINER_OF(ref, struct shitran, ref); + ASSERT(ref); + if(shitran->logger == &shitran->logger__) logger_release(&shitran->logger__); + MEM_RM(shitran->allocator, shitran); +} + +/******************************************************************************* + * Exported functions + ******************************************************************************/ +res_T +shitran_create + (const struct shitran_create_args* args, + struct shitran** out_shitran) +{ + struct mem_allocator* allocator = NULL; + struct shitran* shitran = NULL; + res_T res = RES_OK; + + if(!out_shitran) { res = RES_BAD_ARG; goto error; } + res = check_shitran_create_args(args); + if(res != RES_OK) goto error; + + allocator = args->allocator ? args->allocator : &mem_default_allocator; + shitran = MEM_CALLOC(allocator, 1, sizeof(*shitran)); + if(!shitran) { + #define ERR_STR "Could not allocate the Star-HITRAN data structure.\n" + 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(&shitran->ref); + shitran->allocator = allocator; + shitran->verbose = args->verbose; + if(args->logger) { + shitran->logger = args->logger; + } else { + setup_log_default(shitran); + } + +exit: + if(out_shitran) *out_shitran = shitran; + return res; +error: + if(shitran) { SHITRAN(ref_put(shitran)); shitran = NULL; } + goto exit; +} + +res_T +shitran_ref_get(struct shitran* shitran) +{ + if(!shitran) return RES_BAD_ARG; + ref_get(&shitran->ref); + return RES_OK; +} + +res_T +shitran_ref_put(struct shitran* shitran) +{ + if(!shitran) return RES_BAD_ARG; + ref_put(&shitran->ref, release_shitran); + return RES_OK; +} diff --git a/src/shitran.h b/src/shitran.h @@ -0,0 +1,145 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT + * Copyright (C) 2022 Université Paul Sabatier - Laplace + * + * 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 SHITRAN_H +#define SHITRAN_H + +#include <rsys/rsys.h> + +/* Library symbol management */ +#if defined(SHITRAN_SHARED_BUILD) /* Build shared library */ + #define SHITRAN_API extern EXPORT_SYM +#elif defined(SHITRAN_STATIC) /* Use/build static library */ + #define SHITRAN_API extern LOCAL_SYM +#else + #define SHITRAN_API extern IMPORT_SYM +#endif + +/* Helper macro that asserts if the invocation of the sth function `Func' + * returns an error. One should use this macro on sth function calls for + * which no explicit error checking is performed */ +#ifndef NDEBUG + #define SHITRAN(Func) ASSERT(shitran_ ## Func == RES_OK) +#else + #define SHITRAN(Func) shitran_ ## Func +#endif + +struct shitran_create_args { + struct logger* logger; /* May be NULL <=> default logger */ + struct mem_allocator* allocator; /* NULL <=> use default allocator */ + int verbose; /* Verbosity level */ +}; +#define SHITRAN_CREATE_ARGS_DEFAULT__ {NULL, NULL, 0} +static const struct shitran_create_args SHITRAN_CREATE_ARGS_DEFAULT = + SHITRAN_CREATE_ARGS_DEFAULT__; + +struct shitran_isotope { + double abundance; /* in ]0, 1] */ + double Q296K; /* Partition function at Tref = 296K */ + double molar_mass; /* In g */ + size_t molecule; /* Index of the molecule to which the isotope belongs */ + int gj; /* State independent degeneracy factor */ + int id; /* Unique identifier of the isotope */ +}; +#define SHITRAN_ISOTOPOLOGUE_NULL__ {0,0,0,0,0,0} +static const struct shitran_isotope SHITRAN_ISOTOPOLOGUE_NULL = + SHITRAN_ISOTOPOLOGUE_NULL__; + +struct shitran_molecule { + const char* name; + size_t nisotopes; /* Number of isotopes */ + const struct shitran_isotope* isotopes; + int id; /* Unique identifier */ +}; +#define SHITRAN_MOLECULE_NULL__ {NULL, 0, NULL, -1} +static const struct shitran_molecule SHITRAN_MOLECULE_NULL = + SHITRAN_MOLECULE_NULL__; + +#define SHITRAN_MOLECULE_IS_NULL(Molecule) \ + ( (Molecule)->name == SHITRAN_MOLECULE_NULL.name \ + && (Molecule)->nisotopes == SHITRAN_MOLECULE_NULL.nisotopes \ + && (Molecule)->isotopes == SHITRAN_MOLECULE_NULL.isotopes \ + && (Molecule)->id == SHITRAN_MOLECULE_NULL.id) + +/* Forward declarations */ +struct shitran; +struct shitran_isotope_metadata; + +BEGIN_DECLS + +/******************************************************************************* + * Device API + ******************************************************************************/ +SHITRAN_API res_T +shitran_create + (const struct shitran_create_args* args, + struct shitran** shitran); + +SHITRAN_API res_T +shitran_ref_get + (struct shitran* shitran); + +SHITRAN_API res_T +shitran_ref_put + (struct shitran* shitran); + +/******************************************************************************* + * Isotope metadata API + ******************************************************************************/ +SHITRAN_API res_T +shitran_isotope_metadata_load + (struct shitran* shitran, + const char* path, + struct shitran_isotope_metadata** metadata); + +SHITRAN_API res_T +shitran_isotope_metadata_load_stream + (struct shitran* shitran, + FILE* stream, + const char* stream_name, /* NULL <=> use default stream name */ + struct shitran_isotope_metadata** metadata); + +SHITRAN_API res_T +shitran_isotope_metadata_ref_get + (struct shitran_isotope_metadata* metadata); + +SHITRAN_API res_T +shitran_isotope_metadata_ref_put + (struct shitran_isotope_metadata* metadata); + +SHITRAN_API res_T +shitran_isotope_metadata_get_molecules_count + (const struct shitran_isotope_metadata* metadata, + size_t* nmolecules); + +SHITRAN_API res_T +shitran_isotope_metadata_get_molecule + (const struct shitran_isotope_metadata* metadata, + const size_t imolecule, /* Index of the molecule in [0, molecules_count[ */ + struct shitran_molecule* molecule); + +/* `molecule' is set to SHITRAN_MOLECULE_NULL if ti molecule id is not found */ +SHITRAN_API res_T +shitran_isotope_metadata_find_molecule + (struct shitran_isotope_metadata* metadata, + const int molecule_id, /* Unique identifier of the molecule */ + struct shitran_molecule* molecule); + +END_DECLS + +#endif /* SHITRAN_H */ diff --git a/src/shitran_c.h b/src/shitran_c.h @@ -0,0 +1,35 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT + * Copyright (C) 2022 Université Paul Sabatier - Laplace + * + * 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 SHITRAN_C_H +#define SHITRAN_C_H + +#include <rsys/logger.h> +#include <rsys/ref_count.h> + +struct mem_allocator; + +struct shitran { + struct mem_allocator* allocator; + struct logger* logger; + struct logger logger__; /* Default logger */ + int verbose; + ref_T ref; +}; + +#endif /* SHITRAN_C_H */ diff --git a/src/shitran_isotope_metadata.c b/src/shitran_isotope_metadata.c @@ -0,0 +1,705 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT + * Copyright (C) 2022 Université Paul Sabatier - Laplace + * + * 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/>. */ + +#define _POSIX_C_SOURCE 200112L /* strtok_r support */ + +#include "shitran.h" +#include "shitran_c.h" +#include "shitran_log.h" + +#include <rsys/cstr.h> +#include <rsys/dynamic_array.h> +#include <rsys/hash_table.h> +#include <rsys/ref_count.h> +#include <rsys/str.h> +#include <rsys/text_reader.h> + +#include <ctype.h> +#include <string.h> + +#define C_FORMAT_double "%g" +#define C_FORMAT_int "%d" + +struct param_desc { + const char* path; /* Path where the param lies */ + const char* name; /* Name of the param */ + size_t iline; /* Line number of the param */ + + /* Domain of the param */ + double low, upp; + int is_low_incl; /* Define if the lower bound is inclusive */ + int is_upp_incl; /* Define if the upper bound is inclusive */ +}; +#define PARAM_DESC_NULL__ {NULL, NULL, 0, 0, 0, 0, 0} +static const struct param_desc PARAM_DESC_NULL = PARAM_DESC_NULL__; + +/* Generate the dynamic array of isotopes */ +#define DARRAY_NAME isotope +#define DARRAY_DATA struct shitran_isotope +#include <rsys/dynamic_array.h> + +struct molecule { + struct str name; + size_t isotopes_range[2]; /* Range of the [1st and last[ isotopes */ + int id; /* Unique identifier of the molecule */ +}; +#define MOLECULE_IS_VALID(Molecule) ((Molecule)->id >= 0) + +static INLINE void +molecule_clear(struct molecule* molecule) +{ + ASSERT(molecule); + molecule->isotopes_range[0] = SIZE_MAX; + molecule->isotopes_range[1] = 0; + molecule->id = -1; +} + +static INLINE void +molecule_init(struct mem_allocator* allocator, struct molecule* molecule) +{ + str_init(allocator, &molecule->name); + molecule_clear(molecule); +} + +static INLINE void +molecule_release(struct molecule* molecule) +{ + str_release(&molecule->name); +} + +static INLINE res_T +molecule_copy(struct molecule* dst, const struct molecule* src) +{ + dst->isotopes_range[0] = src->isotopes_range[0]; + dst->isotopes_range[1] = src->isotopes_range[1]; + dst->id = src->id; + return str_copy(&dst->name, &src->name); +} + +static INLINE res_T +molecule_copy_and_release(struct molecule* dst, struct molecule* src) +{ + dst->isotopes_range[0] = src->isotopes_range[0]; + dst->isotopes_range[1] = src->isotopes_range[1]; + dst->id = src->id; + return str_copy_and_release(&dst->name, &src->name); +} + +/* Generate the dynamic array of molecules */ +#define DARRAY_NAME molecule +#define DARRAY_DATA struct molecule +#define DARRAY_FUNCTOR_INIT molecule_init +#define DARRAY_FUNCTOR_RELEASE molecule_release +#define DARRAY_FUNCTOR_COPY molecule_copy +#define DARRAY_FUNCTOR_COPY_AND_RELEASE molecule_copy_and_release +#include <rsys/dynamic_array.h> + +/* Generate the hash table that map a unique identifier to its index */ +#define HTABLE_NAME id2entry +#define HTABLE_KEY int /* Unique identifier */ +#define HTABLE_DATA size_t /* Index of the corresponding registered data */ +#include <rsys/hash_table.h> + +struct shitran_isotope_metadata { + /* List of molecules and isotopes */ + struct darray_molecule molecules; + struct darray_isotope isotopes; + + /* Map the identifier of a molecule/isotope to its correspond index into + * their corresponding dynamic arays into which they are registered */ + struct htable_id2entry molid2idx; + struct htable_id2entry isoid2idx; + + struct shitran* shitran; + ref_T ref; +}; + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +#define DEFINE_PARSE_PARAM_FUNCTION(Type) \ + static res_T \ + parse_param_##Type \ + (struct shitran* shitran, \ + const char* str, \ + const char* path, \ + size_t line_num, \ + const struct param_desc* desc, \ + Type* out_param) \ + { \ + Type param = 0; \ + res_T res = RES_OK; \ + ASSERT(shitran && path && desc && out_param); \ + ASSERT(desc->low < desc->upp \ + || (desc->low == desc->upp && desc->is_low_incl && desc->is_upp_incl));\ + \ + if(!str) { \ + log_err(shitran, "%s:%lu: %s is missing.\n", \ + path, (unsigned long)line_num, desc->name); \ + res = RES_BAD_ARG; \ + goto error; \ + } \ + \ + res = cstr_to_##Type(str, &param); \ + if(res != RES_OK) { \ + log_err(shitran, "%s:%lu: invalid %s `%s'.\n", \ + desc->path, (unsigned long)desc->iline, desc->name, str); \ + res = RES_BAD_ARG; \ + goto error; \ + } \ + \ + if(param < desc->low || (param == desc->low && !desc->is_low_incl) \ + || param > desc->upp || (param == desc->upp && !desc->is_upp_incl)) { \ + log_err(shitran, \ + "%s:%lu: invalid isotope %s `%s'. It must be in " \ + "%c"CONCAT(C_FORMAT_, Type)", "CONCAT(C_FORMAT_, Type)"%c.\n", \ + path, (unsigned long)line_num, desc->name, str, \ + desc->is_low_incl ? '[' : ']', (Type)desc->low, \ + (Type)desc->upp, desc->is_upp_incl ? ']' : '['); \ + res = RES_BAD_ARG; \ + goto error; \ + } \ + \ + exit: \ + *out_param = param; \ + return res; \ + error: \ + goto exit; \ + } +DEFINE_PARSE_PARAM_FUNCTION(double) +DEFINE_PARSE_PARAM_FUNCTION(int) +#undef DEFINE_PARSE_PARAM_FUNCTION + +static res_T +create_isotoplogues + (struct shitran* shitran, + struct shitran_isotope_metadata** out_isotopes) +{ + struct shitran_isotope_metadata* metadata = NULL; + res_T res = RES_OK; + ASSERT(shitran && out_isotopes); + + metadata = MEM_CALLOC(shitran->allocator, 1, sizeof(*metadata)); + if(!metadata) { + log_err(shitran, "Could not allocate the metadata data structure.\n"); + res = RES_MEM_ERR; + goto error; + } + ref_init(&metadata->ref); + SHITRAN(ref_get(shitran)); + metadata->shitran = shitran; + darray_molecule_init(shitran->allocator, &metadata->molecules); + darray_isotope_init(shitran->allocator, &metadata->isotopes); + htable_id2entry_init(shitran->allocator, &metadata->molid2idx); + htable_id2entry_init(shitran->allocator, &metadata->isoid2idx); + +exit: + *out_isotopes = metadata; + return res; +error: + goto exit; +} + +static res_T +flush_molecule + (struct shitran_isotope_metadata* metadata, + struct molecule* molecule, /* Currently parsed molecule */ + struct txtrdr* txtrdr) +{ + size_t ientry = SIZE_MAX; + size_t* pimolecule = NULL; + res_T res = RES_OK; + ASSERT(metadata && molecule && MOLECULE_IS_VALID(molecule)); + + /* Fetch _exclusive_ upper bound */ + molecule->isotopes_range[1] = + darray_isotope_size_get(&metadata->isotopes); + if(molecule->isotopes_range[0] >= molecule->isotopes_range[1]) { + log_warn(metadata->shitran, + "%s: the %s molecule does not have any isotope.\n", + txtrdr_get_name(txtrdr), str_cget(&molecule->name)); + } + + /* Fetch the index where the molecule is going to be store */ + ientry = darray_molecule_size_get(&metadata->molecules); + + /* Store the molecule */ + res = darray_molecule_push_back(&metadata->molecules, molecule); + if(res != RES_OK) { + log_err(metadata->shitran, + "%s: error storing the %s molecule -- %s.\n", + txtrdr_get_name(txtrdr), str_cget(&molecule->name), res_to_cstr(res)); + goto error; + } + + /* Register the molecule */ + pimolecule = htable_id2entry_find(&metadata->molid2idx, &molecule->id); + if(pimolecule) { + const struct molecule* molecule2 = NULL; + molecule2 = darray_molecule_cdata_get(&metadata->molecules) + *pimolecule; + log_err(metadata->shitran, + "%s: cannot register the %s molecule. " + "The %s molecule has the same identifier %i.\n", + txtrdr_get_name(txtrdr), + str_cget(&molecule->name), + str_cget(&molecule2->name), + molecule->id); + res = RES_OK; + goto error; + } + res = htable_id2entry_set(&metadata->molid2idx, &molecule->id, &ientry); + if(res != RES_OK) { + log_err(metadata->shitran, + "%s: error registering the %s molecule -- %s.\n", + txtrdr_get_name(txtrdr), str_cget(&molecule->name), res_to_cstr(res)); + goto error; + } + + molecule_clear(molecule); + +exit: + return res; +error: + if(ientry != SIZE_MAX) darray_molecule_resize(&metadata->molecules, ientry); + htable_id2entry_erase(&metadata->molid2idx, &molecule->id); + goto exit; +} + +static res_T +parse_molecule + (struct shitran_isotope_metadata* metadata, + struct molecule* molecule, + struct txtrdr* txtrdr) +{ + char* name = NULL; + char* id = NULL; + char* tk = NULL; + char* tk_ctx = NULL; + size_t len; + res_T res = RES_OK; + ASSERT(molecule && txtrdr); + + name = strtok_r(txtrdr_get_line(txtrdr), " \t", &tk_ctx); + id = strtok_r(NULL, " \t", &tk_ctx); + + if(!name) { + log_err(metadata->shitran, "%s:%lu: molecule name is missing.\n", + txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr)); + res = RES_BAD_ARG; + goto error; + } + + res = str_set(&molecule->name, name); + if(res != RES_OK) { + log_err(metadata->shitran, + "%s:%lu: error seting the molecule name `%s' -- %s.\n", + txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr), + name, res_to_cstr(res)); + goto error; + } + + len = strlen(id); + if(!id || !len || id[0] != '(' || id[len-1] != ')') { + log_err(metadata->shitran, "%s:%lu: invalid molecule identifier.\n", + txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr)); + res = RES_BAD_ARG; + goto error; + } + + id[len-1] = '\0'; /* Rm trailing parenthesis */ + res = cstr_to_int(id+1/*Rm leading parenthesis*/, &molecule->id); + if(res != RES_OK || !MOLECULE_IS_VALID(molecule)) { + id[len-1] = ')'; /* Re-add the trailing parenthesis */ + log_err(metadata->shitran, "%s:%lu: invalid molecule identifier `%s'.\n", + txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr), id); + res = RES_BAD_ARG; + goto error; + } + + tk = strtok_r(NULL, " \t", &tk_ctx); + if(tk) { + log_warn(metadata->shitran, "%s:%lu: unexpected text `%s'.\n", + txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr), tk); + } + + molecule->isotopes_range[0] = darray_isotope_size_get(&metadata->isotopes); + +exit: + return res; +error: + goto exit; +} + +static res_T +parse_isotope + (struct shitran_isotope_metadata* metadata, + struct txtrdr* txtrdr) +{ + struct shitran_isotope isotope = SHITRAN_ISOTOPOLOGUE_NULL; + struct param_desc param_desc = PARAM_DESC_NULL; + struct shitran* shitran = NULL; + char* tk = NULL; + char* tk_ctx = NULL; + const char* path = NULL; + size_t line_num = 0; + size_t ientry = SIZE_MAX; + res_T res = RES_OK; + ASSERT(metadata && txtrdr); + + shitran = metadata->shitran; + path = txtrdr_get_name(txtrdr); + line_num = txtrdr_get_line_num(txtrdr); + + /* Fetch the index of the molecule to which the isotope belongs */ + isotope.molecule = darray_molecule_size_get(&metadata->molecules); + + tk = strtok_r(txtrdr_get_line(txtrdr), " \t", &tk_ctx); + param_desc.name = "isotope id"; + param_desc.low = 0; + param_desc.upp = INT_MAX; + param_desc.is_low_incl = 1; + param_desc.is_upp_incl = 1; + res = parse_param_int(shitran, tk, path, line_num, &param_desc, &isotope.id); + if(res != RES_OK) goto error; + + tk = strtok_r(NULL, " \t", &tk_ctx); + param_desc.name = "isotope abundance"; + param_desc.low = 0; + param_desc.upp = 1; + param_desc.is_low_incl = 0; + param_desc.is_upp_incl = 1; + res = parse_param_double + (shitran, tk, path, line_num, &param_desc, &isotope.abundance); + if(res != RES_OK) goto error; + + tk = strtok_r(NULL, " \t", &tk_ctx); + param_desc.name = "isotope Q(256K)"; + param_desc.low = 0; + param_desc.upp = INF; + param_desc.is_low_incl = 0; + param_desc.is_upp_incl = 1; + res = parse_param_double + (shitran, tk, path, line_num, &param_desc, &isotope.Q296K); + if(res != RES_OK) goto error; + + tk = strtok_r(NULL, " \t", &tk_ctx); + param_desc.name = "isotope state independant degeneracy factor"; + param_desc.low = -INT_MAX; + param_desc.upp = INT_MAX; + param_desc.is_low_incl = 1; + param_desc.is_upp_incl = 1; + res = parse_param_int + (shitran, tk, path, line_num, &param_desc, &isotope.gj); + if(res != RES_OK) goto error; + + tk = strtok_r(NULL, " \t", &tk_ctx), + param_desc.name = "isotope molar mass"; + param_desc.low = 0; + param_desc.upp = INF; + param_desc.is_low_incl = 0; + param_desc.is_upp_incl = 1; + res = parse_param_double + (shitran, tk, path, line_num, &param_desc, &isotope.molar_mass); + if(res != RES_OK) goto error; + + /* Fetch the index where the isotope is going to be store */ + ientry = darray_isotope_size_get(&metadata->isotopes); + + /* Store the isotope */ + res = darray_isotope_push_back(&metadata->isotopes, &isotope); + if(res != RES_OK) { + log_err(shitran, "%s:%lu: error storing the isotope %d -- %s.\n", + path, (unsigned long)line_num, isotope.id, res_to_cstr(res)); + res = RES_OK; + goto error; + } + + /* Register the isotope */ + if(htable_id2entry_find(&metadata->isoid2idx, &isotope.id)) { + log_warn(shitran, + "%s:%lu: an isotope with the same identifier %i was already registered.\n", + path, (unsigned long)line_num, isotope.id); + res = RES_OK; + goto error; + } + res = htable_id2entry_set(&metadata->isoid2idx, &isotope.id, &ientry); + if(res != RES_OK) { + log_err(shitran, "%s:%lu: error registering the isotopoe %d -- %s.\n", + path, (unsigned long)line_num, isotope.id, res_to_cstr(res)); + res = RES_OK; + goto error; + } + +exit: + return res; +error: + if(ientry != SIZE_MAX) darray_isotope_resize(&metadata->isotopes, ientry); + htable_id2entry_erase(&metadata->isoid2idx, &isotope.id); + goto exit; +} + +static res_T +parse_line + (struct shitran_isotope_metadata* metadata, + struct molecule* molecule, /* Currently parsed molecule */ + struct txtrdr* txtrdr) +{ + const char* line = NULL; + size_t i; + res_T res = RES_OK; + ASSERT(metadata && molecule && txtrdr); + + line = txtrdr_get_cline(txtrdr); + ASSERT(line); + i = strspn(line, " \t"); + ASSERT(i < strlen(line)); + + if(!isalpha(line[i])) { + res = parse_isotope(metadata, txtrdr); + if(res != RES_OK) goto error; + } else { + if(MOLECULE_IS_VALID(molecule)) { + res = flush_molecule(metadata, molecule, txtrdr); + if(res != RES_OK) goto error; + } + res = parse_molecule(metadata, molecule, txtrdr); + if(res != RES_OK) goto error; + } + +exit: + return res; +error: + goto exit; +} + +static res_T +load_stream + (struct shitran* shitran, + FILE* stream, + const char* name, + struct shitran_isotope_metadata** out_isotopes) +{ + struct molecule molecule; /* Current molecule */ + struct shitran_isotope_metadata* metadata = NULL; + struct txtrdr* txtrdr = NULL; + res_T res = RES_OK; + ASSERT(shitran && stream && name && out_isotopes); + + molecule_init(shitran->allocator, &molecule); + + res = create_isotoplogues(shitran, &metadata); + if(res != RES_OK) goto error; + + res = txtrdr_stream(metadata->shitran->allocator, stream, name, + 0/*No comment char*/, &txtrdr); + if(res != RES_OK) { + log_err(shitran, "%s: error creating the text reader -- %s.\n", + name, res_to_cstr(res)); + goto error; + } + + #define READ_LINE { \ + res = txtrdr_read_line(txtrdr); \ + if(res != RES_OK) { \ + log_err(shitran, "%s: error reading the line `%lu' -- %s.\n", \ + name, (unsigned long)txtrdr_get_line_num(txtrdr), res_to_cstr(res)); \ + goto error; \ + } \ + } (void)0 + + /* Skip the 1st line that is a comment line*/ + READ_LINE; + if(!txtrdr_get_cline(txtrdr)) goto exit; + + for(;;) { + READ_LINE; + + if(!txtrdr_get_cline(txtrdr)) break; /* No more parsed line */ + res = parse_line(metadata, &molecule, txtrdr); + if(res != RES_OK) goto error; + } + #undef READ_LINE + + if(MOLECULE_IS_VALID(&molecule)) { + res = flush_molecule(metadata, &molecule, txtrdr); + if(res != RES_OK) goto error; + } + +exit: + if(txtrdr) txtrdr_ref_put(txtrdr); + *out_isotopes = metadata; + molecule_release(&molecule); + return res; +error: + goto exit; +} + +static void +release_isotope_metadata(ref_T* ref) +{ + struct shitran* shitran = NULL; + struct shitran_isotope_metadata* metadata = CONTAINER_OF + (ref, struct shitran_isotope_metadata, ref); + ASSERT(ref); + shitran = metadata->shitran; + darray_molecule_release(&metadata->molecules); + darray_isotope_release(&metadata->isotopes); + htable_id2entry_release(&metadata->molid2idx); + htable_id2entry_release(&metadata->isoid2idx); + MEM_RM(shitran->allocator, metadata); + SHITRAN(ref_put(shitran)); +} + +/******************************************************************************* + * Exported functions + ******************************************************************************/ +res_T +shitran_isotope_metadata_load + (struct shitran* shitran, + const char* path, + struct shitran_isotope_metadata** metadata) +{ + FILE* file = NULL; + res_T res = RES_OK; + + if(!shitran || !path || !metadata) { + res = RES_BAD_ARG; + goto error; + } + + file = fopen(path, "r"); + if(!file) { + log_err(shitran, "%s: error opening file `%s'.\n", FUNC_NAME, path); + res = RES_IO_ERR; + goto error; + } + + res = load_stream(shitran, file, path, metadata); + if(res != RES_OK) goto error; + +exit: + if(file) fclose(file); + return res; +error: + goto exit; +} + +res_T +shitran_isotope_metadata_load_stream + (struct shitran* shitran, + FILE* stream, + const char* stream_name, + struct shitran_isotope_metadata** metadata) +{ + if(!shitran || !stream || !metadata) return RES_BAD_ARG; + return load_stream + (shitran, stream, stream_name ? stream_name : "<stream>", metadata); +} + +res_T +shitran_isotope_metadata_ref_get + (struct shitran_isotope_metadata* metadata) +{ + if(!metadata) return RES_BAD_ARG; + ref_get(&metadata->ref); + return RES_OK; +} + +res_T +shitran_isotope_metadata_ref_put + (struct shitran_isotope_metadata* metadata) +{ + if(!metadata) return RES_BAD_ARG; + ref_put(&metadata->ref, release_isotope_metadata); + return RES_OK; +} + +res_T +shitran_isotope_metadata_get_molecules_count + (const struct shitran_isotope_metadata* metadata, + size_t* nmolecules) +{ + if(!metadata || !nmolecules) return RES_BAD_ARG; + *nmolecules = darray_molecule_size_get(&metadata->molecules); + return RES_OK; + +} + +res_T +shitran_isotope_metadata_get_molecule + (const struct shitran_isotope_metadata* metadata, + const size_t imolecule, + struct shitran_molecule* out_molecule) +{ + const struct molecule* molecule = NULL; + res_T res = RES_OK; + + if(!metadata || !out_molecule) { + res = RES_BAD_ARG; + goto error; + } + if(imolecule >= darray_molecule_size_get(&metadata->molecules)) { + log_err(metadata->shitran, "%s: invalid molecule index %lu.\n", + FUNC_NAME, (unsigned long)imolecule); + res = RES_BAD_ARG; + goto error; + } + + molecule = darray_molecule_cdata_get(&metadata->molecules) + imolecule; + out_molecule->name = str_cget(&molecule->name); + out_molecule->id = molecule->id; + out_molecule->nisotopes = + molecule->isotopes_range[1] - molecule->isotopes_range[0]; + out_molecule->isotopes = + darray_isotope_cdata_get(&metadata->isotopes) + molecule->isotopes_range[0]; + +exit: + return res; +error: + goto exit; +} + +res_T +shitran_isotope_metadata_find_molecule + (struct shitran_isotope_metadata* metadata, + const int molecule_id, + struct shitran_molecule* out_molecule) +{ + size_t* pimolecule = NULL; + res_T res = RES_OK; + + if(!metadata || !out_molecule) { + res = RES_BAD_ARG; + goto error; + } + + pimolecule = htable_id2entry_find(&metadata->molid2idx, &molecule_id); + if(!pimolecule) { + *out_molecule = SHITRAN_MOLECULE_NULL; + } else { + res = shitran_isotope_metadata_get_molecule + (metadata, *pimolecule, out_molecule); + if(res != RES_OK) goto error; + } + +exit: + return res; +error: + goto exit; +} + diff --git a/src/shitran_log.c b/src/shitran_log.c @@ -0,0 +1,127 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT + * Copyright (C) 2022 Université Paul Sabatier - Laplace + * + * 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 "shitran_c.h" +#include "shitran_log.h" + +#include <rsys/cstr.h> +#include <rsys/logger.h> + +#include <stdarg.h> + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static INLINE void +log_msg + (const struct shitran* shitran, + const enum log_type stream, + const char* msg, + va_list vargs) +{ + ASSERT(shitran && msg); + if(shitran->verbose) { + res_T res; (void)res; + res = logger_vprint(shitran->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 shitran* shitran) +{ + res_T res = RES_OK; + ASSERT(shitran); + + res = logger_init(shitran->allocator, &shitran->logger__); + if(res != RES_OK) { + if(shitran->verbose) { + fprintf(stderr, + MSG_ERROR_PREFIX + "Could not setup the default logger for the Star-HITRAN library -- %s.\n", + res_to_cstr(res)); + } + goto error; + } + logger_set_stream(&shitran->logger__, LOG_OUTPUT, print_info, NULL); + logger_set_stream(&shitran->logger__, LOG_ERROR, print_err, NULL); + logger_set_stream(&shitran->logger__, LOG_WARNING, print_warn, NULL); + shitran->logger = &shitran->logger__; + +exit: + return res; +error: + goto exit; +} + +void +log_info(const struct shitran* shitran, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(shitran && msg); + + va_start(vargs_list, msg); + log_msg(shitran, LOG_OUTPUT, msg, vargs_list); + va_end(vargs_list); +} + +void +log_err(const struct shitran* shitran, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(shitran && msg); + + va_start(vargs_list, msg); + log_msg(shitran, LOG_ERROR, msg, vargs_list); + va_end(vargs_list); +} + +void +log_warn(const struct shitran* shitran, const char* msg, ...) +{ + va_list vargs_list; + ASSERT(shitran && msg); + + va_start(vargs_list, msg); + log_msg(shitran, LOG_WARNING, msg, vargs_list); + va_end(vargs_list); +} diff --git a/src/shitran_log.h b/src/shitran_log.h @@ -0,0 +1,73 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT + * Copyright (C) 2022 Université Paul Sabatier - Laplace + * + * 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 SHITRAN_LOG_H +#define SHITRAN_LOG_H + +#include <rsys/rsys.h> + +#define MSG_INFO_PREFIX "Star-HITRAN:\x1b[1m\x1b[32minfo\x1b[0m: " +#define MSG_ERROR_PREFIX "Star-HITRAN:\x1b[1m\x1b[31merror\x1b[0m: " +#define MSG_WARNING_PREFIX "Star-HITRAN:\x1b[1m\x1b[33mwarning\x1b[0m: " + +struct shitran; +struct logger; + +extern LOCAL_SYM res_T +setup_log_default + (struct shitran* shitran); + +/* Conditionally log a message on the LOG_OUTPUT stream of the shitran logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_info + (const struct shitran* shitran, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +/* Conditionally log a message on the LOG_ERROR stream of the shitran logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_err + (const struct shitran* shitran, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + +/* Conditionally log a message on the LOG_WARNING stream of the shitran logger, + * with respect to its verbose flag */ +extern LOCAL_SYM void +log_warn + (const struct shitran* shitran, + const char* msg, + ...) +#ifdef COMPILER_GCC + __attribute((format(printf, 2, 3))) +#endif +; + + +#endif /* SHITRAN_LOG_H */ + diff --git a/src/sht.c b/src/sht.c @@ -1,101 +0,0 @@ -/* Copyright (C) 2022 CNRS - LMD - * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) - * Copyright (C) 2022 Université Paul Sabatier - IRIT - * Copyright (C) 2022 Université Paul Sabatier - Laplace - * - * 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 "sht.h" -#include "sht_c.h" -#include "sht_log.h" - -/******************************************************************************* - * Helper functions - ******************************************************************************/ -static INLINE res_T -check_sht_create_args(const struct sht_create_args* args) -{ - return args ? RES_OK : RES_BAD_ARG; -} - -static void -release_sht(ref_T* ref) -{ - struct sht* sht = CONTAINER_OF(ref, struct sht, ref); - ASSERT(ref); - if(sht->logger == &sht->logger__) logger_release(&sht->logger__); - MEM_RM(sht->allocator, sht); -} - -/******************************************************************************* - * Exported functions - ******************************************************************************/ -res_T -sht_create - (const struct sht_create_args* args, - struct sht** out_sht) -{ - struct mem_allocator* allocator = NULL; - struct sht* sht = NULL; - res_T res = RES_OK; - - if(!out_sht) { res = RES_BAD_ARG; goto error; } - res = check_sht_create_args(args); - if(res != RES_OK) goto error; - - allocator = args->allocator ? args->allocator : &mem_default_allocator; - sht = MEM_CALLOC(allocator, 1, sizeof(*sht)); - if(!sht) { - #define ERR_STR "Could not allocate the Star-HITRAN data structure.\n" - 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(&sht->ref); - sht->allocator = allocator; - sht->verbose = args->verbose; - if(args->logger) { - sht->logger = args->logger; - } else { - setup_log_default(sht); - } - -exit: - if(out_sht) *out_sht = sht; - return res; -error: - if(sht) { SHT(ref_put(sht)); sht = NULL; } - goto exit; -} - -res_T -sht_ref_get(struct sht* sht) -{ - if(!sht) return RES_BAD_ARG; - ref_get(&sht->ref); - return RES_OK; -} - -res_T -sht_ref_put(struct sht* sht) -{ - if(!sht) return RES_BAD_ARG; - ref_put(&sht->ref, release_sht); - return RES_OK; -} diff --git a/src/sht.h b/src/sht.h @@ -1,142 +0,0 @@ -/* Copyright (C) 2022 CNRS - LMD - * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) - * Copyright (C) 2022 Université Paul Sabatier - IRIT - * Copyright (C) 2022 Université Paul Sabatier - Laplace - * - * 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 SHT_H -#define SHT_H - -#include <rsys/rsys.h> - -/* Library symbol management */ -#if defined(SHT_SHARED_BUILD) /* Build shared library */ - #define SHT_API extern EXPORT_SYM -#elif defined(SHT_STATIC) /* Use/build static library */ - #define SHT_API extern LOCAL_SYM -#else - #define SHT_API extern IMPORT_SYM -#endif - -/* Helper macro that asserts if the invocation of the sth function `Func' - * returns an error. One should use this macro on sth function calls for - * which no explicit error checking is performed */ -#ifndef NDEBUG - #define SHT(Func) ASSERT(sht_ ## Func == RES_OK) -#else - #define SHT(Func) sht_ ## Func -#endif - -struct sht_create_args { - struct logger* logger; /* May be NULL <=> default logger */ - struct mem_allocator* allocator; /* NULL <=> use default allocator */ - int verbose; /* Verbosity level */ -}; -#define SHT_CREATE_ARGS_DEFAULT__ {NULL, NULL, 0} -static const struct sht_create_args SHT_CREATE_ARGS_DEFAULT = - SHT_CREATE_ARGS_DEFAULT__; - -struct sht_isotope { - double abundance; /* in ]0, 1] */ - double Q296K; /* Partition function at Tref = 296K */ - double molar_mass; /* In g */ - size_t molecule; /* Index of the molecule to which the isotope belongs */ - int gj; /* State independent degeneracy factor */ - int id; /* Unique identifier of the isotope */ -}; -#define SHT_ISOTOPOLOGUE_NULL__ {0,0,0,0,0,0} -static const struct sht_isotope SHT_ISOTOPOLOGUE_NULL = SHT_ISOTOPOLOGUE_NULL__; - -struct sht_molecule { - const char* name; - size_t nisotopes; /* Number of isotopes */ - const struct sht_isotope* isotopes; - int id; /* Unique identifier */ -}; -#define SHT_MOLECULE_NULL__ {NULL, 0, NULL, -1} -static const struct sht_molecule SHT_MOLECULE_NULL = SHT_MOLECULE_NULL__; - -#define SHT_MOLECULE_IS_NULL(Molecule) \ - ( (Molecule)->name == SHT_MOLECULE_NULL.name \ - && (Molecule)->nisotopes == SHT_MOLECULE_NULL.nisotopes \ - && (Molecule)->isotopes == SHT_MOLECULE_NULL.isotopes \ - && (Molecule)->id == SHT_MOLECULE_NULL.id) - -/* Forward declarations */ -struct sht; -struct sht_isotope_metadata; - -BEGIN_DECLS - -/******************************************************************************* - * Device API - ******************************************************************************/ -SHT_API res_T -sht_create - (const struct sht_create_args* args, - struct sht** sht); - -SHT_API res_T -sht_ref_get - (struct sht* sht); - -SHT_API res_T -sht_ref_put - (struct sht* sht); - -/******************************************************************************* - * Isotope metadata API - ******************************************************************************/ -SHT_API res_T -sht_isotope_metadata_load - (struct sht* sht, - const char* path, - struct sht_isotope_metadata** metadata); - -SHT_API res_T -sht_isotope_metadata_load_stream - (struct sht* sht, - FILE* stream, - const char* stream_name, /* NULL <=> use default stream name */ - struct sht_isotope_metadata** metadata); - -SHT_API res_T -sht_isotope_metadata_ref_get - (struct sht_isotope_metadata* metadata); - -SHT_API res_T -sht_isotope_metadata_ref_put - (struct sht_isotope_metadata* metadata); - -SHT_API res_T -sht_isotope_metadata_get_molecules_count - (const struct sht_isotope_metadata* metadata, - size_t* nmolecules); - -SHT_API res_T -sht_isotope_metadata_get_molecule - (const struct sht_isotope_metadata* metadata, - const size_t imolecule, /* Index of the molecule in [0, molecules_count[ */ - struct sht_molecule* molecule); - -SHT_API res_T -sht_isotope_metadata_find_molecule - (struct sht_isotope_metadata* metadata, - const int molecule_id, /* Unique identifier of the molecule */ - struct sht_molecule* molecule); /* SHT_MOLECULE_NULL if the molecule is not found */ - -END_DECLS - -#endif /* SHT_H */ diff --git a/src/sht_c.h b/src/sht_c.h @@ -1,35 +0,0 @@ -/* Copyright (C) 2022 CNRS - LMD - * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) - * Copyright (C) 2022 Université Paul Sabatier - IRIT - * Copyright (C) 2022 Université Paul Sabatier - Laplace - * - * 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 SHT_C_H -#define SHT_C_H - -#include <rsys/logger.h> -#include <rsys/ref_count.h> - -struct mem_allocator; - -struct sht { - struct mem_allocator* allocator; - struct logger* logger; - struct logger logger__; /* Default logger */ - int verbose; - ref_T ref; -}; - -#endif /* SHT_C_H */ diff --git a/src/sht_isotope_metadata.c b/src/sht_isotope_metadata.c @@ -1,704 +0,0 @@ -/* Copyright (C) 2022 CNRS - LMD - * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) - * Copyright (C) 2022 Université Paul Sabatier - IRIT - * Copyright (C) 2022 Université Paul Sabatier - Laplace - * - * 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/>. */ - -#define _POSIX_C_SOURCE 200112L /* strtok_r support */ - -#include "sht.h" -#include "sht_c.h" -#include "sht_log.h" - -#include <rsys/cstr.h> -#include <rsys/dynamic_array.h> -#include <rsys/hash_table.h> -#include <rsys/ref_count.h> -#include <rsys/str.h> -#include <rsys/text_reader.h> - -#include <ctype.h> -#include <string.h> - -#define C_FORMAT_double "%g" -#define C_FORMAT_int "%d" - -struct param_desc { - const char* path; /* Path where the param lies */ - const char* name; /* Name of the param */ - size_t iline; /* Line number of the param */ - - /* Domain of the param */ - double low, upp; - int is_low_incl; /* Define if the lower bound is inclusive */ - int is_upp_incl; /* Define if the upper bound is inclusive */ -}; -#define PARAM_DESC_NULL__ {NULL, NULL, 0, 0, 0, 0, 0} -static const struct param_desc PARAM_DESC_NULL = PARAM_DESC_NULL__; - -/* Generate the dynamic array of isotopes */ -#define DARRAY_NAME isotope -#define DARRAY_DATA struct sht_isotope -#include <rsys/dynamic_array.h> - -struct molecule { - struct str name; - size_t isotopes_range[2]; /* Range of the [1st and last[ isotopes */ - int id; /* Unique identifier of the molecule */ -}; -#define MOLECULE_IS_VALID(Molecule) ((Molecule)->id >= 0) - -static INLINE void -molecule_clear(struct molecule* molecule) -{ - ASSERT(molecule); - molecule->isotopes_range[0] = SIZE_MAX; - molecule->isotopes_range[1] = 0; - molecule->id = -1; -} - -static INLINE void -molecule_init(struct mem_allocator* allocator, struct molecule* molecule) -{ - str_init(allocator, &molecule->name); - molecule_clear(molecule); -} - -static INLINE void -molecule_release(struct molecule* molecule) -{ - str_release(&molecule->name); -} - -static INLINE res_T -molecule_copy(struct molecule* dst, const struct molecule* src) -{ - dst->isotopes_range[0] = src->isotopes_range[0]; - dst->isotopes_range[1] = src->isotopes_range[1]; - dst->id = src->id; - return str_copy(&dst->name, &src->name); -} - -static INLINE res_T -molecule_copy_and_release(struct molecule* dst, struct molecule* src) -{ - dst->isotopes_range[0] = src->isotopes_range[0]; - dst->isotopes_range[1] = src->isotopes_range[1]; - dst->id = src->id; - return str_copy_and_release(&dst->name, &src->name); -} - -/* Generate the dynamic array of molecules */ -#define DARRAY_NAME molecule -#define DARRAY_DATA struct molecule -#define DARRAY_FUNCTOR_INIT molecule_init -#define DARRAY_FUNCTOR_RELEASE molecule_release -#define DARRAY_FUNCTOR_COPY molecule_copy -#define DARRAY_FUNCTOR_COPY_AND_RELEASE molecule_copy_and_release -#include <rsys/dynamic_array.h> - -/* Generate the hash table that map a unique identifier to its index */ -#define HTABLE_NAME id2entry -#define HTABLE_KEY int /* Unique identifier */ -#define HTABLE_DATA size_t /* Index of the corresponding registered data */ -#include <rsys/hash_table.h> - -struct sht_isotope_metadata { - /* List of molecules and isotopes */ - struct darray_molecule molecules; - struct darray_isotope isotopes; - - /* Map the identifier of a molecule/isotope to its correspond index into - * their corresponding dynamic arays into which they are registered */ - struct htable_id2entry molid2idx; - struct htable_id2entry isoid2idx; - - struct sht* sht; - ref_T ref; -}; - -/******************************************************************************* - * Helper functions - ******************************************************************************/ -#define DEFINE_PARSE_PARAM_FUNCTION(Type) \ - static res_T \ - parse_param_##Type \ - (struct sht* sht, \ - const char* str, \ - const char* path, \ - size_t line_num, \ - const struct param_desc* desc, \ - Type* out_param) \ - { \ - Type param = 0; \ - res_T res = RES_OK; \ - ASSERT(sht && path && desc && out_param); \ - ASSERT(desc->low < desc->upp \ - || (desc->low == desc->upp && desc->is_low_incl && desc->is_upp_incl));\ - \ - if(!str) { \ - log_err(sht, "%s:%lu: %s is missing.\n", \ - path, (unsigned long)line_num, desc->name); \ - res = RES_BAD_ARG; \ - goto error; \ - } \ - \ - res = cstr_to_##Type(str, &param); \ - if(res != RES_OK) { \ - log_err(sht, "%s:%lu: invalid %s `%s'.\n", \ - desc->path, (unsigned long)desc->iline, desc->name, str); \ - res = RES_BAD_ARG; \ - goto error; \ - } \ - \ - if(param < desc->low || (param == desc->low && !desc->is_low_incl) \ - || param > desc->upp || (param == desc->upp && !desc->is_upp_incl)) { \ - log_err(sht, \ - "%s:%lu: invalid isotope %s `%s'. It must be in " \ - "%c"CONCAT(C_FORMAT_, Type)", "CONCAT(C_FORMAT_, Type)"%c.\n", \ - path, (unsigned long)line_num, desc->name, str, \ - desc->is_low_incl ? '[' : ']', (Type)desc->low, \ - (Type)desc->upp, desc->is_upp_incl ? ']' : '['); \ - res = RES_BAD_ARG; \ - goto error; \ - } \ - \ - exit: \ - *out_param = param; \ - return res; \ - error: \ - goto exit; \ - } -DEFINE_PARSE_PARAM_FUNCTION(double) -DEFINE_PARSE_PARAM_FUNCTION(int) -#undef DEFINE_PARSE_PARAM_FUNCTION - -static res_T -create_isotoplogues - (struct sht* sht, - struct sht_isotope_metadata** out_isotopes) -{ - struct sht_isotope_metadata* metadata = NULL; - res_T res = RES_OK; - ASSERT(sht && out_isotopes); - - metadata = MEM_CALLOC(sht->allocator, 1, sizeof(*metadata)); - if(!metadata) { - log_err(sht, "Could not allocate the metadata data structure.\n"); - res = RES_MEM_ERR; - goto error; - } - ref_init(&metadata->ref); - SHT(ref_get(sht)); - metadata->sht = sht; - darray_molecule_init(sht->allocator, &metadata->molecules); - darray_isotope_init(sht->allocator, &metadata->isotopes); - htable_id2entry_init(sht->allocator, &metadata->molid2idx); - htable_id2entry_init(sht->allocator, &metadata->isoid2idx); - -exit: - *out_isotopes = metadata; - return res; -error: - goto exit; -} - -static res_T -flush_molecule - (struct sht_isotope_metadata* metadata, - struct molecule* molecule, /* Currently parsed molecule */ - struct txtrdr* txtrdr) -{ - size_t ientry = SIZE_MAX; - size_t* pimolecule = NULL; - res_T res = RES_OK; - ASSERT(metadata && molecule && MOLECULE_IS_VALID(molecule)); - - /* Fetch _exclusive_ upper bound */ - molecule->isotopes_range[1] = - darray_isotope_size_get(&metadata->isotopes); - if(molecule->isotopes_range[0] >= molecule->isotopes_range[1]) { - log_warn(metadata->sht, - "%s: the %s molecule does not have any isotope.\n", - txtrdr_get_name(txtrdr), str_cget(&molecule->name)); - } - - /* Fetch the index where the molecule is going to be store */ - ientry = darray_molecule_size_get(&metadata->molecules); - - /* Store the molecule */ - res = darray_molecule_push_back(&metadata->molecules, molecule); - if(res != RES_OK) { - log_err(metadata->sht, - "%s: error storing the %s molecule -- %s.\n", - txtrdr_get_name(txtrdr), str_cget(&molecule->name), res_to_cstr(res)); - goto error; - } - - /* Register the molecule */ - pimolecule = htable_id2entry_find(&metadata->molid2idx, &molecule->id); - if(pimolecule) { - const struct molecule* molecule2 = NULL; - molecule2 = darray_molecule_cdata_get(&metadata->molecules) + *pimolecule; - log_err(metadata->sht, - "%s: cannot register the %s molecule. " - "The %s molecule has the same identifier %i.\n", - txtrdr_get_name(txtrdr), - str_cget(&molecule->name), - str_cget(&molecule2->name), - molecule->id); - res = RES_OK; - goto error; - } - res = htable_id2entry_set(&metadata->molid2idx, &molecule->id, &ientry); - if(res != RES_OK) { - log_err(metadata->sht, - "%s: error registering the %s molecule -- %s.\n", - txtrdr_get_name(txtrdr), str_cget(&molecule->name), res_to_cstr(res)); - goto error; - } - - molecule_clear(molecule); - -exit: - return res; -error: - if(ientry != SIZE_MAX) darray_molecule_resize(&metadata->molecules, ientry); - htable_id2entry_erase(&metadata->molid2idx, &molecule->id); - goto exit; -} - -static res_T -parse_molecule - (struct sht_isotope_metadata* metadata, - struct molecule* molecule, - struct txtrdr* txtrdr) -{ - char* name = NULL; - char* id = NULL; - char* tk = NULL; - char* tk_ctx = NULL; - size_t len; - res_T res = RES_OK; - ASSERT(molecule && txtrdr); - - name = strtok_r(txtrdr_get_line(txtrdr), " \t", &tk_ctx); - id = strtok_r(NULL, " \t", &tk_ctx); - - if(!name) { - log_err(metadata->sht, "%s:%lu: molecule name is missing.\n", - txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr)); - res = RES_BAD_ARG; - goto error; - } - - res = str_set(&molecule->name, name); - if(res != RES_OK) { - log_err(metadata->sht, - "%s:%lu: error seting the molecule name `%s' -- %s.\n", - txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr), - name, res_to_cstr(res)); - goto error; - } - - len = strlen(id); - if(!id || !len || id[0] != '(' || id[len-1] != ')') { - log_err(metadata->sht, "%s:%lu: invalid molecule identifier.\n", - txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr)); - res = RES_BAD_ARG; - goto error; - } - - id[len-1] = '\0'; /* Rm trailing parenthesis */ - res = cstr_to_int(id+1/*Rm leading parenthesis*/, &molecule->id); - if(res != RES_OK || !MOLECULE_IS_VALID(molecule)) { - id[len-1] = ')'; /* Re-add the trailing parenthesis */ - log_err(metadata->sht, "%s:%lu: invalid molecule identifier `%s'.\n", - txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr), id); - res = RES_BAD_ARG; - goto error; - } - - tk = strtok_r(NULL, " \t", &tk_ctx); - if(tk) { - log_warn(metadata->sht, "%s:%lu: unexpected text `%s'.\n", - txtrdr_get_name(txtrdr), (unsigned long)txtrdr_get_line_num(txtrdr), tk); - } - - molecule->isotopes_range[0] = darray_isotope_size_get(&metadata->isotopes); - -exit: - return res; -error: - goto exit; -} - -static res_T -parse_isotope - (struct sht_isotope_metadata* metadata, - struct txtrdr* txtrdr) -{ - struct sht_isotope isotope = SHT_ISOTOPOLOGUE_NULL; - struct param_desc param_desc = PARAM_DESC_NULL; - struct sht* sht = NULL; - char* tk = NULL; - char* tk_ctx = NULL; - const char* path = NULL; - size_t line_num = 0; - size_t ientry = SIZE_MAX; - res_T res = RES_OK; - ASSERT(metadata && txtrdr); - - sht = metadata->sht; - path = txtrdr_get_name(txtrdr); - line_num = txtrdr_get_line_num(txtrdr); - - /* Fetch the index of the molecule to which the isotope belongs */ - isotope.molecule = darray_molecule_size_get(&metadata->molecules); - - tk = strtok_r(txtrdr_get_line(txtrdr), " \t", &tk_ctx); - param_desc.name = "isotope id"; - param_desc.low = 0; - param_desc.upp = INT_MAX; - param_desc.is_low_incl = 1; - param_desc.is_upp_incl = 1; - res = parse_param_int(sht, tk, path, line_num, &param_desc, &isotope.id); - if(res != RES_OK) goto error; - - tk = strtok_r(NULL, " \t", &tk_ctx); - param_desc.name = "isotope abundance"; - param_desc.low = 0; - param_desc.upp = 1; - param_desc.is_low_incl = 0; - param_desc.is_upp_incl = 1; - res = parse_param_double - (sht, tk, path, line_num, &param_desc, &isotope.abundance); - if(res != RES_OK) goto error; - - tk = strtok_r(NULL, " \t", &tk_ctx); - param_desc.name = "isotope Q(256K)"; - param_desc.low = 0; - param_desc.upp = INF; - param_desc.is_low_incl = 0; - param_desc.is_upp_incl = 1; - res = parse_param_double - (sht, tk, path, line_num, &param_desc, &isotope.Q296K); - if(res != RES_OK) goto error; - - tk = strtok_r(NULL, " \t", &tk_ctx); - param_desc.name = "isotope state independant degeneracy factor"; - param_desc.low = -INT_MAX; - param_desc.upp = INT_MAX; - param_desc.is_low_incl = 1; - param_desc.is_upp_incl = 1; - res = parse_param_int - (sht, tk, path, line_num, &param_desc, &isotope.gj); - if(res != RES_OK) goto error; - - tk = strtok_r(NULL, " \t", &tk_ctx), - param_desc.name = "isotope molar mass"; - param_desc.low = 0; - param_desc.upp = INF; - param_desc.is_low_incl = 0; - param_desc.is_upp_incl = 1; - res = parse_param_double - (sht, tk, path, line_num, &param_desc, &isotope.molar_mass); - if(res != RES_OK) goto error; - - /* Fetch the index where the isotope is going to be store */ - ientry = darray_isotope_size_get(&metadata->isotopes); - - /* Store the isotope */ - res = darray_isotope_push_back(&metadata->isotopes, &isotope); - if(res != RES_OK) { - log_err(sht, "%s:%lu: error storing the isotope %d -- %s.\n", - path, (unsigned long)line_num, isotope.id, res_to_cstr(res)); - res = RES_OK; - goto error; - } - - /* Register the isotope */ - if(htable_id2entry_find(&metadata->isoid2idx, &isotope.id)) { - log_warn(sht, - "%s:%lu: an isotope with the same identifier %i was already registered.\n", - path, (unsigned long)line_num, isotope.id); - res = RES_OK; - goto error; - } - res = htable_id2entry_set(&metadata->isoid2idx, &isotope.id, &ientry); - if(res != RES_OK) { - log_err(sht, "%s:%lu: error registering the isotopoe %d -- %s.\n", - path, (unsigned long)line_num, isotope.id, res_to_cstr(res)); - res = RES_OK; - goto error; - } - -exit: - return res; -error: - if(ientry != SIZE_MAX) darray_isotope_resize(&metadata->isotopes, ientry); - htable_id2entry_erase(&metadata->isoid2idx, &isotope.id); - goto exit; -} - -static res_T -parse_line - (struct sht_isotope_metadata* metadata, - struct molecule* molecule, /* Currently parsed molecule */ - struct txtrdr* txtrdr) -{ - const char* line = NULL; - size_t i; - res_T res = RES_OK; - ASSERT(metadata && molecule && txtrdr); - - line = txtrdr_get_cline(txtrdr); - ASSERT(line); - i = strspn(line, " \t"); - ASSERT(i < strlen(line)); - - if(!isalpha(line[i])) { - res = parse_isotope(metadata, txtrdr); - if(res != RES_OK) goto error; - } else { - if(MOLECULE_IS_VALID(molecule)) { - res = flush_molecule(metadata, molecule, txtrdr); - if(res != RES_OK) goto error; - } - res = parse_molecule(metadata, molecule, txtrdr); - if(res != RES_OK) goto error; - } - -exit: - return res; -error: - goto exit; -} - -static res_T -load_stream - (struct sht* sht, - FILE* stream, - const char* name, - struct sht_isotope_metadata** out_isotopes) -{ - struct molecule molecule; /* Current molecule */ - struct sht_isotope_metadata* metadata = NULL; - struct txtrdr* txtrdr = NULL; - res_T res = RES_OK; - ASSERT(sht && stream && name && out_isotopes); - - molecule_init(sht->allocator, &molecule); - - res = create_isotoplogues(sht, &metadata); - if(res != RES_OK) goto error; - - res = txtrdr_stream(metadata->sht->allocator, stream, name, - 0/*No comment char*/, &txtrdr); - if(res != RES_OK) { - log_err(sht, "%s: error creating the text reader -- %s.\n", - name, res_to_cstr(res)); - goto error; - } - - #define READ_LINE { \ - res = txtrdr_read_line(txtrdr); \ - if(res != RES_OK) { \ - log_err(sht, "%s: error reading the line `%lu' -- %s.\n", \ - name, (unsigned long)txtrdr_get_line_num(txtrdr), res_to_cstr(res)); \ - goto error; \ - } \ - } (void)0 - - /* Skip the 1st line that is a comment line*/ - READ_LINE; - if(!txtrdr_get_cline(txtrdr)) goto exit; - - for(;;) { - READ_LINE; - - if(!txtrdr_get_cline(txtrdr)) break; /* No more parsed line */ - res = parse_line(metadata, &molecule, txtrdr); - if(res != RES_OK) goto error; - } - #undef READ_LINE - - if(MOLECULE_IS_VALID(&molecule)) { - res = flush_molecule(metadata, &molecule, txtrdr); - if(res != RES_OK) goto error; - } - -exit: - if(txtrdr) txtrdr_ref_put(txtrdr); - *out_isotopes = metadata; - molecule_release(&molecule); - return res; -error: - goto exit; -} - -static void -release_isotope_metadata(ref_T* ref) -{ - struct sht* sht = NULL; - struct sht_isotope_metadata* metadata = CONTAINER_OF - (ref, struct sht_isotope_metadata, ref); - ASSERT(ref); - sht = metadata->sht; - darray_molecule_release(&metadata->molecules); - darray_isotope_release(&metadata->isotopes); - htable_id2entry_release(&metadata->molid2idx); - htable_id2entry_release(&metadata->isoid2idx); - MEM_RM(sht->allocator, metadata); - SHT(ref_put(sht)); -} - -/******************************************************************************* - * Exported functions - ******************************************************************************/ -res_T -sht_isotope_metadata_load - (struct sht* sht, - const char* path, - struct sht_isotope_metadata** metadata) -{ - FILE* file = NULL; - res_T res = RES_OK; - - if(!sht || !path || !metadata) { - res = RES_BAD_ARG; - goto error; - } - - file = fopen(path, "r"); - if(!file) { - log_err(sht, "%s: error opening file `%s'.\n", FUNC_NAME, path); - res = RES_IO_ERR; - goto error; - } - - res = load_stream(sht, file, path, metadata); - if(res != RES_OK) goto error; - -exit: - if(file) fclose(file); - return res; -error: - goto exit; -} - -res_T -sht_isotope_metadata_load_stream - (struct sht* sht, - FILE* stream, - const char* stream_name, - struct sht_isotope_metadata** metadata) -{ - if(!sht || !stream || !metadata) return RES_BAD_ARG; - return load_stream - (sht, stream, stream_name ? stream_name : "<stream>", metadata); -} - -res_T -sht_isotope_metadata_ref_get - (struct sht_isotope_metadata* metadata) -{ - if(!metadata) return RES_BAD_ARG; - ref_get(&metadata->ref); - return RES_OK; -} - -res_T -sht_isotope_metadata_ref_put - (struct sht_isotope_metadata* metadata) -{ - if(!metadata) return RES_BAD_ARG; - ref_put(&metadata->ref, release_isotope_metadata); - return RES_OK; -} - -res_T -sht_isotope_metadata_get_molecules_count - (const struct sht_isotope_metadata* metadata, - size_t* nmolecules) -{ - if(!metadata || !nmolecules) return RES_BAD_ARG; - *nmolecules = darray_molecule_size_get(&metadata->molecules); - return RES_OK; - -} - -res_T -sht_isotope_metadata_get_molecule - (const struct sht_isotope_metadata* metadata, - const size_t imolecule, - struct sht_molecule* out_molecule) -{ - const struct molecule* molecule = NULL; - res_T res = RES_OK; - - if(!metadata || !out_molecule) { - res = RES_BAD_ARG; - goto error; - } - if(imolecule >= darray_molecule_size_get(&metadata->molecules)) { - log_err(metadata->sht, "%s: invalid molecule index %lu.\n", - FUNC_NAME, (unsigned long)imolecule); - res = RES_BAD_ARG; - goto error; - } - - molecule = darray_molecule_cdata_get(&metadata->molecules) + imolecule; - out_molecule->name = str_cget(&molecule->name); - out_molecule->id = molecule->id; - out_molecule->nisotopes = - molecule->isotopes_range[1] - molecule->isotopes_range[0]; - out_molecule->isotopes = - darray_isotope_cdata_get(&metadata->isotopes) + molecule->isotopes_range[0]; - -exit: - return res; -error: - goto exit; -} - -res_T -sht_isotope_metadata_find_molecule - (struct sht_isotope_metadata* metadata, - const int molecule_id, - struct sht_molecule* out_molecule) -{ - size_t* pimolecule = NULL; - res_T res = RES_OK; - - if(!metadata || !out_molecule) { - res = RES_BAD_ARG; - goto error; - } - - pimolecule = htable_id2entry_find(&metadata->molid2idx, &molecule_id); - if(!pimolecule) { - *out_molecule = SHT_MOLECULE_NULL; - } else { - res = sht_isotope_metadata_get_molecule(metadata, *pimolecule, out_molecule); - if(res != RES_OK) goto error; - } - -exit: - return res; -error: - goto exit; -} - diff --git a/src/sht_log.c b/src/sht_log.c @@ -1,127 +0,0 @@ -/* Copyright (C) 2022 CNRS - LMD - * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) - * Copyright (C) 2022 Université Paul Sabatier - IRIT - * Copyright (C) 2022 Université Paul Sabatier - Laplace - * - * 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 "sht_c.h" -#include "sht_log.h" - -#include <rsys/cstr.h> -#include <rsys/logger.h> - -#include <stdarg.h> - -/******************************************************************************* - * Helper functions - ******************************************************************************/ -static INLINE void -log_msg - (const struct sht* sht, - const enum log_type stream, - const char* msg, - va_list vargs) -{ - ASSERT(sht && msg); - if(sht->verbose) { - res_T res; (void)res; - res = logger_vprint(sht->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 sht* sht) -{ - res_T res = RES_OK; - ASSERT(sht); - - res = logger_init(sht->allocator, &sht->logger__); - if(res != RES_OK) { - if(sht->verbose) { - fprintf(stderr, - MSG_ERROR_PREFIX - "Could not setup the default logger for the Star-HITRAN library -- %s.\n", - res_to_cstr(res)); - } - goto error; - } - logger_set_stream(&sht->logger__, LOG_OUTPUT, print_info, NULL); - logger_set_stream(&sht->logger__, LOG_ERROR, print_err, NULL); - logger_set_stream(&sht->logger__, LOG_WARNING, print_warn, NULL); - sht->logger = &sht->logger__; - -exit: - return res; -error: - goto exit; -} - -void -log_info(const struct sht* sht, const char* msg, ...) -{ - va_list vargs_list; - ASSERT(sht && msg); - - va_start(vargs_list, msg); - log_msg(sht, LOG_OUTPUT, msg, vargs_list); - va_end(vargs_list); -} - -void -log_err(const struct sht* sht, const char* msg, ...) -{ - va_list vargs_list; - ASSERT(sht && msg); - - va_start(vargs_list, msg); - log_msg(sht, LOG_ERROR, msg, vargs_list); - va_end(vargs_list); -} - -void -log_warn(const struct sht* sht, const char* msg, ...) -{ - va_list vargs_list; - ASSERT(sht && msg); - - va_start(vargs_list, msg); - log_msg(sht, LOG_WARNING, msg, vargs_list); - va_end(vargs_list); -} diff --git a/src/sht_log.h b/src/sht_log.h @@ -1,73 +0,0 @@ -/* Copyright (C) 2022 CNRS - LMD - * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) - * Copyright (C) 2022 Université Paul Sabatier - IRIT - * Copyright (C) 2022 Université Paul Sabatier - Laplace - * - * 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 SHT_LOG_H -#define SHT_LOG_H - -#include <rsys/rsys.h> - -#define MSG_INFO_PREFIX "Star-HITRAN:\x1b[1m\x1b[32minfo\x1b[0m: " -#define MSG_ERROR_PREFIX "Star-HITRAN:\x1b[1m\x1b[31merror\x1b[0m: " -#define MSG_WARNING_PREFIX "Star-HITRAN:\x1b[1m\x1b[33mwarning\x1b[0m: " - -struct sht; -struct logger; - -extern LOCAL_SYM res_T -setup_log_default - (struct sht* sht); - -/* Conditionally log a message on the LOG_OUTPUT stream of the sht logger, - * with respect to its verbose flag */ -extern LOCAL_SYM void -log_info - (const struct sht* sht, - const char* msg, - ...) -#ifdef COMPILER_GCC - __attribute((format(printf, 2, 3))) -#endif -; - -/* Conditionally log a message on the LOG_ERROR stream of the sht logger, - * with respect to its verbose flag */ -extern LOCAL_SYM void -log_err - (const struct sht* sht, - const char* msg, - ...) -#ifdef COMPILER_GCC - __attribute((format(printf, 2, 3))) -#endif -; - -/* Conditionally log a message on the LOG_WARNING stream of the sht logger, - * with respect to its verbose flag */ -extern LOCAL_SYM void -log_warn - (const struct sht* sht, - const char* msg, - ...) -#ifdef COMPILER_GCC - __attribute((format(printf, 2, 3))) -#endif -; - - -#endif /* SHT_LOG_H */ - diff --git a/src/test_shitran.c b/src/test_shitran.c @@ -0,0 +1,74 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT + * Copyright (C) 2022 Université Paul Sabatier - Laplace + * + * 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 "shitran.h" + +#include <rsys/logger.h> + +static void +log_stream(const char* msg, void* ctx) +{ + ASSERT(msg); + (void)msg, (void)ctx; + printf("%s\n", msg); +} + +int +main(int argc, char** argv) +{ + struct mem_allocator allocator; + struct logger logger; + struct shitran* shitran; + struct shitran_create_args args = SHITRAN_CREATE_ARGS_DEFAULT; + (void)argc, (void)argv; + + CHK(shitran_create(NULL, &shitran) == RES_BAD_ARG); + CHK(shitran_create(&args, NULL) == RES_BAD_ARG); + CHK(shitran_create(&args, &shitran) == RES_OK); + + CHK(shitran_ref_get(NULL) == RES_BAD_ARG); + CHK(shitran_ref_get(shitran) == RES_OK); + CHK(shitran_ref_put(NULL) == RES_BAD_ARG); + CHK(shitran_ref_put(shitran) == RES_OK); + CHK(shitran_ref_put(shitran) == RES_OK); + + CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); + args.allocator = &allocator; + args.verbose = 1; + CHK(shitran_create(&args, &shitran) == RES_OK); + CHK(MEM_ALLOCATED_SIZE(&allocator) != 0); + CHK(shitran_ref_put(shitran) == RES_OK); + + CHK(logger_init(&allocator, &logger) == RES_OK); + logger_set_stream(&logger, LOG_OUTPUT, log_stream, NULL); + logger_set_stream(&logger, LOG_ERROR, log_stream, NULL); + logger_set_stream(&logger, LOG_WARNING, log_stream, NULL); + + args.logger = &logger; + CHK(shitran_create(&args, &shitran) == RES_OK); + CHK(shitran_ref_put(shitran) == RES_OK); + args.allocator = NULL; + CHK(shitran_create(&args, &shitran) == RES_OK); + CHK(shitran_ref_put(shitran) == RES_OK); + + logger_release(&logger); + CHK(MEM_ALLOCATED_SIZE(&allocator) == 0); + mem_shutdown_proxy_allocator(&allocator); + CHK(mem_allocated_size() == 0); + return 0; +} diff --git a/src/test_shitran_isotope_metadata.c b/src/test_shitran_isotope_metadata.c @@ -0,0 +1,179 @@ +/* Copyright (C) 2022 CNRS - LMD + * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) + * Copyright (C) 2022 Université Paul Sabatier - IRIT + * Copyright (C) 2022 Université Paul Sabatier - Laplace + * + * 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 "shitran.h" + +#include <rsys/mem_allocator.h> +#include <rsys/math.h> +#include <string.h> + +static const struct shitran_isotope H2O_isotopes[] = { + {9.97317E-01, 1.7458E+02, 18.010565, 0, 1, 161}, + {1.99983E-03, 1.7605E+02, 20.014811, 0, 1, 181}, + {3.71884E-04, 1.0521E+03, 19.014780, 0, 6, 171}, + {3.10693E-04, 8.6474E+02, 19.016740, 0, 6, 162}, + {6.23003E-07, 8.7557E+02, 21.020985, 0, 6, 182}, + {1.15853E-07, 5.2268E+03, 20.020956, 0, 36, 172}, + {2.41974E-08, 1.0278E+03, 20.022915, 0, 1, 262} +}; + + +static const struct shitran_molecule H2O = { + "H2O", sizeof(H2O_isotopes)/sizeof(struct shitran_isotope), H2O_isotopes, 1, +}; + +static const struct shitran_isotope CO2_isotopes[] = { + {9.84204E-01, 2.8609E+02, 43.989830, 1, 1, 626}, + {1.10574E-02, 5.7664E+02, 44.993185, 1, 2, 636}, + {3.94707E-03, 6.0781E+02, 45.994076, 1, 1, 628}, + {7.33989E-04, 3.5426E+03, 44.994045, 1, 6, 627}, + {4.43446E-05, 1.2255E+03, 46.997431, 1, 2, 638}, + {8.24623E-06, 7.1413E+03, 45.997400, 1, 12, 637}, + {3.95734E-06, 3.2342E+02, 47.998320, 1, 1, 828}, + {1.47180E-06, 3.7666E+03, 46.998291, 1, 6, 827}, + {1.36847E-07, 1.0972E+04, 45.998262, 1, 1, 727}, + {4.44600E-08, 6.5224E+02, 49.001675, 1, 2, 838}, + {1.65354E-08, 7.5950E+03, 48.001646, 1, 12, 837}, + {1.53745E-09, 2.2120E+04, 47.001618, 1, 2, 737} +}; + +static const struct shitran_molecule CO2 = { + "CO2", sizeof(CO2_isotopes)/sizeof(struct shitran_isotope), CO2_isotopes, 2, +}; + +static void +molecule_print(FILE* fp, const struct shitran_molecule* molecule) +{ + size_t i; + CHK(fp && molecule); + + fprintf(fp, " %s (%d)\n", molecule->name, molecule->id); + FOR_EACH(i, 0, molecule->nisotopes) { + fprintf(fp, " %d %.5E %.4E %d %.6f\n", + molecule->isotopes[i].id, + molecule->isotopes[i].abundance, + molecule->isotopes[i].Q296K, + molecule->isotopes[i].gj, + molecule->isotopes[i].molar_mass); + } +} + +static int +isotope_eq(const struct shitran_isotope* i0, const struct shitran_isotope* i1) +{ + CHK(i0 && i1); + return i0->abundance == i1->abundance + && i0->Q296K == i1->Q296K + && i0->molar_mass == i1->molar_mass + && i0->molecule == i1->molecule + && i0->gj == i1->gj + && i0->id == i1->id; +} + +static int +molecule_eq(const struct shitran_molecule* m0, const struct shitran_molecule* m1) +{ + size_t i; + + CHK(m0 && m1); + if(strcmp(m0->name, m1->name) + || m0->id != m1->id + || m0->nisotopes != m1->nisotopes) + return 0; + + FOR_EACH(i, 0, m0->nisotopes) { + if(!isotope_eq(m0->isotopes+i, m1->isotopes+i)) + return 0; + } + return 1; +} + +static void +test_load(struct shitran* shitran) +{ + + const char* filename = "test_isotope_metadata.txt"; + struct shitran_isotope_metadata* metadata = NULL; + struct shitran_molecule molecule = SHITRAN_MOLECULE_NULL; + FILE* fp = NULL; + size_t nmolecules = 0; + + CHK(fp = fopen(filename, "w+")); + + fprintf(fp, "Molecule # Iso Abundance Q(296K) gj Molar Mass(g)\n"); + molecule_print(fp, &H2O); + molecule_print(fp, &CO2); + rewind(fp); + + CHK(shitran_isotope_metadata_load_stream(NULL, fp, NULL, &metadata) == RES_BAD_ARG); + CHK(shitran_isotope_metadata_load_stream(shitran, NULL, NULL, &metadata) == RES_BAD_ARG); + CHK(shitran_isotope_metadata_load_stream(shitran, fp, NULL, NULL) == RES_BAD_ARG); + CHK(shitran_isotope_metadata_load_stream(shitran, fp, NULL, &metadata) == RES_OK); + + CHK(shitran_isotope_metadata_get_molecules_count(NULL, &nmolecules) == RES_BAD_ARG); + CHK(shitran_isotope_metadata_get_molecules_count(metadata, NULL) == RES_BAD_ARG); + CHK(shitran_isotope_metadata_get_molecules_count(metadata, &nmolecules) == RES_OK); + CHK(nmolecules == 2); + + CHK(shitran_isotope_metadata_get_molecule(NULL, 0, &molecule) == RES_BAD_ARG); + CHK(shitran_isotope_metadata_get_molecule(metadata, 2, &molecule) == RES_BAD_ARG); + + CHK(shitran_isotope_metadata_get_molecule(metadata, 0, &molecule) == RES_OK); + CHK(molecule_eq(&molecule, &H2O)); + + CHK(shitran_isotope_metadata_get_molecule(metadata, 1, &molecule) == RES_OK); + CHK(molecule_eq(&molecule, &CO2)); + + CHK(shitran_isotope_metadata_find_molecule(NULL, 1, &molecule) == RES_BAD_ARG); + CHK(shitran_isotope_metadata_find_molecule(metadata, 1, NULL) == RES_BAD_ARG); + CHK(shitran_isotope_metadata_find_molecule(metadata, 1, &molecule) == RES_OK); + CHK(!SHITRAN_MOLECULE_IS_NULL(&molecule)); + CHK(molecule_eq(&molecule, &H2O)); + + CHK(shitran_isotope_metadata_find_molecule(metadata, 2, &molecule) == RES_OK); + CHK(!SHITRAN_MOLECULE_IS_NULL(&molecule)); + CHK(molecule_eq(&molecule, &CO2)); + + CHK(shitran_isotope_metadata_find_molecule(metadata, 0, &molecule) == RES_OK); + CHK(SHITRAN_MOLECULE_IS_NULL(&molecule)); + + CHK(shitran_isotope_metadata_ref_get(NULL) == RES_BAD_ARG); + CHK(shitran_isotope_metadata_ref_get(metadata) == RES_OK); + CHK(shitran_isotope_metadata_ref_put(NULL) == RES_BAD_ARG); + CHK(shitran_isotope_metadata_ref_put(metadata) == RES_OK); + CHK(shitran_isotope_metadata_ref_put(metadata) == RES_OK); + + CHK(fclose(fp) == 0); +} + +int +main(int argc, char** argv) +{ + struct shitran* shitran = NULL; + struct shitran_create_args args = SHITRAN_CREATE_ARGS_DEFAULT; + (void)argc, (void)argv; + + args.verbose = 1; + CHK(shitran_create(&args, &shitran) == RES_OK); + + test_load(shitran); + + CHK(shitran_ref_put(shitran) == RES_OK); + CHK(mem_allocated_size() == 0); + return 0; +} diff --git a/src/test_sht.c b/src/test_sht.c @@ -1,74 +0,0 @@ -/* Copyright (C) 2022 CNRS - LMD - * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) - * Copyright (C) 2022 Université Paul Sabatier - IRIT - * Copyright (C) 2022 Université Paul Sabatier - Laplace - * - * 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 "sht.h" - -#include <rsys/logger.h> - -static void -log_stream(const char* msg, void* ctx) -{ - ASSERT(msg); - (void)msg, (void)ctx; - printf("%s\n", msg); -} - -int -main(int argc, char** argv) -{ - struct mem_allocator allocator; - struct logger logger; - struct sht* sht; - struct sht_create_args args = SHT_CREATE_ARGS_DEFAULT; - (void)argc, (void)argv; - - CHK(sht_create(NULL, &sht) == RES_BAD_ARG); - CHK(sht_create(&args, NULL) == RES_BAD_ARG); - CHK(sht_create(&args, &sht) == RES_OK); - - CHK(sht_ref_get(NULL) == RES_BAD_ARG); - CHK(sht_ref_get(sht) == RES_OK); - CHK(sht_ref_put(NULL) == RES_BAD_ARG); - CHK(sht_ref_put(sht) == RES_OK); - CHK(sht_ref_put(sht) == RES_OK); - - CHK(mem_init_proxy_allocator(&allocator, &mem_default_allocator) == RES_OK); - args.allocator = &allocator; - args.verbose = 1; - CHK(sht_create(&args, &sht) == RES_OK); - CHK(MEM_ALLOCATED_SIZE(&allocator) != 0); - CHK(sht_ref_put(sht) == RES_OK); - - CHK(logger_init(&allocator, &logger) == RES_OK); - logger_set_stream(&logger, LOG_OUTPUT, log_stream, NULL); - logger_set_stream(&logger, LOG_ERROR, log_stream, NULL); - logger_set_stream(&logger, LOG_WARNING, log_stream, NULL); - - args.logger = &logger; - CHK(sht_create(&args, &sht) == RES_OK); - CHK(sht_ref_put(sht) == RES_OK); - args.allocator = NULL; - CHK(sht_create(&args, &sht) == RES_OK); - CHK(sht_ref_put(sht) == RES_OK); - - logger_release(&logger); - CHK(MEM_ALLOCATED_SIZE(&allocator) == 0); - mem_shutdown_proxy_allocator(&allocator); - CHK(mem_allocated_size() == 0); - return 0; -} diff --git a/src/test_sht_isotope_metadata.c b/src/test_sht_isotope_metadata.c @@ -1,179 +0,0 @@ -/* Copyright (C) 2022 CNRS - LMD - * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com) - * Copyright (C) 2022 Université Paul Sabatier - IRIT - * Copyright (C) 2022 Université Paul Sabatier - Laplace - * - * 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 "sht.h" - -#include <rsys/mem_allocator.h> -#include <rsys/math.h> -#include <string.h> - -static const struct sht_isotope H2O_isotopes[] = { - {9.97317E-01, 1.7458E+02, 18.010565, 0, 1, 161}, - {1.99983E-03, 1.7605E+02, 20.014811, 0, 1, 181}, - {3.71884E-04, 1.0521E+03, 19.014780, 0, 6, 171}, - {3.10693E-04, 8.6474E+02, 19.016740, 0, 6, 162}, - {6.23003E-07, 8.7557E+02, 21.020985, 0, 6, 182}, - {1.15853E-07, 5.2268E+03, 20.020956, 0, 36, 172}, - {2.41974E-08, 1.0278E+03, 20.022915, 0, 1, 262} -}; - - -static const struct sht_molecule H2O = { - "H2O", sizeof(H2O_isotopes)/sizeof(struct sht_isotope), H2O_isotopes, 1, -}; - -static const struct sht_isotope CO2_isotopes[] = { - {9.84204E-01, 2.8609E+02, 43.989830, 1, 1, 626}, - {1.10574E-02, 5.7664E+02, 44.993185, 1, 2, 636}, - {3.94707E-03, 6.0781E+02, 45.994076, 1, 1, 628}, - {7.33989E-04, 3.5426E+03, 44.994045, 1, 6, 627}, - {4.43446E-05, 1.2255E+03, 46.997431, 1, 2, 638}, - {8.24623E-06, 7.1413E+03, 45.997400, 1, 12, 637}, - {3.95734E-06, 3.2342E+02, 47.998320, 1, 1, 828}, - {1.47180E-06, 3.7666E+03, 46.998291, 1, 6, 827}, - {1.36847E-07, 1.0972E+04, 45.998262, 1, 1, 727}, - {4.44600E-08, 6.5224E+02, 49.001675, 1, 2, 838}, - {1.65354E-08, 7.5950E+03, 48.001646, 1, 12, 837}, - {1.53745E-09, 2.2120E+04, 47.001618, 1, 2, 737} -}; - -static const struct sht_molecule CO2 = { - "CO2", sizeof(CO2_isotopes)/sizeof(struct sht_isotope), CO2_isotopes, 2, -}; - -static void -molecule_print(FILE* fp, const struct sht_molecule* molecule) -{ - size_t i; - CHK(fp && molecule); - - fprintf(fp, " %s (%d)\n", molecule->name, molecule->id); - FOR_EACH(i, 0, molecule->nisotopes) { - fprintf(fp, " %d %.5E %.4E %d %.6f\n", - molecule->isotopes[i].id, - molecule->isotopes[i].abundance, - molecule->isotopes[i].Q296K, - molecule->isotopes[i].gj, - molecule->isotopes[i].molar_mass); - } -} - -static int -isotope_eq(const struct sht_isotope* i0, const struct sht_isotope* i1) -{ - CHK(i0 && i1); - return i0->abundance == i1->abundance - && i0->Q296K == i1->Q296K - && i0->molar_mass == i1->molar_mass - && i0->molecule == i1->molecule - && i0->gj == i1->gj - && i0->id == i1->id; -} - -static int -molecule_eq(const struct sht_molecule* m0, const struct sht_molecule* m1) -{ - size_t i; - - CHK(m0 && m1); - if(strcmp(m0->name, m1->name) - || m0->id != m1->id - || m0->nisotopes != m1->nisotopes) - return 0; - - FOR_EACH(i, 0, m0->nisotopes) { - if(!isotope_eq(m0->isotopes+i, m1->isotopes+i)) - return 0; - } - return 1; -} - -static void -test_load(struct sht* sht) -{ - - const char* filename = "test_isotope_metadata.txt"; - struct sht_isotope_metadata* metadata = NULL; - struct sht_molecule molecule = SHT_MOLECULE_NULL; - FILE* fp = NULL; - size_t nmolecules = 0; - - CHK(fp = fopen(filename, "w+")); - - fprintf(fp, "Molecule # Iso Abundance Q(296K) gj Molar Mass(g)\n"); - molecule_print(fp, &H2O); - molecule_print(fp, &CO2); - rewind(fp); - - CHK(sht_isotope_metadata_load_stream(NULL, fp, NULL, &metadata) == RES_BAD_ARG); - CHK(sht_isotope_metadata_load_stream(sht, NULL, NULL, &metadata) == RES_BAD_ARG); - CHK(sht_isotope_metadata_load_stream(sht, fp, NULL, NULL) == RES_BAD_ARG); - CHK(sht_isotope_metadata_load_stream(sht, fp, NULL, &metadata) == RES_OK); - - CHK(sht_isotope_metadata_get_molecules_count(NULL, &nmolecules) == RES_BAD_ARG); - CHK(sht_isotope_metadata_get_molecules_count(metadata, NULL) == RES_BAD_ARG); - CHK(sht_isotope_metadata_get_molecules_count(metadata, &nmolecules) == RES_OK); - CHK(nmolecules == 2); - - CHK(sht_isotope_metadata_get_molecule(NULL, 0, &molecule) == RES_BAD_ARG); - CHK(sht_isotope_metadata_get_molecule(metadata, 2, &molecule) == RES_BAD_ARG); - - CHK(sht_isotope_metadata_get_molecule(metadata, 0, &molecule) == RES_OK); - CHK(molecule_eq(&molecule, &H2O)); - - CHK(sht_isotope_metadata_get_molecule(metadata, 1, &molecule) == RES_OK); - CHK(molecule_eq(&molecule, &CO2)); - - CHK(sht_isotope_metadata_find_molecule(NULL, 1, &molecule) == RES_BAD_ARG); - CHK(sht_isotope_metadata_find_molecule(metadata, 1, NULL) == RES_BAD_ARG); - CHK(sht_isotope_metadata_find_molecule(metadata, 1, &molecule) == RES_OK); - CHK(!SHT_MOLECULE_IS_NULL(&molecule)); - CHK(molecule_eq(&molecule, &H2O)); - - CHK(sht_isotope_metadata_find_molecule(metadata, 2, &molecule) == RES_OK); - CHK(!SHT_MOLECULE_IS_NULL(&molecule)); - CHK(molecule_eq(&molecule, &CO2)); - - CHK(sht_isotope_metadata_find_molecule(metadata, 0, &molecule) == RES_OK); - CHK(SHT_MOLECULE_IS_NULL(&molecule)); - - CHK(sht_isotope_metadata_ref_get(NULL) == RES_BAD_ARG); - CHK(sht_isotope_metadata_ref_get(metadata) == RES_OK); - CHK(sht_isotope_metadata_ref_put(NULL) == RES_BAD_ARG); - CHK(sht_isotope_metadata_ref_put(metadata) == RES_OK); - CHK(sht_isotope_metadata_ref_put(metadata) == RES_OK); - - CHK(fclose(fp) == 0); -} - -int -main(int argc, char** argv) -{ - struct sht* sht = NULL; - struct sht_create_args args = SHT_CREATE_ARGS_DEFAULT; - (void)argc, (void)argv; - - args.verbose = 1; - CHK(sht_create(&args, &sht) == RES_OK); - - test_load(sht); - - CHK(sht_ref_put(sht) == RES_OK); - CHK(mem_allocated_size() == 0); - return 0; -}