Commit ed4768b3 authored by Avi Flamholz's avatar Avi Flamholz

Small updates for clarity and documentation.

parent 767c07cf
......@@ -4,6 +4,7 @@ A command-line API with minimal dependencies for calculation of standard thermod
# Current Features
* Example scripts for singleton and bulk calculations.
* Calculation of standard Gibbs potentials of reactions.
* Calculation of standard reduction potentials of half-cells.
......
......@@ -12,24 +12,34 @@ import csv
from numpy import arange, sqrt
import logging
def g2e_str(g):
"""Convert dG to dE in string form'"""
return '%.2f' % (1000.0 * -g / (n_e*FARADAY))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=
'Calculate reduction potentials for '
'a number of reactions.')
parser.add_argument('infile', type=argparse.FileType(),
help='path to input file containing a '
'list of reactions')
parser.add_argument('outfile', type=argparse.FileType('w'),
help='path to output file')
parser.add_argument('--ionic_strength', default=0.2, type=int,
help='ionic strength in molar units.')
parser.add_argument('--pH_min', default=5, type=int,
help='lowest pH to produce E0 for.')
parser.add_argument('--pH_max', default=9, type=int,
help='highest pH to produce E0 for.')
parser.add_argument('--pH_step', default=0.05, type=float,
help='pH increment.')
parser = argparse.ArgumentParser(
description='Calculate potentials for a number of reactions.')
parser.add_argument(
'infile', type=argparse.FileType(),
help='path to input file containing reactions')
parser.add_argument(
'outfile', type=argparse.FileType('w'),
help='path to output file')
parser.add_argument(
'--ionic_strength', default=0.2, type=int,
help='ionic strength in molar units.')
parser.add_argument(
'--pH_min', default=5, type=int,
help='lowest pH to produce a potential for.')
parser.add_argument(
'--pH_max', default=9, type=int,
help='highest pH to produce a potential for.')
parser.add_argument(
'--pH_step', default=0.05, type=float,
help='pH increment between --pH_min and --pH_max.')
logging.getLogger().setLevel(logging.WARNING)
args = parser.parse_args()
......@@ -50,23 +60,24 @@ if __name__ == '__main__':
dG0_prime_list = []
uncertainty_list = []
for pH in pHs:
dG0_prime, U = prep.dG0_prime(reaction, pH=pH,
ionic_strength=ionic_strength)
dG0_prime, U = prep.dG0_prime(
reaction, pH=pH, ionic_strength=ionic_strength)
dG0_prime_list.append(dG0_prime[0, 0])
uncertainty_list.append(sqrt(U[0, 0]))
if n_e != 0: # treat as a half-reaction
E0_prime_mV = list(map(lambda g: '%.2f' % (1000.0 * -g / (n_e*FARADAY)),
dG0_prime_list))
reactions_and_energies.append([formula, 'half-reaction',
'E\'0', 'mV'] + E0_prime_mV)
if n_e != 0:
# treat as a half-reaction
E0_prime_mV = list(map(g2e_str, dG0_prime_list))
data_list = [formula, 'half-reaction', 'E\'0', 'mV']
data_list.extend(E0_prime_mV)
reactions_and_energies.append(data_list)
else:
dG0_prime_kj_mol = list(map(lambda g: '%.2f' % g,
dG0_prime_list))
reactions_and_energies.append([formula, 'reaction', 'dG\'0',
'kJ/mol'] + dG0_prime_kj_mol)
data_list = [formula, 'reaction', 'dG\'0', 'kJ/mol']
data_list.extend(dG0_prime_kj_mol)
reactions_and_energies.append(data_list)
# write all results to the output CSV file
writer = csv.writer(args.outfile)
header = ['formula', 'type', 'estimated_value', 'unit']
......
......@@ -49,18 +49,20 @@ prep = Preprocessing()
n_e = reaction.check_half_reaction_balancing()
if n_e is None:
sys.stderr.write('Reaction is not atomically balanced')
sys.exit(-1)
dG0_prime, U = prep.dG0_prime(reaction, pH=args.ph,
ionic_strength=args.i)
dG0_prime, U = prep.dG0_prime(
reaction, pH=args.ph, ionic_strength=args.i)
dG0_prime = dG0_prime[0, 0]
uncertainty = sqrt(U[0, 0])
if n_e != 0: # treat as a half-reaction
E0_prime_mV = 1000.0 * -dG0_prime / (n_e*FARADAY)
E0_uncertainty = 1000.0 * uncertainty / (n_e*FARADAY)
sys.stdout.write( u'E\'\u00B0 = %.1f \u00B1 %.1f mV\n' %
(E0_prime_mV, E0_uncertainty))
sys.stdout.write(
u'E\'\u00B0 = %.1f \u00B1 %.1f mV\n' %
(E0_prime_mV, E0_uncertainty))
else:
sys.stdout.write(u'\u0394G\'\u00B0 = %.1f \u00B1 %.1f kJ/mol\n' %
(dG0_prime, uncertainty))
......
......@@ -317,6 +317,7 @@ class Reaction(object):
"""
Returns:
The number of electrons that are 'missing' in the half-reaction
or None if the reaction is not atomwise-balanced.
"""
atom_balance_dict = self._get_reaction_atom_balance()
n_e = atom_balance_dict.pop('e-', 0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment