Skip to content

Commit

Permalink
refactor: run nbQA tools pyupgrade, isort and black
Browse files Browse the repository at this point in the history
  • Loading branch information
mwtoews committed Jul 20, 2023
1 parent 9059a44 commit 2328121
Show file tree
Hide file tree
Showing 127 changed files with 3,817 additions and 5,030 deletions.
4 changes: 2 additions & 2 deletions .doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
lines += " :numbered:\n"
lines += " :maxdepth: 1\n\n"
for base_name in gwf_list:
lines += " {} ".format(base_name)
lines += f" {base_name} "
lines += "<{}>\n".format(os.path.join("_notebooks", base_name + ".ipynb"))
for base_name in gwt_list:
lines += " {} ".format(base_name)
lines += f" {base_name} "
lines += "<{}>\n".format(os.path.join("_notebooks", base_name + ".ipynb"))
lines += "\n\n"
f.write(lines)
Expand Down
11 changes: 6 additions & 5 deletions .doc/test_build.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Build files for sphinx.
import os
import sys
import shutil
from subprocess import Popen, PIPE
import sys
from subprocess import PIPE, Popen

import flopy

# -- determine if running on CI or rtd
Expand Down Expand Up @@ -83,14 +84,14 @@

# -- remove ./_notebooks if it exists ----------------------------------------
copy_pth = os.path.join("_notebooks")
print("clean up {}".format(copy_pth))
print(f"clean up {copy_pth}")
if os.path.isdir(copy_pth):
shutil.rmtree(copy_pth)

# -- copy executed notebooks to ./_notebooks ---------------------------------
print("copy files in {} -> {}".format(dst_pth, copy_pth))
print(f"copy files in {dst_pth} -> {copy_pth}")
shutil.copytree(dst_pth, copy_pth)

# -- clean up (remove) dst_pth directory -------------------------------------
print("clean up {}".format(dst_pth))
print(f"clean up {dst_pth}")
shutil.rmtree(dst_pth)
112 changes: 43 additions & 69 deletions common/analytical.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np
from scipy.special import erfc
from scipy.special import erf
from scipy.special import erf, erfc


def diffusion(x, t, v, R, D):
Expand Down Expand Up @@ -35,15 +34,15 @@ def diffusion(x, t, v, R, D):
return t1 + t2 * t3


class Wexler1d(object):
class Wexler1d:
"""
Analytical solution for 1D transport with inflow at a concentration of 1.
at x=0 and a third-type bound at location l.
Wexler Page 17 and Van Genuchten and Alves pages 66-67
"""

def betaeqn(self, beta, d, v, l):
return beta / np.tan(beta) - beta ** 2 * d / v / l + v * l / 4.0 / d
return beta / np.tan(beta) - beta**2 * d / v / l + v * l / 4.0 / d

def fprimebetaeqn(self, beta, d, v, l):
"""
Expand Down Expand Up @@ -103,28 +102,18 @@ def analytical(self, x, t, v, l, d, tol=1.0e-20, nval=5000):
sigma = 0.0
betalist = self.root3(d, v, l, nval=nval)
for i, bi in enumerate(betalist):

denom = bi ** 2 + (v * l / 2.0 / d) ** 2 + v * l / d
denom = bi**2 + (v * l / 2.0 / d) ** 2 + v * l / d
x1 = (
bi
* (
bi * np.cos(bi * x / l)
+ v * l / 2.0 / d * np.sin(bi * x / l)
)
* (bi * np.cos(bi * x / l) + v * l / 2.0 / d * np.sin(bi * x / l))
/ denom
)

denom = bi ** 2 + (v * l / 2.0 / d) ** 2
x2 = np.exp(-1 * bi ** 2 * d * t / l ** 2) / denom
denom = bi**2 + (v * l / 2.0 / d) ** 2
x2 = np.exp(-1 * bi**2 * d * t / l**2) / denom

sigma += x1 * x2
term1 = (
2.0
* v
* l
/ d
* np.exp(v * x / 2.0 / d - v ** 2 * t / 4.0 / d)
)
term1 = 2.0 * v * l / d * np.exp(v * x / 2.0 / d - v**2 * t / 4.0 / d)
conc = 1.0 - term1 * sigma
if i > 0:
diff = abs(conc - concold)
Expand Down Expand Up @@ -160,38 +149,28 @@ def analytical2(self, x, t, v, l, d, e=0.0, tol=1.0e-20, nval=5000):
normalized concentration value
"""
u = v ** 2 + 4.0 * e * d
u = v**2 + 4.0 * e * d
u = np.sqrt(u)
sigma = 0.0
denom = (u + v) / 2.0 / v - (u - v) ** 2.0 / 2.0 / v / (
u + v
) * np.exp(-u * l / d)
denom = (u + v) / 2.0 / v - (u - v) ** 2.0 / 2.0 / v / (u + v) * np.exp(
-u * l / d
)
term1 = np.exp((v - u) * x / 2.0 / d) + (u - v) / (u + v) * np.exp(
(v + u) * x / 2.0 / d - u * l / d
)
term1 = term1 / denom
term2 = (
2.0
* v
* l
/ d
* np.exp(v * x / 2.0 / d - v ** 2 * t / 4.0 / d - e * t)
)
term2 = 2.0 * v * l / d * np.exp(v * x / 2.0 / d - v**2 * t / 4.0 / d - e * t)
betalist = self.root3(d, v, l, nval=nval)
for i, bi in enumerate(betalist):

