harirakul/chemlib

Accuracy

wedobetter opened this issue · 3 comments

chemlib==2.2.3

In [15]: r = Reaction.by_formula('KOH + H3PO4 --> K2HPO4 + H2O')

In [16]: r.balance()

In [17]: r.formula
Out[17]: '2K₁O₁H₁ + 1H₃P₁O₄ --> 1K₂H₁P₁O₄ + 2H₂O₁'

In [18]: dict(zip(r.constituents, [c["grams"] for c in r.get_amounts(3, grams=1)]))
Out[18]: {'K₁O₁H₁': 0.673, 'H₃P₁O₄': 0.588, 'K₂H₁P₁O₄': 1, 'H₂O₁': 0.216}

In [19]: dict(zip(r.constituents, [c["grams"] for c in r.get_amounts(3, grams=1000)]))
Out[19]: {'K₁O₁H₁': 644.255, 'H₃P₁O₄': 562.561, 'K₂H₁P₁O₄': 1000, 'H₂O₁': 206.906}

I'd expect KOH to be something like 673.xxx

1 gram of the third compound in the formula (K₂HPO₄) is equivalent to 0.006 moles of K₂H₁P₁O₄, using its molar mass (174.17 g/mol). Using the 2:1 stoichiometric ratio of KOH : K₂HPO₄ in the balanced equation, this would be 0.012 mol KOH. This results in 0.673 grams of KOH, using its molar mass (56.1 g/mol).

1000 grams of K₂HPO₄ is equivalent to 5.741 moles of K₂HPO₄, using its molar mass (174.17 g/mol). In this case, that would give us 11.482 mol of KOH using the 2:1 stoichiometric ratio. This results in 644.25 grams of KOH, using its molar mass (56.1 g/mol).

Simply using 1000 times more mass of a product in a reaction won't mean that 1000 times more mass of a reactant will be needed. It depends also on the molar masses of the compounds in question, and stoichiometric ratios. I don't see any problems with the current calculation or its accuracy.

The problem is that all the round(value, 3) should be left to the users to do at the end of their calculations.
I'd love to see that 1 gram of K2HPO4 equivalent to 0.005741516908767 moles

By the way I forgot to mention that I really love this library and it's coming up really interesting, thank you!

Oh ok, thanks for the clarification. Perhaps I will add an option for the user to override the default number of decimal values in the calculations.