Skip to content

Commit

Permalink
refactor (python): apply ruff check rules ISC, UP and RUF
Browse files Browse the repository at this point in the history
  • Loading branch information
mwtoews committed Nov 27, 2024
1 parent 333da66 commit b004ac8
Show file tree
Hide file tree
Showing 44 changed files with 269 additions and 306 deletions.
2 changes: 1 addition & 1 deletion .doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
lines += (len(lines) - 1) * "-" + "\n\n"
lines += (
"The Jupyter Notebooks used to create the input files and figures for \n"
+ "each of the MODFLOW 6 `examples <examples.html>`_.\n\n"
"each of the MODFLOW 6 `examples <examples.html>`_.\n\n"
)
f.write(lines)

Expand Down
15 changes: 8 additions & 7 deletions etc/ci_create_examples_rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@
if stdout:
print(stdout.decode("utf-8"))
if stderr:
print("Errors:\n{}".format(stderr.decode("utf-8")))
print("Errors:")
print(stderr.decode("utf-8"))

# read restructured text file as a string
print(f"reading...'{dst}'")
Expand All @@ -125,10 +126,10 @@
eq_labels = []
for v in ex_regex.findall(lines):
tag = f"\\label{{{v}}}"
label = " :label: {}".format(v.replace(":", "-"))
label = f" :label: {v.replace(':', '-')}"
eq_labels.append(label)
tag = "`[{0}] <#{0}>`__".format(v)
replace_eq_labels[tag] = ":eq:`{}`".format(v.replace(":", "-"))
tag = f"`[{v}] <#{v}>`__"
replace_eq_labels[tag] = f":eq:`{v.replace(':', '-')}`"

# find figure references in lines
ex_regex = re.compile("\\`(.*?)\\`__")
Expand Down Expand Up @@ -168,7 +169,7 @@
if key in line:
line = line.replace(key, value)

tag = " "
tag = "\xa0" # non-breaking space
if tag in line:
line = line.replace(tag, " ")

Expand Down Expand Up @@ -273,9 +274,9 @@
line += "----------------\n\n"
line += (
"The Jupyter notebook used to create the MODFLOW 6 input files\n"
+ "for this example and post-process the results is:\n\n"
"for this example and post-process the results is:\n\n"
)
line += "* `{0} <../_notebooks/{0}.html>`_\n".format(ex_root)
line += f"* `{ex_root} <../_notebooks/{ex_root}.html>`_\n"
line += "\n"

# Check to see if there is a gif with the same name as the example name
Expand Down
5 changes: 5 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ select = [
"E", # pycodestyle error
"F", # Pyflakes
"I001", # isort - unsorted-imports
# "ISC", # implicitly concatenated strings
"RUF", # Ruff-specific rules
"UP", # Pyupgrade
]
ignore = [
"E501", # line too long TODO FIXME
"E722", # do not use bare `except`
"E741", # ambiguous variable name
"F841", # local variable assigned but never used
"RUF005", # collection literal concatenation
"UP015", # redundant open modes
]
42 changes: 19 additions & 23 deletions scripts/ex-gwe-ates.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ def build_model(sim_name, verts, cell2d, top, botm):
vertices=verts,
cell2d=cell2d,
pname="DISV",
filename="{}.disv".format(gwfname),
filename=f"{gwfname}.disv",
)

# Instantiating MODFLOW 6 node property flow package
Expand All @@ -582,7 +582,7 @@ def build_model(sim_name, verts, cell2d, top, botm):
save_specific_discharge=True,
save_saturation=True,
pname="NPF",
filename="{}.npf".format(gwfname),
filename=f"{gwfname}.npf",
)

# Instantiating MODFLOW 6 initial conditions package
Expand All @@ -596,7 +596,7 @@ def build_model(sim_name, verts, cell2d, top, botm):
sy=sy,
transient={0: True},
pname="STO",
filename="{}.sto".format(gwfname),
filename=f"{gwfname}.sto",
)

