Actual source code: lcl.c

  1: #include <../src/tao/pde_constrained/impls/lcl/lcl.h>
  2: static PetscErrorCode LCLComputeLagrangianAndGradient(TaoLineSearch,Vec,PetscReal*,Vec,void*);
  3: static PetscErrorCode LCLComputeAugmentedLagrangianAndGradient(TaoLineSearch,Vec,PetscReal*,Vec,void*);
  4: static PetscErrorCode LCLScatter(TAO_LCL*,Vec,Vec,Vec);
  5: static PetscErrorCode LCLGather(TAO_LCL*,Vec,Vec,Vec);

  7: static PetscErrorCode TaoDestroy_LCL(Tao tao)
  8: {
  9:   TAO_LCL        *lclP = (TAO_LCL*)tao->data;

 13:   if (tao->setupcalled) {
 14:     MatDestroy(&lclP->R);
 15:     VecDestroy(&lclP->lamda);
 16:     VecDestroy(&lclP->lamda0);
 17:     VecDestroy(&lclP->WL);
 18:     VecDestroy(&lclP->W);
 19:     VecDestroy(&lclP->X0);
 20:     VecDestroy(&lclP->G0);
 21:     VecDestroy(&lclP->GL);
 22:     VecDestroy(&lclP->GAugL);
 23:     VecDestroy(&lclP->dbar);
 24:     VecDestroy(&lclP->U);
 25:     VecDestroy(&lclP->U0);
 26:     VecDestroy(&lclP->V);
 27:     VecDestroy(&lclP->V0);
 28:     VecDestroy(&lclP->V1);
 29:     VecDestroy(&lclP->GU);
 30:     VecDestroy(&lclP->GV);
 31:     VecDestroy(&lclP->GU0);
 32:     VecDestroy(&lclP->GV0);
 33:     VecDestroy(&lclP->GL_U);
 34:     VecDestroy(&lclP->GL_V);
 35:     VecDestroy(&lclP->GAugL_U);
 36:     VecDestroy(&lclP->GAugL_V);
 37:     VecDestroy(&lclP->GL_U0);
 38:     VecDestroy(&lclP->GL_V0);
 39:     VecDestroy(&lclP->GAugL_U0);
 40:     VecDestroy(&lclP->GAugL_V0);
 41:     VecDestroy(&lclP->DU);
 42:     VecDestroy(&lclP->DV);
 43:     VecDestroy(&lclP->WU);
 44:     VecDestroy(&lclP->WV);
 45:     VecDestroy(&lclP->g1);
 46:     VecDestroy(&lclP->g2);
 47:     VecDestroy(&lclP->con1);

 49:     VecDestroy(&lclP->r);
 50:     VecDestroy(&lclP->s);

 52:     ISDestroy(&tao->state_is);
 53:     ISDestroy(&tao->design_is);

 55:     VecScatterDestroy(&lclP->state_scatter);
 56:     VecScatterDestroy(&lclP->design_scatter);
 57:   }
 58:   MatDestroy(&lclP->R);
 59:   PetscFree(tao->data);
 60:   return(0);
 61: }

 63: static PetscErrorCode TaoSetFromOptions_LCL(PetscOptionItems *PetscOptionsObject,Tao tao)
 64: {
 65:   TAO_LCL        *lclP = (TAO_LCL*)tao->data;

 69:   PetscOptionsHead(PetscOptionsObject,"Linearly-Constrained Augmented Lagrangian Method for PDE-constrained optimization");
 70:   PetscOptionsReal("-tao_lcl_eps1","epsilon 1 tolerance","",lclP->eps1,&lclP->eps1,NULL);
 71:   PetscOptionsReal("-tao_lcl_eps2","epsilon 2 tolerance","",lclP->eps2,&lclP->eps2,NULL);
 72:   PetscOptionsReal("-tao_lcl_rho0","init value for rho","",lclP->rho0,&lclP->rho0,NULL);
 73:   PetscOptionsReal("-tao_lcl_rhomax","max value for rho","",lclP->rhomax,&lclP->rhomax,NULL);
 74:   lclP->phase2_niter = 1;
 75:   PetscOptionsInt("-tao_lcl_phase2_niter","Number of phase 2 iterations in LCL algorithm","",lclP->phase2_niter,&lclP->phase2_niter,NULL);
 76:   lclP->verbose = PETSC_FALSE;
 77:   PetscOptionsBool("-tao_lcl_verbose","Print verbose output","",lclP->verbose,&lclP->verbose,NULL);
 78:   lclP->tau[0] = lclP->tau[1] = lclP->tau[2] = lclP->tau[3] = 1.0e-4;
 79:   PetscOptionsReal("-tao_lcl_tola","Tolerance for first forward solve","",lclP->tau[0],&lclP->tau[0],NULL);
 80:   PetscOptionsReal("-tao_lcl_tolb","Tolerance for first adjoint solve","",lclP->tau[1],&lclP->tau[1],NULL);
 81:   PetscOptionsReal("-tao_lcl_tolc","Tolerance for second forward solve","",lclP->tau[2],&lclP->tau[2],NULL);
 82:   PetscOptionsReal("-tao_lcl_told","Tolerance for second adjoint solve","",lclP->tau[3],&lclP->tau[3],NULL);
 83:   PetscOptionsTail();
 84:   TaoLineSearchSetFromOptions(tao->linesearch);
 85:   MatSetFromOptions(lclP->R);
 86:   return(0);
 87: }

 89: static PetscErrorCode TaoView_LCL(Tao tao, PetscViewer viewer)
 90: {
 91:   return 0;
 92: }

 94: static PetscErrorCode TaoSetup_LCL(Tao tao)
 95: {
 96:   TAO_LCL        *lclP = (TAO_LCL*)tao->data;
 97:   PetscInt       lo, hi, nlocalstate, nlocaldesign;
 99:   IS             is_state, is_design;

102:   if (!tao->state_is) SETERRQ(PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"LCL Solver requires an initial state index set -- use TaoSetStateIS()");
103:   VecDuplicate(tao->solution, &tao->gradient);
104:   VecDuplicate(tao->solution, &tao->stepdirection);
105:   VecDuplicate(tao->solution, &lclP->W);
106:   VecDuplicate(tao->solution, &lclP->X0);
107:   VecDuplicate(tao->solution, &lclP->G0);
108:   VecDuplicate(tao->solution, &lclP->GL);
109:   VecDuplicate(tao->solution, &lclP->GAugL);

111:   VecDuplicate(tao->constraints, &lclP->lamda);
112:   VecDuplicate(tao->constraints, &lclP->WL);
113:   VecDuplicate(tao->constraints, &lclP->lamda0);
114:   VecDuplicate(tao->constraints, &lclP->con1);

116:   VecSet(lclP->lamda,0.0);

118:   VecGetSize(tao->solution, &lclP->n);
119:   VecGetSize(tao->constraints, &lclP->m);

121:   VecCreate(((PetscObject)tao)->comm,&lclP->U);
122:   VecCreate(((PetscObject)tao)->comm,&lclP->V);
123:   ISGetLocalSize(tao->state_is,&nlocalstate);
124:   ISGetLocalSize(tao->design_is,&nlocaldesign);
125:   VecSetSizes(lclP->U,nlocalstate,lclP->m);
126:   VecSetSizes(lclP->V,nlocaldesign,lclP->n-lclP->m);
127:   VecSetType(lclP->U,((PetscObject)(tao->solution))->type_name);
128:   VecSetType(lclP->V,((PetscObject)(tao->solution))->type_name);
129:   VecSetFromOptions(lclP->U);
130:   VecSetFromOptions(lclP->V);
131:   VecDuplicate(lclP->U,&lclP->DU);
132:   VecDuplicate(lclP->U,&lclP->U0);
133:   VecDuplicate(lclP->U,&lclP->GU);
134:   VecDuplicate(lclP->U,&lclP->GU0);
135:   VecDuplicate(lclP->U,&lclP->GAugL_U);
136:   VecDuplicate(lclP->U,&lclP->GL_U);
137:   VecDuplicate(lclP->U,&lclP->GAugL_U0);
138:   VecDuplicate(lclP->U,&lclP->GL_U0);
139:   VecDuplicate(lclP->U,&lclP->WU);
140:   VecDuplicate(lclP->U,&lclP->r);
141:   VecDuplicate(lclP->V,&lclP->V0);
142:   VecDuplicate(lclP->V,&lclP->V1);
143:   VecDuplicate(lclP->V,&lclP->DV);
144:   VecDuplicate(lclP->V,&lclP->s);
145:   VecDuplicate(lclP->V,&lclP->GV);
146:   VecDuplicate(lclP->V,&lclP->GV0);
147:   VecDuplicate(lclP->V,&lclP->dbar);
148:   VecDuplicate(lclP->V,&lclP->GAugL_V);
149:   VecDuplicate(lclP->V,&lclP->GL_V);
150:   VecDuplicate(lclP->V,&lclP->GAugL_V0);
151:   VecDuplicate(lclP->V,&lclP->GL_V0);
152:   VecDuplicate(lclP->V,&lclP->WV);
153:   VecDuplicate(lclP->V,&lclP->g1);
154:   VecDuplicate(lclP->V,&lclP->g2);

156:   /* create scatters for state, design subvecs */
157:   VecGetOwnershipRange(lclP->U,&lo,&hi);
158:   ISCreateStride(((PetscObject)lclP->U)->comm,hi-lo,lo,1,&is_state);
159:   VecGetOwnershipRange(lclP->V,&lo,&hi);
160:   if (0) {
161:     PetscInt sizeU,sizeV;
162:     VecGetSize(lclP->U,&sizeU);
163:     VecGetSize(lclP->V,&sizeV);
164:     PetscPrintf(PETSC_COMM_WORLD,"size(U)=%D, size(V)=%D\n",sizeU,sizeV);
165:   }
166:   ISCreateStride(((PetscObject)lclP->V)->comm,hi-lo,lo,1,&is_design);
167:   VecScatterCreate(tao->solution,tao->state_is,lclP->U,is_state,&lclP->state_scatter);
168:   VecScatterCreate(tao->solution,tao->design_is,lclP->V,is_design,&lclP->design_scatter);
169:   ISDestroy(&is_state);
170:   ISDestroy(&is_design);
171:   return(0);
172: }

