Actual source code: pounders.c

  1: #include <../src/tao/leastsquares/impls/pounders/pounders.h>

  3: static PetscErrorCode pounders_h(Tao subtao, Vec v, Mat H, Mat Hpre, PetscCtx ctx)
  4: {
  5:   PetscFunctionBegin;
  6:   PetscFunctionReturn(PETSC_SUCCESS);
  7: }

  9: static PetscErrorCode pounders_fg(Tao subtao, Vec x, PetscReal *f, Vec g, PetscCtx ctx)
 10: {
 11:   TAO_POUNDERS *mfqP = (TAO_POUNDERS *)ctx;
 12:   PetscReal     d1, d2;

 14:   PetscFunctionBegin;
 15:   /* g = A*x  (add b later)*/
 16:   PetscCall(MatMult(mfqP->subH, x, g));

 18:   /* f = 1/2 * x'*(Ax) + b'*x  */
 19:   PetscCall(VecDot(x, g, &d1));
 20:   PetscCall(VecDot(mfqP->subb, x, &d2));
 21:   *f = 0.5 * d1 + d2;

 23:   /* now  g = g + b */
 24:   PetscCall(VecAXPY(g, 1.0, mfqP->subb));
 25:   PetscFunctionReturn(PETSC_SUCCESS);
 26: }

 28: static PetscErrorCode pounders_feval(Tao tao, Vec x, Vec F, PetscReal *fsum)
 29: {
 30:   TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;
 31:   PetscInt      i, row, col;
 32:   PetscReal     fr, fc;

 34:   PetscFunctionBegin;
 35:   PetscCall(TaoComputeResidual(tao, x, F));
 36:   if (tao->res_weights_v) {
 37:     PetscCall(VecPointwiseMult(mfqP->workfvec, tao->res_weights_v, F));
 38:     PetscCall(VecDot(mfqP->workfvec, mfqP->workfvec, fsum));
 39:   } else if (tao->res_weights_w) {
 40:     *fsum = 0;
 41:     for (i = 0; i < tao->res_weights_n; i++) {
 42:       row = tao->res_weights_rows[i];
 43:       col = tao->res_weights_cols[i];
 44:       PetscCall(VecGetValues(F, 1, &row, &fr));
 45:       PetscCall(VecGetValues(F, 1, &col, &fc));
 46:       *fsum += tao->res_weights_w[i] * fc * fr;
 47:     }
 48:   } else {
 49:     PetscCall(VecDot(F, F, fsum));
 50:   }
 51:   PetscCall(PetscInfo(tao, "Least-squares residual norm: %20.19e\n", (double)*fsum));
 52:   PetscCheck(!PetscIsInfOrNanReal(*fsum), PETSC_COMM_SELF, PETSC_ERR_USER, "User provided compute function generated infinity or NaN");
 53:   PetscFunctionReturn(PETSC_SUCCESS);
 54: }

 56: static PetscErrorCode gqtwrap(Tao tao, PetscReal *gnorm, PetscReal *qmin)
 57: {
 58:   PetscReal     atol = PetscDefined(USE_REAL_SINGLE) ? 1.0e-5 : 1.0e-10;
 59:   PetscInt      info, its;
 60:   TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;

 62:   PetscFunctionBegin;
 63:   if (!mfqP->usegqt) {
 64:     PetscReal maxval;
 65:     PetscInt  i, j;

 67:     PetscCall(VecSetValues(mfqP->subb, mfqP->n, mfqP->indices, mfqP->Gres, INSERT_VALUES));
 68:     PetscCall(VecAssemblyBegin(mfqP->subb));
 69:     PetscCall(VecAssemblyEnd(mfqP->subb));

 71:     PetscCall(VecSet(mfqP->subx, 0.0));

 73:     PetscCall(VecSet(mfqP->subndel, -1.0));
 74:     PetscCall(VecSet(mfqP->subpdel, +1.0));

 76:     /* Complete the lower triangle of the Hessian matrix */
 77:     for (i = 0; i < mfqP->n; i++) {
 78:       for (j = i + 1; j < mfqP->n; j++) mfqP->Hres[j + mfqP->n * i] = mfqP->Hres[mfqP->n * j + i];
 79:     }
 80:     PetscCall(MatSetValues(mfqP->subH, mfqP->n, mfqP->indices, mfqP->n, mfqP->indices, mfqP->Hres, INSERT_VALUES));
 81:     PetscCall(MatAssemblyBegin(mfqP->subH, MAT_FINAL_ASSEMBLY));
 82:     PetscCall(MatAssemblyEnd(mfqP->subH, MAT_FINAL_ASSEMBLY));

 84:     PetscCall(TaoResetStatistics(mfqP->subtao));
 85:     /* PetscCall(TaoSetTolerances(mfqP->subtao,*gnorm,*gnorm,PETSC_CURRENT)); */
 86:     /* enforce bound constraints -- experimental */
 87:     if (tao->XU && tao->XL) {
 88:       PetscCall(VecCopy(tao->XU, mfqP->subxu));
 89:       PetscCall(VecAXPY(mfqP->subxu, -1.0, tao->solution));
 90:       PetscCall(VecScale(mfqP->subxu, 1.0 / mfqP->delta));
 91:       PetscCall(VecCopy(tao->XL, mfqP->subxl));
 92:       PetscCall(VecAXPY(mfqP->subxl, -1.0, tao->solution));
 93:       PetscCall(VecScale(mfqP->subxl, 1.0 / mfqP->delta));

 95:       PetscCall(VecPointwiseMin(mfqP->subxu, mfqP->subxu, mfqP->subpdel));
 96:       PetscCall(VecPointwiseMax(mfqP->subxl, mfqP->subxl, mfqP->subndel));
 97:     } else {
 98:       PetscCall(VecCopy(mfqP->subpdel, mfqP->subxu));
 99:       PetscCall(VecCopy(mfqP->subndel, mfqP->subxl));
100:     }
101:     /* Make sure xu > xl */
102:     PetscCall(VecCopy(mfqP->subxl, mfqP->subpdel));
103:     PetscCall(VecAXPY(mfqP->subpdel, -1.0, mfqP->subxu));
104:     PetscCall(VecMax(mfqP->subpdel, NULL, &maxval));
105:     PetscCheck(maxval <= 1e-10, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "upper bound < lower bound in subproblem");
106:     /* Make sure xu > tao->solution > xl */
107:     PetscCall(VecCopy(mfqP->subxl, mfqP->subpdel));
108:     PetscCall(VecAXPY(mfqP->subpdel, -1.0, mfqP->subx));
109:     PetscCall(VecMax(mfqP->subpdel, NULL, &maxval));
110:     PetscCheck(maxval <= 1e-10, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "initial guess < lower bound in subproblem");

112:     PetscCall(VecCopy(mfqP->subx, mfqP->subpdel));
113:     PetscCall(VecAXPY(mfqP->subpdel, -1.0, mfqP->subxu));
114:     PetscCall(VecMax(mfqP->subpdel, NULL, &maxval));
115:     PetscCheck(maxval <= 1e-10, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "initial guess > upper bound in subproblem");

117:     PetscCall(TaoSolve(mfqP->subtao));
118:     PetscCall(TaoGetSolutionStatus(mfqP->subtao, NULL, qmin, NULL, NULL, NULL, NULL));

120:     /* test bounds post-solution*/
121:     PetscCall(VecCopy(mfqP->subxl, mfqP->subpdel));
122:     PetscCall(VecAXPY(mfqP->subpdel, -1.0, mfqP->subx));
123:     PetscCall(VecMax(mfqP->subpdel, NULL, &maxval));
124:     if (maxval > 1e-5) {
125:       PetscCall(PetscInfo(tao, "subproblem solution < lower bound\n"));
126:       tao->reason = TAO_DIVERGED_TR_REDUCTION;
127:     }

129:     PetscCall(VecCopy(mfqP->subx, mfqP->subpdel));
130:     PetscCall(VecAXPY(mfqP->subpdel, -1.0, mfqP->subxu));
131:     PetscCall(VecMax(mfqP->subpdel, NULL, &maxval));
132:     if (maxval > 1e-5) {
133:       PetscCall(PetscInfo(tao, "subproblem solution > upper bound\n"));
134:       tao->reason = TAO_DIVERGED_TR_REDUCTION;
135:     }
136:   } else {
137:     PetscCall(gqt(mfqP->n, mfqP->Hres, mfqP->n, mfqP->Gres, 1.0, mfqP->gqt_rtol, atol, mfqP->gqt_maxits, gnorm, qmin, mfqP->Xsubproblem, &info, &its, mfqP->work, mfqP->work2, mfqP->work3));
138:   }
139:   *qmin *= -1;
140:   PetscFunctionReturn(PETSC_SUCCESS);
141: }

