Actual source code: pdipm.c

  1: #include <../src/tao/constrained/impls/ipm/pdipm.h>

  3: /*
  4:    TaoPDIPMEvaluateFunctionsAndJacobians - Evaluate the objective function f, gradient fx, constraints, and all the Jacobians at current vector

  6:    Collective

  8:    Input Parameter:
  9: +  tao - solver context
 10: -  x - vector at which all objects to be evaluated

 12:    Level: beginner

 14: .seealso: `TAOPDIPM`, `TaoPDIPMUpdateConstraints()`, `TaoPDIPMSetUpBounds()`
 15: */
 16: static PetscErrorCode TaoPDIPMEvaluateFunctionsAndJacobians(Tao tao, Vec x)
 17: {
 18:   TAO_PDIPM *pdipm = (TAO_PDIPM *)tao->data;

 20:   PetscFunctionBegin;
 21:   /* Compute user objective function and gradient */
 22:   PetscCall(TaoComputeObjectiveAndGradient(tao, x, &pdipm->obj, tao->gradient));

 24:   /* Equality constraints and Jacobian */
 25:   if (pdipm->Ng) {
 26:     PetscCall(TaoComputeEqualityConstraints(tao, x, tao->constraints_equality));
 27:     PetscCall(TaoComputeJacobianEquality(tao, x, tao->jacobian_equality, tao->jacobian_equality_pre));
 28:   }

 30:   /* Inequality constraints and Jacobian */
 31:   if (pdipm->Nh) {
 32:     PetscCall(TaoComputeInequalityConstraints(tao, x, tao->constraints_inequality));
 33:     PetscCall(TaoComputeJacobianInequality(tao, x, tao->jacobian_inequality, tao->jacobian_inequality_pre));
 34:   }
 35:   PetscFunctionReturn(PETSC_SUCCESS);
 36: }

 38: /*
 39:   TaoPDIPMUpdateConstraints - Update the vectors ce and ci at x

 41:   Collective

 43:   Input Parameter:
 44: + tao - Tao context
 45: - x - vector at which constraints to be evaluated

 47:    Level: beginner

 49: .seealso: `TAOPDIPM`, `TaoPDIPMEvaluateFunctionsAndJacobians()`
 50: */
 51: static PetscErrorCode TaoPDIPMUpdateConstraints(Tao tao, Vec x)
 52: {
 53:   TAO_PDIPM         *pdipm = (TAO_PDIPM *)tao->data;
 54:   PetscInt           i, offset, offset1, k, xstart;
 55:   PetscScalar       *carr;
 56:   const PetscInt    *ubptr, *lbptr, *bxptr, *fxptr;
 57:   const PetscScalar *xarr, *xuarr, *xlarr, *garr, *harr;

 59:   PetscFunctionBegin;
 60:   PetscCall(VecGetOwnershipRange(x, &xstart, NULL));
 61:   PetscCall(VecGetArrayRead(x, &xarr));
 62:   PetscCall(VecGetArrayRead(tao->XU, &xuarr));
 63:   PetscCall(VecGetArrayRead(tao->XL, &xlarr));

 65:   /* (1) Update ce vector */
 66:   PetscCall(VecGetArrayWrite(pdipm->ce, &carr));

 68:   if (pdipm->Ng) {
 69:     /* (1.a) Inserting updated g(x) */
 70:     PetscCall(VecGetArrayRead(tao->constraints_equality, &garr));
 71:     PetscCall(PetscArraycpy(carr, garr, pdipm->ng));
 72:     PetscCall(VecRestoreArrayRead(tao->constraints_equality, &garr));
 73:   }

 75:   /* (1.b) Update xfixed */
 76:   if (pdipm->Nxfixed) {
 77:     offset = pdipm->ng;
 78:     PetscCall(ISGetIndices(pdipm->isxfixed, &fxptr)); /* global indices in x */
 79:     for (k = 0; k < pdipm->nxfixed; k++) {
 80:       i                = fxptr[k] - xstart;
 81:       carr[offset + k] = xarr[i] - xuarr[i];
 82:     }
 83:   }
 84:   PetscCall(VecRestoreArrayWrite(pdipm->ce, &carr));

 86:   /* (2) Update ci vector */
 87:   PetscCall(VecGetArrayWrite(pdipm->ci, &carr));

 89:   if (pdipm->Nh) {
 90:     /* (2.a) Inserting updated h(x) */
 91:     PetscCall(VecGetArrayRead(tao->constraints_inequality, &harr));
 92:     PetscCall(PetscArraycpy(carr, harr, pdipm->nh));
 93:     PetscCall(VecRestoreArrayRead(tao->constraints_inequality, &harr));
 94:   }

 96:   /* (2.b) Update xub */
 97:   offset = pdipm->nh;
 98:   if (pdipm->Nxub) {
 99:     PetscCall(ISGetIndices(pdipm->isxub, &ubptr));
100:     for (k = 0; k < pdipm->nxub; k++) {
101:       i                = ubptr[k] - xstart;
102:       carr[offset + k] = xuarr[i] - xarr[i];
103:     }
104:   }

106:   if (pdipm->Nxlb) {
107:     /* (2.c) Update xlb */
108:     offset += pdipm->nxub;
109:     PetscCall(ISGetIndices(pdipm->isxlb, &lbptr)); /* global indices in x */
110:     for (k = 0; k < pdipm->nxlb; k++) {
111:       i                = lbptr[k] - xstart;
112:       carr[offset + k] = xarr[i] - xlarr[i];
113:     }
114:   }

116:   if (pdipm->Nxbox) {
117:     /* (2.d) Update xbox */
118:     offset += pdipm->nxlb;
119:     offset1 = offset + pdipm->nxbox;
120:     PetscCall(ISGetIndices(pdipm->isxbox, &bxptr)); /* global indices in x */
121:     for (k = 0; k < pdipm->nxbox; k++) {
122:       i                 = bxptr[k] - xstart; /* local indices in x */
123:       carr[offset + k]  = xuarr[i] - xarr[i];
124:       carr[offset1 + k] = xarr[i] - xlarr[i];
125:     }
126:   }
127:   PetscCall(VecRestoreArrayWrite(pdipm->ci, &carr));

129:   /* Restoring Vectors */
130:   PetscCall(VecRestoreArrayRead(x, &xarr));
131:   PetscCall(VecRestoreArrayRead(tao->XU, &xuarr));
132:   PetscCall(VecRestoreArrayRead(tao->XL, &xlarr));
133:   PetscFunctionReturn(PETSC_SUCCESS);
134: }

136: /*
137:    TaoPDIPMSetUpBounds - Create upper and lower bound vectors of x

139:    Collective

141:    Input Parameter:
142: .  tao - holds pdipm and XL & XU

144:    Level: beginner

146: .seealso: `TAOPDIPM`, `TaoPDIPMUpdateConstraints`
147: */
148: static PetscErrorCode TaoPDIPMSetUpBounds(Tao tao)
149: {
150:   TAO_PDIPM         *pdipm = (TAO_PDIPM *)tao->data;
151:   const PetscScalar *xl, *xu;
152:   PetscInt           n, *ixlb, *ixub, *ixfixed, *ixfree, *ixbox, i, low, high, idx;
153:   MPI_Comm           comm;
154:   PetscInt           recvbuf[5];

156:   PetscFunctionBegin;
157:   /* Creates upper and lower bounds vectors on x, if not created already */
158:   PetscCheck((tao->XL && tao->XU) || tao->ops->computebounds, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "TAOPDIPM requires lower and upper variable bounds; set them with TaoSetVariableBounds() or TaoSetVariableBoundsRoutine()");
159:   PetscCall(TaoComputeVariableBounds(tao));

161:   PetscCall(VecGetLocalSize(tao->XL, &n));
162:   PetscCall(PetscMalloc5(n, &ixlb, n, &ixub, n, &ixfree, n, &ixfixed, n, &ixbox));

164:   PetscCall(VecGetOwnershipRange(tao->XL, &low, &high));
165:   PetscCall(VecGetArrayRead(tao->XL, &xl));
166:   PetscCall(VecGetArrayRead(tao->XU, &xu));
167:   for (i = 0; i < n; i++) {
168:     idx = low + i;
169:     if ((PetscRealPart(xl[i]) > PETSC_NINFINITY) && (PetscRealPart(xu[i]) < PETSC_INFINITY)) {
170:       if (PetscRealPart(xl[i]) == PetscRealPart(xu[i])) {
171:         ixfixed[pdipm->nxfixed++] = idx;
172:       } else ixbox[pdipm->nxbox++] = idx;
173:     } else {
174:       if ((PetscRealPart(xl[i]) > PETSC_NINFINITY) && (PetscRealPart(xu[i]) >= PETSC_INFINITY)) {
175:         ixlb[pdipm->nxlb++] = idx;
176:       } else if ((PetscRealPart(xl[i]) <= PETSC_NINFINITY) && (PetscRealPart(xu[i]) < PETSC_INFINITY)) {
177:         ixub[pdipm->nxlb++] = idx;
178:       } else ixfree[pdipm->nxfree++] = idx;
179:     }
180:   }
181:   PetscCall(VecRestoreArrayRead(tao->XL, &xl));
182:   PetscCall(VecRestoreArrayRead(tao->XU, &xu));

184:   PetscCall(PetscObjectGetComm((PetscObject)tao, &comm));
185:   recvbuf[0] = pdipm->nxlb;
186:   recvbuf[1] = pdipm->nxub;
187:   recvbuf[2] = pdipm->nxfixed;
188:   recvbuf[3] = pdipm->nxbox;
189:   recvbuf[4] = pdipm->nxfree;

191:   PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, recvbuf, 5, MPIU_INT, MPI_SUM, comm));
192:   pdipm->Nxlb    = recvbuf[0];
193:   pdipm->Nxub    = recvbuf[1];
194:   pdipm->Nxfixed = recvbuf[2];
195:   pdipm->Nxbox   = recvbuf[3];
196:   pdipm->Nxfree  = recvbuf[4];

198:   if (pdipm->Nxlb) PetscCall(ISCreateGeneral(comm, pdipm->nxlb, ixlb, PETSC_COPY_VALUES, &pdipm->isxlb));
199:   if (pdipm->Nxub) PetscCall(ISCreateGeneral(comm, pdipm->nxub, ixub, PETSC_COPY_VALUES, &pdipm->isxub));
200:   if (pdipm->Nxfixed) PetscCall(ISCreateGeneral(comm, pdipm->nxfixed, ixfixed, PETSC_COPY_VALUES, &pdipm->isxfixed));
201:   if (pdipm->Nxbox) PetscCall(ISCreateGeneral(comm, pdipm->nxbox, ixbox, PETSC_COPY_VALUES, &pdipm->isxbox));
202:   if (pdipm->Nxfree) PetscCall(ISCreateGeneral(comm, pdipm->nxfree, ixfree, PETSC_COPY_VALUES, &pdipm->isxfree));
203:   PetscCall(PetscFree5(ixlb, ixub, ixfixed, ixbox, ixfree));
204:   PetscFunctionReturn(PETSC_SUCCESS);
205: }

