Multi-phase chemical equilibria: sodium chloride precipitation

In this notebook we will look at how ChemPy can formulate non-linear systems of equations based on precipitation. For more backgroud on the non-linear equation system you may want to look at the notebook "conditional" in the pyneqsys repo.

In [1]:
from chempy.chemistry import Species, Equilibrium
from chempy.equilibria import EqSystem
In [2]:
Na_p = Species('Na+', 1, composition={11: 1})
Cl_m = Species('Cl-', -1, composition={17: 1})
NaCl = Species('NaCl', phase_idx=1, composition={11: 1, 17: 1})
eq = Equilibrium({'NaCl': 1}, {'Na+': 1, 'Cl-': 1}, 4.0)
eqsys = EqSystem([eq], [Na_p, Cl_m, NaCl])
In [3]:
sol, info, sane = eqsys.root({'Na+': 5, 'Cl-': 5, 'NaCl': 0})
assert info['success'] and sane
sol
Out[3]:
array([2., 2., 3.])
In [4]:
sol, info, sane = eqsys.root({'Na+': 1, 'Cl-': 1, 'NaCl': 0})
assert info['success'] and sane
sol
Out[4]:
array([1.00000000e+00, 1.00000000e+00, 2.31952283e-16])
In [5]:
sol, info, sane = eqsys.root({'Na+': 2, 'Cl-': 2, 'NaCl': 0})
assert info['success'] and sane
sol
Out[5]:
array([2.00000000e+00, 2.00000000e+00, 2.31952283e-16])
In [6]:
sol, info, sane = eqsys.root({'Na+': 0, 'Cl-': 0, 'NaCl': 2})
assert info['success'] and sane
sol
Out[6]:
array([2.00000000e+00, 2.00000000e+00, 2.31952283e-16])
In [7]:
sol, info, sane = eqsys.root({'Na+': 0, 'Cl-': 0, 'NaCl': 5.0})
assert info['success'] and sane
sol
Out[7]:
array([2., 2., 3.])
In [8]:
sol, info, sane = eqsys.root({'Na+': 0, 'Cl-': 0, 'NaCl': 5.0}, rref_preserv=True)
assert info['success'] and sane
sol
Out[8]:
array([2., 2., 3.])
In [9]:
from chempy.equilibria import NumSysLog
sol, info, sane = eqsys.root({'Na+': 0, 'Cl-': 0, 'NaCl': 5.0}, rref_preserv=True, NumSys=NumSysLog)
assert info['success'] and sane
sol
Out[9]:
array([2., 2., 3.])
In [10]:
from chempy.equilibria import NumSysLin
sol, info, sane = eqsys.root({'Na+': 0, 'Cl-': 0, 'NaCl': 5.0}, rref_preserv=True, NumSys=(NumSysLog, NumSysLin))
assert info['success'] and sane
sol
Out[10]:
array([2., 2., 3.])