GiD - The personal pre and post processor

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

This plugin use the features fo the meshio Python module to allow GiD import/export the mesh in all formats supported by this module.

The module is written in python, but GiD use Tcl as main scripting language. The Tohil package is the bridge to allow its use.

The plugin is placed at

<GiD>\plugins\Import\meshio

and like the rest of plugins is automatically loaded from Tcl when GiD starts.

It add to the Files Import/Export menus a new “meshio” item.

to import meshes the Python file gid_meshio.py define for example a function my_meshio_read_mesh

import numpy as np
import meshio

def my_meshio_read_mesh(filename):
    #to avoid that numpy truncate the printed representation of its arrays
    np.set_printoptions(threshold=np.inf)
    mesh=meshio.read(filename)
    return [mesh.points,mesh.cells,mesh.cells_dict]

and the Tcl file meshio.tcl invoke the import of this file in the Python interpreter to have defined the function, and then call the function to obtain the mesh data independently of the mesh file format read.

set filename_python [file join [gid_filesystem::get_folder_standard plugins] Import/meshio/gid_meshio.py]
GiD_Python_Import_File $filename_python
...
set m [GiD_Python_Call gid_meshio.my_meshio_read_mesh $filename_mesh]
...

Here GiD_Python_Import_File is an auxiliary proc (see <GiD>\scripts\gid_python.tcl) that decorate the tohil syntax and basically do

package require tohil

tohil::import $module_name

And GiD_Python_Call is like an alias of the command tohil::call

then the variable m has the data that define a collection of meshes (element type, coordinates of nodes and element connectivities)

this is the code of the proc that create GiD meshes from a file in a format supported by the meshio Python module:

proc MeshIo::ReadPreUnstructuredMesh { filename } {
    set fail 0
    variable meshio_gid_element
    variable meshio_num_nodes_per_cell
    MeshIo::Import_gid_meshio_py ;#to load in python the file gid_meshio.py to define its python functions before be called
    set m [GiD_Python_Call gid_meshio.my_meshio_read_mesh $filename]
    set nodes_coordinates [lindex [MeshIo::PythonArrayToTclList [lindex $m 0]] 0]
    #e.g.[lindex $m 1]  ==  {<meshio CellBlock, type: triangle, num cells: 156, tags: []>}
    set meshio_element_types_and_connectivities [MeshIo::PythonArrayToTclList [lindex $m 2]]
    set layer [GiD_Layers get to_use]
    set offset_nodes [GiD_Info mesh MaxNumNodes]
    set offset_elements [GiD_Info mesh MaxNumElements]
    set last_element_id $offset_elements
    
    #better use GiD_MeshPre_Create with same syintax as GiD_MeshPost (and some day could be implemented to be faster in C++)
    set num_nodes [llength $nodes_coordinates]
    set node_ids [objarray new_from_to intarray [expr $offset_nodes+1] [expr $offset_nodes+$num_nodes]]
    set vertices [objarray new doublearray [expr $num_nodes*3]]
    set i 0
    foreach node $nodes_coordinates {
        foreach value $node {
            objarray set $vertices $i $value
            incr i
        }
    }
    foreach {meshio_element_type meshio_connectivities} $meshio_element_types_and_connectivities {
        set element_type ""
        set element_num_nodes 0
        if { [info exists meshio_gid_element($meshio_element_type)] } {
            set element_type $meshio_gid_element($meshio_element_type)
            set element_num_nodes $meshio_num_nodes_per_cell($meshio_element_type)
        } else {
            W "element $meshio_element_type not supported"
            continue
        }
        set elements [lindex $meshio_connectivities 0]
        set num_elements [llength $elements]
        set element_ids [objarray new_from_to intarray [expr $last_element_id+1] [expr $last_element_id+$num_elements]]
        set element_vertex_indices [objarray new intarray [expr $num_elements*$element_num_nodes]]
        set i 0
        foreach element $elements {
            foreach node_id $element {
                objarray set $element_vertex_indices $i [expr $node_id+$offset_nodes+1]
                incr i
            }
        }
        set zero_based_array 0
        GiD_MeshPre_Create $element_type $element_num_nodes $node_ids $vertices $element_ids $element_vertex_indices $zero_based_array $layer
        incr last_element_id $num_elements
    }
    return $fail
}

  • No labels