Actual source code: nasm.c

petsc-3.7.0 2016-04-25
Report Typos and Errors
  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:       DMGlobalToLocalHookAdd(subdm,DMGlobalToLocalSubDomainDirichletHook_Private,NULL,nasm->xl[i]);
160:     }
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(PetscOptionItems *PetscOptionsObject,SNES snes)
177: {
178:   PetscErrorCode    ierr;
179:   PCASMType         asmtype;
180:   PetscBool         flg,monflg,subviewflg;
181:   SNES_NASM         *nasm = (SNES_NASM*)snes->data;

184:   PetscOptionsHead(PetscOptionsObject,"Nonlinear Additive Schwartz options");
185:   PetscOptionsEnum("-snes_nasm_type","Type of restriction/extension","",SNESNASMTypes,(PetscEnum)nasm->type,(PetscEnum*)&asmtype,&flg);
186:   if (flg) {SNESNASMSetType(snes,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:   MPIU_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:         PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
236:         if (!rank) {
237:           PetscViewerASCIIPushTab(viewer);
238:           SNESView(nasm->subsnes[0],sviewer);
239:           PetscViewerASCIIPopTab(viewer);
240:         }
241:         PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
242:         PetscViewerASCIIPopTab(viewer);
243:       }
244:     } else {
245:       /* print the solver on each block */
246:       PetscViewerASCIIPushSynchronized(viewer);
247:       PetscViewerASCIISynchronizedPrintf(viewer,"  [%d] number of local blocks = %D\n",(int)rank,nasm->n);
248:       PetscViewerFlush(viewer);
249:       PetscViewerASCIIPopSynchronized(viewer);
250:       PetscViewerASCIIPrintf(viewer,"  Local solve info for each block is in the following SNES objects:\n");
251:       PetscViewerASCIIPushTab(viewer);
252:       PetscViewerASCIIPrintf(viewer,"- - - - - - - - - - - - - - - - - -\n");
253:       PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&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:       PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
261:       PetscViewerFlush(viewer);
262:       PetscViewerASCIIPopTab(viewer);
263:     }
264:   } else if (isstring) {
265:     PetscViewerStringSPrintf(viewer," blocks=%D,type=%s",N,SNESNASMTypes[nasm->type]);
266:     PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
267:     if (nasm->subsnes && !rank) {SNESView(nasm->subsnes[0],sviewer);}
268:     PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
269:   }
270:   return(0);
271: }

275: /*@
276:    SNESNASMSetType - Set the type of subdomain update used

278:    Logically Collective on SNES

280:    Input Parameters:
281: +  SNES - the SNES context
282: -  type - the type of update, PC_ASM_BASIC or PC_ASM_RESTRICT

284:    Level: intermediate

286: .keywords: SNES, NASM

288: .seealso: SNESNASM, SNESNASMGetType(), PCASMSetType()
289: @*/
290: PetscErrorCode SNESNASMSetType(SNES snes,PCASMType type)
291: {
293:   PetscErrorCode (*f)(SNES,PCASMType);

296:   PetscObjectQueryFunction((PetscObject)snes,"SNESNASMSetType_C",&f);
297:   if (f) {(f)(snes,type);}
298:   return(0);
299: }

303: PetscErrorCode SNESNASMSetType_NASM(SNES snes,PCASMType type)
304: {
305:   SNES_NASM      *nasm = (SNES_NASM*)snes->data;

308:   if (type != PC_ASM_BASIC && type != PC_ASM_RESTRICT) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_OUTOFRANGE,"SNESNASM only supports basic and restrict types");
309:   nasm->type = type;
310:   return(0);
311: }