# Instantiate WEL package
Expand All @@ -622,12 +622,12 @@ def build_model(sim_name, verts, cell2d, top, botm):
save_flows=True,
stress_period_data=wel_spd,
pname="WEL",
filename="{}.wel".format(gwfname),
filename=f"{gwfname}.wel",
)

# Instantiating MODFLOW 6 output control package (flow model)
head_filerecord = "{}.hds".format(gwfname)
budget_filerecord = "{}.cbc".format(gwfname)
head_filerecord = f"{gwfname}.hds"
budget_filerecord = f"{gwfname}.cbc"
flopy.mf6.ModflowGwfoc(
gwf,
head_filerecord=head_filerecord,
Expand All @@ -645,7 +645,7 @@ def build_model(sim_name, verts, cell2d, top, botm):
sim,
model_type="gwe6",
modelname=gwename,
model_nam_file="{}.nam".format(gwename),
model_nam_file=f"{gwename}.nam",
)

# Create iterative model solution and register the gwe model with it
Expand Down Expand Up @@ -674,8 +674,8 @@ def build_model(sim_name, verts, cell2d, top, botm):
number_orthogonalizations=2,
preconditioner_levels=8,
preconditioner_drop_tolerance=0.001,
rcloserecord="{} strict".format(rclose),
filename="{}.ims".format(gwename),
rcloserecord=f"{rclose} strict",
filename=f"{gwename}.ims",
)
sim.register_ims_package(imsgwe, [gwe.name])

Expand All @@ -692,18 +692,14 @@ def build_model(sim_name, verts, cell2d, top, botm):
vertices=verts,
cell2d=cell2d,
pname="DISV-GWE",
filename="{}.disv".format(gwename),
filename=f"{gwename}.disv",
)

# Instantiating MODFLOW 6 heat transport initial temperature
flopy.mf6.ModflowGweic(
gwe, strt=strt_temp, pname="IC", filename="{}.ic".format(gwename)
)
flopy.mf6.ModflowGweic(gwe, strt=strt_temp, pname="IC", filename=f"{gwename}.ic")

# Instantiating MODFLOW 6 heat transport advection package
flopy.mf6.ModflowGweadv(
gwe, scheme=scheme, pname="ADV", filename="{}.adv".format(gwename)
)
flopy.mf6.ModflowGweadv(gwe, scheme=scheme, pname="ADV", filename=f"{gwename}.adv")

# Instantiating MODFLOW 6 heat transport energy storage package (consider renaming to est)
prsity = determine_param(low_k_id, high_k_id, "prsity")
Expand All @@ -717,7 +713,7 @@ def build_model(sim_name, verts, cell2d, top, botm):
heat_capacity_solid=cps,
density_solid=rhos,
pname="EST",
filename="{}.est".format(gwename),
filename=f"{gwename}.est",
)

# Instantiating MODFLOW 6 heat transport dispersion package
Expand All @@ -730,7 +726,7 @@ def build_model(sim_name, verts, cell2d, top, botm):
ktw=ktw,
kts=kts,
pname="CND",
filename="{}.cnd".format(gwename),
filename=f"{gwename}.cnd",
)

# Instantiating MODFLOW 6 source/sink mixing package for dealing with
Expand All @@ -739,14 +735,14 @@ def build_model(sim_name, verts, cell2d, top, botm):
("WEL", "AUX", "TEMPERATURE"),
]
flopy.mf6.ModflowGwessm(
gwe, sources=sourcerecarray, pname="SSM", filename="{}.ssm".format(gwename)
gwe, sources=sourcerecarray, pname="SSM", filename=f"{gwename}.ssm"
)

