star-phor

Radiative transfer solver for photoreactors.
git clone https://www.edstar.cnrs.fr/git/star-phor.git
Log | Files | Refs | README | LICENSE

commit ae8ff2a771b302742beedb1eeff88a7201ac56f0
parent 2e98424c36a4246da39385c02decd5826ea45952
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date:   Thu, 17 Apr 2025 15:56:57 +0200

Project setup: build system, core features, doc and tests

This commit implements several features for the star-phor project:

- Setup build system;
- Implement core features for the library (memory management and
  logging);
- Begin implementation for the main program and for library tests;
- Begin writing documentation

Diffstat:
A.gitignore | 13+++++++++++++
AMakefile | 207+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MREADME.md | 3++-
Aconfig.mk | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adoc/star-phor.scd | 45+++++++++++++++++++++++++++++++++++++++++++++
Asphor.pc.in | 11+++++++++++
Asrc/sphor.c | 141+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/sphor.h | 94+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/sphor_c.h | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/sphor_config.c | 95+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/sphor_main.c | 176+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/test_sphor_lib.c | 6++++++
12 files changed, 930 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,13 @@ +*~ +*.[adot] +.config +.test +file.txt +*.pc +*.so +*.swp +*.stl +tags +star-phor.1 +star-phor +test_sphor diff --git a/Makefile b/Makefile @@ -0,0 +1,207 @@ +# Copyright (C) 2024-2025 Centre National de la Recherche Scientifique +# Copyright (C) 2024-2025 Clermont Auvergne INP +# Copyright (C) 2024-2025 INSA Lyon +# Copyright (C) 2024-2025 Institut Mines Télécom Albi-Carmaux +# Copyright (C) 2024-2025 Institut National Polytechnique de Toulouse +# Copyright (C) 2024-2025 |Méso|Star> (contact@meso-star.com) +# Copyright (C) 2024-2025 PhotonLyX (info@photonlyx.com) +# Copyright (C) 2024-2025 Université de Lorraine +# Copyright (C) 2024-2025 Université Paul Sabatier +# Copyright (C) 2024-2025 Université Toulouse - Jean Jaurès +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +.POSIX: +.SUFFIXES: # Clean up default inference rules + +include config.mk + +LIBNAME = libsphor.so + +default: executable + +all: executable library tests + +################################################################################ +# Library building +################################################################################ +SRC_LIB =\ + src/sphor.c\ + src/sphor_config.c +OBJ_LIB = $(SRC_LIB:.c=.o) +DEP_LIB = $(SRC_LIB:.c=.d) + +# Compiler and linker options to build the library +CFLAGS_LIB = -std=c99 $(CFLAGS_SO) $(INCS) -DSPHOR_SHARED_BUILD +LDFLAGS_LIB = $(LDFLAGS_SO) $(LIBS) + +library: .config $(DEP_LIB) + @$(MAKE) -fMakefile \ + $$(for i in $(DEP_LIB); do echo -f "$${i}"; done) \ + $(LIBNAME) + +$(DEP_LIB) $(OBJ_LIB): config.mk + +$(LIBNAME): $(OBJ_LIB) + $(CC) $(CFLAGS_LIB) -o $@ $(OBJ_LIB) $(LDFLAGS_LIB) + +.config: config.mk + $(PKG_CONFIG) --atleast-version $(RSYS_VERSION) rsys + $(PKG_CONFIG) --atleast-version $(SPHIN_VERSION) sphin + echo "config done" > $@ + +.SUFFIXES: .c .d .o +.c.d: + @$(CC) $(CFLAGS_LIB) -MM -MT "$(@:.d=.o) $@" $< -MF $@ + +.c.o: + $(CC) $(CFLAGS_LIB) -c $< -o $@ + +clean: clean_test clean_executable + rm -f $(DEP_LIB) $(OBJ_LIB) $(LIBNAME) + rm -f .config sphor-local.pc sphor.pc + +################################################################################ +# Executable building +################################################################################ +SRC_BUILD =\ + src/sphor_main.c +OBJ_BUILD = $(SRC_BUILD:.c=.o) +DEP_BUILD = $(SRC_BUILD:.c=.d) + +PKG_CONFIG_LOCAL = PKG_CONFIG_PATH="./:$${PKG_CONFIG_PATH}" $(PKG_CONFIG) +INCS_BUILD = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsys sphor-local) +LIBS_BUILD = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsys sphor-local) + +# Compiler and linker options for the executable +CFLAGS_BUILD = -std=c99 $(CFLAGS_EXE) $(INCS_BUILD) +LDFLAGS_BUILD = $(LDFLAGS_EXE) $(LIBS_BUILD) + +executable: $(DEP_BUILD) library + @$(MAKE) -fMakefile $$(for i in $(DEP_BUILD); do echo -f $${i}; done) star-phor + +star-phor: .config $(OBJ_BUILD) sphor-local.pc $(LIBNAME) + $(CC) $(CFLAGS_BUILD) -o $@ $(OBJ_BUILD) $(LDFLAGS_BUILD) + +$(DEP_BUILD) $(OBJ_BUILD): .config sphor-local.pc + +$(DEP_BUILD): + @$(CC) $(CFLAGS_BUILD) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@ + +$(OBJ_BUILD): + $(CC) $(CFLAGS_BUILD) -c $(@:.o=.c) -o $@ + +clean_executable: + rm -f $(DEP_BUILD) $(OBJ_BUILD) star-phor + +################################################################################ +# Installation +################################################################################ +pkg: + sed -e 's#@PREFIX@#$(PREFIX)#g'\ + -e 's#@VERSION@#$(VERSION)#g'\ + -e 's#@SPHIN_VERSION@#$(SPHIN_VERSION)#g'\ + sphor.pc.in > sphor.pc + +star-phor.1: doc/star-phor.scd + scdoc < doc/star-phor.scd > $@ + +sphor-local.pc: sphor.pc.in + sed -e '1d'\ + -e 's#^includedir=.*#includedir=./src/#'\ + -e 's#^libdir=.*#libdir=./#'\ + -e 's#@VERSION@#$(VERSION)#g'\ + -e 's#@SPHIN_VERSION@#$(SPHIN_VERSION)#g'\ + sphor.pc.in > $@ + +install: library pkg star-phor.1 + install() { mode="$$1"; prefix="$$2"; shift 2; \ + mkdir -p "$${prefix}"; \ + cp "$$@" "$${prefix}"; \ + chmod "$${mode}" "$$@"; \ + }; \ + install 755 "$(DESTDIR)$(LIBPREFIX)" $(LIBNAME); \ + install 755 "$(DESTDIR)$(BINPREFIX)" star-phor; \ + install 644 "$(DESTDIR)$(LIBPREFIX)/pkgconfig" sphor.pc; \ + install 644 "$(DESTDIR)$(INCPREFIX)/star" src/sphor.h; \ + install 644 "$(DESTDIR)$(MANPREFIX)/man1" star-phor.1; \ + install 644 "$(DESTDIR)$(PREFIX)/share/doc/star-phor" COPYING; \ + install 644 "$(DESTDIR)$(PREFIX)/share/doc/star-phor" README.md + +uninstall: + rm -f "$(DESTDIR)$(LIBPREFIX/)$(LIBNAME)" + rm -f "$(DESTDIR)$(BINPREFIX)/star-phor" + rm -f "$(DESTDIR)$(LIBPREFIX)/pkgconfig/star-phor.pc" + rm -f "$(DESTDIR)$(INCPREFIX)/star/sphor.h" + rm -f "$(DESTDIR)$(MANPREFIX)/man1/star-phor.1" + rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-phor/COPYING" + rm -f "$(DESTDIR)$(PREFIX)/share/doc/star-phor/README.md" + +################################################################################ +# Tests +################################################################################ +TEST_SRC =\ + src/test_sphor_lib.c +TEST_OBJ = $(TEST_SRC:.c=.o) +TEST_DEP = $(TEST_SRC:.c=.d) +TEST_TGT = $(TEST_SRC:.c=.t) + +INCS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --cflags rsys sphin sphor-local) +LIBS_TEST = $$($(PKG_CONFIG_LOCAL) $(PCFLAGS) --libs rsys sphin sphor-local) -lm + +# Compiler and linker options to build the tests +CFLAGS_TEST = -std=c89 $(CFLAGS_EXE) $(INCS_TEST) +LDFLAGS_TEST = $(LDFLAGS_EXE) $(LIBS_TEST) + +test: tests + @err=0; \ + for i in $(TEST_SRC); do \ + test="$$(basename "$${i}" ".c")"; \ + printf '%s' "$${test}"; \ + if "./$${test}" > /dev/null 2>&1; then \ + printf '\n'; \ + else \ + printf ': error %s\n' "$$?"; \ + err=$$((err+1)); \ + fi \ + done; \ + [ "$${err}" -eq 0 ] + + +tests: library $(TEST_DEP) $(TEST_TGT) + @$(MAKE) -f Makefile \ + $$(for i in $(TEST_DEP); do echo -f"$${i}"; done) \ + $$(for i in $(TEST_TGT); do echo -f"$${i}"; done) \ + test_list + +$(TEST_TGT): + @{ \ + exe="$$(basename "$@" ".t")"; \ + printf '%s: %s\n' "$${exe}" $(@:.t=.o); \ + printf 'test_list: %s\n' "$${exe}"; \ + } > $@ + +$(TEST_DEP): config.mk sphor-local.pc + @$(CC) $(CFLAGS_TEST) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@ + +$(TEST_OBJ): config.mk sphor-local.pc + $(CC) $(CFLAGS_TEST) -c $(@:.o=.c) -o $@ + +test_sphor_lib\ +: config.mk sphor-local.pc $(LIBNAME) + $(CC) $(CFLAGS_TEST) -o $@ src/$@.o $(LDFLAGS_TEST) + +clean_test: + rm -f $(TEST_DEP) $(TEST_OBJ) $(TEST_TGT) + for i in $(TEST_SRC); do rm -f "$$(basename "$${i}" ".c")"; done diff --git a/README.md b/README.md @@ -5,9 +5,10 @@ Solver for radiative transfer in photoreactors. ## Requirements - POSIX make -- C compiler +- C compiler (c99) - pkg-config - rsys +- star-phor-input ## Installation diff --git a/config.mk b/config.mk @@ -0,0 +1,76 @@ +VERSION = 0.0 +PREFIX = /usr/local + +BUILD_TYPE = RELEASE +#BUILD_TYPE = DEBUG + +BINPREFIX = $(PREFIX)/bin +LIBPREFIX = $(PREFIX)/lib +INCPREFIX = $(PREFIX)/include +MANPREFIX = $(PREFIX)/share/man + +################################################################################ +# Tools +################################################################################ +AR = ar +CC = cc +LD = ld +OBJCOPY = objcopy +PKG_CONFIG = pkg-config +RANLIB = ranlib + +################################################################################ +# Dependencies +################################################################################ +SPHIN_VERSION=0.0 +RSYS_VERSION=0.14 + +INCS = $$($(PKG_CONFIG) --cflags rsys sphin) +LIBS = $$($(PKG_CONFIG) --libs rsys sphin) + +################################################################################ +# Compilation options +################################################################################ +WFLAGS =\ + -Wall\ + -Wcast-align\ + -Wconversion\ + -Wextra\ + -Wmissing-declarations\ + -Wmissing-prototypes\ + -Wshadow + +CFLAGS_HARDENED =\ + -D_FORTIFY_SOURCES=2\ + -fcf-protection=full\ + -fstack-clash-protection\ + -fstack-protector-strong + +CFLAGS_COMMON =\ + -pedantic\ + -fvisibility=hidden\ + -fstrict-aliasing\ + $(CFLAGS_HARDENED)\ + $(WFLAGS) + +CFLAGS_RELEASE = -O2 -DNDEBUG $(CFLAGS_COMMON) +CFLAGS_DEBUG = -g $(CFLAGS_COMMON) +CFLAGS = $(CFLAGS_$(BUILD_TYPE)) + +CFLAGS_SO = $(CFLAGS) -fPIC +CFLAGS_EXE = $(CFLAGS) -fPIE + +################################################################################ +# Linker options +################################################################################ +LDFLAGS_HARDENED = -Wl,-z,relro,-z,now +LDFLAGS_DEBUG = $(LDFLAGS_HARDENED) +LDFLAGS_RELEASE = -s $(LDFLAGS_HARDENED) +LDFLAGS = $(LDFLAGS_$(BUILD_TYPE)) + +LDFLAGS_SO = $(LDFLAGS) -shared -Wl,--no-undefined +LDFLAGS_EXE = $(LDFLAGS) -pie + +OCPFLAGS_DEBUG = --localize-hidden +OCPFLAGS_RELEASE = --localize-hidden --strip-unneeded +OCPFLAGS = $(OCPFLAGS_$(BUILD_TYPE)) diff --git a/doc/star-phor.scd b/doc/star-phor.scd @@ -0,0 +1,45 @@ +star-phor(1) "UNIX" + +; Copyright (C) 2024-2025 Centre National de la Recherche Scientifique +; Copyright (C) 2024-2025 Clermont Auvergne INP +; Copyright (C) 2024-2025 INSA Lyon +; Copyright (C) 2024-2025 Institut Mines Télécom Albi-Carmaux +; Copyright (C) 2024-2025 Institut National Polytechnique de Toulouse +; Copyright (C) 2024-2025 |Méso|Star> (contact@meso-star.com) +; Copyright (C) 2024-2025 PhotonLyX (info@photonlyx.com) +; Copyright (C) 2024-2025 Université de Lorraine +; Copyright (C) 2024-2025 Université Paul Sabatier +; Copyright (C) 2024-2025 Université Toulouse - Jean Jaurès +; +; This program is free software: you can redistribute it and/or modify +; it under the terms of the GNU General Public License as published by +; the Free Software Foundation, either version 3 of the License, or +; (at your option) any later version. +; +; This program is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this program. If not, see <http://www.gnu.org/licenses/>. + +# NAME + +star-phor - statistical solver for radiative transfer in photoreactors + +# SYNOPSIS + +# DESCRIPTION + +# EXIT STATUS + +# EXAMPLES + +# SEE ALSO +_star-phor-input_(5), _sphin_(1) + +# HISTORY + +*star-phor-input* has been developed as part of the *PEPR SPLEEN* Ecochem +project. diff --git a/sphor.pc.in b/sphor.pc.in @@ -0,0 +1,11 @@ +prefix=@PREFIX@ +includedir=${prefix}/include +libdir=${prefix}/lib + +Requires: rsys >= @RSYS_VERSION@ +Requires.private: sphin >= @SPHIN_VERSION@ +Name: Star-Phor +Description: statistical solver for radiative transfer in photoreactors +Version: @VERSION@ +Libs: -L${libdir} -lsphor +CFlags: -I${includedir} diff --git a/src/sphor.c b/src/sphor.c @@ -0,0 +1,141 @@ +/* Copyright (C) 2024-2025 Centre National de la Recherche Scientifique + * Copyright (C) 2024-2025 Clermont Auvergne INP + * Copyright (C) 2024-2025 INSA Lyon + * Copyright (C) 2024-2025 Institut Mines Télécom Albi-Carmaux + * Copyright (C) 2024-2025 Institut National Polytechnique de Toulouse + * Copyright (C) 2024-2025 |Méso|Star> (contact@meso-star.com) + * Copyright (C) 2024-2025 PhotonLyX (info@photonlyx.com) + * Copyright (C) 2024-2025 Université de Lorraine + * Copyright (C) 2024-2025 Université Paul Sabatier + * Copyright (C) 2024-2025 Université Toulouse - Jean Jaurès + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include "sphor.h" +#include "sphor_c.h" + +#include <rsys/logger.h> +#include <rsys/mem_allocator.h> + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static res_T +check_sphor_create_args + (const struct sphor_create_args* args) +{ + if (NULL == args) { + return RES_BAD_ARG; + } + return RES_OK; +} + +static void +release_sphor + (ref_T* address) +{ + struct sphor* sphor = NULL; + ASSERT(NULL != address); + + sphor = CONTAINER_OF(address, struct sphor, ref); + MEM_RM(sphor->allocator, sphor); +} + +/******************************************************************************* + * Exported functions + ******************************************************************************/ +res_T +sphor_create + (const struct sphor_create_args* args, + struct sphor** out_sphor) +{ + struct mem_allocator* allocator = NULL; + struct sphor* sphor = NULL; + res_T res = RES_OK; + + if (NULL == out_sphor) { + res = RES_BAD_ARG; + goto error; + } + res = check_sphor_create_args(args); + if (RES_OK != res) goto error; + + if (NULL == args->allocator) { + allocator = &mem_default_allocator; + } else { + allocator = args->allocator; + } + sphor = MEM_CALLOC(allocator, 1, sizeof(struct sphor)); + if (NULL == sphor) { + res = RES_MEM_ERR; + goto error; + } + ref_init(&sphor->ref); + sphor->verbose = args->verbose; + sphor->allocator = allocator; + if (NULL == args->logger) { + sphor->logger = LOGGER_DEFAULT; + } else { + sphor->logger = args->logger; + } + + /* TODO load the system and build data structure for Monte Carlo */ + res = setup_config(sphor, args); + if (RES_OK != res) { goto error; } + +exit: + if (NULL != out_sphor) *out_sphor = sphor; + return res; +error: + if (NULL != sphor) { + SPHOR(ref_put(sphor)); /* <=> ASSERT(sphor_ref_put(sphor) == RES_OK) */ + sphor = NULL; + } + goto exit; +} + +res_T +sphor_ref_get(struct sphor* sphor) +{ + if (NULL == sphor) { + return RES_BAD_ARG; + } + ref_get(&sphor->ref); + return RES_OK; +} + +res_T +sphor_ref_put(struct sphor* sphor) +{ + if (NULL == sphor) { + return RES_BAD_ARG; + } + ref_put(&sphor->ref, release_sphor); + return RES_OK; +} + +res_T +sphor_run(struct sphor* sphor) +{ + res_T res = RES_OK; + + if (NULL == sphor) { goto error; } + + /* TODO Run Monte Carlo algorithm */ + +exit: + return res; +error: + goto exit; +} diff --git a/src/sphor.h b/src/sphor.h @@ -0,0 +1,94 @@ +/* Copyright (C) 2024-2025 Centre National de la Recherche Scientifique + * Copyright (C) 2024-2025 Clermont Auvergne INP + * Copyright (C) 2024-2025 INSA Lyon + * Copyright (C) 2024-2025 Institut Mines Télécom Albi-Carmaux + * Copyright (C) 2024-2025 Institut National Polytechnique de Toulouse + * Copyright (C) 2024-2025 |Méso|Star> (contact@meso-star.com) + * Copyright (C) 2024-2025 PhotonLyX (info@photonlyx.com) + * Copyright (C) 2024-2025 Université de Lorraine + * Copyright (C) 2024-2025 Université Paul Sabatier + * Copyright (C) 2024-2025 Université Toulouse - Jean Jaurès + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#ifndef SPHOR_H +#define SPHOR_H + +#include <rsys/rsys.h> + +#include <limits.h> + +/* Library symbol management */ +#if defined(SPHOR_SHARED_BUILD) /* Directive defined only when building + star-phor (see Makefile) */ + #define SPHOR_API extern EXPORT_SYM /* When building star-phor */ +#else + #define SPHOR_API extern IMPORT_SYM /* When using star-phor */ +#endif + +/* Helper macro that asserts if the invocation of the sphor function `Func' + * returns an error. One should use this macro on sphor function calls for which + * no explicit error checking is performed */ +#ifndef NDEBUG + #define SPHOR(Func) ASSERT(sphor_##Func == RES_OK) +#else + #define SPHOR(Func) sphor_##Func +#endif + +struct sphor_create_args { + struct logger* logger; /* May be NULL <=> default logger */ + struct mem_allocator* allocator; /* NULL <=> use default allocator */ + int verbose; /* Verbosity level. Bigger values => more verbosity */ + + unsigned nthreads; /* hint: number of threads; defaults to all available + threads in the computer */ + size_t samples; /* number of realisations used during the Monte Carlo + integrating algorithm */ + char* input_filename; /* May be NULL <=> uses stdin */ + char* output_filename; /* May be NULL <=> uses stdout */ +}; + +#define SPHOR_CREATE_ARGS_DEFAULT__ {NULL, NULL, 0, UINT_MAX, 10000, NULL, NULL} + +static const struct sphor_create_args SPHOR_CREATE_ARGS_DEFAULT = + SPHOR_CREATE_ARGS_DEFAULT__; + +/* Forward declarations */ +struct sphor; + +BEGIN_DECLS + +/******************************************************************************* + * API of the sphor handle + ******************************************************************************/ +SPHOR_API res_T +sphor_create + (const struct sphor_create_args* args, + struct sphor** sphor); + +SPHOR_API res_T +sphor_run + (struct sphor* sphor); + +SPHOR_API res_T +sphor_ref_put + (struct sphor* sphor); + +SPHOR_API res_T +sphor_ref_get + (struct sphor* sphor); + +END_DECLS + +#endif /* SPHOR_H */ diff --git a/src/sphor_c.h b/src/sphor_c.h @@ -0,0 +1,64 @@ +/* Copyright (C) 2024-2025 Centre National de la Recherche Scientifique + * Copyright (C) 2024-2025 Clermont Auvergne INP + * Copyright (C) 2024-2025 INSA Lyon + * Copyright (C) 2024-2025 Institut Mines Télécom Albi-Carmaux + * Copyright (C) 2024-2025 Institut National Polytechnique de Toulouse + * Copyright (C) 2024-2025 |Méso|Star> (contact@meso-star.com) + * Copyright (C) 2024-2025 PhotonLyX (info@photonlyx.com) + * Copyright (C) 2024-2025 Université de Lorraine + * Copyright (C) 2024-2025 Université Paul Sabatier + * Copyright (C) 2024-2025 Université Toulouse - Jean Jaurès + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#ifndef SPHOR_C_H +#define SPHOR_C_H + +#include <rsys/logger.h> +#include <rsys/ref_count.h> + +/* Helper macros for logging */ +#define LOG__(Dev, Lvl, Type, ...) { \ + if ((Dev)->verbose >= (Lvl)) \ + logger_print((Dev)->logger, Type, __VA_ARGS__); \ +} (void)0 +#define ERROR(Dev, ...) LOG__(Dev, 1, LOG_ERROR, __VA_ARGS__) +#define WARN(Dev, ...) LOG__(Dev, 2, LOG_WARNING, __VA_ARGS__) +#define INFO(Dev, ...) LOG__(Dev, 3, LOG_OUTPUT, __VA_ARGS__) + +/* Forward declarations */ +struct logger; +struct mem_allocator; +struct sphin_config; +struct sphor_create_args; + +struct sphor { + /* Physical configuration */ + struct sphin_config* config; + + /* Log */ + struct logger* logger; + int verbose; + + /* Memory management */ + struct mem_allocator* allocator; + ref_T ref; +}; + +extern LOCAL_SYM res_T +setup_config + (struct sphor* sphor, + const struct sphor_create_args* args); + +#endif /* SPHOR_C_H */ diff --git a/src/sphor_config.c b/src/sphor_config.c @@ -0,0 +1,95 @@ +/* Copyright (C) 2024-2025 Centre National de la Recherche Scientifique + * Copyright (C) 2024-2025 Clermont Auvergne INP + * Copyright (C) 2024-2025 INSA Lyon + * Copyright (C) 2024-2025 Institut Mines Télécom Albi-Carmaux + * Copyright (C) 2024-2025 Institut National Polytechnique de Toulouse + * Copyright (C) 2024-2025 |Méso|Star> (contact@meso-star.com) + * Copyright (C) 2024-2025 PhotonLyX (info@photonlyx.com) + * Copyright (C) 2024-2025 Université de Lorraine + * Copyright (C) 2024-2025 Université Paul Sabatier + * Copyright (C) 2024-2025 Université Toulouse - Jean Jaurès + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include "sphor.h" +#include "sphor_c.h" + +#include <star/sphin.h> + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static res_T +load_config + (struct sphor* sphor, + const struct sphor_create_args* args) +{ + struct sphin* sphin = NULL; + struct sphin_config* config = NULL; + struct sphin_create_args sphin_args = SPHIN_CREATE_ARGS_DEFAULT; + res_T res = RES_OK; + + ASSERT(NULL != sphor); + ASSERT(NULL != args); + + /* Create sphin struct */ + sphin_args.verbose = sphor->verbose; + sphin_args.logger = sphor->logger; + sphin_args.allocator = sphor->allocator; + + res = sphin_create(&sphin_args, &sphin); + if (RES_OK != res) { goto error; } + + /* Load config */ + if (NULL != args->input_filename) { + res = sphin_load(sphin, args->input_filename, &config); + } + else { + res = sphin_load_stream(sphin, stdin, "stdin", &config); + } + if (RES_OK != res) { goto error; } + +exit: + if (NULL != sphin) { SPHIN(ref_put(sphin)); } + sphor->config = config; + return res; +error: + if (NULL != config) { + SPHIN(config_ref_put(config)); + config = NULL; + } + goto exit; +} + +/******************************************************************************* + * Local functions + ******************************************************************************/ +res_T +setup_config + (struct sphor* sphor, + const struct sphor_create_args* args) +{ + res_T res = RES_OK; + + ASSERT(NULL != sphor); + ASSERT(NULL != args); + + res = load_config(sphor, args); + if (RES_OK != res) { goto error; } + +exit: + return res; +error: + goto exit; +} diff --git a/src/sphor_main.c b/src/sphor_main.c @@ -0,0 +1,176 @@ +/* Copyright (C) 2024-2025 Centre National de la Recherche Scientifique + * Copyright (C) 2024-2025 Clermont Auvergne INP + * Copyright (C) 2024-2025 INSA Lyon + * Copyright (C) 2024-2025 Institut Mines Télécom Albi-Carmaux + * Copyright (C) 2024-2025 Institut National Polytechnique de Toulouse + * Copyright (C) 2024-2025 |Méso|Star> (contact@meso-star.com) + * Copyright (C) 2024-2025 PhotonLyX (info@photonlyx.com) + * Copyright (C) 2024-2025 Université de Lorraine + * Copyright (C) 2024-2025 Université Paul Sabatier + * Copyright (C) 2024-2025 Université Toulouse - Jean Jaurès + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#define _POSIX_C_SOURCE 200112L /* for getopt support */ + +#include "sphor.h" + +#include <rsys/cstr.h> +#include <rsys/mem_allocator.h> + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +struct args { + int verbose; /* Verbosity level. Bigger values => more verbosity */ + int quit; /* Either stop (1) or continue (0) the execution */ + unsigned nthreads; /* hint: number of threads; defaults to all available + threads in the computer */ + size_t samples; /* number of realisations used during the Monte Carlo + integrating algorithm */ + char* input_filename; /* May be NULL <=> uses stdin */ + char* output_filename; /* May be NULL <=> uses stdout */ +}; + +#define ARGS_DEFAULT__ {0, 0, UINT_MAX, 10000, NULL, NULL} + +static const struct args ARGS_DEFAULT = + ARGS_DEFAULT__; + +static void +usage(FILE* stream) +{ + ASSERT(NULL != stream); + fprintf(stream, + "Usage: star-phor [-hv] [-n samples] [-o output] [-t threads] [file]\n"); +} + +static res_T +parse_args + (struct args* args, + int argc, + char** argv) +{ + unsigned long nreal; + unsigned int nthreads; + int opt = 0; + char* arglist = "hn:o:t:v"; + res_T res = RES_OK; + + ASSERT(NULL != args); + ASSERT(NULL != argv); + ASSERT(argc > 0); + + *args = ARGS_DEFAULT; + + while((opt = getopt(argc, argv, arglist)) != -1) { + switch(opt) { + case 'h': + usage(stdout); + args->quit = 1; + break; + /* Verbosity level */ + case 'v': + /* Increase the verbosity level counter for each 'v' entered, up to a + * maximum of 3. We use the boolean value of (3 > args->verbose): if + * the condition is true, it evaluates to 1, increasing the counter. + * Otherwise, it evaluates to 0, leaving the counter unchanged. */ + args->verbose += (3 > args->verbose); + break; + /* Output filename */ + case 'o': + args->output_filename = optarg; + break; + /* Number of samples */ + case 'n': + res = cstr_to_ulong(optarg, &nreal); + if(res == RES_OK && nreal == 0) res = RES_BAD_ARG; + args->samples = (size_t)nreal; + break; + /* Number of threads */ + case 't': + res = cstr_to_uint(optarg, &nthreads); + if(res == RES_OK && nthreads == 0) res = RES_BAD_ARG; + args->nthreads = nthreads; + break; + default: + res = RES_BAD_ARG; + break; + } + if (RES_OK != res) { + if (NULL != optarg) { + fprintf(stderr, "Invalid option argument: %s -- %c\n", optarg, opt); + } + goto error; + } + } + + /* optind is a global variable that indexes the next argv to be processed */ + if (optind < argc) { /* True if not all the args have been processed */ + args->input_filename = argv[optind]; + } + +exit: + return res; +error: + usage(stderr); + goto exit; +} + +int +main (int argc, char** argv) +{ + struct sphor* sphor = NULL; + struct args args = ARGS_DEFAULT; + struct sphor_create_args sphor_create_args = SPHOR_CREATE_ARGS_DEFAULT; + size_t sz = 0; + int err = EXIT_SUCCESS; + res_T res = RES_OK; + + /* Process command line args */ + res = parse_args(&args, argc, argv); + if (RES_OK != res) { goto error; } + if (0 != args.quit) { goto exit; } + + /* Create sphor struct */ + sphor_create_args.verbose = args.verbose; + sphor_create_args.samples = args.samples; + sphor_create_args.nthreads = args.nthreads; + sphor_create_args.input_filename = args.input_filename; + sphor_create_args.output_filename = args.output_filename; + + res = sphor_create(&sphor_create_args, &sphor); + if (RES_OK != res) { goto error; } + + /* Run simulation */ + res = sphor_run(sphor); + if (RES_OK != res) { goto error; } + + /* TODO write results to output */ + +exit: + if (NULL != sphor) { SPHOR(ref_put(sphor)); } + sz = mem_allocated_size(); + if (sz != 0) { + char buffer[64] = {0}; /* Initialize all fields to 0 */ + size_to_cstr(sz, SIZE_ALL, NULL, buffer, sizeof(buffer)); + fprintf(stderr, "Memory leaks: %s\n", buffer); + err = EXIT_FAILURE; + } + return err; +error: + err = EXIT_FAILURE; + goto exit; +} diff --git a/src/test_sphor_lib.c b/src/test_sphor_lib.c @@ -0,0 +1,6 @@ +int main() { + + return 0; +} + +