207: /*
208:    TaoPDIPMInitializeSolution - Initialize `TAOPDIPM` solution X = [x; lambdae; lambdai; z].
209:    X consists of four subvectors in the order [x; lambdae; lambdai; z]. These
210:      four subvectors need to be initialized and its values copied over to X. Instead
211:      of copying, we use `VecPlaceArray()`/`VecResetArray()` functions to share the memory locations for
212:      X and the subvectors

214:    Collective

216:    Input Parameter:
217: .  tao - Tao context

219:    Level: beginner
220: */
221: static PetscErrorCode TaoPDIPMInitializeSolution(Tao tao)
222: {
223:   TAO_PDIPM         *pdipm = (TAO_PDIPM *)tao->data;
224:   PetscScalar       *Xarr, *z, *lambdai;
225:   PetscInt           i;
226:   const PetscScalar *xarr, *h;

228:   PetscFunctionBegin;
229:   PetscCall(VecGetArrayWrite(pdipm->X, &Xarr));

231:   /* Set Initialize X.x = tao->solution */
232:   PetscCall(VecGetArrayRead(tao->solution, &xarr));
233:   PetscCall(PetscArraycpy(Xarr, xarr, pdipm->nx));
234:   PetscCall(VecRestoreArrayRead(tao->solution, &xarr));

236:   /* Initialize X.lambdae = 0.0 */
237:   if (pdipm->lambdae) PetscCall(VecSet(pdipm->lambdae, 0.0));

239:   /* Initialize X.lambdai = push_init_lambdai, X.z = push_init_slack */
240:   if (pdipm->Nci) {
241:     PetscCall(VecSet(pdipm->lambdai, pdipm->push_init_lambdai));
242:     PetscCall(VecSet(pdipm->z, pdipm->push_init_slack));

244:     /* Additional modification for X.lambdai and X.z */
245:     PetscCall(VecGetArrayWrite(pdipm->lambdai, &lambdai));
246:     PetscCall(VecGetArrayWrite(pdipm->z, &z));
247:     if (pdipm->Nh) {
248:       PetscCall(VecGetArrayRead(tao->constraints_inequality, &h));
249:       for (i = 0; i < pdipm->nh; i++) {
250:         if (h[i] < -pdipm->push_init_slack) z[i] = -h[i];
251:         if (pdipm->mu / z[i] > pdipm->push_init_lambdai) lambdai[i] = pdipm->mu / z[i];
252:       }
253:       PetscCall(VecRestoreArrayRead(tao->constraints_inequality, &h));
254:     }
255:     PetscCall(VecRestoreArrayWrite(pdipm->lambdai, &lambdai));
256:     PetscCall(VecRestoreArrayWrite(pdipm->z, &z));
257:   }

259:   PetscCall(VecRestoreArrayWrite(pdipm->X, &Xarr));
260:   PetscFunctionReturn(PETSC_SUCCESS);
261: }

263: /*
264:    TaoSNESJacobian_PDIPM - Evaluate the Hessian matrix at X

266:    Input Parameter:
267:    snes - SNES context
268:    X - KKT Vector
269:    *ctx - pdipm context

271:    Output Parameter:
272:    J - Hessian matrix
273:    Jpre - matrix to build the preconditioner from
274: */
275: static PetscErrorCode TaoSNESJacobian_PDIPM(SNES snes, Vec X, Mat J, Mat Jpre, PetscCtx ctx)
276: {
277:   Tao                tao   = (Tao)ctx;
278:   TAO_PDIPM         *pdipm = (TAO_PDIPM *)tao->data;
279:   PetscInt           i, row, cols[2], Jrstart, rjstart, nc, j;
280:   const PetscInt    *aj, *ranges, *Jranges, *rranges, *cranges;
281:   const PetscScalar *Xarr, *aa;
282:   PetscScalar        vals[2];
283:   PetscInt           proc, nx_all, *nce_all = pdipm->nce_all;

285:   PetscFunctionBegin;
286:   PetscCall(MatGetOwnershipRanges(Jpre, &Jranges));
287:   PetscCall(MatGetOwnershipRange(Jpre, &Jrstart, NULL));
288:   PetscCall(MatGetOwnershipRangesColumn(tao->hessian, &rranges));
289:   PetscCall(MatGetOwnershipRangesColumn(tao->hessian, &cranges));

291:   PetscCall(VecGetArrayRead(X, &Xarr));

293:   /* (1) insert Z and Ci to the 4th block of Jpre -- overwrite existing values */
294:   if (pdipm->solve_symmetric_kkt) { /* 1 for eq 17 revised pdipm doc 0 for eq 18 (symmetric KKT) */
295:     vals[0] = 1.0;
296:     for (i = 0; i < pdipm->nci; i++) {
297:       row     = Jrstart + pdipm->off_z + i;
298:       cols[0] = Jrstart + pdipm->off_lambdai + i;
299:       cols[1] = row;
300:       vals[1] = Xarr[pdipm->off_lambdai + i] / Xarr[pdipm->off_z + i];
301:       PetscCall(MatSetValues(Jpre, 1, &row, 2, cols, vals, INSERT_VALUES));
302:     }
303:   } else {
304:     for (i = 0; i < pdipm->nci; i++) {
305:       row     = Jrstart + pdipm->off_z + i;
306:       cols[0] = Jrstart + pdipm->off_lambdai + i;
307:       cols[1] = row;
308:       vals[0] = Xarr[pdipm->off_z + i];
309:       vals[1] = Xarr[pdipm->off_lambdai + i];
310:       PetscCall(MatSetValues(Jpre, 1, &row, 2, cols, vals, INSERT_VALUES));
311:     }
312:   }

314:   /* (2) insert 2nd row block of Jpre: [ grad g, 0, 0, 0] */
315:   if (pdipm->Ng) {
316:     PetscCall(MatGetOwnershipRange(tao->jacobian_equality, &rjstart, NULL));
317:     for (i = 0; i < pdipm->ng; i++) {
318:       row = Jrstart + pdipm->off_lambdae + i;

320:       PetscCall(MatGetRow(tao->jacobian_equality, i + rjstart, &nc, &aj, &aa));
321:       proc = 0;
322:       for (j = 0; j < nc; j++) {
323:         while (aj[j] >= cranges[proc + 1]) proc++;
324:         cols[0] = aj[j] - cranges[proc] + Jranges[proc];
325:         PetscCall(MatSetValue(Jpre, row, cols[0], aa[j], INSERT_VALUES));
326:       }
327:       PetscCall(MatRestoreRow(tao->jacobian_equality, i + rjstart, &nc, &aj, &aa));
328:       if (pdipm->kkt_pd) {
329:         /* add shift \delta_c */
330:         PetscCall(MatSetValue(Jpre, row, row, -pdipm->deltac, INSERT_VALUES));
331:       }
332:     }
333:   }

335:   /* (3) insert 3rd row block of Jpre: [ -grad h, 0, deltac, I] */
336:   if (pdipm->Nh) {
337:     PetscCall(MatGetOwnershipRange(tao->jacobian_inequality, &rjstart, NULL));
338:     for (i = 0; i < pdipm->nh; i++) {
339:       row = Jrstart + pdipm->off_lambdai + i;
340:       PetscCall(MatGetRow(tao->jacobian_inequality, i + rjstart, &nc, &aj, &aa));
341:       proc = 0;
342:       for (j = 0; j < nc; j++) {
343:         while (aj[j] >= cranges[proc + 1]) proc++;
344:         cols[0] = aj[j] - cranges[proc] + Jranges[proc];
345:         PetscCall(MatSetValue(Jpre, row, cols[0], -aa[j], INSERT_VALUES));
346:       }
347:       PetscCall(MatRestoreRow(tao->jacobian_inequality, i + rjstart, &nc, &aj, &aa));
348:       if (pdipm->kkt_pd) {
349:         /* add shift \delta_c */
350:         PetscCall(MatSetValue(Jpre, row, row, -pdipm->deltac, INSERT_VALUES));
351:       }
352:     }
353:   }

355:   /* (4) insert 1st row block of Jpre: [Wxx, grad g', -grad h', 0] */
356:   if (pdipm->Ng) { /* grad g' */
357:     PetscCall(MatTranspose(tao->jacobian_equality, MAT_REUSE_MATRIX, &pdipm->jac_equality_trans));
358:   }
359:   if (pdipm->Nh) { /* grad h' */
360:     PetscCall(MatTranspose(tao->jacobian_inequality, MAT_REUSE_MATRIX, &pdipm->jac_inequality_trans));
361:   }

363:   PetscCall(VecPlaceArray(pdipm->x, Xarr));
364:   PetscCall(TaoComputeHessian(tao, pdipm->x, tao->hessian, tao->hessian_pre));
365:   PetscCall(VecResetArray(pdipm->x));

367:   PetscCall(MatGetOwnershipRange(tao->hessian, &rjstart, NULL));
368:   for (i = 0; i < pdipm->nx; i++) {
369:     row = Jrstart + i;

371:     /* insert Wxx = fxx + ... -- provided by user */
372:     PetscCall(MatGetRow(tao->hessian, i + rjstart, &nc, &aj, &aa));
373:     proc = 0;
374:     for (j = 0; j < nc; j++) {
375:       while (aj[j] >= cranges[proc + 1]) proc++;
376:       cols[0] = aj[j] - cranges[proc] + Jranges[proc];
377:       if (row == cols[0] && pdipm->kkt_pd) {
378:         /* add shift deltaw to Wxx component */
379:         PetscCall(MatSetValue(Jpre, row, cols[0], aa[j] + pdipm->deltaw, INSERT_VALUES));
380:       } else {
381:         PetscCall(MatSetValue(Jpre, row, cols[0], aa[j], INSERT_VALUES));
382:       }
383:     }
384:     PetscCall(MatRestoreRow(tao->hessian, i + rjstart, &nc, &aj, &aa));

386:     /* insert grad g' */
387:     if (pdipm->ng) {
388:       PetscCall(MatGetRow(pdipm->jac_equality_trans, i + rjstart, &nc, &aj, &aa));
389:       PetscCall(MatGetOwnershipRanges(tao->jacobian_equality, &ranges));
390:       proc = 0;
391:       for (j = 0; j < nc; j++) {
392:         /* find row ownership of */
393:         while (aj[j] >= ranges[proc + 1]) proc++;
394:         nx_all  = rranges[proc + 1] - rranges[proc];
395:         cols[0] = aj[j] - ranges[proc] + Jranges[proc] + nx_all;
396:         PetscCall(MatSetValue(Jpre, row, cols[0], aa[j], INSERT_VALUES));
397:       }
398:       PetscCall(MatRestoreRow(pdipm->jac_equality_trans, i + rjstart, &nc, &aj, &aa));
399:     }

401:     /* insert -grad h' */
402:     if (pdipm->nh) {
403:       PetscCall(MatGetRow(pdipm->jac_inequality_trans, i + rjstart, &nc, &aj, &aa));
404:       PetscCall(MatGetOwnershipRanges(tao->jacobian_inequality, &ranges));
405:       proc = 0;
406:       for (j = 0; j < nc; j++) {
407:         /* find row ownership of */
408:         while (aj[j] >= ranges[proc + 1]) proc++;
409:         nx_all  = rranges[proc + 1] - rranges[proc];
410:         cols[0] = aj[j] - ranges[proc] + Jranges[proc] + nx_all + nce_all[proc];
411:         PetscCall(MatSetValue(Jpre, row, cols[0], -aa[j], INSERT_VALUES));
412:       }
413:       PetscCall(MatRestoreRow(pdipm->jac_inequality_trans, i + rjstart, &nc, &aj, &aa));
414:     }
415:   }
416:   PetscCall(VecRestoreArrayRead(X, &Xarr));

418:   /* (6) assemble Jpre and J */
419:   PetscCall(MatAssemblyBegin(Jpre, MAT_FINAL_ASSEMBLY));
420:   PetscCall(MatAssemblyEnd(Jpre, MAT_FINAL_ASSEMBLY));

422:   if (J != Jpre) {
423:     PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY));
424:     PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY));
425:   }
426:   PetscFunctionReturn(PETSC_SUCCESS);
427: }

