# ameshref

## Efficient Matlab Implementation of Adaptive Mesh Refinement in 2D

This project provides an efficient implementation of various adaptive mesh refinement strategies in two dimensions. The documentation provides a detailed investigation of the implemented mesh refinement methods and also presents how the methods are realized by utilization of reasonable data structure, use of Matlab built-in functions and vectorization. In the documentation, the focus is set on the deployment of ameshref in the context of the adaptive finite element method. To this end, also codes to realize the adaptive finite element method (AFEM) is additionally provided here. However, our code is kept general such that this package is not restricted to the AFEM context as our other test examples show. This package can be used to adaptively generate meshes in two dimensions in different ways. Moreover, it can also serve educational purposes on how to realize mesh refinement methods in an accessible, short but efficient way. The data structure used is very simple because only the coordinates of the vertices and the element-connectivities are needed. Marking of elements are interpreted as edge-based marking, i.e., an element is marked by marking each edge for bisection. In contrast to other mesh refinement packages, we do not make use of a recursive approach but prefer the realization in terms of vectorization.

## Getting Started

For a use of the refinement methods download the complete repository /ameshref with the test examples and run them on your computer with Matlab. To use it within your own examples you only need the repository /refinement. Please see the following instructions for a correct use.

MATLAB

## Running the test examples

/example1: run test_refinement.m (refinement along a circle)

/example2: run mainAdaptiveScript.m (triangulation of a picture)

/example3: run example1.m or example2.m in each of the repositories (mesh refinement in the context of AFEM)

/example4: run example4.m (for a given discrete set of points all elements containing these points are refined)

### Deployment for your own examples – Data structure

To be able to deploy this package within your framework you need the following data strucure of the mesh:

For triangles you need to define coordinates, elements3, and optionally boundary data dirichlet or neumann.

Similarly, for quadrilaterals you need to define coordinates, elements4, and optionally boundary data dirichlet or neumann.

For meshes with hanging nodes an additional data vector named irregular is needed, where irregular(l,1) and irregular(l,2) are the starting and end point of the lth-irregular edge with hanging node stored in irregular(l,3).

### How to call the functions

We abbreviate the data structure as coordinates (C), elements3 (E3), elements4 (E4), irregular (I), dirichlet (D), and neumann (N). Furthermore, marked elements are stored with the corresponding index in the variable marked.

The different mesh refinement methods are then called by (remember that D and N are optional arguments)

#### TrefineR

``````[C,E3,I,D,N] = TrefineR(C,E3,I,D,N,marked)
``````

#### QrefineR

``````[C,E4,I,D,N] = QrefineR(C,E4,I,D,N,marked)
``````

#### QrefineR2

``````[C,E4,I,D,N] = QrefineR2(C,E4,I,D,N,marked)
``````

#### TrefineNVB

``````[C,E3,D,N] = TrefineNVB(C,E3,D,N,marked)
``````

#### TrefineRGB

``````[C,E3,D,N] = TrefineRGB(C,E3,D,N,marked)
``````

#### TrefineRG

``````[C,E3,D,N] = TrefineRG(C,E3,D,N,marked)
``````

#### QrefineRB

``````[C,E4,D,N] = QrefineRB(C,E4,D,N,marked)
``````

#### QrefineRG

3-step call

``````[C,E4,marked,I] = recoarseedges_tri(C,E3,E4,marked3,marked4)
[C,E4,D,N] = QrefineR(C,E4,D,N,marked)
[C,E4,E3] = regularizeedges_tri(C,E4,I)
``````

#### QrefineRG2

3-step call

``````[C,E4,marked,I] = recoarseedges(C,E4,marked4)
[C,E4,I,D,N] = QrefineR2(C,E4,I,D,N,marked)
[C,E4] = regularizeedges_tri(C,E4,I)
``````

#### Minimal example

The used functions are provided in the repository /example1 and /refinement:

``````C = [0.8,0.7]; % center of circle
r = 0.5; % radius of circle
min_d = 5e-6; % minimal diameter of an element allowed

%% exemplary use of a quadrilateral mesh refinement strategy
coordinates = [0,0;1,0;1,1;0,1;2,0;2,1];
elements4 = [1,2,3,4;2,5,6,3];
while 1
mark4 = markCircle(coordinates,[],elements4,C,r,min_d);
mark4 = find(mark4);
[coordinates,elements4] = QrefineRB(coordinates,elements4,mark4);
if isempty(mark4)
break
end
end
``````

#### Plot your mesh with Matlab

triangular mesh:

``````patch('Faces',elements3,'Vertices',coordinates,'Facecolor','none')
``````

``````patch('Faces',elements4,'Vertices',coordinates,'Facecolor','none')
``````

## Authors

• Stefan A. FunkenAnja Schmidt Institute for Numerical Mathematics, Ulm University, Germany

If you use ameshref in scientific work, please cite:

• Stefan A. Funken, and Anja Schmidt. “Adaptive Mesh Refinement in 2D–An Efficient Implementation in Matlab.” Computational Methods in Applied Mathematics, 2018. https://doi.org/10.1515/cmam-2018-0220  