htsky

Load and structure a vertically stratified atmosphere
git clone git://git.meso-star.fr/htsky.git
Log | Files | Refs | README | LICENSE

htsky_c.h (4472B)


      1 /* Copyright (C) 2018, 2019, 2020, 2021 |Méso|Star> (contact@meso-star.com)
      2  * Copyright (C) 2018, 2019 Centre National de la Recherche Scientifique
      3  * Copyright (C) 2018, 2019 Université Paul Sabatier
      4  *
      5  * This program is free software: you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License as published by
      7  * the Free Software Foundation, either version 3 of the License, or
      8  * (at your option) any later version.
      9  *
     10  * This program is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     13  * GNU General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU General Public License
     16  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     17 
     18 #ifndef HTSKY_C_H
     19 #define HTSKY_C_H
     20 
     21 #include "htsky.h"
     22 
     23 #include <high_tune/htcp.h>
     24 
     25 #include <rsys/logger.h>
     26 #include <rsys/mem_allocator.h>
     27 #include <rsys/ref_count.h>
     28 #include <rsys/str.h>
     29 
     30 /* Declare some constants */
     31 #define DRY_AIR_MOLAR_MASS 0.0289644 /* In kg.mol^-1 */
     32 #define H2O_MOLAR_MASS 0.01801528 /* In kg.mol^-1 */
     33 #define GAS_CONSTANT 8.3144598 /* In kg.m^2.s^-2.mol^-1.K */
     34 
     35 /* Forward declaration of external data types */
     36 struct htcp;
     37 struct htgop;
     38 struct htmie;
     39 struct svx_tree;
     40 struct svx_tree_desc;
     41 
     42 /* Forward declarations of internal data types */
     43 struct atmosphere;
     44 
     45 struct split {
     46   size_t index; /* Index of the current htcp voxel */
     47   double height; /* Absolute height where the next voxel starts */
     48 };
     49 
     50 #define DARRAY_NAME split
     51 #define DARRAY_DATA struct split
     52 #include <rsys/dynamic_array.h>
     53 
     54 /* Properties of a short wave spectral band */
     55 struct band_prop {
     56   /* Average cross section in the band */
     57   double Ca_avg; /* Absorption cross section */
     58   double Cs_avg; /* Scattering cross section */
     59 
     60   /* Average asymmetry parameter the band */
     61   double g_avg;
     62 };
     63 
     64 struct htsky {
     65   struct cloud** clouds; /* Per sw_band cloud data structure */
     66 
     67   /* Per sw_band and per quadrature point atmosphere data structure */
     68   struct atmosphere** atmosphere;
     69 
     70   /* Loaders of... */
     71   struct htcp* htcp;   /* ... Cloud properties */
     72   struct htgop* htgop; /* ... Gas optical properties */
     73   struct htmie* htmie; /* ... Mie's data */
     74 
     75   /* Star-VX library handle */
     76   struct svx_device* svx;
     77 
     78   struct htcp_desc htcp_desc; /* Descriptor of the loaded LES data */
     79 
     80   /* LUT used to map the index of a Z from the regular SVX to the irregular
     81    * HTCP data */
     82   struct darray_split svx2htcp_z;
     83   double lut_cell_sz; /* Size of a svx2htcp_z cell */
     84 
     85   /* Ids and optical properties of spectral bands loaded by HTGOP and currently
     86    * handled by the sky */
     87   enum htsky_spectral_type spectral_type;
     88   size_t bands_range[2]; /* Inclusive band ids */
     89   struct band_prop* bands;
     90 
     91   int repeat_clouds; /* Make clouds infinite in X and Y */
     92   int is_cloudy; /* The sky has clouds */
     93 
     94   unsigned nthreads; /* #threads */
     95 
     96   struct str name; /* Name of the sky */
     97 
     98   struct mem_allocator* allocator;
     99   struct mem_allocator svx_allocator;
    100   struct logger* logger;
    101   struct logger logger__; /* Default logger */
    102   int verbose;
    103   ref_T ref;
    104 };
    105 
    106 static FINLINE double
    107 wavenumber_to_wavelength(const double nu/*In cm^-1*/)
    108 {
    109   return 1.e7 / nu;
    110 }
    111 
    112 /* In cm^-1 */
    113 static FINLINE double
    114 wavelength_to_wavenumber(const double lambda/*In nanometer*/)
    115 {
    116   return wavenumber_to_wavelength(lambda);
    117 }
    118 
    119 /* Compute the dry air density in the cloud */
    120 static FINLINE double
    121 cloud_dry_air_density
    122   (const struct htcp_desc* desc,
    123    const size_t ivox[3]) /* Index of the voxel */
    124 {
    125   double P = 0; /* Pressure in Pa */
    126   double T = 0; /* Temperature in K */
    127   ASSERT(desc && ivox);
    128   P = htcp_desc_PABST_at(desc, ivox[0], ivox[1], ivox[2], 0/*time*/);
    129   T = htcp_desc_T_at(desc, ivox[0], ivox[1], ivox[2], 0/*time*/);
    130   return (P*DRY_AIR_MOLAR_MASS)/(T*GAS_CONSTANT);
    131 }
    132 
    133 /* Compute the water molar fraction */
    134 static FINLINE double
    135 cloud_water_vapor_molar_fraction
    136   (const struct htcp_desc* desc,
    137    const size_t ivox[3])
    138 {
    139   double rvt = 0;
    140   ASSERT(desc && ivox);
    141   rvt = htcp_desc_RVT_at(desc, ivox[0], ivox[1], ivox[2], 0/*time*/);
    142   return rvt / (rvt + H2O_MOLAR_MASS/DRY_AIR_MOLAR_MASS);
    143 }
    144 
    145 /* Transform a position from world to cloud space */
    146 extern LOCAL_SYM double*
    147 world_to_cloud
    148   (const struct htsky* sky,
    149    const double pos_ws[3], /* World space position */
    150    double out_pos_cs[3]);
    151 
    152 #endif /* HTSKY_C_H */