143: static PetscErrorCode pounders_update_res(Tao tao)
144: {
145:   TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;
146:   PetscInt      i, row, col;
147:   PetscBLASInt  blasn, blasn2, blasm, ione = 1;
148:   PetscReal     zero = 0.0, one = 1.0, wii, factor;

150:   PetscFunctionBegin;
151:   PetscCall(PetscBLASIntCast(mfqP->n, &blasn));
152:   PetscCall(PetscBLASIntCast(mfqP->m, &blasm));
153:   PetscCall(PetscBLASIntCast(mfqP->n * mfqP->n, &blasn2));
154:   for (i = 0; i < mfqP->n; i++) mfqP->Gres[i] = 0;
155:   for (i = 0; i < mfqP->n * mfqP->n; i++) mfqP->Hres[i] = 0;

157:   /* Compute Gres= sum_ij[wij * (cjgi + cigj)] */
158:   if (tao->res_weights_v) {
159:     /* Vector(diagonal) weights: gres = sum_i(wii*ci*gi) */
160:     for (i = 0; i < mfqP->m; i++) {
161:       PetscCall(VecGetValues(tao->res_weights_v, 1, &i, &factor));
162:       factor = factor * mfqP->C[i];
163:       PetscCallBLAS("BLASaxpy", BLASaxpy_(&blasn, &factor, &mfqP->Fdiff[blasn * i], &ione, mfqP->Gres, &ione));
164:     }

166:     /* compute Hres = sum_ij [wij * (*ci*Hj + cj*Hi + gi gj' + gj gi') ] */
167:     /* vector(diagonal weights) Hres = sum_i(wii*(ci*Hi + gi * gi')*/
168:     for (i = 0; i < mfqP->m; i++) {
169:       PetscCall(VecGetValues(tao->res_weights_v, 1, &i, &wii));
170:       if (tao->niter > 1) {
171:         factor = wii * mfqP->C[i];
172:         /* add wii * ci * Hi */
173:         PetscCallBLAS("BLASaxpy", BLASaxpy_(&blasn2, &factor, &mfqP->H[i], &blasm, mfqP->Hres, &ione));
174:       }
175:       /* add wii * gi * gi' */
176:       PetscCallBLAS("BLASgemm", BLASgemm_("N", "T", &blasn, &blasn, &ione, &wii, &mfqP->Fdiff[blasn * i], &blasn, &mfqP->Fdiff[blasn * i], &blasn, &one, mfqP->Hres, &blasn));
177:     }
178:   } else if (tao->res_weights_w) {
179:     /* General case: .5 * Gres= sum_ij[wij * (cjgi + cigj)] */
180:     for (i = 0; i < tao->res_weights_n; i++) {
181:       row = tao->res_weights_rows[i];
182:       col = tao->res_weights_cols[i];

184:       factor = tao->res_weights_w[i] * mfqP->C[col] / 2.0;
185:       PetscCallBLAS("BLASaxpy", BLASaxpy_(&blasn, &factor, &mfqP->Fdiff[blasn * row], &ione, mfqP->Gres, &ione));
186:       factor = tao->res_weights_w[i] * mfqP->C[row] / 2.0;
187:       PetscCallBLAS("BLASaxpy", BLASaxpy_(&blasn, &factor, &mfqP->Fdiff[blasn * col], &ione, mfqP->Gres, &ione));
188:     }

190:     /* compute Hres = sum_ij [wij * (*ci*Hj + cj*Hi + gi gj' + gj gi') ] */
191:     /* .5 * sum_ij [wij * (*ci*Hj + cj*Hi + gi gj' + gj gi') ] */
192:     for (i = 0; i < tao->res_weights_n; i++) {
193:       row    = tao->res_weights_rows[i];
194:       col    = tao->res_weights_cols[i];
195:       factor = tao->res_weights_w[i] / 2.0;
196:       /* add wij * gi gj' + wij * gj gi' */
197:       PetscCallBLAS("BLASgemm", BLASgemm_("N", "T", &blasn, &blasn, &ione, &factor, &mfqP->Fdiff[blasn * row], &blasn, &mfqP->Fdiff[blasn * col], &blasn, &one, mfqP->Hres, &blasn));
198:       PetscCallBLAS("BLASgemm", BLASgemm_("N", "T", &blasn, &blasn, &ione, &factor, &mfqP->Fdiff[blasn * col], &blasn, &mfqP->Fdiff[blasn * row], &blasn, &one, mfqP->Hres, &blasn));
199:     }
200:     if (tao->niter > 1) {
201:       for (i = 0; i < tao->res_weights_n; i++) {
202:         row = tao->res_weights_rows[i];
203:         col = tao->res_weights_cols[i];

205:         /* add  wij*cj*Hi */
206:         factor = tao->res_weights_w[i] * mfqP->C[col] / 2.0;
207:         PetscCallBLAS("BLASaxpy", BLASaxpy_(&blasn2, &factor, &mfqP->H[row], &blasm, mfqP->Hres, &ione));

209:         /* add wij*ci*Hj */
210:         factor = tao->res_weights_w[i] * mfqP->C[row] / 2.0;
211:         PetscCallBLAS("BLASaxpy", BLASaxpy_(&blasn2, &factor, &mfqP->H[col], &blasm, mfqP->Hres, &ione));
212:       }
213:     }
214:   } else {
215:     /* Default: Gres= sum_i[cigi] = G*c' */
216:     PetscCall(PetscInfo(tao, "Identity weights\n"));
217:     PetscCallBLAS("BLASgemv", BLASgemv_("N", &blasn, &blasm, &one, mfqP->Fdiff, &blasn, mfqP->C, &ione, &zero, mfqP->Gres, &ione));

219:     /* compute Hres = sum_ij [wij * (*ci*Hj + cj*Hi + gi gj' + gj gi') ] */
220:     /*  Hres = G*G' + 0.5 sum {F(xkin,i)*H(:,:,i)}  */
221:     PetscCallBLAS("BLASgemm", BLASgemm_("N", "T", &blasn, &blasn, &blasm, &one, mfqP->Fdiff, &blasn, mfqP->Fdiff, &blasn, &zero, mfqP->Hres, &blasn));

223:     /* sum(F(xkin,i)*H(:,:,i)) */
224:     if (tao->niter > 1) {
225:       for (i = 0; i < mfqP->m; i++) {
226:         factor = mfqP->C[i];
227:         PetscCallBLAS("BLASaxpy", BLASaxpy_(&blasn2, &factor, &mfqP->H[i], &blasm, mfqP->Hres, &ione));
228:       }
229:     }
230:   }
231:   PetscFunctionReturn(PETSC_SUCCESS);
232: }

234: static PetscErrorCode phi2eval(PetscReal *x, PetscInt n, PetscReal *phi)
235: {
236:   /* Phi = .5*[x(1)^2  sqrt(2)*x(1)*x(2) ... sqrt(2)*x(1)*x(n) ... x(2)^2 sqrt(2)*x(2)*x(3) .. x(n)^2] */
237:   PetscInt  i, j, k;
238:   PetscReal sqrt2 = PetscSqrtReal(2.0);

240:   PetscFunctionBegin;
241:   j = 0;
242:   for (i = 0; i < n; i++) {
243:     phi[j] = 0.5 * x[i] * x[i];
244:     j++;
245:     for (k = i + 1; k < n; k++) {
246:       phi[j] = x[i] * x[k] / sqrt2;
247:       j++;
248:     }
249:   }
250:   PetscFunctionReturn(PETSC_SUCCESS);
251: }

253: static PetscErrorCode getquadpounders(TAO_POUNDERS *mfqP)
254: {
255:   /* Computes the parameters of the quadratic Q(x) = c + g'*x + 0.5*x*G*x'
256:    that satisfies the interpolation conditions Q(X[:,j]) = f(j)
257:    for j=1,...,m and with a Hessian matrix of least Frobenius norm */

259:   /* NB --we are ignoring c */
260:   PetscInt     i, j, k, num, np = mfqP->nmodelpoints;
261:   PetscReal    one = 1.0, zero = 0.0, negone = -1.0;
262:   PetscBLASInt blasnpmax, blasnplus1, blasnp, blasint, blasint2;
263:   PetscBLASInt ione  = 1;
264:   PetscReal    sqrt2 = PetscSqrtReal(2.0);

266:   PetscFunctionBegin;
267:   PetscCall(PetscBLASIntCast(mfqP->npmax, &blasnpmax));
268:   PetscCall(PetscBLASIntCast(mfqP->n + 1, &blasnplus1));
269:   PetscCall(PetscBLASIntCast(np, &blasnp));
270:   PetscCall(PetscBLASIntCast(mfqP->n * (mfqP->n + 1) / 2, &blasint));
271:   PetscCall(PetscBLASIntCast(np - mfqP->n - 1, &blasint2));
272:   for (i = 0; i < mfqP->n * mfqP->m; i++) mfqP->Gdel[i] = 0;
273:   for (i = 0; i < mfqP->n * mfqP->n * mfqP->m; i++) mfqP->Hdel[i] = 0;

275:   /* factor M */
276:   PetscCallLAPACKInfo("LAPACKgetrf", LAPACKgetrf_(&blasnplus1, &blasnp, mfqP->M, &blasnplus1, mfqP->npmaxiwork, &info));

278:   if (np == mfqP->n + 1) {
279:     for (i = 0; i < mfqP->npmax - mfqP->n - 1; i++) mfqP->omega[i] = 0.0;
280:     for (i = 0; i < mfqP->n * (mfqP->n + 1) / 2; i++) mfqP->beta[i] = 0.0;
281:   } else {
282:     /* Let Ltmp = (L'*L) */
283:     PetscCallBLAS("BLASgemm", BLASgemm_("T", "N", &blasint2, &blasint2, &blasint, &one, &mfqP->L[(mfqP->n + 1) * blasint], &blasint, &mfqP->L[(mfqP->n + 1) * blasint], &blasint, &zero, mfqP->L_tmp, &blasint));

285:     /* factor Ltmp */
286:     PetscCallLAPACKInfo("LAPACKpotrf", LAPACKpotrf_("L", &blasint2, mfqP->L_tmp, &blasint, &info));
287:   }

289:   for (k = 0; k < mfqP->m; k++) {
290:     if (np != mfqP->n + 1) {
291:       /* Solve L'*L*Omega = Z' * RESk*/
292:       PetscCallBLAS("BLASgemv", BLASgemv_("T", &blasnp, &blasint2, &one, mfqP->Z, &blasnpmax, &mfqP->RES[mfqP->npmax * k], &ione, &zero, mfqP->omega, &ione));
293:       PetscCallLAPACKInfo("LAPACKpotrs", LAPACKpotrs_("L", &blasint2, &ione, mfqP->L_tmp, &blasint, mfqP->omega, &blasint2, &info));

295:       /* Beta = L*Omega */
296:       PetscCallBLAS("BLASgemv", BLASgemv_("N", &blasint, &blasint2, &one, &mfqP->L[(mfqP->n + 1) * blasint], &blasint, mfqP->omega, &ione, &zero, mfqP->beta, &ione));
297:     }

299:     /* solve M'*Alpha = RESk - N'*Beta */
300:     PetscCallBLAS("BLASgemv", BLASgemv_("T", &blasint, &blasnp, &negone, mfqP->N, &blasint, mfqP->beta, &ione, &one, &mfqP->RES[mfqP->npmax * k], &ione));
301:     PetscCallLAPACKInfo("LAPACKgetrs", LAPACKgetrs_("T", &blasnplus1, &ione, mfqP->M, &blasnplus1, mfqP->npmaxiwork, &mfqP->RES[mfqP->npmax * k], &blasnplus1, &info));

303:     /* Gdel(:,k) = Alpha(2:n+1) */
304:     for (i = 0; i < mfqP->n; i++) mfqP->Gdel[i + mfqP->n * k] = mfqP->RES[mfqP->npmax * k + i + 1];

306:     /* Set Hdels */
307:     num = 0;
308:     for (i = 0; i < mfqP->n; i++) {
309:       /* H[i,i,k] = Beta(num) */
310:       mfqP->Hdel[(i * mfqP->n + i) * mfqP->m + k] = mfqP->beta[num];
311:       num++;
312:       for (j = i + 1; j < mfqP->n; j++) {
313:         /* H[i,j,k] = H[j,i,k] = Beta(num)/sqrt(2) */
314:         mfqP->Hdel[(j * mfqP->n + i) * mfqP->m + k] = mfqP->beta[num] / sqrt2;
315:         mfqP->Hdel[(i * mfqP->n + j) * mfqP->m + k] = mfqP->beta[num] / sqrt2;
316:         num++;
317:       }
318:     }
319:   }
320:   PetscFunctionReturn(PETSC_SUCCESS);
321: }