429: /*
430:    TaoSnesFunction_PDIPM - Evaluate KKT function at X

432:    Input Parameter:
433:    snes - SNES context
434:    X - KKT Vector
435:    *ctx - pdipm

437:    Output Parameter:
438:    F - Updated Lagrangian vector
439: */
440: static PetscErrorCode TaoSNESFunction_PDIPM(SNES snes, Vec X, Vec F, PetscCtx ctx)
441: {
442:   Tao                tao   = (Tao)ctx;
443:   TAO_PDIPM         *pdipm = (TAO_PDIPM *)tao->data;
444:   PetscScalar       *Farr;
445:   Vec                x, L1;
446:   PetscInt           i;
447:   const PetscScalar *Xarr, *carr, *zarr, *larr;

449:   PetscFunctionBegin;
450:   PetscCall(VecSet(F, 0.0));

452:   PetscCall(VecGetArrayRead(X, &Xarr));
453:   PetscCall(VecGetArrayWrite(F, &Farr));

455:   /* (0) Evaluate f, fx, gradG, gradH at X.x Note: pdipm->x is not changed below */
456:   x = pdipm->x;
457:   PetscCall(VecPlaceArray(x, Xarr));
458:   PetscCall(TaoPDIPMEvaluateFunctionsAndJacobians(tao, x));

460:   /* Update ce, ci, and Jci at X.x */
461:   PetscCall(TaoPDIPMUpdateConstraints(tao, x));
462:   PetscCall(VecResetArray(x));

464:   /* (1) L1 = fx + (gradG'*DE + Jce_xfixed'*lambdae_xfixed) - (gradH'*DI + Jci_xb'*lambdai_xb) */
465:   L1 = pdipm->x;
466:   PetscCall(VecPlaceArray(L1, Farr)); /* L1 = 0.0 */
467:   if (pdipm->Nci) {
468:     if (pdipm->Nh) {
469:       /* L1 += gradH'*DI. Note: tao->DI is not changed below */
470:       PetscCall(VecPlaceArray(tao->DI, Xarr + pdipm->off_lambdai));
471:       PetscCall(MatMultTransposeAdd(tao->jacobian_inequality, tao->DI, L1, L1));
472:       PetscCall(VecResetArray(tao->DI));
473:     }

475:     /* L1 += Jci_xb'*lambdai_xb */
476:     PetscCall(VecPlaceArray(pdipm->lambdai_xb, Xarr + pdipm->off_lambdai + pdipm->nh));
477:     PetscCall(MatMultTransposeAdd(pdipm->Jci_xb, pdipm->lambdai_xb, L1, L1));
478:     PetscCall(VecResetArray(pdipm->lambdai_xb));

480:     /* L1 = - (gradH'*DI + Jci_xb'*lambdai_xb) */
481:     PetscCall(VecScale(L1, -1.0));
482:   }

484:   /* L1 += fx */
485:   PetscCall(VecAXPY(L1, 1.0, tao->gradient));

487:   if (pdipm->Nce) {
488:     if (pdipm->Ng) {
489:       /* L1 += gradG'*DE. Note: tao->DE is not changed below */
490:       PetscCall(VecPlaceArray(tao->DE, Xarr + pdipm->off_lambdae));
491:       PetscCall(MatMultTransposeAdd(tao->jacobian_equality, tao->DE, L1, L1));
492:       PetscCall(VecResetArray(tao->DE));
493:     }
494:     if (pdipm->Nxfixed) {
495:       /* L1 += Jce_xfixed'*lambdae_xfixed */
496:       PetscCall(VecPlaceArray(pdipm->lambdae_xfixed, Xarr + pdipm->off_lambdae + pdipm->ng));
497:       PetscCall(MatMultTransposeAdd(pdipm->Jce_xfixed, pdipm->lambdae_xfixed, L1, L1));
498:       PetscCall(VecResetArray(pdipm->lambdae_xfixed));
499:     }
500:   }
501:   PetscCall(VecResetArray(L1));

503:   /* (2) L2 = ce(x) */
504:   if (pdipm->Nce) {
505:     PetscCall(VecGetArrayRead(pdipm->ce, &carr));
506:     for (i = 0; i < pdipm->nce; i++) Farr[pdipm->off_lambdae + i] = carr[i];
507:     PetscCall(VecRestoreArrayRead(pdipm->ce, &carr));
508:   }

510:   if (pdipm->Nci) {
511:     if (pdipm->solve_symmetric_kkt) {
512:       /* (3) L3 = z - ci(x);
513:          (4) L4 = Lambdai * e - mu/z *e  */
514:       PetscCall(VecGetArrayRead(pdipm->ci, &carr));
515:       larr = Xarr + pdipm->off_lambdai;
516:       zarr = Xarr + pdipm->off_z;
517:       for (i = 0; i < pdipm->nci; i++) {
518:         Farr[pdipm->off_lambdai + i] = zarr[i] - carr[i];
519:         Farr[pdipm->off_z + i]       = larr[i] - pdipm->mu / zarr[i];
520:       }
521:       PetscCall(VecRestoreArrayRead(pdipm->ci, &carr));
522:     } else {
523:       /* (3) L3 = z - ci(x);
524:          (4) L4 = Z * Lambdai * e - mu * e  */
525:       PetscCall(VecGetArrayRead(pdipm->ci, &carr));
526:       larr = Xarr + pdipm->off_lambdai;
527:       zarr = Xarr + pdipm->off_z;
528:       for (i = 0; i < pdipm->nci; i++) {
529:         Farr[pdipm->off_lambdai + i] = zarr[i] - carr[i];
530:         Farr[pdipm->off_z + i]       = zarr[i] * larr[i] - pdipm->mu;
531:       }
532:       PetscCall(VecRestoreArrayRead(pdipm->ci, &carr));
533:     }
534:   }

536:   PetscCall(VecRestoreArrayRead(X, &Xarr));
537:   PetscCall(VecRestoreArrayWrite(F, &Farr));
538:   PetscFunctionReturn(PETSC_SUCCESS);
539: }

541: /*
542:   Evaluate F(X); then update tao->gnorm0, tao->step = mu,
543:   tao->residual = norm2(F_x,F_z) and tao->cnorm = norm2(F_ce,F_ci).
544: */
545: static PetscErrorCode TaoSNESFunction_PDIPM_residual(SNES snes, Vec X, Vec F, PetscCtx ctx)
546: {
547:   Tao                tao   = (Tao)ctx;
548:   TAO_PDIPM         *pdipm = (TAO_PDIPM *)tao->data;
549:   PetscScalar       *Farr, *tmparr;
550:   Vec                L1;
551:   PetscInt           i;
552:   PetscReal          res[2], cnorm[2];
553:   const PetscScalar *Xarr = NULL;

555:   PetscFunctionBegin;
556:   PetscCall(TaoSNESFunction_PDIPM(snes, X, F, (void *)tao));
557:   PetscCall(VecGetArrayWrite(F, &Farr));
558:   PetscCall(VecGetArrayRead(X, &Xarr));

560:   /* compute res[0] = norm2(F_x) */
561:   L1 = pdipm->x;
562:   PetscCall(VecPlaceArray(L1, Farr));
563:   PetscCall(VecNorm(L1, NORM_2, &res[0]));
564:   PetscCall(VecResetArray(L1));

566:   /* compute res[1] = norm2(F_z), cnorm[1] = norm2(F_ci) */
567:   if (pdipm->z) {
568:     if (pdipm->solve_symmetric_kkt) {
569:       PetscCall(VecPlaceArray(pdipm->z, Farr + pdipm->off_z));
570:       if (pdipm->Nci) {
571:         PetscCall(VecGetArrayWrite(pdipm->z, &tmparr));
572:         for (i = 0; i < pdipm->nci; i++) tmparr[i] *= Xarr[pdipm->off_z + i];
573:         PetscCall(VecRestoreArrayWrite(pdipm->z, &tmparr));
574:       }

576:       PetscCall(VecNorm(pdipm->z, NORM_2, &res[1]));

578:       if (pdipm->Nci) {
579:         PetscCall(VecGetArrayWrite(pdipm->z, &tmparr));
580:         for (i = 0; i < pdipm->nci; i++) tmparr[i] /= Xarr[pdipm->off_z + i];
581:         PetscCall(VecRestoreArrayWrite(pdipm->z, &tmparr));
582:       }
583:       PetscCall(VecResetArray(pdipm->z));
584:     } else { /* !solve_symmetric_kkt */
585:       PetscCall(VecPlaceArray(pdipm->z, Farr + pdipm->off_z));
586:       PetscCall(VecNorm(pdipm->z, NORM_2, &res[1]));
587:       PetscCall(VecResetArray(pdipm->z));
588:     }

590:     PetscCall(VecPlaceArray(pdipm->ci, Farr + pdipm->off_lambdai));
591:     PetscCall(VecNorm(pdipm->ci, NORM_2, &cnorm[1]));
592:     PetscCall(VecResetArray(pdipm->ci));
593:   } else {
594:     res[1]   = 0.0;
595:     cnorm[1] = 0.0;
596:   }

598:   /* compute cnorm[0] = norm2(F_ce) */
599:   if (pdipm->Nce) {
600:     PetscCall(VecPlaceArray(pdipm->ce, Farr + pdipm->off_lambdae));
601:     PetscCall(VecNorm(pdipm->ce, NORM_2, &cnorm[0]));
602:     PetscCall(VecResetArray(pdipm->ce));
603:   } else cnorm[0] = 0.0;

605:   PetscCall(VecRestoreArrayWrite(F, &Farr));
606:   PetscCall(VecRestoreArrayRead(X, &Xarr));

608:   tao->gnorm0   = tao->residual;
609:   tao->residual = PetscSqrtReal(res[0] * res[0] + res[1] * res[1]);
610:   tao->cnorm    = PetscSqrtReal(cnorm[0] * cnorm[0] + cnorm[1] * cnorm[1]);
611:   tao->step     = pdipm->mu;
612:   PetscFunctionReturn(PETSC_SUCCESS);
613: }

