chemistrylab.reactions

chemistrylab.reactions.reaction

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]
Parameters:
  • 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.

Returns:

The new amounts of each material in the vessel

Return type:

np.array

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

Parameters:
  • 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}\)

Parameters:
  • 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

Returns:

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

Return type:

np.array

chemistrylab.reactions.reaction.newton_solve(stoich_coeff_arr, pre_exp_arr, activ_energy_arr, conc_coeff_arr, num_reagents, temp, conc, dt, N)[source]
Parameters:
  • 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

Returns:

The final concentrations y(dt)

Return type:

np.array

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

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: tuple

property MATERIALS

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

property PRODUCTS

Any products formed in the reaction(s)

property REACTANTS

The reactants involved in the reaction(s)

property SOLVENTS

Any solvents used in the reaction(s)

property activ_energy_arr

The activation energies for the reaction(s)

property conc_coeff_arr

The concentration coefficients of all involved materials in each reaction

dump_to_json(fn)[source]

Saves the reaction information to a json file

Parameters:

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

Returns:

The ReactInfo object parameterized by the json file

Return type:

ReactInfo

property name

The name of the reaction

property pre_exp_arr

The pre-exponential factors for the reaction(s)

property stoich_coeff_arr

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

Parameters:
  • array_str (str) – The json string

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

Returns:

A prettier version of the json string

Return type:

str

chemistrylab.reactions.reaction_info.serial(x)[source]

Turn a numpy array into tuples