In [1]:
import math
In [3]:
class Reactant(object):
def __init__(self, formula=None, molwt=None, mass=None, state=None, electrolyte=None, density=None, pk=None):
self.formula = formula
self.molwt = molwt
self.mass = mass
self.state = state
self.electrolyte = electrolyte
self.density = density
self.pk = pk
In [25]:
def ph(reactants):
'''
lst of object having following attributes:
* formula
* molwt
* state
* electrolyte
* density
* pk
'''
volume = 0.0
C_Hplus = 0.0
C_OHminus = 0.0
for reac in reactants:
if reac.state == "solid" or reac.state == "gel" or reac.formula == "H2O":
volume += reac.mass/reac.density/1000.0
print "Volume = ", volume
for reac in reactants:
if reac.pk > 10.0: # strong electrolyte
if reac.electrolyte == "acid":
C_Hplus += reac.mass/reac.molwt/volume
elif reac.electrolyte == "base":
C_OHminus += reac.mass/reac.molwt/volume
else: # weak electrolyte
if reac.electrolyte == "acid":
C_Hplus += math.sqrt(math.exp(-1.0*reac.pk)*reac.mass/reac.molwt/volume)
elif reac.electrolyte == "base":
C_OHminus += math.sqrt(math.exp(-1.0*reac.pk)*reac.mass/reac.molwt/volume)
if C_Hplus > C_OHminus:
pH = -1.0*math.log(C_Hplus - C_OHminus, 10)
else:
pOH = -1.0*math.log(C_OHminus - C_Hplus, 10)
pH = 14.0 - pOH
return pH
In [27]:
naoh = Reactant(formula="NaOH", molwt=40.00, mass=40.0, state="solid", electrolyte="base", density=2.13, pk=10)
koh = Reactant(formula="KOH", molwt=56.11, mass=10.0, state="solid", electrolyte="base", density=2.044, pk=10)
h2o = Reactant(formula="H2O", molwt=18.02, mass=9970.0, state="liquid", electrolyte="neutral", density=0.9970, pk=10)
reactants = [naoh, h2o]
In [24]:
print "pH = {0:8.4f}".format(ph(reactants))
In [ ]: