Actual source code: pcis.c

  1: #define PETSCKSP_DLL

 3:  #include ../src/ksp/pc/impls/is/pcis.h

  5: /* -------------------------------------------------------------------------- */
  6: /*
  7:    PCISSetUp - 
  8: */
 11: PetscErrorCode  PCISSetUp(PC pc)
 12: {
 13:   PC_IS           *pcis = (PC_IS*)(pc->data);
 14:   Mat_IS          *matis = (Mat_IS*)pc->mat->data;
 15:   PetscInt        i;
 16:   PetscErrorCode  ierr;
 17:   PetscTruth      flg;
 18: 
 20:   PetscTypeCompare((PetscObject)pc->mat,MATIS,&flg);
 21:   if (!flg){
 22:     SETERRQ(PETSC_ERR_ARG_WRONG,"Preconditioner type of Neumann Neumman requires matrix of type MATIS");
 23:   }

 25:   pcis->pure_neumann = matis->pure_neumann;

 27:   /*
 28:     Creating the local vector vec1_N, containing the inverse of the number
 29:     of subdomains to which each local node (either owned or ghost)
 30:     pertains. To accomplish that, we scatter local vectors of 1's to
 31:     a global vector (adding the values); scatter the result back to
 32:     local vectors and finally invert the result.
 33:   */
 34:   {
 35:     Vec    counter;
 36:     VecDuplicate(matis->x,&pcis->vec1_N);
 37:     MatGetVecs(pc->pmat,&counter,0); /* temporary auxiliar vector */
 38:     VecSet(counter,0.0);
 39:     VecSet(pcis->vec1_N,1.0);
 40:     VecScatterBegin(matis->ctx,pcis->vec1_N,counter,ADD_VALUES,SCATTER_REVERSE);
 41:     VecScatterEnd  (matis->ctx,pcis->vec1_N,counter,ADD_VALUES,SCATTER_REVERSE);
 42:     VecScatterBegin(matis->ctx,counter,pcis->vec1_N,INSERT_VALUES,SCATTER_FORWARD);
 43:     VecScatterEnd  (matis->ctx,counter,pcis->vec1_N,INSERT_VALUES,SCATTER_FORWARD);
 44:     VecDestroy(counter);
 45:   }
 46:   /*
 47:     Creating local and global index sets for interior and
 48:     inteface nodes. Notice that interior nodes have D[i]==1.0.
 49:   */
 50:   {
 51:     PetscInt     n_I;
 52:     PetscInt    *idx_I_local,*idx_B_local,*idx_I_global,*idx_B_global;
 53:     PetscScalar *array;
 54:     /* Identifying interior and interface nodes, in local numbering */
 55:     VecGetSize(pcis->vec1_N,&pcis->n);
 56:     VecGetArray(pcis->vec1_N,&array);
 57:     PetscMalloc(pcis->n*sizeof(PetscInt),&idx_I_local);
 58:     PetscMalloc(pcis->n*sizeof(PetscInt),&idx_B_local);
 59:     for (i=0, pcis->n_B=0, n_I=0; i<pcis->n; i++) {
 60:       if (array[i] == 1.0) { idx_I_local[n_I]       = i; n_I++;       }
 61:       else                 { idx_B_local[pcis->n_B] = i; pcis->n_B++; }
 62:     }
 63:     /* Getting the global numbering */
 64:     idx_B_global = idx_I_local + n_I; /* Just avoiding allocating extra memory, since we have vacant space */
 65:     idx_I_global = idx_B_local + pcis->n_B;
 66:     ISLocalToGlobalMappingApply(matis->mapping,pcis->n_B,idx_B_local,idx_B_global);
 67:     ISLocalToGlobalMappingApply(matis->mapping,n_I,      idx_I_local,idx_I_global);
 68:     /* Creating the index sets. */
 69:     ISCreateGeneral(MPI_COMM_SELF,pcis->n_B,idx_B_local, &pcis->is_B_local);
 70:     ISCreateGeneral(MPI_COMM_SELF,pcis->n_B,idx_B_global,&pcis->is_B_global);
 71:     ISCreateGeneral(MPI_COMM_SELF,n_I      ,idx_I_local, &pcis->is_I_local);
 72:     ISCreateGeneral(MPI_COMM_SELF,n_I      ,idx_I_global,&pcis->is_I_global);
 73:     /* Freeing memory and restoring arrays */
 74:     PetscFree(idx_B_local);
 75:     PetscFree(idx_I_local);
 76:     VecRestoreArray(pcis->vec1_N,&array);
 77:   }

 79:   /*
 80:     Extracting the blocks A_II, A_BI, A_IB and A_BB from A. If the numbering
 81:     is such that interior nodes come first than the interface ones, we have

 83:     [           |      ]
 84:     [    A_II   | A_IB ]
 85:     A = [           |      ]
 86:     [-----------+------]
 87:     [    A_BI   | A_BB ]
 88:   */

 90:   MatGetSubMatrix(matis->A,pcis->is_I_local,pcis->is_I_local,MAT_INITIAL_MATRIX,&pcis->A_II);
 91:   MatGetSubMatrix(matis->A,pcis->is_I_local,pcis->is_B_local,MAT_INITIAL_MATRIX,&pcis->A_IB);
 92:   MatGetSubMatrix(matis->A,pcis->is_B_local,pcis->is_I_local,MAT_INITIAL_MATRIX,&pcis->A_BI);
 93:   MatGetSubMatrix(matis->A,pcis->is_B_local,pcis->is_B_local,MAT_INITIAL_MATRIX,&pcis->A_BB);

 95:   /*
 96:     Creating work vectors and arrays
 97:   */
 98:   /* pcis->vec1_N has already been created */
 99:   VecDuplicate(pcis->vec1_N,&pcis->vec2_N);
100:   VecCreateSeq(PETSC_COMM_SELF,pcis->n-pcis->n_B,&pcis->vec1_D);
101:   VecDuplicate(pcis->vec1_D,&pcis->vec2_D);
102:   VecDuplicate(pcis->vec1_D,&pcis->vec3_D);
103:   VecCreateSeq(PETSC_COMM_SELF,pcis->n_B,&pcis->vec1_B);
104:   VecDuplicate(pcis->vec1_B,&pcis->vec2_B);
105:   VecDuplicate(pcis->vec1_B,&pcis->vec3_B);
106:   MatGetVecs(pc->pmat,&pcis->vec1_global,0);
107:   PetscMalloc((pcis->n)*sizeof(PetscScalar),&pcis->work_N);

109:   /* Creating the scatter contexts */
110:   VecScatterCreate(pcis->vec1_global,pcis->is_I_global,pcis->vec1_D,(IS)0,&pcis->global_to_D);
111:   VecScatterCreate(pcis->vec1_N,pcis->is_B_local,pcis->vec1_B,(IS)0,&pcis->N_to_B);
112:   VecScatterCreate(pcis->vec1_global,pcis->is_B_global,pcis->vec1_B,(IS)0,&pcis->global_to_B);

114:   /* Creating scaling "matrix" D, from information in vec1_N */
115:   VecDuplicate(pcis->vec1_B,&pcis->D);
116:   VecScatterBegin(pcis->N_to_B,pcis->vec1_N,pcis->D,INSERT_VALUES,SCATTER_FORWARD);
117:   VecScatterEnd  (pcis->N_to_B,pcis->vec1_N,pcis->D,INSERT_VALUES,SCATTER_FORWARD);
118:   VecReciprocal(pcis->D);

120:   /* See historical note 01, at the bottom of this file. */

122:   /*
123:     Creating the KSP contexts for the local Dirichlet and Neumann problems.
124:   */
125:   {
126:     PC  pc_ctx;
127:     /* Dirichlet */
128:     KSPCreate(PETSC_COMM_SELF,&pcis->ksp_D);
129:     PetscObjectIncrementTabLevel((PetscObject)pcis->ksp_D,(PetscObject)pc,1);
130:     KSPSetOperators(pcis->ksp_D,pcis->A_II,pcis->A_II,SAME_PRECONDITIONER);
131:     KSPSetOptionsPrefix(pcis->ksp_D,"is_localD_");
132:     KSPGetPC(pcis->ksp_D,&pc_ctx);
133:     PCSetType(pc_ctx,PCLU);
134:     KSPSetType(pcis->ksp_D,KSPPREONLY);
135:     KSPSetFromOptions(pcis->ksp_D);
136:     /* the vectors in the following line are dummy arguments, just telling the KSP the vector size. Values are not used */
137:     KSPSetUp(pcis->ksp_D);
138:     /* Neumann */
139:     KSPCreate(PETSC_COMM_SELF,&pcis->ksp_N);
140:     PetscObjectIncrementTabLevel((PetscObject)pcis->ksp_N,(PetscObject)pc,1);
141:     KSPSetOperators(pcis->ksp_N,matis->A,matis->A,SAME_PRECONDITIONER);
142:     KSPSetOptionsPrefix(pcis->ksp_N,"is_localN_");
143:     KSPGetPC(pcis->ksp_N,&pc_ctx);
144:     PCSetType(pc_ctx,PCLU);
145:     KSPSetType(pcis->ksp_N,KSPPREONLY);
146:     KSPSetFromOptions(pcis->ksp_N);
147:     {
148:       PetscTruth damp_fixed = PETSC_FALSE,
149:                  remove_nullspace_fixed = PETSC_FALSE,
150:                  set_damping_factor_floating = PETSC_FALSE,
151:                  not_damp_floating = PETSC_FALSE,
152:                  not_remove_nullspace_floating = PETSC_FALSE;
153:       PetscReal  fixed_factor,
154:                  floating_factor;

156:       PetscOptionsGetReal(((PetscObject)pc_ctx)->prefix,"-pc_is_damp_fixed",&fixed_factor,&damp_fixed);
157:       if (!damp_fixed) { fixed_factor = 0.0; }
158:       PetscOptionsGetTruth(((PetscObject)pc_ctx)->prefix,"-pc_is_damp_fixed",&damp_fixed,PETSC_NULL);

160:       PetscOptionsGetTruth(((PetscObject)pc_ctx)->prefix,"-pc_is_remove_nullspace_fixed",&remove_nullspace_fixed,PETSC_NULL);

162:       PetscOptionsGetReal(((PetscObject)pc_ctx)->prefix,"-pc_is_set_damping_factor_floating",
163:                               &floating_factor,&set_damping_factor_floating);
164:       if (!set_damping_factor_floating) { floating_factor = 0.0; }
165:       PetscOptionsGetTruth(((PetscObject)pc_ctx)->prefix,"-pc_is_set_damping_factor_floating",&set_damping_factor_floating,PETSC_NULL);
166:       if (!set_damping_factor_floating) { floating_factor = 1.e-12; }

168:       PetscOptionsGetTruth(((PetscObject)pc_ctx)->prefix,"-pc_is_not_damp_floating",&not_damp_floating,PETSC_NULL);

170:       PetscOptionsGetTruth(((PetscObject)pc_ctx)->prefix,"-pc_is_not_remove_nullspace_floating",&not_remove_nullspace_floating,PETSC_NULL);

172:       if (pcis->pure_neumann) {  /* floating subdomain */
173:         if (!(not_damp_floating)) {
174:           PCFactorSetShiftType(pc_ctx,MAT_SHIFT_NONZERO);
175:           PCFactorSetShiftAmount(pc_ctx,floating_factor);
176:         }
177:         if (!(not_remove_nullspace_floating)){
178:           MatNullSpace nullsp;
179:           MatNullSpaceCreate(PETSC_COMM_SELF,PETSC_TRUE,0,PETSC_NULL,&nullsp);
180:           KSPSetNullSpace(pcis->ksp_N,nullsp);
181:           MatNullSpaceDestroy(nullsp);
182:         }
183:       } else {  /* fixed subdomain */
184:         if (damp_fixed) {
185:           PCFactorSetShiftType(pc_ctx,MAT_SHIFT_NONZERO);
186:           PCFactorSetShiftAmount(pc_ctx,floating_factor);
187:         }
188:         if (remove_nullspace_fixed) {
189:           MatNullSpace nullsp;
190:           MatNullSpaceCreate(PETSC_COMM_SELF,PETSC_TRUE,0,PETSC_NULL,&nullsp);
191:           KSPSetNullSpace(pcis->ksp_N,nullsp);
192:           MatNullSpaceDestroy(nullsp);
193:         }
194:       }
195:     }
196:     /* the vectors in the following line are dummy arguments, just telling the KSP the vector size. Values are not used */
197:     KSPSetUp(pcis->ksp_N);
198:   }

200:   ISLocalToGlobalMappingGetInfo(((Mat_IS*)(pc->mat->data))->mapping,&(pcis->n_neigh),&(pcis->neigh),&(pcis->n_shared),&(pcis->shared));
201:   pcis->ISLocalToGlobalMappingGetInfoWasCalled = PETSC_TRUE;
202:   return(0);
203: }

