Actual source code: is.c

  1: /*$Id: is.c,v 1.7 2001/03/23 23:23:21 balay Exp $*/
 2:  #include src/sles/pc/impls/is/is.h

  4: /* -------------------------------------------------------------------------- */
  5: /*
  6:    PCISSetUp - 
  7: */
  8: int PCISSetUp(PC pc)
  9: {
 10:   PC_IS  *pcis = (PC_IS*)(pc->data);
 11:   Mat_IS *matis = (Mat_IS*)pc->mat->data;
 12:   int    i, ierr;
 13: 

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

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

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

 75:     [           |      ]
 76:     [    A_II   | A_IB ]
 77:     A = [           |      ]
 78:     [-----------+------]
 79:     [    A_BI   | A_BB ]
 80:   */

 82:   MatGetSubMatrix(matis->A,pcis->is_I_local,pcis->is_I_local,PETSC_DECIDE,MAT_INITIAL_MATRIX,&pcis->A_II);
 83:   MatGetSubMatrix(matis->A,pcis->is_I_local,pcis->is_B_local,PETSC_DECIDE,MAT_INITIAL_MATRIX,&pcis->A_IB);
 84:   MatGetSubMatrix(matis->A,pcis->is_B_local,pcis->is_I_local,PETSC_DECIDE,MAT_INITIAL_MATRIX,&pcis->A_BI);
 85:   MatGetSubMatrix(matis->A,pcis->is_B_local,pcis->is_B_local,PETSC_DECIDE,MAT_INITIAL_MATRIX,&pcis->A_BB);

 87:   /*
 88:     Creating work vectors and arrays
 89:   */
 90:   /* pcis->vec1_N has already been created */
 91:   VecDuplicate(pcis->vec1_N,&pcis->vec2_N);
 92:   VecCreateSeq(PETSC_COMM_SELF,pcis->n-pcis->n_B,&pcis->vec1_D);
 93:   VecDuplicate(pcis->vec1_D,&pcis->vec2_D);
 94:   VecDuplicate(pcis->vec1_D,&pcis->vec3_D);
 95:   VecCreateSeq(PETSC_COMM_SELF,pcis->n_B,&pcis->vec1_B);
 96:   VecDuplicate(pcis->vec1_B,&pcis->vec2_B);
 97:   VecDuplicate(pcis->vec1_B,&pcis->vec3_B);
 98:   {
 99:     Vec global;
100:     PCGetVector(pc,&global);
101:     VecDuplicate(global,&pcis->vec1_global);
102:   }
103:   PetscMalloc((pcis->n)*sizeof(Scalar),&pcis->work_N);

105:   /* Creating the scatter contexts */
106:   VecScatterCreate(pc->vec,pcis->is_I_global,pcis->vec1_D,(IS)0,&pcis->global_to_D);
107:   VecScatterCreate(pcis->vec1_N,pcis->is_B_local,pcis->vec1_B,(IS)0,&pcis->N_to_B);
108:   VecScatterCreate(pc->vec,pcis->is_B_global,pcis->vec1_B,(IS)0,&pcis->global_to_B);

110:   /* Creating scaling "matrix" D, from information in vec1_N */
111:   VecDuplicate(pcis->vec1_B,&pcis->D);
112:   VecScatterBegin(pcis->vec1_N,pcis->D,INSERT_VALUES,SCATTER_FORWARD,pcis->N_to_B);
113:   VecScatterEnd  (pcis->vec1_N,pcis->D,INSERT_VALUES,SCATTER_FORWARD,pcis->N_to_B);
114:   VecReciprocal(pcis->D);

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

118:   /*
119:     Creating the SLES contexts for the local Dirichlet and Neumann problems.
120:   */
121:   {
122:     PC  pc_ctx;
123:     KSP ksp_ctx;
124:     /* Dirichlet */
125:     SLESCreate(PETSC_COMM_SELF,&pcis->sles_D);
126:     SLESSetOperators(pcis->sles_D,pcis->A_II,pcis->A_II,SAME_PRECONDITIONER);
127:     SLESSetOptionsPrefix(pcis->sles_D,"localD_");
128:     SLESGetKSP(pcis->sles_D,&ksp_ctx);
129:     SLESGetPC(pcis->sles_D,&pc_ctx);
130:     PCSetType(pc_ctx,PCLU);
131:     KSPSetType(ksp_ctx,KSPPREONLY);
132:     SLESSetFromOptions(pcis->sles_D);
133:     /* the vectors in the following line are dummy arguments, just telling the SLES the vector size. Values are not used */
134:     SLESSetUp(pcis->sles_D,pcis->vec1_D,pcis->vec2_D);
135:     /* Neumann */
136:     SLESCreate(PETSC_COMM_SELF,&pcis->sles_N);
137:     SLESSetOperators(pcis->sles_N,matis->A,matis->A,SAME_PRECONDITIONER);
138:     SLESSetOptionsPrefix(pcis->sles_N,"localN_");
139:     SLESGetKSP(pcis->sles_N,&ksp_ctx);
140:     SLESGetPC(pcis->sles_N,&pc_ctx);
141:     PCSetType(pc_ctx,PCLU);
142:     KSPSetType(ksp_ctx,KSPPREONLY);
143:     SLESSetFromOptions(pcis->sles_N);
144:     {
145:       PetscTruth damp_fixed,
146:                  remove_nullspace_fixed,
147:                  set_damping_factor_floating,
148:                  not_damp_floating,
149:                  not_remove_nullspace_floating;
150:       double     fixed_factor,
151:                  floating_factor;

153:       PetscOptionsGetDouble(pc_ctx->prefix,"-pc_is_damp_fixed",&fixed_factor,&damp_fixed);
154:       if (!damp_fixed) { fixed_factor = 0.0; }
155:       PetscOptionsHasName(pc_ctx->prefix,"-pc_is_damp_fixed",&damp_fixed);

157:       PetscOptionsHasName(pc_ctx->prefix,"-pc_is_remove_nullspace_fixed",&remove_nullspace_fixed);

159:       PetscOptionsGetDouble(pc_ctx->prefix,"-pc_is_set_damping_factor_floating",
160:                               &floating_factor,&set_damping_factor_floating);
161:       if (!set_damping_factor_floating) { floating_factor = 0.0; }
162:       PetscOptionsHasName(pc_ctx->prefix,"-pc_is_set_damping_factor_floating",&set_damping_factor_floating);
163:       if (!set_damping_factor_floating) { floating_factor = 1.e-12; }

165:       PetscOptionsHasName(pc_ctx->prefix,"-pc_is_not_damp_floating",&not_damp_floating);

167:       PetscOptionsHasName(pc_ctx->prefix,"-pc_is_not_remove_nullspace_floating",&not_remove_nullspace_floating);

169:       if (pcis->pure_neumann) {  /* floating subdomain */
170:         if (!(not_damp_floating)) {
171:           PCLUSetDamping (pc_ctx,floating_factor);
172:           PCILUSetDamping(pc_ctx,floating_factor);
173:         }
174:         if (!(not_remove_nullspace_floating)){
175:           MatNullSpace nullsp;
176:           MatNullSpaceCreate(PETSC_COMM_SELF,1,0,PETSC_NULL,&nullsp);
177:           PCNullSpaceAttach(pc_ctx,nullsp);
178:           MatNullSpaceDestroy(nullsp);
179:         }
180:       } else {  /* fixed subdomain */
181:         if (damp_fixed) {
182:           PCLUSetDamping (pc_ctx,fixed_factor);
183:           PCILUSetDamping(pc_ctx,fixed_factor);
184:         }
185:         if (remove_nullspace_fixed) {
186:           MatNullSpace nullsp;
187:           MatNullSpaceCreate(PETSC_COMM_SELF,1,0,PETSC_NULL,&nullsp);
188:           PCNullSpaceAttach(pc_ctx,nullsp);
189:           MatNullSpaceDestroy(nullsp);
190:         }
191:       }
192:     }
193:     /* the vectors in the following line are dummy arguments, just telling the SLES the vector size. Values are not used */
194:     SLESSetUp(pcis->sles_N,pcis->vec1_N,pcis->vec2_N);
195:   }

197:   ISLocalToGlobalMappingGetInfo(((Mat_IS*)(pc->mat->data))->mapping,&(pcis->n_neigh),&(pcis->neigh),
198:                                        &(pcis->n_shared),&(pcis->shared));
199:   pcis->ISLocalToGlobalMappingGetInfoWasCalled = PETSC_TRUE;

201:   return(0);
202: }