# Instantiating MODFLOW 6 heat transport output control package
flopy.mf6.ModflowGweoc(
gwe,
budget_filerecord="{}.cbc".format(gwename),
temperature_filerecord="{}.ucn".format(gwename),
budget_filerecord=f"{gwename}.cbc",
temperature_filerecord=f"{gwename}.ucn",
temperatureprintrecord=[("COLUMNS", 10, "WIDTH", 15, "DIGITS", 6, "GENERAL")],
saverecord=[("TEMPERATURE", "ALL"), ("BUDGET", "ALL")],
printrecord=[("TEMPERATURE", "ALL"), ("BUDGET", "ALL")],
Expand All @@ -759,7 +755,7 @@ def build_model(sim_name, verts, cell2d, top, botm):
exgtype="GWF6-GWE6",
exgmnamea=gwfname,
exgmnameb=gwename,
filename="{}.gwfgwe".format(gwename),
filename=f"{gwename}.gwfgwe",
)

return sim
Expand Down Expand Up @@ -950,7 +946,7 @@ def update(i):
ani = FuncAnimation(fig, update, range(1, len(times)), init_func=init)
# interval=25,
writer = PillowWriter(fps=10)
fpth = figs_path / "{}{}".format(sim_name, ".gif")
fpth = figs_path / f"{sim_name}.gif"
ani.save(fpth, writer=writer)


Expand Down
48 changes: 22 additions & 26 deletions scripts/ex-gwe-barends.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Barends, F.B.J., 2010. Complete Solution for Transient Heat Transport in
# Porous Media, Following Lauwerier's Concept. Society of Petroleum '
# Engineers, Annual Technical Conference and Exhibition, Florence, Italy,
# 1922 September 2010.
# 19-22 September 2010.
# https://doi.org/10.2118/134670-MS
#
# Below is a diagram of the model cell numbering (assuming delr = 4.0; values
Expand Down Expand Up @@ -275,7 +275,7 @@ def build_mf6_flow_model():
top=top_overburden,
botm=botm,
pname="DIS",
filename="{}.dis".format(gwf_name),
filename=f"{gwf_name}.dis",
)

# Instantiate node-property flow (NPF) package
Expand All @@ -289,12 +289,12 @@ def build_mf6_flow_model():
k=k11,
k33=k33,
pname="NPF-1",
filename="{}.npf".format(gwf_name),
filename=f"{gwf_name}.npf",
)

# Instantiate initial conditions package for the GWF model
flopy.mf6.ModflowGwfic(
gwf, strt=top_overburden, pname="IC-1", filename="{}.ic".format(gwf_name)
gwf, strt=top_overburden, pname="IC-1", filename=f"{gwf_name}.ic"
)

# Instantiating MODFLOW 6 storage package
Expand All @@ -306,7 +306,7 @@ def build_mf6_flow_model():
sy=0,
steady_state={0: True},
pname="STO",
filename="{}.sto".format(gwf_name),
filename=f"{gwf_name}.sto",
)

# Instantiate WEL package for adding water on the left and removing
Expand All @@ -316,19 +316,19 @@ def build_mf6_flow_model():
auxiliary="TEMPERATURE",
stress_period_data=wel_spd_left,
pname="WEL-left",
filename="{}.wel-left".format(gwf_name),
filename=f"{gwf_name}.wel-left",
)
# Instantiate WEL package for extracting water on the right
flopy.mf6.ModflowGwfwel(
gwf,
stress_period_data=wel_spd_right,
pname="WEL-right",
filename="{}.wel-right".format(gwf_name),
filename=f"{gwf_name}.wel-right",
)

# Instantiating MODFLOW 6 output control package (flow model)
head_filerecord = "{}.hds".format(sim_name)
budget_filerecord = "{}.cbc".format(sim_name)
head_filerecord = f"{sim_name}.hds"
budget_filerecord = f"{sim_name}.cbc"
flopy.mf6.ModflowGwfoc(
gwf,
head_filerecord=head_filerecord,
Expand All @@ -344,7 +344,7 @@ def build_mf6_flow_model():

# +
def build_mf6_heat_model():
print("Building mf6gwe model...{}".format(sim_name))
print(f"Building mf6gwe model...{sim_name}")
gwename = sim_name
sim_ws = os.path.join(workspace, sim_name, "mf6gwe")

Expand All @@ -355,7 +355,7 @@ def build_mf6_heat_model():
sim,
model_type="gwe6",
modelname=gwename,
model_nam_file="{}.nam".format(gwename),
model_nam_file=f"{gwename}.nam",
)