323: static PetscErrorCode morepoints(TAO_POUNDERS *mfqP)
324: {
325:   /* Assumes mfqP->model_indices[0]  is minimum index
326:    Finishes adding points to mfqP->model_indices (up to npmax)
327:    Computes L,Z,M,N
328:    np is actual number of points in model (should equal npmax?) */
329:   PetscInt         point, i, j, offset;
330:   PetscInt         reject;
331:   PetscBLASInt     blasn, blasnpmax, blasnplus1, blasnmax, blasint, blasint2, blasnp, blasmaxmn;
332:   const PetscReal *x;
333:   PetscReal        normd;

335:   PetscFunctionBegin;
336:   PetscCall(PetscBLASIntCast(mfqP->npmax, &blasnpmax));
337:   PetscCall(PetscBLASIntCast(mfqP->n, &blasn));
338:   PetscCall(PetscBLASIntCast(mfqP->nmax, &blasnmax));
339:   PetscCall(PetscBLASIntCast(mfqP->n + 1, &blasnplus1));
340:   PetscCall(PetscBLASIntCast(mfqP->n, &blasnp));
341:   /* Initialize M,N */
342:   for (i = 0; i < mfqP->n + 1; i++) {
343:     PetscCall(VecGetArrayRead(mfqP->Xhist[mfqP->model_indices[i]], &x));
344:     mfqP->M[(mfqP->n + 1) * i] = 1.0;
345:     for (j = 0; j < mfqP->n; j++) mfqP->M[j + 1 + ((mfqP->n + 1) * i)] = (x[j] - mfqP->xmin[j]) / mfqP->delta;
346:     PetscCall(VecRestoreArrayRead(mfqP->Xhist[mfqP->model_indices[i]], &x));
347:     PetscCall(phi2eval(&mfqP->M[1 + ((mfqP->n + 1) * i)], mfqP->n, &mfqP->N[mfqP->n * (mfqP->n + 1) / 2 * i]));
348:   }

350:   /* Now we add points until we have npmax starting with the most recent ones */
351:   point              = mfqP->nHist - 1;
352:   mfqP->nmodelpoints = mfqP->n + 1;
353:   while (mfqP->nmodelpoints < mfqP->npmax && point >= 0) {
354:     /* Reject any points already in the model */
355:     reject = 0;
356:     for (j = 0; j < mfqP->n + 1; j++) {
357:       if (point == mfqP->model_indices[j]) {
358:         reject = 1;
359:         break;
360:       }
361:     }

363:     /* Reject if norm(d) >c2 */
364:     if (!reject) {
365:       PetscCall(VecCopy(mfqP->Xhist[point], mfqP->workxvec));
366:       PetscCall(VecAXPY(mfqP->workxvec, -1.0, mfqP->Xhist[mfqP->minindex]));
367:       PetscCall(VecNorm(mfqP->workxvec, NORM_2, &normd));
368:       normd /= mfqP->delta;
369:       if (normd > mfqP->c2) reject = 1;
370:     }
371:     if (reject) {
372:       point--;
373:       continue;
374:     }

376:     PetscCall(VecGetArrayRead(mfqP->Xhist[point], &x));
377:     mfqP->M[(mfqP->n + 1) * mfqP->nmodelpoints] = 1.0;
378:     for (j = 0; j < mfqP->n; j++) mfqP->M[j + 1 + ((mfqP->n + 1) * mfqP->nmodelpoints)] = (x[j] - mfqP->xmin[j]) / mfqP->delta;
379:     PetscCall(VecRestoreArrayRead(mfqP->Xhist[point], &x));
380:     PetscCall(phi2eval(&mfqP->M[1 + (mfqP->n + 1) * mfqP->nmodelpoints], mfqP->n, &mfqP->N[mfqP->n * (mfqP->n + 1) / 2 * (mfqP->nmodelpoints)]));

382:     /* Update QR factorization */
383:     /* Copy M' to Q_tmp */
384:     for (i = 0; i < mfqP->n + 1; i++) {
385:       for (j = 0; j < mfqP->npmax; j++) mfqP->Q_tmp[j + mfqP->npmax * i] = mfqP->M[i + (mfqP->n + 1) * j];
386:     }
387:     PetscCall(PetscBLASIntCast(mfqP->nmodelpoints + 1, &blasnp));
388:     /* Q_tmp,R = qr(M') */
389:     PetscCall(PetscBLASIntCast(PetscMax(mfqP->m, mfqP->n + 1), &blasmaxmn));
390:     PetscCallLAPACKInfo("LAPACKgeqrf", LAPACKgeqrf_(&blasnp, &blasnplus1, mfqP->Q_tmp, &blasnpmax, mfqP->tau_tmp, mfqP->mwork, &blasmaxmn, &info));

392:     /* Reject if min(svd(N*Q(:,n+2:np+1)) <= theta2 */
393:     /* L = N*Qtmp */
394:     PetscCall(PetscBLASIntCast(mfqP->n * (mfqP->n + 1) / 2, &blasint2));
395:     /* Copy N to L_tmp */
396:     for (i = 0; i < mfqP->n * (mfqP->n + 1) / 2 * mfqP->npmax; i++) mfqP->L_tmp[i] = mfqP->N[i];
397:     /* Copy L_save to L_tmp */

399:     /* L_tmp = N*Qtmp' */
400:     PetscCallLAPACKInfo("LAPACKormqr", LAPACKormqr_("R", "N", &blasint2, &blasnp, &blasnplus1, mfqP->Q_tmp, &blasnpmax, mfqP->tau_tmp, mfqP->L_tmp, &blasint2, mfqP->npmaxwork, &blasnmax, &info));

402:     /* Copy L_tmp to L_save */
403:     for (i = 0; i < mfqP->npmax * mfqP->n * (mfqP->n + 1) / 2; i++) mfqP->L_save[i] = mfqP->L_tmp[i];

405:     /* Get svd for L_tmp(:,n+2:np+1) (L_tmp is modified in process) */
406:     PetscCall(PetscBLASIntCast(mfqP->nmodelpoints - mfqP->n, &blasint));
407:     PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
408:     PetscCallLAPACKInfo("LAPACKgesvd", LAPACKgesvd_("N", "N", &blasint2, &blasint, &mfqP->L_tmp[(mfqP->n + 1) * blasint2], &blasint2, mfqP->beta, mfqP->work, &blasn, mfqP->work, &blasn, mfqP->npmaxwork, &blasnmax, &info));
409:     PetscCall(PetscFPTrapPop());

411:     if (mfqP->beta[PetscMin(blasint, blasint2) - 1] > mfqP->theta2) {
412:       /* accept point */
413:       mfqP->model_indices[mfqP->nmodelpoints] = point;
414:       /* Copy Q_tmp to Q */
415:       for (i = 0; i < mfqP->npmax * mfqP->npmax; i++) mfqP->Q[i] = mfqP->Q_tmp[i];
416:       for (i = 0; i < mfqP->npmax; i++) mfqP->tau[i] = mfqP->tau_tmp[i];
417:       mfqP->nmodelpoints++;
418:       PetscCall(PetscBLASIntCast(mfqP->nmodelpoints, &blasnp));

420:       /* Copy L_save to L */
421:       for (i = 0; i < mfqP->npmax * mfqP->n * (mfqP->n + 1) / 2; i++) mfqP->L[i] = mfqP->L_save[i];
422:     }
423:     point--;
424:   }

426:   PetscCall(PetscBLASIntCast(mfqP->nmodelpoints, &blasnp));
427:   /* Copy Q(:,n+2:np) to Z */
428:   /* First set Q_tmp to I */
429:   for (i = 0; i < mfqP->npmax * mfqP->npmax; i++) mfqP->Q_tmp[i] = 0.0;
430:   for (i = 0; i < mfqP->npmax; i++) mfqP->Q_tmp[i + mfqP->npmax * i] = 1.0;

432:   /* Q_tmp = I * Q */
433:   PetscCallLAPACKInfo("LAPACKormqr", LAPACKormqr_("R", "N", &blasnp, &blasnp, &blasnplus1, mfqP->Q, &blasnpmax, mfqP->tau, mfqP->Q_tmp, &blasnpmax, mfqP->npmaxwork, &blasnmax, &info));

435:   /* Copy Q_tmp(:,n+2:np) to Z) */
436:   offset = mfqP->npmax * (mfqP->n + 1);
437:   for (i = offset; i < mfqP->npmax * mfqP->npmax; i++) mfqP->Z[i - offset] = mfqP->Q_tmp[i];

439:   if (mfqP->nmodelpoints == mfqP->n + 1) {
440:     /* Set L to I_{n+1} */
441:     for (i = 0; i < mfqP->npmax * mfqP->n * (mfqP->n + 1) / 2; i++) mfqP->L[i] = 0.0;
442:     for (i = 0; i < mfqP->n; i++) mfqP->L[(mfqP->n * (mfqP->n + 1) / 2) * i + i] = 1.0;
443:   }
444:   PetscFunctionReturn(PETSC_SUCCESS);
445: }

