Date: 2022-03-31T04:48:03+00:00

Actual source code: bddc.c

      1: /* TODOLIST

      3:    Solvers
      4:    - Add support for cholesky for coarse solver (similar to local solvers)
      5:    - Propagate ksp prefixes for solvers to mat objects?

      7:    User interface
      8:    - ** DM attached to pc?

     10:    Debugging output
     11:    - * Better management of verbosity levels of debugging output

     13:    Extra
     14:    - *** Is it possible to work with PCBDDCGraph on boundary indices only (less memory consumed)?
     15:    - BDDC with MG framework?

     17:    MATIS related operations contained in BDDC code
     18:    - Provide general case for subassembling

     20: */

     22: #include <../src/ksp/pc/impls/bddc/bddc.h>
     23: #include <../src/ksp/pc/impls/bddc/bddcprivate.h>
     24: #include <petscblaslapack.h>

     26: static PetscBool PCBDDCPackageInitialized = PETSC_FALSE;

     28: static PetscBool  cited = PETSC_FALSE;
     29: static const char citation[] =
     30: "@article{ZampiniPCBDDC,\n"
     31: "author = {Stefano Zampini},\n"
     32: "title = {{PCBDDC}: A Class of Robust Dual-Primal Methods in {PETS}c},\n"
     33: "journal = {SIAM Journal on Scientific Computing},\n"
     34: "volume = {38},\n"
     35: "number = {5},\n"
     36: "pages = {S282-S306},\n"
     37: "year = {2016},\n"
     38: "doi = {10.1137/15M1025785},\n"
     39: "URL = {http://dx.doi.org/10.1137/15M1025785},\n"
     40: "eprint = {http://dx.doi.org/10.1137/15M1025785}\n"
     41: "}\n";

     43: PetscLogEvent PC_BDDC_Topology[PETSC_PCBDDC_MAXLEVELS];
     44: PetscLogEvent PC_BDDC_LocalSolvers[PETSC_PCBDDC_MAXLEVELS];
     45: PetscLogEvent PC_BDDC_LocalWork[PETSC_PCBDDC_MAXLEVELS];
     46: PetscLogEvent PC_BDDC_CorrectionSetUp[PETSC_PCBDDC_MAXLEVELS];
     47: PetscLogEvent PC_BDDC_ApproxSetUp[PETSC_PCBDDC_MAXLEVELS];
     48: PetscLogEvent PC_BDDC_ApproxApply[PETSC_PCBDDC_MAXLEVELS];
     49: PetscLogEvent PC_BDDC_CoarseSetUp[PETSC_PCBDDC_MAXLEVELS];
     50: PetscLogEvent PC_BDDC_CoarseSolver[PETSC_PCBDDC_MAXLEVELS];
     51: PetscLogEvent PC_BDDC_AdaptiveSetUp[PETSC_PCBDDC_MAXLEVELS];
     52: PetscLogEvent PC_BDDC_Scaling[PETSC_PCBDDC_MAXLEVELS];
     53: PetscLogEvent PC_BDDC_Schurs[PETSC_PCBDDC_MAXLEVELS];
     54: PetscLogEvent PC_BDDC_Solves[PETSC_PCBDDC_MAXLEVELS][3];

     56: const char *const PCBDDCInterfaceExtTypes[] = {"DIRICHLET","LUMP","PCBDDCInterfaceExtType","PC_BDDC_INTERFACE_EXT_",NULL};

     58: PetscErrorCode PCApply_BDDC(PC,Vec,Vec);

     60: PetscErrorCode PCSetFromOptions_BDDC(PetscOptionItems *PetscOptionsObject,PC pc)
     61: {
     62:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
     63:   PetscInt       nt,i;

     65:   PetscOptionsHead(PetscOptionsObject,"BDDC options");
     66:   /* Verbose debugging */
     67:   PetscOptionsInt("-pc_bddc_check_level","Verbose output for PCBDDC (intended for debug)","none",pcbddc->dbg_flag,&pcbddc->dbg_flag,NULL);
     68:   /* Approximate solvers */
     69:   PetscOptionsEnum("-pc_bddc_interface_ext_type","Use DIRICHLET or LUMP to extend interface corrections to interior","PCBDDCSetInterfaceExtType",PCBDDCInterfaceExtTypes,(PetscEnum)pcbddc->interface_extension,(PetscEnum*)&pcbddc->interface_extension,NULL);
     70:   if (pcbddc->interface_extension == PC_BDDC_INTERFACE_EXT_DIRICHLET) {
     71:     PetscOptionsBool("-pc_bddc_dirichlet_approximate","Inform PCBDDC that we are using approximate Dirichlet solvers","none",pcbddc->NullSpace_corr[0],&pcbddc->NullSpace_corr[0],NULL);
     72:     PetscOptionsBool("-pc_bddc_dirichlet_approximate_scale","Inform PCBDDC that we need to scale the Dirichlet solve","none",pcbddc->NullSpace_corr[1],&pcbddc->NullSpace_corr[1],NULL);
     73:   } else {
     74:     /* This flag is needed/implied by lumping */
     75:     pcbddc->switch_static = PETSC_TRUE;
     76:   }
     77:   PetscOptionsBool("-pc_bddc_neumann_approximate","Inform PCBDDC that we are using approximate Neumann solvers","none",pcbddc->NullSpace_corr[2],&pcbddc->NullSpace_corr[2],NULL);
     78:   PetscOptionsBool("-pc_bddc_neumann_approximate_scale","Inform PCBDDC that we need to scale the Neumann solve","none",pcbddc->NullSpace_corr[3],&pcbddc->NullSpace_corr[3],NULL);
     79:   /* Primal space customization */
     80:   PetscOptionsBool("-pc_bddc_use_local_mat_graph","Use or not adjacency graph of local mat for interface analysis","none",pcbddc->use_local_adj,&pcbddc->use_local_adj,NULL);
     81:   PetscOptionsInt("-pc_bddc_graph_maxcount","Maximum number of shared subdomains for a connected component","none",pcbddc->graphmaxcount,&pcbddc->graphmaxcount,NULL);
     82:   PetscOptionsBool("-pc_bddc_corner_selection","Activates face-based corner selection","none",pcbddc->corner_selection,&pcbddc->corner_selection,NULL);
     83:   PetscOptionsBool("-pc_bddc_use_vertices","Use or not corner dofs in coarse space","none",pcbddc->use_vertices,&pcbddc->use_vertices,NULL);
     84:   PetscOptionsBool("-pc_bddc_use_edges","Use or not edge constraints in coarse space","none",pcbddc->use_edges,&pcbddc->use_edges,NULL);
     85:   PetscOptionsBool("-pc_bddc_use_faces","Use or not face constraints in coarse space","none",pcbddc->use_faces,&pcbddc->use_faces,NULL);
     86:   PetscOptionsInt("-pc_bddc_vertex_size","Connected components smaller or equal to vertex size will be considered as primal vertices","none",pcbddc->vertex_size,&pcbddc->vertex_size,NULL);
     87:   PetscOptionsBool("-pc_bddc_use_nnsp","Use near null space attached to the matrix to compute constraints","none",pcbddc->use_nnsp,&pcbddc->use_nnsp,NULL);
     88:   PetscOptionsBool("-pc_bddc_use_nnsp_true","Use near null space attached to the matrix to compute constraints as is","none",pcbddc->use_nnsp_true,&pcbddc->use_nnsp_true,NULL);
     89:   PetscOptionsBool("-pc_bddc_use_qr_single","Use QR factorization for single constraints on cc (QR is always used when multiple constraints are present)","none",pcbddc->use_qr_single,&pcbddc->use_qr_single,NULL);
     90:   /* Change of basis */
     91:   PetscOptionsBool("-pc_bddc_use_change_of_basis","Use or not internal change of basis on local edge nodes","none",pcbddc->use_change_of_basis,&pcbddc->use_change_of_basis,NULL);
     92:   PetscOptionsBool("-pc_bddc_use_change_on_faces","Use or not internal change of basis on local face nodes","none",pcbddc->use_change_on_faces,&pcbddc->use_change_on_faces,NULL);
     93:   if (!pcbddc->use_change_of_basis) {
     94:     pcbddc->use_change_on_faces = PETSC_FALSE;
     95:   }
     96:   /* Switch between M_2 (default) and M_3 preconditioners (as defined by C. Dohrmann in the ref. article) */
     97:   PetscOptionsBool("-pc_bddc_switch_static","Switch on static condensation ops around the interface preconditioner","none",pcbddc->switch_static,&pcbddc->switch_static,NULL);
     98:   PetscOptionsInt("-pc_bddc_coarse_eqs_per_proc","Target number of equations per process for coarse problem redistribution (significant only at the coarsest level)","none",pcbddc->coarse_eqs_per_proc,&pcbddc->coarse_eqs_per_proc,NULL);
     99:   i    = pcbddc->coarsening_ratio;
    100:   PetscOptionsInt("-pc_bddc_coarsening_ratio","Set coarsening ratio used in multilevel coarsening","PCBDDCSetCoarseningRatio",i,&i,NULL);
    101:   PCBDDCSetCoarseningRatio(pc,i);
    102:   i    = pcbddc->max_levels;
    103:   PetscOptionsInt("-pc_bddc_levels","Set maximum number of levels for multilevel","PCBDDCSetLevels",i,&i,NULL);
    104:   PCBDDCSetLevels(pc,i);
    105:   PetscOptionsInt("-pc_bddc_coarse_eqs_limit","Set maximum number of equations on coarsest grid to aim for","none",pcbddc->coarse_eqs_limit,&pcbddc->coarse_eqs_limit,NULL);
    106:   PetscOptionsBool("-pc_bddc_use_coarse_estimates","Use estimated eigenvalues for coarse problem","none",pcbddc->use_coarse_estimates,&pcbddc->use_coarse_estimates,NULL);
    107:   PetscOptionsBool("-pc_bddc_use_deluxe_scaling","Use deluxe scaling for BDDC","none",pcbddc->use_deluxe_scaling,&pcbddc->use_deluxe_scaling,NULL);
    108:   PetscOptionsBool("-pc_bddc_schur_rebuild","Whether or not the interface graph for Schur principal minors has to be rebuilt (i.e. define the interface without any adjacency)","none",pcbddc->sub_schurs_rebuild,&pcbddc->sub_schurs_rebuild,NULL);
    109:   PetscOptionsInt("-pc_bddc_schur_layers","Number of dofs' layers for the computation of principal minors (i.e. -1 uses all dofs)","none",pcbddc->sub_schurs_layers,&pcbddc->sub_schurs_layers,NULL);
    110:   PetscOptionsBool("-pc_bddc_schur_use_useradj","Whether or not the CSR graph specified by the user should be used for computing successive layers (default is to use adj of local mat)","none",pcbddc->sub_schurs_use_useradj,&pcbddc->sub_schurs_use_useradj,NULL);
    111:   PetscOptionsBool("-pc_bddc_schur_exact","Whether or not to use the exact Schur complement instead of the reduced one (which excludes size 1 cc)","none",pcbddc->sub_schurs_exact_schur,&pcbddc->sub_schurs_exact_schur,NULL);
    112:   PetscOptionsBool("-pc_bddc_deluxe_zerorows","Zero rows and columns of deluxe operators associated with primal dofs","none",pcbddc->deluxe_zerorows,&pcbddc->deluxe_zerorows,NULL);
    113:   PetscOptionsBool("-pc_bddc_deluxe_singlemat","Collapse deluxe operators","none",pcbddc->deluxe_singlemat,&pcbddc->deluxe_singlemat,NULL);
    114:   PetscOptionsBool("-pc_bddc_adaptive_userdefined","Use user-defined constraints (should be attached via MatSetNearNullSpace to pmat) in addition to those adaptively generated","none",pcbddc->adaptive_userdefined,&pcbddc->adaptive_userdefined,NULL);
    115:   nt   = 2;
    116:   PetscOptionsRealArray("-pc_bddc_adaptive_threshold","Thresholds to be used for adaptive selection of constraints","none",pcbddc->adaptive_threshold,&nt,NULL);
    117:   if (nt == 1) pcbddc->adaptive_threshold[1] = pcbddc->adaptive_threshold[0];
    118:   PetscOptionsInt("-pc_bddc_adaptive_nmin","Minimum number of constraints per connected components","none",pcbddc->adaptive_nmin,&pcbddc->adaptive_nmin,NULL);
    119:   PetscOptionsInt("-pc_bddc_adaptive_nmax","Maximum number of constraints per connected components","none",pcbddc->adaptive_nmax,&pcbddc->adaptive_nmax,NULL);
    120:   PetscOptionsBool("-pc_bddc_symmetric","Symmetric computation of primal basis functions","none",pcbddc->symmetric_primal,&pcbddc->symmetric_primal,NULL);
    121:   PetscOptionsInt("-pc_bddc_coarse_adj","Number of processors where to map the coarse adjacency list","none",pcbddc->coarse_adj_red,&pcbddc->coarse_adj_red,NULL);
    122:   PetscOptionsBool("-pc_bddc_benign_trick","Apply the benign subspace trick to saddle point problems with discontinuous pressures","none",pcbddc->benign_saddle_point,&pcbddc->benign_saddle_point,NULL);
    123:   PetscOptionsBool("-pc_bddc_benign_change","Compute the pressure change of basis explicitly","none",pcbddc->benign_change_explicit,&pcbddc->benign_change_explicit,NULL);
    124:   PetscOptionsBool("-pc_bddc_benign_compute_correction","Compute the benign correction during PreSolve","none",pcbddc->benign_compute_correction,&pcbddc->benign_compute_correction,NULL);
    125:   PetscOptionsBool("-pc_bddc_nonetflux","Automatic computation of no-net-flux quadrature weights","none",pcbddc->compute_nonetflux,&pcbddc->compute_nonetflux,NULL);
    126:   PetscOptionsBool("-pc_bddc_detect_disconnected","Detects disconnected subdomains","none",pcbddc->detect_disconnected,&pcbddc->detect_disconnected,NULL);
    127:   PetscOptionsBool("-pc_bddc_detect_disconnected_filter","Filters out small entries in the local matrix when detecting disconnected subdomains","none",pcbddc->detect_disconnected_filter,&pcbddc->detect_disconnected_filter,NULL);
    128:   PetscOptionsBool("-pc_bddc_eliminate_dirichlet","Whether or not we want to eliminate dirichlet dofs during presolve","none",pcbddc->eliminate_dirdofs,&pcbddc->eliminate_dirdofs,NULL);
    129:   PetscOptionsTail();
    130:   return 0;
    131: }

    133: static PetscErrorCode PCView_BDDC(PC pc,PetscViewer viewer)
    134: {
    135:   PC_BDDC              *pcbddc = (PC_BDDC*)pc->data;
    136:   PC_IS                *pcis = (PC_IS*)pc->data;
    137:   PetscBool            isascii;
    138:   PetscSubcomm         subcomm;
    139:   PetscViewer          subviewer;

    141:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
    142:   /* ASCII viewer */
    143:   if (isascii) {
    144:     PetscMPIInt   color,rank,size;
    145:     PetscInt64    loc[7],gsum[6],gmax[6],gmin[6],totbenign;
    146:     PetscScalar   interface_size;
    147:     PetscReal     ratio1=0.,ratio2=0.;
    148:     Vec           counter;

    150:     if (!pc->setupcalled) {
    151:       PetscViewerASCIIPrintf(viewer,"  Partial information available: preconditioner has not been setup yet\n");
    152:     }
    153:     PetscViewerASCIIPrintf(viewer,"  Use verbose output: %D\n",pcbddc->dbg_flag);
    154:     PetscViewerASCIIPrintf(viewer,"  Use user-defined CSR: %d\n",!!pcbddc->mat_graph->nvtxs_csr);
    155:     PetscViewerASCIIPrintf(viewer,"  Use local mat graph: %d\n",pcbddc->use_local_adj && !pcbddc->mat_graph->nvtxs_csr);
    156:     if (pcbddc->mat_graph->twodim) {
    157:       PetscViewerASCIIPrintf(viewer,"  Connectivity graph topological dimension: 2\n");
    158:     } else {
    159:       PetscViewerASCIIPrintf(viewer,"  Connectivity graph topological dimension: 3\n");
    160:     }
    161:     if (pcbddc->graphmaxcount != PETSC_MAX_INT) {
    162:       PetscViewerASCIIPrintf(viewer,"  Graph max count: %D\n",pcbddc->graphmaxcount);
    163:     }
    164:     PetscViewerASCIIPrintf(viewer,"  Corner selection: %d (selected %d)\n",pcbddc->corner_selection,pcbddc->corner_selected);
    165:     PetscViewerASCIIPrintf(viewer,"  Use vertices: %d (vertex size %D)\n",pcbddc->use_vertices,pcbddc->vertex_size);
    166:     PetscViewerASCIIPrintf(viewer,"  Use edges: %d\n",pcbddc->use_edges);
    167:     PetscViewerASCIIPrintf(viewer,"  Use faces: %d\n",pcbddc->use_faces);
    168:     PetscViewerASCIIPrintf(viewer,"  Use true near null space: %d\n",pcbddc->use_nnsp_true);
    169:     PetscViewerASCIIPrintf(viewer,"  Use QR for single constraints on cc: %d\n",pcbddc->use_qr_single);
    170:     PetscViewerASCIIPrintf(viewer,"  Use change of basis on local edge nodes: %d\n",pcbddc->use_change_of_basis);
    171:     PetscViewerASCIIPrintf(viewer,"  Use change of basis on local face nodes: %d\n",pcbddc->use_change_on_faces);
    172:     PetscViewerASCIIPrintf(viewer,"  User defined change of basis matrix: %d\n",!!pcbddc->user_ChangeOfBasisMatrix);
    173:     PetscViewerASCIIPrintf(viewer,"  Has change of basis matrix: %d\n",!!pcbddc->ChangeOfBasisMatrix);
    174:     PetscViewerASCIIPrintf(viewer,"  Eliminate dirichlet boundary dofs: %d\n",pcbddc->eliminate_dirdofs);
    175:     PetscViewerASCIIPrintf(viewer,"  Switch on static condensation ops around the interface preconditioner: %d\n",pcbddc->switch_static);
    176:     PetscViewerASCIIPrintf(viewer,"  Use exact dirichlet trick: %d\n",pcbddc->use_exact_dirichlet_trick);
    177:     PetscViewerASCIIPrintf(viewer,"  Interface extension: %s\n",PCBDDCInterfaceExtTypes[pcbddc->interface_extension]);
    178:     PetscViewerASCIIPrintf(viewer,"  Multilevel max levels: %D\n",pcbddc->max_levels);
    179:     PetscViewerASCIIPrintf(viewer,"  Multilevel coarsening ratio: %D\n",pcbddc->coarsening_ratio);
    180:     PetscViewerASCIIPrintf(viewer,"  Use estimated eigs for coarse problem: %d\n",pcbddc->use_coarse_estimates);
    181:     PetscViewerASCIIPrintf(viewer,"  Use deluxe scaling: %d\n",pcbddc->use_deluxe_scaling);
    182:     PetscViewerASCIIPrintf(viewer,"  Use deluxe zerorows: %d\n",pcbddc->deluxe_zerorows);
    183:     PetscViewerASCIIPrintf(viewer,"  Use deluxe singlemat: %d\n",pcbddc->deluxe_singlemat);
    184:     PetscViewerASCIIPrintf(viewer,"  Rebuild interface graph for Schur principal minors: %d\n",pcbddc->sub_schurs_rebuild);
    185:     PetscViewerASCIIPrintf(viewer,"  Number of dofs' layers for the computation of principal minors: %D\n",pcbddc->sub_schurs_layers);
    186:     PetscViewerASCIIPrintf(viewer,"  Use user CSR graph to compute successive layers: %d\n",pcbddc->sub_schurs_use_useradj);
    187:     if (pcbddc->adaptive_threshold[1] != pcbddc->adaptive_threshold[0]) {
    188:       PetscViewerASCIIPrintf(viewer,"  Adaptive constraint selection thresholds (active %d, userdefined %d): %g,%g\n",pcbddc->adaptive_selection,pcbddc->adaptive_userdefined,pcbddc->adaptive_threshold[0],pcbddc->adaptive_threshold[1]);
    189:     } else {
    190:       PetscViewerASCIIPrintf(viewer,"  Adaptive constraint selection threshold (active %d, userdefined %d): %g\n",pcbddc->adaptive_selection,pcbddc->adaptive_userdefined,pcbddc->adaptive_threshold[0]);
    191:     }
    192:     PetscViewerASCIIPrintf(viewer,"  Min constraints / connected component: %D\n",pcbddc->adaptive_nmin);
    193:     PetscViewerASCIIPrintf(viewer,"  Max constraints / connected component: %D\n",pcbddc->adaptive_nmax);
    194:     PetscViewerASCIIPrintf(viewer,"  Invert exact Schur complement for adaptive selection: %d\n",pcbddc->sub_schurs_exact_schur);
    195:     PetscViewerASCIIPrintf(viewer,"  Symmetric computation of primal basis functions: %d\n",pcbddc->symmetric_primal);
    196:     PetscViewerASCIIPrintf(viewer,"  Num. Procs. to map coarse adjacency list: %D\n",pcbddc->coarse_adj_red);
    197:     PetscViewerASCIIPrintf(viewer,"  Coarse eqs per proc (significant at the coarsest level): %D\n",pcbddc->coarse_eqs_per_proc);
    198:     PetscViewerASCIIPrintf(viewer,"  Detect disconnected: %d (filter %d)\n",pcbddc->detect_disconnected,pcbddc->detect_disconnected_filter);
    199:     PetscViewerASCIIPrintf(viewer,"  Benign subspace trick: %d (change explicit %d)\n",pcbddc->benign_saddle_point,pcbddc->benign_change_explicit);
    200:     PetscViewerASCIIPrintf(viewer,"  Benign subspace trick is active: %d\n",pcbddc->benign_have_null);
    201:     PetscViewerASCIIPrintf(viewer,"  Algebraic computation of no-net-flux: %d\n",pcbddc->compute_nonetflux);
    202:     if (!pc->setupcalled) return 0;

    204:     /* compute interface size */
    205:     VecSet(pcis->vec1_B,1.0);
    206:     MatCreateVecs(pc->pmat,&counter,NULL);
    207:     VecSet(counter,0.0);
    208:     VecScatterBegin(pcis->global_to_B,pcis->vec1_B,counter,INSERT_VALUES,SCATTER_REVERSE);
    209:     VecScatterEnd(pcis->global_to_B,pcis->vec1_B,counter,INSERT_VALUES,SCATTER_REVERSE);
    210:     VecSum(counter,&interface_size);
    211:     VecDestroy(&counter);

    213:     /* compute some statistics on the domain decomposition */
    214:     gsum[0] = 1;
    215:     gsum[1] = gsum[2] = gsum[3] = gsum[4] = gsum[5] = 0;
    216:     loc[0]  = !!pcis->n;
    217:     loc[1]  = pcis->n - pcis->n_B;
    218:     loc[2]  = pcis->n_B;
    219:     loc[3]  = pcbddc->local_primal_size;
    220:     loc[4]  = pcis->n;
    221:     loc[5]  = pcbddc->n_local_subs > 0 ? pcbddc->n_local_subs : (pcis->n ? 1 : 0);
    222:     loc[6]  = pcbddc->benign_n;
    223:     MPI_Reduce(loc,gsum,6,MPIU_INT64,MPI_SUM,0,PetscObjectComm((PetscObject)pc));
    224:     if (!loc[0]) loc[1] = loc[2] = loc[3] = loc[4] = loc[5] = -1;
    225:     MPI_Reduce(loc,gmax,6,MPIU_INT64,MPI_MAX,0,PetscObjectComm((PetscObject)pc));
    226:     if (!loc[0]) loc[1] = loc[2] = loc[3] = loc[4] = loc[5] = PETSC_MAX_INT;
    227:     MPI_Reduce(loc,gmin,6,MPIU_INT64,MPI_MIN,0,PetscObjectComm((PetscObject)pc));
    228:     MPI_Reduce(&loc[6],&totbenign,1,MPIU_INT64,MPI_SUM,0,PetscObjectComm((PetscObject)pc));
    229:     if (pcbddc->coarse_size) {
    230:       ratio1 = pc->pmat->rmap->N/(1.*pcbddc->coarse_size);
    231:       ratio2 = PetscRealPart(interface_size)/pcbddc->coarse_size;
    232:     }
    233:     PetscViewerASCIIPrintf(viewer,"********************************** STATISTICS AT LEVEL %d **********************************\n",pcbddc->current_level);
    234:     PetscViewerASCIIPrintf(viewer,"  Global dofs sizes: all %D interface %D coarse %D\n",pc->pmat->rmap->N,(PetscInt)PetscRealPart(interface_size),pcbddc->coarse_size);
    235:     PetscViewerASCIIPrintf(viewer,"  Coarsening ratios: all/coarse %D interface/coarse %D\n",(PetscInt)ratio1,(PetscInt)ratio2);
    236:     PetscViewerASCIIPrintf(viewer,"  Active processes : %D\n",(PetscInt)gsum[0]);
    237:     PetscViewerASCIIPrintf(viewer,"  Total subdomains : %D\n",(PetscInt)gsum[5]);
    238:     if (pcbddc->benign_have_null) {
    239:       PetscViewerASCIIPrintf(viewer,"  Benign subs      : %D\n",(PetscInt)totbenign);
    240:     }
    241:     PetscViewerASCIIPrintf(viewer,"  Dofs type        :\tMIN\tMAX\tMEAN\n");
    242:     PetscViewerASCIIPrintf(viewer,"  Interior  dofs   :\t%D\t%D\t%D\n",(PetscInt)gmin[1],(PetscInt)gmax[1],(PetscInt)(gsum[1]/gsum[0]));
    243:     PetscViewerASCIIPrintf(viewer,"  Interface dofs   :\t%D\t%D\t%D\n",(PetscInt)gmin[2],(PetscInt)gmax[2],(PetscInt)(gsum[2]/gsum[0]));
    244:     PetscViewerASCIIPrintf(viewer,"  Primal    dofs   :\t%D\t%D\t%D\n",(PetscInt)gmin[3],(PetscInt)gmax[3],(PetscInt)(gsum[3]/gsum[0]));
    245:     PetscViewerASCIIPrintf(viewer,"  Local     dofs   :\t%D\t%D\t%D\n",(PetscInt)gmin[4],(PetscInt)gmax[4],(PetscInt)(gsum[4]/gsum[0]));
    246:     PetscViewerASCIIPrintf(viewer,"  Local     subs   :\t%D\t%D\n"    ,(PetscInt)gmin[5],(PetscInt)gmax[5]);
    247:     PetscViewerFlush(viewer);

    249:     MPI_Comm_rank(PetscObjectComm((PetscObject)pc),&rank);

    251:     /* local solvers */
    252:     PetscViewerGetSubViewer(viewer,PetscObjectComm((PetscObject)pcbddc->ksp_D),&subviewer);
    253:     if (rank == 0) {
    254:       PetscViewerASCIIPrintf(subviewer,"--- Interior solver (rank 0)\n");
    255:       PetscViewerASCIIPushTab(subviewer);
    256:       KSPView(pcbddc->ksp_D,subviewer);
    257:       PetscViewerASCIIPopTab(subviewer);
    258:       PetscViewerASCIIPrintf(subviewer,"--- Correction solver (rank 0)\n");
    259:       PetscViewerASCIIPushTab(subviewer);
    260:       KSPView(pcbddc->ksp_R,subviewer);
    261:       PetscViewerASCIIPopTab(subviewer);
    262:       PetscViewerFlush(subviewer);
    263:     }
    264:     PetscViewerRestoreSubViewer(viewer,PetscObjectComm((PetscObject)pcbddc->ksp_D),&subviewer);
    265:     PetscViewerFlush(viewer);

    267:     /* the coarse problem can be handled by a different communicator */
    268:     if (pcbddc->coarse_ksp) color = 1;
    269:     else color = 0;
    270:     MPI_Comm_size(PetscObjectComm((PetscObject)pc),&size);
    271:     PetscSubcommCreate(PetscObjectComm((PetscObject)pc),&subcomm);
    272:     PetscSubcommSetNumber(subcomm,PetscMin(size,2));
    273:     PetscSubcommSetTypeGeneral(subcomm,color,rank);
    274:     PetscViewerGetSubViewer(viewer,PetscSubcommChild(subcomm),&subviewer);
    275:     if (color == 1) {
    276:       PetscViewerASCIIPrintf(subviewer,"--- Coarse solver\n");
    277:       PetscViewerASCIIPushTab(subviewer);
    278:       KSPView(pcbddc->coarse_ksp,subviewer);
    279:       PetscViewerASCIIPopTab(subviewer);
    280:       PetscViewerFlush(subviewer);
    281:     }
    282:     PetscViewerRestoreSubViewer(viewer,PetscSubcommChild(subcomm),&subviewer);
    283:     PetscSubcommDestroy(&subcomm);
    284:     PetscViewerFlush(viewer);
    285:   }
    286:   return 0;
    287: }

    289: static PetscErrorCode PCBDDCSetDiscreteGradient_BDDC(PC pc, Mat G, PetscInt order, PetscInt field, PetscBool global, PetscBool conforming)
    290: {
    291:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;

    293:   PetscObjectReference((PetscObject)G);
    294:   MatDestroy(&pcbddc->discretegradient);
    295:   pcbddc->discretegradient = G;
    296:   pcbddc->nedorder         = order > 0 ? order : -order;
    297:   pcbddc->nedfield         = field;
    298:   pcbddc->nedglobal        = global;
    299:   pcbddc->conforming       = conforming;
    300:   return 0;
    301: }

    303: /*@
    304:  PCBDDCSetDiscreteGradient - Sets the discrete gradient

    306:    Collective on PC

    308:    Input Parameters:
    309: +  pc         - the preconditioning context
    310: .  G          - the discrete gradient matrix (should be in AIJ format)
    311: .  order      - the order of the Nedelec space (1 for the lowest order)
    312: .  field      - the field id of the Nedelec dofs (not used if the fields have not been specified)
    313: .  global     - the type of global ordering for the rows of G
    314: -  conforming - whether the mesh is conforming or not

    316:    Level: advanced

    318:    Notes:
    319:     The discrete gradient matrix G is used to analyze the subdomain edges, and it should not contain any zero entry.
    320:           For variable order spaces, the order should be set to zero.
    321:           If global is true, the rows of G should be given in global ordering for the whole dofs;
    322:           if false, the ordering should be global for the Nedelec field.
    323:           In the latter case, it should hold gid[i] < gid[j] iff geid[i] < geid[j], with gid the global orderding for all the dofs
    324:           and geid the one for the Nedelec field.

    326: .seealso: PCBDDC,PCBDDCSetDofsSplitting(),PCBDDCSetDofsSplittingLocal()
    327: @*/
    328: PetscErrorCode PCBDDCSetDiscreteGradient(PC pc, Mat G, PetscInt order, PetscInt field, PetscBool global, PetscBool conforming)
    329: {
    337:   PetscTryMethod(pc,"PCBDDCSetDiscreteGradient_C",(PC,Mat,PetscInt,PetscInt,PetscBool,PetscBool),(pc,G,order,field,global,conforming));
    338:   return 0;
    339: }

    341: static PetscErrorCode PCBDDCSetDivergenceMat_BDDC(PC pc, Mat divudotp, PetscBool trans, IS vl2l)
    342: {
    343:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;

    345:   PetscObjectReference((PetscObject)divudotp);
    346:   MatDestroy(&pcbddc->divudotp);
    347:   pcbddc->divudotp = divudotp;
    348:   pcbddc->divudotp_trans = trans;
    349:   pcbddc->compute_nonetflux = PETSC_TRUE;
    350:   if (vl2l) {
    351:     PetscObjectReference((PetscObject)vl2l);
    352:     ISDestroy(&pcbddc->divudotp_vl2l);
    353:     pcbddc->divudotp_vl2l = vl2l;
    354:   }
    355:   return 0;
    356: }

    358: /*@
    359:  PCBDDCSetDivergenceMat - Sets the linear operator representing \int_\Omega \div {\bf u} \cdot p dx

    361:    Collective on PC

    363:    Input Parameters:
    364: +  pc - the preconditioning context
    365: .  divudotp - the matrix (must be of type MATIS)
    366: .  trans - if trans if false (resp. true), then pressures are in the test (trial) space and velocities are in the trial (test) space.
    367: -  vl2l - optional index set describing the local (wrt the local matrix in divudotp) to local (wrt the local matrix in the preconditioning matrix) map for the velocities

    369:    Level: advanced

    371:    Notes:
    372:     This auxiliary matrix is used to compute quadrature weights representing the net-flux across subdomain boundaries
    373:           If vl2l is NULL, the local ordering for velocities in divudotp should match that of the preconditioning matrix

    375: .seealso: PCBDDC
    376: @*/
    377: PetscErrorCode PCBDDCSetDivergenceMat(PC pc, Mat divudotp, PetscBool trans, IS vl2l)
    378: {
    379:   PetscBool      ismatis;

    386:   PetscObjectTypeCompare((PetscObject)divudotp,MATIS,&ismatis);
    388:   PetscTryMethod(pc,"PCBDDCSetDivergenceMat_C",(PC,Mat,PetscBool,IS),(pc,divudotp,trans,vl2l));
    389:   return 0;
    390: }

    392: static PetscErrorCode PCBDDCSetChangeOfBasisMat_BDDC(PC pc, Mat change, PetscBool interior)
    393: {
    394:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;

    396:   PetscObjectReference((PetscObject)change);
    397:   MatDestroy(&pcbddc->user_ChangeOfBasisMatrix);
    398:   pcbddc->user_ChangeOfBasisMatrix = change;
    399:   pcbddc->change_interior = interior;
    400:   return 0;
    401: }
    402: /*@
    403:  PCBDDCSetChangeOfBasisMat - Set user defined change of basis for dofs

    405:    Collective on PC

    407:    Input Parameters:
    408: +  pc - the preconditioning context
    409: .  change - the change of basis matrix
    410: -  interior - whether or not the change of basis modifies interior dofs

    412:    Level: intermediate

    414:    Notes:

    416: .seealso: PCBDDC
    417: @*/
    418: PetscErrorCode PCBDDCSetChangeOfBasisMat(PC pc, Mat change, PetscBool interior)
    419: {
    423:   if (pc->mat) {
    424:     PetscInt rows_c,cols_c,rows,cols;
    425:     MatGetSize(pc->mat,&rows,&cols);
    426:     MatGetSize(change,&rows_c,&cols_c);
    429:     MatGetLocalSize(pc->mat,&rows,&cols);
    430:     MatGetLocalSize(change,&rows_c,&cols_c);
    433:   }
    434:   PetscTryMethod(pc,"PCBDDCSetChangeOfBasisMat_C",(PC,Mat,PetscBool),(pc,change,interior));
    435:   return 0;
    436: }

    438: static PetscErrorCode PCBDDCSetPrimalVerticesIS_BDDC(PC pc, IS PrimalVertices)
    439: {
    440:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
    441:   PetscBool      isequal = PETSC_FALSE;

    443:   PetscObjectReference((PetscObject)PrimalVertices);
    444:   if (pcbddc->user_primal_vertices) {
    445:     ISEqual(PrimalVertices,pcbddc->user_primal_vertices,&isequal);
    446:   }
    447:   ISDestroy(&pcbddc->user_primal_vertices);
    448:   ISDestroy(&pcbddc->user_primal_vertices_local);
    449:   pcbddc->user_primal_vertices = PrimalVertices;
    450:   if (!isequal) pcbddc->recompute_topography = PETSC_TRUE;
    451:   return 0;
    452: }

    454: /*@
    455:  PCBDDCSetPrimalVerticesIS - Set additional user defined primal vertices in PCBDDC

    457:    Collective

    459:    Input Parameters:
    460: +  pc - the preconditioning context
    461: -  PrimalVertices - index set of primal vertices in global numbering (can be empty)

    463:    Level: intermediate

    465:    Notes:
    466:      Any process can list any global node

    468: .seealso: PCBDDC, PCBDDCGetPrimalVerticesIS(), PCBDDCSetPrimalVerticesLocalIS(), PCBDDCGetPrimalVerticesLocalIS()
    469: @*/
    470: PetscErrorCode PCBDDCSetPrimalVerticesIS(PC pc, IS PrimalVertices)
    471: {
    475:   PetscTryMethod(pc,"PCBDDCSetPrimalVerticesIS_C",(PC,IS),(pc,PrimalVertices));
    476:   return 0;
    477: }

    479: static PetscErrorCode PCBDDCGetPrimalVerticesIS_BDDC(PC pc, IS *is)
    480: {
    481:   PC_BDDC *pcbddc = (PC_BDDC*)pc->data;

    483:   *is = pcbddc->user_primal_vertices;
    484:   return 0;
    485: }

    487: /*@
    488:  PCBDDCGetPrimalVerticesIS - Get user defined primal vertices set with PCBDDCSetPrimalVerticesIS()

    490:    Collective

    492:    Input Parameters:
    493: .  pc - the preconditioning context

    495:    Output Parameters:
    496: .  is - index set of primal vertices in global numbering (NULL if not set)

    498:    Level: intermediate

    500:    Notes:

    502: .seealso: PCBDDC, PCBDDCSetPrimalVerticesIS(), PCBDDCSetPrimalVerticesLocalIS(), PCBDDCGetPrimalVerticesLocalIS()
    503: @*/
    504: PetscErrorCode PCBDDCGetPrimalVerticesIS(PC pc, IS *is)
    505: {
    508:   PetscUseMethod(pc,"PCBDDCGetPrimalVerticesIS_C",(PC,IS*),(pc,is));
    509:   return 0;
    510: }

    512: static PetscErrorCode PCBDDCSetPrimalVerticesLocalIS_BDDC(PC pc, IS PrimalVertices)
    513: {
    514:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
    515:   PetscBool      isequal = PETSC_FALSE;

    517:   PetscObjectReference((PetscObject)PrimalVertices);
    518:   if (pcbddc->user_primal_vertices_local) {
    519:     ISEqual(PrimalVertices,pcbddc->user_primal_vertices_local,&isequal);
    520:   }
    521:   ISDestroy(&pcbddc->user_primal_vertices);
    522:   ISDestroy(&pcbddc->user_primal_vertices_local);
    523:   pcbddc->user_primal_vertices_local = PrimalVertices;
    524:   if (!isequal) pcbddc->recompute_topography = PETSC_TRUE;
    525:   return 0;
    526: }

    528: /*@
    529:  PCBDDCSetPrimalVerticesLocalIS - Set additional user defined primal vertices in PCBDDC

    531:    Collective

    533:    Input Parameters:
    534: +  pc - the preconditioning context
    535: -  PrimalVertices - index set of primal vertices in local numbering (can be empty)

    537:    Level: intermediate

    539:    Notes:

    541: .seealso: PCBDDC, PCBDDCSetPrimalVerticesIS(), PCBDDCGetPrimalVerticesIS(), PCBDDCGetPrimalVerticesLocalIS()
    542: @*/
    543: PetscErrorCode PCBDDCSetPrimalVerticesLocalIS(PC pc, IS PrimalVertices)
    544: {
    548:   PetscTryMethod(pc,"PCBDDCSetPrimalVerticesLocalIS_C",(PC,IS),(pc,PrimalVertices));
    549:   return 0;
    550: }

    552: static PetscErrorCode PCBDDCGetPrimalVerticesLocalIS_BDDC(PC pc, IS *is)
    553: {
    554:   PC_BDDC *pcbddc = (PC_BDDC*)pc->data;

    556:   *is = pcbddc->user_primal_vertices_local;
    557:   return 0;
    558: }

    560: /*@
    561:  PCBDDCGetPrimalVerticesLocalIS - Get user defined primal vertices set with PCBDDCSetPrimalVerticesLocalIS()

    563:    Collective

    565:    Input Parameters:
    566: .  pc - the preconditioning context

    568:    Output Parameters:
    569: .  is - index set of primal vertices in local numbering (NULL if not set)

    571:    Level: intermediate

    573:    Notes:

    575: .seealso: PCBDDC, PCBDDCSetPrimalVerticesIS(), PCBDDCGetPrimalVerticesIS(), PCBDDCSetPrimalVerticesLocalIS()
    576: @*/
    577: PetscErrorCode PCBDDCGetPrimalVerticesLocalIS(PC pc, IS *is)
    578: {
    581:   PetscUseMethod(pc,"PCBDDCGetPrimalVerticesLocalIS_C",(PC,IS*),(pc,is));
    582:   return 0;
    583: }

    585: static PetscErrorCode PCBDDCSetCoarseningRatio_BDDC(PC pc,PetscInt k)
    586: {
    587:   PC_BDDC  *pcbddc = (PC_BDDC*)pc->data;

    589:   pcbddc->coarsening_ratio = k;
    590:   return 0;
    591: }

    593: /*@
    594:  PCBDDCSetCoarseningRatio - Set coarsening ratio used in multilevel

    596:    Logically collective on PC

    598:    Input Parameters:
    599: +  pc - the preconditioning context
    600: -  k - coarsening ratio (H/h at the coarser level)

    602:    Options Database Keys:
    603: .    -pc_bddc_coarsening_ratio <int> - Set coarsening ratio used in multilevel coarsening

    605:    Level: intermediate

    607:    Notes:
    608:      Approximatively k subdomains at the finer level will be aggregated into a single subdomain at the coarser level

    610: .seealso: PCBDDC, PCBDDCSetLevels()
    611: @*/
    612: PetscErrorCode PCBDDCSetCoarseningRatio(PC pc,PetscInt k)
    613: {
    616:   PetscTryMethod(pc,"PCBDDCSetCoarseningRatio_C",(PC,PetscInt),(pc,k));
    617:   return 0;
    618: }

    620: /* The following functions (PCBDDCSetUseExactDirichlet PCBDDCSetLevel) are not public */
    621: static PetscErrorCode PCBDDCSetUseExactDirichlet_BDDC(PC pc,PetscBool flg)
    622: {
    623:   PC_BDDC  *pcbddc = (PC_BDDC*)pc->data;

    625:   pcbddc->use_exact_dirichlet_trick = flg;
    626:   return 0;
    627: }

    629: PetscErrorCode PCBDDCSetUseExactDirichlet(PC pc,PetscBool flg)
    630: {
    633:   PetscTryMethod(pc,"PCBDDCSetUseExactDirichlet_C",(PC,PetscBool),(pc,flg));
    634:   return 0;
    635: }

    637: static PetscErrorCode PCBDDCSetLevel_BDDC(PC pc,PetscInt level)
    638: {
    639:   PC_BDDC  *pcbddc = (PC_BDDC*)pc->data;

    641:   pcbddc->current_level = level;
    642:   return 0;
    643: }

    645: PetscErrorCode PCBDDCSetLevel(PC pc,PetscInt level)
    646: {
    649:   PetscTryMethod(pc,"PCBDDCSetLevel_C",(PC,PetscInt),(pc,level));
    650:   return 0;
    651: }

    653: static PetscErrorCode PCBDDCSetLevels_BDDC(PC pc,PetscInt levels)
    654: {
    655:   PC_BDDC  *pcbddc = (PC_BDDC*)pc->data;

    658:   pcbddc->max_levels = levels;
    659:   return 0;
    660: }

    662: /*@
    663:  PCBDDCSetLevels - Sets the maximum number of additional levels allowed for multilevel BDDC

    665:    Logically collective on PC

    667:    Input Parameters:
    668: +  pc - the preconditioning context
    669: -  levels - the maximum number of levels

    671:    Options Database Keys:
    672: .    -pc_bddc_levels <int> - Set maximum number of levels for multilevel

    674:    Level: intermediate

    676:    Notes:
    677:      The default value is 0, that gives the classical two-levels BDDC

    679: .seealso: PCBDDC, PCBDDCSetCoarseningRatio()
    680: @*/
    681: PetscErrorCode PCBDDCSetLevels(PC pc,PetscInt levels)
    682: {
    685:   PetscTryMethod(pc,"PCBDDCSetLevels_C",(PC,PetscInt),(pc,levels));
    686:   return 0;
    687: }

    689: static PetscErrorCode PCBDDCSetDirichletBoundaries_BDDC(PC pc,IS DirichletBoundaries)
    690: {
    691:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
    692:   PetscBool      isequal = PETSC_FALSE;

    694:   PetscObjectReference((PetscObject)DirichletBoundaries);
    695:   if (pcbddc->DirichletBoundaries) {
    696:     ISEqual(DirichletBoundaries,pcbddc->DirichletBoundaries,&isequal);
    697:   }
    698:   /* last user setting takes precedence -> destroy any other customization */
    699:   ISDestroy(&pcbddc->DirichletBoundariesLocal);
    700:   ISDestroy(&pcbddc->DirichletBoundaries);
    701:   pcbddc->DirichletBoundaries = DirichletBoundaries;
    702:   if (!isequal) pcbddc->recompute_topography = PETSC_TRUE;
    703:   return 0;
    704: }

    706: /*@
    707:  PCBDDCSetDirichletBoundaries - Set IS defining Dirichlet boundaries for the global problem.

    709:    Collective

    711:    Input Parameters:
    712: +  pc - the preconditioning context
    713: -  DirichletBoundaries - parallel IS defining the Dirichlet boundaries

    715:    Level: intermediate

    717:    Notes:
    718:      Provide the information if you used MatZeroRows/Columns routines. Any process can list any global node

    720: .seealso: PCBDDC, PCBDDCSetDirichletBoundariesLocal(), MatZeroRows(), MatZeroRowsColumns()
    721: @*/
    722: PetscErrorCode PCBDDCSetDirichletBoundaries(PC pc,IS DirichletBoundaries)
    723: {
    727:   PetscTryMethod(pc,"PCBDDCSetDirichletBoundaries_C",(PC,IS),(pc,DirichletBoundaries));
    728:   return 0;
    729: }

    731: static PetscErrorCode PCBDDCSetDirichletBoundariesLocal_BDDC(PC pc,IS DirichletBoundaries)
    732: {
    733:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
    734:   PetscBool      isequal = PETSC_FALSE;

    736:   PetscObjectReference((PetscObject)DirichletBoundaries);
    737:   if (pcbddc->DirichletBoundariesLocal) {
    738:     ISEqual(DirichletBoundaries,pcbddc->DirichletBoundariesLocal,&isequal);
    739:   }
    740:   /* last user setting takes precedence -> destroy any other customization */
    741:   ISDestroy(&pcbddc->DirichletBoundariesLocal);
    742:   ISDestroy(&pcbddc->DirichletBoundaries);
    743:   pcbddc->DirichletBoundariesLocal = DirichletBoundaries;
    744:   if (!isequal) pcbddc->recompute_topography = PETSC_TRUE;
    745:   return 0;
    746: }

    748: /*@
    749:  PCBDDCSetDirichletBoundariesLocal - Set IS defining Dirichlet boundaries for the global problem in local ordering.

    751:    Collective

    753:    Input Parameters:
    754: +  pc - the preconditioning context
    755: -  DirichletBoundaries - parallel IS defining the Dirichlet boundaries (in local ordering)

    757:    Level: intermediate

    759:    Notes:

    761: .seealso: PCBDDC, PCBDDCSetDirichletBoundaries(), MatZeroRows(), MatZeroRowsColumns()
    762: @*/
    763: PetscErrorCode PCBDDCSetDirichletBoundariesLocal(PC pc,IS DirichletBoundaries)
    764: {
    768:   PetscTryMethod(pc,"PCBDDCSetDirichletBoundariesLocal_C",(PC,IS),(pc,DirichletBoundaries));
    769:   return 0;
    770: }

    772: static PetscErrorCode PCBDDCSetNeumannBoundaries_BDDC(PC pc,IS NeumannBoundaries)
    773: {
    774:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
    775:   PetscBool      isequal = PETSC_FALSE;

    777:   PetscObjectReference((PetscObject)NeumannBoundaries);
    778:   if (pcbddc->NeumannBoundaries) {
    779:     ISEqual(NeumannBoundaries,pcbddc->NeumannBoundaries,&isequal);
    780:   }
    781:   /* last user setting takes precedence -> destroy any other customization */
    782:   ISDestroy(&pcbddc->NeumannBoundariesLocal);
    783:   ISDestroy(&pcbddc->NeumannBoundaries);
    784:   pcbddc->NeumannBoundaries = NeumannBoundaries;
    785:   if (!isequal) pcbddc->recompute_topography = PETSC_TRUE;
    786:   return 0;
    787: }

    789: /*@
    790:  PCBDDCSetNeumannBoundaries - Set IS defining Neumann boundaries for the global problem.

    792:    Collective

    794:    Input Parameters:
    795: +  pc - the preconditioning context
    796: -  NeumannBoundaries - parallel IS defining the Neumann boundaries

    798:    Level: intermediate

    800:    Notes:
    801:      Any process can list any global node

    803: .seealso: PCBDDC, PCBDDCSetNeumannBoundariesLocal()
    804: @*/
    805: PetscErrorCode PCBDDCSetNeumannBoundaries(PC pc,IS NeumannBoundaries)
    806: {
    810:   PetscTryMethod(pc,"PCBDDCSetNeumannBoundaries_C",(PC,IS),(pc,NeumannBoundaries));
    811:   return 0;
    812: }

    814: static PetscErrorCode PCBDDCSetNeumannBoundariesLocal_BDDC(PC pc,IS NeumannBoundaries)
    815: {
    816:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
    817:   PetscBool      isequal = PETSC_FALSE;

    819:   PetscObjectReference((PetscObject)NeumannBoundaries);
    820:   if (pcbddc->NeumannBoundariesLocal) {
    821:     ISEqual(NeumannBoundaries,pcbddc->NeumannBoundariesLocal,&isequal);
    822:   }
    823:   /* last user setting takes precedence -> destroy any other customization */
    824:   ISDestroy(&pcbddc->NeumannBoundariesLocal);
    825:   ISDestroy(&pcbddc->NeumannBoundaries);
    826:   pcbddc->NeumannBoundariesLocal = NeumannBoundaries;
    827:   if (!isequal) pcbddc->recompute_topography = PETSC_TRUE;
    828:   return 0;
    829: }

    831: /*@
    832:  PCBDDCSetNeumannBoundariesLocal - Set IS defining Neumann boundaries for the global problem in local ordering.

    834:    Collective

    836:    Input Parameters:
    837: +  pc - the preconditioning context
    838: -  NeumannBoundaries - parallel IS defining the subdomain part of Neumann boundaries (in local ordering)

    840:    Level: intermediate

    842:    Notes:

    844: .seealso: PCBDDC, PCBDDCSetNeumannBoundaries()
    845: @*/
    846: PetscErrorCode PCBDDCSetNeumannBoundariesLocal(PC pc,IS NeumannBoundaries)
    847: {
    851:   PetscTryMethod(pc,"PCBDDCSetNeumannBoundariesLocal_C",(PC,IS),(pc,NeumannBoundaries));
    852:   return 0;
    853: }

    855: static PetscErrorCode PCBDDCGetDirichletBoundaries_BDDC(PC pc,IS *DirichletBoundaries)
    856: {
    857:   PC_BDDC  *pcbddc = (PC_BDDC*)pc->data;

    859:   *DirichletBoundaries = pcbddc->DirichletBoundaries;
    860:   return 0;
    861: }

    863: /*@
    864:  PCBDDCGetDirichletBoundaries - Get parallel IS for Dirichlet boundaries

    866:    Collective

    868:    Input Parameters:
    869: .  pc - the preconditioning context

    871:    Output Parameters:
    872: .  DirichletBoundaries - index set defining the Dirichlet boundaries

    874:    Level: intermediate

    876:    Notes:
    877:      The IS returned (if any) is the same passed in earlier by the user with PCBDDCSetDirichletBoundaries

    879: .seealso: PCBDDC
    880: @*/
    881: PetscErrorCode PCBDDCGetDirichletBoundaries(PC pc,IS *DirichletBoundaries)
    882: {
    884:   PetscUseMethod(pc,"PCBDDCGetDirichletBoundaries_C",(PC,IS*),(pc,DirichletBoundaries));
    885:   return 0;
    886: }

    888: static PetscErrorCode PCBDDCGetDirichletBoundariesLocal_BDDC(PC pc,IS *DirichletBoundaries)
    889: {
    890:   PC_BDDC  *pcbddc = (PC_BDDC*)pc->data;

    892:   *DirichletBoundaries = pcbddc->DirichletBoundariesLocal;
    893:   return 0;
    894: }

    896: /*@
    897:  PCBDDCGetDirichletBoundariesLocal - Get parallel IS for Dirichlet boundaries (in local ordering)

    899:    Collective

    901:    Input Parameters:
    902: .  pc - the preconditioning context

    904:    Output Parameters:
    905: .  DirichletBoundaries - index set defining the subdomain part of Dirichlet boundaries

    907:    Level: intermediate

    909:    Notes:
    910:      The IS returned could be the same passed in earlier by the user (if provided with PCBDDCSetDirichletBoundariesLocal) or a global-to-local map of the global IS (if provided with PCBDDCSetDirichletBoundaries).
    911:           In the latter case, the IS will be available after PCSetUp.

    913: .seealso: PCBDDC
    914: @*/
    915: PetscErrorCode PCBDDCGetDirichletBoundariesLocal(PC pc,IS *DirichletBoundaries)
    916: {
    918:   PetscUseMethod(pc,"PCBDDCGetDirichletBoundariesLocal_C",(PC,IS*),(pc,DirichletBoundaries));
    919:   return 0;
    920: }

    922: static PetscErrorCode PCBDDCGetNeumannBoundaries_BDDC(PC pc,IS *NeumannBoundaries)
    923: {
    924:   PC_BDDC  *pcbddc = (PC_BDDC*)pc->data;

    926:   *NeumannBoundaries = pcbddc->NeumannBoundaries;
    927:   return 0;
    928: }

    930: /*@
    931:  PCBDDCGetNeumannBoundaries - Get parallel IS for Neumann boundaries

    933:    Collective

    935:    Input Parameters:
    936: .  pc - the preconditioning context

    938:    Output Parameters:
    939: .  NeumannBoundaries - index set defining the Neumann boundaries

    941:    Level: intermediate

    943:    Notes:
    944:      The IS returned (if any) is the same passed in earlier by the user with PCBDDCSetNeumannBoundaries

    946: .seealso: PCBDDC
    947: @*/
    948: PetscErrorCode PCBDDCGetNeumannBoundaries(PC pc,IS *NeumannBoundaries)
    949: {
    951:   PetscUseMethod(pc,"PCBDDCGetNeumannBoundaries_C",(PC,IS*),(pc,NeumannBoundaries));
    952:   return 0;
    953: }

    955: static PetscErrorCode PCBDDCGetNeumannBoundariesLocal_BDDC(PC pc,IS *NeumannBoundaries)
    956: {
    957:   PC_BDDC  *pcbddc = (PC_BDDC*)pc->data;

    959:   *NeumannBoundaries = pcbddc->NeumannBoundariesLocal;
    960:   return 0;
    961: }

    963: /*@
    964:  PCBDDCGetNeumannBoundariesLocal - Get parallel IS for Neumann boundaries (in local ordering)

    966:    Collective

    968:    Input Parameters:
    969: .  pc - the preconditioning context

    971:    Output Parameters:
    972: .  NeumannBoundaries - index set defining the subdomain part of Neumann boundaries

    974:    Level: intermediate

    976:    Notes:
    977:      The IS returned could be the same passed in earlier by the user (if provided with PCBDDCSetNeumannBoundariesLocal) or a global-to-local map of the global IS (if provided with PCBDDCSetNeumannBoundaries).
    978:           In the latter case, the IS will be available after PCSetUp.

    980: .seealso: PCBDDC
    981: @*/
    982: PetscErrorCode PCBDDCGetNeumannBoundariesLocal(PC pc,IS *NeumannBoundaries)
    983: {
    985:   PetscUseMethod(pc,"PCBDDCGetNeumannBoundariesLocal_C",(PC,IS*),(pc,NeumannBoundaries));
    986:   return 0;
    987: }

    989: static PetscErrorCode PCBDDCSetLocalAdjacencyGraph_BDDC(PC pc, PetscInt nvtxs,const PetscInt xadj[],const PetscInt adjncy[], PetscCopyMode copymode)
    990: {
    991:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
    992:   PCBDDCGraph    mat_graph = pcbddc->mat_graph;
    993:   PetscBool      same_data = PETSC_FALSE;

    995:   if (!nvtxs) {
    996:     if (copymode == PETSC_OWN_POINTER) {
    997:       PetscFree(xadj);
    998:       PetscFree(adjncy);
    999:     }
    1000:     PCBDDCGraphResetCSR(mat_graph);
    1001:     return 0;
    1002:   }
    1003:   if (mat_graph->nvtxs == nvtxs && mat_graph->freecsr) { /* we own the data */
    1004:     if (mat_graph->xadj == xadj && mat_graph->adjncy == adjncy) same_data = PETSC_TRUE;
    1005:     if (!same_data && mat_graph->xadj[nvtxs] == xadj[nvtxs]) {
    1006:       PetscArraycmp(xadj,mat_graph->xadj,nvtxs+1,&same_data);
    1007:       if (same_data) {
    1008:         PetscArraycmp(adjncy,mat_graph->adjncy,xadj[nvtxs],&same_data);
    1009:       }
    1010:     }
    1011:   }
    1012:   if (!same_data) {
    1013:     /* free old CSR */
    1014:     PCBDDCGraphResetCSR(mat_graph);
    1015:     /* get CSR into graph structure */
    1016:     if (copymode == PETSC_COPY_VALUES) {
    1017:       PetscMalloc1(nvtxs+1,&mat_graph->xadj);
    1018:       PetscMalloc1(xadj[nvtxs],&mat_graph->adjncy);
    1019:       PetscArraycpy(mat_graph->xadj,xadj,nvtxs+1);
    1020:       PetscArraycpy(mat_graph->adjncy,adjncy,xadj[nvtxs]);
    1021:       mat_graph->freecsr = PETSC_TRUE;
    1022:     } else if (copymode == PETSC_OWN_POINTER) {
    1023:       mat_graph->xadj    = (PetscInt*)xadj;
    1024:       mat_graph->adjncy  = (PetscInt*)adjncy;
    1025:       mat_graph->freecsr = PETSC_TRUE;
    1026:     } else if (copymode == PETSC_USE_POINTER) {
    1027:       mat_graph->xadj    = (PetscInt*)xadj;
    1028:       mat_graph->adjncy  = (PetscInt*)adjncy;
    1029:       mat_graph->freecsr = PETSC_FALSE;
    1030:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported copy mode %D",copymode);
    1031:     mat_graph->nvtxs_csr = nvtxs;
    1032:     pcbddc->recompute_topography = PETSC_TRUE;
    1033:   }
    1034:   return 0;
    1035: }

    1037: /*@
    1038:  PCBDDCSetLocalAdjacencyGraph - Set adjacency structure (CSR graph) of the local degrees of freedom.

    1040:    Not collective

    1042:    Input Parameters:
    1043: +  pc - the preconditioning context.
    1044: .  nvtxs - number of local vertices of the graph (i.e., the number of local dofs).
    1045: .  xadj, adjncy - the connectivity of the dofs in CSR format.
    1046: -  copymode - supported modes are PETSC_COPY_VALUES, PETSC_USE_POINTER or PETSC_OWN_POINTER.

    1048:    Level: intermediate

    1050:    Notes:
    1051:     A dof is considered connected with all local dofs if xadj[dof+1]-xadj[dof] == 1 and adjncy[xadj[dof]] is negative.

    1053: .seealso: PCBDDC,PetscCopyMode
    1054: @*/
    1055: PetscErrorCode PCBDDCSetLocalAdjacencyGraph(PC pc,PetscInt nvtxs,const PetscInt xadj[],const PetscInt adjncy[], PetscCopyMode copymode)
    1056: {
    1057:   void (*f)(void) = NULL;

    1060:   if (nvtxs) {
    1063:   }
    1064:   PetscTryMethod(pc,"PCBDDCSetLocalAdjacencyGraph_C",(PC,PetscInt,const PetscInt[],const PetscInt[],PetscCopyMode),(pc,nvtxs,xadj,adjncy,copymode));
    1065:   /* free arrays if PCBDDC is not the PC type */
    1066:   PetscObjectQueryFunction((PetscObject)pc,"PCBDDCSetLocalAdjacencyGraph_C",&f);
    1067:   if (!f && copymode == PETSC_OWN_POINTER) {
    1068:     PetscFree(xadj);
    1069:     PetscFree(adjncy);
    1070:   }
    1071:   return 0;
    1072: }

    1074: static PetscErrorCode PCBDDCSetDofsSplittingLocal_BDDC(PC pc,PetscInt n_is, IS ISForDofs[])
    1075: {
    1076:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
    1077:   PetscInt       i;
    1078:   PetscBool      isequal = PETSC_FALSE;

    1080:   if (pcbddc->n_ISForDofsLocal == n_is) {
    1081:     for (i=0;i<n_is;i++) {
    1082:       PetscBool isequalt;
    1083:       ISEqual(ISForDofs[i],pcbddc->ISForDofsLocal[i],&isequalt);
    1084:       if (!isequalt) break;
    1085:     }
    1086:     if (i == n_is) isequal = PETSC_TRUE;
    1087:   }
    1088:   for (i=0;i<n_is;i++) {
    1089:     PetscObjectReference((PetscObject)ISForDofs[i]);
    1090:   }
    1091:   /* Destroy ISes if they were already set */
    1092:   for (i=0;i<pcbddc->n_ISForDofsLocal;i++) {
    1093:     ISDestroy(&pcbddc->ISForDofsLocal[i]);
    1094:   }
    1095:   PetscFree(pcbddc->ISForDofsLocal);
    1096:   /* last user setting takes precedence -> destroy any other customization */
    1097:   for (i=0;i<pcbddc->n_ISForDofs;i++) {
    1098:     ISDestroy(&pcbddc->ISForDofs[i]);
    1099:   }
    1100:   PetscFree(pcbddc->ISForDofs);
    1101:   pcbddc->n_ISForDofs = 0;
    1102:   /* allocate space then set */
    1103:   if (n_is) {
    1104:     PetscMalloc1(n_is,&pcbddc->ISForDofsLocal);
    1105:   }
    1106:   for (i=0;i<n_is;i++) {
    1107:     pcbddc->ISForDofsLocal[i] = ISForDofs[i];
    1108:   }
    1109:   pcbddc->n_ISForDofsLocal = n_is;
    1110:   if (n_is) pcbddc->user_provided_isfordofs = PETSC_TRUE;
    1111:   if (!isequal) pcbddc->recompute_topography = PETSC_TRUE;
    1112:   return 0;
    1113: }

    1115: /*@
    1116:  PCBDDCSetDofsSplittingLocal - Set index sets defining fields of the local subdomain matrix

    1118:    Collective

    1120:    Input Parameters:
    1121: +  pc - the preconditioning context
    1122: .  n_is - number of index sets defining the fields
    1123: -  ISForDofs - array of IS describing the fields in local ordering

    1125:    Level: intermediate

    1127:    Notes:
    1128:      n_is should be the same among processes. Not all nodes need to be listed: unlisted nodes will belong to the complement field.

    1130: .seealso: PCBDDC
    1131: @*/
    1132: PetscErrorCode PCBDDCSetDofsSplittingLocal(PC pc,PetscInt n_is, IS ISForDofs[])
    1133: {
    1134:   PetscInt       i;

    1138:   for (i=0;i<n_is;i++) {
    1141:   }
    1142:   PetscTryMethod(pc,"PCBDDCSetDofsSplittingLocal_C",(PC,PetscInt,IS[]),(pc,n_is,ISForDofs));
    1143:   return 0;
    1144: }

    1146: static PetscErrorCode PCBDDCSetDofsSplitting_BDDC(PC pc,PetscInt n_is, IS ISForDofs[])
    1147: {
    1148:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
    1149:   PetscInt       i;
    1150:   PetscBool      isequal = PETSC_FALSE;

    1152:   if (pcbddc->n_ISForDofs == n_is) {
    1153:     for (i=0;i<n_is;i++) {
    1154:       PetscBool isequalt;
    1155:       ISEqual(ISForDofs[i],pcbddc->ISForDofs[i],&isequalt);
    1156:       if (!isequalt) break;
    1157:     }
    1158:     if (i == n_is) isequal = PETSC_TRUE;
    1159:   }
    1160:   for (i=0;i<n_is;i++) {
    1161:     PetscObjectReference((PetscObject)ISForDofs[i]);
    1162:   }
    1163:   /* Destroy ISes if they were already set */
    1164:   for (i=0;i<pcbddc->n_ISForDofs;i++) {
    1165:     ISDestroy(&pcbddc->ISForDofs[i]);
    1166:   }
    1167:   PetscFree(pcbddc->ISForDofs);
    1168:   /* last user setting takes precedence -> destroy any other customization */
    1169:   for (i=0;i<pcbddc->n_ISForDofsLocal;i++) {
    1170:     ISDestroy(&pcbddc->ISForDofsLocal[i]);
    1171:   }
    1172:   PetscFree(pcbddc->ISForDofsLocal);
    1173:   pcbddc->n_ISForDofsLocal = 0;
    1174:   /* allocate space then set */
    1175:   if (n_is) {
    1176:     PetscMalloc1(n_is,&pcbddc->ISForDofs);
    1177:   }
    1178:   for (i=0;i<n_is;i++) {
    1179:     pcbddc->ISForDofs[i] = ISForDofs[i];
    1180:   }
    1181:   pcbddc->n_ISForDofs = n_is;
    1182:   if (n_is) pcbddc->user_provided_isfordofs = PETSC_TRUE;
    1183:   if (!isequal) pcbddc->recompute_topography = PETSC_TRUE;
    1184:   return 0;
    1185: }

    1187: /*@
    1188:  PCBDDCSetDofsSplitting - Set index sets defining fields of the global matrix

    1190:    Collective

    1192:    Input Parameters:
    1193: +  pc - the preconditioning context
    1194: .  n_is - number of index sets defining the fields
    1195: -  ISForDofs - array of IS describing the fields in global ordering

    1197:    Level: intermediate

    1199:    Notes:
    1200:      Any process can list any global node. Not all nodes need to be listed: unlisted nodes will belong to the complement field.

    1202: .seealso: PCBDDC
    1203: @*/
    1204: PetscErrorCode PCBDDCSetDofsSplitting(PC pc,PetscInt n_is, IS ISForDofs[])
    1205: {
    1206:   PetscInt       i;

    1210:   for (i=0;i<n_is;i++) {
    1213:   }
    1214:   PetscTryMethod(pc,"PCBDDCSetDofsSplitting_C",(PC,PetscInt,IS[]),(pc,n_is,ISForDofs));
    1215:   return 0;
    1216: }

    1218: /*
    1219:    PCPreSolve_BDDC - Changes the right hand side and (if necessary) the initial
    1220:                      guess if a transformation of basis approach has been selected.

    1222:    Input Parameter:
    1223: +  pc - the preconditioner context

    1225:    Application Interface Routine: PCPreSolve()

    1227:    Notes:
    1228:      The interface routine PCPreSolve() is not usually called directly by
    1229:    the user, but instead is called by KSPSolve().
    1230: */
    1231: static PetscErrorCode PCPreSolve_BDDC(PC pc, KSP ksp, Vec rhs, Vec x)
    1232: {
    1233:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
    1234:   PC_IS          *pcis = (PC_IS*)(pc->data);
    1235:   Vec            used_vec;
    1236:   PetscBool      iscg = PETSC_FALSE, save_rhs = PETSC_TRUE, benign_correction_computed;

    1238:   /* if we are working with CG, one dirichlet solve can be avoided during Krylov iterations */
    1239:   if (ksp) {
    1240:     PetscBool isgroppcg, ispipecg, ispipelcg, ispipecgrr;

    1242:     PetscObjectTypeCompare((PetscObject)ksp,KSPCG,&iscg);
    1243:     PetscObjectTypeCompare((PetscObject)ksp,KSPGROPPCG,&isgroppcg);
    1244:     PetscObjectTypeCompare((PetscObject)ksp,KSPPIPECG,&ispipecg);
    1245:     PetscObjectTypeCompare((PetscObject)ksp,KSPPIPECG,&ispipelcg);
    1246:     PetscObjectTypeCompare((PetscObject)ksp,KSPPIPECGRR,&ispipecgrr);
    1247:     iscg = (PetscBool)(iscg || isgroppcg || ispipecg || ispipelcg || ispipecgrr);
    1248:     if (pcbddc->benign_apply_coarse_only || pcbddc->switch_static || !iscg || pc->mat != pc->pmat) {
    1249:       PCBDDCSetUseExactDirichlet(pc,PETSC_FALSE);
    1250:     }
    1251:   }
    1252:   if (pcbddc->benign_apply_coarse_only || pcbddc->switch_static || pc->mat != pc->pmat) {
    1253:     PCBDDCSetUseExactDirichlet(pc,PETSC_FALSE);
    1254:   }

    1256:   /* Creates parallel work vectors used in presolve */
    1257:   if (!pcbddc->original_rhs) {
    1258:     VecDuplicate(pcis->vec1_global,&pcbddc->original_rhs);
    1259:   }
    1260:   if (!pcbddc->temp_solution) {
    1261:     VecDuplicate(pcis->vec1_global,&pcbddc->temp_solution);
    1262:   }

    1264:   pcbddc->temp_solution_used = PETSC_FALSE;
    1265:   if (x) {
    1266:     PetscObjectReference((PetscObject)x);
    1267:     used_vec = x;
    1268:   } else { /* it can only happen when calling PCBDDCMatFETIDPGetRHS */
    1269:     PetscObjectReference((PetscObject)pcbddc->temp_solution);
    1270:     used_vec = pcbddc->temp_solution;
    1271:     VecSet(used_vec,0.0);
    1272:     pcbddc->temp_solution_used = PETSC_TRUE;
    1273:     VecCopy(rhs,pcbddc->original_rhs);
    1274:     save_rhs = PETSC_FALSE;
    1275:     pcbddc->eliminate_dirdofs = PETSC_TRUE;
    1276:   }

    1278:   /* hack into ksp data structure since PCPreSolve comes earlier than setting to zero the guess in src/ksp/ksp/interface/itfunc.c */
    1279:   if (ksp) {
    1280:     /* store the flag for the initial guess since it will be restored back during PCPostSolve_BDDC */
    1281:     KSPGetInitialGuessNonzero(ksp,&pcbddc->ksp_guess_nonzero);
    1282:     if (!pcbddc->ksp_guess_nonzero) {
    1283:       VecSet(used_vec,0.0);
    1284:     }
    1285:   }

    1287:   pcbddc->rhs_change = PETSC_FALSE;
    1288:   /* Take into account zeroed rows -> change rhs and store solution removed */
    1289:   if (rhs && pcbddc->eliminate_dirdofs) {
    1290:     IS dirIS = NULL;

    1292:     /* DirichletBoundariesLocal may not be consistent among neighbours; gets a dirichlet dofs IS from graph (may be cached) */
    1293:     PCBDDCGraphGetDirichletDofs(pcbddc->mat_graph,&dirIS);
    1294:     if (dirIS) {
    1295:       Mat_IS            *matis = (Mat_IS*)pc->pmat->data;
    1296:       PetscInt          dirsize,i,*is_indices;
    1297:       PetscScalar       *array_x;
    1298:       const PetscScalar *array_diagonal;

    1300:       MatGetDiagonal(pc->pmat,pcis->vec1_global);
    1301:       VecPointwiseDivide(pcis->vec1_global,rhs,pcis->vec1_global);
    1302:       VecScatterBegin(matis->rctx,pcis->vec1_global,pcis->vec2_N,INSERT_VALUES,SCATTER_FORWARD);
    1303:       VecScatterEnd(matis->rctx,pcis->vec1_global,pcis->vec2_N,INSERT_VALUES,SCATTER_FORWARD);
    1304:       VecScatterBegin(matis->rctx,used_vec,pcis->vec1_N,INSERT_VALUES,SCATTER_FORWARD);
    1305:       VecScatterEnd(matis->rctx,used_vec,pcis->vec1_N,INSERT_VALUES,SCATTER_FORWARD);
    1306:       ISGetLocalSize(dirIS,&dirsize);
    1307:       VecGetArray(pcis->vec1_N,&array_x);
    1308:       VecGetArrayRead(pcis->vec2_N,&array_diagonal);
    1309:       ISGetIndices(dirIS,(const PetscInt**)&is_indices);
    1310:       for (i=0; i<dirsize; i++) array_x[is_indices[i]] = array_diagonal[is_indices[i]];
    1311:       ISRestoreIndices(dirIS,(const PetscInt**)&is_indices);
    1312:       VecRestoreArrayRead(pcis->vec2_N,&array_diagonal);
    1313:       VecRestoreArray(pcis->vec1_N,&array_x);
    1314:       VecScatterBegin(matis->rctx,pcis->vec1_N,used_vec,INSERT_VALUES,SCATTER_REVERSE);
    1315:       VecScatterEnd(matis->rctx,pcis->vec1_N,used_vec,INSERT_VALUES,SCATTER_REVERSE);
    1316:       pcbddc->rhs_change = PETSC_TRUE;
    1317:       ISDestroy(&dirIS);
    1318:     }
    1319:   }

    1321:   /* remove the computed solution or the initial guess from the rhs */
    1322:   if (pcbddc->rhs_change || (ksp && pcbddc->ksp_guess_nonzero)) {
    1323:     /* save the original rhs */
    1324:     if (save_rhs) {
    1325:       VecSwap(rhs,pcbddc->original_rhs);
    1326:       save_rhs = PETSC_FALSE;
    1327:     }
    1328:     pcbddc->rhs_change = PETSC_TRUE;
    1329:     VecScale(used_vec,-1.0);
    1330:     MatMultAdd(pc->mat,used_vec,pcbddc->original_rhs,rhs);
    1331:     VecScale(used_vec,-1.0);
    1332:     VecCopy(used_vec,pcbddc->temp_solution);
    1333:     pcbddc->temp_solution_used = PETSC_TRUE;
    1334:     if (ksp) {
    1335:       KSPSetInitialGuessNonzero(ksp,PETSC_FALSE);
    1336:     }
    1337:   }
    1338:   VecDestroy(&used_vec);

    1340:   /* compute initial vector in benign space if needed
    1341:      and remove non-benign solution from the rhs */
    1342:   benign_correction_computed = PETSC_FALSE;
    1343:   if (rhs && pcbddc->benign_compute_correction && (pcbddc->benign_have_null || pcbddc->benign_apply_coarse_only)) {
    1344:     /* compute u^*_h using ideas similar to those in Xuemin Tu's PhD thesis (see Section 4.8.1)
    1345:        Recursively apply BDDC in the multilevel case */
    1346:     if (!pcbddc->benign_vec) {
    1347:       VecDuplicate(rhs,&pcbddc->benign_vec);
    1348:     }
    1349:     /* keep applying coarse solver unless we no longer have benign subdomains */
    1350:     pcbddc->benign_apply_coarse_only = pcbddc->benign_have_null ? PETSC_TRUE : PETSC_FALSE;
    1351:     if (!pcbddc->benign_skip_correction) {
    1352:       PCApply_BDDC(pc,rhs,pcbddc->benign_vec);
    1353:       benign_correction_computed = PETSC_TRUE;
    1354:       if (pcbddc->temp_solution_used) {
    1355:         VecAXPY(pcbddc->temp_solution,1.0,pcbddc->benign_vec);
    1356:       }
    1357:       VecScale(pcbddc->benign_vec,-1.0);
    1358:       /* store the original rhs if not done earlier */
    1359:       if (save_rhs) {
    1360:         VecSwap(rhs,pcbddc->original_rhs);
    1361:       }
    1362:       if (pcbddc->rhs_change) {
    1363:         MatMultAdd(pc->mat,pcbddc->benign_vec,rhs,rhs);
    1364:       } else {
    1365:         MatMultAdd(pc->mat,pcbddc->benign_vec,pcbddc->original_rhs,rhs);
    1366:       }
    1367:       pcbddc->rhs_change = PETSC_TRUE;
    1368:     }
    1369:     pcbddc->benign_apply_coarse_only = PETSC_FALSE;
    1370:   } else {
    1371:     VecDestroy(&pcbddc->benign_vec);
    1372:   }

    1374:   /* dbg output */
    1375:   if (pcbddc->dbg_flag && benign_correction_computed) {
    1376:     Vec v;

    1378:     VecDuplicate(pcis->vec1_global,&v);
    1379:     if (pcbddc->ChangeOfBasisMatrix) {
    1380:       MatMultTranspose(pcbddc->ChangeOfBasisMatrix,rhs,v);
    1381:     } else {
    1382:       VecCopy(rhs,v);
    1383:     }
    1384:     PCBDDCBenignGetOrSetP0(pc,v,PETSC_TRUE);
    1385:     PetscViewerASCIIPrintf(pcbddc->dbg_viewer,"LEVEL %D: is the correction benign?\n",pcbddc->current_level);
    1386:     PetscScalarView(pcbddc->benign_n,pcbddc->benign_p0,pcbddc->dbg_viewer);
    1387:     PetscViewerFlush(pcbddc->dbg_viewer);
    1388:     VecDestroy(&v);
    1389:   }

    1391:   /* set initial guess if using PCG */
    1392:   pcbddc->exact_dirichlet_trick_app = PETSC_FALSE;
    1393:   if (x && pcbddc->use_exact_dirichlet_trick) {
    1394:     VecSet(x,0.0);
    1395:     if (pcbddc->ChangeOfBasisMatrix && pcbddc->change_interior) {
    1396:       if (benign_correction_computed) { /* we have already saved the changed rhs */
    1397:         VecLockReadPop(pcis->vec1_global);
    1398:       } else {
    1399:         MatMultTranspose(pcbddc->ChangeOfBasisMatrix,rhs,pcis->vec1_global);
    1400:       }
    1401:       VecScatterBegin(pcis->global_to_D,pcis->vec1_global,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
    1402:       VecScatterEnd(pcis->global_to_D,pcis->vec1_global,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
    1403:     } else {
    1404:       VecScatterBegin(pcis->global_to_D,rhs,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
    1405:       VecScatterEnd(pcis->global_to_D,rhs,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
    1406:     }
    1407:     PetscLogEventBegin(PC_BDDC_Solves[pcbddc->current_level][0],pc,0,0,0);
    1408:     KSPSolve(pcbddc->ksp_D,pcis->vec1_D,pcis->vec2_D);
    1409:     PetscLogEventEnd(PC_BDDC_Solves[pcbddc->current_level][0],pc,0,0,0);
    1410:     KSPCheckSolve(pcbddc->ksp_D,pc,pcis->vec2_D);
    1411:     if (pcbddc->ChangeOfBasisMatrix && pcbddc->change_interior) {
    1412:       VecSet(pcis->vec1_global,0.);
    1413:       VecScatterBegin(pcis->global_to_D,pcis->vec2_D,pcis->vec1_global,INSERT_VALUES,SCATTER_REVERSE);
    1414:       VecScatterEnd(pcis->global_to_D,pcis->vec2_D,pcis->vec1_global,INSERT_VALUES,SCATTER_REVERSE);
    1415:       MatMult(pcbddc->ChangeOfBasisMatrix,pcis->vec1_global,x);
    1416:     } else {
    1417:       VecScatterBegin(pcis->global_to_D,pcis->vec2_D,x,INSERT_VALUES,SCATTER_REVERSE);
    1418:       VecScatterEnd(pcis->global_to_D,pcis->vec2_D,x,INSERT_VALUES,SCATTER_REVERSE);
    1419:     }
    1420:     if (ksp) {
    1421:       KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
    1422:     }
    1423:     pcbddc->exact_dirichlet_trick_app = PETSC_TRUE;
    1424:   } else if (pcbddc->ChangeOfBasisMatrix && pcbddc->change_interior && benign_correction_computed && pcbddc->use_exact_dirichlet_trick) {
    1425:     VecLockReadPop(pcis->vec1_global);
    1426:   }
    1427:   return 0;
    1428: }

    1430: /*
    1431:    PCPostSolve_BDDC - Changes the computed solution if a transformation of basis
    1432:                      approach has been selected. Also, restores rhs to its original state.

    1434:    Input Parameter:
    1435: +  pc - the preconditioner context

    1437:    Application Interface Routine: PCPostSolve()

    1439:    Notes:
    1440:      The interface routine PCPostSolve() is not usually called directly by
    1441:      the user, but instead is called by KSPSolve().
    1442: */
    1443: static PetscErrorCode PCPostSolve_BDDC(PC pc, KSP ksp, Vec rhs, Vec x)
    1444: {
    1445:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;

    1447:   /* add solution removed in presolve */
    1448:   if (x && pcbddc->rhs_change) {
    1449:     if (pcbddc->temp_solution_used) {
    1450:       VecAXPY(x,1.0,pcbddc->temp_solution);
    1451:     } else if (pcbddc->benign_compute_correction && pcbddc->benign_vec) {
    1452:       VecAXPY(x,-1.0,pcbddc->benign_vec);
    1453:     }
    1454:     /* restore to original state (not for FETI-DP) */
    1455:     if (ksp) pcbddc->temp_solution_used = PETSC_FALSE;
    1456:   }

    1458:   /* restore rhs to its original state (not needed for FETI-DP) */
    1459:   if (rhs && pcbddc->rhs_change) {
    1460:     VecSwap(rhs,pcbddc->original_rhs);
    1461:     pcbddc->rhs_change = PETSC_FALSE;
    1462:   }
    1463:   /* restore ksp guess state */
    1464:   if (ksp) {
    1465:     KSPSetInitialGuessNonzero(ksp,pcbddc->ksp_guess_nonzero);
    1466:     /* reset flag for exact dirichlet trick */
    1467:     pcbddc->exact_dirichlet_trick_app = PETSC_FALSE;
    1468:   }
    1469:   return 0;
    1470: }

    1472: /*
    1473:    PCSetUp_BDDC - Prepares for the use of the BDDC preconditioner
    1474:                   by setting data structures and options.

    1476:    Input Parameter:
    1477: +  pc - the preconditioner context

    1479:    Application Interface Routine: PCSetUp()

    1481:    Notes:
    1482:      The interface routine PCSetUp() is not usually called directly by
    1483:      the user, but instead is called by PCApply() if necessary.
    1484: */
    1485: PetscErrorCode PCSetUp_BDDC(PC pc)
    1486: {
    1487:   PC_BDDC*        pcbddc = (PC_BDDC*)pc->data;
    1488:   PCBDDCSubSchurs sub_schurs;
    1489:   Mat_IS*         matis;
    1490:   MatNullSpace    nearnullspace;
    1491:   Mat             lA;
    1492:   IS              lP,zerodiag = NULL;
    1493:   PetscInt        nrows,ncols;
    1494:   PetscMPIInt     size;
    1495:   PetscBool       computesubschurs;
    1496:   PetscBool       computeconstraintsmatrix;
    1497:   PetscBool       new_nearnullspace_provided,ismatis,rl;

    1499:   PetscObjectTypeCompare((PetscObject)pc->pmat,MATIS,&ismatis);
    1501:   MatGetSize(pc->pmat,&nrows,&ncols);
    1503:   MPI_Comm_size(PetscObjectComm((PetscObject)pc),&size);

    1505:   matis = (Mat_IS*)pc->pmat->data;
    1506:   /* the following lines of code should be replaced by a better logic between PCIS, PCNN, PCBDDC and other future nonoverlapping preconditioners */
    1507:   /* For BDDC we need to define a local "Neumann" problem different to that defined in PCISSetup
    1508:      Also, BDDC builds its own KSP for the Dirichlet problem */
    1509:   rl = pcbddc->recompute_topography;
    1510:   if (!pc->setupcalled || pc->flag == DIFFERENT_NONZERO_PATTERN) rl = PETSC_TRUE;
    1511:   MPIU_Allreduce(&rl,&pcbddc->recompute_topography,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)pc));
    1512:   if (pcbddc->recompute_topography) {
    1513:     pcbddc->graphanalyzed    = PETSC_FALSE;
    1514:     computeconstraintsmatrix = PETSC_TRUE;
    1515:   } else {
    1516:     computeconstraintsmatrix = PETSC_FALSE;
    1517:   }

    1519:   /* check parameters' compatibility */
    1520:   if (!pcbddc->use_deluxe_scaling) pcbddc->deluxe_zerorows = PETSC_FALSE;
    1521:   pcbddc->adaptive_selection   = (PetscBool)(pcbddc->adaptive_threshold[0] != 0.0 || pcbddc->adaptive_threshold[1] != 0.0);
    1522:   pcbddc->use_deluxe_scaling   = (PetscBool)(pcbddc->use_deluxe_scaling && size > 1);
    1523:   pcbddc->adaptive_selection   = (PetscBool)(pcbddc->adaptive_selection && size > 1);
    1524:   pcbddc->adaptive_userdefined = (PetscBool)(pcbddc->adaptive_selection && pcbddc->adaptive_userdefined);
    1525:   if (pcbddc->adaptive_selection) pcbddc->use_faces = PETSC_TRUE;

    1527:   computesubschurs = (PetscBool)(pcbddc->adaptive_selection || pcbddc->use_deluxe_scaling);

    1529:   /* activate all connected components if the netflux has been requested */
    1530:   if (pcbddc->compute_nonetflux) {
    1531:     pcbddc->use_vertices = PETSC_TRUE;
    1532:     pcbddc->use_edges    = PETSC_TRUE;
    1533:     pcbddc->use_faces    = PETSC_TRUE;
    1534:   }

    1536:   /* Get stdout for dbg */
    1537:   if (pcbddc->dbg_flag) {
    1538:     if (!pcbddc->dbg_viewer) {
    1539:       pcbddc->dbg_viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)pc));
    1540:     }
    1541:     PetscViewerASCIIPushSynchronized(pcbddc->dbg_viewer);
    1542:     PetscViewerASCIIAddTab(pcbddc->dbg_viewer,2*pcbddc->current_level);
    1543:   }

    1545:   /* process topology information */
    1546:   PetscLogEventBegin(PC_BDDC_Topology[pcbddc->current_level],pc,0,0,0);
    1547:   if (pcbddc->recompute_topography) {
    1548:     PCBDDCComputeLocalTopologyInfo(pc);
    1549:     if (pcbddc->discretegradient) {
    1550:       PCBDDCNedelecSupport(pc);
    1551:     }
    1552:   }
    1553:   if (pcbddc->corner_selected) pcbddc->use_vertices = PETSC_TRUE;

    1555:   /* change basis if requested by the user */
    1556:   if (pcbddc->user_ChangeOfBasisMatrix) {
    1557:     /* use_change_of_basis flag is used to automatically compute a change of basis from constraints */
    1558:     pcbddc->use_change_of_basis = PETSC_FALSE;
    1559:     PCBDDCComputeLocalMatrix(pc,pcbddc->user_ChangeOfBasisMatrix);
    1560:   } else {
    1561:     MatDestroy(&pcbddc->local_mat);
    1562:     PetscObjectReference((PetscObject)matis->A);
    1563:     pcbddc->local_mat = matis->A;
    1564:   }

    1566:   /*
    1567:      Compute change of basis on local pressures (aka zerodiag dofs) with the benign trick
    1568:      This should come earlier then PCISSetUp for extracting the correct subdomain matrices
    1569:   */
    1570:   PCBDDCBenignShellMat(pc,PETSC_TRUE);
    1571:   if (pcbddc->benign_saddle_point) {
    1572:     PC_IS* pcis = (PC_IS*)pc->data;

    1574:     if (pcbddc->user_ChangeOfBasisMatrix || pcbddc->use_change_of_basis || !computesubschurs) pcbddc->benign_change_explicit = PETSC_TRUE;
    1575:     /* detect local saddle point and change the basis in pcbddc->local_mat */
    1576:     PCBDDCBenignDetectSaddlePoint(pc,(PetscBool)(!pcbddc->recompute_topography),&zerodiag);
    1577:     /* pop B0 mat from local mat */
    1578:     PCBDDCBenignPopOrPushB0(pc,PETSC_TRUE);
    1579:     /* give pcis a hint to not reuse submatrices during PCISCreate */
    1580:     if (pc->flag == SAME_NONZERO_PATTERN && pcis->reusesubmatrices == PETSC_TRUE) {
    1581:       if (pcbddc->benign_n && (pcbddc->benign_change_explicit || pcbddc->dbg_flag)) {
    1582:         pcis->reusesubmatrices = PETSC_FALSE;
    1583:       } else {
    1584:         pcis->reusesubmatrices = PETSC_TRUE;
    1585:       }
    1586:     } else {
    1587:       pcis->reusesubmatrices = PETSC_FALSE;
    1588:     }
    1589:   }

    1591:   /* propagate relevant information */
    1592:   if (matis->A->symmetric_set) {
    1593:     MatSetOption(pcbddc->local_mat,MAT_SYMMETRIC,matis->A->symmetric);
    1594:   }
    1595:   if (matis->A->spd_set) {
    1596:     MatSetOption(pcbddc->local_mat,MAT_SPD,matis->A->spd);
    1597:   }

    1599:   /* Set up all the "iterative substructuring" common block without computing solvers */
    1600:   {
    1601:     Mat temp_mat;

    1603:     temp_mat = matis->A;
    1604:     matis->A = pcbddc->local_mat;
    1605:     PCISSetUp(pc,PETSC_TRUE,PETSC_FALSE);
    1606:     pcbddc->local_mat = matis->A;
    1607:     matis->A = temp_mat;
    1608:   }

    1610:   /* Analyze interface */
    1611:   if (!pcbddc->graphanalyzed) {
    1612:     PCBDDCAnalyzeInterface(pc);
    1613:     computeconstraintsmatrix = PETSC_TRUE;
    1614:     if (pcbddc->adaptive_selection && !pcbddc->use_deluxe_scaling && !pcbddc->mat_graph->twodim) {
    1615:       SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Cannot compute the adaptive primal space for a problem with 3D edges without deluxe scaling");
    1616:     }
    1617:     if (pcbddc->compute_nonetflux) {
    1618:       MatNullSpace nnfnnsp;

    1621:       PCBDDCComputeNoNetFlux(pc->pmat,pcbddc->divudotp,pcbddc->divudotp_trans,pcbddc->divudotp_vl2l,pcbddc->mat_graph,&nnfnnsp);
    1622:       /* TODO what if a nearnullspace is already attached? */
    1623:       if (nnfnnsp) {
    1624:         MatSetNearNullSpace(pc->pmat,nnfnnsp);
    1625:         MatNullSpaceDestroy(&nnfnnsp);
    1626:       }
    1627:     }
    1628:   }
    1629:   PetscLogEventEnd(PC_BDDC_Topology[pcbddc->current_level],pc,0,0,0);

    1631:   /* check existence of a divergence free extension, i.e.
    1632:      b(v_I,p_0) = 0 for all v_I (raise error if not).
    1633:      Also, check that PCBDDCBenignGetOrSetP0 works */
    1634:   if (pcbddc->benign_saddle_point && pcbddc->dbg_flag > 1) {
    1635:     PCBDDCBenignCheck(pc,zerodiag);
    1636:   }
    1637:   ISDestroy(&zerodiag);

    1639:   /* Setup local dirichlet solver ksp_D and sub_schurs solvers */
    1640:   if (computesubschurs && pcbddc->recompute_topography) {
    1641:     PCBDDCInitSubSchurs(pc);
    1642:   }
    1643:   /* SetUp Scaling operator (scaling matrices could be needed in SubSchursSetUp)*/
    1644:   if (!pcbddc->use_deluxe_scaling) {
    1645:     PCBDDCScalingSetUp(pc);
    1646:   }

    1648:   /* finish setup solvers and do adaptive selection of constraints */
    1649:   sub_schurs = pcbddc->sub_schurs;
    1650:   if (sub_schurs && sub_schurs->schur_explicit) {
    1651:     if (computesubschurs) {
    1652:       PCBDDCSetUpSubSchurs(pc);
    1653:     }
    1654:     PCBDDCSetUpLocalSolvers(pc,PETSC_TRUE,PETSC_FALSE);
    1655:   } else {
    1656:     PCBDDCSetUpLocalSolvers(pc,PETSC_TRUE,PETSC_FALSE);
    1657:     if (computesubschurs) {
    1658:       PCBDDCSetUpSubSchurs(pc);
    1659:     }
    1660:   }
    1661:   if (pcbddc->adaptive_selection) {
    1662:     PCBDDCAdaptiveSelection(pc);
    1663:     computeconstraintsmatrix = PETSC_TRUE;
    1664:   }

    1666:   /* infer if NullSpace object attached to Mat via MatSetNearNullSpace has changed */
    1667:   new_nearnullspace_provided = PETSC_FALSE;
    1668:   MatGetNearNullSpace(pc->pmat,&nearnullspace);
    1669:   if (pcbddc->onearnullspace) { /* already used nearnullspace */
    1670:     if (!nearnullspace) { /* near null space attached to mat has been destroyed */
    1671:       new_nearnullspace_provided = PETSC_TRUE;
    1672:     } else {
    1673:       /* determine if the two nullspaces are different (should be lightweight) */
    1674:       if (nearnullspace != pcbddc->onearnullspace) {
    1675:         new_nearnullspace_provided = PETSC_TRUE;
    1676:       } else { /* maybe the user has changed the content of the nearnullspace so check vectors ObjectStateId */
    1677:         PetscInt         i;
    1678:         const Vec        *nearnullvecs;
    1679:         PetscObjectState state;
    1680:         PetscInt         nnsp_size;
    1681:         MatNullSpaceGetVecs(nearnullspace,NULL,&nnsp_size,&nearnullvecs);
    1682:         for (i=0;i<nnsp_size;i++) {
    1683:           PetscObjectStateGet((PetscObject)nearnullvecs[i],&state);
    1684:           if (pcbddc->onearnullvecs_state[i] != state) {
    1685:             new_nearnullspace_provided = PETSC_TRUE;
    1686:             break;
    1687:           }
    1688:         }
    1689:       }
    1690:     }
    1691:   } else {
    1692:     if (!nearnullspace) { /* both nearnullspaces are null */
    1693:       new_nearnullspace_provided = PETSC_FALSE;
    1694:     } else { /* nearnullspace attached later */
    1695:       new_nearnullspace_provided = PETSC_TRUE;
    1696:     }
    1697:   }

    1699:   /* Setup constraints and related work vectors */
    1700:   /* reset primal space flags */
    1701:   PetscLogEventBegin(PC_BDDC_LocalWork[pcbddc->current_level],pc,0,0,0);
    1702:   pcbddc->new_primal_space = PETSC_FALSE;
    1703:   pcbddc->new_primal_space_local = PETSC_FALSE;
    1704:   if (computeconstraintsmatrix || new_nearnullspace_provided) {
    1705:     /* It also sets the primal space flags */
    1706:     PCBDDCConstraintsSetUp(pc);
    1707:   }
    1708:   /* Allocate needed local vectors (which depends on quantities defined during ConstraintsSetUp) */
    1709:   PCBDDCSetUpLocalWorkVectors(pc);

    1711:   if (pcbddc->use_change_of_basis) {
    1712:     PC_IS *pcis = (PC_IS*)(pc->data);

    1714:     PCBDDCComputeLocalMatrix(pc,pcbddc->ChangeOfBasisMatrix);
    1715:     if (pcbddc->benign_change) {
    1716:       MatDestroy(&pcbddc->benign_B0);
    1717:       /* pop B0 from pcbddc->local_mat */
    1718:       PCBDDCBenignPopOrPushB0(pc,PETSC_TRUE);
    1719:     }
    1720:     /* get submatrices */
    1721:     MatDestroy(&pcis->A_IB);
    1722:     MatDestroy(&pcis->A_BI);
    1723:     MatDestroy(&pcis->A_BB);
    1724:     MatCreateSubMatrix(pcbddc->local_mat,pcis->is_B_local,pcis->is_B_local,MAT_INITIAL_MATRIX,&pcis->A_BB);
    1725:     MatCreateSubMatrix(pcbddc->local_mat,pcis->is_I_local,pcis->is_B_local,MAT_INITIAL_MATRIX,&pcis->A_IB);
    1726:     MatCreateSubMatrix(pcbddc->local_mat,pcis->is_B_local,pcis->is_I_local,MAT_INITIAL_MATRIX,&pcis->A_BI);
    1727:     /* set flag in pcis to not reuse submatrices during PCISCreate */
    1728:     pcis->reusesubmatrices = PETSC_FALSE;
    1729:   } else if (!pcbddc->user_ChangeOfBasisMatrix && !pcbddc->benign_change) {
    1730:     MatDestroy(&pcbddc->local_mat);
    1731:     PetscObjectReference((PetscObject)matis->A);
    1732:     pcbddc->local_mat = matis->A;
    1733:   }

    1735:   /* interface pressure block row for B_C */
    1736:   PetscObjectQuery((PetscObject)pc,"__KSPFETIDP_lP" ,(PetscObject*)&lP);
    1737:   PetscObjectQuery((PetscObject)pc,"__KSPFETIDP_lA" ,(PetscObject*)&lA);
    1738:   if (lA && lP) {
    1739:     PC_IS*    pcis = (PC_IS*)pc->data;
    1740:     Mat       B_BI,B_BB,Bt_BI,Bt_BB;
    1741:     PetscBool issym;
    1742:     MatIsSymmetric(lA,PETSC_SMALL,&issym);
    1743:     if (issym) {
    1744:       MatCreateSubMatrix(lA,lP,pcis->is_I_local,MAT_INITIAL_MATRIX,&B_BI);
    1745:       MatCreateSubMatrix(lA,lP,pcis->is_B_local,MAT_INITIAL_MATRIX,&B_BB);
    1746:       MatCreateTranspose(B_BI,&Bt_BI);
    1747:       MatCreateTranspose(B_BB,&Bt_BB);
    1748:     } else {
    1749:       MatCreateSubMatrix(lA,lP,pcis->is_I_local,MAT_INITIAL_MATRIX,&B_BI);
    1750:       MatCreateSubMatrix(lA,lP,pcis->is_B_local,MAT_INITIAL_MATRIX,&B_BB);
    1751:       MatCreateSubMatrix(lA,pcis->is_I_local,lP,MAT_INITIAL_MATRIX,&Bt_BI);
    1752:       MatCreateSubMatrix(lA,pcis->is_B_local,lP,MAT_INITIAL_MATRIX,&Bt_BB);
    1753:     }
    1754:     PetscObjectCompose((PetscObject)pc,"__KSPFETIDP_B_BI",(PetscObject)B_BI);
    1755:     PetscObjectCompose((PetscObject)pc,"__KSPFETIDP_B_BB",(PetscObject)B_BB);
    1756:     PetscObjectCompose((PetscObject)pc,"__KSPFETIDP_Bt_BI",(PetscObject)Bt_BI);
    1757:     PetscObjectCompose((PetscObject)pc,"__KSPFETIDP_Bt_BB",(PetscObject)Bt_BB);
    1758:     MatDestroy(&B_BI);
    1759:     MatDestroy(&B_BB);
    1760:     MatDestroy(&Bt_BI);
    1761:     MatDestroy(&Bt_BB);
    1762:   }
    1763:   PetscLogEventEnd(PC_BDDC_LocalWork[pcbddc->current_level],pc,0,0,0);

    1765:   /* SetUp coarse and local Neumann solvers */
    1766:   PCBDDCSetUpSolvers(pc);
    1767:   /* SetUp Scaling operator */
    1768:   if (pcbddc->use_deluxe_scaling) {
    1769:     PCBDDCScalingSetUp(pc);
    1770:   }

    1772:   /* mark topography as done */
    1773:   pcbddc->recompute_topography = PETSC_FALSE;

    1775:   /* wrap pcis->A_IB and pcis->A_BI if we did not change explicitly the variables on the pressures */
    1776:   PCBDDCBenignShellMat(pc,PETSC_FALSE);

    1778:   if (pcbddc->dbg_flag) {
    1779:     PetscViewerASCIISubtractTab(pcbddc->dbg_viewer,2*pcbddc->current_level);
    1780:     PetscViewerASCIIPopSynchronized(pcbddc->dbg_viewer);
    1781:   }
    1782:   return 0;
    1783: }

    1785: /*
    1786:    PCApply_BDDC - Applies the BDDC operator to a vector.

    1788:    Input Parameters:
    1789: +  pc - the preconditioner context
    1790: -  r - input vector (global)

    1792:    Output Parameter:
    1793: .  z - output vector (global)

    1795:    Application Interface Routine: PCApply()
    1796:  */
    1797: PetscErrorCode PCApply_BDDC(PC pc,Vec r,Vec z)
    1798: {
    1799:   PC_IS             *pcis = (PC_IS*)(pc->data);
    1800:   PC_BDDC           *pcbddc = (PC_BDDC*)(pc->data);
    1801:   Mat               lA = NULL;
    1802:   PetscInt          n_B = pcis->n_B, n_D = pcis->n - n_B;
    1803:   const PetscScalar one = 1.0;
    1804:   const PetscScalar m_one = -1.0;
    1805:   const PetscScalar zero = 0.0;
    1806: /* This code is similar to that provided in nn.c for PCNN
    1807:    NN interface preconditioner changed to BDDC
    1808:    Added support for M_3 preconditioner in the reference article (code is active if pcbddc->switch_static == PETSC_TRUE) */

    1810:   PetscCitationsRegister(citation,&cited);
    1811:   if (pcbddc->switch_static) {
    1812:     MatISGetLocalMat(pc->useAmat ? pc->mat : pc->pmat,&lA);
    1813:   }

    1815:   if (pcbddc->ChangeOfBasisMatrix) {
    1816:     Vec swap;

    1818:     MatMultTranspose(pcbddc->ChangeOfBasisMatrix,r,pcbddc->work_change);
    1819:     swap = pcbddc->work_change;
    1820:     pcbddc->work_change = r;
    1821:     r = swap;
    1822:     /* save rhs so that we don't need to apply the change of basis for the exact dirichlet trick in PreSolve */
    1823:     if (pcbddc->benign_apply_coarse_only && pcbddc->use_exact_dirichlet_trick && pcbddc->change_interior) {
    1824:       VecCopy(r,pcis->vec1_global);
    1825:       VecLockReadPush(pcis->vec1_global);
    1826:     }
    1827:   }
    1828:   if (pcbddc->benign_have_null) { /* get p0 from r */
    1829:     PCBDDCBenignGetOrSetP0(pc,r,PETSC_TRUE);
    1830:   }
    1831:   if (pcbddc->interface_extension == PC_BDDC_INTERFACE_EXT_DIRICHLET && !pcbddc->exact_dirichlet_trick_app && !pcbddc->benign_apply_coarse_only) {
    1832:     VecCopy(r,z);
    1833:     /* First Dirichlet solve */
    1834:     VecScatterBegin(pcis->global_to_D,r,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
    1835:     VecScatterEnd(pcis->global_to_D,r,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
    1836:     /*
    1837:       Assembling right hand side for BDDC operator
    1838:       - pcis->vec1_D for the Dirichlet part (if needed, i.e. pcbddc->switch_static == PETSC_TRUE)
    1839:       - pcis->vec1_B the interface part of the global vector z
    1840:     */
    1841:     if (n_D) {
    1842:       PetscLogEventBegin(PC_BDDC_Solves[pcbddc->current_level][0],pc,0,0,0);
    1843:       KSPSolve(pcbddc->ksp_D,pcis->vec1_D,pcis->vec2_D);
    1844:       PetscLogEventEnd(PC_BDDC_Solves[pcbddc->current_level][0],pc,0,0,0);
    1845:       KSPCheckSolve(pcbddc->ksp_D,pc,pcis->vec2_D);
    1846:       VecScale(pcis->vec2_D,m_one);
    1847:       if (pcbddc->switch_static) {
    1848:         VecSet(pcis->vec1_N,0.);
    1849:         VecScatterBegin(pcis->N_to_D,pcis->vec2_D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
    1850:         VecScatterEnd(pcis->N_to_D,pcis->vec2_D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
    1851:         if (!pcbddc->switch_static_change) {
    1852:           MatMult(lA,pcis->vec1_N,pcis->vec2_N);
    1853:         } else {
    1854:           MatMult(pcbddc->switch_static_change,pcis->vec1_N,pcis->vec2_N);
    1855:           MatMult(lA,pcis->vec2_N,pcis->vec1_N);
    1856:           MatMultTranspose(pcbddc->switch_static_change,pcis->vec1_N,pcis->vec2_N);
    1857:         }
    1858:         VecScatterBegin(pcis->N_to_D,pcis->vec2_N,pcis->vec1_D,ADD_VALUES,SCATTER_FORWARD);
    1859:         VecScatterEnd(pcis->N_to_D,pcis->vec2_N,pcis->vec1_D,ADD_VALUES,SCATTER_FORWARD);
    1860:         VecScatterBegin(pcis->N_to_B,pcis->vec2_N,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
    1861:         VecScatterEnd(pcis->N_to_B,pcis->vec2_N,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
    1862:       } else {
    1863:         MatMult(pcis->A_BI,pcis->vec2_D,pcis->vec1_B);
    1864:       }
    1865:     } else {
    1866:       VecSet(pcis->vec1_B,zero);
    1867:     }
    1868:     VecScatterBegin(pcis->global_to_B,pcis->vec1_B,z,ADD_VALUES,SCATTER_REVERSE);
    1869:     VecScatterEnd(pcis->global_to_B,pcis->vec1_B,z,ADD_VALUES,SCATTER_REVERSE);
    1870:     PCBDDCScalingRestriction(pc,z,pcis->vec1_B);
    1871:   } else {
    1872:     if (!pcbddc->benign_apply_coarse_only) {
    1873:       PCBDDCScalingRestriction(pc,r,pcis->vec1_B);
    1874:     }
    1875:   }
    1876:   if (pcbddc->interface_extension == PC_BDDC_INTERFACE_EXT_LUMP) {
    1878:     VecScatterBegin(pcis->global_to_D,r,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
    1879:     VecScatterEnd(pcis->global_to_D,r,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
    1880:   }

    1882:   /* Apply interface preconditioner
    1883:      input/output vecs: pcis->vec1_B and pcis->vec1_D */
    1884:   PCBDDCApplyInterfacePreconditioner(pc,PETSC_FALSE);

    1886:   /* Apply transpose of partition of unity operator */
    1887:   PCBDDCScalingExtension(pc,pcis->vec1_B,z);
    1888:   if (pcbddc->interface_extension == PC_BDDC_INTERFACE_EXT_LUMP) {
    1889:     VecScatterBegin(pcis->global_to_D,pcis->vec1_D,z,INSERT_VALUES,SCATTER_REVERSE);
    1890:     VecScatterEnd(pcis->global_to_D,pcis->vec1_D,z,INSERT_VALUES,SCATTER_REVERSE);
    1891:     return 0;
    1892:   }
    1893:   /* Second Dirichlet solve and assembling of output */
    1894:   VecScatterBegin(pcis->global_to_B,z,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
    1895:   VecScatterEnd(pcis->global_to_B,z,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
    1896:   if (n_B) {
    1897:     if (pcbddc->switch_static) {
    1898:       VecScatterBegin(pcis->N_to_D,pcis->vec1_D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
    1899:       VecScatterEnd(pcis->N_to_D,pcis->vec1_D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
    1900:       VecScatterBegin(pcis->N_to_B,pcis->vec1_B,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
    1901:       VecScatterEnd(pcis->N_to_B,pcis->vec1_B,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
    1902:       if (!pcbddc->switch_static_change) {
    1903:         MatMult(lA,pcis->vec1_N,pcis->vec2_N);
    1904:       } else {
    1905:         MatMult(pcbddc->switch_static_change,pcis->vec1_N,pcis->vec2_N);
    1906:         MatMult(lA,pcis->vec2_N,pcis->vec1_N);
    1907:         MatMultTranspose(pcbddc->switch_static_change,pcis->vec1_N,pcis->vec2_N);
    1908:       }
    1909:       VecScatterBegin(pcis->N_to_D,pcis->vec2_N,pcis->vec3_D,INSERT_VALUES,SCATTER_FORWARD);
    1910:       VecScatterEnd(pcis->N_to_D,pcis->vec2_N,pcis->vec3_D,INSERT_VALUES,SCATTER_FORWARD);
    1911:     } else {
    1912:       MatMult(pcis->A_IB,pcis->vec1_B,pcis->vec3_D);
    1913:     }
    1914:   } else if (pcbddc->switch_static) { /* n_B is zero */
    1915:     if (!pcbddc->switch_static_change) {
    1916:       MatMult(lA,pcis->vec1_D,pcis->vec3_D);
    1917:     } else {
    1918:       MatMult(pcbddc->switch_static_change,pcis->vec1_D,pcis->vec1_N);
    1919:       MatMult(lA,pcis->vec1_N,pcis->vec2_N);
    1920:       MatMultTranspose(pcbddc->switch_static_change,pcis->vec2_N,pcis->vec3_D);
    1921:     }
    1922:   }
    1923:   PetscLogEventBegin(PC_BDDC_Solves[pcbddc->current_level][0],pc,0,0,0);
    1924:   KSPSolve(pcbddc->ksp_D,pcis->vec3_D,pcis->vec4_D);
    1925:   PetscLogEventEnd(PC_BDDC_Solves[pcbddc->current_level][0],pc,0,0,0);
    1926:   KSPCheckSolve(pcbddc->ksp_D,pc,pcis->vec4_D);

    1928:   if (!pcbddc->exact_dirichlet_trick_app && !pcbddc->benign_apply_coarse_only) {
    1929:     if (pcbddc->switch_static) {
    1930:       VecAXPBYPCZ(pcis->vec2_D,m_one,one,m_one,pcis->vec4_D,pcis->vec1_D);
    1931:     } else {
    1932:       VecAXPBY(pcis->vec2_D,m_one,m_one,pcis->vec4_D);
    1933:     }
    1934:     VecScatterBegin(pcis->global_to_D,pcis->vec2_D,z,INSERT_VALUES,SCATTER_REVERSE);
    1935:     VecScatterEnd(pcis->global_to_D,pcis->vec2_D,z,INSERT_VALUES,SCATTER_REVERSE);
    1936:   } else {
    1937:     if (pcbddc->switch_static) {
    1938:       VecAXPBY(pcis->vec4_D,one,m_one,pcis->vec1_D);
    1939:     } else {
    1940:       VecScale(pcis->vec4_D,m_one);
    1941:     }
    1942:     VecScatterBegin(pcis->global_to_D,pcis->vec4_D,z,INSERT_VALUES,SCATTER_REVERSE);
    1943:     VecScatterEnd(pcis->global_to_D,pcis->vec4_D,z,INSERT_VALUES,SCATTER_REVERSE);
    1944:   }
    1945:   if (pcbddc->benign_have_null) { /* set p0 (computed in PCBDDCApplyInterface) */
    1946:     if (pcbddc->benign_apply_coarse_only) {
    1947:       PetscArrayzero(pcbddc->benign_p0,pcbddc->benign_n);
    1948:     }
    1949:     PCBDDCBenignGetOrSetP0(pc,z,PETSC_FALSE);
    1950:   }

    1952:   if (pcbddc->ChangeOfBasisMatrix) {
    1953:     pcbddc->work_change = r;
    1954:     VecCopy(z,pcbddc->work_change);
    1955:     MatMult(pcbddc->ChangeOfBasisMatrix,pcbddc->work_change,z);
    1956:   }
    1957:   return 0;
    1958: }

    1960: /*
    1961:    PCApplyTranspose_BDDC - Applies the transpose of the BDDC operator to a vector.

    1963:    Input Parameters:
    1964: +  pc - the preconditioner context
    1965: -  r - input vector (global)

    1967:    Output Parameter:
    1968: .  z - output vector (global)

    1970:    Application Interface Routine: PCApplyTranspose()
    1971:  */
    1972: PetscErrorCode PCApplyTranspose_BDDC(PC pc,Vec r,Vec z)
    1973: {
    1974:   PC_IS             *pcis = (PC_IS*)(pc->data);
    1975:   PC_BDDC           *pcbddc = (PC_BDDC*)(pc->data);
    1976:   Mat               lA = NULL;
    1977:   PetscInt          n_B = pcis->n_B, n_D = pcis->n - n_B;
    1978:   const PetscScalar one = 1.0;
    1979:   const PetscScalar m_one = -1.0;
    1980:   const PetscScalar zero = 0.0;

    1982:   PetscCitationsRegister(citation,&cited);
    1983:   if (pcbddc->switch_static) {
    1984:     MatISGetLocalMat(pc->useAmat ? pc->mat : pc->pmat,&lA);
    1985:   }
    1986:   if (pcbddc->ChangeOfBasisMatrix) {
    1987:     Vec swap;

    1989:     MatMultTranspose(pcbddc->ChangeOfBasisMatrix,r,pcbddc->work_change);
    1990:     swap = pcbddc->work_change;
    1991:     pcbddc->work_change = r;
    1992:     r = swap;
    1993:     /* save rhs so that we don't need to apply the change of basis for the exact dirichlet trick in PreSolve */
    1994:     if (pcbddc->benign_apply_coarse_only && pcbddc->exact_dirichlet_trick_app && pcbddc->change_interior) {
    1995:       VecCopy(r,pcis->vec1_global);
    1996:       VecLockReadPush(pcis->vec1_global);
    1997:     }
    1998:   }
    1999:   if (pcbddc->benign_have_null) { /* get p0 from r */
    2000:     PCBDDCBenignGetOrSetP0(pc,r,PETSC_TRUE);
    2001:   }
    2002:   if (!pcbddc->exact_dirichlet_trick_app && !pcbddc->benign_apply_coarse_only) {
    2003:     VecCopy(r,z);
    2004:     /* First Dirichlet solve */
    2005:     VecScatterBegin(pcis->global_to_D,r,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
    2006:     VecScatterEnd(pcis->global_to_D,r,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
    2007:     /*
    2008:       Assembling right hand side for BDDC operator
    2009:       - pcis->vec1_D for the Dirichlet part (if needed, i.e. pcbddc->switch_static == PETSC_TRUE)
    2010:       - pcis->vec1_B the interface part of the global vector z
    2011:     */
    2012:     if (n_D) {
    2013:       PetscLogEventBegin(PC_BDDC_Solves[pcbddc->current_level][0],pc,0,0,0);
    2014:       KSPSolveTranspose(pcbddc->ksp_D,pcis->vec1_D,pcis->vec2_D);
    2015:       PetscLogEventEnd(PC_BDDC_Solves[pcbddc->current_level][0],pc,0,0,0);
    2016:       KSPCheckSolve(pcbddc->ksp_D,pc,pcis->vec2_D);
    2017:       VecScale(pcis->vec2_D,m_one);
    2018:       if (pcbddc->switch_static) {
    2019:         VecSet(pcis->vec1_N,0.);
    2020:         VecScatterBegin(pcis->N_to_D,pcis->vec2_D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
    2021:         VecScatterEnd(pcis->N_to_D,pcis->vec2_D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
    2022:         if (!pcbddc->switch_static_change) {
    2023:           MatMultTranspose(lA,pcis->vec1_N,pcis->vec2_N);
    2024:         } else {
    2025:           MatMult(pcbddc->switch_static_change,pcis->vec1_N,pcis->vec2_N);
    2026:           MatMultTranspose(lA,pcis->vec2_N,pcis->vec1_N);
    2027:           MatMultTranspose(pcbddc->switch_static_change,pcis->vec1_N,pcis->vec2_N);
    2028:         }
    2029:         VecScatterBegin(pcis->N_to_D,pcis->vec2_N,pcis->vec1_D,ADD_VALUES,SCATTER_FORWARD);
    2030:         VecScatterEnd(pcis->N_to_D,pcis->vec2_N,pcis->vec1_D,ADD_VALUES,SCATTER_FORWARD);
    2031:         VecScatterBegin(pcis->N_to_B,pcis->vec2_N,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
    2032:         VecScatterEnd(pcis->N_to_B,pcis->vec2_N,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
    2033:       } else {
    2034:         MatMultTranspose(pcis->A_IB,pcis->vec2_D,pcis->vec1_B);
    2035:       }
    2036:     } else {
    2037:       VecSet(pcis->vec1_B,zero);
    2038:     }
    2039:     VecScatterBegin(pcis->global_to_B,pcis->vec1_B,z,ADD_VALUES,SCATTER_REVERSE);
    2040:     VecScatterEnd(pcis->global_to_B,pcis->vec1_B,z,ADD_VALUES,SCATTER_REVERSE);
    2041:     PCBDDCScalingRestriction(pc,z,pcis->vec1_B);
    2042:   } else {
    2043:     PCBDDCScalingRestriction(pc,r,pcis->vec1_B);
    2044:   }

    2046:   /* Apply interface preconditioner
    2047:      input/output vecs: pcis->vec1_B and pcis->vec1_D */
    2048:   PCBDDCApplyInterfacePreconditioner(pc,PETSC_TRUE);

    2050:   /* Apply transpose of partition of unity operator */
    2051:   PCBDDCScalingExtension(pc,pcis->vec1_B,z);

    2053:   /* Second Dirichlet solve and assembling of output */
    2054:   VecScatterBegin(pcis->global_to_B,z,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
    2055:   VecScatterEnd(pcis->global_to_B,z,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
    2056:   if (n_B) {
    2057:     if (pcbddc->switch_static) {
    2058:       VecScatterBegin(pcis->N_to_D,pcis->vec1_D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
    2059:       VecScatterEnd(pcis->N_to_D,pcis->vec1_D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
    2060:       VecScatterBegin(pcis->N_to_B,pcis->vec1_B,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
    2061:       VecScatterEnd(pcis->N_to_B,pcis->vec1_B,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
    2062:       if (!pcbddc->switch_static_change) {
    2063:         MatMultTranspose(lA,pcis->vec1_N,pcis->vec2_N);
    2064:       } else {
    2065:         MatMult(pcbddc->switch_static_change,pcis->vec1_N,pcis->vec2_N);
    2066:         MatMultTranspose(lA,pcis->vec2_N,pcis->vec1_N);
    2067:         MatMultTranspose(pcbddc->switch_static_change,pcis->vec1_N,pcis->vec2_N);
    2068:       }
    2069:       VecScatterBegin(pcis->N_to_D,pcis->vec2_N,pcis->vec3_D,INSERT_VALUES,SCATTER_FORWARD);
    2070:       VecScatterEnd(pcis->N_to_D,pcis->vec2_N,pcis->vec3_D,INSERT_VALUES,SCATTER_FORWARD);
    2071:     } else {
    2072:       MatMultTranspose(pcis->A_BI,pcis->vec1_B,pcis->vec3_D);
    2073:     }
    2074:   } else if (pcbddc->switch_static) { /* n_B is zero */
    2075:     if (!pcbddc->switch_static_change) {
    2076:       MatMultTranspose(lA,pcis->vec1_D,pcis->vec3_D);
    2077:     } else {
    2078:       MatMult(pcbddc->switch_static_change,pcis->vec1_D,pcis->vec1_N);
    2079:       MatMultTranspose(lA,pcis->vec1_N,pcis->vec2_N);
    2080:       MatMultTranspose(pcbddc->switch_static_change,pcis->vec2_N,pcis->vec3_D);
    2081:     }
    2082:   }
    2083:   PetscLogEventBegin(PC_BDDC_Solves[pcbddc->current_level][0],pc,0,0,0);
    2084:   KSPSolveTranspose(pcbddc->ksp_D,pcis->vec3_D,pcis->vec4_D);
    2085:   PetscLogEventEnd(PC_BDDC_Solves[pcbddc->current_level][0],pc,0,0,0);
    2086:   KSPCheckSolve(pcbddc->ksp_D,pc,pcis->vec4_D);
    2087:   if (!pcbddc->exact_dirichlet_trick_app && !pcbddc->benign_apply_coarse_only) {
    2088:     if (pcbddc->switch_static) {
    2089:       VecAXPBYPCZ(pcis->vec2_D,m_one,one,m_one,pcis->vec4_D,pcis->vec1_D);
    2090:     } else {
    2091:       VecAXPBY(pcis->vec2_D,m_one,m_one,pcis->vec4_D);
    2092:     }
    2093:     VecScatterBegin(pcis->global_to_D,pcis->vec2_D,z,INSERT_VALUES,SCATTER_REVERSE);
    2094:     VecScatterEnd(pcis->global_to_D,pcis->vec2_D,z,INSERT_VALUES,SCATTER_REVERSE);
    2095:   } else {
    2096:     if (pcbddc->switch_static) {
    2097:       VecAXPBY(pcis->vec4_D,one,m_one,pcis->vec1_D);
    2098:     } else {
    2099:       VecScale(pcis->vec4_D,m_one);
    2100:     }
    2101:     VecScatterBegin(pcis->global_to_D,pcis->vec4_D,z,INSERT_VALUES,SCATTER_REVERSE);
    2102:     VecScatterEnd(pcis->global_to_D,pcis->vec4_D,z,INSERT_VALUES,SCATTER_REVERSE);
    2103:   }
    2104:   if (pcbddc->benign_have_null) { /* set p0 (computed in PCBDDCApplyInterface) */
    2105:     PCBDDCBenignGetOrSetP0(pc,z,PETSC_FALSE);
    2106:   }
    2107:   if (pcbddc->ChangeOfBasisMatrix) {
    2108:     pcbddc->work_change = r;
    2109:     VecCopy(z,pcbddc->work_change);
    2110:     MatMult(pcbddc->ChangeOfBasisMatrix,pcbddc->work_change,z);
    2111:   }
    2112:   return 0;
    2113: }

    2115: PetscErrorCode PCReset_BDDC(PC pc)
    2116: {
    2117:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
    2118:   PC_IS          *pcis = (PC_IS*)pc->data;
    2119:   KSP            kspD,kspR,kspC;

    2121:   /* free BDDC custom data  */
    2122:   PCBDDCResetCustomization(pc);
    2123:   /* destroy objects related to topography */
    2124:   PCBDDCResetTopography(pc);
    2125:   /* destroy objects for scaling operator */
    2126:   PCBDDCScalingDestroy(pc);
    2127:   /* free solvers stuff */
    2128:   PCBDDCResetSolvers(pc);
    2129:   /* free global vectors needed in presolve */
    2130:   VecDestroy(&pcbddc->temp_solution);
    2131:   VecDestroy(&pcbddc->original_rhs);
    2132:   /* free data created by PCIS */
    2133:   PCISDestroy(pc);

    2135:   /* restore defaults */
    2136:   kspD = pcbddc->ksp_D;
    2137:   kspR = pcbddc->ksp_R;
    2138:   kspC = pcbddc->coarse_ksp;
    2139:   PetscMemzero(pc->data,sizeof(*pcbddc));
    2140:   pcis->n_neigh                     = -1;
    2141:   pcis->scaling_factor              = 1.0;
    2142:   pcis->reusesubmatrices            = PETSC_TRUE;
    2143:   pcbddc->use_local_adj             = PETSC_TRUE;
    2144:   pcbddc->use_vertices              = PETSC_TRUE;
    2145:   pcbddc->use_edges                 = PETSC_TRUE;
    2146:   pcbddc->symmetric_primal          = PETSC_TRUE;
    2147:   pcbddc->vertex_size               = 1;
    2148:   pcbddc->recompute_topography      = PETSC_TRUE;
    2149:   pcbddc->coarse_size               = -1;
    2150:   pcbddc->use_exact_dirichlet_trick = PETSC_TRUE;
    2151:   pcbddc->coarsening_ratio          = 8;
    2152:   pcbddc->coarse_eqs_per_proc       = 1;
    2153:   pcbddc->benign_compute_correction = PETSC_TRUE;
    2154:   pcbddc->nedfield                  = -1;
    2155:   pcbddc->nedglobal                 = PETSC_TRUE;
    2156:   pcbddc->graphmaxcount             = PETSC_MAX_INT;
    2157:   pcbddc->sub_schurs_layers         = -1;
    2158:   pcbddc->ksp_D                     = kspD;
    2159:   pcbddc->ksp_R                     = kspR;
    2160:   pcbddc->coarse_ksp                = kspC;
    2161:   return 0;
    2162: }

    2164: PetscErrorCode PCDestroy_BDDC(PC pc)
    2165: {
    2166:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;

    2168:   PCReset_BDDC(pc);
    2169:   KSPDestroy(&pcbddc->ksp_D);
    2170:   KSPDestroy(&pcbddc->ksp_R);
    2171:   KSPDestroy(&pcbddc->coarse_ksp);
    2172:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDiscreteGradient_C",NULL);
    2173:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDivergenceMat_C",NULL);
    2174:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetChangeOfBasisMat_C",NULL);
    2175:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetPrimalVerticesLocalIS_C",NULL);
    2176:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetPrimalVerticesIS_C",NULL);
    2177:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetCoarseningRatio_C",NULL);
    2178:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetLevel_C",NULL);
    2179:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetUseExactDirichlet_C",NULL);
    2180:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetLevels_C",NULL);
    2181:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDirichletBoundaries_C",NULL);
    2182:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDirichletBoundariesLocal_C",NULL);
    2183:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetNeumannBoundaries_C",NULL);
    2184:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetNeumannBoundariesLocal_C",NULL);
    2185:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetDirichletBoundaries_C",NULL);
    2186:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetDirichletBoundariesLocal_C",NULL);
    2187:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetNeumannBoundaries_C",NULL);
    2188:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetNeumannBoundariesLocal_C",NULL);
    2189:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDofsSplitting_C",NULL);
    2190:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDofsSplittingLocal_C",NULL);
    2191:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetLocalAdjacencyGraph_C",NULL);
    2192:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCCreateFETIDPOperators_C",NULL);
    2193:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCMatFETIDPGetRHS_C",NULL);
    2194:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCMatFETIDPGetSolution_C",NULL);
    2195:   PetscObjectComposeFunction((PetscObject)pc,"PCPreSolveChangeRHS_C",NULL);
    2196:   PetscObjectComposeFunction((PetscObject)pc,"PCSetCoordinates_C",NULL);
    2197:   PetscFree(pc->data);
    2198:   return 0;
    2199: }

    2201: static PetscErrorCode PCSetCoordinates_BDDC(PC pc, PetscInt dim, PetscInt nloc, PetscReal *coords)
    2202: {
    2203:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
    2204:   PCBDDCGraph    mat_graph = pcbddc->mat_graph;

    2206:   PetscFree(mat_graph->coords);
    2207:   PetscMalloc1(nloc*dim,&mat_graph->coords);
    2208:   PetscArraycpy(mat_graph->coords,coords,nloc*dim);
    2209:   mat_graph->cnloc = nloc;
    2210:   mat_graph->cdim  = dim;
    2211:   mat_graph->cloc  = PETSC_FALSE;
    2212:   /* flg setup */
    2213:   pcbddc->recompute_topography = PETSC_TRUE;
    2214:   pcbddc->corner_selected = PETSC_FALSE;
    2215:   return 0;
    2216: }

    2218: static PetscErrorCode PCPreSolveChangeRHS_BDDC(PC pc, PetscBool* change)
    2219: {
    2220:   *change = PETSC_TRUE;
    2221:   return 0;
    2222: }

    2224: static PetscErrorCode PCBDDCMatFETIDPGetRHS_BDDC(Mat fetidp_mat, Vec standard_rhs, Vec fetidp_flux_rhs)
    2225: {
    2226:   FETIDPMat_ctx  mat_ctx;
    2227:   Vec            work;
    2228:   PC_IS*         pcis;
    2229:   PC_BDDC*       pcbddc;

    2231:   MatShellGetContext(fetidp_mat,&mat_ctx);
    2232:   pcis = (PC_IS*)mat_ctx->pc->data;
    2233:   pcbddc = (PC_BDDC*)mat_ctx->pc->data;

    2235:   VecSet(fetidp_flux_rhs,0.0);
    2236:   /* copy rhs since we may change it during PCPreSolve_BDDC */
    2237:   if (!pcbddc->original_rhs) {
    2238:     VecDuplicate(pcis->vec1_global,&pcbddc->original_rhs);
    2239:   }
    2240:   if (mat_ctx->rhs_flip) {
    2241:     VecPointwiseMult(pcbddc->original_rhs,standard_rhs,mat_ctx->rhs_flip);
    2242:   } else {
    2243:     VecCopy(standard_rhs,pcbddc->original_rhs);
    2244:   }
    2245:   if (mat_ctx->g2g_p) {
    2246:     /* interface pressure rhs */
    2247:     VecScatterBegin(mat_ctx->g2g_p,fetidp_flux_rhs,pcbddc->original_rhs,INSERT_VALUES,SCATTER_REVERSE);
    2248:     VecScatterEnd(mat_ctx->g2g_p,fetidp_flux_rhs,pcbddc->original_rhs,INSERT_VALUES,SCATTER_REVERSE);
    2249:     VecScatterBegin(mat_ctx->g2g_p,standard_rhs,fetidp_flux_rhs,INSERT_VALUES,SCATTER_FORWARD);
    2250:     VecScatterEnd(mat_ctx->g2g_p,standard_rhs,fetidp_flux_rhs,INSERT_VALUES,SCATTER_FORWARD);
    2251:     if (!mat_ctx->rhs_flip) {
    2252:       VecScale(fetidp_flux_rhs,-1.);
    2253:     }
    2254:   }
    2255:   /*
    2256:      change of basis for physical rhs if needed
    2257:      It also changes the rhs in case of dirichlet boundaries
    2258:   */
    2259:   PCPreSolve_BDDC(mat_ctx->pc,NULL,pcbddc->original_rhs,NULL);
    2260:   if (pcbddc->ChangeOfBasisMatrix) {
    2261:     MatMultTranspose(pcbddc->ChangeOfBasisMatrix,pcbddc->original_rhs,pcbddc->work_change);
    2262:     work = pcbddc->work_change;
    2263:    } else {
    2264:     work = pcbddc->original_rhs;
    2265:   }
    2266:   /* store vectors for computation of fetidp final solution */
    2267:   VecScatterBegin(pcis->global_to_D,work,mat_ctx->temp_solution_D,INSERT_VALUES,SCATTER_FORWARD);
    2268:   VecScatterEnd(pcis->global_to_D,work,mat_ctx->temp_solution_D,INSERT_VALUES,SCATTER_FORWARD);
    2269:   /* scale rhs since it should be unassembled */
    2270:   /* TODO use counter scaling? (also below) */
    2271:   VecScatterBegin(pcis->global_to_B,work,mat_ctx->temp_solution_B,INSERT_VALUES,SCATTER_FORWARD);
    2272:   VecScatterEnd(pcis->global_to_B,work,mat_ctx->temp_solution_B,INSERT_VALUES,SCATTER_FORWARD);
    2273:   /* Apply partition of unity */
    2274:   VecPointwiseMult(mat_ctx->temp_solution_B,pcis->D,mat_ctx->temp_solution_B);
    2275:   /* PCBDDCScalingRestriction(mat_ctx->pc,work,mat_ctx->temp_solution_B); */
    2276:   if (!pcbddc->switch_static) {
    2277:     /* compute partially subassembled Schur complement right-hand side */
    2278:     PetscLogEventBegin(PC_BDDC_Solves[pcbddc->current_level][0],mat_ctx->pc,0,0,0);
    2279:     KSPSolve(pcbddc->ksp_D,mat_ctx->temp_solution_D,pcis->vec1_D);
    2280:     PetscLogEventEnd(PC_BDDC_Solves[pcbddc->current_level][0],mat_ctx->pc,0,0,0);
    2281:     /* Cannot propagate up error in KSPSolve() because there is no access to the PC */
    2282:     MatMult(pcis->A_BI,pcis->vec1_D,pcis->vec1_B);
    2283:     VecAXPY(mat_ctx->temp_solution_B,-1.0,pcis->vec1_B);
    2284:     VecSet(work,0.0);
    2285:     VecScatterBegin(pcis->global_to_B,mat_ctx->temp_solution_B,work,ADD_VALUES,SCATTER_REVERSE);
    2286:     VecScatterEnd(pcis->global_to_B,mat_ctx->temp_solution_B,work,ADD_VALUES,SCATTER_REVERSE);
    2287:     /* PCBDDCScalingRestriction(mat_ctx->pc,work,mat_ctx->temp_solution_B); */
    2288:     VecScatterBegin(pcis->global_to_B,work,mat_ctx->temp_solution_B,INSERT_VALUES,SCATTER_FORWARD);
    2289:     VecScatterEnd(pcis->global_to_B,work,mat_ctx->temp_solution_B,INSERT_VALUES,SCATTER_FORWARD);
    2290:     VecPointwiseMult(mat_ctx->temp_solution_B,pcis->D,mat_ctx->temp_solution_B);
    2291:   }
    2292:   /* BDDC rhs */
    2293:   VecCopy(mat_ctx->temp_solution_B,pcis->vec1_B);
    2294:   if (pcbddc->switch_static) {
    2295:     VecCopy(mat_ctx->temp_solution_D,pcis->vec1_D);
    2296:   }
    2297:   /* apply BDDC */
    2298:   PetscArrayzero(pcbddc->benign_p0,pcbddc->benign_n);
    2299:   PCBDDCApplyInterfacePreconditioner(mat_ctx->pc,PETSC_FALSE);
    2300:   PetscArrayzero(pcbddc->benign_p0,pcbddc->benign_n);

    2302:   /* Application of B_delta and assembling of rhs for fetidp fluxes */
    2303:   MatMult(mat_ctx->B_delta,pcis->vec1_B,mat_ctx->lambda_local);
    2304:   VecScatterBegin(mat_ctx->l2g_lambda,mat_ctx->lambda_local,fetidp_flux_rhs,ADD_VALUES,SCATTER_FORWARD);
    2305:   VecScatterEnd(mat_ctx->l2g_lambda,mat_ctx->lambda_local,fetidp_flux_rhs,ADD_VALUES,SCATTER_FORWARD);
    2306:   /* Add contribution to interface pressures */
    2307:   if (mat_ctx->l2g_p) {
    2308:     MatMult(mat_ctx->B_BB,pcis->vec1_B,mat_ctx->vP);
    2309:     if (pcbddc->switch_static) {
    2310:       MatMultAdd(mat_ctx->B_BI,pcis->vec1_D,mat_ctx->vP,mat_ctx->vP);
    2311:     }
    2312:     VecScatterBegin(mat_ctx->l2g_p,mat_ctx->vP,fetidp_flux_rhs,ADD_VALUES,SCATTER_FORWARD);
    2313:     VecScatterEnd(mat_ctx->l2g_p,mat_ctx->vP,fetidp_flux_rhs,ADD_VALUES,SCATTER_FORWARD);
    2314:   }
    2315:   return 0;
    2316: }

    2318: /*@
    2319:  PCBDDCMatFETIDPGetRHS - Compute the right-hand side for FETI-DP linear system using the physical right-hand side

    2321:    Collective

    2323:    Input Parameters:
    2324: +  fetidp_mat      - the FETI-DP matrix object obtained by a call to PCBDDCCreateFETIDPOperators
    2325: -  standard_rhs    - the right-hand side of the original linear system

    2327:    Output Parameters:
    2328: .  fetidp_flux_rhs - the right-hand side for the FETI-DP linear system

    2330:    Level: developer

    2332:    Notes:

    2334: .seealso: PCBDDC, PCBDDCCreateFETIDPOperators, PCBDDCMatFETIDPGetSolution
    2335: @*/
    2336: PetscErrorCode PCBDDCMatFETIDPGetRHS(Mat fetidp_mat, Vec standard_rhs, Vec fetidp_flux_rhs)
    2337: {
    2338:   FETIDPMat_ctx  mat_ctx;

    2343:   MatShellGetContext(fetidp_mat,&mat_ctx);
    2344:   PetscUseMethod(mat_ctx->pc,"PCBDDCMatFETIDPGetRHS_C",(Mat,Vec,Vec),(fetidp_mat,standard_rhs,fetidp_flux_rhs));
    2345:   return 0;
    2346: }

    2348: static PetscErrorCode PCBDDCMatFETIDPGetSolution_BDDC(Mat fetidp_mat, Vec fetidp_flux_sol, Vec standard_sol)
    2349: {
    2350:   FETIDPMat_ctx  mat_ctx;
    2351:   PC_IS*         pcis;
    2352:   PC_BDDC*       pcbddc;
    2353:   Vec            work;

    2355:   MatShellGetContext(fetidp_mat,&mat_ctx);
    2356:   pcis = (PC_IS*)mat_ctx->pc->data;
    2357:   pcbddc = (PC_BDDC*)mat_ctx->pc->data;

    2359:   /* apply B_delta^T */
    2360:   VecSet(pcis->vec1_B,0.);
    2361:   VecScatterBegin(mat_ctx->l2g_lambda,fetidp_flux_sol,mat_ctx->lambda_local,INSERT_VALUES,SCATTER_REVERSE);
    2362:   VecScatterEnd(mat_ctx->l2g_lambda,fetidp_flux_sol,mat_ctx->lambda_local,INSERT_VALUES,SCATTER_REVERSE);
    2363:   MatMultTranspose(mat_ctx->B_delta,mat_ctx->lambda_local,pcis->vec1_B);
    2364:   if (mat_ctx->l2g_p) {
    2365:     VecScatterBegin(mat_ctx->l2g_p,fetidp_flux_sol,mat_ctx->vP,INSERT_VALUES,SCATTER_REVERSE);
    2366:     VecScatterEnd(mat_ctx->l2g_p,fetidp_flux_sol,mat_ctx->vP,INSERT_VALUES,SCATTER_REVERSE);
    2367:     MatMultAdd(mat_ctx->Bt_BB,mat_ctx->vP,pcis->vec1_B,pcis->vec1_B);
    2368:   }

    2370:   /* compute rhs for BDDC application */
    2371:   VecAYPX(pcis->vec1_B,-1.0,mat_ctx->temp_solution_B);
    2372:   if (pcbddc->switch_static) {
    2373:     VecCopy(mat_ctx->temp_solution_D,pcis->vec1_D);
    2374:     if (mat_ctx->l2g_p) {
    2375:       VecScale(mat_ctx->vP,-1.);
    2376:       MatMultAdd(mat_ctx->Bt_BI,mat_ctx->vP,pcis->vec1_D,pcis->vec1_D);
    2377:     }
    2378:   }

    2380:   /* apply BDDC */
    2381:   PetscArrayzero(pcbddc->benign_p0,pcbddc->benign_n);
    2382:   PCBDDCApplyInterfacePreconditioner(mat_ctx->pc,PETSC_FALSE);

    2384:   /* put values into global vector */
    2385:   if (pcbddc->ChangeOfBasisMatrix) work = pcbddc->work_change;
    2386:   else work = standard_sol;
    2387:   VecScatterBegin(pcis->global_to_B,pcis->vec1_B,work,INSERT_VALUES,SCATTER_REVERSE);
    2388:   VecScatterEnd(pcis->global_to_B,pcis->vec1_B,work,INSERT_VALUES,SCATTER_REVERSE);
    2389:   if (!pcbddc->switch_static) {
    2390:     /* compute values into the interior if solved for the partially subassembled Schur complement */
    2391:     MatMult(pcis->A_IB,pcis->vec1_B,pcis->vec1_D);
    2392:     VecAYPX(pcis->vec1_D,-1.0,mat_ctx->temp_solution_D);
    2393:     PetscLogEventBegin(PC_BDDC_Solves[pcbddc->current_level][0],mat_ctx->pc,0,0,0);
    2394:     KSPSolve(pcbddc->ksp_D,pcis->vec1_D,pcis->vec1_D);
    2395:     PetscLogEventEnd(PC_BDDC_Solves[pcbddc->current_level][0],mat_ctx->pc,0,0,0);
    2396:     /* Cannot propagate up error in KSPSolve() because there is no access to the PC */
    2397:   }

    2399:   VecScatterBegin(pcis->global_to_D,pcis->vec1_D,work,INSERT_VALUES,SCATTER_REVERSE);
    2400:   VecScatterEnd(pcis->global_to_D,pcis->vec1_D,work,INSERT_VALUES,SCATTER_REVERSE);
    2401:   /* add p0 solution to final solution */
    2402:   PCBDDCBenignGetOrSetP0(mat_ctx->pc,work,PETSC_FALSE);
    2403:   if (pcbddc->ChangeOfBasisMatrix) {
    2404:     MatMult(pcbddc->ChangeOfBasisMatrix,work,standard_sol);
    2405:   }
    2406:   PCPostSolve_BDDC(mat_ctx->pc,NULL,NULL,standard_sol);
    2407:   if (mat_ctx->g2g_p) {
    2408:     VecScatterBegin(mat_ctx->g2g_p,fetidp_flux_sol,standard_sol,INSERT_VALUES,SCATTER_REVERSE);
    2409:     VecScatterEnd(mat_ctx->g2g_p,fetidp_flux_sol,standard_sol,INSERT_VALUES,SCATTER_REVERSE);
    2410:   }
    2411:   return 0;
    2412: }

    2414: static PetscErrorCode PCView_BDDCIPC(PC pc, PetscViewer viewer)
    2415: {
    2416:   BDDCIPC_ctx    bddcipc_ctx;
    2417:   PetscBool      isascii;

    2419:   PCShellGetContext(pc,&bddcipc_ctx);
    2420:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
    2421:   if (isascii) {
    2422:     PetscViewerASCIIPrintf(viewer,"BDDC interface preconditioner\n");
    2423:   }
    2424:   PetscViewerASCIIPushTab(viewer);
    2425:   PCView(bddcipc_ctx->bddc,viewer);
    2426:   PetscViewerASCIIPopTab(viewer);
    2427:   return 0;
    2428: }

    2430: static PetscErrorCode PCSetUp_BDDCIPC(PC pc)
    2431: {
    2432:   BDDCIPC_ctx    bddcipc_ctx;
    2433:   PetscBool      isbddc;
    2434:   Vec            vv;
    2435:   IS             is;
    2436:   PC_IS          *pcis;

    2438:   PCShellGetContext(pc,&bddcipc_ctx);
    2439:   PetscObjectTypeCompare((PetscObject)bddcipc_ctx->bddc,PCBDDC,&isbddc);
    2441:   PCSetUp(bddcipc_ctx->bddc);

    2443:   /* create interface scatter */
    2444:   pcis = (PC_IS*)(bddcipc_ctx->bddc->data);
    2445:   VecScatterDestroy(&bddcipc_ctx->g2l);
    2446:   MatCreateVecs(pc->pmat,&vv,NULL);
    2447:   ISRenumber(pcis->is_B_global,NULL,NULL,&is);
    2448:   VecScatterCreate(vv,is,pcis->vec1_B,NULL,&bddcipc_ctx->g2l);
    2449:   ISDestroy(&is);
    2450:   VecDestroy(&vv);
    2451:   return 0;
    2452: }

    2454: static PetscErrorCode PCApply_BDDCIPC(PC pc, Vec r, Vec x)
    2455: {
    2456:   BDDCIPC_ctx    bddcipc_ctx;
    2457:   PC_IS          *pcis;
    2458:   VecScatter     tmps;

    2460:   PCShellGetContext(pc,&bddcipc_ctx);
    2461:   pcis = (PC_IS*)(bddcipc_ctx->bddc->data);
    2462:   tmps = pcis->global_to_B;
    2463:   pcis->global_to_B = bddcipc_ctx->g2l;
    2464:   PCBDDCScalingRestriction(bddcipc_ctx->bddc,r,pcis->vec1_B);
    2465:   PCBDDCApplyInterfacePreconditioner(bddcipc_ctx->bddc,PETSC_FALSE);
    2466:   PCBDDCScalingExtension(bddcipc_ctx->bddc,pcis->vec1_B,x);
    2467:   pcis->global_to_B = tmps;
    2468:   return 0;
    2469: }

    2471: static PetscErrorCode PCApplyTranspose_BDDCIPC(PC pc, Vec r, Vec x)
    2472: {
    2473:   BDDCIPC_ctx    bddcipc_ctx;
    2474:   PC_IS          *pcis;
    2475:   VecScatter     tmps;

    2477:   PCShellGetContext(pc,&bddcipc_ctx);
    2478:   pcis = (PC_IS*)(bddcipc_ctx->bddc->data);
    2479:   tmps = pcis->global_to_B;
    2480:   pcis->global_to_B = bddcipc_ctx->g2l;
    2481:   PCBDDCScalingRestriction(bddcipc_ctx->bddc,r,pcis->vec1_B);
    2482:   PCBDDCApplyInterfacePreconditioner(bddcipc_ctx->bddc,PETSC_TRUE);
    2483:   PCBDDCScalingExtension(bddcipc_ctx->bddc,pcis->vec1_B,x);
    2484:   pcis->global_to_B = tmps;
    2485:   return 0;
    2486: }

    2488: static PetscErrorCode PCDestroy_BDDCIPC(PC pc)
    2489: {
    2490:   BDDCIPC_ctx    bddcipc_ctx;

    2492:   PCShellGetContext(pc,&bddcipc_ctx);
    2493:   PCDestroy(&bddcipc_ctx->bddc);
    2494:   VecScatterDestroy(&bddcipc_ctx->g2l);
    2495:   PetscFree(bddcipc_ctx);
    2496:   return 0;
    2497: }

    2499: /*@
    2500:  PCBDDCMatFETIDPGetSolution - Compute the physical solution using the solution of the FETI-DP linear system

    2502:    Collective

    2504:    Input Parameters:
    2505: +  fetidp_mat      - the FETI-DP matrix obtained by a call to PCBDDCCreateFETIDPOperators
    2506: -  fetidp_flux_sol - the solution of the FETI-DP linear system

    2508:    Output Parameters:
    2509: .  standard_sol    - the solution defined on the physical domain

    2511:    Level: developer

    2513:    Notes:

    2515: .seealso: PCBDDC, PCBDDCCreateFETIDPOperators, PCBDDCMatFETIDPGetRHS
    2516: @*/
    2517: PetscErrorCode PCBDDCMatFETIDPGetSolution(Mat fetidp_mat, Vec fetidp_flux_sol, Vec standard_sol)
    2518: {
    2519:   FETIDPMat_ctx  mat_ctx;

    2524:   MatShellGetContext(fetidp_mat,&mat_ctx);
    2525:   PetscUseMethod(mat_ctx->pc,"PCBDDCMatFETIDPGetSolution_C",(Mat,Vec,Vec),(fetidp_mat,fetidp_flux_sol,standard_sol));
    2526:   return 0;
    2527: }

    2529: static PetscErrorCode PCBDDCCreateFETIDPOperators_BDDC(PC pc, PetscBool fully_redundant, const char* prefix, Mat *fetidp_mat, PC *fetidp_pc)
    2530: {

    2532:   FETIDPMat_ctx  fetidpmat_ctx;
    2533:   Mat            newmat;
    2534:   FETIDPPC_ctx   fetidppc_ctx;
    2535:   PC             newpc;
    2536:   MPI_Comm       comm;

    2538:   PetscObjectGetComm((PetscObject)pc,&comm);
    2539:   /* FETI-DP matrix */
    2540:   PCBDDCCreateFETIDPMatContext(pc,&fetidpmat_ctx);
    2541:   fetidpmat_ctx->fully_redundant = fully_redundant;
    2542:   PCBDDCSetupFETIDPMatContext(fetidpmat_ctx);
    2543:   MatCreateShell(comm,fetidpmat_ctx->n,fetidpmat_ctx->n,fetidpmat_ctx->N,fetidpmat_ctx->N,fetidpmat_ctx,&newmat);
    2544:   PetscObjectSetName((PetscObject)newmat,!fetidpmat_ctx->l2g_lambda_only ? "F" : "G");
    2545:   MatShellSetOperation(newmat,MATOP_MULT,(void (*)(void))FETIDPMatMult);
    2546:   MatShellSetOperation(newmat,MATOP_MULT_TRANSPOSE,(void (*)(void))FETIDPMatMultTranspose);
    2547:   MatShellSetOperation(newmat,MATOP_DESTROY,(void (*)(void))PCBDDCDestroyFETIDPMat);
    2548:   /* propagate MatOptions */
    2549:   {
    2550:     PC_BDDC   *pcbddc = (PC_BDDC*)fetidpmat_ctx->pc->data;
    2551:     PetscBool issym;

    2553:     MatGetOption(pc->mat,MAT_SYMMETRIC,&issym);
    2554:     if (issym || pcbddc->symmetric_primal) {
    2555:       MatSetOption(newmat,MAT_SYMMETRIC,PETSC_TRUE);
    2556:     }
    2557:   }
    2558:   MatSetOptionsPrefix(newmat,prefix);
    2559:   MatAppendOptionsPrefix(newmat,"fetidp_");
    2560:   MatSetUp(newmat);
    2561:   /* FETI-DP preconditioner */
    2562:   PCBDDCCreateFETIDPPCContext(pc,&fetidppc_ctx);
    2563:   PCBDDCSetupFETIDPPCContext(newmat,fetidppc_ctx);
    2564:   PCCreate(comm,&newpc);
    2565:   PCSetOperators(newpc,newmat,newmat);
    2566:   PCSetOptionsPrefix(newpc,prefix);
    2567:   PCAppendOptionsPrefix(newpc,"fetidp_");
    2568:   PCSetErrorIfFailure(newpc,pc->erroriffailure);
    2569:   if (!fetidpmat_ctx->l2g_lambda_only) { /* standard FETI-DP */
    2570:     PCSetType(newpc,PCSHELL);
    2571:     PCShellSetName(newpc,"FETI-DP multipliers");
    2572:     PCShellSetContext(newpc,fetidppc_ctx);
    2573:     PCShellSetApply(newpc,FETIDPPCApply);
    2574:     PCShellSetApplyTranspose(newpc,FETIDPPCApplyTranspose);
    2575:     PCShellSetView(newpc,FETIDPPCView);
    2576:     PCShellSetDestroy(newpc,PCBDDCDestroyFETIDPPC);
    2577:   } else { /* saddle-point FETI-DP */
    2578:     Mat       M;
    2579:     PetscInt  psize;
    2580:     PetscBool fake = PETSC_FALSE, isfieldsplit;

    2582:     ISViewFromOptions(fetidpmat_ctx->lagrange,NULL,"-lag_view");
    2583:     ISViewFromOptions(fetidpmat_ctx->pressure,NULL,"-press_view");
    2584:     PetscObjectQuery((PetscObject)pc,"__KSPFETIDP_PPmat",(PetscObject*)&M);
    2585:     PCSetType(newpc,PCFIELDSPLIT);
    2586:     PCFieldSplitSetIS(newpc,"lag",fetidpmat_ctx->lagrange);
    2587:     PCFieldSplitSetIS(newpc,"p",fetidpmat_ctx->pressure);
    2588:     PCFieldSplitSetType(newpc,PC_COMPOSITE_SCHUR);
    2589:     PCFieldSplitSetSchurFactType(newpc,PC_FIELDSPLIT_SCHUR_FACT_DIAG);
    2590:     ISGetSize(fetidpmat_ctx->pressure,&psize);
    2591:     if (psize != M->rmap->N) {
    2592:       Mat      M2;
    2593:       PetscInt lpsize;

    2595:       fake = PETSC_TRUE;
    2596:       ISGetLocalSize(fetidpmat_ctx->pressure,&lpsize);
    2597:       MatCreate(comm,&M2);
    2598:       MatSetType(M2,MATAIJ);
    2599:       MatSetSizes(M2,lpsize,lpsize,psize,psize);
    2600:       MatSetUp(M2);
    2601:       MatAssemblyBegin(M2,MAT_FINAL_ASSEMBLY);
    2602:       MatAssemblyEnd(M2,MAT_FINAL_ASSEMBLY);
    2603:       PCFieldSplitSetSchurPre(newpc,PC_FIELDSPLIT_SCHUR_PRE_USER,M2);
    2604:       MatDestroy(&M2);
    2605:     } else {
    2606:       PCFieldSplitSetSchurPre(newpc,PC_FIELDSPLIT_SCHUR_PRE_USER,M);
    2607:     }
    2608:     PCFieldSplitSetSchurScale(newpc,1.0);

    2610:     /* we need to setfromoptions and setup here to access the blocks */
    2611:     PCSetFromOptions(newpc);
    2612:     PCSetUp(newpc);

    2614:     /* user may have changed the type (e.g. -fetidp_pc_type none) */
    2615:     PetscObjectTypeCompare((PetscObject)newpc,PCFIELDSPLIT,&isfieldsplit);
    2616:     if (isfieldsplit) {
    2617:       KSP       *ksps;
    2618:       PC        ppc,lagpc;
    2619:       PetscInt  nn;
    2620:       PetscBool ismatis,matisok = PETSC_FALSE,check = PETSC_FALSE;

    2622:       /* set the solver for the (0,0) block */
    2623:       PCFieldSplitSchurGetSubKSP(newpc,&nn,&ksps);
    2624:       if (!nn) { /* not of type PC_COMPOSITE_SCHUR */
    2625:         PCFieldSplitGetSubKSP(newpc,&nn,&ksps);
    2626:         if (!fake) { /* pass pmat to the pressure solver */
    2627:           Mat F;

    2629:           KSPGetOperators(ksps[1],&F,NULL);
    2630:           KSPSetOperators(ksps[1],F,M);
    2631:         }
    2632:       } else {
    2633:         PetscBool issym;
    2634:         Mat       S;

    2636:         PCFieldSplitSchurGetS(newpc,&S);

    2638:         MatGetOption(newmat,MAT_SYMMETRIC,&issym);
    2639:         if (issym) {
    2640:           MatSetOption(S,MAT_SYMMETRIC,PETSC_TRUE);
    2641:         }
    2642:       }
    2643:       KSPGetPC(ksps[0],&lagpc);
    2644:       PCSetType(lagpc,PCSHELL);
    2645:       PCShellSetName(lagpc,"FETI-DP multipliers");
    2646:       PCShellSetContext(lagpc,fetidppc_ctx);
    2647:       PCShellSetApply(lagpc,FETIDPPCApply);
    2648:       PCShellSetApplyTranspose(lagpc,FETIDPPCApplyTranspose);
    2649:       PCShellSetView(lagpc,FETIDPPCView);
    2650:       PCShellSetDestroy(lagpc,PCBDDCDestroyFETIDPPC);

    2652:       /* Olof's idea: interface Schur complement preconditioner for the mass matrix */
    2653:       KSPGetPC(ksps[1],&ppc);
    2654:       if (fake) {
    2655:         BDDCIPC_ctx    bddcipc_ctx;
    2656:         PetscContainer c;

    2658:         matisok = PETSC_TRUE;

    2660:         /* create inner BDDC solver */
    2661:         PetscNew(&bddcipc_ctx);
    2662:         PCCreate(comm,&bddcipc_ctx->bddc);
    2663:         PCSetType(bddcipc_ctx->bddc,PCBDDC);
    2664:         PCSetOperators(bddcipc_ctx->bddc,M,M);
    2665:         PetscObjectQuery((PetscObject)pc,"__KSPFETIDP_pCSR",(PetscObject*)&c);
    2666:         PetscObjectTypeCompare((PetscObject)M,MATIS,&ismatis);
    2667:         if (c && ismatis) {
    2668:           Mat      lM;
    2669:           PetscInt *csr,n;

    2671:           MatISGetLocalMat(M,&lM);
    2672:           MatGetSize(lM,&n,NULL);
    2673:           PetscContainerGetPointer(c,(void**)&csr);
    2674:           PCBDDCSetLocalAdjacencyGraph(bddcipc_ctx->bddc,n,csr,csr + (n + 1),PETSC_COPY_VALUES);
    2675:           MatISRestoreLocalMat(M,&lM);
    2676:         }
    2677:         PCSetOptionsPrefix(bddcipc_ctx->bddc,((PetscObject)ksps[1])->prefix);
    2678:         PCSetErrorIfFailure(bddcipc_ctx->bddc,pc->erroriffailure);
    2679:         PCSetFromOptions(bddcipc_ctx->bddc);

    2681:         /* wrap the interface application */
    2682:         PCSetType(ppc,PCSHELL);
    2683:         PCShellSetName(ppc,"FETI-DP pressure");
    2684:         PCShellSetContext(ppc,bddcipc_ctx);
    2685:         PCShellSetSetUp(ppc,PCSetUp_BDDCIPC);
    2686:         PCShellSetApply(ppc,PCApply_BDDCIPC);
    2687:         PCShellSetApplyTranspose(ppc,PCApplyTranspose_BDDCIPC);
    2688:         PCShellSetView(ppc,PCView_BDDCIPC);
    2689:         PCShellSetDestroy(ppc,PCDestroy_BDDCIPC);
    2690:       }

    2692:       /* determine if we need to assemble M to construct a preconditioner */
    2693:       if (!matisok) {
    2694:         PetscObjectTypeCompare((PetscObject)M,MATIS,&ismatis);
    2695:         PetscObjectTypeCompareAny((PetscObject)ppc,&matisok,PCBDDC,PCJACOBI,PCNONE,PCMG,"");
    2696:         if (ismatis && !matisok) {
    2697:           MatConvert(M,MATAIJ,MAT_INPLACE_MATRIX,&M);
    2698:         }
    2699:       }

    2701:       /* run the subproblems to check convergence */
    2702:       PetscOptionsGetBool(NULL,((PetscObject)newmat)->prefix,"-check_saddlepoint",&check,NULL);
    2703:       if (check) {
    2704:         PetscInt i;

    2706:         for (i=0;i<nn;i++) {
    2707:           KSP       kspC;
    2708:           PC        pc;
    2709:           Mat       F,pF;
    2710:           Vec       x,y;
    2711:           PetscBool isschur,prec = PETSC_TRUE;

    2713:           KSPCreate(PetscObjectComm((PetscObject)ksps[i]),&kspC);
    2714:           KSPSetOptionsPrefix(kspC,((PetscObject)ksps[i])->prefix);
    2715:           KSPAppendOptionsPrefix(kspC,"check_");
    2716:           KSPGetOperators(ksps[i],&F,&pF);
    2717:           PetscObjectTypeCompare((PetscObject)F,MATSCHURCOMPLEMENT,&isschur);
    2718:           if (isschur) {
    2719:             KSP  kspS,kspS2;
    2720:             Mat  A00,pA00,A10,A01,A11;
    2721:             char prefix[256];

    2723:             MatSchurComplementGetKSP(F,&kspS);
    2724:             MatSchurComplementGetSubMatrices(F,&A00,&pA00,&A01,&A10,&A11);
    2725:             MatCreateSchurComplement(A00,pA00,A01,A10,A11,&F);
    2726:             MatSchurComplementGetKSP(F,&kspS2);
    2727:             PetscSNPrintf(prefix,sizeof(prefix),"%sschur_",((PetscObject)kspC)->prefix);
    2728:             KSPSetOptionsPrefix(kspS2,prefix);
    2729:             KSPGetPC(kspS2,&pc);
    2730:             PCSetType(pc,PCKSP);
    2731:             PCKSPSetKSP(pc,kspS);
    2732:             KSPSetFromOptions(kspS2);
    2733:             KSPGetPC(kspS2,&pc);
    2734:             PCSetUseAmat(pc,PETSC_TRUE);
    2735:           } else {
    2736:             PetscObjectReference((PetscObject)F);
    2737:           }
    2738:           KSPSetFromOptions(kspC);
    2739:           PetscOptionsGetBool(NULL,((PetscObject)kspC)->prefix,"-preconditioned",&prec,NULL);
    2740:           if (prec)  {
    2741:             KSPGetPC(ksps[i],&pc);
    2742:             KSPSetPC(kspC,pc);
    2743:           }
    2744:           KSPSetOperators(kspC,F,pF);
    2745:           MatCreateVecs(F,&x,&y);
    2746:           VecSetRandom(x,NULL);
    2747:           MatMult(F,x,y);
    2748:           KSPSolve(kspC,y,x);
    2749:           KSPCheckSolve(kspC,pc,x);
    2750:           KSPDestroy(&kspC);
    2751:           MatDestroy(&F);
    2752:           VecDestroy(&x);
    2753:           VecDestroy(&y);
    2754:         }
    2755:       }
    2756:       PetscFree(ksps);
    2757:     }
    2758:   }
    2759:   /* return pointers for objects created */
    2760:   *fetidp_mat = newmat;
    2761:   *fetidp_pc  = newpc;
    2762:   return 0;
    2763: }

    2765: /*@C
    2766:  PCBDDCCreateFETIDPOperators - Create FETI-DP operators

    2768:    Collective

    2770:    Input Parameters:
    2771: +  pc - the BDDC preconditioning context (setup should have been called before)
    2772: .  fully_redundant - true for a fully redundant set of Lagrange multipliers
    2773: -  prefix - optional options database prefix for the objects to be created (can be NULL)

    2775:    Output Parameters:
    2776: +  fetidp_mat - shell FETI-DP matrix object
    2777: -  fetidp_pc  - shell Dirichlet preconditioner for FETI-DP matrix

    2779:    Level: developer

    2781:    Notes:
    2782:      Currently the only operations provided for FETI-DP matrix are MatMult and MatMultTranspose

    2784: .seealso: PCBDDC, PCBDDCMatFETIDPGetRHS, PCBDDCMatFETIDPGetSolution
    2785: @*/
    2786: PetscErrorCode PCBDDCCreateFETIDPOperators(PC pc, PetscBool fully_redundant, const char *prefix, Mat *fetidp_mat, PC *fetidp_pc)
    2787: {
    2789:   if (pc->setupcalled) {
    2790:     PetscUseMethod(pc,"PCBDDCCreateFETIDPOperators_C",(PC,PetscBool,const char*,Mat*,PC*),(pc,fully_redundant,prefix,fetidp_mat,fetidp_pc));
    2791:   } else SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"You must call PCSetup_BDDC() first");
    2792:   return 0;
    2793: }
    2794: /* -------------------------------------------------------------------------- */
    2795: /*MC
    2796:    PCBDDC - Balancing Domain Decomposition by Constraints.

    2798:    An implementation of the BDDC preconditioner based on the bibliography found below.

    2800:    The matrix to be preconditioned (Pmat) must be of type MATIS.

    2802:    Currently works with MATIS matrices with local matrices of type MATSEQAIJ, MATSEQBAIJ or MATSEQSBAIJ, either with real or complex numbers.

    2804:    It also works with unsymmetric and indefinite problems.

    2806:    Unlike 'conventional' interface preconditioners, PCBDDC iterates over all degrees of freedom, not just those on the interface. This allows the use of approximate solvers on the subdomains.

    2808:    Approximate local solvers are automatically adapted (see [1]) if the user has attached a nullspace object to the subdomain matrices, and informed BDDC of using approximate solvers (via the command line).

    2810:    Boundary nodes are split in vertices, edges and faces classes using information from the local to global mapping of dofs and the local connectivity graph of nodes. The latter can be customized by using PCBDDCSetLocalAdjacencyGraph()
    2811:    Additional information on dofs can be provided by using PCBDDCSetDofsSplitting(), PCBDDCSetDirichletBoundaries(), PCBDDCSetNeumannBoundaries(), and PCBDDCSetPrimalVerticesIS() and their local counterparts.

    2813:    Constraints can be customized by attaching a MatNullSpace object to the MATIS matrix via MatSetNearNullSpace(). Non-singular modes are retained via SVD.

    2815:    Change of basis is performed similarly to [2] when requested. When more than one constraint is present on a single connected component (i.e. an edge or a face), a robust method based on local QR factorizations is used.
    2816:    User defined change of basis can be passed to PCBDDC by using PCBDDCSetChangeOfBasisMat()

    2818:    The PETSc implementation also supports multilevel BDDC [3]. Coarse grids are partitioned using a MatPartitioning object.

    2820:    Adaptive selection of primal constraints [4] is supported for SPD systems with high-contrast in the coefficients if MUMPS or MKL_PARDISO are present. Future versions of the code will also consider using PASTIX.

    2822:    An experimental interface to the FETI-DP method is available. FETI-DP operators could be created using PCBDDCCreateFETIDPOperators(). A stand-alone class for the FETI-DP method will be provided in the next releases.

    2824:    Options Database Keys (some of them, run with -help for a complete list):

    2826: +    -pc_bddc_use_vertices <true> - use or not vertices in primal space
    2827: .    -pc_bddc_use_edges <true> - use or not edges in primal space
    2828: .    -pc_bddc_use_faces <false> - use or not faces in primal space
    2829: .    -pc_bddc_symmetric <true> - symmetric computation of primal basis functions. Specify false for unsymmetric problems
    2830: .    -pc_bddc_use_change_of_basis <false> - use change of basis approach (on edges only)
    2831: .    -pc_bddc_use_change_on_faces <false> - use change of basis approach on faces if change of basis has been requested
    2832: .    -pc_bddc_switch_static <false> - switches from M_2 (default) to M_3 operator (see reference article [1])
    2833: .    -pc_bddc_levels <0> - maximum number of levels for multilevel
    2834: .    -pc_bddc_coarsening_ratio <8> - number of subdomains which will be aggregated together at the coarser level (e.g. H/h ratio at the coarser level, significative only in the multilevel case)
    2835: .    -pc_bddc_coarse_redistribute <0> - size of a subset of processors where the coarse problem will be remapped (the value is ignored if not at the coarsest level)
    2836: .    -pc_bddc_use_deluxe_scaling <false> - use deluxe scaling
    2837: .    -pc_bddc_schur_layers <\-1> - select the economic version of deluxe scaling by specifying the number of layers (-1 corresponds to the original deluxe scaling)
    2838: .    -pc_bddc_adaptive_threshold <0.0> - when a value different than zero is specified, adaptive selection of constraints is performed on edges and faces (requires deluxe scaling and MUMPS or MKL_PARDISO installed)
    2839: -    -pc_bddc_check_level <0> - set verbosity level of debugging output

    2841:    Options for Dirichlet, Neumann or coarse solver can be set with
    2842: .vb
    2843:       -pc_bddc_dirichlet_
    2844:       -pc_bddc_neumann_
    2845:       -pc_bddc_coarse_
    2846: .ve
    2847:    e.g. -pc_bddc_dirichlet_ksp_type richardson -pc_bddc_dirichlet_pc_type gamg. PCBDDC uses by default KSPPREONLY and PCLU.

    2849:    When using a multilevel approach, solvers' options at the N-th level (N > 1) can be specified as
    2850: .vb
    2851:       -pc_bddc_dirichlet_lN_
    2852:       -pc_bddc_neumann_lN_
    2853:       -pc_bddc_coarse_lN_
    2854: .ve
    2855:    Note that level number ranges from the finest (0) to the coarsest (N).
    2856:    In order to specify options for the BDDC operators at the coarser levels (and not for the solvers), prepend -pc_bddc_coarse_ or -pc_bddc_coarse_l to the option, e.g.
    2857: .vb
    2858:      -pc_bddc_coarse_pc_bddc_adaptive_threshold 5 -pc_bddc_coarse_l1_pc_bddc_redistribute 3
    2859: .ve
    2860:    will use a threshold of 5 for constraints' selection at the first coarse level and will redistribute the coarse problem of the first coarse level on 3 processors

    2862:    References:
    2863: +  * - C. R. Dohrmann. "An approximate BDDC preconditioner", Numerical Linear Algebra with Applications Volume 14, Issue 2, pages 149--168, March 2007
    2864: .  * - A. Klawonn and O. B. Widlund. "Dual-Primal FETI Methods for Linear Elasticity", Communications on Pure and Applied Mathematics Volume 59, Issue 11, pages 1523--1572, November 2006
    2865: .  * - J. Mandel, B. Sousedik, C. R. Dohrmann. "Multispace and Multilevel BDDC", Computing Volume 83, Issue 2--3, pages 55--85, November 2008
    2866: -  * - C. Pechstein and C. R. Dohrmann. "Modern domain decomposition methods BDDC, deluxe scaling, and an algebraic approach", Seminar talk, Linz, December 2013, http://people.ricam.oeaw.ac.at/c.pechstein/pechstein-bddc2013.pdf

    2868:    Level: intermediate

    2870:    Developer Notes:

    2872:    Contributed by Stefano Zampini

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

    2877: PETSC_EXTERN PetscErrorCode PCCreate_BDDC(PC pc)
    2878: {
    2879:   PC_BDDC             *pcbddc;

    2881:   PetscNewLog(pc,&pcbddc);
    2882:   pc->data = pcbddc;

    2884:   /* create PCIS data structure */
    2885:   PCISCreate(pc);

    2887:   /* create local graph structure */
    2888:   PCBDDCGraphCreate(&pcbddc->mat_graph);

    2890:   /* BDDC nonzero defaults */
    2891:   pcbddc->use_nnsp                  = PETSC_TRUE;
    2892:   pcbddc->use_local_adj             = PETSC_TRUE;
    2893:   pcbddc->use_vertices              = PETSC_TRUE;
    2894:   pcbddc->use_edges                 = PETSC_TRUE;
    2895:   pcbddc->symmetric_primal          = PETSC_TRUE;
    2896:   pcbddc->vertex_size               = 1;
    2897:   pcbddc->recompute_topography      = PETSC_TRUE;
    2898:   pcbddc->coarse_size               = -1;
    2899:   pcbddc->use_exact_dirichlet_trick = PETSC_TRUE;
    2900:   pcbddc->coarsening_ratio          = 8;
    2901:   pcbddc->coarse_eqs_per_proc       = 1;
    2902:   pcbddc->benign_compute_correction = PETSC_TRUE;
    2903:   pcbddc->nedfield                  = -1;
    2904:   pcbddc->nedglobal                 = PETSC_TRUE;
    2905:   pcbddc->graphmaxcount             = PETSC_MAX_INT;
    2906:   pcbddc->sub_schurs_layers         = -1;
    2907:   pcbddc->adaptive_threshold[0]     = 0.0;
    2908:   pcbddc->adaptive_threshold[1]     = 0.0;

    2910:   /* function pointers */
    2911:   pc->ops->apply               = PCApply_BDDC;
    2912:   pc->ops->applytranspose      = PCApplyTranspose_BDDC;
    2913:   pc->ops->setup               = PCSetUp_BDDC;
    2914:   pc->ops->destroy             = PCDestroy_BDDC;
    2915:   pc->ops->setfromoptions      = PCSetFromOptions_BDDC;
    2916:   pc->ops->view                = PCView_BDDC;
    2917:   pc->ops->applyrichardson     = NULL;
    2918:   pc->ops->applysymmetricleft  = NULL;
    2919:   pc->ops->applysymmetricright = NULL;
    2920:   pc->ops->presolve            = PCPreSolve_BDDC;
    2921:   pc->ops->postsolve           = PCPostSolve_BDDC;
    2922:   pc->ops->reset               = PCReset_BDDC;

    2924:   /* composing function */
    2925:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDiscreteGradient_C",PCBDDCSetDiscreteGradient_BDDC);
    2926:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDivergenceMat_C",PCBDDCSetDivergenceMat_BDDC);
    2927:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetChangeOfBasisMat_C",PCBDDCSetChangeOfBasisMat_BDDC);
    2928:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetPrimalVerticesLocalIS_C",PCBDDCSetPrimalVerticesLocalIS_BDDC);
    2929:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetPrimalVerticesIS_C",PCBDDCSetPrimalVerticesIS_BDDC);
    2930:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetPrimalVerticesLocalIS_C",PCBDDCGetPrimalVerticesLocalIS_BDDC);
    2931:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetPrimalVerticesIS_C",PCBDDCGetPrimalVerticesIS_BDDC);
    2932:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetCoarseningRatio_C",PCBDDCSetCoarseningRatio_BDDC);
    2933:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetLevel_C",PCBDDCSetLevel_BDDC);
    2934:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetUseExactDirichlet_C",PCBDDCSetUseExactDirichlet_BDDC);
    2935:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetLevels_C",PCBDDCSetLevels_BDDC);
    2936:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDirichletBoundaries_C",PCBDDCSetDirichletBoundaries_BDDC);
    2937:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDirichletBoundariesLocal_C",PCBDDCSetDirichletBoundariesLocal_BDDC);
    2938:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetNeumannBoundaries_C",PCBDDCSetNeumannBoundaries_BDDC);
    2939:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetNeumannBoundariesLocal_C",PCBDDCSetNeumannBoundariesLocal_BDDC);
    2940:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetDirichletBoundaries_C",PCBDDCGetDirichletBoundaries_BDDC);
    2941:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetDirichletBoundariesLocal_C",PCBDDCGetDirichletBoundariesLocal_BDDC);
    2942:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetNeumannBoundaries_C",PCBDDCGetNeumannBoundaries_BDDC);
    2943:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetNeumannBoundariesLocal_C",PCBDDCGetNeumannBoundariesLocal_BDDC);
    2944:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDofsSplitting_C",PCBDDCSetDofsSplitting_BDDC);
    2945:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDofsSplittingLocal_C",PCBDDCSetDofsSplittingLocal_BDDC);
    2946:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetLocalAdjacencyGraph_C",PCBDDCSetLocalAdjacencyGraph_BDDC);
    2947:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCCreateFETIDPOperators_C",PCBDDCCreateFETIDPOperators_BDDC);
    2948:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCMatFETIDPGetRHS_C",PCBDDCMatFETIDPGetRHS_BDDC);
    2949:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCMatFETIDPGetSolution_C",PCBDDCMatFETIDPGetSolution_BDDC);
    2950:   PetscObjectComposeFunction((PetscObject)pc,"PCPreSolveChangeRHS_C",PCPreSolveChangeRHS_BDDC);
    2951:   PetscObjectComposeFunction((PetscObject)pc,"PCSetCoordinates_C",PCSetCoordinates_BDDC);
    2952:   return 0;
    2953: }

    2955: /*@C
    2956:  PCBDDCInitializePackage - This function initializes everything in the PCBDDC package. It is called
    2957:     from PCInitializePackage().

    2959:  Level: developer

    2961:  .seealso: PetscInitialize()
    2962: @*/
    2963: PetscErrorCode PCBDDCInitializePackage(void)
    2964: {
    2965:   int            i;

    2967:   if (PCBDDCPackageInitialized) return 0;
    2968:   PCBDDCPackageInitialized = PETSC_TRUE;
    2969:   PetscRegisterFinalize(PCBDDCFinalizePackage);

    2971:   /* general events */
    2972:   PetscLogEventRegister("PCBDDCTopo",PC_CLASSID,&PC_BDDC_Topology[0]);
    2973:   PetscLogEventRegister("PCBDDCLKSP",PC_CLASSID,&PC_BDDC_LocalSolvers[0]);
    2974:   PetscLogEventRegister("PCBDDCLWor",PC_CLASSID,&PC_BDDC_LocalWork[0]);
    2975:   PetscLogEventRegister("PCBDDCCorr",PC_CLASSID,&PC_BDDC_CorrectionSetUp[0]);
    2976:   PetscLogEventRegister("PCBDDCASet",PC_CLASSID,&PC_BDDC_ApproxSetUp[0]);
    2977:   PetscLogEventRegister("PCBDDCAApp",PC_CLASSID,&PC_BDDC_ApproxApply[0]);
    2978:   PetscLogEventRegister("PCBDDCCSet",PC_CLASSID,&PC_BDDC_CoarseSetUp[0]);
    2979:   PetscLogEventRegister("PCBDDCCKSP",PC_CLASSID,&PC_BDDC_CoarseSolver[0]);
    2980:   PetscLogEventRegister("PCBDDCAdap",PC_CLASSID,&PC_BDDC_AdaptiveSetUp[0]);
    2981:   PetscLogEventRegister("PCBDDCScal",PC_CLASSID,&PC_BDDC_Scaling[0]);
    2982:   PetscLogEventRegister("PCBDDCSchr",PC_CLASSID,&PC_BDDC_Schurs[0]);
    2983:   PetscLogEventRegister("PCBDDCDirS",PC_CLASSID,&PC_BDDC_Solves[0][0]);
    2984:   PetscLogEventRegister("PCBDDCNeuS",PC_CLASSID,&PC_BDDC_Solves[0][1]);
    2985:   PetscLogEventRegister("PCBDDCCoaS",PC_CLASSID,&PC_BDDC_Solves[0][2]);
    2986:   for (i=1;i<PETSC_PCBDDC_MAXLEVELS;i++) {
    2987:     char ename[32];

    2989:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCTopo l%02d",i);
    2990:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_Topology[i]);
    2991:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCLKSP l%02d",i);
    2992:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_LocalSolvers[i]);
    2993:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCLWor l%02d",i);
    2994:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_LocalWork[i]);
    2995:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCCorr l%02d",i);
    2996:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_CorrectionSetUp[i]);
    2997:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCASet l%02d",i);
    2998:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_ApproxSetUp[i]);
    2999:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCAApp l%02d",i);
    3000:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_ApproxApply[i]);
    3001:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCCSet l%02d",i);
    3002:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_CoarseSetUp[i]);
    3003:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCCKSP l%02d",i);
    3004:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_CoarseSolver[i]);
    3005:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCAdap l%02d",i);
    3006:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_AdaptiveSetUp[i]);
    3007:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCScal l%02d",i);
    3008:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_Scaling[i]);
    3009:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCSchr l%02d",i);
    3010:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_Schurs[i]);
    3011:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCDirS l%02d",i);
    3012:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_Solves[i][0]);
    3013:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCNeuS l%02d",i);
    3014:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_Solves[i][1]);
    3015:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCCoaS l%02d",i);
    3016:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_Solves[i][2]);
    3017:   }
    3018:   return 0;
    3019: }

    3021: /*@C
    3022:  PCBDDCFinalizePackage - This function frees everything from the PCBDDC package. It is
    3023:     called from PetscFinalize() automatically.

    3025:  Level: developer

    3027:  .seealso: PetscFinalize()
    3028: @*/
    3029: PetscErrorCode PCBDDCFinalizePackage(void)
    3030: {
    3031:   PCBDDCPackageInitialized = PETSC_FALSE;
    3032:   return 0;
    3033: }