Skip to content

vulcan.build_atm

build_atm


Module for building the atmospheric structure and calculating the initial mixing ratios for VULCAN.

Imports
  • vulcan.config: Config class for handling configuration settings.
  • vulcan.paths: Paths to various files and directories used in VULCAN.
  • vulcan.phy_const: Physical constants used in VULCAN.
  • chem_funs: ni, spec_list: number of species in the network and the list of species.

Atm(vulcan_cfg)

Bases: object

Build the atmospheric structure stored in store.AtmData.

The methods are called in this order by vulcan.run_vulcan: f_pico, load_TPK, mol_diff, sp_sat (with condensation), then after the initial abundances f_mu_dz and BC_flux, and read_sflux with photochemistry.

vulcan_cfg : Config Run configuration; the profile settings are copied to attributes.

Source code in src/vulcan/build_atm.py
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
def __init__(self, vulcan_cfg: Config):
    """
    Parameters
    ----------
    vulcan_cfg : Config
        Run configuration; the profile settings are copied to attributes.
    """
    self.cfg = vulcan_cfg
    self.gs = vulcan_cfg.gs  # gravity
    self.P_b = vulcan_cfg.P_b
    self.P_t = vulcan_cfg.P_t
    self.type = vulcan_cfg.atm_type
    self.use_Kzz = vulcan_cfg.use_Kzz
    self.Kzz_prof = vulcan_cfg.Kzz_prof
    self.const_Kzz = vulcan_cfg.const_Kzz
    self.use_vz = vulcan_cfg.use_vz
    self.vz_prof = vulcan_cfg.vz_prof
    self.const_vz = vulcan_cfg.const_vz
    self.use_settling = vulcan_cfg.use_settling
    self.non_gas_sp = vulcan_cfg.non_gas_sp

Kzz_prof = vulcan_cfg.Kzz_prof instance-attribute

P_b = vulcan_cfg.P_b instance-attribute

P_t = vulcan_cfg.P_t instance-attribute

cfg = vulcan_cfg instance-attribute

const_Kzz = vulcan_cfg.const_Kzz instance-attribute

const_vz = vulcan_cfg.const_vz instance-attribute

gs = vulcan_cfg.gs instance-attribute

non_gas_sp = vulcan_cfg.non_gas_sp instance-attribute

type = vulcan_cfg.atm_type instance-attribute

use_Kzz = vulcan_cfg.use_Kzz instance-attribute

use_settling = vulcan_cfg.use_settling instance-attribute

use_vz = vulcan_cfg.use_vz instance-attribute

vz_prof = vulcan_cfg.vz_prof instance-attribute

BC_flux(atm)

Read the boundary conditions from the files named in the configuration.

With use_topflux, top_BC_flux_file gives a species and its flux per line. With use_botflux, bot_BC_flux_file gives a species, its flux and its deposition velocity; with use_fix_sp_bot, the fourth column of that file is the fixed mixing ratio.

Parameters:

Name Type Description Default
atm AtmData

Sets top_flux and bot_flux [cm-2 s-1] (positive into the atmosphere), bot_vdep [cm s-1] and bot_fix_sp [dimensionless].

required
Source code in src/vulcan/build_atm.py
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
def BC_flux(self, atm):
    """
    Read the boundary conditions from the files named in the configuration.

    With `use_topflux`, `top_BC_flux_file` gives a species and its flux per line. With
    `use_botflux`, `bot_BC_flux_file` gives a species, its flux and its deposition
    velocity; with `use_fix_sp_bot`, the fourth column of that file is the fixed
    mixing ratio.

    Parameters
    ----------
    atm : store.AtmData
        Sets `top_flux` and `bot_flux` [cm-2 s-1] (positive into the atmosphere),
        `bot_vdep` [cm s-1] and `bot_fix_sp` [dimensionless].
    """
    # read in the const top BC
    if self.cfg.use_topflux:
        log.info('Using the prescribed constant top flux.')
        with open(self.cfg.top_BC_flux_file) as f:
            for line in f.readlines():
                if not line.startswith('#') and line.strip():
                    li = line.split()
                    atm.top_flux[species.index(li[0])] = li[1]

    # read in the const bottom BC
    if self.cfg.use_botflux:
        log.info('Using the prescribed constant bottom flux.')
        with open(self.cfg.bot_BC_flux_file) as f:
            for line in f.readlines():
                if not line.startswith('#') and line.strip():
                    li = line.split()
                    atm.bot_flux[species.index(li[0])] = li[1]
                    atm.bot_vdep[species.index(li[0])] = li[2]

    # using fixed-mixing-ratio BC
    if self.cfg.use_fix_sp_bot:
        log.info('Using the prescribed fixed bottom mixing ratios.')
        with open(self.cfg.bot_BC_flux_file) as f:
            for line in f.readlines():
                if not line.startswith('#') and line.strip():
                    li = line.split()
                    atm.bot_fix_sp[species.index(li[0])] = li[3]

TP_H14(pco, *args_analytical)

Analytical T-P profile of Heng et al. (2014), their eq. 126.

The collision-induced absorption term is switched off. The shortwave scattering parameter sets the albedo, (1 - beta_S) / (1 + beta_S), which reduces T_irr by (1 - albedo)^(1/4). Gravity is Config.gs throughout.

Parameters:

Name Type Description Default
pco ndarray

Pressure [dyn cm-2].

required
*args_analytical float

T_int [K], T_irr [K], kappa_L and kappa_S (longwave and shortwave opacities) [cm2 g-1], beta_S and beta_L [dimensionless], as in Config.para_anaTP.

()

Returns:

Type Description
ndarray

Temperature [K], same shape as pco.

Source code in src/vulcan/build_atm.py
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
def TP_H14(self, pco, *args_analytical):
    """
    Analytical T-P profile of Heng et al. (2014), their eq. 126.

    The collision-induced absorption term is switched off. The shortwave scattering
    parameter sets the albedo, (1 - beta_S) / (1 + beta_S), which reduces T_irr by
    (1 - albedo)^(1/4). Gravity is `Config.gs` throughout.

    Parameters
    ----------
    pco : np.ndarray
        Pressure [dyn cm-2].
    *args_analytical : float
        T_int [K], T_irr [K], kappa_L and kappa_S (longwave and shortwave opacities)
        [cm2 g-1], beta_S and beta_L [dimensionless], as in `Config.para_anaTP`.

    Returns
    -------
    np.ndarray
        Temperature [K], same shape as `pco`.
    """

    # convert args_analytical tuple to a list so we can modify it
    T_int, T_irr, ka_0, ka_s, beta_s, beta_l = list(args_analytical)

    g = self.cfg.gs
    P_b = self.cfg.P_b

    # albedo(beta_s) also affects T_irr
    albedo = (1.0 - beta_s) / (1.0 + beta_s)
    T_irr *= (1 - albedo) ** 0.25
    eps_L = 3.0 / 8
    eps_L3 = 1.0 / 3
    ka_CIA = 0
    m = pco / g
    m_0 = P_b / g
    ka_l = ka_0 + ka_CIA * m / m_0
    term1 = (
        T_int**4
        / 4
        * (1 / eps_L + m / (eps_L3 * beta_l**2) * (ka_0 + ka_CIA * m / (2 * m_0)))
    )
    term2 = 1 / (2 * eps_L) + scipy.special.expn(2, ka_s * m / beta_s) * (
        ka_s / (ka_l * beta_s) - (ka_CIA) * m * beta_s / (eps_L3 * ka_s * m_0 * beta_l**2)
    )
    term3 = (
        ka_0
        * beta_s
        / (eps_L3 * ka_s * beta_l**2)
        * (1.0 / 3 - scipy.special.expn(4, ka_s * m / beta_s))
    )
    term4 = 0.0  # related to CIA
    T = (term1 + T_irr**4 / 8 * (term2 + term3 + term4)) ** 0.25

    return T

f_mu_dz(data_var, data_atm, output)

Set the mean molar mass, gravity, scale height and the height grid.

Integrates hydrostatic balance layer by layer, dz = H_p ln(p_i / p_{i+1}) with H_p = k_B T N_A / (mu g) and g = g_s (R_p / (R_p + z))^2. For a gas giant with P_b >= 1 bar, z = 0 and g = g_s at the interface closest to 1 bar; otherwise at the bottom. With use_settling, also calls settling_velocity. Plots the T-P profile when plot_TP is set and warns when T is outside the 200 to 6000 K range of the thermodynamic data.

