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<molecule name> represents the co-efficient of that molecule.


```python
import numpy as np
import sympy as sm
import scipy as sp
```

Methane Combustion Reaction:



To balance this equation, we will use atomic balance. For each atom, a balance equation can be written.


```python
# 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)
```

    Eq(vCH4 + vCO2, 0)
    Eq(4*vCH4 + 2*vH2O, 0)
    Eq(2*vCO2 + vH2O + 2*vO2, 0)


Now, we need a basis equation to balance the equations. As, this is a combustion, we can take methane and basis as -1.


```python
# basis equation vCH4 and -1
basis_eqn = [sm.Eq(vCH4, -1)]

# checking the basis equations
for eqn in basis_eqn:
    print(eqn)
```

    Eq(vCH4, -1)


Now, let's solve the atom balance equations and basis equations using sympy module. 


```python
# solving the atomic balance equations
balance_one = sm.solve(atom_balances + basis_eqn)

print(balance_one)
```

    {vCH4: -1, vCO2: 1, vH2O: 2, vO2: -2}


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.


```python
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)
```

    ['K', 'Fe', 'S', 'C', 'N', 'Cr', 'O', 'H']
    Eq(2*vK2Cr2O7 + 2*vK2SO4 + 4*vK4FeSCN6 + vKNO3, 0)
    Eq(2*vFe2SO43 + vK4FeSCN6, 0)
    Eq(3*vCr2SO43 + 3*vFe2SO43 + vH2SO4 + vK2SO4 + 6*vK4FeSCN6, 0)
    Eq(vCO2 + 6*vK4FeSCN6, 0)
    Eq(6*vK4FeSCN6 + vKNO3, 0)
    Eq(2*vCr2SO43 + 2*vK2Cr2O7, 0)
    Eq(2*vCO2 + 12*vCr2SO43 + 12*vFe2SO43 + vH2O + 4*vH2SO4 + 7*vK2Cr2O7 + 4*vK2SO4 + 3*vKNO3, 0)
    Eq(vH2O + 2*vH2SO4, 0)



```python
# basis equation vK2Cr2O7 and -1
basis_eqn = [sm.Eq(vK2Cr2O7, -1)]

# checking the basis equations
for eqn in basis_eqn:
    print(eqn)
```

    Eq(vK2Cr2O7, -1)



```python
# solving the atomic balance equations
balance_two = sm.solve(atom_balances + basis_eqn)

# co-efficients of balance equation molecules
print(balance_two)
```

    {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}



```python

```

35e20683cb9b6eed2926c8c2f8949c853c79d222
ipynb
Jupyter Notebook
Reaction Balance and Stoichiometry/reaction_balance.ipynb
galib96/ChemE-with-Python
3cdb42e082d3a9108a5e7a1558a76495ea37674c
Apache-2.0
Reaction Balance and Stoichiometry/reaction_balance.ipynb
galib96/ChemE-with-Python
3cdb42e082d3a9108a5e7a1558a76495ea37674c
Apache-2.0
Reaction Balance and Stoichiometry/reaction_balance.ipynb
galib96/ChemE-with-Python
3cdb42e082d3a9108a5e7a1558a76495ea37674c
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES