Actual source code: bddc.c

petsc-3.14.6 2021-03-30
Report Typos and Errors
  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];

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

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

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

 66:   PetscOptionsHead(PetscOptionsObject,"BDDC options");
 67:   /* Verbose debugging */
 68:   PetscOptionsInt("-pc_bddc_check_level","Verbose output for PCBDDC (intended for debug)","none",pcbddc->dbg_flag,&pcbddc->dbg_flag,NULL);
 69:   /* Approximate solvers */
 70:   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);
 71:   if (pcbddc->interface_extension == PC_BDDC_INTERFACE_EXT_DIRICHLET) {
 72:     PetscOptionsBool("-pc_bddc_dirichlet_approximate","Inform PCBDDC that we are using approximate Dirichlet solvers","none",pcbddc->NullSpace_corr[0],&pcbddc->NullSpace_corr[0],NULL);
 73:     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);
 74:   } else {
 75:     /* This flag is needed/implied by lumping */
 76:     pcbddc->switch_static = PETSC_TRUE;
 77:   }
 78:   PetscOptionsBool("-pc_bddc_neumann_approximate","Inform PCBDDC that we are using approximate Neumann solvers","none",pcbddc->NullSpace_corr[2],&pcbddc->NullSpace_corr[2],NULL);
 79:   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);
 80:   /* Primal space customization */
 81:   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);
 82:   PetscOptionsInt("-pc_bddc_graph_maxcount","Maximum number of shared subdomains for a connected component","none",pcbddc->graphmaxcount,&pcbddc->graphmaxcount,NULL);
 83:   PetscOptionsBool("-pc_bddc_corner_selection","Activates face-based corner selection","none",pcbddc->corner_selection,&pcbddc->corner_selection,NULL);
 84:   PetscOptionsBool("-pc_bddc_use_vertices","Use or not corner dofs in coarse space","none",pcbddc->use_vertices,&pcbddc->use_vertices,NULL);
 85:   PetscOptionsBool("-pc_bddc_use_edges","Use or not edge constraints in coarse space","none",pcbddc->use_edges,&pcbddc->use_edges,NULL);
 86:   PetscOptionsBool("-pc_bddc_use_faces","Use or not face constraints in coarse space","none",pcbddc->use_faces,&pcbddc->use_faces,NULL);
 87:   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);
 88:   PetscOptionsBool("-pc_bddc_use_nnsp","Use near null space attached to the matrix to compute constraints","none",pcbddc->use_nnsp,&pcbddc->use_nnsp,NULL);
 89:   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);
 90:   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);
 91:   /* Change of basis */
 92:   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);
 93:   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);
 94:   if (!pcbddc->use_change_of_basis) {
 95:     pcbddc->use_change_on_faces = PETSC_FALSE;
 96:   }
 97:   /* Switch between M_2 (default) and M_3 preconditioners (as defined by C. Dohrmann in the ref. article) */
 98:   PetscOptionsBool("-pc_bddc_switch_static","Switch on static condensation ops around the interface preconditioner","none",pcbddc->switch_static,&pcbddc->switch_static,NULL);
 99:   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);
100:   i    = pcbddc->coarsening_ratio;
101:   PetscOptionsInt("-pc_bddc_coarsening_ratio","Set coarsening ratio used in multilevel coarsening","PCBDDCSetCoarseningRatio",i,&i,NULL);
102:   PCBDDCSetCoarseningRatio(pc,i);
103:   i    = pcbddc->max_levels;
104:   PetscOptionsInt("-pc_bddc_levels","Set maximum number of levels for multilevel","PCBDDCSetLevels",i,&i,NULL);
105:   PCBDDCSetLevels(pc,i);
106:   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);
107:   PetscOptionsBool("-pc_bddc_use_coarse_estimates","Use estimated eigenvalues for coarse problem","none",pcbddc->use_coarse_estimates,&pcbddc->use_coarse_estimates,NULL);
108:   PetscOptionsBool("-pc_bddc_use_deluxe_scaling","Use deluxe scaling for BDDC","none",pcbddc->use_deluxe_scaling,&pcbddc->use_deluxe_scaling,NULL);
109:   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);
110:   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);
111:   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);
112:   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);
113:   PetscOptionsBool("-pc_bddc_deluxe_zerorows","Zero rows and columns of deluxe operators associated with primal dofs","none",pcbddc->deluxe_zerorows,&pcbddc->deluxe_zerorows,NULL);
114:   PetscOptionsBool("-pc_bddc_deluxe_singlemat","Collapse deluxe operators","none",pcbddc->deluxe_singlemat,&pcbddc->deluxe_singlemat,NULL);
115:   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);
116:   nt   = 2;
117:   PetscOptionsRealArray("-pc_bddc_adaptive_threshold","Thresholds to be used for adaptive selection of constraints","none",pcbddc->adaptive_threshold,&nt,NULL);
118:   if (nt == 1) pcbddc->adaptive_threshold[1] = pcbddc->adaptive_threshold[0];
119:   PetscOptionsInt("-pc_bddc_adaptive_nmin","Minimum number of constraints per connected components","none",pcbddc->adaptive_nmin,&pcbddc->adaptive_nmin,NULL);
120:   PetscOptionsInt("-pc_bddc_adaptive_nmax","Maximum number of constraints per connected components","none",pcbddc->adaptive_nmax,&pcbddc->adaptive_nmax,NULL);
121:   PetscOptionsBool("-pc_bddc_symmetric","Symmetric computation of primal basis functions","none",pcbddc->symmetric_primal,&pcbddc->symmetric_primal,NULL);
122:   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);
123:   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);
124:   PetscOptionsBool("-pc_bddc_benign_change","Compute the pressure change of basis explicitly","none",pcbddc->benign_change_explicit,&pcbddc->benign_change_explicit,NULL);
125:   PetscOptionsBool("-pc_bddc_benign_compute_correction","Compute the benign correction during PreSolve","none",pcbddc->benign_compute_correction,&pcbddc->benign_compute_correction,NULL);
126:   PetscOptionsBool("-pc_bddc_nonetflux","Automatic computation of no-net-flux quadrature weights","none",pcbddc->compute_nonetflux,&pcbddc->compute_nonetflux,NULL);
127:   PetscOptionsBool("-pc_bddc_detect_disconnected","Detects disconnected subdomains","none",pcbddc->detect_disconnected,&pcbddc->detect_disconnected,NULL);
128:   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);
129:   PetscOptionsBool("-pc_bddc_eliminate_dirichlet","Whether or not we want to eliminate dirichlet dofs during presolve","none",pcbddc->eliminate_dirdofs,&pcbddc->eliminate_dirdofs,NULL);
130:   PetscOptionsTail();
131:   return(0);
132: }

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

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

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

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

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

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

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

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

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

297:   PetscObjectReference((PetscObject)G);
298:   MatDestroy(&pcbddc->discretegradient);
299:   pcbddc->discretegradient = G;
300:   pcbddc->nedorder         = order > 0 ? order : -order;
301:   pcbddc->nedfield         = field;
302:   pcbddc->nedglobal        = global;
303:   pcbddc->conforming       = conforming;
304:   return(0);
305: }

307: /*@
308:  PCBDDCSetDiscreteGradient - Sets the discrete gradient

310:    Collective on PC

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

320:    Level: advanced

322:    Notes:
323:     The discrete gradient matrix G is used to analyze the subdomain edges, and it should not contain any zero entry.
324:           For variable order spaces, the order should be set to zero.
325:           If global is true, the rows of G should be given in global ordering for the whole dofs;
326:           if false, the ordering should be global for the Nedelec field.
327:           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
328:           and geid the one for the Nedelec field.

330: .seealso: PCBDDC,PCBDDCSetDofsSplitting(),PCBDDCSetDofsSplittingLocal()
331: @*/
332: PetscErrorCode PCBDDCSetDiscreteGradient(PC pc, Mat G, PetscInt order, PetscInt field, PetscBool global, PetscBool conforming)
333: {

344:   PetscTryMethod(pc,"PCBDDCSetDiscreteGradient_C",(PC,Mat,PetscInt,PetscInt,PetscBool,PetscBool),(pc,G,order,field,global,conforming));
345:   return(0);
346: }

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

354:   PetscObjectReference((PetscObject)divudotp);
355:   MatDestroy(&pcbddc->divudotp);
356:   pcbddc->divudotp = divudotp;
357:   pcbddc->divudotp_trans = trans;
358:   pcbddc->compute_nonetflux = PETSC_TRUE;
359:   if (vl2l) {
360:     PetscObjectReference((PetscObject)vl2l);
361:     ISDestroy(&pcbddc->divudotp_vl2l);
362:     pcbddc->divudotp_vl2l = vl2l;
363:   }
364:   return(0);
365: }

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

370:    Collective on PC

372:    Input Parameters:
373: +  pc - the preconditioning context
374: .  divudotp - the matrix (must be of type MATIS)
375: .  trans - if trans if false (resp. true), then pressures are in the test (trial) space and velocities are in the trial (test) space.
376: -  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

378:    Level: advanced

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

384: .seealso: PCBDDC
385: @*/
386: PetscErrorCode PCBDDCSetDivergenceMat(PC pc, Mat divudotp, PetscBool trans, IS vl2l)
387: {
388:   PetscBool      ismatis;

397:   PetscObjectTypeCompare((PetscObject)divudotp,MATIS,&ismatis);
398:   if (!ismatis) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONG,"Divergence matrix needs to be of type MATIS");
399:   PetscTryMethod(pc,"PCBDDCSetDivergenceMat_C",(PC,Mat,PetscBool,IS),(pc,divudotp,trans,vl2l));
400:   return(0);
401: }

