Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 2d checkered pattern #1118

Merged
merged 15 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
test-output.xml
*.py[cod]
*.pymon
*.ipynb_checkpoints

# C extensions
Expand Down
57 changes: 57 additions & 0 deletions mbuild/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"Random3DPattern",
"Grid2DPattern",
"Grid3DPattern",
"Triangle2DPattern",
]


Expand Down Expand Up @@ -274,6 +275,62 @@ def __init__(self, n, m, l, **kwargs):
super(Grid3DPattern, self).__init__(points=points, **kwargs)


class Triangle2DPattern(Pattern):
"""Generate a 2D triangle (n x m) of points along z = 0.

Notes
daico007 marked this conversation as resolved.
Show resolved Hide resolved
-----
Points span [0,1) along x and y axes

daico007 marked this conversation as resolved.
Show resolved Hide resolved
Attributes
----------
n : int
Number of points along x axis
m : int
Number of points along y axis
shift : str, optional, default="n"
Allow user to choose the pattern to be shifted by "n" or "m"
shift_by : 0 <= float <= 1, optional, default=0.5
Normalized distance to be shift the n or m rows, with value between 0 and 1.
The value should be the relative distance between two consecutive points defined in shift.
shift_by value of 0 will return grid pattern.
"""

def __init__(self, n, m, shift="m", shift_by=0.5, **kwargs):
assert isinstance(shift_by, (float, int))
err_msg = "shift_by value must be between 0 and 1."
assert shift_by >= 0 and shift_by <= 1, err_msg

points = np.zeros(shape=(n * m, 3), dtype=float)
if shift == "m":
for i, j in product(range(n), range(m)):
if i % 2 == 0:
points[i * m + j, 0] = i / n
points[i * m + j, 1] = j / m
else:
points[i * m + j, 0] = i / n
points[i * m + j, 1] = j / m + (1 / (2 * m))

elif shift == "n":
for i, j in product(range(n), range(m)):
if j % 2 == 0:
points[i * m + j, 0] = i / n
points[i * m + j, 1] = j / m
else:
points[i * m + j, 0] = i / n + (1 / (2 * n))
points[i * m + j, 1] = j / m
else:
raise ValueError(
f"shift can only take value of 'column' or 'row', given {shift}"
)

for i in range(len(points)):
if points[i][0] > 1:
points[i][0] -= 1

super(Triangle2DPattern, self).__init__(points=points, **kwargs)


class SpherePattern(Pattern):
"""Generate N evenly distributed points on the unit sphere.

Expand Down
10 changes: 10 additions & 0 deletions mbuild/tests/test_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ def test_grid_3d(self):
pattern = mb.Grid3DPattern(10, 5, 2)
assert len(pattern) == 100

def test_triangle_2d(self):
pattern = mb.Triangle2DPattern(10, 5)
assert len(pattern) == 50

pattern = mb.Triangle2DPattern(5, 5, "n")
assert len(pattern) == 25

with pytest.raises(ValueError):
pattern = mb.Triangle2DPattern(5, 5, "bogus")

Check notice

Code scanning / CodeQL

Unused local variable

Variable pattern is not used.

def test_sphere(self):
pattern = mb.SpherePattern(100)
assert len(pattern) == 100
Expand Down