Actual source code: bddcfetidp.c

petsc-3.14.6 2021-03-30
Report Typos and Errors
  1: #include <../src/ksp/pc/impls/bddc/bddc.h>
  2: #include <../src/ksp/pc/impls/bddc/bddcprivate.h>
  3: #include <petscblaslapack.h>

  5: static PetscErrorCode MatMult_BDdelta_deluxe_nonred(Mat A, Vec x, Vec y)
  6: {
  7:   BDdelta_DN     ctx;

 11:   MatShellGetContext(A,(void**)&ctx);
 12:   MatMultTranspose(ctx->BD,x,ctx->work);
 13:   KSPSolveTranspose(ctx->kBD,ctx->work,y);
 14:   /* No PC so cannot propagate up failure in KSPSolveTranspose() */
 15:   return(0);
 16: }

 18: static PetscErrorCode MatMultTranspose_BDdelta_deluxe_nonred(Mat A, Vec x, Vec y)
 19: {
 20:   BDdelta_DN     ctx;

 24:   MatShellGetContext(A,(void**)&ctx);
 25:   KSPSolve(ctx->kBD,x,ctx->work);
 26:   /* No PC so cannot propagate up failure in KSPSolve() */
 27:   MatMult(ctx->BD,ctx->work,y);
 28:   return(0);
 29: }

 31: static PetscErrorCode MatDestroy_BDdelta_deluxe_nonred(Mat A)
 32: {
 33:   BDdelta_DN     ctx;

 37:   MatShellGetContext(A,(void**)&ctx);
 38:   MatDestroy(&ctx->BD);
 39:   KSPDestroy(&ctx->kBD);
 40:   VecDestroy(&ctx->work);
 41:   PetscFree(ctx);
 42:   return(0);
 43: }


 46: PetscErrorCode PCBDDCCreateFETIDPMatContext(PC pc, FETIDPMat_ctx *fetidpmat_ctx)
 47: {
 48:   FETIDPMat_ctx  newctx;

 52:   PetscNew(&newctx);
 53:   /* increase the reference count for BDDC preconditioner */
 54:   PetscObjectReference((PetscObject)pc);
 55:   newctx->pc              = pc;
 56:   *fetidpmat_ctx          = newctx;
 57:   return(0);
 58: }

 60: PetscErrorCode PCBDDCCreateFETIDPPCContext(PC pc, FETIDPPC_ctx *fetidppc_ctx)
 61: {
 62:   FETIDPPC_ctx   newctx;

 66:   PetscNew(&newctx);
 67:   /* increase the reference count for BDDC preconditioner */
 68:   PetscObjectReference((PetscObject)pc);
 69:   newctx->pc              = pc;
 70:   *fetidppc_ctx           = newctx;
 71:   return(0);
 72: }

 74: PetscErrorCode PCBDDCDestroyFETIDPMat(Mat A)
 75: {
 76:   FETIDPMat_ctx  mat_ctx;

 80:   MatShellGetContext(A,(void**)&mat_ctx);
 81:   VecDestroy(&mat_ctx->lambda_local);
 82:   VecDestroy(&mat_ctx->temp_solution_D);
 83:   VecDestroy(&mat_ctx->temp_solution_B);
 84:   MatDestroy(&mat_ctx->B_delta);
 85:   MatDestroy(&mat_ctx->B_Ddelta);
 86:   MatDestroy(&mat_ctx->B_BB);
 87:   MatDestroy(&mat_ctx->B_BI);
 88:   MatDestroy(&mat_ctx->Bt_BB);
 89:   MatDestroy(&mat_ctx->Bt_BI);
 90:   MatDestroy(&mat_ctx->C);
 91:   VecDestroy(&mat_ctx->rhs_flip);
 92:   VecDestroy(&mat_ctx->vP);
 93:   VecDestroy(&mat_ctx->xPg);
 94:   VecDestroy(&mat_ctx->yPg);
 95:   VecScatterDestroy(&mat_ctx->l2g_lambda);
 96:   VecScatterDestroy(&mat_ctx->l2g_lambda_only);
 97:   VecScatterDestroy(&mat_ctx->l2g_p);
 98:   VecScatterDestroy(&mat_ctx->g2g_p);
 99:   PCDestroy(&mat_ctx->pc); /* decrease PCBDDC reference count */
100:   ISDestroy(&mat_ctx->pressure);
101:   ISDestroy(&mat_ctx->lagrange);
102:   PetscFree(mat_ctx);
103:   return(0);
104: }

106: PetscErrorCode PCBDDCDestroyFETIDPPC(PC pc)
107: {
108:   FETIDPPC_ctx   pc_ctx;

112:   PCShellGetContext(pc,(void**)&pc_ctx);
113:   VecDestroy(&pc_ctx->lambda_local);
114:   MatDestroy(&pc_ctx->B_Ddelta);
115:   VecScatterDestroy(&pc_ctx->l2g_lambda);
116:   MatDestroy(&pc_ctx->S_j);
117:   PCDestroy(&pc_ctx->pc); /* decrease PCBDDC reference count */
118:   VecDestroy(&pc_ctx->xPg);
119:   VecDestroy(&pc_ctx->yPg);
120:   PetscFree(pc_ctx);
121:   return(0);
122: }

