Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_mesh_2d_sparsity (d06cb)

## Purpose

nag_mesh_2d_sparsity (d06cb) generates the sparsity pattern of a finite element matrix associated with a given mesh.

## Syntax

[nnz, irow, icol, ifail] = d06cb(nv, nnzmax, conn, 'nelt', nelt)
[nnz, irow, icol, ifail] = nag_mesh_2d_sparsity(nv, nnzmax, conn, 'nelt', nelt)

## Description

nag_mesh_2d_sparsity (d06cb) generates the sparsity pattern of a finite element matrix associated with a given mesh. The sparsity pattern is returned in a coordinate storage format consistent with the sparse linear algebra functions in Chapter F11. More precisely nag_mesh_2d_sparsity (d06cb) returns the number of nonzero elements in the associated sparse matrix, and their row and column indices. This is designed to assist you in applying finite element discretization to meshes from the D06 Chapter Introduction and in solving the resulting sparse linear system using functions from Chapter F11.
The output sparsity pattern is based on the fact that finite element matrix A$A$ has elements aij${a}_{ij}$ satisfying:
 aij ≠ 0 ⇒ i​ and ​j ​ are vertices belonging to the same triangle. $aij≠0 ⇒i​ and ​j ​ are vertices belonging to the same triangle.$

None.

## Parameters

### Compulsory Input Parameters

1:     nv – int64int32nag_int scalar
The total number of vertices in the input mesh.
Constraint: nv3${\mathbf{nv}}\ge 3$.
2:     nnzmax – int64int32nag_int scalar
The maximum number of nonzero entries in the matrix based on the input mesh. It is the dimension of the arrays irow and icol as declared in the function from which nag_mesh_2d_sparsity (d06cb) is called.
Constraint: 4 × nelt + nvnnzmaxnv2$4×{\mathbf{nelt}}+{\mathbf{nv}}\le {\mathbf{nnzmax}}\le {{\mathbf{nv}}}^{2}$.
3:     conn(3$3$,nelt) – int64int32nag_int array
The connectivity of the mesh between triangles and vertices. For each triangle j$\mathit{j}$, conn(i,j)${\mathbf{conn}}\left(\mathit{i},\mathit{j}\right)$ gives the indices of its three vertices (in anticlockwise order), for i = 1,2,3$\mathit{i}=1,2,3$ and j = 1,2,,nelt$\mathit{j}=1,2,\dots ,{\mathbf{nelt}}$.
Constraint: 1conn(i,j)nv$1\le {\mathbf{conn}}\left(\mathit{i},\mathit{j}\right)\le {\mathbf{nv}}$ and conn(1,j)conn(2,j)${\mathbf{conn}}\left(1,\mathit{j}\right)\ne {\mathbf{conn}}\left(2,\mathit{j}\right)$ and conn(1,j)conn(3,j)${\mathbf{conn}}\left(1,\mathit{j}\right)\ne {\mathbf{conn}}\left(3,\mathit{j}\right)$ and conn(2,j)conn(3,j)${\mathbf{conn}}\left(2,\mathit{j}\right)\ne {\mathbf{conn}}\left(3,\mathit{j}\right)$, for i = 1,2,3$\mathit{i}=1,2,3$ and j = 1,2,,nelt$\mathit{j}=1,2,\dots ,{\mathbf{nelt}}$.

### Optional Input Parameters

1:     nelt – int64int32nag_int scalar
Default: The dimension of the array conn.
The number of triangles in the input mesh.
Constraint: nelt2 × nv1${\mathbf{nelt}}\le 2×{\mathbf{nv}}-1$.

None.

### Output Parameters

1:     nnz – int64int32nag_int scalar
The number of nonzero entries in the matrix associated with the input mesh.
2:     irow(nnzmax) – int64int32nag_int array
3:     icol(nnzmax) – int64int32nag_int array
The first nnz elements contain the row and column indices of the nonzero elements supplied in the finite element matrix A$A$.
4:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
 On entry, nv < 3${\mathbf{nv}}<3$, or nelt > 2 × nv − 1${\mathbf{nelt}}>2×{\mathbf{nv}}-1$, or nnzmax < 4 × nelt + nv${\mathbf{nnzmax}}<4×{\mathbf{nelt}}+{\mathbf{nv}}$ or nnzmax > nv2${\mathbf{nnzmax}}>{{\mathbf{nv}}}^{2}$ or conn(i,j) < 1${\mathbf{conn}}\left(i,j\right)<1$ or conn(i,j) > nv${\mathbf{conn}}\left(i,j\right)>{\mathbf{nv}}$ for some i = 1,3$i=1,3$ and j$j$, 1 ≤ j ≤ nelt$1\le j\le {\mathbf{nelt}}$, or conn(1,j) = conn(2,j)${\mathbf{conn}}\left(1,j\right)={\mathbf{conn}}\left(2,j\right)$ or conn(1,j) = conn(3,j)${\mathbf{conn}}\left(1,j\right)={\mathbf{conn}}\left(3,j\right)$ or conn(2,j) = conn(3,j)${\mathbf{conn}}\left(2,j\right)={\mathbf{conn}}\left(3,j\right)$ for some j = 1,2, … ,nelt$j=1,2,\dots ,{\mathbf{nelt}}$.
