# FEM2D_POISSON_SPARSE Finite Element Solution of Poisson's Equation on a Triangulated Region using a Sparse Matrix Solver

FEM2D_POISSON_SPARSE is a FORTRAN90 program which applies the finite element method to solve a form of Poisson's equation over an arbitrary triangulated region, using sparse matrix storage and an iterative solver.

### Sparse Matrix Modifications:

This program is a revised version of FEM2D_POISSON.

FEM2D_POISSON used a direct LINPACK/LAPACK banded matrix solver. The storage requirements for the banded solver are less than for a full storage solver, but still grow quickly as the problem size increases.

FEM2D_POISSON_SPARSE reduces the storage requirements even further, by using sparse matrix techniques. The storage format chosen is known as DSP or "sparse triplet" format, which essentially simply saves in three vectors A, IA, JA, which record the value, row and column of every nonzero entry. To solve the linear system, the MGMRES iterative solver was applied. With these modifications, FEM2D_POISSON_SPARSE can handle problems much larger than those that FEM2D_POISSON could. A new issue is that the iterative solver must be monitored to ensure that convergence is proceeding properly, and has reached the desired tolerance.

### The Triangulated Region:

The computational region is unknown by the program. The user specifies it by preparing a file containing the coordinates of the nodes, and a file containing the indices of nodes that make up triangles that form a triangulation of the region.

Normally, the user does not type in this information by hand, but has a program fill in the nodes, and perhaps another program that constructs the triangulation. However, in the simplest case, the user might construct a very crude triangulation by hand, and have TRIANGULATION_REFINE refine it to something more reasonable.

For the following ridiculously small example:

```        4----5
|\   |\
| \  | \
|  \ |  \
|   \|   \
1----2----3
```
the node file would be:
```         0.0 0.0
1.0 0.0
2.0 0.0
0.0 1.0
1.0 1.0
```
and the triangle file would be
```        1 2 4
5 4 2
2 3 5
```

### The Poisson Equation:

The program is set up to handle the linear Poisson equation with a right hand side function, and nonhomogeneous Dirichlet boundary conditions. The state variable U(X,Y) is then constrained by:

```        - Del H(x,y) Del U(x,y) + K(x,y) * U(x,y) = F(x,y)  inside the region;
U(x,y) = G(x,y)  on the boundary.
```

A fancier version of the program is eventually intended, which will handle a more interesting nonlinear PDE, and include optional Neumann boundary conditions.

### User Interface:

To specify the boundary condition function G(x,y), the linear coefficients H(x,y) and K(x,y) and the right hand side function F(x,y), the user has to modify a file containing three subroutines,

• SUBROUTINE DIRICHLET_CONDITION ( NODE_NUM, NODE_XY, NODE_BC ) evaluates G(X,Y).
• SUBROUTINE H_COEF ( NODE_NUM, NODE_XY, NODE_H ) evaluates H(x,y).
• SUBROUTINE K_COEF ( NODE_NUM, NODE_XY, NODE_K ) evaluates K(x,y).
• SUBROUTINE RHS ( NODE_NUM, NODE_XY, NODE_F ) evaluates F(x,y).

To run the program, the user compiles the user routines, links them with FEM2D_POISSON_SPARSE, and runs the executable.

The program writes out a file containing an Encapsulated PostScript image of the nodes and elements, with numbers. If there are too many nodes, the plot may be too cluttered to read. For lower values, however, it is a valuable map of what is going on in the geometry.

The program is also able to write out a file containing the solution value at every node. This file may be used to create contour plots of the solution.

### Usage:

The user must create an executable by compiling the user routines and linking them with the main program, perhaps by commands like:

```        gfortran -c fem2d_poisson_sparse.f90
gfortran -c user.f90
gfortran fem2d_poisson_sparse.o user.o
mv a.out fem2d_poisson_sparse
```

Assuming the executable program is called "fem2d_poisson_sparse", then the program is executed by

fem2d_poisson_sparse prefix
where prefix is the common filename prefix, so that:
• prefix_nodes.txt, is a file containing the node coordinates;
• prefix_elements.txt, is a file listing the 3 nodes that make up each element;

### Languages:

FEM2D_POISSON_SPARSE is available in a C++ version and a FORTRAN90 version and a MATLAB version.

### Related Programs:

FEM2D_POISSON, a FORTRAN90 program which solves Poisson's equation on a triangulated region, using the finite element method and a banded solver. In order to run, it requires user-supplied routines that define problem data.

