Skip to content

Commit

Permalink
Apply extra Ruff rules
Browse files Browse the repository at this point in the history
  • Loading branch information
gerlero committed Nov 2, 2024
1 parent 43ea834 commit e6a0ab5
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 105 deletions.
7 changes: 3 additions & 4 deletions examples/1INFILTR/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@
"""

import itertools
import os
import sys
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np

r_unit = "cm"
t_unit = "h"

_filename = os.path.join(sys.path[0], "Nod_Inf.out")
_filename = Path(__file__).parent / "Nod_Inf.out"


t = []
r = None
theta = []
velocity = []

with open(_filename) as file:
with _filename.open() as file:
for i, line in enumerate(file):
if i <= 10:
continue # Skip time 0
Expand Down
5 changes: 2 additions & 3 deletions examples/HF135/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
"""

import os
import sys
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np

_filename = os.path.join(sys.path[0], "groundwaterFoam_results.csv")
_filename = Path(__file__).parent / "groundwaterFoam_results.csv"

name = "porousMultiphaseFoam"

Expand Down
85 changes: 54 additions & 31 deletions fronts/D.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def constant(D0):
is the simplest supported function.
"""
if D0 <= 0:
raise ValueError("D0 must be positive")
msg = "D0 must be positive"
raise ValueError(msg)

def D(_, derivatives=0):
if derivatives == 0:
Expand Down Expand Up @@ -108,10 +109,12 @@ def from_expr(expr, vectorized=True, max_derivatives=2):
elif not free:
return constant(float(expr))
else:
raise ValueError("expression cannot contain more than one variable")
msg = "expression cannot contain more than one variable"
raise ValueError(msg)

if max_derivatives not in {0, 1, 2}:
raise ValueError("max_derivatives must be 0, 1 or 2")
msg = "max_derivatives must be 0, 1 or 2"
raise ValueError(msg)

if max_derivatives == 0:
func = sympy.lambdify(theta, expr, modules=np)
Expand Down Expand Up @@ -152,9 +155,8 @@ def D(theta, derivatives=0):
if derivatives == 2 and max_derivatives == 2:
return funcs[0](theta), funcs[1](theta), funcs[2](theta)

raise ValueError(
f"derivatives must be one of {{{', '.join(str(n) for n in range(max_derivatives+1))}}}"
)
msg = f"derivatives must be one of {{{', '.join(str(n) for n in range(max_derivatives+1))}}}"
raise ValueError(msg)

else:
f0v = sympy.lambdify(theta, exprs[0], modules=np)
Expand All @@ -179,9 +181,8 @@ def D(theta, derivatives=0):
if derivatives == 2 and max_derivatives == 2:
return (*f01(theta), f2(theta))

raise ValueError(
f"derivatives must be one of {{{', '.join(str(n) for n in range(max_derivatives+1))}}}"
)
msg = f"derivatives must be one of {{{', '.join(str(n) for n in range(max_derivatives+1))}}}"
raise ValueError(msg)

return D

Expand Down Expand Up @@ -240,7 +241,8 @@ def D(theta, derivatives=0):
if derivatives == 2:
return D, dD_dtheta, d2D_dtheta2

raise ValueError("derivatives must be 0, 1, or 2")
msg = "derivatives must be 0, 1, or 2"
raise ValueError(msg)

return D

Expand Down Expand Up @@ -278,18 +280,23 @@ def _as_Ks(Ks=None, k=None, nu=1e-6, g=9.81):
"""
if Ks is not None:
if k is not None:
raise TypeError("cannot pass both Ks and k")
msg = "cannot pass both Ks and k"
raise TypeError(msg)
if Ks <= 0:
raise ValueError("Ks must be positive")
msg = "Ks must be positive"
raise ValueError(msg)
return Ks