ifail = 2${\mathbf{ifail}}=2$
A serious error has occurred in an internal call to an auxiliary function. Check the input mesh, especially the connectivity between triangles and vertices (the parameter conn). Array dimensions should be checked as well. If the problem persists, contact NAG.

Not applicable.

None.

## Example

See Section [Example] in (d06cc).
```function nag_mesh_2d_sparsity_example

edge = zeros(3, 100, 'int64');
coor = zeros(2, 250);

% Define boundaries
ncirc     = 3; % 3 circles
nvertices = [40, 30, 30];
centres   = [0, 0; -0.5, 0; -0.5, 0.65];

% First circle is outer circle
csign = 1;
i1 = 0;
nvb = 0;
for icirc = 1:ncirc
for i = 0:nvertices(icirc)-1
i1 = i1+1;
theta = 2*pi*i/nvertices(icirc);
coor(1,i1) = radii(icirc)*cos(theta) + centres(icirc, 1);
coor(2,i1) = csign*radii(icirc)*sin(theta) +  centres(icirc, 2);
edge(1,i1) = i1;
edge(2,i1) = i1 + 1;
edge(3,i1) = 1;
end
edge(2,i1) = nvb + 1;
nvb = nvb + nvertices(icirc);
% Subsequent circles are inner circles
csign = -1;
end
nedge = nvb;

% Initialise mesh control parameters
bspace = zeros(1, 100);
bspace(1:nvb) = 0.05;
smooth = true;
itrace = int64(0);

nnzmax = int64(3000);

% Mesh geometry
[nv, nelt, coor, conn, ifail] = nag_mesh_2d_gen_inc(edge, coor, bspace, smooth, itrace);

% Compute the sparsity of the FE matrix from the input geometry
[nz, irow, icol, ifail] = nag_mesh_2d_sparsity(nv, nnzmax, conn, 'nelt', nelt);

if (ifail == 0)
fprintf('\nNumber of non-zero entries in input mesh:  %d\n', nz);

% Plot sparsity of input mesh
fig1 = figure('Number', 'off');
plot(irow(1:double(nz)), icol(1:double(nz)), '.');
title ('Input Mesh', 'FontSize', 14);
end

% Call the renumbering routine and get the new sparsity
[nz, coor, edge, conn, irow, icol, ifail] = ...
nag_mesh_2d_renumber(nnzmax, coor, edge, conn, itrace, 'nelt', nelt);

if (ifail == 0)
fprintf('Number of non-zero entries in output mesh: %d\n', nz);
% Plot smoothed mesh
fig2 = figure('Number', 'off');
plot(irow(1:double(nz)), icol(1:double(nz)), '.');
title ('Output Mesh', 'FontSize', 14);
end
```
```

Number of non-zero entries in input mesh:  1556
Number of non-zero entries in output mesh: 1556

```
```function d06cb_example

edge = zeros(3, 100, 'int64');
coor = zeros(2, 250);

% Define boundaries
ncirc     = 3; % 3 circles
nvertices = [40, 30, 30];
centres   = [0, 0; -0.5, 0; -0.5, 0.65];

% First circle is outer circle
csign = 1;
i1 = 0;
nvb = 0;
for icirc = 1:ncirc
for i = 0:nvertices(icirc)-1
i1 = i1+1;
theta = 2*pi*i/nvertices(icirc);
coor(1,i1) = radii(icirc)*cos(theta) + centres(icirc, 1);
coor(2,i1) = csign*radii(icirc)*sin(theta) +  centres(icirc, 2);
edge(1,i1) = i1;
edge(2,i1) = i1 + 1;
edge(3,i1) = 1;
end
edge(2,i1) = nvb + 1;
nvb = nvb + nvertices(icirc);
% Subsequent circles are inner circles
csign = -1;
end
nedge = nvb;

% Initialise mesh control parameters
bspace = zeros(1, 100);
bspace(1:nvb) = 0.05;
smooth = true;
itrace = int64(0);

nnzmax = int64(3000);

% Mesh geometry
[nv, nelt, coor, conn, ifail] = d06aa(edge, coor, bspace, smooth, itrace);

% Compute the sparsity of the FE matrix from the input geometry
[nz, irow, icol, ifail] = d06cb(nv, nnzmax, conn, 'nelt', nelt);

if (ifail == 0)
fprintf('\nNumber of non-zero entries in input mesh:  %d\n', nz);

% Plot sparsity of input mesh
fig1 = figure('Number', 'off');
plot(irow(1:double(nz)), icol(1:double(nz)), '.');
title ('Input Mesh', 'FontSize', 14);
end

% Call the renumbering routine and get the new sparsity
[nz, coor, edge, conn, irow, icol, ifail] = ...
d06cc(nnzmax, coor, edge, conn, itrace, 'nelt', nelt);

if (ifail == 0)
fprintf('Number of non-zero entries in output mesh: %d\n', nz);
% Plot smoothed mesh
fig2 = figure('Number', 'off');
plot(irow(1:double(nz)), icol(1:double(nz)), '.');
title ('Output Mesh', 'FontSize', 14);
end
```
```

Number of non-zero entries in input mesh:  1556
Number of non-zero entries in output mesh: 1556

```