205: /* -------------------------------------------------------------------------- */
206: /*
207:    PCISDestroy -
208: */
211: PetscErrorCode  PCISDestroy(PC pc)
212: {
213:   PC_IS          *pcis = (PC_IS*)(pc->data);

217:   if (pcis->is_B_local)  {ISDestroy(pcis->is_B_local);}
218:   if (pcis->is_I_local)  {ISDestroy(pcis->is_I_local);}
219:   if (pcis->is_B_global) {ISDestroy(pcis->is_B_global);}
220:   if (pcis->is_I_global) {ISDestroy(pcis->is_I_global);}
221:   if (pcis->A_II)        {MatDestroy(pcis->A_II);}
222:   if (pcis->A_IB)        {MatDestroy(pcis->A_IB);}
223:   if (pcis->A_BI)        {MatDestroy(pcis->A_BI);}
224:   if (pcis->A_BB)        {MatDestroy(pcis->A_BB);}
225:   if (pcis->D)           {VecDestroy(pcis->D);}
226:   if (pcis->ksp_N)      {KSPDestroy(pcis->ksp_N);}
227:   if (pcis->ksp_D)      {KSPDestroy(pcis->ksp_D);}
228:   if (pcis->vec1_N)      {VecDestroy(pcis->vec1_N);}
229:   if (pcis->vec2_N)      {VecDestroy(pcis->vec2_N);}
230:   if (pcis->vec1_D)      {VecDestroy(pcis->vec1_D);}
231:   if (pcis->vec2_D)      {VecDestroy(pcis->vec2_D);}
232:   if (pcis->vec3_D)      {VecDestroy(pcis->vec3_D);}
233:   if (pcis->vec1_B)      {VecDestroy(pcis->vec1_B);}
234:   if (pcis->vec2_B)      {VecDestroy(pcis->vec2_B);}
235:   if (pcis->vec3_B)      {VecDestroy(pcis->vec3_B);}
236:   if (pcis->vec1_global) {VecDestroy(pcis->vec1_global);}
237:   if (pcis->global_to_D) {VecScatterDestroy(pcis->global_to_D);}
238:   if (pcis->N_to_B)      {VecScatterDestroy(pcis->N_to_B);}
239:   if (pcis->global_to_B) {VecScatterDestroy(pcis->global_to_B);}
240:   PetscFree(pcis->work_N);
241:   if (pcis->ISLocalToGlobalMappingGetInfoWasCalled) {
242:     ISLocalToGlobalMappingRestoreInfo((ISLocalToGlobalMapping)0,&(pcis->n_neigh),&(pcis->neigh),&(pcis->n_shared),&(pcis->shared));
243:   }
244:   return(0);
245: }