403: static PetscErrorCode PCBDDCSetChangeOfBasisMat_BDDC(PC pc, Mat change, PetscBool interior)
404: {
405:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;

409:   PetscObjectReference((PetscObject)change);
410:   MatDestroy(&pcbddc->user_ChangeOfBasisMatrix);
411:   pcbddc->user_ChangeOfBasisMatrix = change;
412:   pcbddc->change_interior = interior;
413:   return(0);
414: }
415: /*@
416:  PCBDDCSetChangeOfBasisMat - Set user defined change of basis for dofs

418:    Collective on PC

420:    Input Parameters:
421: +  pc - the preconditioning context
422: .  change - the change of basis matrix
423: -  interior - whether or not the change of basis modifies interior dofs

425:    Level: intermediate

427:    Notes:

429: .seealso: PCBDDC
430: @*/
431: PetscErrorCode PCBDDCSetChangeOfBasisMat(PC pc, Mat change, PetscBool interior)
432: {

439:   if (pc->mat) {
440:     PetscInt rows_c,cols_c,rows,cols;
441:     MatGetSize(pc->mat,&rows,&cols);
442:     MatGetSize(change,&rows_c,&cols_c);
443:     if (rows_c != rows) SETERRQ2(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Invalid number of rows for change of basis matrix! %D != %D",rows_c,rows);
444:     if (cols_c != cols) SETERRQ2(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Invalid number of columns for change of basis matrix! %D != %D",cols_c,cols);
445:     MatGetLocalSize(pc->mat,&rows,&cols);
446:     MatGetLocalSize(change,&rows_c,&cols_c);
447:     if (rows_c != rows) SETERRQ2(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Invalid number of local rows for change of basis matrix! %D != %D",rows_c,rows);
448:     if (cols_c != cols) SETERRQ2(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Invalid number of local columns for change of basis matrix! %D != %D",cols_c,cols);
449:   }
450:   PetscTryMethod(pc,"PCBDDCSetChangeOfBasisMat_C",(PC,Mat,PetscBool),(pc,change,interior));
451:   return(0);
452: }

454: static PetscErrorCode PCBDDCSetPrimalVerticesIS_BDDC(PC pc, IS PrimalVertices)
455: {
456:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
457:   PetscBool      isequal = PETSC_FALSE;

461:   PetscObjectReference((PetscObject)PrimalVertices);
462:   if (pcbddc->user_primal_vertices) {
463:     ISEqual(PrimalVertices,pcbddc->user_primal_vertices,&isequal);
464:   }
465:   ISDestroy(&pcbddc->user_primal_vertices);
466:   ISDestroy(&pcbddc->user_primal_vertices_local);
467:   pcbddc->user_primal_vertices = PrimalVertices;
468:   if (!isequal) pcbddc->recompute_topography = PETSC_TRUE;
469:   return(0);
470: }

472: /*@
473:  PCBDDCSetPrimalVerticesIS - Set additional user defined primal vertices in PCBDDC

475:    Collective

477:    Input Parameters:
478: +  pc - the preconditioning context
479: -  PrimalVertices - index set of primal vertices in global numbering (can be empty)

481:    Level: intermediate

483:    Notes:
484:      Any process can list any global node

486: .seealso: PCBDDC, PCBDDCGetPrimalVerticesIS(), PCBDDCSetPrimalVerticesLocalIS(), PCBDDCGetPrimalVerticesLocalIS()
487: @*/
488: PetscErrorCode PCBDDCSetPrimalVerticesIS(PC pc, IS PrimalVertices)
489: {

496:   PetscTryMethod(pc,"PCBDDCSetPrimalVerticesIS_C",(PC,IS),(pc,PrimalVertices));
497:   return(0);
498: }

500: static PetscErrorCode PCBDDCGetPrimalVerticesIS_BDDC(PC pc, IS *is)
501: {
502:   PC_BDDC *pcbddc = (PC_BDDC*)pc->data;

505:   *is = pcbddc->user_primal_vertices;
506:   return(0);
507: }

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

512:    Collective

514:    Input Parameters:
515: .  pc - the preconditioning context

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

520:    Level: intermediate

522:    Notes:

524: .seealso: PCBDDC, PCBDDCSetPrimalVerticesIS(), PCBDDCSetPrimalVerticesLocalIS(), PCBDDCGetPrimalVerticesLocalIS()
525: @*/
526: PetscErrorCode PCBDDCGetPrimalVerticesIS(PC pc, IS *is)
527: {

533:   PetscUseMethod(pc,"PCBDDCGetPrimalVerticesIS_C",(PC,IS*),(pc,is));
534:   return(0);
535: }

537: static PetscErrorCode PCBDDCSetPrimalVerticesLocalIS_BDDC(PC pc, IS PrimalVertices)
538: {
539:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
540:   PetscBool      isequal = PETSC_FALSE;

544:   PetscObjectReference((PetscObject)PrimalVertices);
545:   if (pcbddc->user_primal_vertices_local) {
546:     ISEqual(PrimalVertices,pcbddc->user_primal_vertices_local,&isequal);
547:   }
548:   ISDestroy(&pcbddc->user_primal_vertices);
549:   ISDestroy(&pcbddc->user_primal_vertices_local);
550:   pcbddc->user_primal_vertices_local = PrimalVertices;
551:   if (!isequal) pcbddc->recompute_topography = PETSC_TRUE;
552:   return(0);
553: }

555: /*@
556:  PCBDDCSetPrimalVerticesLocalIS - Set additional user defined primal vertices in PCBDDC

558:    Collective

560:    Input Parameters:
561: +  pc - the preconditioning context
562: -  PrimalVertices - index set of primal vertices in local numbering (can be empty)

564:    Level: intermediate

566:    Notes:

568: .seealso: PCBDDC, PCBDDCSetPrimalVerticesIS(), PCBDDCGetPrimalVerticesIS(), PCBDDCGetPrimalVerticesLocalIS()
569: @*/
570: PetscErrorCode PCBDDCSetPrimalVerticesLocalIS(PC pc, IS PrimalVertices)
571: {

578:   PetscTryMethod(pc,"PCBDDCSetPrimalVerticesLocalIS_C",(PC,IS),(pc,PrimalVertices));
579:   return(0);
580: }

582: static PetscErrorCode PCBDDCGetPrimalVerticesLocalIS_BDDC(PC pc, IS *is)
583: {
584:   PC_BDDC *pcbddc = (PC_BDDC*)pc->data;

587:   *is = pcbddc->user_primal_vertices_local;
588:   return(0);
589: }

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

594:    Collective

596:    Input Parameters:
597: .  pc - the preconditioning context

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

602:    Level: intermediate

604:    Notes:

606: .seealso: PCBDDC, PCBDDCSetPrimalVerticesIS(), PCBDDCGetPrimalVerticesIS(), PCBDDCSetPrimalVerticesLocalIS()
607: @*/
608: PetscErrorCode PCBDDCGetPrimalVerticesLocalIS(PC pc, IS *is)
609: {

615:   PetscUseMethod(pc,"PCBDDCGetPrimalVerticesLocalIS_C",(PC,IS*),(pc,is));
616:   return(0);
617: }

619: static PetscErrorCode PCBDDCSetCoarseningRatio_BDDC(PC pc,PetscInt k)
620: {
621:   PC_BDDC  *pcbddc = (PC_BDDC*)pc->data;

624:   pcbddc->coarsening_ratio = k;
625:   return(0);
626: }

628: /*@
629:  PCBDDCSetCoarseningRatio - Set coarsening ratio used in multilevel

631:    Logically collective on PC

633:    Input Parameters:
634: +  pc - the preconditioning context
635: -  k - coarsening ratio (H/h at the coarser level)

637:    Options Database Keys:
638: .    -pc_bddc_coarsening_ratio

640:    Level: intermediate

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

645: .seealso: PCBDDC, PCBDDCSetLevels()
646: @*/
647: PetscErrorCode PCBDDCSetCoarseningRatio(PC pc,PetscInt k)
648: {

654:   PetscTryMethod(pc,"PCBDDCSetCoarseningRatio_C",(PC,PetscInt),(pc,k));
655:   return(0);
656: }

658: /* The following functions (PCBDDCSetUseExactDirichlet PCBDDCSetLevel) are not public */
659: static PetscErrorCode PCBDDCSetUseExactDirichlet_BDDC(PC pc,PetscBool flg)
660: {
661:   PC_BDDC  *pcbddc = (PC_BDDC*)pc->data;

664:   pcbddc->use_exact_dirichlet_trick = flg;
665:   return(0);
666: }

668: PetscErrorCode PCBDDCSetUseExactDirichlet(PC pc,PetscBool flg)
669: {

675:   PetscTryMethod(pc,"PCBDDCSetUseExactDirichlet_C",(PC,PetscBool),(pc,flg));
676:   return(0);
677: }

679: static PetscErrorCode PCBDDCSetLevel_BDDC(PC pc,PetscInt level)
680: {
681:   PC_BDDC  *pcbddc = (PC_BDDC*)pc->data;

684:   pcbddc->current_level = level;
685:   return(0);
686: }

688: PetscErrorCode PCBDDCSetLevel(PC pc,PetscInt level)
689: {

695:   PetscTryMethod(pc,"PCBDDCSetLevel_C",(PC,PetscInt),(pc,level));
696:   return(0);
697: }

699: static PetscErrorCode PCBDDCSetLevels_BDDC(PC pc,PetscInt levels)
700: {
701:   PC_BDDC  *pcbddc = (PC_BDDC*)pc->data;

704:   if (levels > PETSC_PCBDDC_MAXLEVELS-1) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Maximum number of additional levels for BDDC is %d",PETSC_PCBDDC_MAXLEVELS-1);
705:   pcbddc->max_levels = levels;
706:   return(0);
707: }

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

712:    Logically collective on PC

714:    Input Parameters:
715: +  pc - the preconditioning context
716: -  levels - the maximum number of levels

718:    Options Database Keys:
719: .    -pc_bddc_levels

721:    Level: intermediate

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

726: .seealso: PCBDDC, PCBDDCSetCoarseningRatio()
727: @*/
728: PetscErrorCode PCBDDCSetLevels(PC pc,PetscInt levels)
729: {

735:   PetscTryMethod(pc,"PCBDDCSetLevels_C",(PC,PetscInt),(pc,levels));
736:   return(0);
737: }

739: static PetscErrorCode PCBDDCSetDirichletBoundaries_BDDC(PC pc,IS DirichletBoundaries)
740: {
741:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
742:   PetscBool      isequal = PETSC_FALSE;

746:   PetscObjectReference((PetscObject)DirichletBoundaries);
747:   if (pcbddc->DirichletBoundaries) {
748:     ISEqual(DirichletBoundaries,pcbddc->DirichletBoundaries,&isequal);
749:   }
750:   /* last user setting takes precendence -> destroy any other customization */
751:   ISDestroy(&pcbddc->DirichletBoundariesLocal);
752:   ISDestroy(&pcbddc->DirichletBoundaries);
753:   pcbddc->DirichletBoundaries = DirichletBoundaries;
754:   if (!isequal) pcbddc->recompute_topography = PETSC_TRUE;
755:   return(0);
756: }

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

761:    Collective

763:    Input Parameters:
764: +  pc - the preconditioning context
765: -  DirichletBoundaries - parallel IS defining the Dirichlet boundaries

767:    Level: intermediate

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

772: .seealso: PCBDDC, PCBDDCSetDirichletBoundariesLocal(), MatZeroRows(), MatZeroRowsColumns()
773: @*/
774: PetscErrorCode PCBDDCSetDirichletBoundaries(PC pc,IS DirichletBoundaries)
775: {

782:   PetscTryMethod(pc,"PCBDDCSetDirichletBoundaries_C",(PC,IS),(pc,DirichletBoundaries));
783:   return(0);
784: }

786: static PetscErrorCode PCBDDCSetDirichletBoundariesLocal_BDDC(PC pc,IS DirichletBoundaries)
787: {
788:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
789:   PetscBool      isequal = PETSC_FALSE;

793:   PetscObjectReference((PetscObject)DirichletBoundaries);
794:   if (pcbddc->DirichletBoundariesLocal) {
795:     ISEqual(DirichletBoundaries,pcbddc->DirichletBoundariesLocal,&isequal);
796:   }
797:   /* last user setting takes precendence -> destroy any other customization */
798:   ISDestroy(&pcbddc->DirichletBoundariesLocal);
799:   ISDestroy(&pcbddc->DirichletBoundaries);
800:   pcbddc->DirichletBoundariesLocal = DirichletBoundaries;
801:   if (!isequal) pcbddc->recompute_topography = PETSC_TRUE;
802:   return(0);
803: }

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

808:    Collective

810:    Input Parameters:
811: +  pc - the preconditioning context
812: -  DirichletBoundaries - parallel IS defining the Dirichlet boundaries (in local ordering)

814:    Level: intermediate

816:    Notes:

818: .seealso: PCBDDC, PCBDDCSetDirichletBoundaries(), MatZeroRows(), MatZeroRowsColumns()
819: @*/
820: PetscErrorCode PCBDDCSetDirichletBoundariesLocal(PC pc,IS DirichletBoundaries)
821: {

828:   PetscTryMethod(pc,"PCBDDCSetDirichletBoundariesLocal_C",(PC,IS),(pc,DirichletBoundaries));
829:   return(0);
830: }

832: static PetscErrorCode PCBDDCSetNeumannBoundaries_BDDC(PC pc,IS NeumannBoundaries)
833: {
834:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
835:   PetscBool      isequal = PETSC_FALSE;

839:   PetscObjectReference((PetscObject)NeumannBoundaries);
840:   if (pcbddc->NeumannBoundaries) {
841:     ISEqual(NeumannBoundaries,pcbddc->NeumannBoundaries,&isequal);
842:   }
843:   /* last user setting takes precendence -> destroy any other customization */
844:   ISDestroy(&pcbddc->NeumannBoundariesLocal);
845:   ISDestroy(&pcbddc->NeumannBoundaries);
846:   pcbddc->NeumannBoundaries = NeumannBoundaries;
847:   if (!isequal) pcbddc->recompute_topography = PETSC_TRUE;
848:   return(0);
849: }

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

854:    Collective

856:    Input Parameters:
857: +  pc - the preconditioning context
858: -  NeumannBoundaries - parallel IS defining the Neumann boundaries

860:    Level: intermediate

862:    Notes:
863:      Any process can list any global node

865: .seealso: PCBDDC, PCBDDCSetNeumannBoundariesLocal()
866: @*/
867: PetscErrorCode PCBDDCSetNeumannBoundaries(PC pc,IS NeumannBoundaries)
868: {

875:   PetscTryMethod(pc,"PCBDDCSetNeumannBoundaries_C",(PC,IS),(pc,NeumannBoundaries));
876:   return(0);
877: }

879: static PetscErrorCode PCBDDCSetNeumannBoundariesLocal_BDDC(PC pc,IS NeumannBoundaries)
880: {
881:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
882:   PetscBool      isequal = PETSC_FALSE;

886:   PetscObjectReference((PetscObject)NeumannBoundaries);
887:   if (pcbddc->NeumannBoundariesLocal) {
888:     ISEqual(NeumannBoundaries,pcbddc->NeumannBoundariesLocal,&isequal);
889:   }
890:   /* last user setting takes precendence -> destroy any other customization */
891:   ISDestroy(&pcbddc->NeumannBoundariesLocal);
892:   ISDestroy(&pcbddc->NeumannBoundaries);
893:   pcbddc->NeumannBoundariesLocal = NeumannBoundaries;
894:   if (!isequal) pcbddc->recompute_topography = PETSC_TRUE;
895:   return(0);
896: }

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

901:    Collective

903:    Input Parameters:
904: +  pc - the preconditioning context
905: -  NeumannBoundaries - parallel IS defining the subdomain part of Neumann boundaries (in local ordering)

907:    Level: intermediate

909:    Notes:

911: .seealso: PCBDDC, PCBDDCSetNeumannBoundaries()
912: @*/
913: PetscErrorCode PCBDDCSetNeumannBoundariesLocal(PC pc,IS NeumannBoundaries)
914: {

921:   PetscTryMethod(pc,"PCBDDCSetNeumannBoundariesLocal_C",(PC,IS),(pc,NeumannBoundaries));
922:   return(0);
923: }

925: static PetscErrorCode PCBDDCGetDirichletBoundaries_BDDC(PC pc,IS *DirichletBoundaries)
926: {
927:   PC_BDDC  *pcbddc = (PC_BDDC*)pc->data;

930:   *DirichletBoundaries = pcbddc->DirichletBoundaries;
931:   return(0);
932: }

934: /*@
935:  PCBDDCGetDirichletBoundaries - Get parallel IS for Dirichlet boundaries

937:    Collective

939:    Input Parameters:
940: .  pc - the preconditioning context

942:    Output Parameters:
943: .  DirichletBoundaries - index set defining the Dirichlet boundaries

945:    Level: intermediate

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

950: .seealso: PCBDDC
951: @*/
952: PetscErrorCode PCBDDCGetDirichletBoundaries(PC pc,IS *DirichletBoundaries)
953: {

958:   PetscUseMethod(pc,"PCBDDCGetDirichletBoundaries_C",(PC,IS*),(pc,DirichletBoundaries));
959:   return(0);
960: }

962: static PetscErrorCode PCBDDCGetDirichletBoundariesLocal_BDDC(PC pc,IS *DirichletBoundaries)
963: {
964:   PC_BDDC  *pcbddc = (PC_BDDC*)pc->data;

967:   *DirichletBoundaries = pcbddc->DirichletBoundariesLocal;
968:   return(0);
969: }

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

974:    Collective

976:    Input Parameters:
977: .  pc - the preconditioning context

979:    Output Parameters:
980: .  DirichletBoundaries - index set defining the subdomain part of Dirichlet boundaries

982:    Level: intermediate

984:    Notes:
985:      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).
986:           In the latter case, the IS will be available after PCSetUp.

988: .seealso: PCBDDC
989: @*/
990: PetscErrorCode PCBDDCGetDirichletBoundariesLocal(PC pc,IS *DirichletBoundaries)
991: {

996:   PetscUseMethod(pc,"PCBDDCGetDirichletBoundariesLocal_C",(PC,IS*),(pc,DirichletBoundaries));
997:   return(0);
998: }

1000: static PetscErrorCode PCBDDCGetNeumannBoundaries_BDDC(PC pc,IS *NeumannBoundaries)
1001: {
1002:   PC_BDDC  *pcbddc = (PC_BDDC*)pc->data;

1005:   *NeumannBoundaries = pcbddc->NeumannBoundaries;
1006:   return(0);
1007: }

1009: /*@
1010:  PCBDDCGetNeumannBoundaries - Get parallel IS for Neumann boundaries

1012:    Collective

1014:    Input Parameters:
1015: .  pc - the preconditioning context

1017:    Output Parameters:
1018: .  NeumannBoundaries - index set defining the Neumann boundaries

1020:    Level: intermediate

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

1025: .seealso: PCBDDC
1026: @*/
1027: PetscErrorCode PCBDDCGetNeumannBoundaries(PC pc,IS *NeumannBoundaries)
1028: {

1033:   PetscUseMethod(pc,"PCBDDCGetNeumannBoundaries_C",(PC,IS*),(pc,NeumannBoundaries));
1034:   return(0);
1035: }

1037: static PetscErrorCode PCBDDCGetNeumannBoundariesLocal_BDDC(PC pc,IS *NeumannBoundaries)
1038: {
1039:   PC_BDDC  *pcbddc = (PC_BDDC*)pc->data;

1042:   *NeumannBoundaries = pcbddc->NeumannBoundariesLocal;
1043:   return(0);
1044: }

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

1049:    Collective

1051:    Input Parameters:
1052: .  pc - the preconditioning context

1054:    Output Parameters:
1055: .  NeumannBoundaries - index set defining the subdomain part of Neumann boundaries

1057:    Level: intermediate

1059:    Notes:
1060:      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).
1061:           In the latter case, the IS will be available after PCSetUp.

1063: .seealso: PCBDDC
1064: @*/
1065: PetscErrorCode PCBDDCGetNeumannBoundariesLocal(PC pc,IS *NeumannBoundaries)
1066: {

1071:   PetscUseMethod(pc,"PCBDDCGetNeumannBoundariesLocal_C",(PC,IS*),(pc,NeumannBoundaries));
1072:   return(0);
1073: }

1075: static PetscErrorCode PCBDDCSetLocalAdjacencyGraph_BDDC(PC pc, PetscInt nvtxs,const PetscInt xadj[],const PetscInt adjncy[], PetscCopyMode copymode)
1076: {
1077:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
1078:   PCBDDCGraph    mat_graph = pcbddc->mat_graph;
1079:   PetscBool      same_data = PETSC_FALSE;

1083:   if (!nvtxs) {
1084:     if (copymode == PETSC_OWN_POINTER) {
1085:       PetscFree(xadj);
1086:       PetscFree(adjncy);
1087:     }
1088:     PCBDDCGraphResetCSR(mat_graph);
1089:     return(0);
1090:   }
1091:   if (mat_graph->nvtxs == nvtxs && mat_graph->freecsr) { /* we own the data */
1092:     if (mat_graph->xadj == xadj && mat_graph->adjncy == adjncy) same_data = PETSC_TRUE;
1093:     if (!same_data && mat_graph->xadj[nvtxs] == xadj[nvtxs]) {
1094:       PetscArraycmp(xadj,mat_graph->xadj,nvtxs+1,&same_data);
1095:       if (same_data) {
1096:         PetscArraycmp(adjncy,mat_graph->adjncy,xadj[nvtxs],&same_data);
1097:       }
1098:     }
1099:   }
1100:   if (!same_data) {
1101:     /* free old CSR */
1102:     PCBDDCGraphResetCSR(mat_graph);
1103:     /* get CSR into graph structure */
1104:     if (copymode == PETSC_COPY_VALUES) {
1105:       PetscMalloc1(nvtxs+1,&mat_graph->xadj);
1106:       PetscMalloc1(xadj[nvtxs],&mat_graph->adjncy);
1107:       PetscArraycpy(mat_graph->xadj,xadj,nvtxs+1);
1108:       PetscArraycpy(mat_graph->adjncy,adjncy,xadj[nvtxs]);
1109:       mat_graph->freecsr = PETSC_TRUE;
1110:     } else if (copymode == PETSC_OWN_POINTER) {
1111:       mat_graph->xadj    = (PetscInt*)xadj;
1112:       mat_graph->adjncy  = (PetscInt*)adjncy;
1113:       mat_graph->freecsr = PETSC_TRUE;
1114:     } else if (copymode == PETSC_USE_POINTER) {
1115:       mat_graph->xadj    = (PetscInt*)xadj;
1116:       mat_graph->adjncy  = (PetscInt*)adjncy;
1117:       mat_graph->freecsr = PETSC_FALSE;
1118:     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported copy mode %D",copymode);
1119:     mat_graph->nvtxs_csr = nvtxs;
1120:     pcbddc->recompute_topography = PETSC_TRUE;
1121:   }
1122:   return(0);
1123: }

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

1128:    Not collective

1130:    Input Parameters:
1131: +  pc - the preconditioning context.
1132: .  nvtxs - number of local vertices of the graph (i.e., the number of local dofs).
1133: .  xadj, adjncy - the connectivity of the dofs in CSR format.
1134: -  copymode - supported modes are PETSC_COPY_VALUES, PETSC_USE_POINTER or PETSC_OWN_POINTER.

1136:    Level: intermediate

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

1141: .seealso: PCBDDC,PetscCopyMode
1142: @*/
1143: PetscErrorCode PCBDDCSetLocalAdjacencyGraph(PC pc,PetscInt nvtxs,const PetscInt xadj[],const PetscInt adjncy[], PetscCopyMode copymode)
1144: {
1145:   void (*f)(void) = NULL;

1150:   if (nvtxs) {
1153:   }
1154:   PetscTryMethod(pc,"PCBDDCSetLocalAdjacencyGraph_C",(PC,PetscInt,const PetscInt[],const PetscInt[],PetscCopyMode),(pc,nvtxs,xadj,adjncy,copymode));
1155:   /* free arrays if PCBDDC is not the PC type */
1156:   PetscObjectQueryFunction((PetscObject)pc,"PCBDDCSetLocalAdjacencyGraph_C",&f);
1157:   if (!f && copymode == PETSC_OWN_POINTER) {
1158:     PetscFree(xadj);
1159:     PetscFree(adjncy);
1160:   }
1161:   return(0);
1162: }

1164: static PetscErrorCode PCBDDCSetDofsSplittingLocal_BDDC(PC pc,PetscInt n_is, IS ISForDofs[])
1165: {
1166:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
1167:   PetscInt       i;
1168:   PetscBool      isequal = PETSC_FALSE;

1172:   if (pcbddc->n_ISForDofsLocal == n_is) {
1173:     for (i=0;i<n_is;i++) {
1174:       PetscBool isequalt;
1175:       ISEqual(ISForDofs[i],pcbddc->ISForDofsLocal[i],&isequalt);
1176:       if (!isequalt) break;
1177:     }
1178:     if (i == n_is) isequal = PETSC_TRUE;
1179:   }
1180:   for (i=0;i<n_is;i++) {
1181:     PetscObjectReference((PetscObject)ISForDofs[i]);
1182:   }
1183:   /* Destroy ISes if they were already set */
1184:   for (i=0;i<pcbddc->n_ISForDofsLocal;i++) {
1185:     ISDestroy(&pcbddc->ISForDofsLocal[i]);
1186:   }
1187:   PetscFree(pcbddc->ISForDofsLocal);
1188:   /* last user setting takes precendence -> destroy any other customization */
1189:   for (i=0;i<pcbddc->n_ISForDofs;i++) {
1190:     ISDestroy(&pcbddc->ISForDofs[i]);
1191:   }
1192:   PetscFree(pcbddc->ISForDofs);
1193:   pcbddc->n_ISForDofs = 0;
1194:   /* allocate space then set */
1195:   if (n_is) {
1196:     PetscMalloc1(n_is,&pcbddc->ISForDofsLocal);
1197:   }
1198:   for (i=0;i<n_is;i++) {
1199:     pcbddc->ISForDofsLocal[i] = ISForDofs[i];
1200:   }
1201:   pcbddc->n_ISForDofsLocal = n_is;
1202:   if (n_is) pcbddc->user_provided_isfordofs = PETSC_TRUE;
1203:   if (!isequal) pcbddc->recompute_topography = PETSC_TRUE;
1204:   return(0);
1205: }

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

1210:    Collective

1212:    Input Parameters:
1213: +  pc - the preconditioning context
1214: .  n_is - number of index sets defining the fields
1215: -  ISForDofs - array of IS describing the fields in local ordering

1217:    Level: intermediate

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

1222: .seealso: PCBDDC
1223: @*/
1224: PetscErrorCode PCBDDCSetDofsSplittingLocal(PC pc,PetscInt n_is, IS ISForDofs[])
1225: {
1226:   PetscInt       i;

1232:   for (i=0;i<n_is;i++) {
1235:   }
1236:   PetscTryMethod(pc,"PCBDDCSetDofsSplittingLocal_C",(PC,PetscInt,IS[]),(pc,n_is,ISForDofs));
1237:   return(0);
1238: }

1240: static PetscErrorCode PCBDDCSetDofsSplitting_BDDC(PC pc,PetscInt n_is, IS ISForDofs[])
1241: {
1242:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
1243:   PetscInt       i;
1244:   PetscBool      isequal = PETSC_FALSE;

1248:   if (pcbddc->n_ISForDofs == n_is) {
1249:     for (i=0;i<n_is;i++) {
1250:       PetscBool isequalt;
1251:       ISEqual(ISForDofs[i],pcbddc->ISForDofs[i],&isequalt);
1252:       if (!isequalt) break;
1253:     }
1254:     if (i == n_is) isequal = PETSC_TRUE;
1255:   }
1256:   for (i=0;i<n_is;i++) {
1257:     PetscObjectReference((PetscObject)ISForDofs[i]);
1258:   }
1259:   /* Destroy ISes if they were already set */
1260:   for (i=0;i<pcbddc->n_ISForDofs;i++) {
1261:     ISDestroy(&pcbddc->ISForDofs[i]);
1262:   }
1263:   PetscFree(pcbddc->ISForDofs);
1264:   /* last user setting takes precendence -> destroy any other customization */
1265:   for (i=0;i<pcbddc->n_ISForDofsLocal;i++) {
1266:     ISDestroy(&pcbddc->ISForDofsLocal[i]);
1267:   }
1268:   PetscFree(pcbddc->ISForDofsLocal);
1269:   pcbddc->n_ISForDofsLocal = 0;
1270:   /* allocate space then set */
1271:   if (n_is) {
1272:     PetscMalloc1(n_is,&pcbddc->ISForDofs);
1273:   }
1274:   for (i=0;i<n_is;i++) {
1275:     pcbddc->ISForDofs[i] = ISForDofs[i];
1276:   }
1277:   pcbddc->n_ISForDofs = n_is;
1278:   if (n_is) pcbddc->user_provided_isfordofs = PETSC_TRUE;
1279:   if (!isequal) pcbddc->recompute_topography = PETSC_TRUE;
1280:   return(0);
1281: }

1283: /*@
1284:  PCBDDCSetDofsSplitting - Set index sets defining fields of the global matrix

1286:    Collective

1288:    Input Parameters:
1289: +  pc - the preconditioning context
1290: .  n_is - number of index sets defining the fields
1291: -  ISForDofs - array of IS describing the fields in global ordering

1293:    Level: intermediate

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

1298: .seealso: PCBDDC
1299: @*/
1300: PetscErrorCode PCBDDCSetDofsSplitting(PC pc,PetscInt n_is, IS ISForDofs[])
1301: {
1302:   PetscInt       i;

1308:   for (i=0;i<n_is;i++) {
1311:   }
1312:   PetscTryMethod(pc,"PCBDDCSetDofsSplitting_C",(PC,PetscInt,IS[]),(pc,n_is,ISForDofs));
1313:   return(0);
1314: }

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

1320:    Input Parameter:
1321: +  pc - the preconditioner context

1323:    Application Interface Routine: PCPreSolve()

1325:    Notes:
1326:      The interface routine PCPreSolve() is not usually called directly by
1327:    the user, but instead is called by KSPSolve().
1328: */
1329: static PetscErrorCode PCPreSolve_BDDC(PC pc, KSP ksp, Vec rhs, Vec x)
1330: {
1332:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
1333:   PC_IS          *pcis = (PC_IS*)(pc->data);
1334:   Vec            used_vec;
1335:   PetscBool      iscg = PETSC_FALSE, save_rhs = PETSC_TRUE, benign_correction_computed;

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

1342:     PetscObjectTypeCompare((PetscObject)ksp,KSPCG,&iscg);
1343:     PetscObjectTypeCompare((PetscObject)ksp,KSPGROPPCG,&isgroppcg);
1344:     PetscObjectTypeCompare((PetscObject)ksp,KSPPIPECG,&ispipecg);
1345:     PetscObjectTypeCompare((PetscObject)ksp,KSPPIPECG,&ispipelcg);
1346:     PetscObjectTypeCompare((PetscObject)ksp,KSPPIPECGRR,&ispipecgrr);
1347:     iscg = (PetscBool)(iscg || isgroppcg || ispipecg || ispipelcg || ispipecgrr);
1348:     if (pcbddc->benign_apply_coarse_only || pcbddc->switch_static || !iscg || pc->mat != pc->pmat) {
1349:       PCBDDCSetUseExactDirichlet(pc,PETSC_FALSE);
1350:     }
1351:   }
1352:   if (pcbddc->benign_apply_coarse_only || pcbddc->switch_static || pc->mat != pc->pmat) {
1353:     PCBDDCSetUseExactDirichlet(pc,PETSC_FALSE);
1354:   }

1356:   /* Creates parallel work vectors used in presolve */
1357:   if (!pcbddc->original_rhs) {
1358:     VecDuplicate(pcis->vec1_global,&pcbddc->original_rhs);
1359:   }
1360:   if (!pcbddc->temp_solution) {
1361:     VecDuplicate(pcis->vec1_global,&pcbddc->temp_solution);
1362:   }

1364:   pcbddc->temp_solution_used = PETSC_FALSE;
1365:   if (x) {
1366:     PetscObjectReference((PetscObject)x);
1367:     used_vec = x;
1368:   } else { /* it can only happen when calling PCBDDCMatFETIDPGetRHS */
1369:     PetscObjectReference((PetscObject)pcbddc->temp_solution);
1370:     used_vec = pcbddc->temp_solution;
1371:     VecSet(used_vec,0.0);
1372:     pcbddc->temp_solution_used = PETSC_TRUE;
1373:     VecCopy(rhs,pcbddc->original_rhs);
1374:     save_rhs = PETSC_FALSE;
1375:     pcbddc->eliminate_dirdofs = PETSC_TRUE;
1376:   }

1378:   /* hack into ksp data structure since PCPreSolve comes earlier than setting to zero the guess in src/ksp/ksp/interface/itfunc.c */
1379:   if (ksp) {
1380:     /* store the flag for the initial guess since it will be restored back during PCPostSolve_BDDC */
1381:     KSPGetInitialGuessNonzero(ksp,&pcbddc->ksp_guess_nonzero);
1382:     if (!pcbddc->ksp_guess_nonzero) {
1383:       VecSet(used_vec,0.0);
1384:     }
1385:   }

1387:   pcbddc->rhs_change = PETSC_FALSE;
1388:   /* Take into account zeroed rows -> change rhs and store solution removed */
1389:   if (rhs && pcbddc->eliminate_dirdofs) {
1390:     IS dirIS = NULL;

1392:     /* DirichletBoundariesLocal may not be consistent among neighbours; gets a dirichlet dofs IS from graph (may be cached) */
1393:     PCBDDCGraphGetDirichletDofs(pcbddc->mat_graph,&dirIS);
1394:     if (dirIS) {
1395:       Mat_IS            *matis = (Mat_IS*)pc->pmat->data;
1396:       PetscInt          dirsize,i,*is_indices;
1397:       PetscScalar       *array_x;
1398:       const PetscScalar *array_diagonal;

1400:       MatGetDiagonal(pc->pmat,pcis->vec1_global);
1401:       VecPointwiseDivide(pcis->vec1_global,rhs,pcis->vec1_global);
1402:       VecScatterBegin(matis->rctx,pcis->vec1_global,pcis->vec2_N,INSERT_VALUES,SCATTER_FORWARD);
1403:       VecScatterEnd(matis->rctx,pcis->vec1_global,pcis->vec2_N,INSERT_VALUES,SCATTER_FORWARD);
1404:       VecScatterBegin(matis->rctx,used_vec,pcis->vec1_N,INSERT_VALUES,SCATTER_FORWARD);
1405:       VecScatterEnd(matis->rctx,used_vec,pcis->vec1_N,INSERT_VALUES,SCATTER_FORWARD);
1406:       ISGetLocalSize(dirIS,&dirsize);
1407:       VecGetArray(pcis->vec1_N,&array_x);
1408:       VecGetArrayRead(pcis->vec2_N,&array_diagonal);
1409:       ISGetIndices(dirIS,(const PetscInt**)&is_indices);
1410:       for (i=0; i<dirsize; i++) array_x[is_indices[i]] = array_diagonal[is_indices[i]];
1411:       ISRestoreIndices(dirIS,(const PetscInt**)&is_indices);
1412:       VecRestoreArrayRead(pcis->vec2_N,&array_diagonal);
1413:       VecRestoreArray(pcis->vec1_N,&array_x);
1414:       VecScatterBegin(matis->rctx,pcis->vec1_N,used_vec,INSERT_VALUES,SCATTER_REVERSE);
1415:       VecScatterEnd(matis->rctx,pcis->vec1_N,used_vec,INSERT_VALUES,SCATTER_REVERSE);
1416:       pcbddc->rhs_change = PETSC_TRUE;
1417:       ISDestroy(&dirIS);
1418:     }
1419:   }

1421:   /* remove the computed solution or the initial guess from the rhs */
1422:   if (pcbddc->rhs_change || (ksp && pcbddc->ksp_guess_nonzero)) {
1423:     /* save the original rhs */
1424:     if (save_rhs) {
1425:       VecSwap(rhs,pcbddc->original_rhs);
1426:       save_rhs = PETSC_FALSE;
1427:     }
1428:     pcbddc->rhs_change = PETSC_TRUE;
1429:     VecScale(used_vec,-1.0);
1430:     MatMultAdd(pc->mat,used_vec,pcbddc->original_rhs,rhs);
1431:     VecScale(used_vec,-1.0);
1432:     VecCopy(used_vec,pcbddc->temp_solution);
1433:     pcbddc->temp_solution_used = PETSC_TRUE;
1434:     if (ksp) {
1435:       KSPSetInitialGuessNonzero(ksp,PETSC_FALSE);
1436:     }
1437:   }
1438:   VecDestroy(&used_vec);

1440:   /* compute initial vector in benign space if needed
1441:      and remove non-benign solution from the rhs */
1442:   benign_correction_computed = PETSC_FALSE;
1443:   if (rhs && pcbddc->benign_compute_correction && (pcbddc->benign_have_null || pcbddc->benign_apply_coarse_only)) {
1444:     /* compute u^*_h using ideas similar to those in Xuemin Tu's PhD thesis (see Section 4.8.1)
1445:        Recursively apply BDDC in the multilevel case */
1446:     if (!pcbddc->benign_vec) {
1447:       VecDuplicate(rhs,&pcbddc->benign_vec);
1448:     }
1449:     /* keep applying coarse solver unless we no longer have benign subdomains */
1450:     pcbddc->benign_apply_coarse_only = pcbddc->benign_have_null ? PETSC_TRUE : PETSC_FALSE;
1451:     if (!pcbddc->benign_skip_correction) {
1452:       PCApply_BDDC(pc,rhs,pcbddc->benign_vec);
1453:       benign_correction_computed = PETSC_TRUE;
1454:       if (pcbddc->temp_solution_used) {
1455:         VecAXPY(pcbddc->temp_solution,1.0,pcbddc->benign_vec);
1456:       }
1457:       VecScale(pcbddc->benign_vec,-1.0);
1458:       /* store the original rhs if not done earlier */
1459:       if (save_rhs) {
1460:         VecSwap(rhs,pcbddc->original_rhs);
1461:       }
1462:       if (pcbddc->rhs_change) {
1463:         MatMultAdd(pc->mat,pcbddc->benign_vec,rhs,rhs);
1464:       } else {
1465:         MatMultAdd(pc->mat,pcbddc->benign_vec,pcbddc->original_rhs,rhs);
1466:       }
1467:       pcbddc->rhs_change = PETSC_TRUE;
1468:     }
1469:     pcbddc->benign_apply_coarse_only = PETSC_FALSE;
1470:   } else {
1471:     VecDestroy(&pcbddc->benign_vec);
1472:   }

1474:   /* dbg output */
1475:   if (pcbddc->dbg_flag && benign_correction_computed) {
1476:     Vec v;

1478:     VecDuplicate(pcis->vec1_global,&v);
1479:     if (pcbddc->ChangeOfBasisMatrix) {
1480:       MatMultTranspose(pcbddc->ChangeOfBasisMatrix,rhs,v);
1481:     } else {
1482:       VecCopy(rhs,v);
1483:     }
1484:     PCBDDCBenignGetOrSetP0(pc,v,PETSC_TRUE);
1485:     PetscViewerASCIIPrintf(pcbddc->dbg_viewer,"LEVEL %D: is the correction benign?\n",pcbddc->current_level);
1486:     PetscScalarView(pcbddc->benign_n,pcbddc->benign_p0,pcbddc->dbg_viewer);
1487:     PetscViewerFlush(pcbddc->dbg_viewer);
1488:     VecDestroy(&v);
1489:   }

1491:   /* set initial guess if using PCG */
1492:   pcbddc->exact_dirichlet_trick_app = PETSC_FALSE;
1493:   if (x && pcbddc->use_exact_dirichlet_trick) {
1494:     VecSet(x,0.0);
1495:     if (pcbddc->ChangeOfBasisMatrix && pcbddc->change_interior) {
1496:       if (benign_correction_computed) { /* we have already saved the changed rhs */
1497:         VecLockReadPop(pcis->vec1_global);
1498:       } else {
1499:         MatMultTranspose(pcbddc->ChangeOfBasisMatrix,rhs,pcis->vec1_global);
1500:       }
1501:       VecScatterBegin(pcis->global_to_D,pcis->vec1_global,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
1502:       VecScatterEnd(pcis->global_to_D,pcis->vec1_global,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
1503:     } else {
1504:       VecScatterBegin(pcis->global_to_D,rhs,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
1505:       VecScatterEnd(pcis->global_to_D,rhs,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
1506:     }
1507:     KSPSolve(pcbddc->ksp_D,pcis->vec1_D,pcis->vec2_D);
1508:     KSPCheckSolve(pcbddc->ksp_D,pc,pcis->vec2_D);
1509:     if (pcbddc->ChangeOfBasisMatrix && pcbddc->change_interior) {
1510:       VecSet(pcis->vec1_global,0.);
1511:       VecScatterBegin(pcis->global_to_D,pcis->vec2_D,pcis->vec1_global,INSERT_VALUES,SCATTER_REVERSE);
1512:       VecScatterEnd(pcis->global_to_D,pcis->vec2_D,pcis->vec1_global,INSERT_VALUES,SCATTER_REVERSE);
1513:       MatMult(pcbddc->ChangeOfBasisMatrix,pcis->vec1_global,x);
1514:     } else {
1515:       VecScatterBegin(pcis->global_to_D,pcis->vec2_D,x,INSERT_VALUES,SCATTER_REVERSE);
1516:       VecScatterEnd(pcis->global_to_D,pcis->vec2_D,x,INSERT_VALUES,SCATTER_REVERSE);
1517:     }
1518:     if (ksp) {
1519:       KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);
1520:     }
1521:     pcbddc->exact_dirichlet_trick_app = PETSC_TRUE;
1522:   } else if (pcbddc->ChangeOfBasisMatrix && pcbddc->change_interior && benign_correction_computed && pcbddc->use_exact_dirichlet_trick) {
1523:     VecLockReadPop(pcis->vec1_global);
1524:   }
1525:   return(0);
1526: }

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

1532:    Input Parameter:
1533: +  pc - the preconditioner context

1535:    Application Interface Routine: PCPostSolve()

1537:    Notes:
1538:      The interface routine PCPostSolve() is not usually called directly by
1539:      the user, but instead is called by KSPSolve().
1540: */
1541: static PetscErrorCode PCPostSolve_BDDC(PC pc, KSP ksp, Vec rhs, Vec x)
1542: {
1544:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;

1547:   /* add solution removed in presolve */
1548:   if (x && pcbddc->rhs_change) {
1549:     if (pcbddc->temp_solution_used) {
1550:       VecAXPY(x,1.0,pcbddc->temp_solution);
1551:     } else if (pcbddc->benign_compute_correction && pcbddc->benign_vec) {
1552:       VecAXPY(x,-1.0,pcbddc->benign_vec);
1553:     }
1554:     /* restore to original state (not for FETI-DP) */
1555:     if (ksp) pcbddc->temp_solution_used = PETSC_FALSE;
1556:   }

1558:   /* restore rhs to its original state (not needed for FETI-DP) */
1559:   if (rhs && pcbddc->rhs_change) {
1560:     VecSwap(rhs,pcbddc->original_rhs);
1561:     pcbddc->rhs_change = PETSC_FALSE;
1562:   }
1563:   /* restore ksp guess state */
1564:   if (ksp) {
1565:     KSPSetInitialGuessNonzero(ksp,pcbddc->ksp_guess_nonzero);
1566:     /* reset flag for exact dirichlet trick */
1567:     pcbddc->exact_dirichlet_trick_app = PETSC_FALSE;
1568:   }
1569:   return(0);
1570: }

1572: /*
1573:    PCSetUp_BDDC - Prepares for the use of the BDDC preconditioner
1574:                   by setting data structures and options.

1576:    Input Parameter:
1577: +  pc - the preconditioner context

1579:    Application Interface Routine: PCSetUp()

1581:    Notes:
1582:      The interface routine PCSetUp() is not usually called directly by
1583:      the user, but instead is called by PCApply() if necessary.
1584: */
1585: PetscErrorCode PCSetUp_BDDC(PC pc)
1586: {
1587:   PC_BDDC*        pcbddc = (PC_BDDC*)pc->data;
1588:   PCBDDCSubSchurs sub_schurs;
1589:   Mat_IS*         matis;
1590:   MatNullSpace    nearnullspace;
1591:   Mat             lA;
1592:   IS              lP,zerodiag = NULL;
1593:   PetscInt        nrows,ncols;
1594:   PetscMPIInt     size;
1595:   PetscBool       computesubschurs;
1596:   PetscBool       computeconstraintsmatrix;
1597:   PetscBool       new_nearnullspace_provided,ismatis,rl;
1598:   PetscErrorCode  ierr;

1601:   PetscObjectTypeCompare((PetscObject)pc->pmat,MATIS,&ismatis);
1602:   if (!ismatis) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONG,"PCBDDC preconditioner requires matrix of type MATIS");
1603:   MatGetSize(pc->pmat,&nrows,&ncols);
1604:   if (nrows != ncols) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"PCBDDC preconditioner requires a square preconditioning matrix");
1605:   MPI_Comm_size(PetscObjectComm((PetscObject)pc),&size);

1607:   matis = (Mat_IS*)pc->pmat->data;
1608:   /* the following lines of code should be replaced by a better logic between PCIS, PCNN, PCBDDC and other future nonoverlapping preconditioners */
1609:   /* For BDDC we need to define a local "Neumann" problem different to that defined in PCISSetup
1610:      Also, BDDC builds its own KSP for the Dirichlet problem */
1611:   rl = pcbddc->recompute_topography;
1612:   if (!pc->setupcalled || pc->flag == DIFFERENT_NONZERO_PATTERN) rl = PETSC_TRUE;
1613:   MPIU_Allreduce(&rl,&pcbddc->recompute_topography,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)pc));
1614:   if (pcbddc->recompute_topography) {
1615:     pcbddc->graphanalyzed    = PETSC_FALSE;
1616:     computeconstraintsmatrix = PETSC_TRUE;
1617:   } else {
1618:     computeconstraintsmatrix = PETSC_FALSE;
1619:   }

1621:   /* check parameters' compatibility */
1622:   if (!pcbddc->use_deluxe_scaling) pcbddc->deluxe_zerorows = PETSC_FALSE;
1623:   pcbddc->adaptive_selection   = (PetscBool)(pcbddc->adaptive_threshold[0] != 0.0 || pcbddc->adaptive_threshold[1] != 0.0);
1624:   pcbddc->use_deluxe_scaling   = (PetscBool)(pcbddc->use_deluxe_scaling && size > 1);
1625:   pcbddc->adaptive_selection   = (PetscBool)(pcbddc->adaptive_selection && size > 1);
1626:   pcbddc->adaptive_userdefined = (PetscBool)(pcbddc->adaptive_selection && pcbddc->adaptive_userdefined);
1627:   if (pcbddc->adaptive_selection) pcbddc->use_faces = PETSC_TRUE;

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

1631:   /* activate all connected components if the netflux has been requested */
1632:   if (pcbddc->compute_nonetflux) {
1633:     pcbddc->use_vertices = PETSC_TRUE;
1634:     pcbddc->use_edges    = PETSC_TRUE;
1635:     pcbddc->use_faces    = PETSC_TRUE;
1636:   }

1638:   /* Get stdout for dbg */
1639:   if (pcbddc->dbg_flag) {
1640:     if (!pcbddc->dbg_viewer) {
1641:       pcbddc->dbg_viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)pc));
1642:     }
1643:     PetscViewerASCIIPushSynchronized(pcbddc->dbg_viewer);
1644:     PetscViewerASCIIAddTab(pcbddc->dbg_viewer,2*pcbddc->current_level);
1645:   }