124: PetscErrorCode PCBDDCSetupFETIDPMatContext(FETIDPMat_ctx fetidpmat_ctx)
125: {
127:   PC_IS          *pcis=(PC_IS*)fetidpmat_ctx->pc->data;
128:   PC_BDDC        *pcbddc=(PC_BDDC*)fetidpmat_ctx->pc->data;
129:   PCBDDCGraph    mat_graph=pcbddc->mat_graph;
130:   Mat_IS         *matis  = (Mat_IS*)fetidpmat_ctx->pc->pmat->data;
131:   MPI_Comm       comm;
132:   Mat            ScalingMat,BD1,BD2;
133:   Vec            fetidp_global;
134:   IS             IS_l2g_lambda;
135:   IS             subset,subset_mult,subset_n,isvert;
136:   PetscBool      skip_node,fully_redundant;
137:   PetscInt       i,j,k,s,n_boundary_dofs,n_global_lambda,n_vertices,partial_sum;
138:   PetscInt       cum,n_local_lambda,n_lambda_for_dof,dual_size,n_neg_values,n_pos_values;
139:   PetscMPIInt    rank,size,buf_size,neigh;
140:   PetscScalar    scalar_value;
141:   const PetscInt *vertex_indices;
142:   PetscInt       *dual_dofs_boundary_indices,*aux_local_numbering_1;
143:   const PetscInt *aux_global_numbering;
144:   PetscInt       *aux_sums,*cols_B_delta,*l2g_indices;
145:   PetscScalar    *array,*scaling_factors,*vals_B_delta;
146:   PetscScalar    **all_factors;
147:   PetscInt       *aux_local_numbering_2;
148:   PetscLayout    llay;

150:   /* saddlepoint */
151:   ISLocalToGlobalMapping l2gmap_p;
152:   PetscLayout            play;
153:   IS                     gP,pP;
154:   PetscInt               nPl,nPg,nPgl;

157:   PetscObjectGetComm((PetscObject)(fetidpmat_ctx->pc),&comm);
158:   MPI_Comm_rank(comm,&rank);
159:   MPI_Comm_size(comm,&size);

161:   /* saddlepoint */
162:   nPl      = 0;
163:   nPg      = 0;
164:   nPgl     = 0;
165:   gP       = NULL;
166:   pP       = NULL;
167:   l2gmap_p = NULL;
168:   play     = NULL;
169:   PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_pP",(PetscObject*)&pP);
170:   if (pP) { /* saddle point */
171:     /* subdomain pressures in global numbering */
172:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_gP",(PetscObject*)&gP);
173:     if (!gP) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"gP not present");
174:     ISGetLocalSize(gP,&nPl);
175:     VecCreate(PETSC_COMM_SELF,&fetidpmat_ctx->vP);
176:     VecSetSizes(fetidpmat_ctx->vP,nPl,nPl);
177:     VecSetType(fetidpmat_ctx->vP,VECSTANDARD);
178:     VecSetUp(fetidpmat_ctx->vP);

180:     /* pressure matrix */
181:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_C",(PetscObject*)&fetidpmat_ctx->C);
182:     if (!fetidpmat_ctx->C) { /* null pressure block, compute layout and global numbering for pressures */
183:       IS Pg;

185:       ISRenumber(gP,NULL,&nPg,&Pg);
186:       ISLocalToGlobalMappingCreateIS(Pg,&l2gmap_p);
187:       ISDestroy(&Pg);
188:       PetscLayoutCreate(comm,&play);
189:       PetscLayoutSetBlockSize(play,1);
190:       PetscLayoutSetSize(play,nPg);
191:       ISGetLocalSize(pP,&nPgl);
192:       PetscLayoutSetLocalSize(play,nPgl);
193:       PetscLayoutSetUp(play);
194:     } else {
195:       PetscObjectReference((PetscObject)fetidpmat_ctx->C);
196:       MatGetLocalToGlobalMapping(fetidpmat_ctx->C,&l2gmap_p,NULL);
197:       PetscObjectReference((PetscObject)l2gmap_p);
198:       MatGetSize(fetidpmat_ctx->C,&nPg,NULL);
199:       MatGetLocalSize(fetidpmat_ctx->C,NULL,&nPgl);
200:       MatGetLayouts(fetidpmat_ctx->C,NULL,&llay);
201:       PetscLayoutReference(llay,&play);
202:     }
203:     VecCreateMPIWithArray(comm,1,nPgl,nPg,NULL,&fetidpmat_ctx->xPg);
204:     VecCreateMPIWithArray(comm,1,nPgl,nPg,NULL,&fetidpmat_ctx->yPg);

206:     /* import matrices for pressures coupling */
207:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_B_BI",(PetscObject*)&fetidpmat_ctx->B_BI);
208:     if (!fetidpmat_ctx->B_BI) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"B_BI not present");
209:     PetscObjectReference((PetscObject)fetidpmat_ctx->B_BI);

211:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_B_BB",(PetscObject*)&fetidpmat_ctx->B_BB);
212:     if (!fetidpmat_ctx->B_BB) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"B_BB not present");
213:     PetscObjectReference((PetscObject)fetidpmat_ctx->B_BB);

215:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_Bt_BI",(PetscObject*)&fetidpmat_ctx->Bt_BI);
216:     if (!fetidpmat_ctx->Bt_BI) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Bt_BI not present");
217:     PetscObjectReference((PetscObject)fetidpmat_ctx->Bt_BI);

219:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_Bt_BB",(PetscObject*)&fetidpmat_ctx->Bt_BB);
220:     if (!fetidpmat_ctx->Bt_BB) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Bt_BB not present");
221:     PetscObjectReference((PetscObject)fetidpmat_ctx->Bt_BB);

223:     PetscObjectQuery((PetscObject)fetidpmat_ctx->pc,"__KSPFETIDP_flip" ,(PetscObject*)&fetidpmat_ctx->rhs_flip);
224:     if (fetidpmat_ctx->rhs_flip) {
225:       PetscObjectReference((PetscObject)fetidpmat_ctx->rhs_flip);
226:     }
227:   }

229:   /* Default type of lagrange multipliers is non-redundant */
230:   fully_redundant = fetidpmat_ctx->fully_redundant;

232:   /* Evaluate local and global number of lagrange multipliers */
233:   VecSet(pcis->vec1_N,0.0);
234:   n_local_lambda = 0;
235:   partial_sum = 0;
236:   n_boundary_dofs = 0;
237:   s = 0;

239:   /* Get Vertices used to define the BDDC */
240:   PCBDDCGraphGetCandidatesIS(pcbddc->mat_graph,NULL,NULL,NULL,NULL,&isvert);
241:   ISGetLocalSize(isvert,&n_vertices);
242:   ISGetIndices(isvert,&vertex_indices);

244:   dual_size = pcis->n_B-n_vertices;
245:   PetscMalloc1(dual_size,&dual_dofs_boundary_indices);
246:   PetscMalloc1(dual_size,&aux_local_numbering_1);
247:   PetscMalloc1(dual_size,&aux_local_numbering_2);

249:   VecGetArray(pcis->vec1_N,&array);
250:   for (i=0;i<pcis->n;i++){
251:     j = mat_graph->count[i]; /* RECALL: mat_graph->count[i] does not count myself */
252:     if (j > 0) n_boundary_dofs++;
253:     skip_node = PETSC_FALSE;
254:     if (s < n_vertices && vertex_indices[s] == i) { /* it works for a sorted set of vertices */
255:       skip_node = PETSC_TRUE;
256:       s++;
257:     }
258:     if (j < 1) skip_node = PETSC_TRUE;
259:     if (mat_graph->special_dof[i] == PCBDDCGRAPH_DIRICHLET_MARK) skip_node = PETSC_TRUE;
260:     if (!skip_node) {
261:       if (fully_redundant) {
262:         /* fully redundant set of lagrange multipliers */
263:         n_lambda_for_dof = (j*(j+1))/2;
264:       } else {
265:         n_lambda_for_dof = j;
266:       }
267:       n_local_lambda += j;
268:       /* needed to evaluate global number of lagrange multipliers */
269:       array[i]=(1.0*n_lambda_for_dof)/(j+1.0); /* already scaled for the next global sum */
270:       /* store some data needed */
271:       dual_dofs_boundary_indices[partial_sum] = n_boundary_dofs-1;
272:       aux_local_numbering_1[partial_sum] = i;
273:       aux_local_numbering_2[partial_sum] = n_lambda_for_dof;
274:       partial_sum++;
275:     }
276:   }
277:   VecRestoreArray(pcis->vec1_N,&array);
278:   ISRestoreIndices(isvert,&vertex_indices);
279:   PCBDDCGraphRestoreCandidatesIS(pcbddc->mat_graph,NULL,NULL,NULL,NULL,&isvert);
280:   dual_size = partial_sum;

282:   /* compute global ordering of lagrange multipliers and associate l2g map */
283:   ISCreateGeneral(comm,partial_sum,aux_local_numbering_1,PETSC_COPY_VALUES,&subset_n);
284:   ISLocalToGlobalMappingApplyIS(pcis->mapping,subset_n,&subset);
285:   ISDestroy(&subset_n);
286:   ISCreateGeneral(comm,partial_sum,aux_local_numbering_2,PETSC_OWN_POINTER,&subset_mult);
287:   ISRenumber(subset,subset_mult,&fetidpmat_ctx->n_lambda,&subset_n);
288:   ISDestroy(&subset);

