Actual source code: ipm.c

petsc-3.11.4 2019-09-28
Report Typos and Errors
  1:  #include <petsctaolinesearch.h>
  2:  #include <../src/tao/constrained/impls/ipm/ipm.h>

  4: /*
  5:    x,d in R^n
  6:    f in R
  7:    nb = mi + nlb+nub
  8:    s in R^nb is slack vector CI(x) / x-XL / -x+XU
  9:    bin in R^mi (tao->constraints_inequality)
 10:    beq in R^me (tao->constraints_equality)
 11:    lamdai in R^nb (ipmP->lamdai)
 12:    lamdae in R^me (ipmP->lamdae)
 13:    Jeq in R^(me x n) (tao->jacobian_equality)
 14:    Jin in R^(mi x n) (tao->jacobian_inequality)
 15:    Ai in  R^(nb x n) (ipmP->Ai)
 16:    H in R^(n x n) (tao->hessian)
 17:    min f=(1/2)*x'*H*x + d'*x
 18:    s.t.  CE(x) == 0
 19:          CI(x) >= 0
 20:          x >= tao->XL
 21:          -x >= -tao->XU
 22: */

 24: static PetscErrorCode IPMComputeKKT(Tao tao);
 25: static PetscErrorCode IPMPushInitialPoint(Tao tao);
 26: static PetscErrorCode IPMEvaluate(Tao tao);
 27: static PetscErrorCode IPMUpdateK(Tao tao);
 28: static PetscErrorCode IPMUpdateAi(Tao tao);
 29: static PetscErrorCode IPMGatherRHS(Tao tao,Vec,Vec,Vec,Vec,Vec);
 30: static PetscErrorCode IPMScatterStep(Tao tao,Vec,Vec,Vec,Vec,Vec);
 31: static PetscErrorCode IPMInitializeBounds(Tao tao);

 33: static PetscErrorCode TaoSolve_IPM(Tao tao)
 34: {
 35:   PetscErrorCode     ierr;
 36:   TAO_IPM            *ipmP = (TAO_IPM*)tao->data;
 37:   PetscInt           its,i;
 38:   PetscScalar        stepsize=1.0;
 39:   PetscScalar        step_s,step_l,alpha,tau,sigma,phi_target;

 42:   /* Push initial point away from bounds */
 43:   IPMInitializeBounds(tao);
 44:   IPMPushInitialPoint(tao);
 45:   VecCopy(tao->solution,ipmP->rhs_x);
 46:   IPMEvaluate(tao);
 47:   IPMComputeKKT(tao);
 48: 
 49:   tao->reason = TAO_CONTINUE_ITERATING;
 50:   TaoLogConvergenceHistory(tao,ipmP->kkt_f,ipmP->phi,0.0,tao->ksp_its);
 51:   TaoMonitor(tao,tao->niter,ipmP->kkt_f,ipmP->phi,0.0,1.0);
 52:   (*tao->ops->convergencetest)(tao,tao->cnvP);
 53: 
 54:   while (tao->reason == TAO_CONTINUE_ITERATING) {
 55:     /* Call general purpose update function */
 56:     if (tao->ops->update) {
 57:       (*tao->ops->update)(tao, tao->niter, tao->user_update);
 58:     }
 59: 
 60:     tao->ksp_its=0;
 61:     IPMUpdateK(tao);
 62:     /*
 63:        rhs.x    = -rd
 64:        rhs.lame = -rpe
 65:        rhs.lami = -rpi
 66:        rhs.com  = -com
 67:     */

 69:     VecCopy(ipmP->rd,ipmP->rhs_x);
 70:     if (ipmP->me > 0) {
 71:       VecCopy(ipmP->rpe,ipmP->rhs_lamdae);
 72:     }
 73:     if (ipmP->nb > 0) {
 74:       VecCopy(ipmP->rpi,ipmP->rhs_lamdai);
 75:       VecCopy(ipmP->complementarity,ipmP->rhs_s);
 76:     }
 77:     IPMGatherRHS(tao,ipmP->bigrhs,ipmP->rhs_x,ipmP->rhs_lamdae,ipmP->rhs_lamdai,ipmP->rhs_s);
 78:     VecScale(ipmP->bigrhs,-1.0);

 80:     /* solve K * step = rhs */
 81:     KSPSetOperators(tao->ksp,ipmP->K,ipmP->K);
 82:     KSPSolve(tao->ksp,ipmP->bigrhs,ipmP->bigstep);

 84:     IPMScatterStep(tao,ipmP->bigstep,tao->stepdirection,ipmP->ds,ipmP->dlamdae,ipmP->dlamdai);
 85:     KSPGetIterationNumber(tao->ksp,&its);
 86:     tao->ksp_its += its;
 87:     tao->ksp_tot_its+=its;
 88:      /* Find distance along step direction to closest bound */
 89:     if (ipmP->nb > 0) {
 90:       VecStepBoundInfo(ipmP->s,ipmP->ds,ipmP->Zero_nb,ipmP->Inf_nb,&step_s,NULL,NULL);
 91:       VecStepBoundInfo(ipmP->lamdai,ipmP->dlamdai,ipmP->Zero_nb,ipmP->Inf_nb,&step_l,NULL,NULL);
 92:       alpha = PetscMin(step_s,step_l);
 93:       alpha = PetscMin(alpha,1.0);
 94:       ipmP->alpha1 = alpha;
 95:     } else {
 96:       ipmP->alpha1 = alpha = 1.0;
 97:     }

 99:     /* x_aff = x + alpha*d */
100:     VecCopy(tao->solution,ipmP->save_x);
101:     if (ipmP->me > 0) {
102:       VecCopy(ipmP->lamdae,ipmP->save_lamdae);
103:     }
104:     if (ipmP->nb > 0) {
105:       VecCopy(ipmP->lamdai,ipmP->save_lamdai);
106:       VecCopy(ipmP->s,ipmP->save_s);
107:     }

109:     VecAXPY(tao->solution,alpha,tao->stepdirection);
110:     if (ipmP->me > 0) {
111:       VecAXPY(ipmP->lamdae,alpha,ipmP->dlamdae);
112:     }
113:     if (ipmP->nb > 0) {
114:       VecAXPY(ipmP->lamdai,alpha,ipmP->dlamdai);
115:       VecAXPY(ipmP->s,alpha,ipmP->ds);
116:     }

118:     /* Recompute kkt to find centering parameter sigma = (new_mu/old_mu)^3 */
119:     if (ipmP->mu == 0.0) {
120:       sigma = 0.0;
121:     } else {
122:       sigma = 1.0/ipmP->mu;
123:     }
124:     IPMComputeKKT(tao);
125:     sigma *= ipmP->mu;
126:     sigma*=sigma*sigma;

128:     /* revert kkt info */
129:     VecCopy(ipmP->save_x,tao->solution);
130:     if (ipmP->me > 0) {
131:       VecCopy(ipmP->save_lamdae,ipmP->lamdae);
132:     }
133:     if (ipmP->nb > 0) {
134:       VecCopy(ipmP->save_lamdai,ipmP->lamdai);
135:       VecCopy(ipmP->save_s,ipmP->s);
136:     }
137:     IPMComputeKKT(tao);

139:     /* update rhs with new complementarity vector */
140:     if (ipmP->nb > 0) {
141:       VecCopy(ipmP->complementarity,ipmP->rhs_s);
142:       VecScale(ipmP->rhs_s,-1.0);
143:       VecShift(ipmP->rhs_s,sigma*ipmP->mu);
144:     }
145:     IPMGatherRHS(tao,ipmP->bigrhs,NULL,NULL,NULL,ipmP->rhs_s);

147:     /* solve K * step = rhs */
148:     KSPSetOperators(tao->ksp,ipmP->K,ipmP->K);
149:     KSPSolve(tao->ksp,ipmP->bigrhs,ipmP->bigstep);

151:     IPMScatterStep(tao,ipmP->bigstep,tao->stepdirection,ipmP->ds,ipmP->dlamdae,ipmP->dlamdai);
152:     KSPGetIterationNumber(tao->ksp,&its);
153:     tao->ksp_its += its;
154:     tao->ksp_tot_its+=its;
155:     if (ipmP->nb > 0) {
156:       /* Get max step size and apply frac-to-boundary */
157:       tau = PetscMax(ipmP->taumin,1.0-ipmP->mu);
158:       tau = PetscMin(tau,1.0);
159:       if (tau != 1.0) {
160:         VecScale(ipmP->s,tau);
161:         VecScale(ipmP->lamdai,tau);
162:       }
163:       VecStepBoundInfo(ipmP->s,ipmP->ds,ipmP->Zero_nb,ipmP->Inf_nb,&step_s,NULL,NULL);
164:       VecStepBoundInfo(ipmP->lamdai,ipmP->dlamdai,ipmP->Zero_nb,ipmP->Inf_nb,&step_l,NULL,NULL);
165:       if (tau != 1.0) {
166:         VecCopy(ipmP->save_s,ipmP->s);
167:         VecCopy(ipmP->save_lamdai,ipmP->lamdai);
168:       }
169:       alpha = PetscMin(step_s,step_l);
170:       alpha = PetscMin(alpha,1.0);
171:     } else {
172:       alpha = 1.0;
173:     }
174:     ipmP->alpha2 = alpha;
175:     /* TODO make phi_target meaningful */
176:     phi_target = ipmP->dec * ipmP->phi;
177:     for (i=0; i<11;i++) {
178:       VecAXPY(tao->solution,alpha,tao->stepdirection);
179:       if (ipmP->nb > 0) {
180:         VecAXPY(ipmP->s,alpha,ipmP->ds);
181:         VecAXPY(ipmP->lamdai,alpha,ipmP->dlamdai);
182:       }
183:       if (ipmP->me > 0) {
184:         VecAXPY(ipmP->lamdae,alpha,ipmP->dlamdae);
185:       }

187:       /* update dual variables */
188:       if (ipmP->me > 0) {
189:         VecCopy(ipmP->lamdae,tao->DE);
190:       }

192:       IPMEvaluate(tao);
193:       IPMComputeKKT(tao);
194:       if (ipmP->phi <= phi_target) break;
195:       alpha /= 2.0;
196:     }

198:     TaoLogConvergenceHistory(tao,ipmP->kkt_f,ipmP->phi,0.0,tao->ksp_its);
199:     TaoMonitor(tao,tao->niter,ipmP->kkt_f,ipmP->phi,0.0,stepsize);
200:     (*tao->ops->convergencetest)(tao,tao->cnvP);
201:     tao->niter++;
202:   }
203:   return(0);
204: }

