Actual source code: vi.c
1: #include <petsc/private/snesimpl.h>
2: #include <petscdm.h>
4: /*@C
5: SNESVISetComputeVariableBounds - Sets a function that is called to compute the variable bounds
7: Input parameter:
8: + snes - the SNES context
9: - compute - computes the bounds
11: Level: advanced
13: .seealso: SNESVISetVariableBounds()
15: @*/
16: PetscErrorCode SNESVISetComputeVariableBounds(SNES snes, PetscErrorCode (*compute)(SNES,Vec,Vec))
17: {
18: PetscErrorCode ierr,(*f)(SNES,PetscErrorCode (*)(SNES,Vec,Vec));
22: PetscObjectQueryFunction((PetscObject)snes,"SNESVISetComputeVariableBounds_C",&f);
23: if (!f) {
24: SNESVISetComputeVariableBounds_VI(snes,compute);
25: } else {
26: PetscUseMethod(snes,"SNESVISetComputeVariableBounds_C",(SNES,PetscErrorCode (*)(SNES,Vec,Vec)),(snes,compute));
27: }
28: return(0);
29: }
31: PetscErrorCode SNESVISetComputeVariableBounds_VI(SNES snes,SNESVIComputeVariableBoundsFunction compute)
32: {
34: snes->ops->computevariablebounds = compute;
35: return(0);
36: }
38: /* --------------------------------------------------------------------------------------------------------*/
40: PetscErrorCode SNESVIMonitorResidual(SNES snes,PetscInt its,PetscReal fgnorm,void *dummy)
41: {
43: Vec X, F, Finactive;
44: IS isactive;
45: PetscViewer viewer = (PetscViewer) dummy;
48: SNESGetFunction(snes,&F,NULL,NULL);
49: SNESGetSolution(snes,&X);
50: SNESVIGetActiveSetIS(snes,X,F,&isactive);
51: VecDuplicate(F,&Finactive);
52: VecCopy(F,Finactive);
53: VecISSet(Finactive,isactive,0.0);
54: ISDestroy(&isactive);
55: VecView(Finactive,viewer);
56: VecDestroy(&Finactive);
57: return(0);
58: }
60: PetscErrorCode SNESMonitorVI(SNES snes,PetscInt its,PetscReal fgnorm,void *dummy)
61: {
62: PetscErrorCode ierr;
63: PetscViewer viewer = (PetscViewer) dummy;
64: const PetscScalar *x,*xl,*xu,*f;
65: PetscInt i,n,act[2] = {0,0},fact[2],N;
66: /* Number of components that actually hit the bounds (c.f. active variables) */
67: PetscInt act_bound[2] = {0,0},fact_bound[2];
68: PetscReal rnorm,fnorm,zerotolerance = snes->vizerotolerance;
69: double tmp;
73: VecGetLocalSize(snes->vec_sol,&n);
74: VecGetSize(snes->vec_sol,&N);
75: VecGetArrayRead(snes->xl,&xl);
76: VecGetArrayRead(snes->xu,&xu);
77: VecGetArrayRead(snes->vec_sol,&x);
78: VecGetArrayRead(snes->vec_func,&f);
80: rnorm = 0.0;
81: for (i=0; i<n; i++) {
82: if (((PetscRealPart(x[i]) > PetscRealPart(xl[i]) + zerotolerance || (PetscRealPart(f[i]) <= 0.0)) && ((PetscRealPart(x[i]) < PetscRealPart(xu[i]) - zerotolerance) || PetscRealPart(f[i]) >= 0.0))) rnorm += PetscRealPart(PetscConj(f[i])*f[i]);
83: else if (PetscRealPart(x[i]) <= PetscRealPart(xl[i]) + zerotolerance && PetscRealPart(f[i]) > 0.0) act[0]++;
84: else if (PetscRealPart(x[i]) >= PetscRealPart(xu[i]) - zerotolerance && PetscRealPart(f[i]) < 0.0) act[1]++;
85: else SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_PLIB,"Can never get here");
86: }
88: for (i=0; i<n; i++) {
89: if (PetscRealPart(x[i]) <= PetscRealPart(xl[i]) + zerotolerance) act_bound[0]++;
90: else if (PetscRealPart(x[i]) >= PetscRealPart(xu[i]) - zerotolerance) act_bound[1]++;
91: }
92: VecRestoreArrayRead(snes->vec_func,&f);
93: VecRestoreArrayRead(snes->xl,&xl);
94: VecRestoreArrayRead(snes->xu,&xu);
95: VecRestoreArrayRead(snes->vec_sol,&x);
96: MPIU_Allreduce(&rnorm,&fnorm,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)snes));
97: MPIU_Allreduce(act,fact,2,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)snes));
98: MPIU_Allreduce(act_bound,fact_bound,2,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)snes));
99: fnorm = PetscSqrtReal(fnorm);
101: PetscViewerASCIIAddTab(viewer,((PetscObject)snes)->tablevel);
102: if (snes->ntruebounds) tmp = ((double)(fact[0]+fact[1]))/((double)snes->ntruebounds);
103: else tmp = 0.0;
104: PetscViewerASCIIPrintf(viewer,"%3D SNES VI Function norm %g Active lower constraints %D/%D upper constraints %D/%D Percent of total %g Percent of bounded %g\n",its,(double)fnorm,fact[0],fact_bound[0],fact[1],fact_bound[1],((double)(fact[0]+fact[1]))/((double)N),tmp);
106: PetscViewerASCIISubtractTab(viewer,((PetscObject)snes)->tablevel);
107: return(0);
108: }
110: /*
111: Checks if J^T F = 0 which implies we've found a local minimum of the norm of the function,
112: || F(u) ||_2 but not a zero, F(u) = 0. In the case when one cannot compute J^T F we use the fact that
113: 0 = (J^T F)^T W = F^T J W iff W not in the null space of J. Thanks for Jorge More
114: for this trick. One assumes that the probability that W is in the null space of J is very, very small.
115: */
116: PetscErrorCode SNESVICheckLocalMin_Private(SNES snes,Mat A,Vec F,Vec W,PetscReal fnorm,PetscBool *ismin)
117: {
118: PetscReal a1;
120: PetscBool hastranspose;
123: *ismin = PETSC_FALSE;
124: MatHasOperation(A,MATOP_MULT_TRANSPOSE,&hastranspose);
125: if (hastranspose) {
126: /* Compute || J^T F|| */
127: MatMultTranspose(A,F,W);
128: VecNorm(W,NORM_2,&a1);
129: PetscInfo1(snes,"|| J^T F|| %g near zero implies found a local minimum\n",(double)(a1/fnorm));
130: if (a1/fnorm < 1.e-4) *ismin = PETSC_TRUE;
131: } else {
132: Vec work;
133: PetscScalar result;
134: PetscReal wnorm;
136: VecSetRandom(W,NULL);
137: VecNorm(W,NORM_2,&wnorm);
138: VecDuplicate(W,&work);
139: MatMult(A,W,work);
140: VecDot(F,work,&result);
141: VecDestroy(&work);
142: a1 = PetscAbsScalar(result)/(fnorm*wnorm);
143: PetscInfo1(snes,"(F^T J random)/(|| F ||*||random|| %g near zero implies found a local minimum\n",(double)a1);
144: if (a1 < 1.e-4) *ismin = PETSC_TRUE;
145: }
146: return(0);
147: }
149: /*
150: Checks if J^T(F - J*X) = 0
151: */
152: PetscErrorCode SNESVICheckResidual_Private(SNES snes,Mat A,Vec F,Vec X,Vec W1,Vec W2)
153: {
154: PetscReal a1,a2;
156: PetscBool hastranspose;
159: MatHasOperation(A,MATOP_MULT_TRANSPOSE,&hastranspose);
160: if (hastranspose) {
161: MatMult(A,X,W1);
162: VecAXPY(W1,-1.0,F);
164: /* Compute || J^T W|| */
165: MatMultTranspose(A,W1,W2);
166: VecNorm(W1,NORM_2,&a1);
167: VecNorm(W2,NORM_2,&a2);
168: if (a1 != 0.0) {
169: PetscInfo1(snes,"||J^T(F-Ax)||/||F-AX|| %g near zero implies inconsistent rhs\n",(double)(a2/a1));
170: }
171: }
172: return(0);
173: }
175: /*
176: SNESConvergedDefault_VI - Checks the convergence of the semismooth newton algorithm.
178: Notes:
179: The convergence criterion currently implemented is
180: merit < abstol
181: merit < rtol*merit_initial
182: */
183: PetscErrorCode SNESConvergedDefault_VI(SNES snes,PetscInt it,PetscReal xnorm,PetscReal gradnorm,PetscReal fnorm,SNESConvergedReason *reason,void *dummy)
184: {
191: *reason = SNES_CONVERGED_ITERATING;
193: if (!it) {
194: /* set parameter for default relative tolerance convergence test */
195: snes->ttol = fnorm*snes->rtol;
196: }
197: if (fnorm != fnorm) {
198: PetscInfo(snes,"Failed to converged, function norm is NaN\n");
199: *reason = SNES_DIVERGED_FNORM_NAN;
200: } else if (fnorm < snes->abstol && (it || !snes->forceiteration)) {
201: PetscInfo2(snes,"Converged due to function norm %g < %g\n",(double)fnorm,(double)snes->abstol);
202: *reason = SNES_CONVERGED_FNORM_ABS;
203: } else if (snes->nfuncs >= snes->max_funcs && snes->max_funcs >= 0) {
204: PetscInfo2(snes,"Exceeded maximum number of function evaluations: %D > %D\n",snes->nfuncs,snes->max_funcs);
205: *reason = SNES_DIVERGED_FUNCTION_COUNT;
206: }
208: if (it && !*reason) {
209: if (fnorm < snes->ttol) {
210: PetscInfo2(snes,"Converged due to function norm %g < %g (relative tolerance)\n",(double)fnorm,(double)snes->ttol);
211: *reason = SNES_CONVERGED_FNORM_RELATIVE;
212: }
213: }
214: return(0);
215: }
217: /* -------------------------------------------------------------------------- */
218: /*
219: SNESVIProjectOntoBounds - Projects X onto the feasible region so that Xl[i] <= X[i] <= Xu[i] for i = 1...n.
221: Input Parameters:
222: . SNES - nonlinear solver context
224: Output Parameters:
225: . X - Bound projected X
227: */
229: PetscErrorCode SNESVIProjectOntoBounds(SNES snes,Vec X)
230: {
231: PetscErrorCode ierr;
232: const PetscScalar *xl,*xu;
233: PetscScalar *x;
234: PetscInt i,n;
237: VecGetLocalSize(X,&n);
238: VecGetArray(X,&x);
239: VecGetArrayRead(snes->xl,&xl);
240: VecGetArrayRead(snes->xu,&xu);
242: for (i = 0; i<n; i++) {
243: if (PetscRealPart(x[i]) < PetscRealPart(xl[i])) x[i] = xl[i];
244: else if (PetscRealPart(x[i]) > PetscRealPart(xu[i])) x[i] = xu[i];
245: }
246: VecRestoreArray(X,&x);
247: VecRestoreArrayRead(snes->xl,&xl);
248: VecRestoreArrayRead(snes->xu,&xu);
249: return(0);
250: }
252: /*
253: SNESVIGetActiveSetIndices - Gets the global indices for the active set variables
255: Input parameter:
256: . snes - the SNES context
257: . X - the snes solution vector
258: . F - the nonlinear function vector
260: Output parameter:
261: . ISact - active set index set
262: */
263: PetscErrorCode SNESVIGetActiveSetIS(SNES snes,Vec X,Vec F,IS *ISact)
264: {
265: PetscErrorCode ierr;
266: Vec Xl=snes->xl,Xu=snes->xu;
267: const PetscScalar *x,*f,*xl,*xu;
268: PetscInt *idx_act,i,nlocal,nloc_isact=0,ilow,ihigh,i1=0;
269: PetscReal zerotolerance = snes->vizerotolerance;
272: VecGetLocalSize(X,&nlocal);
273: VecGetOwnershipRange(X,&ilow,&ihigh);
274: VecGetArrayRead(X,&x);
275: VecGetArrayRead(Xl,&xl);
276: VecGetArrayRead(Xu,&xu);
277: VecGetArrayRead(F,&f);
278: /* Compute active set size */
279: for (i=0; i < nlocal;i++) {
280: if (!((PetscRealPart(x[i]) > PetscRealPart(xl[i]) + zerotolerance || (PetscRealPart(f[i]) <= 0.0)) && ((PetscRealPart(x[i]) < PetscRealPart(xu[i]) - zerotolerance) || PetscRealPart(f[i]) >= 0.0))) nloc_isact++;
281: }
283: PetscMalloc1(nloc_isact,&idx_act);
285: /* Set active set indices */
286: for (i=0; i < nlocal; i++) {
287: if (!((PetscRealPart(x[i]) > PetscRealPart(xl[i]) + zerotolerance || (PetscRealPart(f[i]) <= 0.0)) && ((PetscRealPart(x[i]) < PetscRealPart(xu[i]) - zerotolerance) || PetscRealPart(f[i]) >= 0.0))) idx_act[i1++] = ilow+i;
288: }
290: /* Create active set IS */
291: ISCreateGeneral(PetscObjectComm((PetscObject)snes),nloc_isact,idx_act,PETSC_OWN_POINTER,ISact);
293: VecRestoreArrayRead(X,&x);
294: VecRestoreArrayRead(Xl,&xl);
295: VecRestoreArrayRead(Xu,&xu);
296: VecRestoreArrayRead(F,&f);
297: return(0);
298: }
300: PetscErrorCode SNESVICreateIndexSets_RS(SNES snes,Vec X,Vec F,IS *ISact,IS *ISinact)
301: {
303: PetscInt rstart,rend;
306: SNESVIGetActiveSetIS(snes,X,F,ISact);
307: VecGetOwnershipRange(X,&rstart,&rend);
308: ISComplement(*ISact,rstart,rend,ISinact);
309: return(0);
310: }
312: PetscErrorCode SNESVIComputeInactiveSetFnorm(SNES snes,Vec F,Vec X, PetscReal *fnorm)
313: {
314: PetscErrorCode ierr;
315: const PetscScalar *x,*xl,*xu,*f;
316: PetscInt i,n;
317: PetscReal rnorm,zerotolerance = snes->vizerotolerance;
320: VecGetLocalSize(X,&n);
321: VecGetArrayRead(snes->xl,&xl);
322: VecGetArrayRead(snes->xu,&xu);
323: VecGetArrayRead(X,&x);
324: VecGetArrayRead(F,&f);
325: rnorm = 0.0;
326: for (i=0; i<n; i++) {
327: if (((PetscRealPart(x[i]) > PetscRealPart(xl[i]) + zerotolerance || (PetscRealPart(f[i]) <= 0.0)) && ((PetscRealPart(x[i]) < PetscRealPart(xu[i]) - zerotolerance) || PetscRealPart(f[i]) >= 0.0))) rnorm += PetscRealPart(PetscConj(f[i])*f[i]);
328: }
329: VecRestoreArrayRead(F,&f);
330: VecRestoreArrayRead(snes->xl,&xl);
331: VecRestoreArrayRead(snes->xu,&xu);
332: VecRestoreArrayRead(X,&x);
333: MPIU_Allreduce(&rnorm,fnorm,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)snes));
334: *fnorm = PetscSqrtReal(*fnorm);
335: return(0);
336: }
338: PetscErrorCode SNESVIDMComputeVariableBounds(SNES snes,Vec xl, Vec xu)
339: {
343: DMComputeVariableBounds(snes->dm, xl, xu);
344: return(0);
345: }
347: /* -------------------------------------------------------------------------- */
348: /*
349: SNESSetUp_VI - Does setup common to all VI solvers -- basically makes sure bounds have been properly set up
350: of the SNESVI nonlinear solver.
352: Input Parameter:
353: . snes - the SNES context
355: Application Interface Routine: SNESSetUp()
357: Notes:
358: For basic use of the SNES solvers, the user need not explicitly call
359: SNESSetUp(), since these actions will automatically occur during
360: the call to SNESSolve().
361: */
362: PetscErrorCode SNESSetUp_VI(SNES snes)
363: {
365: PetscInt i_start[3],i_end[3];
368: SNESSetWorkVecs(snes,1);
369: SNESSetUpMatrices(snes);
371: if (!snes->ops->computevariablebounds && snes->dm) {
372: PetscBool flag;
373: DMHasVariableBounds(snes->dm, &flag);
374: if (flag) {
375: snes->ops->computevariablebounds = SNESVIDMComputeVariableBounds;
376: }
377: }
378: if (!snes->usersetbounds) {
379: if (snes->ops->computevariablebounds) {
380: if (!snes->xl) {VecDuplicate(snes->vec_sol,&snes->xl);}
381: if (!snes->xu) {VecDuplicate(snes->vec_sol,&snes->xu);}
382: (*snes->ops->computevariablebounds)(snes,snes->xl,snes->xu);
383: } else if (!snes->xl && !snes->xu) {
384: /* If the lower and upper bound on variables are not set, set it to -Inf and Inf */
385: VecDuplicate(snes->vec_sol, &snes->xl);
386: VecSet(snes->xl,PETSC_NINFINITY);
387: VecDuplicate(snes->vec_sol, &snes->xu);
388: VecSet(snes->xu,PETSC_INFINITY);
389: } else {
390: /* Check if lower bound, upper bound and solution vector distribution across the processors is identical */
391: VecGetOwnershipRange(snes->vec_sol,i_start,i_end);
392: VecGetOwnershipRange(snes->xl,i_start+1,i_end+1);
393: VecGetOwnershipRange(snes->xu,i_start+2,i_end+2);
394: if ((i_start[0] != i_start[1]) || (i_start[0] != i_start[2]) || (i_end[0] != i_end[1]) || (i_end[0] != i_end[2]))
395: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Distribution of lower bound, upper bound and the solution vector should be identical across all the processors.");
396: }
397: }
398: return(0);
399: }
400: /* -------------------------------------------------------------------------- */
401: PetscErrorCode SNESReset_VI(SNES snes)
402: {
406: VecDestroy(&snes->xl);
407: VecDestroy(&snes->xu);
408: snes->usersetbounds = PETSC_FALSE;
409: return(0);
410: }
412: /*
413: SNESDestroy_VI - Destroys the private SNES_VI context that was created
414: with SNESCreate_VI().
416: Input Parameter:
417: . snes - the SNES context
419: Application Interface Routine: SNESDestroy()
420: */
421: PetscErrorCode SNESDestroy_VI(SNES snes)
422: {
426: PetscFree(snes->data);
428: /* clear composed functions */
429: PetscObjectComposeFunction((PetscObject)snes,"SNESLineSearchSet_C",NULL);
430: PetscObjectComposeFunction((PetscObject)snes,"SNESLineSearchSetDefaultMonitor_C",NULL);
431: return(0);
432: }
434: /*@
435: SNESVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu.
437: Input Parameters:
438: + snes - the SNES context.
439: . xl - lower bound.
440: - xu - upper bound.
442: Notes:
443: If this routine is not called then the lower and upper bounds are set to
444: PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
446: Level: advanced
448: @*/
449: PetscErrorCode SNESVISetVariableBounds(SNES snes, Vec xl, Vec xu)
450: {
451: PetscErrorCode ierr,(*f)(SNES,Vec,Vec);
457: PetscObjectQueryFunction((PetscObject)snes,"SNESVISetVariableBounds_C",&f);
458: if (!f) {
459: SNESVISetVariableBounds_VI(snes, xl, xu);
460: } else {
461: PetscUseMethod(snes,"SNESVISetVariableBounds_C",(SNES,Vec,Vec),(snes,xl,xu));
462: }
463: snes->usersetbounds = PETSC_TRUE;
464: return(0);
465: }
467: PetscErrorCode SNESVISetVariableBounds_VI(SNES snes,Vec xl,Vec xu)
468: {
469: PetscErrorCode ierr;
470: const PetscScalar *xxl,*xxu;
471: PetscInt i,n, cnt = 0;
474: SNESGetFunction(snes,&snes->vec_func,NULL,NULL);
475: if (!snes->vec_func) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call SNESSetFunction() or SNESSetDM() first");
476: {
477: PetscInt xlN,xuN,N;
478: VecGetSize(xl,&xlN);
479: VecGetSize(xu,&xuN);
480: VecGetSize(snes->vec_func,&N);
481: if (xlN != N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector lengths lower bound = %D solution vector = %D",xlN,N);
482: if (xuN != N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Incompatible vector lengths: upper bound = %D solution vector = %D",xuN,N);
483: }
484: PetscObjectReference((PetscObject)xl);
485: PetscObjectReference((PetscObject)xu);
486: VecDestroy(&snes->xl);
487: VecDestroy(&snes->xu);
488: snes->xl = xl;
489: snes->xu = xu;
490: VecGetLocalSize(xl,&n);
491: VecGetArrayRead(xl,&xxl);
492: VecGetArrayRead(xu,&xxu);
493: for (i=0; i<n; i++) cnt += ((xxl[i] != PETSC_NINFINITY) || (xxu[i] != PETSC_INFINITY));
495: MPIU_Allreduce(&cnt,&snes->ntruebounds,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)snes));
496: VecRestoreArrayRead(xl,&xxl);
497: VecRestoreArrayRead(xu,&xxu);
498: return(0);
499: }
501: PetscErrorCode SNESSetFromOptions_VI(PetscOptionItems *PetscOptionsObject,SNES snes)
502: {
504: PetscBool flg = PETSC_FALSE;
507: PetscOptionsHead(PetscOptionsObject,"SNES VI options");
508: PetscOptionsReal("-snes_vi_zero_tolerance","Tolerance for considering x[] value to be on a bound","None",snes->vizerotolerance,&snes->vizerotolerance,NULL);
509: PetscOptionsBool("-snes_vi_monitor","Monitor all non-active variables","SNESMonitorResidual",flg,&flg,NULL);
510: if (flg) {
511: SNESMonitorSet(snes,SNESMonitorVI,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)snes)),NULL);
512: }
513: flg = PETSC_FALSE;
514: PetscOptionsBool("-snes_vi_monitor_residual","Monitor residual all non-active variables; using zero for active constraints","SNESMonitorVIResidual",flg,&flg,NULL);
515: if (flg) {
516: SNESMonitorSet(snes,SNESVIMonitorResidual,PETSC_VIEWER_DRAW_(PetscObjectComm((PetscObject)snes)),NULL);
517: }
518: PetscOptionsTail();
519: return(0);
520: }