204: /* -------------------------------------------------------------------------- */
205: /*
206:    PCISDestroy -
207: */
208: int PCISDestroy(PC pc)
209: {
210:   PC_IS *pcis = (PC_IS*)(pc->data);
211:   int   ierr;


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

243:   return(0);
244: }

246: /* -------------------------------------------------------------------------- */
247: /*
248:    PCISCreate - 
249: */
250: int PCISCreate(PC pc)
251: {
252:   PC_IS *pcis = (PC_IS*)(pc->data);


256:   pcis->is_B_local  = 0;
257:   pcis->is_I_local  = 0;
258:   pcis->is_B_global = 0;
259:   pcis->is_I_global = 0;
260:   pcis->A_II        = 0;
261:   pcis->A_IB        = 0;
262:   pcis->A_BI        = 0;
263:   pcis->A_BB        = 0;
264:   pcis->D           = 0;
265:   pcis->sles_N      = 0;
266:   pcis->sles_D      = 0;
267:   pcis->vec1_N      = 0;
268:   pcis->vec2_N      = 0;
269:   pcis->vec1_D      = 0;
270:   pcis->vec2_D      = 0;
271:   pcis->vec3_D      = 0;
272:   pcis->vec1_B      = 0;
273:   pcis->vec2_B      = 0;
274:   pcis->vec3_B      = 0;
275:   pcis->vec1_global = 0;
276:   pcis->work_N      = 0;
277:   pcis->global_to_D = 0;
278:   pcis->N_to_B      = 0;
279:   pcis->global_to_B = 0;
280:   pcis->ISLocalToGlobalMappingGetInfoWasCalled = PETSC_FALSE;

282:   return(0);
283: }