315: /*@
316:    SNESNASMGetType - Get the type of subdomain update used

318:    Logically Collective on SNES

320:    Input Parameters:
321: .  SNES - the SNES context

323:    Output Parameters:
324: .  type - the type of update

326:    Level: intermediate

328: .keywords: SNES, NASM

330: .seealso: SNESNASM, SNESNASMSetType(), PCASMGetType()
331: @*/
332: PetscErrorCode SNESNASMGetType(SNES snes,PCASMType *type)
333: {
335:   PetscErrorCode (*f)(SNES,PCASMType*);

338:   PetscObjectQueryFunction((PetscObject)snes,"SNESNASMGetType_C",&f);
339:   if (f) {(f)(snes,type);}
340:   return(0);
341: }

345: PetscErrorCode SNESNASMGetType_NASM(SNES snes,PCASMType *type)
346: {
347:   SNES_NASM      *nasm = (SNES_NASM*)snes->data;

350:   *type = nasm->type;
351:   return(0);
352: }

356: /*@
357:    SNESNASMSetSubdomains - Manually Set the context required to restrict and solve subdomain problems.

359:    Not Collective

361:    Input Parameters:
362: +  SNES - the SNES context
363: .  n - the number of local subdomains
364: .  subsnes - solvers defined on the local subdomains
365: .  iscatter - scatters into the nonoverlapping portions of the local subdomains
366: .  oscatter - scatters into the overlapping portions of the local subdomains
367: -  gscatter - scatters into the (ghosted) local vector of the local subdomain

369:    Level: intermediate

371: .keywords: SNES, NASM

373: .seealso: SNESNASM, SNESNASMGetSubdomains()
374: @*/
375: PetscErrorCode SNESNASMSetSubdomains(SNES snes,PetscInt n,SNES subsnes[],VecScatter iscatter[],VecScatter oscatter[],VecScatter gscatter[])
376: {
378:   PetscErrorCode (*f)(SNES,PetscInt,SNES*,VecScatter*,VecScatter*,VecScatter*);

381:   PetscObjectQueryFunction((PetscObject)snes,"SNESNASMSetSubdomains_C",&f);
382:   if (f) {(f)(snes,n,subsnes,iscatter,oscatter,gscatter);}
383:   return(0);
384: }

388: PetscErrorCode SNESNASMSetSubdomains_NASM(SNES snes,PetscInt n,SNES subsnes[],VecScatter iscatter[],VecScatter oscatter[],VecScatter gscatter[])
389: {
390:   PetscInt       i;
392:   SNES_NASM      *nasm = (SNES_NASM*)snes->data;

395:   if (snes->setupcalled) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"SNESNASMSetSubdomains() should be called before calling SNESSetUp().");

397:   /* tear down the previously set things */
398:   SNESReset(snes);

400:   nasm->n = n;
401:   if (oscatter) {
402:     for (i=0; i<n; i++) {PetscObjectReference((PetscObject)oscatter[i]);}
403:   }
404:   if (iscatter) {
405:     for (i=0; i<n; i++) {PetscObjectReference((PetscObject)iscatter[i]);}
406:   }
407:   if (gscatter) {
408:     for (i=0; i<n; i++) {PetscObjectReference((PetscObject)gscatter[i]);}
409:   }
410:   if (oscatter) {
411:     PetscMalloc1(n,&nasm->oscatter);
412:     for (i=0; i<n; i++) {
413:       nasm->oscatter[i] = oscatter[i];
414:     }
415:   }
416:   if (iscatter) {
417:     PetscMalloc1(n,&nasm->iscatter);
418:     for (i=0; i<n; i++) {
419:       nasm->iscatter[i] = iscatter[i];
420:     }
421:   }
422:   if (gscatter) {
423:     PetscMalloc1(n,&nasm->gscatter);
424:     for (i=0; i<n; i++) {
425:       nasm->gscatter[i] = gscatter[i];
426:     }
427:   }

429:   if (subsnes) {
430:     PetscMalloc1(n,&nasm->subsnes);
431:     for (i=0; i<n; i++) {
432:       nasm->subsnes[i] = subsnes[i];
433:     }
434:     nasm->same_local_solves = PETSC_FALSE;
435:   }
436:   return(0);
437: }

