Skip to content

Commit

Permalink
overhaul logging system; adjust message levels
Browse files Browse the repository at this point in the history
  • Loading branch information
rkingsbury committed Mar 13, 2024
1 parent 8a807ec commit 6e091c4
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 150 deletions.
19 changes: 9 additions & 10 deletions src/pyEQL/activity_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
:license: LGPL, see LICENSE for more details.
"""

import logging
import math

from pint import Quantity

from pyEQL import ureg
from pyEQL.logging_system import logger
from pyEQL.utils import create_water_substance

logger = logging.getLogger(__name__)


def _debye_parameter_B(temperature: str = "25 degC") -> Quantity:
r"""
Expand Down Expand Up @@ -115,7 +118,7 @@ def _debye_parameter_activity(temperature: str = "25 degC") -> "Quantity":
/ (4 * math.pi * ureg.epsilon_0 * water_substance.epsilon * ureg.boltzmann_constant * T) ** 1.5
)

logger.info(rf"Computed Debye-Huckel Limiting Law Constant A^{{\gamma}} = {debyeparam} at {temperature}")
logger.debug(rf"Computed Debye-Huckel Limiting Law Constant A^{{\gamma}} = {debyeparam} at {temperature}")
return debyeparam.to("kg ** 0.5 / mol ** 0.5")


Expand Down Expand Up @@ -149,7 +152,7 @@ def _debye_parameter_osmotic(temperature="25 degC"):
"""
output = 1 / 3 * _debye_parameter_activity(temperature)
logger.info(f"Computed Debye-Huckel Limiting slope for osmotic coefficient A^phi = {output} at {temperature}")
logger.debug(f"Computed Debye-Huckel Limiting slope for osmotic coefficient A^phi = {output} at {temperature}")
return output.to("kg ** 0.5 /mol ** 0.5")


Expand Down Expand Up @@ -206,7 +209,7 @@ def _debye_parameter_volume(temperature="25 degC"):
if T.to("degC").magnitude != 25:
logger.warning("Debye-Huckel limiting slope for volume is approximate when T is not equal to 25 degC")

logger.info(f"Computed Debye-Huckel Limiting Slope for volume A^V = {result} at {temperature}")
logger.debug(f"Computed Debye-Huckel Limiting Slope for volume A^V = {result} at {temperature}")

return result.to("cm ** 3 * kg ** 0.5 / mol ** 1.5")

Expand Down Expand Up @@ -280,9 +283,7 @@ def get_activity_coefficient_guntelberg(ionic_strength, z=1, temperature="25 deg
if not ionic_strength.magnitude <= 0.1:
logger.warning("Ionic strength exceeds valid range of the Guntelberg approximation")

log_f = (
-_debye_parameter_activity(temperature) * z**2 * ionic_strength**0.5 / (1 + ionic_strength.magnitude**0.5)
)
log_f = -_debye_parameter_activity(temperature) * z**2 * ionic_strength**0.5 / (1 + ionic_strength.magnitude**0.5)

Check warning on line 286 in src/pyEQL/activity_correction.py

View check run for this annotation

Codecov / codecov/patch

src/pyEQL/activity_correction.py#L286

Added line #L286 was not covered by tests

return math.exp(log_f) * ureg.Quantity(1, "dimensionless")

Expand Down Expand Up @@ -615,9 +616,7 @@ def _pitzer_B_MX(ionic_strength, alpha1, alpha2, beta0, beta1, beta2):
:func:`_pitzer_f1`
"""
coeff = (
beta0 + beta1 * _pitzer_f1(alpha1 * ionic_strength**0.5) + beta2 * _pitzer_f1(alpha2 * ionic_strength**0.5)
)
coeff = beta0 + beta1 * _pitzer_f1(alpha1 * ionic_strength**0.5) + beta2 * _pitzer_f1(alpha2 * ionic_strength**0.5)
return coeff.magnitude


Expand Down
24 changes: 11 additions & 13 deletions src/pyEQL/engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""

