fem2d_poisson_rectangle, a C++ code which solves the 2D Poisson equation using the finite element method.
The computational region is a rectangle, with Dirichlet boundary conditions applied along the boundary, and the Poisson equation applied inside. Thus, the state variable U(x,y) satisfies:
- ( Uxx + Uyy ) = F(x,y) in the box; U(x,y) = G(x,y) on the box boundary;For this program, the boundary condition function G(x,y) is identically zero.
The computational region is first covered with an NX by NY rectangular array of points, creating (NX-1)*(NY-1) subrectangles. Each subrectangle is divided into two triangles, creating a total of 2*(NX-1)*(NY-1) geometric "elements". Because quadratic basis functions are to be used, each triangle will be associated not only with the three corner nodes that defined it, but with three extra midside nodes. If we include these additional nodes, there are now a total of (2*NX-1)*(2*NY-1) nodes in the region.
We now assume that the unknown function U(x,y) can be represented as a linear combination of the basis functions associated with each node. The value of U at the boundary nodes is obvious, so we concentrate on the NUNK interior nodes where U(x,y) is unknown. For each node I, we determine a basis function PHI(I)(x,y), and evaluate the following finite element integral:
Integral ( Ux(x,y) * PHIx(I)(x,y) + Uy(x,y) * PHIy(I)(x,y) ) = Integral ( F(x,y) * PHI(I)(x,y)The set of all such equations yields a linear system for the coefficients of the representation of U.
The program allows the user to supply two routines:
There are a few variables that are easy to manipulate. In particular, the user can change the variables NX and NY in the main program, to change the number of nodes and elements. The variables (XL,YB) and (XR,YT) define the location of the lower left and upper right corners of the rectangular region, and these can also be changed in a single place in the main program.
The program writes out a file containing an Encapsulated PostScript image of the nodes and elements, with numbers. For values of NX and NY over 10, the plot is 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.
The original version of this code comes from Professor Janet Peterson.
The computer code and data files described and made available on this web page are distributed under the MIT license
fem2d_poisson_rectangle is available in a C version and a C++ version and a FORTRAN90 version and a MATLAB version.
FEM2D_POISSON_RECTANGLE_LINEAR, a C++ code which solves the 2D Poisson equation on a rectangle, using the finite element method, and piecewise linear triangular elements.