Skip to content

Commit

Permalink
fix: moved generic methods to base class
Browse files Browse the repository at this point in the history
- cell indexing, global indexing etc
  • Loading branch information
Lachlan Grose committed Oct 8, 2021
1 parent e4365a0 commit 23ec788
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 230 deletions.
125 changes: 123 additions & 2 deletions LoopStructural/interpolators/base_structured_3d_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self,
# we use property decorators to update these when different parts of
# the geometry need to change
# inisialise the private attributes
self._nsteps = np.array(nsteps)
self._nsteps = np.array(nsteps,dtype=int)
self._step_vector = np.array(step_vector)
self._origin = np.array(origin)
self.supporttype='Base'
Expand Down Expand Up @@ -181,4 +181,125 @@ def check_position(self, pos):
if len(pos.shape) != 2:
print("Position array needs to be a list of points or a point")
return False
return pos
return pos

def global_cell_indicies(self, indexes):
"""
Convert from cell indexes to global cell index
Parameters
----------
indexes
Returns
-------
"""
indexes = np.array(indexes).swapaxes(0, 2)
return indexes[:, :, 0] + self.nsteps_cells[None, None, 0] \
* indexes[:, :, 1] + self.nsteps_cells[None, None, 0] * \
self.nsteps_cells[None, None, 1] * indexes[:, :, 2]

def cell_corner_indexes(self, x_cell_index, y_cell_index, z_cell_index):
"""
Returns the indexes of the corners of a cell given its location xi,
yi, zi
Parameters
----------
x_cell_index
y_cell_index
z_cell_index
Returns
-------
"""
xcorner = np.array([0, 1, 0, 0, 1, 0, 1, 1])
ycorner = np.array([0, 0, 1, 0, 0, 1, 1, 1])
zcorner = np.array([0, 0, 0, 1, 1, 1, 0, 1])
xcorners = x_cell_index[:, None] + xcorner[None, :]
ycorners = y_cell_index[:, None] + ycorner[None, :]
zcorners = z_cell_index[:, None] + zcorner[None, :]
return xcorners, ycorners, zcorners

def position_to_cell_corners(self, pos):

inside = self.inside(pos)
ix, iy, iz = self.position_to_cell_index(pos)
cornersx, cornersy, cornersz = self.cell_corner_indexes(ix, iy, iz)
globalidx = self.global_indicies(
np.dstack([cornersx, cornersy, cornersz]).T)
# if global index is not inside the support set to -1
globalidx[~inside] = -1
return globalidx, inside

def node_indexes_to_position(self, xindex, yindex, zindex):

x = self.origin[0] + self.step_vector[0] * xindex
y = self.origin[1] + self.step_vector[1] * yindex
z = self.origin[2] + self.step_vector[2] * zindex

return x, y, z

def global_index_to_cell_index(self, global_index):
"""
Convert from global indexes to xi,yi,zi
Parameters
----------
global_index
Returns
-------
"""
# determine the ijk indices for the global index.
# remainder when dividing by nx = i
# remained when dividing modulus of nx by ny is j

x_index = global_index % self.nsteps_cells[0, None]
y_index = global_index // self.nsteps_cells[0, None] % \
self.nsteps_cells[1, None]
z_index = global_index // self.nsteps_cells[0, None] // \
self.nsteps_cells[1, None]
return x_index, y_index, z_index