441: /*@
442:    SNESNASMGetSubdomains - Get the local subdomain context.

444:    Not Collective

446:    Input Parameters:
447: .  SNES - the SNES context

449:    Output Parameters:
450: +  n - the number of local subdomains
451: .  subsnes - solvers defined on the local subdomains
452: .  iscatter - scatters into the nonoverlapping portions of the local subdomains
453: .  oscatter - scatters into the overlapping portions of the local subdomains
454: -  gscatter - scatters into the (ghosted) local vector of the local subdomain

456:    Level: intermediate

458: .keywords: SNES, NASM

460: .seealso: SNESNASM, SNESNASMSetSubdomains()
461: @*/
462: PetscErrorCode SNESNASMGetSubdomains(SNES snes,PetscInt *n,SNES *subsnes[],VecScatter *iscatter[],VecScatter *oscatter[],VecScatter *gscatter[])
463: {
465:   PetscErrorCode (*f)(SNES,PetscInt*,SNES**,VecScatter**,VecScatter**,VecScatter**);

468:   PetscObjectQueryFunction((PetscObject)snes,"SNESNASMGetSubdomains_C",&f);
469:   if (f) {(f)(snes,n,subsnes,iscatter,oscatter,gscatter);}
470:   return(0);
471: }

475: PetscErrorCode SNESNASMGetSubdomains_NASM(SNES snes,PetscInt *n,SNES *subsnes[],VecScatter *iscatter[],VecScatter *oscatter[],VecScatter *gscatter[])
476: {
477:   SNES_NASM      *nasm = (SNES_NASM*)snes->data;

480:   if (n) *n = nasm->n;
481:   if (oscatter) *oscatter = nasm->oscatter;
482:   if (iscatter) *iscatter = nasm->iscatter;
483:   if (gscatter) *gscatter = nasm->gscatter;
484:   if (subsnes)  {
485:     *subsnes  = nasm->subsnes;
486:     nasm->same_local_solves = PETSC_FALSE;
487:   }
488:   return(0);
489: }

493: /*@
494:    SNESNASMGetSubdomainVecs - Get the processor-local subdomain vectors

496:    Not Collective

498:    Input Parameters:
499: .  SNES - the SNES context

501:    Output Parameters:
502: +  n - the number of local subdomains
503: .  x - The subdomain solution vector
504: .  y - The subdomain step vector
505: .  b - The subdomain RHS vector
506: -  xl - The subdomain local vectors (ghosted)

508:    Level: developer

510: .keywords: SNES, NASM

512: .seealso: SNESNASM, SNESNASMGetSubdomains()
513: @*/
514: PetscErrorCode SNESNASMGetSubdomainVecs(SNES snes,PetscInt *n,Vec **x,Vec **y,Vec **b, Vec **xl)
515: {
517:   PetscErrorCode (*f)(SNES,PetscInt*,Vec**,Vec**,Vec**,Vec**);

520:   PetscObjectQueryFunction((PetscObject)snes,"SNESNASMGetSubdomainVecs_C",&f);
521:   if (f) {(f)(snes,n,x,y,b,xl);}
522:   return(0);
523: }

527: PetscErrorCode SNESNASMGetSubdomainVecs_NASM(SNES snes,PetscInt *n,Vec **x,Vec **y,Vec **b,Vec **xl)
528: {
529:   SNES_NASM      *nasm = (SNES_NASM*)snes->data;

532:   if (n)  *n  = nasm->n;
533:   if (x)  *x  = nasm->x;
534:   if (y)  *y  = nasm->y;
535:   if (b)  *b  = nasm->b;
536:   if (xl) *xl = nasm->xl;
537:   return(0);
538: }

