fe_gmsh

fe_gmsh#

Purpose

GMSH interface. You can download GMSH at http://www.geuz.org/gmsh/ and tell where to find GMSH using

Syntax

setpref('OpenFEM','gmsh','/path_to_binary/gmsh.exe') % Config
model=fe_gmsh(command,model,...)
model=fe_gmsh('write -run','FileName.stl')

Description

The main operation is the automatic meshing of surfaces. To create a simple mesh from a CAD file (.stp, .igs,...), a dedicated Tab has been built to do it using GUI.

Tab#

Open the GMSH tab, shown in figure 10.3 with the command

cf=feplot(5);
fecom(cf,'initGMSH');

Figure 10.3: GMSH Tab and mesh result

Please find below the description of each parameter

  • FileName : select the CAD file to mesh from GUI by clicking on the icon or directly paste the file path in the field
  • MeshDim : specify if this is a 1D (curves), 2D (surfaces) or 3D (volumes) mesh
  • MeshOrder : linear or quadratic (with middle nodes) meshes
  • clmim : minimum characteristic length
  • clmax : maximum characteristic length
  • clscale : scale applied to clmin and clmax (useful to do mm -> m)
  • optimize : two mesh optimization algorithms to improve elements quality (see GMSH documentation for details)
    • netgen
    • highorder
  • Mesh : compute the mesh using selected parameters
    • PostCb : if empty, the mesh is displayed in feplot, else it is forwarded to the callback.

When clicking on Mesh, the equivalent command line call is displayed in the console if you need to redo this from script.

Example#

This example illustrates the automatic meshing of a plate

 FEnode = [1 0 0 0  0 0 0; 2 0 0 0  1 0 0; 3 0 0 0  0 2 0];
 femesh('objectholeinplate 1 2 3 .5 .5 3 4 4');
 model=femesh('model0');
 model.Elt=feutil('selelt seledge ',model);
 model.Node=feutil('getnode groupall',model);
 model=fe_gmsh('addline',model,'groupall');
 model.Node(:,4)=0; % reset default length
 mo1=fe_gmsh('write del.geo -lc .3 -run -2 -v 0',model);
 feplot(mo1)

This other example makes a circular hole in a plate

% Hole in plate :
model=feutil('Objectquad 1 1',[0 0 0; 1 0 0;1 1 0;0 1 0],1,1); %
model=fe_gmsh('addline -loop1',model,[1 2; 2 4]);
model=fe_gmsh('addline -loop1',model,[4 3; 3 1]);
model=fe_gmsh('AddFullCircle -loop2',model,[.5 .5 0; .4 .5 0; 0 0 1]);

model.Stack{end}.PlaneSurface=[1 2]; mo1=fe_gmsh('write del.geo -lc .02 -run -2 -v 0',model) feplot(mo1)

To allow automated running of GMSH from MATLAB, this function uses a info,GMSH stack entry with the following fields


