import logging


def check_element_balance(elements, species, reaction, limit=1e-5):
    """Check the given reaction for their element balance."""
    elm_sum = {key: 0.0 for key in elements.keys()}

    for spc, spc_nu in reaction.reactants.items():
        for key in species[spc].thermo.composition:
            elm_sum[key] += species[spc].thermo.composition[key] * spc_nu

    for spc, spc_nu in reaction.products.items():
        for key in species[spc].thermo.composition:
            elm_sum[key] -= species[spc].thermo.composition[key] * spc_nu

    if any(abs(x) > limit for x in elm_sum.values()):
        logging.warning('Element conservation violation: %s!', elm_sum)

    return all(abs(x) <= limit for x in elm_sum.values())