uclchem.makerates.io_functions#

Functions to read in the species and reaction files and write output files

Module Contents#

Functions#

array_to_string(→ str)

Write an array to fortran source code

build_ode_string(→ str)

A long, complex function that does the messy work of creating the actual ODE

check_reaction(→ bool)

Checks a row parsed from a reaction file and checks it only contains acceptable things.

find_reactant(→ int)

Try to find a reactant in the species list

get_desorption_freeze_partners(...)

Every desorption has a corresponding freeze out eg desorption of #CO and freeze of CO.

kida_parser(kida_file)

KIDA used a fixed format file so we read each line in the chunks they specify

output_drops(dropped_reactions[, output_dir, write_files])

Writes the reactions that are dropped to disk/logs

read_grain_assisted_recombination_file(→ dict)

read_reaction_file(...)

Reads in a reaction file of any kind (user, UMIST, KIDA)

read_species_file(...)

Reads in a Makerates species file

species_ode_string(→ str)

Build the string of Fortran code for a species once it's loss and gains

truncate_line(→ str)

Take a string and adds line endings at regular intervals

write_evap_lists(→ int)

Two phase networks mimic episodic thermal desorption seen in lab (see Viti et al. 2004)

write_f90_constants(→ None)

Write the physical reactions to the f2py_constants.f90 file after every run of

write_jacobian(→ None)

Write jacobian in Modern Fortran. This has never improved UCLCHEM's speed

write_network_file(file_name, network[, ...])

Write the Fortran code file that contains all network information for UCLCHEM.

write_odes_f90(→ None)

Write the ODEs in Modern Fortran. This is an actual code file.

write_outputs(→ None)

Write the ODE and Network fortran source files to the fortran source.

write_python_constants(→ None)

Function to write the python constants to the constants.py file after every run,

write_reactions(→ None)

Write the human readable reaction file.

write_species(→ None)

Write the human readable species file. Note UCLCHEM doesn't use this file.

uclchem.makerates.io_functions.array_to_string(name: str, array: numpy.array, type: str = 'int', parameter: bool = True) str[source]#

Write an array to fortran source code

Parameters:
  • name (str) – Variable name of array in Fortran

  • array (iterable) – List of values of array

  • type (str, optional) – The array’s type. Must be one of “int”,”float”, or “string”.Defaults to “int”.

  • parameter (bool, optional) – Whether the array is a Fortran PARAMETER (constant). Defaults to True.

Raises:

ValueError – Raises an error if type isn’t “int”,”float”, or “string”

Returns:

String containing the Fortran code to declare this array.

Return type:

str

uclchem.makerates.io_functions.build_ode_string(species_list: list[uclchem.makerates.species.Species], reaction_list: list[uclchem.makerates.reaction.Reaction], rates_to_disk: bool = False) str[source]#

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.

Parameters:
  • species_list (list) – List of species in network

  • reaction_list (list) – List of reactions in network

  • rates_to_disk (bool) – Enable the writing of the rates to the disk.

Returns:

One long string containing the entire ODE fortran code.

Return type:

str

uclchem.makerates.io_functions.check_reaction(reaction_row, keep_list) bool[source]#

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.

Parameters:
  • reaction_row (list) – List parsed from a reaction file and formatted to be able to called Reaction(reaction_row)

  • keep_list (list) – list of elements that are acceptable in the reactant or product bits of row

Returns:

Whether the row contains acceptable entries.

Return type:

bool

uclchem.makerates.io_functions.find_reactant(species_list: list[str], reactant: str) int[source]#

Try to find a reactant in the species list

Parameters:
  • species_list (list[str]) – A list of species in the network

  • reactant (str) – The reactant to be indexed

Returns:

The index of the reactant, if it is not found, 9999

Return type:

int

uclchem.makerates.io_functions.get_desorption_freeze_partners(reaction_list: list[uclchem.makerates.reaction.Reaction]) list[uclchem.makerates.reaction.Reaction][source]#

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.

Parameters:

reaction_list (list) – Reactions in network

Returns:

list of indices of freeze out reactions matching order of desorptions.

Return type:

list

uclchem.makerates.io_functions.kida_parser(kida_file)[source]#

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.

uclchem.makerates.io_functions.output_drops(dropped_reactions: list[uclchem.makerates.reaction.Reaction], output_dir: str = None, write_files: bool = True)[source]#

Writes the reactions that are dropped to disk/logs

Parameters:
  • dropped_reactions (list[Reaction]) – The reactions that were dropped

  • output_dir (str) – The directory that dropped_reactions.csv will be written to.

  • write_files (bool, optional) – Whether or not to write the file. Defaults to True.

