Subversion Repositories slepc-dev

Rev

Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1376 slepc 1
/*
2
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1672 slepc 3
   SLEPc - Scalable Library for Eigenvalue Problem Computations
2575 eromero 4
   Copyright (c) 2002-2011, Universitat Politecnica de Valencia, Spain
290 dsic.upv.es!antodo 5
 
1672 slepc 6
   This file is part of SLEPc.
7
 
8
   SLEPc is free software: you can redistribute it and/or modify it under  the
9
   terms of version 3 of the GNU Lesser General Public License as published by
10
   the Free Software Foundation.
11
 
12
   SLEPc  is  distributed in the hope that it will be useful, but WITHOUT  ANY
13
   WARRANTY;  without even the implied warranty of MERCHANTABILITY or  FITNESS
14
   FOR  A  PARTICULAR PURPOSE. See the GNU Lesser General Public  License  for
15
   more details.
16
 
17
   You  should have received a copy of the GNU Lesser General  Public  License
18
   along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
1376 slepc 19
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20
*/
21
 
302 dsic.upv.es!antodo 22
static char help[] = "Computes the smallest nonzero eigenvalue of the Laplacian of a graph.\n\n"
1926 jroman 23
  "This example illustrates EPSSetDeflationSpace(). The example graph corresponds to a "
1156 slepc 24
  "2-D regular mesh. The command line options are:\n"
25
  "  -n <n>, where <n> = number of grid subdivisions in x dimension.\n"
302 dsic.upv.es!antodo 26
  "  -m <m>, where <m> = number of grid subdivisions in y dimension.\n\n";
290 dsic.upv.es!antodo 27
 
2283 jroman 28
#include <slepceps.h>
290 dsic.upv.es!antodo 29
 
30
#undef __FUNCT__
31
#define __FUNCT__ "main"
2331 jroman 32
int main (int argc,char **argv)
290 dsic.upv.es!antodo 33
{
2316 jroman 34
  EPS            eps;             /* eigenproblem solver context */
35
  Mat            A;               /* operator matrix */
36
  Vec            x;
1507 slepc 37
  const EPSType  type;
2503 jroman 38
  PetscReal      tol;
39
  PetscInt       N,n=10,m,i,j,II,Istart,Iend,nev,maxit,its;
2316 jroman 40
  PetscScalar    w;
41
  PetscBool      flag;
2317 jroman 42
  PetscErrorCode ierr;
290 dsic.upv.es!antodo 43
 
44
  SlepcInitialize(&argc,&argv,(char*)0,help);
45
 
46
  ierr = PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);CHKERRQ(ierr);
302 dsic.upv.es!antodo 47
  ierr = PetscOptionsGetInt(PETSC_NULL,"-m",&m,&flag);CHKERRQ(ierr);
2177 jroman 48
  if(!flag) m=n;
302 dsic.upv.es!antodo 49
  N = n*m;
2503 jroman 50
  ierr = PetscPrintf(PETSC_COMM_WORLD,"\nFiedler vector of a 2-D regular mesh, N=%D (%Dx%D grid)\n\n",N,n,m);CHKERRQ(ierr);
290 dsic.upv.es!antodo 51
 
52
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
53
     Compute the operator matrix that defines the eigensystem, Ax=kx
305 dsic.upv.es!jroman 54
     In this example, A = L(G), where L is the Laplacian of graph G, i.e.
55
     Lii = degree of node i, Lij = -1 if edge (i,j) exists in G
290 dsic.upv.es!antodo 56
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
57
 
828 dsic.upv.es!antodo 58
  ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
59
  ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);CHKERRQ(ierr);
290 dsic.upv.es!antodo 60
  ierr = MatSetFromOptions(A);CHKERRQ(ierr);
61
 
62
  ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr);