Parameters:

Name Type Description Default
data_var Variables

Reads ymix.

required
data_atm AtmData

Reads Tco and pico; sets mu, g, Hp and the height grid (dz, dzi, zco, zmco). See store.AtmData for units.

required
output Output

Not used.

required

Returns:

Type Description
AtmData

data_atm.

Raises:

Type Description
ValueError

If a non-gaseous species has no particle radius or density.

Source code in src/vulcan/build_atm.py
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
def f_mu_dz(self, data_var, data_atm, output):
    """
    Set the mean molar mass, gravity, scale height and the height grid.

    Integrates hydrostatic balance layer by layer, dz = H_p ln(p_i / p_{i+1}) with
    H_p = k_B T N_A / (mu g) and g = g_s (R_p / (R_p + z))^2. For a gas giant with
    `P_b` >= 1 bar, z = 0 and g = g_s at the interface closest to 1 bar; otherwise at
    the bottom. With `use_settling`, also calls `settling_velocity`. Plots the T-P
    profile when `plot_TP` is set and warns when T is outside the 200 to 6000 K range of
    the thermodynamic data.

    Parameters
    ----------
    data_var : store.Variables
        Reads `ymix`.
    data_atm : store.AtmData
        Reads `Tco` and `pico`; sets `mu`, `g`, `Hp` and the height grid (`dz`, `dzi`,
        `zco`, `zmco`). See `store.AtmData` for units.
    output : op.Output
        Not used.

    Returns
    -------
    store.AtmData
        `data_atm`.

    Raises
    ------
    ValueError
        If a non-gaseous species has no particle radius or density.
    """

    nz = self.cfg.nz
    dz, zco = data_atm.dz, data_atm.zco  # pressure defined at interfaces
    Tco, pico = data_atm.Tco.copy(), data_atm.pico.copy()
    Hp = data_atm.Hp

    if (
        not self.cfg.rocky and self.P_b >= 1e6
    ):  # if the lower BC greater than 1bar for gas giants
        # Find the index of pico closest to 1bar
        pref_indx = min(range(nz + 1), key=lambda i: abs(np.log10(pico[i]) - 6.0))
    else:
        pref_indx = 0

    # updating and storing mu
    data_atm = self.mean_mass(data_var, data_atm, ni)
    mu = data_atm.mu
    gs = self.gs
    gz = data_atm.g
    # updating and storing mu
    data_atm = self.mean_mass(data_var, data_atm, ni)

    for i in range(pref_indx, nz):
        if i == pref_indx:
            gz[i] = gs
            Hp[i] = kb * Tco[i] / (mu[i] / Navo * gs)
        else:
            gz[i] = gs * (self.cfg.Rp / (self.cfg.Rp + zco[i])) ** 2
            Hp[i] = kb * Tco[i] / (mu[i] / Navo * gz[i])
        dz[i] = Hp[i] * np.log(
            pico[i] / pico[i + 1]
        )  # pico[i+1] has a lower P than pico[i] (higer height)
        zco[i + 1] = zco[i] + dz[i]  # zco is set zero at 1bar for gas giants

    # for pref_indx != zero
    if not pref_indx == 0:
        for i in range(pref_indx - 1, -1, -1):
            gz[i] = gs * (self.cfg.Rp / (self.cfg.Rp + zco[i + 1])) ** 2
            Hp[i] = kb * Tco[i] / (mu[i] / Navo * gz[i])
            dz[i] = Hp[i] * np.log(pico[i] / pico[i + 1])
            zco[i] = zco[i + 1] - dz[i]  # from i+1 propogating down to i

    zmco = 0.5 * (zco + np.roll(zco, -1))
    zmco = zmco[:-1]
    dzi = 0.5 * (dz + np.roll(dz, 1))
    dzi = dzi[1:]
    # for the j grid, dzi[j] from the grid above and dz[j-1] from the grid below

    # for the molecular diffsuion
    if self.cfg.use_moldiff:
        Ti = 0.5 * (Tco + np.roll(Tco, -1))
        data_atm.Ti = Ti[:-1]
        Hpi = 0.5 * (Hp + np.roll(Hp, -1))
        data_atm.Hpi = Hpi[:-1]

    # updating and storing dz and dzi
    # data_atm.dz = dz
    data_atm.dzi = dzi
    # data_atm.zco = zco
    data_atm.zmco = zmco
    data_atm.g, data_atm.gs = gz, gs
    data_atm.pref_indx = pref_indx

    if self.use_settling:
        self.settling_velocity(data_atm)

    # plot T-P profile
    if self.cfg.plot_TP:
        plot.plot_TP(self.cfg, data_atm)

    # print warning when T exceeds the valid range of Gibbs free energy (NASA polynomials)
    if np.any(np.logical_or(data_atm.Tco < 200, data_atm.Tco > 6000)):
        log.warning('Temperatures exceed the valid range of Gibbs free energy.\n')

    return data_atm

f_pico(data_atm)

Set the interface pressures from the layer-centre pressures.

Each interior interface takes the geometric mean of the two adjacent centres; the bottom and top interfaces are extrapolated by the same ratio in log p.

Parameters:

Name Type Description Default
data_atm AtmData

Reads pco [dyn cm-2]; sets pico, shape (nz + 1,) [dyn cm-2].

required

Returns:

Type Description
AtmData

data_atm.

Source code in src/vulcan/build_atm.py
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
def f_pico(self, data_atm):
    """
    Set the interface pressures from the layer-centre pressures.

    Each interior interface takes the geometric mean of the two adjacent centres; the
    bottom and top interfaces are extrapolated by the same ratio in log p.

    Parameters
    ----------
    data_atm : store.AtmData
        Reads `pco` [dyn cm-2]; sets `pico`, shape (nz + 1,) [dyn cm-2].

    Returns
    -------
    store.AtmData
        `data_atm`.
    """

    pco = data_atm.pco

    # construct pico
    pco_up1 = np.roll(pco, 1)
    pi = (pco * pco_up1) ** 0.5
    pi[0] = pco[0] ** 1.5 * pco[1] ** (-0.5)
    pi = np.append(pi, pco[-1] ** 1.5 * pco[-2] ** (-0.5))

    # store pico
    data_atm.pico = pi
    # data_atm.pco = pco

    return data_atm

load_TPK(data_atm)

Set the temperature, eddy diffusion and vertical velocity profiles.

  • Config.atm_type sets T: 'isothermal' (Tiso), 'analytical' (TP_H14), 'file' (from atm_file, constant beyond its pressure range), 'vulcan_ini' (from a previous run) or 'table' (p and T from vul_ini).
  • Kzz_prof sets Kzz on the interior interfaces: 'const' (const_Kzz), 'file', 'JM16' (1e5 (300 / p[mbar])^0.5 cm2 s-1, at least K_deep), 'Pfunc' (K_max (K_p_lev / p)^0.4, at least K_max) or 'climate' (zero until the first climate update, before the integration).
  • vz_prof sets vz: 'const' (const_vz) or 'file' (needs atm_type = 'file').

Kzz and vz are zero when use_Kzz and use_vz are off.

Parameters:

Name Type Description Default
data_atm AtmData

Reads pco and pico [dyn cm-2]; sets Tco [K], Kzz [cm2 s-1], vz [cm s-1], and M and n_0 = p / (k_B T) [cm-3]. 'table' also replaces pco.

required

Returns:

Type Description
AtmData

data_atm.

Raises:

Type Description
ValueError

If atm_type is not recognised, a 'table' file has a different number of layers, or T is not finite and positive in every layer.

IOError

If Kzz_prof or vz_prof is not recognised.

