Convert PyVista Unstructured Grid to PyDPF Meshed Region

Options
Ayush Kumar
Ayush Kumar Member, Moderator, Employee Posts: 510
100 Answers 250 Likes 100 Comments Second Anniversary
✭✭✭✭

Convert PyVista Unstructured Grid to PyDPF Meshed Region

Tagged:

Comments

  • Ayush Kumar
    Ayush Kumar Member, Moderator, Employee Posts: 510
    100 Answers 250 Likes 100 Comments Second Anniversary
    ✭✭✭✭
    edited June 16
    import pyvista as pv
    from pyvista import examples
    from ansys.dpf import core as dpf
    
    dataset = examples.download_unstructured_grid()
    dataset.plot(show_edges=True)
    
    print(dataset)
    
    # Get Nodes
    nodal_coords = dataset.points
    
    # Get connectivity as a list of lists
    connectivity_list = []
    e_type = []  # Element type list
    for i in range(dataset.n_cells):
        cell = dataset.get_cell(i)
        connectivity_list.append(cell.point_ids)
        e_type.append(pv.CellType(cell.type).name)
    
    # Create DPF Meshed Region
    n_nodes = len(nodal_coords)
    n_elements = len(connectivity_list)
    
    # Convert to DPF Meshed Region
    dpf_mesh = dpf.MeshedRegion(num_nodes=n_nodes, num_elements=n_elements)
    dpf_mesh.unit = "mm"
    
    field_coord = dpf.fields_factory.create_3d_vector_field(n_nodes)
    field_coord.data = nodal_coords.flatten()
    field_coord.scoping.ids = list(range(1, n_nodes + 1))
    
    dpf_mesh.nodes.coordinates_field = field_coord
    
    # Add Elements
    for eid, connect in enumerate(connectivity_list, start=1):
        if e_type[eid - 1] in ["HEXAHEDRON", "TETRA"]:
            dpf_mesh.elements.add_solid_element(eid, connect)
        elif e_type[eid - 1] in ["QUAD", "TRIANGLE"]:
            dpf_mesh.elements.add_shell_element(eid, connect)
        elif e_type[eid - 1] in ["LINE"]:
            dpf_mesh.elements.add_beam_element(eid, connect)
        elif e_type[eid - 1] in ["VERTEX"]:
            dpf_mesh.elements.add_point_element(eid, connect)
    
    print(dpf_mesh)
    
    dpf_mesh.plot()