uclchem.makerates.network#
Unified Network implementation for UCLCHEM.
This module provides the Network class and factory functions for creating networks in different contexts: - load_network_from_csv(): Load compiled networks for analysis - build_network(): Build new networks with full validation - create_network(): Direct instantiation from objects
The Network class implements a complete interface for accessing and modifying chemical reaction networks, suitable for build-time and analysis-time use.
For runtime parameter modification during model execution, use RuntimeNetwork from uclchem.advanced.runtime_network instead.
Module Contents#
Classes#
Base implementation providing common network operations. |
|
Extended interface for networks that support full CRUD operations. |
|
Universal network representation for build and analysis contexts. |
|
Base abstract class defining the read-only network interface. |
Functions#
|
Build a new network with full validation and automatic generation. |
|
Create a network directly from species and reaction lists. |
|
Load a network from CSV files for analysis. |
Attributes#
- class uclchem.makerates.network.BaseNetwork[source]#
Bases:
NetworkABCBase implementation providing common network operations.
Implements all read and query operations that are common between Network and RuntimeNetwork. Both classes store data in _species_dict and _reactions_dict, so this base class can implement all the shared logic.
Subclasses only need to: 1. Initialize _species_dict and _reactions_dict 2. Implement modification methods (change_binding_energy, change_reaction_barrier) 3. Optionally implement add/remove operations (MutableNetworkABC)
- find_similar_reactions(reaction: uclchem.makerates.reaction.Reaction) dict[int, uclchem.makerates.reaction.Reaction][source]#
Find reactions with same reactants and products.
- get_reaction(reaction_idx: int) uclchem.makerates.reaction.Reaction[source]#
Get a reaction by index (copy).
- get_reaction_dict() dict[int, uclchem.makerates.reaction.Reaction][source]#
Get reactions dictionary (copy).
- get_reaction_index(reaction: uclchem.makerates.reaction.Reaction) int[source]#
Get the index of a reaction in the network.
- Parameters:
reaction (Reaction) – Reaction to find
- Returns:
Index of the reaction
- Return type:
- Raises:
ValueError – If reaction not found or multiple matches exist
- get_reaction_list() list[uclchem.makerates.reaction.Reaction][source]#
Get all reactions as a list.
- get_reactions_by_types(reaction_type: str | list[str]) list[uclchem.makerates.reaction.Reaction][source]#
Get all reactions of specific type(s).
- get_specie(specie_name: str) uclchem.makerates.species.Species[source]#
Get a species by name (copy).
- get_species_dict() dict[str, uclchem.makerates.species.Species][source]#
Get species dictionary (copy).
- get_species_list() list[uclchem.makerates.species.Species][source]#
Get all species as a list.
- property reactions: dict[int, uclchem.makerates.reaction.Reaction][source]#
Get reactions dictionary.
- class uclchem.makerates.network.MutableNetworkABC[source]#
Bases:
NetworkABCExtended interface for networks that support full CRUD operations.
Adds add/remove/set operations for species and reactions on top of the base NetworkABC interface. Used by the Network class for build-time and analysis-time operations.
NOT implemented by RuntimeNetwork since the Fortran-backed network is read directly from the fortran files, only allowing for the editing of existing reaction and species parameters.
- abstractmethod add_reactions(reactions: uclchem.makerates.reaction.Reaction | list) None[source]#
Add one or more reactions to the network.
- abstractmethod add_species(species: uclchem.makerates.species.Species | list) None[source]#
Add one or more species to the network.
- abstractmethod remove_reaction(reaction: uclchem.makerates.reaction.Reaction) None[source]#
Remove a reaction from the network.
- Parameters:
reaction (Reaction) – Reaction instance to look up or modify.
- abstractmethod remove_reaction_by_index(reaction_idx: int) None[source]#
Remove a reaction by its index.
- Parameters:
reaction_idx (int) – Index of the reaction in the network.
- abstractmethod remove_species(specie_name: str) None[source]#
Remove a species from the network.
- Parameters:
specie_name (str) – Name of the species.
- abstractmethod set_reaction(reaction_idx: int, reaction: uclchem.makerates.reaction.Reaction) None[source]#
Set/update a reaction at a specific index.
- abstractmethod set_reaction_dict(new_dict: dict[int, uclchem.makerates.reaction.Reaction]) None[source]#
Replace the entire reaction dictionary.
- abstractmethod set_specie(species_name: str, species: uclchem.makerates.species.Species) None[source]#
Set/update a species in the network.
- class uclchem.makerates.network.Network(species_dict: dict[str, uclchem.makerates.species.Species], reaction_dict: dict[int, uclchem.makerates.reaction.Reaction])[source]#
Bases:
BaseNetwork,MutableNetworkABCUniversal network representation for build and analysis contexts.
A single Network class that serves all use cases: - Build time: Full validation and automatic reaction generation - Analysis time: Fast loading of compiled networks from CSV
The Network class can be created via: - Direct instantiation: Network(species_dict, reaction_dict) - Factory methods: from_csv(), from_lists(), build() - Factory functions: load_network_from_csv(), build_network(), etc.
All creation methods produce a Network instance that implements the full NetworkABC interface, ensuring consistent access patterns.
For runtime parameter modification during model execution, use RuntimeNetwork from uclchem.advanced.runtime_network instead.
Examples
>>> # Load for analysis >>> network = Network.from_csv()
>>> # Build with validation >>> from uclchem.makerates.io_functions import read_species_file, read_reaction_file >>> from uclchem.utils import UCLCHEM_ROOT_DIR >>> >>> species_list, user_defined_bulk = read_species_file( ... UCLCHEM_ROOT_DIR / "../../Makerates/data/default/default_species.csv" ... ) >>> reactions_list, dropped_reactions = read_reaction_file( ... UCLCHEM_ROOT_DIR / "../../Makerates/data/default/default_grain_network.csv", ... species_list, ... "UCL", ... ) >>> network = Network.build( ... species_list, reactions_list, gas_phase_extrapolation=True ... )
Initialize network with species and reactions.
This is the low-level constructor. Most users should prefer factory methods: - Network.from_csv() for analysis - Network.from_lists() for direct instantiation - Network.build() for full build with validation
Or use the module-level factory functions for clearer documentation.
For runtime parameter modification, use RuntimeNetwork from uclchem.advanced.runtime_network instead.
- Parameters:
- add_reactions(reactions: uclchem.makerates.reaction.Reaction | list) None[source]#
Add reactions to network.
- Parameters:
reactions (Reaction | list) – Reaction object, list of Reactions, or CSV-style entries
- Raises:
ValueError – If there is an error when converting the CSV-style entries to Reaction instances
TypeError – If input was not a Reaction, list of Reaction instances, or CSV-style entries.
- add_species(species: uclchem.makerates.species.Species | list) None[source]#
Add species to network.
- Parameters:
species (Species | list) – Species object, list of Species, or CSV-style entries
- Raises:
ValueError – If there is an error when converting the CSV-style entries to Species instances
TypeError – If input was not a Species, list of Species instances, or CSV-style entries.
- classmethod build(species: list[uclchem.makerates.species.Species], reactions: list[uclchem.makerates.reaction.Reaction], **build_options) Network[source]#
Build network with full validation and automatic generation.
This is the primary method for building new networks with full validation, automatic reaction generation (freeze-out, desorption, bulk), branching ratio checks, and all build-time operations. Delegates to NetworkBuilder.
- Parameters:
**build_options (dict) – Options passed to NetworkBuilder: - user_defined_bulk: List of user-defined bulk species - gas_phase_extrapolation: bool (default False) - add_crp_photo_to_grain: bool (default False) - derive_reaction_exothermicity: list[str] or None - database_reaction_exothermicity: list[Union[str, Path]] or None
- Returns:
Fully built and validated network
- Return type:
Examples
>>> from uclchem.makerates.io_functions import read_species_file, read_reaction_file >>> from uclchem.utils import UCLCHEM_ROOT_DIR >>> >>> species_list, user_defined_bulk = read_species_file( ... UCLCHEM_ROOT_DIR / "../../Makerates/data/default/default_species.csv" ... ) >>> reactions_list, dropped_reactions = read_reaction_file( ... UCLCHEM_ROOT_DIR / "../../Makerates/data/default/default_grain_network.csv", ... species_list, ... "UCL", ... ) >>> network = Network.build( ... species=species_list, ... reactions=reactions_list, ... gas_phase_extrapolation=True, ... add_crp_photo_to_grain=True ... )
- change_binding_energy(specie: str, new_binding_energy: float) None[source]#
Change binding energy of a species.
Handles special case of @H2O which affects other bulk species.
- Parameters:
- Raises:
ValueError – If specie is not in the network.
- change_reaction_barrier(reaction: uclchem.makerates.reaction.Reaction, barrier: float) None[source]#
Change activation barrier of a reaction.
Looks up reaction in Network by its reactants and products. If Fortran interface is available, also updates Fortran.
- Parameters:
- Raises:
RuntimeError – If multiple matching reactions are found in the network.
- classmethod from_csv(species_path: str | bytes | pathlib.Path | None = None, reactions_path: str | bytes | pathlib.Path | None = None) Network[source]#
Load network from CSV files.
Loads a pre-compiled network from CSV files without any validation or automatic generation. This is the primary method for loading networks for analysis purposes.
- Parameters:
- Returns:
Loaded network instance
- Return type:
Examples
>>> # Load default compiled network >>> network = Network.from_csv()
>>> # Load old/custom network for analysis >>> network = Network.from_csv('old/species.csv', 'old/reactions.csv')
- classmethod from_lists(species: list[uclchem.makerates.species.Species], reactions: list[uclchem.makerates.reaction.Reaction]) Network[source]#
Create network directly from lists.
Direct instantiation from species and reaction lists without any validation or automatic generation. Useful for programmatic network construction or as a base for NetworkBuilder.
- Parameters:
- Returns:
Network instance
- Return type:
Examples
>>> from uclchem.makerates.io_functions import read_species_file, read_reaction_file >>> from uclchem.utils import UCLCHEM_ROOT_DIR >>> >>> species_list, user_defined_bulk = read_species_file( ... UCLCHEM_ROOT_DIR / "../../Makerates/data/default/default_species.csv" ... ) >>> reactions_list, dropped_reactions = read_reaction_file( ... UCLCHEM_ROOT_DIR / "../../Makerates/data/default/default_grain_network.csv", ... species_list, ... "UCL", ... ) >>> network = Network.from_lists(species_list, reactions_list)
- get_all_partners(reaction: uclchem.makerates.reaction.Reaction) list[uclchem.makerates.reaction.Reaction][source]#
Get a list of all reactions that have
reactionas their partner.- Parameters:
reaction (Reaction) – Reaction
- Returns:
reactions_coupled_to_reaction – List of reactions that have
reactionas their partner.- Return type:
- Raises:
RuntimeError – If the partner of a
CoupledReactioninstance in the network is None.
- get_reactions_by_types(reaction_type: str | list[str]) list[uclchem.makerates.reaction.Reaction][source]#
Get the union of all reactions of a certain type.
- remove_reaction(reaction: uclchem.makerates.reaction.Reaction) None[source]#
Remove a reaction from network.
- Parameters:
reaction (Reaction) – Reaction to remove from the network.
- Raises:
RuntimeError – If multiple matching reactions are found in the network.
- remove_reaction_by_index(reaction_idx: int) None[source]#
Remove a reaction by its index.
- Parameters:
reaction_idx (int) – Index of the reaction in the network.
- remove_species(specie_name: str) None[source]#
Remove a species from network.
- Parameters:
specie_name (str) – Name of the species.
- set_reaction(reaction_idx: int, reaction: uclchem.makerates.reaction.Reaction) None[source]#
Set/update a reaction at specific index.
- Parameters:
- Raises:
AssertionError – If setting the reaction changes the total count of reactions.
- set_reaction_dict(new_dict: dict[int, uclchem.makerates.reaction.Reaction]) None[source]#
Replace entire reaction dictionary.
- set_specie(species_name: str, species: uclchem.makerates.species.Species) None[source]#
Set/update a species.
- set_species_dict(new_species_dict: dict[str, uclchem.makerates.species.Species]) None[source]#
Replace entire species dictionary.
- sort_reactions() None[source]#
Sort reactions by type and first reactant.
- Raises:
AssertionError – If sorting changes the total count of reactions.
- property species: dict[str, uclchem.makerates.species.Species][source]#
Get species dictionary.
- class uclchem.makerates.network.NetworkABC[source]#
Bases:
abc.ABCBase abstract class defining the read-only network interface.
Defines operations common to ALL network types: reading data, querying, and modifying existing parameters. Does NOT include add/remove operations since some implementations (like RuntimeNetwork) have fixed structure.
This interface is implemented by: - Network: Full interactive network (via MutableNetworkABC) - RuntimeNetwork: Fortran-backed runtime network (read + parameter modification only)
- abstractmethod change_binding_energy(specie: str, new_binding_energy: float) None[source]#
Change binding energy of an existing species.
- abstractmethod change_reaction_barrier(reaction: uclchem.makerates.reaction.Reaction, barrier: float) None[source]#
Change activation barrier of an existing reaction.
- abstractmethod find_similar_reactions(reaction: uclchem.makerates.reaction.Reaction) dict[int, uclchem.makerates.reaction.Reaction][source]#
Find reactions with same reactants/products.
- Parameters:
reaction (Reaction) – Reaction instance to look up or modify.
- abstractmethod get_reaction(reaction_idx: int) uclchem.makerates.reaction.Reaction[source]#
Get a specific reaction by index.
- Parameters:
reaction_idx (int) – Index of the reaction in the network.
- abstractmethod get_reaction_dict() dict[int, uclchem.makerates.reaction.Reaction][source]#
Get the reactions dictionary.
- abstractmethod get_reaction_index(reaction: uclchem.makerates.reaction.Reaction) int[source]#
Get the unique index of a reaction.
- Parameters:
reaction (Reaction) – Reaction instance to look up or modify.
- abstractmethod get_reaction_list() list[uclchem.makerates.reaction.Reaction][source]#
Get all reactions in the network.
- abstractmethod get_reactions_by_types(reaction_type: str | list[str]) list[uclchem.makerates.reaction.Reaction][source]#
Get all reactions of specific type(s).
- abstractmethod get_specie(specie_name: str) uclchem.makerates.species.Species[source]#
Get a specific species by name.
- Parameters:
specie_name (str) – Name of the species.
- abstractmethod get_species_dict() dict[str, uclchem.makerates.species.Species][source]#
Get the species dictionary.
- abstractmethod get_species_list() list[uclchem.makerates.species.Species][source]#
Get all species in the network.
- property reactions: dict[int, uclchem.makerates.reaction.Reaction][source]#
- Abstractmethod:
Get the reactions collection.
- property species: dict[str, uclchem.makerates.species.Species][source]#
- Abstractmethod:
Get the species collection.
- uclchem.makerates.network.build_network(species: list[uclchem.makerates.species.Species], reactions: list[uclchem.makerates.reaction.Reaction], user_defined_bulk: list | None = None, gas_phase_extrapolation: bool = False, add_crp_photo_to_grain: bool = False, derive_reaction_exothermicity: list[str] | None = None, database_reaction_exothermicity: list[str | pathlib.Path] | None = None) Network[source]#
Build a new network with full validation and automatic generation.
This is a module-level factory function that provides clear documentation and intent. It calls Network.build() internally.
Use this when creating new networks at build time. This performs: - Input validation and duplicate checking - Automatic freeze-out reaction generation - Automatic bulk species and reaction generation - Automatic desorption reaction generation - Branching ratio validation and correction - Temperature range collision detection - Optional gas-phase extrapolation - Optional reaction exothermicity calculation
- Parameters:
user_defined_bulk (list | None) – User-specified bulk species (optional). Defaults to
None.gas_phase_extrapolation (bool) – Extrapolate gas-phase reactions temperatures. Defaults to
False.add_crp_photo_to_grain (bool) – Add CRP/PHOTON reactions to grain surface. Defaults to
False.derive_reaction_exothermicity (list[str] | None) – Reaction types to calculate exothermicity for. Defaults to
None.database_reaction_exothermicity (list[str | Path] | None) – Custom exothermicity database files. Defaults to
None.
- Returns:
Fully built and validated network
- Return type:
Examples
>>> from uclchem.makerates.io_functions import read_species_file, read_reaction_file >>> from uclchem.utils import UCLCHEM_ROOT_DIR >>> >>> species_list, user_defined_bulk = read_species_file( ... UCLCHEM_ROOT_DIR / "../../Makerates/data/default/default_species.csv" ... ) >>> reactions_list, dropped_reactions = read_reaction_file( ... UCLCHEM_ROOT_DIR / "../../Makerates/data/default/default_grain_network.csv", ... species_list, ... "UCL", ... )
>>> # Build network with standard options >>> network = build_network( ... species=species_list, ... reactions=reactions_list, ... gas_phase_extrapolation=True ... )
>>> # Build with custom exothermicity >>> network = build_network( ... species=species_list, ... reactions=reactions_list, ... derive_reaction_exothermicity=['PHOTON', 'CRP'], ... database_reaction_exothermicity=['custom_heating.csv'] ... )
- uclchem.makerates.network.create_network(species: list[uclchem.makerates.species.Species], reactions: list[uclchem.makerates.reaction.Reaction]) Network[source]#
Create a network directly from species and reaction lists.
This is a module-level factory function that provides clear documentation and intent. It calls Network.from_lists() internally.
Use this when you want direct instantiation without validation or automatic generation. Suitable for programmatic network construction or when you already have a fully prepared network.
- Parameters:
- Returns:
Network instance
- Return type:
Examples
>>> from uclchem.makerates.io_functions import read_species_file, read_reaction_file >>> from uclchem.utils import UCLCHEM_ROOT_DIR >>> >>> species_list, user_defined_bulk = read_species_file( ... UCLCHEM_ROOT_DIR / "../../Makerates/data/default/default_species.csv" ... ) >>> reactions_list, dropped_reactions = read_reaction_file( ... UCLCHEM_ROOT_DIR / "../../Makerates/data/default/default_grain_network.csv", ... species_list, ... "UCL", ... ) >>> >>> network = create_network(species_list, reactions_list) >>> >>> # Add some additional reactions >>> network.add_reactions(additional_reactions)
- uclchem.makerates.network.load_network_from_csv(species_path: str | pathlib.Path | None = None, reactions_path: str | pathlib.Path | None = None) Network[source]#
Load a network from CSV files for analysis.
This is a module-level factory function that provides clear documentation and intent. It calls Network.from_csv() internally.
Use this when analyzing pre-compiled networks, comparing network versions, or loading old networks for analysis.
- Parameters:
- Returns:
Loaded network instance
- Return type:
Examples
>>> # Load default cmpiled network >>> network = load_network_from_csv()
>>> # Load old version for comparison >>> old_network = load_network_from_csv( ... 'archive/v3.0/species.csv', ... 'archive/v3.0/reactions.csv' ... ) >>> print(f"Species added: {len(network.get_species_list()) - len(old_network.get_species_list())}")