bjodah / chempy

⚗ A package useful for chemistry written in Python
BSD 2-Clause "Simplified" License
544 stars 78 forks source link

balance_stoichiometry raises "ValueError: Component '0' not among products" when using electrons #158

Closed BobBurrow closed 5 years ago

BobBurrow commented 5 years ago

For the newer versions (I tested chempy versions 0.7.8 & 0.6.8), when I try to balance semi-reactions, I get the exception "ValueError: Component '0' not among products": :

from chempy import balance_stoichiometry from pprint import pprint reac, prod = balance_stoichiometry({'Zn+2', 'e-'}, {'Zn'}) Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.6/site-packages/chempy/chemistry.py", line 1207, in balance_stoichiometry raise ValueError("Component '%s' not among products" % ck) ValueError: Component '0' not among products

This snippet worked fine for chempy 0.4.1:

from chempy import balance_stoichiometry from pprint import pprint reac, prod = balance_stoichiometry({'Zn+2', 'e-'}, {'Zn'}) pprint(dict(reac)) {'Zn+2': 1, 'e-': 2} pprint(dict(prod)) {'Zn': 1}

Strangely, for 0.6.8 & 0.7.8, if I put the electrons on the wrong side, the function works, but returns negative coefficients:

reac, prod = balance_stoichiometry({'Zn+2'}, {'Zn','e-'}) pprint(dict(reac)) {'Zn+2': -1} pprint(dict(prod)) {'Zn': -1, 'e-': 2}

I'm running python on OpenSuse Leap 15.0 which all the python packages updated (except for chempy for the version tests) using pip3.

I can do what I want with 0.4.1 (it's a huge time-saver!), but you should correct this bug.

Yours, Bob Burrow

bjodah commented 5 years ago

Thank you Bob for reporting this, and I'm glad to hear that you've found chempy useful. I've changed the code for balance_stoichiometry as per user requests (added a few options), and I must have introduced this bug in that process. I will definitely look into this as soon as I find some time (and add a regression test to prevent the bug from resurfacing once fixed).

bjodah commented 5 years ago

@BobBurrow, this is fixed in master. Thanks again for the report.

BobBurrow commented 5 years ago

Thanks a lot! Glad I could help make ChemPy more helpful.