2331 jroman 63
  for (II=Istart;II<Iend;II++) {
1961 antodo 64
    i = II/n; j = II-i*n;
302 dsic.upv.es!antodo 65
    w = 0.0;
1961 antodo 66
    if(i>0) { ierr = MatSetValue(A,II,II-n,-1.0,INSERT_VALUES);CHKERRQ(ierr); w=w+1.0; }
67
    if(i<m-1) { ierr = MatSetValue(A,II,II+n,-1.0,INSERT_VALUES);CHKERRQ(ierr); w=w+1.0; }
68
    if(j>0) { ierr = MatSetValue(A,II,II-1,-1.0,INSERT_VALUES);CHKERRQ(ierr); w=w+1.0; }
69
    if(j<n-1) { ierr = MatSetValue(A,II,II+1,-1.0,INSERT_VALUES);CHKERRQ(ierr); w=w+1.0; }
70
    ierr = MatSetValue(A,II,II,w,INSERT_VALUES);CHKERRQ(ierr);
290 dsic.upv.es!antodo 71
  }
72
 
73
  ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
74
  ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
75
 
76
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77
                Create the eigensolver and set various options
78
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
79
 
80
  /*
81
     Create eigensolver context
82
  */
83
  ierr = EPSCreate(PETSC_COMM_WORLD,&eps);CHKERRQ(ierr);
84
 
85
  /*
86
     Set operators. In this case, it is a standard eigenvalue problem
87
  */
88
  ierr = EPSSetOperators(eps,A,PETSC_NULL);CHKERRQ(ierr);
408 dsic.upv.es!antodo 89
  ierr = EPSSetProblemType(eps,EPS_HEP);CHKERRQ(ierr);
302 dsic.upv.es!antodo 90
 
91
  /*
943 dsic.upv.es!jroman 92
     Select portion of spectrum
302 dsic.upv.es!antodo 93
  */
943 dsic.upv.es!jroman 94
  ierr = EPSSetWhichEigenpairs(eps,EPS_SMALLEST_REAL);CHKERRQ(ierr);
290 dsic.upv.es!antodo 95
 
96
  /*
97
     Set solver parameters at runtime
98
  */
99
  ierr = EPSSetFromOptions(eps);CHKERRQ(ierr);
100
 
302 dsic.upv.es!antodo 101
  /*
305 dsic.upv.es!jroman 102
     Attach deflation space: in this case, the matrix has a constant
103
     nullspace, [1 1 ... 1]^T is the eigenvector of the zero eigenvalue
302 dsic.upv.es!antodo 104
  */
105
  ierr = MatGetVecs(A,&x,PETSC_NULL);CHKERRQ(ierr);
828 dsic.upv.es!antodo 106
  ierr = VecSet(x,1.0);CHKERRQ(ierr);
1926 jroman 107
  ierr = EPSSetDeflationSpace(eps,1,&x);CHKERRQ(ierr);
2305 jroman 108
  ierr = VecDestroy(&x);
302 dsic.upv.es!antodo 109
 
290 dsic.upv.es!antodo 110
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111
                      Solve the eigensystem
112
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
113
 
114
  ierr = EPSSolve(eps);CHKERRQ(ierr);
2331 jroman 115
  ierr = EPSGetIterationNumber(eps,&its);CHKERRQ(ierr);
2503 jroman 116
  ierr = PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %D\n",its);CHKERRQ(ierr);
290 dsic.upv.es!antodo 117
 
118
  /*
119
     Optional: Get some information from the solver and display it
120
  */
121
  ierr = EPSGetType(eps,&type);CHKERRQ(ierr);
122
  ierr = PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);CHKERRQ(ierr);
1575 slepc 123
  ierr = EPSGetDimensions(eps,&nev,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
2503 jroman 124
  ierr = PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %D\n",nev);CHKERRQ(ierr);
290 dsic.upv.es!antodo 125
  ierr = EPSGetTolerances(eps,&tol,&maxit);CHKERRQ(ierr);
2503 jroman 126
  ierr = PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4G, maxit=%D\n",tol,maxit);CHKERRQ(ierr);
290 dsic.upv.es!antodo 127
 
128
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
129
                    Display solution and clean up
130
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
131
 
2503 jroman 132
  ierr = EPSPrintSolution(eps,PETSC_NULL);CHKERRQ(ierr);
2308 jroman 133
  ierr = EPSDestroy(&eps);CHKERRQ(ierr);
2305 jroman 134
  ierr = MatDestroy(&A);CHKERRQ(ierr);
290 dsic.upv.es!antodo 135
  ierr = SlepcFinalize();CHKERRQ(ierr);
136
  return 0;
137
}
138