Source code in src/vulcan/build_atm.py
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
def load_TPK(self, data_atm):
    """
    Set the temperature, eddy diffusion and vertical velocity profiles.

    - `Config.atm_type` sets T: 'isothermal' (`Tiso`), 'analytical' (`TP_H14`), 'file'
      (from `atm_file`, constant beyond its pressure range), 'vulcan_ini' (from a
      previous run) or 'table' (p and T from `vul_ini`).
    - `Kzz_prof` sets Kzz on the interior interfaces: 'const' (`const_Kzz`), 'file',
      'JM16' (1e5 (300 / p[mbar])^0.5 cm2 s-1, at least `K_deep`), 'Pfunc'
      (`K_max` (`K_p_lev` / p)^0.4, at least `K_max`) or 'climate' (zero until the
      first climate update, before the integration).
    - `vz_prof` sets vz: 'const' (`const_vz`) or 'file' (needs `atm_type = 'file'`).

    Kzz and vz are zero when `use_Kzz` and `use_vz` are off.

    Parameters
    ----------
    data_atm : store.AtmData
        Reads `pco` and `pico` [dyn cm-2]; sets `Tco` [K], `Kzz` [cm2 s-1], `vz`
        [cm s-1], and `M` and `n_0` = p / (k_B T) [cm-3]. 'table' also replaces `pco`.

    Returns
    -------
    store.AtmData
        `data_atm`.

    Raises
    ------
    ValueError
        If `atm_type` is not recognised, a 'table' file has a different number of
        layers, or T is not finite and positive in every layer.
    IOError
        If `Kzz_prof` or `vz_prof` is not recognised.
    """

    PTK_fun = {}
    nz = self.cfg.nz

    # IF switches for TP types
    if self.type == 'isothermal':
        data_atm.Tco = np.repeat(self.cfg.Tiso, nz)
        # data_atm.Kzz = np.repeat(self.const_Kzz,nz-1)
        # data_atm.vz = np.repeat(self.const_vz,nz-1)

    elif self.type == 'analytical':
        # plotting T-P on the fly
        para_atm = self.cfg.para_anaTP

        # return the P-T function
        PTK_fun['pT'] = lambda pressure: self.TP_H14(pressure, *para_atm)
        data_atm.Tco = PTK_fun['pT'](data_atm.pco)
        # data_atm.Kzz = np.repeat(self.const_Kzz,nz-1)
        # data_atm.vz = np.repeat(self.const_vz,nz-1)

    # for atm_type = 'file' and also Kzz_prof = 'file
    elif self.type == 'file':
        if self.Kzz_prof == 'file':
            atm_table = np.genfromtxt(
                self.cfg.atm_file, names=True, dtype=None, skip_header=1
            )
            p_file, T_file, Kzz_file = (
                atm_table['Pressure'],
                atm_table['Temp'],
                atm_table['Kzz'],
            )

        else:
            atm_table = np.genfromtxt(
                self.cfg.atm_file, names=True, dtype=None, skip_header=1
            )
            p_file, T_file = atm_table['Pressure'], atm_table['Temp']

        if max(p_file) < data_atm.pco[0] or min(p_file) > data_atm.pco[-1]:
            log.warning(
                'P_b and P_t assgined in vulcan.cfg are out of range of the input. Constant extension is used.'
            )

        PTK_fun['pT'] = interpolate.interp1d(
            p_file,
            T_file,
            assume_sorted=False,
            bounds_error=False,
            fill_value=(T_file[np.argmin(p_file)], T_file[np.argmax(p_file)]),
        )
        # store Tco in data_atm
        try:
            data_atm.Tco = PTK_fun['pT'](data_atm.pco)

        # for SciPy earlier than v0.18.0
        except ValueError:
            PTK_fun['pT'] = interpolate.interp1d(
                p_file,
                T_file,
                assume_sorted=False,
                bounds_error=False,
                fill_value=T_file[np.argmin(p_file)],
            )
            data_atm.Tco = PTK_fun['pT'](data_atm.pco)

        if self.use_Kzz and self.Kzz_prof == 'file':
            PTK_fun['pK'] = interpolate.interp1d(
                p_file,
                Kzz_file,
                assume_sorted=False,
                bounds_error=False,
                fill_value=(Kzz_file[np.argmin(p_file)], Kzz_file[np.argmax(p_file)]),
            )
            # store Kzz in data_atm
            try:
                data_atm.Kzz = PTK_fun['pK'](data_atm.pico[1:-1])
            # for SciPy earlier than v0.18.0
            except ValueError:
                PTK_fun['pK'] = interpolate.interp1d(
                    p_file,
                    Kzz_file,
                    assume_sorted=False,
                    bounds_error=False,
                    fill_value=Kzz_file[np.argmin(p_file)],
                )
                data_atm.Kzz = PTK_fun['pK'](data_atm.pico[1:-1])

        elif self.Kzz_prof == 'const':
            data_atm.Kzz = np.repeat(self.const_Kzz, nz - 1)

    elif self.type == 'vulcan_ini':
        log.info('Initializing PT from the prvious run ' + self.cfg.vul_ini)
        with open(self.cfg.vul_ini, 'rb') as handle:
            vul_data = pickle.load(handle)

        data_atm.Tco = vul_data['atm']['Tco']

    elif self.type == 'table':
        log.info('Initializing PT from the prvious run ' + self.cfg.vul_ini)
        table = np.genfromtxt(self.cfg.vul_ini, names=True, dtype=None, skip_header=1)
        if not len(data_atm.pco) == len(table['Pressure']):
            raise ValueError(
                'The input profile has different layers than the current setting...'
            )
        data_atm.pco = table['Pressure']
        data_atm.Tco = table['Temp']

    else:
        raise ValueError(f'atm_type {self.type} not recongized.')

    # IF switches for Kzz types
    if self.Kzz_prof == 'const':
        data_atm.Kzz = np.repeat(self.const_Kzz, nz - 1)
    elif self.Kzz_prof == 'JM16':  # Kzz profiles assumed in Moses et al.2016
        data_atm.Kzz = 1e5 * (300.0 / (data_atm.pico[1:-1] * 1e-3)) ** 0.5
        data_atm.Kzz = np.maximum(self.cfg.K_deep, data_atm.Kzz)
    elif self.Kzz_prof == 'Pfunc':  # Kzz profiles assumed in Tsai 2020
        data_atm.Kzz = (
            self.cfg.K_max * (self.cfg.K_p_lev * 1e6 / (data_atm.pico[1:-1])) ** 0.4
        )
        data_atm.Kzz = np.maximum(self.cfg.K_max, data_atm.Kzz)

    elif self.Kzz_prof == 'file':
        pass  # already defined within atm_type = 'file
    elif self.Kzz_prof == 'climate':
        data_atm.Kzz = np.zeros(nz - 1)  # set by the climate hook before integrating
    else:
        raise IOError(
            '"Kzz_prof" (the type of Kzz profile) cannot be recongized.\nPlease assign it as "file" or "const" or "JM16" in vulcan_cfg.'
        )

    # IF switches for Vz types
    if self.vz_prof == 'const':
        data_atm.vz = np.repeat(self.const_vz, nz - 1)
    elif self.vz_prof == 'file':
        inter_vz = interpolate.interp1d(
            atm_table['Pressure'],
            atm_table['vz'],
            assume_sorted=False,
            bounds_error=False,
            fill_value=0,
        )
        data_atm.vz = inter_vz(data_atm.pico[1:-1])
    else:
        raise IOError(
            '"vz_prof" cannot be recongized.\nPlease assign it as "file" or "const" in vulcan_cfg.'
        )

    if not self.use_Kzz:
        # store Kzz in data_atm
        data_atm.Kzz = np.zeros(nz - 1)
    if not self.use_vz:
        data_atm.vz = np.zeros(nz - 1)

    # every rate coefficient and the number density depend on T: reject unphysical
    # profiles here rather than letting NaNs spread through the integration
    if not np.all(np.isfinite(data_atm.Tco) & (data_atm.Tco > 0)):
        raise ValueError(
            'Temperature must be finite and positive in every layer; got T from '
            f'{np.nanmin(data_atm.Tco)} to {np.nanmax(data_atm.Tco)} K '
            f'(atm_type = {self.type})'
        )

    # calculating and storing M(the third body)
    data_atm.M = data_atm.pco / (kb * data_atm.Tco)
    data_atm.n_0 = data_atm.M.copy()

    return data_atm

mean_mass(var, atm, ni)

Set the mean molar mass atm.mu [g mol-1] from the mixing ratios var.ymix.

Parameters:

Name Type Description Default
var Variables

Reads ymix.

required
atm AtmData

Sets mu, shape (nz,) [g mol-1].

required
ni int

Number of species.

required

Returns:

Type Description
AtmData

atm.

Source code in src/vulcan/build_atm.py
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
def mean_mass(self, var, atm, ni):
    """
    Set the mean molar mass `atm.mu` [g mol-1] from the mixing ratios `var.ymix`.

    Parameters
    ----------
    var : store.Variables
        Reads `ymix`.
    atm : store.AtmData
        Sets `mu`, shape (nz,) [g mol-1].
    ni : int
        Number of species.

    Returns
    -------
    store.AtmData
        `atm`.
    """
    nz = self.cfg.nz
    mu = np.zeros(nz)
    for i in range(ni):
        mu += self.mol_mass(species[i]) * var.ymix[:, i]
    atm.mu = mu
    return atm

