Skip to content

Commit

Permalink
Add 2d checkered pattern (#1118)
Browse files Browse the repository at this point in the history
* add 2d checkered patter

* add checkered2dpattern to __all__

* add unit test, might need to make it more complicated

* add option to be shifted by column or row

* update unit test

* add pymon to gitignore

* Address Chris's comment
Change class and variables name.
Add shift_by variable.
Add sanity check.

* update unit test

* replace old class calls

* change default value

* add more descriptions for Triangle2DPattern
  • Loading branch information
daico007 authored Jun 6, 2023
1 parent 5e505ab commit 7eb74c6
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
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
65 changes: 65 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,70 @@ 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.
Generate a square grid of dimensions n by m, then shifts points accordingly to generate
a triangular arrangement. shift='n' means shifting occurs in the x direction, where the
code shift every other row in the range specified by 'm', and vice versa for shift='m'.
By default, shifting will be half the distance betwen neighboring points in the direction
of shifting.
Notes
-----
Points span [0,1) along x and y axes. This code will allow patterns to be generated that
are only periodic in one direction. To generate a periodic pattern, 'm' should be even
when shift='n' and vice versa.
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="n", 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, "m")
assert len(pattern) == 25

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

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

0 comments on commit 7eb74c6

Please sign in to comment.