Coverage for src/abm_initialization_collection/coordinate/make_grid_coordinates.py: 100%
25 statements
« prev ^ index » next coverage.py v7.1.0, created at 2024-07-26 20:12 +0000
« prev ^ index » next coverage.py v7.1.0, created at 2024-07-26 20:12 +0000
1from math import floor, sqrt
3import numpy as np
4from hexalattice import hexalattice
7def make_grid_coordinates(
8 grid: str,
9 bounds: tuple[int, int, int],
10 increment_xy: float,
11 increment_z: float,
12) -> list:
13 """
14 Get all coordinates within given bounding box for selected grid type.
16 Parameters
17 ----------
18 grid : {'rect', 'hex'}
19 Type of grid.
20 bounds
21 Bounds in the x, y, and z directions.
22 increment_xy
23 Increment size in x/y directions.
24 increment_z
25 Increment size in z direction.
27 Returns
28 -------
29 :
30 List of grid coordinates.
31 """
33 if grid == "rect":
34 return make_rect_grid_coordinates(bounds, increment_xy, increment_z)
36 if grid == "hex":
37 return make_hex_grid_coordinates(bounds, increment_xy, increment_z)
39 raise ValueError(f"invalid grid type {grid}")
42def make_rect_grid_coordinates(
43 bounds: tuple[int, int, int],
44 increment_xy: float,
45 increment_z: float,
46) -> list:
47 """
48 Get list of bounded (x, y, z) coordinates for rect grid.
50 Parameters
51 ----------
52 bounds
53 Bounds in the x, y, and z directions.
54 increment_xy
55 Increment size in x/y directions.
56 increment_z
57 Increment size in z direction.
59 Returns
60 -------
61 :
62 List of grid coordinates.
63 """
65 x_bound, y_bound, z_bound = bounds
67 z_indices = np.arange(0, z_bound, increment_z)
68 x_indices = np.arange(0, x_bound, increment_xy)
69 y_indices = np.arange(0, y_bound, increment_xy)
71 coordinates = [(x, y, z) for z in z_indices for x in x_indices for y in y_indices]
73 return coordinates
76def make_hex_grid_coordinates(
77 bounds: tuple[int, int, int],
78 increment_xy: float,
79 increment_z: float,
80) -> list:
81 """
82 Get list of bounded (x, y, z) coordinates for hex grid.
84 Coordinates are offset in sets of three z slices to form a face-centered
85 cubic (FCC) packing.
87 Parameters
88 ----------
89 bounds
90 Bounds in the x, y, and z directions.
91 increment_xy
92 Increment size in x/y directions.
93 increment_z
94 Increment size in z direction.
96 Returns
97 -------
98 :
99 List of grid coordinates.
100 """
102 x_bound, y_bound, z_bound = bounds
104 z_indices = np.arange(0, z_bound, increment_z)
105 z_offsets = [(i % 3) for i in range(len(z_indices))]
107 xy_indices, _ = hexalattice.create_hex_grid(
108 nx=floor(x_bound / increment_xy),
109 ny=floor(y_bound / increment_xy * sqrt(3)),
110 min_diam=increment_xy,
111 align_to_origin=False,
112 do_plot=False,
113 )
115 x_offsets = [(increment_xy / 2) if z_offset == 1 else 0 for z_offset in z_offsets]
116 y_offsets = [(increment_xy / 2) * sqrt(3) / 3 * z_offset for z_offset in z_offsets]
118 coordinates = [
119 (x + x_offset, y + y_offset, z)
120 for z, x_offset, y_offset in zip(z_indices, x_offsets, y_offsets)
121 for x, y in xy_indices
122 if round(x + x_offset) < x_bound and round(y + y_offset) < y_bound
123 ]
125 return coordinates