Vitens / phreeqpython

Object-oriented python wrapper for the VIPhreeqc module
Apache License 2.0
68 stars 19 forks source link

Charge balance of a solution #10

Closed ELarrea587 closed 4 years ago

ELarrea587 commented 4 years ago

Hi, Is there a method to get the charge balance in equivalents (and/or percent error) for a solution. I want to know if my solutions are electrically balanced and possibly manipulate an element to achieve balance. Thank you!

AbelHeinsbroek commented 4 years ago

Hi,

There is no default method, but it's fairly simple to calculate the balance from the speciation using something like the method below:

def get_balance(solution):
  reg = r"([+-]\d?)"

  balance = 0

  for st, mol in solution.species.items():
      match = re.findall(reg,st) # get charge ("+", "+2", "-", "-2", etc.)
      charge = match[0] if len(match) > 0 else "00" # if no charge
      charge = charge+"1" if len(charge) == 1 else charge # add a 1 to + or -
      balance += int(charge) * mol # calculate balance

  return balance

Does that answer your question?

ELarrea587 commented 4 years ago

It answers my question very well. Thank you for your help!