Actual source code: ex24.c
1: /*$Id: ex24.c,v 1.25 2001/08/07 03:04:16 balay Exp $*/
3: static char help[] = "Solves PDE optimization problem of ex22.c with AD for adjoint.nn";
5: #include petscda.h
6: #include petscpf.h
7: #include petscmg.h
8: #include petscsnes.h
10: /*
12: Minimize F(w,u) such that G(w,u) = 0
14: L(w,u,lambda) = F(w,u) + lambda^T G(w,u)
16: w - design variables (what we change to get an optimal solution)
17: u - state variables (i.e. the PDE solution)
18: lambda - the Lagrange multipliers
20: U = (w u lambda)
22: fu, fw, flambda contain the gradient of L(w,u,lambda)
24: FU = (fw fu flambda)
26: In this example the PDE is
27: Uxx - u^2 = 2,
28: u(0) = w(0), thus this is the free parameter
29: u(1) = 0
30: the function we wish to minimize is
31: integral u^{2}
33: The exact solution for u is given by u(x) = x*x - 1.25*x + .25
35: Use the usual centered finite differences.
37: Note we treat the problem as non-linear though it happens to be linear
39: The lambda and u are NOT interlaced.
41: We optionally provide a preconditioner on each level from the operator
43: (1 0 0)
44: (0 J 0)
45: (0 0 J')
47:
48: */
51: extern int FormFunction(SNES,Vec,Vec,void*);
52: extern int PDEFormFunctionLocal(DALocalInfo*,PetscScalar*,PetscScalar*,PassiveScalar*);
54: typedef struct {
55: Mat J; /* Jacobian of PDE system */
56: SLES sles; /* Solver for that Jacobian */
57: } AppCtx;
59: int myPCApply(DMMG dmmg,Vec x,Vec y)
60: {
61: Vec xu,xlambda,yu,ylambda;
62: PetscScalar *xw,*yw;
63: int ierr;
64: VecPack packer = (VecPack)dmmg->dm;
65: AppCtx *appctx = (AppCtx*)dmmg->user;
68: VecPackGetAccess(packer,x,&xw,&xu,&xlambda);
69: VecPackGetAccess(packer,y,&yw,&yu,&ylambda);
70: if (yw && xw) {
71: yw[0] = xw[0];
72: }
73: SLESSolve(appctx->sles,xu,yu,PETSC_IGNORE);
74: SLESSolveTranspose(appctx->sles,xlambda,ylambda,PETSC_IGNORE);
75: /* VecCopy(xu,yu);
76: VecCopy(xlambda,ylambda); */
77: VecPackRestoreAccess(packer,x,&xw,&xu,&xlambda);
78: VecPackRestoreAccess(packer,y,&yw,&yu,&ylambda);
79: return(0);
80: }
82: int myPCView(DMMG dmmg,PetscViewer v)
83: {
84: int ierr;
85: AppCtx *appctx = (AppCtx*)dmmg->user;
88: SLESView(appctx->sles,v);
89: return(0);
90: }
92: int main(int argc,char **argv)
93: {
94: int ierr,nlevels,i,j;
95: DA da;
96: DMMG *dmmg;
97: VecPack packer;
98: AppCtx *appctx;
99: ISColoring iscoloring;
100: PetscTruth bdp;
102: PetscInitialize(&argc,&argv,PETSC_NULL,help);
104: /* Hardwire several options; can be changed at command line */
105: PetscOptionsSetValue("-dmmg_grid_sequence",PETSC_NULL);
106: PetscOptionsSetValue("-ksp_type","fgmres");
107: PetscOptionsSetValue("-ksp_max_it","5");
108: PetscOptionsSetValue("-pc_mg_type","full");
109: PetscOptionsSetValue("-mg_coarse_ksp_type","gmres");
110: PetscOptionsSetValue("-mg_levels_ksp_type","gmres");
111: PetscOptionsSetValue("-mg_coarse_ksp_max_it","6");
112: PetscOptionsSetValue("-mg_levels_ksp_max_it","3");
113: PetscOptionsSetValue("-snes_mf_type","wp");
114: PetscOptionsSetValue("-snes_mf_compute_norma","no");
115: PetscOptionsSetValue("-snes_mf_compute_normu","no");
116: PetscOptionsSetValue("-snes_ls","basic");
117: PetscOptionsSetValue("-dmmg_jacobian_mf_fd",0);
118: /* PetscOptionsSetValue("-snes_ls","basicnonorms"); */
119: PetscOptionsInsert(&argc,&argv,PETSC_NULL);
121: /* create VecPack object to manage composite vector */
122: VecPackCreate(PETSC_COMM_WORLD,&packer);
123: VecPackAddArray(packer,1);
124: DACreate1d(PETSC_COMM_WORLD,DA_NONPERIODIC,-5,1,1,PETSC_NULL,&da);
125: VecPackAddDA(packer,da);
126: VecPackAddDA(packer,da);
127: DADestroy(da);
129: /* create nonlinear multi-level solver */
130: DMMGCreate(PETSC_COMM_WORLD,2,PETSC_NULL,&dmmg);
131: DMMGSetDM(dmmg,(DM)packer);
132: VecPackDestroy(packer);
134: /* Create Jacobian of PDE function for each level */
135: nlevels = DMMGGetLevels(dmmg);
136: for (i=0; i<nlevels; i++) {
137: packer = (VecPack)dmmg[i]->dm;
138: ierr = VecPackGetEntries(packer,PETSC_NULL,&da,PETSC_NULL);
139: ierr = PetscNew(AppCtx,&appctx);
140: ierr = DAGetColoring(da,IS_COLORING_GHOSTED,&iscoloring);
141: ierr = DAGetMatrix(da,MATMPIAIJ,&appctx->J);
142: ierr = MatSetColoring(appctx->J,iscoloring);
143: ierr = ISColoringDestroy(iscoloring);
144: ierr = DASetLocalFunction(da,(DALocalFunction1)PDEFormFunctionLocal);
145: ierr = DASetLocalAdicFunction(da,ad_PDEFormFunctionLocal);
146: dmmg[i]->user = (void*)appctx;
147: }
149: DMMGSetSNES(dmmg,FormFunction,PETSC_NULL);
151: PetscOptionsHasName(PETSC_NULL,"-bdp",&bdp);
152: if (bdp) {
153: for (i=0; i<nlevels; i++) {
154: SLES sles;
155: PC pc,mpc;
157: appctx = (AppCtx*) dmmg[i]->user;
158: ierr = SLESCreate(PETSC_COMM_WORLD,&appctx->sles);
159: ierr = SLESSetOptionsPrefix(appctx->sles,"bdp_");
160: ierr = SLESSetFromOptions(appctx->sles);
162: SNESGetSLES(dmmg[i]->snes,&sles);
163: SLESGetPC(sles,&pc);
164: for (j=0; j<=i; j++) {
165: MGGetSmoother(pc,j,&sles);
166: SLESGetPC(sles,&mpc);
167: PCSetType(mpc,PCSHELL);
168: PCShellSetApply(mpc,(int (*)(void*,Vec,Vec))myPCApply,dmmg[j]);
169: PCShellSetView(mpc,(int (*)(void*,PetscViewer))myPCView);
170: }
171: }
172: }
174: DMMGSolve(dmmg);
176: /* VecView(DMMGGetx(dmmg),PETSC_VIEWER_SOCKET_WORLD); */
177: for (i=0; i<nlevels; i++) {
178: appctx = (AppCtx*)dmmg[i]->user;
179: ierr = MatDestroy(appctx->J);
180: if (appctx->sles) {SLESDestroy(appctx->sles);}
181: ierr = PetscFree(appctx);
182: }
183: DMMGDestroy(dmmg);
185: PetscFinalize();
186: return 0;
187: }
188:
189: /*
190: Enforces the PDE on the grid
191: This local function acts on the ghosted version of U (accessed via DAGetLocalVector())
192: BUT the global, nonghosted version of FU
194: Process adiC: PDEFormFunctionLocal
195: */
196: int PDEFormFunctionLocal(DALocalInfo *info,PetscScalar *u,PetscScalar *fu,PassiveScalar *w)
197: {
198: int xs = info->xs,xm = info->xm,i,mx = info->mx;
199: PetscScalar d,h;
201: d = mx-1.0;
202: h = 1.0/d;
204: for (i=xs; i<xs+xm; i++) {
205: if (i == 0) fu[i] = 2.0*d*(u[i] - w[0]) + h*u[i]*u[i];
206: else if (i == mx-1) fu[i] = 2.0*d*u[i] + h*u[i]*u[i];
207: else fu[i] = -(d*(u[i+1] - 2.0*u[i] + u[i-1]) - 2.0*h) + h*u[i]*u[i];
208: }
210: PetscLogFlops(9*mx);
211: return 0;
212: }
214: /*
215: Evaluates FU = Gradiant(L(w,u,lambda))
217: This is the function that is usually passed to the SNESSetJacobian() or DMMGSetSNES() and
218: defines the nonlinear set of equations that are to be solved.
220: This local function acts on the ghosted version of U (accessed via VecPackGetLocalVectors() and
221: VecPackScatter()) BUT the global, nonghosted version of FU (via VecPackAccess()).
223: This function uses PDEFormFunction() to enforce the PDE constraint equations and its adjoint
224: for the Lagrange multiplier equations
226: */
227: int FormFunction(SNES snes,Vec U,Vec FU,void* dummy)
228: {
229: DMMG dmmg = (DMMG)dummy;
230: int ierr,xs,xm,i,N,nredundant;
231: PetscScalar *u,*w,*fw,*fu,*lambda,*flambda,d,h,h2;
232: Vec vu,vlambda,vfu,vflambda,vglambda;
233: DA da;
234: VecPack packer = (VecPack)dmmg->dm;
235: AppCtx *appctx = (AppCtx*)dmmg->user;
236: PetscTruth skipadic;
239: PetscOptionsHasName(0,"-skipadic",&skipadic);
241: VecPackGetEntries(packer,&nredundant,&da,PETSC_IGNORE);
242: DAGetCorners(da,&xs,PETSC_NULL,PETSC_NULL,&xm,PETSC_NULL,PETSC_NULL);
243: DAGetInfo(da,0,&N,0,0,0,0,0,0,0,0,0);
244: d = (N-1.0);
245: h = 1.0/d;
246: h2 = 2.0*h;
248: VecPackGetLocalVectors(packer,&w,&vu,&vlambda);
249: VecPackScatter(packer,U,w,vu,vlambda);
250: VecPackGetAccess(packer,FU,&fw,&vfu,&vflambda);
251: VecPackGetAccess(packer,U,0,0,&vglambda);
253: /* G() */
254: DAFormFunction1(da,vu,vfu,w);
255: if (!skipadic) {
256: /* lambda^T G_u() */
257: DAComputeJacobian1WithAdic(da,vu,appctx->J,w);
258: if (appctx->sles) {
259: SLESSetOperators(appctx->sles,appctx->J,appctx->J,SAME_NONZERO_PATTERN);
260: }
261: MatMultTranspose(appctx->J,vglambda,vflambda);
262: }
264: DAVecGetArray(da,vu,(void**)&u);
265: DAVecGetArray(da,vfu,(void**)&fu);
266: DAVecGetArray(da,vlambda,(void**)&lambda);
267: DAVecGetArray(da,vflambda,(void**)&flambda);
269: /* L_w */
270: if (xs == 0) { /* only first processor computes this */
271: fw[0] = -2.*d*lambda[0];
272: }
274: /* lambda^T G_u() */
275: if (skipadic) {
276: for (i=xs; i<xs+xm; i++) {
277: if (i == 0) flambda[0] = 2.*d*lambda[0] - d*lambda[1] + h2*lambda[0]*u[0];
278: else if (i == 1) flambda[1] = 2.*d*lambda[1] - d*lambda[2] + h2*lambda[1]*u[1];
279: else if (i == N-1) flambda[N-1] = 2.*d*lambda[N-1] - d*lambda[N-2] + h2*lambda[N-1]*u[N-1];
280: else if (i == N-2) flambda[N-2] = 2.*d*lambda[N-2] - d*lambda[N-3] + h2*lambda[N-2]*u[N-2];
281: else flambda[i] = - d*(lambda[i+1] - 2.0*lambda[i] + lambda[i-1]) + h2*lambda[i]*u[i];
282: }
283: }
285: /* F_u */
286: for (i=xs; i<xs+xm; i++) {
287: if (i == 0) flambda[0] += h*u[0];
288: else if (i == 1) flambda[1] += h2*u[1];
289: else if (i == N-1) flambda[N-1] += h*u[N-1];
290: else if (i == N-2) flambda[N-2] += h2*u[N-2];
291: else flambda[i] += h2*u[i];
292: }
294: DAVecRestoreArray(da,vu,(void**)&u);
295: DAVecRestoreArray(da,vfu,(void**)&fu);
296: DAVecRestoreArray(da,vlambda,(void**)&lambda);
297: DAVecRestoreArray(da,vflambda,(void**)&flambda);
299: VecPackRestoreLocalVectors(packer,&w,&vu,&vlambda);
300: VecPackRestoreAccess(packer,FU,&fw,&vfu,&vflambda);
301: VecPackRestoreAccess(packer,U,0,0,&vglambda);
303: PetscLogFlops(9*N);
304: return(0);
305: }