206: static PetscErrorCode TaoSetup_IPM(Tao tao)
207: {
208:   TAO_IPM        *ipmP = (TAO_IPM*)tao->data;

212:   ipmP->nb = ipmP->mi = ipmP->me = 0;
213:   ipmP->K=0;
214:   VecGetSize(tao->solution,&ipmP->n);
215:   if (!tao->gradient) {
216:     VecDuplicate(tao->solution, &tao->gradient);
217:     VecDuplicate(tao->solution, &tao->stepdirection);
218:     VecDuplicate(tao->solution, &ipmP->rd);
219:     VecDuplicate(tao->solution, &ipmP->rhs_x);
220:     VecDuplicate(tao->solution, &ipmP->work);
221:     VecDuplicate(tao->solution, &ipmP->save_x);
222:   }
223:   if (tao->constraints_equality) {
224:     VecGetSize(tao->constraints_equality,&ipmP->me);
225:     VecDuplicate(tao->constraints_equality,&ipmP->lamdae);
226:     VecDuplicate(tao->constraints_equality,&ipmP->dlamdae);
227:     VecDuplicate(tao->constraints_equality,&ipmP->rhs_lamdae);
228:     VecDuplicate(tao->constraints_equality,&ipmP->save_lamdae);
229:     VecDuplicate(tao->constraints_equality,&ipmP->rpe);
230:     VecDuplicate(tao->constraints_equality,&tao->DE);
231:   }
232:   if (tao->constraints_inequality) {
233:     VecDuplicate(tao->constraints_inequality,&tao->DI);
234:   }
235:   return(0);
236: }

