atrtp

Thermodynamic properties of a medium in combustion
git clone git://git.meso-star.fr/atrtp.git
Log | Files | Refs | README | LICENSE

commit ea7b3a8801b3289190bb18e21ff045ed9e63ccef
parent eb092fd573e56390090f9e8b75cabfe2ac01cabb
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Mon, 26 Oct 2020 15:55:34 +0100

Implement the load procedures

Diffstat:
Mdoc/atrtp | 2+-
Msrc/atrtp.c | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 118 insertions(+), 1 deletion(-)

diff --git a/doc/atrtp b/doc/atrtp @@ -16,5 +16,5 @@ <xH2O> ::= <double> # Molar fraction of H2O in mol(H2O)/mol(mixture) <xCO2> ::= <double> # Molar fraction of CO2 in mol(CO2)/mol(mixture) <xCO> ::= <double> # Molar fraction of CO in mol(CO)/mol(mixture) -<fv-soot> ::= <double> # Volumic fraction of soot in m^3(soot)/ m^3 +<vf-soot> ::= <double> # Volumic fraction of soot in m^3(soot)/ m^3 diff --git a/src/atrtp.c b/src/atrtp.c @@ -23,8 +23,19 @@ #include <unistd.h> +#include <errno.h> #include <sys/mman.h> /* mmap */ +enum atrtp_type { + ATRTP_PRESSURE, + ATRTP_TEMPERATURE, + ATRTP_XH20, + ATRTP_XCO2, + ATRTP_XCO, + ATRTP_VOLFRAC_SOOT, + ATRTP_COUNT__ +}; + /******************************************************************************* * Helper functions ******************************************************************************/ @@ -39,6 +50,73 @@ reset_atrtp(struct atrtp* atrtp) atrtp->map_len = 0; } +static res_T +load_stream(struct atrtp* atrtp, FILE* stream, const char* stream_name) +{ + off_t offset; + size_t filesz; + res_T res = RES_OK; + ASSERT(atrtp && stream && stream_name); + + reset_atrtp(atrtp); + + /* Read file header */ + #define READ(Var, N, Name) { \ + if(fread((Var), sizeof(*(Var)), (N), stream) != (N)) { \ + log_err(atrtp, "%s: could not read the %s.\n", stream_name, (Name)); \ + res = RES_IO_ERR; \ + goto error; \ + } \ + } (void)0 + READ(&atrtp->pagesize, 1, "page size"); + READ(&atrtp->nnodes, 1, "number of nodes"); + #undef READ + + if(!IS_ALIGNED(atrtp->pagesize, atrtp->pagesize_os)) { + log_err(atrtp, + "%s: invalid page size %li. The page size attribute must be aligned on " + "the page size of the operating system (%lu).\n", + stream_name, atrtp->pagesize, (unsigned long)atrtp->pagesize_os); + res = RES_BAD_ARG; + goto error; + } + + /* Compute the length in bytes of the data to map */ + atrtp->map_len = atrtp->nnodes * sizeof(double[ATRTP_COUNT__]); + + /* Find the offsets of the positions/indices data into the stream */ + offset = (off_t)ALIGN_SIZE((uint64_t)ftell(stream), atrtp->pagesize); + + /* Retrieve the overall filesize */ + fseek(stream, 0, SEEK_END); + filesz = (size_t)ftell(stream); + + /* Check that the file has sufficient data */ + if((size_t)offset + atrtp->map_len > filesz) { + log_err(atrtp, + "%s: the thermodynamic properties to load exceed the file size.\n", + stream_name); + res = RES_IO_ERR; + goto error; + } + + /* Map the thermodynamic properties */ + atrtp->props = mmap(NULL, atrtp->map_len, PROT_READ, + MAP_PRIVATE|MAP_POPULATE, fileno(stream), offset); + if(atrtp->props == MAP_FAILED) { + log_err(atrtp, "%s: could not map the thermodynamic properties --%s.\n", + stream_name, strerror(errno)); + res = RES_IO_ERR; + goto error; + } + +exit: + return res; +error: + reset_atrtp(atrtp); + goto exit; +} + static void release_atrtp(ref_T* ref) { @@ -120,3 +198,42 @@ atrtp_ref_put(struct atrtp* atrtp) ref_put(&atrtp->ref, release_atrtp); return RES_OK; } + +res_T +atrtp_load(struct atrtp* atrtp, const char* path) +{ + FILE* file = NULL; + res_T res = RES_OK; + + if(!atrtp || !path) { + res = RES_BAD_ARG; + goto error; + } + + file = fopen(path, "r"); + if(!file) { + log_err(atrtp, "%s: error opening file `%s'.\n", FUNC_NAME, path); + res = RES_IO_ERR; + goto error; + } + + res = load_stream(atrtp, file, path); + if(res != RES_OK) goto error; + +exit: + if(file) fclose(file); + return res; +error: + goto exit; +} + +res_T +atrtp_load_stream + (struct atrtp* atrtp, + FILE* stream, + const char* stream_name) +{ + if(!atrtp || !stream) return RES_BAD_ARG; + return load_stream(atrtp, stream, stream_name ? stream_name : "<stream>"); +} +