Actual source code: ex3.c
slepc-3.18.1 2022-11-02
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-, 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[] = "Solves the same eigenproblem as in example ex2, but using a shell matrix. "
12: "The problem is a standard symmetric eigenproblem corresponding to the 2-D Laplacian operator.\n\n"
13: "The command line options are:\n"
14: " -n <n>, where <n> = number of grid subdivisions in both x and y dimensions.\n\n";
16: #include <slepceps.h>
18: /*
19: User-defined routines
20: */
21: PetscErrorCode MatMult_Laplacian2D(Mat A,Vec x,Vec y);
22: PetscErrorCode MatGetDiagonal_Laplacian2D(Mat A,Vec diag);
24: int main(int argc,char **argv)
25: {
26: Mat A; /* operator matrix */
27: EPS eps; /* eigenproblem solver context */
28: EPSType type;
29: PetscMPIInt size;
30: PetscInt N,n=10,nev;
31: PetscBool terse;
34: SlepcInitialize(&argc,&argv,(char*)0,help);
35: MPI_Comm_size(PETSC_COMM_WORLD,&size);
38: PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
39: N = n*n;
40: PetscPrintf(PETSC_COMM_WORLD,"\n2-D Laplacian Eigenproblem (matrix-free version), N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,n);
42: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
43: Create the operator matrix that defines the eigensystem, Ax=kx
44: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
46: MatCreateShell(PETSC_COMM_WORLD,N,N,N,N,&n,&A);
47: MatShellSetOperation(A,MATOP_MULT,(void(*)(void))MatMult_Laplacian2D);
48: MatShellSetOperation(A,MATOP_MULT_TRANSPOSE,(void(*)(void))MatMult_Laplacian2D);
49: MatShellSetOperation(A,MATOP_GET_DIAGONAL,(void(*)(void))MatGetDiagonal_Laplacian2D);
51: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
52: Create the eigensolver and set various options
53: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
55: /*
56: Create eigensolver context
57: */
58: EPSCreate(PETSC_COMM_WORLD,&eps);
60: /*
61: Set operators. In this case, it is a standard eigenvalue problem
62: */
63: EPSSetOperators(eps,A,NULL);
64: EPSSetProblemType(eps,EPS_HEP);
66: /*
67: Set solver parameters at runtime
68: */
69: EPSSetFromOptions(eps);
71: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
72: Solve the eigensystem
73: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
75: EPSSolve(eps);
77: /*
78: Optional: Get some information from the solver and display it
79: */
80: EPSGetType(eps,&type);
81: PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);
82: EPSGetDimensions(eps,&nev,NULL,NULL);
83: PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %" PetscInt_FMT "\n",nev);
85: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
86: Display solution and clean up
87: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
89: /* show detailed info unless -terse option is given by user */
90: PetscOptionsHasName(NULL,NULL,"-terse",&terse);
91: if (terse) EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
92: else {
93: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
94: EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD);
95: EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
96: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
97: }
98: EPSDestroy(&eps);
99: MatDestroy(&A);
100: SlepcFinalize();
101: return 0;
102: }
104: /*
105: Compute the matrix vector multiplication y<---T*x where T is a nx by nx
106: tridiagonal matrix with DD on the diagonal, DL on the subdiagonal, and
107: DU on the superdiagonal.
108: */
109: static void tv(int nx,const PetscScalar *x,PetscScalar *y)
110: {
111: PetscScalar dd,dl,du;
112: int j;
114: dd = 4.0;
115: dl = -1.0;
116: du = -1.0;
118: y[0] = dd*x[0] + du*x[1];
119: for (j=1;j<nx-1;j++)
120: y[j] = dl*x[j-1] + dd*x[j] + du*x[j+1];
121: y[nx-1] = dl*x[nx-2] + dd*x[nx-1];
122: }
124: /*
125: Matrix-vector product subroutine for the 2D Laplacian.
127: The matrix used is the 2 dimensional discrete Laplacian on unit square with
128: zero Dirichlet boundary condition.
130: Computes y <-- A*x, where A is the block tridiagonal matrix
132: | T -I |
133: |-I T -I |
134: A = | -I T |
135: | ... -I|
136: | -I T|
138: The subroutine TV is called to compute y<--T*x.
139: */
140: PetscErrorCode MatMult_Laplacian2D(Mat A,Vec x,Vec y)
141: {
142: void *ctx;
143: int nx,lo,i,j;
144: const PetscScalar *px;
145: PetscScalar *py;
148: MatShellGetContext(A,&ctx);
149: nx = *(int*)ctx;
150: VecGetArrayRead(x,&px);
151: VecGetArray(y,&py);
153: tv(nx,&px[0],&py[0]);
154: for (i=0;i<nx;i++) py[i] -= px[nx+i];
156: for (j=2;j<nx;j++) {
157: lo = (j-1)*nx;
158: tv(nx,&px[lo],&py[lo]);
159: for (i=0;i<nx;i++) py[lo+i] -= px[lo-nx+i] + px[lo+nx+i];
160: }
162: lo = (nx-1)*nx;
163: tv(nx,&px[lo],&py[lo]);
164: for (i=0;i<nx;i++) py[lo+i] -= px[lo-nx+i];
166: VecRestoreArrayRead(x,&px);
167: VecRestoreArray(y,&py);
168: return 0;
169: }
171: PetscErrorCode MatGetDiagonal_Laplacian2D(Mat A,Vec diag)
172: {
174: VecSet(diag,4.0);
175: return 0;
176: }
178: /*TEST
180: test:
181: suffix: 1
182: args: -n 72 -eps_nev 4 -eps_ncv 20 -terse
183: requires: !single
185: TEST*/