Compare commits

..

No commits in common. "9ab97e763cc5662df4297c51bc2331652698c345" and "73d07bbfe0845ed72d76a2235c9df99e9e7605ea" have entirely different histories.

7 changed files with 51 additions and 91 deletions

29
.flake8
View File

@ -1,29 +0,0 @@
[flake8]
ignore =
# E501 line too long
E501,
# W391 newlines at EOF
W391,
# E241 multiple spaces after comma
E241,
# E302 expected 2 newlines
E302,
# W503 line break before binary operator (to be deprecated)
W503,
# E265 block comment should start with '# '
E265,
# E123 closing bracket does not match indentation of opening bracket's line
E123,
# E124 closing bracket does not match visual indentation
E124,
# E221 multiple spaces before operator
E221,
# E201 whitespace after '['
E201,
# E741 ambiguous variable name 'I'
E741,
per-file-ignores =
# F401 import without use
*/__init__.py: F401,

View File

@ -14,7 +14,7 @@ the coordinates of the boundary points along each axis).
## Installation ## Installation
Requirements: Requirements:
* python >3.11 (written and tested with 3.12) * python 3 (written and tested with 3.9)
* numpy * numpy
* [float_raster](https://mpxd.net/code/jan/float_raster) * [float_raster](https://mpxd.net/code/jan/float_raster)
* matplotlib (optional, used for visualization functions) * matplotlib (optional, used for visualization functions)

View File

@ -1,7 +1,7 @@
""" """
Drawing-related methods for Grid class Drawing-related methods for Grid class
""" """
from typing import Union, Sequence, Callable from typing import List, Optional, Union, Sequence, Callable
import numpy import numpy
from numpy.typing import NDArray, ArrayLike from numpy.typing import NDArray, ArrayLike
@ -27,7 +27,7 @@ def draw_polygons(
center: ArrayLike, center: ArrayLike,
polygons: Sequence[NDArray], polygons: Sequence[NDArray],
thickness: float, thickness: float,
foreground: Sequence[foreground_t] | foreground_t, foreground: Union[Sequence[foreground_t], foreground_t],
) -> None: ) -> None:
""" """
Draw polygons on an axis-aligned plane. Draw polygons on an axis-aligned plane.
@ -59,7 +59,7 @@ def draw_polygons(
for i, polygon in enumerate(polygons): for i, polygon in enumerate(polygons):
malformed = f'Malformed polygon: ({i})' malformed = f'Malformed polygon: ({i})'
if polygon.shape[1] not in (2, 3): if polygon.shape[1] not in (2, 3):
raise GridError(malformed + 'must be a Nx2 or Nx3 ndarray') raise GridError(malformed + 'must be a Nx2 or Nx3 ndarray')
if polygon.shape[1] == 3: if polygon.shape[1] == 3:
polygon = polygon[surface, :] polygon = polygon[surface, :]
@ -70,9 +70,9 @@ def draw_polygons(
+ 'xyz'[surface_normal]) + 'xyz'[surface_normal])
# Broadcast foreground where necessary # Broadcast foreground where necessary
foregrounds: Sequence[foreground_callable_t] | Sequence[float] foregrounds: Union[Sequence[foreground_callable_t], Sequence[float]]
if numpy.size(foreground) == 1: # type: ignore if numpy.size(foreground) == 1: # type: ignore
foregrounds = [foreground] * len(cell_data) # type: ignore foregrounds = [foreground] * len(cell_data) # type: ignore
elif isinstance(foreground, numpy.ndarray): elif isinstance(foreground, numpy.ndarray):
raise GridError('ndarray not supported for foreground') raise GridError('ndarray not supported for foreground')
else: else:
@ -113,7 +113,7 @@ def draw_polygons(
foregrounds_i = foregrounds[i] foregrounds_i = foregrounds[i]
if callable(foregrounds_i): if callable(foregrounds_i):
# meshgrid over the (shifted) domain # meshgrid over the (shifted) domain
domain = [self.shifted_xyz(i)[k][bdi_min[k]:bdi_max[k] + 1] for k in range(3)] domain = [self.shifted_xyz(i)[k][bdi_min[k]:bdi_max[k]+1] for k in range(3)]
(x0, y0, z0) = numpy.meshgrid(*domain, indexing='ij') (x0, y0, z0) = numpy.meshgrid(*domain, indexing='ij')
# evaluate on the meshgrid # evaluate on the meshgrid
@ -202,7 +202,7 @@ def draw_polygon(
center: ArrayLike, center: ArrayLike,
polygon: ArrayLike, polygon: ArrayLike,
thickness: float, thickness: float,
foreground: Sequence[foreground_t] | foreground_t, foreground: Union[Sequence[foreground_t], foreground_t],
) -> None: ) -> None:
""" """
Draw a polygon on an axis-aligned plane. Draw a polygon on an axis-aligned plane.
@ -226,7 +226,7 @@ def draw_slab(
surface_normal: int, surface_normal: int,
center: ArrayLike, center: ArrayLike,
thickness: float, thickness: float,
foreground: Sequence[foreground_t] | foreground_t, foreground: Union[Sequence[foreground_t], foreground_t],
) -> None: ) -> None:
""" """
Draw an axis-aligned infinite slab. Draw an axis-aligned infinite slab.
@ -276,7 +276,7 @@ def draw_cuboid(
cell_data: NDArray, cell_data: NDArray,
center: ArrayLike, center: ArrayLike,
dimensions: ArrayLike, dimensions: ArrayLike,
foreground: Sequence[foreground_t] | foreground_t, foreground: Union[Sequence[foreground_t], foreground_t],
) -> None: ) -> None:
""" """
Draw an axis-aligned cuboid Draw an axis-aligned cuboid
@ -305,7 +305,7 @@ def draw_cylinder(
radius: float, radius: float,
thickness: float, thickness: float,
num_points: int, num_points: int,
foreground: Sequence[foreground_t] | foreground_t, foreground: Union[Sequence[foreground_t], foreground_t],
) -> None: ) -> None:
""" """
Draw an axis-aligned cylinder. Approximated by a num_points-gon Draw an axis-aligned cylinder. Approximated by a num_points-gon
@ -319,7 +319,7 @@ def draw_cylinder(
num_points: The circle is approximated by a polygon with `num_points` vertices num_points: The circle is approximated by a polygon with `num_points` vertices
foreground: Value to draw with ('brush color'). See `draw_polygons()` for details. foreground: Value to draw with ('brush color'). See `draw_polygons()` for details.
""" """
theta = numpy.linspace(0, 2 * numpy.pi, num_points, endpoint=False) theta = numpy.linspace(0, 2*numpy.pi, num_points, endpoint=False)
x = radius * numpy.sin(theta) x = radius * numpy.sin(theta)
y = radius * numpy.cos(theta) y = radius * numpy.cos(theta)
polygon = numpy.hstack((x[:, None], y[:, None])) polygon = numpy.hstack((x[:, None], y[:, None]))
@ -360,8 +360,8 @@ def draw_extrude_rectangle(
surface = numpy.delete(range(3), direction) surface = numpy.delete(range(3), direction)
dim = numpy.fabs(numpy.diff(rectangle, axis=0).T)[surface] dim = numpy.fabs(numpy.diff(rectangle, axis=0).T)[surface]
p = numpy.vstack((numpy.array([-1, -1, 1, 1], dtype=float) * dim[0] * 0.5, p = numpy.vstack((numpy.array([-1, -1, 1, 1], dtype=float) * dim[0]/2.0,
numpy.array([-1, 1, 1, -1], dtype=float) * dim[1] * 0.5)).T numpy.array([-1, 1, 1, -1], dtype=float) * dim[1]/2.0)).T
thickness = distance thickness = distance
foreground_func = [] foreground_func = []
@ -371,7 +371,7 @@ def draw_extrude_rectangle(
ind = [int(numpy.floor(z)) if i == direction else slice(None) for i in range(3)] ind = [int(numpy.floor(z)) if i == direction else slice(None) for i in range(3)]
fpart = z - numpy.floor(z) fpart = z - numpy.floor(z)
mult = [1 - fpart, fpart][::s] # reverses if s negative mult = [1-fpart, fpart][::s] # reverses if s negative
foreground = mult[0] * grid[tuple(ind)] foreground = mult[0] * grid[tuple(ind)]
ind[direction] += 1 # type: ignore #(known safe) ind[direction] += 1 # type: ignore #(known safe)

View File

@ -1,7 +1,8 @@
from typing import Callable, Sequence, ClassVar, Self from typing import List, Tuple, Callable, Dict, Optional, Union, Sequence, ClassVar, TypeVar
import numpy import numpy
from numpy.typing import NDArray, ArrayLike from numpy.typing import NDArray, ArrayLike
from numpy import diff, floor, ceil, zeros, hstack, newaxis
import pickle import pickle
import warnings import warnings
@ -11,6 +12,7 @@ from . import GridError
foreground_callable_type = Callable[[NDArray, NDArray, NDArray], NDArray] foreground_callable_type = Callable[[NDArray, NDArray, NDArray], NDArray]
T = TypeVar('T', bound='Grid')
class Grid: class Grid:
@ -47,10 +49,10 @@ class Grid:
Because of this, we either assume this 'ghost' cell is the same size as the last Because of this, we either assume this 'ghost' cell is the same size as the last
real cell, or, if `self.periodic[a]` is set to `True`, the same size as the first cell. real cell, or, if `self.periodic[a]` is set to `True`, the same size as the first cell.
""" """
exyz: list[NDArray] exyz: List[NDArray]
"""Cell edges. Monotonically increasing without duplicates.""" """Cell edges. Monotonically increasing without duplicates."""
periodic: list[bool] periodic: List[bool]
"""For each axis, determines how far the rightmost boundary gets shifted. """ """For each axis, determines how far the rightmost boundary gets shifted. """
shifts: NDArray shifts: NDArray
@ -78,7 +80,7 @@ class Grid:
from .position import ind2pos, pos2ind from .position import ind2pos, pos2ind
@property @property
def dxyz(self) -> list[NDArray]: def dxyz(self) -> List[NDArray]:
""" """
Cell sizes for each axis, no shifts applied Cell sizes for each axis, no shifts applied
@ -88,7 +90,7 @@ class Grid:
return [numpy.diff(ee) for ee in self.exyz] return [numpy.diff(ee) for ee in self.exyz]
@property @property
def xyz(self) -> list[NDArray]: def xyz(self) -> List[NDArray]:
""" """
Cell centers for each axis, no shifts applied Cell centers for each axis, no shifts applied
@ -122,7 +124,7 @@ class Grid:
return numpy.hstack((self.num_grids, self.shape)) return numpy.hstack((self.num_grids, self.shape))
@property @property
def dxyz_with_ghost(self) -> list[NDArray]: def dxyz_with_ghost(self) -> List[NDArray]:
""" """
Gives dxyz with an additional 'ghost' cell at the end, whose value depends Gives dxyz with an additional 'ghost' cell at the end, whose value depends
on whether or not the axis has periodic boundary conditions. See main description on whether or not the axis has periodic boundary conditions. See main description
@ -151,7 +153,7 @@ class Grid:
return numpy.array(centers, dtype=float) return numpy.array(centers, dtype=float)
@property @property
def dxyz_limits(self) -> tuple[NDArray, NDArray]: def dxyz_limits(self) -> Tuple[NDArray, NDArray]:
""" """
Returns the minimum and maximum cell size for each axis, as a tuple of two 3-element Returns the minimum and maximum cell size for each axis, as a tuple of two 3-element
ndarrays. No shifts are applied, so these are extreme bounds on these values (as a ndarrays. No shifts are applied, so these are extreme bounds on these values (as a
@ -164,7 +166,7 @@ class Grid:
d_max = numpy.array([max(self.dxyz[a]) for a in range(3)], dtype=float) d_max = numpy.array([max(self.dxyz[a]) for a in range(3)], dtype=float)
return d_min, d_max return d_min, d_max
def shifted_exyz(self, which_shifts: int | None) -> list[NDArray]: def shifted_exyz(self, which_shifts: Optional[int]) -> List[NDArray]:
""" """
Returns edges for which_shifts. Returns edges for which_shifts.
@ -186,7 +188,7 @@ class Grid:
return [self.exyz[a] + dxyz[a] * shifts[a] for a in range(3)] return [self.exyz[a] + dxyz[a] * shifts[a] for a in range(3)]
def shifted_dxyz(self, which_shifts: int | None) -> list[NDArray]: def shifted_dxyz(self, which_shifts: Optional[int]) -> List[NDArray]:
""" """
Returns cell sizes for `which_shifts`. Returns cell sizes for `which_shifts`.
@ -213,7 +215,7 @@ class Grid:
return sdxyz return sdxyz
def shifted_xyz(self, which_shifts: int | None) -> list[NDArray[numpy.float64]]: def shifted_xyz(self, which_shifts: Optional[int]) -> List[NDArray[numpy.float64]]:
""" """
Returns cell centers for `which_shifts`. Returns cell centers for `which_shifts`.
@ -229,7 +231,7 @@ class Grid:
dxyz = self.shifted_dxyz(which_shifts) dxyz = self.shifted_dxyz(which_shifts)
return [exyz[a][:-1] + dxyz[a] / 2.0 for a in range(3)] return [exyz[a][:-1] + dxyz[a] / 2.0 for a in range(3)]
def autoshifted_dxyz(self) -> list[NDArray[numpy.float64]]: def autoshifted_dxyz(self) -> List[NDArray[numpy.float64]]:
""" """
Return cell widths, with each dimension shifted by the corresponding shifts. Return cell widths, with each dimension shifted by the corresponding shifts.
@ -240,7 +242,7 @@ class Grid:
raise GridError('Autoshifting requires exactly 3 grids') raise GridError('Autoshifting requires exactly 3 grids')
return [self.shifted_dxyz(which_shifts=a)[a] for a in range(3)] return [self.shifted_dxyz(which_shifts=a)[a] for a in range(3)]
def allocate(self, fill_value: float | None = 1.0, dtype=numpy.float32) -> NDArray: def allocate(self, fill_value: Optional[float] = 1.0, dtype=numpy.float32) -> NDArray:
""" """
Allocate an ndarray for storing grid data. Allocate an ndarray for storing grid data.
@ -261,7 +263,7 @@ class Grid:
self, self,
pixel_edge_coordinates: Sequence[ArrayLike], pixel_edge_coordinates: Sequence[ArrayLike],
shifts: ArrayLike = Yee_Shifts_E, shifts: ArrayLike = Yee_Shifts_E,
periodic: bool | Sequence[bool] = False, periodic: Union[bool, Sequence[bool]] = False,
) -> None: ) -> None:
""" """
Args: Args:
@ -318,7 +320,7 @@ class Grid:
g.__dict__.update(tmp_dict) g.__dict__.update(tmp_dict)
return g return g
def save(self, filename: str) -> Self: def save(self: T, filename: str) -> T:
""" """
Save to file. Save to file.
@ -332,7 +334,7 @@ class Grid:
pickle.dump(self.__dict__, f, protocol=2) pickle.dump(self.__dict__, f, protocol=2)
return self return self
def copy(self) -> Self: def copy(self: T) -> T:
""" """
Returns: Returns:
Deep copy of the grid. Deep copy of the grid.

View File

@ -1,6 +1,8 @@
""" """
Position-related methods for Grid class Position-related methods for Grid class
""" """
from typing import List, Optional, Sequence
import numpy import numpy
from numpy.typing import NDArray, ArrayLike from numpy.typing import NDArray, ArrayLike
@ -10,7 +12,7 @@ from . import GridError
def ind2pos( def ind2pos(
self, self,
ind: NDArray, ind: NDArray,
which_shifts: int | None = None, which_shifts: Optional[int] = None,
round_ind: bool = True, round_ind: bool = True,
check_bounds: bool = True check_bounds: bool = True
) -> NDArray[numpy.float64]: ) -> NDArray[numpy.float64]:
@ -62,7 +64,7 @@ def ind2pos(
def pos2ind( def pos2ind(
self, self,
r: ArrayLike, r: ArrayLike,
which_shifts: int | None, which_shifts: Optional[int],
round_ind: bool = True, round_ind: bool = True,
check_bounds: bool = True check_bounds: bool = True
) -> NDArray[numpy.float64]: ) -> NDArray[numpy.float64]:
@ -99,7 +101,7 @@ def pos2ind(
grid_pos = numpy.zeros((3,)) grid_pos = numpy.zeros((3,))
for a in range(3): for a in range(3):
xi = numpy.digitize(r[a], sexyz[a]) - 1 # Figure out which cell we're in xi = numpy.digitize(r[a], sexyz[a]) - 1 # Figure out which cell we're in
xi_clipped = numpy.clip(xi, 0, sexyz[a].size - 2) # Clip back into grid bounds xi_clipped = numpy.clip(xi, 0, sexyz[a].size - 2) # Clip back into grid bounds
# No need to interpolate if round_ind is true or we were outside the grid # No need to interpolate if round_ind is true or we were outside the grid

View File

@ -1,18 +1,13 @@
""" """
Readback and visualization methods for Grid class Readback and visualization methods for Grid class
""" """
from typing import Any, TYPE_CHECKING from typing import Dict, Optional, Union, Any
import numpy import numpy
from numpy.typing import NDArray from numpy.typing import NDArray, ArrayLike
from . import GridError from . import GridError
if TYPE_CHECKING:
import matplotlib.axes
import matplotlib.figure
# .visualize_* uses matplotlib # .visualize_* uses matplotlib
# .visualize_isosurface uses skimage # .visualize_isosurface uses skimage
# .visualize_isosurface uses mpl_toolkits.mplot3d # .visualize_isosurface uses mpl_toolkits.mplot3d
@ -90,8 +85,8 @@ def visualize_slice(
which_shifts: int = 0, which_shifts: int = 0,
sample_period: int = 1, sample_period: int = 1,
finalize: bool = True, finalize: bool = True,
pcolormesh_args: dict[str, Any] | None = None, pcolormesh_args: Optional[Dict[str, Any]] = None,
) -> tuple['matplotlib.axes.Axes', 'matplotlib.figure.Figure']: ) -> None:
""" """
Visualize a slice of a grid. Visualize a slice of a grid.
Interpolates if given a position between two planes. Interpolates if given a position between two planes.
@ -102,9 +97,6 @@ def visualize_slice(
which_shifts: Which grid to display. Default is the first grid (0). which_shifts: Which grid to display. Default is the first grid (0).
sample_period: Period for down-sampling the image. Default 1 (disabled) sample_period: Period for down-sampling the image. Default 1 (disabled)
finalize: Whether to call `pyplot.show()` after constructing the plot. Default `True` finalize: Whether to call `pyplot.show()` after constructing the plot. Default `True`
Returns:
(Figure, Axes)
""" """
from matplotlib import pyplot from matplotlib import pyplot
@ -123,27 +115,25 @@ def visualize_slice(
xmesh, ymesh = numpy.meshgrid(x, y, indexing='ij') xmesh, ymesh = numpy.meshgrid(x, y, indexing='ij')
x_label, y_label = ('xyz'[a] for a in surface) x_label, y_label = ('xyz'[a] for a in surface)
fig, ax = pyplot.subplots() pyplot.figure()
mappable = ax.pcolormesh(xmesh, ymesh, grid_slice, **pcolormesh_args) pyplot.pcolormesh(xmesh, ymesh, grid_slice, **pcolormesh_args)
fig.colorbar(mappable) pyplot.colorbar()
ax.set_aspect('equal', adjustable='box') pyplot.gca().set_aspect('equal', adjustable='box')
ax.set_xlabel(x_label) pyplot.xlabel(x_label)
ax.set_ylabel(y_label) pyplot.ylabel(y_label)
if finalize: if finalize:
pyplot.show() pyplot.show()
return fig, ax
def visualize_isosurface( def visualize_isosurface(
self, self,
cell_data: NDArray, cell_data: NDArray,
level: float | None = None, level: Optional[float] = None,
which_shifts: int = 0, which_shifts: int = 0,
sample_period: int = 1, sample_period: int = 1,
show_edges: bool = True, show_edges: bool = True,
finalize: bool = True, finalize: bool = True,
) -> tuple['matplotlib.axes.Axes', 'matplotlib.figure.Figure']: ) -> None:
""" """
Draw an isosurface plot of the device. Draw an isosurface plot of the device.
@ -154,9 +144,6 @@ def visualize_isosurface(
sample_period: Period for down-sampling the image. Default 1 (disabled) sample_period: Period for down-sampling the image. Default 1 (disabled)
show_edges: Whether to draw triangle edges. Default `True` show_edges: Whether to draw triangle edges. Default `True`
finalize: Whether to call `pyplot.show()` after constructing the plot. Default `True` finalize: Whether to call `pyplot.show()` after constructing the plot. Default `True`
Returns:
(Figure, Axes)
""" """
from matplotlib import pyplot from matplotlib import pyplot
import skimage.measure import skimage.measure
@ -198,5 +185,3 @@ def visualize_isosurface(
if finalize: if finalize:
pyplot.show() pyplot.show()
return fig, ax

View File

@ -32,13 +32,13 @@ classifiers = [
"Topic :: Scientific/Engineering :: Physics", "Topic :: Scientific/Engineering :: Physics",
"Topic :: Scientific/Engineering :: Visualization", "Topic :: Scientific/Engineering :: Visualization",
] ]
requires-python = ">=3.11" requires-python = ">=3.8"
include = [ include = [
"LICENSE.md" "LICENSE.md"
] ]
dynamic = ["version"] dynamic = ["version"]
dependencies = [ dependencies = [
"numpy~=1.26", "numpy~=1.21",
"float_raster", "float_raster",
] ]