1647:   /* process topology information */
1648:   PetscLogEventBegin(PC_BDDC_Topology[pcbddc->current_level],pc,0,0,0);
1649:   if (pcbddc->recompute_topography) {
1650:     PCBDDCComputeLocalTopologyInfo(pc);
1651:     if (pcbddc->discretegradient) {
1652:       PCBDDCNedelecSupport(pc);
1653:     }
1654:   }
1655:   if (pcbddc->corner_selected) pcbddc->use_vertices = PETSC_TRUE;

1657:   /* change basis if requested by the user */
1658:   if (pcbddc->user_ChangeOfBasisMatrix) {
1659:     /* use_change_of_basis flag is used to automatically compute a change of basis from constraints */
1660:     pcbddc->use_change_of_basis = PETSC_FALSE;
1661:     PCBDDCComputeLocalMatrix(pc,pcbddc->user_ChangeOfBasisMatrix);
1662:   } else {
1663:     MatDestroy(&pcbddc->local_mat);
1664:     PetscObjectReference((PetscObject)matis->A);
1665:     pcbddc->local_mat = matis->A;
1666:   }

1668:   /*
1669:      Compute change of basis on local pressures (aka zerodiag dofs) with the benign trick
1670:      This should come earlier then PCISSetUp for extracting the correct subdomain matrices
1671:   */
1672:   PCBDDCBenignShellMat(pc,PETSC_TRUE);
1673:   if (pcbddc->benign_saddle_point) {
1674:     PC_IS* pcis = (PC_IS*)pc->data;

1676:     if (pcbddc->user_ChangeOfBasisMatrix || pcbddc->use_change_of_basis || !computesubschurs) pcbddc->benign_change_explicit = PETSC_TRUE;
1677:     /* detect local saddle point and change the basis in pcbddc->local_mat */
1678:     PCBDDCBenignDetectSaddlePoint(pc,(PetscBool)(!pcbddc->recompute_topography),&zerodiag);
1679:     /* pop B0 mat from local mat */
1680:     PCBDDCBenignPopOrPushB0(pc,PETSC_TRUE);
1681:     /* give pcis a hint to not reuse submatrices during PCISCreate */
1682:     if (pc->flag == SAME_NONZERO_PATTERN && pcis->reusesubmatrices == PETSC_TRUE) {
1683:       if (pcbddc->benign_n && (pcbddc->benign_change_explicit || pcbddc->dbg_flag)) {
1684:         pcis->reusesubmatrices = PETSC_FALSE;
1685:       } else {
1686:         pcis->reusesubmatrices = PETSC_TRUE;
1687:       }
1688:     } else {
1689:       pcis->reusesubmatrices = PETSC_FALSE;
1690:     }
1691:   }

