Line data Source code
1 : /*
2 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3 : SLEPc - Scalable Library for Eigenvalue Problem Computations
4 : Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
5 :
6 : This file is part of SLEPc.
7 : SLEPc is distributed under a 2-clause BSD license (see LICENSE).
8 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9 : */
10 :
11 : static char help[] = "Illustrates feeding exact eigenvectors as initial vectors of a second solve.\n\n"
12 : "Based on ex2.\n"
13 : "The command line options are:\n"
14 : " -n <n>, where <n> = number of grid subdivisions in x dimension.\n"
15 : " -m <m>, where <m> = number of grid subdivisions in y dimension.\n\n";
16 :
17 : #include <slepceps.h>
18 :
19 8 : int main(int argc,char **argv)
20 : {
21 8 : Mat A;
22 8 : EPS eps;
23 8 : PetscInt N,n=10,m,Istart,Iend,II,nev,nconv,i,j,neigs=5;
24 8 : PetscBool flag,terse;
25 8 : Vec v,*X;
26 :
27 8 : PetscFunctionBeginUser;
28 8 : PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
29 8 : PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
30 8 : PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag));
31 8 : if (!flag) m=n;
32 8 : N = n*m;
33 8 : PetscCall(PetscOptionsGetInt(NULL,NULL,"-neigs",&neigs,NULL));
34 8 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n2-D Laplacian Eigenproblem, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid), neigs=%" PetscInt_FMT "\n\n",N,n,m,neigs));
35 :
36 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
37 : Create the 2-D Laplacian
38 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
39 :
40 8 : PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
41 8 : PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N));
42 8 : PetscCall(MatSetFromOptions(A));
43 8 : PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
44 708 : for (II=Istart;II<Iend;II++) {
45 700 : i = II/n; j = II-i*n;
46 700 : if (i>0) PetscCall(MatSetValue(A,II,II-n,-1.0,INSERT_VALUES));
47 700 : if (i<m-1) PetscCall(MatSetValue(A,II,II+n,-1.0,INSERT_VALUES));
48 700 : if (j>0) PetscCall(MatSetValue(A,II,II-1,-1.0,INSERT_VALUES));
49 700 : if (j<n-1) PetscCall(MatSetValue(A,II,II+1,-1.0,INSERT_VALUES));
50 700 : PetscCall(MatSetValue(A,II,II,4.0,INSERT_VALUES));
51 : }
52 8 : PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
53 8 : PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
54 8 : PetscCall(MatCreateVecs(A,&v,NULL));
55 :
56 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
57 : Create the eigensolver and set various options
58 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
59 :
60 8 : PetscCall(EPSCreate(PETSC_COMM_WORLD,&eps));
61 8 : PetscCall(EPSSetOperators(eps,A,NULL));
62 8 : PetscCall(EPSSetProblemType(eps,EPS_HEP));
63 8 : PetscCall(EPSSetWhichEigenpairs(eps,EPS_SMALLEST_REAL));
64 8 : PetscCall(EPSSetDimensions(eps,neigs,PETSC_DETERMINE,PETSC_DETERMINE));
65 8 : PetscCall(EPSSetFromOptions(eps));
66 :
67 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
68 : Solve the eigensystem for the first time
69 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
70 :
71 8 : PetscCall(EPSSolve(eps));
72 8 : PetscCall(EPSGetDimensions(eps,&nev,NULL,NULL));
73 8 : PetscCall(PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev));
74 :
75 8 : PetscCall(EPSGetConverged(eps,&nconv));
76 8 : PetscCheck(nconv>=neigs,PETSC_COMM_WORLD,PETSC_ERR_CONV_FAILED,"Only %" PetscInt_FMT " eigenvalues have converged, %" PetscInt_FMT " requested",nconv,neigs);
77 8 : PetscCall(VecDuplicateVecs(v,neigs,&X));
78 48 : for (i=0;i<neigs;i++) PetscCall(EPSGetEigenvector(eps,i,X[i],NULL));
79 :
80 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
81 : Display solution of first solve
82 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
83 :
84 8 : PetscCall(PetscOptionsHasName(NULL,NULL,"-terse",&terse));
85 8 : if (terse) PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));
86 : else {
87 0 : PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
88 0 : PetscCall(EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD));
89 0 : PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
90 0 : PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
91 : }
92 :
93 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
94 : Solve the eigensystem again, feeding the initial vectors
95 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
96 :
97 8 : PetscCall(PetscPrintf(PETSC_COMM_WORLD," Solving again with eigenvectors as initial guesses\n"));
98 8 : PetscCall(EPSSetInitialSpace(eps,neigs,X));
99 8 : PetscCall(EPSSolve(eps));
100 :
101 8 : if (terse) PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL));
102 : else {
103 0 : PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL));
104 0 : PetscCall(EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD));
105 0 : PetscCall(EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD));
106 0 : PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
107 : }
108 :
109 8 : PetscCall(VecDestroy(&v));
110 8 : PetscCall(VecDestroyVecs(neigs,&X));
111 8 : PetscCall(EPSDestroy(&eps));
112 8 : PetscCall(MatDestroy(&A));
113 8 : PetscCall(SlepcFinalize());
114 : return 0;
115 : }
116 :
117 : /*TEST
118 :
119 : test:
120 : suffix: 1
121 : args: -eps_type {{gd jd rqcg lobpcg}} -terse
122 :
123 : testset:
124 : args: -eps_interval .17,1.3 -terse
125 : filter: grep -v "requested"
126 : output_file: output/test27_2.out
127 : test:
128 : suffix: 2
129 : args: -st_type filter -st_filter_degree 150 -eps_nev 1
130 : requires: !single
131 : test:
132 : suffix: 2_evsl
133 : nsize: {{1 2}}
134 : args: -eps_type evsl
135 : requires: evsl
136 :
137 : TEST*/
|