Actual source code: test8.c
slepc-3.16.0 2021-09-30
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-2021, Universitat Politecnica de Valencia, Spain
6: This file is part of SLEPc.
7: SLEPc is distributed under a 2-clause BSD license (see LICENSE).
8: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9: */
11: static char help[] = "Test interface functions of polynomial JD.\n\n"
12: "This is based on ex16.c. The command line options are:\n"
13: " -n <n>, where <n> = number of grid subdivisions in x dimension.\n"
14: " -m <m>, where <m> = number of grid subdivisions in y dimension.\n\n";
16: #include <slepcpep.h>
18: int main(int argc,char **argv)
19: {
20: Mat M,C,K,A[3]; /* problem matrices */
21: PEP pep; /* polynomial eigenproblem solver context */
22: PetscInt N,n=10,m,Istart,Iend,II,i,j,midx;
23: PetscReal restart,fix;
24: PetscBool flag,reuse;
25: PEPJDProjection proj;
26: PetscErrorCode ierr;
28: SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
30: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
31: PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag);
32: if (!flag) m=n;
33: N = n*m;
34: PetscPrintf(PETSC_COMM_WORLD,"\nQuadratic Eigenproblem, N=%D (%Dx%D grid)\n\n",N,n,m);
36: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
37: Compute the matrices that define the eigensystem, (k^2*M+k*C+K)x=0
38: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
40: /* K is the 2-D Laplacian */
41: MatCreate(PETSC_COMM_WORLD,&K);
42: MatSetSizes(K,PETSC_DECIDE,PETSC_DECIDE,N,N);
43: MatSetFromOptions(K);
44: MatSetUp(K);
45: MatGetOwnershipRange(K,&Istart,&Iend);
46: for (II=Istart;II<Iend;II++) {
47: i = II/n; j = II-i*n;
48: if (i>0) { MatSetValue(K,II,II-n,-1.0,INSERT_VALUES); }
49: if (i<m-1) { MatSetValue(K,II,II+n,-1.0,INSERT_VALUES); }
50: if (j>0) { MatSetValue(K,II,II-1,-1.0,INSERT_VALUES); }
51: if (j<n-1) { MatSetValue(K,II,II+1,-1.0,INSERT_VALUES); }
52: MatSetValue(K,II,II,4.0,INSERT_VALUES);
53: }
54: MatAssemblyBegin(K,MAT_FINAL_ASSEMBLY);
55: MatAssemblyEnd(K,MAT_FINAL_ASSEMBLY);
57: /* C is the 1-D Laplacian on horizontal lines */
58: MatCreate(PETSC_COMM_WORLD,&C);
59: MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,N,N);
60: MatSetFromOptions(C);
61: MatSetUp(C);
62: MatGetOwnershipRange(C,&Istart,&Iend);
63: for (II=Istart;II<Iend;II++) {
64: i = II/n; j = II-i*n;
65: if (j>0) { MatSetValue(C,II,II-1,-1.0,INSERT_VALUES); }
66: if (j<n-1) { MatSetValue(C,II,II+1,-1.0,INSERT_VALUES); }
67: MatSetValue(C,II,II,2.0,INSERT_VALUES);
68: }
69: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
70: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
72: /* M is a diagonal matrix */
73: MatCreate(PETSC_COMM_WORLD,&M);
74: MatSetSizes(M,PETSC_DECIDE,PETSC_DECIDE,N,N);
75: MatSetFromOptions(M);
76: MatSetUp(M);
77: MatGetOwnershipRange(M,&Istart,&Iend);
78: for (II=Istart;II<Iend;II++) {
79: MatSetValue(M,II,II,(PetscReal)(II+1),INSERT_VALUES);
80: }
81: MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
82: MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);
84: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
85: Create the eigensolver and set various options
86: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
88: PEPCreate(PETSC_COMM_WORLD,&pep);
89: A[0] = K; A[1] = C; A[2] = M;
90: PEPSetOperators(pep,3,A);
91: PEPSetType(pep,PEPJD);
93: /*
94: Test interface functions of STOAR solver
95: */
96: PEPJDGetRestart(pep,&restart);
97: PetscPrintf(PETSC_COMM_WORLD," Restart parameter before changing = %g",(double)restart);
98: PEPJDSetRestart(pep,0.3);
99: PEPJDGetRestart(pep,&restart);
100: PetscPrintf(PETSC_COMM_WORLD," ... changed to %g\n",(double)restart);
102: PEPJDGetFix(pep,&fix);
103: PetscPrintf(PETSC_COMM_WORLD," Fix parameter before changing = %g",(double)fix);
104: PEPJDSetFix(pep,0.001);
105: PEPJDGetFix(pep,&fix);
106: PetscPrintf(PETSC_COMM_WORLD," ... changed to %g\n",(double)fix);
108: PEPJDGetReusePreconditioner(pep,&reuse);
109: PetscPrintf(PETSC_COMM_WORLD," Reuse preconditioner flag before changing = %d",(int)reuse);
110: PEPJDSetReusePreconditioner(pep,PETSC_TRUE);
111: PEPJDGetReusePreconditioner(pep,&reuse);
112: PetscPrintf(PETSC_COMM_WORLD," ... changed to %d\n",(int)reuse);
114: PEPJDGetProjection(pep,&proj);
115: PetscPrintf(PETSC_COMM_WORLD," Projection type before changing = %d",(int)proj);
116: PEPJDSetProjection(pep,PEP_JD_PROJECTION_ORTHOGONAL);
117: PEPJDGetProjection(pep,&proj);
118: PetscPrintf(PETSC_COMM_WORLD," ... changed to %d\n",(int)proj);
120: PEPJDGetMinimalityIndex(pep,&midx);
121: PetscPrintf(PETSC_COMM_WORLD," Minimality index before changing = %D",midx);
122: PEPJDSetMinimalityIndex(pep,2);
123: PEPJDGetMinimalityIndex(pep,&midx);
124: PetscPrintf(PETSC_COMM_WORLD," ... changed to %D\n",midx);
126: PEPSetFromOptions(pep);
128: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
129: Solve the eigensystem
130: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
132: PEPSolve(pep);
133: PEPErrorView(pep,PEP_ERROR_BACKWARD,NULL);
134: PEPDestroy(&pep);
135: MatDestroy(&M);
136: MatDestroy(&C);
137: MatDestroy(&K);
138: SlepcFinalize();
139: return ierr;
140: }
142: /*TEST
144: test:
145: args: -n 12 -pep_nev 2 -pep_ncv 21 -pep_conv_abs
147: TEST*/