238: static PetscErrorCode IPMInitializeBounds(Tao tao)
239: {
240:   TAO_IPM        *ipmP = (TAO_IPM*)tao->data;
241:   Vec            xtmp;
242:   PetscInt       xstart,xend;
243:   PetscInt       ucstart,ucend; /* user ci */
244:   PetscInt       ucestart,uceend; /* user ce */
245:   PetscInt       sstart = 0 ,send = 0;
246:   PetscInt       bigsize;
247:   PetscInt       i,counter,nloc;
248:   PetscInt       *cind,*xind,*ucind,*uceind,*stepind;
249:   VecType        vtype;
250:   const PetscInt *xli,*xui;
251:   PetscInt       xl_offset,xu_offset;
252:   IS             bigxl,bigxu,isuc,isc,isx,sis,is1;
254:   MPI_Comm       comm;

257:   cind=xind=ucind=uceind=stepind=0;
258:   ipmP->mi=0;
259:   ipmP->nxlb=0;
260:   ipmP->nxub=0;
261:   ipmP->nb=0;
262:   ipmP->nslack=0;

264:   VecDuplicate(tao->solution,&xtmp);
265:   if (!tao->XL && !tao->XU && tao->ops->computebounds) {
266:     TaoComputeVariableBounds(tao);
267:   }
268:   if (tao->XL) {
269:     VecSet(xtmp,PETSC_NINFINITY);
270:     VecWhichGreaterThan(tao->XL,xtmp,&ipmP->isxl);
271:     ISGetSize(ipmP->isxl,&ipmP->nxlb);
272:   } else {
273:     ipmP->nxlb=0;
274:   }
275:   if (tao->XU) {
276:     VecSet(xtmp,PETSC_INFINITY);
277:     VecWhichLessThan(tao->XU,xtmp,&ipmP->isxu);
278:     ISGetSize(ipmP->isxu,&ipmP->nxub);
279:   } else {
280:     ipmP->nxub=0;
281:   }
282:   VecDestroy(&xtmp);
283:   if (tao->constraints_inequality) {
284:     VecGetSize(tao->constraints_inequality,&ipmP->mi);
285:   } else {
286:     ipmP->mi = 0;
287:   }
288:   ipmP->nb = ipmP->nxlb + ipmP->nxub + ipmP->mi;

290:   PetscObjectGetComm((PetscObject)tao->solution,&comm);

292:   bigsize = ipmP->n+2*ipmP->nb+ipmP->me;
293:   PetscMalloc1(bigsize,&stepind);
294:   PetscMalloc1(ipmP->n,&xind);
295:   PetscMalloc1(ipmP->me,&uceind);
296:   VecGetOwnershipRange(tao->solution,&xstart,&xend);

298:   if (ipmP->nb > 0) {
299:     VecCreate(comm,&ipmP->s);
300:     VecSetSizes(ipmP->s,PETSC_DECIDE,ipmP->nb);
301:     VecSetFromOptions(ipmP->s);
302:     VecDuplicate(ipmP->s,&ipmP->ds);
303:     VecDuplicate(ipmP->s,&ipmP->rhs_s);
304:     VecDuplicate(ipmP->s,&ipmP->complementarity);
305:     VecDuplicate(ipmP->s,&ipmP->ci);

307:     VecDuplicate(ipmP->s,&ipmP->lamdai);
308:     VecDuplicate(ipmP->s,&ipmP->dlamdai);
309:     VecDuplicate(ipmP->s,&ipmP->rhs_lamdai);
310:     VecDuplicate(ipmP->s,&ipmP->save_lamdai);

312:     VecDuplicate(ipmP->s,&ipmP->save_s);
313:     VecDuplicate(ipmP->s,&ipmP->rpi);
314:     VecDuplicate(ipmP->s,&ipmP->Zero_nb);
315:     VecSet(ipmP->Zero_nb,0.0);
316:     VecDuplicate(ipmP->s,&ipmP->One_nb);
317:     VecSet(ipmP->One_nb,1.0);
318:     VecDuplicate(ipmP->s,&ipmP->Inf_nb);
319:     VecSet(ipmP->Inf_nb,PETSC_INFINITY);

321:     PetscMalloc1(ipmP->nb,&cind);
322:     PetscMalloc1(ipmP->mi,&ucind);
323:     VecGetOwnershipRange(ipmP->s,&sstart,&send);

325:     if (ipmP->mi > 0) {
326:       VecGetOwnershipRange(tao->constraints_inequality,&ucstart,&ucend);
327:       counter=0;
328:       for (i=ucstart;i<ucend;i++) {
329:         cind[counter++] = i;
330:       }
331:       ISCreateGeneral(comm,counter,cind,PETSC_COPY_VALUES,&isuc);
332:       ISCreateGeneral(comm,counter,cind,PETSC_COPY_VALUES,&isc);
333:       VecScatterCreate(tao->constraints_inequality,isuc,ipmP->ci,isc,&ipmP->ci_scat);

335:       ISDestroy(&isuc);
336:       ISDestroy(&isc);
337:     }
338:     /* need to know how may xbound indices are on each process */
339:     /* TODO better way */
340:     if (ipmP->nxlb) {
341:       ISAllGather(ipmP->isxl,&bigxl);
342:       ISGetIndices(bigxl,&xli);
343:       /* find offsets for this processor */
344:       xl_offset = ipmP->mi;
345:       for (i=0;i<ipmP->nxlb;i++) {
346:         if (xli[i] < xstart) {
347:           xl_offset++;
348:         } else break;
349:       }
350:       ISRestoreIndices(bigxl,&xli);

352:       ISGetIndices(ipmP->isxl,&xli);
353:       ISGetLocalSize(ipmP->isxl,&nloc);
354:       for (i=0;i<nloc;i++) {
355:         xind[i] = xli[i];
356:         cind[i] = xl_offset+i;
357:       }

359:       ISCreateGeneral(comm,nloc,xind,PETSC_COPY_VALUES,&isx);
360:       ISCreateGeneral(comm,nloc,cind,PETSC_COPY_VALUES,&isc);
361:       VecScatterCreate(tao->XL,isx,ipmP->ci,isc,&ipmP->xl_scat);
362:       ISDestroy(&isx);
363:       ISDestroy(&isc);
364:       ISDestroy(&bigxl);
365:     }

367:     if (ipmP->nxub) {
368:       ISAllGather(ipmP->isxu,&bigxu);
369:       ISGetIndices(bigxu,&xui);
370:       /* find offsets for this processor */
371:       xu_offset = ipmP->mi + ipmP->nxlb;
372:       for (i=0;i<ipmP->nxub;i++) {
373:         if (xui[i] < xstart) {
374:           xu_offset++;
375:         } else break;
376:       }
377:       ISRestoreIndices(bigxu,&xui);

379:       ISGetIndices(ipmP->isxu,&xui);
380:       ISGetLocalSize(ipmP->isxu,&nloc);
381:       for (i=0;i<nloc;i++) {
382:         xind[i] = xui[i];
383:         cind[i] = xu_offset+i;
384:       }

386:       ISCreateGeneral(comm,nloc,xind,PETSC_COPY_VALUES,&isx);
387:       ISCreateGeneral(comm,nloc,cind,PETSC_COPY_VALUES,&isc);
388:       VecScatterCreate(tao->XU,isx,ipmP->ci,isc,&ipmP->xu_scat);
389:       ISDestroy(&isx);
390:       ISDestroy(&isc);
391:       ISDestroy(&bigxu);
392:     }
393:   }
394:   VecCreate(comm,&ipmP->bigrhs);
395:   VecGetType(tao->solution,&vtype);
396:   VecSetType(ipmP->bigrhs,vtype);
397:   VecSetSizes(ipmP->bigrhs,PETSC_DECIDE,bigsize);
398:   VecSetFromOptions(ipmP->bigrhs);
399:   VecDuplicate(ipmP->bigrhs,&ipmP->bigstep);

401:   /* create scatters for step->x and x->rhs */
402:   for (i=xstart;i<xend;i++) {
403:     stepind[i-xstart] = i;
404:     xind[i-xstart] = i;
405:   }
406:   ISCreateGeneral(comm,xend-xstart,stepind,PETSC_COPY_VALUES,&sis);
407:   ISCreateGeneral(comm,xend-xstart,xind,PETSC_COPY_VALUES,&is1);
408:   VecScatterCreate(ipmP->bigstep,sis,tao->solution,is1,&ipmP->step1);
409:   VecScatterCreate(tao->solution,is1,ipmP->bigrhs,sis,&ipmP->rhs1);
410:   ISDestroy(&sis);
411:   ISDestroy(&is1);

413:   if (ipmP->nb > 0) {
414:     for (i=sstart;i<send;i++) {
415:       stepind[i-sstart] = i+ipmP->n;
416:       cind[i-sstart] = i;
417:     }
418:     ISCreateGeneral(comm,send-sstart,stepind,PETSC_COPY_VALUES,&sis);
419:     ISCreateGeneral(comm,send-sstart,cind,PETSC_COPY_VALUES,&is1);
420:     VecScatterCreate(ipmP->bigstep,sis,ipmP->s,is1,&ipmP->step2);
421:     ISDestroy(&sis);

423:     for (i=sstart;i<send;i++) {
424:       stepind[i-sstart] = i+ipmP->n+ipmP->me;
425:       cind[i-sstart] = i;
426:     }
427:     ISCreateGeneral(comm,send-sstart,stepind,PETSC_COPY_VALUES,&sis);
428:     VecScatterCreate(ipmP->s,is1,ipmP->bigrhs,sis,&ipmP->rhs3);
429:     ISDestroy(&sis);
430:     ISDestroy(&is1);
431:   }

433:   if (ipmP->me > 0) {
434:     VecGetOwnershipRange(tao->constraints_equality,&ucestart,&uceend);
435:     for (i=ucestart;i<uceend;i++) {
436:       stepind[i-ucestart] = i + ipmP->n+ipmP->nb;
437:       uceind[i-ucestart] = i;
438:     }

440:     ISCreateGeneral(comm,uceend-ucestart,stepind,PETSC_COPY_VALUES,&sis);
441:     ISCreateGeneral(comm,uceend-ucestart,uceind,PETSC_COPY_VALUES,&is1);
442:     VecScatterCreate(ipmP->bigstep,sis,tao->constraints_equality,is1,&ipmP->step3);
443:     ISDestroy(&sis);

445:     for (i=ucestart;i<uceend;i++) {
446:       stepind[i-ucestart] = i + ipmP->n;
447:     }

449:     ISCreateGeneral(comm,uceend-ucestart,stepind,PETSC_COPY_VALUES,&sis);
450:     VecScatterCreate(tao->constraints_equality,is1,ipmP->bigrhs,sis,&ipmP->rhs2);
451:     ISDestroy(&sis);
452:     ISDestroy(&is1);
453:   }

455:   if (ipmP->nb > 0) {
456:     for (i=sstart;i<send;i++) {
457:       stepind[i-sstart] = i + ipmP->n + ipmP->nb + ipmP->me;
458:       cind[i-sstart] = i;
459:     }
460:     ISCreateGeneral(comm,send-sstart,cind,PETSC_COPY_VALUES,&is1);
461:     ISCreateGeneral(comm,send-sstart,stepind,PETSC_COPY_VALUES,&sis);
462:     VecScatterCreate(ipmP->bigstep,sis,ipmP->s,is1,&ipmP->step4);
463:     VecScatterCreate(ipmP->s,is1,ipmP->bigrhs,sis,&ipmP->rhs4);
464:     ISDestroy(&sis);
465:     ISDestroy(&is1);
466:   }

468:   PetscFree(stepind);
469:   PetscFree(cind);
470:   PetscFree(ucind);
471:   PetscFree(uceind);
472:   PetscFree(xind);
473:   return(0);
474: }

