Introduction to mesh generation (in Matlab)
By Allan P. Engsig-Karup
Overview
!! Introduction to mesh generation !! Introduction to DistMesh for Matlab !! Goal: Introduce you to DistMesh for use with DG-FEM based models.
2/18
Why do we need a mesh?
!! We need to represent the (usually finite) physical domain in some way discretely for numerical computations. !! In sub domain methods, e.g. Finite volume or FEM methods, it is possible to independently consider the problem solution procedure and mesh generation as two distinct problems. !! This is very convenient if we want to solve more than one problem governed by the same PDEs!
3/18
What defines a mesh?
!! Here we define a mesh as a discrete representation !h of some spatial domain or topology !. !! A mesh can be sub divided into K smaller non-overlapping sub domains K such that !k k . h !h = ! !h !! Mesh generation can be a demanding and non-trivial task. E.g. for complex geometries or objects. !! Unstructured triangular meshes have good support for representing complex domains (or geometries) and mesh adaption (coarsening/refinement).
k =1
F-15. From: www.USEMe.org
Example. Triangulation.
4/18
What defines a mesh?
!! A mesh can be completely defined in terms of (unique) vertices and a mesh element table (triangulation). !! For the purpose of specifying appropriate boundary conditions we may for convenience use a boundary type table. !! Simple meshes can be created manually by hand. However, automatic mesh generation is generally faster and more efficient, although may require some user input for handling complex meshes. !! Note: Mesh data can conveniently be stored for reuse several times.
5/18
Mesh generators available?
!! Lots of standard mesh generators available! These generators can be used to solve a given mesh generation problem. (but may require a translation script) !! An example of a free software distribution for generating unstructured and triangular meshes is DistMesh (Matlab).
6/18
Introduction to DistMesh for Matlab
!! Persson, P.-O. and Strang, G. 2004 A simple mesh generator in Matlab. SIAM Review. Download scripts at: http://www-math.mit.edu/~persson/mesh/index.html !! A simple algorithm that combines a physical principle of force equilibrium in a truss structure with a mathematical representation of the geometry using signed distance functions.
7/18
Introduction to DistMesh for Matlab
!! Algorithm (Conceptual);
Step 1. Define a domain using signed distance functions. Step 2. Distribute a set of nodes interior to the domain. Step 3. Move interior nodes to obtain force equilibirum. Step 4. Apply terminate criterion when all nodes are fixed in space.
!! Post-processing steps (Preparation);
Step 5. Validate output! Step 6. Reorder element vertices to be defined anti-clockwise for use with DG-FEM. Step 7. Setup boundary table. Step 8. Store mesh for reuse.
8/18
Introduction to DistMesh for Matlab
Signed distance function, d(x);
(interior) $< 0 , x ( % ! d ( x) = # 0 , x ( '% (boundary) !> 0 , x & % (exterior) "
Define metric using an appropriate norm. E.g. The usual Euclidian metric.
d=0 !" d<0
#!" d>0
9/18
Introduction to DistMesh for Matlab
!! Combine geometries defined by distance functions using the Union, difference and intersection operations (set theory);
Union: Difference: Intersection;
min(d1 ( x), d2 ( x))
max(d1 ( x ), !d2 ( x )) " max(!d1 ( x ), d2 ( x ))
max(d1 ( x), d2 ( x))
10/18
Introduction to DistMesh for Matlab
Example 1. Create a uniform mesh using DistMesh. Square with hole
Visualized distance function
Mesh
Using DistMesh (in Matlab) in only 3 lines of code:
>> fd=inline('ddiff(drectangle(p,-1,1,-1,1),dcircle(p,0,0,0.4))','p'); >> pfix = [-1,-1;-1,1;1,-1;1,1]; >> [p,t] = distmesh2d(fd,@huniform,0.125,[-1,-1;1,1],pfix);
11/18
Introduction to DistMesh for Matlab
DistMesh output; (two tables) p t Unique vertice coordinates Element to Vertice table (not reordered automatically by DistMesh)
From this we can determine, e.g. >> K=size(t,1); >> Nv=size(p,1); >> Nfaces=size(t,2); >> VX = p(:,1); >> VY = p(:,2); >> EToV = t; %Number of elements %Number of vertices in mesh %Number of faces/element %Vertice x-coordinates %Vertice y-coordinates %Element to Vertice table
12/18
Introduction to DistMesh for Matlab
DG-FEM convention for standard element definitions;
!! Vertices are numbered anti-clockwise. !! Faces are numbered anti-clockwise with the first face beeing the one that connects the first two vertices.
13/18
Introduction to DistMesh for Matlab
Example 2. Create a refined mesh using DistMesh.
Element size function
Size*h0
Square with hole
2.5
1.5
1 1 0.5 0 -0.5 y -1 -1 -0.5 x 0
1 0.5
Visualized element size function
Mesh
>> fd = inline('ddiff(drectangle(p,-1,1,-1,1),dcircle(p,0,0,0.4))','p'); >> pfix = [-1,-1;-1,1;1,-1;1,1]; >> fh = inline(['min( sqrt( p(:,1).^2 + p(:,2).^2 ) , 1 )'],'p'); >> [p,t] = distmesh2d(fd,fh,0.125/2.5,[-1,-1;1,1],pfix);
14/18
Introduction to DistMesh for Matlab
Element size function in DistMesh;
Element size function
2.5
Size*h0
1.5
1 1 0.5 0 -0.5 y -1 -1 -0.5 x 0
1 0.5
From former example;
Visualized element size function
Mesh
>> fh = inline(['min( sqrt( p(:,1).^2 + p(:,2).^2 ) , 1 )'],'p');
>> [p,t] = distmesh2d(fd,fh,h0,[-1,-1;1,1],pfix);
!! Function fh Defines relative sizes of elements in final mesh. (fh=constant result in uniform distribution) !! The initial characteristic size of the elements is h0. !! In final distribution, the characteristic size of the smallest elements in the mesh will be approx. h0;
15/18
Introduction to DistMesh for Matlab
Example 3. Selecting boundary nodes. Square with hole
Nodes can be selected using distance functions; |d| = 0 or |d| < tol
Inner boundary nodes
Outer boundary nodes
>> fdInner = inline(dcircle(p,0,0,0.4),p); >> nodesInner = find(abs(fdInner([p]))<1e-3); >> fdOuter = inline(drectangle(p,-1,1,-1,1),p); >> nodesOuter = find(abs(fdOuter([p]))<1e-3); >> nodesB = find(abs(fd([p]))<1e-3);
16/18
Introduction to DistMesh for Matlab
Example 4. Updating boundary table. Square with hole
>> BCcode = 99; >> BCType = zeros(size(EToV')); % empty BCType table >> BCType = CorrectBCTable(K,EToV,BCType,nodesB,BCcode); The BCType boundary table can be used to create different maps (see the script BuildBCMaps2D.m, Section 6.4 in the textbook) for imposing different types of boundary conditions.
17/18
Final remarks
These notes together with example scripts for DistMesh can be found at my webpage: http://www.imm.dtu.dk/~apek/
18/18