285: /* -------------------------------------------------------------------------- */
286: /*
287:    PCISApplySchur -

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

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

299: */
300: int PCISApplySchur(PC pc, Vec v, Vec vec1_B, Vec vec2_B, Vec vec1_D, Vec vec2_D)
301: {
302:   int    ierr, its;
303:   Scalar m_one = -1.0;
304:   PC_IS  *pcis = (PC_IS*)(pc->data);


308:   if (vec2_B == (Vec)0) { vec2_B = v; }

310:   MatMult(pcis->A_BB,v,vec1_B);
311:   MatMult(pcis->A_IB,v,vec1_D);
312:   SLESSolve(pcis->sles_D,vec1_D,vec2_D,&its);
313:   MatMult(pcis->A_BI,vec2_D,vec2_B);
314:   VecAXPY(&m_one,vec2_B,vec1_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: */
337: int PCISScatterArrayNToVecB (Scalar *array_N, Vec v_B, InsertMode imode, ScatterMode smode, PC pc)
338: {
339:   int    i, ierr, *index;
340:   Scalar *array_B;
341:   PC_IS  *pcis = (PC_IS*)(pc->data);


345:   VecGetArray(v_B,&array_B);
346:   ISGetIndices(pcis->is_B_local,&index);

348:   if (smode == SCATTER_FORWARD) {
349:     if (imode == INSERT_VALUES) {
350:       for (i=0; i<pcis->n_B; i++) { array_B[i]  = array_N[index[i]]; }
351:     } else {  /* ADD_VALUES */
352:       for (i=0; i<pcis->n_B; i++) { array_B[i] += array_N[index[i]]; }
353:     }
354:   } else {  /* SCATTER_REVERSE */
355:     if (imode == INSERT_VALUES) {
356:       for (i=0; i<pcis->n_B; i++) { array_N[index[i]]  = array_B[i]; }
357:     } else {  /* ADD_VALUES */
358:       for (i=0; i<pcis->n_B; i++) { array_N[index[i]] += array_B[i]; }
359:     }
360:   }

362:   ISRestoreIndices(pcis->is_B_local,&index);
363:   VecRestoreArray(v_B,&array_B);

365:   return(0);
366: }

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

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

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

386: */
387: int PCISApplyInvSchur (PC pc, Vec b, Vec x, Vec vec1_N, Vec vec2_N)
388: {
389:   int    ierr, its;
390:   PC_IS  *pcis = (PC_IS*)(pc->data);
391:   Scalar zero  = 0.0;


395:   /*
396:     Neumann solvers. 
397:     Applying the inverse of the local Schur complement, i.e, solving a Neumann
398:     Problem with zero at the interior nodes of the RHS and extracting the interface
399:     part of the solution. inverse Schur complement is applied to b and the result
400:     is stored in x.
401:   */
402:   /* Setting the RHS vec1_N */
403:   VecSet(&zero,vec1_N);
404:   VecScatterBegin(b,vec1_N,INSERT_VALUES,SCATTER_REVERSE,pcis->N_to_B);
405:   VecScatterEnd  (b,vec1_N,INSERT_VALUES,SCATTER_REVERSE,pcis->N_to_B);
406:   /* Checking for consistency of the RHS */
407:   {
408:     PetscTruth flg;
409:     PetscOptionsHasName(PETSC_NULL,"-check_consistency",&flg);
410:     if (flg) {
411:       Scalar average;
412:       VecSum(vec1_N,&average);
413:       average = average / ((PetscReal)pcis->n);
414:       if (pcis->pure_neumann) {
415:         PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_(pc->comm),"Subdomain %04d is floating. Average = % 1.14en",
416:                                              PetscGlobalRank,PetscAbsScalar(average));
417:       } else {
418:         PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_(pc->comm),"Subdomain %04d is fixed.    Average = % 1.14en",
419:                                              PetscGlobalRank,PetscAbsScalar(average));
420:       }
421:       PetscViewerFlush(PETSC_VIEWER_STDOUT_(pc->comm));
422:     }
423:   }
424:   /* Solving the system for vec2_N */
425:   SLESSolve(pcis->sles_N,vec1_N,vec2_N,&its);
426:   /* Extracting the local interface vector out of the solution */
427:   VecScatterBegin(vec2_N,x,INSERT_VALUES,SCATTER_FORWARD,pcis->N_to_B);
428:   VecScatterEnd  (vec2_N,x,INSERT_VALUES,SCATTER_FORWARD,pcis->N_to_B);

430:   return(0);
431: }