476: static PetscErrorCode TaoDestroy_IPM(Tao tao)
477: {
478:   TAO_IPM        *ipmP = (TAO_IPM*)tao->data;

482:   VecDestroy(&ipmP->rd);
483:   VecDestroy(&ipmP->rpe);
484:   VecDestroy(&ipmP->rpi);
485:   VecDestroy(&ipmP->work);
486:   VecDestroy(&ipmP->lamdae);
487:   VecDestroy(&ipmP->lamdai);
488:   VecDestroy(&ipmP->s);
489:   VecDestroy(&ipmP->ds);
490:   VecDestroy(&ipmP->ci);

492:   VecDestroy(&ipmP->rhs_x);
493:   VecDestroy(&ipmP->rhs_lamdae);
494:   VecDestroy(&ipmP->rhs_lamdai);
495:   VecDestroy(&ipmP->rhs_s);

497:   VecDestroy(&ipmP->save_x);
498:   VecDestroy(&ipmP->save_lamdae);
499:   VecDestroy(&ipmP->save_lamdai);
500:   VecDestroy(&ipmP->save_s);

502:   VecScatterDestroy(&ipmP->step1);
503:   VecScatterDestroy(&ipmP->step2);
504:   VecScatterDestroy(&ipmP->step3);
505:   VecScatterDestroy(&ipmP->step4);

507:   VecScatterDestroy(&ipmP->rhs1);
508:   VecScatterDestroy(&ipmP->rhs2);
509:   VecScatterDestroy(&ipmP->rhs3);
510:   VecScatterDestroy(&ipmP->rhs4);

512:   VecScatterDestroy(&ipmP->ci_scat);
513:   VecScatterDestroy(&ipmP->xl_scat);
514:   VecScatterDestroy(&ipmP->xu_scat);

516:   VecDestroy(&ipmP->dlamdai);
517:   VecDestroy(&ipmP->dlamdae);
518:   VecDestroy(&ipmP->Zero_nb);
519:   VecDestroy(&ipmP->One_nb);
520:   VecDestroy(&ipmP->Inf_nb);
521:   VecDestroy(&ipmP->complementarity);

523:   VecDestroy(&ipmP->bigrhs);
524:   VecDestroy(&ipmP->bigstep);
525:   MatDestroy(&ipmP->Ai);
526:   MatDestroy(&ipmP->K);
527:   ISDestroy(&ipmP->isxu);
528:   ISDestroy(&ipmP->isxl);
529:   PetscFree(tao->data);
530:   return(0);
531: }