615: /*
616:   PCPostSetup_PDIPM -- called when the KKT matrix is Cholesky factored for the preconditioner. Checks the inertia of Cholesky factor of the KKT matrix.
617:   If it does not match the numbers of prime and dual variables, add shifts to the KKT matrix.
618: */
619: static PetscErrorCode PCPostSetUp_PDIPM(PC pc)
620: {
621:   Tao        tao;
622:   TAO_PDIPM *pdipm;
623:   Vec        X;
624:   SNES       snes;
625:   KSP        ksp;
626:   Mat        Factor;
627:   PetscBool  isCHOL;
628:   PetscInt   nneg, nzero, npos;

630:   PetscFunctionBegin;
631:   PetscCall(PCGetApplicationContext(pc, &tao));
632:   pdipm = (TAO_PDIPM *)tao->data;
633:   X     = pdipm->X;
634:   snes  = pdipm->snes;

636:   /* Get the inertia of Cholesky factor */
637:   PetscCall(SNESGetKSP(snes, &ksp));
638:   PetscCall(KSPGetPC(ksp, &pc));
639:   PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCCHOLESKY, &isCHOL));
640:   if (!isCHOL) PetscFunctionReturn(PETSC_SUCCESS);

642:   PetscCall(PCFactorGetMatrix(pc, &Factor));
643:   PetscCall(MatGetInertia(Factor, &nneg, &nzero, &npos));

645:   if (npos < pdipm->Nx + pdipm->Nci) {
646:     pdipm->deltaw = PetscMax(pdipm->lastdeltaw / 3, 1.e-4 * PETSC_MACHINE_EPSILON);
647:     PetscCall(PetscInfo(tao, "Test reduced deltaw=%g; previous MatInertia: nneg %" PetscInt_FMT ", nzero %" PetscInt_FMT ", npos %" PetscInt_FMT "(<%" PetscInt_FMT ")\n", (double)pdipm->deltaw, nneg, nzero, npos, pdipm->Nx + pdipm->Nci));
648:     PetscCall(TaoSNESJacobian_PDIPM(snes, X, pdipm->K, pdipm->K, tao));
649:     PetscCall(PCSetPostSetUp(pc, NULL));
650:     PetscCall(PCSetUp(pc));
651:     PetscCall(MatGetInertia(Factor, &nneg, &nzero, &npos));

653:     if (npos < pdipm->Nx + pdipm->Nci) {
654:       pdipm->deltaw = pdipm->lastdeltaw;                                           /* in case reduction update does not help, this prevents that step from impacting increasing update */
655:       while (npos < pdipm->Nx + pdipm->Nci && pdipm->deltaw <= 1. / PETSC_SMALL) { /* increase deltaw */
656:         PetscCall(PetscInfo(tao, "  deltaw=%g fails, MatInertia: nneg %" PetscInt_FMT ", nzero %" PetscInt_FMT ", npos %" PetscInt_FMT "(<%" PetscInt_FMT ")\n", (double)pdipm->deltaw, nneg, nzero, npos, pdipm->Nx + pdipm->Nci));
657:         pdipm->deltaw = PetscMin(8 * pdipm->deltaw, PetscPowReal(10, 20));
658:         PetscCall(TaoSNESJacobian_PDIPM(snes, X, pdipm->K, pdipm->K, tao));
659:         PetscCall(PCSetUp(pc));
660:         PetscCall(MatGetInertia(Factor, &nneg, &nzero, &npos));
661:       }

663:       PetscCheck(pdipm->deltaw < 1. / PETSC_SMALL, PetscObjectComm((PetscObject)tao), PETSC_ERR_CONV_FAILED, "Reached maximum delta w will not converge, try different initial x0");

665:       PetscCall(PetscInfo(tao, "Updated deltaw %g\n", (double)pdipm->deltaw));
666:       pdipm->lastdeltaw = pdipm->deltaw;
667:       pdipm->deltaw     = 0.0;
668:     }
669:   }

671:   if (nzero) { /* Jacobian is singular */
672:     if (pdipm->deltac == 0.0) {
673:       pdipm->deltac = PETSC_SQRT_MACHINE_EPSILON;
674:     } else {
675:       pdipm->deltac = pdipm->deltac * PetscPowReal(pdipm->mu, .25);
676:     }
677:     PetscCall(PetscInfo(tao, "Updated deltac=%g, MatInertia: nneg %" PetscInt_FMT ", nzero %" PetscInt_FMT "(!=0), npos %" PetscInt_FMT "\n", (double)pdipm->deltac, nneg, nzero, npos));
678:     PetscCall(TaoSNESJacobian_PDIPM(snes, X, pdipm->K, pdipm->K, tao));
679:     PetscCall(PCSetPostSetUp(pc, NULL));
680:     PetscCall(PCSetUp(pc));
681:     PetscCall(MatGetInertia(Factor, &nneg, &nzero, &npos));
682:   }
683:   PetscCall(PCSetPostSetUp(pc, PCPostSetUp_PDIPM));
684:   PetscFunctionReturn(PETSC_SUCCESS);
685: }

687: /*
688:    SNESLineSearch_PDIPM - Custom line search used with PDIPM.

690:    Collective

692:    Notes:
693:    This routine employs a simple backtracking line-search to keep
694:    the slack variables (z) and inequality constraints Lagrange multipliers
695:    (lambdai) positive, i.e., z,lambdai >=0. It does this by calculating scalars
696:    alpha_p and alpha_d to keep z,lambdai non-negative. The decision (x), and the
697:    slack variables are updated as X = X - alpha_d*dx. The constraint multipliers
698:    are updated as Lambdai = Lambdai + alpha_p*dLambdai. The barrier parameter mu
699:    is also updated as mu = mu + z'lambdai/Nci
700: */
701: static PetscErrorCode SNESLineSearch_PDIPM(SNESLineSearch linesearch, PetscCtx ctx)
702: {
703:   Tao                tao   = (Tao)ctx;
704:   TAO_PDIPM         *pdipm = (TAO_PDIPM *)tao->data;
705:   SNES               snes;
706:   Vec                X, F, Y;
707:   PetscInt           i, iter;
708:   PetscReal          alpha_p = 1.0, alpha_d = 1.0, alpha[2];
709:   PetscScalar       *Xarr, *z, *lambdai, dot, *taosolarr;
710:   const PetscScalar *dXarr, *dz, *dlambdai;

712:   PetscFunctionBegin;
713:   PetscCall(SNESLineSearchGetSNES(linesearch, &snes));
714:   PetscCall(SNESGetIterationNumber(snes, &iter));

716:   PetscCall(SNESLineSearchSetReason(linesearch, SNES_LINESEARCH_SUCCEEDED));
717:   PetscCall(SNESLineSearchGetVecs(linesearch, &X, &F, &Y, NULL, NULL));

719:   PetscCall(VecGetArrayWrite(X, &Xarr));
720:   PetscCall(VecGetArrayRead(Y, &dXarr));
721:   z  = Xarr + pdipm->off_z;
722:   dz = dXarr + pdipm->off_z;
723:   for (i = 0; i < pdipm->nci; i++) {
724:     if (z[i] - dz[i] < 0.0) alpha_p = PetscMin(alpha_p, 0.9999 * z[i] / dz[i]);
725:   }

727:   lambdai  = Xarr + pdipm->off_lambdai;
728:   dlambdai = dXarr + pdipm->off_lambdai;

730:   for (i = 0; i < pdipm->nci; i++) {
731:     if (lambdai[i] - dlambdai[i] < 0.0) alpha_d = PetscMin(0.9999 * lambdai[i] / dlambdai[i], alpha_d);
732:   }

734:   alpha[0] = alpha_p;
735:   alpha[1] = alpha_d;
736:   PetscCall(VecRestoreArrayRead(Y, &dXarr));
737:   PetscCall(VecRestoreArrayWrite(X, &Xarr));

739:   /* alpha = min(alpha) over all processes */
740:   PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, alpha, 2, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)tao)));

742:   alpha_p = alpha[0];
743:   alpha_d = alpha[1];

745:   /* X = X - alpha * Y */
746:   PetscCall(VecGetArrayWrite(X, &Xarr));
747:   PetscCall(VecGetArrayRead(Y, &dXarr));
748:   for (i = 0; i < pdipm->nx; i++) Xarr[i] -= alpha_p * dXarr[i];
749:   for (i = 0; i < pdipm->nce; i++) Xarr[i + pdipm->off_lambdae] -= alpha_d * dXarr[i + pdipm->off_lambdae];

751:   for (i = 0; i < pdipm->nci; i++) {
752:     Xarr[i + pdipm->off_lambdai] -= alpha_d * dXarr[i + pdipm->off_lambdai];
753:     Xarr[i + pdipm->off_z] -= alpha_p * dXarr[i + pdipm->off_z];
754:   }
755:   PetscCall(VecGetArrayWrite(tao->solution, &taosolarr));
756:   PetscCall(PetscArraycpy(taosolarr, Xarr, pdipm->nx));
757:   PetscCall(VecRestoreArrayWrite(tao->solution, &taosolarr));

759:   PetscCall(VecRestoreArrayWrite(X, &Xarr));
760:   PetscCall(VecRestoreArrayRead(Y, &dXarr));

762:   /* Update mu = mu_update_factor * dot(z,lambdai)/pdipm->nci at updated X */
763:   if (pdipm->z) PetscCall(VecDot(pdipm->z, pdipm->lambdai, &dot));
764:   else dot = 0.0;

766:   /* if (PetscAbsReal(pdipm->gradL) < 0.9*pdipm->mu)  */
767:   pdipm->mu = pdipm->Nci ? pdipm->mu_update_factor * dot / pdipm->Nci : 0.;

769:   /* Update F; get tao->residual and tao->cnorm */
770:   PetscCall(TaoSNESFunction_PDIPM_residual(snes, X, F, (void *)tao));

772:   tao->niter++;
773:   PetscCall(TaoLogConvergenceHistory(tao, pdipm->obj, tao->residual, tao->cnorm, tao->niter));
774:   PetscCall(TaoMonitor(tao, tao->niter, pdipm->obj, tao->residual, tao->cnorm, pdipm->mu));

776:   PetscUseTypeMethod(tao, convergencetest, tao->cnvP);
777:   if (tao->reason) PetscCall(SNESSetConvergedReason(snes, SNES_CONVERGED_FNORM_ABS));
778:   PetscFunctionReturn(PETSC_SUCCESS);
779: }

