This package aims to offer some subroutines to export data to the gmsh post processing software (https://gmsh.info/). Import of gmsh meshes (pre-processing) is performed by using the Gmsh library.
This package is not registerd and shoud be installed by using
]add https://github.com/CodeLenz/Lgmshor
using Pkg
Pkg.add("url=https://github.com/CodeLenz/Lgmsh")All post-processing must start with the creation of a file with
Lgmsh_export_init(filename::String,nn::T,ne::T,coord::Array{F},etype::Vector{T},connect::Array{T}) where {T,F}where
Example
using Lgmsh
#
# Create a model with a square and a triangle
#
# Number of nodes
nn = 5
# Nodal coordinates
coord = [0.0 0.0 ;
1.0 0.0 ;
1.0 1.0 ;
0.0 1.0 ;
0.5 2.0 ]
# Element types
# 2 is triange and 3 is a rectangle
etype = [3;2]
# Connectivities.
ne = 2
# Observe that the rectangle has 4 nodes and the triangle has 3. Thus,
# we must insert a zero in the last row for this element.
connect = [1 2 3 4 ;
4 3 5 0]
# Filename
filename = "example.pos"
# Create the file
Lgmsh_export_init(filename,nn,ne,coord,etype,connect)After creating the initial header, it is possible to export scalar and vector views to this file. Tensor views will be added in the near future.
Currently, there are two main subroutines to export scalar views
Export element (centroidal) scalar values to filename
Lgmsh_export_element_scalar(filename::String,element::Vector,viewname::String,time=0.0) where
# Create a random centroidal scalar field
element = rand(ne)
# Append the view to the existing file
Lgmsh_export_element_scalar(filename,element,"Random centroidal Scalar") and
Export nodal scalar values to filename
Lgmsh_export_nodal_scalar(filename::String,nodal::Vector,viewname::String,time=0.0) where
# Create a random nodal scalar field
nodal = rand(nn)
# Append the view to the existing file
Lgmsh_export_nodal_scalar(filename,nodal,"Random nodal Scalar") Vector fiedls are created with
Lgmsh_export_nodal_vector(filename::String,vector::Vector,dim::Int,viewname::String,time=0.0)where
Example
# 2D
# Dimension
dim = 2
# Two dimensional vector for each node
vector = rand(dim*nn)
# Create view
Lgmsh_export_nodal_vector(filename,vector,dim,"Nodal vector 2D")
# 3D
# Dimension
dim = 3
# Three dimensional vector for each node
vector = rand(dim*nn)
# Create view
Lgmsh_export_nodal_vector(filename,vector,dim,"Nodal vector 3D") The same logic applies to export vector views to element data, assuming that there is one vector per element.
Lgmsh_export_element_vector(filename::String,vector::Vector,dim::Int,viewname::String,time=0.0)where
Example
# 2D
# Dimension
dim = 2
# Two dimensional vector for each element
vector = rand(dim*ne)
# Create view
Lgmsh_export_element_vector(filename,vector,dim,"Element vector 2D")
# 3D
# Dimension
dim = 3
# Three dimensional vector for each node
vector = rand(dim*ne)
# Create view
Lgmsh_export_element_vector(filename,vector,dim,"Element vector 3D") Pre-processing Gmsh .msh files is not very easy, since there are a lot of additional information on the .msh file. For example, if one creates a simple domain comprised of four points, four lines, one planar surface and a 2D mesh, there will be, at least, three types of finite elements; lines, points and the 2D elements (triangles and quads, for example). To make things worst, the imposition of boundary conditions is not direct and we must define Physical Groups. Thus, there are some soubroutines to parse basic information from such files. Those informations must be further processed to some specific format.
To recover nodes and nodal coordinates
nn, norder, coord = Lgmsh_import_coordinates(filename)where
To recover element types
etypes = Lgmsh_import_etypes(filename)where
To recover elements of a given (valid) type
ne, number, connect = Lgmsh_import_element_by_type(filename,type)where
To list Physical Groups and their names
pgroups, pgnames = Lgmsh_import_physical_groups(filename)where
$pggroups" is a vector of tuples (dim,tag)
$pgnames" is a vector of strings
To recover all the entities belonging to a given Physical Group name
entities = Lgmsh_import_entities_physical_group(filename,name)Example
Process the file testmesh1.msh in the test directory
# Load the package
using Lgmsh
# Path to the mesh file
filename = joinpath(pathof(Lgmsh)[1:end-12],"geo/cantilever.msh")
# Obtain nodes and coordinates
# This model has 10 nodes
nn, norder, coord = Lgmsh_import_coordinates(filename)
# Obtain the list of element types
etypes = Lgmsh_import_etypes(filename)
# Obtain the information about the triangular elements (type 2)
ne, number, connect = Lgmsh_import_element_by_type(filename,2)
# Obtain the Physical Groups and names
pgroups, pgnames = Lgmsh_import_physical_groups(filename)
# Obtain the entities with for the second group
entities = Lgmsh_import_entities_physical_group(filename,pgnames[2])
Some higer level auxiliary functions for processing .msh files are provided
# Load the package
using Lgmsh
# Path to the mesh file
# This file corresponds to a 2D region with a circular hole. Some
# boundary conditions are imposed at nodes, lines and regions, by
# means of Physical Groups.
filename = joinpath(pathof(Lgmsh)[1:end-12],"geo/plate_hole.msh")
# Read the file, elements of type 2 (tri) and 3 (quads)
# nn is the number of nodes
# ne is the number of elements
# coord has the coordinates (x,y,z) for each node
# etypes has the types of each element
# connect has the connectivities for each element
# centroids has the centroids of each element
# etags is a dictionary with the original tags of each element
#
nn,coord,ne,etypes,connect,centroids,etags = Readmesh(filename,[3])
# Obtain the Physical Groups and names
pgroups, pgnames = Lgmsh_import_physical_groups(filename)
#
# There are four groups
#
# ["U,Y,0.0", "P,X,100.0", "U,X,0.0", "Domain"]
# Get all nodes for the group "U,X,0.0"
nodesx = Readnodesgroup(filename,"U,X,0.0")
# Get all nodes for the group "U,Y,0.0"
nodesy = Readnodesgroup(filename,"U,X,0.0")
# Get all elements inside Domain. We must pass
# etags to return the proper element numbers
elems_domain = Readelementsgroup(filename,"Domain",etags)
# The data must then be processed to some specific formatThere are some examples (.geo and associated .msh files) in the
geo. They can be accesses by using the joinpath command, as in the previous examples. The .geo files can be eddited with any text editor and the .msh can be generated by using
Those .geo files are adapted from the tutrials at https://gitlab.onelab.info/gmsh/gmsh/-/tree/master/tutorials.