atrstm

Load and structure a combustion gas mixture
git clone git://git.meso-star.fr/atrstm.git
Log | Files | Refs | README | LICENSE

atrstm.h (15417B)


      1 /* Copyright (C) 2022, 2023 |Méso|Star> (contact@meso-star.com)
      2  * Copyright (C) 2020, 2021 Centre National de la Recherche Scientifique
      3  *
      4  * This program is free software: you can redistribute it and/or modify
      5  * it under the terms of the GNU General Public License as published by
      6  * the Free Software Foundation, either version 3 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     12  * GNU General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU General Public License
     15  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     16 
     17 #ifndef ATRSTM_H
     18 #define ATRSTM_H
     19 
     20 #include <star/suvm.h>
     21 #include <star/svx.h>
     22 
     23 #include <rsys/rsys.h>
     24 
     25 #include <float.h>
     26 #include <limits.h>
     27 
     28 /* Library symbol management */
     29 #if defined(ATRSTM_SHARED_BUILD) /* Build shared library */
     30   #define ATRSTM_API extern EXPORT_SYM
     31 #elif defined(ATRSTM_STATIC) /* Use/build static library */
     32   #define ATRSTM_API extern LOCAL_SYM
     33 #else /* Use shared library */
     34   #define ATRSTM_API extern IMPORT_SYM
     35 #endif
     36 
     37 /* Helper macro that asserts if the invocation of the atrstm function `Func'
     38  * returns an error. One should use this macro on suvm function calls for
     39  * which no explicit error checking is performed */
     40 #ifndef NDEBUG
     41   #define ATRSTM(Func) ASSERT(atrstm_ ## Func == RES_OK)
     42 #else
     43   #define ATRSTM(Func) atrstm_ ## Func
     44 #endif
     45 
     46 /* Radiative coefficients */
     47 enum atrstm_radcoef {
     48   ATRSTM_RADCOEF_Ks, /* Scattering coefficient */
     49   ATRSTM_RADCOEF_Ka, /* Absorption coefficient */
     50   ATRSTM_RADCOEF_Kext, /* Extinction coefficient = Ks + Ka */
     51   ATRSTM_RADCOEFS_COUNT__
     52 };
     53 
     54 enum atrstm_radcoef_flag {
     55   ATRSTM_RADCOEF_FLAG_Ks = BIT(ATRSTM_RADCOEF_Ks),
     56   ATRSTM_RADCOEF_FLAG_Ka = BIT(ATRSTM_RADCOEF_Ka),
     57   ATRSTM_RADCOEF_FLAG_Kext = BIT(ATRSTM_RADCOEF_Kext),
     58   ATRSTM_RADCOEFS_MASK_ALL =
     59     ATRSTM_RADCOEF_FLAG_Ks
     60   | ATRSTM_RADCOEF_FLAG_Ka
     61   | ATRSTM_RADCOEF_FLAG_Kext
     62 };
     63 
     64 /* List of medium components */
     65 enum atrstm_component {
     66   ATRSTM_CPNT_SOOT,
     67   ATRSTM_CPNT_GAS,
     68   ATRSTM_CPNTS_COUNT__
     69 };
     70 
     71 enum atrstm_component_flag {
     72   ATRSTM_CPNT_FLAG_SOOT = BIT(ATRSTM_CPNT_SOOT),
     73   ATRSTM_CPNT_FLAG_GAS = BIT(ATRSTM_CPNT_GAS),
     74   ATRSTM_CPNTS_MASK_ALL = ATRSTM_CPNT_FLAG_SOOT | ATRSTM_CPNT_FLAG_GAS
     75 };
     76 
     77 enum atrstm_svx_op {
     78   ATRSTM_SVX_OP_MIN,
     79   ATRSTM_SVX_OP_MAX,
     80   ATRSTM_SVX_OPS_COUNT__
     81 };
     82 
     83 enum atrstm_svx_op_flag {
     84   ATRSTM_SVX_OP_FLAG_MIN = BIT(ATRSTM_SVX_OP_MIN),
     85   ATRSTM_SVX_OP_FLAG_MAX = BIT(ATRSTM_SVX_OP_MAX),
     86   ATRSTM_SVX_OPS_MASK_ALL = ATRSTM_SVX_OP_FLAG_MIN | ATRSTM_SVX_OP_FLAG_MAX
     87 };
     88 
     89 enum atrstm_spectral_type {
     90   ATRSTM_SPECTRAL_LW, /* Longwave */
     91   ATRSTM_SPECTRAL_SW, /* Shortwave */
     92   ATRSTM_SPECTRAL_TYPES_COUNT__
     93 };
     94 
     95 struct atrstm_args {
     96   const char* sth_filename; /* Filename of the Star-TetraHedra mesh */
     97   const char* atrck_filename; /* Filename of the Correlated K */
     98   const char* atrtp_filename; /* Filename of the Thermodynamic properties */
     99   const char* atrri_filename; /* Filename of the refraction index LUT */
    100   const char* cache_filename; /* Cache to use. NULL <=> no cache */
    101   const char* name; /* Name of the medium */
    102 
    103   enum atrstm_spectral_type spectral_type; /* Longwave/shortwave */
    104   double wlen_range[2]; /* Spectral range to handle In nm */
    105 
    106   double fractal_prefactor;
    107   double fractal_dimension;
    108 
    109   unsigned grid_max_definition[3]; /* Fixed grid definition along the 3 axes */
    110   unsigned auto_grid_definition_hint; /* Hint on the grid definition to eval */
    111   int auto_grid_definition; /* Switch between auto and fixed grid definition */
    112 
    113   double optical_thickness; /* Threshold used during octree building */
    114 
    115   int precompute_normals; /* Pre-compute the tetrahedra normals */
    116   int use_simd; /* Enable the use of the SIMD instruction set if available */
    117 
    118   unsigned nthreads; /* Hint on the number of threads to use */
    119   int verbose; /* Verbosity level */
    120 };
    121 
    122 #define ATRSTM_ARGS_DEFAULT__ {                                                \
    123   NULL, /* STh filename */                                                     \
    124   NULL, /* AtrCK filename */                                                   \
    125   NULL, /* AtrTP filename */                                                   \
    126   NULL, /* AtrRI filename */                                                   \
    127   NULL, /* Cache filename */                                                   \
    128   "semi transparent medium", /* Name */                                        \
    129                                                                                \
    130   ATRSTM_SPECTRAL_SW, /* Spectral type */                                      \
    131   {500,500}, /* Spectral integration range */                                  \
    132                                                                                \
    133   1.70, /* Fractal prefactor */                                                \
    134   1.75, /* Fractal dimension */                                                \
    135                                                                                \
    136   {256, 256, 256}, /* Acceleration grid max definition */                      \
    137   256, /* Hint on grid Definition in 'auto grid definition' mode */            \
    138   1, /* Enable/disable 'auto grid definition' mode */                          \
    139                                                                                \
    140   1, /* Optical thickness */                                                   \
    141                                                                                \
    142   0, /* Precompute tetrahedra normals */                                       \
    143   1, /* Use SIMD */                                                            \
    144                                                                                \
    145   (unsigned)~0, /* #threads */                                                 \
    146   0 /* Verbosity level */                                                      \
    147 }
    148 static const struct atrstm_args ATRSTM_ARGS_DEFAULT = ATRSTM_ARGS_DEFAULT__;
    149 
    150 struct atrstm_fetch_radcoefs_args {
    151   struct suvm_primitive prim; /* Volumetric primitive to query */
    152   double bcoords[4]; /* Barycentric coordinates of the pos in prim to query */
    153   size_t iband; /* Spectral band index. Not use in shortwave */
    154   size_t iquad; /* Quadrature point index. Not use in shortwave */
    155   double wavelength; /* In nm */
    156 
    157   int radcoefs_mask; /* Combination of atrstm_radcoef_flag */
    158   int components_mask; /* Combination of atrstm_component_flag */
    159 
    160   /* For debug: assert if the fetched property is not in [k_min, k_max] */
    161   double k_min[ATRSTM_RADCOEFS_COUNT__];
    162   double k_max[ATRSTM_RADCOEFS_COUNT__];
    163 };
    164 
    165 #define ATRSTM_FETCH_RADCOEFS_ARGS_DEFAULT__ {                                 \
    166   SUVM_PRIMITIVE_NULL__,                                                       \
    167   {0, 0, 0, 1}, /* Barycentric coordinates */                                  \
    168   SIZE_MAX, /* Index of the spectral band */                                   \
    169   SIZE_MAX, /* Index of the quadrature point */                                \
    170   DBL_MAX, /* Wavelength */                                                    \
    171                                                                                \
    172   ATRSTM_RADCOEFS_MASK_ALL, /*  Mask of radiative properties to fetch */       \
    173   ATRSTM_CPNTS_MASK_ALL, /* Mask of component to handle */                     \
    174                                                                                \
    175   /* For debug */                                                              \
    176   {-DBL_MAX,-DBL_MAX,-DBL_MAX}, /* Kmin */                                     \
    177   { DBL_MAX, DBL_MAX, DBL_MAX}, /* Kmax */                                     \
    178 }
    179 static const struct atrstm_fetch_radcoefs_args
    180 ATRSTM_FETCH_RADCOEFS_ARGS_DEFAULT = ATRSTM_FETCH_RADCOEFS_ARGS_DEFAULT__;
    181 
    182 struct atrstm_fetch_radcoefs_svx_args {
    183   double pos[3]; /* Position to query */
    184   size_t iband; /* Spectral band index. Not use in shortwave */
    185   size_t iquad; /* Quadrature point index. Not use in shortwave */
    186 
    187   int radcoefs_mask; /* Combination of atrstm_radcoef_flag */
    188   int components_mask; /* Combination of atrstm_component_flag */
    189   int operations_mask; /* Combination of atrstm_svx_op_flag */
    190 };
    191 
    192 #define ATRSTM_FETCH_RADCOEFS_SVX_ARGS_DEFAULT__ {                             \
    193   {0, 0, 0}, /* Position */                                                    \
    194   SIZE_MAX, /* Index of the spectral band */                                   \
    195   SIZE_MAX, /* Index of the quadrature point */                                \
    196                                                                                \
    197   ATRSTM_RADCOEFS_MASK_ALL, /*  Mask of radiative properties to fetch */       \
    198   ATRSTM_CPNTS_MASK_ALL, /* Mask of component to handle */                     \
    199   ATRSTM_SVX_OPS_MASK_ALL, /* Mask of operations to query */                   \
    200 }
    201 static const struct atrstm_fetch_radcoefs_svx_args
    202 ATRSTM_FETCH_RADCOEFS_SVX_ARGS_DEFAULT =
    203   ATRSTM_FETCH_RADCOEFS_SVX_ARGS_DEFAULT__;
    204 
    205 struct atrstm_fetch_radcoefs_svx_voxel_args {
    206   struct svx_voxel voxel; /* Voxel to query */
    207   size_t iband; /* Spectral band index. Not use in shortwave */
    208   size_t iquad; /* Quadrature point index. Not use in shortwave */
    209 
    210   int radcoefs_mask; /* Combination of atrstm_radcoef_flag */
    211   int components_mask; /* Combination of atrstm_component_flag */
    212   int operations_mask; /* Combination of atrstm_svx_op_flag */
    213 };
    214 
    215 #define ATRSTM_FETCH_RADCOEFS_SVX_VOXEL_ARGS_DEFAULT__ {                       \
    216   SVX_VOXEL_NULL__, /* Voxel */                                                \
    217   SIZE_MAX, /* Index of the spectral band */                                   \
    218   SIZE_MAX, /* Index of the quadrature point */                                \
    219                                                                                \
    220   ATRSTM_RADCOEFS_MASK_ALL, /*  Mask of radiative properties to fetch */       \
    221   ATRSTM_CPNTS_MASK_ALL, /* Mask of component to handle */                     \
    222   ATRSTM_SVX_OPS_MASK_ALL, /* Mask of operations to query */                   \
    223 }
    224 static const struct atrstm_fetch_radcoefs_svx_voxel_args
    225 ATRSTM_FETCH_RADCOEFS_SVX_VOXEL_ARGS_DEFAULT =
    226   ATRSTM_FETCH_RADCOEFS_SVX_VOXEL_ARGS_DEFAULT__;
    227 
    228 struct atrstm_dump_svx_octree_args {
    229   size_t iband; /* Spectral band index. Not use in shortwave */
    230   size_t iquad; /* Quadrature point index. Not use in shortwave */
    231 };
    232 
    233 #define ATRSTM_DUMP_SVX_OCTREE_ARGS_DEFAULT__ {SIZE_MAX, SIZE_MAX}
    234 static const struct atrstm_dump_svx_octree_args
    235 ATRSTM_DUMP_SVX_OCTREE_ARGS_DEFAULT = ATRSTM_DUMP_SVX_OCTREE_ARGS_DEFAULT__;
    236 
    237 struct atrstm_trace_ray_args {
    238   double ray_org[3];
    239   double ray_dir[3];
    240   double ray_range[2];
    241 
    242   svx_hit_challenge_T challenge; /* NULL <=> Traversed up to the leaves */
    243   svx_hit_filter_T filter; /* NULL <=> Stop RT at the 1st hit voxel */
    244   void* context; /* User data send to the 'challenge' & 'filter' function */
    245 
    246   size_t iband; /* Spectral band id. Not use in shortwave */
    247   size_t iquad; /* Quadrature point. Not use in short wave */
    248 };
    249 
    250 #define ATRSTM_TRACE_RAY_ARGS_DEFAULT__ {                                      \
    251   {0,0,0}, /* Ray origin */                                                    \
    252   {0,0,1}, /* Ray direction */                                                 \
    253   {0, DBL_MAX}, /* Ray range */                                                \
    254                                                                                \
    255   NULL, /* Challenge functor */                                                \
    256   NULL, /* Filter functor */                                                   \
    257   NULL, /* User defined data */                                                \
    258                                                                                \
    259   SIZE_MAX, /* Index of the spectral band */                                   \
    260   SIZE_MAX, /* Index of the quadrature point */                                \
    261 }
    262 static const struct atrstm_trace_ray_args ATRSTM_TRACE_RAY_ARGS_DEFAULT =
    263   ATRSTM_TRACE_RAY_ARGS_DEFAULT__;
    264 
    265 struct atrstm_fetch_rdgfa_args {
    266   struct suvm_primitive prim; /* Volumetric primitive to query */
    267   double bcoords[4]; /* Barycentric coordinates of the pos in prim to query */
    268   double wavelength; /* In nm */
    269 };
    270 
    271 #define ATRSTM_FETCH_RDGFA_ARGS_DEFAULT__ {                                    \
    272   SUVM_PRIMITIVE_NULL__,                                                       \
    273   {0, 0, 0, 1}, /* Barycentric coordinates */                                  \
    274   DBL_MAX, /* Wavelength */                                                    \
    275 }
    276 static const struct atrstm_fetch_rdgfa_args
    277 ATRSTM_FETCH_RDGFA_ARGS_DEFAULT = ATRSTM_FETCH_RDGFA_ARGS_DEFAULT__;
    278 
    279 struct atrstm_rdgfa {
    280   double wavelength; /* In nm */
    281   double fractal_dimension;
    282   double gyration_radius;
    283 };
    284 
    285 #define ATRSTM_RDGFA_NULL__ {0,0,0}
    286 static const struct atrstm_rdgfa ATRSTM_RDGFA_NULL =
    287   ATRSTM_RDGFA_NULL__;
    288 
    289 /* Types used as syntactic sugar that store the radiative coefficients */
    290 typedef double atrstm_radcoefs_T[ATRSTM_RADCOEFS_COUNT__];
    291 typedef double atrstm_radcoefs_svx_T[ATRSTM_RADCOEFS_COUNT__][ATRSTM_SVX_OPS_COUNT__];
    292 
    293 /* Forward declaration of extern data types */
    294 struct logger;
    295 struct mem_allocator;
    296 
    297 /* Forward declaration of opaque data type */
    298 struct atrstm;
    299 
    300 BEGIN_DECLS
    301 
    302 /*******************************************************************************
    303  * AtrSTM API
    304  ******************************************************************************/
    305 ATRSTM_API res_T
    306 atrstm_create
    307   (struct logger* logger, /* NULL <=> use default logger */
    308    struct mem_allocator* allocator, /* NULL <=> use default allocator */
    309    const struct atrstm_args* args,
    310    struct atrstm** atrstm);
    311 
    312 ATRSTM_API res_T
    313 atrstm_ref_get
    314   (struct atrstm* atrstm);
    315 
    316 ATRSTM_API res_T
    317 atrstm_ref_put
    318   (struct atrstm* atrstm);
    319 
    320 ATRSTM_API const char*
    321 atrstm_get_name
    322   (const struct atrstm* atrstm);
    323 
    324 ATRSTM_API void
    325 atrstm_get_aabb
    326   (const struct atrstm* atrstm,
    327    double lower[3],
    328    double upper[3]);
    329 
    330 ATRSTM_API res_T
    331 atrstm_at
    332   (const struct atrstm* atrstm,
    333    const double pos[3],
    334    struct suvm_primitive* prim, /* Volumetric primitive */
    335    double barycentric_coords[4]); /* `pos' in `prim' */
    336 
    337 ATRSTM_API res_T
    338 atrstm_fetch_radcoefs
    339   (const struct atrstm* atrstm,
    340    const struct atrstm_fetch_radcoefs_args* args,
    341    atrstm_radcoefs_T radcoefs); /* In m^-1 */
    342 
    343 ATRSTM_API res_T
    344 atrstm_fetch_radcoefs_svx
    345   (const struct atrstm* atrstm,
    346    const struct atrstm_fetch_radcoefs_svx_args* args,
    347    atrstm_radcoefs_svx_T radcoefs); /* In m^-1 */
    348 
    349 ATRSTM_API res_T
    350 atrstm_fetch_radcoefs_svx_voxel
    351   (const struct atrstm* atrstm,
    352    const struct atrstm_fetch_radcoefs_svx_voxel_args* args,
    353    atrstm_radcoefs_svx_T radcoefs); /* In m^-1 */
    354 
    355 /* Trace a ray into the SVX octree data structure */
    356 ATRSTM_API res_T
    357 atrstm_trace_ray
    358   (const struct atrstm* atstm,
    359    const struct atrstm_trace_ray_args* args,
    360    struct svx_hit* hit);
    361 
    362 ATRSTM_API res_T
    363 atrstm_dump_svx_octree
    364   (const struct atrstm* atrstm,
    365    const struct atrstm_dump_svx_octree_args* args,
    366    FILE* stream);
    367 
    368 ATRSTM_API res_T
    369 atrstm_fetch_rdgfa
    370   (const struct atrstm* atrstm,
    371    const struct atrstm_fetch_rdgfa_args* args,
    372    struct atrstm_rdgfa* rdgfa);
    373 
    374 END_DECLS
    375 
    376 #endif /* ATRSTM_H */