star-line

Structure for accelerating line importance sampling
git clone git://git.meso-star.fr/star-line.git
Log | Files | Refs | README | LICENSE

sln.h (13316B)


      1 /* Copyright (C) 2022, 2026 |Méso|Star> (contact@meso-star.com)
      2  * Copyright (C) 2026 Université de Lorraine
      3  * Copyright (C) 2022 Centre National de la Recherche Scientifique
      4  * Copyright (C) 2022 Université Paul Sabatier
      5  *
      6  * This program is free software: you can redistribute it and/or modify
      7  * it under the terms of the GNU General Public License as published by
      8  * the Free Software Foundation, either version 3 of the License, or
      9  * (at your option) any later version.
     10  *
     11  * This program is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     14  * GNU General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU General Public License
     17  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     18 
     19 #ifndef SLN_H
     20 #define SLN_H
     21 
     22 #include <star/shtr.h>
     23 #include <rsys/rsys.h>
     24 
     25 #include <float.h>
     26 #include <math.h>
     27 
     28 /* Library symbol management */
     29 #if defined(SLN_SHARED_BUILD)  /* Build shared library */
     30   #define SLN_API extern EXPORT_SYM
     31 #elif defined(SLN_STATIC)  /* Use/build static library */
     32   #define SLN_API extern LOCAL_SYM
     33 #else
     34   #define SLN_API extern IMPORT_SYM
     35 #endif
     36 
     37 /* Helper macro that asserts if the invocation of the sln function `Func'
     38  * returns an error. One should use this macro on sln calls for which no
     39  * explicit error checking is performed */
     40 #ifndef NDEBUG
     41   #define SLN(Func) ASSERT(sln_ ## Func == RES_OK)
     42 #else
     43   #define SLN(Func) sln_ ## Func
     44 #endif
     45 
     46 /* Maximum arity of a tree */
     47 #define SLN_TREE_ARITY_MAX 2
     48 
     49 /* Forward declaration of external data structures */
     50 struct logger;
     51 struct mem_allocator;
     52 struct shtr;
     53 struct shtr_line;
     54 struct shtr_isotope_metadata;
     55 struct shtr_line_list;
     56 
     57 enum sln_mesh_type {
     58   SLN_MESH_FIT, /* Fit the spectrum */
     59   SLN_MESH_UPPER, /* Upper limit of the spectrum */
     60   SLN_MESH_TYPES_COUNT__
     61 };
     62 
     63 enum sln_line_profile {
     64   SLN_LINE_PROFILE_VOIGT,
     65   SLN_LINE_PROFILES_COUNT__
     66 };
     67 
     68 struct sln_device_create_args {
     69   struct logger* logger; /* May be NULL <=> default logger */
     70   struct mem_allocator* allocator; /* NULL <=> use default allocator */
     71   int verbose; /* Verbosity level */
     72 };
     73 #define SLN_DEVICE_CREATE_ARGS_DEFAULT__ {NULL,NULL,0}
     74 static const struct sln_device_create_args SLN_DEVICE_CREATE_ARGS_DEFAULT =
     75   SLN_DEVICE_CREATE_ARGS_DEFAULT__;
     76 
     77 struct sln_isotope {
     78   double abundance; /* in [0, 1] */
     79   int id; /* Identifier of the isotope */
     80 };
     81 
     82 struct sln_molecule {
     83   struct sln_isotope isotopes[SHTR_MAX_ISOTOPE_COUNT];
     84   double concentration;
     85   double cutoff; /* [cm^-1] */
     86   int non_default_isotope_abundances;
     87 };
     88 #define SLN_MOLECULE_NULL__ {{{0}},0,0,0}
     89 static const struct sln_molecule SLN_MOLECULE_NULL = SLN_MOLECULE_NULL__;
     90 
     91 struct sln_tree_create_args {
     92   /* Isotope metadata and list of spectral lines */
     93   struct shtr_isotope_metadata* metadata;
     94   struct shtr_line_list* lines;
     95 
     96   enum sln_line_profile line_profile;
     97   /* Mixture description */
     98   struct sln_molecule molecules[SHTR_MAX_MOLECULE_COUNT];
     99 
    100     /* Thermo dynamic properties */
    101   double pressure; /* [atm] */
    102   double temperature; /* [K] */
    103 
    104   /* Hint on the number of vertices around the line center */
    105   size_t nvertices_hint;
    106 
    107   /* Relative error used to simplify the spectrum mesh. The larger it is, the
    108    * coarser the mesh */
    109   double mesh_decimation_err; /* > 0 */
    110   enum sln_mesh_type mesh_type; /* Type of mesh to generate */
    111 
    112   /* Maximum number of children per node */
    113   unsigned arity;
    114 };
    115 #define SLN_TREE_CREATE_ARGS_DEFAULT__ { \
    116   NULL, /* metadata */ \
    117   NULL, /* line list */ \
    118   SLN_LINE_PROFILE_VOIGT, /* Profile */ \
    119   {SLN_MOLECULE_NULL__}, /* Molecules */ \
    120   0, /* Pressure [atm] */ \
    121   0, /* Temperature [K] */ \
    122   16, /* #vertices hint */ \
    123   0.01f, /* Mesh decimation error */ \
    124   SLN_MESH_UPPER, /* Mesh type */ \
    125   2 /* Arity */ \
    126 }
    127 static const struct sln_tree_create_args SLN_TREE_CREATE_ARGS_DEFAULT =
    128   SLN_TREE_CREATE_ARGS_DEFAULT__;
    129 
    130 struct sln_tree_read_args {
    131   /* Metadata and list of spectral lines from which the tree was constructed */
    132   struct shtr_isotope_metadata* metadata;
    133   struct shtr_line_list* lines;
    134 
    135   /* Name of the file to read or of the provided stream.
    136    * NULL <=> uses a default name for the stream to be read, which must
    137    * therefore be defined. */
    138   const char* filename; /* Name of the file to read */
    139   FILE* file; /* Stream from where data are read. NULL <=> read from file */
    140 };
    141 #define SLN_TREE_READ_ARGS_NULL__ {NULL,NULL,NULL,NULL}
    142 static const struct sln_tree_read_args SLN_TREE_READ_ARGS_NULL =
    143   SLN_TREE_READ_ARGS_NULL__;
    144 
    145 struct sln_tree_write_args {
    146   /* Name of the file in which the tree is serialized.
    147    * NULL <=> uses a default name for the stream to be written, which must
    148    * therefore be defined. */
    149   const char* filename; /* Name of the file to read */
    150 
    151   /* Stream where data is written.
    152    * NULL <=> write to the file defined by "filename" */
    153   FILE* file;
    154 };
    155 #define SLN_TREE_WRITE_ARGS_NULL__ {NULL,NULL}
    156 static const struct sln_tree_write_args SLN_TREE_WRITE_ARGS_NULL =
    157   SLN_TREE_WRITE_ARGS_NULL__;
    158 
    159 struct sln_tree_desc {
    160   size_t max_nlines_per_leaf;
    161   double mesh_decimation_err;
    162   enum sln_mesh_type mesh_type;
    163   enum sln_line_profile line_profile;
    164 
    165   double pressure; /* [atm] */
    166   double temperature; /* [K] */
    167 
    168   unsigned depth; /* #edges from the root to the deepest leaf */
    169   size_t nlines;
    170   size_t nvertices;
    171   size_t nnodes;
    172   unsigned arity;
    173 };
    174 #define SLN_TREE_DESC_NULL__ { \
    175   0, 0, SLN_MESH_TYPES_COUNT__, SLN_LINE_PROFILES_COUNT__, 0, 0, 0, 0, 0, 0, 0 \
    176 }
    177 static const struct sln_tree_desc SLN_TREE_DESC_NULL = SLN_TREE_DESC_NULL__;
    178 
    179 struct sln_node_desc {
    180   size_t nlines;
    181   size_t nvertices;
    182 };
    183 #define SLN_NODE_DESC_NULL__ {0,0}
    184 static const struct sln_node_desc SLN_NODE_DESC_NULL = SLN_NODE_DESC_NULL__;
    185 
    186 struct sln_vertex { /* 8 Bytes */
    187   float wavenumber; /* in cm^-1 */
    188   float ka;
    189 };
    190 #define SLN_VERTEX_NULL__ {0,0}
    191 static const struct sln_vertex SLN_VERTEX_NULL = SLN_VERTEX_NULL__;
    192 
    193 struct sln_mesh {
    194   const struct sln_vertex* vertices;
    195   size_t nvertices;
    196 };
    197 #define SLN_MESH_NULL__ {NULL,0}
    198 static const struct sln_mesh SLN_MESH_NULL = SLN_MESH_NULL__;
    199 
    200 struct sln_mixture_load_args {
    201   const char* filename; /* Name of the file to load or of the provided stream */
    202   FILE* file; /* Stream from where data are loaded. NULL <=> load from file */
    203 
    204   /* Metadata from which the mix is defined */
    205   struct shtr_isotope_metadata* molparam;
    206 };
    207 #define SLN_MIXTURE_LOAD_ARGS_NULL__ {NULL,NULL,NULL}
    208 static const struct sln_mixture_load_args SLN_MIXTURE_LOAD_ARGS_NULL =
    209   SLN_MIXTURE_LOAD_ARGS_NULL__;
    210 
    211 /* Forward declarations of opaque data structures */
    212 struct sln_device;
    213 struct sln_mixture;
    214 struct sln_node;
    215 struct sln_tree;
    216 
    217 BEGIN_DECLS
    218 
    219 /*******************************************************************************
    220  * Device API
    221  ******************************************************************************/
    222 SLN_API res_T
    223 sln_device_create
    224   (const struct sln_device_create_args* args,
    225    struct sln_device** sln);
    226 
    227 SLN_API res_T
    228 sln_device_ref_get
    229   (struct sln_device* sln);
    230 
    231 SLN_API res_T
    232 sln_device_ref_put
    233   (struct sln_device* sln);
    234 
    235 
    236 /*******************************************************************************
    237  * Mixture API
    238  ******************************************************************************/
    239 SLN_API res_T
    240 sln_mixture_load
    241   (struct sln_device* dev,
    242    const struct sln_mixture_load_args* args,
    243    struct sln_mixture** mixture);
    244 
    245 SLN_API res_T
    246 sln_mixture_ref_get
    247   (struct sln_mixture* mixture);
    248 
    249 SLN_API res_T
    250 sln_mixture_ref_put
    251   (struct sln_mixture* mixture);
    252 
    253 SLN_API int
    254 sln_mixture_get_molecule_count
    255   (const struct sln_mixture* mixture);
    256 
    257 SLN_API enum shtr_molecule_id
    258 sln_mixture_get_molecule_id
    259   (const struct sln_mixture* mixture,
    260    const int index);
    261 
    262 SLN_API res_T
    263 sln_mixture_get_molecule
    264   (const struct sln_mixture* mixture,
    265    const int index,
    266    struct sln_molecule* molecule);
    267 
    268 /*******************************************************************************
    269  * Tree API
    270  ******************************************************************************/
    271 SLN_API res_T
    272 sln_tree_create
    273   (struct sln_device* dev,
    274    const struct sln_tree_create_args* args,
    275    struct sln_tree** tree);
    276 
    277 /* Read a tree serialized with the "sln_tree_write" function */
    278 SLN_API res_T
    279 sln_tree_read
    280   (struct sln_device* sln,
    281    const struct sln_tree_read_args* args,
    282    struct sln_tree** tree);
    283 
    284 SLN_API res_T
    285 sln_tree_ref_get
    286   (struct sln_tree* tree);
    287 
    288 SLN_API res_T
    289 sln_tree_ref_put
    290   (struct sln_tree* tree);
    291 
    292 SLN_API res_T
    293 sln_tree_get_desc
    294   (const struct sln_tree* tree,
    295    struct sln_tree_desc* desc);
    296 
    297 SLN_API const struct sln_node* /* NULL <=> No node */
    298 sln_tree_get_root
    299   (const struct sln_tree* tree);
    300 
    301 SLN_API int
    302 sln_node_is_leaf
    303   (const struct sln_node* node);
    304 
    305 /* The node must not be a leaf */
    306 SLN_API const struct sln_node*
    307 sln_node_get_child
    308   (const struct sln_node* node,
    309    const unsigned ichild); /* 0 or 1 */
    310 
    311 SLN_API size_t
    312 sln_node_get_lines_count
    313   (const struct sln_node* node);
    314 
    315 SLN_API res_T
    316 sln_node_get_line
    317   (const struct sln_tree* tree,
    318    const struct sln_node* node,
    319    const size_t iline,
    320    struct shtr_line* line);
    321 
    322 SLN_API res_T
    323 sln_node_get_mesh
    324   (const struct sln_tree* tree,
    325    const struct sln_node* node,
    326    struct sln_mesh* mesh);
    327 
    328 SLN_API double
    329 sln_node_eval
    330   (const struct sln_tree* tree,
    331    const struct sln_node* node,
    332    const double wavenumber); /* In cm^-1 */
    333 
    334 SLN_API res_T
    335 sln_node_get_desc
    336   (const struct sln_node* node,
    337    struct sln_node_desc* desc);
    338 
    339 SLN_API double
    340 sln_mesh_eval
    341   (const struct sln_mesh* mesh,
    342    const double wavenumber); /* In cm^-1 */
    343 
    344 SLN_API res_T
    345 sln_tree_write
    346   (const struct sln_tree* tree,
    347    const struct sln_tree_write_args* args);
    348 
    349 /*******************************************************************************
    350  * Helper functions
    351  ******************************************************************************/
    352 /* Purpose: to calculate the Faddeeva function with relative error less than
    353  * 10^(-4).
    354  *
    355  * Inputs: x and y, parameters for the Voigt function :
    356  * - x is defined as x=(nu-nu_c)/gamma_D*sqrt(ln(2)) with nu the current
    357  *   wavenumber, nu_c the wavenumber at line center, gamma_D the Doppler
    358  *   linewidth.
    359  * - y is defined as y=gamma_L/gamma_D*sqrt(ln(2)) with gamma_L the Lorentz
    360  *   linewith and gamma_D the Doppler linewidth
    361  *
    362  * Output: k, the Voigt function; it has to be multiplied by
    363  * sqrt(ln(2)/pi)*1/gamma_D so that the result may be interpretable in terms of
    364  * line profile.
    365  *
    366  * TODO check the copyright */
    367 SLN_API double
    368 sln_faddeeva
    369   (const double x,
    370    const double y);
    371 
    372 static INLINE double
    373 sln_compute_line_half_width_doppler
    374   (const double nu, /* Line center wrt pressure in cm^-1 */ /* TODO check this */
    375    const double molar_mass, /* In kg.mol^-1 */
    376    const double temperature) /* In K */
    377 {
    378   /* kb = 1.3806e-23
    379    * Na = 6.02214076e23
    380    * c = 299792458
    381    * sqrt(2*log(2)*kb*Na)/c */
    382   const double sqrt_two_ln2_kb_Na_over_c = 1.1324431552553545042e-08;
    383   const double gamma_d = nu * sqrt_two_ln2_kb_Na_over_c * sqrt(temperature/molar_mass);
    384   ASSERT(temperature >= 0 && molar_mass > 0);
    385   return gamma_d;
    386 }
    387 
    388 static INLINE double
    389 sln_compute_line_half_width_lorentz
    390   (const double gamma_air, /* Air broadening half width [cm^-1.atm^-1] */
    391    const double gamma_self, /* Air broadening half width [cm^-1.atm^-1] */
    392    const double temperature, /* [K] */
    393    const double pressure, /* [atm^-1] */
    394    const double n_air,
    395    const double concentration)
    396 {
    397   const double TREF=296; /* Ref temperature [K] for HITRAN/HITEMP database */
    398   const double Ps = pressure * concentration;
    399   const double n_self = n_air; /* In HITRAN n_air == n_self */
    400   const double gamma_l =
    401     pow(TREF/temperature, n_air)  * (pressure - Ps) * gamma_air
    402   + pow(TREF/temperature, n_self) * Ps * gamma_self;
    403   ASSERT(gamma_air > 0 && gamma_self > 0);
    404   ASSERT(pressure > 0 && concentration >= 0 && concentration <= 1);
    405 
    406   return gamma_l;
    407 }
    408 
    409 static INLINE double
    410 sln_compute_voigt_profile
    411   (const double wavenumber, /* In cm^-1 */
    412    const double nu, /* Line center in cm^-1 */
    413    const double gamma_d, /* Doppler line half width in cm^-1 */
    414    const double gamma_l) /* Lorentz line half width in cm^-1 */
    415 {
    416   /* Constants */
    417   const double sqrt_ln2 = 0.83255461115769768821; /* sqrt(log(2)) */
    418   const double sqrt_ln2_over_pi = 0.46971863934982566180; /* sqrt(log(2)/M_PI) */
    419   const double sqrt_ln2_over_gamma_d = sqrt_ln2 / gamma_d;
    420 
    421   const double x = (wavenumber - nu) * sqrt_ln2_over_gamma_d;
    422   const double y = gamma_l * sqrt_ln2_over_gamma_d;
    423   const double k = sln_faddeeva(x, y);
    424   return k*sqrt_ln2_over_pi/gamma_d;
    425 }
    426 
    427 static INLINE const char*
    428 sln_mesh_type_cstr(const enum sln_mesh_type type)
    429 {
    430   const char* cstr = NULL;
    431 
    432   switch(type) {
    433     case SLN_MESH_FIT:   cstr = "fit"; break;
    434     case SLN_MESH_UPPER: cstr = "upper"; break;
    435     default: FATAL("Unreachable code\n"); break;
    436   }
    437   return cstr;
    438 }
    439 
    440 static INLINE const char*
    441 sln_line_profile_cstr(const enum sln_line_profile profile)
    442 {
    443   const char* cstr = NULL;
    444 
    445   switch(profile) {
    446     case SLN_LINE_PROFILE_VOIGT: cstr = "voigt"; break;
    447     default: FATAL("Unreachable code\n"); break;
    448   }
    449   return cstr;
    450 }
    451 
    452 END_DECLS
    453 
    454 #endif /* SLN_H */