542: /*@
543:    SNESNASMSetComputeFinalJacobian - Schedules the computation of the global and subdomain jacobians upon convergence

545:    Collective on SNES

547:    Input Parameters:
548: +  SNES - the SNES context
549: -  flg - indication of whether to compute the jacobians or not

551:    Level: developer

553:    Notes: This is used almost exclusively in the implementation of ASPIN, where the converged subdomain and global jacobian
554:    is needed at each linear iteration.

556: .keywords: SNES, NASM, ASPIN

558: .seealso: SNESNASM, SNESNASMGetSubdomains()
559: @*/
560: PetscErrorCode SNESNASMSetComputeFinalJacobian(SNES snes,PetscBool flg)
561: {
562:   PetscErrorCode (*f)(SNES,PetscBool);

566:   PetscObjectQueryFunction((PetscObject)snes,"SNESNASMSetComputeFinalJacobian_C",&f);
567:   if (f) {(f)(snes,flg);}
568:   return(0);
569: }

573: PetscErrorCode SNESNASMSetComputeFinalJacobian_NASM(SNES snes,PetscBool flg)
574: {
575:   SNES_NASM      *nasm = (SNES_NASM*)snes->data;

578:   nasm->finaljacobian = flg;
579:   if (flg) snes->usesksp = PETSC_TRUE;
580:   return(0);
581: }

585: /*@
586:    SNESNASMSetDamping - Sets the update damping for NASM

588:    Logically collective on SNES

590:    Input Parameters:
591: +  SNES - the SNES context
592: -  dmp - damping

594:    Level: intermediate

596: .keywords: SNES, NASM, damping

598: .seealso: SNESNASM, SNESNASMGetDamping()
599: @*/
600: PetscErrorCode SNESNASMSetDamping(SNES snes,PetscReal dmp)
601: {
602:   PetscErrorCode (*f)(SNES,PetscReal);

606:   PetscObjectQueryFunction((PetscObject)snes,"SNESNASMSetDamping_C",(void (**)(void))&f);
607:   if (f) {(f)(snes,dmp);}
608:   return(0);
609: }

613: PetscErrorCode SNESNASMSetDamping_NASM(SNES snes,PetscReal dmp)
614: {
615:   SNES_NASM      *nasm = (SNES_NASM*)snes->data;

618:   nasm->damping = dmp;
619:   return(0);
620: }

624: /*@
625:    SNESNASMGetDamping - Gets the update damping for NASM

627:    Not Collective

629:    Input Parameters:
630: +  SNES - the SNES context
631: -  dmp - damping

633:    Level: intermediate

635: .keywords: SNES, NASM, damping

637: .seealso: SNESNASM, SNESNASMSetDamping()
638: @*/
639: PetscErrorCode SNESNASMGetDamping(SNES snes,PetscReal *dmp)
640: {
641:   PetscErrorCode (*f)(SNES,PetscReal*);

645:   PetscObjectQueryFunction((PetscObject)snes,"SNESNASMGetDamping_C",(void (**)(void))&f);
646:   if (f) {(f)(snes,dmp);}
647:   return(0);
648: }

652: PetscErrorCode SNESNASMGetDamping_NASM(SNES snes,PetscReal *dmp)
653: {
654:   SNES_NASM      *nasm = (SNES_NASM*)snes->data;

657:   *dmp = nasm->damping;
658:   return(0);
659: }


