Actual source code: nasm.c
petsc-dev 2014-02-02
1: #include <petsc-private/snesimpl.h> /*I "petscsnes.h" I*/
2: #include <petscdm.h>
4: typedef struct {
5: PetscInt n; /* local subdomains */
6: SNES *subsnes; /* nonlinear solvers for each subdomain */
7: Vec *x; /* solution vectors */
8: Vec *xl; /* solution local vectors */
9: Vec *y; /* step vectors */
10: Vec *b; /* rhs vectors */
11: VecScatter *oscatter; /* scatter from global space to the subdomain global space */
12: VecScatter *iscatter; /* scatter from global space to the nonoverlapping subdomain space */
13: VecScatter *gscatter; /* scatter from global space to the subdomain local space */
14: PCASMType type; /* ASM type */
15: PetscBool usesdm; /* use the DM for setting up the subproblems */
16: PetscBool finaljacobian; /* compute the jacobian of the converged solution */
17: PetscReal damping; /* damping parameter for updates from the blocks */
18: PetscBool same_local_solves; /* flag to determine if the solvers have been individually modified */
20: /* logging events */
21: PetscLogEvent eventrestrictinterp;
22: PetscLogEvent eventsubsolve;
24: PetscInt fjtype; /* type of computed jacobian */
25: Vec xinit; /* initial solution in case the final jacobian type is computed as first */
26: } SNES_NASM;
28: const char *const SNESNASMTypes[] = {"NONE","RESTRICT","INTERPOLATE","BASIC","PCASMType","PC_ASM_",0};
29: const char *const SNESNASMFJTypes[] = {"FINALOUTER","FINALINNER","INITIAL"};
33: PetscErrorCode SNESReset_NASM(SNES snes)
34: {
35: SNES_NASM *nasm = (SNES_NASM*)snes->data;
37: PetscInt i;
40: for (i=0; i<nasm->n; i++) {
41: if (nasm->xl) { VecDestroy(&nasm->xl[i]); }
42: if (nasm->x) { VecDestroy(&nasm->x[i]); }
43: if (nasm->y) { VecDestroy(&nasm->y[i]); }
44: if (nasm->b) { VecDestroy(&nasm->b[i]); }
46: if (nasm->subsnes) { SNESDestroy(&nasm->subsnes[i]); }
47: if (nasm->oscatter) { VecScatterDestroy(&nasm->oscatter[i]); }
48: if (nasm->iscatter) { VecScatterDestroy(&nasm->iscatter[i]); }
49: if (nasm->gscatter) { VecScatterDestroy(&nasm->gscatter[i]); }
50: }
52: if (nasm->x) {PetscFree(nasm->x);}
53: if (nasm->xl) {PetscFree(nasm->xl);}
54: if (nasm->y) {PetscFree(nasm->y);}
55: if (nasm->b) {PetscFree(nasm->b);}
57: if (nasm->xinit) {VecDestroy(&nasm->xinit);}
59: if (nasm->subsnes) {PetscFree(nasm->subsnes);}
60: if (nasm->oscatter) {PetscFree(nasm->oscatter);}
61: if (nasm->iscatter) {PetscFree(nasm->iscatter);}
62: if (nasm->gscatter) {PetscFree(nasm->gscatter);}
64: nasm->eventrestrictinterp = 0;
65: nasm->eventsubsolve = 0;
66: return(0);
67: }
71: PetscErrorCode SNESDestroy_NASM(SNES snes)
72: {
76: SNESReset_NASM(snes);
77: PetscFree(snes->data);
78: return(0);
79: }
83: PetscErrorCode DMGlobalToLocalSubDomainDirichletHook_Private(DM dm,Vec g,InsertMode mode,Vec l,void *ctx)
84: {
86: Vec bcs = (Vec)ctx;
89: VecCopy(bcs,l);
90: return(0);
91: }
95: PetscErrorCode SNESSetUp_NASM(SNES snes)
96: {
97: SNES_NASM *nasm = (SNES_NASM*)snes->data;
99: DM dm,subdm;
100: DM *subdms;
101: PetscInt i;
102: const char *optionsprefix;
103: Vec F;
104: PetscMPIInt size;
105: KSP ksp;
106: PC pc;
109: if (!nasm->subsnes) {
110: SNESGetDM(snes,&dm);
111: if (dm) {
112: nasm->usesdm = PETSC_TRUE;
113: DMCreateDomainDecomposition(dm,&nasm->n,NULL,NULL,NULL,&subdms);
114: if (!subdms) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"DM has no default decomposition defined. Set subsolves manually with SNESNASMSetSubdomains().");
115: DMCreateDomainDecompositionScatters(dm,nasm->n,subdms,&nasm->iscatter,&nasm->oscatter,&nasm->gscatter);
117: SNESGetOptionsPrefix(snes, &optionsprefix);
118: PetscMalloc1(nasm->n,&nasm->subsnes);
119: for (i=0; i<nasm->n; i++) {
120: SNESCreate(PETSC_COMM_SELF,&nasm->subsnes[i]);
121: SNESAppendOptionsPrefix(nasm->subsnes[i],optionsprefix);
122: SNESAppendOptionsPrefix(nasm->subsnes[i],"sub_");
123: SNESSetDM(nasm->subsnes[i],subdms[i]);
124: MPI_Comm_size(PetscObjectComm((PetscObject)nasm->subsnes[i]),&size);
125: if (size == 1) {
126: SNESGetKSP(nasm->subsnes[i],&ksp);
127: KSPGetPC(ksp,&pc);
128: KSPSetType(ksp,KSPPREONLY);
129: PCSetType(pc,PCLU);
130: }
131: SNESSetFromOptions(nasm->subsnes[i]);
132: DMDestroy(&subdms[i]);
133: }
134: PetscFree(subdms);
135: } else SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"Cannot construct local problems automatically without a DM!");
136: } else SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"Must set subproblems manually if there is no DM!");
137: /* allocate the global vectors */
138: if (!nasm->x) {
139: PetscCalloc1(nasm->n,&nasm->x);
140: }
141: if (!nasm->xl) {
142: PetscCalloc1(nasm->n,&nasm->xl);
143: }
144: if (!nasm->y) {
145: PetscCalloc1(nasm->n,&nasm->y);
146: }
147: if (!nasm->b) {
148: PetscCalloc1(nasm->n,&nasm->b);
149: }
151: for (i=0; i<nasm->n; i++) {
152: SNESGetFunction(nasm->subsnes[i],&F,NULL,NULL);
153: if (!nasm->x[i]) {VecDuplicate(F,&nasm->x[i]);}
154: if (!nasm->y[i]) {VecDuplicate(F,&nasm->y[i]);}
155: if (!nasm->b[i]) {VecDuplicate(F,&nasm->b[i]);}
156: if (!nasm->xl[i]) {
157: SNESGetDM(nasm->subsnes[i],&subdm);
158: DMCreateLocalVector(subdm,&nasm->xl[i]);
159: }
160: DMGlobalToLocalHookAdd(subdm,DMGlobalToLocalSubDomainDirichletHook_Private,NULL,nasm->xl[i]);
161: }
162: if (nasm->finaljacobian) {
163: SNESSetUpMatrices(snes);
164: if (nasm->fjtype == 2) {
165: VecDuplicate(snes->vec_sol,&nasm->xinit);
166: }
167: for (i=0; i<nasm->n;i++) {
168: SNESSetUpMatrices(nasm->subsnes[i]);
169: }
170: }
171: return(0);
172: }
176: PetscErrorCode SNESSetFromOptions_NASM(SNES snes)
177: {
178: PetscErrorCode ierr;
179: PCASMType asmtype;
180: PetscBool flg,monflg,subviewflg;
181: SNES_NASM *nasm = (SNES_NASM*)snes->data;
184: PetscOptionsHead("Nonlinear Additive Schwartz options");
185: PetscOptionsEnum("-snes_nasm_type","Type of restriction/extension","",SNESNASMTypes,(PetscEnum)nasm->type,(PetscEnum*)&asmtype,&flg);
186: if (flg) nasm->type = asmtype;
187: flg = PETSC_FALSE;
188: monflg = PETSC_TRUE;
189: PetscOptionsReal("-snes_nasm_damping","Log times for subSNES solves and restriction","SNESNASMSetDamping",nasm->damping,&nasm->damping,&flg);
190: if (flg) {SNESNASMSetDamping(snes,nasm->damping);}
191: subviewflg = PETSC_FALSE;
192: PetscOptionsBool("-snes_nasm_sub_view","Print detailed information for every processor when using -snes_view","",subviewflg,&subviewflg,&flg);
193: if (flg) {
194: nasm->same_local_solves = PETSC_FALSE;
195: if (!subviewflg) {
196: nasm->same_local_solves = PETSC_TRUE;
197: }
198: }
199: PetscOptionsBool("-snes_nasm_finaljacobian","Compute the global jacobian of the final iterate (for ASPIN)","",nasm->finaljacobian,&nasm->finaljacobian,NULL);
200: PetscOptionsEList("-snes_nasm_finaljacobian_type","The type of the final jacobian computed.","",SNESNASMFJTypes,3,SNESNASMFJTypes[0],&nasm->fjtype,NULL);
201: PetscOptionsBool("-snes_nasm_log","Log times for subSNES solves and restriction","",monflg,&monflg,&flg);
202: if (flg) {
203: PetscLogEventRegister("SNESNASMSubSolve",((PetscObject)snes)->classid,&nasm->eventsubsolve);
204: PetscLogEventRegister("SNESNASMRestrict",((PetscObject)snes)->classid,&nasm->eventrestrictinterp);
205: }
206: PetscOptionsTail();
207: return(0);
208: }
212: PetscErrorCode SNESView_NASM(SNES snes, PetscViewer viewer)
213: {
214: SNES_NASM *nasm = (SNES_NASM*)snes->data;
216: PetscMPIInt rank,size;
217: PetscInt i,N,bsz;
218: PetscBool iascii,isstring;
219: PetscViewer sviewer;
220: MPI_Comm comm;
223: PetscObjectGetComm((PetscObject)snes,&comm);
224: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
225: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
226: MPI_Comm_rank(comm,&rank);
227: MPI_Comm_size(comm,&size);
228: MPI_Allreduce(&nasm->n,&N,1,MPIU_INT,MPI_SUM,comm);
229: if (iascii) {
230: PetscViewerASCIIPrintf(viewer, " Nonlinear Additive Schwarz: total subdomain blocks = %D\n",N);
231: if (nasm->same_local_solves) {
232: if (nasm->subsnes) {
233: PetscViewerASCIIPrintf(viewer," Local solve is the same for all blocks:\n");
234: PetscViewerASCIIPushTab(viewer);
235: PetscViewerGetSingleton(viewer,&sviewer);
236: if (!rank) {
237: PetscViewerASCIIPushTab(viewer);
238: SNESView(nasm->subsnes[0],sviewer);
239: PetscViewerASCIIPopTab(viewer);
240: }
241: PetscViewerRestoreSingleton(viewer,&sviewer);
242: PetscViewerASCIIPopTab(viewer);
243: }
244: } else {
245: /* print the solver on each block */
246: PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);
247: PetscViewerASCIISynchronizedPrintf(viewer," [%d] number of local blocks = %D\n",(int)rank,nasm->n);
248: PetscViewerFlush(viewer);
249: PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);
250: PetscViewerASCIIPrintf(viewer," Local solve info for each block is in the following SNES objects:\n");
251: PetscViewerASCIIPushTab(viewer);
252: PetscViewerASCIIPrintf(viewer,"- - - - - - - - - - - - - - - - - -\n");
253: PetscViewerGetSingleton(viewer,&sviewer);
254: for (i=0; i<nasm->n; i++) {
255: VecGetLocalSize(nasm->x[i],&bsz);
256: PetscViewerASCIIPrintf(sviewer,"[%d] local block number %D, size = %D\n",(int)rank,i,bsz);
257: SNESView(nasm->subsnes[i],sviewer);
258: PetscViewerASCIIPrintf(sviewer,"- - - - - - - - - - - - - - - - - -\n");
259: }
260: PetscViewerRestoreSingleton(viewer,&sviewer);
261: PetscViewerFlush(viewer);
262: PetscViewerASCIIPopTab(viewer);
263: }
264: } else if (isstring) {
265: PetscViewerStringSPrintf(viewer," blocks=%D,type=%s",N,SNESNASMTypes[nasm->type]);
266: PetscViewerGetSingleton(viewer,&sviewer);
267: if (nasm->subsnes && !rank) {SNESView(nasm->subsnes[0],sviewer);}
268: PetscViewerRestoreSingleton(viewer,&sviewer);
269: }
270: return(0);
271: }
275: /*@
276: SNESNASMSetSubdomains - Manually Set the context required to restrict and solve subdomain problems.
278: Not Collective
280: Input Parameters:
281: + SNES - the SNES context
282: . n - the number of local subdomains
283: . subsnes - solvers defined on the local subdomains
284: . iscatter - scatters into the nonoverlapping portions of the local subdomains
285: . oscatter - scatters into the overlapping portions of the local subdomains
286: - gscatter - scatters into the (ghosted) local vector of the local subdomain
288: Level: intermediate
290: .keywords: SNES, NASM
292: .seealso: SNESNASM, SNESNASMGetSubdomains()
293: @*/
294: PetscErrorCode SNESNASMSetSubdomains(SNES snes,PetscInt n,SNES subsnes[],VecScatter iscatter[],VecScatter oscatter[],VecScatter gscatter[])
295: {
297: PetscErrorCode (*f)(SNES,PetscInt,SNES*,VecScatter*,VecScatter*,VecScatter*);
300: PetscObjectQueryFunction((PetscObject)snes,"SNESNASMSetSubdomains_C",&f);
301: if (f) {(f)(snes,n,subsnes,iscatter,oscatter,gscatter);}
302: return(0);
303: }
307: PetscErrorCode SNESNASMSetSubdomains_NASM(SNES snes,PetscInt n,SNES subsnes[],VecScatter iscatter[],VecScatter oscatter[],VecScatter gscatter[])
308: {
309: PetscInt i;
311: SNES_NASM *nasm = (SNES_NASM*)snes->data;
314: if (snes->setupcalled) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"SNESNASMSetSubdomains() should be called before calling SNESSetUp().");
316: /* tear down the previously set things */
317: SNESReset(snes);
319: nasm->n = n;
320: if (oscatter) {
321: for (i=0; i<n; i++) {PetscObjectReference((PetscObject)oscatter[i]);}
322: }
323: if (iscatter) {
324: for (i=0; i<n; i++) {PetscObjectReference((PetscObject)iscatter[i]);}
325: }
326: if (gscatter) {
327: for (i=0; i<n; i++) {PetscObjectReference((PetscObject)gscatter[i]);}
328: }
329: if (oscatter) {
330: PetscMalloc1(n,&nasm->oscatter);
331: for (i=0; i<n; i++) {
332: nasm->oscatter[i] = oscatter[i];
333: }
334: }
335: if (iscatter) {
336: PetscMalloc1(n,&nasm->iscatter);
337: for (i=0; i<n; i++) {
338: nasm->iscatter[i] = iscatter[i];
339: }
340: }
341: if (gscatter) {
342: PetscMalloc1(n,&nasm->gscatter);
343: for (i=0; i<n; i++) {
344: nasm->gscatter[i] = gscatter[i];
345: }
346: }
348: if (subsnes) {
349: PetscMalloc1(n,&nasm->subsnes);
350: for (i=0; i<n; i++) {
351: nasm->subsnes[i] = subsnes[i];
352: }
353: nasm->same_local_solves = PETSC_FALSE;
354: }
355: return(0);
356: }
360: /*@
361: SNESNASMGetSubdomains - Get the local subdomain context.
363: Not Collective
365: Input Parameters:
366: . SNES - the SNES context
368: Output Parameters:
369: + n - the number of local subdomains
370: . subsnes - solvers defined on the local subdomains
371: . iscatter - scatters into the nonoverlapping portions of the local subdomains
372: . oscatter - scatters into the overlapping portions of the local subdomains
373: - gscatter - scatters into the (ghosted) local vector of the local subdomain
375: Level: intermediate
377: .keywords: SNES, NASM
379: .seealso: SNESNASM, SNESNASMSetSubdomains()
380: @*/
381: PetscErrorCode SNESNASMGetSubdomains(SNES snes,PetscInt *n,SNES *subsnes[],VecScatter *iscatter[],VecScatter *oscatter[],VecScatter *gscatter[])
382: {
384: PetscErrorCode (*f)(SNES,PetscInt*,SNES**,VecScatter**,VecScatter**,VecScatter**);
387: PetscObjectQueryFunction((PetscObject)snes,"SNESNASMGetSubdomains_C",&f);
388: if (f) {(f)(snes,n,subsnes,iscatter,oscatter,gscatter);}
389: return(0);
390: }
394: PetscErrorCode SNESNASMGetSubdomains_NASM(SNES snes,PetscInt *n,SNES *subsnes[],VecScatter *iscatter[],VecScatter *oscatter[],VecScatter *gscatter[])
395: {
396: SNES_NASM *nasm = (SNES_NASM*)snes->data;
399: if (n) *n = nasm->n;
400: if (oscatter) *oscatter = nasm->oscatter;
401: if (iscatter) *iscatter = nasm->iscatter;
402: if (gscatter) *gscatter = nasm->gscatter;
403: if (subsnes) {
404: *subsnes = nasm->subsnes;
405: nasm->same_local_solves = PETSC_FALSE;
406: }
407: return(0);
408: }
412: /*@
413: SNESNASMGetSubdomainVecs - Get the processor-local subdomain vectors
415: Not Collective
417: Input Parameters:
418: . SNES - the SNES context
420: Output Parameters:
421: + n - the number of local subdomains
422: . x - The subdomain solution vector
423: . y - The subdomain step vector
424: . b - The subdomain RHS vector
425: - xl - The subdomain local vectors (ghosted)
427: Level: developer
429: .keywords: SNES, NASM
431: .seealso: SNESNASM, SNESNASMGetSubdomains()
432: @*/
433: PetscErrorCode SNESNASMGetSubdomainVecs(SNES snes,PetscInt *n,Vec **x,Vec **y,Vec **b, Vec **xl)
434: {
436: PetscErrorCode (*f)(SNES,PetscInt*,Vec**,Vec**,Vec**,Vec**);
439: PetscObjectQueryFunction((PetscObject)snes,"SNESNASMGetSubdomainVecs_C",&f);
440: if (f) {(f)(snes,n,x,y,b,xl);}
441: return(0);
442: }
446: PetscErrorCode SNESNASMGetSubdomainVecs_NASM(SNES snes,PetscInt *n,Vec **x,Vec **y,Vec **b,Vec **xl)
447: {
448: SNES_NASM *nasm = (SNES_NASM*)snes->data;
451: if (n) *n = nasm->n;
452: if (x) *x = nasm->x;
453: if (y) *y = nasm->y;
454: if (b) *b = nasm->b;
455: if (xl) *xl = nasm->xl;
456: return(0);
457: }
461: /*@
462: SNESNASMSetComputeFinalJacobian - Schedules the computation of the global and subdomain jacobians upon convergence
464: Collective on SNES
466: Input Parameters:
467: + SNES - the SNES context
468: - flg - indication of whether to compute the jacobians or not
470: Level: developer
472: Notes: This is used almost exclusively in the implementation of ASPIN, where the converged subdomain and global jacobian
473: is needed at each linear iteration.
475: .keywords: SNES, NASM, ASPIN
477: .seealso: SNESNASM, SNESNASMGetSubdomains()
478: @*/
479: PetscErrorCode SNESNASMSetComputeFinalJacobian(SNES snes,PetscBool flg)
480: {
481: PetscErrorCode (*f)(SNES,PetscBool);
485: PetscObjectQueryFunction((PetscObject)snes,"SNESNASMSetComputeFinalJacobian_C",&f);
486: if (f) {(f)(snes,flg);}
487: return(0);
488: }
492: PetscErrorCode SNESNASMSetComputeFinalJacobian_NASM(SNES snes,PetscBool flg)
493: {
494: SNES_NASM *nasm = (SNES_NASM*)snes->data;
497: nasm->finaljacobian = flg;
498: if (flg) snes->usesksp = PETSC_TRUE;
499: return(0);
500: }
504: /*@
505: SNESNASMSetDamping - Sets the update damping for NASM
507: Logically collective on SNES
509: Input Parameters:
510: + SNES - the SNES context
511: - dmp - damping
513: Level: intermediate
515: .keywords: SNES, NASM, damping
517: .seealso: SNESNASM, SNESNASMGetDamping()
518: @*/
519: PetscErrorCode SNESNASMSetDamping(SNES snes,PetscReal dmp)
520: {
521: PetscErrorCode (*f)(SNES,PetscReal);
525: PetscObjectQueryFunction((PetscObject)snes,"SNESNASMSetDamping_C",(void (**)(void))&f);
526: if (f) {(f)(snes,dmp);}
527: return(0);
528: }
532: PetscErrorCode SNESNASMSetDamping_NASM(SNES snes,PetscReal dmp)
533: {
534: SNES_NASM *nasm = (SNES_NASM*)snes->data;
537: nasm->damping = dmp;
538: return(0);
539: }
543: /*@
544: SNESNASMGetDamping - Gets the update damping for NASM
546: Not Collective
548: Input Parameters:
549: + SNES - the SNES context
550: - dmp - damping
552: Level: intermediate
554: .keywords: SNES, NASM, damping
556: .seealso: SNESNASM, SNESNASMSetDamping()
557: @*/
558: PetscErrorCode SNESNASMGetDamping(SNES snes,PetscReal *dmp)
559: {
560: PetscErrorCode (*f)(SNES,PetscReal*);
564: PetscObjectQueryFunction((PetscObject)snes,"SNESNASMGetDamping_C",(void (**)(void))&f);
565: if (f) {(f)(snes,dmp);}
566: return(0);
567: }
571: PetscErrorCode SNESNASMGetDamping_NASM(SNES snes,PetscReal *dmp)
572: {
573: SNES_NASM *nasm = (SNES_NASM*)snes->data;
576: *dmp = nasm->damping;
577: return(0);
578: }
583: PetscErrorCode SNESNASMSolveLocal_Private(SNES snes,Vec B,Vec Y,Vec X)
584: {
585: SNES_NASM *nasm = (SNES_NASM*)snes->data;
586: SNES subsnes;
587: PetscInt i;
588: PetscReal dmp;
590: Vec Xlloc,Xl,Bl,Yl;
591: VecScatter iscat,oscat,gscat;
592: DM dm,subdm;
595: SNESGetDM(snes,&dm);
596: SNESNASMGetDamping(snes,&dmp);
597: VecSet(Y,0);
598: if (nasm->eventrestrictinterp) {PetscLogEventBegin(nasm->eventrestrictinterp,snes,0,0,0);}
599: for (i=0; i<nasm->n; i++) {
600: /* scatter the solution to the local solution */
601: Xlloc = nasm->xl[i];
602: gscat = nasm->gscatter[i];
603: oscat = nasm->oscatter[i];
604: VecScatterBegin(gscat,X,Xlloc,INSERT_VALUES,SCATTER_FORWARD);
605: if (B) {
606: /* scatter the RHS to the local RHS */
607: Bl = nasm->b[i];
608: VecScatterBegin(oscat,B,Bl,INSERT_VALUES,SCATTER_FORWARD);
609: }
610: }
611: if (nasm->eventrestrictinterp) {PetscLogEventEnd(nasm->eventrestrictinterp,snes,0,0,0);}
614: if (nasm->eventsubsolve) {PetscLogEventBegin(nasm->eventsubsolve,snes,0,0,0);}
615: for (i=0; i<nasm->n; i++) {
616: Xl = nasm->x[i];
617: Xlloc = nasm->xl[i];
618: Yl = nasm->y[i];
619: subsnes = nasm->subsnes[i];
620: SNESGetDM(subsnes,&subdm);
621: iscat = nasm->iscatter[i];
622: oscat = nasm->oscatter[i];
623: gscat = nasm->gscatter[i];
624: VecScatterEnd(gscat,X,Xlloc,INSERT_VALUES,SCATTER_FORWARD);
625: if (B) {
626: Bl = nasm->b[i];
627: VecScatterEnd(oscat,B,Bl,INSERT_VALUES,SCATTER_FORWARD);
628: } else Bl = NULL;
629: DMSubDomainRestrict(dm,oscat,gscat,subdm);
630: DMLocalToGlobalBegin(subdm,Xlloc,INSERT_VALUES,Xl);
631: DMLocalToGlobalEnd(subdm,Xlloc,INSERT_VALUES,Xl);
632: VecCopy(Xl,Yl);
633: SNESSolve(subsnes,Bl,Xl);
634: VecAYPX(Yl,-1.0,Xl);
635: if (nasm->type == PC_ASM_BASIC) {
636: VecScatterBegin(oscat,Yl,Y,ADD_VALUES,SCATTER_REVERSE);
637: } else if (nasm->type == PC_ASM_RESTRICT) {
638: VecScatterBegin(iscat,Yl,Y,ADD_VALUES,SCATTER_REVERSE);
639: } else SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"Only basic and restrict types are supported for SNESNASM");
640: }
641: if (nasm->eventsubsolve) {PetscLogEventEnd(nasm->eventsubsolve,snes,0,0,0);}
642: if (nasm->eventrestrictinterp) {PetscLogEventBegin(nasm->eventrestrictinterp,snes,0,0,0);}
643: for (i=0; i<nasm->n; i++) {
644: Yl = nasm->y[i];
645: iscat = nasm->iscatter[i];
646: oscat = nasm->oscatter[i];
647: if (nasm->type == PC_ASM_BASIC) {
648: VecScatterEnd(oscat,Yl,Y,ADD_VALUES,SCATTER_REVERSE);
649: } else if (nasm->type == PC_ASM_RESTRICT) {
650: VecScatterEnd(iscat,Yl,Y,ADD_VALUES,SCATTER_REVERSE);
651: } else SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"Only basic and restrict types are supported for SNESNASM");
652: }
653: if (nasm->eventrestrictinterp) {PetscLogEventEnd(nasm->eventrestrictinterp,snes,0,0,0);}
654: VecAXPY(X,dmp,Y);
655: return(0);
656: }
660: PetscErrorCode SNESNASMComputeFinalJacobian_Private(SNES snes, Vec Xfinal)
661: {
662: Vec X = Xfinal;
663: SNES_NASM *nasm = (SNES_NASM*)snes->data;
664: SNES subsnes;
665: PetscInt i,lag = 1;
667: Vec Xlloc,Xl,Fl,F;
668: VecScatter oscat,gscat;
669: DM dm,subdm;
670: MatStructure flg = DIFFERENT_NONZERO_PATTERN;
672: if (nasm->fjtype == 2) X = nasm->xinit;
673: F = snes->vec_func;
674: if (snes->normschedule == SNES_NORM_NONE) {SNESComputeFunction(snes,X,F);}
675: SNESComputeJacobian(snes,X,&snes->jacobian,&snes->jacobian_pre,&flg);
676: SNESGetDM(snes,&dm);
677: if (nasm->eventrestrictinterp) {PetscLogEventBegin(nasm->eventrestrictinterp,snes,0,0,0);}
678: if (nasm->fjtype != 1) {
679: for (i=0; i<nasm->n; i++) {
680: Xlloc = nasm->xl[i];
681: gscat = nasm->gscatter[i];
682: oscat = nasm->oscatter[i];
683: VecScatterBegin(gscat,X,Xlloc,INSERT_VALUES,SCATTER_FORWARD);
684: }
685: }
686: if (nasm->eventrestrictinterp) {PetscLogEventEnd(nasm->eventrestrictinterp,snes,0,0,0);}
687: for (i=0; i<nasm->n; i++) {
688: Fl = nasm->subsnes[i]->vec_func;
689: Xl = nasm->x[i];
690: Xlloc = nasm->xl[i];
691: subsnes = nasm->subsnes[i];
692: oscat = nasm->oscatter[i];
693: gscat = nasm->gscatter[i];
694: if (nasm->fjtype != 1) {VecScatterEnd(gscat,X,Xlloc,INSERT_VALUES,SCATTER_FORWARD);}
695: SNESGetDM(subsnes,&subdm);
696: DMSubDomainRestrict(dm,oscat,gscat,subdm);
697: if (nasm->fjtype != 1) {
698: DMLocalToGlobalBegin(subdm,Xlloc,INSERT_VALUES,Xl);
699: DMLocalToGlobalEnd(subdm,Xlloc,INSERT_VALUES,Xl);
700: }
701: if (subsnes->lagjacobian == -1) subsnes->lagjacobian = -2;
702: else if (subsnes->lagjacobian > 1) lag = subsnes->lagjacobian;
703: SNESComputeFunction(subsnes,Xl,Fl);
704: SNESComputeJacobian(subsnes,Xl,&subsnes->jacobian,&subsnes->jacobian_pre,&flg);
705: if (lag > 1) subsnes->lagjacobian = lag;
706: KSPSetOperators(subsnes->ksp,subsnes->jacobian,subsnes->jacobian_pre,flg);
707: }
708: return(0);
709: }
713: PetscErrorCode SNESSolve_NASM(SNES snes)
714: {
715: Vec F;
716: Vec X;
717: Vec B;
718: Vec Y;
719: PetscInt i;
720: PetscReal fnorm = 0.0;
721: PetscErrorCode ierr;
722: SNESNormSchedule normschedule;
723: SNES_NASM *nasm = (SNES_NASM*)snes->data;
726: X = snes->vec_sol;
727: Y = snes->vec_sol_update;
728: F = snes->vec_func;
729: B = snes->vec_rhs;
731: PetscObjectSAWsTakeAccess((PetscObject)snes);
732: snes->iter = 0;
733: snes->norm = 0.;
734: PetscObjectSAWsGrantAccess((PetscObject)snes);
735: snes->reason = SNES_CONVERGED_ITERATING;
736: SNESGetNormSchedule(snes, &normschedule);
737: if (normschedule == SNES_NORM_ALWAYS || normschedule == SNES_NORM_INITIAL_ONLY || normschedule == SNES_NORM_INITIAL_FINAL_ONLY) {
738: /* compute the initial function and preconditioned update delX */
739: if (!snes->vec_func_init_set) {
740: SNESComputeFunction(snes,X,F);
741: if (snes->domainerror) {
742: snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
743: return(0);
744: }
745: } else snes->vec_func_init_set = PETSC_FALSE;
747: VecNorm(F, NORM_2, &fnorm); /* fnorm <- ||F|| */
748: if (PetscIsInfOrNanReal(fnorm)) {
749: snes->reason = SNES_DIVERGED_FNORM_NAN;
750: return(0);
751: }
752: PetscObjectSAWsTakeAccess((PetscObject)snes);
753: snes->iter = 0;
754: snes->norm = fnorm;
755: PetscObjectSAWsGrantAccess((PetscObject)snes);
756: SNESLogConvergenceHistory(snes,snes->norm,0);
757: SNESMonitor(snes,0,snes->norm);
759: /* test convergence */
760: (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
761: if (snes->reason) return(0);
762: } else {
763: PetscObjectSAWsGrantAccess((PetscObject)snes);
764: SNESLogConvergenceHistory(snes,snes->norm,0);
765: SNESMonitor(snes,0,snes->norm);
766: }
768: /* Call general purpose update function */
769: if (snes->ops->update) {
770: (*snes->ops->update)(snes, snes->iter);
771: }
772: /* copy the initial solution over for later */
773: if (nasm->fjtype == 2) {VecCopy(X,nasm->xinit);}
775: for (i = 0; i < snes->max_its; i++) {
776: SNESNASMSolveLocal_Private(snes,B,Y,X);
777: if (normschedule == SNES_NORM_ALWAYS || ((i == snes->max_its - 1) && (normschedule == SNES_NORM_INITIAL_FINAL_ONLY || normschedule == SNES_NORM_FINAL_ONLY))) {
778: SNESComputeFunction(snes,X,F);
779: if (snes->domainerror) {
780: snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
781: break;
782: }
783: VecNorm(F, NORM_2, &fnorm); /* fnorm <- ||F|| */
784: if (PetscIsInfOrNanReal(fnorm)) {
785: snes->reason = SNES_DIVERGED_FNORM_NAN;
786: break;
787: }
788: }
789: /* Monitor convergence */
790: PetscObjectSAWsTakeAccess((PetscObject)snes);
791: snes->iter = i+1;
792: snes->norm = fnorm;
793: PetscObjectSAWsGrantAccess((PetscObject)snes);
794: SNESLogConvergenceHistory(snes,snes->norm,0);
795: SNESMonitor(snes,snes->iter,snes->norm);
796: /* Test for convergence */
797: if (normschedule == SNES_NORM_ALWAYS) {(*snes->ops->converged)(snes,snes->iter,0.0,0.0,fnorm,&snes->reason,snes->cnvP);}
798: if (snes->reason) break;
799: /* Call general purpose update function */
800: if (snes->ops->update) {(*snes->ops->update)(snes, snes->iter);}
801: }
802: if (nasm->finaljacobian) {SNESNASMComputeFinalJacobian_Private(snes,X);}
803: if (normschedule == SNES_NORM_ALWAYS) {
804: if (i == snes->max_its) {
805: PetscInfo1(snes,"Maximum number of iterations has been reached: %D\n",snes->max_its);
806: if (!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT;
807: }
808: } else if (!snes->reason) snes->reason = SNES_CONVERGED_ITS; /* NASM is meant to be used as a preconditioner */
809: return(0);
810: }
812: /*MC
813: SNESNASM - Nonlinear Additive Schwartz
815: Options Database:
816: + -snes_nasm_log - enable logging events for the communication and solve stages
817: . -snes_nasm_type <basic,restrict> - type of subdomain update used
818: . -snes_nasm_finaljacobian - compute the local and global jacobians of the final iterate
819: . -snes_nasm_finaljacobian_type <finalinner,finalouter,initial> pick state the jacobian is calculated at
820: . -sub_snes_ - options prefix of the subdomain nonlinear solves
821: . -sub_ksp_ - options prefix of the subdomain Krylov solver
822: - -sub_pc_ - options prefix of the subdomain preconditioner
824: Level: advanced
826: .seealso: SNESCreate(), SNES, SNESSetType(), SNESType (for list of available types)
827: M*/
831: PETSC_EXTERN PetscErrorCode SNESCreate_NASM(SNES snes)
832: {
833: SNES_NASM *nasm;
837: PetscNewLog(snes,&nasm);
838: snes->data = (void*)nasm;
840: nasm->n = PETSC_DECIDE;
841: nasm->subsnes = 0;
842: nasm->x = 0;
843: nasm->xl = 0;
844: nasm->y = 0;
845: nasm->b = 0;
846: nasm->oscatter = 0;
847: nasm->iscatter = 0;
848: nasm->gscatter = 0;
849: nasm->damping = 1.;
851: nasm->type = PC_ASM_BASIC;
852: nasm->finaljacobian = PETSC_FALSE;
853: nasm->same_local_solves = PETSC_TRUE;
855: snes->ops->destroy = SNESDestroy_NASM;
856: snes->ops->setup = SNESSetUp_NASM;
857: snes->ops->setfromoptions = SNESSetFromOptions_NASM;
858: snes->ops->view = SNESView_NASM;
859: snes->ops->solve = SNESSolve_NASM;
860: snes->ops->reset = SNESReset_NASM;
862: snes->usesksp = PETSC_FALSE;
863: snes->usespc = PETSC_FALSE;
865: nasm->fjtype = 0;
866: nasm->xinit = NULL;
867: nasm->eventrestrictinterp = 0;
868: nasm->eventsubsolve = 0;
870: if (!snes->tolerancesset) {
871: snes->max_its = 10000;
872: snes->max_funcs = 10000;
873: }
875: PetscObjectComposeFunction((PetscObject)snes,"SNESNASMSetSubdomains_C",SNESNASMSetSubdomains_NASM);
876: PetscObjectComposeFunction((PetscObject)snes,"SNESNASMGetSubdomains_C",SNESNASMGetSubdomains_NASM);
877: PetscObjectComposeFunction((PetscObject)snes,"SNESNASMSetDamping_C",SNESNASMSetDamping_NASM);
878: PetscObjectComposeFunction((PetscObject)snes,"SNESNASMGetDamping_C",SNESNASMGetDamping_NASM);
879: PetscObjectComposeFunction((PetscObject)snes,"SNESNASMGetSubdomainVecs_C",SNESNASMGetSubdomainVecs_NASM);
880: PetscObjectComposeFunction((PetscObject)snes,"SNESNASMSetComputeFinalJacobian_C",SNESNASMSetComputeFinalJacobian_NASM);
881: return(0);
882: }