rnsf

Define and load a phase function data format
git clone git://git.meso-star.fr/rnsf.git
Log | Files | Refs | README | LICENSE

test_rnsf_load.c (3289B)


      1 /* Copyright (C) 2022, 2023 Centre National de la Recherche Scientifique
      2  * Copyright (C) 2022, 2023 Institut Pierre-Simon Laplace
      3  * Copyright (C) 2022, 2023 Institut de Physique du Globe de Paris
      4  * Copyright (C) 2022, 2023 |Méso|Star> (contact@meso-star.com)
      5  * Copyright (C) 2022, 2023 Observatoire de Paris
      6  * Copyright (C) 2022, 2023 Université de Reims Champagne-Ardenne
      7  * Copyright (C) 2022, 2023 Université de Versaille Saint-Quentin
      8  * Copyright (C) 2022, 2023 Université Paul Sabatier
      9  *
     10  * This program is free software: you can redistribute it and/or modify
     11  * it under the terms of the GNU General Public License as published by
     12  * the Free Software Foundation, either version 3 of the License, or
     13  * (at your option) any later version.
     14  *
     15  * This program is distributed in the hope that it will be useful,
     16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     18  * GNU General Public License for more details.
     19  *
     20  * You should have received a copy of the GNU General Public License
     21  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     22 
     23 #include "rnsf.h"
     24 #include <rsys/math.h>
     25 #include <rsys/mem_allocator.h>
     26 
     27 static void
     28 check_phase_fn_discrete(const struct rnsf_phase_fn* phase_fn)
     29 {
     30   struct rnsf_phase_fn_discrete discrete = RNSF_PHASE_FN_DISCRETE_NULL;
     31   size_t i;
     32 
     33   CHK(rnsf_phase_fn_get_discrete(phase_fn, &discrete) == RES_OK);
     34 
     35   /* !NaN */
     36   CHK(discrete.wavelengths[0] == discrete.wavelengths[0]);
     37   CHK(discrete.wavelengths[1] == discrete.wavelengths[1]);
     38   CHK(discrete.nitems >= 2);
     39 
     40   FOR_EACH(i, 0, discrete.nitems) {
     41     const struct rnsf_discrete_item* item = discrete.items + i;
     42 
     43     /* !NaN */
     44     CHK(item->theta == item->theta);
     45     CHK(item->value == item->value);
     46 
     47     CHK(item->theta >= 0);
     48     CHK(item->theta < PI || eq_eps(item->theta, PI, 1.e-6));
     49 
     50     if(i > 0) {
     51       CHK(item[0].theta > item[-1].theta);
     52     }
     53   }
     54 }
     55 
     56 static void
     57 check_phase_fn_hg(const struct rnsf_phase_fn* phase_fn)
     58 {
     59   struct rnsf_phase_fn_hg hg = RNSF_PHASE_FN_HG_NULL;
     60 
     61   CHK(rnsf_phase_fn_get_hg(phase_fn, &hg) == RES_OK);
     62 
     63   /* !NaN */
     64   CHK(hg.wavelengths[0] == hg.wavelengths[0]);
     65   CHK(hg.wavelengths[1] == hg.wavelengths[1]);
     66   CHK(hg.g = hg.g);
     67 
     68   CHK(hg.wavelengths[0] <= hg.wavelengths[1]);
     69   CHK(-1 <= hg.g && hg.g <= 1);
     70 }
     71 
     72 int
     73 main(int argc, char** argv)
     74 {
     75   struct rnsf_create_args args = RNSF_CREATE_ARGS_DEFAULT;
     76   struct rnsf* rnsf = NULL;
     77   int i;
     78   (void)argc, (void)argv;
     79 
     80   args.verbose = 1;
     81   CHK(rnsf_create(&args, &rnsf) == RES_OK);
     82 
     83   FOR_EACH(i, 1, argc) {
     84     size_t iphase_fn;
     85     size_t nphase_fn;
     86 
     87     printf("Load %s\n", argv[i]);
     88     CHK(rnsf_load(rnsf, argv[i]) == RES_OK);
     89 
     90     nphase_fn = rnsf_get_phase_fn_count(rnsf);
     91     FOR_EACH(iphase_fn, 0, nphase_fn) {
     92       const struct rnsf_phase_fn* phase_fn = rnsf_get_phase_fn(rnsf, iphase_fn);
     93 
     94       switch(rnsf_phase_fn_get_type(phase_fn)) {
     95         case RNSF_PHASE_FN_DISCRETE:
     96           check_phase_fn_discrete(phase_fn);
     97           break;
     98         case RNSF_PHASE_FN_HG:
     99           check_phase_fn_hg(phase_fn);
    100           break;
    101         default: FATAL("Unreachable code\n"); break;
    102       }
    103     }
    104   }
    105   CHK(rnsf_ref_put(rnsf) == RES_OK);
    106   CHK(mem_allocated_size() == 0);
    107   return 0;
    108 }