Skip to content

Commit

Permalink
Removed unnecessary packages & bugfix in vtk export
Browse files Browse the repository at this point in the history
  • Loading branch information
melc-da committed Aug 22, 2024
2 parents 4aef5c4 + 784572b commit 97646fe
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 34 deletions.
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ authors:
given-names: "Eric"
orcid: "https://orcid.org/0000-0002-3479-9143"
title: "Crack Analysis Tool in Python - CrackPy"
version: 1.2.2
version: 1.2.3
doi: 10.1038/s41598-024-63915-x
date-released: 2024-07-23
date-released: 2024-08-20
url: "https://github.com/dlr-wf/crackpy"
2 changes: 1 addition & 1 deletion crackpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
import crackpy.structure_elements

# package information
__version__ = "1.2.2"
__version__ = "1.2.3"
52 changes: 27 additions & 25 deletions crackpy/fracture_analysis/data_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import numpy as np
import pyvista
from pyvista import CellType
from copy import deepcopy
from crackpy.structure_elements import data_files
from crackpy.structure_elements.material import Material

Expand Down Expand Up @@ -110,13 +111,25 @@ def set_connection_file(self, connection_file: str, folder: str):
"""
connection_file_path = os.path.join(folder, connection_file)
np_df = np.genfromtxt(connection_file_path, dtype=int, delimiter=';', skip_header=1)

# Check if there are any elements with -1 as node number
self.connections = self._cut_none_elements(np_df)

# remap node numbers
old_facet_id = self.facet_id - 1
new_facet_id = np.arange(0, len(self.facet_id))
mapping = dict(zip(old_facet_id, new_facet_id))
renumber_nodes_vec = np.vectorize(self._renumber_nodes)
self.connections[:, 2:] = renumber_nodes_vec(self.connections[:, 2:], mapping)

# Check if there are any elements with -1 as node number
self.connections = self._cut_none_elements(self.connections)

def read_header(self, meta_attributes_to_keywords: dict = None):
"""Get meta data by reading from header.
"""Get metadata by reading from header.
Args:
meta_attributes_to_keywords: dictionary with meta data attributes as keys and corresponding keyword in header as values
meta_attributes_to_keywords: dictionary with metadata attributes as keys and corresponding keyword in header as values
if None, the class attributes are used as keywords
"""
Expand Down Expand Up @@ -339,7 +352,8 @@ def _calculate_principal_stresses(self):
self.sig_1 = principal_stresses[:, 0]
self.sig_2 = principal_stresses[:, 1]

def _renumber_nodes(self, old_node_number: int, mapping: dict):
@staticmethod
def _renumber_nodes(old_node_number: int, mapping: dict):
"""Renumber nodes according to mapping table.
Args:
Expand All @@ -350,11 +364,7 @@ def _renumber_nodes(self, old_node_number: int, mapping: dict):
new_node_number: it is -1 if the node number is not in the mapping table
"""
try:
new_node_number = mapping[old_node_number]
except KeyError:
new_node_number = -1

new_node_number = mapping.get(old_node_number, -1)
return new_node_number

def to_vtk(self, output_folder: str = None, metadata: bool = True, alpha: float = 1.0):
Expand All @@ -369,25 +379,16 @@ def to_vtk(self, output_folder: str = None, metadata: bool = True, alpha: float
Returns:
PyVista mesh object
"""
# create node
# create nodes
nodes = np.stack((self.coor_x, self.coor_y, self.coor_z), axis=1)

# remap node numbers
old_facet_id = self.facet_id - 1
new_facet_id = np.arange(0, len(self.facet_id))
mapping = dict(zip(old_facet_id, new_facet_id))
renumber_nodes_vec = np.vectorize(self._renumber_nodes)

# create mesh
if self.connections is not None:
# define elements
elements = self.connections[:, 1:5]
elements[:, 0] = 3
elements[:, 1:] = renumber_nodes_vec(elements[:, 1:], mapping)
elements = deepcopy(self.connections[:, 1:5])

# Check if there are any elements with -1 as node number
mask = np.any(elements[:, 1:] == -1, axis=1)
elements = elements[~mask]
# Element type 3 is a triangle
elements[:, 0] = 3

# define cell types
cell_types = np.full(len(elements[:, 0]), fill_value=CellType.TRIANGLE, dtype=np.uint8)
Expand All @@ -396,11 +397,12 @@ def to_vtk(self, output_folder: str = None, metadata: bool = True, alpha: float
mesh = pyvista.UnstructuredGrid(elements, cell_types, nodes)

else:
print(f'No connectivity data provided for {self.nodemap_name}. Reconstructing a mesh from the nodes using Delaunay triangulation with alpha = {alpha}.')
cloud = pyvista.wrap(nodes)
print(
f'No connectivity data provided for {self.nodemap_name}. '
f'Reconstructing a mesh from the nodes using Delaunay triangulation with alpha = {alpha}.')
cloud = pyvista.PolyData(nodes)
mesh = cloud.delaunay_2d(alpha=alpha)


# add data
mesh.point_data['x [mm]'] = self.coor_x
mesh.point_data['y [mm]'] = self.coor_y
Expand Down Expand Up @@ -452,7 +454,7 @@ def _cut_nans(df):
@staticmethod
def _cut_none_elements(df):
"""Reads an array and deletes each row containing any '-1' value."""
mask = np.any(df == -1, axis=1)
mask = np.any(df.astype(int) == -1, axis=1)
cut_none_elements = df[~mask]
return cut_none_elements

Expand Down
4 changes: 1 addition & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ matplotlib>=3.5.3
numpy>=1.23.2
opencv_python>=4.5.4.60
pandas>=1.4.3
Pillow>=9.2.0
pyvista>=0.37.0
scikit_image>=0.19.3
scikit_learn>=1.1.2
scipy>=1.9.0
seaborn>=0.11.2
torch>=1.13.1
torchvision>=0.13.1
rich>=12.5.1
rich>=12.5.1
3 changes: 0 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@ def get_property(prop, project):
license='MIT',
include_package_data=True,
install_requires=[
'ansys_mapdl_core>=0.63.0',
'matplotlib>=3.5.3',
'numpy>=1.23.2',
'opencv_python>=4.5.4.60',
'pandas>=1.4.3',
'Pillow>=9.2.0',
'pyvista>=0.37.0',
'scikit_image>=0.19.3',
'scikit_learn>=1.1.2',
'scipy>=1.9.0',
'seaborn>=0.11.2',
'torch>=1.13.1',
'torchvision>=0.13.1',
'rich>=12.5.1'
Expand Down

0 comments on commit 97646fe

Please sign in to comment.