174: static PetscErrorCode TaoSolve_LCL(Tao tao)
175: {
176:   TAO_LCL                      *lclP = (TAO_LCL*)tao->data;
177:   PetscInt                     phase2_iter,nlocal,its;
178:   TaoLineSearchConvergedReason ls_reason = TAOLINESEARCH_CONTINUE_ITERATING;
179:   PetscReal                    step=1.0,f, descent, aldescent;
180:   PetscReal                    cnorm, mnorm;
181:   PetscReal                    adec,r2,rGL_U,rWU;
182:   PetscBool                    set,pset,flag,pflag,symmetric;
183:   PetscErrorCode               ierr;

186:   lclP->rho = lclP->rho0;
187:   VecGetLocalSize(lclP->U,&nlocal);
188:   VecGetLocalSize(lclP->V,&nlocal);
189:   MatSetSizes(lclP->R, nlocal, nlocal, lclP->n-lclP->m, lclP->n-lclP->m);
190:   MatLMVMAllocate(lclP->R,lclP->V,lclP->V);
191:   lclP->recompute_jacobian_flag = PETSC_TRUE;

193:   /* Scatter to U,V */
194:   LCLScatter(lclP,tao->solution,lclP->U,lclP->V);

196:   /* Evaluate Function, Gradient, Constraints, and Jacobian */
197:   TaoComputeObjectiveAndGradient(tao,tao->solution,&f,tao->gradient);
198:   TaoComputeJacobianState(tao,tao->solution,tao->jacobian_state,tao->jacobian_state_pre,tao->jacobian_state_inv);
199:   TaoComputeJacobianDesign(tao,tao->solution,tao->jacobian_design);
200:   TaoComputeConstraints(tao,tao->solution, tao->constraints);

202:   /* Scatter gradient to GU,GV */
203:   LCLScatter(lclP,tao->gradient,lclP->GU,lclP->GV);

205:   /* Evaluate Lagrangian function and gradient */
206:   /* p0 */
207:   VecSet(lclP->lamda,0.0); /*  Initial guess in CG */
208:   MatIsSymmetricKnown(tao->jacobian_state,&set,&flag);
209:   if (tao->jacobian_state_pre) {
210:     MatIsSymmetricKnown(tao->jacobian_state_pre,&pset,&pflag);
211:   } else {
212:     pset = pflag = PETSC_TRUE;
213:   }
214:   if (set && pset && flag && pflag) symmetric = PETSC_TRUE;
215:   else symmetric = PETSC_FALSE;

217:   lclP->solve_type = LCL_ADJOINT2;
218:   if (tao->jacobian_state_inv) {
219:     if (symmetric) {
220:       MatMult(tao->jacobian_state_inv, lclP->GU, lclP->lamda); } else {
221:       MatMultTranspose(tao->jacobian_state_inv, lclP->GU, lclP->lamda);
222:     }
223:   } else {
224:     KSPSetOperators(tao->ksp, tao->jacobian_state, tao->jacobian_state_pre);
225:     if (symmetric) {
226:       KSPSolve(tao->ksp, lclP->GU,  lclP->lamda);
227:     } else {
228:       KSPSolveTranspose(tao->ksp, lclP->GU,  lclP->lamda);
229:     }
230:     KSPGetIterationNumber(tao->ksp,&its);
231:     tao->ksp_its+=its;
232:     tao->ksp_tot_its+=its;
233:   }
234:   VecCopy(lclP->lamda,lclP->lamda0);
235:   LCLComputeAugmentedLagrangianAndGradient(tao->linesearch,tao->solution,&lclP->aug,lclP->GAugL,tao);

237:   LCLScatter(lclP,lclP->GL,lclP->GL_U,lclP->GL_V);
238:   LCLScatter(lclP,lclP->GAugL,lclP->GAugL_U,lclP->GAugL_V);

240:   /* Evaluate constraint norm */
241:   VecNorm(tao->constraints, NORM_2, &cnorm);
242:   VecNorm(lclP->GAugL, NORM_2, &mnorm);

244:   /* Monitor convergence */
245:   tao->reason = TAO_CONTINUE_ITERATING;
246:   TaoLogConvergenceHistory(tao,f,mnorm,cnorm,tao->ksp_its);
247:   TaoMonitor(tao,tao->niter,f,mnorm,cnorm,step);
248:   (*tao->ops->convergencetest)(tao,tao->cnvP);

250:   while (tao->reason == TAO_CONTINUE_ITERATING) {
251:     /* Call general purpose update function */
252:     if (tao->ops->update) {
253:       (*tao->ops->update)(tao, tao->niter, tao->user_update);
254:     }
255:     tao->ksp_its=0;
256:     /* Compute a descent direction for the linearly constrained subproblem
257:        minimize f(u+du, v+dv)
258:        s.t. A(u0,v0)du + B(u0,v0)dv = -g(u0,v0) */

260:     /* Store the points around the linearization */
261:     VecCopy(lclP->U, lclP->U0);
262:     VecCopy(lclP->V, lclP->V0);
263:     VecCopy(lclP->GU,lclP->GU0);
264:     VecCopy(lclP->GV,lclP->GV0);
265:     VecCopy(lclP->GAugL_U,lclP->GAugL_U0);
266:     VecCopy(lclP->GAugL_V,lclP->GAugL_V0);
267:     VecCopy(lclP->GL_U,lclP->GL_U0);
268:     VecCopy(lclP->GL_V,lclP->GL_V0);

270:     lclP->aug0 = lclP->aug;
271:     lclP->lgn0 = lclP->lgn;

273:     /* Given the design variables, we need to project the current iterate
274:        onto the linearized constraint.  We choose to fix the design variables
275:        and solve the linear system for the state variables.  The resulting
276:        point is the Newton direction */

278:     /* Solve r = A\con */
279:     lclP->solve_type = LCL_FORWARD1;
280:     VecSet(lclP->r,0.0); /*  Initial guess in CG */

282:     if (tao->jacobian_state_inv) {
283:       MatMult(tao->jacobian_state_inv, tao->constraints, lclP->r);
284:     } else {
285:       KSPSetOperators(tao->ksp, tao->jacobian_state, tao->jacobian_state_pre);
286:       KSPSolve(tao->ksp, tao->constraints,  lclP->r);
287:       KSPGetIterationNumber(tao->ksp,&its);
288:       tao->ksp_its+=its;
289:       tao->ksp_tot_its+=tao->ksp_its;
290:     }

292:     /* Set design step direction dv to zero */
293:     VecSet(lclP->s, 0.0);

295:     /*
296:        Check sufficient descent for constraint merit function .5*||con||^2
297:        con' Ak r >= eps1 ||r||^(2+eps2)
298:     */

300:     /* Compute WU= Ak' * con */
301:     if (symmetric)  {
302:       MatMult(tao->jacobian_state,tao->constraints,lclP->WU);
303:     } else {
304:       MatMultTranspose(tao->jacobian_state,tao->constraints,lclP->WU);
305:     }
306:     /* Compute r * Ak' * con */
307:     VecDot(lclP->r,lclP->WU,&rWU);

309:     /* compute ||r||^(2+eps2) */
310:     VecNorm(lclP->r,NORM_2,&r2);
311:     r2 = PetscPowScalar(r2,2.0+lclP->eps2);
312:     adec = lclP->eps1 * r2;

314:     if (rWU < adec) {
315:       PetscInfo(tao,"Newton direction not descent for constraint, feasibility phase required\n");
316:       if (lclP->verbose) {
317:         PetscPrintf(PETSC_COMM_WORLD,"Newton direction not descent for constraint: %g -- using steepest descent\n",(double)descent);
318:       }

320:       PetscInfo(tao,"Using steepest descent direction instead.\n");
321:       VecSet(lclP->r,0.0);
322:       VecAXPY(lclP->r,-1.0,lclP->WU);
323:       VecDot(lclP->r,lclP->r,&rWU);
324:       VecNorm(lclP->r,NORM_2,&r2);
325:       r2 = PetscPowScalar(r2,2.0+lclP->eps2);
326:       VecDot(lclP->r,lclP->GAugL_U,&descent);
327:       adec = lclP->eps1 * r2;
328:     }

330:     /*
331:        Check descent for aug. lagrangian
332:        r' (GUk - Ak'*yk - rho*Ak'*con) <= -eps1 ||r||^(2+eps2)
333:           GL_U = GUk - Ak'*yk
334:           WU   = Ak'*con
335:                                          adec=eps1||r||^(2+eps2)

337:        ==>
338:        Check r'GL_U - rho*r'WU <= adec
339:     */

341:     VecDot(lclP->r,lclP->GL_U,&rGL_U);
342:     aldescent =  rGL_U - lclP->rho*rWU;
343:     if (aldescent > -adec) {
344:       if (lclP->verbose) {
345:         PetscPrintf(PETSC_COMM_WORLD," Newton direction not descent for augmented Lagrangian: %g",(double)aldescent);
346:       }
347:       PetscInfo1(tao,"Newton direction not descent for augmented Lagrangian: %g\n",(double)aldescent);
348:       lclP->rho =  (rGL_U - adec)/rWU;
349:       if (lclP->rho > lclP->rhomax) {
350:         lclP->rho = lclP->rhomax;
351:         SETERRQ1(PETSC_COMM_WORLD,PETSC_ERR_SUP,"rho=%g > rhomax, case not implemented.  Increase rhomax (-tao_lcl_rhomax)",(double)lclP->rho);
352:       }
353:       if (lclP->verbose) {
354:         PetscPrintf(PETSC_COMM_WORLD,"  Increasing penalty parameter to %g\n",(double)lclP->rho);
355:       }
356:       PetscInfo1(tao,"  Increasing penalty parameter to %g\n",(double)lclP->rho);
357:     }

359:     LCLComputeAugmentedLagrangianAndGradient(tao->linesearch,tao->solution,&lclP->aug,lclP->GAugL,tao);
360:     LCLScatter(lclP,lclP->GL,lclP->GL_U,lclP->GL_V);
361:     LCLScatter(lclP,lclP->GAugL,lclP->GAugL_U,lclP->GAugL_V);

363:     /* We now minimize the augmented Lagrangian along the Newton direction */
364:     VecScale(lclP->r,-1.0);
365:     LCLGather(lclP, lclP->r,lclP->s,tao->stepdirection);
366:     VecScale(lclP->r,-1.0);
367:     LCLGather(lclP, lclP->GAugL_U0, lclP->GAugL_V0, lclP->GAugL);
368:     LCLGather(lclP, lclP->U0,lclP->V0,lclP->X0);

370:     lclP->recompute_jacobian_flag = PETSC_TRUE;

372:     TaoLineSearchSetInitialStepLength(tao->linesearch,1.0);
373:     TaoLineSearchSetType(tao->linesearch, TAOLINESEARCHMT);
374:     TaoLineSearchSetFromOptions(tao->linesearch);
375:     TaoLineSearchApply(tao->linesearch, tao->solution, &lclP->aug, lclP->GAugL, tao->stepdirection, &step, &ls_reason);
376:     if (lclP->verbose) {
377:       PetscPrintf(PETSC_COMM_WORLD,"Steplength = %g\n",(double)step);
378:     }

380:     LCLScatter(lclP,tao->solution,lclP->U,lclP->V);
381:     TaoComputeObjectiveAndGradient(tao,tao->solution,&f,tao->gradient);
382:     LCLScatter(lclP,tao->gradient,lclP->GU,lclP->GV);

384:     LCLScatter(lclP,lclP->GAugL,lclP->GAugL_U,lclP->GAugL_V);

386:     /* Check convergence */
387:     VecNorm(lclP->GAugL, NORM_2, &mnorm);
388:     VecNorm(tao->constraints, NORM_2, &cnorm);
389:     TaoLogConvergenceHistory(tao,f,mnorm,cnorm,tao->ksp_its);
390:     TaoMonitor(tao,tao->niter,f,mnorm,cnorm,step);
391:     (*tao->ops->convergencetest)(tao,tao->cnvP);
392:     if (tao->reason != TAO_CONTINUE_ITERATING) {
393:       break;
394:     }

396:     /* TODO: use a heuristic to choose how many iterations should be performed within phase 2 */
397:     for (phase2_iter=0; phase2_iter<lclP->phase2_niter; phase2_iter++) {
398:       /* We now minimize the objective function starting from the fraction of
399:          the Newton point accepted by applying one step of a reduced-space
400:          method.  The optimization problem is:

402:          minimize f(u+du, v+dv)
403:          s. t.    A(u0,v0)du + B(u0,v0)du = -alpha g(u0,v0)

405:          In particular, we have that
406:          du = -inv(A)*(Bdv + alpha g) */

408:       TaoComputeJacobianState(tao,lclP->X0,tao->jacobian_state,tao->jacobian_state_pre,tao->jacobian_state_inv);
409:       TaoComputeJacobianDesign(tao,lclP->X0,tao->jacobian_design);

411:       /* Store V and constraints */
412:       VecCopy(lclP->V, lclP->V1);
413:       VecCopy(tao->constraints,lclP->con1);

415:       /* Compute multipliers */
416:       /* p1 */
417:       VecSet(lclP->lamda,0.0); /*  Initial guess in CG */
418:       lclP->solve_type = LCL_ADJOINT1;
419:       MatIsSymmetricKnown(tao->jacobian_state,&set,&flag);
420:       if (tao->jacobian_state_pre) {
421:         MatIsSymmetricKnown(tao->jacobian_state_pre,&pset,&pflag);
422:       } else {
423:         pset = pflag = PETSC_TRUE;
424:       }
425:       if (set && pset && flag && pflag) symmetric = PETSC_TRUE;
426:       else symmetric = PETSC_FALSE;

428:       if (tao->jacobian_state_inv) {
429:         if (symmetric) {
430:           MatMult(tao->jacobian_state_inv, lclP->GAugL_U, lclP->lamda);
431:         } else {
432:           MatMultTranspose(tao->jacobian_state_inv, lclP->GAugL_U, lclP->lamda);
433:         }
434:       } else {
435:         if (symmetric) {
436:           KSPSolve(tao->ksp, lclP->GAugL_U,  lclP->lamda);
437:         } else {
438:           KSPSolveTranspose(tao->ksp, lclP->GAugL_U,  lclP->lamda);
439:         }
440:         KSPGetIterationNumber(tao->ksp,&its);
441:         tao->ksp_its+=its;
442:         tao->ksp_tot_its+=its;
443:       }
444:       MatMultTranspose(tao->jacobian_design,lclP->lamda,lclP->g1);
445:       VecAXPY(lclP->g1,-1.0,lclP->GAugL_V);

447:       /* Compute the limited-memory quasi-newton direction */
448:       if (tao->niter > 0) {
449:         MatSolve(lclP->R,lclP->g1,lclP->s);
450:         VecDot(lclP->s,lclP->g1,&descent);
451:         if (descent <= 0) {
452:           if (lclP->verbose) {
453:             PetscPrintf(PETSC_COMM_WORLD,"Reduced-space direction not descent: %g\n",(double)descent);
454:           }
455:           VecCopy(lclP->g1,lclP->s);
456:         }
457:       } else {
458:         VecCopy(lclP->g1,lclP->s);
459:       }
460:       VecScale(lclP->g1,-1.0);

462:       /* Recover the full space direction */
463:       MatMult(tao->jacobian_design,lclP->s,lclP->WU);
464:       /* VecSet(lclP->r,0.0); */ /*  Initial guess in CG */
465:       lclP->solve_type = LCL_FORWARD2;
466:       if (tao->jacobian_state_inv) {
467:         MatMult(tao->jacobian_state_inv,lclP->WU,lclP->r);
468:       } else {
469:         KSPSolve(tao->ksp, lclP->WU, lclP->r);
470:         KSPGetIterationNumber(tao->ksp,&its);
471:         tao->ksp_its += its;
472:         tao->ksp_tot_its+=its;
473:       }

475:       /* We now minimize the augmented Lagrangian along the direction -r,s */
476:       VecScale(lclP->r, -1.0);
477:       LCLGather(lclP,lclP->r,lclP->s,tao->stepdirection);
478:       VecScale(lclP->r, -1.0);
479:       lclP->recompute_jacobian_flag = PETSC_TRUE;

481:       TaoLineSearchSetInitialStepLength(tao->linesearch,1.0);
482:       TaoLineSearchSetType(tao->linesearch,TAOLINESEARCHMT);
483:       TaoLineSearchSetFromOptions(tao->linesearch);
484:       TaoLineSearchApply(tao->linesearch, tao->solution, &lclP->aug, lclP->GAugL, tao->stepdirection,&step,&ls_reason);
485:       if (lclP->verbose) {
486:         PetscPrintf(PETSC_COMM_WORLD,"Reduced-space steplength =  %g\n",(double)step);
487:       }

489:       LCLScatter(lclP,tao->solution,lclP->U,lclP->V);
490:       LCLScatter(lclP,lclP->GL,lclP->GL_U,lclP->GL_V);
491:       LCLScatter(lclP,lclP->GAugL,lclP->GAugL_U,lclP->GAugL_V);
492:       TaoComputeObjectiveAndGradient(tao,tao->solution,&f,tao->gradient);
493:       LCLScatter(lclP,tao->gradient,lclP->GU,lclP->GV);

495:       /* Compute the reduced gradient at the new point */

497:       TaoComputeJacobianState(tao,lclP->X0,tao->jacobian_state,tao->jacobian_state_pre,tao->jacobian_state_inv);
498:       TaoComputeJacobianDesign(tao,lclP->X0,tao->jacobian_design);

500:       /* p2 */
501:       /* Compute multipliers, using lamda-rho*con as an initial guess in PCG */
502:       if (phase2_iter==0) {
503:         VecWAXPY(lclP->lamda,-lclP->rho,lclP->con1,lclP->lamda0);
504:       } else {
505:         VecAXPY(lclP->lamda,-lclP->rho,tao->constraints);
506:       }

508:       MatIsSymmetricKnown(tao->jacobian_state,&set,&flag);
509:       if (tao->jacobian_state_pre) {
510:         MatIsSymmetricKnown(tao->jacobian_state_pre,&pset,&pflag);
511:       } else {
512:         pset = pflag = PETSC_TRUE;
513:       }
514:       if (set && pset && flag && pflag) symmetric = PETSC_TRUE;
515:       else symmetric = PETSC_FALSE;

517:       lclP->solve_type = LCL_ADJOINT2;
518:       if (tao->jacobian_state_inv) {
519:         if (symmetric) {
520:           MatMult(tao->jacobian_state_inv, lclP->GU, lclP->lamda);
521:         } else {
522:           MatMultTranspose(tao->jacobian_state_inv, lclP->GU, lclP->lamda);
523:         }
524:       } else {
525:         if (symmetric) {
526:           KSPSolve(tao->ksp, lclP->GU,  lclP->lamda);
527:         } else {
528:           KSPSolveTranspose(tao->ksp, lclP->GU,  lclP->lamda);
529:         }
530:         KSPGetIterationNumber(tao->ksp,&its);
531:         tao->ksp_its += its;
532:         tao->ksp_tot_its += its;
533:       }

535:       MatMultTranspose(tao->jacobian_design,lclP->lamda,lclP->g2);
536:       VecAXPY(lclP->g2,-1.0,lclP->GV);

538:       VecScale(lclP->g2,-1.0);

540:       /* Update the quasi-newton approximation */
541:       MatLMVMUpdate(lclP->R,lclP->V,lclP->g2);
542:       /* Use "-tao_ls_type gpcg -tao_ls_ftol 0 -tao_lmm_broyden_phi 0.0 -tao_lmm_scale_type scalar" to obtain agreement with Matlab code */

544:     }

546:     VecCopy(lclP->lamda,lclP->lamda0);

548:     /* Evaluate Function, Gradient, Constraints, and Jacobian */
549:     TaoComputeObjectiveAndGradient(tao,tao->solution,&f,tao->gradient);
550:     LCLScatter(lclP,tao->solution,lclP->U,lclP->V);
551:     LCLScatter(lclP,tao->gradient,lclP->GU,lclP->GV);

553:     TaoComputeJacobianState(tao,tao->solution,tao->jacobian_state,tao->jacobian_state_pre,tao->jacobian_state_inv);
554:     TaoComputeJacobianDesign(tao,tao->solution,tao->jacobian_design);
555:     TaoComputeConstraints(tao,tao->solution, tao->constraints);

557:     LCLComputeAugmentedLagrangianAndGradient(tao->linesearch,tao->solution,&lclP->aug,lclP->GAugL,tao);

559:     VecNorm(lclP->GAugL, NORM_2, &mnorm);

561:     /* Evaluate constraint norm */
562:     VecNorm(tao->constraints, NORM_2, &cnorm);

564:     /* Monitor convergence */
565:     tao->niter++;
566:     TaoLogConvergenceHistory(tao,f,mnorm,cnorm,tao->ksp_its);
567:     TaoMonitor(tao,tao->niter,f,mnorm,cnorm,step);
568:     (*tao->ops->convergencetest)(tao,tao->cnvP);
569:   }
570:   return(0);
571: }

