sln.h (15364B)
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 #define SLN_TREE_READ_ARGS_NULL__ {NULL,NULL,NULL,NULL} 165 static const struct sln_tree_read_args SLN_TREE_READ_ARGS_NULL = 166 SLN_TREE_READ_ARGS_NULL__; 167 168 struct sln_tree_write_args { 169 /* Name of the file in which the tree is serialized. 170 * NULL <=> uses a default name for the stream to be written, which must 171 * therefore be defined. */ 172 const char* filename; /* Name of the file to read */ 173 174 /* Stream where data is written. 175 * NULL <=> write to the file defined by "filename" */ 176 FILE* file; 177 }; 178 #define SLN_TREE_WRITE_ARGS_NULL__ {NULL,NULL} 179 static const struct sln_tree_write_args SLN_TREE_WRITE_ARGS_NULL = 180 SLN_TREE_WRITE_ARGS_NULL__; 181 182 struct sln_tree_desc { 183 double mesh_decimation_err; 184 enum sln_mesh_type mesh_type; 185 enum sln_line_profile line_profile; 186 187 double pressure; /* [atm] */ 188 double temperature; /* [K] */ 189 190 unsigned depth; /* #edges from the root to the deepest leaf */ 191 size_t nlines; 192 size_t nvertices; 193 size_t nnodes; 194 unsigned arity; 195 unsigned leaf_nlines; 196 }; 197 #define SLN_TREE_DESC_NULL__ { \ 198 0,SLN_MESH_TYPES_COUNT__,SLN_LINE_PROFILES_COUNT__,0,0,0,0,0,0,0,0 \ 199 } 200 static const struct sln_tree_desc SLN_TREE_DESC_NULL = SLN_TREE_DESC_NULL__; 201 202 struct sln_node_desc { 203 /* Range of lines belonging to the node. The endpoints are included */ 204 size_t ilines[2]; 205 size_t nvertices; 206 unsigned nchildren; 207 }; 208 #define SLN_NODE_DESC_NULL__ {{0,0},0,0} 209 static const struct sln_node_desc SLN_NODE_DESC_NULL = SLN_NODE_DESC_NULL__; 210 211 struct sln_vertex { /* 8 Bytes */ 212 float wavenumber; /* in cm^-1 */ 213 float ka; 214 }; 215 #define SLN_VERTEX_NULL__ {0,0} 216 static const struct sln_vertex SLN_VERTEX_NULL = SLN_VERTEX_NULL__; 217 218 struct sln_mesh { 219 const struct sln_vertex* vertices; 220 size_t nvertices; 221 }; 222 #define SLN_MESH_NULL__ {NULL,0} 223 static const struct sln_mesh SLN_MESH_NULL = SLN_MESH_NULL__; 224 225 struct sln_mixture_load_args { 226 const char* filename; /* Name of the file to load or of the provided stream */ 227 FILE* file; /* Stream from where data are loaded. NULL <=> load from file */ 228 229 /* Metadata from which the mix is defined */ 230 struct shtr_isotope_metadata* molparam; 231 }; 232 #define SLN_MIXTURE_LOAD_ARGS_NULL__ {NULL,NULL,NULL} 233 static const struct sln_mixture_load_args SLN_MIXTURE_LOAD_ARGS_NULL = 234 SLN_MIXTURE_LOAD_ARGS_NULL__; 235 236 struct sln_line { 237 double wavenumber; /* Line center wrt pressure in cm^-1 */ 238 double profile_factor; /* m^-1.cm^-1 (1e2*density*intensity) */ 239 double gamma_d; /* Doppler half width */ 240 double gamma_l; /* Lorentz half width */ 241 enum shtr_molecule_id molecule_id; 242 }; 243 #define SLN_LINE_NULL__ {0,0,0,0,SHTR_MOLECULE_ID_NULL} 244 static const struct sln_line SLN_LINE_NULL = SLN_LINE_NULL__; 245 246 /* Forward declarations of opaque data structures */ 247 struct sln_device; 248 struct sln_mixture; 249 struct sln_node; 250 struct sln_tree; 251 252 BEGIN_DECLS 253 254 /******************************************************************************* 255 * Device API 256 ******************************************************************************/ 257 SLN_API res_T 258 sln_device_create 259 (const struct sln_device_create_args* args, 260 struct sln_device** sln); 261 262 SLN_API res_T 263 sln_device_ref_get 264 (struct sln_device* sln); 265 266 SLN_API res_T 267 sln_device_ref_put 268 (struct sln_device* sln); 269 270 271 /******************************************************************************* 272 * Mixture API 273 ******************************************************************************/ 274 SLN_API res_T 275 sln_mixture_load 276 (struct sln_device* dev, 277 const struct sln_mixture_load_args* args, 278 struct sln_mixture** mixture); 279 280 SLN_API res_T 281 sln_mixture_ref_get 282 (struct sln_mixture* mixture); 283 284 SLN_API res_T 285 sln_mixture_ref_put 286 (struct sln_mixture* mixture); 287 288 SLN_API int 289 sln_mixture_get_molecule_count 290 (const struct sln_mixture* mixture); 291 292 SLN_API enum shtr_molecule_id 293 sln_mixture_get_molecule_id 294 (const struct sln_mixture* mixture, 295 const int index); 296 297 SLN_API res_T 298 sln_mixture_get_molecule 299 (const struct sln_mixture* mixture, 300 const int index, 301 struct sln_molecule* molecule); 302 303 /******************************************************************************* 304 * Tree API 305 ******************************************************************************/ 306 SLN_API res_T 307 sln_tree_create 308 (struct sln_device* dev, 309 const struct sln_tree_create_args* args, 310 struct sln_tree** tree); 311 312 /* Read a tree serialized with the "sln_tree_write" function */ 313 SLN_API res_T 314 sln_tree_read 315 (struct sln_device* sln, 316 const struct sln_tree_read_args* args, 317 struct sln_tree** tree); 318 319 SLN_API res_T 320 sln_tree_ref_get 321 (struct sln_tree* tree); 322 323 SLN_API res_T 324 sln_tree_ref_put 325 (struct sln_tree* tree); 326 327 SLN_API res_T 328 sln_tree_get_desc 329 (const struct sln_tree* tree, 330 struct sln_tree_desc* desc); 331 332 SLN_API const struct sln_node* /* NULL <=> No node */ 333 sln_tree_get_root 334 (const struct sln_tree* tree); 335 336 SLN_API res_T 337 sln_tree_get_line 338 (const struct sln_tree* tree, 339 const size_t iline, 340 struct sln_line* line); 341 342 SLN_API res_T 343 sln_tree_write 344 (const struct sln_tree* tree, 345 const struct sln_tree_write_args* args); 346 347 /******************************************************************************* 348 * Node API 349 ******************************************************************************/ 350 SLN_API int 351 sln_node_is_leaf 352 (const struct sln_node* node); 353 354 SLN_API unsigned 355 sln_node_get_child_count 356 (const struct sln_tree* tree, 357 const struct sln_node* node); 358 359 /* The node must not be a leaf */ 360 SLN_API const struct sln_node* 361 sln_node_get_child 362 (const struct sln_tree* tree, 363 const struct sln_node* node, 364 const unsigned ichild); /* 0 or #children */ 365 366 SLN_API double 367 sln_node_eval 368 (const struct sln_tree* tree, 369 const struct sln_node* node, 370 const double wavenumber); /* In cm^-1 */ 371 372 SLN_API res_T 373 sln_node_get_desc 374 (const struct sln_tree* tree, 375 const struct sln_node* node, 376 struct sln_node_desc* desc); 377 378 SLN_API res_T 379 sln_node_get_mesh 380 (const struct sln_tree* tree, 381 const struct sln_node* node, 382 struct sln_mesh* mesh); 383 384 /******************************************************************************* 385 * Miscellaneous 386 ******************************************************************************/ 387 SLN_API double 388 sln_line_eval 389 (const struct sln_tree* tree, 390 const struct sln_line* line, 391 const double wavenumber); /* In cm^-1 */ 392 393 SLN_API double 394 sln_mesh_eval 395 (const struct sln_mesh* mesh, 396 const double wavenumber); /* In cm^-1 */ 397 398 /******************************************************************************* 399 * Helper functions 400 ******************************************************************************/ 401 /* Purpose: to calculate the Faddeeva function with relative error less than 402 * 10^(-4). 403 * 404 * Inputs: x and y, parameters for the Voigt function : 405 * - x is defined as x=(nu-nu_c)/gamma_D*sqrt(ln(2)) with nu the current 406 * wavenumber, nu_c the wavenumber at line center, gamma_D the Doppler 407 * linewidth. 408 * - y is defined as y=gamma_L/gamma_D*sqrt(ln(2)) with gamma_L the Lorentz 409 * linewith and gamma_D the Doppler linewidth 410 * 411 * Output: k, the Voigt function; it has to be multiplied by 412 * sqrt(ln(2)/pi)*1/gamma_D so that the result may be interpretable in terms of 413 * line profile. 414 * 415 * TODO check the copyright */ 416 SLN_API double 417 sln_faddeeva 418 (const double x, 419 const double y); 420 421 static INLINE double 422 sln_compute_line_half_width_doppler 423 (const double nu, /* Line center wrt pressure in cm^-1 */ /* TODO check this */ 424 const double molar_mass, /* In kg.mol^-1 */ 425 const double temperature) /* In K */ 426 { 427 /* kb = 1.3806e-23 428 * Na = 6.02214076e23 429 * c = 299792458 430 * sqrt(2*log(2)*kb*Na)/c */ 431 const double sqrt_two_ln2_kb_Na_over_c = 1.1324431552553545042e-08; 432 const double gamma_d = nu * sqrt_two_ln2_kb_Na_over_c * sqrt(temperature/molar_mass); 433 ASSERT(temperature >= 0 && molar_mass > 0); 434 return gamma_d; 435 } 436 437 static INLINE double 438 sln_compute_line_half_width_lorentz 439 (const double gamma_air, /* Air broadening half width [cm^-1.atm^-1] */ 440 const double gamma_self, /* Air broadening half width [cm^-1.atm^-1] */ 441 const double temperature, /* [K] */ 442 const double pressure, /* [atm^-1] */ 443 const double n_air, 444 const double concentration) 445 { 446 const double TREF=296; /* Ref temperature [K] for HITRAN/HITEMP database */ 447 const double Ps = pressure * concentration; 448 const double n_self = n_air; /* In HITRAN n_air == n_self */ 449 const double gamma_l = 450 pow(TREF/temperature, n_air) * (pressure - Ps) * gamma_air 451 + pow(TREF/temperature, n_self) * Ps * gamma_self; 452 ASSERT(gamma_air > 0 && gamma_self > 0); 453 ASSERT(pressure > 0 && concentration >= 0 && concentration <= 1); 454 455 return gamma_l; 456 } 457 458 static INLINE double 459 sln_compute_voigt_profile 460 (const double wavenumber, /* In cm^-1 */ 461 const double nu, /* Line center in cm^-1 */ 462 const double gamma_d, /* Doppler line half width in cm^-1 */ 463 const double gamma_l) /* Lorentz line half width in cm^-1 */ 464 { 465 /* Constants */ 466 const double sqrt_ln2 = 0.83255461115769768821; /* sqrt(log(2)) */ 467 const double sqrt_ln2_over_pi = 0.46971863934982566180; /* sqrt(log(2)/M_PI) */ 468 const double sqrt_ln2_over_gamma_d = sqrt_ln2 / gamma_d; 469 470 const double x = (wavenumber - nu) * sqrt_ln2_over_gamma_d; 471 const double y = gamma_l * sqrt_ln2_over_gamma_d; 472 const double k = sln_faddeeva(x, y); 473 return k*sqrt_ln2_over_pi/gamma_d; 474 } 475 476 static INLINE const char* 477 sln_mesh_type_cstr(const enum sln_mesh_type type) 478 { 479 const char* cstr = NULL; 480 481 switch(type) { 482 case SLN_MESH_FIT: cstr = "fit"; break; 483 case SLN_MESH_UPPER: cstr = "upper"; break; 484 default: FATAL("Unreachable code\n"); break; 485 } 486 return cstr; 487 } 488 489 static INLINE const char* 490 sln_line_profile_cstr(const enum sln_line_profile profile) 491 { 492 const char* cstr = NULL; 493 494 switch(profile) { 495 case SLN_LINE_PROFILE_VOIGT: cstr = "voigt"; break; 496 default: FATAL("Unreachable code\n"); break; 497 } 498 return cstr; 499 } 500 501 END_DECLS 502 503 #endif /* SLN_H */