chemistrylab.reactions package

chemistrylab.reactions.reaction module

class chemistrylab.reactions.reaction.Reaction(react_info: ReactInfo, solver: str = 'RK45', newton_steps: int = 100)[source]

Bases: object

react(n: array, temp: float, volume: float, dt: float)[source]
  • n (np.array) – An array containing the amounts of each material in the vessel.

  • temp (float) – The temperature of the system in Kelvin.

  • volume (float) – The volume of the system in Litres.

  • dt (float) – The time-step demarcating separate steps in seconds.


The new amounts of each material in the vessel

Return type:


update_concentrations(vessel: Vessel, dt: float = 0)[source]

Takes in a vessel and applies the reaction to it, updating the material and solvent dicts in the process

  • vessel (Vessel) – The vessel to perform the reaction on

  • dt (float) – The amount of time passed during the reaction

chemistrylab.reactions.reaction.get_rates(stoich_coeff_arr, pre_exp_arr, activ_energy_arr, conc_coeff_arr, num_reagents, temp, conc)[source]

Finds the rate of reaction \(\frac{dy}{dt}\)

  • num_reagents (int) – The number of reactants involved in the reaction

  • temp (float) – The temperature of the reactions

  • conc (float) – The initial concentrations of the materials

  • *_arr (np.array) – See ReactInfo


Rates of change in concentration \(\frac{dy}{dt}\).

Return type:


chemistrylab.reactions.reaction.newton_solve(stoich_coeff_arr, pre_exp_arr, activ_energy_arr, conc_coeff_arr, num_reagents, temp, conc, dt, N)[source]
  • num_reagents (int) – The number of reactants involved in the reaction

  • temp (float) – The temperature of the reactions

  • conc (float) – The initial concentrations of the materials

  • dt (float) – The amount of time to pass

  • N (int) – The minimum number of time-steps to break dt into

  • *_arr (np.array) – See ReactInfo


The final concentrations y(dt)

Return type:


Solves the initial value problem \(\frac{dy}{dt} = f(y)\) specifically in the case where f(y) is chemical a rate calculation

(The problem is that we have \(y(t_0)\) and need \(y(t_0+dt))\)

This solver uses newton’s method:

set \(T = \frac{dt}{N}\)

set \(y_0 = y(t_0)\)

for n = 1,2,3,…N:

\(y_n = y_{n-1} + f(y_{n-1})*T\)

\(y(dt) \leftarrow y_N\)

Intuitively, it is like taking a Riemann sum of dy/dt (but you get dy/dt by bootstrapping your current sum for y(t)) This implementation uses a variable step size in order to account for super fast-changing concentrations (wurtz distill)

chemistrylab.reactions.reaction.react(vessel: Vessel, dt: float, other_vessel: None, reaction: Reaction)[source]

Event function to perform a reaction

chemistrylab.reactions.reaction_info module

class chemistrylab.reactions.reaction_info.ReactInfo(name, REACTANTS, PRODUCTS, SOLVENTS, MATERIALS, pre_exp_arr, activ_energy_arr, stoich_coeff_arr, conc_coeff_arr)[source]

Bases: NamedTuple

MATERIALS: Tuple[str]

All materials or substances involved in the reaction(s) (a union of reactants products and solvents)

PRODUCTS: Tuple[str]

Any products formed in the reaction(s)

REACTANTS: Tuple[str]

The reactants involved in the reaction(s)

SOLVENTS: Tuple[str]

Any solvents used in the reaction(s)

activ_energy_arr: array

The activation energies for the reaction(s)

conc_coeff_arr: array

The concentration coefficients of all involved materials in each reaction


Saves the reaction information to a json file


fn (str) – The filename to save as.

static from_json(fn)[source]

Creates a new ReactInfo object from a json file :param fn: The filename to load from :type fn: str


The ReactInfo object parameterized by the json file

Return type:


name: str

The name of the reaction

pre_exp_arr: array

The pre-exponential factors for the reaction(s)

stoich_coeff_arr: array

Stoichiometric coefficients of the reactants in each reaction

chemistrylab.reactions.reaction_info.format_2d_array_string(array_str, indent=2)[source]

Special json formatting function to create pretty arrays

  • array_str (str) – The json string

  • indent (int) – The amount of indentation in the json string


A prettier version of the json string

Return type:



Turn a numpy array into tuples

Module contents