781: static PetscErrorCode TaoSolve_PDIPM(Tao tao)
782: {
783:   TAO_PDIPM     *pdipm = (TAO_PDIPM *)tao->data;
784:   SNESLineSearch linesearch; /* SNESLineSearch context */
785:   Vec            dummy;

787:   PetscFunctionBegin;
788:   PetscCheck(tao->constraints_equality || tao->constraints_inequality, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_NULL, "Equality and inequality constraints are not set. Either set them or switch to a different algorithm");

790:   /* Initialize all variables */
791:   PetscCall(TaoPDIPMInitializeSolution(tao));

793:   /* Set linesearch */
794:   PetscCall(SNESGetLineSearch(pdipm->snes, &linesearch));
795:   PetscCall(SNESLineSearchSetType(linesearch, SNESLINESEARCHSHELL));
796:   PetscCall(SNESLineSearchShellSetApply(linesearch, SNESLineSearch_PDIPM, tao));
797:   PetscCall(SNESLineSearchSetFromOptions(linesearch));

799:   tao->reason = TAO_CONTINUE_ITERATING;

801:   /* -tao_monitor for iteration 0 and check convergence */
802:   PetscCall(VecDuplicate(pdipm->X, &dummy));
803:   PetscCall(TaoSNESFunction_PDIPM_residual(pdipm->snes, pdipm->X, dummy, (void *)tao));

805:   PetscCall(TaoLogConvergenceHistory(tao, pdipm->obj, tao->residual, tao->cnorm, tao->niter));
806:   PetscCall(TaoMonitor(tao, tao->niter, pdipm->obj, tao->residual, tao->cnorm, pdipm->mu));
807:   PetscCall(VecDestroy(&dummy));
808:   PetscUseTypeMethod(tao, convergencetest, tao->cnvP);
809:   if (tao->reason) PetscCall(SNESSetConvergedReason(pdipm->snes, SNES_CONVERGED_FNORM_ABS));

811:   while (tao->reason == TAO_CONTINUE_ITERATING) {
812:     SNESConvergedReason reason;
813:     PetscCall(SNESSolve(pdipm->snes, NULL, pdipm->X));

815:     /* Check SNES convergence */
816:     PetscCall(SNESGetConvergedReason(pdipm->snes, &reason));
817:     if (reason < 0) PetscCall(PetscPrintf(PetscObjectComm((PetscObject)pdipm->snes), "SNES solve did not converged due to reason %s\n", SNESConvergedReasons[reason]));

819:     /* Check TAO convergence */
820:     PetscCheck(!PetscIsInfOrNanReal(pdipm->obj), PETSC_COMM_SELF, PETSC_ERR_SUP, "User-provided compute function generated infinity or NaN");
821:   }
822:   PetscFunctionReturn(PETSC_SUCCESS);
823: }

825: static PetscErrorCode TaoView_PDIPM(Tao tao, PetscViewer viewer)
826: {
827:   TAO_PDIPM *pdipm = (TAO_PDIPM *)tao->data;

829:   PetscFunctionBegin;
830:   tao->constrained = PETSC_TRUE;
831:   PetscCall(PetscViewerASCIIPushTab(viewer));
832:   PetscCall(PetscViewerASCIIPrintf(viewer, "Number of prime=%" PetscInt_FMT ", Number of dual=%" PetscInt_FMT "\n", pdipm->Nx + pdipm->Nci, pdipm->Nce + pdipm->Nci));
833:   if (pdipm->kkt_pd) PetscCall(PetscViewerASCIIPrintf(viewer, "KKT shifts deltaw=%g, deltac=%g\n", (double)pdipm->deltaw, (double)pdipm->deltac));
834:   PetscCall(PetscViewerASCIIPopTab(viewer));
835:   PetscFunctionReturn(PETSC_SUCCESS);
836: }