mol_diff(atm)

Set the molar masses and the molecular diffusion coefficients.

Without Config.use_moldiff, only ms is set. Otherwise each species gets a reference binary coefficient A T^s / n in the atm_base gas (H2: CH4-H2 from Moses 2000a; N2 and O2: CH4-N2 and CH4-O2 from Banks & Kockarts 1973; CO2: H2-CO2), scaled by the square root of the ratio of reduced masses. Non-gaseous species get zero.

Parameters:

Name Type Description Default
atm AtmData

Reads Tco [K] and n_0 [cm-3]; sets ms [g mol-1], Dzz [cm2 s-1] and alpha [dimensionless].

required

Raises:

Type Description
IOError

If atm_base is not H2, N2, O2 or CO2.

Source code in src/vulcan/build_atm.py
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
def mol_diff(self, atm):
    """
    Set the molar masses and the molecular diffusion coefficients.

    Without `Config.use_moldiff`, only `ms` is set. Otherwise each species gets a
    reference binary coefficient A T^s / n in the `atm_base` gas (H2: CH4-H2 from
    Moses 2000a; N2 and O2: CH4-N2 and CH4-O2 from Banks & Kockarts 1973; CO2: H2-CO2),
    scaled by the square root of the ratio of reduced masses. Non-gaseous species get
    zero.

    Parameters
    ----------
    atm : store.AtmData
        Reads `Tco` [K] and `n_0` [cm-3]; sets `ms` [g mol-1], `Dzz` [cm2 s-1] and
        `alpha` [dimensionless].

    Raises
    ------
    IOError
        If `atm_base` is not H2, N2, O2 or CO2.
    """
    Tco = atm.Tco
    n_0 = atm.n_0

    # using the value defined on the interface
    Tco_i = np.delete((Tco + np.roll(Tco, 1)) * 0.5, 0)
    n0_i = np.delete((n_0 + np.roll(n_0, 1)) * 0.5, 0)

    if not self.cfg.use_moldiff:
        for i in range(len(species)):
            # this is required even without molecular weight
            atm.ms[i] = compo[compo_row.index(species[i])][-1]
        return

    if self.cfg.atm_base == 'H2':
        Dzz_gen = lambda T, n_tot, mi: (
            2.2965e17 * T**0.765 / n_tot * (16.04 / mi * (mi + 2.016) / 18.059) ** 0.5
        )  # from Moses 2000a

        # scaling with (15.27) in [Aeronomy part B by Banks & Kockarts(1973)]
        # *( m_ref/mi*(mi+ m_base)/m_tot )**0.5 (m_ref is the molecular mass of the ref-base e.g. CH4 in CH4-H2 in Moses 2000a)

        # # thermal diffusion factor (>0 means (heavier) components diffuse toward colder rigions)
        if 'H' in species:
            atm.alpha[species.index('H')] = -0.1  # simplified from Moses 2000a
        if 'He' in species:
            atm.alpha[species.index('He')] = 0.145
        for sp in species:
            if self.mol_mass(sp) > 4.0:
                atm.alpha[species.index(sp)] = 0.25

    elif (
        self.cfg.atm_base == 'N2'
    ):  # use CH4-N2 in Aeronomy [Banks ] as a reference to scale by the molecular mass
        Dzz_gen = lambda T, n_tot, mi: (
            7.34e16 * T**0.75 / n_tot * (16.04 / mi * (mi + 28.014) / 44.054) ** 0.5
        )

        # # thermal diffusion factor (>0 means (heavier) components diffuse toward colder rigions)
        if 'H' in species:
            atm.alpha[species.index('H')] = -0.25
        if 'H2' in species:
            atm.alpha[species.index('H2')] = -0.25
        if 'He' in species:
            atm.alpha[species.index('He')] = -0.25
        if 'Ar' in species:
            atm.alpha[species.index('Ar')] = 0.17

    elif (
        self.cfg.atm_base == 'O2'
    ):  # use CH4-O2 in Aeronomy [Banks ] as a reference to scale by the molecular mass
        Dzz_gen = lambda T, n_tot, mi: (
            7.51e16 * T**0.759 / n_tot * (16.04 / mi * (mi + 32) / 48.04) ** 0.5
        )

        # # thermal diffusion factor (>0 means (heavier) components diffuse toward colder rigions)
        if 'H' in species:
            atm.alpha[species.index('H')] = -0.25
        if 'H2' in species:
            atm.alpha[species.index('H2')] = -0.25
        if 'He' in species:
            atm.alpha[species.index('He')] = -0.25
        if 'Ar' in species:
            atm.alpha[species.index('Ar')] = 0.17

    elif (
        self.cfg.atm_base == 'CO2'
    ):  # use H2-CO2 in Hu seager as a reference to scale by the molecular mass
        Dzz_gen = lambda T, n_tot, mi: (
            2.15e17 * T**0.750 / n_tot * (2.016 / mi * (mi + 44.001) / 46.017) ** 0.5
        )

        # # thermal diffusion factor (>0 means (heavier) components diffuse toward colder rigions)
        if 'H' in species:
            atm.alpha[species.index('H')] = -0.25
        if 'H2' in species:
            atm.alpha[species.index('H2')] = -0.25
        if 'He' in species:
            atm.alpha[species.index('He')] = -0.25
        if 'Ar' in species:
            atm.alpha[species.index('Ar')] = 0.17

    else:
        raise IOError('\n Unknow atm_base!')

    for i in range(len(species)):
        # input should be float or in the form of nz-long 1D array
        atm.Dzz[:, i] = Dzz_gen(Tco_i, n0_i, self.mol_mass(species[i]))
        # constructing the molecular weight for every species
        # this is required even without molecular weight
        atm.ms[i] = compo[compo_row.index(species[i])][-1]

    # setting the molecuar diffusion of the non-gaseous species to zero
    for sp in [_ for _ in self.cfg.non_gas_sp if _ in species]:
        atm.Dzz[:, species.index(sp)] = 0

mol_mass(sp)

Molar mass of a species from the composition file paths.COM_FILE [g mol-1].

Source code in src/vulcan/build_atm.py
865
866
867
868
869
def mol_mass(self, sp):
    """
    Molar mass of a species from the composition file `paths.COM_FILE` [g mol-1].
    """
    return compo['mass'][compo_row.index(sp)]

read_sflux(var, atm)

Read the stellar spectrum and put the flux at the top of the atmosphere on the bins.

The flux at the stellar surface from Config.sflux_file (wavelength [nm], flux [erg cm-2 s-1 nm-1]) is scaled by (R_star / a)^2 and interpolated linearly onto var.bins, with zero outside the file range. Logs how much of the energy the binning keeps.

Parameters:

Name Type Description Default
var Variables

Reads bins [nm]; sets sflux_top [erg cm-2 s-1 nm-1] and sflux_din12_indx, the first bin at Config.dbin_12trans (-1 if none).

required
atm AtmData

Sets sflux_raw, the spectrum as read.