247: /* -------------------------------------------------------------------------- */
248: /*
249:    PCISCreate - 
250: */
253: PetscErrorCode  PCISCreate(PC pc)
254: {
255:   PC_IS *pcis = (PC_IS*)(pc->data);

258:   pcis->is_B_local  = 0;
259:   pcis->is_I_local  = 0;
260:   pcis->is_B_global = 0;
261:   pcis->is_I_global = 0;
262:   pcis->A_II        = 0;
263:   pcis->A_IB        = 0;
264:   pcis->A_BI        = 0;
265:   pcis->A_BB        = 0;
266:   pcis->D           = 0;
267:   pcis->ksp_N      = 0;
268:   pcis->ksp_D      = 0;
269:   pcis->vec1_N      = 0;
270:   pcis->vec2_N      = 0;
271:   pcis->vec1_D      = 0;
272:   pcis->vec2_D      = 0;
273:   pcis->vec3_D      = 0;
274:   pcis->vec1_B      = 0;
275:   pcis->vec2_B      = 0;
276:   pcis->vec3_B      = 0;
277:   pcis->vec1_global = 0;
278:   pcis->work_N      = 0;
279:   pcis->global_to_D = 0;
280:   pcis->N_to_B      = 0;
281:   pcis->global_to_B = 0;
282:   pcis->ISLocalToGlobalMappingGetInfoWasCalled = PETSC_FALSE;
283:   return(0);
284: }