1693:   /* propagate relevant information */
1694:   if (matis->A->symmetric_set) {
1695:     MatSetOption(pcbddc->local_mat,MAT_SYMMETRIC,matis->A->symmetric);
1696:   }
1697:   if (matis->A->spd_set) {
1698:     MatSetOption(pcbddc->local_mat,MAT_SPD,matis->A->spd);
1699:   }

1701:   /* Set up all the "iterative substructuring" common block without computing solvers */
1702:   {
1703:     Mat temp_mat;

1705:     temp_mat = matis->A;
1706:     matis->A = pcbddc->local_mat;
1707:     PCISSetUp(pc,PETSC_TRUE,PETSC_FALSE);
1708:     pcbddc->local_mat = matis->A;
1709:     matis->A = temp_mat;
1710:   }

1712:   /* Analyze interface */
1713:   if (!pcbddc->graphanalyzed) {
1714:     PCBDDCAnalyzeInterface(pc);
1715:     computeconstraintsmatrix = PETSC_TRUE;
1716:     if (pcbddc->adaptive_selection && !pcbddc->use_deluxe_scaling && !pcbddc->mat_graph->twodim) {
1717:       SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Cannot compute the adaptive primal space for a problem with 3D edges without deluxe scaling");
1718:     }
1719:     if (pcbddc->compute_nonetflux) {
1720:       MatNullSpace nnfnnsp;

1722:       if (!pcbddc->divudotp) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Missing divudotp operator");
1723:       PCBDDCComputeNoNetFlux(pc->pmat,pcbddc->divudotp,pcbddc->divudotp_trans,pcbddc->divudotp_vl2l,pcbddc->mat_graph,&nnfnnsp);
1724:       /* TODO what if a nearnullspace is already attached? */
1725:       if (nnfnnsp) {
1726:         MatSetNearNullSpace(pc->pmat,nnfnnsp);
1727:         MatNullSpaceDestroy(&nnfnnsp);
1728:       }
1729:     }
1730:   }
1731:   PetscLogEventEnd(PC_BDDC_Topology[pcbddc->current_level],pc,0,0,0);

1733:   /* check existence of a divergence free extension, i.e.
1734:      b(v_I,p_0) = 0 for all v_I (raise error if not).
1735:      Also, check that PCBDDCBenignGetOrSetP0 works */
1736:   if (pcbddc->benign_saddle_point && pcbddc->dbg_flag > 1) {
1737:     PCBDDCBenignCheck(pc,zerodiag);
1738:   }
1739:   ISDestroy(&zerodiag);

1741:   /* Setup local dirichlet solver ksp_D and sub_schurs solvers */
1742:   if (computesubschurs && pcbddc->recompute_topography) {
1743:     PCBDDCInitSubSchurs(pc);
1744:   }
1745:   /* SetUp Scaling operator (scaling matrices could be needed in SubSchursSetUp)*/
1746:   if (!pcbddc->use_deluxe_scaling) {
1747:     PCBDDCScalingSetUp(pc);
1748:   }

1750:   /* finish setup solvers and do adaptive selection of constraints */
1751:   sub_schurs = pcbddc->sub_schurs;
1752:   if (sub_schurs && sub_schurs->schur_explicit) {
1753:     if (computesubschurs) {
1754:       PCBDDCSetUpSubSchurs(pc);
1755:     }
1756:     PCBDDCSetUpLocalSolvers(pc,PETSC_TRUE,PETSC_FALSE);
1757:   } else {
1758:     PCBDDCSetUpLocalSolvers(pc,PETSC_TRUE,PETSC_FALSE);
1759:     if (computesubschurs) {
1760:       PCBDDCSetUpSubSchurs(pc);
1761:     }
1762:   }
1763:   if (pcbddc->adaptive_selection) {
1764:     PCBDDCAdaptiveSelection(pc);
1765:     computeconstraintsmatrix = PETSC_TRUE;
1766:   }

1768:   /* infer if NullSpace object attached to Mat via MatSetNearNullSpace has changed */
1769:   new_nearnullspace_provided = PETSC_FALSE;
1770:   MatGetNearNullSpace(pc->pmat,&nearnullspace);
1771:   if (pcbddc->onearnullspace) { /* already used nearnullspace */
1772:     if (!nearnullspace) { /* near null space attached to mat has been destroyed */
1773:       new_nearnullspace_provided = PETSC_TRUE;
1774:     } else {
1775:       /* determine if the two nullspaces are different (should be lightweight) */
1776:       if (nearnullspace != pcbddc->onearnullspace) {
1777:         new_nearnullspace_provided = PETSC_TRUE;
1778:       } else { /* maybe the user has changed the content of the nearnullspace so check vectors ObjectStateId */
1779:         PetscInt         i;
1780:         const Vec        *nearnullvecs;
1781:         PetscObjectState state;
1782:         PetscInt         nnsp_size;
1783:         MatNullSpaceGetVecs(nearnullspace,NULL,&nnsp_size,&nearnullvecs);
1784:         for (i=0;i<nnsp_size;i++) {
1785:           PetscObjectStateGet((PetscObject)nearnullvecs[i],&state);
1786:           if (pcbddc->onearnullvecs_state[i] != state) {
1787:             new_nearnullspace_provided = PETSC_TRUE;
1788:             break;
1789:           }
1790:         }
1791:       }
1792:     }
1793:   } else {
1794:     if (!nearnullspace) { /* both nearnullspaces are null */
1795:       new_nearnullspace_provided = PETSC_FALSE;
1796:     } else { /* nearnullspace attached later */
1797:       new_nearnullspace_provided = PETSC_TRUE;
1798:     }
1799:   }

1801:   /* Setup constraints and related work vectors */
1802:   /* reset primal space flags */
1803:   PetscLogEventBegin(PC_BDDC_LocalWork[pcbddc->current_level],pc,0,0,0);
1804:   pcbddc->new_primal_space = PETSC_FALSE;
1805:   pcbddc->new_primal_space_local = PETSC_FALSE;
1806:   if (computeconstraintsmatrix || new_nearnullspace_provided) {
1807:     /* It also sets the primal space flags */
1808:     PCBDDCConstraintsSetUp(pc);
1809:   }
1810:   /* Allocate needed local vectors (which depends on quantities defined during ConstraintsSetUp) */
1811:   PCBDDCSetUpLocalWorkVectors(pc);

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

1816:     PCBDDCComputeLocalMatrix(pc,pcbddc->ChangeOfBasisMatrix);
1817:     if (pcbddc->benign_change) {
1818:       MatDestroy(&pcbddc->benign_B0);
1819:       /* pop B0 from pcbddc->local_mat */
1820:       PCBDDCBenignPopOrPushB0(pc,PETSC_TRUE);
1821:     }
1822:     /* get submatrices */
1823:     MatDestroy(&pcis->A_IB);
1824:     MatDestroy(&pcis->A_BI);
1825:     MatDestroy(&pcis->A_BB);
1826:     MatCreateSubMatrix(pcbddc->local_mat,pcis->is_B_local,pcis->is_B_local,MAT_INITIAL_MATRIX,&pcis->A_BB);
1827:     MatCreateSubMatrix(pcbddc->local_mat,pcis->is_I_local,pcis->is_B_local,MAT_INITIAL_MATRIX,&pcis->A_IB);
1828:     MatCreateSubMatrix(pcbddc->local_mat,pcis->is_B_local,pcis->is_I_local,MAT_INITIAL_MATRIX,&pcis->A_BI);
1829:     /* set flag in pcis to not reuse submatrices during PCISCreate */
1830:     pcis->reusesubmatrices = PETSC_FALSE;
1831:   } else if (!pcbddc->user_ChangeOfBasisMatrix && !pcbddc->benign_change) {
1832:     MatDestroy(&pcbddc->local_mat);
1833:     PetscObjectReference((PetscObject)matis->A);
1834:     pcbddc->local_mat = matis->A;
1835:   }

1837:   /* interface pressure block row for B_C */
1838:   PetscObjectQuery((PetscObject)pc,"__KSPFETIDP_lP" ,(PetscObject*)&lP);
1839:   PetscObjectQuery((PetscObject)pc,"__KSPFETIDP_lA" ,(PetscObject*)&lA);
1840:   if (lA && lP) {
1841:     PC_IS*    pcis = (PC_IS*)pc->data;
1842:     Mat       B_BI,B_BB,Bt_BI,Bt_BB;
1843:     PetscBool issym;
1844:     MatIsSymmetric(lA,PETSC_SMALL,&issym);
1845:     if (issym) {
1846:       MatCreateSubMatrix(lA,lP,pcis->is_I_local,MAT_INITIAL_MATRIX,&B_BI);
1847:       MatCreateSubMatrix(lA,lP,pcis->is_B_local,MAT_INITIAL_MATRIX,&B_BB);
1848:       MatCreateTranspose(B_BI,&Bt_BI);
1849:       MatCreateTranspose(B_BB,&Bt_BB);
1850:     } else {
1851:       MatCreateSubMatrix(lA,lP,pcis->is_I_local,MAT_INITIAL_MATRIX,&B_BI);
1852:       MatCreateSubMatrix(lA,lP,pcis->is_B_local,MAT_INITIAL_MATRIX,&B_BB);
1853:       MatCreateSubMatrix(lA,pcis->is_I_local,lP,MAT_INITIAL_MATRIX,&Bt_BI);
1854:       MatCreateSubMatrix(lA,pcis->is_B_local,lP,MAT_INITIAL_MATRIX,&Bt_BB);
1855:     }
1856:     PetscObjectCompose((PetscObject)pc,"__KSPFETIDP_B_BI",(PetscObject)B_BI);
1857:     PetscObjectCompose((PetscObject)pc,"__KSPFETIDP_B_BB",(PetscObject)B_BB);
1858:     PetscObjectCompose((PetscObject)pc,"__KSPFETIDP_Bt_BI",(PetscObject)Bt_BI);
1859:     PetscObjectCompose((PetscObject)pc,"__KSPFETIDP_Bt_BB",(PetscObject)Bt_BB);
1860:     MatDestroy(&B_BI);
1861:     MatDestroy(&B_BB);
1862:     MatDestroy(&Bt_BI);
1863:     MatDestroy(&Bt_BB);
1864:   }
1865:   PetscLogEventEnd(PC_BDDC_LocalWork[pcbddc->current_level],pc,0,0,0);

