star-line

Structure for accelerating line importance sampling
git clone git://git.meso-star.fr/star-line.git
Log | Files | Refs | README | LICENSE

commit dc5fdc77a74f8f722d7daf81ad0af401e83b9dc0
parent 79226753f351fbe1536b06073c55ef254bd312a5
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed,  4 Feb 2026 16:34:09 +0100

Test the mixture API

Diffstat:
M.gitignore | 1+
MMakefile | 6++++--
Msrc/test_sln_mixture.c | 272+++++++++++++++++++++++++++++--------------------------------------------------
3 files changed, 105 insertions(+), 174 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -6,6 +6,7 @@ *~ .config .gitignore +mixture.txt sln-build tags tags diff --git a/Makefile b/Makefile @@ -103,7 +103,7 @@ sln-build: config.mk sln-local.pc src/sln_build.o $(LIBNAME) $(CC) $(CFLAGS_UTIL) -o $@ src/sln_build.o $(LDFLAGS_UTIL) $(UTIL_DEP): config.mk sln-local.pc - $(CC) $(CFLAGS_UTIL) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@ + @$(CC) $(CFLAGS_UTIL) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@ $(UTIL_OBJ): config.mk sln-local.pc $(CC) $(CFLAGS_UTIL) -c $(@:.o=.c) -o $@ @@ -167,6 +167,7 @@ lint: TEST_SRC =\ src/test_sln_device.c\ src/test_sln_mesh.c\ + src/test_sln_mixture.c\ src/test_sln_tree.c TEST_OBJ = $(TEST_SRC:.c=.o) TEST_DEP = $(TEST_SRC:.c=.d) @@ -199,12 +200,13 @@ $(TEST_OBJ): config.mk sln-local.pc test_sln_device \ test_sln_mesh \ +test_sln_mixture \ test_sln_tree \ : config.mk sln-local.pc $(LIBNAME) $(CC) $(CFLAGS_TEST) -o $@ src/$@.o $(LDFLAGS_TEST) clean_test: - rm -f $(TEST_DEP) $(TEST_OBJ) $(TEST_TGT) + rm -f $(TEST_DEP) $(TEST_OBJ) $(TEST_TGT) mixture.txt for i in $(TEST_SRC); do rm -f "$$(basename "$${i}" ".c")"; done test: tests diff --git a/src/test_sln_mixture.c b/src/test_sln_mixture.c @@ -27,219 +27,147 @@ * Helper function ******************************************************************************/ static void -test_mixture +test_api (struct sln_device* sln, - struct shtr_isotope_metadata* metadata, - struct shtr_line_list* line_list) + struct shtr_isotope_metadata* molparam) { - struct sln_mixture_create_args mixture_args = SLN_MIXTURE_CREATE_ARGS_DEFAULT; - struct sln_mixture_desc desc = SLN_MIXTURE_DESC_NULL; + struct sln_mixture_load_args args = SLN_MIXTURE_LOAD_ARGS_NULL; struct sln_mixture* mixture = NULL; - size_t nlines = 0; - - mixture_args.metadata = metadata; - mixture_args.lines = line_list; - mixture_args.molecules[0].nisotopes = 0; /* Handle all isotopes */ - mixture_args.molecules[0].concentration = 1.0/3.0; - mixture_args.molecules[0].cutoff = 25; - mixture_args.molecules[0].id = 1; /* H2O */ - mixture_args.molecules[1].nisotopes = 0; /* Handle all isotopes */ - mixture_args.molecules[1].concentration = 1.0/3.0; - mixture_args.molecules[1].cutoff = 50; - mixture_args.molecules[1].id = 2; /* CO2 */ - mixture_args.molecules[2].nisotopes = 0; /* Handle all isotopes */ - mixture_args.molecules[2].concentration = 1.0/3.0; - mixture_args.molecules[2].cutoff = 25; - mixture_args.molecules[2].id = 3; /* O3 */ - mixture_args.nmolecules = 3; - mixture_args.wavenumber_range[0] = 0; - mixture_args.wavenumber_range[1] = INF; - mixture_args.pressure = 1; - mixture_args.temperature = 296; - CHK(sln_mixture_create(NULL, &mixture_args, &mixture) == RES_BAD_ARG); - CHK(sln_mixture_create(sln, NULL, &mixture) == RES_BAD_ARG); - CHK(sln_mixture_create(sln, NULL, NULL) == RES_BAD_ARG); - CHK(sln_mixture_create(sln, &mixture_args, &mixture) == RES_OK); - - CHK(sln_mixture_get_desc(NULL, &desc) == RES_BAD_ARG); - CHK(sln_mixture_get_desc(mixture, NULL) == RES_BAD_ARG); - CHK(sln_mixture_get_desc(mixture, &desc) == RES_OK); - - CHK(shtr_line_list_get_size(line_list, &nlines) == RES_OK); - - CHK(desc.wavenumber_range[0] == mixture_args.wavenumber_range[0]); - CHK(desc.wavenumber_range[1] == mixture_args.wavenumber_range[1]); - CHK(desc.temperature = mixture_args.temperature); - CHK(desc.pressure == mixture_args.pressure); - CHK(desc.nlines == nlines); /* All the lines are taken into the count */ - - CHK(sln_mixture_ref_get(NULL) == RES_BAD_ARG); - CHK(sln_mixture_ref_get(mixture) == RES_OK); - CHK(sln_mixture_ref_put(NULL) == RES_BAD_ARG); - CHK(sln_mixture_ref_put(mixture) == RES_OK); - CHK(sln_mixture_ref_put(mixture) == RES_OK); - - mixture_args.metadata = NULL; - CHK(sln_mixture_create(sln, &mixture_args, &mixture) == RES_BAD_ARG); - - mixture_args.metadata = metadata; - mixture_args.lines = NULL; - CHK(sln_mixture_create(sln, &mixture_args, &mixture) == RES_BAD_ARG); + struct sln_molecule molecule = SLN_MOLECULE_NULL; + + const struct sln_isotope H2O_isotopes[] = { + {2.41974E-08, 161}, + {1.15853E-07, 181}, + {6.23003E-07, 171}, + {3.10693E-04, 162}, + {3.71884E-04, 182}, + {1.99983E-03, 172}, + {9.97317E-01, 262} + }; + const size_t H2O_nisotopes = sizeof(H2O_isotopes)/sizeof(H2O_isotopes[0]); + + const char* filename = "mixture.txt"; + FILE* fp = NULL; + size_t i = 0; - mixture_args.metadata = metadata; - mixture_args.lines = NULL; - CHK(sln_mixture_create(sln, &mixture_args, &mixture) == RES_BAD_ARG); + CHK(fp = fopen(filename, "w+")); - mixture_args.lines = line_list; - mixture_args.molecules[0].concentration = 1; - CHK(sln_mixture_create(sln, &mixture_args, &mixture) == RES_BAD_ARG); + fprintf(fp, "# Molecule concentration cutoff [cm-1]\n"); + fprintf(fp, " H2O 0.3 25\n"); + fprintf(fp, "# Isotopes abundance\n"); + FOR_EACH(i, 0, H2O_nisotopes) { + fprintf(fp, "%d %E\n", H2O_isotopes[i].id, H2O_isotopes[i].abundance); + } - mixture_args.molecules[0].concentration = -1; - CHK(sln_mixture_create(sln, &mixture_args, &mixture) == RES_BAD_ARG); + fprintf(fp, "# Molecule concentration cutoff [cm-1]\n"); + fprintf(fp, " CO2 0.7 50\n"); - mixture_args.molecules[0].concentration = 1.0/3.0; - mixture_args.molecules[0].cutoff = 0; - CHK(sln_mixture_create(sln, &mixture_args, &mixture) == RES_BAD_ARG); + rewind(fp); - mixture_args.molecules[0].cutoff = 25; - mixture_args.pressure = -1; - CHK(sln_mixture_create(sln, &mixture_args, &mixture) == RES_BAD_ARG); + args.filename = filename; + args.molparam = molparam; + CHK(sln_mixture_load(NULL, &args, &mixture) == RES_BAD_ARG); + CHK(sln_mixture_load(sln, NULL, &mixture) == RES_BAD_ARG); + CHK(sln_mixture_load(sln, &args, NULL) == RES_BAD_ARG); + CHK(sln_mixture_load(sln, &args, &mixture) == RES_OK); + + CHK(sln_mixture_get_molecule_count(mixture) == 2); + CHK(sln_mixture_get_molecule_id(mixture, 0) == SHTR_H2O); + CHK(sln_mixture_get_molecule_id(mixture, 1) == SHTR_CO2); + + CHK(sln_mixture_get_molecule(NULL, 0, &molecule) == RES_BAD_ARG); + CHK(sln_mixture_get_molecule(mixture, -1, &molecule) == RES_BAD_ARG); + CHK(sln_mixture_get_molecule(mixture, 2, &molecule) == RES_BAD_ARG); + CHK(sln_mixture_get_molecule(mixture, 0, NULL) == RES_BAD_ARG); + + /* Check the H2O molecule */ + CHK(sln_mixture_get_molecule(mixture, 0, &molecule) == RES_OK); + CHK(molecule.concentration == 0.3); + CHK(molecule.cutoff == 25.0); + CHK(molecule.non_default_isotope_abundances != 0); + FOR_EACH(i, 0, H2O_nisotopes) { + CHK(molecule.isotopes[i].id == H2O_isotopes[i].id); + CHK(molecule.isotopes[i].abundance == H2O_isotopes[i].abundance); + } + + /* Check the CO2 molecule */ + CHK(sln_mixture_get_molecule(mixture, 1, &molecule) == RES_OK); + CHK(molecule.concentration == 0.7); + CHK(molecule.cutoff == 50); + CHK(molecule.non_default_isotope_abundances == 0); - mixture_args.pressure = 1; - mixture_args.temperature = -1; - CHK(sln_mixture_create(sln, &mixture_args, &mixture) == RES_BAD_ARG); - mixture_args.temperature = 296; + CHK(sln_mixture_ref_get(NULL) == RES_BAD_ARG); + CHK(sln_mixture_ref_get(mixture) == RES_OK); + CHK(sln_mixture_ref_put(NULL) == RES_BAD_ARG); + CHK(sln_mixture_ref_put(mixture) == RES_OK); + CHK(sln_mixture_ref_put(mixture) == RES_OK); - mixture_args.molecules[0].nisotopes = 2; - mixture_args.molecules[0].isotopes[0].abundance = 0.6; - mixture_args.molecules[0].isotopes[0].id_local = 0; - mixture_args.molecules[0].isotopes[1].abundance = 0.5; - mixture_args.molecules[0].isotopes[1].id_local = 1; - CHK(sln_mixture_create(sln, &mixture_args, &mixture) == RES_BAD_ARG); - mixture_args.molecules[0].isotopes[0].abundance = 0.5; + /* Check load from stream */ + args.file = fp; + CHK(sln_mixture_load(sln, &args, &mixture) == RES_OK); + + CHK(sln_mixture_get_molecule_id(mixture, 0) == SHTR_H2O); + CHK(sln_mixture_get_molecule(mixture, 0, &molecule) == RES_OK); + CHK(molecule.concentration == 0.3); + CHK(molecule.cutoff == 25.0); + CHK(molecule.non_default_isotope_abundances != 0); + CHK(sln_mixture_get_molecule_id(mixture, 1) == SHTR_CO2); + CHK(sln_mixture_get_molecule(mixture, 1, &molecule) == RES_OK); + CHK(molecule.concentration == 0.7); + CHK(molecule.cutoff == 50); + CHK(molecule.non_default_isotope_abundances == 0); - CHK(sln_mixture_create(sln, &mixture_args, &mixture) == RES_OK); CHK(sln_mixture_ref_put(mixture) == RES_OK); - mixture_args.molecules[0].id = SLN_MAX_MOLECULES_COUNT; - CHK(sln_mixture_create(sln, &mixture_args, &mixture) == RES_BAD_ARG); - mixture_args.molecules[0].id = 1; - mixture_args.molecules[0].isotopes[0].id_local = SLN_MAX_ISOTOPES_COUNT; - CHK(sln_mixture_create(sln, &mixture_args, &mixture) == RES_BAD_ARG); + CHK(fclose(fp) == 0); } -static void -test_mixture_serialization - (struct sln_device* sln, - struct shtr* shtr, - struct shtr_isotope_metadata* metadata, - struct shtr_line_list* line_list) +static struct shtr_isotope_metadata* +load_isotope_metadata(struct shtr* shtr) { - struct sln_mixture_create_args mixture_args = SLN_MIXTURE_CREATE_ARGS_DEFAULT; - struct sln_mixture_desc desc1 = SLN_MIXTURE_DESC_NULL; - struct sln_mixture_desc desc2 = SLN_MIXTURE_DESC_NULL; - struct sln_mixture* mixture1 = NULL; - struct sln_mixture* mixture2 = NULL; + struct shtr_isotope_metadata* molparam = NULL; FILE* fp = NULL; - mixture_args.metadata = metadata; - mixture_args.lines = line_list; - mixture_args.molecules[0].nisotopes = 0; /* Handle all isotopes */ - mixture_args.molecules[0].concentration = 1.0/3.0; - mixture_args.molecules[0].cutoff = 25; - mixture_args.molecules[0].id = 1; /* H2O */ - mixture_args.molecules[1].nisotopes = 0; /* Handle all isotopes */ - mixture_args.molecules[1].concentration = 1.0/3.0; - mixture_args.molecules[1].cutoff = 50; - mixture_args.molecules[1].id = 2; /* CO2 */ - mixture_args.molecules[2].nisotopes = 0; /* Handle all isotopes */ - mixture_args.molecules[2].concentration = 1.0/3.0; - mixture_args.molecules[2].cutoff = 25; - mixture_args.molecules[2].id = 3; /* O3 */ - mixture_args.nmolecules = 3; - mixture_args.wavenumber_range[0] = 0; - mixture_args.wavenumber_range[1] = INF; - mixture_args.pressure = 1; - mixture_args.temperature = 296; - CHK(sln_mixture_create(sln, &mixture_args, &mixture1) == RES_OK); - CHK(fp = tmpfile()); - CHK(sln_mixture_write(NULL, fp) == RES_BAD_ARG); - CHK(sln_mixture_write(mixture1, NULL) == RES_BAD_ARG); - CHK(sln_mixture_write(mixture1, fp) == RES_OK); - rewind(fp); - - CHK(sln_mixture_create_from_stream(NULL, shtr, fp, &mixture2) == RES_BAD_ARG); - CHK(sln_mixture_create_from_stream(sln, NULL, fp, &mixture2) == RES_BAD_ARG); - CHK(sln_mixture_create_from_stream(sln, shtr, NULL, &mixture2) == RES_BAD_ARG); - CHK(sln_mixture_create_from_stream(sln, shtr, fp, NULL) == RES_BAD_ARG); - CHK(sln_mixture_create_from_stream(sln, shtr, fp, &mixture2) == RES_OK); - fclose(fp); - CHK(sln_mixture_get_desc(mixture1, &desc1) == RES_OK); - CHK(sln_mixture_get_desc(mixture2, &desc2) == RES_OK); + fprintf(fp, "Molecule # Iso Abundance Q(296K) gj Molar Mass(g)\n"); - CHK(desc1.wavenumber_range[0] == desc2.wavenumber_range[0]); - CHK(desc1.wavenumber_range[1] == desc2.wavenumber_range[1]); - CHK(desc1.pressure == desc2.pressure); - CHK(desc1.temperature == desc2.temperature); - CHK(desc1.nlines == desc2.nlines); + write_shtr_molecule(fp, &g_H2O); + write_shtr_molecule(fp, &g_CO2); + write_shtr_molecule(fp, &g_O3); - CHK(sln_mixture_ref_put(mixture1) == RES_OK); - CHK(sln_mixture_ref_put(mixture2) == RES_OK); + rewind(fp); + CHK(shtr_isotope_metadata_load_stream(shtr, fp, NULL, &molparam) == RES_OK); + CHK(fclose(fp) == 0); + return molparam; } /******************************************************************************* * Test function ******************************************************************************/ int -main(int argc, char** argv) +main(void) { - struct sln_device_create_args dev_args = SLN_DEVICE_CREATE_ARGS_DEFAULT; - + struct sln_device_create_args sln_args = SLN_DEVICE_CREATE_ARGS_DEFAULT; struct sln_device* sln = NULL; struct shtr_create_args shtr_args = SHTR_CREATE_ARGS_DEFAULT; struct shtr* shtr = NULL; - struct shtr_line_list* line_list = NULL; - struct shtr_isotope_metadata* metadata = NULL; - - FILE* fp_lines = NULL; - FILE* fp_mdata = NULL; - (void)argc, (void)argv; - - /* Generate the file of the isotope metadata */ - CHK(fp_mdata = tmpfile()); - fprintf(fp_mdata, "Molecule # Iso Abundance Q(296K) gj Molar Mass(g)\n"); - write_shtr_molecule(fp_mdata, &g_H2O); - write_shtr_molecule(fp_mdata, &g_CO2); - write_shtr_molecule(fp_mdata, &g_O3); - rewind(fp_mdata); - - /* Generate the file of lines */ - CHK(fp_lines = tmpfile()); - write_shtr_lines(fp_lines, g_lines, g_nlines); - rewind(fp_lines); - - /* Load the isotope metadata and the lines */ + struct shtr_isotope_metadata* molparam = NULL; + shtr_args.verbose = 1; CHK(shtr_create(&shtr_args, &shtr) == RES_OK); - CHK(shtr_isotope_metadata_load_stream(shtr, fp_mdata, NULL, &metadata) == RES_OK); - CHK(shtr_line_list_load_stream(shtr, fp_lines, NULL, &line_list) == RES_OK); - CHK(fclose(fp_lines) == 0); - CHK(fclose(fp_mdata) == 0); + sln_args.verbose = 1; + CHK(sln_device_create(&sln_args, &sln) == RES_OK); - dev_args.verbose = 1; - CHK(sln_device_create(&dev_args, &sln) == RES_OK); + molparam = load_isotope_metadata(shtr); - test_mixture(sln, metadata, line_list); - test_mixture_serialization(sln, shtr, metadata, line_list); + test_api(sln, molparam); CHK(sln_device_ref_put(sln) == RES_OK); CHK(shtr_ref_put(shtr) == RES_OK); - CHK(shtr_line_list_ref_put(line_list) == RES_OK); - CHK(shtr_isotope_metadata_ref_put(metadata) == RES_OK); + CHK(shtr_isotope_metadata_ref_put(molparam) == RES_OK); CHK(mem_allocated_size() == 0); return 0; }