denom = bi ** 2 + (v * l / 2.0 / d) ** 2 + v * l / d
denom = bi**2 + (v * l / 2.0 / d) ** 2 + v * l / d
x1 = (
bi
* (
bi * np.cos(bi * x / l)
+ v * l / 2.0 / d * np.sin(bi * x / l)
)
* (bi * np.cos(bi * x / l) + v * l / 2.0 / d * np.sin(bi * x / l))
/ denom
)

denom = bi ** 2 + (v * l / 2.0 / d) ** 2 + e * l ** 2 / d
x2 = np.exp(-1 * bi ** 2 * d * t / l ** 2) / denom
denom = bi**2 + (v * l / 2.0 / d) ** 2 + e * l**2 / d
x2 = np.exp(-1 * bi**2 * d * t / l**2) / denom

sigma += x1 * x2

Expand All @@ -212,18 +191,14 @@ class Wexler3d:
"""

def calcgamma(self, x, y, z, xc, yc, zc, dx, dy, dz):
gam = np.sqrt(
(x - xc) ** 2 + dx / dy * (y - yc) ** 2 + dx / dz * (z - zc) ** 2
)
gam = np.sqrt((x - xc) ** 2 + dx / dy * (y - yc) ** 2 + dx / dz * (z - zc) ** 2)
return gam

def calcbeta(self, v, dx, gam, lam):
beta = np.sqrt(v ** 2 + 4.0 * dx * gam * lam)
beta = np.sqrt(v**2 + 4.0 * dx * gam * lam)
return beta

def analytical(
self, x, y, z, t, v, xc, yc, zc, dx, dy, dz, n, q, lam=0.0, c0=1.0
):
def analytical(self, x, y, z, t, v, xc, yc, zc, dx, dy, dz, n, q, lam=0.0, c0=1.0):
gam = self.calcgamma(x, y, z, xc, yc, zc, dx, dy, dz)
beta = self.calcbeta(v, dx, gam, lam)
term1 = (
Expand All @@ -244,9 +219,7 @@ def analytical(
)
return term1 * (term2 + term3)

def multiwell(
self, x, y, z, t, v, xc, yc, zc, dx, dy, dz, n, ql, lam=0.0, c0=1.0
):
def multiwell(self, x, y, z, t, v, xc, yc, zc, dx, dy, dz, n, ql, lam=0.0, c0=1.0):
shape = self.analytical(
x, y, z, t, v, xc[0], yc[0], zc[0], dx, dy, dz, n, ql[0], lam
).shape
Expand Down Expand Up @@ -406,10 +379,10 @@ def hechtMendez_SS_3d(
"""

# calculate transverse horizontal heat dispersion
Dy = ath * (va ** 2 / abs(va)) + thermdiff
Dy = ath * (va**2 / abs(va)) + thermdiff
t2 = erf(Y3d / (4 * np.sqrt(Dy * (x_pos / va))))

