Actual source code: test9.c

slepc-3.21.1 2024-04-26
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[] = "Test ST with four matrices and split preconditioner.\n\n";

 13: #include <slepcst.h>

 15: int main(int argc,char **argv)
 16: {
 17:   Mat            A,B,C,D,Pa,Pb,Pc,Pd,Pmat,mat[4];
 18:   ST             st;
 19:   KSP            ksp;
 20:   PC             pc;
 21:   Vec            v,w;
 22:   STType         type;
 23:   PetscScalar    sigma;
 24:   PetscInt       n=10,i,Istart,Iend;

 26:   PetscFunctionBeginUser;
 27:   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
 28:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
 29:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nTest ST with four matrices, n=%" PetscInt_FMT "\n\n",n));
 30:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 31:      Compute the operator matrices
 32:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 34:   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
 35:   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n));
 36:   PetscCall(MatSetFromOptions(A));

 38:   PetscCall(MatCreate(PETSC_COMM_WORLD,&B));
 39:   PetscCall(MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,n,n));
 40:   PetscCall(MatSetFromOptions(B));

 42:   PetscCall(MatCreate(PETSC_COMM_WORLD,&C));
 43:   PetscCall(MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,n,n));
 44:   PetscCall(MatSetFromOptions(C));

 46:   PetscCall(MatCreate(PETSC_COMM_WORLD,&D));
 47:   PetscCall(MatSetSizes(D,PETSC_DECIDE,PETSC_DECIDE,n,n));
 48:   PetscCall(MatSetFromOptions(D));

 50:   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
 51:   for (i=Istart;i<Iend;i++) {
 52:     PetscCall(MatSetValue(A,i,i,2.0,INSERT_VALUES));
 53:     if (i>0) {
 54:       PetscCall(MatSetValue(A,i,i-1,-1.0,INSERT_VALUES));
 55:       PetscCall(MatSetValue(B,i,i,(PetscScalar)i,INSERT_VALUES));
 56:     } else PetscCall(MatSetValue(B,i,i,-1.0,INSERT_VALUES));
 57:     if (i<n-1) PetscCall(MatSetValue(A,i,i+1,-1.0,INSERT_VALUES));
 58:     PetscCall(MatSetValue(C,i,n-i-1,1.0,INSERT_VALUES));
 59:     PetscCall(MatSetValue(D,i,i,i*.1,INSERT_VALUES));
 60:     if (i==0) PetscCall(MatSetValue(D,0,n-1,1.0,INSERT_VALUES));
 61:     if (i==n-1) PetscCall(MatSetValue(D,n-1,0,1.0,INSERT_VALUES));
 62:   }

 64:   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
 65:   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
 66:   PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
 67:   PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));
 68:   PetscCall(MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY));
 69:   PetscCall(MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY));
 70:   PetscCall(MatAssemblyBegin(D,MAT_FINAL_ASSEMBLY));
 71:   PetscCall(MatAssemblyEnd(D,MAT_FINAL_ASSEMBLY));
 72:   PetscCall(MatCreateVecs(A,&v,&w));
 73:   PetscCall(VecSet(v,1.0));

 75:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 76:      Compute the split preconditioner matrices (four diagonals)
 77:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 79:   PetscCall(MatCreate(PETSC_COMM_WORLD,&Pa));
 80:   PetscCall(MatSetSizes(Pa,PETSC_DECIDE,PETSC_DECIDE,n,n));
 81:   PetscCall(MatSetFromOptions(Pa));

 83:   PetscCall(MatCreate(PETSC_COMM_WORLD,&Pb));
 84:   PetscCall(MatSetSizes(Pb,PETSC_DECIDE,PETSC_DECIDE,n,n));
 85:   PetscCall(MatSetFromOptions(Pb));

 87:   PetscCall(MatCreate(PETSC_COMM_WORLD,&Pc));
 88:   PetscCall(MatSetSizes(Pc,PETSC_DECIDE,PETSC_DECIDE,n,n));
 89:   PetscCall(MatSetFromOptions(Pc));

 91:   PetscCall(MatCreate(PETSC_COMM_WORLD,&Pd));
 92:   PetscCall(MatSetSizes(Pd,PETSC_DECIDE,PETSC_DECIDE,n,n));
 93:   PetscCall(MatSetFromOptions(Pd));

 95:   PetscCall(MatGetOwnershipRange(Pa,&Istart,&Iend));
 96:   for (i=Istart;i<Iend;i++) {
 97:     PetscCall(MatSetValue(Pa,i,i,2.0,INSERT_VALUES));
 98:     if (i>0) PetscCall(MatSetValue(Pb,i,i,(PetscScalar)i,INSERT_VALUES));
 99:     else PetscCall(MatSetValue(Pb,i,i,-1.0,INSERT_VALUES));
100:     PetscCall(MatSetValue(Pd,i,i,i*.1,INSERT_VALUES));
101:   }

