rnatm

Load and structure data describing an atmosphere
git clone git://git.meso-star.fr/rnatm.git
Log | Files | Refs | README | LICENSE

commit 6cc415ceb369c624c5b6de17520d947ff2ff7010
parent 5b4d7990f21cf83c41e7f289a68b2a6d21b73d21
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Fri, 18 Nov 2022 11:26:07 +0100

Upd the API of rnatm_get_radcoef and rnatm_sample_component

The input position of these functions is no longer an absolute position
but is now relative to a list of cells per component in which the
position is located. The aforementioned functions can therefore use the
input cells directly rather than having to retrieve them from the BVH
that partitions the cells.

This API change optimizes the caller's code when the same position is
used to sample a component, retrieve its radiative coefficients, or
create the associated phase function; the caller can query BVH once with
the rnatm_fetch_cell_list function and reuse the returned cells multiple
times, thus amortizing the cost of traversing the BVH.

Diffstat:
Msrc/rnatm.h | 16++++++++++++----
Msrc/rnatm_properties.c | 30+++++++++++-------------------
2 files changed, 23 insertions(+), 23 deletions(-)

diff --git a/src/rnatm.h b/src/rnatm.h @@ -109,7 +109,9 @@ static const struct rnatm_band_desc RNATM_BAND_DESC_NULL = RNATM_BAND_DESC_NULL__; struct rnatm_get_radcoef_args { - double pos[3]; /* Position to be queried */ + /* Cells to be queried. Refer to the rnatm_fetch_cell_list function */ + struct rnatm_cell_pos* cells; + size_t iband; /* Index of the spectral band to consider */ size_t iquad; /* Index of the quadrature point to consider */ @@ -120,13 +122,15 @@ struct rnatm_get_radcoef_args { double k_max; }; #define RNATM_GET_RADCOEF_ARGS_NULL__ { \ - {0,0,0}, 0, 0, RNATM_RADCOEFS_COUNT__, -DBL_MAX, DBL_MAX \ + NULL, 0, 0, RNATM_RADCOEFS_COUNT__, -DBL_MAX, DBL_MAX \ } static const struct rnatm_get_radcoef_args RNATM_GET_RADCOEF_ARGS_NULL = RNATM_GET_RADCOEF_ARGS_NULL__; struct rnatm_sample_component_args { - double pos[3]; /* Position to be queried */ + /* Cells to be queried. Refer to the rnatm_fetch_cell_list function */ + const struct rnatm_cell_pos* cells; + size_t iband; /* Index of the spectral band to consider */ size_t iquad; /* Index of the quadrature point to consider */ @@ -135,7 +139,7 @@ struct rnatm_sample_component_args { double r; /* Random number uniformaly distributed in [0, 1[ */ }; #define RNATM_SAMPLE_COMPONENT_ARGS_NULL__ { \ - {0,0,0}, 0, 0, RNATM_RADCOEFS_COUNT__, 0 \ + NULL, 0, 0, RNATM_RADCOEFS_COUNT__, 0 \ } static const struct rnatm_sample_component_args RNATM_SAMPLE_COMPONENT_ARGS_NULL = RNATM_SAMPLE_COMPONENT_ARGS_NULL__; @@ -254,6 +258,10 @@ static const struct rnatm_create_args RNATM_CREATE_ARGS_DEFAULT = /* Opaque data types */ struct rnatm; +/* Helper macro that returns the cell of a component from a list of + * rnatm_cell_pos returned by the rnatm_fetch_cell_list function */ +#define RNATM_GET_COMPONENT_CELL(Cells, Cpnt) ((Cells)[(Cpnt)+1]) + BEGIN_DECLS /******************************************************************************* diff --git a/src/rnatm_properties.c b/src/rnatm_properties.c @@ -46,6 +46,9 @@ check_rnatm_get_radcoef_args if(!args) return RES_BAD_ARG; + /* Invalid cells */ + if(!args->cells) return RES_BAD_ARG; + /* Invalid band index */ n = darray_band_size_get(&atm->bands) - 1; if(args->iband < darray_band_cdata_get(&atm->bands)[0].index @@ -78,6 +81,9 @@ check_rnatm_sample_component_args if(!args) return RES_BAD_ARG; + /* Invalid cells */ + if(!args->cells) return RES_BAD_ARG; + /* Invalid band index */ n = darray_band_size_get(&atm->bands) - 1; if(args->iband < darray_band_cdata_get(&atm->bands)[0].index @@ -965,7 +971,6 @@ compute_unnormalized_cumulative_radcoef (const struct rnatm* atm, const enum rnatm_radcoef radcoef, const struct rnatm_cell_pos* cells, - const size_t ncells, const size_t iband, const size_t iquad, float cumulative[RNATM_MAX_COMPONENTS_COUNT], @@ -980,13 +985,12 @@ compute_unnormalized_cumulative_radcoef size_t icumul = 0; float k = 0; res_T res = RES_OK; - ASSERT(atm && cells && ncells && (unsigned)radcoef < RNATM_RADCOEFS_COUNT__); + ASSERT(atm && cells && (unsigned)radcoef < RNATM_RADCOEFS_COUNT__); ASSERT(cumulative && cumulative_sz); (void)k_min; naerosols = darray_aerosol_size_get(&atm->aerosols); ASSERT(naerosols+1 < RNATM_MAX_COMPONENTS_COUNT); - ASSERT(naerosols+1 == ncells); /* Setup the arguments common to all components */ cell_args.iband = iband; @@ -1031,9 +1035,7 @@ rnatm_get_radcoef const struct rnatm_get_radcoef_args* args, double* out_k) { - struct rnatm_cell_pos cells[RNATM_MAX_COMPONENTS_COUNT]; float cumul[RNATM_MAX_COMPONENTS_COUNT]; - size_t ncells; size_t cumul_sz; double k = 0; res_T res = RES_OK; @@ -1042,15 +1044,11 @@ rnatm_get_radcoef res = check_rnatm_get_radcoef_args(atm, args); if(res != RES_OK) goto error; - /* Retrieve the cell by component in which pos is located */ - res = rnatm_fetch_cell_list(atm, args->pos, cells, &ncells); - if(res != RES_OK) goto error; - /* Calculate the cumulative (unnormalized) of radiative coefficients. Its last * entry is the sum of the radiative coefficients of each component, which is * the atmospheric radiative coefficient to be returned. */ - res = compute_unnormalized_cumulative_radcoef(atm, args->radcoef, cells, - ncells, args->iband, args->iquad, cumul, &cumul_sz, args->k_min, + res = compute_unnormalized_cumulative_radcoef(atm, args->radcoef, + args->cells, args->iband, args->iquad, cumul, &cumul_sz, args->k_min, args->k_max); if(res != RES_OK) goto error; @@ -1074,11 +1072,9 @@ rnatm_sample_component const struct rnatm_sample_component_args* args, size_t* cpnt) { - struct rnatm_cell_pos cells[RNATM_MAX_COMPONENTS_COUNT]; float cumul[RNATM_MAX_COMPONENTS_COUNT]; float norm; size_t cumul_sz; - size_t ncells; size_t i; res_T res = RES_OK; @@ -1092,12 +1088,8 @@ rnatm_sample_component goto exit; } - /* Retrieve the cell by component in which pos is located */ - res = rnatm_fetch_cell_list(atm, args->pos, cells, &ncells); - if(res != RES_OK) goto error; - - res = compute_unnormalized_cumulative_radcoef(atm, args->radcoef, cells, - ncells, args->iband, args->iquad, cumul, &cumul_sz, -DBL_MAX, DBL_MAX); + res = compute_unnormalized_cumulative_radcoef(atm, args->radcoef, args->cells, + args->iband, args->iquad, cumul, &cumul_sz, -DBL_MAX, DBL_MAX); if(res != RES_OK) goto error; ASSERT(cumul_sz >= 1);