838: static PetscErrorCode TaoSetup_PDIPM(Tao tao)
839: {
840:   TAO_PDIPM         *pdipm = (TAO_PDIPM *)tao->data;
841:   MPI_Comm           comm;
842:   PetscMPIInt        size;
843:   PetscInt           row, col, Jcrstart, Jcrend, k, tmp, nc, proc, *nh_all, *ng_all;
844:   PetscInt           offset, *xa, *xb, i, j, rstart, rend;
845:   PetscScalar        one = 1.0, neg_one = -1.0;
846:   const PetscInt    *cols, *rranges, *cranges, *aj, *ranges;
847:   const PetscScalar *aa, *Xarr;
848:   Mat                J;
849:   Mat                Jce_xfixed_trans, Jci_xb_trans;
850:   PetscInt          *dnz, *onz, rjstart, nx_all, *nce_all, *Jranges, cols1[2];

852:   PetscFunctionBegin;
853:   PetscCall(PetscObjectGetComm((PetscObject)tao, &comm));
854:   PetscCallMPI(MPI_Comm_size(comm, &size));

856:   /* (1) Setup Bounds and create Tao vectors */
857:   PetscCall(TaoPDIPMSetUpBounds(tao));

859:   if (!tao->gradient) {
860:     PetscCall(VecDuplicate(tao->solution, &tao->gradient));
861:     PetscCall(VecDuplicate(tao->solution, &tao->stepdirection));
862:   }

864:   /* (2) Get sizes */
865:   /* Size of vector x - This is set by TaoSetSolution */
866:   PetscCall(VecGetSize(tao->solution, &pdipm->Nx));
867:   PetscCall(VecGetLocalSize(tao->solution, &pdipm->nx));

869:   /* Size of equality constraints and vectors */
870:   if (tao->constraints_equality) {
871:     PetscCall(VecGetSize(tao->constraints_equality, &pdipm->Ng));
872:     PetscCall(VecGetLocalSize(tao->constraints_equality, &pdipm->ng));
873:   } else {
874:     pdipm->ng = pdipm->Ng = 0;
875:   }

877:   pdipm->nce = pdipm->ng + pdipm->nxfixed;
878:   pdipm->Nce = pdipm->Ng + pdipm->Nxfixed;

880:   /* Size of inequality constraints and vectors */
881:   if (tao->constraints_inequality) {
882:     PetscCall(VecGetSize(tao->constraints_inequality, &pdipm->Nh));
883:     PetscCall(VecGetLocalSize(tao->constraints_inequality, &pdipm->nh));
884:   } else {
885:     pdipm->nh = pdipm->Nh = 0;
886:   }

888:   pdipm->nci = pdipm->nh + pdipm->nxlb + pdipm->nxub + 2 * pdipm->nxbox;
889:   pdipm->Nci = pdipm->Nh + pdipm->Nxlb + pdipm->Nxub + 2 * pdipm->Nxbox;

891:   /* Full size of the KKT system to be solved */
892:   pdipm->n = pdipm->nx + pdipm->nce + 2 * pdipm->nci;
893:   pdipm->N = pdipm->Nx + pdipm->Nce + 2 * pdipm->Nci;

895:   /* (3) Offsets for subvectors */
896:   pdipm->off_lambdae = pdipm->nx;
897:   pdipm->off_lambdai = pdipm->off_lambdae + pdipm->nce;
898:   pdipm->off_z       = pdipm->off_lambdai + pdipm->nci;

900:   /* (4) Create vectors and subvectors */
901:   /* Ce and Ci vectors */
902:   PetscCall(VecCreate(comm, &pdipm->ce));
903:   PetscCall(VecSetSizes(pdipm->ce, pdipm->nce, pdipm->Nce));
904:   PetscCall(VecSetFromOptions(pdipm->ce));

906:   PetscCall(VecCreate(comm, &pdipm->ci));
907:   PetscCall(VecSetSizes(pdipm->ci, pdipm->nci, pdipm->Nci));
908:   PetscCall(VecSetFromOptions(pdipm->ci));

910:   /* X=[x; lambdae; lambdai; z] for the big KKT system */
911:   PetscCall(VecCreate(comm, &pdipm->X));
912:   PetscCall(VecSetSizes(pdipm->X, pdipm->n, pdipm->N));
913:   PetscCall(VecSetFromOptions(pdipm->X));

915:   /* Subvectors; they share local arrays with X */
916:   PetscCall(VecGetArrayRead(pdipm->X, &Xarr));
917:   /* x shares local array with X.x */
918:   if (pdipm->Nx) PetscCall(VecCreateMPIWithArray(comm, 1, pdipm->nx, pdipm->Nx, Xarr, &pdipm->x));

920:   /* lambdae shares local array with X.lambdae */
921:   if (pdipm->Nce) PetscCall(VecCreateMPIWithArray(comm, 1, pdipm->nce, pdipm->Nce, Xarr + pdipm->off_lambdae, &pdipm->lambdae));

923:   /* tao->DE shares local array with X.lambdae_g */
924:   if (pdipm->Ng) {
925:     PetscCall(VecCreateMPIWithArray(comm, 1, pdipm->ng, pdipm->Ng, Xarr + pdipm->off_lambdae, &tao->DE));

927:     PetscCall(VecCreate(comm, &pdipm->lambdae_xfixed));
928:     PetscCall(VecSetSizes(pdipm->lambdae_xfixed, pdipm->nxfixed, PETSC_DECIDE));
929:     PetscCall(VecSetFromOptions(pdipm->lambdae_xfixed));
930:   }

932:   if (pdipm->Nci) {
933:     /* lambdai shares local array with X.lambdai */
934:     PetscCall(VecCreateMPIWithArray(comm, 1, pdipm->nci, pdipm->Nci, Xarr + pdipm->off_lambdai, &pdipm->lambdai));

936:     /* z for slack variables; it shares local array with X.z */
937:     PetscCall(VecCreateMPIWithArray(comm, 1, pdipm->nci, pdipm->Nci, Xarr + pdipm->off_z, &pdipm->z));
938:   }

940:   /* tao->DI which shares local array with X.lambdai_h */
941:   if (pdipm->Nh) PetscCall(VecCreateMPIWithArray(comm, 1, pdipm->nh, pdipm->Nh, Xarr + pdipm->off_lambdai, &tao->DI));
942:   PetscCall(VecCreate(comm, &pdipm->lambdai_xb));
943:   PetscCall(VecSetSizes(pdipm->lambdai_xb, pdipm->nci - pdipm->nh, PETSC_DECIDE));
944:   PetscCall(VecSetFromOptions(pdipm->lambdai_xb));

946:   PetscCall(VecRestoreArrayRead(pdipm->X, &Xarr));

948:   /* (5) Create Jacobians Jce_xfixed and Jci */
949:   /* (5.1) PDIPM Jacobian of equality bounds cebound(x) = J_nxfixed */
950:   if (pdipm->Nxfixed) {
951:     /* Create Jce_xfixed */
952:     PetscCall(MatCreate(comm, &pdipm->Jce_xfixed));
953:     PetscCall(MatSetSizes(pdipm->Jce_xfixed, pdipm->nxfixed, pdipm->nx, PETSC_DECIDE, pdipm->Nx));
954:     PetscCall(MatSetFromOptions(pdipm->Jce_xfixed));
955:     PetscCall(MatSeqAIJSetPreallocation(pdipm->Jce_xfixed, 1, NULL));
956:     PetscCall(MatMPIAIJSetPreallocation(pdipm->Jce_xfixed, 1, NULL, 1, NULL));

958:     PetscCall(MatGetOwnershipRange(pdipm->Jce_xfixed, &Jcrstart, &Jcrend));
959:     PetscCall(ISGetIndices(pdipm->isxfixed, &cols));
960:     k = 0;
961:     for (row = Jcrstart; row < Jcrend; row++) {
962:       PetscCall(MatSetValues(pdipm->Jce_xfixed, 1, &row, 1, cols + k, &one, INSERT_VALUES));
963:       k++;
964:     }
965:     PetscCall(ISRestoreIndices(pdipm->isxfixed, &cols));
966:     PetscCall(MatAssemblyBegin(pdipm->Jce_xfixed, MAT_FINAL_ASSEMBLY));
967:     PetscCall(MatAssemblyEnd(pdipm->Jce_xfixed, MAT_FINAL_ASSEMBLY));
968:   }

970:   /* (5.2) PDIPM inequality Jacobian Jci = [tao->jacobian_inequality; ...] */
971:   PetscCall(MatCreate(comm, &pdipm->Jci_xb));
972:   PetscCall(MatSetSizes(pdipm->Jci_xb, pdipm->nci - pdipm->nh, pdipm->nx, PETSC_DECIDE, pdipm->Nx));
973:   PetscCall(MatSetFromOptions(pdipm->Jci_xb));
974:   PetscCall(MatSeqAIJSetPreallocation(pdipm->Jci_xb, 1, NULL));
975:   PetscCall(MatMPIAIJSetPreallocation(pdipm->Jci_xb, 1, NULL, 1, NULL));

977:   PetscCall(MatGetOwnershipRange(pdipm->Jci_xb, &Jcrstart, &Jcrend));
978:   offset = Jcrstart;
979:   if (pdipm->Nxub) {
980:     /* Add xub to Jci_xb */
981:     PetscCall(ISGetIndices(pdipm->isxub, &cols));
982:     k = 0;
983:     for (row = offset; row < offset + pdipm->nxub; row++) {
984:       PetscCall(MatSetValues(pdipm->Jci_xb, 1, &row, 1, cols + k, &neg_one, INSERT_VALUES));
985:       k++;
986:     }
987:     PetscCall(ISRestoreIndices(pdipm->isxub, &cols));
988:   }

990:   if (pdipm->Nxlb) {
991:     /* Add xlb to Jci_xb */
992:     PetscCall(ISGetIndices(pdipm->isxlb, &cols));
993:     k = 0;
994:     offset += pdipm->nxub;
995:     for (row = offset; row < offset + pdipm->nxlb; row++) {
996:       PetscCall(MatSetValues(pdipm->Jci_xb, 1, &row, 1, cols + k, &one, INSERT_VALUES));
997:       k++;
998:     }
999:     PetscCall(ISRestoreIndices(pdipm->isxlb, &cols));
1000:   }

1002:   /* Add xbox to Jci_xb */
1003:   if (pdipm->Nxbox) {
1004:     PetscCall(ISGetIndices(pdipm->isxbox, &cols));
1005:     k = 0;
1006:     offset += pdipm->nxlb;
1007:     for (row = offset; row < offset + pdipm->nxbox; row++) {
1008:       PetscCall(MatSetValues(pdipm->Jci_xb, 1, &row, 1, cols + k, &neg_one, INSERT_VALUES));
1009:       tmp = row + pdipm->nxbox;
1010:       PetscCall(MatSetValues(pdipm->Jci_xb, 1, &tmp, 1, cols + k, &one, INSERT_VALUES));
1011:       k++;
1012:     }
1013:     PetscCall(ISRestoreIndices(pdipm->isxbox, &cols));
1014:   }

1016:   PetscCall(MatAssemblyBegin(pdipm->Jci_xb, MAT_FINAL_ASSEMBLY));
1017:   PetscCall(MatAssemblyEnd(pdipm->Jci_xb, MAT_FINAL_ASSEMBLY));
1018:   /* PetscCall(MatView(pdipm->Jci_xb,PETSC_VIEWER_STDOUT_WORLD)); */

1020:   /* (6) Set up ISs for PC Fieldsplit */
1021:   if (pdipm->solve_reduced_kkt) {
1022:     PetscCall(PetscMalloc2(pdipm->nx + pdipm->nce, &xa, 2 * pdipm->nci, &xb));
1023:     for (i = 0; i < pdipm->nx + pdipm->nce; i++) xa[i] = i;
1024:     for (i = 0; i < 2 * pdipm->nci; i++) xb[i] = pdipm->off_lambdai + i;

1026:     PetscCall(ISCreateGeneral(comm, pdipm->nx + pdipm->nce, xa, PETSC_OWN_POINTER, &pdipm->is1));
1027:     PetscCall(ISCreateGeneral(comm, 2 * pdipm->nci, xb, PETSC_OWN_POINTER, &pdipm->is2));
1028:   }

1030:   /* (7) Gather offsets from all processes */
1031:   PetscCall(PetscMalloc1(size, &pdipm->nce_all));

1033:   /* Get rstart of KKT matrix */
1034:   PetscCallMPI(MPI_Scan(&pdipm->n, &rstart, 1, MPIU_INT, MPI_SUM, comm));
1035:   rstart -= pdipm->n;

1037:   PetscCallMPI(MPI_Allgather(&pdipm->nce, 1, MPIU_INT, pdipm->nce_all, 1, MPIU_INT, comm));

1039:   PetscCall(PetscMalloc3(size, &ng_all, size, &nh_all, size, &Jranges));
1040:   PetscCallMPI(MPI_Allgather(&rstart, 1, MPIU_INT, Jranges, 1, MPIU_INT, comm));
1041:   PetscCallMPI(MPI_Allgather(&pdipm->nh, 1, MPIU_INT, nh_all, 1, MPIU_INT, comm));
1042:   PetscCallMPI(MPI_Allgather(&pdipm->ng, 1, MPIU_INT, ng_all, 1, MPIU_INT, comm));

1044:   PetscCall(MatGetOwnershipRanges(tao->hessian, &rranges));
1045:   PetscCall(MatGetOwnershipRangesColumn(tao->hessian, &cranges));

1047:   if (pdipm->Ng) {
1048:     PetscCall(TaoComputeJacobianEquality(tao, tao->solution, tao->jacobian_equality, tao->jacobian_equality_pre));
1049:     PetscCall(MatTranspose(tao->jacobian_equality, MAT_INITIAL_MATRIX, &pdipm->jac_equality_trans));
1050:   }
1051:   if (pdipm->Nh) {
1052:     PetscCall(TaoComputeJacobianInequality(tao, tao->solution, tao->jacobian_inequality, tao->jacobian_inequality_pre));
1053:     PetscCall(MatTranspose(tao->jacobian_inequality, MAT_INITIAL_MATRIX, &pdipm->jac_inequality_trans));
1054:   }

1056:   /* Count dnz,onz for preallocation of KKT matrix */
1057:   nce_all = pdipm->nce_all;

1059:   if (pdipm->Nxfixed) PetscCall(MatTranspose(pdipm->Jce_xfixed, MAT_INITIAL_MATRIX, &Jce_xfixed_trans));
1060:   PetscCall(MatTranspose(pdipm->Jci_xb, MAT_INITIAL_MATRIX, &Jci_xb_trans));

1062:   MatPreallocateBegin(comm, pdipm->n, pdipm->n, dnz, onz);

1064:   /* 1st row block of KKT matrix: [Wxx; gradCe'; -gradCi'; 0] */
1065:   PetscCall(TaoPDIPMEvaluateFunctionsAndJacobians(tao, pdipm->x));
1066:   PetscCall(TaoComputeHessian(tao, tao->solution, tao->hessian, tao->hessian_pre));

1068:   /* Insert tao->hessian */
1069:   PetscCall(MatGetOwnershipRange(tao->hessian, &rjstart, NULL));
1070:   for (i = 0; i < pdipm->nx; i++) {
1071:     row = rstart + i;

1073:     PetscCall(MatGetRow(tao->hessian, i + rjstart, &nc, &aj, NULL));
1074:     proc = 0;
1075:     for (j = 0; j < nc; j++) {
1076:       while (aj[j] >= cranges[proc + 1]) proc++;
1077:       col = aj[j] - cranges[proc] + Jranges[proc];
1078:       PetscCall(MatPreallocateSet(row, 1, &col, dnz, onz));
1079:     }
1080:     PetscCall(MatRestoreRow(tao->hessian, i + rjstart, &nc, &aj, NULL));

1082:     if (pdipm->ng) {
1083:       /* Insert grad g' */
1084:       PetscCall(MatGetRow(pdipm->jac_equality_trans, i + rjstart, &nc, &aj, NULL));
1085:       PetscCall(MatGetOwnershipRanges(tao->jacobian_equality, &ranges));
1086:       proc = 0;
1087:       for (j = 0; j < nc; j++) {
1088:         /* find row ownership of */
1089:         while (aj[j] >= ranges[proc + 1]) proc++;
1090:         nx_all = rranges[proc + 1] - rranges[proc];
1091:         col    = aj[j] - ranges[proc] + Jranges[proc] + nx_all;
1092:         PetscCall(MatPreallocateSet(row, 1, &col, dnz, onz));
1093:       }
1094:       PetscCall(MatRestoreRow(pdipm->jac_equality_trans, i + rjstart, &nc, &aj, NULL));
1095:     }

1097:     /* Insert Jce_xfixed^T' */
1098:     if (pdipm->nxfixed) {
1099:       PetscCall(MatGetRow(Jce_xfixed_trans, i + rjstart, &nc, &aj, NULL));
1100:       PetscCall(MatGetOwnershipRanges(pdipm->Jce_xfixed, &ranges));
1101:       proc = 0;
1102:       for (j = 0; j < nc; j++) {
1103:         /* find row ownership of */
1104:         while (aj[j] >= ranges[proc + 1]) proc++;
1105:         nx_all = rranges[proc + 1] - rranges[proc];
1106:         col    = aj[j] - ranges[proc] + Jranges[proc] + nx_all + ng_all[proc];
1107:         PetscCall(MatPreallocateSet(row, 1, &col, dnz, onz));
1108:       }
1109:       PetscCall(MatRestoreRow(Jce_xfixed_trans, i + rjstart, &nc, &aj, NULL));
1110:     }

1112:     if (pdipm->nh) {
1113:       /* Insert -grad h' */
1114:       PetscCall(MatGetRow(pdipm->jac_inequality_trans, i + rjstart, &nc, &aj, NULL));
1115:       PetscCall(MatGetOwnershipRanges(tao->jacobian_inequality, &ranges));
1116:       proc = 0;
1117:       for (j = 0; j < nc; j++) {
1118:         /* find row ownership of */
1119:         while (aj[j] >= ranges[proc + 1]) proc++;
1120:         nx_all = rranges[proc + 1] - rranges[proc];
1121:         col    = aj[j] - ranges[proc] + Jranges[proc] + nx_all + nce_all[proc];
1122:         PetscCall(MatPreallocateSet(row, 1, &col, dnz, onz));
1123:       }
1124:       PetscCall(MatRestoreRow(pdipm->jac_inequality_trans, i + rjstart, &nc, &aj, NULL));
1125:     }

1127:     /* Insert Jci_xb^T' */
1128:     PetscCall(MatGetRow(Jci_xb_trans, i + rjstart, &nc, &aj, NULL));
1129:     PetscCall(MatGetOwnershipRanges(pdipm->Jci_xb, &ranges));
1130:     proc = 0;
1131:     for (j = 0; j < nc; j++) {
1132:       /* find row ownership of */
1133:       while (aj[j] >= ranges[proc + 1]) proc++;
1134:       nx_all = rranges[proc + 1] - rranges[proc];
1135:       col    = aj[j] - ranges[proc] + Jranges[proc] + nx_all + nce_all[proc] + nh_all[proc];
1136:       PetscCall(MatPreallocateSet(row, 1, &col, dnz, onz));
1137:     }
1138:     PetscCall(MatRestoreRow(Jci_xb_trans, i + rjstart, &nc, &aj, NULL));
1139:   }

1141:   /* 2nd Row block of KKT matrix: [grad Ce, deltac*I, 0, 0] */
1142:   if (pdipm->Ng) {
1143:     PetscCall(MatGetOwnershipRange(tao->jacobian_equality, &rjstart, NULL));
1144:     for (i = 0; i < pdipm->ng; i++) {
1145:       row = rstart + pdipm->off_lambdae + i;

1147:       PetscCall(MatGetRow(tao->jacobian_equality, i + rjstart, &nc, &aj, NULL));
1148:       proc = 0;
1149:       for (j = 0; j < nc; j++) {
1150:         while (aj[j] >= cranges[proc + 1]) proc++;
1151:         col = aj[j] - cranges[proc] + Jranges[proc];
1152:         PetscCall(MatPreallocateSet(row, 1, &col, dnz, onz)); /* grad g */
1153:       }
1154:       PetscCall(MatRestoreRow(tao->jacobian_equality, i + rjstart, &nc, &aj, NULL));
1155:     }
1156:   }
1157:   /* Jce_xfixed */
1158:   if (pdipm->Nxfixed) {
1159:     PetscCall(MatGetOwnershipRange(pdipm->Jce_xfixed, &Jcrstart, NULL));
1160:     for (i = 0; i < (pdipm->nce - pdipm->ng); i++) {
1161:       row = rstart + pdipm->off_lambdae + pdipm->ng + i;

1163:       PetscCall(MatGetRow(pdipm->Jce_xfixed, i + Jcrstart, &nc, &cols, NULL));
1164:       PetscCheck(nc == 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "nc != 1");

1166:       proc = 0;
1167:       j    = 0;
1168:       while (cols[j] >= cranges[proc + 1]) proc++;
1169:       col = cols[j] - cranges[proc] + Jranges[proc];
1170:       PetscCall(MatPreallocateSet(row, 1, &col, dnz, onz));
1171:       PetscCall(MatRestoreRow(pdipm->Jce_xfixed, i + Jcrstart, &nc, &cols, NULL));
1172:     }
1173:   }

1175:   /* 3rd Row block of KKT matrix: [ gradCi, 0, deltac*I, -I] */
1176:   if (pdipm->Nh) {
1177:     PetscCall(MatGetOwnershipRange(tao->jacobian_inequality, &rjstart, NULL));
1178:     for (i = 0; i < pdipm->nh; i++) {
1179:       row = rstart + pdipm->off_lambdai + i;

1181:       PetscCall(MatGetRow(tao->jacobian_inequality, i + rjstart, &nc, &aj, NULL));
1182:       proc = 0;
1183:       for (j = 0; j < nc; j++) {
1184:         while (aj[j] >= cranges[proc + 1]) proc++;
1185:         col = aj[j] - cranges[proc] + Jranges[proc];
1186:         PetscCall(MatPreallocateSet(row, 1, &col, dnz, onz)); /* grad h */
1187:       }
1188:       PetscCall(MatRestoreRow(tao->jacobian_inequality, i + rjstart, &nc, &aj, NULL));
1189:     }
1190:     /* I */
1191:     for (i = 0; i < pdipm->nh; i++) {
1192:       row = rstart + pdipm->off_lambdai + i;
1193:       col = rstart + pdipm->off_z + i;
1194:       PetscCall(MatPreallocateSet(row, 1, &col, dnz, onz));
1195:     }
1196:   }

1198:   /* Jci_xb */
1199:   PetscCall(MatGetOwnershipRange(pdipm->Jci_xb, &Jcrstart, NULL));
1200:   for (i = 0; i < (pdipm->nci - pdipm->nh); i++) {
1201:     row = rstart + pdipm->off_lambdai + pdipm->nh + i;

1203:     PetscCall(MatGetRow(pdipm->Jci_xb, i + Jcrstart, &nc, &cols, NULL));
1204:     PetscCheck(nc == 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "nc != 1");
1205:     proc = 0;
1206:     for (j = 0; j < nc; j++) {
1207:       while (cols[j] >= cranges[proc + 1]) proc++;
1208:       col = cols[j] - cranges[proc] + Jranges[proc];
1209:       PetscCall(MatPreallocateSet(row, 1, &col, dnz, onz));
1210:     }
1211:     PetscCall(MatRestoreRow(pdipm->Jci_xb, i + Jcrstart, &nc, &cols, NULL));
1212:     /* I */
1213:     col = rstart + pdipm->off_z + pdipm->nh + i;
1214:     PetscCall(MatPreallocateSet(row, 1, &col, dnz, onz));
1215:   }

1217:   /* 4-th Row block of KKT matrix: Z and Ci */
1218:   for (i = 0; i < pdipm->nci; i++) {
1219:     row      = rstart + pdipm->off_z + i;
1220:     cols1[0] = rstart + pdipm->off_lambdai + i;
1221:     cols1[1] = row;
1222:     PetscCall(MatPreallocateSet(row, 2, cols1, dnz, onz));
1223:   }

1225:   /* diagonal entry */
1226:   for (i = 0; i < pdipm->n; i++) dnz[i]++; /* diagonal entry */

1228:   /* Create KKT matrix */
1229:   PetscCall(MatCreate(comm, &J));
1230:   PetscCall(MatSetSizes(J, pdipm->n, pdipm->n, PETSC_DECIDE, PETSC_DECIDE));
1231:   PetscCall(MatSetFromOptions(J));
1232:   PetscCall(MatSeqAIJSetPreallocation(J, 0, dnz));
1233:   PetscCall(MatMPIAIJSetPreallocation(J, 0, dnz, 0, onz));
1234:   MatPreallocateEnd(dnz, onz);
1235:   pdipm->K = J;

1237:   /* (8) Insert constant entries to  K */
1238:   /* Set 0.0 to diagonal of K, so that the solver does not complain *about missing diagonal value */
1239:   PetscCall(MatGetOwnershipRange(J, &rstart, &rend));
1240:   for (i = rstart; i < rend; i++) PetscCall(MatSetValue(J, i, i, 0.0, INSERT_VALUES));
1241:   /* In case Wxx has no diagonal entries preset set diagonal to deltaw given */
1242:   if (pdipm->kkt_pd) {
1243:     for (i = 0; i < pdipm->nh; i++) {
1244:       row = rstart + i;
1245:       PetscCall(MatSetValue(J, row, row, pdipm->deltaw, INSERT_VALUES));
1246:     }
1247:   }

1249:   /* Row block of K: [ grad Ce, 0, 0, 0] */
1250:   if (pdipm->Nxfixed) {
1251:     PetscCall(MatGetOwnershipRange(pdipm->Jce_xfixed, &Jcrstart, NULL));
1252:     for (i = 0; i < (pdipm->nce - pdipm->ng); i++) {
1253:       row = rstart + pdipm->off_lambdae + pdipm->ng + i;

1255:       PetscCall(MatGetRow(pdipm->Jce_xfixed, i + Jcrstart, &nc, &cols, &aa));
1256:       proc = 0;
1257:       for (j = 0; j < nc; j++) {
1258:         while (cols[j] >= cranges[proc + 1]) proc++;
1259:         col = cols[j] - cranges[proc] + Jranges[proc];
1260:         PetscCall(MatSetValue(J, row, col, aa[j], INSERT_VALUES)); /* grad Ce */
1261:         PetscCall(MatSetValue(J, col, row, aa[j], INSERT_VALUES)); /* grad Ce' */
1262:       }
1263:       PetscCall(MatRestoreRow(pdipm->Jce_xfixed, i + Jcrstart, &nc, &cols, &aa));
1264:     }
1265:   }

1267:   /* Row block of K: [ -grad Ci, 0, 0, I] */
1268:   PetscCall(MatGetOwnershipRange(pdipm->Jci_xb, &Jcrstart, NULL));
1269:   for (i = 0; i < pdipm->nci - pdipm->nh; i++) {
1270:     row = rstart + pdipm->off_lambdai + pdipm->nh + i;

1272:     PetscCall(MatGetRow(pdipm->Jci_xb, i + Jcrstart, &nc, &cols, &aa));
1273:     proc = 0;
1274:     for (j = 0; j < nc; j++) {
1275:       while (cols[j] >= cranges[proc + 1]) proc++;
1276:       col = cols[j] - cranges[proc] + Jranges[proc];
1277:       PetscCall(MatSetValue(J, col, row, -aa[j], INSERT_VALUES));
1278:       PetscCall(MatSetValue(J, row, col, -aa[j], INSERT_VALUES));
1279:     }
1280:     PetscCall(MatRestoreRow(pdipm->Jci_xb, i + Jcrstart, &nc, &cols, &aa));

1282:     col = rstart + pdipm->off_z + pdipm->nh + i;
1283:     PetscCall(MatSetValue(J, row, col, 1, INSERT_VALUES));
1284:   }

1286:   for (i = 0; i < pdipm->nh; i++) {
1287:     row = rstart + pdipm->off_lambdai + i;
1288:     col = rstart + pdipm->off_z + i;
1289:     PetscCall(MatSetValue(J, row, col, 1, INSERT_VALUES));
1290:   }

1292:   /* Row block of K: [ 0, 0, I, ...] */
1293:   for (i = 0; i < pdipm->nci; i++) {
1294:     row = rstart + pdipm->off_z + i;
1295:     col = rstart + pdipm->off_lambdai + i;
1296:     PetscCall(MatSetValue(J, row, col, 1, INSERT_VALUES));
1297:   }

1299:   if (pdipm->Nxfixed) PetscCall(MatDestroy(&Jce_xfixed_trans));
1300:   PetscCall(MatDestroy(&Jci_xb_trans));
1301:   PetscCall(PetscFree3(ng_all, nh_all, Jranges));

1303:   /* (9) Set up nonlinear solver SNES */
1304:   PetscCall(SNESSetFunction(pdipm->snes, NULL, TaoSNESFunction_PDIPM, (void *)tao));
1305:   PetscCall(SNESSetJacobian(pdipm->snes, J, J, TaoSNESJacobian_PDIPM, (void *)tao));

1307:   if (pdipm->solve_reduced_kkt) {
1308:     PC pc;
1309:     PetscCall(KSPGetPC(tao->ksp, &pc));
1310:     PetscCall(PCSetType(pc, PCFIELDSPLIT));
1311:     PetscCall(PCFieldSplitSetType(pc, PC_COMPOSITE_SCHUR));
1312:     PetscCall(PCFieldSplitSetIS(pc, "2", pdipm->is2));
1313:     PetscCall(PCFieldSplitSetIS(pc, "1", pdipm->is1));
1314:   }
1315:   PetscCall(SNESSetFromOptions(pdipm->snes));

1317:   /* (10) Setup PCPostSetUp() for pdipm->solve_symmetric_kkt */
1318:   if (pdipm->solve_symmetric_kkt) {
1319:     KSP       ksp;
1320:     PC        pc;
1321:     PetscBool isCHOL;

1323:     PetscCall(SNESGetKSP(pdipm->snes, &ksp));
1324:     PetscCall(KSPGetPC(ksp, &pc));
1325:     PetscCall(PCSetPostSetUp(pc, PCPostSetUp_PDIPM));

1327:     PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCCHOLESKY, &isCHOL));
1328:     if (isCHOL) {
1329:       Mat Factor;

1331:       PetscCheck(PetscDefined(HAVE_MUMPS), PetscObjectComm((PetscObject)tao), PETSC_ERR_SUP, "Requires external package MUMPS");
1332:       PetscCall(PCFactorGetMatrix(pc, &Factor));
1333:       PetscCall(MatMumpsSetIcntl(Factor, 24, 1));               /* detection of null pivot rows */
1334:       if (size > 1) PetscCall(MatMumpsSetIcntl(Factor, 13, 1)); /* parallelism of the root node (enable ScaLAPACK) and its splitting */
1335:     }
1336:   }
1337:   PetscFunctionReturn(PETSC_SUCCESS);
1338: }

