Source code for GUIBRUSHR.core.config.chemistry

"""
Chemistry configuration module for GUIBRUSHR.

Provides a focused ``ChemistryConfig`` class that loads all chemistry-related
constants and species definitions from the YAML configuration files.  This
covers physical constants, chemical species groups, molecular dictionaries,
hybrid element definitions, and condensed species — everything that was
previously scattered through the ``ConstantVariables`` class under the
"Chemical Species Configuration", "Molecular and Element Configuration", and
condensed-species sections.
"""

from pathlib import Path
from typing import Any, Dict, List

import yaml


def _load_yaml_config(file_path: str) -> Dict[str, Any]:
    """Load and parse a YAML configuration file.

    Parameters
    ----------
    file_path : str
        Absolute path to the YAML file.

    Returns
    -------
    Dict[str, Any]
        Parsed contents of the YAML file.

    Raises
    ------
    RuntimeError
        If the file cannot be opened or parsed.
    """
    try:
        with open(file_path, "r") as f:
            return yaml.safe_load(f)
    except Exception as e:
        raise RuntimeError(
            f"Failed to load YAML configuration {file_path}: {str(e)}"
        )


[docs] class ChemistryConfig: """Chemistry constants and species configuration for GUIBRUSHR. Loads ``general.yaml``, ``molecules.yaml``, ``hybrid_elements.yaml``, and ``condensed.yaml`` from *configuration_yaml_path* and exposes the chemistry-related constants and species dictionaries used throughout the application. Parameters ---------- configuration_yaml_path : str Absolute path to the directory that contains the YAML configuration files (the same value as :attr:`~GUIBRUSHR.General_Constants.FunctionsAndConstants.Constant_Variables.ConstantVariables.configuration_yaml_path`). Attributes ---------- NOT_VMR_METALS : list Species excluded from the VMR metals group. HCNO_NEUTRALS : str Space-separated list of HCNO neutral species. IONS : str Space-separated list of ion species. ALKALI : str Space-separated list of alkali species. METALS : str Space-separated list of metal species. METAL_OXIDES : str Space-separated list of metal-oxide species. LIST_ELEMENT_FOR_HYBRID : List[str] Sorted list of elements used in hybrid abundance calculations. CLIGHT : float Speed of light in km/s. SOLAR_TO_JUPITER_MASSES : float Conversion factor from solar masses to Jupiter masses. AU : float Astronomical unit in metres. R_JUP_MEAN : float Mean Jupiter radius in CGS units. R_JUP : float Jupiter equatorial radius in CGS units. M_JUP : float Jupiter mass in CGS units. R_SUN : float Solar radius in CGS units. M_SUN : float Solar mass in CGS units. G : float Gravitational constant in CGS units. RATIO_RSUN_RJUP : float Ratio R_SUN / R_JUP. RATIO_RSUN_RJUP_MEAN : float Ratio R_SUN / R_JUP_MEAN. ALL_MOLECS_DICT : Dict[str, Dict] Full molecules dictionary loaded from ``molecules.yaml``. ALL_MOLEC : List[str] Ordered list of molecule keys. DICT_LABELS_MOLEC : Dict[str, str] Mapping of molecule key to display label. ELEMENTS_DICT : Dict[str, Dict] Full hybrid-elements dictionary loaded from ``hybrid_elements.yaml``. ALL_ELEMENT : List[str] Ordered list of element keys. DICT_LABELS_ELEMENTS : Dict[str, str] Mapping of element key to display label. ALL_CONDENSED_MOLECS_DICT : Dict[str, Dict] Full condensed-species dictionary loaded from ``condensed.yaml``. ALL_CONDENSED_MOLEC : Dict[str, str] Mapping of condensed species long name to its YAML key. DICT_LABELS_CONDENSED_MOLEC : Dict[str, str] Mapping of condensed species key to display label. """
[docs] def __init__(self, configuration_yaml_path: str) -> None: # ------------------------------------------------------------------ # # Load raw YAML files # # ------------------------------------------------------------------ # general_yaml: Dict[str, Any] = _load_yaml_config( str(Path(configuration_yaml_path, "general.yaml")) ) molecules_yaml: Dict[str, Any] = _load_yaml_config( str(Path(configuration_yaml_path, "molecules.yaml")) ) hybrid_yaml: Dict[str, Any] = _load_yaml_config( str(Path(configuration_yaml_path, "hybrid_elements.yaml")) ) condensed_yaml: Dict[str, Any] = _load_yaml_config( str(Path(configuration_yaml_path, "condensed.yaml")) ) # ------------------------------------------------------------------ # # Chemical species groups # # ------------------------------------------------------------------ # self.NOT_VMR_METALS = general_yaml["not_vmr_metals"] self.HCNO_NEUTRALS: str = general_yaml["HCNO_neutrals"] self.IONS: str = general_yaml["ions"] self.ALKALI: str = general_yaml["alkali"] self.METALS: str = general_yaml["metals"] self.METAL_OXIDES: str = general_yaml["metal_oxides"] self.LIST_ELEMENT_FOR_HYBRID: List[str] = sorted( general_yaml["list_element_for_hybrid"].split() ) # ------------------------------------------------------------------ # # Physical constants # # ------------------------------------------------------------------ # self.CLIGHT: float = float(general_yaml["clight"]) self.SOLAR_TO_JUPITER_MASSES: float = float( general_yaml["solar_to_jupiter_masses"] ) self.AU: float = float(general_yaml["au"]) self.R_JUP_MEAN: float = float(general_yaml["r_jup_mean"]) self.R_JUP: float = float(general_yaml["r_jup"]) self.M_JUP: float = float(general_yaml["m_jup"]) self.R_SUN: float = float(general_yaml["r_sun"]) self.M_SUN: float = float(general_yaml["m_sun"]) self.G: float = float(general_yaml["G"]) self.RATIO_RSUN_RJUP: float = self.R_SUN / self.R_JUP self.RATIO_RSUN_RJUP_MEAN: float = self.R_SUN / self.R_JUP_MEAN # ------------------------------------------------------------------ # # Molecular species # # ------------------------------------------------------------------ # self.ALL_MOLECS_DICT: Dict[str, Dict] = molecules_yaml self.ALL_MOLEC: List[str] = list(self.ALL_MOLECS_DICT.keys()) self.DICT_LABELS_MOLEC: Dict[str, str] = { key: val["label"] for key, val in self.ALL_MOLECS_DICT.items() } # ------------------------------------------------------------------ # # Hybrid elements # # ------------------------------------------------------------------ # self.ELEMENTS_DICT: Dict[str, Dict] = hybrid_yaml self.ALL_ELEMENT: List[str] = list(self.ELEMENTS_DICT.keys()) self.DICT_LABELS_ELEMENTS: Dict[str, str] = { key: val["label"] for key, val in self.ELEMENTS_DICT.items() } # ------------------------------------------------------------------ # # Condensed species (loaded from YAML only; filesystem scanning is # # handled by ConstantVariables._load_molecular_configs) # # ------------------------------------------------------------------ # self.ALL_CONDENSED_MOLECS_DICT: Dict[str, Dict] = condensed_yaml self.ALL_CONDENSED_MOLEC: Dict[str, str] = { val["name"]: key for key, val in condensed_yaml.items() } self.DICT_LABELS_CONDENSED_MOLEC: Dict[str, str] = { key: val["label"] for key, val in condensed_yaml.items() }