Skip to content

Commit

Permalink
Added method to compute mesh resolution for rectangular prisms
Browse files Browse the repository at this point in the history
  • Loading branch information
calleman21 committed Oct 3, 2024
1 parent f64e275 commit c65d414
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
19 changes: 11 additions & 8 deletions exodus_helper/element_calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def calculate_volume_element(coords, shape_functions='trilinear'):
Args:
coords (list(list(float))): A length-8 (HEX) list of nodal
coordinate values.
shape_functions (str, optional): The name of the shape function used to
interpolate the element's volume from a list of discrete nodal
shape_functions (str, optional): The name of the shape function used to
interpolate the element's volume from a list of discrete nodal
coordinate values. Defaults to `trilinear`.
Returns:
Expand Down Expand Up @@ -190,7 +190,7 @@ def calculate_volume_hexahedron(coords):
numpy.float64: The volume of the hexahedron.
Raises:
AssertionError: The coordinates are not compatible with right-hand
AssertionError: The coordinates are not compatible with right-hand
positive winding.
"""
centroid = np.mean(coords, axis=0)
Expand All @@ -215,8 +215,8 @@ def calculate_volume_hexahedron(coords):
return volume


def calculate_volumes_element(mesh, id_blk):
"""Calculate the volume of each element in a specified element block within
def calculate_volumes_element(mesh, id_blk=None):
"""Calculate the volume of each element in a specified element block within
a given mesh.
Args:
Expand All @@ -226,8 +226,11 @@ def calculate_volumes_element(mesh, id_blk):
Returns:
numpy.ndarray(float): An array of element volumes ordered by element ID
"""
connectivity, num_elems, num_nodes = mesh.get_elem_connectivity(id_blk)
connectivity = connectivity.reshape((num_elems, num_nodes))
if id_blk:
connectivity, num_elems, num_nodes = mesh.get_elem_connectivity(id_blk)
connectivity = connectivity.reshape((num_elems, num_nodes))
else:
connectivity = mesh.get_elem_connectivity_full()
coords_node = np.column_stack(mesh.get_coords())
volumes_element = np.array(
[calculate_volume_element(coords_node[ns - 1]) for ns in connectivity])
Expand All @@ -241,7 +244,7 @@ def calculate_volumes_block(mesh):
mesh (Exodus): The `Exodus` instance of a mesh.
Returns:
numpy.ndarray(float): An array of element volumes ordered by element
numpy.ndarray(float): An array of element volumes ordered by element
block ID.
"""
ids_blk = mesh.get_elem_blk_ids()
Expand Down
9 changes: 9 additions & 0 deletions exodus_helper/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ def __init__(
self.put_elem_id_map(range(1, num_elements + 1))
self.put_node_id_map(range(1, num_nodes + 1))

@property
def resolution(self):
return self.get_resolution()

@property
def shape(self):
"""tuple(int, int, int): The shape of the mesh."""
Expand Down Expand Up @@ -261,6 +265,11 @@ def get_patches_on_surface(self, surface):
[coordinates[f - 1] for f in face[1][:num_check]]))
return patches

def get_resolution(self):
conn = self.get_elem_connectivity_full()
coords = np.column_stack(self.get_coords())
return np.max(np.abs(np.diff(coords[conn[0] - 1], axis=0)), axis=0)

def get_shape(self):
num_xs = self.numElem // self.get_elements_on_surface(1).size
num_ys = self.numElem // self.get_elements_on_surface(3).size
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = 'poetry.core.masonry.api'

[tool.poetry]
name = 'exodus_helper'
version = '1.0.3'
version = '1.0.4'
description = 'A package for manipulating ExodusII databases'
license = 'BSD-3-Clause'
authors = ['Coleman Alleman <[email protected]>']
Expand Down
14 changes: 14 additions & 0 deletions tests/test_exodus_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,20 @@ def test_get_qa_records(mesh):
assert isinstance(record[3], str)


def test_get_resolution(dir_test_file, monkeypatch):
try:
shape = (3, 4, 5)
resolution = (0.3, 0.2, 0.1)
file_path = os.path.join(dir_test_file, 'test_get_shape.g')
monkeypatch.setattr('builtins.input', lambda _: 'y')
mesh = exodus_helper.RectangularPrism(
file_path, shape=shape, resolution=resolution, mode='w')
assert np.allclose(mesh.get_resolution(), resolution)
mesh.close()
finally:
os.remove(file_path)


def test_get_shape(dir_test_file, monkeypatch):
try:
shape = (3, 4, 5)
Expand Down

0 comments on commit c65d414

Please sign in to comment.