FEM2D_POISSON_CG, a FORTRAN90 program which solves Poisson's equation on a triangulated region, using the finite element method, sparse storage, and a conjugate gradient solver.

FEM2D_POISSON_SPARSE_BAFFLE, a FORTRAN90 library which defines the geometry of a rectangle channel containing 13 hexagonal baffles, as well as boundary conditions for a given Poisson problem, and is called by fem2d_poisson_sparse as part of a solution procedure.

FEM2D_POISSON_SPARSE_ELL, a FORTRAN90 library which defines the geometry of an L-shaped region, as well as boundary conditions for a given Poisson problem, and is called by FEM2D_POISSON_SPARSE as part of a solution procedure.

FEM2D_POISSON_SPARSE_LAKE, a FORTRAN90 library which defines the geometry of a lake-shaped region, as well as boundary conditions for a given Poisson problem, and is called by FEM2D_POISSON_SPARSE as part of a solution procedure.

MGMRES, a FORTRAN90 library which applies the restarted Generalized Minimum Residual (GMRES) algorithm to solve a sparse linear system, by Lili Ju.

### Reference:

1. Hans Rudolf Schwarz,
Finite Element Methods,
ISBN: 0126330107,
LC: TA347.F5.S3313.
2. Gilbert Strang, George Fix,
An Analysis of the Finite Element Method,
Cambridge, 1973,
ISBN: 096140888X,
LC: TA335.S77.
3. Olgierd Zienkiewicz,
The Finite Element Method,
Sixth Edition,
Butterworth-Heinemann, 2005,
ISBN: 0750663200,
LC: TA640.2.Z54

### List of Routines:

• MAIN is the main program of FEM2D_POISSON_SPARSE.
• ASSEMBLE_POISSON_DSP assembles the system for the Poisson equation.
• AX computes A * X for a sparse matrix.
• BASIS_ONE_T3 evaluates a linear basis function.
• CH_CAP capitalizes a single character.
• CH_EQI is a case insensitive comparison of two characters for equality.
• CH_TO_DIGIT returns the integer value of a base 10 digit.
• DIRICHLET_APPLY_DSP accounts for Dirichlet boundary conditions.
• DSP_IJ_TO_K seeks the compressed index of the (I,J) entry of A.
• DSP_PRINT_SOME prints some of a DSP matrix.
• FILE_COLUMN_COUNT counts the number of columns in the first line of a file.
• FILE_NAME_SPECIFICATION determines the names of the input files.
• FILE_ROW_COUNT counts the number of row records in a file.
• GET_UNIT returns a free FORTRAN unit number.
• I4_HUGE returns a "huge" I4.
• I4_MODP returns the nonnegative remainder of integer division.
• I4_WRAP forces an integer to lie between given limits by wrapping.
• I4COL_COMPARE compares columns I and J of an I4COL.
• I4COL_SORT_A ascending sorts an I4COL.
• I4COL_SWAP swaps columns I and J of an I4COL.
• I4MAT_TRANSPOSE_PRINT_SOME prints some of the transpose of an I4mat.
• I4VEC2_COMPARE compares pairs of integers stored in two vectors.
• I4VEC2_SORT_A ascending sorts a vector of pairs of integers.
• MGMRES applies the restarted GMRES iteration to a linear system.
• MULT_GIVENS applies a Givens rotation to two successive entries of a vector.
• POINTS_PLOT plots a pointset.
• R8MAT_TRANSPOSE_PRINT_SOME prints some of an R8MAT, transposed.
• R8MAT_WRITE writes an R8MAT file.
• R8VEC_PRINT_SOME prints "some" of an R8VEC.
• R8VEC_UNIFORM_01 returns a unit pseudorandom R8VEC.
• REFERENCE_TO_PHYSICAL_T3 maps reference points to physical points.
• S_TO_I4 reads an I4 from a string.
• S_TO_I4VEC reads an I4VEC from a string.
• S_TO_R8 reads an R8 from a string.
• S_TO_R8VEC reads an R8VEC from a string.
• S_WORD_COUNT counts the number of "words" in a string.
• SOLUTION_EVALUATE evaluates the solution at a point in an element.
• SORT_HEAP_EXTERNAL externally sorts a list of items into ascending order.
• TIMESTAMP prints the current YMDHMS date as a time stamp.
• TRIANGLE_AREA_2D computes the area of a triangle in 2D.