1867:   /* SetUp coarse and local Neumann solvers */
1868:   PCBDDCSetUpSolvers(pc);
1869:   /* SetUp Scaling operator */
1870:   if (pcbddc->use_deluxe_scaling) {
1871:     PCBDDCScalingSetUp(pc);
1872:   }

1874:   /* mark topography as done */
1875:   pcbddc->recompute_topography = PETSC_FALSE;

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

1880:   if (pcbddc->dbg_flag) {
1881:     PetscViewerASCIISubtractTab(pcbddc->dbg_viewer,2*pcbddc->current_level);
1882:     PetscViewerASCIIPopSynchronized(pcbddc->dbg_viewer);
1883:   }
1884:   return(0);
1885: }

1887: /*
1888:    PCApply_BDDC - Applies the BDDC operator to a vector.

1890:    Input Parameters:
1891: +  pc - the preconditioner context
1892: -  r - input vector (global)

1894:    Output Parameter:
1895: .  z - output vector (global)

1897:    Application Interface Routine: PCApply()
1898:  */
1899: PetscErrorCode PCApply_BDDC(PC pc,Vec r,Vec z)
1900: {
1901:   PC_IS             *pcis = (PC_IS*)(pc->data);
1902:   PC_BDDC           *pcbddc = (PC_BDDC*)(pc->data);
1903:   Mat               lA = NULL;
1904:   PetscInt          n_B = pcis->n_B, n_D = pcis->n - n_B;
1905:   PetscErrorCode    ierr;
1906:   const PetscScalar one = 1.0;
1907:   const PetscScalar m_one = -1.0;
1908:   const PetscScalar zero = 0.0;
1909: /* This code is similar to that provided in nn.c for PCNN
1910:    NN interface preconditioner changed to BDDC
1911:    Added support for M_3 preconditioner in the reference article (code is active if pcbddc->switch_static == PETSC_TRUE) */

1914:   PetscCitationsRegister(citation,&cited);
1915:   if (pcbddc->switch_static) {
1916:     MatISGetLocalMat(pc->useAmat ? pc->mat : pc->pmat,&lA);
1917:   }

1919:   if (pcbddc->ChangeOfBasisMatrix) {
1920:     Vec swap;

1922:     MatMultTranspose(pcbddc->ChangeOfBasisMatrix,r,pcbddc->work_change);
1923:     swap = pcbddc->work_change;
1924:     pcbddc->work_change = r;
1925:     r = swap;
1926:     /* save rhs so that we don't need to apply the change of basis for the exact dirichlet trick in PreSolve */
1927:     if (pcbddc->benign_apply_coarse_only && pcbddc->use_exact_dirichlet_trick && pcbddc->change_interior) {
1928:       VecCopy(r,pcis->vec1_global);
1929:       VecLockReadPush(pcis->vec1_global);
1930:     }
1931:   }
1932:   if (pcbddc->benign_have_null) { /* get p0 from r */
1933:     PCBDDCBenignGetOrSetP0(pc,r,PETSC_TRUE);
1934:   }
1935:   if (pcbddc->interface_extension == PC_BDDC_INTERFACE_EXT_DIRICHLET && !pcbddc->exact_dirichlet_trick_app && !pcbddc->benign_apply_coarse_only) {
1936:     VecCopy(r,z);
1937:     /* First Dirichlet solve */
1938:     VecScatterBegin(pcis->global_to_D,r,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
1939:     VecScatterEnd(pcis->global_to_D,r,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
1940:     /*
1941:       Assembling right hand side for BDDC operator
1942:       - pcis->vec1_D for the Dirichlet part (if needed, i.e. pcbddc->switch_static == PETSC_TRUE)
1943:       - pcis->vec1_B the interface part of the global vector z
1944:     */
1945:     if (n_D) {
1946:       KSPSolve(pcbddc->ksp_D,pcis->vec1_D,pcis->vec2_D);
1947:       KSPCheckSolve(pcbddc->ksp_D,pc,pcis->vec2_D);
1948:       VecScale(pcis->vec2_D,m_one);
1949:       if (pcbddc->switch_static) {
1950:         VecSet(pcis->vec1_N,0.);
1951:         VecScatterBegin(pcis->N_to_D,pcis->vec2_D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
1952:         VecScatterEnd(pcis->N_to_D,pcis->vec2_D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
1953:         if (!pcbddc->switch_static_change) {
1954:           MatMult(lA,pcis->vec1_N,pcis->vec2_N);
1955:         } else {
1956:           MatMult(pcbddc->switch_static_change,pcis->vec1_N,pcis->vec2_N);
1957:           MatMult(lA,pcis->vec2_N,pcis->vec1_N);
1958:           MatMultTranspose(pcbddc->switch_static_change,pcis->vec1_N,pcis->vec2_N);
1959:         }
1960:         VecScatterBegin(pcis->N_to_D,pcis->vec2_N,pcis->vec1_D,ADD_VALUES,SCATTER_FORWARD);
1961:         VecScatterEnd(pcis->N_to_D,pcis->vec2_N,pcis->vec1_D,ADD_VALUES,SCATTER_FORWARD);
1962:         VecScatterBegin(pcis->N_to_B,pcis->vec2_N,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
1963:         VecScatterEnd(pcis->N_to_B,pcis->vec2_N,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
1964:       } else {
1965:         MatMult(pcis->A_BI,pcis->vec2_D,pcis->vec1_B);
1966:       }
1967:     } else {
1968:       VecSet(pcis->vec1_B,zero);
1969:     }
1970:     VecScatterBegin(pcis->global_to_B,pcis->vec1_B,z,ADD_VALUES,SCATTER_REVERSE);
1971:     VecScatterEnd(pcis->global_to_B,pcis->vec1_B,z,ADD_VALUES,SCATTER_REVERSE);
1972:     PCBDDCScalingRestriction(pc,z,pcis->vec1_B);
1973:   } else {
1974:     if (!pcbddc->benign_apply_coarse_only) {
1975:       PCBDDCScalingRestriction(pc,r,pcis->vec1_B);
1976:     }
1977:   }
1978:   if (pcbddc->interface_extension == PC_BDDC_INTERFACE_EXT_LUMP) {
1979:     if (!pcbddc->switch_static) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"You forgot to pass -pc_bddc_switch_static");
1980:     VecScatterBegin(pcis->global_to_D,r,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
1981:     VecScatterEnd(pcis->global_to_D,r,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
1982:   }

1984:   /* Apply interface preconditioner
1985:      input/output vecs: pcis->vec1_B and pcis->vec1_D */
1986:   PCBDDCApplyInterfacePreconditioner(pc,PETSC_FALSE);

1988:   /* Apply transpose of partition of unity operator */
1989:   PCBDDCScalingExtension(pc,pcis->vec1_B,z);
1990:   if (pcbddc->interface_extension == PC_BDDC_INTERFACE_EXT_LUMP) {
1991:     VecScatterBegin(pcis->global_to_D,pcis->vec1_D,z,INSERT_VALUES,SCATTER_REVERSE);
1992:     VecScatterEnd(pcis->global_to_D,pcis->vec1_D,z,INSERT_VALUES,SCATTER_REVERSE);
1993:     return(0);
1994:   }
1995:   /* Second Dirichlet solve and assembling of output */
1996:   VecScatterBegin(pcis->global_to_B,z,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
1997:   VecScatterEnd(pcis->global_to_B,z,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
1998:   if (n_B) {
1999:     if (pcbddc->switch_static) {
2000:       VecScatterBegin(pcis->N_to_D,pcis->vec1_D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
2001:       VecScatterEnd(pcis->N_to_D,pcis->vec1_D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
2002:       VecScatterBegin(pcis->N_to_B,pcis->vec1_B,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
2003:       VecScatterEnd(pcis->N_to_B,pcis->vec1_B,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
2004:       if (!pcbddc->switch_static_change) {
2005:         MatMult(lA,pcis->vec1_N,pcis->vec2_N);
2006:       } else {
2007:         MatMult(pcbddc->switch_static_change,pcis->vec1_N,pcis->vec2_N);
2008:         MatMult(lA,pcis->vec2_N,pcis->vec1_N);
2009:         MatMultTranspose(pcbddc->switch_static_change,pcis->vec1_N,pcis->vec2_N);
2010:       }
2011:       VecScatterBegin(pcis->N_to_D,pcis->vec2_N,pcis->vec3_D,INSERT_VALUES,SCATTER_FORWARD);
2012:       VecScatterEnd(pcis->N_to_D,pcis->vec2_N,pcis->vec3_D,INSERT_VALUES,SCATTER_FORWARD);
2013:     } else {
2014:       MatMult(pcis->A_IB,pcis->vec1_B,pcis->vec3_D);
2015:     }
2016:   } else if (pcbddc->switch_static) { /* n_B is zero */
2017:     if (!pcbddc->switch_static_change) {
2018:       MatMult(lA,pcis->vec1_D,pcis->vec3_D);
2019:     } else {
2020:       MatMult(pcbddc->switch_static_change,pcis->vec1_D,pcis->vec1_N);
2021:       MatMult(lA,pcis->vec1_N,pcis->vec2_N);
2022:       MatMultTranspose(pcbddc->switch_static_change,pcis->vec2_N,pcis->vec3_D);
2023:     }
2024:   }
2025:   KSPSolve(pcbddc->ksp_D,pcis->vec3_D,pcis->vec4_D);
2026:   KSPCheckSolve(pcbddc->ksp_D,pc,pcis->vec4_D);

2028:   if (!pcbddc->exact_dirichlet_trick_app && !pcbddc->benign_apply_coarse_only) {
2029:     if (pcbddc->switch_static) {
2030:       VecAXPBYPCZ(pcis->vec2_D,m_one,one,m_one,pcis->vec4_D,pcis->vec1_D);
2031:     } else {
2032:       VecAXPBY(pcis->vec2_D,m_one,m_one,pcis->vec4_D);
2033:     }
2034:     VecScatterBegin(pcis->global_to_D,pcis->vec2_D,z,INSERT_VALUES,SCATTER_REVERSE);
2035:     VecScatterEnd(pcis->global_to_D,pcis->vec2_D,z,INSERT_VALUES,SCATTER_REVERSE);
2036:   } else {
2037:     if (pcbddc->switch_static) {
2038:       VecAXPBY(pcis->vec4_D,one,m_one,pcis->vec1_D);
2039:     } else {
2040:       VecScale(pcis->vec4_D,m_one);
2041:     }
2042:     VecScatterBegin(pcis->global_to_D,pcis->vec4_D,z,INSERT_VALUES,SCATTER_REVERSE);
2043:     VecScatterEnd(pcis->global_to_D,pcis->vec4_D,z,INSERT_VALUES,SCATTER_REVERSE);
2044:   }
2045:   if (pcbddc->benign_have_null) { /* set p0 (computed in PCBDDCApplyInterface) */
2046:     if (pcbddc->benign_apply_coarse_only) {
2047:       PetscArrayzero(pcbddc->benign_p0,pcbddc->benign_n);
2048:     }
2049:     PCBDDCBenignGetOrSetP0(pc,z,PETSC_FALSE);
2050:   }

2052:   if (pcbddc->ChangeOfBasisMatrix) {
2053:     pcbddc->work_change = r;
2054:     VecCopy(z,pcbddc->work_change);
2055:     MatMult(pcbddc->ChangeOfBasisMatrix,pcbddc->work_change,z);
2056:   }
2057:   return(0);
2058: }

2060: /*
2061:    PCApplyTranspose_BDDC - Applies the transpose of the BDDC operator to a vector.

2063:    Input Parameters:
2064: +  pc - the preconditioner context
2065: -  r - input vector (global)

2067:    Output Parameter:
2068: .  z - output vector (global)

2070:    Application Interface Routine: PCApplyTranspose()
2071:  */
2072: PetscErrorCode PCApplyTranspose_BDDC(PC pc,Vec r,Vec z)
2073: {
2074:   PC_IS             *pcis = (PC_IS*)(pc->data);
2075:   PC_BDDC           *pcbddc = (PC_BDDC*)(pc->data);
2076:   Mat               lA = NULL;
2077:   PetscInt          n_B = pcis->n_B, n_D = pcis->n - n_B;
2078:   PetscErrorCode    ierr;
2079:   const PetscScalar one = 1.0;
2080:   const PetscScalar m_one = -1.0;
2081:   const PetscScalar zero = 0.0;

2084:   PetscCitationsRegister(citation,&cited);
2085:   if (pcbddc->switch_static) {
2086:     MatISGetLocalMat(pc->useAmat ? pc->mat : pc->pmat,&lA);
2087:   }
2088:   if (pcbddc->ChangeOfBasisMatrix) {
2089:     Vec swap;

2091:     MatMultTranspose(pcbddc->ChangeOfBasisMatrix,r,pcbddc->work_change);
2092:     swap = pcbddc->work_change;
2093:     pcbddc->work_change = r;
2094:     r = swap;
2095:     /* save rhs so that we don't need to apply the change of basis for the exact dirichlet trick in PreSolve */
2096:     if (pcbddc->benign_apply_coarse_only && pcbddc->exact_dirichlet_trick_app && pcbddc->change_interior) {
2097:       VecCopy(r,pcis->vec1_global);
2098:       VecLockReadPush(pcis->vec1_global);
2099:     }
2100:   }
2101:   if (pcbddc->benign_have_null) { /* get p0 from r */
2102:     PCBDDCBenignGetOrSetP0(pc,r,PETSC_TRUE);
2103:   }
2104:   if (!pcbddc->exact_dirichlet_trick_app && !pcbddc->benign_apply_coarse_only) {
2105:     VecCopy(r,z);
2106:     /* First Dirichlet solve */
2107:     VecScatterBegin(pcis->global_to_D,r,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
2108:     VecScatterEnd(pcis->global_to_D,r,pcis->vec1_D,INSERT_VALUES,SCATTER_FORWARD);
2109:     /*
2110:       Assembling right hand side for BDDC operator
2111:       - pcis->vec1_D for the Dirichlet part (if needed, i.e. pcbddc->switch_static == PETSC_TRUE)
2112:       - pcis->vec1_B the interface part of the global vector z
2113:     */
2114:     if (n_D) {
2115:       KSPSolveTranspose(pcbddc->ksp_D,pcis->vec1_D,pcis->vec2_D);
2116:       KSPCheckSolve(pcbddc->ksp_D,pc,pcis->vec2_D);
2117:       VecScale(pcis->vec2_D,m_one);
2118:       if (pcbddc->switch_static) {
2119:         VecSet(pcis->vec1_N,0.);
2120:         VecScatterBegin(pcis->N_to_D,pcis->vec2_D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
2121:         VecScatterEnd(pcis->N_to_D,pcis->vec2_D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
2122:         if (!pcbddc->switch_static_change) {
2123:           MatMultTranspose(lA,pcis->vec1_N,pcis->vec2_N);
2124:         } else {
2125:           MatMult(pcbddc->switch_static_change,pcis->vec1_N,pcis->vec2_N);
2126:           MatMultTranspose(lA,pcis->vec2_N,pcis->vec1_N);
2127:           MatMultTranspose(pcbddc->switch_static_change,pcis->vec1_N,pcis->vec2_N);
2128:         }
2129:         VecScatterBegin(pcis->N_to_D,pcis->vec2_N,pcis->vec1_D,ADD_VALUES,SCATTER_FORWARD);
2130:         VecScatterEnd(pcis->N_to_D,pcis->vec2_N,pcis->vec1_D,ADD_VALUES,SCATTER_FORWARD);
2131:         VecScatterBegin(pcis->N_to_B,pcis->vec2_N,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
2132:         VecScatterEnd(pcis->N_to_B,pcis->vec2_N,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
2133:       } else {
2134:         MatMultTranspose(pcis->A_IB,pcis->vec2_D,pcis->vec1_B);
2135:       }
2136:     } else {
2137:       VecSet(pcis->vec1_B,zero);
2138:     }
2139:     VecScatterBegin(pcis->global_to_B,pcis->vec1_B,z,ADD_VALUES,SCATTER_REVERSE);
2140:     VecScatterEnd(pcis->global_to_B,pcis->vec1_B,z,ADD_VALUES,SCATTER_REVERSE);
2141:     PCBDDCScalingRestriction(pc,z,pcis->vec1_B);
2142:   } else {
2143:     PCBDDCScalingRestriction(pc,r,pcis->vec1_B);
2144:   }

2146:   /* Apply interface preconditioner
2147:      input/output vecs: pcis->vec1_B and pcis->vec1_D */
2148:   PCBDDCApplyInterfacePreconditioner(pc,PETSC_TRUE);

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

2153:   /* Second Dirichlet solve and assembling of output */
2154:   VecScatterBegin(pcis->global_to_B,z,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
2155:   VecScatterEnd(pcis->global_to_B,z,pcis->vec1_B,INSERT_VALUES,SCATTER_FORWARD);
2156:   if (n_B) {
2157:     if (pcbddc->switch_static) {
2158:       VecScatterBegin(pcis->N_to_D,pcis->vec1_D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
2159:       VecScatterEnd(pcis->N_to_D,pcis->vec1_D,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
2160:       VecScatterBegin(pcis->N_to_B,pcis->vec1_B,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
2161:       VecScatterEnd(pcis->N_to_B,pcis->vec1_B,pcis->vec1_N,INSERT_VALUES,SCATTER_REVERSE);
2162:       if (!pcbddc->switch_static_change) {
2163:         MatMultTranspose(lA,pcis->vec1_N,pcis->vec2_N);
2164:       } else {
2165:         MatMult(pcbddc->switch_static_change,pcis->vec1_N,pcis->vec2_N);
2166:         MatMultTranspose(lA,pcis->vec2_N,pcis->vec1_N);
2167:         MatMultTranspose(pcbddc->switch_static_change,pcis->vec1_N,pcis->vec2_N);
2168:       }
2169:       VecScatterBegin(pcis->N_to_D,pcis->vec2_N,pcis->vec3_D,INSERT_VALUES,SCATTER_FORWARD);
2170:       VecScatterEnd(pcis->N_to_D,pcis->vec2_N,pcis->vec3_D,INSERT_VALUES,SCATTER_FORWARD);
2171:     } else {
2172:       MatMultTranspose(pcis->A_BI,pcis->vec1_B,pcis->vec3_D);
2173:     }
2174:   } else if (pcbddc->switch_static) { /* n_B is zero */
2175:     if (!pcbddc->switch_static_change) {
2176:       MatMultTranspose(lA,pcis->vec1_D,pcis->vec3_D);
2177:     } else {
2178:       MatMult(pcbddc->switch_static_change,pcis->vec1_D,pcis->vec1_N);
2179:       MatMultTranspose(lA,pcis->vec1_N,pcis->vec2_N);
2180:       MatMultTranspose(pcbddc->switch_static_change,pcis->vec2_N,pcis->vec3_D);
2181:     }
2182:   }
2183:   KSPSolveTranspose(pcbddc->ksp_D,pcis->vec3_D,pcis->vec4_D);
2184:   KSPCheckSolve(pcbddc->ksp_D,pc,pcis->vec4_D);
2185:   if (!pcbddc->exact_dirichlet_trick_app && !pcbddc->benign_apply_coarse_only) {
2186:     if (pcbddc->switch_static) {
2187:       VecAXPBYPCZ(pcis->vec2_D,m_one,one,m_one,pcis->vec4_D,pcis->vec1_D);
2188:     } else {
2189:       VecAXPBY(pcis->vec2_D,m_one,m_one,pcis->vec4_D);
2190:     }
2191:     VecScatterBegin(pcis->global_to_D,pcis->vec2_D,z,INSERT_VALUES,SCATTER_REVERSE);
2192:     VecScatterEnd(pcis->global_to_D,pcis->vec2_D,z,INSERT_VALUES,SCATTER_REVERSE);
2193:   } else {
2194:     if (pcbddc->switch_static) {
2195:       VecAXPBY(pcis->vec4_D,one,m_one,pcis->vec1_D);
2196:     } else {
2197:       VecScale(pcis->vec4_D,m_one);
2198:     }
2199:     VecScatterBegin(pcis->global_to_D,pcis->vec4_D,z,INSERT_VALUES,SCATTER_REVERSE);
2200:     VecScatterEnd(pcis->global_to_D,pcis->vec4_D,z,INSERT_VALUES,SCATTER_REVERSE);
2201:   }
2202:   if (pcbddc->benign_have_null) { /* set p0 (computed in PCBDDCApplyInterface) */
2203:     PCBDDCBenignGetOrSetP0(pc,z,PETSC_FALSE);
2204:   }
2205:   if (pcbddc->ChangeOfBasisMatrix) {
2206:     pcbddc->work_change = r;
2207:     VecCopy(z,pcbddc->work_change);
2208:     MatMult(pcbddc->ChangeOfBasisMatrix,pcbddc->work_change,z);
2209:   }
2210:   return(0);
2211: }

2213: PetscErrorCode PCReset_BDDC(PC pc)
2214: {
2215:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
2216:   PC_IS          *pcis = (PC_IS*)pc->data;
2217:   KSP            kspD,kspR,kspC;

2221:   /* free BDDC custom data  */
2222:   PCBDDCResetCustomization(pc);
2223:   /* destroy objects related to topography */
2224:   PCBDDCResetTopography(pc);
2225:   /* destroy objects for scaling operator */
2226:   PCBDDCScalingDestroy(pc);
2227:   /* free solvers stuff */
2228:   PCBDDCResetSolvers(pc);
2229:   /* free global vectors needed in presolve */
2230:   VecDestroy(&pcbddc->temp_solution);
2231:   VecDestroy(&pcbddc->original_rhs);
2232:   /* free data created by PCIS */
2233:   PCISDestroy(pc);

2235:   /* restore defaults */
2236:   kspD = pcbddc->ksp_D;
2237:   kspR = pcbddc->ksp_R;
2238:   kspC = pcbddc->coarse_ksp;
2239:   PetscMemzero(pc->data,sizeof(*pcbddc));
2240:   pcis->n_neigh                     = -1;
2241:   pcis->scaling_factor              = 1.0;
2242:   pcis->reusesubmatrices            = PETSC_TRUE;
2243:   pcbddc->use_local_adj             = PETSC_TRUE;
2244:   pcbddc->use_vertices              = PETSC_TRUE;
2245:   pcbddc->use_edges                 = PETSC_TRUE;
2246:   pcbddc->symmetric_primal          = PETSC_TRUE;
2247:   pcbddc->vertex_size               = 1;
2248:   pcbddc->recompute_topography      = PETSC_TRUE;
2249:   pcbddc->coarse_size               = -1;
2250:   pcbddc->use_exact_dirichlet_trick = PETSC_TRUE;
2251:   pcbddc->coarsening_ratio          = 8;
2252:   pcbddc->coarse_eqs_per_proc       = 1;
2253:   pcbddc->benign_compute_correction = PETSC_TRUE;
2254:   pcbddc->nedfield                  = -1;
2255:   pcbddc->nedglobal                 = PETSC_TRUE;
2256:   pcbddc->graphmaxcount             = PETSC_MAX_INT;
2257:   pcbddc->sub_schurs_layers         = -1;
2258:   pcbddc->ksp_D                     = kspD;
2259:   pcbddc->ksp_R                     = kspR;
2260:   pcbddc->coarse_ksp                = kspC;
2261:   return(0);
2262: }

2264: PetscErrorCode PCDestroy_BDDC(PC pc)
2265: {
2266:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;

2270:   PCReset_BDDC(pc);
2271:   KSPDestroy(&pcbddc->ksp_D);
2272:   KSPDestroy(&pcbddc->ksp_R);
2273:   KSPDestroy(&pcbddc->coarse_ksp);
2274:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDiscreteGradient_C",NULL);
2275:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDivergenceMat_C",NULL);
2276:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetChangeOfBasisMat_C",NULL);
2277:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetPrimalVerticesLocalIS_C",NULL);
2278:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetPrimalVerticesIS_C",NULL);
2279:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetCoarseningRatio_C",NULL);
2280:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetLevel_C",NULL);
2281:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetUseExactDirichlet_C",NULL);
2282:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetLevels_C",NULL);
2283:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDirichletBoundaries_C",NULL);
2284:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDirichletBoundariesLocal_C",NULL);
2285:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetNeumannBoundaries_C",NULL);
2286:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetNeumannBoundariesLocal_C",NULL);
2287:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetDirichletBoundaries_C",NULL);
2288:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetDirichletBoundariesLocal_C",NULL);
2289:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetNeumannBoundaries_C",NULL);
2290:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetNeumannBoundariesLocal_C",NULL);
2291:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDofsSplitting_C",NULL);
2292:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDofsSplittingLocal_C",NULL);
2293:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetLocalAdjacencyGraph_C",NULL);
2294:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCCreateFETIDPOperators_C",NULL);
2295:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCMatFETIDPGetRHS_C",NULL);
2296:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCMatFETIDPGetSolution_C",NULL);
2297:   PetscObjectComposeFunction((PetscObject)pc,"PCPreSolveChangeRHS_C",NULL);
2298:   PetscObjectComposeFunction((PetscObject)pc,"PCSetCoordinates_C",NULL);
2299:   PetscFree(pc->data);
2300:   return(0);
2301: }

2303: static PetscErrorCode PCSetCoordinates_BDDC(PC pc, PetscInt dim, PetscInt nloc, PetscReal *coords)
2304: {
2305:   PC_BDDC        *pcbddc = (PC_BDDC*)pc->data;
2306:   PCBDDCGraph    mat_graph = pcbddc->mat_graph;

2310:   PetscFree(mat_graph->coords);
2311:   PetscMalloc1(nloc*dim,&mat_graph->coords);
2312:   PetscArraycpy(mat_graph->coords,coords,nloc*dim);
2313:   mat_graph->cnloc = nloc;
2314:   mat_graph->cdim  = dim;
2315:   mat_graph->cloc  = PETSC_FALSE;
2316:   /* flg setup */
2317:   pcbddc->recompute_topography = PETSC_TRUE;
2318:   pcbddc->corner_selected = PETSC_FALSE;
2319:   return(0);
2320: }

2322: static PetscErrorCode PCPreSolveChangeRHS_BDDC(PC pc, PetscBool* change)
2323: {
2325:   *change = PETSC_TRUE;
2326:   return(0);
2327: }

2329: static PetscErrorCode PCBDDCMatFETIDPGetRHS_BDDC(Mat fetidp_mat, Vec standard_rhs, Vec fetidp_flux_rhs)
2330: {
2331:   FETIDPMat_ctx  mat_ctx;
2332:   Vec            work;
2333:   PC_IS*         pcis;
2334:   PC_BDDC*       pcbddc;

2338:   MatShellGetContext(fetidp_mat,&mat_ctx);
2339:   pcis = (PC_IS*)mat_ctx->pc->data;
2340:   pcbddc = (PC_BDDC*)mat_ctx->pc->data;

2342:   VecSet(fetidp_flux_rhs,0.0);
2343:   /* copy rhs since we may change it during PCPreSolve_BDDC */
2344:   if (!pcbddc->original_rhs) {
2345:     VecDuplicate(pcis->vec1_global,&pcbddc->original_rhs);
2346:   }
2347:   if (mat_ctx->rhs_flip) {
2348:     VecPointwiseMult(pcbddc->original_rhs,standard_rhs,mat_ctx->rhs_flip);
2349:   } else {
2350:     VecCopy(standard_rhs,pcbddc->original_rhs);
2351:   }
2352:   if (mat_ctx->g2g_p) {
2353:     /* interface pressure rhs */
2354:     VecScatterBegin(mat_ctx->g2g_p,fetidp_flux_rhs,pcbddc->original_rhs,INSERT_VALUES,SCATTER_REVERSE);
2355:     VecScatterEnd(mat_ctx->g2g_p,fetidp_flux_rhs,pcbddc->original_rhs,INSERT_VALUES,SCATTER_REVERSE);
2356:     VecScatterBegin(mat_ctx->g2g_p,standard_rhs,fetidp_flux_rhs,INSERT_VALUES,SCATTER_FORWARD);
2357:     VecScatterEnd(mat_ctx->g2g_p,standard_rhs,fetidp_flux_rhs,INSERT_VALUES,SCATTER_FORWARD);
2358:     if (!mat_ctx->rhs_flip) {
2359:       VecScale(fetidp_flux_rhs,-1.);
2360:     }
2361:   }
2362:   /*
2363:      change of basis for physical rhs if needed
2364:      It also changes the rhs in case of dirichlet boundaries
2365:   */
2366:   PCPreSolve_BDDC(mat_ctx->pc,NULL,pcbddc->original_rhs,NULL);
2367:   if (pcbddc->ChangeOfBasisMatrix) {
2368:     MatMultTranspose(pcbddc->ChangeOfBasisMatrix,pcbddc->original_rhs,pcbddc->work_change);
2369:     work = pcbddc->work_change;
2370:    } else {
2371:     work = pcbddc->original_rhs;
2372:   }
2373:   /* store vectors for computation of fetidp final solution */
2374:   VecScatterBegin(pcis->global_to_D,work,mat_ctx->temp_solution_D,INSERT_VALUES,SCATTER_FORWARD);
2375:   VecScatterEnd(pcis->global_to_D,work,mat_ctx->temp_solution_D,INSERT_VALUES,SCATTER_FORWARD);
2376:   /* scale rhs since it should be unassembled */
2377:   /* TODO use counter scaling? (also below) */
2378:   VecScatterBegin(pcis->global_to_B,work,mat_ctx->temp_solution_B,INSERT_VALUES,SCATTER_FORWARD);
2379:   VecScatterEnd(pcis->global_to_B,work,mat_ctx->temp_solution_B,INSERT_VALUES,SCATTER_FORWARD);
2380:   /* Apply partition of unity */
2381:   VecPointwiseMult(mat_ctx->temp_solution_B,pcis->D,mat_ctx->temp_solution_B);
2382:   /* PCBDDCScalingRestriction(mat_ctx->pc,work,mat_ctx->temp_solution_B); */
2383:   if (!pcbddc->switch_static) {
2384:     /* compute partially subassembled Schur complement right-hand side */
2385:     KSPSolve(pcbddc->ksp_D,mat_ctx->temp_solution_D,pcis->vec1_D);
2386:     /* Cannot propagate up error in KSPSolve() because there is no access to the PC */
2387:     MatMult(pcis->A_BI,pcis->vec1_D,pcis->vec1_B);
2388:     VecAXPY(mat_ctx->temp_solution_B,-1.0,pcis->vec1_B);
2389:     VecSet(work,0.0);
2390:     VecScatterBegin(pcis->global_to_B,mat_ctx->temp_solution_B,work,ADD_VALUES,SCATTER_REVERSE);
2391:     VecScatterEnd(pcis->global_to_B,mat_ctx->temp_solution_B,work,ADD_VALUES,SCATTER_REVERSE);
2392:     /* PCBDDCScalingRestriction(mat_ctx->pc,work,mat_ctx->temp_solution_B); */
2393:     VecScatterBegin(pcis->global_to_B,work,mat_ctx->temp_solution_B,INSERT_VALUES,SCATTER_FORWARD);
2394:     VecScatterEnd(pcis->global_to_B,work,mat_ctx->temp_solution_B,INSERT_VALUES,SCATTER_FORWARD);
2395:     VecPointwiseMult(mat_ctx->temp_solution_B,pcis->D,mat_ctx->temp_solution_B);
2396:   }
2397:   /* BDDC rhs */
2398:   VecCopy(mat_ctx->temp_solution_B,pcis->vec1_B);
2399:   if (pcbddc->switch_static) {
2400:     VecCopy(mat_ctx->temp_solution_D,pcis->vec1_D);
2401:   }
2402:   /* apply BDDC */
2403:   PetscArrayzero(pcbddc->benign_p0,pcbddc->benign_n);
2404:   PCBDDCApplyInterfacePreconditioner(mat_ctx->pc,PETSC_FALSE);
2405:   PetscArrayzero(pcbddc->benign_p0,pcbddc->benign_n);

2407:   /* Application of B_delta and assembling of rhs for fetidp fluxes */
2408:   MatMult(mat_ctx->B_delta,pcis->vec1_B,mat_ctx->lambda_local);
2409:   VecScatterBegin(mat_ctx->l2g_lambda,mat_ctx->lambda_local,fetidp_flux_rhs,ADD_VALUES,SCATTER_FORWARD);
2410:   VecScatterEnd(mat_ctx->l2g_lambda,mat_ctx->lambda_local,fetidp_flux_rhs,ADD_VALUES,SCATTER_FORWARD);
2411:   /* Add contribution to interface pressures */
2412:   if (mat_ctx->l2g_p) {
2413:     MatMult(mat_ctx->B_BB,pcis->vec1_B,mat_ctx->vP);
2414:     if (pcbddc->switch_static) {
2415:       MatMultAdd(mat_ctx->B_BI,pcis->vec1_D,mat_ctx->vP,mat_ctx->vP);
2416:     }
2417:     VecScatterBegin(mat_ctx->l2g_p,mat_ctx->vP,fetidp_flux_rhs,ADD_VALUES,SCATTER_FORWARD);
2418:     VecScatterEnd(mat_ctx->l2g_p,mat_ctx->vP,fetidp_flux_rhs,ADD_VALUES,SCATTER_FORWARD);
2419:   }
2420:   return(0);
2421: }

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

2426:    Collective

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

2432:    Output Parameters:
2433: .  fetidp_flux_rhs - the right-hand side for the FETI-DP linear system

2435:    Level: developer

2437:    Notes:

2439: .seealso: PCBDDC, PCBDDCCreateFETIDPOperators, PCBDDCMatFETIDPGetSolution
2440: @*/
2441: PetscErrorCode PCBDDCMatFETIDPGetRHS(Mat fetidp_mat, Vec standard_rhs, Vec fetidp_flux_rhs)
2442: {
2443:   FETIDPMat_ctx  mat_ctx;

2450:   MatShellGetContext(fetidp_mat,&mat_ctx);
2451:   PetscUseMethod(mat_ctx->pc,"PCBDDCMatFETIDPGetRHS_C",(Mat,Vec,Vec),(fetidp_mat,standard_rhs,fetidp_flux_rhs));
2452:   return(0);
2453: }

2455: static PetscErrorCode PCBDDCMatFETIDPGetSolution_BDDC(Mat fetidp_mat, Vec fetidp_flux_sol, Vec standard_sol)
2456: {
2457:   FETIDPMat_ctx  mat_ctx;
2458:   PC_IS*         pcis;
2459:   PC_BDDC*       pcbddc;
2461:   Vec            work;

2464:   MatShellGetContext(fetidp_mat,&mat_ctx);
2465:   pcis = (PC_IS*)mat_ctx->pc->data;
2466:   pcbddc = (PC_BDDC*)mat_ctx->pc->data;

2468:   /* apply B_delta^T */
2469:   VecSet(pcis->vec1_B,0.);
2470:   VecScatterBegin(mat_ctx->l2g_lambda,fetidp_flux_sol,mat_ctx->lambda_local,INSERT_VALUES,SCATTER_REVERSE);
2471:   VecScatterEnd(mat_ctx->l2g_lambda,fetidp_flux_sol,mat_ctx->lambda_local,INSERT_VALUES,SCATTER_REVERSE);
2472:   MatMultTranspose(mat_ctx->B_delta,mat_ctx->lambda_local,pcis->vec1_B);
2473:   if (mat_ctx->l2g_p) {
2474:     VecScatterBegin(mat_ctx->l2g_p,fetidp_flux_sol,mat_ctx->vP,INSERT_VALUES,SCATTER_REVERSE);
2475:     VecScatterEnd(mat_ctx->l2g_p,fetidp_flux_sol,mat_ctx->vP,INSERT_VALUES,SCATTER_REVERSE);
2476:     MatMultAdd(mat_ctx->Bt_BB,mat_ctx->vP,pcis->vec1_B,pcis->vec1_B);
2477:   }

2479:   /* compute rhs for BDDC application */
2480:   VecAYPX(pcis->vec1_B,-1.0,mat_ctx->temp_solution_B);
2481:   if (pcbddc->switch_static) {
2482:     VecCopy(mat_ctx->temp_solution_D,pcis->vec1_D);
2483:     if (mat_ctx->l2g_p) {
2484:       VecScale(mat_ctx->vP,-1.);
2485:       MatMultAdd(mat_ctx->Bt_BI,mat_ctx->vP,pcis->vec1_D,pcis->vec1_D);
2486:     }
2487:   }

2489:   /* apply BDDC */
2490:   PetscArrayzero(pcbddc->benign_p0,pcbddc->benign_n);
2491:   PCBDDCApplyInterfacePreconditioner(mat_ctx->pc,PETSC_FALSE);

2493:   /* put values into global vector */
2494:   if (pcbddc->ChangeOfBasisMatrix) work = pcbddc->work_change;
2495:   else work = standard_sol;
2496:   VecScatterBegin(pcis->global_to_B,pcis->vec1_B,work,INSERT_VALUES,SCATTER_REVERSE);
2497:   VecScatterEnd(pcis->global_to_B,pcis->vec1_B,work,INSERT_VALUES,SCATTER_REVERSE);
2498:   if (!pcbddc->switch_static) {
2499:     /* compute values into the interior if solved for the partially subassembled Schur complement */
2500:     MatMult(pcis->A_IB,pcis->vec1_B,pcis->vec1_D);
2501:     VecAYPX(pcis->vec1_D,-1.0,mat_ctx->temp_solution_D);
2502:     KSPSolve(pcbddc->ksp_D,pcis->vec1_D,pcis->vec1_D);
2503:     /* Cannot propagate up error in KSPSolve() because there is no access to the PC */
2504:   }

2506:   VecScatterBegin(pcis->global_to_D,pcis->vec1_D,work,INSERT_VALUES,SCATTER_REVERSE);
2507:   VecScatterEnd(pcis->global_to_D,pcis->vec1_D,work,INSERT_VALUES,SCATTER_REVERSE);
2508:   /* add p0 solution to final solution */
2509:   PCBDDCBenignGetOrSetP0(mat_ctx->pc,work,PETSC_FALSE);
2510:   if (pcbddc->ChangeOfBasisMatrix) {
2511:     MatMult(pcbddc->ChangeOfBasisMatrix,work,standard_sol);
2512:   }
2513:   PCPostSolve_BDDC(mat_ctx->pc,NULL,NULL,standard_sol);
2514:   if (mat_ctx->g2g_p) {
2515:     VecScatterBegin(mat_ctx->g2g_p,fetidp_flux_sol,standard_sol,INSERT_VALUES,SCATTER_REVERSE);
2516:     VecScatterEnd(mat_ctx->g2g_p,fetidp_flux_sol,standard_sol,INSERT_VALUES,SCATTER_REVERSE);
2517:   }
2518:   return(0);
2519: }

2521: static PetscErrorCode PCView_BDDCIPC(PC pc, PetscViewer viewer)
2522: {
2524:   BDDCIPC_ctx    bddcipc_ctx;
2525:   PetscBool      isascii;

2528:   PCShellGetContext(pc,(void **)&bddcipc_ctx);
2529:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
2530:   if (isascii) {
2531:     PetscViewerASCIIPrintf(viewer,"BDDC interface preconditioner\n");
2532:   }
2533:   PetscViewerASCIIPushTab(viewer);
2534:   PCView(bddcipc_ctx->bddc,viewer);
2535:   PetscViewerASCIIPopTab(viewer);
2536:   return(0);
2537: }

2539: static PetscErrorCode PCSetUp_BDDCIPC(PC pc)
2540: {
2542:   BDDCIPC_ctx    bddcipc_ctx;
2543:   PetscBool      isbddc;
2544:   Vec            vv;
2545:   IS             is;
2546:   PC_IS          *pcis;

2549:   PCShellGetContext(pc,(void **)&bddcipc_ctx);
2550:   PetscObjectTypeCompare((PetscObject)bddcipc_ctx->bddc,PCBDDC,&isbddc);
2551:   if (!isbddc) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Invalid type %s. Must be of type bddc",((PetscObject)bddcipc_ctx->bddc)->type_name);
2552:   PCSetUp(bddcipc_ctx->bddc);

2554:   /* create interface scatter */
2555:   pcis = (PC_IS*)(bddcipc_ctx->bddc->data);
2556:   VecScatterDestroy(&bddcipc_ctx->g2l);
2557:   MatCreateVecs(pc->pmat,&vv,NULL);
2558:   ISRenumber(pcis->is_B_global,NULL,NULL,&is);
2559:   VecScatterCreate(vv,is,pcis->vec1_B,NULL,&bddcipc_ctx->g2l);
2560:   ISDestroy(&is);
2561:   VecDestroy(&vv);
2562:   return(0);
2563: }

2565: static PetscErrorCode PCApply_BDDCIPC(PC pc, Vec r, Vec x)
2566: {
2568:   BDDCIPC_ctx    bddcipc_ctx;
2569:   PC_IS          *pcis;
2570:   VecScatter     tmps;

2573:   PCShellGetContext(pc,(void **)&bddcipc_ctx);
2574:   pcis = (PC_IS*)(bddcipc_ctx->bddc->data);
2575:   tmps = pcis->global_to_B;
2576:   pcis->global_to_B = bddcipc_ctx->g2l;
2577:   PCBDDCScalingRestriction(bddcipc_ctx->bddc,r,pcis->vec1_B);
2578:   PCBDDCApplyInterfacePreconditioner(bddcipc_ctx->bddc,PETSC_FALSE);
2579:   PCBDDCScalingExtension(bddcipc_ctx->bddc,pcis->vec1_B,x);
2580:   pcis->global_to_B = tmps;
2581:   return(0);
2582: }

2584: static PetscErrorCode PCApplyTranspose_BDDCIPC(PC pc, Vec r, Vec x)
2585: {
2587:   BDDCIPC_ctx    bddcipc_ctx;
2588:   PC_IS          *pcis;
2589:   VecScatter     tmps;

2592:   PCShellGetContext(pc,(void **)&bddcipc_ctx);
2593:   pcis = (PC_IS*)(bddcipc_ctx->bddc->data);
2594:   tmps = pcis->global_to_B;
2595:   pcis->global_to_B = bddcipc_ctx->g2l;
2596:   PCBDDCScalingRestriction(bddcipc_ctx->bddc,r,pcis->vec1_B);
2597:   PCBDDCApplyInterfacePreconditioner(bddcipc_ctx->bddc,PETSC_TRUE);
2598:   PCBDDCScalingExtension(bddcipc_ctx->bddc,pcis->vec1_B,x);
2599:   pcis->global_to_B = tmps;
2600:   return(0);
2601: }

2603: static PetscErrorCode PCDestroy_BDDCIPC(PC pc)
2604: {
2606:   BDDCIPC_ctx    bddcipc_ctx;

2609:   PCShellGetContext(pc,(void **)&bddcipc_ctx);
2610:   PCDestroy(&bddcipc_ctx->bddc);
2611:   VecScatterDestroy(&bddcipc_ctx->g2l);
2612:   PetscFree(bddcipc_ctx);
2613:   return(0);
2614: }

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

2619:    Collective

2621:    Input Parameters:
2622: +  fetidp_mat      - the FETI-DP matrix obtained by a call to PCBDDCCreateFETIDPOperators
2623: -  fetidp_flux_sol - the solution of the FETI-DP linear system

2625:    Output Parameters:
2626: .  standard_sol    - the solution defined on the physical domain

2628:    Level: developer

2630:    Notes:

2632: .seealso: PCBDDC, PCBDDCCreateFETIDPOperators, PCBDDCMatFETIDPGetRHS
2633: @*/
2634: PetscErrorCode PCBDDCMatFETIDPGetSolution(Mat fetidp_mat, Vec fetidp_flux_sol, Vec standard_sol)
2635: {
2636:   FETIDPMat_ctx  mat_ctx;

2643:   MatShellGetContext(fetidp_mat,&mat_ctx);
2644:   PetscUseMethod(mat_ctx->pc,"PCBDDCMatFETIDPGetSolution_C",(Mat,Vec,Vec),(fetidp_mat,fetidp_flux_sol,standard_sol));
2645:   return(0);
2646: }

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

2651:   FETIDPMat_ctx  fetidpmat_ctx;
2652:   Mat            newmat;
2653:   FETIDPPC_ctx   fetidppc_ctx;
2654:   PC             newpc;
2655:   MPI_Comm       comm;

2659:   PetscObjectGetComm((PetscObject)pc,&comm);
2660:   /* FETI-DP matrix */
2661:   PCBDDCCreateFETIDPMatContext(pc,&fetidpmat_ctx);
2662:   fetidpmat_ctx->fully_redundant = fully_redundant;
2663:   PCBDDCSetupFETIDPMatContext(fetidpmat_ctx);
2664:   MatCreateShell(comm,fetidpmat_ctx->n,fetidpmat_ctx->n,fetidpmat_ctx->N,fetidpmat_ctx->N,fetidpmat_ctx,&newmat);
2665:   PetscObjectSetName((PetscObject)newmat,!fetidpmat_ctx->l2g_lambda_only ? "F" : "G");
2666:   MatShellSetOperation(newmat,MATOP_MULT,(void (*)(void))FETIDPMatMult);
2667:   MatShellSetOperation(newmat,MATOP_MULT_TRANSPOSE,(void (*)(void))FETIDPMatMultTranspose);
2668:   MatShellSetOperation(newmat,MATOP_DESTROY,(void (*)(void))PCBDDCDestroyFETIDPMat);
2669:   /* propagate MatOptions */
2670:   {
2671:     PC_BDDC   *pcbddc = (PC_BDDC*)fetidpmat_ctx->pc->data;
2672:     PetscBool issym;

2674:     MatGetOption(pc->mat,MAT_SYMMETRIC,&issym);
2675:     if (issym || pcbddc->symmetric_primal) {
2676:       MatSetOption(newmat,MAT_SYMMETRIC,PETSC_TRUE);
2677:     }
2678:   }
2679:   MatSetOptionsPrefix(newmat,prefix);
2680:   MatAppendOptionsPrefix(newmat,"fetidp_");
2681:   MatSetUp(newmat);
2682:   /* FETI-DP preconditioner */
2683:   PCBDDCCreateFETIDPPCContext(pc,&fetidppc_ctx);
2684:   PCBDDCSetupFETIDPPCContext(newmat,fetidppc_ctx);
2685:   PCCreate(comm,&newpc);
2686:   PCSetOperators(newpc,newmat,newmat);
2687:   PCSetOptionsPrefix(newpc,prefix);
2688:   PCAppendOptionsPrefix(newpc,"fetidp_");
2689:   PCSetErrorIfFailure(newpc,pc->erroriffailure);
2690:   if (!fetidpmat_ctx->l2g_lambda_only) { /* standard FETI-DP */
2691:     PCSetType(newpc,PCSHELL);
2692:     PCShellSetName(newpc,"FETI-DP multipliers");
2693:     PCShellSetContext(newpc,fetidppc_ctx);
2694:     PCShellSetApply(newpc,FETIDPPCApply);
2695:     PCShellSetApplyTranspose(newpc,FETIDPPCApplyTranspose);
2696:     PCShellSetView(newpc,FETIDPPCView);
2697:     PCShellSetDestroy(newpc,PCBDDCDestroyFETIDPPC);
2698:   } else { /* saddle-point FETI-DP */
2699:     Mat       M;
2700:     PetscInt  psize;
2701:     PetscBool fake = PETSC_FALSE, isfieldsplit;

2703:     ISViewFromOptions(fetidpmat_ctx->lagrange,NULL,"-lag_view");
2704:     ISViewFromOptions(fetidpmat_ctx->pressure,NULL,"-press_view");
2705:     PetscObjectQuery((PetscObject)pc,"__KSPFETIDP_PPmat",(PetscObject*)&M);
2706:     PCSetType(newpc,PCFIELDSPLIT);
2707:     PCFieldSplitSetIS(newpc,"lag",fetidpmat_ctx->lagrange);
2708:     PCFieldSplitSetIS(newpc,"p",fetidpmat_ctx->pressure);
2709:     PCFieldSplitSetType(newpc,PC_COMPOSITE_SCHUR);
2710:     PCFieldSplitSetSchurFactType(newpc,PC_FIELDSPLIT_SCHUR_FACT_DIAG);
2711:     ISGetSize(fetidpmat_ctx->pressure,&psize);
2712:     if (psize != M->rmap->N) {
2713:       Mat      M2;
2714:       PetscInt lpsize;

2716:       fake = PETSC_TRUE;
2717:       ISGetLocalSize(fetidpmat_ctx->pressure,&lpsize);
2718:       MatCreate(comm,&M2);
2719:       MatSetType(M2,MATAIJ);
2720:       MatSetSizes(M2,lpsize,lpsize,psize,psize);
2721:       MatSetUp(M2);
2722:       MatAssemblyBegin(M2,MAT_FINAL_ASSEMBLY);
2723:       MatAssemblyEnd(M2,MAT_FINAL_ASSEMBLY);
2724:       PCFieldSplitSetSchurPre(newpc,PC_FIELDSPLIT_SCHUR_PRE_USER,M2);
2725:       MatDestroy(&M2);
2726:     } else {
2727:       PCFieldSplitSetSchurPre(newpc,PC_FIELDSPLIT_SCHUR_PRE_USER,M);
2728:     }
2729:     PCFieldSplitSetSchurScale(newpc,1.0);

2731:     /* we need to setfromoptions and setup here to access the blocks */
2732:     PCSetFromOptions(newpc);
2733:     PCSetUp(newpc);

2735:     /* user may have changed the type (e.g. -fetidp_pc_type none) */
2736:     PetscObjectTypeCompare((PetscObject)newpc,PCFIELDSPLIT,&isfieldsplit);
2737:     if (isfieldsplit) {
2738:       KSP       *ksps;
2739:       PC        ppc,lagpc;
2740:       PetscInt  nn;
2741:       PetscBool ismatis,matisok = PETSC_FALSE,check = PETSC_FALSE;

2743:       /* set the solver for the (0,0) block */
2744:       PCFieldSplitSchurGetSubKSP(newpc,&nn,&ksps);
2745:       if (!nn) { /* not of type PC_COMPOSITE_SCHUR */
2746:         PCFieldSplitGetSubKSP(newpc,&nn,&ksps);
2747:         if (!fake) { /* pass pmat to the pressure solver */
2748:           Mat F;

2750:           KSPGetOperators(ksps[1],&F,NULL);
2751:           KSPSetOperators(ksps[1],F,M);
2752:         }
2753:       } else {
2754:         PetscBool issym;
2755:         Mat       S;

2757:         PCFieldSplitSchurGetS(newpc,&S);

2759:         MatGetOption(newmat,MAT_SYMMETRIC,&issym);
2760:         if (issym) {
2761:           MatSetOption(S,MAT_SYMMETRIC,PETSC_TRUE);
2762:         }
2763:       }
2764:       KSPGetPC(ksps[0],&lagpc);
2765:       PCSetType(lagpc,PCSHELL);
2766:       PCShellSetName(lagpc,"FETI-DP multipliers");
2767:       PCShellSetContext(lagpc,fetidppc_ctx);
2768:       PCShellSetApply(lagpc,FETIDPPCApply);
2769:       PCShellSetApplyTranspose(lagpc,FETIDPPCApplyTranspose);
2770:       PCShellSetView(lagpc,FETIDPPCView);
2771:       PCShellSetDestroy(lagpc,PCBDDCDestroyFETIDPPC);

2773:       /* Olof's idea: interface Schur complement preconditioner for the mass matrix */
2774:       KSPGetPC(ksps[1],&ppc);
2775:       if (fake) {
2776:         BDDCIPC_ctx    bddcipc_ctx;
2777:         PetscContainer c;

2779:         matisok = PETSC_TRUE;

2781:         /* create inner BDDC solver */
2782:         PetscNew(&bddcipc_ctx);
2783:         PCCreate(comm,&bddcipc_ctx->bddc);
2784:         PCSetType(bddcipc_ctx->bddc,PCBDDC);
2785:         PCSetOperators(bddcipc_ctx->bddc,M,M);
2786:         PetscObjectQuery((PetscObject)pc,"__KSPFETIDP_pCSR",(PetscObject*)&c);
2787:         PetscObjectTypeCompare((PetscObject)M,MATIS,&ismatis);
2788:         if (c && ismatis) {
2789:           Mat      lM;
2790:           PetscInt *csr,n;

2792:           MatISGetLocalMat(M,&lM);
2793:           MatGetSize(lM,&n,NULL);
2794:           PetscContainerGetPointer(c,(void**)&csr);
2795:           PCBDDCSetLocalAdjacencyGraph(bddcipc_ctx->bddc,n,csr,csr + (n + 1),PETSC_COPY_VALUES);
2796:           MatISRestoreLocalMat(M,&lM);
2797:         }
2798:         PCSetOptionsPrefix(bddcipc_ctx->bddc,((PetscObject)ksps[1])->prefix);
2799:         PCSetErrorIfFailure(bddcipc_ctx->bddc,pc->erroriffailure);
2800:         PCSetFromOptions(bddcipc_ctx->bddc);

2802:         /* wrap the interface application */
2803:         PCSetType(ppc,PCSHELL);
2804:         PCShellSetName(ppc,"FETI-DP pressure");
2805:         PCShellSetContext(ppc,bddcipc_ctx);
2806:         PCShellSetSetUp(ppc,PCSetUp_BDDCIPC);
2807:         PCShellSetApply(ppc,PCApply_BDDCIPC);
2808:         PCShellSetApplyTranspose(ppc,PCApplyTranspose_BDDCIPC);
2809:         PCShellSetView(ppc,PCView_BDDCIPC);
2810:         PCShellSetDestroy(ppc,PCDestroy_BDDCIPC);
2811:       }

2813:       /* determine if we need to assemble M to construct a preconditioner */
2814:       if (!matisok) {
2815:         PetscObjectTypeCompare((PetscObject)M,MATIS,&ismatis);
2816:         PetscObjectTypeCompareAny((PetscObject)ppc,&matisok,PCBDDC,PCJACOBI,PCNONE,PCMG,"");
2817:         if (ismatis && !matisok) {
2818:           MatConvert(M,MATAIJ,MAT_INPLACE_MATRIX,&M);
2819:         }
2820:       }

2822:       /* run the subproblems to check convergence */
2823:       PetscOptionsGetBool(NULL,((PetscObject)newmat)->prefix,"-check_saddlepoint",&check,NULL);
2824:       if (check) {
2825:         PetscInt i;

2827:         for (i=0;i<nn;i++) {
2828:           KSP       kspC;
2829:           PC        pc;
2830:           Mat       F,pF;
2831:           Vec       x,y;
2832:           PetscBool isschur,prec = PETSC_TRUE;

2834:           KSPCreate(PetscObjectComm((PetscObject)ksps[i]),&kspC);
2835:           KSPSetOptionsPrefix(kspC,((PetscObject)ksps[i])->prefix);
2836:           KSPAppendOptionsPrefix(kspC,"check_");
2837:           KSPGetOperators(ksps[i],&F,&pF);
2838:           PetscObjectTypeCompare((PetscObject)F,MATSCHURCOMPLEMENT,&isschur);
2839:           if (isschur) {
2840:             KSP  kspS,kspS2;
2841:             Mat  A00,pA00,A10,A01,A11;
2842:             char prefix[256];

2844:             MatSchurComplementGetKSP(F,&kspS);
2845:             MatSchurComplementGetSubMatrices(F,&A00,&pA00,&A01,&A10,&A11);
2846:             MatCreateSchurComplement(A00,pA00,A01,A10,A11,&F);
2847:             MatSchurComplementGetKSP(F,&kspS2);
2848:             PetscSNPrintf(prefix,sizeof(prefix),"%sschur_",((PetscObject)kspC)->prefix);
2849:             KSPSetOptionsPrefix(kspS2,prefix);
2850:             KSPGetPC(kspS2,&pc);
2851:             PCSetType(pc,PCKSP);
2852:             PCKSPSetKSP(pc,kspS);
2853:             KSPSetFromOptions(kspS2);
2854:             KSPGetPC(kspS2,&pc);
2855:             PCSetUseAmat(pc,PETSC_TRUE);
2856:           } else {
2857:             PetscObjectReference((PetscObject)F);
2858:           }
2859:           KSPSetFromOptions(kspC);
2860:           PetscOptionsGetBool(NULL,((PetscObject)kspC)->prefix,"-preconditioned",&prec,NULL);
2861:           if (prec)  {
2862:             KSPGetPC(ksps[i],&pc);
2863:             KSPSetPC(kspC,pc);
2864:           }
2865:           KSPSetOperators(kspC,F,pF);
2866:           MatCreateVecs(F,&x,&y);
2867:           VecSetRandom(x,NULL);
2868:           MatMult(F,x,y);
2869:           KSPSolve(kspC,y,x);
2870:           KSPCheckSolve(kspC,pc,x);
2871:           KSPDestroy(&kspC);
2872:           MatDestroy(&F);
2873:           VecDestroy(&x);
2874:           VecDestroy(&y);
2875:         }
2876:       }
2877:       PetscFree(ksps);
2878:     }
2879:   }
2880:   /* return pointers for objects created */
2881:   *fetidp_mat = newmat;
2882:   *fetidp_pc  = newpc;
2883:   return(0);
2884: }

2886: /*@C
2887:  PCBDDCCreateFETIDPOperators - Create FETI-DP operators

2889:    Collective

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

2896:    Output Parameters:
2897: +  fetidp_mat - shell FETI-DP matrix object
2898: -  fetidp_pc  - shell Dirichlet preconditioner for FETI-DP matrix

2900:    Level: developer

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

2905: .seealso: PCBDDC, PCBDDCMatFETIDPGetRHS, PCBDDCMatFETIDPGetSolution
2906: @*/
2907: PetscErrorCode PCBDDCCreateFETIDPOperators(PC pc, PetscBool fully_redundant, const char *prefix, Mat *fetidp_mat, PC *fetidp_pc)
2908: {

2913:   if (pc->setupcalled) {
2914:     PetscUseMethod(pc,"PCBDDCCreateFETIDPOperators_C",(PC,PetscBool,const char*,Mat*,PC*),(pc,fully_redundant,prefix,fetidp_mat,fetidp_pc));
2915:   } else SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"You must call PCSetup_BDDC() first");
2916:   return(0);
2917: }
2918: /* -------------------------------------------------------------------------- */
2919: /*MC
2920:    PCBDDC - Balancing Domain Decomposition by Constraints.

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

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

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

2928:    It also works with unsymmetric and indefinite problems.

2930:    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.

2932:    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).

2934:    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()
2935:    Additional information on dofs can be provided by using PCBDDCSetDofsSplitting(), PCBDDCSetDirichletBoundaries(), PCBDDCSetNeumannBoundaries(), and PCBDDCSetPrimalVerticesIS() and their local counterparts.

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

2939:    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.
2940:    User defined change of basis can be passed to PCBDDC by using PCBDDCSetChangeOfBasisMat()

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

2944:    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.

2946:    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.

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

2950: +    -pc_bddc_use_vertices <true> - use or not vertices in primal space
2951: .    -pc_bddc_use_edges <true> - use or not edges in primal space
2952: .    -pc_bddc_use_faces <false> - use or not faces in primal space
2953: .    -pc_bddc_symmetric <true> - symmetric computation of primal basis functions. Specify false for unsymmetric problems
2954: .    -pc_bddc_use_change_of_basis <false> - use change of basis approach (on edges only)
2955: .    -pc_bddc_use_change_on_faces <false> - use change of basis approach on faces if change of basis has been requested
2956: .    -pc_bddc_switch_static <false> - switches from M_2 (default) to M_3 operator (see reference article [1])
2957: .    -pc_bddc_levels <0> - maximum number of levels for multilevel
2958: .    -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)
2959: .    -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)
2960: .    -pc_bddc_use_deluxe_scaling <false> - use deluxe scaling
2961: .    -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)
2962: .    -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)
2963: -    -pc_bddc_check_level <0> - set verbosity level of debugging output

2965:    Options for Dirichlet, Neumann or coarse solver can be set with
2966: .vb
2967:       -pc_bddc_dirichlet_
2968:       -pc_bddc_neumann_
2969:       -pc_bddc_coarse_
2970: .ve
2971:    e.g. -pc_bddc_dirichlet_ksp_type richardson -pc_bddc_dirichlet_pc_type gamg. PCBDDC uses by default KSPPREONLY and PCLU.

2973:    When using a multilevel approach, solvers' options at the N-th level (N > 1) can be specified as
2974: .vb
2975:       -pc_bddc_dirichlet_lN_
2976:       -pc_bddc_neumann_lN_
2977:       -pc_bddc_coarse_lN_
2978: .ve
2979:    Note that level number ranges from the finest (0) to the coarsest (N).
2980:    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.
2981: .vb
2982:      -pc_bddc_coarse_pc_bddc_adaptive_threshold 5 -pc_bddc_coarse_l1_pc_bddc_redistribute 3
2983: .ve
2984:    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

2986:    References:
2987: +   [1] - C. R. Dohrmann. "An approximate BDDC preconditioner", Numerical Linear Algebra with Applications Volume 14, Issue 2, pages 149--168, March 2007
2988: .   [2] - 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
2989: .   [3] - J. Mandel, B. Sousedik, C. R. Dohrmann. "Multispace and Multilevel BDDC", Computing Volume 83, Issue 2--3, pages 55--85, November 2008
2990: -   [4] - 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

2992:    Level: intermediate

2994:    Developer Notes:

2996:    Contributed by Stefano Zampini

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

3001: PETSC_EXTERN PetscErrorCode PCCreate_BDDC(PC pc)
3002: {
3003:   PetscErrorCode      ierr;
3004:   PC_BDDC             *pcbddc;

3007:   PetscNewLog(pc,&pcbddc);
3008:   pc->data = (void*)pcbddc;

3010:   /* create PCIS data structure */
3011:   PCISCreate(pc);

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

3016:   /* BDDC nonzero defaults */
3017:   pcbddc->use_nnsp                  = PETSC_TRUE;
3018:   pcbddc->use_local_adj             = PETSC_TRUE;
3019:   pcbddc->use_vertices              = PETSC_TRUE;
3020:   pcbddc->use_edges                 = PETSC_TRUE;
3021:   pcbddc->symmetric_primal          = PETSC_TRUE;
3022:   pcbddc->vertex_size               = 1;
3023:   pcbddc->recompute_topography      = PETSC_TRUE;
3024:   pcbddc->coarse_size               = -1;
3025:   pcbddc->use_exact_dirichlet_trick = PETSC_TRUE;
3026:   pcbddc->coarsening_ratio          = 8;
3027:   pcbddc->coarse_eqs_per_proc       = 1;
3028:   pcbddc->benign_compute_correction = PETSC_TRUE;
3029:   pcbddc->nedfield                  = -1;
3030:   pcbddc->nedglobal                 = PETSC_TRUE;
3031:   pcbddc->graphmaxcount             = PETSC_MAX_INT;
3032:   pcbddc->sub_schurs_layers         = -1;
3033:   pcbddc->adaptive_threshold[0]     = 0.0;
3034:   pcbddc->adaptive_threshold[1]     = 0.0;

3036:   /* function pointers */
3037:   pc->ops->apply               = PCApply_BDDC;
3038:   pc->ops->applytranspose      = PCApplyTranspose_BDDC;
3039:   pc->ops->setup               = PCSetUp_BDDC;
3040:   pc->ops->destroy             = PCDestroy_BDDC;
3041:   pc->ops->setfromoptions      = PCSetFromOptions_BDDC;
3042:   pc->ops->view                = PCView_BDDC;
3043:   pc->ops->applyrichardson     = NULL;
3044:   pc->ops->applysymmetricleft  = NULL;
3045:   pc->ops->applysymmetricright = NULL;
3046:   pc->ops->presolve            = PCPreSolve_BDDC;
3047:   pc->ops->postsolve           = PCPostSolve_BDDC;
3048:   pc->ops->reset               = PCReset_BDDC;

3050:   /* composing function */
3051:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDiscreteGradient_C",PCBDDCSetDiscreteGradient_BDDC);
3052:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDivergenceMat_C",PCBDDCSetDivergenceMat_BDDC);
3053:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetChangeOfBasisMat_C",PCBDDCSetChangeOfBasisMat_BDDC);
3054:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetPrimalVerticesLocalIS_C",PCBDDCSetPrimalVerticesLocalIS_BDDC);
3055:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetPrimalVerticesIS_C",PCBDDCSetPrimalVerticesIS_BDDC);
3056:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetPrimalVerticesLocalIS_C",PCBDDCGetPrimalVerticesLocalIS_BDDC);
3057:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetPrimalVerticesIS_C",PCBDDCGetPrimalVerticesIS_BDDC);
3058:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetCoarseningRatio_C",PCBDDCSetCoarseningRatio_BDDC);
3059:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetLevel_C",PCBDDCSetLevel_BDDC);
3060:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetUseExactDirichlet_C",PCBDDCSetUseExactDirichlet_BDDC);
3061:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetLevels_C",PCBDDCSetLevels_BDDC);
3062:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDirichletBoundaries_C",PCBDDCSetDirichletBoundaries_BDDC);
3063:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDirichletBoundariesLocal_C",PCBDDCSetDirichletBoundariesLocal_BDDC);
3064:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetNeumannBoundaries_C",PCBDDCSetNeumannBoundaries_BDDC);
3065:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetNeumannBoundariesLocal_C",PCBDDCSetNeumannBoundariesLocal_BDDC);
3066:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetDirichletBoundaries_C",PCBDDCGetDirichletBoundaries_BDDC);
3067:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetDirichletBoundariesLocal_C",PCBDDCGetDirichletBoundariesLocal_BDDC);
3068:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetNeumannBoundaries_C",PCBDDCGetNeumannBoundaries_BDDC);
3069:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCGetNeumannBoundariesLocal_C",PCBDDCGetNeumannBoundariesLocal_BDDC);
3070:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDofsSplitting_C",PCBDDCSetDofsSplitting_BDDC);
3071:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetDofsSplittingLocal_C",PCBDDCSetDofsSplittingLocal_BDDC);
3072:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCSetLocalAdjacencyGraph_C",PCBDDCSetLocalAdjacencyGraph_BDDC);
3073:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCCreateFETIDPOperators_C",PCBDDCCreateFETIDPOperators_BDDC);
3074:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCMatFETIDPGetRHS_C",PCBDDCMatFETIDPGetRHS_BDDC);
3075:   PetscObjectComposeFunction((PetscObject)pc,"PCBDDCMatFETIDPGetSolution_C",PCBDDCMatFETIDPGetSolution_BDDC);
3076:   PetscObjectComposeFunction((PetscObject)pc,"PCPreSolveChangeRHS_C",PCPreSolveChangeRHS_BDDC);
3077:   PetscObjectComposeFunction((PetscObject)pc,"PCSetCoordinates_C",PCSetCoordinates_BDDC);
3078:   return(0);
3079: }

3081: /*@C
3082:  PCBDDCInitializePackage - This function initializes everything in the PCBDDC package. It is called
3083:     from PCInitializePackage().

3085:  Level: developer

3087:  .seealso: PetscInitialize()
3088: @*/
3089: PetscErrorCode PCBDDCInitializePackage(void)
3090: {
3092:   int            i;

3095:   if (PCBDDCPackageInitialized) return(0);
3096:   PCBDDCPackageInitialized = PETSC_TRUE;
3097:   PetscRegisterFinalize(PCBDDCFinalizePackage);

3099:   /* general events */
3100:   PetscLogEventRegister("PCBDDCTopo",PC_CLASSID,&PC_BDDC_Topology[0]);
3101:   PetscLogEventRegister("PCBDDCLKSP",PC_CLASSID,&PC_BDDC_LocalSolvers[0]);
3102:   PetscLogEventRegister("PCBDDCLWor",PC_CLASSID,&PC_BDDC_LocalWork[0]);
3103:   PetscLogEventRegister("PCBDDCCorr",PC_CLASSID,&PC_BDDC_CorrectionSetUp[0]);
3104:   PetscLogEventRegister("PCBDDCASet",PC_CLASSID,&PC_BDDC_ApproxSetUp[0]);
3105:   PetscLogEventRegister("PCBDDCAApp",PC_CLASSID,&PC_BDDC_ApproxApply[0]);
3106:   PetscLogEventRegister("PCBDDCCSet",PC_CLASSID,&PC_BDDC_CoarseSetUp[0]);
3107:   PetscLogEventRegister("PCBDDCCKSP",PC_CLASSID,&PC_BDDC_CoarseSolver[0]);
3108:   PetscLogEventRegister("PCBDDCAdap",PC_CLASSID,&PC_BDDC_AdaptiveSetUp[0]);
3109:   PetscLogEventRegister("PCBDDCScal",PC_CLASSID,&PC_BDDC_Scaling[0]);
3110:   PetscLogEventRegister("PCBDDCSchr",PC_CLASSID,&PC_BDDC_Schurs[0]);
3111:   for (i=1;i<PETSC_PCBDDC_MAXLEVELS;i++) {
3112:     char ename[32];

3114:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCTopo l%02d",i);
3115:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_Topology[i]);
3116:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCLKSP l%02d",i);
3117:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_LocalSolvers[i]);
3118:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCLWor l%02d",i);
3119:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_LocalWork[i]);
3120:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCCorr l%02d",i);
3121:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_CorrectionSetUp[i]);
3122:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCASet l%02d",i);
3123:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_ApproxSetUp[i]);
3124:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCAApp l%02d",i);
3125:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_ApproxApply[i]);
3126:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCCSet l%02d",i);
3127:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_CoarseSetUp[i]);
3128:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCCKSP l%02d",i);
3129:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_CoarseSolver[i]);
3130:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCAdap l%02d",i);
3131:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_AdaptiveSetUp[i]);
3132:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCScal l%02d",i);
3133:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_Scaling[i]);
3134:     PetscSNPrintf(ename,sizeof(ename),"PCBDDCSchr l%02d",i);
3135:     PetscLogEventRegister(ename,PC_CLASSID,&PC_BDDC_Schurs[i]);
3136:   }
3137:   return(0);
3138: }

3140: /*@C
3141:  PCBDDCFinalizePackage - This function frees everything from the PCBDDC package. It is
3142:     called from PetscFinalize() automatically.

3144:  Level: developer

3146:  .seealso: PetscFinalize()
3147: @*/
3148: PetscErrorCode PCBDDCFinalizePackage(void)
3149: {
3151:   PCBDDCPackageInitialized = PETSC_FALSE;
3152:   return(0);
3153: }