447: /* Only call from modelimprove, addpoint() needs ->Q_tmp and ->work to be set */
448: static PetscErrorCode addpoint(Tao tao, TAO_POUNDERS *mfqP, PetscInt index)
449: {
450:   PetscFunctionBegin;
451:   /* Create new vector in history: X[newidx] = X[mfqP->index] + delta*X[index]*/
452:   PetscCall(VecDuplicate(mfqP->Xhist[0], &mfqP->Xhist[mfqP->nHist]));
453:   PetscCall(VecSetValues(mfqP->Xhist[mfqP->nHist], mfqP->n, mfqP->indices, &mfqP->Q_tmp[index * mfqP->npmax], INSERT_VALUES));
454:   PetscCall(VecAssemblyBegin(mfqP->Xhist[mfqP->nHist]));
455:   PetscCall(VecAssemblyEnd(mfqP->Xhist[mfqP->nHist]));
456:   PetscCall(VecAYPX(mfqP->Xhist[mfqP->nHist], mfqP->delta, mfqP->Xhist[mfqP->minindex]));

458:   /* Project into feasible region */
459:   if (tao->XU && tao->XL) PetscCall(VecMedian(mfqP->Xhist[mfqP->nHist], tao->XL, tao->XU, mfqP->Xhist[mfqP->nHist]));

461:   /* Compute value of new vector */
462:   PetscCall(VecDuplicate(mfqP->Fhist[0], &mfqP->Fhist[mfqP->nHist]));
463:   CHKMEMQ;
464:   PetscCall(pounders_feval(tao, mfqP->Xhist[mfqP->nHist], mfqP->Fhist[mfqP->nHist], &mfqP->Fres[mfqP->nHist]));

466:   /* Add new vector to model */
467:   mfqP->model_indices[mfqP->nmodelpoints] = mfqP->nHist;
468:   mfqP->nmodelpoints++;
469:   mfqP->nHist++;
470:   PetscFunctionReturn(PETSC_SUCCESS);
471: }

473: static PetscErrorCode modelimprove(Tao tao, TAO_POUNDERS *mfqP, PetscInt addallpoints)
474: {
475:   /* modeld = Q(:,np+1:n)' */
476:   PetscInt     i, j, minindex = 0;
477:   PetscReal    dp, half = 0.5, one = 1.0, minvalue = PETSC_INFINITY;
478:   PetscBLASInt blasn, blasnpmax, blask;
479:   PetscBLASInt blas1 = 1, blasnmax;

481:   PetscFunctionBegin;
482:   PetscCall(PetscBLASIntCast(mfqP->n, &blasn));
483:   PetscCall(PetscBLASIntCast(mfqP->npmax, &blasnpmax));
484:   PetscCall(PetscBLASIntCast(mfqP->nmodelpoints, &blask));
485:   PetscCall(PetscBLASIntCast(mfqP->nmax, &blasnmax));

487:   /* Qtmp = I(n x n) */
488:   for (i = 0; i < mfqP->n; i++) {
489:     for (j = 0; j < mfqP->n; j++) mfqP->Q_tmp[i + mfqP->npmax * j] = 0.0;
490:   }
491:   for (j = 0; j < mfqP->n; j++) mfqP->Q_tmp[j + mfqP->npmax * j] = 1.0;

493:   /* Qtmp = Q * I */
494:   PetscCallLAPACKInfo("LAPACKormqr", LAPACKormqr_("R", "N", &blasn, &blasn, &blask, mfqP->Q, &blasnpmax, mfqP->tau, mfqP->Q_tmp, &blasnpmax, mfqP->npmaxwork, &blasnmax, &info));

496:   for (i = mfqP->nmodelpoints; i < mfqP->n; i++) {
497:     PetscCallBLAS("BLASdot", dp = BLASdot_(&blasn, &mfqP->Q_tmp[i * mfqP->npmax], &blas1, mfqP->Gres, &blas1));
498:     if (dp > 0.0) { /* Model says use the other direction! */
499:       for (j = 0; j < mfqP->n; j++) mfqP->Q_tmp[i * mfqP->npmax + j] *= -1;
500:     }
501:     /* mfqP->work[i] = Cres+Modeld(i,:)*(Gres+.5*Hres*Modeld(i,:)') */
502:     for (j = 0; j < mfqP->n; j++) mfqP->work2[j] = mfqP->Gres[j];
503:     PetscCallBLAS("BLASgemv", BLASgemv_("N", &blasn, &blasn, &half, mfqP->Hres, &blasn, &mfqP->Q_tmp[i * mfqP->npmax], &blas1, &one, mfqP->work2, &blas1));
504:     PetscCallBLAS("BLASdot", mfqP->work[i] = BLASdot_(&blasn, &mfqP->Q_tmp[i * mfqP->npmax], &blas1, mfqP->work2, &blas1));
505:     if (i == mfqP->nmodelpoints || mfqP->work[i] < minvalue) {
506:       minindex = i;
507:       minvalue = mfqP->work[i];
508:     }
509:     if (addallpoints != 0) PetscCall(addpoint(tao, mfqP, i));
510:   }
511:   if (!addallpoints) PetscCall(addpoint(tao, mfqP, minindex));
512:   PetscFunctionReturn(PETSC_SUCCESS);
513: }

515: static PetscErrorCode affpoints(TAO_POUNDERS *mfqP, PetscReal *xmin, PetscReal c)
516: {
517:   PetscInt         i, j;
518:   PetscBLASInt     blasm, blasj, blask, blasn, ione = 1;
519:   PetscBLASInt     blasnpmax, blasmaxmn;
520:   PetscReal        proj, normd;
521:   const PetscReal *x;

523:   PetscFunctionBegin;
524:   PetscCall(PetscBLASIntCast(mfqP->npmax, &blasnpmax));
525:   PetscCall(PetscBLASIntCast(mfqP->m, &blasm));
526:   PetscCall(PetscBLASIntCast(mfqP->n, &blasn));
527:   for (i = mfqP->nHist - 1; i >= 0; i--) {
528:     PetscCall(VecGetArrayRead(mfqP->Xhist[i], &x));
529:     for (j = 0; j < mfqP->n; j++) mfqP->work[j] = (x[j] - xmin[j]) / mfqP->delta;
530:     PetscCall(VecRestoreArrayRead(mfqP->Xhist[i], &x));
531:     PetscCallBLAS("BLAScopy", BLAScopy_(&blasn, mfqP->work, &ione, mfqP->work2, &ione));
532:     PetscCallBLAS("BLASnrm2", normd = BLASnrm2_(&blasn, mfqP->work, &ione));
533:     if (normd <= c) {
534:       PetscCall(PetscBLASIntCast(PetscMax(mfqP->n - mfqP->nmodelpoints, 0), &blasj));
535:       if (!mfqP->q_is_I) {
536:         /* project D onto null */
537:         PetscCall(PetscBLASIntCast(mfqP->nmodelpoints, &blask));
538:         PetscCallLAPACKInfo("LAPACKormqr", LAPACKormqr_("R", "N", &ione, &blasn, &blask, mfqP->Q, &blasnpmax, mfqP->tau, mfqP->work2, &ione, mfqP->mwork, &blasm, &info));
539:       }
540:       PetscCallBLAS("BLASnrm2", proj = BLASnrm2_(&blasj, &mfqP->work2[mfqP->nmodelpoints], &ione));

542:       if (proj >= mfqP->theta1) { /* add this index to model */
543:         mfqP->model_indices[mfqP->nmodelpoints] = i;
544:         mfqP->nmodelpoints++;
545:         PetscCallBLAS("BLAScopy", BLAScopy_(&blasn, mfqP->work, &ione, &mfqP->Q_tmp[mfqP->npmax * (mfqP->nmodelpoints - 1)], &ione));
546:         PetscCall(PetscBLASIntCast(mfqP->npmax * (mfqP->nmodelpoints), &blask));
547:         PetscCallBLAS("BLAScopy", BLAScopy_(&blask, mfqP->Q_tmp, &ione, mfqP->Q, &ione));
548:         PetscCall(PetscBLASIntCast(mfqP->nmodelpoints, &blask));
549:         PetscCall(PetscBLASIntCast(PetscMax(mfqP->m, mfqP->n), &blasmaxmn));
550:         PetscCallLAPACKInfo("LAPACKgeqrf", LAPACKgeqrf_(&blasn, &blask, mfqP->Q, &blasnpmax, mfqP->tau, mfqP->mwork, &blasmaxmn, &info));
551:         mfqP->q_is_I = 0;
552:       }
553:       if (mfqP->nmodelpoints == mfqP->n) break;
554:     }
555:   }
556:   PetscFunctionReturn(PETSC_SUCCESS);
557: }

