Generate mesh from .dat file

Hi everyone,

Is it possible for me to get a help with my .dat file? I would like to import it into ParaView, and then generate the view of the mesh, but I’m not havin success with that. Could you please help me? Here you have my .dat (mesh) file.
FEM_OPTARQ -h3.dat (34.6 KB)

Best,
Vinícius Gonçalves

To load your .dat file, it needs to be converted into a format that ParaView can read. This requires creating a custom conversion program, but nowadays, generative AI makes this process much easier. Below is a Python program generated by Grok, which required minimal modifications. Please use it as a reference.

import vtk
import numpy as np

def parse_dat_file(dat_file_path):
    # Initialize lists to store nodes and elements
    nodes = []
    elements = []
    reading_nodes = False
    reading_elements = False

    # Open and read the DAT file
    with open(dat_file_path, 'r') as file:
        lines = file.readlines()

    # Process each line in the file
    for line in lines:
        line = line.strip()
        # Start reading node coordinates
        if line.startswith('NODE_COORDINATES'):
            reading_nodes = True
            continue
        # Start reading element connectivity
        elif line.startswith('ELEMENTS'):
            reading_nodes = False
            reading_elements = True
            continue
        # Stop reading elements at THICKNESS section
        elif line.startswith('THICKNESS'):
            reading_elements = False
            continue

        # Parse node coordinates
        if reading_nodes and line:
            parts = line.split()
            if len(parts) >= 4:
                node_id = int(parts[0]) - 1  # Convert to 0-based indexing for VTK
                x, y, z = map(float, parts[1:4])
                nodes.append([x, y, z])

        # Parse element connectivity
        if reading_elements and line:
            parts = line.split()
            if len(parts) >= 10:
                elem_id = int(parts[0]) - 1  # Convert to 0-based indexing for VTK
                node_ids = [int(n) - 1 for n in parts[2:10]]  # Extract 8 node IDs for hexahedron
                elements.append(node_ids)

    return np.array(nodes), elements

def create_vtu_file(nodes, elements, output_file):
    # Create a VTK UnstructuredGrid object
    unstructured_grid = vtk.vtkUnstructuredGrid()
    points = vtk.vtkPoints()

    # Set node coordinates
    for node in nodes:
        points.InsertNextPoint(node)
    unstructured_grid.SetPoints(points)

    # Set elements (hexahedrons)
    for elem in elements:
        hex = vtk.vtkHexahedron()
        for i, node_id in enumerate(elem):
            hex.GetPointIds().SetId(i, node_id)
        unstructured_grid.InsertNextCell(hex.GetCellType(), hex.GetPointIds())

    # Write to VTU file
    writer = vtk.vtkXMLUnstructuredGridWriter()
    writer.SetFileName(output_file)
    writer.SetInputData(unstructured_grid)
    writer.Write()

def main():
    # Define input and output file paths
    input_dat = "FEM_OPTARQ -h3.dat"
    output_vtu = "output.vtu"
    # Parse the DAT file
    nodes, elements = parse_dat_file(input_dat)
    # Create the VTU file
    create_vtu_file(nodes, elements, output_vtu)
    print(f"VTU file '{output_vtu}' has been generated successfully.")

if __name__ == "__main__":
    main()

The converted vtu file is also attached.
output.vtu (8.5 KB)

1 Like

Really helpful, thanks @Kenichiro-Yoshimi!!