103:   PetscCall(MatAssemblyBegin(Pa,MAT_FINAL_ASSEMBLY));
104:   PetscCall(MatAssemblyEnd(Pa,MAT_FINAL_ASSEMBLY));
105:   PetscCall(MatAssemblyBegin(Pb,MAT_FINAL_ASSEMBLY));
106:   PetscCall(MatAssemblyEnd(Pb,MAT_FINAL_ASSEMBLY));
107:   PetscCall(MatAssemblyBegin(Pc,MAT_FINAL_ASSEMBLY));
108:   PetscCall(MatAssemblyEnd(Pc,MAT_FINAL_ASSEMBLY));
109:   PetscCall(MatAssemblyBegin(Pd,MAT_FINAL_ASSEMBLY));
110:   PetscCall(MatAssemblyEnd(Pd,MAT_FINAL_ASSEMBLY));

112:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
113:                 Create the spectral transformation object
114:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

116:   PetscCall(STCreate(PETSC_COMM_WORLD,&st));
117:   mat[0] = A;
118:   mat[1] = B;
119:   mat[2] = C;
120:   mat[3] = D;
121:   PetscCall(STSetMatrices(st,4,mat));
122:   mat[0] = Pa;
123:   mat[1] = Pb;
124:   mat[2] = Pc;
125:   mat[3] = Pd;
126:   PetscCall(STSetSplitPreconditioner(st,4,mat,SUBSET_NONZERO_PATTERN));
127:   PetscCall(STGetKSP(st,&ksp));
128:   PetscCall(KSPSetTolerances(ksp,100*PETSC_MACHINE_EPSILON,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT));
129:   PetscCall(STSetTransform(st,PETSC_TRUE));
130:   PetscCall(STSetFromOptions(st));
131:   PetscCall(STGetKSP(st,&ksp));
132:   PetscCall(KSPGetPC(ksp,&pc));

134:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
135:                    Apply the operator
136:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

138:   /* sigma=0.0 */
139:   PetscCall(STSetUp(st));
140:   PetscCall(STGetType(st,&type));
141:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"ST type %s\n",type));
142:   PetscCall(PCGetOperators(pc,NULL,&Pmat));
143:   PetscCall(MatView(Pmat,NULL));
144:   PetscCall(STMatSolve(st,v,w));
145:   PetscCall(VecView(w,NULL));

147:   /* sigma=0.1 */
148:   sigma = 0.1;
149:   PetscCall(STSetShift(st,sigma));
150:   PetscCall(STGetShift(st,&sigma));
151:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"With shift=%g\n",(double)PetscRealPart(sigma)));
152:   PetscCall(PCGetOperators(pc,NULL,&Pmat));
153:   PetscCall(MatView(Pmat,NULL));
154:   PetscCall(STMatSolve(st,v,w));
155:   PetscCall(VecView(w,NULL));

157:   PetscCall(STDestroy(&st));
158:   PetscCall(MatDestroy(&A));
159:   PetscCall(MatDestroy(&B));
160:   PetscCall(MatDestroy(&C));
161:   PetscCall(MatDestroy(&D));
162:   PetscCall(MatDestroy(&Pa));
163:   PetscCall(MatDestroy(&Pb));
164:   PetscCall(MatDestroy(&Pc));
165:   PetscCall(MatDestroy(&Pd));
166:   PetscCall(VecDestroy(&v));
167:   PetscCall(VecDestroy(&w));
168:   PetscCall(SlepcFinalize());
169:   return 0;
170: }

172: /*TEST

174:    test:
175:       suffix: 1
176:       args: -st_type {{shift sinvert}separate output} -st_pc_type jacobi
177:       requires: !single

179: TEST*/