533: static PetscErrorCode TaoSetFromOptions_IPM(PetscOptionItems *PetscOptionsObject,Tao tao)
534: {
535:   TAO_IPM        *ipmP = (TAO_IPM*)tao->data;

539:   PetscOptionsHead(PetscOptionsObject,"IPM method for constrained optimization");
540:   PetscOptionsBool("-tao_ipm_monitorkkt","monitor kkt status",NULL,ipmP->monitorkkt,&ipmP->monitorkkt,NULL);
541:   PetscOptionsReal("-tao_ipm_pushs","parameter to push initial slack variables away from bounds",NULL,ipmP->pushs,&ipmP->pushs,NULL);
542:   PetscOptionsReal("-tao_ipm_pushnu","parameter to push initial (inequality) dual variables away from bounds",NULL,ipmP->pushnu,&ipmP->pushnu,NULL);
543:   PetscOptionsTail();
544:   KSPSetFromOptions(tao->ksp);
545:   return(0);
546: }

548: static PetscErrorCode TaoView_IPM(Tao tao, PetscViewer viewer)
549: {
550:   return 0;
551: }

553: /* IPMObjectiveAndGradient()
554:    f = d'x + 0.5 * x' * H * x
555:    rd = H*x + d + Ae'*lame - Ai'*lami
556:    rpe = Ae*x - be
557:    rpi = Ai*x - yi - bi
558:    mu = yi' * lami/mi;
559:    com = yi.*lami

561:    phi = ||rd|| + ||rpe|| + ||rpi|| + ||com||
562: */
563: /*
564: static PetscErrorCode IPMObjective(TaoLineSearch ls, Vec X, PetscReal *f, void *tptr)
565: {
566:   Tao tao = (Tao)tptr;
567:   TAO_IPM *ipmP = (TAO_IPM*)tao->data;
570:   IPMComputeKKT(tao);
571:   *f = ipmP->phi;
572:   return(0);
573: }
574: */

576: /*
577:    f = d'x + 0.5 * x' * H * x
578:    rd = H*x + d + Ae'*lame - Ai'*lami
579:        Ai =   jac_ineq
580:                I (w/lb)
581:               -I (w/ub)

583:    rpe = ce
584:    rpi = ci - s;
585:    com = s.*lami
586:    mu = yi' * lami/mi;

588:    phi = ||rd|| + ||rpe|| + ||rpi|| + ||com||
589: */
590: static PetscErrorCode IPMComputeKKT(Tao tao)
591: {
592:   TAO_IPM        *ipmP = (TAO_IPM *)tao->data;
593:   PetscScalar    norm;

597:   VecCopy(tao->gradient,ipmP->rd);

599:   if (ipmP->me > 0) {
600:     /* rd = gradient + Ae'*lamdae */
601:     MatMultTranspose(tao->jacobian_equality,ipmP->lamdae,ipmP->work);
602:     VecAXPY(ipmP->rd, 1.0, ipmP->work);

604:     /* rpe = ce(x) */
605:     VecCopy(tao->constraints_equality,ipmP->rpe);
606:   }
607:   if (ipmP->nb > 0) {
608:     /* rd = rd - Ai'*lamdai */
609:     MatMultTranspose(ipmP->Ai,ipmP->lamdai,ipmP->work);
610:     VecAXPY(ipmP->rd, -1.0, ipmP->work);

612:     /* rpi = cin - s */
613:     VecCopy(ipmP->ci,ipmP->rpi);
614:     VecAXPY(ipmP->rpi, -1.0, ipmP->s);

616:     /* com = s .* lami */
617:     VecPointwiseMult(ipmP->complementarity, ipmP->s,ipmP->lamdai);
618:   }
619:   /* phi = ||rd; rpe; rpi; com|| */
620:   VecDot(ipmP->rd,ipmP->rd,&norm);
621:   ipmP->phi = norm;
622:   if (ipmP->me > 0 ) {
623:     VecDot(ipmP->rpe,ipmP->rpe,&norm);
624:     ipmP->phi += norm;
625:   }
626:   if (ipmP->nb > 0) {
627:     VecDot(ipmP->rpi,ipmP->rpi,&norm);
628:     ipmP->phi += norm;
629:     VecDot(ipmP->complementarity,ipmP->complementarity,&norm);
630:     ipmP->phi += norm;
631:     /* mu = s'*lami/nb */
632:     VecDot(ipmP->s,ipmP->lamdai,&ipmP->mu);
633:     ipmP->mu /= ipmP->nb;
634:   } else {
635:     ipmP->mu = 1.0;
636:   }

638:   ipmP->phi = PetscSqrtScalar(ipmP->phi);
639:   return(0);
640: }

642: /* evaluate user info at current point */
643: PetscErrorCode IPMEvaluate(Tao tao)
644: {
645:   TAO_IPM        *ipmP = (TAO_IPM *)tao->data;

649:   TaoComputeObjectiveAndGradient(tao,tao->solution,&ipmP->kkt_f,tao->gradient);
650:   TaoComputeHessian(tao,tao->solution,tao->hessian,tao->hessian_pre);
651:   if (ipmP->me > 0) {
652:     TaoComputeEqualityConstraints(tao,tao->solution,tao->constraints_equality);
653:     TaoComputeJacobianEquality(tao,tao->solution,tao->jacobian_equality,tao->jacobian_equality_pre);
654:   }
655:   if (ipmP->mi > 0) {
656:     TaoComputeInequalityConstraints(tao,tao->solution,tao->constraints_inequality);
657:     TaoComputeJacobianInequality(tao,tao->solution,tao->jacobian_inequality,tao->jacobian_inequality_pre);
658:   }
659:   if (ipmP->nb > 0) {
660:     /* Ai' =   jac_ineq | I (w/lb) | -I (w/ub)  */
661:     IPMUpdateAi(tao);
662:   }
663:   return(0);
664: }

666: /* Push initial point away from bounds */
667: PetscErrorCode IPMPushInitialPoint(Tao tao)
668: {
669:   TAO_IPM        *ipmP = (TAO_IPM *)tao->data;

673:   TaoComputeVariableBounds(tao);
674:   if (tao->XL && tao->XU) {
675:     VecMedian(tao->XL, tao->solution, tao->XU, tao->solution);
676:   }
677:   if (ipmP->nb > 0) {
678:     VecSet(ipmP->s,ipmP->pushs);
679:     VecSet(ipmP->lamdai,ipmP->pushnu);
680:     if (ipmP->mi > 0) {
681:       VecSet(tao->DI,ipmP->pushnu);
682:     }
683:   }
684:   if (ipmP->me > 0) {
685:     VecSet(tao->DE,1.0);
686:     VecSet(ipmP->lamdae,1.0);
687:   }
688:   return(0);
689: }

