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 7 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
43 changes: 43 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",
"Checkered2DPattern",
Fixed Show fixed Hide fixed
]


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


class Checkered2DPattern(Pattern):
daico007 marked this conversation as resolved.
Show resolved Hide resolved
"""Generate a 2D grid (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 grid rows
m : int
Number of grid columns
daico007 marked this conversation as resolved.
Show resolved Hide resolved
shift : str, optional, default="column"
daico007 marked this conversation as resolved.
Show resolved Hide resolved
Allow user to choose the pattern to be shifted by "column" or "row"
"""

def __init__(self, n, m, shift="column", **kwargs):
points = np.zeros(shape=(n * m, 3), dtype=float)
if shift == "row":
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 == "column":
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}"
)
super(Checkered2DPattern, 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_checkered_2d(self):
pattern = mb.Checkered2DPattern(10, 5)
assert len(pattern) == 50

pattern = mb.Checkered2DPattern(5, 5, "row")
assert len(pattern) == 25

with pytest.raises(ValueError):
pattern = mb.Checkered2DPattern(5, 5, "bogus")
Fixed Show fixed Hide fixed

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