import logging
import os
import warnings
from abc import ABC, abstractmethod
Expand All @@ -14,20 +15,17 @@

from phreeqpython import PhreeqPython

# internal pyEQL imports
import pyEQL.activity_correction as ac

# import the parameters database
# the pint unit registry
from pyEQL import ureg
from pyEQL.logging_system import logger
from pyEQL.salt_ion_match import Salt
from pyEQL.utils import standardize_formula

# These are the only elements that are allowed to have parenthetical oxidation states
# PHREEQC will ignore others (e.g., 'Na(1)')
SPECIAL_ELEMENTS = ["S", "C", "N", "Cu", "Fe", "Mn"]

logger = logging.getLogger(__name__)


class EOS(ABC):
"""
Expand Down Expand Up @@ -367,14 +365,14 @@ def get_activity_coefficient(self, solution, solute):
str(solution.temperature),
)

logger.info(
logger.debug(
f"Calculated activity coefficient of species {solute} as {activity_coefficient} based on salt {salt} using Pitzer model"
)
molal = activity_coefficient

# for very low ionic strength, use the Debye-Huckel limiting law
elif solution.ionic_strength.magnitude <= 0.005:
logger.info(
logger.debug(
"Ionic strength = %s. Using Debye-Huckel to calculate activity coefficient." % solution.ionic_strength
)
molal = ac.get_activity_coefficient_debyehuckel(
Expand All @@ -385,7 +383,7 @@ def get_activity_coefficient(self, solution, solute):

# use the Guntelberg approximation for 0.005 < I < 0.1
elif solution.ionic_strength.magnitude <= 0.1:
logger.info(
logger.debug(

Check warning on line 386 in src/pyEQL/engines.py

View check run for this annotation

Codecov / codecov/patch

src/pyEQL/engines.py#L386

Added line #L386 was not covered by tests
"Ionic strength = %s. Using Guntelberg to calculate activity coefficient." % solution.ionic_strength
)
molal = ac.get_activity_coefficient_guntelberg(
Expand All @@ -396,7 +394,7 @@ def get_activity_coefficient(self, solution, solute):

# use the Davies equation for 0.1 < I < 0.5
elif solution.ionic_strength.magnitude <= 0.5:
logger.info(
logger.debug(

Check warning on line 397 in src/pyEQL/engines.py

View check run for this annotation

Codecov / codecov/patch

src/pyEQL/engines.py#L397

Added line #L397 was not covered by tests
"Ionic strength = %s. Using Davies equation to calculate activity coefficient."
% solution.ionic_strength
)
Expand Down Expand Up @@ -544,7 +542,7 @@ def get_osmotic_coefficient(self, solution):
str(solution.temperature),
)

logger.info(
logger.debug(
f"Calculated osmotic coefficient of water as {osmotic_coefficient} based on salt {item.formula} using Pitzer model"
)
effective_osmotic_sum += concentration * osmotic_coefficient
Expand All @@ -554,7 +552,7 @@ def get_osmotic_coefficient(self, solution):
"Cannot calculate osmotic coefficient because Pitzer parameters for salt %s are not specified. Returning unit osmotic coefficient"
% item.formula
)
effective_osmotic_sum += concentration * osmotic_coefficient
return 1

try:
return effective_osmotic_sum / molality_sum
Expand Down Expand Up @@ -621,7 +619,7 @@ def get_solute_volume(self, solution):

pitzer_calc = True

logger.info("Updated solution volume using Pitzer model for solute %s" % salt.formula)
logger.debug("Updated solution volume using Pitzer model for solute %s" % salt.formula)

# add the partial molar volume of any other solutes, except for water
# or the parent salt, which is already accounted for by the Pitzer parameters
Expand All @@ -637,7 +635,7 @@ def get_solute_volume(self, solution):
part_vol = solution.get_property(solute, "size.molar_volume")
if part_vol is not None:
solute_vol += part_vol * ureg.Quantity(mol, "mol")
logger.info("Updated solution volume using direct partial molar volume for solute %s" % solute)
logger.debug("Updated solution volume using direct partial molar volume for solute %s" % solute)

else:
logger.warning(
Expand Down
16 changes: 10 additions & 6 deletions src/pyEQL/equilibrium.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
:license: LGPL, see LICENSE for more details.
"""

