diff --git a/src/pyEQL/engines.py b/src/pyEQL/engines.py index 9ba9c338..d62b1ebb 100644 --- a/src/pyEQL/engines.py +++ b/src/pyEQL/engines.py @@ -761,7 +761,7 @@ def equilibrate( # tolerance (in moles) for detecting cases where an element amount # is no longer balanced because of species that are not recognized # by PHREEQC. - _atol = 1e-16 + _rtol = 0.05 # differing by more than 5% new_el_dict = solution.get_el_amt_dict(nested=True) for el in orig_el_dict: @@ -770,7 +770,8 @@ def equilibrate( # If this element went "missing", add back all components that # contain this element (for any valence value) - if orig_el_amount - new_el_amount > _atol: + # if orig_el_amount - new_el_amount > _atol: + if new_el_amount == 0 and orig_el_amount > 0: logger.info( f"PHREEQC discarded element {el} during equilibration. Adding all components for this element." ) @@ -782,6 +783,12 @@ def equilibrate( if component not in solution.components } ) + elif abs(orig_el_amount - new_el_amount) / orig_el_amount > _rtol: + logger.warning( + f"PHREEQC returned a total Element {el} concentration of {new_el_amount} mol, " + f"which differs from the original concentration of {orig_el_amount}. This " + "should never occur and indicates an error in the PHREEQC database or calculation." + ) # re-adjust charge balance for any missing species # note that if balance_charge is set, it will have been passed to PHREEQC, so the only reason to re-adjust charge balance here is to account for any missing species.