required
Source code in src/vulcan/build_atm.py
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
def read_sflux(self, var, atm):
    """
    Read the stellar spectrum and put the flux at the top of the atmosphere on the bins.

    The flux at the stellar surface from `Config.sflux_file` (wavelength [nm], flux
    [erg cm-2 s-1 nm-1]) is scaled by (R_star / a)^2 and interpolated linearly onto
    `var.bins`, with zero outside the file range. Logs how much of the energy the
    binning keeps.

    Parameters
    ----------
    var : store.Variables
        Reads `bins` [nm]; sets `sflux_top` [erg cm-2 s-1 nm-1] and
        `sflux_din12_indx`, the first bin at `Config.dbin_12trans` (-1 if none).
    atm : store.AtmData
        Sets `sflux_raw`, the spectrum as read.
    """
    atm.sflux_raw = np.genfromtxt(
        self.cfg.sflux_file, dtype=float, skip_header=1, names=['lambda', 'flux']
    )

    # for values outside the boundary => fill_value = 0
    bins = var.bins

    dbin1 = self.cfg.dbin1
    dbin2 = self.cfg.dbin2

    inter_sflux = interpolate.interp1d(
        atm.sflux_raw['lambda'],
        atm.sflux_raw['flux']
        * (self.cfg.r_star * r_sun / (au * self.cfg.orbit_radius)) ** 2,
        bounds_error=False,
        fill_value=0,
    )
    for n, ld in enumerate(var.bins):
        var.sflux_top[n] = inter_sflux(ld)
        if ld == self.cfg.dbin_12trans:
            var.sflux_din12_indx = n
        # not converting to actinic flux yet *1/(hc/ld)

    # Check for energy conservation
    # finding the index for the left & right pts that match var.bins in the raw data
    raw_flux = (
        atm.sflux_raw['flux']
        * (self.cfg.r_star * r_sun / (au * self.cfg.orbit_radius)) ** 2
    )
    raw_left_indx = np.searchsorted(atm.sflux_raw['lambda'], bins[0], side='right')
    raw_right_indx = np.searchsorted(atm.sflux_raw['lambda'], bins[-1], side='right') - 1

    sum_orgin = 0
    # for checking the trapezoidal error in energy conservation
    for n in range(raw_left_indx, raw_right_indx):
        sum_orgin += (
            0.5
            * (raw_flux[n] + raw_flux[n + 1])
            * (atm.sflux_raw['lambda'][n + 1] - atm.sflux_raw['lambda'][n])
        )
    sum_orgin += (
        0.5
        * (inter_sflux(bins[0]) + raw_flux[raw_left_indx])
        * (atm.sflux_raw['lambda'][raw_left_indx] - bins[0])
    )
    sum_orgin += (
        0.5
        * (inter_sflux(bins[-1]) + raw_flux[raw_right_indx])
        * (bins[-1] - atm.sflux_raw['lambda'][raw_right_indx])
    )

    # dbin_12trans outside the bins
    if 'sflux_din12_indx' not in vars(var).keys():
        var.sflux_din12_indx = -1

    sum_bin = dbin1 * np.sum(var.sflux_top[: var.sflux_din12_indx])
    sum_bin -= dbin1 * 0.5 * (var.sflux_top[0] + var.sflux_top[var.sflux_din12_indx - 1])
    sum_bin += dbin2 * np.sum(var.sflux_top[var.sflux_din12_indx :])
    sum_bin -= dbin2 * 0.5 * (var.sflux_top[var.sflux_din12_indx] + var.sflux_top[-1])

    log.debug(
        'The stellar flux is interpolated onto uniform grid of '
        + str(self.cfg.dbin1)
        + ' (<'
        + str(self.cfg.dbin_12trans)
        + ' nm) and '
        + str(self.cfg.dbin2)
        + ' (>='
        + str(self.cfg.dbin_12trans)
        + ' nm)'
        + ' and conserving '
        + '{:.2f}'.format(100 * sum_bin / sum_orgin)
        + ' %'
        + ' energy.'
    )

settling_velocity(data_atm)

Set the terminal (Stokes) settling velocity of the non-gaseous species.

vs = -2/9 rho_p r_p^2 g / eta on each interface, with g averaged over the two layers and eta = a T^n / (b + T) [g cm-1 s-1] the dynamic viscosity of the Config.atm_base gas in the layer above, from Cloutman (Table 1); CO2 uses the N2 values. Called by f_mu_dz and again after each climate update.

Parameters:

Name Type Description Default
data_atm AtmData

Reads Tco [K], g [cm s-2], rho_p [g cm-3] and r_p [cm]; sets the columns of vs [cm s-1] of the species in Config.non_gas_sp, shape (nz - 1, ni), negative downward.

required

Raises:

Type Description
ValueError

If a non-gaseous species has no particle radius or density.

NameError

If Config.atm_base is not N2, H2, CO2, H2O or O2.

Source code in src/vulcan/build_atm.py
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
def settling_velocity(self, data_atm):
    """
    Set the terminal (Stokes) settling velocity of the non-gaseous species.

    vs = -2/9 rho_p r_p^2 g / eta on each interface, with g averaged over the two layers
    and eta = a T^n / (b + T) [g cm-1 s-1] the dynamic viscosity of the `Config.atm_base`
    gas in the layer above, from Cloutman (Table 1); CO2 uses the N2 values. Called by
    `f_mu_dz` and again after each climate update.

    Parameters
    ----------
    data_atm : store.AtmData
        Reads `Tco` [K], `g` [cm s-2], `rho_p` [g cm-3] and `r_p` [cm]; sets the columns
        of `vs` [cm s-1] of the species in `Config.non_gas_sp`, shape (nz - 1, ni),
        negative downward.

    Raises
    ------
    ValueError
        If a non-gaseous species has no particle radius or density.
    NameError
        If `Config.atm_base` is not N2, H2, CO2, H2O or O2.
    """
    # TESTing settling velocity
    # based on L. D. Cloutman: A Database of Selected Transport Coefficients for Combustion Studies (Table 1.)
    if self.cfg.atm_base == 'N2':
        na = 1.52
        a = 1.186e-5
        b = 86.54
    elif self.cfg.atm_base == 'H2':
        na = 1.67
        a = 1.936e-6
        b = 2.187
    elif self.cfg.atm_base == 'CO2':
        log.warning('NO CO2 viscosity yet! (using N2 instead)')
        na = 1.52
        a = 1.186e-5
        b = 86.54
    elif self.cfg.atm_base == 'H2O':
        na = 1.5
        a = 1.6e-5
        b = 0
    elif self.cfg.atm_base == 'O2':
        na = 1.46
        a = 2.294e-5
        b = 164.4

    dmu = a * data_atm.Tco**na / (b + data_atm.Tco)  # g cm-1 s-1 dynamic viscosity

    for sp in self.cfg.non_gas_sp:
        try:
            rho_p = data_atm.rho_p[sp]
            r_p = data_atm.r_p[sp]

        except (ValueError, KeyError):
            raise ValueError(sp + ' has not been prescribed size and density!')

        # Calculating the setteling (terminal) velocity
        gi = 0.5 * (data_atm.g + np.roll(data_atm.g, -1))
        gi = gi[:-1]
        data_atm.vs[:, species.index(sp)] = -1.0 * (2.0 / 9 * rho_p * r_p**2 * gi / dmu[1:])

sp_sat(atm)

Set the saturation vapour pressure of each species in Config.condense_sp.

Fits by species: H2O over ice and water (Ackerman & Marley 2001), NH3 (Weast 1971), H2SO4 (Ayers), S2 and S8 (Zahnle 2017, refitted from Lyons 2008), C (NIST) and H2S over ice and liquid (Giauque & Blue 1936).

Parameters:

Name Type Description Default
atm AtmData

Reads Tco [K]; sets sat_p[sp], shape (nz,) [dyn cm-2].

required

Raises:

Type Description
IOError

If a species has no saturation data.

