Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/pyEQL/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -2699,3 +2699,33 @@ def add_solvent(self, formula: str, amount: str): # pragma: no cover
mw = self.get_property(formula, "molecular_weight")
target_mol = quantity.to("moles", "chem", mw=mw, volume=self.volume, solvent_mass=self.solvent_mass)
self.components[formula] = target_mol.to("moles").magnitude

def to_phreeqc(self, units: str = "mol/L", charge_balance: bool = "False") -> str:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The charge_balance should not be a kwarg here, but rather be read from the Solution.balance_charge attribute which is set on __init__. See here for the documentation.

This will require some careful handling depending on the different cases - the pH case is easy and will work very much like you've coded below.

If 'balance_charge' is another species, you'll have to determine the associated element (e.g., Na+ -> Na) and then append the charge to that line (I think).

"""Generate a PHREEQC input string representing the Solution.

Returns:
A string that can be used as part of a PHREEQC input file to define
the solution.
"""
lines = ["SOLUTION 0"]
lines.append(f" temp {self.temperature.to('degC').magnitude:.2f}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solution also has pressure and volume attributes. How does PHREEQC handle those? If possible, pass them into the input file, too

if charge_balance == "True":
lines.append(f" pH {self.pH:.2f} charge")
else:
lines.append(f" pH {self.pH:.2f}")
lines.append(f" pe {self.pE:.2f}")
lines.append(f" units {units}")
for solute, amount in self.components.items():
amount = self.get_amount(solute, units).magnitude
if solute in ["H2O(aq)", "H[+1]", "OH[-1]"]:
continue
k = standardize_formula(solute)
spl = k.split("[")
el = spl[0]
chg = spl[1].split("]")[0]
if chg[-1] == "1":
chg = chg[0]
k = el + chg
Comment on lines +2718 to +2728
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, I think you will need to specify the total element concentrations via get_total_amount(), because the phreeqc convention is not to specify individual species, right?

Also, any formula that is coming out of components is guaranteed to already be standardized, so it's not necessary to call standardize_formula on it

lines.append(f" {k} {amount:.6e}")
lines.append("END")
return "\n".join(lines)
Loading