uclchem.makerates.io_functions.read_grain_assisted_recombination_file(file_name: pathlib.Path) dict[source]#
uclchem.makerates.io_functions.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]][source]#

Reads in a reaction file of any kind (user, UMIST, KIDA) produces a list of reactions for the network, filtered by species_list

Parameters:
  • file_name (str) – A file name for the reaction file to read.

  • species_list (list[Species]) – A list of chemical species to be used in the reading.

  • ftype (str) – ‘UMIST’,’UCL’, or ‘KIDA’ to describe format of file_name

Returns:

Lists of kept and dropped reactions.

Return type:

list,list

uclchem.makerates.io_functions.read_species_file(file_name: pathlib.Path) list[uclchem.makerates.species.Species][source]#

Reads in a Makerates species file

Parameters:

fileName (str) – path to file containing the species list

Returns:

List of Species objects

Return type:

list

uclchem.makerates.io_functions.species_ode_string(n: int, species: uclchem.makerates.species.Species) str[source]#

Build the string of Fortran code for a species once it’s loss and gains strings have been produced.

Parameters:
  • n (int) – Index of species in python format

  • species (Species) – species object

Returns:

the fortran code for the rate of change of the species

Return type:

str

uclchem.makerates.io_functions.truncate_line(input_string: str, lineLength: int = 72) str[source]#

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

Parameters:
  • input_string (str) – Line of code to be truncated

  • lineLength (int, optional) – rough line length. Defaults to 72.

Returns:

Code string with line endings at regular intervals

Return type:

str

uclchem.makerates.io_functions.write_evap_lists(network_file, species_list: list[uclchem.makerates.species.Species]) int[source]#

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.

Parameters:
  • network_file (file) – Open file object to which the network code is being written

  • species_list (list[Species]) – List of species in network

uclchem.makerates.io_functions.write_f90_constants(replace_dict: Dict[str, int], output_file_name: pathlib.Path, template_file_path: pathlib.Path = 'fortran_templates') None[source]#

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.

Parameters:
  • replace_dict (Dict[str, int]) – The dictionary with keys to replace and their values

  • output_file_name (Path) – The path to the target f2py_constants.f90 file

  • template_file_path (Path, optional) – The file to use as the template. Defaults to “fortran_templates”.

uclchem.makerates.io_functions.write_jacobian(file_name: pathlib.Path, species_list: list[uclchem.makerates.species.Species]) None[source]#

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.

Parameters:
  • file_name (str) – Path to jacobian file

  • species_list (species_list) – List of species AFTER being processed by build_ode_string

uclchem.makerates.io_functions.write_network_file(file_name: pathlib.Path, network: uclchem.makerates.network.Network, rates_to_disk: bool = False, gar_database=None)[source]#

Write the Fortran code file that contains all network information for UCLCHEM. This includes lists of reactants, products, binding energies, formationEnthalpies and so on.

Parameters:
  • file_name (str) – The file name where the code will be written.

  • network (Network) – A Network object built from lists of species and reactions.

uclchem.makerates.io_functions.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[source]#

Write the ODEs in Modern Fortran. This is an actual code file.

Parameters:
  • file_name (str) – Path to file where code will be written

  • species_list (list) – List of species describing network

  • reaction_list (list) – List of reactions describing network

uclchem.makerates.io_functions.write_outputs(network: uclchem.makerates.network.Network, output_dir: str = None, rates_to_disk: bool = False, gar_database: dict[str, numpy.array] = None) None[source]#

Write the ODE and Network fortran source files to the fortran source.

Parameters:
  • network (network) – The makerates Network class

  • output_dir (bool) – The directory to write to.

uclchem.makerates.io_functions.write_python_constants(replace_dict: Dict[str, int], python_constants_file: pathlib.Path) None[source]#

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.

Parameters:
  • replace_dict (Dict[str, int]]) – Dict with keys to replace and their values

  • python_constants_file (Path) – Path to the target constant files.

uclchem.makerates.io_functions.write_reactions(fileName, reaction_list) None[source]#

Write the human readable reaction file.

Parameters:
  • fileName (str) – path to output file

  • reaction_list (list) – List of reaction objects for network

uclchem.makerates.io_functions.write_species(file_name: pathlib.Path, species_list: list[uclchem.makerates.species.Species]) None[source]#

Write the human readable species file. Note UCLCHEM doesn’t use this file.

Parameters:
  • fileName (str) – path to output file

  • species_list (list) – List of species objects for network