573: /*MC
574:  TAOLCL - linearly constrained lagrangian method for pde-constrained optimization

576: + -tao_lcl_eps1 - epsilon 1 tolerance
577: . -tao_lcl_eps2","epsilon 2 tolerance","",lclP->eps2,&lclP->eps2,NULL);
578: . -tao_lcl_rho0","init value for rho","",lclP->rho0,&lclP->rho0,NULL);
579: . -tao_lcl_rhomax","max value for rho","",lclP->rhomax,&lclP->rhomax,NULL);
580: . -tao_lcl_phase2_niter - Number of phase 2 iterations in LCL algorithm
581: . -tao_lcl_verbose - Print verbose output if True
582: . -tao_lcl_tola - Tolerance for first forward solve
583: . -tao_lcl_tolb - Tolerance for first adjoint solve
584: . -tao_lcl_tolc - Tolerance for second forward solve
585: - -tao_lcl_told - Tolerance for second adjoint solve

587:   Level: beginner
588: M*/
589: PETSC_EXTERN PetscErrorCode TaoCreate_LCL(Tao tao)
590: {
591:   TAO_LCL        *lclP;
593:   const char     *morethuente_type = TAOLINESEARCHMT;

596:   tao->ops->setup = TaoSetup_LCL;
597:   tao->ops->solve = TaoSolve_LCL;
598:   tao->ops->view = TaoView_LCL;
599:   tao->ops->setfromoptions = TaoSetFromOptions_LCL;
600:   tao->ops->destroy = TaoDestroy_LCL;
601:   PetscNewLog(tao,&lclP);
602:   tao->data = (void*)lclP;

604:   /* Override default settings (unless already changed) */
605:   if (!tao->max_it_changed) tao->max_it = 200;
606:   if (!tao->catol_changed) tao->catol = 1.0e-4;
607:   if (!tao->gatol_changed) tao->gttol = 1.0e-4;
608:   if (!tao->grtol_changed) tao->gttol = 1.0e-4;
609:   if (!tao->gttol_changed) tao->gttol = 1.0e-4;
610:   lclP->rho0 = 1.0e-4;
611:   lclP->rhomax=1e5;
612:   lclP->eps1 = 1.0e-8;
613:   lclP->eps2 = 0.0;
614:   lclP->solve_type=2;
615:   lclP->tau[0] = lclP->tau[1] = lclP->tau[2] = lclP->tau[3] = 1.0e-4;
616:   TaoLineSearchCreate(((PetscObject)tao)->comm, &tao->linesearch);
617:   PetscObjectIncrementTabLevel((PetscObject)tao->linesearch, (PetscObject)tao, 1);
618:   TaoLineSearchSetType(tao->linesearch, morethuente_type);
619:   TaoLineSearchSetOptionsPrefix(tao->linesearch,tao->hdr.prefix);

621:   TaoLineSearchSetObjectiveAndGradientRoutine(tao->linesearch,LCLComputeAugmentedLagrangianAndGradient, tao);
622:   KSPCreate(((PetscObject)tao)->comm,&tao->ksp);
623:   PetscObjectIncrementTabLevel((PetscObject)tao->ksp, (PetscObject)tao, 1);
624:   KSPSetOptionsPrefix(tao->ksp, tao->hdr.prefix);
625:   KSPSetFromOptions(tao->ksp);

627:   MatCreate(((PetscObject)tao)->comm, &lclP->R);
628:   MatSetType(lclP->R, MATLMVMBFGS);
629:   return(0);
630: }

