star-line

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

commit bdb9e3b90ad7b1504bf5004122dbeeb54abeb9f0
parent 7db83a395565e46516faaddf55d1463d00d1b533
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Fri, 30 Jan 2026 14:56:21 +0100

sln-build: initialization of command execution

Loading the list of lines and molecule metadata, and opening the output
stream. But the mixture parameters still need to be loaded.  In fact,
its file format has not even been defined yet.

Diffstat:
MMakefile | 4++--
Msrc/sln_build.c | 147+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 149 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile @@ -87,8 +87,8 @@ UTIL_DEP = $(UTIL_SRC:.c=.d) PKG_CONFIG_LOCAL = PKG_CONFIG_PATH="./:$${PKG_CONFIG_PATH}" $(PKG_CONFIG) -INCS_UTIL = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsys sln-local) -LIBS_UTIL = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsys sln-local) +INCS_UTIL = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsys sln-local shtr) +LIBS_UTIL = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsys sln-local shtr) CFLAGS_UTIL = -std=c89 $(CFLAGS_EXE) $(INCS_UTIL) LDFLAGS_UTIL = $(LDFLAGS_EXE) $(LIBS_UTIL) diff --git a/src/sln_build.c b/src/sln_build.c @@ -82,6 +82,17 @@ struct args { } static const struct args ARGS_DEFAULT = ARGS_DEFAULT__; +struct cmd { + struct sln_device* sln; + + struct shtr* shtr; + struct shtr_line_list* lines; + struct shtr_isotope_metadata* molparams; + + FILE* output; +}; +static const struct cmd CMD_NULL = {0}; + /******************************************************************************* * Helper functions ******************************************************************************/ @@ -246,6 +257,8 @@ args_init(struct args* args, int argc, char** argv) } } + if(optind < argc) args->lines = argv[optind]; + #define MANDATORY(Cond, Name, Opt) { \ if(!(Cond)) { \ fprintf(stderr, "%s: %s missing -- option '-%c'\n", argv[0], (Name), (Opt)); \ @@ -266,6 +279,136 @@ error: goto exit; } +static res_T +load_lines_hitran(struct cmd* cmd, const struct args* args) +{ + struct shtr_line_list_load_args load_args = SHTR_LINE_LIST_LOAD_ARGS_NULL; + ASSERT(cmd && args); + + if(args->lines != NULL) { + load_args.filename = args->lines; + } else { + load_args.filename = "stdin"; + load_args.file = stdin; + } + + return shtr_line_list_load(cmd->shtr, &load_args, &cmd->lines); +} + +static res_T +load_lines_shtr(struct cmd* cmd, const struct args* args) +{ + FILE* fp = NULL; + res_T res = RES_OK; + + ASSERT(cmd && args); + + if(args->lines == NULL) { + fp = stdin; + } else { + fp = fopen(args->lines, "r"); + if(!fp) { + fprintf(stderr, "error opening file `%s' -- %s\n", + args->lines, strerror(errno)); + res = RES_IO_ERR; + goto error; + } + } + + res = shtr_line_list_create_from_stream(cmd->shtr, fp, &cmd->lines); + if(res != RES_OK) goto error; + +exit: + if(cmd->lines) SHTR(line_list_ref_put(cmd->lines)); + if(fp && fp != stdin) CHK(fclose(fp) == 0); + return res; +error: + goto exit; +} + +static res_T +load_lines(struct cmd* cmd, const struct args* args) +{ + res_T res = RES_OK; + switch(args->line_format) { + case LINE_LIST_HITRAN: res = load_lines_hitran(cmd, args); break; + case LINE_LIST_SHTR: res = load_lines_shtr(cmd, args); break; + default: FATAL("Unreachable code\n"); break; + } + return res; +} + +static res_T +setup_output(struct cmd* cmd, const struct args* args) +{ + res_T res = RES_OK; + ASSERT(cmd && args); + + if(!args->output) { + cmd->output = stdout; + + } else { + cmd->output = fopen(args->output, "w"); + if(!cmd->output) { + fprintf(stderr, "error opening file `%s' -- %s\n", + args->output, strerror(errno)); + res = RES_IO_ERR; + goto error; + } + } + +exit: + return res; +error: + if(cmd->output && cmd->output != stdout) CHK(fclose(cmd->output) == 0); + goto exit; +} + +static void +cmd_release(struct cmd* cmd) +{ + ASSERT(cmd); + if(cmd->sln) SLN(device_ref_put(cmd->sln)); + if(cmd->shtr) SHTR(ref_put(cmd->shtr)); + if(cmd->lines) SHTR(line_list_ref_put(cmd->lines)); + if(cmd->molparams) SHTR(isotope_metadata_ref_put(cmd->molparams)); +} + +static res_T +cmd_init(struct cmd* cmd, const struct args* args) +{ + struct sln_device_create_args sln_args = SLN_DEVICE_CREATE_ARGS_DEFAULT; + struct shtr_create_args shtr_args = SHTR_CREATE_ARGS_DEFAULT; + res_T res = RES_OK; + + ASSERT(cmd && args); + + *cmd = CMD_NULL; + + shtr_args.verbose = args->verbose; + res = shtr_create(&shtr_args, &cmd->shtr); + if(res != RES_OK) goto error; + + sln_args.verbose = args->verbose; + res = sln_device_create(&sln_args, &cmd->sln); + if(res != RES_OK) goto error; + + res = load_lines(cmd, args); + if(res != RES_OK) goto error; + + res = shtr_isotope_metadata_load(cmd->shtr, args->molparams, &cmd->molparams); + if(res != RES_OK) goto error; + + res = setup_output(cmd, args); + if(res != RES_OK) goto error; + +exit: + return res; +error: + cmd_release(cmd); + goto exit; +} + /******************************************************************************* * The program ******************************************************************************/ @@ -273,13 +416,17 @@ int main(int argc, char** argv) { struct args args = ARGS_DEFAULT; + struct cmd cmd = CMD_NULL; int err = 0; res_T res = RES_OK; if((res = args_init(&args, argc, argv)) != RES_OK) goto error; if(args.quit) goto exit; + if((res = cmd_init(&cmd, &args)) != RES_OK) goto error; + exit: + cmd_release(&cmd); CHK(mem_allocated_size() == 0); return err; error: