FEATool  v1.6
Finite Element Analysis Toolbox
 All Files Functions Pages
Grid

After a model geometry has been defined, a computational grid or mesh must be generated to allow for the finite element discretization. This section describes how to create or import a suitable grid.

Grid Mode

Grid mode can be selected by pressing the mode button or corresponding menu option. In grid mode the toolbar buttons allow for grid generation, refinement, selecting and deleting grid cells, and setting subdomain and boundary numberings.

grid_main_50.png
  • calls the default grid generation function to generate and unstructured grid of triangles in 2D and tetrahedra in 3D. The desired overall mean Grid Size is specified in the corresponding edit field.
  • The button is used to uniformly refine a grid. This will split triangular and quadrilateral (2D) grid cells into four subcells, and tetrahedral and hexahedral (3D) ones into eight subcells each. Newly created grid points on boundaries will also be aligned with any geometries in 2D.
  • Grid cells can be selected pressing the button and entering a logical expression in the corresponding dialog box.
grid_selcells_50.png
  • After a selection has been made the selected cells can be deleted by pressing the button.
  • Similarly the subdomain number of the selected cells can be changed by pressing the button. The initial subdomain number shown in the Set Subdomain dialog box corresponds to the current number of subdomains + 1, assuming that a completely new subdomain is to be created from the selected cells.
grid_setsubd_50.png
  • The button allows changing and modifying assignment of boundary numbering. The Edit Boundaries dialog box allows one to call the automatic boundary calculation routine which tries to split boundary cells according to the angle between boundary edges and faces. Alternatively, one can manually select boundary cells, edges, and faces by entering a logical expression such as x>0&y>0 and ib==2 which selects the boundaries in the top positive quadrant and with current boundary group equal to 2, respectively.
grid_setbdr_50.png


The Grid menu options allows for accessing and specifying the Grid Generation Call... which is a Matlab/Octave function call to support external grid generation tools instead of the default one. Generate Quadrilateral Grid calls the structured quadrilateral grid generation routine (see section quadrilateral grid generation section below). Convert Grid Cells is used to convert between triangular and quadrilateral cells in 2D, and between tetrahedral and hexahedral cells in 3D. The Grid Smoothing menu option performs smoothing steps with either Laplacian or umbrella smoothing and allows for a relaxation input parameter.

The Grid menu also supports the following import and export of grids through external ascii format files


Note that in one dimension (1D) the only available option is to create a line grid.


Reference Material

This sectiond describes the format of the grid data structure that FEATool employs as well as advanced command line (CLI) usage such as manually creating and importing grids.

Grid Format

The grid format used by FEATool is specified in the grid struct with the following fields

Field Description Size
p Grid point coordinates (n_sdim, n_points)
c Grid cell connectivity (n_edges_per_cell, n_cells)
a Grid cell adjacency (n_edges_per_cell, n_cells)
b Boundary information (3+n_sdim, n_boundary_points)
s Grid cell subdomain list (1, n_cells)

The coordinates of the grid points are specified by an array p, with the row number indicating the i-th coordinate direction, and column number the corresponding grid point number.

Cell connectivities are given in the c array, which specify which grid points make up each cell. Here the row index gives the local vertex number and the column index the cell number. Moreover, the grid points are numbered counterclockwise in each cell.

Adjacency, meaning pointers to neighboring cells, are given in the a array. Similar to c the row index gives the local edge number (starting with the corresponding grid point in c) and the column index points to the cell number. If the edge is on a boundary the corresponding value in a is 0.

Boundary information is specified in the b array. The cell, edge/face, and boundary numbers are given in the first to third rows. The last n_sdim rows give the outward pointing normals corresponding to a boundary edge (or face in 3D). (Note that in higher dimensions each boundary point is specified as many times as boundary edges intersect a point.)

A list of subdomain numbers for each cell is given in the s vector.


Grid Example 1

The following code can be used to define a one dimensional grid with 10 uniformly spaced cells on the line (0..1)

grid.p = 0:0.1:1;
grid.c = [1:10;2:11];
grid.a = [0:9;2:10 0];
grid.b = [1 10;1 2;1 2;-1 1];
grid.s = ones(1,10);

Alternatively one can use the grid utility function linegrid

grid = linegrid( 10, 0, 1 );

The plotgrid function can be used to plot and visualize a grid

plotgrid( grid )
grid_ex1_plotgrid_50.svg


Grid Example 2

A 2 by 2 rectangular grid on the unit square can be created with the following commands

grid.p = [repmat([0,0.5,1],1,3);0 0 0 0.5 0.5 0.5 1 1 1];
grid.c = [1 2 5 4;2 3 6 5;4 5 8 7;5 6 9 8]';
grid.a = [0 2 3 0;0 0 4 1;1 4 0 0;2 0 0 3];
grid.b = [1 2 2 4 4 3 3 1;1 1 2 2 3 3 4 4;1 1 2 2 3 3 4 4; ...
          0 0 1 1 0 0 -1 -1;-1 -1 0 0 1 1 0 0];
grid.s = ones(1,4);

The rectgrid function can also be used to create rectangular grids, in this case

grid = rectgrid( 2, 2, [0 1;0 1] );

As before the grid can be plotted, visualizing both grid point and cell numbers, with

plotgrid( grid, 'nodelabels', 'on', 'cellabels', 'on' )
grid_ex2_plotgrid_50.svg

Similarly, the boundaries can be visualized with the function plotbdr

plotbdr( grid )
grid_ex2_plotbdr_50.svg

As FEATool also supports simplex triangular cells in two dimensions a grid consisting of quadrilaterals can easily be converted with the utility function quad2tri

grid = quad2tri( grid )
grid_ex2_quad2tri_50.svg


Grid Example 3

A more complex example, a grid for a rectangle with a circular hole can be created by first creating geometry objects, applying a formula to construct the geometry shape, and then calling the automatic unstructured grid generation function gridgen

% Geometry definition.
xmin  = 0;
xmax  = 1;
ymin  = 0;
ymax  = 1;
tag1  = 'R1';
gobj1 =  gobj_rectangle( xmin, xmax, ymin, ymax, tag1 );

xc    = (xmax-xmin)/2;
yc    = (ymax-ymin)/2;
r     = 0.25;
tag2  = 'C1';
gobj2 =  gobj_circle( [xc yc], r, tag2 );

geom.objects = { gobj1 gobj2 };
formula = 'R1 - C1';
geom = geom_apply_formula( geom, formula );
fea.geom = geom;

% Grid generation.
hmax = 0.1;
fea.grid = gridgen( fea, 'hmax', hmax );

As before the grid can be plotted, visualizing both grid point and cell numbers, with

plotgrid( fea )
grid_ex3_plotgrid_50.svg


Grid Import and Export

FEATool allows importing and exporting finite element grids between FeatFlow, GiD, Gmsh, General Mesh Viewer (GMV), and Triangle formats with the corresponding impexp_feat2d, impexp_feat3d, impexp_gid, impexp_gmsh, impexp_gmv, and impexp_triangle functions. These functions can also be accessed from the Grid and Postprocessing menus of the FEATool GUI.