# Instantiate Iterative model solution package
Expand Down Expand Up @@ -390,18 +390,14 @@ def build_mf6_heat_model():
top=top_overburden,
botm=botm,
pname="DIS",
filename="{}.dis".format(gwename),
filename=f"{gwename}.dis",
)

# Instantiating MODFLOW 6 heat transport initial temperature
flopy.mf6.ModflowGweic(
gwe, strt=T0, pname="IC-gwe", filename="{}.ic".format(gwename)
)
flopy.mf6.ModflowGweic(gwe, strt=T0, pname="IC-gwe", filename=f"{gwename}.ic")

# Instantiating MODFLOW 6 heat transport advection package
flopy.mf6.ModflowGweadv(
gwe, scheme=scheme, pname="ADV", filename="{}.adv".format(gwename)
)
flopy.mf6.ModflowGweadv(gwe, scheme=scheme, pname="ADV", filename=f"{gwename}.adv")

# Instantiating MODFLOW 6 heat transport dispersion package
if ktw != 0:
Expand All @@ -413,7 +409,7 @@ def build_mf6_heat_model():
ktw=ktw,
kts=kts,
pname="CND",
filename="{}.cnd".format(gwename),
filename=f"{gwename}.cnd",
)

# Instantiating MODFLOW 6 heat transport mass storage package (consider renaming to est)
Expand All @@ -425,7 +421,7 @@ def build_mf6_heat_model():
density_solid=rhos,
density_water=rhow,
pname="EST",
filename="{}.est".format(gwename),
filename=f"{gwename}.est",
)

flopy.mf6.ModflowGwectp(
Expand All @@ -435,7 +431,7 @@ def build_mf6_heat_model():
maxbound=1,
stress_period_data=ctp_spd,
pname="CTP",
filename="{}.ctp".format(gwename),
filename=f"{gwename}.ctp",
)

# Instantiating MODFLOW 6 source/sink mixing package for dealing with
Expand All @@ -446,15 +442,15 @@ def build_mf6_heat_model():
print_flows=True,
sources=sourcerecarray,
pname="SSM",
filename="{}.ssm".format(gwename),
filename=f"{gwename}.ssm",
)

# Instantiating MODFLOW 6 heat transport output control package
# e.g., day 100 = 100 * 86400 = 8,640,000 = 8.64e6
flopy.mf6.ModflowGweoc(
gwe,
budget_filerecord="{}.cbc".format(gwename),
temperature_filerecord="{}.ucn".format(gwename),
budget_filerecord=f"{gwename}.cbc",
temperature_filerecord=f"{gwename}.ucn",
temperatureprintrecord=[("COLUMNS", 10, "WIDTH", 15, "DIGITS", 6, "GENERAL")],
saverecord={
0: [
Expand Down Expand Up @@ -520,7 +516,7 @@ def plot_thermal_bleeding(sim_gwe):
if plot_show:
plt.show()
if plot_save:
fpth = os.path.join(figs_path / "{}-gridView{}".format(sim_name, ".png"))
fpth = os.path.join(figs_path / f"{sim_name}-gridView.png")
fig.savefig(fpth, dpi=600)

# Next, plot model output
Expand Down Expand Up @@ -594,7 +590,7 @@ def plot_thermal_bleeding(sim_gwe):
if plot_show:
plt.show()
if plot_save:
fpth = os.path.join(figs_path / "{}-200yrs{}".format(sim_name, ".png"))
fpth = os.path.join(figs_path / f"{sim_name}-200yrs.png")
fig2.savefig(fpth, dpi=600)

return
Expand Down
Loading

0 comments on commit b004ac8

Please sign in to comment.