664: /*
665:   Input Parameters:
666: + snes - The solver
667: . B - The RHS vector
668: - X - The initial guess

670:   Output Parameters:
671: . Y - The solution update

673:   TODO: All scatters should be packed into one
674: */
675: PetscErrorCode SNESNASMSolveLocal_Private(SNES snes,Vec B,Vec Y,Vec X)
676: {
677:   SNES_NASM      *nasm = (SNES_NASM*)snes->data;
678:   SNES           subsnes;
679:   PetscInt       i;
680:   PetscReal      dmp;
682:   Vec            Xlloc,Xl,Bl,Yl;
683:   VecScatter     iscat,oscat,gscat;
684:   DM             dm,subdm;
685:   PCASMType      type;

688:   SNESNASMGetType(snes,&type);
689:   SNESGetDM(snes,&dm);
690:   SNESNASMGetDamping(snes,&dmp);
691:   VecSet(Y,0);
692:   if (nasm->eventrestrictinterp) {PetscLogEventBegin(nasm->eventrestrictinterp,snes,0,0,0);}
693:   for (i=0; i<nasm->n; i++) {
694:     /* scatter the solution to the local solution */
695:     Xlloc = nasm->xl[i];
696:     gscat   = nasm->gscatter[i];
697:     oscat   = nasm->oscatter[i];
698:     VecScatterBegin(gscat,X,Xlloc,INSERT_VALUES,SCATTER_FORWARD);
699:     if (B) {
700:       /* scatter the RHS to the local RHS */
701:       Bl   = nasm->b[i];
702:       VecScatterBegin(oscat,B,Bl,INSERT_VALUES,SCATTER_FORWARD);
703:     }
704:   }
705:   if (nasm->eventrestrictinterp) {PetscLogEventEnd(nasm->eventrestrictinterp,snes,0,0,0);}


708:   if (nasm->eventsubsolve) {PetscLogEventBegin(nasm->eventsubsolve,snes,0,0,0);}
709:   for (i=0; i<nasm->n; i++) {
710:     Xl    = nasm->x[i];
711:     Xlloc = nasm->xl[i];
712:     Yl    = nasm->y[i];
713:     subsnes = nasm->subsnes[i];
714:     SNESGetDM(subsnes,&subdm);
715:     iscat   = nasm->iscatter[i];
716:     oscat   = nasm->oscatter[i];
717:     gscat   = nasm->gscatter[i];
718:     VecScatterEnd(gscat,X,Xlloc,INSERT_VALUES,SCATTER_FORWARD);
719:     if (B) {
720:       Bl   = nasm->b[i];
721:       VecScatterEnd(oscat,B,Bl,INSERT_VALUES,SCATTER_FORWARD);
722:     } else Bl = NULL;
723:     DMSubDomainRestrict(dm,oscat,gscat,subdm);
724:     /* Could scatter directly from X */
725:     DMLocalToGlobalBegin(subdm,Xlloc,INSERT_VALUES,Xl);
726:     DMLocalToGlobalEnd(subdm,Xlloc,INSERT_VALUES,Xl);
727:     VecCopy(Xl,Yl);
728:     SNESSolve(subsnes,Bl,Xl);
729:     VecAYPX(Yl,-1.0,Xl);
730:     if (type == PC_ASM_BASIC) {
731:       VecScatterBegin(oscat,Yl,Y,ADD_VALUES,SCATTER_REVERSE);
732:     } else if (type == PC_ASM_RESTRICT) {
733:       VecScatterBegin(iscat,Yl,Y,ADD_VALUES,SCATTER_REVERSE);
734:     } else SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"Only basic and restrict types are supported for SNESNASM");
735:   }
736:   if (nasm->eventsubsolve) {PetscLogEventEnd(nasm->eventsubsolve,snes,0,0,0);}
737:   if (nasm->eventrestrictinterp) {PetscLogEventBegin(nasm->eventrestrictinterp,snes,0,0,0);}
738:   for (i=0; i<nasm->n; i++) {
739:     Yl    = nasm->y[i];
740:     iscat   = nasm->iscatter[i];
741:     oscat   = nasm->oscatter[i];
742:     if (type == PC_ASM_BASIC) {
743:       VecScatterEnd(oscat,Yl,Y,ADD_VALUES,SCATTER_REVERSE);
744:     } else if (type == PC_ASM_RESTRICT) {
745:       VecScatterEnd(iscat,Yl,Y,ADD_VALUES,SCATTER_REVERSE);
746:     } else SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE,"Only basic and restrict types are supported for SNESNASM");
747:   }
748:   if (nasm->eventrestrictinterp) {PetscLogEventEnd(nasm->eventrestrictinterp,snes,0,0,0);}
749:   VecAXPY(X,dmp,Y);
750:   return(0);
751: }