559: static PetscErrorCode TaoSolve_POUNDERS(Tao tao)
560: {
561:   TAO_POUNDERS    *mfqP = (TAO_POUNDERS *)tao->data;
562:   PetscInt         i, ii, j, k, l;
563:   PetscReal        step = 1.0;
564:   PetscInt         low, high;
565:   PetscReal        minnorm;
566:   PetscReal       *x, *f;
567:   const PetscReal *xmint, *fmin;
568:   PetscReal        deltaold;
569:   PetscReal        gnorm;
570:   PetscBLASInt     info, ione = 1, iblas;
571:   PetscBool        valid, same;
572:   PetscReal        mdec, rho, normxsp;
573:   PetscReal        one = 1.0, zero = 0.0, ratio;
574:   PetscBLASInt     blasm, blasn, blasncopy, blasnpmax;
575:   static PetscBool set = PETSC_FALSE;

577:   /* n = # of parameters
578:      m = dimension (components) of function  */
579:   PetscFunctionBegin;
580:   PetscCall(PetscCitationsRegister("@article{UNEDF0,\n"
581:                                    "title = {Nuclear energy density optimization},\n"
582:                                    "author = {Kortelainen, M.  and Lesinski, T.  and Mor\'e, J.  and Nazarewicz, W.\n"
583:                                    "          and Sarich, J.  and Schunck, N.  and Stoitsov, M. V. and Wild, S. },\n"
584:                                    "journal = {Phys. Rev. C},\n"
585:                                    "volume = {82},\n"
586:                                    "number = {2},\n"
587:                                    "pages = {024313},\n"
588:                                    "numpages = {18},\n"
589:                                    "year = {2010},\n"
590:                                    "month = {Aug},\n"
591:                                    "doi = {10.1103/PhysRevC.82.024313}\n}\n",
592:                                    &set));
593:   tao->niter = 0;
594:   if (tao->XL && tao->XU) {
595:     /* Check x0 <= XU */
596:     PetscReal val;

598:     PetscCall(VecCopy(tao->solution, mfqP->Xhist[0]));
599:     PetscCall(VecAXPY(mfqP->Xhist[0], -1.0, tao->XU));
600:     PetscCall(VecMax(mfqP->Xhist[0], NULL, &val));
601:     PetscCheck(val <= 1e-10, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "X0 > upper bound");

603:     /* Check x0 >= xl */
604:     PetscCall(VecCopy(tao->XL, mfqP->Xhist[0]));
605:     PetscCall(VecAXPY(mfqP->Xhist[0], -1.0, tao->solution));
606:     PetscCall(VecMax(mfqP->Xhist[0], NULL, &val));
607:     PetscCheck(val <= 1e-10, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "X0 < lower bound");

609:     /* Check x0 + delta < XU  -- should be able to get around this eventually */

611:     PetscCall(VecSet(mfqP->Xhist[0], mfqP->delta));
612:     PetscCall(VecAXPY(mfqP->Xhist[0], 1.0, tao->solution));
613:     PetscCall(VecAXPY(mfqP->Xhist[0], -1.0, tao->XU));
614:     PetscCall(VecMax(mfqP->Xhist[0], NULL, &val));
615:     PetscCheck(val <= 1e-10, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "X0 + delta > upper bound");
616:   }

618:   PetscCall(PetscBLASIntCast(mfqP->m, &blasm));
619:   PetscCall(PetscBLASIntCast(mfqP->n, &blasn));
620:   PetscCall(PetscBLASIntCast(mfqP->npmax, &blasnpmax));
621:   for (i = 0; i < mfqP->n * mfqP->n * mfqP->m; ++i) mfqP->H[i] = 0;

623:   PetscCall(VecCopy(tao->solution, mfqP->Xhist[0]));

625:   /* This provides enough information to approximate the gradient of the objective */
626:   /* using a forward difference scheme. */

628:   PetscCall(PetscInfo(tao, "Initialize simplex; delta = %10.9e\n", (double)mfqP->delta));
629:   PetscCall(pounders_feval(tao, mfqP->Xhist[0], mfqP->Fhist[0], &mfqP->Fres[0]));
630:   mfqP->minindex = 0;
631:   minnorm        = mfqP->Fres[0];

633:   PetscCall(VecGetOwnershipRange(mfqP->Xhist[0], &low, &high));
634:   for (i = 1; i < mfqP->n + 1; ++i) {
635:     PetscCall(VecCopy(mfqP->Xhist[0], mfqP->Xhist[i]));

637:     if (i - 1 >= low && i - 1 < high) {
638:       PetscCall(VecGetArray(mfqP->Xhist[i], &x));
639:       x[i - 1 - low] += mfqP->delta;
640:       PetscCall(VecRestoreArray(mfqP->Xhist[i], &x));
641:     }
642:     CHKMEMQ;
643:     PetscCall(pounders_feval(tao, mfqP->Xhist[i], mfqP->Fhist[i], &mfqP->Fres[i]));
644:     if (mfqP->Fres[i] < minnorm) {
645:       mfqP->minindex = i;
646:       minnorm        = mfqP->Fres[i];
647:     }
648:   }
649:   PetscCall(VecCopy(mfqP->Xhist[mfqP->minindex], tao->solution));
650:   PetscCall(VecCopy(mfqP->Fhist[mfqP->minindex], tao->ls_res));
651:   PetscCall(PetscInfo(tao, "Finalize simplex; minnorm = %10.9e\n", (double)minnorm));

653:   /* Gather mpi vecs to one big local vec */

655:   /* Begin serial code */

657:   /* Disp[i] = Xi-xmin, i=1,..,mfqP->minindex-1,mfqP->minindex+1,..,n */
658:   /* Fdiff[i] = (Fi-Fmin)', i=1,..,mfqP->minindex-1,mfqP->minindex+1,..,n */
659:   /* (Column oriented for blas calls) */
660:   ii = 0;

662:   PetscCall(PetscInfo(tao, "Build matrix: %d\n", mfqP->size));
663:   if (1 == mfqP->size) {
664:     PetscCall(VecGetArrayRead(mfqP->Xhist[mfqP->minindex], &xmint));
665:     for (i = 0; i < mfqP->n; i++) mfqP->xmin[i] = xmint[i];
666:     PetscCall(VecRestoreArrayRead(mfqP->Xhist[mfqP->minindex], &xmint));
667:     PetscCall(VecGetArrayRead(mfqP->Fhist[mfqP->minindex], &fmin));
668:     for (i = 0; i < mfqP->n + 1; i++) {
669:       if (i == mfqP->minindex) continue;

671:       PetscCall(VecGetArray(mfqP->Xhist[i], &x));
672:       for (j = 0; j < mfqP->n; j++) mfqP->Disp[ii + mfqP->npmax * j] = (x[j] - mfqP->xmin[j]) / mfqP->delta;
673:       PetscCall(VecRestoreArray(mfqP->Xhist[i], &x));

675:       PetscCall(VecGetArray(mfqP->Fhist[i], &f));
676:       for (j = 0; j < mfqP->m; j++) mfqP->Fdiff[ii + mfqP->n * j] = f[j] - fmin[j];
677:       PetscCall(VecRestoreArray(mfqP->Fhist[i], &f));

679:       mfqP->model_indices[ii++] = i;
680:     }
681:     for (j = 0; j < mfqP->m; j++) mfqP->C[j] = fmin[j];
682:     PetscCall(VecRestoreArrayRead(mfqP->Fhist[mfqP->minindex], &fmin));
683:   } else {
684:     PetscCall(VecSet(mfqP->localxmin, 0));
685:     PetscCall(VecScatterBegin(mfqP->scatterx, mfqP->Xhist[mfqP->minindex], mfqP->localxmin, INSERT_VALUES, SCATTER_FORWARD));
686:     PetscCall(VecScatterEnd(mfqP->scatterx, mfqP->Xhist[mfqP->minindex], mfqP->localxmin, INSERT_VALUES, SCATTER_FORWARD));

688:     PetscCall(VecGetArrayRead(mfqP->localxmin, &xmint));
689:     for (i = 0; i < mfqP->n; i++) mfqP->xmin[i] = xmint[i];
690:     PetscCall(VecRestoreArrayRead(mfqP->localxmin, &xmint));

692:     PetscCall(VecScatterBegin(mfqP->scatterf, mfqP->Fhist[mfqP->minindex], mfqP->localfmin, INSERT_VALUES, SCATTER_FORWARD));
693:     PetscCall(VecScatterEnd(mfqP->scatterf, mfqP->Fhist[mfqP->minindex], mfqP->localfmin, INSERT_VALUES, SCATTER_FORWARD));
694:     PetscCall(VecGetArrayRead(mfqP->localfmin, &fmin));
695:     for (i = 0; i < mfqP->n + 1; i++) {
696:       if (i == mfqP->minindex) continue;

698:       PetscCall(VecScatterBegin(mfqP->scatterx, mfqP->Xhist[ii], mfqP->localx, INSERT_VALUES, SCATTER_FORWARD));
699:       PetscCall(VecScatterEnd(mfqP->scatterx, mfqP->Xhist[ii], mfqP->localx, INSERT_VALUES, SCATTER_FORWARD));
700:       PetscCall(VecGetArray(mfqP->localx, &x));
701:       for (j = 0; j < mfqP->n; j++) mfqP->Disp[ii + mfqP->npmax * j] = (x[j] - mfqP->xmin[j]) / mfqP->delta;
702:       PetscCall(VecRestoreArray(mfqP->localx, &x));

704:       PetscCall(VecScatterBegin(mfqP->scatterf, mfqP->Fhist[ii], mfqP->localf, INSERT_VALUES, SCATTER_FORWARD));
705:       PetscCall(VecScatterEnd(mfqP->scatterf, mfqP->Fhist[ii], mfqP->localf, INSERT_VALUES, SCATTER_FORWARD));
706:       PetscCall(VecGetArray(mfqP->localf, &f));
707:       for (j = 0; j < mfqP->m; j++) mfqP->Fdiff[ii + mfqP->n * j] = f[j] - fmin[j];
708:       PetscCall(VecRestoreArray(mfqP->localf, &f));

710:       mfqP->model_indices[ii++] = i;
711:     }
712:     for (j = 0; j < mfqP->m; j++) mfqP->C[j] = fmin[j];
713:     PetscCall(VecRestoreArrayRead(mfqP->localfmin, &fmin));
714:   }

716:   /* Determine the initial quadratic models */
717:   /* G = D(ModelIn,:) \ (F(ModelIn,1:m)-repmat(F(xkin,1:m),n,1)); */
718:   /* D (nxn) Fdiff (nxm)  => G (nxm) */
719:   blasncopy = blasn;
720:   PetscCallBLAS("LAPACKgesv", LAPACKgesv_(&blasn, &blasm, mfqP->Disp, &blasnpmax, mfqP->iwork, mfqP->Fdiff, &blasncopy, &info));
721:   PetscCall(PetscInfo(tao, "Linear solve return: %" PetscBLASInt_FMT "\n", info));

723:   PetscCall(pounders_update_res(tao));

725:   valid = PETSC_TRUE;

727:   PetscCall(VecSetValues(tao->gradient, mfqP->n, mfqP->indices, mfqP->Gres, INSERT_VALUES));
728:   PetscCall(VecAssemblyBegin(tao->gradient));
729:   PetscCall(VecAssemblyEnd(tao->gradient));
730:   PetscCall(VecNorm(tao->gradient, NORM_2, &gnorm));
731:   gnorm *= mfqP->delta;
732:   PetscCall(VecCopy(mfqP->Xhist[mfqP->minindex], tao->solution));

734:   tao->reason = TAO_CONTINUE_ITERATING;
735:   PetscCall(TaoLogConvergenceHistory(tao, minnorm, gnorm, 0.0, tao->ksp_its));
736:   PetscCall(TaoMonitor(tao, tao->niter, minnorm, gnorm, 0.0, step));
737:   PetscUseTypeMethod(tao, convergencetest, tao->cnvP);

739:   mfqP->nHist        = mfqP->n + 1;
740:   mfqP->nmodelpoints = mfqP->n + 1;
741:   PetscCall(PetscInfo(tao, "Initial gradient: %20.19e\n", (double)gnorm));

743:   while (tao->reason == TAO_CONTINUE_ITERATING) {
744:     PetscReal gnm = 1e-4;
745:     /* Call general purpose update function */
746:     PetscTryTypeMethod(tao, update, tao->niter, tao->user_update);
747:     tao->niter++;
748:     /* Solve the subproblem min{Q(s): ||s|| <= 1.0} */
749:     PetscCall(gqtwrap(tao, &gnm, &mdec));
750:     /* Evaluate the function at the new point */

752:     for (i = 0; i < mfqP->n; i++) mfqP->work[i] = mfqP->Xsubproblem[i] * mfqP->delta + mfqP->xmin[i];
753:     PetscCall(VecDuplicate(tao->solution, &mfqP->Xhist[mfqP->nHist]));
754:     PetscCall(VecDuplicate(tao->ls_res, &mfqP->Fhist[mfqP->nHist]));
755:     PetscCall(VecSetValues(mfqP->Xhist[mfqP->nHist], mfqP->n, mfqP->indices, mfqP->work, INSERT_VALUES));
756:     PetscCall(VecAssemblyBegin(mfqP->Xhist[mfqP->nHist]));
757:     PetscCall(VecAssemblyEnd(mfqP->Xhist[mfqP->nHist]));

759:     PetscCall(pounders_feval(tao, mfqP->Xhist[mfqP->nHist], mfqP->Fhist[mfqP->nHist], &mfqP->Fres[mfqP->nHist]));

761:     rho = (mfqP->Fres[mfqP->minindex] - mfqP->Fres[mfqP->nHist]) / mdec;
762:     mfqP->nHist++;

764:     /* Update the center */
765:     if ((rho >= mfqP->eta1) || (rho > mfqP->eta0 && valid == PETSC_TRUE)) {
766:       /* Update model to reflect new base point */
767:       for (i = 0; i < mfqP->n; i++) mfqP->work[i] = (mfqP->work[i] - mfqP->xmin[i]) / mfqP->delta;
768:       for (j = 0; j < mfqP->m; j++) {
769:         /* C(j) = C(j) + work*G(:,j) + .5*work*H(:,:,j)*work';
770:          G(:,j) = G(:,j) + H(:,:,j)*work' */
771:         for (k = 0; k < mfqP->n; k++) {
772:           mfqP->work2[k] = 0.0;
773:           for (l = 0; l < mfqP->n; l++) mfqP->work2[k] += mfqP->H[j + mfqP->m * (k + l * mfqP->n)] * mfqP->work[l];
774:         }
775:         for (i = 0; i < mfqP->n; i++) {
776:           mfqP->C[j] += mfqP->work[i] * (mfqP->Fdiff[i + mfqP->n * j] + 0.5 * mfqP->work2[i]);
777:           mfqP->Fdiff[i + mfqP->n * j] += mfqP->work2[i];
778:         }
779:       }
780:       /* Cres += work*Gres + .5*work*Hres*work';
781:        Gres += Hres*work'; */

783:       PetscCallBLAS("BLASgemv", BLASgemv_("N", &blasn, &blasn, &one, mfqP->Hres, &blasn, mfqP->work, &ione, &zero, mfqP->work2, &ione));
784:       for (i = 0; i < mfqP->n; i++) mfqP->Gres[i] += mfqP->work2[i];
785:       mfqP->minindex = mfqP->nHist - 1;
786:       minnorm        = mfqP->Fres[mfqP->minindex];
787:       PetscCall(VecCopy(mfqP->Fhist[mfqP->minindex], tao->ls_res));
788:       /* Change current center */
789:       PetscCall(VecGetArrayRead(mfqP->Xhist[mfqP->minindex], &xmint));
790:       for (i = 0; i < mfqP->n; i++) mfqP->xmin[i] = xmint[i];
791:       PetscCall(VecRestoreArrayRead(mfqP->Xhist[mfqP->minindex], &xmint));
792:     }

794:     /* Evaluate at a model-improving point if necessary */
795:     if (valid == PETSC_FALSE) {
796:       mfqP->q_is_I       = 1;
797:       mfqP->nmodelpoints = 0;
798:       PetscCall(affpoints(mfqP, mfqP->xmin, mfqP->c1));
799:       if (mfqP->nmodelpoints < mfqP->n) {
800:         PetscCall(PetscInfo(tao, "Model not valid -- model-improving\n"));
801:         PetscCall(modelimprove(tao, mfqP, 1));
802:       }
803:     }

805:     /* Update the trust region radius */
806:     deltaold = mfqP->delta;
807:     normxsp  = 0;
808:     for (i = 0; i < mfqP->n; i++) normxsp += mfqP->Xsubproblem[i] * mfqP->Xsubproblem[i];
809:     normxsp = PetscSqrtReal(normxsp);
810:     if (rho >= mfqP->eta1 && normxsp > 0.5 * mfqP->delta) {
811:       mfqP->delta = PetscMin(mfqP->delta * mfqP->gamma1, mfqP->deltamax);
812:     } else if (valid == PETSC_TRUE) {
813:       mfqP->delta = PetscMax(mfqP->delta * mfqP->gamma0, mfqP->deltamin);
814:     }

816:     /* Compute the next interpolation set */
817:     mfqP->q_is_I       = 1;
818:     mfqP->nmodelpoints = 0;
819:     PetscCall(PetscInfo(tao, "Affine Points: xmin = %20.19e, c1 = %20.19e\n", (double)*mfqP->xmin, (double)mfqP->c1));
820:     PetscCall(affpoints(mfqP, mfqP->xmin, mfqP->c1));
821:     if (mfqP->nmodelpoints == mfqP->n) {
822:       valid = PETSC_TRUE;
823:     } else {
824:       valid = PETSC_FALSE;
825:       PetscCall(PetscInfo(tao, "Affine Points: xmin = %20.19e, c2 = %20.19e\n", (double)*mfqP->xmin, (double)mfqP->c2));
826:       PetscCall(affpoints(mfqP, mfqP->xmin, mfqP->c2));
827:       if (mfqP->n > mfqP->nmodelpoints) {
828:         PetscCall(PetscInfo(tao, "Model not valid -- adding geometry points\n"));
829:         PetscCall(modelimprove(tao, mfqP, mfqP->n - mfqP->nmodelpoints));
830:       }
831:     }
832:     for (i = mfqP->nmodelpoints; i > 0; i--) mfqP->model_indices[i] = mfqP->model_indices[i - 1];
833:     mfqP->nmodelpoints++;
834:     mfqP->model_indices[0] = mfqP->minindex;
835:     PetscCall(morepoints(mfqP));
836:     for (i = 0; i < mfqP->nmodelpoints; i++) {
837:       PetscCall(VecGetArray(mfqP->Xhist[mfqP->model_indices[i]], &x));
838:       for (j = 0; j < mfqP->n; j++) mfqP->Disp[i + mfqP->npmax * j] = (x[j] - mfqP->xmin[j]) / deltaold;
839:       PetscCall(VecRestoreArray(mfqP->Xhist[mfqP->model_indices[i]], &x));
840:       PetscCall(VecGetArray(mfqP->Fhist[mfqP->model_indices[i]], &f));
841:       for (j = 0; j < mfqP->m; j++) {
842:         for (k = 0; k < mfqP->n; k++) {
843:           mfqP->work[k] = 0.0;
844:           for (l = 0; l < mfqP->n; l++) mfqP->work[k] += mfqP->H[j + mfqP->m * (k + mfqP->n * l)] * mfqP->Disp[i + mfqP->npmax * l];
845:         }
846:         PetscCallBLAS("BLASdot", mfqP->RES[j * mfqP->npmax + i] = -mfqP->C[j] - BLASdot_(&blasn, &mfqP->Fdiff[j * mfqP->n], &ione, &mfqP->Disp[i], &blasnpmax) - 0.5 * BLASdot_(&blasn, mfqP->work, &ione, &mfqP->Disp[i], &blasnpmax) + f[j]);
847:       }
848:       PetscCall(VecRestoreArray(mfqP->Fhist[mfqP->model_indices[i]], &f));
849:     }

851:     /* Update the quadratic model */
852:     PetscCall(PetscInfo(tao, "Get Quad, size: %" PetscInt_FMT ", points: %" PetscInt_FMT "\n", mfqP->n, mfqP->nmodelpoints));
853:     PetscCall(getquadpounders(mfqP));
854:     PetscCall(VecGetArrayRead(mfqP->Fhist[mfqP->minindex], &fmin));
855:     PetscCallBLAS("BLAScopy", BLAScopy_(&blasm, fmin, &ione, mfqP->C, &ione));
856:     /* G = G*(delta/deltaold) + Gdel */
857:     ratio = mfqP->delta / deltaold;
858:     iblas = blasm * blasn;
859:     PetscCallBLAS("BLASscal", BLASscal_(&iblas, &ratio, mfqP->Fdiff, &ione));
860:     PetscCallBLAS("BLASaxpy", BLASaxpy_(&iblas, &one, mfqP->Gdel, &ione, mfqP->Fdiff, &ione));
861:     /* H = H*(delta/deltaold)^2 + Hdel */
862:     iblas = blasm * blasn * blasn;
863:     ratio *= ratio;
864:     PetscCallBLAS("BLASscal", BLASscal_(&iblas, &ratio, mfqP->H, &ione));
865:     PetscCallBLAS("BLASaxpy", BLASaxpy_(&iblas, &one, mfqP->Hdel, &ione, mfqP->H, &ione));

867:     /* Get residuals */
868:     PetscCall(pounders_update_res(tao));

870:     /* Export solution and gradient residual to TAO */
871:     PetscCall(VecCopy(mfqP->Xhist[mfqP->minindex], tao->solution));
872:     PetscCall(VecSetValues(tao->gradient, mfqP->n, mfqP->indices, mfqP->Gres, INSERT_VALUES));
873:     PetscCall(VecAssemblyBegin(tao->gradient));
874:     PetscCall(VecAssemblyEnd(tao->gradient));
875:     PetscCall(VecNorm(tao->gradient, NORM_2, &gnorm));
876:     gnorm *= mfqP->delta;
877:     /*  final criticality test */
878:     PetscCall(TaoLogConvergenceHistory(tao, minnorm, gnorm, 0.0, tao->ksp_its));
879:     PetscCall(TaoMonitor(tao, tao->niter, minnorm, gnorm, 0.0, step));
880:     PetscUseTypeMethod(tao, convergencetest, tao->cnvP);
881:     /* test for repeated model */
882:     if (mfqP->nmodelpoints == mfqP->last_nmodelpoints) {
883:       same = PETSC_TRUE;
884:     } else {
885:       same = PETSC_FALSE;
886:     }
887:     for (i = 0; i < mfqP->nmodelpoints; i++) {
888:       if (same) {
889:         if (mfqP->model_indices[i] == mfqP->last_model_indices[i]) {
890:           same = PETSC_TRUE;
891:         } else {
892:           same = PETSC_FALSE;
893:         }
894:       }
895:       mfqP->last_model_indices[i] = mfqP->model_indices[i];
896:     }
897:     mfqP->last_nmodelpoints = mfqP->nmodelpoints;
898:     if (same && mfqP->delta == deltaold) {
899:       PetscCall(PetscInfo(tao, "Identical model used in successive iterations\n"));
900:       tao->reason = TAO_CONVERGED_STEPTOL;
901:     }
902:   }
903:   PetscFunctionReturn(PETSC_SUCCESS);
904: }