if k is not None:
if k <= 0:
raise ValueError("k must be positive")
msg = "k must be positive"
raise ValueError(msg)
if nu <= 0:
raise ValueError("nu must be positive")
msg = "nu must be positive"
raise ValueError(msg)
if g <= 0:
raise ValueError("g must be positive")
msg = "g must be positive"
raise ValueError(msg)
return g * k / nu

return 1
Expand Down Expand Up @@ -373,12 +380,14 @@ def brooks_and_corey(
Papers, Colorado State University, 1964, vol. 24, p. 37.
"""
if alpha <= 0:
raise ValueError("alpha must be positive")
msg = "alpha must be positive"
raise ValueError(msg)

Ks = _as_Ks(Ks=Ks, k=k, nu=nu, g=g)

if theta_range[1] <= theta_range[0]:
raise ValueError("theta_range[1] must be greater than theta_range[0]")
msg = "theta_range[1] must be greater than theta_range[0]"
raise ValueError(msg)

# - Code generated with functionstr() from ../symbolic/generate.py - #
# Source: ../symbolic/brooks_and_corey.py
Expand All @@ -398,7 +407,8 @@ def D(theta, derivatives=0):
d2D_dtheta2 = x4 * (-3 * x3 + 2 + x2**2 / n**2) / x0**3
if derivatives == 2:
return D, dD_dtheta, d2D_dtheta2
raise ValueError("derivatives must be 0, 1 or 2")
msg = "derivatives must be 0, 1 or 2"
raise ValueError(msg)

# ----------------------- End generated code ----------------------- #

Expand Down Expand Up @@ -498,24 +508,30 @@ def van_genuchten(
"""
if n is not None:
if m is not None:
raise TypeError("cannot pass both n and m")
msg = "cannot pass both n and m"
raise TypeError(msg)
if n <= 1:
raise ValueError("n must be greater than 1.0")
msg = "n must be greater than 1.0"
raise ValueError(msg)
m = 1 - 1 / n

elif m is None:
raise TypeError("either n or m must be given")
msg = "either n or m must be given"
raise TypeError(msg)

if not (0 < m < 1):
raise ValueError("m must be strictly between 0.0 and 1.0")
msg = "m must be strictly between 0.0 and 1.0"
raise ValueError(msg)

if alpha <= 0:
raise ValueError("alpha must be positive")
msg = "alpha must be positive"
raise ValueError(msg)

Ks = _as_Ks(Ks=Ks, k=k, nu=nu, g=g)

if theta_range[1] - theta_range[0] <= 0:
raise ValueError("theta_range[1] must be greater than theta_range[0]")
msg = "theta_range[1] must be greater than theta_range[0]"
raise ValueError(msg)

# - Code generated with functionstr() from ../symbolic/generate.py - #
# Source: ../symbolic/van_genuchten.py
Expand Down Expand Up @@ -578,7 +594,8 @@ def D(theta, derivatives=0):
)
if derivatives == 2:
return D, dD_dtheta, d2D_dtheta2
raise ValueError("derivatives must be 0, 1 or 2")
msg = "derivatives must be 0, 1 or 2"
raise ValueError(msg)

# ----------------------- End generated code ----------------------- #

Expand Down Expand Up @@ -825,7 +842,8 @@ def D(theta, derivatives=0):
)
if derivatives == 2:
return D, dD_dtheta, d2D_dtheta2
raise ValueError("derivatives must be 0, 1 or 2")
msg = "derivatives must be 0, 1 or 2"
raise ValueError(msg)

# ----------------------- End generated code ----------------------- #

Expand Down Expand Up @@ -919,7 +937,8 @@ def D(theta, derivatives=0):
)
if derivatives == 2:
return D, dD_dtheta, d2D_dtheta2
raise ValueError("derivatives must be 0, 1 or 2")
msg = "derivatives must be 0, 1 or 2"
raise ValueError(msg)

# ----------------------- End generated code ----------------------- #

