commit 182c3ba5fbe0842b6e34669cbee3a7988d26f04d
parent bf62e61449ddc040f79bd4c7ffe570e118b030f8
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Tue, 3 Mar 2026 14:57:34 +0100
Improve sgas creation interface
The mesh and list of thermodynamic properties can be loaded from a file
or stream. It is the caller who defines from where these data are
loaded.
There is no longer any default behavior on the library scale that sets
that only the list of thermodynamic properties can be loaded from either
a file or the standard input. Nor even that the stream is necessarily the
standard input. Everything is left to the caller's control.
The sgas-lint utility is updated to take into account the update of the
API, but retains its behavior, which becames only a specific way to use
the library.
Diffstat:
3 files changed, 67 insertions(+), 26 deletions(-)
diff --git a/src/sgas.c b/src/sgas.c
@@ -123,9 +123,20 @@ struct sgas {
* Helper functions
******************************************************************************/
static INLINE res_T
+check_sgas_file(const struct sgas_file* file)
+{
+ return (!file || (!file->name && !file->stream)) ? RES_BAD_ARG : RES_OK;
+}
+
+static INLINE res_T
check_sgas_create_args(const struct sgas_create_args* args)
{
- if(!args || !args->mesh) return RES_BAD_ARG;
+ res_T res = RES_OK;
+
+ if(!args) return RES_BAD_ARG;
+ if((res = check_sgas_file(&args->mesh)) != RES_OK) return res;
+ if((res = check_sgas_file(&args->therm_props_list)) != RES_OK) return res;
+
return RES_OK;
}
@@ -322,8 +333,9 @@ load_mesh
const struct sgas_create_args* args,
struct smsh** out_mesh)
{
+ #define MESH_NAME (args->mesh.name ? args->mesh.name : "stream")
+
struct smsh_create_args create_args = SMSH_CREATE_ARGS_DEFAULT;
- struct smsh_load_args load_args = SMSH_LOAD_ARGS_NULL;
struct smsh_desc mesh_desc = SMSH_DESC_NULL;
struct smsh* mesh = NULL;
res_T res = RES_OK;
@@ -333,20 +345,30 @@ load_mesh
create_args.allocator = gas->allocator;
create_args.logger = gas->logger;
create_args.verbose = gas->verbose;
- res = smsh_create(&create_args, &mesh);
- if(res != RES_OK) goto error;
+ if((res = smsh_create(&create_args, &mesh)) != RES_OK) goto error;
- load_args.path = args->mesh;
- load_args.memory_mapping = 1;
- res = smsh_load(mesh, &load_args);
- if(res != RES_OK) goto error;
+
+ if(!args->mesh.stream) {
+ /* Load from file */
+ struct smsh_load_args load_args = SMSH_LOAD_ARGS_NULL;
+ load_args.path = MESH_NAME;
+ load_args.memory_mapping = 1;
+ if((res = smsh_load(mesh, &load_args)) != RES_OK) goto error;
+
+ } else {
+ /* Load from stream */
+ struct smsh_load_stream_args load_args = SMSH_LOAD_STREAM_ARGS_NULL;
+ load_args.stream = args->mesh.stream;
+ load_args.name = MESH_NAME;
+ load_args.memory_mapping = 1;
+ if((res = smsh_load_stream(mesh, &load_args)) != RES_OK) goto error;
+ }
res = smsh_get_desc(mesh, &mesh_desc);
if(res != RES_OK) goto error;
if(mesh_desc.dcell != 4) {
- ERROR(gas, "%s: it is not a tetrahedral mesh as expected\n",
- args->mesh);
+ ERROR(gas, "%s: it is not a tetrahedral mesh as expected\n", MESH_NAME);
res = RES_BAD_ARG;
goto error;
}
@@ -356,10 +378,11 @@ exit:
return res;
error:
ERROR(gas, "Error loading gas mesh \"%s\" -- %s\n",
- args->mesh, res_to_cstr(res));
+ MESH_NAME, res_to_cstr(res));
if(mesh) { SMSH(ref_put(mesh)); mesh = NULL; }
goto exit;
+ #undef MESH_NAME
}
static res_T
@@ -497,22 +520,25 @@ setup_therm_props
const struct sgas_create_args* args)
{
struct txtrdr* txtrdr;
- char* name = NULL;
res_T res = RES_OK;
ASSERT(gas && args); /* Pre-conditions */
- if(args->therm_props_list) {
+ #define PROPS_NAME \
+ (args->therm_props_list.name ? args->therm_props_list.name : "stream")
+
+ if(!args->therm_props_list.stream) {
/* Read the list of thermodynamic properties from an input file */
- name = args->therm_props_list;
- res = txtrdr_file(gas->allocator, name, '#', &txtrdr);
+ res = txtrdr_file(gas->allocator, PROPS_NAME, '#', &txtrdr);
+
} else {
- /* Read the list of thermodynamic properties from the standard input */
- name = "stdin";
- res = txtrdr_stream(gas->allocator, stdin, name, '#', &txtrdr);
+ /* Read the list of thermodynamic properties from a stream */
+ res = txtrdr_stream
+ (gas->allocator, args->therm_props_list.stream, PROPS_NAME, '#', &txtrdr);
}
if(res != RES_OK) {
- ERROR(gas, "Cannot create \"%s\" reader -- %s\n", name, res_to_cstr(res));
+ ERROR(gas, "Cannot create \"%s\" reader -- %s\n",
+ PROPS_NAME, res_to_cstr(res));
goto error;
}
@@ -531,6 +557,8 @@ setup_therm_props
if(res != RES_OK) goto error;
}
+ #undef PROPS_NAME
+
exit:
if(txtrdr) txtrdr_ref_put(txtrdr);
return res;
diff --git a/src/sgas.h b/src/sgas.h
@@ -39,12 +39,20 @@
struct logger;
struct mem_allocator;
-struct sgas_create_args {
- char* mesh; /* Path to a tetrahedral mesh in smsh(5) format */
+struct sgas_file {
+ /* The name variable is either the stream name or the file to load, depending
+ * on whether stream is set or not. If name is NULL, then the data is loaded
+ * from stream, which must be set. In this case, the name of the stream is a
+ * default name. */
+ char* name; /* NULL <=> load from stream */
+ FILE* stream; /* NULL <=> load from name */
+};
+#define SGAS_FILE_NULL__ {0}
+static const struct sgas_file SGAS_FILE_NULL = SGAS_FILE_NULL__;
- /* Path to the list of time vayring atrtp(5) files.
- * NULL <=> read from stdin */
- char* therm_props_list;
+struct sgas_create_args {
+ struct sgas_file mesh; /* Tetrahedral mesh in smsh(5) format */
+ struct sgas_file therm_props_list; /* Time varying thermodynamic properties */
struct logger* logger; /* NULL <=> default logger */
struct mem_allocator* allocator; /* NULL <=> default allocator */
diff --git a/src/sgas_lint.c b/src/sgas_lint.c
@@ -110,8 +110,13 @@ cmd_init(struct cmd* cmd, const struct args* args)
cmd->args = *args;
- gas_args.mesh = args->mesh;
- gas_args.therm_props_list = args->therm_props_list;
+ gas_args.mesh.name = args->mesh;
+ if(args->therm_props_list) {
+ gas_args.therm_props_list.name = args->therm_props_list;
+ } else {
+ gas_args.therm_props_list.name = "stdin";
+ gas_args.therm_props_list.stream = stdin;
+ }
gas_args.verbose = args->verbose;
if((res = sgas_create(&gas_args, &cmd->gas)) != RES_OK) goto error;