906: static PetscErrorCode TaoSetUp_POUNDERS(Tao tao)
907: {
908:   TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;
909:   IS            isfloc, isfglob, isxloc, isxglob;

911:   PetscFunctionBegin;
912:   if (!tao->gradient) PetscCall(VecDuplicate(tao->solution, &tao->gradient));
913:   if (!tao->stepdirection) PetscCall(VecDuplicate(tao->solution, &tao->stepdirection));
914:   PetscCall(VecGetSize(tao->solution, &mfqP->n));
915:   PetscCall(VecGetSize(tao->ls_res, &mfqP->m));
916:   mfqP->c1 = PetscSqrtReal((PetscReal)mfqP->n);
917:   if (mfqP->npmax == PETSC_CURRENT) mfqP->npmax = 2 * mfqP->n + 1;
918:   mfqP->npmax = PetscMin((mfqP->n + 1) * (mfqP->n + 2) / 2, mfqP->npmax);
919:   mfqP->npmax = PetscMax(mfqP->npmax, mfqP->n + 2);

921:   PetscCall(PetscMalloc1(tao->max_funcs + 100, &mfqP->Xhist));
922:   PetscCall(PetscMalloc1(tao->max_funcs + 100, &mfqP->Fhist));
923:   for (PetscInt i = 0; i < mfqP->n + 1; i++) {
924:     PetscCall(VecDuplicate(tao->solution, &mfqP->Xhist[i]));
925:     PetscCall(VecDuplicate(tao->ls_res, &mfqP->Fhist[i]));
926:   }
927:   PetscCall(VecDuplicate(tao->solution, &mfqP->workxvec));
928:   PetscCall(VecDuplicate(tao->ls_res, &mfqP->workfvec));
929:   mfqP->nHist = 0;

931:   PetscCall(PetscMalloc1(tao->max_funcs + 100, &mfqP->Fres));
932:   PetscCall(PetscMalloc1(mfqP->npmax * mfqP->m, &mfqP->RES));
933:   PetscCall(PetscMalloc1(mfqP->n, &mfqP->work));
934:   PetscCall(PetscMalloc1(mfqP->n, &mfqP->work2));
935:   PetscCall(PetscMalloc1(mfqP->n, &mfqP->work3));
936:   PetscCall(PetscMalloc1(PetscMax(mfqP->m, mfqP->n + 1), &mfqP->mwork));
937:   PetscCall(PetscMalloc1(mfqP->npmax - mfqP->n - 1, &mfqP->omega));
938:   PetscCall(PetscMalloc1(mfqP->n * (mfqP->n + 1) / 2, &mfqP->beta));
939:   PetscCall(PetscMalloc1(mfqP->n + 1, &mfqP->alpha));

941:   PetscCall(PetscMalloc1(mfqP->n * mfqP->n * mfqP->m, &mfqP->H));
942:   PetscCall(PetscMalloc1(mfqP->npmax * mfqP->npmax, &mfqP->Q));
943:   PetscCall(PetscMalloc1(mfqP->npmax * mfqP->npmax, &mfqP->Q_tmp));
944:   PetscCall(PetscMalloc1(mfqP->n * (mfqP->n + 1) / 2 * (mfqP->npmax), &mfqP->L));
945:   PetscCall(PetscMalloc1(mfqP->n * (mfqP->n + 1) / 2 * (mfqP->npmax), &mfqP->L_tmp));
946:   PetscCall(PetscMalloc1(mfqP->n * (mfqP->n + 1) / 2 * (mfqP->npmax), &mfqP->L_save));
947:   PetscCall(PetscMalloc1(mfqP->n * (mfqP->n + 1) / 2 * (mfqP->npmax), &mfqP->N));
948:   PetscCall(PetscMalloc1(mfqP->npmax * (mfqP->n + 1), &mfqP->M));
949:   PetscCall(PetscMalloc1(mfqP->npmax * (mfqP->npmax - mfqP->n - 1), &mfqP->Z));
950:   PetscCall(PetscMalloc1(mfqP->npmax, &mfqP->tau));
951:   PetscCall(PetscMalloc1(mfqP->npmax, &mfqP->tau_tmp));
952:   mfqP->nmax = PetscMax(5 * mfqP->npmax, mfqP->n * (mfqP->n + 1) / 2);
953:   PetscCall(PetscMalloc1(mfqP->nmax, &mfqP->npmaxwork));
954:   PetscCall(PetscMalloc1(mfqP->nmax, &mfqP->npmaxiwork));
955:   PetscCall(PetscMalloc1(mfqP->n, &mfqP->xmin));
956:   PetscCall(PetscMalloc1(mfqP->m, &mfqP->C));
957:   PetscCall(PetscMalloc1(mfqP->m * mfqP->n, &mfqP->Fdiff));
958:   PetscCall(PetscMalloc1(mfqP->npmax * mfqP->n, &mfqP->Disp));
959:   PetscCall(PetscMalloc1(mfqP->n, &mfqP->Gres));
960:   PetscCall(PetscMalloc1(mfqP->n * mfqP->n, &mfqP->Hres));
961:   PetscCall(PetscMalloc1(mfqP->n * mfqP->n, &mfqP->Gpoints));
962:   PetscCall(PetscMalloc1(mfqP->npmax, &mfqP->model_indices));
963:   PetscCall(PetscMalloc1(mfqP->npmax, &mfqP->last_model_indices));
964:   PetscCall(PetscMalloc1(mfqP->n, &mfqP->Xsubproblem));
965:   PetscCall(PetscMalloc1(mfqP->m * mfqP->n, &mfqP->Gdel));
966:   PetscCall(PetscMalloc1(mfqP->n * mfqP->n * mfqP->m, &mfqP->Hdel));
967:   PetscCall(PetscMalloc1(PetscMax(mfqP->m, mfqP->n), &mfqP->indices));
968:   PetscCall(PetscMalloc1(mfqP->n, &mfqP->iwork));
969:   PetscCall(PetscMalloc1(mfqP->m * mfqP->m, &mfqP->w));
970:   for (PetscInt i = 0; i < mfqP->m; i++) {
971:     for (PetscInt j = 0; j < mfqP->m; j++) {
972:       if (i == j) {
973:         mfqP->w[i + mfqP->m * j] = 1.0;
974:       } else {
975:         mfqP->w[i + mfqP->m * j] = 0.0;
976:       }
977:     }
978:   }
979:   for (PetscInt i = 0; i < PetscMax(mfqP->m, mfqP->n); i++) mfqP->indices[i] = i;
980:   PetscCallMPI(MPI_Comm_size(((PetscObject)tao)->comm, &mfqP->size));
981:   if (mfqP->size > 1) {
982:     PetscCall(VecCreateSeq(PETSC_COMM_SELF, mfqP->n, &mfqP->localx));
983:     PetscCall(VecCreateSeq(PETSC_COMM_SELF, mfqP->n, &mfqP->localxmin));
984:     PetscCall(VecCreateSeq(PETSC_COMM_SELF, mfqP->m, &mfqP->localf));
985:     PetscCall(VecCreateSeq(PETSC_COMM_SELF, mfqP->m, &mfqP->localfmin));
986:     PetscCall(ISCreateStride(MPI_COMM_SELF, mfqP->n, 0, 1, &isxloc));
987:     PetscCall(ISCreateStride(MPI_COMM_SELF, mfqP->n, 0, 1, &isxglob));
988:     PetscCall(ISCreateStride(MPI_COMM_SELF, mfqP->m, 0, 1, &isfloc));
989:     PetscCall(ISCreateStride(MPI_COMM_SELF, mfqP->m, 0, 1, &isfglob));

991:     PetscCall(VecScatterCreate(tao->solution, isxglob, mfqP->localx, isxloc, &mfqP->scatterx));
992:     PetscCall(VecScatterCreate(tao->ls_res, isfglob, mfqP->localf, isfloc, &mfqP->scatterf));

994:     PetscCall(ISDestroy(&isxloc));
995:     PetscCall(ISDestroy(&isxglob));
996:     PetscCall(ISDestroy(&isfloc));
997:     PetscCall(ISDestroy(&isfglob));
998:   }

1000:   if (!mfqP->usegqt) {
1001:     KSP ksp;
1002:     PC  pc;
1003:     PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, mfqP->n, mfqP->n, mfqP->Xsubproblem, &mfqP->subx));
1004:     PetscCall(VecCreateSeq(PETSC_COMM_SELF, mfqP->n, &mfqP->subxl));
1005:     PetscCall(VecDuplicate(mfqP->subxl, &mfqP->subb));
1006:     PetscCall(VecDuplicate(mfqP->subxl, &mfqP->subxu));
1007:     PetscCall(VecDuplicate(mfqP->subxl, &mfqP->subpdel));
1008:     PetscCall(VecDuplicate(mfqP->subxl, &mfqP->subndel));
1009:     PetscCall(TaoCreate(PETSC_COMM_SELF, &mfqP->subtao));
1010:     PetscCall(PetscObjectIncrementTabLevel((PetscObject)mfqP->subtao, (PetscObject)tao, 1));
1011:     PetscCall(TaoSetType(mfqP->subtao, TAOBNTR));
1012:     PetscCall(TaoSetOptionsPrefix(mfqP->subtao, "pounders_subsolver_"));
1013:     PetscCall(TaoSetSolution(mfqP->subtao, mfqP->subx));
1014:     PetscCall(TaoSetObjectiveAndGradient(mfqP->subtao, NULL, pounders_fg, (void *)mfqP));
1015:     PetscCall(TaoSetMaximumIterations(mfqP->subtao, mfqP->gqt_maxits));
1016:     PetscCall(TaoSetFromOptions(mfqP->subtao));
1017:     PetscCall(TaoGetKSP(mfqP->subtao, &ksp));
1018:     if (ksp) {
1019:       PetscCall(KSPGetPC(ksp, &pc));
1020:       PetscCall(PCSetType(pc, PCNONE));
1021:     }
1022:     PetscCall(TaoSetVariableBounds(mfqP->subtao, mfqP->subxl, mfqP->subxu));
1023:     PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, mfqP->n, mfqP->n, mfqP->Hres, &mfqP->subH));
1024:     PetscCall(TaoSetHessian(mfqP->subtao, mfqP->subH, mfqP->subH, pounders_h, (void *)mfqP));
1025:   }
1026:   PetscFunctionReturn(PETSC_SUCCESS);
1027: }

