Skip to content

Commit

Permalink
black
Browse files Browse the repository at this point in the history
  • Loading branch information
jaclark5 committed Dec 23, 2024
1 parent e5ffa36 commit 742c602
Show file tree
Hide file tree
Showing 43 changed files with 715 additions and 2,247 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jobs:
- name: Flake8
shell: bash -l {0}
run: |
python -m flake8 despasito --count --ignore=E741,W503 --max-line-length=92 --show-source --statistics
python -m flake8 despasito --count --ignore=E741,W503 --max-line-length=120 --per-file-ignores="__init__.py:F401" --show-source --statistics
paper:
runs-on: ubuntu-latest
Expand Down
9 changes: 2 additions & 7 deletions despasito/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ def initiate_logger(console=None, log_file=None, verbose=30):
# Set up logging to console
if console and handler_console is None:
console_handler = logging.StreamHandler() # sys.stderr
console_handler.setFormatter(
logging.Formatter("[%(levelname)s](%(name)s): %(message)s")
)
console_handler.setFormatter(logging.Formatter("[%(levelname)s](%(name)s): %(message)s"))
console_handler.setLevel(verbose)
logger.addHandler(console_handler)
elif console:
Expand All @@ -75,10 +73,7 @@ def initiate_logger(console=None, log_file=None, verbose=30):

log_file_handler = logging.handlers.RotatingFileHandler(log_file)
log_file_handler.setFormatter(
logging.Formatter(
"%(asctime)s [%(levelname)s](%(name)s:%(funcName)s:%(lineno)d): "
+ "%(message)s"
)
logging.Formatter("%(asctime)s [%(levelname)s](%(name)s:%(funcName)s:%(lineno)d): " + "%(message)s")
)
log_file_handler.setLevel(verbose)
logger.addHandler(log_file_handler)
Expand Down
8 changes: 2 additions & 6 deletions despasito/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,15 @@
os.remove(args.logFile)
log_file_handler = logging.handlers.RotatingFileHandler(args.logFile)
log_file_handler.setFormatter(
logging.Formatter(
"%(asctime)s [%(levelname)s](%(name)s:%(funcName)s:%(lineno)d): %(message)s"
)
logging.Formatter("%(asctime)s [%(levelname)s](%(name)s:%(funcName)s:%(lineno)d): %(message)s")
)
log_file_handler.setLevel(args.verbose)
logger.addHandler(log_file_handler)

if args.console:
# Set up logging to console
console_handler = logging.StreamHandler() # sys.stderr
console_handler.setFormatter(
logging.Formatter("[%(levelname)s](%(name)s): %(message)s")
)
console_handler.setFormatter(logging.Formatter("[%(levelname)s](%(name)s): %(message)s"))
console_handler.setLevel(args.verbose)
logger.addHandler(console_handler)

Expand Down
2 changes: 1 addition & 1 deletion despasito/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.3.0+16.gcc3fbe0.dirty"
__version__ = "0.3.0+28.ge5ffa36.dirty"
28 changes: 7 additions & 21 deletions despasito/equations_of_state/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,15 @@ def __init__(self, numba=True, cython=False, python=False):

def __str__(self):

string = "Compilation: numba {}, cython {}, python {}".format(
self.numba, self.cython, self.python
)
string = "Compilation: numba {}, cython {}, python {}".format(self.numba, self.cython, self.python)

return string


logger = logging.getLogger(__name__)