691: PetscErrorCode IPMUpdateAi(Tao tao)
692: {
693:   /* Ai =     Ji
694:               I (w/lb)
695:              -I (w/ub) */

697:   /* Ci =    user->ci
698:              Xi - lb (w/lb)
699:              -Xi + ub (w/ub)  */

701:   TAO_IPM           *ipmP = (TAO_IPM *)tao->data;
702:   MPI_Comm          comm;
703:   PetscInt          i;
704:   PetscScalar       newval;
705:   PetscInt          newrow,newcol,ncols;
706:   const PetscScalar *vals;
707:   const PetscInt    *cols;
708:   PetscInt          astart,aend,jstart,jend;
709:   PetscInt          *nonzeros;
710:   PetscInt          r2,r3,r4;
711:   PetscMPIInt       size;
712:   PetscErrorCode    ierr;

715:   r2 = ipmP->mi;
716:   r3 = r2 + ipmP->nxlb;
717:   r4 = r3 + ipmP->nxub;

719:   if (!ipmP->nb) return(0);

721:   /* Create Ai matrix if it doesn't exist yet */
722:   if (!ipmP->Ai) {
723:     comm = ((PetscObject)(tao->solution))->comm;
724:     PetscMalloc1(ipmP->nb,&nonzeros);
725:     MPI_Comm_size(comm,&size);
726:     if (size == 1) {
727:       for (i=0;i<ipmP->mi;i++) {
728:         MatGetRow(tao->jacobian_inequality,i,&ncols,NULL,NULL);
729:         nonzeros[i] = ncols;
730:         MatRestoreRow(tao->jacobian_inequality,i,&ncols,NULL,NULL);
731:       }
732:       for (i=r2;i<r4;i++) {
733:         nonzeros[i] = 1;
734:       }
735:     }
736:     MatCreate(comm,&ipmP->Ai);
737:     MatSetType(ipmP->Ai,MATAIJ);
738:     MatSetSizes(ipmP->Ai,PETSC_DECIDE,PETSC_DECIDE,ipmP->nb,ipmP->n);
739:     MatSetFromOptions(ipmP->Ai);
740:     MatMPIAIJSetPreallocation(ipmP->Ai,ipmP->nb,NULL,ipmP->nb,NULL);
741:     MatSeqAIJSetPreallocation(ipmP->Ai,PETSC_DEFAULT,nonzeros);
742:     if (size ==1) {
743:       PetscFree(nonzeros);
744:     }
745:   }

747:   /* Copy values from user jacobian to Ai */
748:   MatGetOwnershipRange(ipmP->Ai,&astart,&aend);

750:   /* Ai w/lb */
751:   if (ipmP->mi) {
752:     MatZeroEntries(ipmP->Ai);
753:     MatGetOwnershipRange(tao->jacobian_inequality,&jstart,&jend);
754:     for (i=jstart;i<jend;i++) {
755:       MatGetRow(tao->jacobian_inequality,i,&ncols,&cols,&vals);
756:       newrow = i;
757:       MatSetValues(ipmP->Ai,1,&newrow,ncols,cols,vals,INSERT_VALUES);
758:       MatRestoreRow(tao->jacobian_inequality,i,&ncols,&cols,&vals);
759:     }
760:   }

762:   /* I w/ xlb */
763:   if (ipmP->nxlb) {
764:     for (i=0;i<ipmP->nxlb;i++) {
765:       if (i>=astart && i<aend) {
766:         newrow = i+r2;
767:         newcol = i;
768:         newval = 1.0;
769:         MatSetValues(ipmP->Ai,1,&newrow,1,&newcol,&newval,INSERT_VALUES);
770:       }
771:     }
772:   }
773:   if (ipmP->nxub) {
774:     /* I w/ xub */
775:     for (i=0;i<ipmP->nxub;i++) {
776:       if (i>=astart && i<aend) {
777:       newrow = i+r3;
778:       newcol = i;
779:       newval = -1.0;
780:       MatSetValues(ipmP->Ai,1,&newrow,1,&newcol,&newval,INSERT_VALUES);
781:       }
782:     }
783:   }

785:   MatAssemblyBegin(ipmP->Ai,MAT_FINAL_ASSEMBLY);
786:   MatAssemblyEnd(ipmP->Ai,MAT_FINAL_ASSEMBLY);
787:   CHKMEMQ;

789:   VecSet(ipmP->ci,0.0);

791:   /* user ci */
792:   if (ipmP->mi > 0) {
793:     VecScatterBegin(ipmP->ci_scat,tao->constraints_inequality,ipmP->ci,INSERT_VALUES,SCATTER_FORWARD);
794:     VecScatterEnd(ipmP->ci_scat,tao->constraints_inequality,ipmP->ci,INSERT_VALUES,SCATTER_FORWARD);
795:   }
796:   if (!ipmP->work){
797:     VecDuplicate(tao->solution,&ipmP->work);
798:   }
799:   VecCopy(tao->solution,ipmP->work);
800:   if (tao->XL) {
801:     VecAXPY(ipmP->work,-1.0,tao->XL);

803:     /* lower bounds on variables */
804:     if (ipmP->nxlb > 0) {
805:       VecScatterBegin(ipmP->xl_scat,ipmP->work,ipmP->ci,INSERT_VALUES,SCATTER_FORWARD);
806:       VecScatterEnd(ipmP->xl_scat,ipmP->work,ipmP->ci,INSERT_VALUES,SCATTER_FORWARD);
807:     }
808:   }
809:   if (tao->XU) {
810:     /* upper bounds on variables */
811:     VecCopy(tao->solution,ipmP->work);
812:     VecScale(ipmP->work,-1.0);
813:     VecAXPY(ipmP->work,1.0,tao->XU);
814:     if (ipmP->nxub > 0) {
815:       VecScatterBegin(ipmP->xu_scat,ipmP->work,ipmP->ci,INSERT_VALUES,SCATTER_FORWARD);
816:       VecScatterEnd(ipmP->xu_scat,ipmP->work,ipmP->ci,INSERT_VALUES,SCATTER_FORWARD);
817:     }
818:   }
819:   return(0);
820: }