1029: static PetscErrorCode TaoDestroy_POUNDERS(Tao tao)
1030: {
1031:   TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;

1033:   PetscFunctionBegin;
1034:   if (!mfqP->usegqt) {
1035:     PetscCall(TaoDestroy(&mfqP->subtao));
1036:     PetscCall(VecDestroy(&mfqP->subx));
1037:     PetscCall(VecDestroy(&mfqP->subxl));
1038:     PetscCall(VecDestroy(&mfqP->subxu));
1039:     PetscCall(VecDestroy(&mfqP->subb));
1040:     PetscCall(VecDestroy(&mfqP->subpdel));
1041:     PetscCall(VecDestroy(&mfqP->subndel));
1042:     PetscCall(MatDestroy(&mfqP->subH));
1043:   }
1044:   PetscCall(PetscFree(mfqP->Fres));
1045:   PetscCall(PetscFree(mfqP->RES));
1046:   PetscCall(PetscFree(mfqP->work));
1047:   PetscCall(PetscFree(mfqP->work2));
1048:   PetscCall(PetscFree(mfqP->work3));
1049:   PetscCall(PetscFree(mfqP->mwork));
1050:   PetscCall(PetscFree(mfqP->omega));
1051:   PetscCall(PetscFree(mfqP->beta));
1052:   PetscCall(PetscFree(mfqP->alpha));
1053:   PetscCall(PetscFree(mfqP->H));
1054:   PetscCall(PetscFree(mfqP->Q));
1055:   PetscCall(PetscFree(mfqP->Q_tmp));
1056:   PetscCall(PetscFree(mfqP->L));
1057:   PetscCall(PetscFree(mfqP->L_tmp));
1058:   PetscCall(PetscFree(mfqP->L_save));
1059:   PetscCall(PetscFree(mfqP->N));
1060:   PetscCall(PetscFree(mfqP->M));
1061:   PetscCall(PetscFree(mfqP->Z));
1062:   PetscCall(PetscFree(mfqP->tau));
1063:   PetscCall(PetscFree(mfqP->tau_tmp));
1064:   PetscCall(PetscFree(mfqP->npmaxwork));
1065:   PetscCall(PetscFree(mfqP->npmaxiwork));
1066:   PetscCall(PetscFree(mfqP->xmin));
1067:   PetscCall(PetscFree(mfqP->C));
1068:   PetscCall(PetscFree(mfqP->Fdiff));
1069:   PetscCall(PetscFree(mfqP->Disp));
1070:   PetscCall(PetscFree(mfqP->Gres));
1071:   PetscCall(PetscFree(mfqP->Hres));
1072:   PetscCall(PetscFree(mfqP->Gpoints));
1073:   PetscCall(PetscFree(mfqP->model_indices));
1074:   PetscCall(PetscFree(mfqP->last_model_indices));
1075:   PetscCall(PetscFree(mfqP->Xsubproblem));
1076:   PetscCall(PetscFree(mfqP->Gdel));
1077:   PetscCall(PetscFree(mfqP->Hdel));
1078:   PetscCall(PetscFree(mfqP->indices));
1079:   PetscCall(PetscFree(mfqP->iwork));
1080:   PetscCall(PetscFree(mfqP->w));
1081:   for (PetscInt i = 0; i < mfqP->nHist; i++) {
1082:     PetscCall(VecDestroy(&mfqP->Xhist[i]));
1083:     PetscCall(VecDestroy(&mfqP->Fhist[i]));
1084:   }
1085:   PetscCall(VecDestroy(&mfqP->workxvec));
1086:   PetscCall(VecDestroy(&mfqP->workfvec));
1087:   PetscCall(PetscFree(mfqP->Xhist));
1088:   PetscCall(PetscFree(mfqP->Fhist));

1090:   if (mfqP->size > 1) {
1091:     PetscCall(VecDestroy(&mfqP->localx));
1092:     PetscCall(VecDestroy(&mfqP->localxmin));
1093:     PetscCall(VecDestroy(&mfqP->localf));
1094:     PetscCall(VecDestroy(&mfqP->localfmin));
1095:   }
1096:   PetscCall(PetscFree(tao->data));
1097:   PetscFunctionReturn(PETSC_SUCCESS);
1098: }

