Actual source code: ex4.c

petsc-master 2019-07-15
Report Typos and Errors

  2: static char help[] = "Solves a linear system in parallel with KSP and HMG.\n\
  3: Input parameters include:\n\
  4:   -view_exact_sol    : write exact solution vector to stdout\n\
  5:   -m  <mesh_x>       : number of mesh points in x-direction\n\
  6:   -n  <mesh_n>       : number of mesh points in y-direction\n\
  7:   -bs                : number of variables on each mesh vertex \n\n";

  9: /*
 10:   Simple example is used to test PCHMG
 11: */
 12:  #include <petscksp.h>

 14: int main(int argc,char **args)
 15: {
 16:   Vec            x,b,u;    /* approx solution, RHS, exact solution */
 17:   Mat            A;        /* linear system matrix */
 18:   KSP            ksp;      /* linear solver context */
 19:   PetscReal      norm;     /* norm of solution error */
 20:   PetscInt       i,j,Ii,J,Istart,Iend,m = 8,n = 7,its,bs=1,II,JJ,jj;
 22:   PetscBool      flg,test=PETSC_FALSE,reuse=PETSC_FALSE;
 23:   PetscScalar    v;
 24:   PC             pc;

 26:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 27:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
 28:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 29:   PetscOptionsGetInt(NULL,NULL,"-bs",&bs,NULL);
 30:   PetscOptionsGetBool(NULL,NULL,"-test_hmg_interface",&test,NULL);
 31:   PetscOptionsGetBool(NULL,NULL,"-test_reuse_interpolation",&reuse,NULL);

 33:   MatCreate(PETSC_COMM_WORLD,&A);
 34:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m*n*bs,m*n*bs);
 35:   MatSetBlockSize(A,bs);
 36:   MatSetFromOptions(A);
 37:   MatMPIAIJSetPreallocation(A,5,NULL,5,NULL);
 38:   MatSeqAIJSetPreallocation(A,5,NULL);

 40:   MatGetOwnershipRange(A,&Istart,&Iend);

 42:   for (Ii=Istart/bs; Ii<Iend/bs; Ii++) {
 43:     v = -1.0; i = Ii/n; j = Ii - i*n;
 44:     if (i>0) {
 45:       J = Ii - n;
 46:       for (jj=0; jj<bs; jj++) {
 47:         II = Ii*bs + jj;
 48:         JJ = J*bs + jj;
 49:         MatSetValues(A,1,&II,1,&JJ,&v,ADD_VALUES);
 50:       }
 51:     }
 52:     if (i<m-1) {
 53:       J = Ii + n;
 54:       for (jj=0; jj<bs; jj++) {
 55:         II = Ii*bs + jj;
 56:         JJ = J*bs + jj;
 57:         MatSetValues(A,1,&II,1,&JJ,&v,ADD_VALUES);
 58:       }
 59:     }
 60:     if (j>0) {
 61:       J = Ii - 1;
 62:       for (jj=0; jj<bs; jj++) {
 63:         II = Ii*bs + jj;
 64:         JJ = J*bs + jj;
 65:         MatSetValues(A,1,&II,1,&JJ,&v,ADD_VALUES);
 66:       }
 67:     }
 68:     if (j<n-1) {
 69:       J = Ii + 1;
 70:       for (jj=0; jj<bs; jj++) {
 71:         II = Ii*bs + jj;
 72:         JJ = J*bs + jj;
 73:         MatSetValues(A,1,&II,1,&JJ,&v,ADD_VALUES);
 74:       }
 75:     }
 76:     v = 4.0;
 77:     for (jj=0; jj<bs; jj++) {
 78:       II = Ii*bs + jj;
 79:       MatSetValues(A,1,&II,1,&II,&v,ADD_VALUES);
 80:     }
 81:   }

 83:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 84:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 86:   MatCreateVecs(A,&u,NULL);
 87:   VecDuplicate(u,&b);
 88:   VecDuplicate(b,&x);

 90:   VecSet(u,1.0);
 91:   MatMult(A,u,b);

 93:   flg  = PETSC_FALSE;
 94:   PetscOptionsGetBool(NULL,NULL,"-view_exact_sol",&flg,NULL);
 95:   if (flg) {VecView(u,PETSC_VIEWER_STDOUT_WORLD);}

 97:   KSPCreate(PETSC_COMM_WORLD,&ksp);
 98:   KSPSetOperators(ksp,A,A);
 99:   KSPSetTolerances(ksp,1.e-2/((m+1)*(n+1)),1.e-50,PETSC_DEFAULT,PETSC_DEFAULT);

