star-ck

Describe the radiative properties of gas mixtures
git clone git://git.meso-star.fr/star-ck.git
Log | Files | Refs | README | LICENSE

commit b95159c064306498d5bf725a78e7f0dbfc37acdc
parent d20041c579fca392de2484fc147caec513410441
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Mon,  2 Nov 2020 12:54:39 +0100

Test the loading procedures

Diffstat:
Mcmake/CMakeLists.txt | 1+
Asrc/test_atrck_load.c | 176+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 177 insertions(+), 0 deletions(-)

diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -78,6 +78,7 @@ if(NOT NO_TEST) endfunction() new_test(test_atrck) + new_test(test_atrck_load) endif() ################################################################################ diff --git a/src/test_atrck_load.c b/src/test_atrck_load.c @@ -0,0 +1,176 @@ +/* Copyright (C) 2020 CNRS + * + * 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 "atrck.h" + +#include <rsys/mem_allocator.h> + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static void +check_atrck_load + (const struct atrck* atrck, + const size_t nbands, + const size_t nnodes) +{ + struct atrck_band band = ATRCK_BAND_NULL; + struct atrck_quad_pt qpt = ATRCK_QUAD_PT_NULL; + size_t iband; + + CHK(atrck); + CHK(nbands); + CHK(nnodes); + + CHK(atrck_get_bands_count(atrck) == nbands); + CHK(atrck_get_nodes_count(atrck) == nnodes); + + CHK(atrck_get_band(NULL, 0, &band) == RES_BAD_ARG); + CHK(atrck_get_band(atrck, nbands, &band) == RES_BAD_ARG); + CHK(atrck_get_band(atrck, nbands, NULL) == RES_BAD_ARG); + CHK(atrck_get_band(atrck, 0, &band) == RES_OK); + + CHK(band.quad_pts_count == 1); + CHK(atrck_band_get_quad_pt(NULL, &band, 0, &qpt) == RES_BAD_ARG); + CHK(atrck_band_get_quad_pt(atrck, NULL, 0, &qpt) == RES_BAD_ARG); + CHK(atrck_band_get_quad_pt(atrck, &band, 1, &qpt) == RES_BAD_ARG); + CHK(atrck_band_get_quad_pt(atrck, &band, 0, NULL) == RES_BAD_ARG); + + FOR_EACH(iband, 0, nbands) { + const double low = (double)iband; + const double upp = (double)(iband+1); + const size_t nqpts = iband + 1; + size_t iqpt; + + CHK(atrck_get_band(atrck, iband, &band) == RES_OK); + CHK(band.lower == low); + CHK(band.upper == upp); + CHK(band.id == iband); + CHK(band.quad_pts_count == nqpts); + + FOR_EACH(iqpt, 0, nqpts) { + const double abscissa = (double)iqpt*10.0; + const double weight = (double)(iqpt+1)*0.1; + size_t inode; + + CHK(atrck_band_get_quad_pt(atrck, &band, iqpt, &qpt) == RES_OK); + CHK(qpt.abscissa == abscissa); + CHK(qpt.weight == weight); + + FOR_EACH(inode, 0, nnodes) { + const double ka = (double)(iband*1000 + iqpt*100 + inode); + CHK(qpt.ka_list[inode] == ka); + } + } + } +} + +static void +test_load(struct atrck* atrck) +{ + FILE* fp = NULL; + const char* filename = "test_file.atrck"; + const uint64_t pagesize = 16384; + const uint64_t nbands = 3; + const uint64_t nnodes = 100; + uint64_t iband; + const char byte = 0; + + fp = fopen(filename, "w+"); + CHK(fp); + + /* Write the header */ + CHK(fwrite(&pagesize, sizeof(pagesize), 1, fp) == 1); + CHK(fwrite(&nbands, sizeof(nbands), 1, fp) == 1); + CHK(fwrite(&nnodes, sizeof(nnodes), 1, fp) == 1); + + + FOR_EACH(iband, 0, nbands) { + const double low = (double)iband; + const double upp = (double)(iband+1); + const uint64_t nqpts = iband + 1; + uint64_t iqpt; + + /* Write band description */ + CHK(fwrite(&iband, sizeof(iband), 1, fp) == 1); + CHK(fwrite(&low, sizeof(low), 1, fp) == 1); + CHK(fwrite(&upp, sizeof(upp), 1, fp) == 1); + CHK(fwrite(&nqpts, sizeof(nqpts), 1, fp) == 1); + + /* Write per band quadrature points */ + FOR_EACH(iqpt, 0, nqpts) { + const double abscissa = (double)iqpt*10.0; + const double weight = (double)(iqpt+1)*0.1; + CHK(fwrite(&abscissa, sizeof(abscissa), 1, fp) == 1); + CHK(fwrite(&weight, sizeof(weight), 1, fp) == 1); + } + } + + /* Write per band and per quadrature point list of ka */ + FOR_EACH(iband, 0, nbands) { + const uint64_t nqpts = iband + 1; + uint64_t iqpt; + FOR_EACH(iqpt, 0, nqpts) { + uint64_t inode; + + /* Padding */ + CHK(fseek(fp, (long)ALIGN_SIZE((size_t)ftell(fp), pagesize), SEEK_SET)==0); + + FOR_EACH(inode, 0, nnodes) { + const double ka = (double)(iband*1000 + iqpt*100 + inode); + CHK(fwrite(&ka, sizeof(ka), 1, fp) == 1); + } + } + } + + /* Padding. Write one char to position the EOF indicator */ + CHK(fseek(fp, (long)ALIGN_SIZE((size_t)ftell(fp), pagesize)-1, SEEK_SET) == 0); + CHK(fwrite(&byte, sizeof(byte), 1, fp) == 1); + + rewind(fp); + + CHK(atrck_load_stream(NULL, fp, filename) == RES_BAD_ARG); + CHK(atrck_load_stream(atrck, NULL, filename) == RES_BAD_ARG); + CHK(atrck_load_stream(atrck, fp, NULL) == RES_OK); + check_atrck_load(atrck, nbands, nnodes); + + rewind(fp); + CHK(atrck_load_stream(atrck, fp, filename) == RES_OK); + + CHK(atrck_load(NULL, filename) == RES_BAD_ARG); + CHK(atrck_load(atrck, NULL) == RES_BAD_ARG); + CHK(atrck_load(atrck, "nop") == RES_IO_ERR); + CHK(atrck_load(atrck, filename) == RES_OK); + + fclose(fp); +} + +/******************************************************************************* + * Main function + ******************************************************************************/ +int +main(int argc, char** argv) +{ + struct atrck* atrck = NULL; + (void)argc, (void)argv; + + CHK(atrck_create(NULL, &mem_default_allocator, 1, &atrck) == RES_OK); + + test_load(atrck); + + CHK(atrck_ref_put(atrck) == RES_OK); + CHK(mem_allocated_size() == 0); + return 0; +}