def initiate_eos(
eos="saft.gamma_mie", numba=True, cython=False, python=False, **kwargs
):
def initiate_eos(eos="saft.gamma_mie", numba=True, cython=False, python=False, **kwargs):
"""
Interface between the user and our library of equations of state (EOS).
Expand Down Expand Up @@ -69,39 +65,29 @@ def initiate_eos(

kwargs["method_stat"] = method_stat(numba=numba, cython=cython, python=python)

factory_families = [
"saft"
] # Eos families in this list have a general object with a factory to import
factory_families = ["saft"] # Eos families in this list have a general object with a factory to import
# relevant modules

logger.info("Using EOS: {}".format(eos))

try:
eos_fam, eos_type = eos.split(".")
except Exception:
raise ValueError(
"Input should be in the form EOSfamily.EOSname (e.g. saft.gamme_mie)."
)
raise ValueError("Input should be in the form EOSfamily.EOSname (e.g. saft.gamme_mie).")

class_name = "EosType"
try:
if eos_fam in factory_families:
eos_module = import_module(
"." + eos_fam, package="despasito.equations_of_state." + eos_fam
)
eos_module = import_module("." + eos_fam, package="despasito.equations_of_state." + eos_fam)
kwargs["saft_name"] = eos_type

else:
eos_module = import_module(
"." + eos_type, package="despasito.equations_of_state." + eos_fam
)
eos_module = import_module("." + eos_type, package="despasito.equations_of_state." + eos_fam)
eos_class = getattr(eos_module, class_name)
except AttributeError:
raise ImportError(
"Based on your input, '{}', we expect the class, {}, in a module, {},"
" found in the package, {}, which indicates the EOS family.".format(
eos, class_name, eos_type, eos_fam
)
" found in the package, {}, which indicates the EOS family.".format(eos, class_name, eos_type, eos_fam)
)
instance = eos_class(**kwargs)

Expand Down
34 changes: 9 additions & 25 deletions despasito/equations_of_state/combining_rule_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,7 @@ def volumetric_geometric_mean(beadA, beadB, parameter, weighting_parameters=[]):

tmp1 = np.sqrt(beadA[parameter] * beadB[parameter])
param2 = weighting_parameters[0]
tmp2 = (
np.sqrt((beadA[param2] ** 3) * (beadB[param2] ** 3))
* 8
/ ((beadA[param2] + beadB[param2]) ** 3)
)
tmp2 = np.sqrt((beadA[param2] ** 3) * (beadB[param2] ** 3)) * 8 / ((beadA[param2] + beadB[param2]) ** 3)
return tmp1 * tmp2


Expand Down Expand Up @@ -115,9 +111,9 @@ def weighted_mean(beadA, beadB, parameter, weighting_parameters=[]):
"""

param2 = weighting_parameters[0]
parameter12 = (
beadA[parameter] * beadA[param2] + beadB[parameter] * beadB[param2]
) / (beadA[param2] + beadB[param2])
parameter12 = (beadA[parameter] * beadA[param2] + beadB[parameter] * beadB[param2]) / (
beadA[param2] + beadB[param2]
)

return parameter12

Expand Down Expand Up @@ -173,23 +169,15 @@ def square_well_berthelot(beadA, beadB, parameter, weighting_parameters=[]):
param2, param3 = weighting_parameters[0], weighting_parameters[1]

tmp1 = np.sqrt(beadA[parameter] * beadB[parameter])
tmp2 = (
np.sqrt((beadA[param2] ** 3) * (beadB[param2] ** 3))
* 8
/ ((beadA[param2] + beadB[param2]) ** 3)
)
tmp2 = np.sqrt((beadA[param2] ** 3) * (beadB[param2] ** 3)) * 8 / ((beadA[param2] + beadB[param2]) ** 3)

param3_12 = weighted_mean(beadA, beadB, param3, weighting_parameters=[param2])
tmp3 = np.sqrt((beadA[param3] ** 3 - 1) * (beadB[param3] ** 3 - 1)) / (
param3_12**3 - 1
)
tmp3 = np.sqrt((beadA[param3] ** 3 - 1) * (beadB[param3] ** 3 - 1)) / (param3_12**3 - 1)

return tmp1 * tmp2 * tmp3


