Demo: Solution of the eigenvalue problem for the 2-D Laplace equation

We present a program to compute eigenvalues and eigenvectors of the Laplace equation with homogeneous Dirichlet boundary conditions, discretized by the P1 finite element method.

The Code

  • We start, as usual, by including the main header file of the library, and the header for the Laplace equation. The program has, as argument the name of the project file, from which we deduce the name of the mesh file and the number of sought eigenvalues (variable nb). We also construct a Mesh instance.

    #include "OFELI.h"
    #include "Laplace.h"
                              
    using namespace OFELI;
                              
    int main(int argc, char *argv[])
    {
       IPF data(argv[1]);
       Mesh ms(data.getMeshFile());
       int nb = data.getInteger("nb");

  • We construct an instance of class Laplace2DT3 using the Mesh instance, and then construct an instance of class EigenProblemSolver by using the Laplace2DT3 instance. Finally, we run the subspace method to extract the eigenvalues.

       Laplace2DT3 eq(ms);
       EigenProblemSolver e(eq);
       e.run(nb);

  • We end by printing the number of performed iterations. Then we print the eigenvalues. The eigenvectors are stored one by one in vtk files. The names of theses files are given in the project file, by using the tag plot_file.

       cout << "Nb. of iterations: " << e.getNbIter() << endl;
       Vect<double> v(ms);
       for (int i=1; i<=nb; i++) {
          cout << "Eigenvalue #" << i << ": " << e.getEigenValue(i) << endl;
          e.getEigenVector(i,v);
          saveField(v,ms,data.getPlotFile(i),GMSH);
       }
       return 0;
    }

A Project File

We give here an example of a project file.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<OFELI_File>
   <info>
      <title>EigenMode Project</title>
      <date>January 1, 2016</date>
      <author>R. Touzani</author>
   </info>
   <Project name="eigen">
      <mesh_file value="test.m" />
      <parameter label="nb" value="5" />
      <plot_file value="test-1.pos" />
      <plot_file value="test-2.pos" />
      <plot_file value="test-3.pos" />
      <plot_file value="test-4.pos" />
      <plot_file value="test-5.pos" />
   </Project>
</OFELI_File>