755: PetscErrorCode SNESNASMComputeFinalJacobian_Private(SNES snes, Vec Xfinal)
756: {
757:   Vec            X = Xfinal;
758:   SNES_NASM      *nasm = (SNES_NASM*)snes->data;
759:   SNES           subsnes;
760:   PetscInt       i,lag = 1;
762:   Vec            Xlloc,Xl,Fl,F;
763:   VecScatter     oscat,gscat;
764:   DM             dm,subdm;

767:   if (nasm->fjtype == 2) X = nasm->xinit;
768:   F = snes->vec_func;
769:   if (snes->normschedule == SNES_NORM_NONE) {SNESComputeFunction(snes,X,F);}
770:   SNESComputeJacobian(snes,X,snes->jacobian,snes->jacobian_pre);
771:   SNESGetDM(snes,&dm);
772:   if (nasm->eventrestrictinterp) {PetscLogEventBegin(nasm->eventrestrictinterp,snes,0,0,0);}
773:   if (nasm->fjtype != 1) {
774:     for (i=0; i<nasm->n; i++) {
775:       Xlloc = nasm->xl[i];
776:       gscat = nasm->gscatter[i];
777:       VecScatterBegin(gscat,X,Xlloc,INSERT_VALUES,SCATTER_FORWARD);
778:     }
779:   }
780:   if (nasm->eventrestrictinterp) {PetscLogEventEnd(nasm->eventrestrictinterp,snes,0,0,0);}
781:   for (i=0; i<nasm->n; i++) {
782:     Fl      = nasm->subsnes[i]->vec_func;
783:     Xl      = nasm->x[i];
784:     Xlloc   = nasm->xl[i];
785:     subsnes = nasm->subsnes[i];
786:     oscat   = nasm->oscatter[i];
787:     gscat   = nasm->gscatter[i];
788:     if (nasm->fjtype != 1) {VecScatterEnd(gscat,X,Xlloc,INSERT_VALUES,SCATTER_FORWARD);}
789:     SNESGetDM(subsnes,&subdm);
790:     DMSubDomainRestrict(dm,oscat,gscat,subdm);
791:     if (nasm->fjtype != 1) {
792:       DMLocalToGlobalBegin(subdm,Xlloc,INSERT_VALUES,Xl);
793:       DMLocalToGlobalEnd(subdm,Xlloc,INSERT_VALUES,Xl);
794:     }
795:     if (subsnes->lagjacobian == -1)    subsnes->lagjacobian = -2;
796:     else if (subsnes->lagjacobian > 1) lag = subsnes->lagjacobian;
797:     SNESComputeFunction(subsnes,Xl,Fl);
798:     SNESComputeJacobian(subsnes,Xl,subsnes->jacobian,subsnes->jacobian_pre);
799:     if (lag > 1) subsnes->lagjacobian = lag;
800:   }
801:   return(0);
802: }