def multipole(
beadA, beadB, parameter, temperature=None, mode="curve fit", scaled=False
):
def multipole(beadA, beadB, parameter, temperature=None, mode="curve fit", scaled=False):
r"""
Calculates cross interaction parameter with the multipole combining rules from
the plug-in `MAPSCI <https://github.com/jaclark5/mapsci>`_.
Expand Down Expand Up @@ -239,17 +227,13 @@ def multipole(
tmp[key]["sigma"] = value["sigma"] * 10 # convert from nm to angstroms

if mode == "curve fit":
dict_cross, _ = mr.extended_combining_rules_fitting(
tmp, temperature, shape_factor_scale=shape_factor_scale
)
dict_cross, _ = mr.extended_combining_rules_fitting(tmp, temperature, shape_factor_scale=shape_factor_scale)
elif mode == "analytical":
dict_cross, _ = mr.extended_combining_rules_analytical(
tmp, temperature, shape_factor_scale=shape_factor_scale
)
else:
raise ValueError(
"Multipole mixing rule must be either 'curve fit' or 'analytical'."
)
raise ValueError("Multipole mixing rule must be either 'curve fit' or 'analytical'.")
output = dict_cross["beadA"]["beadB"]
else:
logger.warning("Temperature is None, using geometric mean.")
Expand Down
60 changes: 13 additions & 47 deletions despasito/equations_of_state/cubic/peng_robinson.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,14 @@ def __init__(self, **kwargs):
self._test_critical = [False for _ in self.beads]
self._test_parameters = [False for _ in self.beads]
for i, bead in enumerate(self.beads):
if (
"omega" in self.bead_library[bead]
and "kappa" not in self.bead_library[bead]
):
if "omega" in self.bead_library[bead] and "kappa" not in self.bead_library[bead]:
self._test_kappa[i] = True

self._test_critical[i] = (
"Tc" in self.bead_library[bead] and "Pc" in self.bead_library[bead]
)
self._test_parameters[i] = (
"ai" in self.bead_library[bead] and "bi" in self.bead_library[bead]
)
self._test_critical[i] = "Tc" in self.bead_library[bead] and "Pc" in self.bead_library[bead]
self._test_parameters[i] = "ai" in self.bead_library[bead] and "bi" in self.bead_library[bead]

if not self._test_critical[i] and not self._test_parameters[i]:
raise ValueError(
"Either 'Tc' or 'Pc' was not provided for component: {}".format(
bead
)
)
raise ValueError("Either 'Tc' or 'Pc' was not provided for component: {}".format(bead))

# Cross interaction parameters
if "cross_library" in kwargs:
Expand Down Expand Up @@ -155,9 +144,7 @@ def _calc_temp_dependent_parameters(self, T):
for i, bead in enumerate(self.beads):
if "kappa" in self.bead_library[bead]:
self.eos_dict["alpha"][i] = (
1
+ self.bead_library[bead]["kappa"]
* (1 - np.sqrt(T / self.bead_library[bead]["Tc"]))
1 + self.bead_library[bead]["kappa"] * (1 - np.sqrt(T / self.bead_library[bead]["Tc"]))
) ** 2
else:
self.eos_dict["alpha"][i] = 1.0
Expand Down Expand Up @@ -235,11 +222,8 @@ def pressure(self, rho, T, xi):
elif not isinstance(rho, np.ndarray):
rho = np.array(rho)

P = constants.R * self.T * rho / (
1 - self.eos_dict["bij"] * rho
) - rho**2 * self.eos_dict["aij"] / (
(1 + self.eos_dict["bij"] * rho)
+ rho * self.eos_dict["bij"] * (1 - self.eos_dict["bij"] * rho)
P = constants.R * self.T * rho / (1 - self.eos_dict["bij"] * rho) - rho**2 * self.eos_dict["aij"] / (
(1 + self.eos_dict["bij"] * rho) + rho * self.eos_dict["bij"] * (1 - self.eos_dict["bij"] * rho)
)

return P
Expand Down Expand Up @@ -296,11 +280,7 @@ def fugacity_coefficient(self, P, rho, xi, T):
A = self.eos_dict["aij"] * P / tmp_RT**2

sqrt2 = np.sqrt(2.0)
tmp1 = (
A
/ (2.0 * sqrt2 * B)
* np.log((Z + (1 + sqrt2) * B) / (Z + (1 - sqrt2) * B))
)
tmp1 = A / (2.0 * sqrt2 * B) * np.log((Z + (1 + sqrt2) * B) / (Z + (1 - sqrt2) * B))
tmp3 = Bi * (Z - 1) / B - np.log(Z - B)
tmp2 = np.zeros(len(xi))

Expand Down Expand Up @@ -363,15 +343,8 @@ def update_parameter(self, param_name, bead_names, param_value):
Value of parameter
"""

if (
param_name in ["ai", "bi"]
and self._test_critical[self.beads.index(bead_names[0])]
):
raise ValueError(
"Bead, {}, initialized with critical properties, not ai and bi".format(
bead_names[0]
)
)
if param_name in ["ai", "bi"] and self._test_critical[self.beads.index(bead_names[0])]:
raise ValueError("Bead, {}, initialized with critical properties, not ai and bi".format(bead_names[0]))
super().update_parameter(param_name, bead_names, param_value)