Due to the easy grid format it is possible to manually import grids from other software. The process essentially consists of first exporting the grid point coordinates, and grid cell connectivity data into separate text files. Then import them into Octave or Matlab, after which they can be reshaped and used by FEATool. The following steps describe the process

  1. The first step is to output your grid to an ASCII text format. If possible save the grid output to two files, one for the grid vertex coordinates, and another with the grid cell connectivities (this specifies which grid points/vertices make up each cell). If this is not possible you will manually have to open your grid output file in a text editor and cut and save the grid coordinates and cell connectivities to two different files.

  2. Load the two files in Octave/Matlab (here it is assumed they are saved as p.txt and c.txt):

     load p.txt
     load c.txt
    


  3. Reshape the grid coordinates (p variable) so that it has the form $n_{sdim}\times n_p$ where $n_{sdim}$ is the number of space dimensions (1, 2 (or 3)) and $n_p$ is the total number of grid points (p essentially consists of rows of x, y, (and z-coordinates)). Precisely how to reshape depends on the output format from your grid generator, you might not have to do anything (check the shape by giving the command size(p) or whos), it might be enough to transpose p = p';, or you might have to really reshape with something like p = reshape(p,n_sdim,n_p);.

  4. Similarly, reshape the cell connectivity array c so that it has the shape $n_v\times n_c$ where $n_v$ is the number of vertices on each cell (for example 3 for triangles) and $n_c$ is the total number of cells. Each column should point to the corresponding grid points in p that make up the cell.

  5. The grid vertices must run in counterclockwise order on each cell, so reorient them if necessary. This is usually accomplished by changing the row order, for example c = c([3 2 1],:); which the ordering for triangles.

  6. Use the function gridadj to create an array that points to neighboring cells for each cell edge

     n_sdim = size(p,1);
     a = gridadj(c,n_sdim);
    


  7. Create a vector that assigns a subdomain number for each cell. If the geometry should be one single block and thus not split, a unit vector is sufficient

     n_c = size(c,2);
     s = ones(1,n_c);
    


  8. Use the function gridbdr to create boundary edge information

     b = gridbdr(p,c,a);
    


  9. Create a grid struct to hold all the grid information

     grid.p = p;
     grid.c = c;
     grid.a = a;
     grid.s = s;
     grid.b = b;
    


  10. Now the grid can be used by FEATool subroutines on the command line.

  11. Optionally, you can also import the grid into the FEATool GUI by using the Menu option

     File > Import > Variables From Main Workspace...
    


    Select the created grid variable to import and press Import. This loads it into the local FEATool memory workspace and can now be accessed from the FEATool Terminal (the bottom command **>>** edit field in the main GUI window). By entering the command

     fea.grid = grid;
    


    in the FEATool Terminal command line (not the main Octave/Matlab command window) effectively replaces the current grid with the imported one. Press the Grid mode button to update and show the new grid.


Creating Structured Grids

The computational finite element library in FEATool supports FEM shape functions for structured grids (quadrilaterals in 2D and hexahedra in 3D). Although more difficult to generate automatically, structured grids are often computationally superior, allowing for higher accuracy with a smaller number of cells.

In FEATool, structured tensor-product grids of basic geometric shapes can easily be generated on the command line with the following Octave/Matlab m-script functions

Function Description
linegrid Create a 1D line grid
circgrid Create a 2D structured circular grid of quadrilateral cells
holegrid Create a 2D rectangular grid with a circular hole
rectgrid Create a 2D rectangular grid of quadrilateral cells
ringgrid Create a 2D grid of a ring shaped domain
blockgrid Create a 3D structured block grid
cylgrid Create a 3D structured cylindrical grid
spheregrid Create a 3D grid for a spherical domain
featool_structured_grid_primitives_50.png

Moreover, the grid utility functions delcells, selcells, gridextrude, gridmerge, gridrevolve, gridrotate, and gridscale can be used to manually modify, transform, and join grids to more complex structures. FEATool - Grid transformation and utility functions

featool_grid_transformation_functions_50.png

The figure below shows three examples of more complex grids created by using these functions.

featool_create_complex_structured_grids_50.png

Create complex structured grids by combining and using the grid utilities together.

The first cylinder benchmark grid is for example created with the following commands:

 grid1 = ringgrid( [0.05 0.06 0.08 0.11 0.15], ...
                   32, [], [], [0.2;0.2] );
 grid2 = holegrid( 8, 1, [0 0.41;0 0.41], 0.15, [0.2;0.2] );
 grid2 = gridmerge( grid1, 5:8, grid2, 1:4 );
 grid3 = rectgrid( [0.41 0.5 0.7 1 1.4 1.8 2.2], ...
                   8, [0.41 2.2;0 0.41] );
 grid  = gridmerge( grid3, 4, grid2, 6 );

And the lower right revolved grid with these commands:

 grid = rectgrid();
 grid = gridscale( grid, {'1-(y>0.5).*(y-0.5)' 1} );
 grid = delcells( selcells( ...
          '((x<=0.8).*(x>=0.2)).*(y<=0.2)', grid ), grid );
 grid = gridrevolve( grid, 20, 0, 1/4, 2, pi/2, 0 );