632: static PetscErrorCode LCLComputeLagrangianAndGradient(TaoLineSearch ls, Vec X, PetscReal *f, Vec G, void *ptr)
633: {
634:   Tao            tao = (Tao)ptr;
635:   TAO_LCL        *lclP = (TAO_LCL*)tao->data;
636:   PetscBool      set,pset,flag,pflag,symmetric;
637:   PetscReal      cdotl;

641:   TaoComputeObjectiveAndGradient(tao,X,f,G);
642:   LCLScatter(lclP,G,lclP->GU,lclP->GV);
643:   if (lclP->recompute_jacobian_flag) {
644:     TaoComputeJacobianState(tao,X,tao->jacobian_state,tao->jacobian_state_pre,tao->jacobian_state_inv);
645:     TaoComputeJacobianDesign(tao,X,tao->jacobian_design);
646:   }
647:   TaoComputeConstraints(tao,X, tao->constraints);
648:   MatIsSymmetricKnown(tao->jacobian_state,&set,&flag);
649:   if (tao->jacobian_state_pre) {
650:     MatIsSymmetricKnown(tao->jacobian_state_pre,&pset,&pflag);
651:   } else {
652:     pset = pflag = PETSC_TRUE;
653:   }
654:   if (set && pset && flag && pflag) symmetric = PETSC_TRUE;
655:   else symmetric = PETSC_FALSE;

657:   VecDot(lclP->lamda0, tao->constraints, &cdotl);
658:   lclP->lgn = *f - cdotl;

660:   /* Gradient of Lagrangian GL = G - J' * lamda */
661:   /*      WU = A' * WL
662:           WV = B' * WL */
663:   if (symmetric) {
664:     MatMult(tao->jacobian_state,lclP->lamda0,lclP->GL_U);
665:   } else {
666:     MatMultTranspose(tao->jacobian_state,lclP->lamda0,lclP->GL_U);
667:   }
668:   MatMultTranspose(tao->jacobian_design,lclP->lamda0,lclP->GL_V);
669:   VecScale(lclP->GL_U,-1.0);
670:   VecScale(lclP->GL_V,-1.0);
671:   VecAXPY(lclP->GL_U,1.0,lclP->GU);
672:   VecAXPY(lclP->GL_V,1.0,lclP->GV);
673:   LCLGather(lclP,lclP->GL_U,lclP->GL_V,lclP->GL);

675:   f[0] = lclP->lgn;
676:   VecCopy(lclP->GL,G);
677:   return(0);
678: }

