#!/usr/bin/env python3
"""Print illustrative isothermal proton-transfer energies as CSV; no network or files.

Delta pH = destination pH - source pH.
Delta psi = destination electric potential - source electric potential.
Negative delta_mu favours the specified transfer. These are calculated examples,
not measured hydrothermal conditions or a prediction of reaction yield.
Constants: NIST SP 961, 2022 CODATA values (May 2024).
https://physics.nist.gov/cuu/pdf/wall_2022.pdf
"""
import csv
from decimal import Decimal
import math
import sys

AVOGADRO = Decimal('6.02214076e23')
R = float(AVOGADRO * Decimal('1.380649e-23'))  # J mol^-1 K^-1
F = float(AVOGADRO * Decimal('1.602176634e-19'))  # C mol^-1
SCENARIOS = (
    (25, 1, 0), (25, 3, 0), (25, 4, 0), (25, 6, 0),
    (90, 4, 0), (25, 4, 100), (25, -4, 0), (25, 0, 0),
)


def rows():
    for celsius, delta_ph, delta_psi_mv in SCENARIOS:
        kelvin = celsius + 273.15
        chemical = -math.log(10) * R * kelvin * delta_ph / 1000
        electrical = F * delta_psi_mv / 1_000_000
        yield (celsius, delta_ph, delta_psi_mv,
               f'{chemical:.6f}', f'{electrical:.6f}', f'{chemical + electrical:.6f}')


if __name__ == '__main__':
    writer = csv.writer(sys.stdout, lineterminator='\n')
    writer.writerow(('temperature_c', 'delta_ph_destination_minus_source',
                     'delta_psi_mv_destination_minus_source',
                     'chemical_kj_per_mol_protons', 'electrical_kj_per_mol_protons',
                     'delta_mu_kj_per_mol_protons'))
    writer.writerows(rows())
