Actual source code: is.c
1: /*$Id: is.c,v 1.9 2001/08/07 03:03:41 balay Exp $*/
2: #include src/sles/pc/impls/is/is.h
4: /* -------------------------------------------------------------------------- */
5: /*
6: PCISSetUp -
7: */
8: int PCISSetUp(PC pc)
9: {
10: PC_IS *pcis = (PC_IS*)(pc->data);
11: Mat_IS *matis = (Mat_IS*)pc->mat->data;
12: int i, ierr;
13: PetscTruth flg;
14:
16: PetscTypeCompare((PetscObject)pc->mat,MATIS,&flg);
17: if (!flg){
18: SETERRQ(1,"Preconditioner type of Neumann Neumman requires matrix of type MATIS");
19: }
21: pcis->pure_neumann = matis->pure_neumann;
23: /*
24: Creating the local vector vec1_N, containing the inverse of the number
25: of subdomains to which each local node (either owned or ghost)
26: pertains. To accomplish that, we scatter local vectors of 1's to
27: a global vector (adding the values); scatter the result back to
28: local vectors and finally invert the result.
29: */
30: {
31: Vec counter;
32: PetscScalar one=1.0, zero=0.0;
33: VecDuplicate(matis->x,&pcis->vec1_N);
34: VecDuplicate(pc->vec,&counter); /* temporary auxiliar vector */
35: VecSet(&zero,counter);
36: VecSet(&one,pcis->vec1_N);
37: VecScatterBegin(pcis->vec1_N,counter,ADD_VALUES,SCATTER_REVERSE,matis->ctx);
38: VecScatterEnd (pcis->vec1_N,counter,ADD_VALUES,SCATTER_REVERSE,matis->ctx);
39: VecScatterBegin(counter,pcis->vec1_N,INSERT_VALUES,SCATTER_FORWARD,matis->ctx);
40: VecScatterEnd (counter,pcis->vec1_N,INSERT_VALUES,SCATTER_FORWARD,matis->ctx);
41: VecDestroy(counter);
42: }
43: /*
44: Creating local and global index sets for interior and
45: inteface nodes. Notice that interior nodes have D[i]==1.0.
46: */
47: {
48: int n_I;
49: int *idx_I_local,*idx_B_local,*idx_I_global,*idx_B_global;
50: PetscScalar *array;
51: /* Identifying interior and interface nodes, in local numbering */
52: VecGetSize(pcis->vec1_N,&pcis->n);
53: VecGetArray(pcis->vec1_N,&array);
54: PetscMalloc(pcis->n*sizeof(int),&idx_I_local);
55: PetscMalloc(pcis->n*sizeof(int),&idx_B_local);
56: for (i=0, pcis->n_B=0, n_I=0; i<pcis->n; i++) {
57: if (array[i] == 1.0) { idx_I_local[n_I] = i; n_I++; }
58: else { idx_B_local[pcis->n_B] = i; pcis->n_B++; }
59: }
60: /* Getting the global numbering */
61: idx_B_global = idx_I_local + n_I; /* Just avoiding allocating extra memory, since we have vacant space */
62: idx_I_global = idx_B_local + pcis->n_B;
63: ISLocalToGlobalMappingApply(matis->mapping,pcis->n_B,idx_B_local,idx_B_global);
64: ISLocalToGlobalMappingApply(matis->mapping,n_I, idx_I_local,idx_I_global);
65: /* Creating the index sets. */
66: ISCreateGeneral(MPI_COMM_SELF,pcis->n_B,idx_B_local, &pcis->is_B_local);
67: ISCreateGeneral(MPI_COMM_SELF,pcis->n_B,idx_B_global,&pcis->is_B_global);
68: ISCreateGeneral(MPI_COMM_SELF,n_I ,idx_I_local, &pcis->is_I_local);
69: ISCreateGeneral(MPI_COMM_SELF,n_I ,idx_I_global,&pcis->is_I_global);
70: /* Freeing memory and restoring arrays */
71: PetscFree(idx_B_local);
72: PetscFree(idx_I_local);
73: VecRestoreArray(pcis->vec1_N,&array);
74: }
76: /*
77: Extracting the blocks A_II, A_BI, A_IB and A_BB from A. If the numbering
78: is such that interior nodes come first than the interface ones, we have
80: [ | ]
81: [ A_II | A_IB ]
82: A = [ | ]
83: [-----------+------]
84: [ A_BI | A_BB ]
85: */
87: MatGetSubMatrix(matis->A,pcis->is_I_local,pcis->is_I_local,PETSC_DECIDE,MAT_INITIAL_MATRIX,&pcis->A_II);
88: MatGetSubMatrix(matis->A,pcis->is_I_local,pcis->is_B_local,PETSC_DECIDE,MAT_INITIAL_MATRIX,&pcis->A_IB);
89: MatGetSubMatrix(matis->A,pcis->is_B_local,pcis->is_I_local,PETSC_DECIDE,MAT_INITIAL_MATRIX,&pcis->A_BI);
90: MatGetSubMatrix(matis->A,pcis->is_B_local,pcis->is_B_local,PETSC_DECIDE,MAT_INITIAL_MATRIX,&pcis->A_BB);
92: /*
93: Creating work vectors and arrays
94: */
95: /* pcis->vec1_N has already been created */
96: VecDuplicate(pcis->vec1_N,&pcis->vec2_N);
97: VecCreateSeq(PETSC_COMM_SELF,pcis->n-pcis->n_B,&pcis->vec1_D);
98: VecDuplicate(pcis->vec1_D,&pcis->vec2_D);
99: VecDuplicate(pcis->vec1_D,&pcis->vec3_D);
100: VecCreateSeq(PETSC_COMM_SELF,pcis->n_B,&pcis->vec1_B);
101: VecDuplicate(pcis->vec1_B,&pcis->vec2_B);
102: VecDuplicate(pcis->vec1_B,&pcis->vec3_B);
103: {
104: Vec global;
105: PCGetVector(pc,&global);
106: VecDuplicate(global,&pcis->vec1_global);
107: }
108: PetscMalloc((pcis->n)*sizeof(PetscScalar),&pcis->work_N);
110: /* Creating the scatter contexts */
111: VecScatterCreate(pc->vec,pcis->is_I_global,pcis->vec1_D,(IS)0,&pcis->global_to_D);
112: VecScatterCreate(pcis->vec1_N,pcis->is_B_local,pcis->vec1_B,(IS)0,&pcis->N_to_B);
113: VecScatterCreate(pc->vec,pcis->is_B_global,pcis->vec1_B,(IS)0,&pcis->global_to_B);
115: /* Creating scaling "matrix" D, from information in vec1_N */
116: VecDuplicate(pcis->vec1_B,&pcis->D);
117: VecScatterBegin(pcis->vec1_N,pcis->D,INSERT_VALUES,SCATTER_FORWARD,pcis->N_to_B);
118: VecScatterEnd (pcis->vec1_N,pcis->D,INSERT_VALUES,SCATTER_FORWARD,pcis->N_to_B);
119: VecReciprocal(pcis->D);
121: /* See historical note 01, at the bottom of this file. */
123: /*
124: Creating the SLES contexts for the local Dirichlet and Neumann problems.
125: */
126: {
127: PC pc_ctx;
128: KSP ksp_ctx;
129: /* Dirichlet */
130: SLESCreate(PETSC_COMM_SELF,&pcis->sles_D);
131: SLESSetOperators(pcis->sles_D,pcis->A_II,pcis->A_II,SAME_PRECONDITIONER);
132: SLESSetOptionsPrefix(pcis->sles_D,"localD_");
133: SLESGetKSP(pcis->sles_D,&ksp_ctx);
134: SLESGetPC(pcis->sles_D,&pc_ctx);
135: PCSetType(pc_ctx,PCLU);
136: KSPSetType(ksp_ctx,KSPPREONLY);
137: SLESSetFromOptions(pcis->sles_D);
138: /* the vectors in the following line are dummy arguments, just telling the SLES the vector size. Values are not used */
139: SLESSetUp(pcis->sles_D,pcis->vec1_D,pcis->vec2_D);
140: /* Neumann */
141: SLESCreate(PETSC_COMM_SELF,&pcis->sles_N);
142: SLESSetOperators(pcis->sles_N,matis->A,matis->A,SAME_PRECONDITIONER);
143: SLESSetOptionsPrefix(pcis->sles_N,"localN_");
144: SLESGetKSP(pcis->sles_N,&ksp_ctx);
145: SLESGetPC(pcis->sles_N,&pc_ctx);
146: PCSetType(pc_ctx,PCLU);
147: KSPSetType(ksp_ctx,KSPPREONLY);
148: SLESSetFromOptions(pcis->sles_N);
149: {
150: PetscTruth damp_fixed,
151: remove_nullspace_fixed,
152: set_damping_factor_floating,
153: not_damp_floating,
154: not_remove_nullspace_floating;
155: PetscReal fixed_factor,
156: floating_factor;
158: PetscOptionsGetReal(pc_ctx->prefix,"-pc_is_damp_fixed",&fixed_factor,&damp_fixed);
159: if (!damp_fixed) { fixed_factor = 0.0; }
160: PetscOptionsHasName(pc_ctx->prefix,"-pc_is_damp_fixed",&damp_fixed);
162: PetscOptionsHasName(pc_ctx->prefix,"-pc_is_remove_nullspace_fixed",&remove_nullspace_fixed);
164: PetscOptionsGetReal(pc_ctx->prefix,"-pc_is_set_damping_factor_floating",
165: &floating_factor,&set_damping_factor_floating);
166: if (!set_damping_factor_floating) { floating_factor = 0.0; }
167: PetscOptionsHasName(pc_ctx->prefix,"-pc_is_set_damping_factor_floating",&set_damping_factor_floating);
168: if (!set_damping_factor_floating) { floating_factor = 1.e-12; }
170: PetscOptionsHasName(pc_ctx->prefix,"-pc_is_not_damp_floating",¬_damp_floating);
172: PetscOptionsHasName(pc_ctx->prefix,"-pc_is_not_remove_nullspace_floating",¬_remove_nullspace_floating);
174: if (pcis->pure_neumann) { /* floating subdomain */
175: if (!(not_damp_floating)) {
176: PCLUSetDamping (pc_ctx,floating_factor);
177: PCILUSetDamping(pc_ctx,floating_factor);
178: }
179: if (!(not_remove_nullspace_floating)){
180: MatNullSpace nullsp;
181: MatNullSpaceCreate(PETSC_COMM_SELF,1,0,PETSC_NULL,&nullsp);
182: PCNullSpaceAttach(pc_ctx,nullsp);
183: MatNullSpaceDestroy(nullsp);
184: }
185: } else { /* fixed subdomain */
186: if (damp_fixed) {
187: PCLUSetDamping (pc_ctx,fixed_factor);
188: PCILUSetDamping(pc_ctx,fixed_factor);
189: }
190: if (remove_nullspace_fixed) {
191: MatNullSpace nullsp;
192: MatNullSpaceCreate(PETSC_COMM_SELF,1,0,PETSC_NULL,&nullsp);
193: PCNullSpaceAttach(pc_ctx,nullsp);
194: MatNullSpaceDestroy(nullsp);
195: }
196: }
197: }
198: /* the vectors in the following line are dummy arguments, just telling the SLES the vector size. Values are not used */
199: SLESSetUp(pcis->sles_N,pcis->vec1_N,pcis->vec2_N);
200: }
202: ISLocalToGlobalMappingGetInfo(((Mat_IS*)(pc->mat->data))->mapping,&(pcis->n_neigh),&(pcis->neigh),
203: &(pcis->n_shared),&(pcis->shared));
204: pcis->ISLocalToGlobalMappingGetInfoWasCalled = PETSC_TRUE;
206: return(0);
207: }
209: /* -------------------------------------------------------------------------- */
210: /*
211: PCISDestroy -
212: */
213: int PCISDestroy(PC pc)
214: {
215: PC_IS *pcis = (PC_IS*)(pc->data);
216: int ierr;
220: if (pcis->is_B_local) {ISDestroy(pcis->is_B_local);}
221: if (pcis->is_I_local) {ISDestroy(pcis->is_I_local);}
222: if (pcis->is_B_global) {ISDestroy(pcis->is_B_global);}
223: if (pcis->is_I_global) {ISDestroy(pcis->is_I_global);}
224: if (pcis->A_II) {MatDestroy(pcis->A_II);}
225: if (pcis->A_IB) {MatDestroy(pcis->A_IB);}
226: if (pcis->A_BI) {MatDestroy(pcis->A_BI);}
227: if (pcis->A_BB) {MatDestroy(pcis->A_BB);}
228: if (pcis->D) {VecDestroy(pcis->D);}
229: if (pcis->sles_N) {SLESDestroy(pcis->sles_N);}
230: if (pcis->sles_D) {SLESDestroy(pcis->sles_D);}
231: if (pcis->vec1_N) {VecDestroy(pcis->vec1_N);}
232: if (pcis->vec2_N) {VecDestroy(pcis->vec2_N);}
233: if (pcis->vec1_D) {VecDestroy(pcis->vec1_D);}
234: if (pcis->vec2_D) {VecDestroy(pcis->vec2_D);}
235: if (pcis->vec3_D) {VecDestroy(pcis->vec3_D);}
236: if (pcis->vec1_B) {VecDestroy(pcis->vec1_B);}
237: if (pcis->vec2_B) {VecDestroy(pcis->vec2_B);}
238: if (pcis->vec3_B) {VecDestroy(pcis->vec3_B);}
239: if (pcis->vec1_global) {VecDestroy(pcis->vec1_global);}
240: if (pcis->work_N) {PetscFree(pcis->work_N);}
241: if (pcis->global_to_D) {VecScatterDestroy(pcis->global_to_D);}
242: if (pcis->N_to_B) {VecScatterDestroy(pcis->N_to_B);}
243: if (pcis->global_to_B) {VecScatterDestroy(pcis->global_to_B);}
244: if (pcis->ISLocalToGlobalMappingGetInfoWasCalled) {
245: ISLocalToGlobalMappingRestoreInfo((ISLocalToGlobalMapping)0,&(pcis->n_neigh),&(pcis->neigh),&(pcis->n_shared),&(pcis->shared));
246: }
248: return(0);
249: }
251: /* -------------------------------------------------------------------------- */
252: /*
253: PCISCreate -
254: */
255: int PCISCreate(PC pc)
256: {
257: PC_IS *pcis = (PC_IS*)(pc->data);
261: pcis->is_B_local = 0;
262: pcis->is_I_local = 0;
263: pcis->is_B_global = 0;
264: pcis->is_I_global = 0;
265: pcis->A_II = 0;
266: pcis->A_IB = 0;
267: pcis->A_BI = 0;
268: pcis->A_BB = 0;
269: pcis->D = 0;
270: pcis->sles_N = 0;
271: pcis->sles_D = 0;
272: pcis->vec1_N = 0;
273: pcis->vec2_N = 0;
274: pcis->vec1_D = 0;
275: pcis->vec2_D = 0;
276: pcis->vec3_D = 0;
277: pcis->vec1_B = 0;
278: pcis->vec2_B = 0;
279: pcis->vec3_B = 0;
280: pcis->vec1_global = 0;
281: pcis->work_N = 0;
282: pcis->global_to_D = 0;
283: pcis->N_to_B = 0;
284: pcis->global_to_B = 0;
285: pcis->ISLocalToGlobalMappingGetInfoWasCalled = PETSC_FALSE;
287: return(0);
288: }
290: /* -------------------------------------------------------------------------- */
291: /*
292: PCISApplySchur -
294: Input parameters:
295: . pc - preconditioner context
296: . v - vector to which the Schur complement is to be applied (it is NOT modified inside this function, UNLESS vec2_B is null)
298: Output parameters:
299: . vec1_B - result of Schur complement applied to chunk
300: . vec2_B - garbage (used as work space), or null (and v is used as workspace)
301: . vec1_D - garbage (used as work space)
302: . vec2_D - garbage (used as work space)
304: */
305: int PCISApplySchur(PC pc, Vec v, Vec vec1_B, Vec vec2_B, Vec vec1_D, Vec vec2_D)
306: {
307: int ierr, its;
308: PetscScalar m_one = -1.0;
309: PC_IS *pcis = (PC_IS*)(pc->data);
313: if (vec2_B == (Vec)0) { vec2_B = v; }
315: MatMult(pcis->A_BB,v,vec1_B);
316: MatMult(pcis->A_IB,v,vec1_D);
317: SLESSolve(pcis->sles_D,vec1_D,vec2_D,&its);
318: MatMult(pcis->A_BI,vec2_D,vec2_B);
319: VecAXPY(&m_one,vec2_B,vec1_B);
321: return(0);
322: }
324: /* -------------------------------------------------------------------------- */
325: /*
326: PCISScatterArrayNToVecB - Scatters interface node values from a big array (of all local nodes, interior or interface,
327: including ghosts) into an interface vector, when in SCATTER_FORWARD mode, or vice-versa, when in SCATTER_REVERSE
328: mode.
330: Input parameters:
331: . pc - preconditioner context
332: . array_N - [when in SCATTER_FORWARD mode] Array to be scattered into the vector
333: . v_B - [when in SCATTER_REVERSE mode] Vector to be scattered into the array
335: Output parameter:
336: . array_N - [when in SCATTER_REVERSE mode] Array to receive the scattered vector
337: . v_B - [when in SCATTER_FORWARD mode] Vector to receive the scattered array
339: Notes:
340: The entries in the array that do not correspond to interface nodes remain unaltered.
341: */
342: int PCISScatterArrayNToVecB (PetscScalar *array_N, Vec v_B, InsertMode imode, ScatterMode smode, PC pc)
343: {
344: int i, ierr, *index;
345: PetscScalar *array_B;
346: PC_IS *pcis = (PC_IS*)(pc->data);
350: VecGetArray(v_B,&array_B);
351: ISGetIndices(pcis->is_B_local,&index);
353: if (smode == SCATTER_FORWARD) {
354: if (imode == INSERT_VALUES) {
355: for (i=0; i<pcis->n_B; i++) { array_B[i] = array_N[index[i]]; }
356: } else { /* ADD_VALUES */
357: for (i=0; i<pcis->n_B; i++) { array_B[i] += array_N[index[i]]; }
358: }
359: } else { /* SCATTER_REVERSE */
360: if (imode == INSERT_VALUES) {
361: for (i=0; i<pcis->n_B; i++) { array_N[index[i]] = array_B[i]; }
362: } else { /* ADD_VALUES */
363: for (i=0; i<pcis->n_B; i++) { array_N[index[i]] += array_B[i]; }
364: }
365: }
367: ISRestoreIndices(pcis->is_B_local,&index);
368: VecRestoreArray(v_B,&array_B);
370: return(0);
371: }
373: /* -------------------------------------------------------------------------- */
374: /*
375: PCISApplyInvSchur - Solves the Neumann problem related to applying the inverse of the Schur complement.
376: More precisely, solves the problem:
377: [ A_II A_IB ] [ . ] [ 0 ]
378: [ ] [ ] = [ ]
379: [ A_BI A_BB ] [ x ] [ b ]
381: Input parameters:
382: . pc - preconditioner context
383: . b - vector of local interface nodes (including ghosts)
385: Output parameters:
386: . x - vector of local interface nodes (including ghosts); returns the application of the inverse of the Schur
387: complement to b
388: . vec1_N - vector of local nodes (interior and interface, including ghosts); returns garbage (used as work space)
389: . vec2_N - vector of local nodes (interior and interface, including ghosts); returns garbage (used as work space)
391: */
392: int PCISApplyInvSchur (PC pc, Vec b, Vec x, Vec vec1_N, Vec vec2_N)
393: {
394: int ierr, its;
395: PC_IS *pcis = (PC_IS*)(pc->data);
396: PetscScalar zero = 0.0;
400: /*
401: Neumann solvers.
402: Applying the inverse of the local Schur complement, i.e, solving a Neumann
403: Problem with zero at the interior nodes of the RHS and extracting the interface
404: part of the solution. inverse Schur complement is applied to b and the result
405: is stored in x.
406: */
407: /* Setting the RHS vec1_N */
408: VecSet(&zero,vec1_N);
409: VecScatterBegin(b,vec1_N,INSERT_VALUES,SCATTER_REVERSE,pcis->N_to_B);
410: VecScatterEnd (b,vec1_N,INSERT_VALUES,SCATTER_REVERSE,pcis->N_to_B);
411: /* Checking for consistency of the RHS */
412: {
413: PetscTruth flg;
414: PetscOptionsHasName(PETSC_NULL,"-check_consistency",&flg);
415: if (flg) {
416: PetscScalar average;
417: VecSum(vec1_N,&average);
418: average = average / ((PetscReal)pcis->n);
419: if (pcis->pure_neumann) {
420: PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_(pc->comm),"Subdomain %04d is floating. Average = % 1.14en",
421: PetscGlobalRank,PetscAbsScalar(average));
422: } else {
423: PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_(pc->comm),"Subdomain %04d is fixed. Average = % 1.14en",
424: PetscGlobalRank,PetscAbsScalar(average));
425: }
426: PetscViewerFlush(PETSC_VIEWER_STDOUT_(pc->comm));
427: }
428: }
429: /* Solving the system for vec2_N */
430: SLESSolve(pcis->sles_N,vec1_N,vec2_N,&its);
431: /* Extracting the local interface vector out of the solution */
432: VecScatterBegin(vec2_N,x,INSERT_VALUES,SCATTER_FORWARD,pcis->N_to_B);
433: VecScatterEnd (vec2_N,x,INSERT_VALUES,SCATTER_FORWARD,pcis->N_to_B);
435: return(0);
436: }