Expand Down Expand Up @@ -1014,7 +1033,8 @@ def D(theta, derivatives=0):
if derivatives == 2:
return D, dD_dtheta, d2D_dtheta2

raise ValueError("derivatives must be 0, 1, or 2")
msg = "derivatives must be 0, 1, or 2"
raise ValueError(msg)

return D

Expand Down Expand Up @@ -1046,15 +1066,18 @@ def _checked(D, theta=None):
try:
D_, dD_dtheta = D(theta, 1)
except (ValueError, ArithmeticError) as e:
raise ValueError(f"D({theta}, 1) failed with {e.__class__.__name__}") from e
msg = f"D({theta}, 1) failed with {e.__class__.__name__}"
raise ValueError(msg) from e

try:
D_ = float(D_)
dD_dtheta = float(dD_dtheta)
except TypeError as e:
raise ValueError(f"D({theta}, 1) returned wrong type") from e
msg = f"D({theta}, 1) returned wrong type"
raise ValueError(msg) from e

if not np.isfinite(D_) or D_ <= 0 or not np.isfinite(dD_dtheta):
raise ValueError(f"D({theta}, 1) returned invalid value")
msg = f"D({theta}, 1) returned invalid value"
raise ValueError(msg)

return D_
11 changes: 6 additions & 5 deletions fronts/_boltzmann.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,13 @@ def as_o(r=None, t=None, o=None):
"""
if o is not None:
if r is not None or t is not None:
raise TypeError("must pass either r and t, or just o")
msg = "must pass either r and t, or just o"
raise TypeError(msg)
return o

if r is None or t is None:
raise TypeError("must pass either r and t, or just o")
msg = "must pass either r and t, or just o"
raise TypeError(msg)
return _o(r, t)


Expand Down Expand Up @@ -312,9 +314,8 @@ def ode(D, radial=False, catch_errors=False):
try:
k = _k[radial]
except KeyError:
raise ValueError(
f"radial must be one of {{{', '.join(repr(key) for key in _k)}}}"
) from None
msg = f"radial must be one of {{{', '.join(repr(key) for key in _k)}}}"
raise ValueError(msg) from None

def fun(o, y):
theta, dtheta_do = y
Expand Down
13 changes: 8 additions & 5 deletions fronts/_inverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def inverse(o, samples):
first derivative at ``theta``
* ``D(theta, 2)`` returns the value of :math:`D`, its first
derivative, and its second derivative at ``theta``
In all cases, the argument ``theta`` may be a single float or a NumPy
array.
Expand All @@ -66,20 +66,22 @@ def inverse(o, samples):
[1] GERLERO, G. S.; BERLI, C. L. A.; KLER, P. A. Open-source
high-performance software packages for direct and inverse solving of
horizontal capillary flow. Capillarity, 2023, vol. 6, no. 2, pp. 31-40.
[2] BRUCE, R. R.; KLUTE, A. The measurement of soil moisture diffusivity.
Soil Science Society of America Journal, 1956, vol. 20, no. 4, pp. 458-462.
"""
o = np.asarray(o)

if not np.all(np.diff(o) > 0):
raise ValueError("o must be strictly increasing")
msg = "o must be strictly increasing"
raise ValueError(msg)

samples = np.asarray(samples)

dsamples = np.diff(samples)
if not (np.all(dsamples >= -1e-12) or np.all(dsamples <= 1e-12)):
raise ValueError("samples must be monotonic")
msg = "samples must be monotonic"
raise ValueError(msg)

i = samples[-1]

Expand Down Expand Up @@ -118,7 +120,8 @@ def D(theta, derivatives=0):
if derivatives == 2:
return D, dD_dtheta, d2D_dtheta2

raise ValueError("derivatives must be 0, 1, or 2")
msg = "derivatives must be 0, 1, or 2"
raise ValueError(msg)

return D

Expand Down
Loading

0 comments on commit e6a0ab5

Please sign in to comment.