Skip to content

Commit

Permalink
Enable F403 except in demos
Browse files Browse the repository at this point in the history
  • Loading branch information
jwallwork23 committed Dec 24, 2024
1 parent 6fa4c51 commit 50e5d87
Show file tree
Hide file tree
Showing 21 changed files with 238 additions and 236 deletions.
6 changes: 3 additions & 3 deletions goalie/error_estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def get_dwr_indicator(F, adjoint_error, test_space=None):
elif not isinstance(adjoint_error, dict):
raise TypeError(
"Expected 'adjoint_error' to be a Function or dict, not"
" '{type(adjoint_error)}'."
f" '{type(adjoint_error)}'."
)

# Process input for test_space as a dictionary
Expand All @@ -111,7 +111,7 @@ def get_dwr_indicator(F, adjoint_error, test_space=None):
elif not isinstance(test_space, dict):
raise TypeError(
"Expected 'test_space' to be a FunctionSpace or dict, not"
" '{type(test_space)}'."
f" '{type(test_space)}'."
)

# Construct the mapping for each component
Expand All @@ -122,7 +122,7 @@ def get_dwr_indicator(F, adjoint_error, test_space=None):
if not isinstance(fs, WithGeometry):
raise TypeError(
f"Expected 'test_space['{key}']' to be a FunctionSpace, not"
" '{type(fs)}'."
f" '{type(fs)}'."
)
if F.ufl_domain() != err.function_space().mesh():
raise ValueError(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ select = [
]
ignore = [
"C901", # function is too complex (TODO #165: enable this)
"F403", # unable to detect undefined names
"F405", # name may be undefined, or defined from star imports
]
[tool.ruff.lint.per-file-ignores]
Expand All @@ -61,6 +60,7 @@ ignore = [
]
"demos/*.py" = [
"E402", # module level import not at top of file
"F403", # unable to detect undefined names
]

[tool.pytest.ini_options]
Expand Down
26 changes: 16 additions & 10 deletions test/adjoint/examples/burgers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
https://firedrakeproject.org/demos/burgers.py.html
"""

from firedrake import *
import ufl
from firedrake.__future__ import interpolate
from firedrake.assemble import assemble
from firedrake.function import Function
from firedrake.functionspace import FunctionSpace, VectorFunctionSpace
from firedrake.solving import solve
from firedrake.ufl_expr import TestFunction
from firedrake.utility_meshes import UnitSquareMesh

# Problem setup
n = 32
Expand All @@ -22,9 +28,7 @@


def get_function_spaces(mesh):
r"""
:math:`\mathbb P2` space.
"""
r""":math:`\mathbb P2` space."""
return {"uv_2d": VectorFunctionSpace(mesh, "CG", 2)}


Expand All @@ -47,9 +51,9 @@ def solver(i):
nu = Function(R).assign(0.0001)
v = TestFunction(fs)
F = (
inner((u - u_) / dtc, v) * dx
+ inner(dot(u, nabla_grad(u)), v) * dx
+ nu * inner(grad(u), grad(v)) * dx
ufl.inner((u - u_) / dtc, v) * ufl.dx
+ ufl.inner(ufl.dot(u, ufl.nabla_grad(u)), v) * ufl.dx
+ nu * ufl.inner(ufl.grad(u), ufl.grad(v)) * ufl.dx
)

# Time integrate from t_start to t_end
Expand All @@ -72,8 +76,10 @@ def get_initial_condition(self):
Initial condition which is sinusoidal in the x-direction.
"""
init_fs = self.function_spaces["uv_2d"][0]
x, y = SpatialCoordinate(self.meshes[0])
return {"uv_2d": assemble(interpolate(as_vector([sin(pi * x), 0]), init_fs))}
x, y = ufl.SpatialCoordinate(self.meshes[0])
return {
"uv_2d": assemble(interpolate(ufl.as_vector([ufl.sin(ufl.pi * x), 0]), init_fs))
}


def get_qoi(self, i):
Expand All @@ -86,7 +92,7 @@ def get_qoi(self, i):

def time_integrated_qoi(t):
u = self.fields["uv_2d"][0]
return dtc * inner(u, u) * ds(2)
return dtc * ufl.inner(u, u) * ufl.ds(2)

def end_time_qoi():
return time_integrated_qoi(end_time)
Expand Down
83 changes: 39 additions & 44 deletions test/adjoint/examples/point_discharge2d.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
"""
Problem specification for a simple
advection-diffusion test case with a
point source, from [Riadh et al. 2014].
This test case is notable for Goalie
because it has an analytical solution,
meaning the effectivity index can be
computed.
[Riadh et al. 2014] A. Riadh, G.
Cedric, M. Jean, "TELEMAC modeling
system: 2D hydrodynamics TELEMAC-2D
software release 7.0 user manual."
Paris: R&D, Electricite de France,
p. 134 (2014).
Problem specification for a simple advection-diffusion test case with a point source,
from [Riadh et al. 2014].
This test case is notable for Goalie because it has an analytical solution, meaning the
effectivity index can be computed.
[Riadh et al. 2014] A. Riadh, G. Cedric, M. Jean, "TELEMAC modeling system: 2D
hydrodynamics TELEMAC-2D software release 7.0 user manual." Paris: R&D, Electricite
de France, p. 134 (2014).
"""

import numpy as np
from firedrake import *
import ufl
from firedrake.assemble import assemble
from firedrake.bcs import DirichletBC
from firedrake.function import Function
from firedrake.functionspace import FunctionSpace
from firedrake.solving import solve
from firedrake.ufl_expr import TestFunction
from firedrake.utility_meshes import RectangleMesh

from goalie.math import bessk0

Expand All @@ -43,19 +44,15 @@ def get_function_spaces(mesh):

def source(mesh):
"""
Gaussian approximation to a point source
at (2, 5) with discharge rate 100 on a
Gaussian approximation to a point source at (2, 5) with discharge rate 100 on a
given mesh.
"""
x, y = SpatialCoordinate(mesh)
return 100.0 * exp(-((x - src_x) ** 2 + (y - src_y) ** 2) / src_r**2)
x, y = ufl.SpatialCoordinate(mesh)
return 100.0 * ufl.exp(-((x - src_x) ** 2 + (y - src_y) ** 2) / src_r**2)


def get_solver(self):
"""
Advection-diffusion equation
solved using a direct method.
"""
"""Advection-diffusion equation solved using a direct method."""

def solver(i):
fs = self.function_spaces["tracer_2d"][i]
Expand All @@ -67,22 +64,22 @@ def solver(i):
D = Function(R).assign(0.1)
u_x = Function(R).assign(1.0)
u_y = Function(R).assign(0.0)
u = as_vector([u_x, u_y])
h = CellSize(self[i])
u = ufl.as_vector([u_x, u_y])
h = ufl.CellSize(self[i])
S = source(self[i])

# Stabilisation parameter
unorm = sqrt(dot(u, u))
unorm = ufl.sqrt(ufl.dot(u, u))
tau = 0.5 * h / unorm
tau = min_value(tau, unorm * h / (6 * D))
tau = ufl.min_value(tau, unorm * h / (6 * D))

# Setup variational problem
psi = TestFunction(fs)
psi = psi + tau * dot(u, grad(psi))
psi = psi + tau * ufl.dot(u, ufl.grad(psi))
F = (
S * psi * dx
- dot(u, grad(c)) * psi * dx
- inner(D * grad(c), grad(psi)) * dx
S * psi * ufl.dx
- ufl.dot(u, ufl.grad(c)) * psi * ufl.dx
- ufl.inner(D * ufl.grad(c), ufl.grad(psi)) * ufl.dx
)

# Zero Dirichlet condition on the left-hand (inlet) boundary
Expand All @@ -104,32 +101,30 @@ def solver(i):

def get_qoi(self, i):
"""
Quantity of interest which integrates
the tracer concentration over an offset
Quantity of interest which integrates the tracer concentration over an offset
receiver region.
"""

def steady_qoi():
c = self.fields["tracer_2d"]
x, y = SpatialCoordinate(self[i])
kernel = conditional((x - rec_x) ** 2 + (y - rec_y) ** 2 < rec_r**2, 1, 0)
area = assemble(kernel * dx)
area_analytical = pi * rec_r**2
x, y = ufl.SpatialCoordinate(self[i])
kernel = ufl.conditional((x - rec_x) ** 2 + (y - rec_y) ** 2 < rec_r**2, 1, 0)
area = assemble(kernel * ufl.dx)
area_analytical = ufl.pi * rec_r**2
scaling = 1.0 if np.allclose(area, 0.0) else area_analytical / area
return scaling * kernel * c * dx
return scaling * kernel * c * ufl.dx

return steady_qoi


def analytical_solution(mesh):
"""
Analytical solution as represented on
a given mesh. See [Riadh et al. 2014].
Analytical solution as represented on a given mesh. See [Riadh et al. 2014].
"""
x, y = SpatialCoordinate(mesh)
x, y = ufl.SpatialCoordinate(mesh)
R = FunctionSpace(mesh, "R", 0)
u = Function(R).assign(1.0)
D = Function(R).assign(0.1)
Pe = 0.5 * u / D
r = max_value(sqrt((x - src_x) ** 2 + (y - src_y) ** 2), src_r)
return 0.5 / (pi * D) * exp(Pe * (x - src_x)) * bessk0(Pe * r)
r = ufl.max_value(ufl.sqrt((x - src_x) ** 2 + (y - src_y) ** 2), src_r)
return 0.5 / (ufl.pi * D) * ufl.exp(Pe * (x - src_x)) * bessk0(Pe * r)
Loading

0 comments on commit 50e5d87

Please sign in to comment.