Skip to content

Commit

Permalink
Merge 4f12032 into 3e6a289
Browse files Browse the repository at this point in the history
  • Loading branch information
din14970 committed Dec 17, 2020
2 parents 3e6a289 + 4f12032 commit 079e24c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
10 changes: 6 additions & 4 deletions diffsims/generators/rotation_list_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
get_uv_sphere_mesh_vertices,
get_cube_mesh_vertices,
get_icosahedral_mesh_vertices,
get_random_sphere_vertices,
beam_directions_grid_to_euler,
)

Expand Down Expand Up @@ -186,8 +187,8 @@ def get_beam_directions_grid(crystal_system, resolution, mesh="spherified_cube_c
mesh : str
Type of meshing of the sphere that defines how the grid is
created. Options are: uv_sphere, normalized_cube,
spherified_cube_corner (default), spherified_cube_edge, and
icosahedral.
spherified_cube_corner (default), spherified_cube_edge,
icosahedral, random.
Returns
-------
Expand Down Expand Up @@ -216,13 +217,14 @@ def get_beam_directions_grid(crystal_system, resolution, mesh="spherified_cube_c
points_in_cartesians = get_cube_mesh_vertices(
resolution, grid_type="spherified_edge"
)

elif mesh == "random":
points_in_cartesians = get_random_sphere_vertices(resolution)
else:
raise NotImplementedError(
f"The mesh {mesh} is not recognized. "
f"Please use: uv_sphere, normalized_cube, "
f"spherified_cube_edge, "
f"spherified_cube_corner, icosahedral"
f"spherified_cube_corner, icosahedral, random"
)

if crystal_system == "triclinic":
Expand Down
34 changes: 34 additions & 0 deletions diffsims/generators/sphere_mesh_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,40 @@ def get_icosahedral_mesh_vertices(resolution):
return vertices


def get_random_sphere_vertices(resolution,seed=None):
"""
Create a mesh that randomly samples the surface of a sphere
Parameters
----------
resolution : float
The expected mean angle between nearest neighbor
grid points in degrees.
seed : int, optional
passed to np.random.default_rng(), defaults to None which
will give a "new" random result each time
Returns
-------
points_in_cartesian : numpy.ndarray (N,3)
Rows are x, y, z where z is the 001 pole direction
References
----------
https://mathworld.wolfram.com/SpherePointPicking.html
"""
# convert resolution in degrees to number of points
number = int(1/(4*np.pi)*(360/resolution)**2)
if seed is not None:
rng =np.random.default_rng(seed=seed)
else:
rng = np.random.default_rng()

xyz = rng.normal(size=(number, 3))
xyz = (xyz.T/np.linalg.norm(xyz, axis=1)).T
return xyz


def beam_directions_grid_to_euler(vectors):
"""
Convert list of vectors representing zones to a list of Euler angles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def test_get_grid_around_beam_direction():
"spherified_cube_edge",
"spherified_cube_corner",
"icosahedral",
"random",
],
)
@pytest.mark.parametrize(
Expand All @@ -74,6 +75,7 @@ def test_get_grid_around_beam_direction():
def test_get_beam_directions_grid(crystal_system, mesh):
grid = get_beam_directions_grid(crystal_system, 5, mesh=mesh)


@pytest.mark.xfail()
def test_invalid_mesh_beam_directions():
_ = get_beam_directions_grid("cubic", 10, mesh="invalid")
7 changes: 7 additions & 0 deletions diffsims/tests/test_generators/test_sphere_mesh_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
get_uv_sphere_mesh_vertices,
get_cube_mesh_vertices,
get_icosahedral_mesh_vertices,
get_random_sphere_vertices,
beam_directions_grid_to_euler,
_normalize_vectors,
_get_first_nearest_neighbors,
Expand All @@ -30,6 +31,12 @@
)


def test_random_sphere_mesh():
grid = get_random_sphere_vertices(1)
assert grid.shape[0] == 10313
assert grid.shape[1] == 3


def test_get_uv_sphere_mesh_vertices():
grid = get_uv_sphere_mesh_vertices(10)
np.testing.assert_almost_equal(np.sum(grid), 0)
Expand Down

0 comments on commit 079e24c

Please sign in to comment.