.Lineone line per row referencing NodeId. Can be defined using addline commands.
.Circledefine properties of circles.
.LineLooprows define a closed line as combination of elementary lines. Values are row indices in the .Line field. One can also define LineLoop from circle arcs (or mixed arcs and lines) using a cell array whose each row describes a lineloop as {'LineType',LineInd,...} where LineType can be Circle or Line and LineInd row indices in corresponding .Line or .Circle field.
.TransfiniteLinesDefines lines which seeding is controlled.
.PlaneSurfacerows define surfaces as a combination of line loops, values are row indices in the .LineLoop field. Negative values are used to reverse the line orientation. 1st column describes the exterior contour, and followings the interiors to be removed. As .PlaneSurface is a matrix, extra columns can be filled by zeros.
.EmbeddedLinesdefine line indices which do not define mesh contours but add additional constrains to the final mesh (see Line In Surface in the gmsh documentation.
.SurfaceLooprows define a closed surface as combination of elementary surfaces. Values are row indices in the .PlaneSurface field.

The local mesh size is defined at nodes by GMSH. This is stored in column 4 of the model.Node. Command option -lcval in the command resets the value val for all nodes that do not have a prior value.

Add...#

Typical calls are of the form [mdl,RO]=fe_gmsh('Add Cmd',mdl,data). The optional second output argument can be used to obtain additional information like the LoopInfo. Accepted command options are

  • -loop i is used to add the given geometries and append the associated indices into the LineLoop(i).
  • FullCircle defines a circle defined using data with rows giving center coordinates, an edge node coordinates and the normal in the last row. 4 arcs of circle are added. In the LineLoop field the entry has the form {'Circle',[ind1 ind2 ind3 ind4]} where indi are the row indices of the 4 arcs of circle created in .Circle field.
  • CircleArc defines a circle arc using data
    • 3x3 matrix, with 1rst row giving center coordinates, second and third rows are respectively the first and second edges defined by node coordinates.
    • 3x1 vector, giving the 3 NodeId (center, 1st and 2nd edge) as a column instead of x y z.
    • with a -tangent1 option, 3x3 matrix whose 1st row defines a tangent vector of the circle arc at the 1st edge node (resp. at the second edge node with the option -tangent2). 2nd row defines the 1st edge node coordinates and third row the 2nd edge node coordinate.
  • Disk ...
  • Line accepts multiple formats. data can be a 2 column matrix which each row defines a couple of points from their NodeId.

    data can also be a 2 by 3 matrix defining the coordinates of the 2 extremities.

    data can also be a string defining a line selection.

    • It is possible to specify a seeding on the line for further meshing operation using additional arguments seed and the number of nodes to seed on the line. E.g.: mdl=fe_gmsh('AddLine',mdl,data,'seed',5); will ask gmsh to place 5 nodes on each line declared in data.
    • It is possible to define line constrains in mesh interiors using embedded lines (depending on the gmsh version). mdl=fe_gmsh('AddLine',mdl,data,'embed',1); will thus declare the edges found in data not as line loops defining surfaces, but as interior mesh constrains. This feature is only supported for lines specified as selections.
  • AddLine3 can be used to declare splines instead of lines in the geometry. For this command to work, beam3 elements must be used, so that a middle node exists to be declared as the spline control point. For this command, data can only be an element string selection.

config#

The fe_gmsh function uses the OpenFEM preference to launch the GMSH mesher.

 setpref('OpenFEM','gmsh','$HOME_GMSH/gmsh.exe')

Ver#

Command Ver returns the version of gmsh, the version is transformed into a double to simplify hierarchy handling (e.g. version 2.5.1 is transformed into 251). This command also provides a good test to check your gmsh setup as the output will be empty if gmsh could not be found.

Read#

fe_gmsh('read FileName.msh') reads a mesh from the GMSH output format. Starting with GMSH 4 .msh is an hybrid between mesh and CAD so that direct reading is not possible. You should then use an extention .ext field to force GMSH to export to a format that SDT supports ()

Write#

fe_gmsh('write FileName.geo',model); writes a model (.Node, .Elt) and geometry data in model.Stack'info','GMSH' into a .geo file which root name is specified as FileName (if you use del.geo the file is deleted on exit).

  • Command option -lc allows specifying a characteristic length. You can also define a nodewise characteristic length by setting non zero values in model.Node(:,4).
  • Command option -multiple can be used for automated meshing of several closed contours. The default behavior will define a single Plane Surface combining all contours, while -multiple variant will declare each contour as a single Plane Surface.
  • Command option -keepContour can be used to force gmsh not to add nodes in declared line objects (Transfinite Line feature).
  • Command option -spline can be used (when lines have been declared using command AddLine3 from beam3 elements) to write spline objects instead of line objects in the .geo file
  • .stl writing format is also supported, by using extension .stl instead of .geo in the command line.
  • Command option -run allows to run gmsh on the written file for meshing. All characters in the command after -run will be passed to the gmsh batch call performed. fe_gmsh then outputs the model processed by gmsh, which is usually written in .msh file format.

    All text after the -run is propagated to GMSH, see sample options below.
    It also possible to add a different output file name NewFile.msh, using model=fe_gmsh('write NewFile.msh -run','FileName.stl').

  • Conversion of files through fe_gmsh into .msh, or SDT/OpenFEM format is possible, for all input files readable by gmsh. Use command option -run and specify in second argument the file name.
    For example: model=fe_gmsh('write -run','FileName.stl') convert .stl to .mesh then open into SDT/OpenFem. Some warning can occur if no FileName.mesh is given, but without effect on the result.

Known options for the run are

  • -1 or -2 or -3) specifies the meshing dimension.
  • -order 2 uses quadratic elements.
  • -v 0 makes a silent run.
  • -clmax float sets maximum mesh size, -clmin float for minimum.

From a geometry file the simplest meshing call is illustrated below

 filename=demosdt('download-back http://www.sdtools.com/contrib/component8.step')
 RO=struct( ... % Predefine materials
   'pl',m_elastic('dbval -unit TM 1 steel'), ...
   'ext','.msh', ... % Select output format by extension (use .m MATLAB for GMSH>4)
   'sel','selelt eltname tetra10', ... % Elements to retain at end
   'Run','-3 -order 2 -clmax 3 -clmin 2 -v 0');  %RunCommand
  model=fe_gmsh('write',filename,RO);

It is also possible to write GMSH post-processing command lines, written at the end of the file (see the GMSH documentation) by providing a cell array (one cell by command line) in the field .Post of the RO structure.

See also

missread