Skip to content

Commit

Permalink
tweak the format
Browse files Browse the repository at this point in the history
  • Loading branch information
mawc2019 committed Jul 31, 2023
1 parent c632fa1 commit 6e3a85c
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions python/adjoint/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,21 @@ def _get_resolution(resolution: ArrayLikeType) -> tuple:
return resolution[0], resolution[0]
else:
raise ValueError(
"The dimension of the design-grid resolution is incorrect.")
"The dimension of the design-grid resolution is incorrect."
)
elif isinstance(resolution, (int, float)):
return resolution, resolution
else:
raise ValueError(
"The input for design-grid resolution is invalid.")
raise ValueError("The input for design-grid resolution is invalid.")


def mesh_grid(
radius: float,
Lx: float,
Ly: float,
resolution: ArrayLikeType,
periodic_axes: ArrayLikeType = None) -> tuple:
periodic_axes: ArrayLikeType = None,
) -> tuple:
"""Obtains the numbers of grid points and the coordinates of the grid
of the design region.
Expand All @@ -227,7 +228,6 @@ def mesh_grid(
A four-element tuple composed of the numbers of grid points and
the coordinates of the grid.
"""

resolution = _get_resolution(resolution)
Nx = int(round(Lx * resolution[0])) + 1
Ny = int(round(Ly * resolution[1])) + 1
Expand All @@ -241,9 +241,17 @@ def mesh_grid(
if periodic_axes is not None:
periodic_axes = np.array(periodic_axes)
if 0 in periodic_axes:
xv = np.arange(0, np.ceil(2 * radius / Lx) * Lx / 2, 1 / resolution[0]) if resolution[0] > 0 else [0]
xv = (
np.arange(0, np.ceil(2 * radius / Lx) * Lx / 2, 1 / resolution[0])
if resolution[0] > 0
else [0]
)
if 1 in periodic_axes:
yv = np.arange(0, np.ceil(2 * radius / Ly) * Ly / 2, 1 / resolution[1]) if resolution[1] > 0 else [0]
yv = (
np.arange(0, np.ceil(2 * radius / Ly) * Ly / 2, 1 / resolution[1])
if resolution[1] > 0
else [0]
)

X, Y = np.meshgrid(xv, yv, sparse=True, indexing="ij")

Expand Down Expand Up @@ -349,7 +357,7 @@ def gaussian_filter(
Returns:
The filtered design weights.
"""
Nx, Ny, X, Y = mesh_grid(3*sigma, Lx, Ly, resolution, periodic_axes)
Nx, Ny, X, Y = mesh_grid(3 * sigma, Lx, Ly, resolution, periodic_axes)
x = x.reshape(Nx, Ny) # Ensure the input is 2d
h = np.exp(-(X**2 + Y**2) / sigma**2)

Expand Down Expand Up @@ -1058,4 +1066,4 @@ def gray_indicator(x):
[1] Lazarov, B. S., Wang, F., & Sigmund, O. (2016). Length scale and manufacturability in
density-based topology optimization. Archive of Applied Mechanics, 86(1-2), 189-218.
"""
return npa.mean(4 * x.flatten() * (1 - x.flatten())) * 100
return npa.mean(4 * x.flatten() * (1 - x.flatten())) * 100

0 comments on commit 6e3a85c

Please sign in to comment.