Source code in src/vulcan/build_atm.py
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
def sp_sat(self, atm):
    """
    Set the saturation vapour pressure of each species in `Config.condense_sp`.

    Fits by species: H2O over ice and water (Ackerman & Marley 2001), NH3 (Weast 1971),
    H2SO4 (Ayers), S2 and S8 (Zahnle 2017, refitted from Lyons 2008), C (NIST) and
    H2S over ice and liquid (Giauque & Blue 1936).

    Parameters
    ----------
    atm : store.AtmData
        Reads `Tco` [K]; sets `sat_p[sp]`, shape (nz,) [dyn cm-2].

    Raises
    ------
    IOError
        If a species has no saturation data.
    """
    # the list that the data has been coded
    sat_sp_list = ['H2O', 'NH3', 'H2SO4', 'S2', 'S8', 'C', 'H2S']

    nz = self.cfg.nz

    for sp in self.cfg.condense_sp:
        if sp not in sat_sp_list:
            raise IOError(
                'No saturation vapor data for '
                + sp
                + '. Check the sp_sat function in build_atm.py'
            )

        T = np.copy(atm.Tco)

        if sp == 'H2O':
            # T is in C
            T -= 273.0
            # from Seinfeld & Pandis 2006, P in mbar in the book
            # a_water = (
            #     6.107799961,
            #     4.436518521e-1,
            #     1.428945805e-2,
            #     2.650648471e-4,
            #     3.031240396e-6,
            #     2.034080948e-8,
            #     6.136820929e-11,
            # )
            # a_ice = (
            #     6.109177956,
            #     5.034698970e-1,
            #     1.886013408e-2,
            #     4.176223716e-4,
            #     5.824720280e-6,
            #     4.838803174e-8,
            #     1.838826904e-10,
            # )

            # saturate_p_1 = (T<0)*( a_ice[0] + a_ice[1]*T + a_ice[2]*T**2 + a_ice[3]*T**3 + a_ice[4]*T**4 + a_ice[5]*T**5 + a_ice[6]*T**6 ) +\
            #  (T>0)*(a_water[0] + a_water[1]*T + a_water[2]*T**2 + a_water[3]*T**3 + a_water[4]*T**4 + a_water[5]*T**5 + a_water[6]*T**6)

            # ice from Ackerman&Marley (2001)
            c0 = 6111.5
            c1 = 23.036
            c2 = -333.7
            c3 = 279.82
            # water from Ackerman&Marley (2001)
            w0 = 6112.1
            w1 = 18.729
            w2 = -227.3
            w3 = 257.87

            saturate_p = (T < 0) * (c0 * np.exp((c1 * T + T**2 / c2) / (T + c3))) + (
                T > 0
            ) * (w0 * np.exp((w1 * T + T**2 / w2) / (T + w3)))

            atm.sat_p[sp] = saturate_p
            # atm.sat_p[sp] = c0 * np.exp( (c1*T + T**2/c2)/(T + c3) )
            # atm.sat_p[sp] = moses_ice

        elif sp == 'NH3':
            # from Weast (1971) in bar
            c0 = 10.53
            c1 = -2161.0
            c2 = -86596.0
            saturate_p = np.exp(c0 + c1 / T + c2 / T**2)
            atm.sat_p[sp] = saturate_p * 1.0e6

        elif sp == 'H2SO4':
            # change to Kulmala later
            p_ayers = np.e ** (-10156.0 / T + 16.259)  # in atm
            atm.sat_p[sp] = p_ayers * 1.01325 * 1e6  # arm to cgs

        elif sp == 'S2':
            atm.sat_p[sp] = np.zeros(nz)
            # from Zahnle 2017 (refitted from Lyons 2008)
            atm.sat_p[sp][T < 413] = (
                np.exp(27.0 - 18500.0 / T[T < 413]) * 1e6
            )  # in bar => cgs
            atm.sat_p[sp][T >= 413] = np.exp(16.1 - 14000.0 / T[T >= 413]) * 1e6

        elif sp == 'S4':
            atm.sat_p[sp] = np.zeros(nz)
            # from Lyons 2008
            atm.sat_p[sp] = 10 ** (6.0028 - 6047.5 / T) * 1.01325e6  # atm to vgs

        elif sp == 'S8':
            atm.sat_p[sp] = np.zeros(nz)
            # from Zahnle 2017 (refitted from Lyons 2008)
            atm.sat_p[sp][T < 413] = (
                np.exp(20.0 - 11800.0 / T[T < 413]) * 1e6
            )  # in bar => cgs
            atm.sat_p[sp][T >= 413] = np.exp(9.6 - 7510.0 / T[T >= 413]) * 1e6

        elif sp == 'C':
            atm.sat_p[sp] = np.zeros(nz)
            # from NIST (dyna cm^-2)
            a = 3.27860e01
            b = -8.65139e04
            c = 4.80395e-01
            atm.sat_p[sp] = np.exp(a + b / (atm.Tco + c))

        elif sp == 'H2S':
            # from Giauque and Blue(1936) in cmHg (adoped in Atreya's book)
            h2s_ice_log10 = -1329.0 / T + 9.28588 - 0.0051263 * T  # 164.9 <= T <= 187.6
            h2s_l_log10 = -1145.0 / T + 7.94746 - 0.00322 * T  # 187.6 < T <= 213.2

            saturate_p = 10 ** ((T <= 187.6) * h2s_ice_log10 + (T > 187.6) * h2s_l_log10)
            atm.sat_p[sp] = saturate_p * 0.001333 * 1.0e6

InitialAbun(vulcan_cfg)

Bases: object

Initial composition of the atmosphere, chosen by Config.ini_mix.

The options are 'eq' (chemical equilibrium from FastChem), 'vulcan_ini' (the result of a previous run), 'table' (mixing ratios from a text file), 'const_mix' (constant mixing ratios) and 'const_lowt' (H2, H2O, CH4, NH3 and He solved from the elemental ratios).

vulcan_cfg : Config Run configuration. ini_mix is converted to lower case in place.

Source code in src/vulcan/build_atm.py
87
88
89
90
91
92
93
94
95
96
97
def __init__(self, vulcan_cfg: Config):
    """
    Parameters
    ----------
    vulcan_cfg : Config
        Run configuration. `ini_mix` is converted to lower case in place.
    """
    self.cfg = vulcan_cfg
    self.ini_m = [0.9, 0.1, 0.0, 0.0, 0]  # initial guess
    self.atom_list = vulcan_cfg.atom_list
    self.cfg.ini_mix = self.cfg.ini_mix.lower()  # normalise string to lowercase

atom_list = vulcan_cfg.atom_list instance-attribute

cfg = vulcan_cfg instance-attribute

ini_m = [0.9, 0.1, 0.0, 0.0, 0] instance-attribute

abun_highT(x)

Residuals of the high-temperature composition, for scipy.optimize.fsolve.

Not called by the current code; ini_mol uses abun_lowT only.

Parameters:

Name Type Description Default
x sequence of float

Mixing ratios of H2, H2O, CO, He and N2 [dimensionless].

required

Returns:

Type Description
tuple of float

Residuals of x1 + x2 + x3 + x4 = 1 and of the O/H, C/H, He/H and N/H number ratios.

Source code in src/vulcan/build_atm.py
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
def abun_highT(self, x):
    """
    Residuals of the high-temperature composition, for `scipy.optimize.fsolve`.

    Not called by the current code; `ini_mol` uses `abun_lowT` only.

    Parameters
    ----------
    x : sequence of float
        Mixing ratios of H2, H2O, CO, He and N2 [dimensionless].

    Returns
    -------
    tuple of float
        Residuals of x1 + x2 + x3 + x4 = 1 and of the O/H, C/H, He/H and N/H number
        ratios.
    """
    O_H, C_H, He_H, N_H = self.cfg.O_H, self.cfg.C_H, self.cfg.He_H, self.cfg.N_H
    x1, x2, x3, x4, x5 = x
    f1 = x1 + x2 + x3 + x4 - 1.0
    f2 = x2 + x3 - (2 * x1 + 2 * x2) * O_H
    f3 = x3 - (2 * x1 + 2 * x2) * C_H
    f4 = x4 - (2 * x1 + 2 * x2) * He_H
    f5 = x5 * 2 - (2 * x1 + 2 * x2) * N_H
    return f1, f2, f3, f4, f5

abun_lowT(x)

Residuals of the low-temperature composition, for scipy.optimize.fsolve.

Parameters:

Name Type Description Default
x sequence of float

Mixing ratios of H2, H2O, CH4, He and NH3 [dimensionless].

required

Returns:

Type Description
tuple of float

Residuals of x1 + x2 + x3 + x4 = 1 and of the O/H, C/H, He/H and N/H number ratios (Config.O_H, C_H, He_H, N_H).

Source code in src/vulcan/build_atm.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
def abun_lowT(self, x):
    """
    Residuals of the low-temperature composition, for `scipy.optimize.fsolve`.

    Parameters
    ----------
    x : sequence of float
        Mixing ratios of H2, H2O, CH4, He and NH3 [dimensionless].

    Returns
    -------
    tuple of float
        Residuals of x1 + x2 + x3 + x4 = 1 and of the O/H, C/H, He/H and N/H number
        ratios (`Config.O_H`, `C_H`, `He_H`, `N_H`).
    """
    O_H, C_H, He_H, N_H = self.cfg.O_H, self.cfg.C_H, self.cfg.He_H, self.cfg.N_H
    x1, x2, x3, x4, x5 = x
    f1 = x1 + x2 + x3 + x4 - 1.0
    f2 = x2 - (2 * x1 + 2 * x2 + 4 * x3 + 3 * x5) * O_H
    f3 = x3 - (2 * x1 + 2 * x2 + 4 * x3 + 3 * x5) * C_H
    f4 = x4 - (2 * x1 + 2 * x2 + 4 * x3 + 3 * x5) * He_H
    f5 = x5 - (2 * x1 + 2 * x2 + 4 * x3 + 3 * x5) * N_H
    return f1, f2, f3, f4, f5

