@@ -261,39 +261,44 @@ def __init__(
261
261
for item in self ._solutes :
262
262
self .add_solute (* item )
263
263
264
- # adjust the charge balance, if necessary
264
+ # determine the species that will be used for charge balancing, when needed.
265
+ # this is necessary to do even if the composition is already electroneutral,
266
+ # because the appropriate species also needs to be passed to equilibrate
267
+ # to keep from distorting the charge balance.
265
268
cb = self .charge_balance
269
+ if self .balance_charge is None :
270
+ self ._cb_species = None
271
+ elif self .balance_charge == "pH" :
272
+ self ._cb_species = "H[+1]"
273
+ elif self .balance_charge == "pE" :
274
+ raise NotImplementedError ("Balancing charge via redox (pE) is not yet implemented!" )
275
+ elif self .balance_charge == "auto" :
276
+ # add the most abundant ion of the opposite charge
277
+ if cb <= 0 :
278
+ self ._cb_species = max (self .cations , key = self .cations .get )
279
+ elif cb > 0 :
280
+ self ._cb_species = max (self .anions , key = self .anions .get )
281
+ else :
282
+ ions = set ().union (* [self .cations , self .anions ]) # all ions
283
+ self ._cb_species = self .balance_charge
284
+ if self ._cb_species not in ions :
285
+ raise ValueError (
286
+ f"Charge balancing species { self ._cb_species } was not found in the solution!. "
287
+ f"Species { ions } were found."
288
+ )
289
+
290
+ # adjust charge balance, if necessary
266
291
if not np .isclose (cb , 0 , atol = 1e-8 ) and self .balance_charge is not None :
267
292
balanced = False
268
293
self .logger .info (
269
- f"Solution is not electroneutral (C.B. = { cb } eq/L). Adding { balance_charge } to compensate."
294
+ f"Solution is not electroneutral (C.B. = { cb } eq/L). Adding { self . _cb_species } to compensate."
270
295
)
271
- if self .balance_charge == "pH" :
272
- self .components ["H+" ] += (
273
- - 1 * cb * self .volume .to ("L" ).magnitude
274
- ) # if C.B. is negative, we need to add cations. H+ is 1 eq/mol
296
+ z = self .get_property (self ._cb_species , "charge" )
297
+ self .components [self ._cb_species ] += - 1 * cb / z * self .volume .to ("L" ).magnitude
298
+ if np .isclose (self .charge_balance , 0 , atol = 1e-8 ):
275
299
balanced = True
276
- elif self .balance_charge == "pE" :
277
- raise NotImplementedError ("Balancing charge via redox (pE) is not yet implemented!" )
278
- else :
279
- ions = set ().union (* [self .cations , self .anions ]) # all ions
280
- if self .balance_charge == "auto" :
281
- # add the most abundant ion of the opposite charge
282
- if cb <= 0 :
283
- self .balance_charge = max (self .cations , key = self .cations .get )
284
- elif cb > 0 :
285
- self .balance_charge = max (self .anions , key = self .anions .get )
286
- if self .balance_charge not in ions :
287
- raise ValueError (
288
- f"Charge balancing species { self .balance_charge } was not found in the solution!. "
289
- f"Species { ions } were found."
290
- )
291
- z = self .get_property (self .balance_charge , "charge" )
292
- self .components [self .balance_charge ] += - 1 * cb / z * self .volume .to ("L" ).magnitude
293
- balanced = True
294
-
295
300
if not balanced :
296
- warnings .warn (f"Unable to balance charge using species { self .balance_charge } " )
301
+ warnings .warn (f"Unable to balance charge using species { self ._cb_species } " )
297
302
298
303
@property
299
304
def mass (self ) -> Quantity :
0 commit comments