Actual source code: test13.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[] = "Solve a quadratic problem with CISS.\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 <slepcpep.h>

 18: int main(int argc,char **argv)
 19: {
 20:   Mat               M,C,K,A[3];
 21:   PEP               pep;
 22:   RG                rg;
 23:   KSP               *ksp;
 24:   PC                pc;
 25:   PEPCISSExtraction ext;
 26:   PetscInt          N,n=10,m,Istart,Iend,II,i,j,nsolve;
 27:   PetscBool         flg;

 29:   SlepcInitialize(&argc,&argv,(char*)0,help);
 30:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 31:   PetscOptionsGetInt(NULL,NULL,"-m",&m,&flg);
 32:   if (!flg) m=n;
 33:   N = n*m;
 34:   PetscPrintf(PETSC_COMM_WORLD,"\nQuadratic Eigenproblem, N=%" PetscInt_FMT " (%" PetscInt_FMT "x%" PetscInt_FMT " 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++) MatSetValue(M,II,II,(PetscReal)(II+1),INSERT_VALUES);
 79:   MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);
 80:   MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);

 82:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 83:              Create the eigensolver and solve the eigensystem
 84:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 86:   PEPCreate(PETSC_COMM_WORLD,&pep);
 87:   A[0] = K; A[1] = C; A[2] = M;
 88:   PEPSetOperators(pep,3,A);
 89:   PEPSetProblemType(pep,PEP_GENERAL);

 91:   /* customize polynomial eigensolver; set runtime options */
 92:   PEPSetType(pep,PEPCISS);
 93:   PEPGetRG(pep,&rg);
 94:   RGSetType(rg,RGELLIPSE);
 95:   RGEllipseSetParameters(rg,PetscCMPLX(-0.1,0.3),0.1,0.25);
 96:   PEPCISSSetSizes(pep,24,PETSC_DEFAULT,PETSC_DEFAULT,1,PETSC_DEFAULT,PETSC_TRUE);
 97:   PEPCISSGetKSPs(pep,&nsolve,&ksp);
 98:   for (i=0;i<nsolve;i++) {
 99:     KSPSetTolerances(ksp[i],1e-12,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);
100:     KSPSetType(ksp[i],KSPPREONLY);
101:     KSPGetPC(ksp[i],&pc);
102:     PCSetType(pc,PCLU);
103:   }
104:   PEPSetFromOptions(pep);

106:   /* solve */
107:   PetscObjectTypeCompare((PetscObject)pep,PEPCISS,&flg);
108:   if (flg) {
109:     PEPCISSGetExtraction(pep,&ext);
110:     PetscPrintf(PETSC_COMM_WORLD," Running CISS with %" PetscInt_FMT " KSP solvers (%s extraction)\n",nsolve,PEPCISSExtractions[ext]);
111:   }
112:   PEPSolve(pep);

114:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
115:                     Display solution and clean up
116:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

118:   PEPErrorView(pep,PEP_ERROR_BACKWARD,NULL);
119:   PEPDestroy(&pep);
120:   MatDestroy(&M);
121:   MatDestroy(&C);
122:   MatDestroy(&K);
123:   SlepcFinalize();
124:   return 0;
125: }

127: /*TEST

129:    build:
130:       requires: complex

132:    test:
133:       suffix: 1
134:       requires: complex

136: TEST*/