Actual source code: ex49.c

slepc-3.17.1 2022-04-11
Report Typos and Errors
  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[] = "User-defined split preconditioner when solving a generalized eigenproblem.\n\n"
 12:   "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 <slepceps.h>

 18: int main(int argc,char **argv)
 19: {
 20:   Mat            A,B,A0,B0,mats[2]; /* problem matrices and sparser approximations */
 21:   EPS            eps;               /* eigenproblem solver context */
 22:   ST             st;
 23:   PetscInt       N,n=24,m,Istart,Iend,II,i,j;
 24:   PetscBool      flag,terse;

 26:   SlepcInitialize(&argc,&argv,(char*)0,help);

 28:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 29:   PetscOptionsGetInt(NULL,NULL,"-m",&m,&flag);
 30:   if (!flag) m=n;
 31:   N = n*m;
 32:   PetscPrintf(PETSC_COMM_WORLD,"\nGHEP with split preconditioner, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " grid)\n\n",N,n,m);

 34:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 35:           Compute the problem matrices A and B
 36:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 38:   MatCreate(PETSC_COMM_WORLD,&A);
 39:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
 40:   MatSetFromOptions(A);
 41:   MatSetUp(A);

 43:   MatCreate(PETSC_COMM_WORLD,&B);
 44:   MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,N,N);
 45:   MatSetFromOptions(B);
 46:   MatSetUp(B);

 48:   MatGetOwnershipRange(A,&Istart,&Iend);
 49:   for (II=Istart;II<Iend;II++) {
 50:     i = II/n; j = II-i*n;
 51:     if (i>0) MatSetValue(A,II,II-n,-0.2,INSERT_VALUES);
 52:     if (i<m-1) MatSetValue(A,II,II+n,-0.2,INSERT_VALUES);
 53:     if (j>0) MatSetValue(A,II,II-1,-3.0,INSERT_VALUES);
 54:     if (j<n-1) MatSetValue(A,II,II+1,-3.0,INSERT_VALUES);
 55:     MatSetValue(A,II,II,7.0,INSERT_VALUES);
 56:     MatSetValue(B,II,II,2.0,INSERT_VALUES);
 57:   }
 58:   if (Istart==0) {
 59:     MatSetValue(B,0,0,6.0,INSERT_VALUES);
 60:     MatSetValue(B,0,1,-1.0,INSERT_VALUES);
 61:     MatSetValue(B,1,0,-1.0,INSERT_VALUES);
 62:     MatSetValue(B,1,1,1.0,INSERT_VALUES);
 63:   }
 64:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 65:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
 66:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
 67:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

 69:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 70:           Compute sparser approximations A0 and B0
 71:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 73:   MatCreate(PETSC_COMM_WORLD,&A0);
 74:   MatSetSizes(A0,PETSC_DECIDE,PETSC_DECIDE,N,N);
 75:   MatSetFromOptions(A0);
 76:   MatSetUp(A0);

 78:   MatCreate(PETSC_COMM_WORLD,&B0);
 79:   MatSetSizes(B0,PETSC_DECIDE,PETSC_DECIDE,N,N);
 80:   MatSetFromOptions(B0);
 81:   MatSetUp(B0);

 83:   MatGetOwnershipRange(A0,&Istart,&Iend);
 84:   for (II=Istart;II<Iend;II++) {
 85:     i = II/n; j = II-i*n;
 86:     if (j>0) MatSetValue(A0,II,II-1,-3.0,INSERT_VALUES);
 87:     if (j<n-1) MatSetValue(A0,II,II+1,-3.0,INSERT_VALUES);
 88:     MatSetValue(A0,II,II,7.0,INSERT_VALUES);
 89:     MatSetValue(B0,II,II,2.0,INSERT_VALUES);
 90:   }
 91:   if (Istart==0) {
 92:     MatSetValue(B0,0,0,6.0,INSERT_VALUES);
 93:     MatSetValue(B0,1,1,1.0,INSERT_VALUES);
 94:   }
 95:   MatAssemblyBegin(A0,MAT_FINAL_ASSEMBLY);
 96:   MatAssemblyEnd(A0,MAT_FINAL_ASSEMBLY);
 97:   MatAssemblyBegin(B0,MAT_FINAL_ASSEMBLY);
 98:   MatAssemblyEnd(B0,MAT_FINAL_ASSEMBLY);

100:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
101:                 Create the eigensolver and set various options
102:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

104:   EPSCreate(PETSC_COMM_WORLD,&eps);
105:   EPSSetOperators(eps,A,B);
106:   EPSSetProblemType(eps,EPS_GHEP);
107:   EPSGetST(eps,&st);
108:   STSetType(st,STSINVERT);
109:   mats[0] = A0; mats[1] = B0;
110:   STSetSplitPreconditioner(st,2,mats,SUBSET_NONZERO_PATTERN);
111:   EPSSetTarget(eps,0.0);
112:   EPSSetWhichEigenpairs(eps,EPS_TARGET_MAGNITUDE);
113:   EPSSetFromOptions(eps);

115:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
116:                  Solve the eigensystem and display solution
117:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

119:   EPSSolve(eps);

121:   /* show detailed info unless -terse option is given by user */
122:   PetscOptionsHasName(NULL,NULL,"-terse",&terse);
123:   if (terse) EPSErrorView(eps,EPS_ERROR_RELATIVE,NULL);
124:   else {
125:     PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO_DETAIL);
126:     EPSConvergedReasonView(eps,PETSC_VIEWER_STDOUT_WORLD);
127:     EPSErrorView(eps,EPS_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
128:     PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
129:   }
130:   EPSDestroy(&eps);
131:   MatDestroy(&A);
132:   MatDestroy(&B);
133:   MatDestroy(&A0);
134:   MatDestroy(&B0);
135:   SlepcFinalize();
136:   return 0;
137: }

139: /*TEST

141:    testset:
142:       args: -eps_nev 4 -terse
143:       output_file: output/ex49_1.out
144:       requires: !single
145:       test:
146:          suffix: 1
147:       test:
148:          suffix: 1_jd
149:          args: -eps_type jd -st_type precond
150:       test:
151:          suffix: 1_lobpcg
152:          args: -eps_type lobpcg -st_type precond -eps_smallest_real -st_shift 0.2

154:    testset:
155:       args: -eps_type ciss -eps_all -rg_type ellipse -rg_ellipse_center 0 -rg_ellipse_radius 0.34 -rg_ellipse_vscale .2 -terse
156:       output_file: output/ex49_2.out
157:       test:
158:          suffix: 2
159:       test:
160:          suffix: 2_nost
161:          args: -eps_ciss_usest 0
162:          requires: !single
163:       test:
164:          suffix: 2_par
165:          nsize: 2
166:          args: -eps_ciss_partitions 2
167:          requires: !single

169: TEST*/