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


      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_MAX_ISOTOPES_COUNT 10
     47 
     48 /* Forward declaration of external data structures */
     49 struct logger;
     50 struct mem_allocator;
     51 struct shtr;
     52 struct shtr_line;
     53 struct shtr_isotope_metadata;
     54 struct shtr_line_list;
     55 
     56 enum sln_mesh_type {
     57   SLN_MESH_FIT, /* Fit the spectrum */
     58   SLN_MESH_UPPER, /* Upper limit of the spectrum */
     59   SLN_MESH_TYPES_COUNT__
     60 };
     61 
     62 enum sln_line_profile {
     63   SLN_LINE_PROFILE_VOIGT,
     64   SLN_LINE_PROFILES_COUNT__
     65 };
     66 
     67 struct sln_device_create_args {
     68   struct logger* logger; /* May be NULL <=> default logger */
     69   struct mem_allocator* allocator; /* NULL <=> use default allocator */
     70   int verbose; /* Verbosity level */
     71 };
     72 #define SLN_DEVICE_CREATE_ARGS_DEFAULT__ {NULL,NULL,0}
     73 static const struct sln_device_create_args SLN_DEVICE_CREATE_ARGS_DEFAULT =
     74   SLN_DEVICE_CREATE_ARGS_DEFAULT__;
     75 
     76 struct sln_isotope {
     77   double abundance; /* in [0, 1] */
     78   int id; /* Identifier of the isotope */
     79 };
     80 
     81 struct sln_molecule {
     82   struct sln_isotope isotopes[SLN_MAX_ISOTOPES_COUNT];
     83   double concentration;
     84   double cutoff; /* [cm^-1] */
     85   int non_default_isotope_abundances;
     86 };
     87 #define SLN_MOLECULE_NULL__ {{{0}},0,0,0}
     88 static const struct sln_molecule SLN_MOLECULE_NULL = SLN_MOLECULE_NULL__;
     89 
     90 struct sln_tree_create_args {
     91   /* Isotope metadata and lise of spectral lines */
     92   struct shtr_isotope_metadata* metadata;
     93   struct shtr_line_list* lines;
     94 
     95   enum sln_line_profile line_profile;
     96   /* Mixture description */
     97   struct sln_molecule molecules[SHTR_MAX_MOLECULE_COUNT];
     98 
     99     /* Thermo dynamic properties */
    100   double pressure; /* [atm] */
    101   double temperature; /* [K] */
    102 
    103   /* Hint on the number of vertices around the line center */
    104   size_t nvertices_hint;
    105 
    106   /* Relative error used to simplify the spectrum mesh. The larger it is, the
    107    * coarser the mesh */
    108   double mesh_decimation_err; /* > 0 */
    109   enum sln_mesh_type mesh_type; /* Type of mesh to generate */
    110 };
    111 #define SLN_TREE_CREATE_ARGS_DEFAULT__ { \
    112   NULL, /* metadata */ \
    113   NULL, /* line list */ \
    114   SLN_LINE_PROFILE_VOIGT, /* Profile */ \
    115   {SLN_MOLECULE_NULL__}, /* Molecules */ \
    116   0, /* Pressure [atm] */ \
    117   0, /* Temperature [K] */ \
    118   16, /* #vertices hint */ \
    119   0.01f, /* Mesh decimation error */ \
    120   SLN_MESH_UPPER, /* Mesh type */ \
    121 }
    122 static const struct sln_tree_create_args SLN_TREE_CREATE_ARGS_DEFAULT =
    123   SLN_TREE_CREATE_ARGS_DEFAULT__;
    124 
    125 struct sln_tree_read_args {
    126   /* Handle of the Star-HITRAN library to be used for loading isotopic metadata
    127    * and line lists */
    128   struct shtr* shtr;
    129 
    130   /* Name of the file to read or of the provided stream.
    131    * NULL <=> uses a default name for the stream to be read, which must
    132    * therefore be defined. */
    133   const char* filename; /* Name of the file to read */
    134   FILE* file; /* Stream from where data are read. NULL <=> read from file */
    135 };
    136 #define SLN_TREE_READ_ARGS_NULL__ {NULL,NULL,NULL}
    137 static const struct sln_tree_read_args SLN_TREE_READ_ARGS_NULL =
    138   SLN_TREE_READ_ARGS_NULL__;
    139 
    140 struct sln_tree_write_args {
    141   /* Name of the file in which the tree is serialized.
    142    * NULL <=> uses a default name for the stream to be written, which must
    143    * therefore be defined. */
    144   const char* filename; /* Name of the file to read */
    145 
    146   /* Stream where data is written.
    147    * NULL <=> write to the file defined by "filename" */
    148   FILE* file;
    149 };
    150 #define SLN_TREE_WRITE_ARGS_NULL__ {NULL,NULL}
    151 static const struct sln_tree_write_args SLN_TREE_WRITE_ARGS_NULL =
    152   SLN_TREE_WRITE_ARGS_NULL__;
    153 
    154 struct sln_tree_desc {
    155   size_t max_nlines_per_leaf;
    156   double mesh_decimation_err;
    157   enum sln_mesh_type mesh_type;
    158   enum sln_line_profile line_profile;
    159 
    160   size_t nvertices;
    161   size_t nnodes;
    162 };
    163 #define SLN_TREE_DESC_NULL__ { \
    164   0, 0, SLN_MESH_TYPES_COUNT__, SLN_LINE_PROFILES_COUNT__, 0, 0 \
    165 }
    166 static const struct sln_tree_desc SLN_TREE_DESC_NULL = SLN_TREE_DESC_NULL__;
    167 
    168 struct sln_vertex { /* 8 Bytes */
    169   float wavenumber; /* in cm^-1 */
    170   float ka;
    171 };
    172 #define SLN_VERTEX_NULL__ {0,0}
    173 static const struct sln_vertex SLN_VERTEX_NULL = SLN_VERTEX_NULL__;
    174 
    175 struct sln_mesh {
    176   const struct sln_vertex* vertices;
    177   size_t nvertices;
    178 };
    179 #define SLN_MESH_NULL__ {NULL,0}
    180 static const struct sln_mesh SLN_MESH_NULL = SLN_MESH_NULL__;
    181 
    182 struct sln_mixture_load_args {
    183   const char* filename; /* Name of the file to load or of the provided stream */
    184   FILE* file; /* Stream from where data are loaded. NULL <=> load from file */
    185 
    186   /* Metadata from which the mix is defined */
    187   struct shtr_isotope_metadata* molparam;
    188 };
    189 #define SLN_MIXTURE_LOAD_ARGS_NULL__ {NULL,NULL,NULL}
    190 static const struct sln_mixture_load_args SLN_MIXTURE_LOAD_ARGS_NULL =
    191   SLN_MIXTURE_LOAD_ARGS_NULL__;
    192 
    193 /* Forward declarations of opaque data structures */
    194 struct sln_device;
    195 struct sln_mixture;
    196 struct sln_node;
    197 struct sln_tree;
    198 
    199 BEGIN_DECLS
    200 
    201 /*******************************************************************************
    202  * Device API
    203  ******************************************************************************/
    204 SLN_API res_T
    205 sln_device_create
    206   (const struct sln_device_create_args* args,
    207    struct sln_device** sln);
    208 
    209 SLN_API res_T
    210 sln_device_ref_get
    211   (struct sln_device* sln);
    212 
    213 SLN_API res_T
    214 sln_device_ref_put
    215   (struct sln_device* sln);
    216 
    217 
    218 /*******************************************************************************
    219  * Mixture API
    220  ******************************************************************************/
    221 SLN_API res_T
    222 sln_mixture_load
    223   (struct sln_device* dev,
    224    const struct sln_mixture_load_args* args,
    225    struct sln_mixture** mixture);
    226 
    227 SLN_API res_T
    228 sln_mixture_ref_get
    229   (struct sln_mixture* mixture);
    230 
    231 SLN_API res_T
    232 sln_mixture_ref_put
    233   (struct sln_mixture* mixture);
    234 
    235 SLN_API int
    236 sln_mixture_get_molecule_count
    237   (const struct sln_mixture* mixture);
    238 
    239 SLN_API enum shtr_molecule_id
    240 sln_mixture_get_molecule_id
    241   (const struct sln_mixture* mixture,
    242    const int index);
    243 
    244 SLN_API res_T
    245 sln_mixture_get_molecule
    246   (const struct sln_mixture* mixture,
    247    const int index,
    248    struct sln_molecule* molecule);
    249 
    250 /*******************************************************************************
    251  * Tree API
    252  ******************************************************************************/
    253 SLN_API res_T
    254 sln_tree_create
    255   (struct sln_device* dev,
    256    const struct sln_tree_create_args* args,
    257    struct sln_tree** tree);
    258 
    259 /* Read a tree serialized with the "sln_tree_write" function */
    260 SLN_API res_T
    261 sln_tree_read
    262   (struct sln_device* sln,
    263    const struct sln_tree_read_args* args,
    264    struct sln_tree** tree);
    265 
    266 SLN_API res_T
    267 sln_tree_ref_get
    268   (struct sln_tree* tree);
    269 
    270 SLN_API res_T
    271 sln_tree_ref_put
    272   (struct sln_tree* tree);
    273 
    274 SLN_API res_T
    275 sln_tree_get_desc
    276   (const struct sln_tree* tree,
    277    struct sln_tree_desc* desc);
    278 
    279 SLN_API const struct sln_node*
    280 sln_tree_get_root
    281   (const struct sln_tree* tree);
    282 
    283 SLN_API int
    284 sln_node_is_leaf
    285   (const struct sln_node* node);
    286 
    287 /* Return NULL if the node is a leaf */
    288 SLN_API const struct sln_node*
    289 sln_node_get_child
    290   (const struct sln_node* node,
    291    const unsigned ichild); /* 0 or 1 */
    292 
    293 SLN_API size_t
    294 sln_node_get_lines_count
    295   (const struct sln_node* node);
    296 
    297 SLN_API res_T
    298 sln_node_get_line
    299   (const struct sln_tree* tree,
    300    const struct sln_node* node,
    301    const size_t iline,
    302    struct shtr_line* line);
    303 
    304 SLN_API res_T
    305 sln_node_get_mesh
    306   (const struct sln_tree* tree,
    307    const struct sln_node* node,
    308    struct sln_mesh* mesh);
    309 
    310 SLN_API double
    311 sln_node_eval
    312   (const struct sln_tree* tree,
    313    const struct sln_node* node,
    314    const double wavenumber); /* In cm^-1 */
    315 
    316 SLN_API double
    317 sln_mesh_eval
    318   (const struct sln_mesh* mesh,
    319    const double wavenumber); /* In cm^-1 */
    320 
    321 SLN_API res_T
    322 sln_tree_write
    323   (const struct sln_tree* tree,
    324    const struct sln_tree_write_args* args);
    325 
    326 /*******************************************************************************
    327  * Helper functions
    328  ******************************************************************************/
    329 /* Purpose: to calculate the Faddeeva function with relative error less than
    330  * 10^(-4).
    331  *
    332  * Inputs: x and y, parameters for the Voigt function :
    333  * - x is defined as x=(nu-nu_c)/gamma_D*sqrt(ln(2)) with nu the current
    334  *   wavenumber, nu_c the wavenumber at line center, gamma_D the Doppler
    335  *   linewidth.
    336  * - y is defined as y=gamma_L/gamma_D*sqrt(ln(2)) with gamma_L the Lorentz
    337  *   linewith and gamma_D the Doppler linewidth
    338  *
    339  * Output: k, the Voigt function; it has to be multiplied by
    340  * sqrt(ln(2)/pi)*1/gamma_D so that the result may be interpretable in terms of
    341  * line profile.
    342  *
    343  * TODO check the copyright */
    344 SLN_API double
    345 sln_faddeeva
    346   (const double x,
    347    const double y);
    348 
    349 static INLINE double
    350 sln_compute_line_half_width_doppler
    351   (const double nu, /* Line center wrt pressure in cm^-1 */ /* TODO check this */
    352    const double molar_mass, /* In kg.mol^-1 */
    353    const double temperature) /* In K */
    354 {
    355   /* kb = 1.3806e-23
    356    * Na = 6.02214076e23
    357    * c = 299792458
    358    * sqrt(2*log(2)*kb*Na)/c */
    359   const double sqrt_two_ln2_kb_Na_over_c = 1.1324431552553545042e-08;
    360   const double gamma_d = nu * sqrt_two_ln2_kb_Na_over_c * sqrt(temperature/molar_mass);
    361   ASSERT(temperature >= 0 && molar_mass > 0);
    362   return gamma_d;
    363 }
    364 
    365 static INLINE double
    366 sln_compute_line_half_width_lorentz
    367   (const double gamma_air, /* Air broadening half width [cm^-1.atm^-1] */
    368    const double gamma_self, /* Air broadening half width [cm^-1.atm^-1] */
    369    const double pressure, /* [atm^-1] */
    370    const double concentration)
    371 {
    372   const double Ps = pressure * concentration;
    373   const double gamma_l = (pressure - Ps) * gamma_air + Ps * gamma_self;
    374   ASSERT(gamma_air > 0 && gamma_self > 0);
    375   ASSERT(pressure > 0 && concentration >= 0 && concentration <= 1);
    376   return gamma_l;
    377 }
    378 
    379 static INLINE double
    380 sln_compute_voigt_profile
    381   (const double wavenumber, /* In cm^-1 */
    382    const double nu, /* Line center in cm^-1 */
    383    const double gamma_d, /* Doppler line half width in cm^-1 */
    384    const double gamma_l) /* Lorentz line half width in cm^-1 */
    385 {
    386   /* Constants */
    387   const double sqrt_ln2 = 0.83255461115769768821; /* sqrt(log(2)) */
    388   const double sqrt_ln2_over_pi = 0.46971863934982566180; /* sqrt(log(2)/M_PI) */
    389   const double sqrt_ln2_over_gamma_d = sqrt_ln2 / gamma_d;
    390 
    391   const double x = (wavenumber - nu) * sqrt_ln2_over_gamma_d;
    392   const double y = gamma_l * sqrt_ln2_over_gamma_d;
    393   const double k = sln_faddeeva(x, y);
    394   return k*sqrt_ln2_over_pi/gamma_d;
    395 }
    396 
    397 END_DECLS
    398 
    399 #endif /* SLN_H */