commit 42babffb9fe19f3d49a30794e1a3eca380e03fc0
parent af5fea6978ffd0a8e3e5ba0070652a479a613377
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Tue, 8 Feb 2022 15:29:12 +0100
Implement and test the accessors to the molecule metadata
Diffstat:
4 files changed, 145 insertions(+), 6 deletions(-)
diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
@@ -87,7 +87,6 @@ if(NOT NO_TEST)
new_test(test_sht)
new_test(test_sht_isotope_metadata)
-
endif()
################################################################################
diff --git a/src/sht.h b/src/sht.h
@@ -1,5 +1,5 @@
/* Copyright (C) 2022 CNRS - LMD
- * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com)
+ * Copyright (C) 2022 |Meso|Star> (contact@meso-star.com)
* Copyright (C) 2022 Université Paul Sabatier - IRIT
* Copyright (C) 2022 Université Paul Sabatier - Laplace
*
@@ -48,6 +48,19 @@ struct sht_create_args {
static const struct sht_create_args SHT_CREATE_ARGS_DEFAULT =
SHT_CREATE_ARGS_DEFAULT__;
+struct sht_molecule {
+ const char* name;
+ size_t nisotopes; /* Number of isotopes */
+ int id; /* Unique identifier */
+};
+#define SHT_MOLECULE_NULL__ {NULL, 0, -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)->id == SHT_MOLECULE_NULL.id)
+
/* Forware declarations */
struct sht;
struct sht_isotope_metadata;
@@ -71,7 +84,7 @@ sht_ref_put
(struct sht* sht);
/*******************************************************************************
- * Isotopologues API
+ * Isotope metadata API
******************************************************************************/
SHT_API res_T
sht_isotope_metadata_load
@@ -94,6 +107,23 @@ 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_isotope_metadata.c b/src/sht_isotope_metadata.c
@@ -66,7 +66,7 @@ static const struct isotope ISOTOPOLOGUE_NULL = ISOTOPOLOGUE_NULL__;
struct molecule {
struct str name;
- size_t isotopes_range[2]; /* Range of the 1st and last isotopes */
+ 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)
@@ -303,7 +303,7 @@ parse_molecule
res = str_set(&molecule->name, name);
if(res != RES_OK) {
- log_err(metadata->sht,
+ 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));
@@ -619,3 +619,75 @@ sht_isotope_metadata_ref_put
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];
+
+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/test_sht_isotope_metadata.c b/src/test_sht_isotope_metadata.c
@@ -19,18 +19,21 @@
#include "sht.h"
#include <rsys/mem_allocator.h>
+#include <string.h>
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");
- fprintf(fp, " H20 (1)\n");
+ fprintf(fp, " H2O (1)\n");
fprintf(fp, " 161 9.97317E-01 1.7458E+02 1 18.010565\n");
fprintf(fp, " 181 1.99983E-03 1.7605E+02 1 20.014811\n");
fprintf(fp, " 171 3.71884E-04 1.0521E+03 6 19.014780\n");
@@ -58,6 +61,41 @@ test_load(struct sht* sht)
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(!strcmp(molecule.name, "H2O"));
+ CHK(molecule.id == 1);
+ CHK(molecule.nisotopes == 7);
+
+ CHK(sht_isotope_metadata_get_molecule(metadata, 1, &molecule) == RES_OK);
+ CHK(!strcmp(molecule.name, "CO2"));
+ CHK(molecule.id == 2);
+ CHK(molecule.nisotopes == 12);
+
+ 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(!strcmp(molecule.name, "H2O"));
+ CHK(molecule.id == 1);
+ CHK(molecule.nisotopes == 7);
+
+ CHK(sht_isotope_metadata_find_molecule(metadata, 2, &molecule) == RES_OK);
+ CHK(!SHT_MOLECULE_IS_NULL(&molecule));
+ CHK(!strcmp(molecule.name, "CO2"));
+ CHK(molecule.id == 2);
+ CHK(molecule.nisotopes == 12);
+
+ 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);