1340: static PetscErrorCode TaoDestroy_PDIPM(Tao tao)
1341: {
1342:   TAO_PDIPM *pdipm = (TAO_PDIPM *)tao->data;

1344:   PetscFunctionBegin;
1345:   /* Freeing Vectors assocaiated with KKT (X) */
1346:   PetscCall(VecDestroy(&pdipm->x));       /* Solution x */
1347:   PetscCall(VecDestroy(&pdipm->lambdae)); /* Equality constraints lagrangian multiplier*/
1348:   PetscCall(VecDestroy(&pdipm->lambdai)); /* Inequality constraints lagrangian multiplier*/
1349:   PetscCall(VecDestroy(&pdipm->z));       /* Slack variables */
1350:   PetscCall(VecDestroy(&pdipm->X));       /* Big KKT system vector [x; lambdae; lambdai; z] */

1352:   /* work vectors */
1353:   PetscCall(VecDestroy(&pdipm->lambdae_xfixed));
1354:   PetscCall(VecDestroy(&pdipm->lambdai_xb));

1356:   /* Legrangian equality and inequality Vec */
1357:   PetscCall(VecDestroy(&pdipm->ce)); /* Vec of equality constraints */
1358:   PetscCall(VecDestroy(&pdipm->ci)); /* Vec of inequality constraints */

1360:   /* Matrices */
1361:   PetscCall(MatDestroy(&pdipm->Jce_xfixed));
1362:   PetscCall(MatDestroy(&pdipm->Jci_xb)); /* Jacobian of inequality constraints Jci = [tao->jacobian_inequality ; J(nxub); J(nxlb); J(nxbx)] */
1363:   PetscCall(MatDestroy(&pdipm->K));

1365:   /* Index Sets */
1366:   if (pdipm->Nxub) PetscCall(ISDestroy(&pdipm->isxub)); /* Finite upper bound only -inf < x < ub */

1368:   if (pdipm->Nxlb) PetscCall(ISDestroy(&pdipm->isxlb)); /* Finite lower bound only  lb <= x < inf */

1370:   if (pdipm->Nxfixed) PetscCall(ISDestroy(&pdipm->isxfixed)); /* Fixed variables         lb =  x = ub */

1372:   if (pdipm->Nxbox) PetscCall(ISDestroy(&pdipm->isxbox)); /* Boxed variables         lb <= x <= ub */

1374:   if (pdipm->Nxfree) PetscCall(ISDestroy(&pdipm->isxfree)); /* Free variables        -inf <= x <= inf */

1376:   if (pdipm->solve_reduced_kkt) {
1377:     PetscCall(ISDestroy(&pdipm->is1));
1378:     PetscCall(ISDestroy(&pdipm->is2));
1379:   }

1381:   /* SNES */
1382:   PetscCall(SNESDestroy(&pdipm->snes)); /* Nonlinear solver */
1383:   PetscCall(PetscFree(pdipm->nce_all));
1384:   PetscCall(MatDestroy(&pdipm->jac_equality_trans));
1385:   PetscCall(MatDestroy(&pdipm->jac_inequality_trans));

1387:   /* Destroy pdipm */
1388:   PetscCall(PetscFree(tao->data)); /* Holding locations of pdipm */

1390:   /* Destroy Dual */
1391:   PetscCall(VecDestroy(&tao->DE)); /* equality dual */
1392:   PetscCall(VecDestroy(&tao->DI)); /* dinequality dual */
1393:   PetscFunctionReturn(PETSC_SUCCESS);
1394: }

