commit 51854d4ab04f1b5a102ad1423a91293694c4bc9b
parent dc5fdc77a74f8f722d7daf81ad0af401e83b9dc0
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Wed, 4 Feb 2026 16:52:54 +0100
Check the sum of the concentrations of the molecules
It must be less than or equal to 1, because some molecules may not be
defined if they are radiatively inactive.
Diffstat:
2 files changed, 47 insertions(+), 0 deletions(-)
diff --git a/src/sln_mixture.c b/src/sln_mixture.c
@@ -56,6 +56,37 @@ check_sln_mixture_load_args(const struct sln_mixture_load_args* args)
return RES_OK;
}
+static res_T
+check_molecules_cocentration
+ (const struct sln_mixture* mixture,
+ const char* mixture_name)
+{
+ double sum = 0;
+ int i = 0;
+ res_T res = RES_OK;
+ ASSERT(mixture);
+
+ FOR_EACH(i, 0, mixture->nmolecules) {
+ sum += mixture->molecules[i].param.concentration;
+ }
+
+ /* The sum of molecular concentrations must be less than or equal to 1. It may
+ * be less than 1 if the remaining part of the mixture is (implicitly) defined
+ * as a radiatively inactive gas */
+ if(sum > 1 && sum-1 > 1e-6) {
+ ERROR(mixture->sln,
+ "%s: the sum of molecule concentrations is greater than 1: %g\n",
+ mixture_name, sum);
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+exit:
+ return res;
+error:
+ goto exit;
+}
+
static INLINE res_T
flush_molecule
(struct sln_mixture* mixture,
@@ -407,6 +438,9 @@ load_mixture
res = load_stream(mixture, fp, args);
if(res != RES_OK) goto error;
+ res = check_molecules_cocentration(mixture, args->filename);
+ if(res != RES_OK) goto error;
+
exit:
if(fp && fp != args->file) fclose(fp);
return res;
diff --git a/src/sln_tree.c b/src/sln_tree.c
@@ -102,6 +102,7 @@ check_molecules
{
char molecule_ok[SHTR_MAX_MOLECULE_COUNT] = {0};
+ double concentrations_sum = 0;
size_t iline = 0;
size_t nlines = 0;
res_T res = RES_OK;
@@ -140,6 +141,8 @@ check_molecules
return RES_BAD_ARG;
}
+ concentrations_sum += molecule->concentration;
+
if(molecule->cutoff <= 0) {
/* ... cutoff either */
ERROR(sln, "%s: invalid %s cutoff: %g.\n",
@@ -153,6 +156,16 @@ check_molecules
molecule_ok[line.molecule_id] = 1;
}
+ /* The sum of molecular concentrations must be less than or equal to 1. It may
+ * be less than 1 if the remaining part of the mixture is (implicitly) defined
+ * as a radiatively inactive gas */
+ if(concentrations_sum > 1 && (concentrations_sum - 1) > 1e-6) {
+ ERROR(sln,
+ "%s: the sum of molecule concentrations is greater than 1: %g\n",
+ caller, concentrations_sum);
+ return RES_BAD_ARG;
+ }
+
return RES_OK;
}