822: /* create K = [ Hlag , 0 , Ae', -Ai'];
823:               [Ae , 0,   0  , 0];
824:               [Ai ,-I,   0 ,  0];
825:               [ 0 , S ,  0,   Y ];  */
826: PetscErrorCode IPMUpdateK(Tao tao)
827: {
828:   TAO_IPM         *ipmP = (TAO_IPM *)tao->data;
829:   MPI_Comm        comm;
830:   PetscMPIInt     size;
831:   PetscErrorCode  ierr;
832:   PetscInt        i,j,row;
833:   PetscInt        ncols,newcol,newcols[2],newrow;
834:   const PetscInt  *cols;
835:   const PetscReal *vals;
836:   const PetscReal *l,*y;
837:   PetscReal       *newvals;
838:   PetscReal       newval;
839:   PetscInt        subsize;
840:   const PetscInt  *indices;
841:   PetscInt        *nonzeros,*d_nonzeros,*o_nonzeros;
842:   PetscInt        bigsize;
843:   PetscInt        r1,r2,r3;
844:   PetscInt        c1,c2,c3;
845:   PetscInt        klocalsize;
846:   PetscInt        hstart,hend,kstart,kend;
847:   PetscInt        aistart,aiend,aestart,aeend;
848:   PetscInt        sstart,send;

851:   comm = ((PetscObject)(tao->solution))->comm;
852:   MPI_Comm_size(comm,&size);
853:   IPMUpdateAi(tao);

855:   /* allocate workspace */
856:   subsize = PetscMax(ipmP->n,ipmP->nb);
857:   subsize = PetscMax(ipmP->me,subsize);
858:   subsize = PetscMax(2,subsize);
859:   PetscMalloc1(subsize,(PetscInt**)&indices);
860:   PetscMalloc1(subsize,&newvals);

862:   r1 = c1 = ipmP->n;
863:   r2 = r1 + ipmP->me;  c2 = c1 + ipmP->nb;
864:   r3 = c3 = r2 + ipmP->nb;

866:   bigsize = ipmP->n+2*ipmP->nb+ipmP->me;
867:   VecGetOwnershipRange(ipmP->bigrhs,&kstart,&kend);
868:   MatGetOwnershipRange(tao->hessian,&hstart,&hend);
869:   klocalsize = kend-kstart;
870:   if (!ipmP->K) {
871:     if (size == 1) {
872:       PetscMalloc1(kend-kstart,&nonzeros);
873:       for (i=0;i<bigsize;i++) {
874:         if (i<r1) {
875:           MatGetRow(tao->hessian,i,&ncols,NULL,NULL);
876:           nonzeros[i] = ncols;
877:           MatRestoreRow(tao->hessian,i,&ncols,NULL,NULL);
878:           nonzeros[i] += ipmP->me+ipmP->nb;
879:         } else if (i<r2) {
880:           nonzeros[i-kstart] = ipmP->n;
881:         } else if (i<r3) {
882:           nonzeros[i-kstart] = ipmP->n+1;
883:         } else if (i<bigsize) {
884:           nonzeros[i-kstart] = 2;
885:         }
886:       }
887:       MatCreate(comm,&ipmP->K);
888:       MatSetType(ipmP->K,MATSEQAIJ);
889:       MatSetSizes(ipmP->K,klocalsize,klocalsize,PETSC_DETERMINE,PETSC_DETERMINE);
890:       MatSeqAIJSetPreallocation(ipmP->K,0,nonzeros);
891:       MatSetFromOptions(ipmP->K);
892:       PetscFree(nonzeros);
893:     } else {
894:       PetscMalloc1(kend-kstart,&d_nonzeros);
895:       PetscMalloc1(kend-kstart,&o_nonzeros);
896:       for (i=kstart;i<kend;i++) {
897:         if (i<r1) {
898:           /* TODO fix preallocation for mpi mats */
899:           d_nonzeros[i-kstart] = PetscMin(ipmP->n+ipmP->me+ipmP->nb,kend-kstart);
900:           o_nonzeros[i-kstart] = PetscMin(ipmP->n+ipmP->me+ipmP->nb,bigsize-(kend-kstart));
901:         } else if (i<r2) {
902:           d_nonzeros[i-kstart] = PetscMin(ipmP->n,kend-kstart);
903:           o_nonzeros[i-kstart] = PetscMin(ipmP->n,bigsize-(kend-kstart));
904:         } else if (i<r3) {
905:           d_nonzeros[i-kstart] = PetscMin(ipmP->n+2,kend-kstart);
906:           o_nonzeros[i-kstart] = PetscMin(ipmP->n+2,bigsize-(kend-kstart));
907:         } else {
908:           d_nonzeros[i-kstart] = PetscMin(2,kend-kstart);
909:           o_nonzeros[i-kstart] = PetscMin(2,bigsize-(kend-kstart));
910:         }
911:       }
912:       MatCreate(comm,&ipmP->K);
913:       MatSetType(ipmP->K,MATMPIAIJ);
914:       MatSetSizes(ipmP->K,klocalsize,klocalsize,PETSC_DETERMINE,PETSC_DETERMINE);
915:       MatMPIAIJSetPreallocation(ipmP->K,0,d_nonzeros,0,o_nonzeros);
916:       PetscFree(d_nonzeros);
917:       PetscFree(o_nonzeros);
918:       MatSetFromOptions(ipmP->K);
919:     }
920:   }

922:   MatZeroEntries(ipmP->K);
923:   /* Copy H */
924:   for (i=hstart;i<hend;i++) {
925:     MatGetRow(tao->hessian,i,&ncols,&cols,&vals);
926:     if (ncols > 0) {
927:       MatSetValues(ipmP->K,1,&i,ncols,cols,vals,INSERT_VALUES);
928:     }
929:     MatRestoreRow(tao->hessian,i,&ncols,&cols,&vals);
930:   }

932:   /* Copy Ae and Ae' */
933:   if (ipmP->me > 0) {
934:     MatGetOwnershipRange(tao->jacobian_equality,&aestart,&aeend);
935:     for (i=aestart;i<aeend;i++) {
936:       MatGetRow(tao->jacobian_equality,i,&ncols,&cols,&vals);
937:       if (ncols > 0) {
938:         /*Ae*/
939:         row = i+r1;
940:         MatSetValues(ipmP->K,1,&row,ncols,cols,vals,INSERT_VALUES);
941:         /*Ae'*/
942:         for (j=0;j<ncols;j++) {
943:           newcol = i + c2;
944:           newrow = cols[j];
945:           newval = vals[j];
946:           MatSetValues(ipmP->K,1,&newrow,1,&newcol,&newval,INSERT_VALUES);
947:         }
948:       }
949:       MatRestoreRow(tao->jacobian_equality,i,&ncols,&cols,&vals);
950:     }
951:   }

953:   if (ipmP->nb > 0) {
954:     MatGetOwnershipRange(ipmP->Ai,&aistart,&aiend);
955:     /* Copy Ai,and Ai' */
956:     for (i=aistart;i<aiend;i++) {
957:       row = i+r2;
958:       MatGetRow(ipmP->Ai,i,&ncols,&cols,&vals);
959:       if (ncols > 0) {
960:         /*Ai*/
961:         MatSetValues(ipmP->K,1,&row,ncols,cols,vals,INSERT_VALUES);
962:         /*-Ai'*/
963:         for (j=0;j<ncols;j++) {
964:           newcol = i + c3;
965:           newrow = cols[j];
966:           newval = -vals[j];
967:           MatSetValues(ipmP->K,1,&newrow,1,&newcol,&newval,INSERT_VALUES);
968:         }
969:       }
970:       MatRestoreRow(ipmP->Ai,i,&ncols,&cols,&vals);
971:     }

973:     /* -I */
974:     for (i=kstart;i<kend;i++) {
975:       if (i>=r2 && i<r3) {
976:         newrow = i;
977:         newcol = i-r2+c1;
978:         newval = -1.0;
979:         MatSetValues(ipmP->K,1,&newrow,1,&newcol,&newval,INSERT_VALUES);
980:       }
981:     }

983:     /* Copy L,Y */
984:     VecGetOwnershipRange(ipmP->s,&sstart,&send);
985:     VecGetArrayRead(ipmP->lamdai,&l);
986:     VecGetArrayRead(ipmP->s,&y);

988:     for (i=sstart;i<send;i++) {
989:       newcols[0] = c1+i;
990:       newcols[1] = c3+i;
991:       newvals[0] = l[i-sstart];
992:       newvals[1] = y[i-sstart];
993:       newrow = r3+i;
994:       MatSetValues(ipmP->K,1,&newrow,2,newcols,newvals,INSERT_VALUES);
995:     }

997:     VecRestoreArrayRead(ipmP->lamdai,&l);
998:     VecRestoreArrayRead(ipmP->s,&y);
999:   }

1001:   PetscFree(indices);
1002:   PetscFree(newvals);
1003:   MatAssemblyBegin(ipmP->K,MAT_FINAL_ASSEMBLY);
1004:   MatAssemblyEnd(ipmP->K,MAT_FINAL_ASSEMBLY);
1005:   return(0);
1006: }