The last example with two brackets attached to an I-beam section is more complex:

 grid01 = ringgrid( 1, 20, 0.03, 0.06, [0;0] );
 indc01 = selcells( grid01, 'y<=sqrt(eps)' );
 grid01 = delcells( grid01, indc01 );

 grid02 = holegrid( 5, 1, .06*[-1 1;-1 1], .03, [0;0] );
 indc02 = selcells( grid02, 'y>=-sqrt(eps)' );
 grid02 = delcells( grid02, indc02 );
 grid2d = gridmerge( grid01, [5 6], grid02, [7 8] );

 grid1 = gridextrude( grid2d, 1, 0.02 );
 grid1 = gridrotate( grid1, pi/2, 1 );
 grid2 = grid1;
 grid1.p(2,:) = grid1.p(2,:) + 0.03;
 grid2.p(2,:) = grid2.p(2,:) - 0.01;

 x_coord = [ -0.08 linspace(-0.06,0.06,6) 0.08 ];
 y_coord = [ -0.2 -0.15 -0.1 -0.05 -0.03 -0.01 ...
              0.01  0.03  0.05  0.1  0.15  0.2 ];
 grid3 = blockgrid( x_coord, y_coord, 1, ...
                    [-0.08 0.08;-0.2 0.2;-0.08 -0.06] );
 grid4 = blockgrid( 1, y_coord, 5, ...
                    [-0.01 0.01;-0.2 0.2;-0.18 -0.08] );
 grid5 = grid3;
 grid5.p(3,:) = grid5.p(3,:) - 0.12;

 grid = gridmerge( grid1, 9, grid3, 6 );
 grid = gridmerge( grid2, 9, grid, 17 );
 grid = gridmerge( grid4, 6, grid, 23, 1 );
 grid = gridmerge( grid5, 6, grid, 29, 2 );

After, the grids have been created on the command line they can also be imported into the FEATool GUI (by using the File > Import > Variables From Main Workspace option, after which the command fea.grid = grid; needs to be entered at the FEATool command prompt).


Quadrilateral Grid Generation

Using quadrilateral grid cells are often advantageous to simplex or triangular cells in that they can provide somewhat more accuracy when aligned with geometry features and also tends to require less overall grid cells. Quadrilaterals unfortunately do not tend to allow for easy automatic grid generation although it is possible to subdivide triangles into quads the resulting grids are often of poor quality.

The optional gridgen_quad function was originally designed to align quadrilateral cell edges with immersed interfaces described by distance and level set functions. The algorithm then uses the zero level set contour from the distance functions to align grid cell edges with external geometry object boundaries. Furthermore, the cells are split in a way to treat edge cases such as when and interface segment crosses a cell diagonal.

featool_gridgen_quad_collage_50.jpg

As well as using the Generate Quadrilateral Grid menu option in the FEATool Gui, gridgen_quad can be called on the Matlab command line just as one would call gridgen.


Grid Utility Functions

The following functions are available for working with and processing grids

Function Description
gridgen Unstructured automatic grid simplex generation
gridgen_quad Unstructured automatic grid quadrilateral generation
gridrefine Refine a grid uniformly
gridsmooth Apply smoothing to a grid
gridextrude Extrude a grid from 2D to 3D
gridrevolve Extrude and revolve a 2D grid to 3D
gridrotate Rotate a grid along a specified axis
gridscale Apply scaling to a grid
gridmerge Merge two grids along boundaries
quad2tri Convert a grid of quadrilateral cells to triangular cells
tri2quad Convert a grid of triangular cells to quadrilateral cells
hex2tet Convert a grid of hexahedral cells to tetrahedral cells
tet2hex Convert a grid of tetrahedral cells to hexahedral cells
impexp_feat2d Import and export 2D Feat(Flow) grid data
impexp_feat3d Import and export 3D Feat(Flow) grid data
impexp_gid Import and export GiD grid data
impexp_gmv Import and export GMV grid and postprocessing data
impexp_gmsh Import Gmsh grid and postprocessing data
impexp_triangle Import and export 2D Triangle grid and polygon data
gridcheck Check grid for errors
gridadj Create grid adjacency information
gridbdr Create grid boundary information
gridvert Create grid vertex information
gridedge Create grid edge information
gridface Create grid face information
selcells Find cell indices from an expression
delcells Delete a selection of cells from a grid
plotbdr Plot and visualize boundaries
plotsubd Plot and visualize subdomains
plotgrid Plot and visualize grid