ele_sum(data_var)

Record the initial element totals for the conservation check.

Sets atom_ini[atom], the sum over species and layers of the number of atoms times the number density [cm-3], and resets atom_loss and atom_conden to 0.

Parameters:

Name Type Description Default
data_var Variables

Reads y; filled in place.

required

Returns:

Type Description
Variables

data_var.

Source code in src/vulcan/build_atm.py
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
def ele_sum(self, data_var):
    """
    Record the initial element totals for the conservation check.

    Sets `atom_ini[atom]`, the sum over species and layers of the number of atoms times
    the number density [cm-3], and resets `atom_loss` and `atom_conden` to 0.

    Parameters
    ----------
    data_var : store.Variables
        Reads `y`; filled in place.

    Returns
    -------
    store.Variables
        `data_var`.
    """

    for atom in self.atom_list:
        data_var.atom_ini[atom] = np.sum(
            [compo[compo_row.index(species[i])][atom] * data_var.y[:, i] for i in range(ni)]
        )
        data_var.atom_loss[atom] = 0.0
        data_var.atom_conden[atom] = 0.0

    return data_var

ini_fc(data_var, data_atm)

Run FastChem for the equilibrium composition on the current T-P profile.

Writes the FastChem inputs (elemental abundances and the T-P profile in bar and K) inside paths.FASTCHEM_DIR and runs FastChem there; ini_y reads the result from output/vulcan_EQ.dat. With Config.use_solar = False, the elements in atom_list take the ratios <X>_H from the configuration and the others are scaled by fastchem_met_scale.

Parameters:

Name Type Description Default
data_var Variables

Not used.

required
data_atm AtmData

Reads pco [dyn cm-2] and Tco [K].

required

Raises:

Type Description
RuntimeError

If the FastChem executable is missing.

Source code in src/vulcan/build_atm.py
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
def ini_fc(self, data_var, data_atm):
    """
    Run FastChem for the equilibrium composition on the current T-P profile.

    Writes the FastChem inputs (elemental abundances and the T-P profile in bar and K)
    inside `paths.FASTCHEM_DIR` and runs FastChem there; `ini_y` reads the result from
    `output/vulcan_EQ.dat`. With `Config.use_solar = False`, the elements in `atom_list`
    take the ratios `<X>_H` from the configuration and the others are scaled by
    `fastchem_met_scale`.

    Parameters
    ----------
    data_var : store.Variables
        Not used.
    data_atm : store.AtmData
        Reads `pco` [dyn cm-2] and `Tco` [K].

    Raises
    ------
    RuntimeError
        If the FastChem executable is missing.
    """

    # check that fastchem is compiled in the correct place
    if not os.path.isfile(os.path.join(FASTCHEM_DIR, 'fastchem')):
        raise RuntimeError(
            f'FastChem cannot be found. Try compiling it by running `make` inside {FASTCHEM_DIR}'
        )

    # reading-in the default elemental abundances from Lodders 2009
    # depending on including ion or not (whether there is e- in the fastchem elemental abundance dat)
    solar_ele = os.path.join(FASTCHEM_DIR, 'input', 'solar_element_abundances.dat')
    if self.cfg.use_ion:
        copyfile(
            os.path.join(FASTCHEM_DIR, 'input', 'parameters_ion.dat'),
            os.path.join(FASTCHEM_DIR, 'input', 'parameters.dat'),
        )
    else:
        copyfile(
            os.path.join(FASTCHEM_DIR, 'input', 'parameters_wo_ion.dat'),
            os.path.join(FASTCHEM_DIR, 'input', 'parameters.dat'),
        )

    with open(solar_ele, 'r') as f:
        new_str = ''
        ele_list = list(self.cfg.atom_list)
        ele_list.remove('H')

        fc_list = [
            'C',
            'N',
            'O',
            'S',
            'P',
            'Si',
            'Ti',
            'V',
            'Cl',
            'K',
            'Na',
            'Mg',
            'F',
            'Ca',
            'Fe',
        ]

        if self.cfg.use_solar:
            new_str = f.read()  # read in as a string
            log.info('Initializing with the default solar abundance.')

        else:  # using costomized elemental abundances
            log.info('Initializing with the customized elemental abundance:')
            log.info('{:4}'.format('H') + str('1.'))
            for line in f.readlines():
                li = line.split()
                sp = li[0].strip()

                if sp in ele_list:
                    # read-in vulcan_cfg.sp_H
                    sp_abun = getattr(self.cfg, sp + '_H')
                    fc_abun = 12.0 + np.log10(sp_abun)
                    line = sp + '\t' + '{0:.4f}'.format(fc_abun) + '\n'
                    log.info('{:4}'.format(sp) + '{0:.4E}'.format(sp_abun))

                elif sp in fc_list:  # other elements included in fastchem but not in VULCAN
                    sol_ratio = li[1].strip()
                    if hasattr(
                        self.cfg, 'fastchem_met_scale'
                    ):  # vulcan_cfg.fastchem_met_scale exists
                        met_scale = self.cfg.fastchem_met_scale
                    else:
                        met_scale = 1.0
                        log.warning(
                            'fastchem_met_scale not specified in vulcan_cfg. Using solar metallicity for other elements not included in vulcan.'
                        )

                    new_ratio = float(sol_ratio) + np.log10(met_scale)
                    line = sp + '\t' + '{0:.4f}'.format(new_ratio) + '\n'

                new_str += line

        # make the new elemental abundance file
        with open(
            os.path.join(FASTCHEM_DIR, 'input', 'element_abundances_vulcan.dat'), 'w'
        ) as f:
            f.write(new_str)

    # write a T-P text file for fast_chem
    vulcan_TP_dir = os.path.join(FASTCHEM_DIR, 'input', 'vulcan_TP')
    os.makedirs(vulcan_TP_dir, exist_ok=True)
    with open(os.path.join(vulcan_TP_dir, 'vulcan_TP.dat'), 'w') as f:
        ost = '#p (bar)    T (K)\n'
        for n, p in enumerate(data_atm.pco):  # p in bar in fast_chem
            ost += (
                '{:.3e}'.format(p / 1.0e6) + '\t' + '{:.1f}'.format(data_atm.Tco[n]) + '\n'
            )
        ost = ost[:-1]
        f.write(ost)

    # run fastchem
    try:
        fc_cmd = [
            os.path.join(FASTCHEM_DIR, 'fastchem'),
            os.path.join(FASTCHEM_DIR, 'input', 'config.input'),
        ]
        log.debug(f'Fastchem command: {fc_cmd}')
        process = subprocess.Popen(
            fc_cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            cwd=FASTCHEM_DIR,
        )

        def check_io():
            """Log each line FastChem has written to stdout, until none is waiting."""
            while True:
                out_std = process.stdout.readline().decode().strip()
                if out_std:
                    log.log(logging.INFO, out_std)
                else:
                    break

        # keep checking stdout/stderr until the child exits
        while process.poll() is None:
            check_io()

    except subprocess.CalledProcessError:
        raise RuntimeError(
            f'FastChem cannot run properly. Try compiling it by running `make` inside {FASTCHEM_DIR}'
        )

ini_mol()

Solve abun_lowT for ini_mix = 'const_lowt'.

Returns:

Type Description
ndarray or None

Mixing ratios of H2, H2O, CH4, He and NH3 [dimensionless], or None for any other ini_mix.

Source code in src/vulcan/build_atm.py
149
150
151
152
153
154
155
156
157
158
159
160
def ini_mol(self):
    """
    Solve `abun_lowT` for `ini_mix = 'const_lowt'`.

    Returns
    -------
    np.ndarray or None
        Mixing ratios of H2, H2O, CH4, He and NH3 [dimensionless], or None for any
        other `ini_mix`.
    """
    if self.cfg.ini_mix == 'const_lowt':
        return np.array(sop.fsolve(self.abun_lowT, self.ini_m))

ini_y(data_var, data_atm)

Set the initial number densities and mixing ratios for Config.ini_mix.

With Config.use_condense and use_ini_cold_trap (not for ini_mix = 'table'), each condensing species is capped at saturation and held at its cold-trap value above the cold trap.

Parameters:

Name Type Description Default
data_var Variables

Sets y and y_ini [cm-3], ymix and, with use_ion, charge_list.

required
data_atm AtmData

