<jupyter_start><jupyter_text>In this notebook, we'll explore chemical reaction balance using python. We'll use atomic balance equations along with Python's built-in sympy package.Notes: 1. v represents the co-efficient of that molecule.<jupyter_code>import numpy as np
import sympy as sm
import scipy as sp<jupyter_output><empty_output><jupyter_text>Methane Combustion Reaction:To balance this equation, we will use atomic balance. For each atom, a balance equation can be written.<jupyter_code># creating variables from molecule name
sm.var(['vCH4','vO2','vCO2','vH2O'])

# writing atomic balance equation in sympy format
atom_balances = [
    sm.Eq(vCH4 + vCO2, 0), # Carbon (C) balance
    sm.Eq(4*vCH4 + 2*vH2O, 0), # Hydrogen (H) balance
    sm.Eq(2*vO2 + 2*vCO2 + vH2O, 0) # Oxygen (O) balance
]

# checking the atomic balance equations
for eqn in atom_balances:
    print(eqn)<jupyter_output>Eq(vCH4 + vCO2, 0)
Eq(4*vCH4 + 2*vH2O, 0)
Eq(2*vCO2 + vH2O + 2*vO2, 0)<jupyter_text>Now, we need a basis equation to balance the equations. As, this is a combustion, we can take methane and basis as -1.<jupyter_code># basis equation vCH4 and -1
basis_eqn = [sm.Eq(vCH4, -1)]

# checking the basis equations
for eqn in basis_eqn:
    print(eqn)<jupyter_output>Eq(vCH4, -1)<jupyter_text>Now, let's solve the atom balance equations and basis equations using sympy module.<jupyter_code># solving the atomic balance equations
balance_one = sm.solve(atom_balances + basis_eqn)

print(balance_one)<jupyter_output>{vCH4: -1, vCO2: 1, vH2O: 2, vO2: -2}<jupyter_text>So, the balanced co-efficients are saved in a dictionary. For left hand side of the equation, co-efficient is negative and for right-hand side, co-efficient is positive. Let's solve a Complex chemical reaction.Here is the equation:as before, we'll use atomic balance to solve this.<jupyter_code>atoms = ['K','Fe','S','C','N','Cr','O','H']

# creating variables from molecule name
sm.var(['vK4FeSCN6','vK2Cr2O7','vH2SO4','vFe2SO43','vCr2SO43','vCO2','vH2O','vK2SO4','vKNO3'])

# writing atomic balance equation in sympy format
atom_balances = [
    sm.Eq(4*vK4FeSCN6+2*vK2Cr2O7+0*vH2SO4+0*vFe2SO43+0*vCr2SO43+0*vCO2+0*vH2O+2*vK2SO4+vKNO3,0), # Potassium (K) balance
    sm.Eq(vK4FeSCN6+0*vK2Cr2O7+0*vH2SO4+2*vFe2SO43+0*vCr2SO43+0*vCO2+0*vH2O+0*vK2SO4+0*vKNO3,0), # Iron (Fe) balance
    sm.Eq(6*vK4FeSCN6+0*vK2Cr2O7+vH2SO4+3*vFe2SO43+3*vCr2SO43+0*vCO2+0*vH2O+vK2SO4+0*vKNO3,0), # Sulfur (S) balance
    sm.Eq(6*vK4FeSCN6+0*vK2Cr2O7+0*vH2SO4+0*vFe2SO43+0*vCr2SO43+vCO2+0*vH2O+0*vK2SO4+0*vKNO3,0), # Carbon (C) balance
    sm.Eq(6*vK4FeSCN6+0*vK2Cr2O7+0*vH2SO4+0*vFe2SO43+0*vCr2SO43+0*vCO2+0*vH2O+0*vK2SO4+vKNO3,0), # Nitrogen (N) balance
    sm.Eq(0*vK4FeSCN6+2*vK2Cr2O7+0*vH2SO4+0*vFe2SO43+2*vCr2SO43+0*vCO2+0*vH2O+0*vK2SO4+0*vKNO3,0), # Chromium (Cr) balance
    sm.Eq(0*vK4FeSCN6+7*vK2Cr2O7+4*vH2SO4+4*3*vFe2SO43+4*3*vCr2SO43+2*vCO2+vH2O+4*vK2SO4+3*vKNO3,0), # Oxygen (O) balance
    sm.Eq(0*vK4FeSCN6+0*vK2Cr2O7+2*vH2SO4+0*vFe2SO43+0*vCr2SO43+0*vCO2+vH2O+0*vK2SO4+0*vKNO3,0), # Hydrogen (H) balance
]

# checking the atomic balance equations
print(atoms)

for eqn in atom_balances:
    print(eqn)
# basis equation vK2Cr2O7 and -1
basis_eqn = [sm.Eq(vK2Cr2O7, -1)]

# checking the basis equations
for eqn in basis_eqn:
    print(eqn)
# solving the atomic balance equations
balance_two = sm.solve(atom_balances + basis_eqn)

# co-efficients of balance equation molecules
print(balance_two)<jupyter_output>{vCO2: -6/43, vCr2SO43: 1, vFe2SO43: -1/86, vH2O: 355/43, vH2SO4: -355/86, vK2Cr2O7: -1, vK2SO4: 44/43, vK4FeSCN6: 1/43, vKNO3: -6/43}
{'fraction_non_alphanumeric': 0.13936022253129346, 'fraction_numerical': 0.09401947148817802, 'mean_word_length': 6.25, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '164963', 'license': 'Apache-2.0', 'n_tokens_mistral': 1737, 'n_tokens_neox': 1656, 'n_words': 327, 'path': 'Reaction Balance and Stoichiometry/reaction_balance.ipynb', 'repo_name': 'galib96/ChemE-with-Python', 'size': '3595'}