This repository contains the code and data used in the paper "Comprehensive Analysis of Acid-Base Equilibria Using MBE, CBE, and PBE". The code is written in Python and can be run in any Python environment with the required libraries installed. Jupyter notebooks are also provided for interactive exploration of the calculations and results.
The paper is available in the documents
folder, and the compiled pKa data used in the calculations is available in the pKa_data
folder.
π Want to read the paper? Click here
π Prefer the online version? Click here
- Table of contents
- Introduction
- Theory
- Installation through pip
- Installation through github repository
- Running the Code
- Code Structure
- Examples
- Contact
- License
This project explores the calculation of acid-base equilibrium calculations, focusing on the Material Balance Equation (MBE), Charge Balance Equation (CBE), and Proton Balance Equation (PBE). The code provided in this repository allows for the calculation of pH values for various acid-base systems using the CBE and PBE methods.
Acid-base reactions in solution are governed by the activity of ions, which refers to their effective concentration in chemical reactions. Due to electrostatic interactions between ions, their effective concentration (activity) can differ from their actual concentration. The relationship between the activity
where
where
The equilibrium constant
The relationship between
The Material Balance Equation (MBE) states that the total concentration of a substance in a chemical equilibrium system is equal to the sum of the equilibrium concentrations of all relevant species. For example, for a solution with concentration
The Charge Balance Equation (CBE) ensures that the total positive charge in a solution equals the total negative charge. (As the solution is always neutral in charge) For example, in a solution with concentration
Below is a diagram of the CBE curve for a solution of
The Proton Balance Equation (PBE) states that the amount of protons gained by substances (bases) equals the amount of protons lost by substances (acids). For example, in an aqueous solution of
These equations are essential tools for understanding and predicting the behavior of acid-base systems in solution.
Here is an example of the PBE curve for a solution of
Not that we have the equations, we can calculate the pH of a solution using the CBE or PBE method. The general steps are as follows:
- Define the acid and base species in the solution, including their charges, concentrations, and pKa values.
- Calculate the equilibrium concentrations of all species using the MBE.
- Use the CBE or PBE to calculate the Error function, which is a measure of the deviation from charge balance or proton balance.
- Use an optimization algorithm to minimize the Error function and find the pH of the solution.
- Output the pH value and other relevant information about the acid-base system.
Below is a diagram of the absolute error of CBE for a solution of
We can easily find the point where CBE_error = 0 by using the minimization algorithm or gradient descent. The pH value at this point is the pH of the solution.
For more details, please refer to the paper at documents/doc.pdf or the online version at phcal.ericxin.eu.
pip install phcal == 0.1.0
-
Clone the repository:
git clone https://github.com/Eric-xin/pH_Calculation.git cd pH_Calculation
-
Create a virtual environment and activate it:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install the required libraries:
pip install -r requirements.txt
To run the code, you can use the provided Jupyter notebooks or run the Python scripts directly. For example, to run the CBE.ipynb
notebook:
-
Start Jupyter Notebook:
jupyter notebook
-
Open
CBE.ipynb
in the Jupyter interface and run the cells.
The code is organized as follows:
.
βββ CBE.ipynb # Jupyter notebook for Charge Balance Equation (CBE) based calculations
βββ LICENSE # MIT License
βββ PBE_investigation.ipynb # Jupyter notebook for Proton Balance Equation (PBE) based calculations (investigation)
βββ assets
β βββ img # Image files used in the readme
βββ calculation # Code for MBE, CBE, and PBE calculations
β βββ Basics.py # Basic classes and functions for acid-base calculations, based on MBE
β βββ CBE_calc.py
β βββ PBE_calc.py
βββ documents
β βββ doc.pdf # Paper "Comprehensive Analysis of Acid-Base Equilibria Using MBE, CBE, and PBE"
βββ pKa_data # Compiled pKa data for various acids, used in the calculations
β βββ pka-compilation-williams.pdf
β βββ readme.md
βββ readme.md # This file
Charge Balance Equation (CBE) Calculation
from calculation import CBE_Inert, CBE_calc
HCl = CBE_Inert(charge=-1, conc=0.01)
pH = CBE_calc(HCl)
pH.pH_calc()
print(pH.pH) # the result should be 1.9999977111816385
Note: if you are using the python package, you should import from phcal
instead of calculation
.
from phcal import CBE_Inert, CBE_calc
...
Proton Balance Equation (PBE) Calculation
from calculation import PBE_Inert
HCl = PBE_Inert(conc=0.01, proton=1, proton_ref=1)
# HCl = PBE_Acid(conc=0.01, Ka=100000, proton=1, proton_ref=1)
calc = PBE_calc(HCl)
calc.pH_calc()
print(calc.pH)
Charge Balance Equation (CBE) Calculation
from calculation import CBE_Acid, CBE_calc
NH4 = CBE_Acid(charge=1, conc=0.01*3, pKa=9.25)
pKa = [1.97, 6.82, 12.5]
P = CBE_Acid(charge=0, conc=0.01, pKa=pKa)
pH = CBE_calc(NH4, P)
pH.pH_calc()
print(pH.pH) # the result should be 8.952952575683597
Proton Balance Equation (PBE) Calculation
from calculation import PBE_Acid, PBE_calc
NH4 = PBE_Acid(conc=0.01 * 3, pKa=9.25, proton=1, proton_ref=1)
pKa = [1.97, 6.82, 12.5]
P = PBE_Acid(conc=0.01, pKa=pKa, proton=3, proton_ref=0)
calc = PBE_calc(NH4, P)
calc.pH_calc()
print(calc.pH)
For more examples, see the Jupyter notebooks. 'CBE.ipynb' for CBE calculations and 'PBE.ipynb' for PBE calculations.
If you have any questions or suggestions, please feel free to contact me at me@ericxin.eu.
This project is licensed under the MIT License. See the LICENSE file for details.