# import libraries for scientific functions
import logging
import math

# the pint unit registry
from pyEQL import ureg
from pyEQL.logging_system import logger

logger = logging.getLogger(__name__)

# TODO - not used. Remove?
SPECIES_ALIAISES = {
Expand Down Expand Up @@ -93,11 +95,13 @@ def adjust_temp_vanthoff(equilibrium_constant, enthalpy, temperature, reference_
enthalpy / ureg.R * (1 / reference_temperature.to("K") - 1 / temperature.to("K"))
)

logger.info(
logger.debug(

Check warning on line 98 in src/pyEQL/equilibrium.py

View check run for this annotation

Codecov / codecov/patch

src/pyEQL/equilibrium.py#L98

Added line #L98 was not covered by tests
"Adjusted equilibrium constant K=%s from %s to %s degrees Celsius with Delta H = %s. Adjusted K = %s % equilibrium_constant,reference_temperature,temperature,enthalpy,output"
)

logger.warning("Van't Hoff equation assumes enthalpy is independent of temperature over the range of interest")
logger.info(

Check warning on line 102 in src/pyEQL/equilibrium.py

View check run for this annotation

Codecov / codecov/patch

src/pyEQL/equilibrium.py#L102

Added line #L102 was not covered by tests
"Note that the Van't Hoff equation assumes enthalpy is independent of temperature over the range of interest"
)
return output


Expand Down Expand Up @@ -137,7 +141,7 @@ def adjust_temp_arrhenius(
activation_energy / ureg.R * (1 / reference_temperature.to("K") - 1 / temperature.to("K"))
)

logger.info(
logger.debug(

Check warning on line 144 in src/pyEQL/equilibrium.py

View check run for this annotation

Codecov / codecov/patch

src/pyEQL/equilibrium.py#L144

Added line #L144 was not covered by tests
"Adjusted parameter %s from %s to %s degrees Celsius with Activation Energy = %s kJ/mol. Adjusted value = %s % rate_constant,reference_temperature,temperature,activation_energy,output"
)

Expand Down Expand Up @@ -215,7 +219,7 @@ def alpha(n, pH, pKa_list):

# return the desired distribution factor
alpha = terms_list[n] / sum(terms_list)
logger.info(
logger.debug(
"Calculated %s-deprotonated acid distribution coefficient of %s for pKa=%s at pH %s % n,alpha,pKa_list,pH"
)
return alpha
5 changes: 4 additions & 1 deletion src/pyEQL/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
:license: LGPL, see LICENSE for more details.
"""

import logging
import math
from typing import Literal

from monty.dev import deprecated

from pyEQL import Solution, ureg
from pyEQL.logging_system import logger

logger = logging.getLogger(__name__)


def gibbs_mix(solution1: Solution, solution2: Solution):
Expand Down
78 changes: 0 additions & 78 deletions src/pyEQL/logging_system.py

This file was deleted.

1 change: 1 addition & 0 deletions src/pyEQL/salt_ion_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
:license: LGPL, see LICENSE for more details.
"""

from monty.json import MSONable
from pymatgen.core.ion import Ion

Expand Down
3 changes: 3 additions & 0 deletions src/pyEQL/solute.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from __future__ import annotations

import logging
import warnings
from dataclasses import asdict, dataclass, field
from typing import Literal
Expand All @@ -23,6 +24,8 @@

from pyEQL.utils import standardize_formula

logger = logging.getLogger(__name__)


@dataclass
class Datum:
Expand Down
Loading

0 comments on commit 6e091c4

Please sign in to comment.