806: PetscErrorCode SNESSolve_NASM(SNES snes)
807: {
808:   Vec              F;
809:   Vec              X;
810:   Vec              B;
811:   Vec              Y;
812:   PetscInt         i;
813:   PetscReal        fnorm = 0.0;
814:   PetscErrorCode   ierr;
815:   SNESNormSchedule normschedule;
816:   SNES_NASM        *nasm = (SNES_NASM*)snes->data;


820:   if (snes->xl || snes->xu || snes->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE, "SNES solver %s does not support bounds", ((PetscObject)snes)->type_name);

822:   PetscCitationsRegister(SNESCitation,&SNEScite);
823:   X = snes->vec_sol;
824:   Y = snes->vec_sol_update;
825:   F = snes->vec_func;
826:   B = snes->vec_rhs;

828:   PetscObjectSAWsTakeAccess((PetscObject)snes);
829:   snes->iter   = 0;
830:   snes->norm   = 0.;
831:   PetscObjectSAWsGrantAccess((PetscObject)snes);
832:   snes->reason = SNES_CONVERGED_ITERATING;
833:   SNESGetNormSchedule(snes, &normschedule);
834:   if (normschedule == SNES_NORM_ALWAYS || normschedule == SNES_NORM_INITIAL_ONLY || normschedule == SNES_NORM_INITIAL_FINAL_ONLY) {
835:     /* compute the initial function and preconditioned update delX */
836:     if (!snes->vec_func_init_set) {
837:       SNESComputeFunction(snes,X,F);
838:     } else snes->vec_func_init_set = PETSC_FALSE;

840:     VecNorm(F, NORM_2, &fnorm); /* fnorm <- ||F||  */
841:     SNESCheckFunctionNorm(snes,fnorm);
842:     PetscObjectSAWsTakeAccess((PetscObject)snes);
843:     snes->iter = 0;
844:     snes->norm = fnorm;
845:     PetscObjectSAWsGrantAccess((PetscObject)snes);
846:     SNESLogConvergenceHistory(snes,snes->norm,0);
847:     SNESMonitor(snes,0,snes->norm);

849:     /* test convergence */
850:     (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
851:     if (snes->reason) return(0);
852:   } else {
853:     PetscObjectSAWsGrantAccess((PetscObject)snes);
854:     SNESLogConvergenceHistory(snes,snes->norm,0);
855:     SNESMonitor(snes,0,snes->norm);
856:   }

858:   /* Call general purpose update function */
859:   if (snes->ops->update) {
860:     (*snes->ops->update)(snes, snes->iter);
861:   }
862:   /* copy the initial solution over for later */
863:   if (nasm->fjtype == 2) {VecCopy(X,nasm->xinit);}

865:   for (i = 0; i < snes->max_its; i++) {
866:     SNESNASMSolveLocal_Private(snes,B,Y,X);
867:     if (normschedule == SNES_NORM_ALWAYS || ((i == snes->max_its - 1) && (normschedule == SNES_NORM_INITIAL_FINAL_ONLY || normschedule == SNES_NORM_FINAL_ONLY))) {
868:       SNESComputeFunction(snes,X,F);
869:       VecNorm(F, NORM_2, &fnorm); /* fnorm <- ||F||  */
870:       SNESCheckFunctionNorm(snes,fnorm);
871:     }
872:     /* Monitor convergence */
873:     PetscObjectSAWsTakeAccess((PetscObject)snes);
874:     snes->iter = i+1;
875:     snes->norm = fnorm;
876:     PetscObjectSAWsGrantAccess((PetscObject)snes);
877:     SNESLogConvergenceHistory(snes,snes->norm,0);
878:     SNESMonitor(snes,snes->iter,snes->norm);
879:     /* Test for convergence */
880:     if (normschedule == SNES_NORM_ALWAYS) {(*snes->ops->converged)(snes,snes->iter,0.0,0.0,fnorm,&snes->reason,snes->cnvP);}
881:     if (snes->reason) break;
882:     /* Call general purpose update function */
883:     if (snes->ops->update) {(*snes->ops->update)(snes, snes->iter);}
884:   }
885:   if (nasm->finaljacobian) {SNESNASMComputeFinalJacobian_Private(snes,X);}
886:   if (normschedule == SNES_NORM_ALWAYS) {
887:     if (i == snes->max_its) {
888:       PetscInfo1(snes,"Maximum number of iterations has been reached: %D\n",snes->max_its);
889:       if (!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT;
890:     }
891:   } else if (!snes->reason) snes->reason = SNES_CONVERGED_ITS; /* NASM is meant to be used as a preconditioner */
892:   return(0);
893: }

895: /*MC
896:   SNESNASM - Nonlinear Additive Schwartz

898:    Options Database:
899: +  -snes_nasm_log - enable logging events for the communication and solve stages
900: .  -snes_nasm_type <basic,restrict> - type of subdomain update used
901: .  -snes_nasm_finaljacobian - compute the local and global jacobians of the final iterate
902: .  -snes_nasm_finaljacobian_type <finalinner,finalouter,initial> - pick state the jacobian is calculated at
903: .  -sub_snes_ - options prefix of the subdomain nonlinear solves
904: .  -sub_ksp_ - options prefix of the subdomain Krylov solver
905: -  -sub_pc_ - options prefix of the subdomain preconditioner

907:    Level: advanced

909:    References:
910: .  1. - Peter R. Brune, Matthew G. Knepley, Barry F. Smith, and Xuemin Tu, "Composing Scalable Nonlinear Algebraic Solvers",
911:    SIAM Review, 57(4), 2015

913: .seealso: SNESCreate(), SNES, SNESSetType(), SNESType (for list of available types)
914: M*/

918: PETSC_EXTERN PetscErrorCode SNESCreate_NASM(SNES snes)
919: {
920:   SNES_NASM      *nasm;

924:   PetscNewLog(snes,&nasm);
925:   snes->data = (void*)nasm;

927:   nasm->n        = PETSC_DECIDE;
928:   nasm->subsnes  = 0;
929:   nasm->x        = 0;
930:   nasm->xl       = 0;
931:   nasm->y        = 0;
932:   nasm->b        = 0;
933:   nasm->oscatter = 0;
934:   nasm->iscatter = 0;
935:   nasm->gscatter = 0;
936:   nasm->damping  = 1.;

938:   nasm->type = PC_ASM_BASIC;
939:   nasm->finaljacobian = PETSC_FALSE;
940:   nasm->same_local_solves = PETSC_TRUE;

942:   snes->ops->destroy        = SNESDestroy_NASM;
943:   snes->ops->setup          = SNESSetUp_NASM;
944:   snes->ops->setfromoptions = SNESSetFromOptions_NASM;
945:   snes->ops->view           = SNESView_NASM;
946:   snes->ops->solve          = SNESSolve_NASM;
947:   snes->ops->reset          = SNESReset_NASM;

949:   snes->usesksp = PETSC_FALSE;
950:   snes->usespc  = PETSC_FALSE;

952:   nasm->fjtype              = 0;
953:   nasm->xinit               = NULL;
954:   nasm->eventrestrictinterp = 0;
955:   nasm->eventsubsolve       = 0;

957:   if (!snes->tolerancesset) {
958:     snes->max_its   = 10000;
959:     snes->max_funcs = 10000;
960:   }

962:   PetscObjectComposeFunction((PetscObject)snes,"SNESNASMSetType_C",SNESNASMSetType_NASM);
963:   PetscObjectComposeFunction((PetscObject)snes,"SNESNASMGetType_C",SNESNASMGetType_NASM);
964:   PetscObjectComposeFunction((PetscObject)snes,"SNESNASMSetSubdomains_C",SNESNASMSetSubdomains_NASM);
965:   PetscObjectComposeFunction((PetscObject)snes,"SNESNASMGetSubdomains_C",SNESNASMGetSubdomains_NASM);
966:   PetscObjectComposeFunction((PetscObject)snes,"SNESNASMSetDamping_C",SNESNASMSetDamping_NASM);
967:   PetscObjectComposeFunction((PetscObject)snes,"SNESNASMGetDamping_C",SNESNASMGetDamping_NASM);
968:   PetscObjectComposeFunction((PetscObject)snes,"SNESNASMGetSubdomainVecs_C",SNESNASMGetSubdomainVecs_NASM);
969:   PetscObjectComposeFunction((PetscObject)snes,"SNESNASMSetComputeFinalJacobian_C",SNESNASMSetComputeFinalJacobian_NASM);
970:   return(0);
971: }