1008: PetscErrorCode IPMGatherRHS(Tao tao,Vec RHS,Vec X1,Vec X2,Vec X3,Vec X4)
1009: {
1010:   TAO_IPM        *ipmP = (TAO_IPM *)tao->data;

1014:   /* rhs = [x1      (n)
1015:             x2     (me)
1016:             x3     (nb)
1017:             x4     (nb)] */
1018:   if (X1) {
1019:     VecScatterBegin(ipmP->rhs1,X1,RHS,INSERT_VALUES,SCATTER_FORWARD);
1020:     VecScatterEnd(ipmP->rhs1,X1,RHS,INSERT_VALUES,SCATTER_FORWARD);
1021:   }
1022:   if (ipmP->me > 0 && X2) {
1023:     VecScatterBegin(ipmP->rhs2,X2,RHS,INSERT_VALUES,SCATTER_FORWARD);
1024:     VecScatterEnd(ipmP->rhs2,X2,RHS,INSERT_VALUES,SCATTER_FORWARD);
1025:   }
1026:   if (ipmP->nb > 0) {
1027:     if (X3) {
1028:       VecScatterBegin(ipmP->rhs3,X3,RHS,INSERT_VALUES,SCATTER_FORWARD);
1029:       VecScatterEnd(ipmP->rhs3,X3,RHS,INSERT_VALUES,SCATTER_FORWARD);
1030:     }
1031:     if (X4) {
1032:       VecScatterBegin(ipmP->rhs4,X4,RHS,INSERT_VALUES,SCATTER_FORWARD);
1033:       VecScatterEnd(ipmP->rhs4,X4,RHS,INSERT_VALUES,SCATTER_FORWARD);
1034:     }
1035:   }
1036:   return(0);
1037: }

1039: PetscErrorCode IPMScatterStep(Tao tao, Vec STEP, Vec X1, Vec X2, Vec X3, Vec X4)
1040: {
1041:   TAO_IPM        *ipmP = (TAO_IPM *)tao->data;

1045:   CHKMEMQ;
1046:   /*        [x1    (n)
1047:              x2    (nb) may be 0
1048:              x3    (me) may be 0
1049:              x4    (nb) may be 0 */
1050:   if (X1) {
1051:     VecScatterBegin(ipmP->step1,STEP,X1,INSERT_VALUES,SCATTER_FORWARD);
1052:     VecScatterEnd(ipmP->step1,STEP,X1,INSERT_VALUES,SCATTER_FORWARD);
1053:   }
1054:   if (X2 && ipmP->nb > 0) {
1055:     VecScatterBegin(ipmP->step2,STEP,X2,INSERT_VALUES,SCATTER_FORWARD);
1056:     VecScatterEnd(ipmP->step2,STEP,X2,INSERT_VALUES,SCATTER_FORWARD);
1057:   }
1058:   if (X3 && ipmP->me > 0) {
1059:     VecScatterBegin(ipmP->step3,STEP,X3,INSERT_VALUES,SCATTER_FORWARD);
1060:     VecScatterEnd(ipmP->step3,STEP,X3,INSERT_VALUES,SCATTER_FORWARD);
1061:   }
1062:   if (X4 && ipmP->nb > 0) {
1063:     VecScatterBegin(ipmP->step4,STEP,X4,INSERT_VALUES,SCATTER_FORWARD);
1064:     VecScatterEnd(ipmP->step4,STEP,X4,INSERT_VALUES,SCATTER_FORWARD);
1065:   }
1066:   CHKMEMQ;
1067:   return(0);
1068: }

1070: /*MC
1071:   TAOIPM - Interior point algorithm for generally constrained optimization.

1073:   Option Database Keys:
1074: +   -tao_ipm_pushnu - parameter to push initial dual variables away from bounds
1075: .   -tao_ipm_pushs - parameter to push initial slack variables away from bounds

1077:   Notes:
1078:     This algorithm is more of a place-holder for future constrained optimization algorithms and should not yet be used for large problems or production code.
1079:   Level: beginner

1081: M*/

1083: PETSC_EXTERN PetscErrorCode TaoCreate_IPM(Tao tao)
1084: {
1085:   TAO_IPM        *ipmP;

1089:   tao->ops->setup = TaoSetup_IPM;
1090:   tao->ops->solve = TaoSolve_IPM;
1091:   tao->ops->view = TaoView_IPM;
1092:   tao->ops->setfromoptions = TaoSetFromOptions_IPM;
1093:   tao->ops->destroy = TaoDestroy_IPM;
1094:   /* tao->ops->computedual = TaoComputeDual_IPM; */

1096:   PetscNewLog(tao,&ipmP);
1097:   tao->data = (void*)ipmP;

1099:   /* Override default settings (unless already changed) */
1100:   if (!tao->max_it_changed) tao->max_it = 200;
1101:   if (!tao->max_funcs_changed) tao->max_funcs = 500;

1103:   ipmP->dec = 10000; /* line search critera */
1104:   ipmP->taumin = 0.995;
1105:   ipmP->monitorkkt = PETSC_FALSE;
1106:   ipmP->pushs = 100;
1107:   ipmP->pushnu = 100;
1108:   KSPCreate(((PetscObject)tao)->comm, &tao->ksp);
1109:   PetscObjectIncrementTabLevel((PetscObject)tao->ksp, (PetscObject)tao, 1);
1110:   KSPSetOptionsPrefix(tao->ksp, tao->hdr.prefix);
1111:   return(0);
1112: }