Actual source code: ex15.c
petsc-dev 2014-02-02
1: #include <petscsnes.h>
2: #include <petscdmda.h>
3: #include <../src/snes/impls/vi/viimpl.h>
5: static char help[]=
6: "This example is an implementation of the journal bearing problem from TAO package\n\
7: (src/bound/examples/tutorials/jbearing.c). This example is based on \n\
8: the problem DPJB from the MINPACK-2 test suite. This pressure journal \n\
9: bearing problem is an example of elliptic variational problem defined over \n\
10: a two dimensional rectangle. By discretizing the domain into triangular \n\
11: elements, the pressure surrounding the journal bearing is defined as the \n\
12: minimum of a quadratic function whose variables are bounded below by zero.\n";
14: typedef struct {
15: /* problem parameters */
16: PetscReal ecc; /* test problem parameter */
17: PetscReal b; /* A dimension of journal bearing */
18: PetscInt nx,ny; /* discretization in x, y directions */
19: DM da; /* distributed array data structure */
20: Mat A; /* Quadratic Objective term */
21: Vec B; /* Linear Objective term */
22: } AppCtx;
24: /* User-defined routines */
25: static PetscReal p(PetscReal xi,PetscReal ecc);
26: PetscErrorCode FormGradient(SNES,Vec,Vec,void*);
27: PetscErrorCode FormHessian(SNES,Vec,Mat*,Mat*,MatStructure*,void*);
28: PetscErrorCode ComputeB(AppCtx*);
32: int main(int argc, char **argv)
33: {
34: PetscErrorCode ierr; /* used to check for functions returning nonzeros */
35: Vec x; /* variables vector */
36: Vec xl,xu; /* lower and upper bound on variables */
37: PetscBool flg; /* A return variable when checking for user options */
38: SNESConvergedReason reason;
39: AppCtx user; /* user-defined work context */
40: SNES snes;
41: Vec r;
42: PetscReal zero=0.0,thnd=1000;
45: /* Initialize PETSC */
46: PetscInitialize(&argc, &argv,(char*)0,help);
48: #if defined(PETSC_USE_COMPLEX)
49: SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"This example does not work for scalar type complex\n");
50: #endif
52: /* Set the default values for the problem parameters */
53: user.nx = 50; user.ny = 50; user.ecc = 0.1; user.b = 10.0;
55: /* Check for any command line arguments that override defaults */
56: PetscOptionsGetReal(NULL,"-ecc",&user.ecc,&flg);
57: PetscOptionsGetReal(NULL,"-b",&user.b,&flg);
59: /*
60: A two dimensional distributed array will help define this problem,
61: which derives from an elliptic PDE on two dimensional domain. From
62: the distributed array, Create the vectors.
63: */
64: DMDACreate2d(PETSC_COMM_WORLD, DMDA_BOUNDARY_NONE, DMDA_BOUNDARY_NONE,DMDA_STENCIL_STAR,-50,-50,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,&user.da);
65: DMDAGetIerr(user.da,PETSC_IGNORE,&user.nx,&user.ny,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);
67: PetscPrintf(PETSC_COMM_WORLD,"\n---- Journal Bearing Problem -----\n");
68: PetscPrintf(PETSC_COMM_WORLD,"mx: %d, my: %d, ecc: %4.3f, b:%3.1f \n",
69: user.nx,user.ny,user.ecc,user.b);
70: /*
71: Extract global and local vectors from DA; the vector user.B is
72: used solely as work space for the evaluation of the function,
73: gradient, and Hessian. Duplicate for remaining vectors that are
74: the same types.
75: */
76: DMCreateGlobalVector(user.da,&x); /* Solution */
77: VecDuplicate(x,&user.B); /* Linear objective */
78: VecDuplicate(x,&r);
80: /* Create matrix user.A to store quadratic, Create a local ordering scheme. */
81: DMSetMatType(user.da,MATAIJ);
82: DMCreateMatrix(user.da,&user.A);
84: /* User defined function -- compute linear term of quadratic */
85: ComputeB(&user);
87: /* Create nonlinear solver context */
88: SNESCreate(PETSC_COMM_WORLD,&snes);
90: /* Set function evaluation and Jacobian evaluation routines */
91: SNESSetFunction(snes,r,FormGradient,&user);
92: SNESSetJacobian(snes,user.A,user.A,FormHessian,&user);
94: /* Set the initial solution guess */
95: VecSet(x, zero);
97: SNESSetFromOptions(snes);
99: /* Set variable bounds */
100: VecDuplicate(x,&xl);
101: VecDuplicate(x,&xu);
102: VecSet(xl,zero);
103: VecSet(xu,thnd);
104: SNESVISetVariableBounds(snes,xl,xu);
106: /* Solve the application */
107: SNESSolve(snes,NULL,x);
109: SNESGetConvergedReason(snes,&reason);
110: if (reason <= 0) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"The SNESVI solver did not converge, adjust some parameters, or check the function evaluation routines\n");
112: /* Free memory */
113: VecDestroy(&x);
114: VecDestroy(&xl);
115: VecDestroy(&xu);
116: VecDestroy(&r);
117: MatDestroy(&user.A);
118: VecDestroy(&user.B);
119: DMDestroy(&user.da);
120: SNESDestroy(&snes);
122: PetscFinalize();
124: return 0;
125: }
127: static PetscReal p(PetscReal xi, PetscReal ecc)
128: {
129: PetscReal t=1.0+ecc*PetscCosReal(xi);
130: return(t*t*t);
131: }
135: PetscErrorCode ComputeB(AppCtx *user)
136: {
138: PetscInt i,j;
139: PetscInt nx,ny,xs,xm,ys,ym;
140: PetscReal two=2.0, pi=4.0*atan(1.0);
141: PetscReal hx,hy,ehxhy;
142: PetscReal temp;
143: PetscReal ecc=user->ecc;
144: PetscReal **b;
147: nx = user->nx;
148: ny = user->ny;
149: hx = two*pi/(nx+1.0);
150: hy = two*user->b/(ny+1.0);
151: ehxhy = ecc*hx*hy;
153: /* Get pointer to local vector data */
154: DMDAVecGetArray(user->da,user->B, &b);
156: DMDAGetCorners(user->da,&xs,&ys,NULL,&xm,&ym,NULL);
158: /* Compute the linear term in the objective function */
159: for (i=xs; i<xs+xm; i++) {
160: temp=PetscSinReal((i+1)*hx);
161: for (j=ys; j<ys+ym; j++) b[j][i] = -ehxhy*temp;
162: }
163: /* Restore vectors */
164: DMDAVecRestoreArray(user->da,user->B,&b);
165: PetscLogFlops(5*xm*ym+3*xm);
166: return(0);
167: }
171: PetscErrorCode FormGradient(SNES snes, Vec X, Vec G,void *ctx)
172: {
173: AppCtx *user=(AppCtx*)ctx;
175: PetscInt i,j,k,kk;
176: PetscInt row[5],col[5];
177: PetscInt nx,ny,xs,xm,ys,ym;
178: PetscReal one=1.0, two=2.0, six=6.0,pi=4.0*atan(1.0);
179: PetscReal hx,hy,hxhy,hxhx,hyhy;
180: PetscReal xi,v[5];
181: PetscReal ecc=user->ecc, trule1,trule2,trule3,trule4,trule5,trule6;
182: PetscReal vmiddle, vup, vdown, vleft, vright;
183: PetscReal tt;
184: PetscReal **x,**g;
185: PetscReal zero=0.0;
186: Vec localX;
189: nx = user->nx;
190: ny = user->ny;
191: hx = two*pi/(nx+1.0);
192: hy = two*user->b/(ny+1.0);
193: hxhy = hx*hy;
194: hxhx = one/(hx*hx);
195: hyhy = one/(hy*hy);
197: VecSet(G, zero);
199: /* Get local vector */
200: DMGetLocalVector(user->da,&localX);
201: /* Get ghoist points */
202: DMGlobalToLocalBegin(user->da,X,INSERT_VALUES,localX);
203: DMGlobalToLocalEnd(user->da,X,INSERT_VALUES,localX);
204: /* Get pointer to vector data */
205: DMDAVecGetArray(user->da,localX,&x);
206: DMDAVecGetArray(user->da,G,&g);
208: DMDAGetCorners(user->da,&xs,&ys,NULL,&xm,&ym,NULL);
210: for (i=xs; i< xs+xm; i++) {
211: xi = (i+1)*hx;
212: trule1 = hxhy*(p(xi,ecc) + p(xi+hx,ecc) + p(xi,ecc)) / six; /* L(i,j) */
213: trule2 = hxhy*(p(xi,ecc) + p(xi-hx,ecc) + p(xi,ecc)) / six; /* U(i,j) */
214: trule3 = hxhy*(p(xi,ecc) + p(xi+hx,ecc) + p(xi+hx,ecc)) / six; /* U(i+1,j) */
215: trule4 = hxhy*(p(xi,ecc) + p(xi-hx,ecc) + p(xi-hx,ecc)) / six; /* L(i-1,j) */
216: trule5 = trule1; /* L(i,j-1) */
217: trule6 = trule2; /* U(i,j+1) */
219: vdown = -(trule5+trule2)*hyhy;
220: vleft = -hxhx*(trule2+trule4);
221: vright = -hxhx*(trule1+trule3);
222: vup = -hyhy*(trule1+trule6);
223: vmiddle = (hxhx)*(trule1+trule2+trule3+trule4)+hyhy*(trule1+trule2+trule5+trule6);
225: for (j=ys; j<ys+ym; j++) {
227: v[0]=0; v[1]=0; v[2]=0; v[3]=0; v[4]=0;
229: k=0;
230: if (j > 0) {
231: v[k]=vdown; row[k] = i; col[k] = j-1; k++;
232: }
234: if (i > 0) {
235: v[k]= vleft; row[k] = i-1; col[k] = j; k++;
236: }
238: v[k]= vmiddle; row[k] = i; col[k] = j; k++;
240: if (i+1 < nx) {
241: v[k]= vright; row[k] = i+1; col[k] = j; k++;
242: }
244: if (j+1 < ny) {
245: v[k]= vup; row[k] = i; col[k] = j+1; k++;
246: }
247: tt=0;
248: for (kk=0; kk<k; kk++) tt+=v[kk]*x[col[kk]][row[kk]];
249: g[j][i] = tt;
251: }
253: }
255: /* Restore vectors */
256: DMDAVecRestoreArray(user->da,localX, &x);
257: DMDAVecRestoreArray(user->da,G, &g);
258: DMRestoreLocalVector(user->da,&localX);
260: VecAXPY(G, one, user->B);
262: PetscLogFlops((91 + 10*ym) * xm);
263: return(0);
264: }
270: /*
271: FormHessian computes the quadratic term in the quadratic objective function
272: Notice that the objective function in this problem is quadratic (therefore a constant
273: hessian). If using a nonquadratic solver, then you might want to reconsider this function
274: */
275: PetscErrorCode FormHessian(SNES snes,Vec X,Mat *H, Mat *Hpre, MatStructure *flg, void *ptr)
276: {
277: AppCtx *user=(AppCtx*)ptr;
279: PetscInt i,j,k;
280: MatStencil row,col[5];
281: PetscInt nx,ny,xs,xm,ys,ym;
282: PetscReal one=1.0, two=2.0, six=6.0,pi=4.0*atan(1.0);
283: PetscReal hx,hy,hxhy,hxhx,hyhy;
284: PetscReal xi,v[5];
285: PetscReal ecc=user->ecc, trule1,trule2,trule3,trule4,trule5,trule6;
286: PetscReal vmiddle, vup, vdown, vleft, vright;
287: Mat hes=*H;
288: PetscBool assembled;
289: PetscReal **x;
290: Vec localX;
293: nx = user->nx;
294: ny = user->ny;
295: hx = two*pi/(nx+1.0);
296: hy = two*user->b/(ny+1.0);
297: hxhy = hx*hy;
298: hxhx = one/(hx*hx);
299: hyhy = one/(hy*hy);
301: MatAssembled(hes,&assembled);
302: if (assembled) {MatZeroEntries(hes);}
303: *flg=SAME_NONZERO_PATTERN;
305: /* Get local vector */
306: DMGetLocalVector(user->da,&localX);
307: /* Get ghost points */
308: DMGlobalToLocalBegin(user->da,X,INSERT_VALUES,localX);
309: DMGlobalToLocalEnd(user->da,X,INSERT_VALUES,localX);
311: /* Get pointers to vector data */
312: DMDAVecGetArray(user->da,localX, &x);
314: DMDAGetCorners(user->da,&xs,&ys,NULL,&xm,&ym,NULL);
316: for (i=xs; i< xs+xm; i++) {
317: xi = (i+1)*hx;
318: trule1 = hxhy*(p(xi,ecc) + p(xi+hx,ecc) + p(xi,ecc)) / six; /* L(i,j) */
319: trule2 = hxhy*(p(xi,ecc) + p(xi-hx,ecc) + p(xi,ecc)) / six; /* U(i,j) */
320: trule3 = hxhy*(p(xi,ecc) + p(xi+hx,ecc) + p(xi+hx,ecc)) / six; /* U(i+1,j) */
321: trule4 = hxhy*(p(xi,ecc) + p(xi-hx,ecc) + p(xi-hx,ecc)) / six; /* L(i-1,j) */
322: trule5 = trule1; /* L(i,j-1) */
323: trule6 = trule2; /* U(i,j+1) */
325: vdown = -(trule5+trule2)*hyhy;
326: vleft = -hxhx*(trule2+trule4);
327: vright = -hxhx*(trule1+trule3);
328: vup = -hyhy*(trule1+trule6);
329: vmiddle = (hxhx)*(trule1+trule2+trule3+trule4)+hyhy*(trule1+trule2+trule5+trule6);
331: v[0]=0; v[1]=0; v[2]=0; v[3]=0; v[4]=0;
333: for (j=ys; j<ys+ym; j++) {
334: k =0;
335: row.i = i; row.j = j;
336: if (j > 0) {
337: v[k]=vdown; col[k].i=i;col[k].j = j-1; k++;
338: }
340: if (i > 0) {
341: v[k]= vleft; col[k].i= i-1; col[k].j = j;k++;
342: }
344: v[k]= vmiddle; col[k].i=i; col[k].j = j;k++;
346: if (i+1 < nx) {
347: v[k]= vright; col[k].i = i+1; col[k].j = j; k++;
348: }
350: if (j+1 < ny) {
351: v[k]= vup; col[k].i = i; col[k].j = j+1; k++;
352: }
353: MatSetValuesStencil(hes,1,&row,k,col,v,INSERT_VALUES);
354: }
355: }
357: MatAssemblyBegin(hes,MAT_FINAL_ASSEMBLY);
358: DMDAVecRestoreArray(user->da,localX,&x);
359: MatAssemblyEnd(hes,MAT_FINAL_ASSEMBLY);
360: DMRestoreLocalVector(user->da,&localX);
362: /*
363: Tell the matrix we will never add a new nonzero location to the
364: matrix. If we do it will generate an error.
365: */
366: MatSetOption(hes,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
367: MatSetOption(hes,MAT_SYMMETRIC,PETSC_TRUE);
369: PetscLogFlops(9*xm*ym+49*xm);
370: return(0);
371: }