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 (15913B)


      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 #define SLN_TREE_DEPTH_MAX 64 /* Maximum depth of a tree */
     47 #define SLN_TREE_ARITY_MAX 256  /* Maximum arity of a tree */
     48 #define SLN_LEAF_NLINES_MAX 16384 /* Maximum number of lines per leaf */
     49 
     50 /* Forward declaration of external data structures */
     51 struct logger;
     52 struct mem_allocator;
     53 struct shtr;
     54 struct shtr_line;
     55 struct shtr_isotope_metadata;
     56 struct shtr_line_list;
     57 
     58 enum sln_mesh_type {
     59   SLN_MESH_FIT, /* Fit the spectrum */
     60   SLN_MESH_UPPER, /* Upper limit of the spectrum */
     61   SLN_MESH_TYPES_COUNT__
     62 };
     63 
     64 enum sln_line_profile {
     65   SLN_LINE_PROFILE_VOIGT,
     66   SLN_LINE_PROFILES_COUNT__
     67 };
     68 
     69 struct sln_device_create_args {
     70   struct logger* logger; /* May be NULL <=> default logger */
     71   struct mem_allocator* allocator; /* NULL <=> use default allocator */
     72   int verbose; /* Verbosity level */
     73 };
     74 #define SLN_DEVICE_CREATE_ARGS_DEFAULT__ {NULL,NULL,0}
     75 static const struct sln_device_create_args SLN_DEVICE_CREATE_ARGS_DEFAULT =
     76   SLN_DEVICE_CREATE_ARGS_DEFAULT__;
     77 
     78 struct sln_isotope {
     79   double abundance; /* in [0, 1] */
     80   int id; /* Identifier of the isotope */
     81 };
     82 
     83 struct sln_molecule {
     84   struct sln_isotope isotopes[SHTR_MAX_ISOTOPE_COUNT];
     85   double concentration;
     86   double cutoff; /* [cm^-1] */
     87   int non_default_isotope_abundances;
     88 };
     89 #define SLN_MOLECULE_NULL__ {{{0}},0,0,0}
     90 static const struct sln_molecule SLN_MOLECULE_NULL = SLN_MOLECULE_NULL__;
     91 
     92 struct sln_tree_create_args {
     93   /* Isotope metadata and list of spectral lines */
     94   struct shtr_isotope_metadata* metadata;
     95   struct shtr_line_list* lines;
     96 
     97   enum sln_line_profile line_profile;
     98   /* Mixture description */
     99   struct sln_molecule molecules[SHTR_MAX_MOLECULE_COUNT];
    100 
    101     /* Thermo dynamic properties */
    102   double pressure; /* [atm] */
    103   double temperature; /* [K] */
    104 
    105   /* Hint on the number of vertices around the line center */
    106   size_t nvertices_hint;
    107 
    108   /* Relative error used to simplify the spectrum mesh. The larger it is, the
    109    * coarser the mesh */
    110   double mesh_decimation_err; /* > 0 */
    111   enum sln_mesh_type mesh_type; /* Type of mesh to generate */
    112 
    113   /* Maximum number of children per node */
    114   unsigned arity;
    115 
    116   /* Maximum number of lines per leaf */
    117   unsigned leaf_nlines;
    118 
    119   /* When this option is enabled, the polylines of internal nodes are
    120    * constructed by merging their children's polylines in pairs (and then
    121    * simplifying the result), and repeating the process until only a single
    122    * polyline remains, which becomes the internal node's polyline.
    123    *
    124    * If this option is disabled, all child polylines are merged in a single step
    125    * before being simplified.
    126    *
    127    * Enabling this option only makes sense for trees with an arity greater than
    128    * two. For a binary tree, both methods should produce exactly the same tree,
    129    * down to the bit */
    130   int collapse_polylines;
    131 
    132   /* Advice on the number of threads to use */
    133   unsigned nthreads_hint;
    134 };
    135 #define SLN_TREE_CREATE_ARGS_DEFAULT__ { \
    136   NULL, /* metadata */ \
    137   NULL, /* line list */ \
    138   SLN_LINE_PROFILE_VOIGT, /* Profile */ \
    139   {SLN_MOLECULE_NULL__}, /* Molecules */ \
    140   0, /* Pressure [atm] */ \
    141   0, /* Temperature [K] */ \
    142   16, /* #vertices hint */ \
    143   0.01f, /* Mesh decimation error */ \
    144   SLN_MESH_UPPER, /* Mesh type */ \
    145   2, /* Arity */ \
    146   1, /* Number of lines per leaf */ \
    147   0, /* Collapse polylines */ \
    148   (unsigned)(-1), /* #threads hint */ \
    149 }
    150 static const struct sln_tree_create_args SLN_TREE_CREATE_ARGS_DEFAULT =
    151   SLN_TREE_CREATE_ARGS_DEFAULT__;
    152 
    153 struct sln_tree_read_args {
    154   /* Metadata and list of spectral lines from which the tree was constructed */
    155   struct shtr_isotope_metadata* metadata;
    156   struct shtr_line_list* lines;
    157 
    158   /* Name of the file to read or of the provided stream.
    159    * NULL <=> uses a default name for the stream to be read, which must
    160    * therefore be defined. */
    161   const char* filename; /* Name of the file to read */
    162   FILE* file; /* Stream from where data are read. NULL <=> read from file */
    163 
    164   /* Verify that the digital signature of the input lines matches the one stored
    165    * in the tree. In other words, ensure that this list of lines is indeed the
    166    * one used to construct the tree. An error is returned if the signatures do
    167    * not match.
    168    *
    169    * Although it is always advisable to verify that the data matches what is
    170    * expected, calculating the signatures of the lines can be time-consuming.
    171    * Therefore, a user who is _certain_ that the data matches can disable this
    172    * verification */
    173   int disable_line_hash_check;
    174 };
    175 #define SLN_TREE_READ_ARGS_NULL__ {NULL,NULL,NULL,NULL,0}
    176 static const struct sln_tree_read_args SLN_TREE_READ_ARGS_NULL =
    177   SLN_TREE_READ_ARGS_NULL__;
    178 
    179 struct sln_tree_write_args {
    180   /* Name of the file in which the tree is serialized.
    181    * NULL <=> uses a default name for the stream to be written, which must
    182    * therefore be defined. */
    183   const char* filename; /* Name of the file to read */
    184 
    185   /* Stream where data is written.
    186    * NULL <=> write to the file defined by "filename" */
    187   FILE* file;
    188 };
    189 #define SLN_TREE_WRITE_ARGS_NULL__ {NULL,NULL}
    190 static const struct sln_tree_write_args SLN_TREE_WRITE_ARGS_NULL =
    191   SLN_TREE_WRITE_ARGS_NULL__;
    192 
    193 struct sln_tree_desc {
    194   double mesh_decimation_err;
    195   enum sln_mesh_type mesh_type;
    196   enum sln_line_profile line_profile;
    197 
    198   double pressure; /* [atm] */
    199   double temperature; /* [K] */
    200 
    201   unsigned depth; /* #edges from the root to the deepest leaf */
    202   size_t nlines;
    203   size_t nvertices;
    204   size_t nnodes;
    205   unsigned arity;
    206   unsigned leaf_nlines;
    207 };
    208 #define SLN_TREE_DESC_NULL__ { \
    209   0,SLN_MESH_TYPES_COUNT__,SLN_LINE_PROFILES_COUNT__,0,0,0,0,0,0,0,0 \
    210 }
    211 static const struct sln_tree_desc SLN_TREE_DESC_NULL = SLN_TREE_DESC_NULL__;
    212 
    213 struct sln_node_desc {
    214   /* Range of lines belonging to the node. The endpoints are included */
    215   size_t ilines[2];
    216   size_t nvertices;
    217   unsigned nchildren;
    218 };
    219 #define SLN_NODE_DESC_NULL__ {{0,0},0,0}
    220 static const struct sln_node_desc SLN_NODE_DESC_NULL = SLN_NODE_DESC_NULL__;
    221 
    222 struct sln_vertex { /* 8 Bytes */
    223   float wavenumber; /* in cm^-1 */
    224   float ka;
    225 };
    226 #define SLN_VERTEX_NULL__ {0,0}
    227 static const struct sln_vertex SLN_VERTEX_NULL = SLN_VERTEX_NULL__;
    228 
    229 struct sln_mesh {
    230   const struct sln_vertex* vertices;
    231   size_t nvertices;
    232 };
    233 #define SLN_MESH_NULL__ {NULL,0}
    234 static const struct sln_mesh SLN_MESH_NULL = SLN_MESH_NULL__;
    235 
    236 struct sln_mixture_load_args {
    237   const char* filename; /* Name of the file to load or of the provided stream */
    238   FILE* file; /* Stream from where data are loaded. NULL <=> load from file */
    239 
    240   /* Metadata from which the mix is defined */
    241   struct shtr_isotope_metadata* molparam;
    242 };
    243 #define SLN_MIXTURE_LOAD_ARGS_NULL__ {NULL,NULL,NULL}
    244 static const struct sln_mixture_load_args SLN_MIXTURE_LOAD_ARGS_NULL =
    245   SLN_MIXTURE_LOAD_ARGS_NULL__;
    246 
    247 struct sln_line {
    248   double wavenumber; /* Line center wrt pressure in cm^-1 */
    249   double profile_factor; /* m^-1.cm^-1 (1e2*density*intensity) */
    250   double gamma_d; /* Doppler half width */
    251   double gamma_l; /* Lorentz half width */
    252   enum shtr_molecule_id molecule_id;
    253 };
    254 #define SLN_LINE_NULL__ {0,0,0,0,SHTR_MOLECULE_ID_NULL}
    255 static const struct sln_line SLN_LINE_NULL = SLN_LINE_NULL__;
    256 
    257 /* Forward declarations of opaque data structures */
    258 struct sln_device;
    259 struct sln_mixture;
    260 struct sln_node;
    261 struct sln_tree;
    262 
    263 BEGIN_DECLS
    264 
    265 /*******************************************************************************
    266  * Device API
    267  ******************************************************************************/
    268 SLN_API res_T
    269 sln_device_create
    270   (const struct sln_device_create_args* args,
    271    struct sln_device** sln);
    272 
    273 SLN_API res_T
    274 sln_device_ref_get
    275   (struct sln_device* sln);
    276 
    277 SLN_API res_T
    278 sln_device_ref_put
    279   (struct sln_device* sln);
    280 
    281 
    282 /*******************************************************************************
    283  * Mixture API
    284  ******************************************************************************/
    285 SLN_API res_T
    286 sln_mixture_load
    287   (struct sln_device* dev,
    288    const struct sln_mixture_load_args* args,
    289    struct sln_mixture** mixture);
    290 
    291 SLN_API res_T
    292 sln_mixture_ref_get
    293   (struct sln_mixture* mixture);
    294 
    295 SLN_API res_T
    296 sln_mixture_ref_put
    297   (struct sln_mixture* mixture);
    298 
    299 SLN_API int
    300 sln_mixture_get_molecule_count
    301   (const struct sln_mixture* mixture);
    302 
    303 SLN_API enum shtr_molecule_id
    304 sln_mixture_get_molecule_id
    305   (const struct sln_mixture* mixture,
    306    const int index);
    307 
    308 SLN_API res_T
    309 sln_mixture_get_molecule
    310   (const struct sln_mixture* mixture,
    311    const int index,
    312    struct sln_molecule* molecule);
    313 
    314 /*******************************************************************************
    315  * Tree API
    316  ******************************************************************************/
    317 SLN_API res_T
    318 sln_tree_create
    319   (struct sln_device* dev,
    320    const struct sln_tree_create_args* args,
    321    struct sln_tree** tree);
    322 
    323 /* Read a tree serialized with the "sln_tree_write" function */
    324 SLN_API res_T
    325 sln_tree_read
    326   (struct sln_device* sln,
    327    const struct sln_tree_read_args* args,
    328    struct sln_tree** tree);
    329 
    330 SLN_API res_T
    331 sln_tree_ref_get
    332   (struct sln_tree* tree);
    333 
    334 SLN_API res_T
    335 sln_tree_ref_put
    336   (struct sln_tree* tree);
    337 
    338 SLN_API res_T
    339 sln_tree_get_desc
    340   (const struct sln_tree* tree,
    341    struct sln_tree_desc* desc);
    342 
    343 SLN_API const struct sln_node* /* NULL <=> No node */
    344 sln_tree_get_root
    345   (const struct sln_tree* tree);
    346 
    347 SLN_API res_T
    348 sln_tree_get_line
    349   (const struct sln_tree* tree,
    350    const size_t iline,
    351    struct sln_line* line);
    352 
    353 SLN_API res_T
    354 sln_tree_write
    355   (const struct sln_tree* tree,
    356    const struct sln_tree_write_args* args);
    357 
    358 /*******************************************************************************
    359  * Node API
    360  ******************************************************************************/
    361 SLN_API int
    362 sln_node_is_leaf
    363   (const struct sln_node* node);
    364 
    365 SLN_API unsigned
    366 sln_node_get_child_count
    367   (const struct sln_tree* tree,
    368    const struct sln_node* node);
    369 
    370 /* The node must not be a leaf */
    371 SLN_API const struct sln_node*
    372 sln_node_get_child
    373   (const struct sln_tree* tree,
    374    const struct sln_node* node,
    375    const unsigned ichild); /* 0 or #children */
    376 
    377 SLN_API double
    378 sln_node_eval
    379   (const struct sln_tree* tree,
    380    const struct sln_node* node,
    381    const double wavenumber); /* In cm^-1 */
    382 
    383 SLN_API res_T
    384 sln_node_get_desc
    385   (const struct sln_tree* tree,
    386    const struct sln_node* node,
    387    struct sln_node_desc* desc);
    388 
    389 SLN_API res_T
    390 sln_node_get_mesh
    391   (const struct sln_tree* tree,
    392    const struct sln_node* node,
    393    struct sln_mesh* mesh);
    394 
    395 /*******************************************************************************
    396  * Miscellaneous
    397  ******************************************************************************/
    398 SLN_API double
    399 sln_line_eval
    400   (const struct sln_tree* tree,
    401    const struct sln_line* line,
    402    const double wavenumber); /* In cm^-1 */
    403 
    404 SLN_API double
    405 sln_mesh_eval
    406   (const struct sln_mesh* mesh,
    407    const double wavenumber); /* In cm^-1 */
    408 
    409 /*******************************************************************************
    410  * Helper functions
    411  ******************************************************************************/
    412 /* Purpose: to calculate the Faddeeva function with relative error less than
    413  * 10^(-4).
    414  *
    415  * Inputs: x and y, parameters for the Voigt function :
    416  * - x is defined as x=(nu-nu_c)/gamma_D*sqrt(ln(2)) with nu the current
    417  *   wavenumber, nu_c the wavenumber at line center, gamma_D the Doppler
    418  *   linewidth.
    419  * - y is defined as y=gamma_L/gamma_D*sqrt(ln(2)) with gamma_L the Lorentz
    420  *   linewith and gamma_D the Doppler linewidth
    421  *
    422  * Output: k, the Voigt function; it has to be multiplied by
    423  * sqrt(ln(2)/pi)*1/gamma_D so that the result may be interpretable in terms of
    424  * line profile.
    425  *
    426  * TODO check the copyright */
    427 SLN_API double
    428 sln_faddeeva
    429   (const double x,
    430    const double y);
    431 
    432 static INLINE double
    433 sln_compute_line_half_width_doppler
    434   (const double nu, /* Line center wrt pressure in cm^-1 */ /* TODO check this */
    435    const double molar_mass, /* In kg.mol^-1 */
    436    const double temperature) /* In K */
    437 {
    438   /* kb = 1.3806e-23
    439    * Na = 6.02214076e23
    440    * c = 299792458
    441    * sqrt(2*log(2)*kb*Na)/c */
    442   const double sqrt_two_ln2_kb_Na_over_c = 1.1324431552553545042e-08;
    443   const double gamma_d = nu * sqrt_two_ln2_kb_Na_over_c * sqrt(temperature/molar_mass);
    444   ASSERT(temperature >= 0 && molar_mass > 0);
    445   return gamma_d;
    446 }
    447 
    448 static INLINE double
    449 sln_compute_line_half_width_lorentz
    450   (const double gamma_air, /* Air broadening half width [cm^-1.atm^-1] */
    451    const double gamma_self, /* Air broadening half width [cm^-1.atm^-1] */
    452    const double temperature, /* [K] */
    453    const double pressure, /* [atm^-1] */
    454    const double n_air,
    455    const double concentration)
    456 {
    457   const double TREF=296; /* Ref temperature [K] for HITRAN/HITEMP database */
    458   const double Ps = pressure * concentration;
    459   const double n_self = n_air; /* In HITRAN n_air == n_self */
    460   const double gamma_l =
    461     pow(TREF/temperature, n_air)  * (pressure - Ps) * gamma_air
    462   + pow(TREF/temperature, n_self) * Ps * gamma_self;
    463   ASSERT(gamma_air > 0 && gamma_self > 0);
    464   ASSERT(pressure > 0 && concentration >= 0 && concentration <= 1);
    465 
    466   return gamma_l;
    467 }
    468 
    469 static INLINE double
    470 sln_compute_voigt_profile
    471   (const double wavenumber, /* In cm^-1 */
    472    const double nu, /* Line center in cm^-1 */
    473    const double gamma_d, /* Doppler line half width in cm^-1 */
    474    const double gamma_l) /* Lorentz line half width in cm^-1 */
    475 {
    476   /* Constants */
    477   const double sqrt_ln2 = 0.83255461115769768821; /* sqrt(log(2)) */
    478   const double sqrt_ln2_over_pi = 0.46971863934982566180; /* sqrt(log(2)/M_PI) */
    479   const double sqrt_ln2_over_gamma_d = sqrt_ln2 / gamma_d;
    480 
    481   const double x = (wavenumber - nu) * sqrt_ln2_over_gamma_d;
    482   const double y = gamma_l * sqrt_ln2_over_gamma_d;
    483   const double k = sln_faddeeva(x, y);
    484   return k*sqrt_ln2_over_pi/gamma_d;
    485 }
    486 
    487 static INLINE const char*
    488 sln_mesh_type_cstr(const enum sln_mesh_type type)
    489 {
    490   const char* cstr = NULL;
    491 
    492   switch(type) {
    493     case SLN_MESH_FIT:   cstr = "fit"; break;
    494     case SLN_MESH_UPPER: cstr = "upper"; break;
    495     default: FATAL("Unreachable code\n"); break;
    496   }
    497   return cstr;
    498 }
    499 
    500 static INLINE const char*
    501 sln_line_profile_cstr(const enum sln_line_profile profile)
    502 {
    503   const char* cstr = NULL;
    504 
    505   switch(profile) {
    506     case SLN_LINE_PROFILE_VOIGT: cstr = "voigt"; break;
    507     default: FATAL("Unreachable code\n"); break;
    508   }
    509   return cstr;
    510 }
    511 
    512 END_DECLS
    513 
    514 #endif /* SLN_H */