290:   if (PetscDefined(USE_DEBUG)) {
291:     VecSet(pcis->vec1_global,0.0);
292:     VecScatterBegin(matis->rctx,pcis->vec1_N,pcis->vec1_global,ADD_VALUES,SCATTER_REVERSE);
293:     VecScatterEnd(matis->rctx,pcis->vec1_N,pcis->vec1_global,ADD_VALUES,SCATTER_REVERSE);
294:     VecSum(pcis->vec1_global,&scalar_value);
295:     i = (PetscInt)PetscRealPart(scalar_value);
296:     if (i != fetidpmat_ctx->n_lambda) SETERRQ2(PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Global number of multipliers mismatch! (%D != %D)",fetidpmat_ctx->n_lambda,i);
297:   }

299:   /* init data for scaling factors exchange */
300:   if (!pcbddc->use_deluxe_scaling) {
301:     PetscInt    *ptrs_buffer,neigh_position;
302:     PetscScalar *send_buffer,*recv_buffer;
303:     MPI_Request *send_reqs,*recv_reqs;

305:     partial_sum = 0;
306:     PetscMalloc1(pcis->n_neigh,&ptrs_buffer);
307:     PetscMalloc1(PetscMax(pcis->n_neigh-1,0),&send_reqs);
308:     PetscMalloc1(PetscMax(pcis->n_neigh-1,0),&recv_reqs);
309:     PetscMalloc1(pcis->n+1,&all_factors);
310:     if (pcis->n_neigh > 0) ptrs_buffer[0]=0;
311:     for (i=1;i<pcis->n_neigh;i++) {
312:       partial_sum += pcis->n_shared[i];
313:       ptrs_buffer[i] = ptrs_buffer[i-1]+pcis->n_shared[i];
314:     }
315:     PetscMalloc1(partial_sum,&send_buffer);
316:     PetscMalloc1(partial_sum,&recv_buffer);
317:     PetscMalloc1(partial_sum,&all_factors[0]);
318:     for (i=0;i<pcis->n-1;i++) {
319:       j = mat_graph->count[i];
320:       all_factors[i+1]=all_factors[i]+j;
321:     }

323:     /* scatter B scaling to N vec */
324:     VecScatterBegin(pcis->N_to_B,pcis->D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
325:     VecScatterEnd(pcis->N_to_B,pcis->D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
326:     /* communications */
327:     VecGetArrayRead(pcis->vec1_N,(const PetscScalar**)&array);
328:     for (i=1;i<pcis->n_neigh;i++) {
329:       for (j=0;j<pcis->n_shared[i];j++) {
330:         send_buffer[ptrs_buffer[i-1]+j]=array[pcis->shared[i][j]];
331:       }
332:       PetscMPIIntCast(ptrs_buffer[i]-ptrs_buffer[i-1],&buf_size);
333:       PetscMPIIntCast(pcis->neigh[i],&neigh);
334:       MPI_Isend(&send_buffer[ptrs_buffer[i-1]],buf_size,MPIU_SCALAR,neigh,0,comm,&send_reqs[i-1]);
335:       MPI_Irecv(&recv_buffer[ptrs_buffer[i-1]],buf_size,MPIU_SCALAR,neigh,0,comm,&recv_reqs[i-1]);
336:     }
337:     VecRestoreArrayRead(pcis->vec1_N,(const PetscScalar**)&array);
338:     if (pcis->n_neigh > 0) {
339:       MPI_Waitall(pcis->n_neigh-1,recv_reqs,MPI_STATUSES_IGNORE);
340:     }
341:     /* put values in correct places */
342:     for (i=1;i<pcis->n_neigh;i++) {
343:       for (j=0;j<pcis->n_shared[i];j++) {
344:         k = pcis->shared[i][j];
345:         neigh_position = 0;
346:         while (mat_graph->neighbours_set[k][neigh_position] != pcis->neigh[i]) {neigh_position++;}
347:         all_factors[k][neigh_position]=recv_buffer[ptrs_buffer[i-1]+j];
348:       }
349:     }
350:     if (pcis->n_neigh > 0) {
351:       MPI_Waitall(pcis->n_neigh-1,send_reqs,MPI_STATUSES_IGNORE);
352:     }
353:     PetscFree(send_reqs);
354:     PetscFree(recv_reqs);
355:     PetscFree(send_buffer);
356:     PetscFree(recv_buffer);
357:     PetscFree(ptrs_buffer);
358:   }

360:   /* Compute B and B_delta (local actions) */
361:   PetscMalloc1(pcis->n_neigh,&aux_sums);
362:   PetscMalloc1(n_local_lambda,&l2g_indices);
363:   PetscMalloc1(n_local_lambda,&vals_B_delta);
364:   PetscMalloc1(n_local_lambda,&cols_B_delta);
365:   if (!pcbddc->use_deluxe_scaling) {
366:     PetscMalloc1(n_local_lambda,&scaling_factors);
367:   } else {
368:     scaling_factors = NULL;
369:     all_factors     = NULL;
370:   }
371:   ISGetIndices(subset_n,&aux_global_numbering);
372:   partial_sum=0;
373:   cum = 0;
374:   for (i=0;i<dual_size;i++) {
375:     n_global_lambda = aux_global_numbering[cum];
376:     j = mat_graph->count[aux_local_numbering_1[i]];
377:     aux_sums[0]=0;
378:     for (s=1;s<j;s++) {
379:       aux_sums[s]=aux_sums[s-1]+j-s+1;
380:     }
381:     if (all_factors) array = all_factors[aux_local_numbering_1[i]];
382:     n_neg_values = 0;
383:     while (n_neg_values < j && mat_graph->neighbours_set[aux_local_numbering_1[i]][n_neg_values] < rank) {n_neg_values++;}
384:     n_pos_values = j - n_neg_values;
385:     if (fully_redundant) {
386:       for (s=0;s<n_neg_values;s++) {
387:         l2g_indices    [partial_sum+s]=aux_sums[s]+n_neg_values-s-1+n_global_lambda;
388:         cols_B_delta   [partial_sum+s]=dual_dofs_boundary_indices[i];
389:         vals_B_delta   [partial_sum+s]=-1.0;
390:         if (!pcbddc->use_deluxe_scaling) scaling_factors[partial_sum+s]=array[s];
391:       }
392:       for (s=0;s<n_pos_values;s++) {
393:         l2g_indices    [partial_sum+s+n_neg_values]=aux_sums[n_neg_values]+s+n_global_lambda;
394:         cols_B_delta   [partial_sum+s+n_neg_values]=dual_dofs_boundary_indices[i];
395:         vals_B_delta   [partial_sum+s+n_neg_values]=1.0;
396:         if (!pcbddc->use_deluxe_scaling) scaling_factors[partial_sum+s+n_neg_values]=array[s+n_neg_values];
397:       }
398:       partial_sum += j;
399:     } else {
400:       /* l2g_indices and default cols and vals of B_delta */
401:       for (s=0;s<j;s++) {
402:         l2g_indices    [partial_sum+s]=n_global_lambda+s;
403:         cols_B_delta   [partial_sum+s]=dual_dofs_boundary_indices[i];
404:         vals_B_delta   [partial_sum+s]=0.0;
405:       }
406:       /* B_delta */
407:       if (n_neg_values > 0) { /* there's a rank next to me to the left */
408:         vals_B_delta   [partial_sum+n_neg_values-1]=-1.0;
409:       }
410:       if (n_neg_values < j) { /* there's a rank next to me to the right */
411:         vals_B_delta   [partial_sum+n_neg_values]=1.0;
412:       }
413:       /* scaling as in Klawonn-Widlund 1999 */
414:       if (!pcbddc->use_deluxe_scaling) {
415:         for (s=0;s<n_neg_values;s++) {
416:           scalar_value = 0.0;
417:           for (k=0;k<s+1;k++) scalar_value += array[k];
418:           scaling_factors[partial_sum+s] = -scalar_value;
419:         }
420:         for (s=0;s<n_pos_values;s++) {
421:           scalar_value = 0.0;
422:           for (k=s+n_neg_values;k<j;k++) scalar_value += array[k];
423:           scaling_factors[partial_sum+s+n_neg_values] = scalar_value;
424:         }
425:       }
426:       partial_sum += j;
427:     }
428:     cum += aux_local_numbering_2[i];
429:   }
430:   ISRestoreIndices(subset_n,&aux_global_numbering);
431:   ISDestroy(&subset_mult);
432:   ISDestroy(&subset_n);
433:   PetscFree(aux_sums);
434:   PetscFree(aux_local_numbering_1);
435:   PetscFree(dual_dofs_boundary_indices);
436:   if (all_factors) {
437:     PetscFree(all_factors[0]);
438:     PetscFree(all_factors);
439:   }

441:   /* Create local part of B_delta */
442:   MatCreate(PETSC_COMM_SELF,&fetidpmat_ctx->B_delta);
443:   MatSetSizes(fetidpmat_ctx->B_delta,n_local_lambda,pcis->n_B,n_local_lambda,pcis->n_B);
444:   MatSetType(fetidpmat_ctx->B_delta,MATSEQAIJ);
445:   MatSeqAIJSetPreallocation(fetidpmat_ctx->B_delta,1,NULL);
446:   MatSetOption(fetidpmat_ctx->B_delta,MAT_IGNORE_ZERO_ENTRIES,PETSC_TRUE);
447:   for (i=0;i<n_local_lambda;i++) {
448:     MatSetValue(fetidpmat_ctx->B_delta,i,cols_B_delta[i],vals_B_delta[i],INSERT_VALUES);
449:   }
450:   PetscFree(vals_B_delta);
451:   MatAssemblyBegin(fetidpmat_ctx->B_delta,MAT_FINAL_ASSEMBLY);
452:   MatAssemblyEnd(fetidpmat_ctx->B_delta,MAT_FINAL_ASSEMBLY);

454:   BD1 = NULL;
455:   BD2 = NULL;
456:   if (fully_redundant) {
457:     if (pcbddc->use_deluxe_scaling) SETERRQ(comm,PETSC_ERR_SUP,"Deluxe FETIDP with fully-redundant multipliers to be implemented");
458:     MatCreate(PETSC_COMM_SELF,&ScalingMat);
459:     MatSetSizes(ScalingMat,n_local_lambda,n_local_lambda,n_local_lambda,n_local_lambda);
460:     MatSetType(ScalingMat,MATSEQAIJ);
461:     MatSeqAIJSetPreallocation(ScalingMat,1,NULL);
462:     for (i=0;i<n_local_lambda;i++) {
463:       MatSetValue(ScalingMat,i,i,scaling_factors[i],INSERT_VALUES);
464:     }
465:     MatAssemblyBegin(ScalingMat,MAT_FINAL_ASSEMBLY);
466:     MatAssemblyEnd(ScalingMat,MAT_FINAL_ASSEMBLY);
467:     MatMatMult(ScalingMat,fetidpmat_ctx->B_delta,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&fetidpmat_ctx->B_Ddelta);
468:     MatDestroy(&ScalingMat);
469:   } else {
470:     MatCreate(PETSC_COMM_SELF,&fetidpmat_ctx->B_Ddelta);
471:     MatSetSizes(fetidpmat_ctx->B_Ddelta,n_local_lambda,pcis->n_B,n_local_lambda,pcis->n_B);
472:     if (!pcbddc->use_deluxe_scaling || !pcbddc->sub_schurs) {
473:       MatSetType(fetidpmat_ctx->B_Ddelta,MATSEQAIJ);
474:       MatSeqAIJSetPreallocation(fetidpmat_ctx->B_Ddelta,1,NULL);
475:       for (i=0;i<n_local_lambda;i++) {
476:         MatSetValue(fetidpmat_ctx->B_Ddelta,i,cols_B_delta[i],scaling_factors[i],INSERT_VALUES);
477:       }
478:       MatAssemblyBegin(fetidpmat_ctx->B_Ddelta,MAT_FINAL_ASSEMBLY);
479:       MatAssemblyEnd(fetidpmat_ctx->B_Ddelta,MAT_FINAL_ASSEMBLY);
480:     } else {
481:       /* scaling as in Klawonn-Widlund 1999 */
482:       PCBDDCDeluxeScaling deluxe_ctx = pcbddc->deluxe_ctx;
483:       PCBDDCSubSchurs     sub_schurs = pcbddc->sub_schurs;
484:       Mat                 T;
485:       PetscScalar         *W,lwork,*Bwork;
486:       const PetscInt      *idxs = NULL;
487:       PetscInt            cum,mss,*nnz;
488:       PetscBLASInt        *pivots,B_lwork,B_N,B_ierr;

490:       if (!pcbddc->deluxe_singlemat) SETERRQ(comm,PETSC_ERR_USER,"Cannot compute B_Ddelta! rerun with -pc_bddc_deluxe_singlemat");
491:       mss  = 0;
492:       PetscCalloc1(pcis->n_B,&nnz);
493:       if (sub_schurs->is_Ej_all) {
494:         ISGetIndices(sub_schurs->is_Ej_all,&idxs);
495:         for (i=0,cum=0;i<sub_schurs->n_subs;i++) {
496:           PetscInt subset_size;

498:           ISGetLocalSize(sub_schurs->is_subs[i],&subset_size);
499:           for (j=0;j<subset_size;j++) nnz[idxs[j+cum]] = subset_size;
500:           mss  = PetscMax(mss,subset_size);
501:           cum += subset_size;
502:         }
503:       }
504:       MatCreate(PETSC_COMM_SELF,&T);
505:       MatSetSizes(T,pcis->n_B,pcis->n_B,pcis->n_B,pcis->n_B);
506:       MatSetType(T,MATSEQAIJ);
507:       MatSeqAIJSetPreallocation(T,0,nnz);
508:       PetscFree(nnz);

510:       /* workspace allocation */
511:       B_lwork = 0;
512:       if (mss) {
513:         PetscScalar dummy = 1;

515:         B_lwork = -1;
516:         PetscBLASIntCast(mss,&B_N);
517:         PetscFPTrapPush(PETSC_FP_TRAP_OFF);
518:         PetscStackCallBLAS("LAPACKgetri",LAPACKgetri_(&B_N,&dummy,&B_N,&B_N,&lwork,&B_lwork,&B_ierr));
519:         PetscFPTrapPop();
520:         if (B_ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in query to GETRI Lapack routine %d",(int)B_ierr);
521:         PetscBLASIntCast((PetscInt)PetscRealPart(lwork),&B_lwork);
522:       }
523:       PetscMalloc3(mss*mss,&W,mss,&pivots,B_lwork,&Bwork);

525:       for (i=0,cum=0;i<sub_schurs->n_subs;i++) {
526:         const PetscScalar *M;
527:         PetscInt          subset_size;

529:         ISGetLocalSize(sub_schurs->is_subs[i],&subset_size);
530:         PetscBLASIntCast(subset_size,&B_N);
531:         MatDenseGetArrayRead(deluxe_ctx->seq_mat[i],&M);
532:         PetscArraycpy(W,M,subset_size*subset_size);
533:         MatDenseRestoreArrayRead(deluxe_ctx->seq_mat[i],&M);
534:         PetscFPTrapPush(PETSC_FP_TRAP_OFF);
535:         PetscStackCallBLAS("LAPACKgetrf",LAPACKgetrf_(&B_N,&B_N,W,&B_N,pivots,&B_ierr));
536:         if (B_ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in GETRF Lapack routine %d",(int)B_ierr);
537:         PetscStackCallBLAS("LAPACKgetri",LAPACKgetri_(&B_N,W,&B_N,pivots,Bwork,&B_lwork,&B_ierr));
538:         if (B_ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in GETRI Lapack routine %d",(int)B_ierr);
539:         PetscFPTrapPop();
540:         /* silent static analyzer */
541:         if (!idxs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"IDXS not present");
542:         MatSetValues(T,subset_size,idxs+cum,subset_size,idxs+cum,W,INSERT_VALUES);
543:         cum += subset_size;
544:       }
545:       MatAssemblyBegin(T,MAT_FINAL_ASSEMBLY);
546:       MatAssemblyEnd(T,MAT_FINAL_ASSEMBLY);
547:       MatMatTransposeMult(T,fetidpmat_ctx->B_delta,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&BD1);
548:       MatMatMult(fetidpmat_ctx->B_delta,BD1,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&BD2);
549:       MatDestroy(&T);
550:       PetscFree3(W,pivots,Bwork);
551:       if (sub_schurs->is_Ej_all) {
552:         ISRestoreIndices(sub_schurs->is_Ej_all,&idxs);
553:       }
554:     }
555:   }
556:   PetscFree(scaling_factors);
557:   PetscFree(cols_B_delta);

559:   /* Layout of multipliers */
560:   PetscLayoutCreate(comm,&llay);
561:   PetscLayoutSetBlockSize(llay,1);
562:   PetscLayoutSetSize(llay,fetidpmat_ctx->n_lambda);
563:   PetscLayoutSetUp(llay);
564:   PetscLayoutGetLocalSize(llay,&fetidpmat_ctx->n);

566:   /* Local work vector of multipliers */
567:   VecCreate(PETSC_COMM_SELF,&fetidpmat_ctx->lambda_local);
568:   VecSetSizes(fetidpmat_ctx->lambda_local,n_local_lambda,n_local_lambda);
569:   VecSetType(fetidpmat_ctx->lambda_local,VECSEQ);

571:   if (BD2) {
572:     ISLocalToGlobalMapping l2g;
573:     Mat                    T,TA,*pT;
574:     IS                     is;
575:     PetscInt               nl,N;
576:     BDdelta_DN             ctx;

578:     PetscLayoutGetLocalSize(llay,&nl);
579:     PetscLayoutGetSize(llay,&N);
580:     MatCreate(comm,&T);
581:     MatSetSizes(T,nl,nl,N,N);
582:     MatSetType(T,MATIS);
583:     ISLocalToGlobalMappingCreate(comm,1,n_local_lambda,l2g_indices,PETSC_COPY_VALUES,&l2g);
584:     MatSetLocalToGlobalMapping(T,l2g,l2g);
585:     ISLocalToGlobalMappingDestroy(&l2g);
586:     MatISSetLocalMat(T,BD2);
587:     MatAssemblyBegin(T,MAT_FINAL_ASSEMBLY);
588:     MatAssemblyEnd(T,MAT_FINAL_ASSEMBLY);
589:     MatDestroy(&BD2);
590:     MatConvert(T,MATAIJ,MAT_INITIAL_MATRIX,&TA);
591:     MatDestroy(&T);
592:     ISCreateGeneral(comm,n_local_lambda,l2g_indices,PETSC_USE_POINTER,&is);
593:     MatCreateSubMatrices(TA,1,&is,&is,MAT_INITIAL_MATRIX,&pT);
594:     MatDestroy(&TA);
595:     ISDestroy(&is);
596:     BD2  = pT[0];
597:     PetscFree(pT);

599:     /* B_Ddelta for non-redundant multipliers with deluxe scaling */
600:     PetscNew(&ctx);
601:     MatSetType(fetidpmat_ctx->B_Ddelta,MATSHELL);
602:     MatShellSetContext(fetidpmat_ctx->B_Ddelta,(void *)ctx);
603:     MatShellSetOperation(fetidpmat_ctx->B_Ddelta,MATOP_MULT,(void (*)(void))MatMult_BDdelta_deluxe_nonred);
604:     MatShellSetOperation(fetidpmat_ctx->B_Ddelta,MATOP_MULT_TRANSPOSE,(void (*)(void))MatMultTranspose_BDdelta_deluxe_nonred);
605:     MatShellSetOperation(fetidpmat_ctx->B_Ddelta,MATOP_DESTROY,(void (*)(void))MatDestroy_BDdelta_deluxe_nonred);
606:     MatSetUp(fetidpmat_ctx->B_Ddelta);

608:     PetscObjectReference((PetscObject)BD1);
609:     ctx->BD = BD1;
610:     KSPCreate(PETSC_COMM_SELF,&ctx->kBD);
611:     KSPSetOperators(ctx->kBD,BD2,BD2);
612:     VecDuplicate(fetidpmat_ctx->lambda_local,&ctx->work);
613:     fetidpmat_ctx->deluxe_nonred = PETSC_TRUE;
614:   }
615:   MatDestroy(&BD1);
616:   MatDestroy(&BD2);

618:   /* fetidpmat sizes */
619:   fetidpmat_ctx->n += nPgl;
620:   fetidpmat_ctx->N  = fetidpmat_ctx->n_lambda+nPg;

622:   /* Global vector for FETI-DP linear system */
623:   VecCreate(comm,&fetidp_global);
624:   VecSetSizes(fetidp_global,fetidpmat_ctx->n,fetidpmat_ctx->N);
625:   VecSetType(fetidp_global,VECMPI);
626:   VecSetUp(fetidp_global);

628:   /* Decide layout for fetidp dofs: if it is a saddle point problem
629:      pressure is ordered first in the local part of the global vector
630:      of the FETI-DP linear system */
631:   if (nPg) {
632:     Vec            v;
633:     IS             IS_l2g_p,ais;
634:     PetscLayout    alay;
635:     const PetscInt *idxs,*pranges,*aranges,*lranges;
636:     PetscInt       *l2g_indices_p,rst;
637:     PetscMPIInt    rank;

639:     PetscMalloc1(nPl,&l2g_indices_p);
640:     VecGetLayout(fetidp_global,&alay);
641:     PetscLayoutGetRanges(alay,&aranges);
642:     PetscLayoutGetRanges(play,&pranges);
643:     PetscLayoutGetRanges(llay,&lranges);

645:     MPI_Comm_rank(PetscObjectComm((PetscObject)fetidp_global),&rank);
646:     ISCreateStride(PetscObjectComm((PetscObject)fetidp_global),pranges[rank+1]-pranges[rank],aranges[rank],1,&fetidpmat_ctx->pressure);
647:     PetscObjectSetName((PetscObject)fetidpmat_ctx->pressure,"F_P");
648:     ISCreateStride(PetscObjectComm((PetscObject)fetidp_global),lranges[rank+1]-lranges[rank],aranges[rank]+pranges[rank+1]-pranges[rank],1,&fetidpmat_ctx->lagrange);
649:     PetscObjectSetName((PetscObject)fetidpmat_ctx->lagrange,"F_L");
650:     ISLocalToGlobalMappingGetIndices(l2gmap_p,&idxs);
651:     /* shift local to global indices for pressure */
652:     for (i=0;i<nPl;i++) {
653:       PetscMPIInt owner;

655:       PetscLayoutFindOwner(play,idxs[i],&owner);
656:       l2g_indices_p[i] = idxs[i]-pranges[owner]+aranges[owner];
657:     }
658:     ISLocalToGlobalMappingRestoreIndices(l2gmap_p,&idxs);
659:     ISCreateGeneral(comm,nPl,l2g_indices_p,PETSC_OWN_POINTER,&IS_l2g_p);

661:     /* local to global scatter for pressure */
662:     VecScatterCreate(fetidpmat_ctx->vP,NULL,fetidp_global,IS_l2g_p,&fetidpmat_ctx->l2g_p);
663:     ISDestroy(&IS_l2g_p);

665:     /* scatter for lagrange multipliers only */
666:     VecCreate(comm,&v);
667:     VecSetType(v,VECSTANDARD);
668:     VecSetLayout(v,llay);
669:     VecSetUp(v);
670:     ISCreateGeneral(comm,n_local_lambda,l2g_indices,PETSC_COPY_VALUES,&ais);
671:     VecScatterCreate(fetidpmat_ctx->lambda_local,NULL,v,ais,&fetidpmat_ctx->l2g_lambda_only);
672:     ISDestroy(&ais);
673:     VecDestroy(&v);

675:     /* shift local to global indices for multipliers */
676:     for (i=0;i<n_local_lambda;i++) {
677:       PetscInt    ps;
678:       PetscMPIInt owner;

680:       PetscLayoutFindOwner(llay,l2g_indices[i],&owner);
681:       ps = pranges[owner+1]-pranges[owner];
682:       l2g_indices[i] = l2g_indices[i]-lranges[owner]+aranges[owner]+ps;
683:     }

685:     /* scatter from alldofs to pressures global fetidp vector */
686:     PetscLayoutGetRange(alay,&rst,NULL);
687:     ISCreateStride(comm,nPgl,rst,1,&ais);
688:     VecScatterCreate(pcis->vec1_global,pP,fetidp_global,ais,&fetidpmat_ctx->g2g_p);
689:     ISDestroy(&ais);
690:   }
691:   PetscLayoutDestroy(&llay);
692:   PetscLayoutDestroy(&play);
693:   ISCreateGeneral(comm,n_local_lambda,l2g_indices,PETSC_OWN_POINTER,&IS_l2g_lambda);

695:   /* scatter from local to global multipliers */
696:   VecScatterCreate(fetidpmat_ctx->lambda_local,NULL,fetidp_global,IS_l2g_lambda,&fetidpmat_ctx->l2g_lambda);
697:   ISDestroy(&IS_l2g_lambda);
698:   ISLocalToGlobalMappingDestroy(&l2gmap_p);
699:   VecDestroy(&fetidp_global);

701:   /* Create some work vectors needed by fetidp */
702:   VecDuplicate(pcis->vec1_B,&fetidpmat_ctx->temp_solution_B);
703:   VecDuplicate(pcis->vec1_D,&fetidpmat_ctx->temp_solution_D);
704:   return(0);
705: }


708: PetscErrorCode PCBDDCSetupFETIDPPCContext(Mat fetimat, FETIDPPC_ctx fetidppc_ctx)
709: {
710:   FETIDPMat_ctx  mat_ctx;
711:   PC_BDDC        *pcbddc = (PC_BDDC*)fetidppc_ctx->pc->data;
712:   PC_IS          *pcis = (PC_IS*)fetidppc_ctx->pc->data;
713:   PetscBool      lumped = PETSC_FALSE;

717:   MatShellGetContext(fetimat,(void**)&mat_ctx);
718:   /* get references from objects created when setting up feti mat context */
719:   PetscObjectReference((PetscObject)mat_ctx->lambda_local);
720:   fetidppc_ctx->lambda_local = mat_ctx->lambda_local;
721:   PetscObjectReference((PetscObject)mat_ctx->B_Ddelta);
722:   fetidppc_ctx->B_Ddelta = mat_ctx->B_Ddelta;
723:   if (mat_ctx->deluxe_nonred) {
724:     PC               pc,mpc;
725:     BDdelta_DN       ctx;
726:     MatSolverType    solver;
727:     const char       *prefix;

729:     MatShellGetContext(mat_ctx->B_Ddelta,&ctx);
730:     KSPSetType(ctx->kBD,KSPPREONLY);
731:     KSPGetPC(ctx->kBD,&mpc);
732:     KSPGetPC(pcbddc->ksp_D,&pc);
733:     PCSetType(mpc,PCLU);
734:     PCFactorGetMatSolverType(pc,(MatSolverType*)&solver);
735:     if (solver) {
736:       PCFactorSetMatSolverType(mpc,solver);
737:     }
738:     MatGetOptionsPrefix(fetimat,&prefix);
739:     KSPSetOptionsPrefix(ctx->kBD,prefix);
740:     KSPAppendOptionsPrefix(ctx->kBD,"bddelta_");
741:     KSPSetFromOptions(ctx->kBD);
742:   }

744:   if (mat_ctx->l2g_lambda_only) {
745:     PetscObjectReference((PetscObject)mat_ctx->l2g_lambda_only);
746:     fetidppc_ctx->l2g_lambda = mat_ctx->l2g_lambda_only;
747:   } else {
748:     PetscObjectReference((PetscObject)mat_ctx->l2g_lambda);
749:     fetidppc_ctx->l2g_lambda = mat_ctx->l2g_lambda;
750:   }
751:   /* Dirichlet preconditioner */
752:   PetscOptionsGetBool(NULL,((PetscObject)fetimat)->prefix,"-pc_lumped",&lumped,NULL);
753:   if (!lumped) {
754:     IS        iV;
755:     PetscBool discrete_harmonic = PETSC_FALSE;

757:     PetscObjectQuery((PetscObject)fetidppc_ctx->pc,"__KSPFETIDP_iV",(PetscObject*)&iV);
758:     if (iV) {
759:       PetscOptionsGetBool(NULL,((PetscObject)fetimat)->prefix,"-pc_discrete_harmonic",&discrete_harmonic,NULL);
760:     }
761:     if (discrete_harmonic) {
762:       KSP             sksp;
763:       PC              pc;
764:       PCBDDCSubSchurs sub_schurs = pcbddc->sub_schurs;
765:       Mat             A_II,A_IB,A_BI;
766:       IS              iP = NULL;
767:       PetscBool       isshell,reuse = PETSC_FALSE;
768:       KSPType         ksptype;
769:       const char      *prefix;

771:       /*
772:         We constructs a Schur complement for

774:         | A_II A_ID |
775:         | A_DI A_DD |

777:         instead of

779:         | A_II  B^t_II A_ID |
780:         | B_II -C_II   B_ID |
781:         | A_DI  B^t_ID A_DD |

783:       */
784:       if (sub_schurs && sub_schurs->reuse_solver) {
785:         PetscObjectQuery((PetscObject)sub_schurs->A,"__KSPFETIDP_iP",(PetscObject*)&iP);
786:         if (iP) reuse = PETSC_TRUE;
787:       }
788:       if (!reuse) {
789:         IS       aB;
790:         PetscInt nb;
791:         ISGetLocalSize(pcis->is_B_local,&nb);
792:         ISCreateStride(PetscObjectComm((PetscObject)pcis->A_II),nb,0,1,&aB);
793:         MatCreateSubMatrix(pcis->A_II,iV,iV,MAT_INITIAL_MATRIX,&A_II);
794:         MatCreateSubMatrix(pcis->A_IB,iV,aB,MAT_INITIAL_MATRIX,&A_IB);
795:         MatCreateSubMatrix(pcis->A_BI,aB,iV,MAT_INITIAL_MATRIX,&A_BI);
796:         ISDestroy(&aB);
797:       } else {
798:         MatCreateSubMatrix(sub_schurs->A,pcis->is_I_local,pcis->is_B_local,MAT_INITIAL_MATRIX,&A_IB);
799:         MatCreateSubMatrix(sub_schurs->A,pcis->is_B_local,pcis->is_I_local,MAT_INITIAL_MATRIX,&A_BI);
800:         PetscObjectReference((PetscObject)pcis->A_II);
801:         A_II = pcis->A_II;
802:       }
803:       MatCreateSchurComplement(A_II,A_II,A_IB,A_BI,pcis->A_BB,&fetidppc_ctx->S_j);

805:       /* propagate settings of solver */
806:       MatSchurComplementGetKSP(fetidppc_ctx->S_j,&sksp);
807:       KSPGetType(pcis->ksp_D,&ksptype);
808:       KSPSetType(sksp,ksptype);
809:       KSPGetPC(pcis->ksp_D,&pc);
810:       PetscObjectTypeCompare((PetscObject)pc,PCSHELL,&isshell);
811:       if (!isshell) {
812:         MatSolverType    solver;
813:         PCType           pctype;

815:         PCGetType(pc,&pctype);
816:         PCFactorGetMatSolverType(pc,(MatSolverType*)&solver);
817:         KSPGetPC(sksp,&pc);
818:         PCSetType(pc,pctype);
819:         if (solver) {
820:           PCFactorSetMatSolverType(pc,solver);
821:         }
822:       } else {
823:         KSPGetPC(sksp,&pc);
824:         PCSetType(pc,PCLU);
825:       }
826:       MatDestroy(&A_II);
827:       MatDestroy(&A_IB);
828:       MatDestroy(&A_BI);
829:       MatGetOptionsPrefix(fetimat,&prefix);
830:       KSPSetOptionsPrefix(sksp,prefix);
831:       KSPAppendOptionsPrefix(sksp,"harmonic_");
832:       KSPSetFromOptions(sksp);
833:       if (reuse) {
834:         KSPSetPC(sksp,sub_schurs->reuse_solver->interior_solver);
835:         PetscObjectIncrementTabLevel((PetscObject)sub_schurs->reuse_solver->interior_solver,(PetscObject)sksp,0);
836:       }
837:     } else { /* default Dirichlet preconditioner is pde-harmonic */
838:       MatCreateSchurComplement(pcis->A_II,pcis->A_II,pcis->A_IB,pcis->A_BI,pcis->A_BB,&fetidppc_ctx->S_j);
839:       MatSchurComplementSetKSP(fetidppc_ctx->S_j,pcis->ksp_D);
840:     }
841:   } else {
842:     PetscObjectReference((PetscObject)pcis->A_BB);
843:     fetidppc_ctx->S_j = pcis->A_BB;
844:   }
845:   /* saddle-point */
846:   if (mat_ctx->xPg) {
847:     PetscObjectReference((PetscObject)mat_ctx->xPg);
848:     fetidppc_ctx->xPg = mat_ctx->xPg;
849:     PetscObjectReference((PetscObject)mat_ctx->yPg);
850:     fetidppc_ctx->yPg = mat_ctx->yPg;
851:   }
852:   return(0);
853: }

855: PetscErrorCode FETIDPMatMult_Kernel(Mat fetimat, Vec x, Vec y, PetscBool trans)
856: {
857:   FETIDPMat_ctx  mat_ctx;
858:   PC_BDDC        *pcbddc;
859:   PC_IS          *pcis;

863:   MatShellGetContext(fetimat,(void**)&mat_ctx);
864:   pcis = (PC_IS*)mat_ctx->pc->data;
865:   pcbddc = (PC_BDDC*)mat_ctx->pc->data;
866:   /* Application of B_delta^T */
867:   VecSet(pcis->vec1_B,0.);
868:   VecScatterBegin(mat_ctx->l2g_lambda,x,mat_ctx->lambda_local,INSERT_VALUES,SCATTER_REVERSE);
869:   VecScatterEnd(mat_ctx->l2g_lambda,x,mat_ctx->lambda_local,INSERT_VALUES,SCATTER_REVERSE);
870:   MatMultTranspose(mat_ctx->B_delta,mat_ctx->lambda_local,pcis->vec1_B);

872:   /* Add contribution from saddle point */
873:   if (mat_ctx->l2g_p) {
874:     VecScatterBegin(mat_ctx->l2g_p,x,mat_ctx->vP,INSERT_VALUES,SCATTER_REVERSE);
875:     VecScatterEnd(mat_ctx->l2g_p,x,mat_ctx->vP,INSERT_VALUES,SCATTER_REVERSE);
876:     if (pcbddc->switch_static) {
877:       if (trans) {
878:         MatMultTranspose(mat_ctx->B_BI,mat_ctx->vP,pcis->vec1_D);
879:       } else {
880:         MatMult(mat_ctx->Bt_BI,mat_ctx->vP,pcis->vec1_D);
881:       }
882:     }
883:     if (trans) {
884:       MatMultTransposeAdd(mat_ctx->B_BB,mat_ctx->vP,pcis->vec1_B,pcis->vec1_B);
885:     } else {
886:       MatMultAdd(mat_ctx->Bt_BB,mat_ctx->vP,pcis->vec1_B,pcis->vec1_B);
887:     }
888:   } else {
889:     if (pcbddc->switch_static) {
890:       VecSet(pcis->vec1_D,0.0);
891:     }
892:   }
893:   /* Application of \widetilde{S}^-1 */
894:   PetscArrayzero(pcbddc->benign_p0,pcbddc->benign_n);
895:   PCBDDCApplyInterfacePreconditioner(mat_ctx->pc,trans);
896:   PetscArrayzero(pcbddc->benign_p0,pcbddc->benign_n);
897:   VecSet(y,0.0);
898:   /* Application of B_delta */
899:   MatMult(mat_ctx->B_delta,pcis->vec1_B,mat_ctx->lambda_local);
900:   /* Contribution from boundary pressures */
901:   if (mat_ctx->C) {
902:     const PetscScalar *lx;
903:     PetscScalar       *ly;

905:     /* pressure ordered first in the local part of x and y */
906:     VecGetArrayRead(x,&lx);
907:     VecGetArray(y,&ly);
908:     VecPlaceArray(mat_ctx->xPg,lx);
909:     VecPlaceArray(mat_ctx->yPg,ly);
910:     if (trans) {
911:       MatMultTranspose(mat_ctx->C,mat_ctx->xPg,mat_ctx->yPg);
912:     } else {
913:       MatMult(mat_ctx->C,mat_ctx->xPg,mat_ctx->yPg);
914:     }
915:     VecResetArray(mat_ctx->xPg);
916:     VecResetArray(mat_ctx->yPg);
917:     VecRestoreArrayRead(x,&lx);
918:     VecRestoreArray(y,&ly);
919:   }
920:   /* Add contribution from saddle point */
921:   if (mat_ctx->l2g_p) {
922:     if (trans) {
923:       MatMultTranspose(mat_ctx->Bt_BB,pcis->vec1_B,mat_ctx->vP);
924:     } else {
925:       MatMult(mat_ctx->B_BB,pcis->vec1_B,mat_ctx->vP);
926:     }
927:     if (pcbddc->switch_static) {
928:       if (trans) {
929:         MatMultTransposeAdd(mat_ctx->Bt_BI,pcis->vec1_D,mat_ctx->vP,mat_ctx->vP);
930:       } else {
931:         MatMultAdd(mat_ctx->B_BI,pcis->vec1_D,mat_ctx->vP,mat_ctx->vP);
932:       }
933:     }
934:     VecScatterBegin(mat_ctx->l2g_p,mat_ctx->vP,y,ADD_VALUES,SCATTER_FORWARD);
935:     VecScatterEnd(mat_ctx->l2g_p,mat_ctx->vP,y,ADD_VALUES,SCATTER_FORWARD);
936:   }
937:   VecScatterBegin(mat_ctx->l2g_lambda,mat_ctx->lambda_local,y,ADD_VALUES,SCATTER_FORWARD);
938:   VecScatterEnd(mat_ctx->l2g_lambda,mat_ctx->lambda_local,y,ADD_VALUES,SCATTER_FORWARD);
939:   return(0);
940: }

942: PetscErrorCode FETIDPMatMult(Mat fetimat, Vec x, Vec y)
943: {

947:   FETIDPMatMult_Kernel(fetimat,x,y,PETSC_FALSE);
948:   return(0);
949: }

951: PetscErrorCode FETIDPMatMultTranspose(Mat fetimat, Vec x, Vec y)
952: {

956:   FETIDPMatMult_Kernel(fetimat,x,y,PETSC_TRUE);
957:   return(0);
958: }

960: PetscErrorCode FETIDPPCApply_Kernel(PC fetipc, Vec x, Vec y, PetscBool trans)
961: {
962:   FETIDPPC_ctx   pc_ctx;
963:   PC_IS          *pcis;

967:   PCShellGetContext(fetipc,(void**)&pc_ctx);
968:   pcis = (PC_IS*)pc_ctx->pc->data;
969:   /* Application of B_Ddelta^T */
970:   VecScatterBegin(pc_ctx->l2g_lambda,x,pc_ctx->lambda_local,INSERT_VALUES,SCATTER_REVERSE);
971:   VecScatterEnd(pc_ctx->l2g_lambda,x,pc_ctx->lambda_local,INSERT_VALUES,SCATTER_REVERSE);
972:   VecSet(pcis->vec2_B,0.0);
973:   MatMultTranspose(pc_ctx->B_Ddelta,pc_ctx->lambda_local,pcis->vec2_B);
974:   /* Application of local Schur complement */
975:   if (trans) {
976:     MatMultTranspose(pc_ctx->S_j,pcis->vec2_B,pcis->vec1_B);
977:   } else {
978:     MatMult(pc_ctx->S_j,pcis->vec2_B,pcis->vec1_B);
979:   }
980:   /* Application of B_Ddelta */
981:   MatMult(pc_ctx->B_Ddelta,pcis->vec1_B,pc_ctx->lambda_local);
982:   VecSet(y,0.0);
983:   VecScatterBegin(pc_ctx->l2g_lambda,pc_ctx->lambda_local,y,ADD_VALUES,SCATTER_FORWARD);
984:   VecScatterEnd(pc_ctx->l2g_lambda,pc_ctx->lambda_local,y,ADD_VALUES,SCATTER_FORWARD);
985:   return(0);
986: }

988: PetscErrorCode FETIDPPCApply(PC pc, Vec x, Vec y)
989: {

993:   FETIDPPCApply_Kernel(pc,x,y,PETSC_FALSE);
994:   return(0);
995: }

997: PetscErrorCode FETIDPPCApplyTranspose(PC pc, Vec x, Vec y)
998: {

1002:   FETIDPPCApply_Kernel(pc,x,y,PETSC_TRUE);
1003:   return(0);
1004: }

1006: PetscErrorCode FETIDPPCView(PC pc, PetscViewer viewer)
1007: {
1008:   FETIDPPC_ctx      pc_ctx;
1009:   PetscBool         iascii;
1010:   PetscViewer       sviewer;
1011:   PetscErrorCode    ierr;

1014:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1015:   if (iascii) {
1016:     PetscMPIInt rank;
1017:     PetscBool   isschur,isshell;

1019:     PCShellGetContext(pc,(void**)&pc_ctx);
1020:     MPI_Comm_rank(PetscObjectComm((PetscObject)pc),&rank);
1021:     PetscObjectTypeCompare((PetscObject)pc_ctx->S_j,MATSCHURCOMPLEMENT,&isschur);
1022:     if (isschur) {
1023:       PetscViewerASCIIPrintf(viewer,"  Dirichlet preconditioner (just from rank 0)\n");
1024:     } else {
1025:       PetscViewerASCIIPrintf(viewer,"  Lumped preconditioner (just from rank 0)\n");
1026:     }
1027:     PetscViewerGetSubViewer(viewer,PetscObjectComm((PetscObject)pc_ctx->S_j),&sviewer);
1028:     if (!rank) {
1029:       PetscViewerPushFormat(sviewer,PETSC_VIEWER_ASCII_INFO);
1030:       PetscViewerASCIIPushTab(sviewer);
1031:       MatView(pc_ctx->S_j,sviewer);
1032:       PetscViewerASCIIPopTab(sviewer);
1033:       PetscViewerPopFormat(sviewer);
1034:     }
1035:     PetscViewerRestoreSubViewer(viewer,PetscObjectComm((PetscObject)pc_ctx->S_j),&sviewer);
1036:     PetscObjectTypeCompare((PetscObject)pc_ctx->B_Ddelta,MATSHELL,&isshell);
1037:     if (isshell) {
1038:       BDdelta_DN ctx;
1039:       PetscViewerASCIIPrintf(viewer,"  FETI-DP BDdelta: DB^t * (B D^-1 B^t)^-1 for deluxe scaling (just from rank 0)\n");
1040:       MatShellGetContext(pc_ctx->B_Ddelta,&ctx);
1041:       PetscViewerGetSubViewer(viewer,PetscObjectComm((PetscObject)pc_ctx->S_j),&sviewer);
1042:       if (!rank) {
1043:         PetscInt tl;

1045:         PetscViewerASCIIGetTab(sviewer,&tl);
1046:         PetscObjectSetTabLevel((PetscObject)ctx->kBD,tl);
1047:         KSPView(ctx->kBD,sviewer);
1048:         PetscViewerPushFormat(sviewer,PETSC_VIEWER_ASCII_INFO);
1049:         MatView(ctx->BD,sviewer);
1050:         PetscViewerPopFormat(sviewer);
1051:       }
1052:       PetscViewerRestoreSubViewer(viewer,PetscObjectComm((PetscObject)pc_ctx->S_j),&sviewer);
1053:     }
1054:     PetscViewerFlush(viewer);
1055:   }
1056:   return(0);
1057: }