1396: static PetscErrorCode TaoSetFromOptions_PDIPM(Tao tao, PetscOptionItems PetscOptionsObject)
1397: {
1398:   TAO_PDIPM *pdipm = (TAO_PDIPM *)tao->data;

1400:   PetscFunctionBegin;
1401:   PetscOptionsHeadBegin(PetscOptionsObject, "PDIPM method for constrained optimization");
1402:   PetscCall(PetscOptionsReal("-tao_pdipm_push_init_slack", "parameter to push initial slack variables away from bounds", NULL, pdipm->push_init_slack, &pdipm->push_init_slack, NULL));
1403:   PetscCall(PetscOptionsReal("-tao_pdipm_push_init_lambdai", "parameter to push initial (inequality) dual variables away from bounds", NULL, pdipm->push_init_lambdai, &pdipm->push_init_lambdai, NULL));
1404:   PetscCall(PetscOptionsBool("-tao_pdipm_solve_reduced_kkt", "Solve reduced KKT system using Schur-complement", NULL, pdipm->solve_reduced_kkt, &pdipm->solve_reduced_kkt, NULL));
1405:   PetscCall(PetscOptionsReal("-tao_pdipm_mu_update_factor", "Update scalar for barrier parameter (mu) update", NULL, pdipm->mu_update_factor, &pdipm->mu_update_factor, NULL));
1406:   PetscCall(PetscOptionsBool("-tao_pdipm_symmetric_kkt", "Solve non reduced symmetric KKT system", NULL, pdipm->solve_symmetric_kkt, &pdipm->solve_symmetric_kkt, NULL));
1407:   PetscCall(PetscOptionsBool("-tao_pdipm_kkt_shift_pd", "Add shifts to make KKT matrix positive definite", NULL, pdipm->kkt_pd, &pdipm->kkt_pd, NULL));
1408:   PetscOptionsHeadEnd();
1409:   PetscFunctionReturn(PETSC_SUCCESS);
1410: }

1412: /*MC
1413:   TAOPDIPM - Barrier-based primal-dual interior point algorithm for generally constrained optimization.

1415:   Options Database Keys:
1416: +   -tao_pdipm_push_init_lambdai - parameter to push initial dual variables away from bounds (> 0)
1417: .   -tao_pdipm_push_init_slack   - parameter to push initial slack variables away from bounds (> 0)
1418: .   -tao_pdipm_mu_update_factor  - update scalar for barrier parameter (mu) update (> 0)
1419: .   -tao_pdipm_symmetric_kkt     - Solve non-reduced symmetric KKT system
1420: -   -tao_pdipm_kkt_shift_pd      - Add shifts to make KKT matrix positive definite

1422:   Level: beginner

1424:   Note:
1425:   Variable bounds are required; set them with `TaoSetVariableBounds()` or `TaoSetVariableBoundsRoutine()`, using
1426:   `PETSC_NINFINITY` and `PETSC_INFINITY` entries for unbounded variables.

1428: .seealso: `TAOPDIPM`, `Tao`, `TaoType`, `TaoSetVariableBounds()`, `TaoSetVariableBoundsRoutine()`
1429: M*/

1431: PETSC_EXTERN PetscErrorCode TaoCreate_PDIPM(Tao tao)
1432: {
1433:   TAO_PDIPM *pdipm;
1434:   PC         pc;

1436:   PetscFunctionBegin;
1437:   tao->ops->setup            = TaoSetup_PDIPM;
1438:   tao->ops->solve            = TaoSolve_PDIPM;
1439:   tao->ops->setfromoptions   = TaoSetFromOptions_PDIPM;
1440:   tao->ops->view             = TaoView_PDIPM;
1441:   tao->ops->destroy          = TaoDestroy_PDIPM;
1442:   tao->uses_gradient         = PETSC_TRUE;
1443:   tao->uses_hessian_matrices = PETSC_TRUE;

1445:   PetscCall(PetscNew(&pdipm));
1446:   tao->data = (void *)pdipm;

1448:   pdipm->nx = pdipm->Nx = 0;
1449:   pdipm->nxfixed = pdipm->Nxfixed = 0;
1450:   pdipm->nxlb = pdipm->Nxlb = 0;
1451:   pdipm->nxub = pdipm->Nxub = 0;
1452:   pdipm->nxbox = pdipm->Nxbox = 0;
1453:   pdipm->nxfree = pdipm->Nxfree = 0;

1455:   pdipm->ng = pdipm->Ng = pdipm->nce = pdipm->Nce = 0;
1456:   pdipm->nh = pdipm->Nh = pdipm->nci = pdipm->Nci = 0;
1457:   pdipm->n = pdipm->N     = 0;
1458:   pdipm->mu               = 1.0;
1459:   pdipm->mu_update_factor = 0.1;

1461:   pdipm->deltaw     = 0.0;
1462:   pdipm->lastdeltaw = 3 * 1.e-4;
1463:   pdipm->deltac     = 0.0;
1464:   pdipm->kkt_pd     = PETSC_FALSE;

1466:   pdipm->push_init_slack     = 1.0;
1467:   pdipm->push_init_lambdai   = 1.0;
1468:   pdipm->solve_reduced_kkt   = PETSC_FALSE;
1469:   pdipm->solve_symmetric_kkt = PETSC_TRUE;

1471:   /* Override default settings (unless already changed) */
1472:   PetscObjectParameterSetDefault(tao, max_it, 200);
1473:   PetscObjectParameterSetDefault(tao, max_funcs, 500);

1475:   PetscCall(SNESCreate(((PetscObject)tao)->comm, &pdipm->snes));
1476:   PetscCall(SNESSetOptionsPrefix(pdipm->snes, tao->hdr.prefix));
1477:   PetscCall(SNESGetKSP(pdipm->snes, &tao->ksp));
1478:   PetscCall(PetscObjectReference((PetscObject)tao->ksp));
1479:   PetscCall(KSPGetPC(tao->ksp, &pc));
1480:   PetscCall(PCSetApplicationContext(pc, (void *)tao));
1481:   PetscFunctionReturn(PETSC_SUCCESS);
1482: }