Date: 2016-07-24T17:03:54+00:00

Actual source code: nn.c
petsc-3.7.3 2016-07-24
Report Typos and Errors

      2: #include <../src/ksp/pc/impls/is/nn/nn.h>

      4: /* -------------------------------------------------------------------------- */
      5: /*
      6:    PCSetUp_NN - Prepares for the use of the NN preconditioner
      7:                     by setting data structures and options.

      9:    Input Parameter:
     10: .  pc - the preconditioner context

     12:    Application Interface Routine: PCSetUp()

     14:    Notes:
     15:    The interface routine PCSetUp() is not usually called directly by
     16:    the user, but instead is called by PCApply() if necessary.
     17: */
     20: static PetscErrorCode PCSetUp_NN(PC pc)
     21: {

     25:   if (!pc->setupcalled) {
     26:     /* Set up all the "iterative substructuring" common block */
     27:     PCISSetUp(pc,PETSC_TRUE);
     28:     /* Create the coarse matrix. */
     29:     PCNNCreateCoarseMatrix(pc);
     30:   }
     31:   return(0);
     32: }

     34: /* -------------------------------------------------------------------------- */
     35: /*
     36:    PCApply_NN - Applies the NN preconditioner to a vector.

     38:    Input Parameters:
     39: .  pc - the preconditioner context
     40: .  r - input vector (global)

     42:    Output Parameter:
     43: .  z - output vector (global)

     45:    Application Interface Routine: PCApply()
     46:  */
     49: static PetscErrorCode PCApply_NN(PC pc,Vec r,Vec z)
     50: {
     51:   PC_IS          *pcis = (PC_IS*)(pc->data);
     53:   PetscScalar    m_one = -1.0;
     54:   Vec            w     = pcis->vec1_global;

     57:   /*
     58:     Dirichlet solvers.
     59:     Solving $ B_I^{(i)}r_I^{(i)} $ at each processor.
     60:     Storing the local results at vec2_D
     61:   */
     62:   VecScatterBegin(pcis->global_to_D,r,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
     63:   VecScatterEnd  (pcis->global_to_D,r,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
     64:   KSPSolve(pcis->ksp_D,pcis->vec1_D,pcis->vec2_D);

     66:   /*
     67:     Computing $ r_B - \sum_j \tilde R_j^T A_{BI}^{(j)} (B_I^{(j)}r_I^{(j)}) $ .
     68:     Storing the result in the interface portion of the global vector w.
     69:   */
     70:   MatMult(pcis->A_BI,pcis->vec2_D,pcis->vec1_B);
     71:   VecScale(pcis->vec1_B,m_one);
     72:   VecCopy(r,w);
     73:   VecScatterBegin(pcis->global_to_B,pcis->vec1_B,w,ADD_VALUES,SCATTER_REVERSE);
     74:   VecScatterEnd  (pcis->global_to_B,pcis->vec1_B,w,ADD_VALUES,SCATTER_REVERSE);

     76:   /*
     77:     Apply the interface preconditioner
     78:   */
     79:   PCNNApplyInterfacePreconditioner(pc,w,z,pcis->work_N,pcis->vec1_B,pcis->vec2_B,pcis->vec3_B,pcis->vec1_D,
     80:                                           pcis->vec3_D,pcis->vec1_N,pcis->vec2_N);

     82:   /*
     83:     Computing $ t_I^{(i)} = A_{IB}^{(i)} \tilde R_i z_B $
     84:     The result is stored in vec1_D.
     85:   */
     86:   VecScatterBegin(pcis->global_to_B,z,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
     87:   VecScatterEnd  (pcis->global_to_B,z,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
     88:   MatMult(pcis->A_IB,pcis->vec1_B,pcis->vec1_D);

     90:   /*
     91:     Dirichlet solvers.
     92:     Computing $ B_I^{(i)}t_I^{(i)} $ and sticking into the global vector the blocks
     93:     $ B_I^{(i)}r_I^{(i)} - B_I^{(i)}t_I^{(i)} $.
     94:   */
     95:   VecScatterBegin(pcis->global_to_D,pcis->vec2_D,z,INSERT_VALUES,SCATTER_REVERSE);
     96:   VecScatterEnd  (pcis->global_to_D,pcis->vec2_D,z,INSERT_VALUES,SCATTER_REVERSE);
     97:   KSPSolve(pcis->ksp_D,pcis->vec1_D,pcis->vec2_D);
     98:   VecScale(pcis->vec2_D,m_one);
     99:   VecScatterBegin(pcis->global_to_D,pcis->vec2_D,z,ADD_VALUES,SCATTER_REVERSE);
    100:   VecScatterEnd  (pcis->global_to_D,pcis->vec2_D,z,ADD_VALUES,SCATTER_REVERSE);
    101:   return(0);
    102: }

    104: /* -------------------------------------------------------------------------- */
    105: /*
    106:    PCDestroy_NN - Destroys the private context for the NN preconditioner
    107:    that was created with PCCreate_NN().

    109:    Input Parameter:
    110: .  pc - the preconditioner context

    112:    Application Interface Routine: PCDestroy()
    113: */
    116: static PetscErrorCode PCDestroy_NN(PC pc)
    117: {
    118:   PC_NN          *pcnn = (PC_NN*)pc->data;

    122:   PCISDestroy(pc);

    124:   MatDestroy(&pcnn->coarse_mat);
    125:   VecDestroy(&pcnn->coarse_x);
    126:   VecDestroy(&pcnn->coarse_b);
    127:   KSPDestroy(&pcnn->ksp_coarse);
    128:   if (pcnn->DZ_IN) {
    129:     PetscFree(pcnn->DZ_IN[0]);
    130:     PetscFree(pcnn->DZ_IN);
    131:   }

    133:   /*
    134:       Free the private data structure that was hanging off the PC
    135:   */
    136:   PetscFree(pc->data);
    137:   return(0);
    138: }

    140: /* -------------------------------------------------------------------------- */
    141: /*MC
    142:    PCNN - Balancing Neumann-Neumann for scalar elliptic PDEs.

    144:    Options Database Keys:
    145: +    -pc_nn_turn_off_first_balancing - do not balance the residual before solving the local Neumann problems
    146:                                        (this skips the first coarse grid solve in the preconditioner)
    147: .    -pc_nn_turn_off_second_balancing - do not balance the solution solving the local Neumann problems
    148:                                        (this skips the second coarse grid solve in the preconditioner)
    149: .    -pc_is_damp_fixed <fact> -
    150: .    -pc_is_remove_nullspace_fixed -
    151: .    -pc_is_set_damping_factor_floating <fact> -
    152: .    -pc_is_not_damp_floating -
    153: +    -pc_is_not_remove_nullspace_floating -

    155:    Level: intermediate

    157:    Notes: The matrix used with this preconditioner must be of type MATIS

    159:           Unlike more 'conventional' Neumann-Neumann preconditioners this iterates over ALL the
    160:           degrees of freedom, NOT just those on the interface (this allows the use of approximate solvers
    161:           on the subdomains; though in our experience using approximate solvers is slower.).

    163:           Options for the coarse grid preconditioner can be set with -nn_coarse_pc_xxx
    164:           Options for the Dirichlet subproblem preconditioner can be set with -is_localD_pc_xxx
    165:           Options for the Neumann subproblem preconditioner can be set with -is_localN_pc_xxx

    167:    Contributed by Paulo Goldfeld

    169: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC,  MATIS
    170: M*/

    174: PETSC_EXTERN PetscErrorCode PCCreate_NN(PC pc)
    175: {
    177:   PC_NN          *pcnn;

    180:   /*
    181:      Creates the private data structure for this preconditioner and
    182:      attach it to the PC object.
    183:   */
    184:   PetscNewLog(pc,&pcnn);
    185:   pc->data = (void*)pcnn;

    187:   PCISCreate(pc);
    188:   pcnn->coarse_mat = 0;
    189:   pcnn->coarse_x   = 0;
    190:   pcnn->coarse_b   = 0;
    191:   pcnn->ksp_coarse = 0;
    192:   pcnn->DZ_IN      = 0;

    194:   /*
    195:       Set the pointers for the functions that are provided above.
    196:       Now when the user-level routines (such as PCApply(), PCDestroy(), etc.)
    197:       are called, they will automatically call these functions.  Note we
    198:       choose not to provide a couple of these functions since they are
    199:       not needed.
    200:   */
    201:   pc->ops->apply               = PCApply_NN;
    202:   pc->ops->applytranspose      = 0;
    203:   pc->ops->setup               = PCSetUp_NN;
    204:   pc->ops->destroy             = PCDestroy_NN;
    205:   pc->ops->view                = 0;
    206:   pc->ops->applyrichardson     = 0;
    207:   pc->ops->applysymmetricleft  = 0;
    208:   pc->ops->applysymmetricright = 0;
    209:   return(0);
    210: }

    212: /* -------------------------------------------------------------------------- */
    213: /*
    214:    PCNNCreateCoarseMatrix -
    215: */
    218: PetscErrorCode PCNNCreateCoarseMatrix(PC pc)
    219: {
    220:   MPI_Request    *send_request, *recv_request;
    222:   PetscInt       i, j, k;
    223:   PetscScalar    *mat;     /* Sub-matrix with this subdomain's contribution to the coarse matrix             */
    224:   PetscScalar    **DZ_OUT; /* proc[k].DZ_OUT[i][] = bit of vector to be sent from processor k to processor i */

    226:   /* aliasing some names */
    227:   PC_IS       *pcis     = (PC_IS*)(pc->data);
    228:   PC_NN       *pcnn     = (PC_NN*)pc->data;
    229:   PetscInt    n_neigh   = pcis->n_neigh;
    230:   PetscInt    *neigh    = pcis->neigh;
    231:   PetscInt    *n_shared = pcis->n_shared;
    232:   PetscInt    **shared  = pcis->shared;
    233:   PetscScalar **DZ_IN;   /* Must be initialized after memory allocation. */

    236:   /* Allocate memory for mat (the +1 is to handle the case n_neigh equal to zero) */
    237:   PetscMalloc1(n_neigh*n_neigh+1,&mat);

    239:   /* Allocate memory for DZ */
    240:   /* Notice that DZ_OUT[0] is allocated some space that is never used. */
    241:   /* This is just in order to DZ_OUT and DZ_IN to have exactly the same form. */
    242:   {
    243:     PetscInt size_of_Z = 0;
    244:     PetscMalloc ((n_neigh+1)*sizeof(PetscScalar*),&pcnn->DZ_IN);
    245:     DZ_IN = pcnn->DZ_IN;
    246:     PetscMalloc ((n_neigh+1)*sizeof(PetscScalar*),&DZ_OUT);
    247:     for (i=0; i<n_neigh; i++) size_of_Z += n_shared[i];
    248:     PetscMalloc ((size_of_Z+1)*sizeof(PetscScalar),&DZ_IN[0]);
    249:     PetscMalloc ((size_of_Z+1)*sizeof(PetscScalar),&DZ_OUT[0]);
    250:   }
    251:   for (i=1; i<n_neigh; i++) {
    252:     DZ_IN[i]  = DZ_IN [i-1] + n_shared[i-1];
    253:     DZ_OUT[i] = DZ_OUT[i-1] + n_shared[i-1];
    254:   }

    256:   /* Set the values of DZ_OUT, in order to send this info to the neighbours */
    257:   /* First, set the auxiliary array pcis->work_N. */
    258:   PCISScatterArrayNToVecB(pcis->work_N,pcis->D,INSERT_VALUES,SCATTER_REVERSE,pc);
    259:   for (i=1; i<n_neigh; i++) {
    260:     for (j=0; j<n_shared[i]; j++) {
    261:       DZ_OUT[i][j] = pcis->work_N[shared[i][j]];
    262:     }
    263:   }

    265:   /* Non-blocking send/receive the common-interface chunks of scaled nullspaces */
    266:   /* Notice that send_request[] and recv_request[] could have one less element. */
    267:   /* We make them longer to have request[i] corresponding to neigh[i].          */
    268:   {
    269:     PetscMPIInt tag;
    270:     PetscObjectGetNewTag((PetscObject)pc,&tag);
    271:     PetscMalloc2(n_neigh+1,&send_request,n_neigh+1,&recv_request);
    272:     for (i=1; i<n_neigh; i++) {
    273:       MPI_Isend((void*)(DZ_OUT[i]),n_shared[i],MPIU_SCALAR,neigh[i],tag,PetscObjectComm((PetscObject)pc),&(send_request[i]));
    274:       MPI_Irecv((void*)(DZ_IN [i]),n_shared[i],MPIU_SCALAR,neigh[i],tag,PetscObjectComm((PetscObject)pc),&(recv_request[i]));
    275:     }
    276:   }

    278:   /* Set DZ_IN[0][] (recall that neigh[0]==rank, always) */
    279:   for (j=0; j<n_shared[0]; j++) DZ_IN[0][j] = pcis->work_N[shared[0][j]];

    281:   /* Start computing with local D*Z while communication goes on.    */
    282:   /* Apply Schur complement. The result is "stored" in vec (more    */
    283:   /* precisely, vec points to the result, stored in pc_nn->vec1_B)  */
    284:   /* and also scattered to pcnn->work_N.                            */
    285:   PCNNApplySchurToChunk(pc,n_shared[0],shared[0],DZ_IN[0],pcis->work_N,pcis->vec1_B,
    286:                                pcis->vec2_B,pcis->vec1_D,pcis->vec2_D);

    288:   /* Compute the first column, while completing the receiving. */
    289:   for (i=0; i<n_neigh; i++) {
    290:     MPI_Status  stat;
    291:     PetscMPIInt ind=0;
    292:     if (i>0) { MPI_Waitany(n_neigh-1,recv_request+1,&ind,&stat); ind++;}
    293:     mat[ind*n_neigh+0] = 0.0;
    294:     for (k=0; k<n_shared[ind]; k++) mat[ind*n_neigh+0] += DZ_IN[ind][k] * pcis->work_N[shared[ind][k]];
    295:   }

    297:   /* Compute the remaining of the columns */
    298:   for (j=1; j<n_neigh; j++) {
    299:     PCNNApplySchurToChunk(pc,n_shared[j],shared[j],DZ_IN[j],pcis->work_N,pcis->vec1_B,
    300:                                  pcis->vec2_B,pcis->vec1_D,pcis->vec2_D);
    301:     for (i=0; i<n_neigh; i++) {
    302:       mat[i*n_neigh+j] = 0.0;
    303:       for (k=0; k<n_shared[i]; k++) mat[i*n_neigh+j] += DZ_IN[i][k] * pcis->work_N[shared[i][k]];
    304:     }
    305:   }

    307:   /* Complete the sending. */
    308:   if (n_neigh>1) {
    309:     MPI_Status *stat;
    310:     PetscMalloc1(n_neigh-1,&stat);
    311:     if (n_neigh-1) {MPI_Waitall(n_neigh-1,&(send_request[1]),stat);}
    312:     PetscFree(stat);
    313:   }

    315:   /* Free the memory for the MPI requests */
    316:   PetscFree2(send_request,recv_request);

    318:   /* Free the memory for DZ_OUT */
    319:   if (DZ_OUT) {
    320:     PetscFree(DZ_OUT[0]);
    321:     PetscFree(DZ_OUT);
    322:   }

    324:   {
    325:     PetscMPIInt size;
    326:     MPI_Comm_size(PetscObjectComm((PetscObject)pc),&size);
    327:     /* Create the global coarse vectors (rhs and solution). */
    328:     VecCreateMPI(PetscObjectComm((PetscObject)pc),1,size,&(pcnn->coarse_b));
    329:     VecDuplicate(pcnn->coarse_b,&(pcnn->coarse_x));
    330:     /* Create and set the global coarse AIJ matrix. */
    331:     MatCreate(PetscObjectComm((PetscObject)pc),&(pcnn->coarse_mat));
    332:     MatSetSizes(pcnn->coarse_mat,1,1,size,size);
    333:     MatSetType(pcnn->coarse_mat,MATAIJ);
    334:     MatSeqAIJSetPreallocation(pcnn->coarse_mat,1,NULL);
    335:     MatMPIAIJSetPreallocation(pcnn->coarse_mat,1,NULL,n_neigh,NULL);
    336:     MatSetOption(pcnn->coarse_mat,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);
    337:     MatSetOption(pcnn->coarse_mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);
    338:     MatSetValues(pcnn->coarse_mat,n_neigh,neigh,n_neigh,neigh,mat,ADD_VALUES);
    339:     MatAssemblyBegin(pcnn->coarse_mat,MAT_FINAL_ASSEMBLY);
    340:     MatAssemblyEnd  (pcnn->coarse_mat,MAT_FINAL_ASSEMBLY);
    341:   }

    343:   {
    344:     PetscMPIInt rank;
    345:     PetscScalar one = 1.0;
    346:     MPI_Comm_rank(PetscObjectComm((PetscObject)pc),&rank);
    347:     /* "Zero out" rows of not-purely-Neumann subdomains */
    348:     if (pcis->pure_neumann) {  /* does NOT zero the row; create an empty index set. The reason is that MatZeroRows() is collective. */
    349:       MatZeroRows(pcnn->coarse_mat,0,NULL,one,0,0);
    350:     } else { /* here it DOES zero the row, since it's not a floating subdomain. */
    351:       PetscInt row = (PetscInt) rank;
    352:       MatZeroRows(pcnn->coarse_mat,1,&row,one,0,0);
    353:     }
    354:   }

    356:   /* Create the coarse linear solver context */
    357:   {
    358:     PC  pc_ctx, inner_pc;
    359:     KSP inner_ksp;

    361:     KSPCreate(PetscObjectComm((PetscObject)pc),&pcnn->ksp_coarse);
    362:     PetscObjectIncrementTabLevel((PetscObject)pcnn->ksp_coarse,(PetscObject)pc,2);
    363:     KSPSetOperators(pcnn->ksp_coarse,pcnn->coarse_mat,pcnn->coarse_mat);
    364:     KSPGetPC(pcnn->ksp_coarse,&pc_ctx);
    365:     PCSetType(pc_ctx,PCREDUNDANT);
    366:     KSPSetType(pcnn->ksp_coarse,KSPPREONLY);
    367:     PCRedundantGetKSP(pc_ctx,&inner_ksp);
    368:     KSPGetPC(inner_ksp,&inner_pc);
    369:     PCSetType(inner_pc,PCLU);
    370:     KSPSetOptionsPrefix(pcnn->ksp_coarse,"nn_coarse_");
    371:     KSPSetFromOptions(pcnn->ksp_coarse);
    372:     /* the vectors in the following line are dummy arguments, just telling the KSP the vector size. Values are not used */
    373:     KSPSetUp(pcnn->ksp_coarse);
    374:   }

    376:   /* Free the memory for mat */
    377:   PetscFree(mat);

    379:   /* for DEBUGGING, save the coarse matrix to a file. */
    380:   {
    381:     PetscBool flg = PETSC_FALSE;
    382:     PetscOptionsGetBool(NULL,NULL,"-pc_nn_save_coarse_matrix",&flg,NULL);
    383:     if (flg) {
    384:       PetscViewer viewer;
    385:       PetscViewerASCIIOpen(PETSC_COMM_WORLD,"coarse.m",&viewer);
    386:       PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
    387:       MatView(pcnn->coarse_mat,viewer);
    388:       PetscViewerPopFormat(viewer);
    389:       PetscViewerDestroy(&viewer);
    390:     }
    391:   }

    393:   /*  Set the variable pcnn->factor_coarse_rhs. */
    394:   pcnn->factor_coarse_rhs = (pcis->pure_neumann) ? 1.0 : 0.0;

    396:   /* See historical note 02, at the bottom of this file. */
    397:   return(0);
    398: }

    400: /* -------------------------------------------------------------------------- */
    401: /*
    402:    PCNNApplySchurToChunk -

    404:    Input parameters:
    405: .  pcnn
    406: .  n - size of chunk
    407: .  idx - indices of chunk
    408: .  chunk - values

    410:    Output parameters:
    411: .  array_N - result of Schur complement applied to chunk, scattered to big array
    412: .  vec1_B  - result of Schur complement applied to chunk
    413: .  vec2_B  - garbage (used as work space)
    414: .  vec1_D  - garbage (used as work space)
    415: .  vec2_D  - garbage (used as work space)

    417: */
    420: PetscErrorCode PCNNApplySchurToChunk(PC pc, PetscInt n, PetscInt *idx, PetscScalar *chunk, PetscScalar *array_N, Vec vec1_B, Vec vec2_B, Vec vec1_D, Vec vec2_D)
    421: {
    423:   PetscInt       i;
    424:   PC_IS          *pcis = (PC_IS*)(pc->data);

    427:   PetscMemzero((void*)array_N, pcis->n*sizeof(PetscScalar));
    428:   for (i=0; i<n; i++) array_N[idx[i]] = chunk[i];
    429:   PCISScatterArrayNToVecB(array_N,vec2_B,INSERT_VALUES,SCATTER_FORWARD,pc);
    430:   PCISApplySchur(pc,vec2_B,vec1_B,(Vec)0,vec1_D,vec2_D);
    431:   PCISScatterArrayNToVecB(array_N,vec1_B,INSERT_VALUES,SCATTER_REVERSE,pc);
    432:   return(0);
    433: }

    435: /* -------------------------------------------------------------------------- */
    436: /*
    437:    PCNNApplyInterfacePreconditioner - Apply the interface preconditioner, i.e.,
    438:                                       the preconditioner for the Schur complement.

    440:    Input parameter:
    441: .  r - global vector of interior and interface nodes. The values on the interior nodes are NOT used.

    443:    Output parameters:
    444: .  z - global vector of interior and interface nodes. The values on the interface are the result of
    445:        the application of the interface preconditioner to the interface part of r. The values on the
    446:        interior nodes are garbage.
    447: .  work_N - array of local nodes (interior and interface, including ghosts); returns garbage (used as work space)
    448: .  vec1_B - vector of local interface nodes (including ghosts); returns garbage (used as work space)
    449: .  vec2_B - vector of local interface nodes (including ghosts); returns garbage (used as work space)
    450: .  vec3_B - vector of local interface nodes (including ghosts); returns garbage (used as work space)
    451: .  vec1_D - vector of local interior nodes; returns garbage (used as work space)
    452: .  vec2_D - vector of local interior nodes; returns garbage (used as work space)
    453: .  vec1_N - vector of local nodes (interior and interface, including ghosts); returns garbage (used as work space)
    454: .  vec2_N - vector of local nodes (interior and interface, including ghosts); returns garbage (used as work space)

    456: */
    459: PetscErrorCode PCNNApplyInterfacePreconditioner(PC pc, Vec r, Vec z, PetscScalar *work_N, Vec vec1_B, Vec vec2_B, Vec vec3_B, Vec vec1_D,Vec vec2_D, Vec vec1_N, Vec vec2_N)
    460: {
    462:   PC_IS          *pcis = (PC_IS*)(pc->data);

    465:   /*
    466:     First balancing step.
    467:   */
    468:   {
    469:     PetscBool flg = PETSC_FALSE;
    470:     PetscOptionsGetBool(NULL,NULL,"-pc_nn_turn_off_first_balancing",&flg,NULL);
    471:     if (!flg) {
    472:       PCNNBalancing(pc,r,(Vec)0,z,vec1_B,vec2_B,(Vec)0,vec1_D,vec2_D,work_N);
    473:     } else {
    474:       VecCopy(r,z);
    475:     }
    476:   }

    478:   /*
    479:     Extract the local interface part of z and scale it by D
    480:   */
    481:   VecScatterBegin(pcis->global_to_B,z,vec1_B,INSERT_VALUES,SCATTER_FORWARD);
    482:   VecScatterEnd  (pcis->global_to_B,z,vec1_B,INSERT_VALUES,SCATTER_FORWARD);
    483:   VecPointwiseMult(vec2_B,pcis->D,vec1_B);

    485:   /* Neumann Solver */
    486:   PCISApplyInvSchur(pc,vec2_B,vec1_B,vec1_N,vec2_N);

    488:   /*
    489:     Second balancing step.
    490:   */
    491:   {
    492:     PetscBool flg = PETSC_FALSE;
    493:     PetscOptionsGetBool(NULL,NULL,"-pc_turn_off_second_balancing",&flg,NULL);
    494:     if (!flg) {
    495:       PCNNBalancing(pc,r,vec1_B,z,vec2_B,vec3_B,(Vec)0,vec1_D,vec2_D,work_N);
    496:     } else {
    497:       VecPointwiseMult(vec2_B,pcis->D,vec1_B);
    498:       VecSet(z,0.0);
    499:       VecScatterBegin(pcis->global_to_B,vec2_B,z,ADD_VALUES,SCATTER_REVERSE);
    500:       VecScatterEnd  (pcis->global_to_B,vec2_B,z,ADD_VALUES,SCATTER_REVERSE);
    501:     }
    502:   }
    503:   return(0);
    504: }

    506: /* -------------------------------------------------------------------------- */
    507: /*
    508:    PCNNBalancing - Computes z, as given in equations (15) and (16) (if the
    509:                    input argument u is provided), or s, as given in equations
    510:                    (12) and (13), if the input argument u is a null vector.
    511:                    Notice that the input argument u plays the role of u_i in
    512:                    equation (14). The equation numbers refer to [Man93].

    514:    Input Parameters:
    515: .  pcnn - NN preconditioner context.
    516: .  r - MPI vector of all nodes (interior and interface). It's preserved.
    517: .  u - (Optional) sequential vector of local interface nodes. It's preserved UNLESS vec3_B is null.

    519:    Output Parameters:
    520: .  z - MPI vector of interior and interface nodes. Returns s or z (see description above).
    521: .  vec1_B - Sequential vector of local interface nodes. Workspace.
    522: .  vec2_B - Sequential vector of local interface nodes. Workspace.
    523: .  vec3_B - (Optional) sequential vector of local interface nodes. Workspace.
    524: .  vec1_D - Sequential vector of local interior nodes. Workspace.
    525: .  vec2_D - Sequential vector of local interior nodes. Workspace.
    526: .  work_N - Array of all local nodes (interior and interface). Workspace.

    528: */
    531: PetscErrorCode PCNNBalancing(PC pc, Vec r, Vec u, Vec z, Vec vec1_B, Vec vec2_B, Vec vec3_B,Vec vec1_D, Vec vec2_D, PetscScalar *work_N)
    532: {
    534:   PetscInt       k;
    535:   PetscScalar    value;
    536:   PetscScalar    *lambda;
    537:   PC_NN          *pcnn = (PC_NN*)(pc->data);
    538:   PC_IS          *pcis = (PC_IS*)(pc->data);

    541:   PetscLogEventBegin(PC_ApplyCoarse,0,0,0,0);
    542:   if (u) {
    543:     if (!vec3_B) vec3_B = u;
    544:     VecPointwiseMult(vec1_B,pcis->D,u);
    545:     VecSet(z,0.0);
    546:     VecScatterBegin(pcis->global_to_B,vec1_B,z,ADD_VALUES,SCATTER_REVERSE);
    547:     VecScatterEnd  (pcis->global_to_B,vec1_B,z,ADD_VALUES,SCATTER_REVERSE);
    548:     VecScatterBegin(pcis->global_to_B,z,vec2_B,INSERT_VALUES,SCATTER_FORWARD);
    549:     VecScatterEnd  (pcis->global_to_B,z,vec2_B,INSERT_VALUES,SCATTER_FORWARD);
    550:     PCISApplySchur(pc,vec2_B,vec3_B,(Vec)0,vec1_D,vec2_D);
    551:     VecScale(vec3_B,-1.0);
    552:     VecCopy(r,z);
    553:     VecScatterBegin(pcis->global_to_B,vec3_B,z,ADD_VALUES,SCATTER_REVERSE);
    554:     VecScatterEnd  (pcis->global_to_B,vec3_B,z,ADD_VALUES,SCATTER_REVERSE);
    555:   } else {
    556:     VecCopy(r,z);
    557:   }
    558:   VecScatterBegin(pcis->global_to_B,z,vec2_B,INSERT_VALUES,SCATTER_FORWARD);
    559:   VecScatterEnd  (pcis->global_to_B,z,vec2_B,INSERT_VALUES,SCATTER_FORWARD);
    560:   PCISScatterArrayNToVecB(work_N,vec2_B,INSERT_VALUES,SCATTER_REVERSE,pc);
    561:   for (k=0, value=0.0; k<pcis->n_shared[0]; k++) value += pcnn->DZ_IN[0][k] * work_N[pcis->shared[0][k]];
    562:   value *= pcnn->factor_coarse_rhs;  /* This factor is set in CreateCoarseMatrix(). */
    563:   {
    564:     PetscMPIInt rank;
    565:     MPI_Comm_rank(PetscObjectComm((PetscObject)pc),&rank);
    566:     VecSetValue(pcnn->coarse_b,rank,value,INSERT_VALUES);
    567:     /*
    568:        Since we are only inserting local values (one value actually) we don't need to do the
    569:        reduction that tells us there is no data that needs to be moved. Hence we comment out these
    570:        VecAssemblyBegin(pcnn->coarse_b);
    571:        VecAssemblyEnd  (pcnn->coarse_b);
    572:     */
    573:   }
    574:   KSPSolve(pcnn->ksp_coarse,pcnn->coarse_b,pcnn->coarse_x);
    575:   if (!u) { VecScale(pcnn->coarse_x,-1.0); }
    576:   VecGetArray(pcnn->coarse_x,&lambda);
    577:   for (k=0; k<pcis->n_shared[0]; k++) work_N[pcis->shared[0][k]] = *lambda * pcnn->DZ_IN[0][k];
    578:   VecRestoreArray(pcnn->coarse_x,&lambda);
    579:   PCISScatterArrayNToVecB(work_N,vec2_B,INSERT_VALUES,SCATTER_FORWARD,pc);
    580:   VecSet(z,0.0);
    581:   VecScatterBegin(pcis->global_to_B,vec2_B,z,ADD_VALUES,SCATTER_REVERSE);
    582:   VecScatterEnd  (pcis->global_to_B,vec2_B,z,ADD_VALUES,SCATTER_REVERSE);
    583:   if (!u) {
    584:     VecScatterBegin(pcis->global_to_B,z,vec2_B,INSERT_VALUES,SCATTER_FORWARD);
    585:     VecScatterEnd  (pcis->global_to_B,z,vec2_B,INSERT_VALUES,SCATTER_FORWARD);
    586:     PCISApplySchur(pc,vec2_B,vec1_B,(Vec)0,vec1_D,vec2_D);
    587:     VecCopy(r,z);
    588:   }
    589:   VecScatterBegin(pcis->global_to_B,vec1_B,z,ADD_VALUES,SCATTER_REVERSE);
    590:   VecScatterEnd  (pcis->global_to_B,vec1_B,z,ADD_VALUES,SCATTER_REVERSE);
    591:   PetscLogEventEnd(PC_ApplyCoarse,0,0,0,0);
    592:   return(0);
    593: }

    599: /*  -------   E N D   O F   T H E   C O D E   -------  */
    600: /*                                                     */
    601: /*  From now on, "footnotes" (or "historical notes").  */
    602: /*                                                     */
    603: /*  -------------------------------------------------  */

    607: /* --------------------------------------------------------------------------
    608:    Historical note 01
    609:    -------------------------------------------------------------------------- */
    610: /*
    611:    We considered the possibility of an alternative D_i that would still
    612:    provide a partition of unity (i.e., $ \sum_i  N_i D_i N_i^T = I $).
    613:    The basic principle was still the pseudo-inverse of the counting
    614:    function; the difference was that we would not count subdomains
    615:    that do not contribute to the coarse space (i.e., not pure-Neumann
    616:    subdomains).

    618:    This turned out to be a bad idea:  we would solve trivial Neumann
    619:    problems in the not pure-Neumann subdomains, since we would be scaling
    620:    the balanced residual by zero.
    621: */

    626: /* --------------------------------------------------------------------------
    627:    Historical note 02
    628:    -------------------------------------------------------------------------- */
    629: /*
    630:    We tried an alternative coarse problem, that would eliminate exactly a
    631:    constant error. Turned out not to improve the overall convergence.
    632: */