680: static PetscErrorCode LCLComputeAugmentedLagrangianAndGradient(TaoLineSearch ls, Vec X, PetscReal *f, Vec G, void *ptr)
681: {
682:   Tao            tao = (Tao)ptr;
683:   TAO_LCL        *lclP = (TAO_LCL*)tao->data;
684:   PetscReal      con2;
685:   PetscBool      flag,pflag,set,pset,symmetric;

689:   LCLComputeLagrangianAndGradient(tao->linesearch,X,f,G,tao);
690:   LCLScatter(lclP,G,lclP->GL_U,lclP->GL_V);
691:   VecDot(tao->constraints,tao->constraints,&con2);
692:   lclP->aug = lclP->lgn + 0.5*lclP->rho*con2;

694:   /* Gradient of Aug. Lagrangian GAugL = GL + rho * J' c */
695:   /*      WU = A' * c
696:           WV = B' * c */
697:   MatIsSymmetricKnown(tao->jacobian_state,&set,&flag);
698:   if (tao->jacobian_state_pre) {
699:     MatIsSymmetricKnown(tao->jacobian_state_pre,&pset,&pflag);
700:   } else {
701:     pset = pflag = PETSC_TRUE;
702:   }
703:   if (set && pset && flag && pflag) symmetric = PETSC_TRUE;
704:   else symmetric = PETSC_FALSE;

706:   if (symmetric) {
707:     MatMult(tao->jacobian_state,tao->constraints,lclP->GAugL_U);
708:   } else {
709:     MatMultTranspose(tao->jacobian_state,tao->constraints,lclP->GAugL_U);
710:   }

712:   MatMultTranspose(tao->jacobian_design,tao->constraints,lclP->GAugL_V);
713:   VecAYPX(lclP->GAugL_U,lclP->rho,lclP->GL_U);
714:   VecAYPX(lclP->GAugL_V,lclP->rho,lclP->GL_V);
715:   LCLGather(lclP,lclP->GAugL_U,lclP->GAugL_V,lclP->GAugL);

717:   f[0] = lclP->aug;
718:   VecCopy(lclP->GAugL,G);
719:   return(0);
720: }