1100: static PetscErrorCode TaoSetFromOptions_POUNDERS(Tao tao, PetscOptionItems PetscOptionsObject)
1101: {
1102:   TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;

1104:   PetscFunctionBegin;
1105:   PetscOptionsHeadBegin(PetscOptionsObject, "POUNDERS method for least-squares optimization");
1106:   PetscCall(PetscOptionsReal("-tao_pounders_delta", "initial delta", "", mfqP->delta, &mfqP->delta0, NULL));
1107:   mfqP->delta = mfqP->delta0;
1108:   PetscCall(PetscOptionsInt("-tao_pounders_npmax", "max number of points in model", "", mfqP->npmax, &mfqP->npmax, NULL));
1109:   PetscCall(PetscOptionsBool("-tao_pounders_gqt", "use gqt algorithm for subproblem", "", mfqP->usegqt, &mfqP->usegqt, NULL));
1110:   PetscOptionsHeadEnd();
1111:   PetscFunctionReturn(PETSC_SUCCESS);
1112: }

1114: static PetscErrorCode TaoView_POUNDERS(Tao tao, PetscViewer viewer)
1115: {
1116:   TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;
1117:   PetscBool     isascii;

1119:   PetscFunctionBegin;
1120:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1121:   if (isascii) {
1122:     PetscCall(PetscViewerASCIIPrintf(viewer, "initial delta: %g\n", (double)mfqP->delta0));
1123:     PetscCall(PetscViewerASCIIPrintf(viewer, "final delta: %g\n", (double)mfqP->delta));
1124:     PetscCall(PetscViewerASCIIPrintf(viewer, "model points: %" PetscInt_FMT "\n", mfqP->nmodelpoints));
1125:     if (mfqP->usegqt) {
1126:       PetscCall(PetscViewerASCIIPrintf(viewer, "subproblem solver: gqt\n"));
1127:     } else {
1128:       PetscCall(TaoView(mfqP->subtao, viewer));
1129:     }
1130:   }
1131:   PetscFunctionReturn(PETSC_SUCCESS);
1132: }
1133: /*MC
1134:   TAOPOUNDERS - POUNDERS derivate-free model-based algorithm for nonlinear least squares

1136:   Options Database Keys:
1137: + -tao_pounders_delta - initial step length
1138: . -tao_pounders_npmax - maximum number of points in model
1139: - -tao_pounders_gqt   - use gqt algorithm for subproblem instead of TRON

1141:   Level: beginner

1143: .seealso: `Tao`, `TAONTR`, `TAONTL`, `TAONM`, `TaoType`, `TaoCreate()`
1144: M*/

1146: PETSC_EXTERN PetscErrorCode TaoCreate_POUNDERS(Tao tao)
1147: {
1148:   TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;

1150:   PetscFunctionBegin;
1151:   tao->ops->setup          = TaoSetUp_POUNDERS;
1152:   tao->ops->solve          = TaoSolve_POUNDERS;
1153:   tao->ops->view           = TaoView_POUNDERS;
1154:   tao->ops->setfromoptions = TaoSetFromOptions_POUNDERS;
1155:   tao->ops->destroy        = TaoDestroy_POUNDERS;

1157:   PetscCall(PetscNew(&mfqP));
1158:   tao->data = (void *)mfqP;

1160:   /* Override default settings (unless already changed) */
1161:   PetscObjectParameterSetDefault(tao, max_it, 2000);
1162:   PetscObjectParameterSetDefault(tao, max_funcs, 4000);

1164:   mfqP->npmax      = PETSC_CURRENT;
1165:   mfqP->delta0     = 0.1;
1166:   mfqP->delta      = 0.1;
1167:   mfqP->deltamax   = 1e3;
1168:   mfqP->deltamin   = 1e-6;
1169:   mfqP->c2         = 10.0;
1170:   mfqP->theta1     = 1e-5;
1171:   mfqP->theta2     = 1e-4;
1172:   mfqP->gamma0     = 0.5;
1173:   mfqP->gamma1     = 2.0;
1174:   mfqP->eta0       = 0.0;
1175:   mfqP->eta1       = 0.1;
1176:   mfqP->usegqt     = PETSC_FALSE;
1177:   mfqP->gqt_rtol   = 0.001;
1178:   mfqP->gqt_maxits = 50;
1179:   mfqP->workxvec   = NULL;
1180:   PetscFunctionReturn(PETSC_SUCCESS);
1181: }