def global_index_to_node_index(self, global_index):
"""
Convert from global indexes to xi,yi,zi
Parameters
----------
global_index
Returns
-------
"""
# determine the ijk indices for the global index.
# remainder when dividing by nx = i
# remained when dividing modulus of nx by ny is j
x_index = global_index % self.nsteps[0, None]
y_index = global_index // self.nsteps[0, None] % \
self.nsteps[1, None]
z_index = global_index // self.nsteps[0, None] // \
self.nsteps[1, None]
return x_index, y_index, z_index
def global_node_indicies(self, indexes):
"""
Convert from node indexes to global node index
Parameters
----------
indexes
Returns
-------
"""
indexes = np.array(indexes).swapaxes(0, 2)
return indexes[:, :, 0] + self.nsteps[None, None, 0] \
* indexes[:, :, 1] + self.nsteps[None, None, 0] * \
self.nsteps[None, None, 1] * indexes[:, :, 2]
60 changes: 1 addition & 59 deletions LoopStructural/interpolators/structured_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,70 +194,12 @@ def neighbour_global_indexes(self, mask = None, **kwargs):
self.nsteps[0, None, None] * self.nsteps[
1, None, None] * neighbours[2, :, :]).astype(np.int64)

def cell_corner_indexes(self, x_cell_index, y_cell_index, z_cell_index):
"""
Returns the indexes of the corners of a cell given its location xi,
yi, zi


Parameters
----------
x_cell_index
y_cell_index
z_cell_index

Returns
-------

"""
xcorner = np.array([0, 1, 0, 0, 1, 0, 1, 1])
ycorner = np.array([0, 0, 1, 0, 0, 1, 1, 1])
zcorner = np.array([0, 0, 0, 1, 1, 1, 0, 1])
xcorners = x_cell_index[:, None] + xcorner[None, :]
ycorners = y_cell_index[:, None] + ycorner[None, :]
zcorners = z_cell_index[:, None] + zcorner[None, :]
return xcorners, ycorners, zcorners

def global_index_to_cell_index(self, global_index):
"""
Convert from global indexes to xi,yi,zi

Parameters
----------
global_index
Returns
-------

"""
# determine the ijk indices for the global index.
# remainder when dividing by nx = i
# remained when dividing modulus of nx by ny is j

x_index = global_index % self.nsteps_cells[0, None]
y_index = global_index // self.nsteps_cells[0, None] % \
self.nsteps_cells[1, None]
z_index = global_index // self.nsteps_cells[0, None] // \
self.nsteps_cells[1, None]
return x_index, y_index, z_index

def node_indexes_to_position(self, xindex, yindex, zindex):

x = self.origin[0] + self.step_vector[0] * xindex
y = self.origin[1] + self.step_vector[1] * yindex
z = self.origin[2] + self.step_vector[2] * zindex

return x, y, z

def position_to_cell_corners(self, pos):

inside = self.inside(pos)
ix, iy, iz = self.position_to_cell_index(pos)
cornersx, cornersy, cornersz = self.cell_corner_indexes(ix, iy, iz)
globalidx = self.global_indicies(
np.dstack([cornersx, cornersy, cornersz]).T)
# if global index is not inside the support set to -1
globalidx[~inside] = -1
return globalidx, inside

def evaluate_value(self, evaluation_points, property_array):
"""
Expand Down
171 changes: 2 additions & 169 deletions LoopStructural/interpolators/structured_tetra.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,34 +268,7 @@ def get_constant_gradient(self, region, direction=None):
B = np.zeros(c.shape[0])
self.cg = (c,idc,B)
return self.cg[0], self.cg[1], self.cg[2]
def get_constant_norm(self, region):
"""
Get the constant gradient for the specified nodes

Parameters
----------
region : np.array(dtype=bool)
mask of nodes to calculate cg for
Returns
-------
"""

logger.info("Running constant gradient")
elements_gradients = self.get_element_gradients(np.arange(self.ntetra))
region = region.astype('int64')

neighbours = self.get_neighbours()
elements = self.get_elements()
idc, c, ncons = constant_norm(elements_gradients, neighbours.astype('int64'), elements.astype('int64'), self.nodes,
region.astype('int64'))

idc = np.array(idc[:ncons, :])
c = np.array(c[:ncons, :])
B = np.zeros(c.shape[0])

return c,idc,B
def get_elements(self):
"""
Get a numpy array of all of the elements in the mesh
Expand Down Expand Up @@ -412,151 +385,11 @@ def get_tetra_gradient_for_location(self, pos):