722: PetscErrorCode LCLGather(TAO_LCL *lclP, Vec u, Vec v, Vec x)
723: {
726:   VecScatterBegin(lclP->state_scatter, u, x, INSERT_VALUES, SCATTER_REVERSE);
727:   VecScatterEnd(lclP->state_scatter, u, x, INSERT_VALUES, SCATTER_REVERSE);
728:   VecScatterBegin(lclP->design_scatter, v, x, INSERT_VALUES, SCATTER_REVERSE);
729:   VecScatterEnd(lclP->design_scatter, v, x, INSERT_VALUES, SCATTER_REVERSE);
730:   return(0);

732: }
733: PetscErrorCode LCLScatter(TAO_LCL *lclP, Vec x, Vec u, Vec v)
734: {
737:   VecScatterBegin(lclP->state_scatter, x, u, INSERT_VALUES, SCATTER_FORWARD);
738:   VecScatterEnd(lclP->state_scatter, x, u, INSERT_VALUES, SCATTER_FORWARD);
739:   VecScatterBegin(lclP->design_scatter, x, v, INSERT_VALUES, SCATTER_FORWARD);
740:   VecScatterEnd(lclP->design_scatter, x, v, INSERT_VALUES, SCATTER_FORWARD);
741:   return(0);

743: }