uclchem.makerates.io_functions ============================== .. py:module:: uclchem.makerates.io_functions .. autoapi-nested-parse:: Functions to read in the species and reaction files and write output files Module Contents --------------- Functions ~~~~~~~~~ .. autoapisummary:: uclchem.makerates.io_functions.array_to_string uclchem.makerates.io_functions.build_ode_string uclchem.makerates.io_functions.check_reaction uclchem.makerates.io_functions.find_reactant uclchem.makerates.io_functions.get_desorption_freeze_partners uclchem.makerates.io_functions.kida_parser uclchem.makerates.io_functions.output_drops uclchem.makerates.io_functions.read_grain_assisted_recombination_file uclchem.makerates.io_functions.read_reaction_file uclchem.makerates.io_functions.read_species_file uclchem.makerates.io_functions.species_ode_string uclchem.makerates.io_functions.truncate_line uclchem.makerates.io_functions.write_evap_lists uclchem.makerates.io_functions.write_f90_constants uclchem.makerates.io_functions.write_jacobian uclchem.makerates.io_functions.write_network_file uclchem.makerates.io_functions.write_odes_f90 uclchem.makerates.io_functions.write_outputs uclchem.makerates.io_functions.write_python_constants uclchem.makerates.io_functions.write_reactions uclchem.makerates.io_functions.write_species .. py:function:: array_to_string(name: str, array: numpy.array, type: str = 'int', parameter: bool = True) -> str Write an array to fortran source code :param name: Variable name of array in Fortran :type name: str :param array: List of values of array :type array: iterable :param type: The array's type. Must be one of "int","float", or "string".Defaults to "int". :type type: str, optional :param parameter: Whether the array is a Fortran PARAMETER (constant). Defaults to True. :type parameter: bool, optional :raises ValueError: Raises an error if type isn't "int","float", or "string" :returns: String containing the Fortran code to declare this array. :rtype: str .. py:function:: build_ode_string(species_list: list[uclchem.makerates.species.Species], reaction_list: list[uclchem.makerates.reaction.Reaction], rates_to_disk: bool = False) -> str A long, complex function that does the messy work of creating the actual ODE code to calculate the rate of change of each species. Test any change to this code thoroughly because ODE mistakes are very hard to spot. :param species_list: List of species in network :type species_list: list :param reaction_list: List of reactions in network :type reaction_list: list :param rates_to_disk: Enable the writing of the rates to the disk. :type rates_to_disk: bool :returns: One long string containing the entire ODE fortran code. :rtype: str .. py:function:: check_reaction(reaction_row, keep_list) -> bool Checks a row parsed from a reaction file and checks it only contains acceptable things. It checks if all species in the reaction are present, and adds the temperature range is none is specified. :param reaction_row: List parsed from a reaction file and formatted to be able to called Reaction(reaction_row) :type reaction_row: list :param keep_list: list of elements that are acceptable in the reactant or product bits of row :type keep_list: list :returns: Whether the row contains acceptable entries. :rtype: bool .. py:function:: find_reactant(species_list: list[str], reactant: str) -> int Try to find a reactant in the species list :param species_list: A list of species in the network :type species_list: list[str] :param reactant: The reactant to be indexed :type reactant: str :returns: The index of the reactant, if it is not found, 9999 :rtype: int .. py:function:: get_desorption_freeze_partners(reaction_list: list[uclchem.makerates.reaction.Reaction]) -> list[uclchem.makerates.reaction.Reaction] Every desorption has a corresponding freeze out eg desorption of #CO and freeze of CO. This find the corresponding freeze out for every desorb so that when desorb>>freeze we can turn off freeze out in UCLCHEM. :param reaction_list: Reactions in network :type reaction_list: list :returns: list of indices of freeze out reactions matching order of desorptions. :rtype: list .. py:function:: kida_parser(kida_file) KIDA used a fixed format file so we read each line in the chunks they specify and use python built in classes to convert to the necessary types. NOTE KIDA defines some of the same reaction types to UMIST but with different names and coefficients. We fix that by converting them here. .. py:function:: output_drops(dropped_reactions: list[uclchem.makerates.reaction.Reaction], output_dir: str = None, write_files: bool = True) Writes the reactions that are dropped to disk/logs :param dropped_reactions: The reactions that were dropped :type dropped_reactions: list[Reaction] :param output_dir: The directory that dropped_reactions.csv will be written to. :type output_dir: str :param write_files: Whether or not to write the file. Defaults to True. :type write_files: bool, optional .. py:function:: read_grain_assisted_recombination_file(file_name: pathlib.Path) -> dict .. py:function:: read_reaction_file(file_name: pathlib.Path, species_list: list[uclchem.makerates.species.Species], ftype: str) -> tuple[list[uclchem.makerates.reaction.Reaction], list[uclchem.makerates.reaction.Reaction]] Reads in a reaction file of any kind (user, UMIST, KIDA) produces a list of reactions for the network, filtered by species_list :param file_name: A file name for the reaction file to read. :type file_name: str :param species_list: A list of chemical species to be used in the reading. :type species_list: list[Species] :param ftype: 'UMIST','UCL', or 'KIDA' to describe format of file_name :type ftype: str :returns: Lists of kept and dropped reactions. :rtype: list,list .. py:function:: read_species_file(file_name: pathlib.Path) -> list[uclchem.makerates.species.Species] Reads in a Makerates species file :param fileName: path to file containing the species list :type fileName: str :returns: List of Species objects :rtype: list .. py:function:: species_ode_string(n: int, species: uclchem.makerates.species.Species) -> str Build the string of Fortran code for a species once it's loss and gains strings have been produced. :param n: Index of species in python format :type n: int :param species: species object :type species: Species :returns: the fortran code for the rate of change of the species :rtype: str .. py:function:: truncate_line(input_string: str, lineLength: int = 72) -> str Take a string and adds line endings at regular intervals keeps us from overshooting fortran's line limits and, frankly, makes for nicer ode.f90 even if human readability isn't very important :param input_string: Line of code to be truncated :type input_string: str :param lineLength: rough line length. Defaults to 72. :type lineLength: int, optional :returns: Code string with line endings at regular intervals :rtype: str .. py:function:: write_evap_lists(network_file, species_list: list[uclchem.makerates.species.Species]) -> int Two phase networks mimic episodic thermal desorption seen in lab (see Viti et al. 2004) by desorbing fixed fractions of material at specific temperatures. Three phase networks just use binding energy and that fact we set binding energies in bulk to water by default. This function writes all necessary arrays to the network file so these processes work. :param network_file: Open file object to which the network code is being written :type network_file: file :param species_list: List of species in network :type species_list: list[Species] .. py:function:: write_f90_constants(replace_dict: Dict[str, int], output_file_name: pathlib.Path, template_file_path: pathlib.Path = 'fortran_templates') -> None Write the physical reactions to the f2py_constants.f90 file after every run of makerates, this ensures the Fortran and Python bits are compatible with one another. :param replace_dict: The dictionary with keys to replace and their values :type replace_dict: Dict[str, int] :param output_file_name: The path to the target f2py_constants.f90 file :type output_file_name: Path :param template_file_path: The file to use as the template. Defaults to "fortran_templates". :type template_file_path: Path, optional .. py:function:: write_jacobian(file_name: pathlib.Path, species_list: list[uclchem.makerates.species.Species]) -> None Write jacobian in Modern Fortran. This has never improved UCLCHEM's speed and so is not used in the code as it stands. Current only works for three phase model. :param file_name: Path to jacobian file :type file_name: str :param species_list: List of species AFTER being processed by build_ode_string :type species_list: species_list .. py:function:: write_network_file(file_name: pathlib.Path, network: uclchem.makerates.network.Network, rates_to_disk: bool = False, gar_database=None) Write the Fortran code file that contains all network information for UCLCHEM. This includes lists of reactants, products, binding energies, formationEnthalpies and so on. :param file_name: The file name where the code will be written. :type file_name: str :param network: A Network object built from lists of species and reactions. :type network: Network .. py:function:: write_odes_f90(file_name: pathlib.Path, species_list: list[uclchem.makerates.species.Species], reaction_list: list[uclchem.makerates.reaction.Reaction], rates_to_disk: bool = False) -> None Write the ODEs in Modern Fortran. This is an actual code file. :param file_name: Path to file where code will be written :type file_name: str :param species_list: List of species describing network :type species_list: list :param reaction_list: List of reactions describing network :type reaction_list: list .. py:function:: write_outputs(network: uclchem.makerates.network.Network, output_dir: str = None, rates_to_disk: bool = False, gar_database: dict[str, numpy.array] = None) -> None Write the ODE and Network fortran source files to the fortran source. :param network: The makerates Network class :type network: network :param output_dir: The directory to write to. :type output_dir: bool .. py:function:: write_python_constants(replace_dict: Dict[str, int], python_constants_file: pathlib.Path) -> None Function to write the python constants to the constants.py file after every run, this ensure the Python and Fortran bits are compatible with one another. :param replace_dict: Dict with keys to replace and their values :type replace_dict: Dict[str, int]] :param python_constants_file: Path to the target constant files. :type python_constants_file: Path .. py:function:: write_reactions(fileName, reaction_list) -> None Write the human readable reaction file. :param fileName: path to output file :type fileName: str :param reaction_list: List of reaction objects for network :type reaction_list: list .. py:function:: write_species(file_name: pathlib.Path, species_list: list[uclchem.makerates.species.Species]) -> None Write the human readable species file. Note UCLCHEM doesn't use this file. :param fileName: path to output file :type fileName: str :param species_list: List of species objects for network :type species_list: list