Actual source code: virs.c
petsc-3.5.4 2015-05-23
2: #include <../src/snes/impls/vi/rs/virsimpl.h> /*I "petscsnes.h" I*/
3: #include <petsc-private/kspimpl.h>
4: #include <petsc-private/matimpl.h>
5: #include <petsc-private/dmimpl.h>
6: #include <petsc-private/vecimpl.h>
10: /*
11: SNESVIGetInactiveSet - Gets the global indices for the inactive set variables (these correspond to the degrees of freedom the linear
12: system is solved on)
14: Input parameter
15: . snes - the SNES context
17: Output parameter
18: . ISact - active set index set
20: */
21: PetscErrorCode SNESVIGetInactiveSet(SNES snes,IS *inact)
22: {
23: SNES_VINEWTONRSLS *vi = (SNES_VINEWTONRSLS*)snes->data;
26: *inact = vi->IS_inact_prev;
27: return(0);
28: }
30: /*
31: Provides a wrapper to a DM to allow it to be used to generated the interpolation/restriction from the DM for the smaller matrices and vectors
32: defined by the reduced space method.
34: Simple calls the regular DM interpolation and restricts it to operation on the variables not associated with active constraints.
36: <*/
37: typedef struct {
38: PetscInt n; /* size of vectors in the reduced DM space */
39: IS inactive;
41: PetscErrorCode (*createinterpolation)(DM,DM,Mat*,Vec*); /* DM's original routines */
42: PetscErrorCode (*coarsen)(DM, MPI_Comm, DM*);
43: PetscErrorCode (*createglobalvector)(DM,Vec*);
45: DM dm; /* when destroying this object we need to reset the above function into the base DM */
46: } DM_SNESVI;
50: /*
51: DMCreateGlobalVector_SNESVI - Creates global vector of the size of the reduced space
53: */
54: PetscErrorCode DMCreateGlobalVector_SNESVI(DM dm,Vec *vec)
55: {
57: PetscContainer isnes;
58: DM_SNESVI *dmsnesvi;
61: PetscObjectQuery((PetscObject)dm,"VI",(PetscObject*)&isnes);
62: if (!isnes) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_PLIB,"Composed SNES is missing");
63: PetscContainerGetPointer(isnes,(void**)&dmsnesvi);
64: VecCreateMPI(PetscObjectComm((PetscObject)dm),dmsnesvi->n,PETSC_DETERMINE,vec);
65: return(0);
66: }
70: /*
71: DMCreateInterpolation_SNESVI - Modifieds the interpolation obtained from the DM by removing all rows and columns associated with active constraints.
73: */
74: PetscErrorCode DMCreateInterpolation_SNESVI(DM dm1,DM dm2,Mat *mat,Vec *vec)
75: {
77: PetscContainer isnes;
78: DM_SNESVI *dmsnesvi1,*dmsnesvi2;
79: Mat interp;
82: PetscObjectQuery((PetscObject)dm1,"VI",(PetscObject*)&isnes);
83: if (!isnes) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_PLIB,"Composed VI data structure is missing");
84: PetscContainerGetPointer(isnes,(void**)&dmsnesvi1);
85: PetscObjectQuery((PetscObject)dm2,"VI",(PetscObject*)&isnes);
86: if (!isnes) SETERRQ(PetscObjectComm((PetscObject)dm2),PETSC_ERR_PLIB,"Composed VI data structure is missing");
87: PetscContainerGetPointer(isnes,(void**)&dmsnesvi2);
89: (*dmsnesvi1->createinterpolation)(dm1,dm2,&interp,NULL);
90: MatGetSubMatrix(interp,dmsnesvi2->inactive,dmsnesvi1->inactive,MAT_INITIAL_MATRIX,mat);
91: MatDestroy(&interp);
92: *vec = 0;
93: return(0);
94: }
96: extern PetscErrorCode DMSetVI(DM,IS);
100: /*
101: DMCoarsen_SNESVI - Computes the regular coarsened DM then computes additional information about its inactive set
103: */
104: PetscErrorCode DMCoarsen_SNESVI(DM dm1,MPI_Comm comm,DM *dm2)
105: {
107: PetscContainer isnes;
108: DM_SNESVI *dmsnesvi1;
109: Vec finemarked,coarsemarked;
110: IS inactive;
111: VecScatter inject;
112: const PetscInt *index;
113: PetscInt n,k,cnt = 0,rstart,*coarseindex;
114: PetscScalar *marked;
117: PetscObjectQuery((PetscObject)dm1,"VI",(PetscObject*)&isnes);
118: if (!isnes) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_PLIB,"Composed VI data structure is missing");
119: PetscContainerGetPointer(isnes,(void**)&dmsnesvi1);
121: /* get the original coarsen */
122: (*dmsnesvi1->coarsen)(dm1,comm,dm2);
124: /* not sure why this extra reference is needed, but without the dm2 disappears too early */
125: PetscObjectReference((PetscObject)*dm2);
127: /* need to set back global vectors in order to use the original injection */
128: DMClearGlobalVectors(dm1);
130: dm1->ops->createglobalvector = dmsnesvi1->createglobalvector;
132: DMCreateGlobalVector(dm1,&finemarked);
133: DMCreateGlobalVector(*dm2,&coarsemarked);
135: /*
136: fill finemarked with locations of inactive points
137: */
138: ISGetIndices(dmsnesvi1->inactive,&index);
139: ISGetLocalSize(dmsnesvi1->inactive,&n);
140: VecSet(finemarked,0.0);
141: for (k=0; k<n; k++) {
142: VecSetValue(finemarked,index[k],1.0,INSERT_VALUES);
143: }
144: VecAssemblyBegin(finemarked);
145: VecAssemblyEnd(finemarked);
147: DMCreateInjection(*dm2,dm1,&inject);
148: VecScatterBegin(inject,finemarked,coarsemarked,INSERT_VALUES,SCATTER_FORWARD);
149: VecScatterEnd(inject,finemarked,coarsemarked,INSERT_VALUES,SCATTER_FORWARD);
150: VecScatterDestroy(&inject);
152: /*
153: create index set list of coarse inactive points from coarsemarked
154: */
155: VecGetLocalSize(coarsemarked,&n);
156: VecGetOwnershipRange(coarsemarked,&rstart,NULL);
157: VecGetArray(coarsemarked,&marked);
158: for (k=0; k<n; k++) {
159: if (marked[k] != 0.0) cnt++;
160: }
161: PetscMalloc1(cnt,&coarseindex);
162: cnt = 0;
163: for (k=0; k<n; k++) {
164: if (marked[k] != 0.0) coarseindex[cnt++] = k + rstart;
165: }
166: VecRestoreArray(coarsemarked,&marked);
167: ISCreateGeneral(PetscObjectComm((PetscObject)coarsemarked),cnt,coarseindex,PETSC_OWN_POINTER,&inactive);
169: DMClearGlobalVectors(dm1);
171: dm1->ops->createglobalvector = DMCreateGlobalVector_SNESVI;
173: DMSetVI(*dm2,inactive);
175: VecDestroy(&finemarked);
176: VecDestroy(&coarsemarked);
177: ISDestroy(&inactive);
178: return(0);
179: }
183: PetscErrorCode DMDestroy_SNESVI(DM_SNESVI *dmsnesvi)
184: {
188: /* reset the base methods in the DM object that were changed when the DM_SNESVI was reset */
189: dmsnesvi->dm->ops->createinterpolation = dmsnesvi->createinterpolation;
190: dmsnesvi->dm->ops->coarsen = dmsnesvi->coarsen;
191: dmsnesvi->dm->ops->createglobalvector = dmsnesvi->createglobalvector;
192: /* need to clear out this vectors because some of them may not have a reference to the DM
193: but they are counted as having references to the DM in DMDestroy() */
194: DMClearGlobalVectors(dmsnesvi->dm);
196: ISDestroy(&dmsnesvi->inactive);
197: PetscFree(dmsnesvi);
198: return(0);
199: }
203: /*
204: DMSetVI - Marks a DM as associated with a VI problem. This causes the interpolation/restriction operators to
205: be restricted to only those variables NOT associated with active constraints.
207: */
208: PetscErrorCode DMSetVI(DM dm,IS inactive)
209: {
211: PetscContainer isnes;
212: DM_SNESVI *dmsnesvi;
215: if (!dm) return(0);
217: PetscObjectReference((PetscObject)inactive);
219: PetscObjectQuery((PetscObject)dm,"VI",(PetscObject*)&isnes);
220: if (!isnes) {
221: PetscContainerCreate(PetscObjectComm((PetscObject)dm),&isnes);
222: PetscContainerSetUserDestroy(isnes,(PetscErrorCode (*)(void*))DMDestroy_SNESVI);
223: PetscNew(&dmsnesvi);
224: PetscContainerSetPointer(isnes,(void*)dmsnesvi);
225: PetscObjectCompose((PetscObject)dm,"VI",(PetscObject)isnes);
226: PetscContainerDestroy(&isnes);
228: dmsnesvi->createinterpolation = dm->ops->createinterpolation;
229: dm->ops->createinterpolation = DMCreateInterpolation_SNESVI;
230: dmsnesvi->coarsen = dm->ops->coarsen;
231: dm->ops->coarsen = DMCoarsen_SNESVI;
232: dmsnesvi->createglobalvector = dm->ops->createglobalvector;
233: dm->ops->createglobalvector = DMCreateGlobalVector_SNESVI;
234: } else {
235: PetscContainerGetPointer(isnes,(void**)&dmsnesvi);
236: ISDestroy(&dmsnesvi->inactive);
237: }
238: DMClearGlobalVectors(dm);
239: ISGetLocalSize(inactive,&dmsnesvi->n);
241: dmsnesvi->inactive = inactive;
242: dmsnesvi->dm = dm;
243: return(0);
244: }
248: /*
249: DMDestroyVI - Frees the DM_SNESVI object contained in the DM
250: - also resets the function pointers in the DM for createinterpolation() etc to use the original DM
251: */
252: PetscErrorCode DMDestroyVI(DM dm)
253: {
257: if (!dm) return(0);
258: PetscObjectCompose((PetscObject)dm,"VI",(PetscObject)NULL);
259: return(0);
260: }
262: /* --------------------------------------------------------------------------------------------------------*/
269: PetscErrorCode SNESCreateIndexSets_VINEWTONRSLS(SNES snes,Vec X,Vec F,IS *ISact,IS *ISinact)
270: {
274: SNESVIGetActiveSetIS(snes,X,F,ISact);
275: ISComplement(*ISact,X->map->rstart,X->map->rend,ISinact);
276: return(0);
277: }
279: /* Create active and inactive set vectors. The local size of this vector is set and petsc computes the global size */
282: PetscErrorCode SNESCreateSubVectors_VINEWTONRSLS(SNES snes,PetscInt n,Vec *newv)
283: {
285: Vec v;
288: VecCreate(PetscObjectComm((PetscObject)snes),&v);
289: VecSetSizes(v,n,PETSC_DECIDE);
290: VecSetType(v,VECSTANDARD);
291: *newv = v;
292: return(0);
293: }
295: /* Resets the snes PC and KSP when the active set sizes change */
298: PetscErrorCode SNESVIResetPCandKSP(SNES snes,Mat Amat,Mat Pmat)
299: {
301: KSP snesksp;
304: SNESGetKSP(snes,&snesksp);
305: KSPReset(snesksp);
307: /*
308: KSP kspnew;
309: PC pcnew;
310: const MatSolverPackage stype;
313: KSPCreate(PetscObjectComm((PetscObject)snes),&kspnew);
314: kspnew->pc_side = snesksp->pc_side;
315: kspnew->rtol = snesksp->rtol;
316: kspnew->abstol = snesksp->abstol;
317: kspnew->max_it = snesksp->max_it;
318: KSPSetType(kspnew,((PetscObject)snesksp)->type_name);
319: KSPGetPC(kspnew,&pcnew);
320: PCSetType(kspnew->pc,((PetscObject)snesksp->pc)->type_name);
321: PCSetOperators(kspnew->pc,Amat,Pmat);
322: PCFactorGetMatSolverPackage(snesksp->pc,&stype);
323: PCFactorSetMatSolverPackage(kspnew->pc,stype);
324: KSPDestroy(&snesksp);
325: snes->ksp = kspnew;
326: PetscLogObjectParent((PetscObject)snes,(PetscObject)kspnew);
327: KSPSetFromOptions(kspnew);*/
328: return(0);
329: }
331: /* Variational Inequality solver using reduce space method. No semismooth algorithm is
332: implemented in this algorithm. It basically identifies the active constraints and does
333: a linear solve on the other variables (those not associated with the active constraints). */
336: PetscErrorCode SNESSolve_VINEWTONRSLS(SNES snes)
337: {
338: SNES_VINEWTONRSLS *vi = (SNES_VINEWTONRSLS*)snes->data;
339: PetscErrorCode ierr;
340: PetscInt maxits,i,lits;
341: PetscBool lssucceed;
342: PetscReal fnorm,gnorm,xnorm=0,ynorm;
343: Vec Y,X,F;
344: KSPConvergedReason kspreason;
347: snes->numFailures = 0;
348: snes->numLinearSolveFailures = 0;
349: snes->reason = SNES_CONVERGED_ITERATING;
351: maxits = snes->max_its; /* maximum number of iterations */
352: X = snes->vec_sol; /* solution vector */
353: F = snes->vec_func; /* residual vector */
354: Y = snes->work[0]; /* work vectors */
356: SNESLineSearchSetVIFunctions(snes->linesearch, SNESVIProjectOntoBounds, SNESVIComputeInactiveSetFnorm);
357: SNESLineSearchSetVecs(snes->linesearch, X, NULL, NULL, NULL, NULL);
358: SNESLineSearchSetUp(snes->linesearch);
360: PetscObjectSAWsTakeAccess((PetscObject)snes);
361: snes->iter = 0;
362: snes->norm = 0.0;
363: PetscObjectSAWsGrantAccess((PetscObject)snes);
365: SNESVIProjectOntoBounds(snes,X);
366: SNESComputeFunction(snes,X,F);
367: if (snes->domainerror) {
368: snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
369: return(0);
370: }
371: SNESVIComputeInactiveSetFnorm(snes,F,X,&fnorm);
372: VecNormBegin(X,NORM_2,&xnorm); /* xnorm <- ||x|| */
373: VecNormEnd(X,NORM_2,&xnorm);
374: if (PetscIsInfOrNanReal(fnorm)) SETERRQ(PetscObjectComm((PetscObject)X),PETSC_ERR_FP,"User provided compute function generated a Not-a-Number");
376: PetscObjectSAWsTakeAccess((PetscObject)snes);
377: snes->norm = fnorm;
378: PetscObjectSAWsGrantAccess((PetscObject)snes);
379: SNESLogConvergenceHistory(snes,fnorm,0);
380: SNESMonitor(snes,0,fnorm);
382: /* test convergence */
383: (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);
384: if (snes->reason) return(0);
387: for (i=0; i<maxits; i++) {
389: IS IS_act,IS_inact; /* _act -> active set _inact -> inactive set */
390: IS IS_redact; /* redundant active set */
391: VecScatter scat_act,scat_inact;
392: PetscInt nis_act,nis_inact;
393: Vec Y_act,Y_inact,F_inact;
394: Mat jac_inact_inact,prejac_inact_inact;
395: PetscBool isequal;
397: /* Call general purpose update function */
398: if (snes->ops->update) {
399: (*snes->ops->update)(snes, snes->iter);
400: }
401: SNESComputeJacobian(snes,X,snes->jacobian,snes->jacobian_pre);
404: /* Create active and inactive index sets */
406: /*original
407: SNESVICreateIndexSets_RS(snes,X,F,&IS_act,&IS_inact);
408: */
409: SNESVIGetActiveSetIS(snes,X,F,&IS_act);
411: if (vi->checkredundancy) {
412: (*vi->checkredundancy)(snes,IS_act,&IS_redact,vi->ctxP);
413: if (IS_redact) {
414: ISSort(IS_redact);
415: ISComplement(IS_redact,X->map->rstart,X->map->rend,&IS_inact);
416: ISDestroy(&IS_redact);
417: } else {
418: ISComplement(IS_act,X->map->rstart,X->map->rend,&IS_inact);
419: }
420: } else {
421: ISComplement(IS_act,X->map->rstart,X->map->rend,&IS_inact);
422: }
425: /* Create inactive set submatrix */
426: MatGetSubMatrix(snes->jacobian,IS_inact,IS_inact,MAT_INITIAL_MATRIX,&jac_inact_inact);
428: if (0) { /* Dead code (temporary developer hack) */
429: IS keptrows;
430: MatFindNonzeroRows(jac_inact_inact,&keptrows);
431: if (keptrows) {
432: PetscInt cnt,*nrows,k;
433: const PetscInt *krows,*inact;
434: PetscInt rstart=jac_inact_inact->rmap->rstart;
436: MatDestroy(&jac_inact_inact);
437: ISDestroy(&IS_act);
439: ISGetLocalSize(keptrows,&cnt);
440: ISGetIndices(keptrows,&krows);
441: ISGetIndices(IS_inact,&inact);
442: PetscMalloc1(cnt,&nrows);
443: for (k=0; k<cnt; k++) nrows[k] = inact[krows[k]-rstart];
444: ISRestoreIndices(keptrows,&krows);
445: ISRestoreIndices(IS_inact,&inact);
446: ISDestroy(&keptrows);
447: ISDestroy(&IS_inact);
449: ISCreateGeneral(PetscObjectComm((PetscObject)snes),cnt,nrows,PETSC_OWN_POINTER,&IS_inact);
450: ISComplement(IS_inact,F->map->rstart,F->map->rend,&IS_act);
451: MatGetSubMatrix(snes->jacobian,IS_inact,IS_inact,MAT_INITIAL_MATRIX,&jac_inact_inact);
452: }
453: }
454: DMSetVI(snes->dm,IS_inact);
455: /* remove later */
457: /*
458: VecView(vi->xu,PETSC_VIEWER_BINARY_(((PetscObject)(vi->xu))->comm));
459: VecView(vi->xl,PETSC_VIEWER_BINARY_(((PetscObject)(vi->xl))->comm));
460: VecView(X,PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)X)));
461: VecView(F,PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)F)));
462: ISView(IS_inact,PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)IS_inact)));
463: */
465: /* Get sizes of active and inactive sets */
466: ISGetLocalSize(IS_act,&nis_act);
467: ISGetLocalSize(IS_inact,&nis_inact);
469: /* Create active and inactive set vectors */
470: SNESCreateSubVectors_VINEWTONRSLS(snes,nis_inact,&F_inact);
471: SNESCreateSubVectors_VINEWTONRSLS(snes,nis_act,&Y_act);
472: SNESCreateSubVectors_VINEWTONRSLS(snes,nis_inact,&Y_inact);
474: /* Create scatter contexts */
475: VecScatterCreate(Y,IS_act,Y_act,NULL,&scat_act);
476: VecScatterCreate(Y,IS_inact,Y_inact,NULL,&scat_inact);
478: /* Do a vec scatter to active and inactive set vectors */
479: VecScatterBegin(scat_inact,F,F_inact,INSERT_VALUES,SCATTER_FORWARD);
480: VecScatterEnd(scat_inact,F,F_inact,INSERT_VALUES,SCATTER_FORWARD);
482: VecScatterBegin(scat_act,Y,Y_act,INSERT_VALUES,SCATTER_FORWARD);
483: VecScatterEnd(scat_act,Y,Y_act,INSERT_VALUES,SCATTER_FORWARD);
485: VecScatterBegin(scat_inact,Y,Y_inact,INSERT_VALUES,SCATTER_FORWARD);
486: VecScatterEnd(scat_inact,Y,Y_inact,INSERT_VALUES,SCATTER_FORWARD);
488: /* Active set direction = 0 */
489: VecSet(Y_act,0);
490: if (snes->jacobian != snes->jacobian_pre) {
491: MatGetSubMatrix(snes->jacobian_pre,IS_inact,IS_inact,MAT_INITIAL_MATRIX,&prejac_inact_inact);
492: } else prejac_inact_inact = jac_inact_inact;
494: ISEqual(vi->IS_inact_prev,IS_inact,&isequal);
495: if (!isequal) {
496: SNESVIResetPCandKSP(snes,jac_inact_inact,prejac_inact_inact);
497: }
499: /* ISView(IS_inact,0); */
500: /* ISView(IS_act,0);*/
501: /* MatView(snes->jacobian_pre,0); */
505: KSPSetOperators(snes->ksp,jac_inact_inact,prejac_inact_inact);
506: KSPSetUp(snes->ksp);
507: {
508: PC pc;
509: PetscBool flg;
510: KSPGetPC(snes->ksp,&pc);
511: PetscObjectTypeCompare((PetscObject)pc,PCFIELDSPLIT,&flg);
512: if (flg) {
513: KSP *subksps;
514: PCFieldSplitGetSubKSP(pc,NULL,&subksps);
515: KSPGetPC(subksps[0],&pc);
516: PetscFree(subksps);
517: PetscObjectTypeCompare((PetscObject)pc,PCBJACOBI,&flg);
518: if (flg) {
519: PetscInt n,N = 101*101,j,cnts[3] = {0,0,0};
520: const PetscInt *ii;
522: ISGetSize(IS_inact,&n);
523: ISGetIndices(IS_inact,&ii);
524: for (j=0; j<n; j++) {
525: if (ii[j] < N) cnts[0]++;
526: else if (ii[j] < 2*N) cnts[1]++;
527: else if (ii[j] < 3*N) cnts[2]++;
528: }
529: ISRestoreIndices(IS_inact,&ii);
531: PCBJacobiSetTotalBlocks(pc,3,cnts);
532: }
533: }
534: }
536: KSPSolve(snes->ksp,F_inact,Y_inact);
537: KSPGetConvergedReason(snes->ksp,&kspreason);
538: if (kspreason < 0) {
539: if (++snes->numLinearSolveFailures >= snes->maxLinearSolveFailures) {
540: PetscInfo2(snes,"iter=%D, number linear solve failures %D greater than current SNES allowed, stopping solve\n",snes->iter,snes->numLinearSolveFailures);
541: snes->reason = SNES_DIVERGED_LINEAR_SOLVE;
542: break;
543: }
544: }
546: VecScatterBegin(scat_act,Y_act,Y,INSERT_VALUES,SCATTER_REVERSE);
547: VecScatterEnd(scat_act,Y_act,Y,INSERT_VALUES,SCATTER_REVERSE);
548: VecScatterBegin(scat_inact,Y_inact,Y,INSERT_VALUES,SCATTER_REVERSE);
549: VecScatterEnd(scat_inact,Y_inact,Y,INSERT_VALUES,SCATTER_REVERSE);
551: VecDestroy(&F_inact);
552: VecDestroy(&Y_act);
553: VecDestroy(&Y_inact);
554: VecScatterDestroy(&scat_act);
555: VecScatterDestroy(&scat_inact);
556: ISDestroy(&IS_act);
557: if (!isequal) {
558: ISDestroy(&vi->IS_inact_prev);
559: ISDuplicate(IS_inact,&vi->IS_inact_prev);
560: }
561: ISDestroy(&IS_inact);
562: MatDestroy(&jac_inact_inact);
563: if (snes->jacobian != snes->jacobian_pre) {
564: MatDestroy(&prejac_inact_inact);
565: }
566: KSPGetIterationNumber(snes->ksp,&lits);
567: snes->linear_its += lits;
568: PetscInfo2(snes,"iter=%D, linear solve iterations=%D\n",snes->iter,lits);
569: /*
570: if (snes->ops->precheck) {
571: PetscBool changed_y = PETSC_FALSE;
572: (*snes->ops->precheck)(snes,X,Y,snes->precheck,&changed_y);
573: }
575: if (PetscLogPrintInfo) {
576: SNESVICheckResidual_Private(snes,snes->jacobian,F,Y,G,W);
577: }
578: */
579: /* Compute a (scaled) negative update in the line search routine:
580: Y <- X - lambda*Y
581: and evaluate G = function(Y) (depends on the line search).
582: */
583: VecCopy(Y,snes->vec_sol_update);
584: ynorm = 1; gnorm = fnorm;
585: SNESLineSearchApply(snes->linesearch, X, F, &gnorm, Y);
586: SNESLineSearchGetSuccess(snes->linesearch, &lssucceed);
587: SNESLineSearchGetNorms(snes->linesearch, &xnorm, &gnorm, &ynorm);
588: PetscInfo4(snes,"fnorm=%18.16e, gnorm=%18.16e, ynorm=%18.16e, lssucceed=%d\n",(double)fnorm,(double)gnorm,(double)ynorm,(int)lssucceed);
589: if (snes->reason == SNES_DIVERGED_FUNCTION_COUNT) break;
590: if (snes->domainerror) {
591: snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
592: DMDestroyVI(snes->dm);
593: return(0);
594: }
595: if (!lssucceed) {
596: if (++snes->numFailures >= snes->maxFailures) {
597: PetscBool ismin;
598: snes->reason = SNES_DIVERGED_LINE_SEARCH;
599: SNESVICheckLocalMin_Private(snes,snes->jacobian,F,X,gnorm,&ismin);
600: if (ismin) snes->reason = SNES_DIVERGED_LOCAL_MIN;
601: break;
602: }
603: }
604: /* Update function and solution vectors */
605: fnorm = gnorm;
606: /* Monitor convergence */
607: PetscObjectSAWsTakeAccess((PetscObject)snes);
608: snes->iter = i+1;
609: snes->norm = fnorm;
610: PetscObjectSAWsGrantAccess((PetscObject)snes);
611: SNESLogConvergenceHistory(snes,snes->norm,lits);
612: SNESMonitor(snes,snes->iter,snes->norm);
613: /* Test for convergence, xnorm = || X || */
614: if (snes->ops->converged != SNESConvergedSkip) { VecNorm(X,NORM_2,&xnorm); }
615: (*snes->ops->converged)(snes,snes->iter,xnorm,ynorm,fnorm,&snes->reason,snes->cnvP);
616: if (snes->reason) break;
617: }
618: DMDestroyVI(snes->dm);
619: if (i == maxits) {
620: PetscInfo1(snes,"Maximum number of iterations has been reached: %D\n",maxits);
621: if (!snes->reason) snes->reason = SNES_DIVERGED_MAX_IT;
622: }
623: return(0);
624: }
628: PetscErrorCode SNESVISetRedundancyCheck(SNES snes,PetscErrorCode (*func)(SNES,IS,IS*,void*),void *ctx)
629: {
630: SNES_VINEWTONRSLS *vi = (SNES_VINEWTONRSLS*)snes->data;
634: vi->checkredundancy = func;
635: vi->ctxP = ctx;
636: return(0);
637: }
639: #if defined(PETSC_HAVE_MATLAB_ENGINE)
640: #include <engine.h>
641: #include <mex.h>
642: typedef struct {char *funcname; mxArray *ctx;} SNESMatlabContext;
646: PetscErrorCode SNESVIRedundancyCheck_Matlab(SNES snes,IS is_act,IS *is_redact,void *ctx)
647: {
648: PetscErrorCode ierr;
649: SNESMatlabContext *sctx = (SNESMatlabContext*)ctx;
650: int nlhs = 1, nrhs = 5;
651: mxArray *plhs[1], *prhs[5];
652: long long int l1 = 0, l2 = 0, ls = 0;
653: PetscInt *indices=NULL;
661: /* Create IS for reduced active set of size 0, its size and indices will
662: bet set by the Matlab function */
663: ISCreateGeneral(PetscObjectComm((PetscObject)snes),0,indices,PETSC_OWN_POINTER,is_redact);
664: /* call Matlab function in ctx */
665: PetscMemcpy(&ls,&snes,sizeof(snes));
666: PetscMemcpy(&l1,&is_act,sizeof(is_act));
667: PetscMemcpy(&l2,is_redact,sizeof(is_act));
668: prhs[0] = mxCreateDoubleScalar((double)ls);
669: prhs[1] = mxCreateDoubleScalar((double)l1);
670: prhs[2] = mxCreateDoubleScalar((double)l2);
671: prhs[3] = mxCreateString(sctx->funcname);
672: prhs[4] = sctx->ctx;
673: mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscSNESVIRedundancyCheckInternal");
674: mxGetScalar(plhs[0]);
675: mxDestroyArray(prhs[0]);
676: mxDestroyArray(prhs[1]);
677: mxDestroyArray(prhs[2]);
678: mxDestroyArray(prhs[3]);
679: mxDestroyArray(plhs[0]);
680: return(0);
681: }
685: PetscErrorCode SNESVISetRedundancyCheckMatlab(SNES snes,const char *func,mxArray *ctx)
686: {
687: PetscErrorCode ierr;
688: SNESMatlabContext *sctx;
691: /* currently sctx is memory bleed */
692: PetscMalloc(sizeof(SNESMatlabContext),&sctx);
693: PetscStrallocpy(func,&sctx->funcname);
694: sctx->ctx = mxDuplicateArray(ctx);
695: SNESVISetRedundancyCheck(snes,SNESVIRedundancyCheck_Matlab,sctx);
696: return(0);
697: }
699: #endif
701: /* -------------------------------------------------------------------------- */
702: /*
703: SNESSetUp_VINEWTONRSLS - Sets up the internal data structures for the later use
704: of the SNESVI nonlinear solver.
706: Input Parameter:
707: . snes - the SNES context
708: . x - the solution vector
710: Application Interface Routine: SNESSetUp()
712: Notes:
713: For basic use of the SNES solvers, the user need not explicitly call
714: SNESSetUp(), since these actions will automatically occur during
715: the call to SNESSolve().
716: */
719: PetscErrorCode SNESSetUp_VINEWTONRSLS(SNES snes)
720: {
721: PetscErrorCode ierr;
722: SNES_VINEWTONRSLS *vi = (SNES_VINEWTONRSLS*) snes->data;
723: PetscInt *indices;
724: PetscInt i,n,rstart,rend;
725: SNESLineSearch linesearch;
728: SNESSetUp_VI(snes);
730: /* Set up previous active index set for the first snes solve
731: vi->IS_inact_prev = 0,1,2,....N */
733: VecGetOwnershipRange(snes->vec_sol,&rstart,&rend);
734: VecGetLocalSize(snes->vec_sol,&n);
735: PetscMalloc1(n,&indices);
736: for (i=0; i < n; i++) indices[i] = rstart + i;
737: ISCreateGeneral(PetscObjectComm((PetscObject)snes),n,indices,PETSC_OWN_POINTER,&vi->IS_inact_prev);
739: /* set the line search functions */
740: if (!snes->linesearch) {
741: SNESGetLineSearch(snes, &linesearch);
742: SNESLineSearchSetType(linesearch, SNESLINESEARCHBT);
743: }
744: return(0);
745: }
746: /* -------------------------------------------------------------------------- */
749: PetscErrorCode SNESReset_VINEWTONRSLS(SNES snes)
750: {
751: SNES_VINEWTONRSLS *vi = (SNES_VINEWTONRSLS*) snes->data;
752: PetscErrorCode ierr;
755: SNESReset_VI(snes);
756: ISDestroy(&vi->IS_inact_prev);
757: return(0);
758: }
760: /* -------------------------------------------------------------------------- */
761: /*MC
762: SNESVINEWTONRSLS - Reduced space active set solvers for variational inequalities based on Newton's method
764: Options Database:
765: + -snes_vi_type <ss,rs,rsaug> a semi-smooth solver, a reduced space active set method, and a reduced space active set method that does not eliminate the active constraints from the Jacobian instead augments the Jacobian with additional variables that enforce the constraints
766: - -snes_vi_monitor - prints the number of active constraints at each iteration.
768: Level: beginner
770: References:
771: - T. S. Munson, and S. Benson. Flexible Complementarity Solvers for Large-Scale
772: Applications, Optimization Methods and Software, 21 (2006).
774: .seealso: SNESVISetVariableBounds(), SNESVISetComputeVariableBounds(), SNESCreate(), SNES, SNESSetType(), SNESVINEWTONRSLS, SNESVINEWTONSSLS, SNESNEWTONTR, SNESLineSearchSet(),
775: SNESLineSearchSetPostCheck(), SNESLineSearchNo(), SNESLineSearchCubic(), SNESLineSearchQuadratic(),
776: SNESLineSearchSet(), SNESLineSearchNoNorms(), SNESLineSearchSetPreCheck(), SNESLineSearchSetParams(), SNESLineSearchGetParams()
778: M*/
781: PETSC_EXTERN PetscErrorCode SNESCreate_VINEWTONRSLS(SNES snes)
782: {
783: PetscErrorCode ierr;
784: SNES_VINEWTONRSLS *vi;
787: snes->ops->reset = SNESReset_VINEWTONRSLS;
788: snes->ops->setup = SNESSetUp_VINEWTONRSLS;
789: snes->ops->solve = SNESSolve_VINEWTONRSLS;
790: snes->ops->destroy = SNESDestroy_VI;
791: snes->ops->setfromoptions = SNESSetFromOptions_VI;
792: snes->ops->view = NULL;
793: snes->ops->converged = SNESConvergedDefault_VI;
795: snes->usesksp = PETSC_TRUE;
796: snes->usespc = PETSC_FALSE;
798: PetscNewLog(snes,&vi);
799: snes->data = (void*)vi;
800: vi->checkredundancy = NULL;
802: PetscObjectComposeFunction((PetscObject)snes,"SNESVISetVariableBounds_C",SNESVISetVariableBounds_VI);
803: PetscObjectComposeFunction((PetscObject)snes,"SNESVISetComputeVariableBounds_C",SNESVISetComputeVariableBounds_VI);
804: return(0);
805: }