Dz = atv * (va ** 2 / abs(va)) + thermdiff
Dz = atv * (va**2 / abs(va)) + thermdiff
t3 = erf(Z3d / (4 * np.sqrt(Dz * (x_pos / va))))

# initial temperature at the source
Expand Down Expand Up @@ -449,19 +422,15 @@ def hechtMendezSS(x_pos, y, a, F0, va, n, rhow, cw, thermdiff):
"""

# calculate transverse horizontal heat dispersion
Dth = a * (va ** 2 / abs(va)) + thermdiff
Dth = a * (va**2 / abs(va)) + thermdiff

t1 = F0 / (
va * n * rhow * cw * ((4 * np.pi * Dth * (x_pos / va)) ** (0.5))
)
t2 = np.exp((-1 * va * y ** 2) / (4 * Dth * x_pos))
t1 = F0 / (va * n * rhow * cw * ((4 * np.pi * Dth * (x_pos / va)) ** (0.5)))
t2 = np.exp((-1 * va * y**2) / (4 * Dth * x_pos))
sln = t1 * t2
return sln


def hechtMendez3d(
x_pos, t, Y, Z, al, ath, atv, thermdiff, va, n, R, Fplanar, cw, rhow
):
def hechtMendez3d(x_pos, t, Y, Z, al, ath, atv, thermdiff, va, n, R, Fplanar, cw, rhow):
"""
Calculate the analytical solution for three-dimensional changes in
temperature based on the solution provided in the appendix of Hecht-Mendez
Expand Down Expand Up @@ -502,30 +471,35 @@ def hechtMendez3d(
"""
To_planar = Fplanar / (va * n * rhow * cw)

Dl = al * (va ** 2 / abs(va)) + thermdiff
Dl = al * (va**2 / abs(va)) + thermdiff
numer = R * x_pos - va * t
denom = 2 * np.sqrt(Dl * R * t)

t1 = (To_planar / 2) * erfc(numer / denom)

Dth = ath * (va ** 2 / abs(va)) + thermdiff
Dth = ath * (va**2 / abs(va)) + thermdiff
t2 = erf(Y / (4 * np.sqrt(Dth * (x_pos / va))))

Dtv = atv * (va ** 2 / abs(va)) + thermdiff
Dtv = atv * (va**2 / abs(va)) + thermdiff
t3 = erf(Z / (4 * np.sqrt(Dtv * (x_pos / va))))

sln = t1 * t2 * t3
return sln


# Analytical solution for Stallman analysis (Stallman 1965, JGR)
def Stallman(T_az,dT,tau,t,c_rho,darcy_flux,ko,c_w,rho_w,zbotm,nlay):
def Stallman(T_az, dT, tau, t, c_rho, darcy_flux, ko, c_w, rho_w, zbotm, nlay):
zstallman = np.zeros((nlay, 2))
K = np.pi*c_rho/ko/tau
V = darcy_flux*c_w*rho_w/2/ko
a = ((K**2+V**4/4)**0.5+V**2/2)**0.5-V
b = ((K**2+V**4/4)**0.5-V**2/2)**0.5
K = np.pi * c_rho / ko / tau
V = darcy_flux * c_w * rho_w / 2 / ko
a = ((K**2 + V**4 / 4) ** 0.5 + V**2 / 2) ** 0.5 - V
b = ((K**2 + V**4 / 4) ** 0.5 - V**2 / 2) ** 0.5
for i in range(len(zstallman)):
zstallman[i,0] = zbotm[i]
zstallman[i,1] = dT*np.exp(-a*(-zstallman[i,0]))*np.sin(2*np.pi*t/tau-b*(-zstallman[i,0])) + T_az
zstallman[i, 0] = zbotm[i]
zstallman[i, 1] = (
dT
* np.exp(-a * (-zstallman[i, 0]))
* np.sin(2 * np.pi * t / tau - b * (-zstallman[i, 0]))
+ T_az
)
return zstallman

34 changes: 19 additions & 15 deletions common/build_table.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os


def build_table(caption, fpth, arr, headings=None, col_widths=None):
if headings is None:
headings = arr.dtype.names
Expand All @@ -15,7 +16,7 @@ def build_table(caption, fpth, arr, headings=None, col_widths=None):
line += "\t\t\\rowcolor{Gray}\n"
line += "\t\t"
for jdx, name in enumerate(arr.dtype.names):
line += "{}".format(arr[name][idx])
line += f"{arr[name][idx]}"
if jdx < ncols - 1:
line += " & "
line += " \\\\\n"
Expand All @@ -29,6 +30,7 @@ def build_table(caption, fpth, arr, headings=None, col_widths=None):

return


def get_header(caption, label, headings, col_widths=None, center=True, firsthead=False):
ncol = len(headings)
if col_widths is None:
Expand All @@ -42,49 +44,51 @@ def get_header(caption, label, headings, col_widths=None, center=True, firsthead
header = "\\small\n"
header += "\\begin{longtable}[!htbp]{\n"
for col_width in col_widths:
header += 38 * " " + \
"{}".format(align) + \
"{{{}\\linewidth-2\\arraycolsep}}\n".format(col_width)
header += 38 * " " + f"{align}" + f"{{{col_width}\\linewidth-2\\arraycolsep}}\n"
header += 38 * " " + "}\n"
header += "\t\\caption{{{}}} \\label{{{}}} \\\\\n\n".format(caption, label)
header += f"\t\\caption{{{caption}}} \\label{{{label}}} \\\\\n\n"

if firsthead:
header += "\t\\hline \\hline\n"
header += "\t\\rowcolor{Gray}\n"
header += "\t\\rowcolor{Gray}\n"
header += "\t"
for idx, s in enumerate(headings):
header += "\\textbf{{{}}}".format(s)
header += f"\\textbf{{{s}}}"
if idx < len(headings) - 1:
header += " & "
header += " \\\\\n"
header += "\t\\hline\n"
header += "\t\\endfirsthead\n\n"
header += "\t\\hline\n"
header += "\t\\endfirsthead\n\n"

header += "\t\\hline \\hline\n"
header += "\t\\rowcolor{Gray}\n"
header += "\t\\rowcolor{Gray}\n"
header += "\t"
for idx, s in enumerate(headings):
header += "\\textbf{{{}}}".format(s)
header += f"\\textbf{{{s}}}"
if idx < len(headings) - 1:
header += " & "
header += " \\\\\n"
header += "\t\\hline\n"
header += "\t\\endhead\n\n"
header += "\t\\hline\n"
header += "\t\\endhead\n\n"

return header


def get_footer():
return "\t\\hline \\hline\n\\end{longtable}\n\\normalsize\n\n"


def exp_format(v):
s = "{:.2e}".format(v)
s = f"{v:.2e}"
s = s.replace("e-0", "e-")
s = s.replace("e+0", "e+")
# s = s.replace("e", " \\times 10^{") + "}$"
return s


def float_format(v, fmt="{:.2f}"):
return fmt.format(v)


def int_format(v):
return "{:d}".format(v)
return f"{v:d}"
9 changes: 4 additions & 5 deletions common/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
import pathlib as pl
import sys
import time

import matplotlib.pyplot as plt
from IPython import get_ipython
import pathlib as pl

# Setup working directories
work_directories = (
Expand Down Expand Up @@ -46,7 +47,7 @@ def timed(*args, **kw):
name = kw.get("log_name", method.__name__.upper())
kw["log_time"][name] = int((te - ts) * 1000)
else:
print("{} {:,.2f} ms".format(method.__name__, (te - ts) * 1000))
print(f"{method.__name__} {(te - ts) * 1000:,.2f} ms")
return result

return timed
Expand Down Expand Up @@ -77,9 +78,7 @@ def timed(*args, **kw):
extension = sys.argv[idx + 1]
if not extension.startswith("."):
extension = "." + extension
figure_exts = tuple(
plt.gcf().canvas.get_supported_filetypes().keys()
)
figure_exts = tuple(plt.gcf().canvas.get_supported_filetypes().keys())
if extension.lower() in figure_exts:
figure_ext = extension

Expand Down
Loading

0 comments on commit 2328121

Please sign in to comment.