Reads pco [dyn cm-2], Tco [K], M and n_0 [cm-3], and with condensation sat_p [dyn cm-2]; sets sat_mix and conden_min_lev.

required

Returns:

Type Description
Variables

data_var.

Raises:

Type Description
ValueError

If a 'table' file has a different number of layers, or use_ion is set and no charged species were found (always the case for ini_mix = 'table').

IOError

If ini_mix is not recognised.

Source code in src/vulcan/build_atm.py
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
def ini_y(self, data_var, data_atm):
    """
    Set the initial number densities and mixing ratios for `Config.ini_mix`.

    With `Config.use_condense` and `use_ini_cold_trap` (not for `ini_mix = 'table'`),
    each condensing species is capped at saturation and held at its cold-trap value
    above the cold trap.

    Parameters
    ----------
    data_var : store.Variables
        Sets `y` and `y_ini` [cm-3], `ymix` and, with `use_ion`, `charge_list`.
    data_atm : store.AtmData
        Reads `pco` [dyn cm-2], `Tco` [K], `M` and `n_0` [cm-3], and with condensation
        `sat_p` [dyn cm-2]; sets `sat_mix` and `conden_min_lev`.

    Returns
    -------
    store.Variables
        `data_var`.

    Raises
    ------
    ValueError
        If a 'table' file has a different number of layers, or `use_ion` is set and no
        charged species were found (always the case for `ini_mix = 'table'`).
    IOError
        If `ini_mix` is not recognised.
    """

    nz = self.cfg.nz
    ini_mol = self.ini_mol()
    ini = np.zeros(ni)
    y_ini = data_var.y
    gas_tot = data_atm.M
    charge_list = []  # list of charged species excluding echarge_list

    if self.cfg.ini_mix == 'eq':
        self.ini_fc(data_var, data_atm)
        fc = np.genfromtxt(
            os.path.join(FASTCHEM_DIR, 'output', 'vulcan_EQ.dat'),
            names=True,
            dtype=None,
            skip_header=0,
        )
        for sp in species:
            # Atomic P hack (genfromtxt gets the Pressure as index otherwise)
            if sp == 'P':
                y_ini[:, species.index(sp)] = (
                    fc['P_1'] * gas_tot
                )  # this also changes data_var.y because the address of y array has passed to y_ini
                # print(sp,y_ini[:,species.index(sp)]/gas_tot)
                continue
            if sp in fc.dtype.names:
                y_ini[:, species.index(sp)] = (
                    fc[sp] * gas_tot
                )  # this also changes data_var.y because the address of y array has passed to y_ini

            else:
                log.warning(sp + ' not included in fastchem.')

            if self.cfg.use_ion:
                if compo[compo_row.index(sp)]['e'] != 0:
                    charge_list.append(sp)

        # remove the fc output
        subprocess.call(['rm', 'vulcan_EQ.dat'], cwd=os.path.join(FASTCHEM_DIR, 'output'))

    elif self.cfg.ini_mix == 'vulcan_ini':
        log.info('Initializing with compositions from pickle file ' + self.cfg.vul_ini)
        with open(self.cfg.vul_ini, 'rb') as handle:
            vul_data = pickle.load(handle)

        # y_ini = np.copy(vul_data['variable']['y'])
        # data_var.y = np.copy(y_ini)
        for sp in species:
            if sp in vul_data['variable']['species']:
                y_ini[:, species.index(sp)] = vul_data['variable']['y'][
                    :, vul_data['variable']['species'].index(sp)
                ]
            else:
                log.warning(sp + ' not included in the input mixing ratios file.')

        # if vulcan_cfg.use_ion : charge_list = vul_data['variable']['charge_list']
        if self.cfg.use_ion:
            for sp in species:
                if compo[compo_row.index(sp)]['e'] != 0:
                    charge_list.append(sp)

    elif self.cfg.ini_mix == 'table':
        log.info('Initializing with compositions from ASCII file ' + self.cfg.vul_ini)
        table = np.genfromtxt(self.cfg.vul_ini, names=True, dtype=None, skip_header=1)
        if not len(data_atm.pco) == len(table['Pressure']):
            raise ValueError(
                'The input profile has different layers than the current setting'
            )
        for sp in species:
            try:
                arr = data_atm.n_0 * table[sp]
            except (ValueError, KeyError):
                arr = np.zeros(len(data_atm.pco))
            data_var.y[:, species.index(sp)] = arr

    elif self.cfg.ini_mix == 'const_mix':
        log.info(
            'Initializing with constant (well-mixed) abundances: ' + str(self.cfg.const_mix)
        )
        for sp in self.cfg.const_mix.keys():
            y_ini[:, species.index(sp)] = (
                gas_tot * self.cfg.const_mix[sp]
            )  # this also changes data_var.y
        if self.cfg.use_ion:
            for sp in species:
                if compo[compo_row.index(sp)]['e'] != 0:
                    charge_list.append(sp)

    else:
        for i in range(nz):
            if self.cfg.ini_mix == 'const_lowt':
                y_ini[i, :] = ini
                y_ini[i, species.index('H2')] = ini_mol[0] * gas_tot[i]
                y_ini[i, species.index('H2O')] = ini_mol[1] * gas_tot[i]
                y_ini[i, species.index('CH4')] = ini_mol[2] * gas_tot[i]
                y_ini[i, species.index('NH3')] = ini_mol[4] * gas_tot[i]
                # assign rest of the particles to He
                y_ini[i, species.index('He')] = gas_tot[i] - np.sum(y_ini[i, :])

            else:
                raise IOError(
                    'Initial mixing ratios unknown. Check the setting in vulcan_cfg.py.'
                )

    if self.cfg.use_condense:
        for sp in self.cfg.condense_sp:
            data_atm.sat_mix[sp] = saturation_mixing_ratio(self.cfg, data_atm, sp)

            if self.cfg.use_ini_cold_trap:
                if self.cfg.ini_mix != 'table' and self.cfg.ini_mix != 'vul_ini':
                    # the level where condensation starts
                    conden_bot = np.argmax(
                        data_atm.n_0 * data_atm.sat_mix[sp]
                        <= data_var.y[:, species.index(sp)]
                    )
                    # conden_status: ture if the partial p >= the saturation p
                    sat_rho = data_atm.n_0 * data_atm.sat_mix[sp]
                    conden_status = data_var.y[:, species.index(sp)] >= sat_rho

                    # take the min between the mixing ratio and the saturation mixing ratio
                    data_var.y[:, species.index(sp)] = np.minimum(
                        data_atm.n_0 * data_atm.sat_mix[sp],
                        data_var.y[:, species.index(sp)],
                    )

                    if list(
                        data_var.y[conden_status, species.index(sp)]
                    ):  # if it condenses
                        min_sat = np.amin(
                            data_atm.sat_mix[sp][conden_status]
                        )  # the mininum value of the saturation p within the saturation region
                        conden_min_lev = np.where(data_atm.sat_mix[sp] == min_sat)[0][0]

                        data_atm.conden_min_lev = conden_min_lev

                        log.debug(
                            sp
                            + ' condensed from nz = '
                            + str(conden_bot)
                            + ' to the minimum level nz = '
                            + str(conden_min_lev)
                            + ' (cold trap)'
                        )
                        # data_var.y[conden_min_lev:,species.index(sp)] = (y_ini[conden_min_lev,species.index(sp)]/data_atm.n_0[conden_min_lev]) *data_atm.n_0[conden_min_lev:]
                        data_var.y[conden_min_lev:, species.index(sp)] = (
                            data_atm.sat_mix[sp][conden_min_lev]
                            * data_atm.n_0[conden_min_lev:]
                        )

    # re-normalisation
    # TEST
    # Excluding the non-gaseous species
    if self.cfg.use_condense:
        exc_conden = [_ for _ in range(ni) if species[_] not in self.cfg.non_gas_sp]
        ysum = np.sum(y_ini[:, exc_conden], axis=1).reshape((-1, 1))
    else:
        ysum = np.sum(y_ini, axis=1).reshape((-1, 1))

    data_var.y_ini = np.copy(y_ini)
    data_var.ymix = y_ini / ysum

    if self.cfg.use_ion:
        # if the charge_list is empty (no species with nonzero charges include)
        if not charge_list:
            raise ValueError(
                'vulcan_cfg.use_ion = True but the network with ions is not supplied.'
            )
        else:
            if 'e' in charge_list:
                charge_list.remove('e')
            data_var.charge_list = charge_list

    return data_var