def parameter_refresh(self):
Expand All @@ -384,10 +357,7 @@ def parameter_refresh(self):
"""

for i, bead in enumerate(self.beads):
if (
"omega" in self.bead_library[bead]
and "kappa" not in self.bead_library[bead]
):
if "omega" in self.bead_library[bead] and "kappa" not in self.bead_library[bead]:
self.bead_library[bead]["kappa"] = (
0.37464
+ 1.54226 * self.bead_library[bead]["omega"]
Expand All @@ -396,14 +366,10 @@ def parameter_refresh(self):

if self._test_critical[i] and not self._test_parameters[i]:
self.bead_library[bead]["ai"] = (
0.45723553
* (constants.R * self.bead_library[bead]["Tc"]) ** 2
/ self.bead_library[bead]["Pc"]
0.45723553 * (constants.R * self.bead_library[bead]["Tc"]) ** 2 / self.bead_library[bead]["Pc"]
)
self.bead_library[bead]["bi"] = 0.07779607 * (
constants.R
* self.bead_library[bead]["Tc"]
/ self.bead_library[bead]["Pc"]
constants.R * self.bead_library[bead]["Tc"] / self.bead_library[bead]["Pc"]
)

parameters = ["ai", "bi"]
Expand Down
38 changes: 8 additions & 30 deletions despasito/equations_of_state/eos_toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ def remove_insignificant_components(xi_old, massi_old):
return xi_new, massi_new


def partial_density_central_difference(
xi, rho, T, func, step_size=1e-2, log_method=False
):
def partial_density_central_difference(xi, rho, T, func, step_size=1e-2, log_method=False):
"""
Take the derivative of a dependent variable calculated with a given function
using the central difference method.
Expand Down Expand Up @@ -164,9 +162,7 @@ def calc_massi(molecular_composition, bead_library, beads):
if "mass" in bead_library[bead]:
massi[i] += molecular_composition[i, k] * bead_library[bead]["mass"]
else:
raise ValueError(
"The mass for bead, {}, was not provided.".format(bead)
)
raise ValueError("The mass for bead, {}, was not provided.".format(bead))

return massi

Expand Down Expand Up @@ -205,9 +201,7 @@ def extract_property(prop, bead_library, beads, default=None):
prop_array[i] = bead_library[bead][prop]
else:
if default is None:
raise ValueError(
"The property {} for bead, {}, was not provided.".format(prop, bead)
)
raise ValueError("The property {} for bead, {}, was not provided.".format(prop, bead))
else:
prop_array[i] = default

Expand Down Expand Up @@ -249,8 +243,7 @@ def check_bead_parameters(bead_library0, parameter_defaults):
)
else:
raise ValueError(
"Parameter, {}, should have been defined for parametrized "
"group, {}.".format(parameter, bead)
"Parameter, {}, should have been defined for parametrized " "group, {}.".format(parameter, bead)
)

return bead_library
Expand Down Expand Up @@ -299,27 +292,14 @@ def cross_interaction_from_dict(beads, bead_library, combining_dict, cross_libra
for j, beadname2 in enumerate(beads):
if j > i:
for key in combining_dict:
if (
cross_library.get(beadname, {})
.get(beadname2, {})
.get(key, None)
is not None
):
if cross_library.get(beadname, {}).get(beadname2, {}).get(key, None) is not None:
output[key][i, j] = cross_library[beadname][beadname2][key]
elif (
cross_library.get(beadname2, {})
.get(beadname, {})
.get(key, None)
is not None
):
elif cross_library.get(beadname2, {}).get(beadname, {}).get(key, None) is not None:
output[key][i, j] = cross_library[beadname2][beadname][key]
else:
try:
tmp = combining_rules(
bead_library[beadname],
bead_library[beadname2],
key,
**combining_dict[key]
bead_library[beadname], bead_library[beadname2], key, **combining_dict[key]
)
except Exception:
raise ValueError(
Expand Down Expand Up @@ -369,9 +349,7 @@ def construct_dummy_bead_library(input_dict, keys=None):
keys = [str(x) for x in range(len(input_dict[parameter]))]
flag = True
if len(keys) != len(input_dict[parameter]):
raise ValueError(
"Number of keys is not equal to the number of quantities given"
)
raise ValueError("Number of keys is not equal to the number of quantities given")

for i, bead in enumerate(keys):
if bead not in output:
Expand Down
Loading

0 comments on commit 742c602

Please sign in to comment.