Date: 2023-01-26T12:18:24+00:00

Actual source code: test7.c
slepc-3.18.2 2023-01-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 one matrix and split preconditioner.\n\n";

     13: #include <slepcst.h>

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

     28:   SlepcInitialize(&argc,&argv,(char*)0,help);
     29:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
     30:   PetscPrintf(PETSC_COMM_WORLD,"\n1-D Laplacian, n=%" PetscInt_FMT "\n\n",n);

     32:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     33:      Compute the operator matrix for the 1-D Laplacian
     34:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     36:   MatCreate(PETSC_COMM_WORLD,&A);
     37:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);
     38:   MatSetFromOptions(A);
     39:   MatSetUp(A);

     41:   MatGetOwnershipRange(A,&Istart,&Iend);
     42:   for (i=Istart;i<Iend;i++) {
     43:     if (i>0) MatSetValue(A,i,i-1,-1.0,INSERT_VALUES);
     44:     if (i<n-1) MatSetValue(A,i,i+1,-1.0,INSERT_VALUES);
     45:     MatSetValue(A,i,i,2.0,INSERT_VALUES);
     46:   }
     47:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
     48:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
     49:   MatCreateVecs(A,&v,&w);
     50:   VecSet(v,1.0);

     52:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     53:      Compute the split preconditioner matrix (one diagonal)
     54:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     56:   MatCreate(PETSC_COMM_WORLD,&Pa);
     57:   MatSetSizes(Pa,PETSC_DECIDE,PETSC_DECIDE,n,n);
     58:   MatSetFromOptions(Pa);
     59:   MatSetUp(Pa);

     61:   MatGetOwnershipRange(Pa,&Istart,&Iend);
     62:   for (i=Istart;i<Iend;i++) MatSetValue(Pa,i,i,2.0,INSERT_VALUES);
     63:   MatAssemblyBegin(Pa,MAT_FINAL_ASSEMBLY);
     64:   MatAssemblyEnd(Pa,MAT_FINAL_ASSEMBLY);

     66:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     67:                 Create the spectral transformation object
     68:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     70:   STCreate(PETSC_COMM_WORLD,&st);
     71:   mat[0] = A;
     72:   STSetMatrices(st,1,mat);
     73:   mat[0] = Pa;
     74:   STSetSplitPreconditioner(st,1,mat,SAME_NONZERO_PATTERN);
     75:   STSetTransform(st,PETSC_TRUE);
     76:   STSetFromOptions(st);
     77:   STCayleySetAntishift(st,-0.3);   /* only relevant for cayley */

     79:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     80:                Form the preconditioner matrix and print it
     81:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     83:   PetscObjectTypeCompareAny((PetscObject)st,&flg,STSINVERT,STCAYLEY,"");
     84:   if (flg) {
     85:     STGetKSP(st,&ksp);
     86:     KSPGetPC(ksp,&pc);
     87:     STGetOperator(st,NULL);
     88:     PCGetOperators(pc,NULL,&Pmat);
     89:     MatView(Pmat,NULL);
     90:   }

     92:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     93:                     Apply the operator
     94:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     96:   /* sigma=0.0 */
     97:   STSetUp(st);
     98:   STGetType(st,&type);
     99:   PetscPrintf(PETSC_COMM_WORLD,"ST type %s\n",type);
    100:   STApply(st,v,w);
    101:   VecView(w,NULL);

    103:   /* sigma=0.1 */
    104:   sigma = 0.1;
    105:   STSetShift(st,sigma);
    106:   STGetShift(st,&sigma);
    107:   PetscPrintf(PETSC_COMM_WORLD,"With shift=%g\n",(double)PetscRealPart(sigma));
    108:   if (flg) {
    109:     STGetOperator(st,NULL);
    110:     PCGetOperators(pc,NULL,&Pmat);
    111:     MatView(Pmat,NULL);
    112:   }
    113:   STApply(st,v,w);
    114:   VecView(w,NULL);

    116:   STDestroy(&st);
    117:   MatDestroy(&A);
    118:   MatDestroy(&Pa);
    119:   VecDestroy(&v);
    120:   VecDestroy(&w);
    121:   SlepcFinalize();
    122:   return 0;
    123: }

    125: /*TEST

    127:    test:
    128:       suffix: 1
    129:       args: -st_type {{cayley shift sinvert}separate output}
    130:       requires: !single

    132: TEST*/