htrdr

Solving radiative transfer in heterogeneous media
git clone git://git.meso-star.fr/htrdr.git
Log | Files | Refs | README | LICENSE

htrdr_atmosphere_main.c (3094B)


      1 /* Copyright (C) 2018-2019, 2022-2025 Centre National de la Recherche Scientifique
      2  * Copyright (C) 2020-2022 Institut Mines Télécom Albi-Carmaux
      3  * Copyright (C) 2022-2025 Institut Pierre-Simon Laplace
      4  * Copyright (C) 2022-2025 Institut de Physique du Globe de Paris
      5  * Copyright (C) 2018-2025 |Méso|Star> (contact@meso-star.com)
      6  * Copyright (C) 2022-2025 Observatoire de Paris
      7  * Copyright (C) 2022-2025 Université de Reims Champagne-Ardenne
      8  * Copyright (C) 2022-2025 Université de Versaille Saint-Quentin
      9  * Copyright (C) 2018-2019, 2022-2025 Université Paul Sabatier
     10  *
     11  * This program is free software: you can redistribute it and/or modify
     12  * it under the terms of the GNU General Public License as published by
     13  * the Free Software Foundation, either version 3 of the License, or
     14  * (at your option) any later version.
     15  *
     16  * This program is distributed in the hope that it will be useful,
     17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     19  * GNU General Public License for more details.
     20  *
     21  * You should have received a copy of the GNU General Public License
     22  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     23 
     24 #include "atmosphere/htrdr_atmosphere.h"
     25 #include "atmosphere/htrdr_atmosphere_args.h"
     26 
     27 #include "core/htrdr.h"
     28 #include "core/htrdr_log.h"
     29 
     30 #include <rsys/mem_allocator.h>
     31 
     32 int
     33 htrdr_atmosphere_main(int argc, char** argv)
     34 {
     35   char cmd_name[] = "htrdr-atmosphere";
     36   struct htrdr_args htrdr_args = HTRDR_ARGS_DEFAULT;
     37   struct htrdr_atmosphere_args cmd_args = HTRDR_ATMOSPHERE_ARGS_DEFAULT;
     38   struct htrdr* htrdr = NULL;
     39   struct htrdr_atmosphere* cmd = NULL;
     40   const size_t memsz_begin = mem_allocated_size();
     41   size_t memsz_end;
     42   int is_mpi_init = 0;
     43   int err = 0;
     44   res_T res = RES_OK;
     45 
     46   /* Overwrite command name */
     47   argv[0] = cmd_name;
     48 
     49   res = htrdr_mpi_init(argc, argv);
     50   if(res != RES_OK) goto error;
     51   is_mpi_init = 1;
     52 
     53   res = htrdr_atmosphere_args_init(&cmd_args, argc, argv);
     54   if(res != RES_OK) goto error;
     55   if(cmd_args.quit) goto exit;
     56 
     57   htrdr_args.nthreads = cmd_args.nthreads;
     58   htrdr_args.verbose = cmd_args.verbose;
     59   res = htrdr_create(&mem_default_allocator, &htrdr_args, &htrdr);
     60   if(res != RES_OK) goto error;
     61 
     62   if(cmd_args.output_type == HTRDR_ATMOSPHERE_ARGS_OUTPUT_OCTREES
     63   && htrdr_get_mpi_rank(htrdr) != 0) {
     64     goto exit; /* Nothing to do except for the master process */
     65   }
     66 
     67   res = htrdr_atmosphere_create(htrdr, &cmd_args, &cmd);
     68   if(res != RES_OK) goto error;
     69 
     70   res = htrdr_atmosphere_run(cmd);
     71   if(res != RES_OK) goto error;
     72 
     73 exit:
     74   htrdr_atmosphere_args_release(&cmd_args);
     75   if(is_mpi_init) htrdr_mpi_finalize();
     76   if(htrdr) htrdr_ref_put(htrdr);
     77   if(cmd) htrdr_atmosphere_ref_put(cmd);
     78 
     79   /* Check memory leaks */
     80   memsz_end = mem_allocated_size();
     81   if(memsz_begin != memsz_end) {
     82     ASSERT(memsz_end >= memsz_begin);
     83     fprintf(stderr, HTRDR_LOG_WARNING_PREFIX"Memory leaks: %lu Bytes\n",
     84       (unsigned long)(memsz_end - memsz_begin));
     85     err = -1;
     86   }
     87   return err;
     88 error:
     89   err = -1;
     90   goto exit;
     91 }
     92