101:   if (test) {
102:     KSPGetPC(ksp,&pc);
103:     PCSetType(pc,PCHMG);
104:     PCHMGSetInnerPCType(pc,PCGAMG);
105:     PCHMGSetReuseInterpolation(pc,PETSC_TRUE);
106:     PCHMGSetUseSubspaceCoarsening(pc,PETSC_TRUE);
107:   }

109:   KSPSetFromOptions(ksp);
110:   KSPSolve(ksp,b,x);

112:   if (reuse) {
113:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
114:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
115:     KSPSolve(ksp,b,x);
116:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
117:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
118:     KSPSolve(ksp,b,x);
119:     /* Make sparsity pattern different and reuse interpolation */
120:     MatSetOption(A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);
121:     MatSetOption(A,MAT_IGNORE_ZERO_ENTRIES,PETSC_FALSE);
122:     MatGetSize(A,&m,NULL);
123:     n = 0;
124:     v = 0;
125:     m--;
126:     /* Connect the last element to the first element */
127:     MatSetValue(A,m,n,v,ADD_VALUES);
128:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
129:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
130:     KSPSolve(ksp,b,x);
131:   }

133:   VecAXPY(x,-1.0,u);
134:   VecNorm(x,NORM_2,&norm);
135:   KSPGetIterationNumber(ksp,&its);

137:   PetscPrintf(PETSC_COMM_WORLD,"Norm of error %g iterations %D\n",(double)norm,its);

139:   KSPDestroy(&ksp);
140:   VecDestroy(&u);
141:   VecDestroy(&x);
142:   VecDestroy(&b);
143:   MatDestroy(&A);

145:   PetscFinalize();
146:   return ierr;
147: }

149: /*TEST

151:    build:
152:       requires: !complex !single

154:    test:
155:       suffix: hypre
156:       nsize: 2
157:       requires: hypre
158:       args: -ksp_monitor -pc_type hmg -ksp_rtol 1e-6 -hmg_inner_pc_type hypre

160:    test:
161:       suffix: hypre_bs4
162:       nsize: 2
163:       requires: hypre
164:       args: -ksp_monitor -pc_type hmg -ksp_rtol 1e-6 -hmg_inner_pc_type hypre -bs 4 -pc_hmg_use_subspace_coarsening 1

166:    test:
167:       suffix: hypre_asm
168:       nsize: 2
169:       requires: hypre
170:       args: -ksp_monitor -pc_type hmg -ksp_rtol 1e-6 -hmg_inner_pc_type hypre -bs 4 -pc_hmg_use_subspace_coarsening 1 -mg_levels_3_pc_type asm

172:    test:
173:       suffix: hypre_fieldsplit
174:       nsize: 2
175:       requires: hypre
176:       args: -ksp_monitor -pc_type hmg -ksp_rtol 1e-6 -hmg_inner_pc_type hypre -bs 4 -mg_levels_4_pc_type fieldsplit

178:    test:
179:       suffix: gamg
180:       nsize: 2
181:       args: -ksp_monitor -pc_type hmg -ksp_rtol 1e-6 -hmg_inner_pc_type gamg

183:    test:
184:       suffix: gamg_bs4
185:       nsize: 2
186:       args: -ksp_monitor -pc_type hmg -ksp_rtol 1e-6 -hmg_inner_pc_type gamg -bs 4 -pc_hmg_use_subspace_coarsening 1

188:    test:
189:       suffix: gamg_asm
190:       nsize: 2
191:       args: -ksp_monitor -pc_type hmg -ksp_rtol 1e-6 -hmg_inner_pc_type gamg -bs 4 -pc_hmg_use_subspace_coarsening 1 -mg_levels_1_pc_type asm

193:    test:
194:       suffix: gamg_fieldsplit
195:       nsize: 2
196:       args: -ksp_monitor -pc_type hmg -ksp_rtol 1e-6 -hmg_inner_pc_type gamg -bs 4 -mg_levels_1_pc_type fieldsplit

198:    test:
199:       suffix: interface
200:       nsize: 2
201:       args: -ksp_monitor -ksp_rtol 1e-6 -test_hmg_interface 1 -bs 4

203:    test:
204:       suffix: reuse
205:       nsize: 2
206:       args: -ksp_monitor -ksp_rtol 1e-6   -pc_type hmg -pc_hmg_reuse_interpolation 1 -test_reuse_interpolation 1 -hmg_inner_pc_type gamg

208: TEST*/