remnoving isolated vertices and elements from unstructured mesh vtk file

59 Views Asked by At

I have utilized pygmsh in Python to generate an unstructured mesh file in VTK format. Mesh refinement has been applied near a line s1 and a point s2. However, upon exporting the VTK file, I noticed isolated vertices on top of the line s1 and the point s2, which are not connected to any other triangles. I aim to remove these vertices to prevent them from being displayed in ParaView. Additionally, I do not want the line s1 to be included in the mesh, as it was only used for refinement purposes. My intention is to utilize pygmsh for mesh generation for a Hydraulic model called Adaptive Hydraulics (AdH). Any assistance with this issue would be greatly appreciated. Below is my code:

import numpy as np
import pygmsh
import meshio

# Create geometry and mesh
with pygmsh.geo.Geometry() as geom:
    # Define the polygon
    poly = geom.add_polygon(
        [
            [0.0, 0.0],
            [2.0, 0.0],
            [3.0, 1.0],
            [1.0, 2.0],
            [0.0, 1.0],
        ],
        mesh_size=0.3,
    )

    # Define a spline
    s1 = geom.add_spline([poly.points[0], poly.points[2]])

    # Add a point
    s2 = geom.add_point([1, 1])

    # Define boundary layer refinement
    field1 = geom.add_boundary_layer(
        nodes_list=[poly.points[0], poly.points[2], s2],
        edges_list=[s1],
        lcmin=0.05,
        lcmax=0.2,
        distmin=0.1,
        distmax=0.4,
    )

    # Generate mesh
    geom.set_background_mesh([field1], operator="Min")
    mesh = geom.generate_mesh()

# Export mesh vertices to a text file
vertices = mesh.points
np.savetxt("mesh_vertices.txt", vertices, fmt="%.6f")

# Get triangle data
triangles = None
for cell_block in mesh.cells:
    if cell_block.type == "triangle":
        triangles = cell_block

# Check if triangles are found
if triangles is None:
    raise ValueError("Triangle cells not found in the mesh.")

# Get the node indices for each triangle
triangle_node_ids = triangles.data

# Save triangle data to a text file
with open("triangle_data.txt", "w") as f:
    for i, triangle in enumerate(triangle_node_ids):
        # Adjust node indices to start from 1
        adjusted_triangle = [node_id for node_id in triangle]
        node_ids_str = " ".join(str(node_id) for node_id in adjusted_triangle)
        f.write(f"Triangle {i+1}: {node_ids_str}\n")

print("Mesh vertices and triangle data have been saved to text files.")

# Write the mesh to a VTK file
mesh.write("out5.vtk")

Here is the photo of some of the isolated vertices (marked in pink usign paraview) that are not connected to any triangular elements: enter image description here

here is the vtk file in google drive: https://drive.google.com/file/d/1fPkuNnix9Zlqq-FY-jxx-I8lgG5_Ld46/view?usp=sharing

0

There are 0 best solutions below