clearer naming + better comments, thanks to @p.eisenlohr

This commit is contained in:
Martin Diehl 2020-12-07 17:49:37 +01:00
parent 171d642dbd
commit 78192ef3fd
5 changed files with 30 additions and 30 deletions

View File

@ -302,7 +302,7 @@ class Grid:
Each unique combintation of values results in one material ID.
"""
cells,size,origin = grid_filters.cellSizeOrigin_coordinates0_point(table.get(coordinates))
cells,size,origin = grid_filters.cellsSizeOrigin_coordinates0_point(table.get(coordinates))
labels_ = [labels] if isinstance(labels,str) else labels
unique,unique_inverse = np.unique(np.hstack([table.get(l) for l in labels_]),return_inverse=True,axis=0)

View File

@ -763,7 +763,7 @@ class Rotation:
def _dg(eu,deg):
"""Return infinitesimal Euler space volume of bin(s)."""
phi_sorted = eu[np.lexsort((eu[:,0],eu[:,1],eu[:,2]))]
steps,size,_ = grid_filters.cellSizeOrigin_coordinates0_point(phi_sorted)
steps,size,_ = grid_filters.cellsSizeOrigin_coordinates0_point(phi_sorted)
delta = np.radians(size/steps) if deg else size/steps
return delta[0]*2.0*np.sin(delta[1]/2.0)*delta[2] / 8.0 / np.pi**2 * np.sin(np.radians(eu[:,1]) if deg else eu[:,1])

View File

@ -208,7 +208,7 @@ def coordinates_point(size,F,origin=_np.zeros(3)):
return coordinates0_point(F.shape[:3],size,origin) + displacement_point(size,F)
def cellSizeOrigin_coordinates0_point(coordinates0,ordered=True):
def cellsSizeOrigin_coordinates0_point(coordinates0,ordered=True):
"""
Return grid 'DNA', i.e. cells, size, and origin from 1D array of point positions.
@ -218,6 +218,7 @@ def cellSizeOrigin_coordinates0_point(coordinates0,ordered=True):
Undeformed cell coordinates.
ordered : bool, optional
Expect coordinates0 data to be ordered (x fast, z slow).
Defaults to True.
"""
coords = [_np.unique(coordinates0[:,i]) for i in range(3)]
@ -242,7 +243,7 @@ def cellSizeOrigin_coordinates0_point(coordinates0,ordered=True):
if not (_np.allclose(coords[0],_np.linspace(start[0],end[0],cells[0]),atol=atol) and \
_np.allclose(coords[1],_np.linspace(start[1],end[1],cells[1]),atol=atol) and \
_np.allclose(coords[2],_np.linspace(start[2],end[2],cells[2]),atol=atol)):
raise ValueError('Regular cells spacing violated.')
raise ValueError('Regular cell spacing violated.')
if ordered and not _np.allclose(coordinates0.reshape(tuple(cells)+(3,),order='F'),
coordinates0_point(cells,size,origin),atol=atol):
@ -261,7 +262,7 @@ def coordinates0_check(coordinates0):
Array of undeformed cell coordinates.
"""
cellSizeOrigin_coordinates0_point(coordinates0,ordered=True)
cellsSizeOrigin_coordinates0_point(coordinates0,ordered=True)
def coordinates0_node(cells,size,origin=_np.zeros(3)):
@ -296,7 +297,7 @@ def displacement_fluct_node(size,F):
Deformation gradient field.
"""
return point_2_node(displacement_fluct_point(size,F))
return point_to_node(displacement_fluct_point(size,F))
def displacement_avg_node(size,F):
@ -347,7 +348,7 @@ def coordinates_node(size,F,origin=_np.zeros(3)):
return coordinates0_node(F.shape[:3],size,origin) + displacement_node(size,F)
def point_2_node(cell_data):
def point_to_node(cell_data):
"""Interpolate periodic point data to nodal data."""
n = ( cell_data + _np.roll(cell_data,1,(0,1,2))
+ _np.roll(cell_data,1,(0,)) + _np.roll(cell_data,1,(1,)) + _np.roll(cell_data,1,(2,))
@ -365,7 +366,7 @@ def node_2_point(node_data):
return c[1:,1:,1:]
def cellSizeOrigin_coordinates0_node(coordinates0,ordered=True):
def cellsSizeOrigin_coordinates0_node(coordinates0,ordered=True):
"""
Return grid 'DNA', i.e. cells, size, and origin from 1D array of nodal positions.
@ -375,6 +376,7 @@ def cellSizeOrigin_coordinates0_node(coordinates0,ordered=True):
Undeformed nodal coordinates.
ordered : bool, optional
Expect coordinates0 data to be ordered (x fast, z slow).
Defaults to True.
"""
coords = [_np.unique(coordinates0[:,i]) for i in range(3)]
@ -391,7 +393,7 @@ def cellSizeOrigin_coordinates0_node(coordinates0,ordered=True):
if not (_np.allclose(coords[0],_np.linspace(mincorner[0],maxcorner[0],cells[0]+1),atol=atol) and \
_np.allclose(coords[1],_np.linspace(mincorner[1],maxcorner[1],cells[1]+1),atol=atol) and \
_np.allclose(coords[2],_np.linspace(mincorner[2],maxcorner[2],cells[2]+1),atol=atol)):
raise ValueError('Regular cells spacing violated.')
raise ValueError('Regular cell spacing violated.')
if ordered and not _np.allclose(coordinates0.reshape(tuple(cells+1)+(3,),order='F'),
coordinates0_node(cells,size,origin),atol=atol):
@ -400,9 +402,9 @@ def cellSizeOrigin_coordinates0_node(coordinates0,ordered=True):
return (cells,size,origin)
def regrid(size,F,cells_new):
def regrid(size,F,cells):
"""
Return mapping from coordinates in deformed configuration to a regular cells.
Return mapping from coordinates in deformed configuration to a regular grid.
Parameters
----------
@ -410,13 +412,11 @@ def regrid(size,F,cells_new):
Physical size.
F : numpy.ndarray of shape (:,:,:,3,3)
Deformation gradient field.
cells_new : numpy.ndarray of shape (3)
New cells for undeformed coordinates.
cells : numpy.ndarray of shape (3)
Cell count along x,y,z of remapping grid.
"""
c = coordinates0_point(F.shape[:3],size) \
+ displacement_avg_point(size,F) \
+ displacement_fluct_point(size,F)
c = coordinates_point(size,F)
outer = _np.dot(_np.average(F,axis=(0,1,2)),size)
for d in range(3):
@ -424,4 +424,4 @@ def regrid(size,F,cells_new):
c[_np.where(c[:,:,:,d]>outer[d])] -= outer[d]
tree = _spatial.cKDTree(c.reshape(-1,3),boxsize=outer)
return tree.query(coordinates0_point(cells_new,outer))[1].flatten()
return tree.query(coordinates0_point(cells,outer))[1].flatten()

View File

@ -18,8 +18,8 @@ def from_random(size,N_seeds,cells=None,rng_seed=None):
N_seeds : int
Number of seeds.
cells : numpy.ndarray of shape (3), optional.
If given, ensures that all seeds initiate one grain if using a
standard Voronoi tessellation.
If given, ensures that each seed results in a grain for a standard
Voronoi tessellation.
rng_seed : {None, int, array_like[ints], SeedSequence, BitGenerator, Generator}, optional
A seed to initialize the BitGenerator. Defaults to None.
If None, then fresh, unpredictable entropy will be pulled from the OS.
@ -51,7 +51,7 @@ def from_Poisson_disc(size,N_seeds,N_candidates,distance,periodic=True,rng_seed=
distance : float
Minimum acceptable distance to other seeds.
periodic : boolean, optional
Calculate minimum distance for periodically repeated cells.
Calculate minimum distance for periodically repeated grid.
rng_seed : {None, int, array_like[ints], SeedSequence, BitGenerator, Generator}, optional
A seed to initialize the BitGenerator. Defaults to None.
If None, then fresh, unpredictable entropy will be pulled from the OS.

View File

@ -26,12 +26,12 @@ class TestGridFilters:
@pytest.mark.parametrize('mode',['point','node'])
def test_grid_DNA(self,mode):
"""Ensure that cellSizeOrigin_coordinates0_xx is the inverse of coordinates0_xx."""
"""Ensure that cellsSizeOrigin_coordinates0_xx is the inverse of coordinates0_xx."""
cells = np.random.randint(8,32,(3))
size = np.random.random(3)
origin = np.random.random(3)
coord0 = eval(f'grid_filters.coordinates0_{mode}(cells,size,origin)') # noqa
_cells,_size,_origin = eval(f'grid_filters.cellSizeOrigin_coordinates0_{mode}(coord0.reshape(-1,3,order="F"))')
_cells,_size,_origin = eval(f'grid_filters.cellsSizeOrigin_coordinates0_{mode}(coord0.reshape(-1,3,order="F"))')
assert np.allclose(cells,_cells) and np.allclose(size,_size) and np.allclose(origin,_origin)
def test_displacement_fluct_equivalence(self):
@ -40,14 +40,14 @@ class TestGridFilters:
cells = np.random.randint(8,32,(3))
F = np.random.random(tuple(cells)+(3,3))
assert np.allclose(grid_filters.displacement_fluct_node(size,F),
grid_filters.point_2_node(grid_filters.displacement_fluct_point(size,F)))
grid_filters.point_to_node(grid_filters.displacement_fluct_point(size,F)))
def test_interpolation_to_node(self):
size = np.random.random(3)
cells = np.random.randint(8,32,(3))
F = np.random.random(tuple(cells)+(3,3))
assert np.allclose(grid_filters.coordinates_node(size,F) [1:-1,1:-1,1:-1],
grid_filters.point_2_node(grid_filters.coordinates_point(size,F))[1:-1,1:-1,1:-1])
grid_filters.point_to_node(grid_filters.coordinates_point(size,F))[1:-1,1:-1,1:-1])
def test_interpolation_to_cell(self):
cells = np.random.randint(1,30,(3))
@ -94,15 +94,15 @@ class TestGridFilters:
assert np.allclose(function(size,F),0.0)
@pytest.mark.parametrize('function',[grid_filters.coordinates0_check,
grid_filters.cellSizeOrigin_coordinates0_node,
grid_filters.cellSizeOrigin_coordinates0_point])
grid_filters.cellsSizeOrigin_coordinates0_node,
grid_filters.cellsSizeOrigin_coordinates0_point])
def test_invalid_coordinates(self,function):
invalid_coordinates = np.random.random((np.random.randint(12,52),3))
with pytest.raises(ValueError):
function(invalid_coordinates)
@pytest.mark.parametrize('function',[grid_filters.cellSizeOrigin_coordinates0_node,
grid_filters.cellSizeOrigin_coordinates0_point])
@pytest.mark.parametrize('function',[grid_filters.cellsSizeOrigin_coordinates0_node,
grid_filters.cellsSizeOrigin_coordinates0_point])
def test_uneven_spaced_coordinates(self,function):
start = np.random.random(3)
end = np.random.random(3)*10. + start
@ -116,8 +116,8 @@ class TestGridFilters:
@pytest.mark.parametrize('mode',[True,False])
@pytest.mark.parametrize('function',[grid_filters.cellSizeOrigin_coordinates0_node,
grid_filters.cellSizeOrigin_coordinates0_point])
@pytest.mark.parametrize('function',[grid_filters.cellsSizeOrigin_coordinates0_node,
grid_filters.cellsSizeOrigin_coordinates0_point])
def test_unordered_coordinates(self,function,mode):
origin = np.random.random(3)
size = np.random.random(3)*10.+origin