def global_node_indicies(self, indexes):
"""
Convert from node indexes to global node index
Parameters
----------
indexes
Returns
-------
"""
indexes = np.array(indexes).swapaxes(0, 2)
return indexes[:, :, 0] + self.nsteps[None, None, 0] \
* indexes[:, :, 1] + self.nsteps[None, None, 0] * \
self.nsteps[None, None, 1] * indexes[:, :, 2]

def global_cell_indicies(self, indexes):
"""
Convert from cell indexes to global cell index
Parameters
----------
indexes
Returns
-------
"""
indexes = np.array(indexes).swapaxes(0, 2)
return indexes[:, :, 0] + self.nsteps_cells[None, None, 0] \
* indexes[:, :, 1] + self.nsteps_cells[None, None, 0] * \
self.nsteps_cells[None, None, 1] * indexes[:, :, 2]

def cell_corner_indexes(self, x_cell_index, y_cell_index, z_cell_index):
"""
Returns the indexes of the corners of a cell given its location xi,
yi, zi
Parameters
----------
x_cell_index
y_cell_index
z_cell_index
Returns
-------
"""
x_cell_index = np.array(x_cell_index)
y_cell_index = np.array(y_cell_index)
z_cell_index = np.array(z_cell_index)

xcorner = np.array([0, 1, 0, 1, 0, 1, 0, 1])
ycorner = np.array([0, 0, 1, 1, 0, 0, 1, 1])
zcorner = np.array([0, 0, 0, 0, 1, 1, 1, 1])
xcorners = x_cell_index[:, None] + xcorner[None, :]
ycorners = y_cell_index[:, None] + ycorner[None, :]
zcorners = z_cell_index[:, None] + zcorner[None, :]
return xcorners, ycorners, zcorners

def position_to_cell_corners(self, pos):
"""
Find the nodes that belong to a cell which contains a point
Parameters
----------
pos
Returns
-------

"""
inside = self.inside(pos)
ix, iy, iz = self.position_to_cell_index(pos)
cornersx, cornersy, cornersz = self.cell_corner_indexes(ix, iy, iz)
globalidx = self.global_cell_indicies(
np.dstack([cornersx, cornersy, cornersz]).T)
return globalidx, inside




def node_indexes_to_position(self, xindex, yindex, zindex):
"""
Get the xyz position from the node coordinates
Parameters
----------
xindex
yindex
zindex
Returns
-------
"""
x = self.origin[0] + self.step_vector[0] * xindex
y = self.origin[1] + self.step_vector[1] * yindex
z = self.origin[2] + self.step_vector[2] * zindex

return np.array([x, y, z])

def global_index_to_node_index(self, global_index):
"""
Convert from global indexes to xi,yi,zi
Parameters
----------
global_index
Returns
-------
"""
# determine the ijk indices for the global index.
# remainder when dividing by nx = i
# remained when dividing modulus of nx by ny is j
x_index = global_index % self.nsteps[0, None]
y_index = global_index // self.nsteps[0, None] % \
self.nsteps[1, None]
z_index = global_index // self.nsteps[0, None] // \
self.nsteps[1, None]
return x_index, y_index, z_index

def global_index_to_cell_index(self, global_index):
"""
Convert from global indexes to xi,yi,zi
Parameters
----------
global_index
Returns
-------
"""
# determine the ijk indices for the global index.
# remainder when dividing by nx = i
# remained when dividing modulus of nx by ny is j

x_index = global_index % self.nsteps_cells[0, None]
y_index = global_index // self.nsteps_cells[0, None] % \
self.nsteps_cells[1, None]
z_index = global_index // self.nsteps_cells[0, None] // \
self.nsteps_cells[1, None]
return x_index, y_index, z_index

def get_neighbours(self):
"""
Expand Down

0 comments on commit 23ec788

Please sign in to comment.