286: /* -------------------------------------------------------------------------- */
287: /*
288:    PCISApplySchur -

290:    Input parameters:
291: .  pc - preconditioner context
292: .  v - vector to which the Schur complement is to be applied (it is NOT modified inside this function, UNLESS vec2_B is null)

294:    Output parameters:
295: .  vec1_B - result of Schur complement applied to chunk
296: .  vec2_B - garbage (used as work space), or null (and v is used as workspace)
297: .  vec1_D - garbage (used as work space)
298: .  vec2_D - garbage (used as work space)

300: */
303: PetscErrorCode  PCISApplySchur(PC pc, Vec v, Vec vec1_B, Vec vec2_B, Vec vec1_D, Vec vec2_D)
304: {
306:   PC_IS          *pcis = (PC_IS*)(pc->data);

309:   if (!vec2_B) { vec2_B = v; }

311:   MatMult(pcis->A_BB,v,vec1_B);
312:   MatMult(pcis->A_IB,v,vec1_D);
313:   KSPSolve(pcis->ksp_D,vec1_D,vec2_D);
314:   MatMult(pcis->A_BI,vec2_D,vec2_B);
315:   VecAXPY(vec1_B,-1.0,vec2_B);
316:   return(0);
317: }

319: /* -------------------------------------------------------------------------- */
320: /*
321:    PCISScatterArrayNToVecB - Scatters interface node values from a big array (of all local nodes, interior or interface,
322:    including ghosts) into an interface vector, when in SCATTER_FORWARD mode, or vice-versa, when in SCATTER_REVERSE
323:    mode.

325:    Input parameters:
326: .  pc - preconditioner context
327: .  array_N - [when in SCATTER_FORWARD mode] Array to be scattered into the vector
328: .  v_B - [when in SCATTER_REVERSE mode] Vector to be scattered into the array

330:    Output parameter:
331: .  array_N - [when in SCATTER_REVERSE mode] Array to receive the scattered vector
332: .  v_B - [when in SCATTER_FORWARD mode] Vector to receive the scattered array

334:    Notes:
335:    The entries in the array that do not correspond to interface nodes remain unaltered.
336: */
339: PetscErrorCode  PCISScatterArrayNToVecB (PetscScalar *array_N, Vec v_B, InsertMode imode, ScatterMode smode, PC pc)
340: {
341:   PetscInt       i;
342:   const PetscInt *idex;
344:   PetscScalar    *array_B;
345:   PC_IS          *pcis = (PC_IS*)(pc->data);

348:   VecGetArray(v_B,&array_B);
349:   ISGetIndices(pcis->is_B_local,&idex);

351:   if (smode == SCATTER_FORWARD) {
352:     if (imode == INSERT_VALUES) {
353:       for (i=0; i<pcis->n_B; i++) { array_B[i]  = array_N[idex[i]]; }
354:     } else {  /* ADD_VALUES */
355:       for (i=0; i<pcis->n_B; i++) { array_B[i] += array_N[idex[i]]; }
356:     }
357:   } else {  /* SCATTER_REVERSE */
358:     if (imode == INSERT_VALUES) {
359:       for (i=0; i<pcis->n_B; i++) { array_N[idex[i]]  = array_B[i]; }
360:     } else {  /* ADD_VALUES */
361:       for (i=0; i<pcis->n_B; i++) { array_N[idex[i]] += array_B[i]; }
362:     }
363:   }
364:   ISRestoreIndices(pcis->is_B_local,&idex);
365:   VecRestoreArray(v_B,&array_B);
366:   return(0);
367: }

369: /* -------------------------------------------------------------------------- */
370: /*
371:    PCISApplyInvSchur - Solves the Neumann problem related to applying the inverse of the Schur complement.
372:    More precisely, solves the problem:
373:                                         [ A_II  A_IB ] [ . ]   [ 0 ]
374:                                         [            ] [   ] = [   ]
375:                                         [ A_BI  A_BB ] [ x ]   [ b ]

377:    Input parameters:
378: .  pc - preconditioner context
379: .  b - vector of local interface nodes (including ghosts)

381:    Output parameters:
382: .  x - vector of local interface nodes (including ghosts); returns the application of the inverse of the Schur
383:        complement to b
384: .  vec1_N - vector of local nodes (interior and interface, including ghosts); returns garbage (used as work space)
385: .  vec2_N - vector of local nodes (interior and interface, including ghosts); returns garbage (used as work space)

387: */
390: PetscErrorCode  PCISApplyInvSchur (PC pc, Vec b, Vec x, Vec vec1_N, Vec vec2_N)
391: {
393:   PC_IS          *pcis = (PC_IS*)(pc->data);

396:   /*
397:     Neumann solvers. 
398:     Applying the inverse of the local Schur complement, i.e, solving a Neumann
399:     Problem with zero at the interior nodes of the RHS and extracting the interface
400:     part of the solution. inverse Schur complement is applied to b and the result
401:     is stored in x.
402:   */
403:   /* Setting the RHS vec1_N */
404:   VecSet(vec1_N,0.0);
405:   VecScatterBegin(pcis->N_to_B,b,vec1_N,INSERT_VALUES,SCATTER_REVERSE);
406:   VecScatterEnd  (pcis->N_to_B,b,vec1_N,INSERT_VALUES,SCATTER_REVERSE);
407:   /* Checking for consistency of the RHS */
408:   {
409:     PetscTruth flg = PETSC_FALSE;
410:     PetscOptionsGetTruth(PETSC_NULL,"-pc_is_check_consistency",&flg,PETSC_NULL);
411:     if (flg) {
412:       PetscScalar average;
413:       PetscViewer viewer;
414:       PetscViewerASCIIGetStdout(((PetscObject)pc)->comm,&viewer);

416:       VecSum(vec1_N,&average);
417:       average = average / ((PetscReal)pcis->n);
418:       if (pcis->pure_neumann) {

420:         PetscViewerASCIISynchronizedPrintf(viewer,"Subdomain %04d is floating. Average = % 1.14e\n",
421:                                              PetscGlobalRank,PetscAbsScalar(average));
422:       } else {
423:         PetscViewerASCIISynchronizedPrintf(viewer,"Subdomain %04d is fixed.    Average = % 1.14e\n",
424:                                              PetscGlobalRank,PetscAbsScalar(average));
425:       }
426:       PetscViewerFlush(viewer);
427:     }
428:   }
429:   /* Solving the system for vec2_N */
430:   KSPSolve(pcis->ksp_N,vec1_N,vec2_N);
431:   /* Extracting the local interface vector out of the solution */
432:   VecScatterBegin(pcis->N_to_B,vec2_N,x,INSERT_VALUES,SCATTER_FORWARD);
433:   VecScatterEnd  (pcis->N_to_B,vec2_N,x,INSERT_VALUES,SCATTER_FORWARD);
434:   return(0);
435: }