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: #if defined(PETSC_USE_REAL_SINGLE)
59: PetscReal atol = 1.0e-5;
60: #else
61: PetscReal atol = 1.0e-10;
62: #endif
63: PetscInt info, its;
64: TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;
66: PetscFunctionBegin;
67: if (!mfqP->usegqt) {
68: PetscReal maxval;
69: PetscInt i, j;
71: PetscCall(VecSetValues(mfqP->subb, mfqP->n, mfqP->indices, mfqP->Gres, INSERT_VALUES));
72: PetscCall(VecAssemblyBegin(mfqP->subb));
73: PetscCall(VecAssemblyEnd(mfqP->subb));
75: PetscCall(VecSet(mfqP->subx, 0.0));
77: PetscCall(VecSet(mfqP->subndel, -1.0));
78: PetscCall(VecSet(mfqP->subpdel, +1.0));
80: /* Complete the lower triangle of the Hessian matrix */
81: for (i = 0; i < mfqP->n; i++) {
82: for (j = i + 1; j < mfqP->n; j++) mfqP->Hres[j + mfqP->n * i] = mfqP->Hres[mfqP->n * j + i];
83: }
84: PetscCall(MatSetValues(mfqP->subH, mfqP->n, mfqP->indices, mfqP->n, mfqP->indices, mfqP->Hres, INSERT_VALUES));
85: PetscCall(MatAssemblyBegin(mfqP->subH, MAT_FINAL_ASSEMBLY));
86: PetscCall(MatAssemblyEnd(mfqP->subH, MAT_FINAL_ASSEMBLY));
88: PetscCall(TaoResetStatistics(mfqP->subtao));
89: /* PetscCall(TaoSetTolerances(mfqP->subtao,*gnorm,*gnorm,PETSC_CURRENT)); */
90: /* enforce bound constraints -- experimental */
91: if (tao->XU && tao->XL) {
92: PetscCall(VecCopy(tao->XU, mfqP->subxu));
93: PetscCall(VecAXPY(mfqP->subxu, -1.0, tao->solution));
94: PetscCall(VecScale(mfqP->subxu, 1.0 / mfqP->delta));
95: PetscCall(VecCopy(tao->XL, mfqP->subxl));
96: PetscCall(VecAXPY(mfqP->subxl, -1.0, tao->solution));
97: PetscCall(VecScale(mfqP->subxl, 1.0 / mfqP->delta));
99: PetscCall(VecPointwiseMin(mfqP->subxu, mfqP->subxu, mfqP->subpdel));
100: PetscCall(VecPointwiseMax(mfqP->subxl, mfqP->subxl, mfqP->subndel));
101: } else {
102: PetscCall(VecCopy(mfqP->subpdel, mfqP->subxu));
103: PetscCall(VecCopy(mfqP->subndel, mfqP->subxl));
104: }
105: /* Make sure xu > xl */
106: PetscCall(VecCopy(mfqP->subxl, mfqP->subpdel));
107: PetscCall(VecAXPY(mfqP->subpdel, -1.0, mfqP->subxu));
108: PetscCall(VecMax(mfqP->subpdel, NULL, &maxval));
109: PetscCheck(maxval <= 1e-10, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "upper bound < lower bound in subproblem");
110: /* Make sure xu > tao->solution > xl */
111: PetscCall(VecCopy(mfqP->subxl, mfqP->subpdel));
112: PetscCall(VecAXPY(mfqP->subpdel, -1.0, mfqP->subx));
113: PetscCall(VecMax(mfqP->subpdel, NULL, &maxval));
114: PetscCheck(maxval <= 1e-10, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "initial guess < lower bound in subproblem");
116: PetscCall(VecCopy(mfqP->subx, mfqP->subpdel));
117: PetscCall(VecAXPY(mfqP->subpdel, -1.0, mfqP->subxu));
118: PetscCall(VecMax(mfqP->subpdel, NULL, &maxval));
119: PetscCheck(maxval <= 1e-10, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "initial guess > upper bound in subproblem");
121: PetscCall(TaoSolve(mfqP->subtao));
122: PetscCall(TaoGetSolutionStatus(mfqP->subtao, NULL, qmin, NULL, NULL, NULL, NULL));
124: /* test bounds post-solution*/
125: PetscCall(VecCopy(mfqP->subxl, mfqP->subpdel));
126: PetscCall(VecAXPY(mfqP->subpdel, -1.0, mfqP->subx));
127: PetscCall(VecMax(mfqP->subpdel, NULL, &maxval));
128: if (maxval > 1e-5) {
129: PetscCall(PetscInfo(tao, "subproblem solution < lower bound\n"));
130: tao->reason = TAO_DIVERGED_TR_REDUCTION;
131: }
133: PetscCall(VecCopy(mfqP->subx, mfqP->subpdel));
134: PetscCall(VecAXPY(mfqP->subpdel, -1.0, mfqP->subxu));
135: PetscCall(VecMax(mfqP->subpdel, NULL, &maxval));
136: if (maxval > 1e-5) {
137: PetscCall(PetscInfo(tao, "subproblem solution > upper bound\n"));
138: tao->reason = TAO_DIVERGED_TR_REDUCTION;
139: }
140: } else {
141: 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));
142: }
143: *qmin *= -1;
144: PetscFunctionReturn(PETSC_SUCCESS);
145: }
147: static PetscErrorCode pounders_update_res(Tao tao)
148: {
149: TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;
150: PetscInt i, row, col;
151: PetscBLASInt blasn, blasn2, blasm, ione = 1;
152: PetscReal zero = 0.0, one = 1.0, wii, factor;
154: PetscFunctionBegin;
155: PetscCall(PetscBLASIntCast(mfqP->n, &blasn));
156: PetscCall(PetscBLASIntCast(mfqP->m, &blasm));
157: PetscCall(PetscBLASIntCast(mfqP->n * mfqP->n, &blasn2));
158: for (i = 0; i < mfqP->n; i++) mfqP->Gres[i] = 0;
159: for (i = 0; i < mfqP->n * mfqP->n; i++) mfqP->Hres[i] = 0;
161: /* Compute Gres= sum_ij[wij * (cjgi + cigj)] */
162: if (tao->res_weights_v) {
163: /* Vector(diagonal) weights: gres = sum_i(wii*ci*gi) */
164: for (i = 0; i < mfqP->m; i++) {
165: PetscCall(VecGetValues(tao->res_weights_v, 1, &i, &factor));
166: factor = factor * mfqP->C[i];
167: PetscCallBLAS("BLASaxpy", BLASaxpy_(&blasn, &factor, &mfqP->Fdiff[blasn * i], &ione, mfqP->Gres, &ione));
168: }
170: /* compute Hres = sum_ij [wij * (*ci*Hj + cj*Hi + gi gj' + gj gi') ] */
171: /* vector(diagonal weights) Hres = sum_i(wii*(ci*Hi + gi * gi')*/
172: for (i = 0; i < mfqP->m; i++) {
173: PetscCall(VecGetValues(tao->res_weights_v, 1, &i, &wii));
174: if (tao->niter > 1) {
175: factor = wii * mfqP->C[i];
176: /* add wii * ci * Hi */
177: PetscCallBLAS("BLASaxpy", BLASaxpy_(&blasn2, &factor, &mfqP->H[i], &blasm, mfqP->Hres, &ione));
178: }
179: /* add wii * gi * gi' */
180: PetscCallBLAS("BLASgemm", BLASgemm_("N", "T", &blasn, &blasn, &ione, &wii, &mfqP->Fdiff[blasn * i], &blasn, &mfqP->Fdiff[blasn * i], &blasn, &one, mfqP->Hres, &blasn));
181: }
182: } else if (tao->res_weights_w) {
183: /* General case: .5 * Gres= sum_ij[wij * (cjgi + cigj)] */
184: for (i = 0; i < tao->res_weights_n; i++) {
185: row = tao->res_weights_rows[i];
186: col = tao->res_weights_cols[i];
188: factor = tao->res_weights_w[i] * mfqP->C[col] / 2.0;
189: PetscCallBLAS("BLASaxpy", BLASaxpy_(&blasn, &factor, &mfqP->Fdiff[blasn * row], &ione, mfqP->Gres, &ione));
190: factor = tao->res_weights_w[i] * mfqP->C[row] / 2.0;
191: PetscCallBLAS("BLASaxpy", BLASaxpy_(&blasn, &factor, &mfqP->Fdiff[blasn * col], &ione, mfqP->Gres, &ione));
192: }
194: /* compute Hres = sum_ij [wij * (*ci*Hj + cj*Hi + gi gj' + gj gi') ] */
195: /* .5 * sum_ij [wij * (*ci*Hj + cj*Hi + gi gj' + gj gi') ] */
196: for (i = 0; i < tao->res_weights_n; i++) {
197: row = tao->res_weights_rows[i];
198: col = tao->res_weights_cols[i];
199: factor = tao->res_weights_w[i] / 2.0;
200: /* add wij * gi gj' + wij * gj gi' */
201: PetscCallBLAS("BLASgemm", BLASgemm_("N", "T", &blasn, &blasn, &ione, &factor, &mfqP->Fdiff[blasn * row], &blasn, &mfqP->Fdiff[blasn * col], &blasn, &one, mfqP->Hres, &blasn));
202: PetscCallBLAS("BLASgemm", BLASgemm_("N", "T", &blasn, &blasn, &ione, &factor, &mfqP->Fdiff[blasn * col], &blasn, &mfqP->Fdiff[blasn * row], &blasn, &one, mfqP->Hres, &blasn));
203: }
204: if (tao->niter > 1) {
205: for (i = 0; i < tao->res_weights_n; i++) {
206: row = tao->res_weights_rows[i];
207: col = tao->res_weights_cols[i];
209: /* add wij*cj*Hi */
210: factor = tao->res_weights_w[i] * mfqP->C[col] / 2.0;
211: PetscCallBLAS("BLASaxpy", BLASaxpy_(&blasn2, &factor, &mfqP->H[row], &blasm, mfqP->Hres, &ione));
213: /* add wij*ci*Hj */
214: factor = tao->res_weights_w[i] * mfqP->C[row] / 2.0;
215: PetscCallBLAS("BLASaxpy", BLASaxpy_(&blasn2, &factor, &mfqP->H[col], &blasm, mfqP->Hres, &ione));
216: }
217: }
218: } else {
219: /* Default: Gres= sum_i[cigi] = G*c' */
220: PetscCall(PetscInfo(tao, "Identity weights\n"));
221: PetscCallBLAS("BLASgemv", BLASgemv_("N", &blasn, &blasm, &one, mfqP->Fdiff, &blasn, mfqP->C, &ione, &zero, mfqP->Gres, &ione));
223: /* compute Hres = sum_ij [wij * (*ci*Hj + cj*Hi + gi gj' + gj gi') ] */
224: /* Hres = G*G' + 0.5 sum {F(xkin,i)*H(:,:,i)} */
225: PetscCallBLAS("BLASgemm", BLASgemm_("N", "T", &blasn, &blasn, &blasm, &one, mfqP->Fdiff, &blasn, mfqP->Fdiff, &blasn, &zero, mfqP->Hres, &blasn));
227: /* sum(F(xkin,i)*H(:,:,i)) */
228: if (tao->niter > 1) {
229: for (i = 0; i < mfqP->m; i++) {
230: factor = mfqP->C[i];
231: PetscCallBLAS("BLASaxpy", BLASaxpy_(&blasn2, &factor, &mfqP->H[i], &blasm, mfqP->Hres, &ione));
232: }
233: }
234: }
235: PetscFunctionReturn(PETSC_SUCCESS);
236: }
238: static PetscErrorCode phi2eval(PetscReal *x, PetscInt n, PetscReal *phi)
239: {
240: /* 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] */
241: PetscInt i, j, k;
242: PetscReal sqrt2 = PetscSqrtReal(2.0);
244: PetscFunctionBegin;
245: j = 0;
246: for (i = 0; i < n; i++) {
247: phi[j] = 0.5 * x[i] * x[i];
248: j++;
249: for (k = i + 1; k < n; k++) {
250: phi[j] = x[i] * x[k] / sqrt2;
251: j++;
252: }
253: }
254: PetscFunctionReturn(PETSC_SUCCESS);
255: }
257: static PetscErrorCode getquadpounders(TAO_POUNDERS *mfqP)
258: {
259: /* Computes the parameters of the quadratic Q(x) = c + g'*x + 0.5*x*G*x'
260: that satisfies the interpolation conditions Q(X[:,j]) = f(j)
261: for j=1,...,m and with a Hessian matrix of least Frobenius norm */
263: /* NB --we are ignoring c */
264: PetscInt i, j, k, num, np = mfqP->nmodelpoints;
265: PetscReal one = 1.0, zero = 0.0, negone = -1.0;
266: PetscBLASInt blasnpmax, blasnplus1, blasnp, blasint, blasint2;
267: PetscBLASInt ione = 1;
268: PetscReal sqrt2 = PetscSqrtReal(2.0);
270: PetscFunctionBegin;
271: PetscCall(PetscBLASIntCast(mfqP->npmax, &blasnpmax));
272: PetscCall(PetscBLASIntCast(mfqP->n + 1, &blasnplus1));
273: PetscCall(PetscBLASIntCast(np, &blasnp));
274: PetscCall(PetscBLASIntCast(mfqP->n * (mfqP->n + 1) / 2, &blasint));
275: PetscCall(PetscBLASIntCast(np - mfqP->n - 1, &blasint2));
276: for (i = 0; i < mfqP->n * mfqP->m; i++) mfqP->Gdel[i] = 0;
277: for (i = 0; i < mfqP->n * mfqP->n * mfqP->m; i++) mfqP->Hdel[i] = 0;
279: /* factor M */
280: PetscCallLAPACKInfo("LAPACKgetrf", LAPACKgetrf_(&blasnplus1, &blasnp, mfqP->M, &blasnplus1, mfqP->npmaxiwork, &info));
282: if (np == mfqP->n + 1) {
283: for (i = 0; i < mfqP->npmax - mfqP->n - 1; i++) mfqP->omega[i] = 0.0;
284: for (i = 0; i < mfqP->n * (mfqP->n + 1) / 2; i++) mfqP->beta[i] = 0.0;
285: } else {
286: /* Let Ltmp = (L'*L) */
287: 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));
289: /* factor Ltmp */
290: PetscCallLAPACKInfo("LAPACKpotrf", LAPACKpotrf_("L", &blasint2, mfqP->L_tmp, &blasint, &info));
291: }
293: for (k = 0; k < mfqP->m; k++) {
294: if (np != mfqP->n + 1) {
295: /* Solve L'*L*Omega = Z' * RESk*/
296: PetscCallBLAS("BLASgemv", BLASgemv_("T", &blasnp, &blasint2, &one, mfqP->Z, &blasnpmax, &mfqP->RES[mfqP->npmax * k], &ione, &zero, mfqP->omega, &ione));
297: PetscCallLAPACKInfo("LAPACKpotrs", LAPACKpotrs_("L", &blasint2, &ione, mfqP->L_tmp, &blasint, mfqP->omega, &blasint2, &info));
299: /* Beta = L*Omega */
300: PetscCallBLAS("BLASgemv", BLASgemv_("N", &blasint, &blasint2, &one, &mfqP->L[(mfqP->n + 1) * blasint], &blasint, mfqP->omega, &ione, &zero, mfqP->beta, &ione));
301: }
303: /* solve M'*Alpha = RESk - N'*Beta */
304: PetscCallBLAS("BLASgemv", BLASgemv_("T", &blasint, &blasnp, &negone, mfqP->N, &blasint, mfqP->beta, &ione, &one, &mfqP->RES[mfqP->npmax * k], &ione));
305: PetscCallLAPACKInfo("LAPACKgetrs", LAPACKgetrs_("T", &blasnplus1, &ione, mfqP->M, &blasnplus1, mfqP->npmaxiwork, &mfqP->RES[mfqP->npmax * k], &blasnplus1, &info));
307: /* Gdel(:,k) = Alpha(2:n+1) */
308: for (i = 0; i < mfqP->n; i++) mfqP->Gdel[i + mfqP->n * k] = mfqP->RES[mfqP->npmax * k + i + 1];
310: /* Set Hdels */
311: num = 0;
312: for (i = 0; i < mfqP->n; i++) {
313: /* H[i,i,k] = Beta(num) */
314: mfqP->Hdel[(i * mfqP->n + i) * mfqP->m + k] = mfqP->beta[num];
315: num++;
316: for (j = i + 1; j < mfqP->n; j++) {
317: /* H[i,j,k] = H[j,i,k] = Beta(num)/sqrt(2) */
318: mfqP->Hdel[(j * mfqP->n + i) * mfqP->m + k] = mfqP->beta[num] / sqrt2;
319: mfqP->Hdel[(i * mfqP->n + j) * mfqP->m + k] = mfqP->beta[num] / sqrt2;
320: num++;
321: }
322: }
323: }
324: PetscFunctionReturn(PETSC_SUCCESS);
325: }
327: static PetscErrorCode morepoints(TAO_POUNDERS *mfqP)
328: {
329: /* Assumes mfqP->model_indices[0] is minimum index
330: Finishes adding points to mfqP->model_indices (up to npmax)
331: Computes L,Z,M,N
332: np is actual number of points in model (should equal npmax?) */
333: PetscInt point, i, j, offset;
334: PetscInt reject;
335: PetscBLASInt blasn, blasnpmax, blasnplus1, blasnmax, blasint, blasint2, blasnp, blasmaxmn;
336: const PetscReal *x;
337: PetscReal normd;
339: PetscFunctionBegin;
340: PetscCall(PetscBLASIntCast(mfqP->npmax, &blasnpmax));
341: PetscCall(PetscBLASIntCast(mfqP->n, &blasn));
342: PetscCall(PetscBLASIntCast(mfqP->nmax, &blasnmax));
343: PetscCall(PetscBLASIntCast(mfqP->n + 1, &blasnplus1));
344: PetscCall(PetscBLASIntCast(mfqP->n, &blasnp));
345: /* Initialize M,N */
346: for (i = 0; i < mfqP->n + 1; i++) {
347: PetscCall(VecGetArrayRead(mfqP->Xhist[mfqP->model_indices[i]], &x));
348: mfqP->M[(mfqP->n + 1) * i] = 1.0;
349: for (j = 0; j < mfqP->n; j++) mfqP->M[j + 1 + ((mfqP->n + 1) * i)] = (x[j] - mfqP->xmin[j]) / mfqP->delta;
350: PetscCall(VecRestoreArrayRead(mfqP->Xhist[mfqP->model_indices[i]], &x));
351: PetscCall(phi2eval(&mfqP->M[1 + ((mfqP->n + 1) * i)], mfqP->n, &mfqP->N[mfqP->n * (mfqP->n + 1) / 2 * i]));
352: }
354: /* Now we add points until we have npmax starting with the most recent ones */
355: point = mfqP->nHist - 1;
356: mfqP->nmodelpoints = mfqP->n + 1;
357: while (mfqP->nmodelpoints < mfqP->npmax && point >= 0) {
358: /* Reject any points already in the model */
359: reject = 0;
360: for (j = 0; j < mfqP->n + 1; j++) {
361: if (point == mfqP->model_indices[j]) {
362: reject = 1;
363: break;
364: }
365: }
367: /* Reject if norm(d) >c2 */
368: if (!reject) {
369: PetscCall(VecCopy(mfqP->Xhist[point], mfqP->workxvec));
370: PetscCall(VecAXPY(mfqP->workxvec, -1.0, mfqP->Xhist[mfqP->minindex]));
371: PetscCall(VecNorm(mfqP->workxvec, NORM_2, &normd));
372: normd /= mfqP->delta;
373: if (normd > mfqP->c2) reject = 1;
374: }
375: if (reject) {
376: point--;
377: continue;
378: }
380: PetscCall(VecGetArrayRead(mfqP->Xhist[point], &x));
381: mfqP->M[(mfqP->n + 1) * mfqP->nmodelpoints] = 1.0;
382: for (j = 0; j < mfqP->n; j++) mfqP->M[j + 1 + ((mfqP->n + 1) * mfqP->nmodelpoints)] = (x[j] - mfqP->xmin[j]) / mfqP->delta;
383: PetscCall(VecRestoreArrayRead(mfqP->Xhist[point], &x));
384: PetscCall(phi2eval(&mfqP->M[1 + (mfqP->n + 1) * mfqP->nmodelpoints], mfqP->n, &mfqP->N[mfqP->n * (mfqP->n + 1) / 2 * (mfqP->nmodelpoints)]));
386: /* Update QR factorization */
387: /* Copy M' to Q_tmp */
388: for (i = 0; i < mfqP->n + 1; i++) {
389: for (j = 0; j < mfqP->npmax; j++) mfqP->Q_tmp[j + mfqP->npmax * i] = mfqP->M[i + (mfqP->n + 1) * j];
390: }
391: PetscCall(PetscBLASIntCast(mfqP->nmodelpoints + 1, &blasnp));
392: /* Q_tmp,R = qr(M') */
393: PetscCall(PetscBLASIntCast(PetscMax(mfqP->m, mfqP->n + 1), &blasmaxmn));
394: PetscCallLAPACKInfo("LAPACKgeqrf", LAPACKgeqrf_(&blasnp, &blasnplus1, mfqP->Q_tmp, &blasnpmax, mfqP->tau_tmp, mfqP->mwork, &blasmaxmn, &info));
396: /* Reject if min(svd(N*Q(:,n+2:np+1)) <= theta2 */
397: /* L = N*Qtmp */
398: PetscCall(PetscBLASIntCast(mfqP->n * (mfqP->n + 1) / 2, &blasint2));
399: /* Copy N to L_tmp */
400: for (i = 0; i < mfqP->n * (mfqP->n + 1) / 2 * mfqP->npmax; i++) mfqP->L_tmp[i] = mfqP->N[i];
401: /* Copy L_save to L_tmp */
403: /* L_tmp = N*Qtmp' */
404: PetscCallLAPACKInfo("LAPACKormqr", LAPACKormqr_("R", "N", &blasint2, &blasnp, &blasnplus1, mfqP->Q_tmp, &blasnpmax, mfqP->tau_tmp, mfqP->L_tmp, &blasint2, mfqP->npmaxwork, &blasnmax, &info));
406: /* Copy L_tmp to L_save */
407: for (i = 0; i < mfqP->npmax * mfqP->n * (mfqP->n + 1) / 2; i++) mfqP->L_save[i] = mfqP->L_tmp[i];
409: /* Get svd for L_tmp(:,n+2:np+1) (L_tmp is modified in process) */
410: PetscCall(PetscBLASIntCast(mfqP->nmodelpoints - mfqP->n, &blasint));
411: PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
412: 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));
413: PetscCall(PetscFPTrapPop());
415: if (mfqP->beta[PetscMin(blasint, blasint2) - 1] > mfqP->theta2) {
416: /* accept point */
417: mfqP->model_indices[mfqP->nmodelpoints] = point;
418: /* Copy Q_tmp to Q */
419: for (i = 0; i < mfqP->npmax * mfqP->npmax; i++) mfqP->Q[i] = mfqP->Q_tmp[i];
420: for (i = 0; i < mfqP->npmax; i++) mfqP->tau[i] = mfqP->tau_tmp[i];
421: mfqP->nmodelpoints++;
422: PetscCall(PetscBLASIntCast(mfqP->nmodelpoints, &blasnp));
424: /* Copy L_save to L */
425: for (i = 0; i < mfqP->npmax * mfqP->n * (mfqP->n + 1) / 2; i++) mfqP->L[i] = mfqP->L_save[i];
426: }
427: point--;
428: }
430: PetscCall(PetscBLASIntCast(mfqP->nmodelpoints, &blasnp));
431: /* Copy Q(:,n+2:np) to Z */
432: /* First set Q_tmp to I */
433: for (i = 0; i < mfqP->npmax * mfqP->npmax; i++) mfqP->Q_tmp[i] = 0.0;
434: for (i = 0; i < mfqP->npmax; i++) mfqP->Q_tmp[i + mfqP->npmax * i] = 1.0;
436: /* Q_tmp = I * Q */
437: PetscCallLAPACKInfo("LAPACKormqr", LAPACKormqr_("R", "N", &blasnp, &blasnp, &blasnplus1, mfqP->Q, &blasnpmax, mfqP->tau, mfqP->Q_tmp, &blasnpmax, mfqP->npmaxwork, &blasnmax, &info));
439: /* Copy Q_tmp(:,n+2:np) to Z) */
440: offset = mfqP->npmax * (mfqP->n + 1);
441: for (i = offset; i < mfqP->npmax * mfqP->npmax; i++) mfqP->Z[i - offset] = mfqP->Q_tmp[i];
443: if (mfqP->nmodelpoints == mfqP->n + 1) {
444: /* Set L to I_{n+1} */
445: for (i = 0; i < mfqP->npmax * mfqP->n * (mfqP->n + 1) / 2; i++) mfqP->L[i] = 0.0;
446: for (i = 0; i < mfqP->n; i++) mfqP->L[(mfqP->n * (mfqP->n + 1) / 2) * i + i] = 1.0;
447: }
448: PetscFunctionReturn(PETSC_SUCCESS);
449: }
451: /* Only call from modelimprove, addpoint() needs ->Q_tmp and ->work to be set */
452: static PetscErrorCode addpoint(Tao tao, TAO_POUNDERS *mfqP, PetscInt index)
453: {
454: PetscFunctionBegin;
455: /* Create new vector in history: X[newidx] = X[mfqP->index] + delta*X[index]*/
456: PetscCall(VecDuplicate(mfqP->Xhist[0], &mfqP->Xhist[mfqP->nHist]));
457: PetscCall(VecSetValues(mfqP->Xhist[mfqP->nHist], mfqP->n, mfqP->indices, &mfqP->Q_tmp[index * mfqP->npmax], INSERT_VALUES));
458: PetscCall(VecAssemblyBegin(mfqP->Xhist[mfqP->nHist]));
459: PetscCall(VecAssemblyEnd(mfqP->Xhist[mfqP->nHist]));
460: PetscCall(VecAYPX(mfqP->Xhist[mfqP->nHist], mfqP->delta, mfqP->Xhist[mfqP->minindex]));
462: /* Project into feasible region */
463: if (tao->XU && tao->XL) PetscCall(VecMedian(mfqP->Xhist[mfqP->nHist], tao->XL, tao->XU, mfqP->Xhist[mfqP->nHist]));
465: /* Compute value of new vector */
466: PetscCall(VecDuplicate(mfqP->Fhist[0], &mfqP->Fhist[mfqP->nHist]));
467: CHKMEMQ;
468: PetscCall(pounders_feval(tao, mfqP->Xhist[mfqP->nHist], mfqP->Fhist[mfqP->nHist], &mfqP->Fres[mfqP->nHist]));
470: /* Add new vector to model */
471: mfqP->model_indices[mfqP->nmodelpoints] = mfqP->nHist;
472: mfqP->nmodelpoints++;
473: mfqP->nHist++;
474: PetscFunctionReturn(PETSC_SUCCESS);
475: }
477: static PetscErrorCode modelimprove(Tao tao, TAO_POUNDERS *mfqP, PetscInt addallpoints)
478: {
479: /* modeld = Q(:,np+1:n)' */
480: PetscInt i, j, minindex = 0;
481: PetscReal dp, half = 0.5, one = 1.0, minvalue = PETSC_INFINITY;
482: PetscBLASInt blasn, blasnpmax, blask;
483: PetscBLASInt blas1 = 1, blasnmax;
485: PetscFunctionBegin;
486: PetscCall(PetscBLASIntCast(mfqP->n, &blasn));
487: PetscCall(PetscBLASIntCast(mfqP->npmax, &blasnpmax));
488: PetscCall(PetscBLASIntCast(mfqP->nmodelpoints, &blask));
489: PetscCall(PetscBLASIntCast(mfqP->nmax, &blasnmax));
491: /* Qtmp = I(n x n) */
492: for (i = 0; i < mfqP->n; i++) {
493: for (j = 0; j < mfqP->n; j++) mfqP->Q_tmp[i + mfqP->npmax * j] = 0.0;
494: }
495: for (j = 0; j < mfqP->n; j++) mfqP->Q_tmp[j + mfqP->npmax * j] = 1.0;
497: /* Qtmp = Q * I */
498: PetscCallLAPACKInfo("LAPACKormqr", LAPACKormqr_("R", "N", &blasn, &blasn, &blask, mfqP->Q, &blasnpmax, mfqP->tau, mfqP->Q_tmp, &blasnpmax, mfqP->npmaxwork, &blasnmax, &info));
500: for (i = mfqP->nmodelpoints; i < mfqP->n; i++) {
501: PetscCallBLAS("BLASdot", dp = BLASdot_(&blasn, &mfqP->Q_tmp[i * mfqP->npmax], &blas1, mfqP->Gres, &blas1));
502: if (dp > 0.0) { /* Model says use the other direction! */
503: for (j = 0; j < mfqP->n; j++) mfqP->Q_tmp[i * mfqP->npmax + j] *= -1;
504: }
505: /* mfqP->work[i] = Cres+Modeld(i,:)*(Gres+.5*Hres*Modeld(i,:)') */
506: for (j = 0; j < mfqP->n; j++) mfqP->work2[j] = mfqP->Gres[j];
507: PetscCallBLAS("BLASgemv", BLASgemv_("N", &blasn, &blasn, &half, mfqP->Hres, &blasn, &mfqP->Q_tmp[i * mfqP->npmax], &blas1, &one, mfqP->work2, &blas1));
508: PetscCallBLAS("BLASdot", mfqP->work[i] = BLASdot_(&blasn, &mfqP->Q_tmp[i * mfqP->npmax], &blas1, mfqP->work2, &blas1));
509: if (i == mfqP->nmodelpoints || mfqP->work[i] < minvalue) {
510: minindex = i;
511: minvalue = mfqP->work[i];
512: }
513: if (addallpoints != 0) PetscCall(addpoint(tao, mfqP, i));
514: }
515: if (!addallpoints) PetscCall(addpoint(tao, mfqP, minindex));
516: PetscFunctionReturn(PETSC_SUCCESS);
517: }
519: static PetscErrorCode affpoints(TAO_POUNDERS *mfqP, PetscReal *xmin, PetscReal c)
520: {
521: PetscInt i, j;
522: PetscBLASInt blasm, blasj, blask, blasn, ione = 1;
523: PetscBLASInt blasnpmax, blasmaxmn;
524: PetscReal proj, normd;
525: const PetscReal *x;
527: PetscFunctionBegin;
528: PetscCall(PetscBLASIntCast(mfqP->npmax, &blasnpmax));
529: PetscCall(PetscBLASIntCast(mfqP->m, &blasm));
530: PetscCall(PetscBLASIntCast(mfqP->n, &blasn));
531: for (i = mfqP->nHist - 1; i >= 0; i--) {
532: PetscCall(VecGetArrayRead(mfqP->Xhist[i], &x));
533: for (j = 0; j < mfqP->n; j++) mfqP->work[j] = (x[j] - xmin[j]) / mfqP->delta;
534: PetscCall(VecRestoreArrayRead(mfqP->Xhist[i], &x));
535: PetscCallBLAS("BLAScopy", BLAScopy_(&blasn, mfqP->work, &ione, mfqP->work2, &ione));
536: PetscCallBLAS("BLASnrm2", normd = BLASnrm2_(&blasn, mfqP->work, &ione));
537: if (normd <= c) {
538: PetscCall(PetscBLASIntCast(PetscMax(mfqP->n - mfqP->nmodelpoints, 0), &blasj));
539: if (!mfqP->q_is_I) {
540: /* project D onto null */
541: PetscCall(PetscBLASIntCast(mfqP->nmodelpoints, &blask));
542: PetscCallLAPACKInfo("LAPACKormqr", LAPACKormqr_("R", "N", &ione, &blasn, &blask, mfqP->Q, &blasnpmax, mfqP->tau, mfqP->work2, &ione, mfqP->mwork, &blasm, &info));
543: }
544: PetscCallBLAS("BLASnrm2", proj = BLASnrm2_(&blasj, &mfqP->work2[mfqP->nmodelpoints], &ione));
546: if (proj >= mfqP->theta1) { /* add this index to model */
547: mfqP->model_indices[mfqP->nmodelpoints] = i;
548: mfqP->nmodelpoints++;
549: PetscCallBLAS("BLAScopy", BLAScopy_(&blasn, mfqP->work, &ione, &mfqP->Q_tmp[mfqP->npmax * (mfqP->nmodelpoints - 1)], &ione));
550: PetscCall(PetscBLASIntCast(mfqP->npmax * (mfqP->nmodelpoints), &blask));
551: PetscCallBLAS("BLAScopy", BLAScopy_(&blask, mfqP->Q_tmp, &ione, mfqP->Q, &ione));
552: PetscCall(PetscBLASIntCast(mfqP->nmodelpoints, &blask));
553: PetscCall(PetscBLASIntCast(PetscMax(mfqP->m, mfqP->n), &blasmaxmn));
554: PetscCallLAPACKInfo("LAPACKgeqrf", LAPACKgeqrf_(&blasn, &blask, mfqP->Q, &blasnpmax, mfqP->tau, mfqP->mwork, &blasmaxmn, &info));
555: mfqP->q_is_I = 0;
556: }
557: if (mfqP->nmodelpoints == mfqP->n) break;
558: }
559: }
560: PetscFunctionReturn(PETSC_SUCCESS);
561: }
563: static PetscErrorCode TaoSolve_POUNDERS(Tao tao)
564: {
565: TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;
566: PetscInt i, ii, j, k, l;
567: PetscReal step = 1.0;
568: PetscInt low, high;
569: PetscReal minnorm;
570: PetscReal *x, *f;
571: const PetscReal *xmint, *fmin;
572: PetscReal deltaold;
573: PetscReal gnorm;
574: PetscBLASInt info, ione = 1, iblas;
575: PetscBool valid, same;
576: PetscReal mdec, rho, normxsp;
577: PetscReal one = 1.0, zero = 0.0, ratio;
578: PetscBLASInt blasm, blasn, blasncopy, blasnpmax;
579: static PetscBool set = PETSC_FALSE;
581: /* n = # of parameters
582: m = dimension (components) of function */
583: PetscFunctionBegin;
584: PetscCall(PetscCitationsRegister("@article{UNEDF0,\n"
585: "title = {Nuclear energy density optimization},\n"
586: "author = {Kortelainen, M. and Lesinski, T. and Mor\'e, J. and Nazarewicz, W.\n"
587: " and Sarich, J. and Schunck, N. and Stoitsov, M. V. and Wild, S. },\n"
588: "journal = {Phys. Rev. C},\n"
589: "volume = {82},\n"
590: "number = {2},\n"
591: "pages = {024313},\n"
592: "numpages = {18},\n"
593: "year = {2010},\n"
594: "month = {Aug},\n"
595: "doi = {10.1103/PhysRevC.82.024313}\n}\n",
596: &set));
597: tao->niter = 0;
598: if (tao->XL && tao->XU) {
599: /* Check x0 <= XU */
600: PetscReal val;
602: PetscCall(VecCopy(tao->solution, mfqP->Xhist[0]));
603: PetscCall(VecAXPY(mfqP->Xhist[0], -1.0, tao->XU));
604: PetscCall(VecMax(mfqP->Xhist[0], NULL, &val));
605: PetscCheck(val <= 1e-10, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "X0 > upper bound");
607: /* Check x0 >= xl */
608: PetscCall(VecCopy(tao->XL, mfqP->Xhist[0]));
609: PetscCall(VecAXPY(mfqP->Xhist[0], -1.0, tao->solution));
610: PetscCall(VecMax(mfqP->Xhist[0], NULL, &val));
611: PetscCheck(val <= 1e-10, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "X0 < lower bound");
613: /* Check x0 + delta < XU -- should be able to get around this eventually */
615: PetscCall(VecSet(mfqP->Xhist[0], mfqP->delta));
616: PetscCall(VecAXPY(mfqP->Xhist[0], 1.0, tao->solution));
617: PetscCall(VecAXPY(mfqP->Xhist[0], -1.0, tao->XU));
618: PetscCall(VecMax(mfqP->Xhist[0], NULL, &val));
619: PetscCheck(val <= 1e-10, PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_OUTOFRANGE, "X0 + delta > upper bound");
620: }
622: PetscCall(PetscBLASIntCast(mfqP->m, &blasm));
623: PetscCall(PetscBLASIntCast(mfqP->n, &blasn));
624: PetscCall(PetscBLASIntCast(mfqP->npmax, &blasnpmax));
625: for (i = 0; i < mfqP->n * mfqP->n * mfqP->m; ++i) mfqP->H[i] = 0;
627: PetscCall(VecCopy(tao->solution, mfqP->Xhist[0]));
629: /* This provides enough information to approximate the gradient of the objective */
630: /* using a forward difference scheme. */
632: PetscCall(PetscInfo(tao, "Initialize simplex; delta = %10.9e\n", (double)mfqP->delta));
633: PetscCall(pounders_feval(tao, mfqP->Xhist[0], mfqP->Fhist[0], &mfqP->Fres[0]));
634: mfqP->minindex = 0;
635: minnorm = mfqP->Fres[0];
637: PetscCall(VecGetOwnershipRange(mfqP->Xhist[0], &low, &high));
638: for (i = 1; i < mfqP->n + 1; ++i) {
639: PetscCall(VecCopy(mfqP->Xhist[0], mfqP->Xhist[i]));
641: if (i - 1 >= low && i - 1 < high) {
642: PetscCall(VecGetArray(mfqP->Xhist[i], &x));
643: x[i - 1 - low] += mfqP->delta;
644: PetscCall(VecRestoreArray(mfqP->Xhist[i], &x));
645: }
646: CHKMEMQ;
647: PetscCall(pounders_feval(tao, mfqP->Xhist[i], mfqP->Fhist[i], &mfqP->Fres[i]));
648: if (mfqP->Fres[i] < minnorm) {
649: mfqP->minindex = i;
650: minnorm = mfqP->Fres[i];
651: }
652: }
653: PetscCall(VecCopy(mfqP->Xhist[mfqP->minindex], tao->solution));
654: PetscCall(VecCopy(mfqP->Fhist[mfqP->minindex], tao->ls_res));
655: PetscCall(PetscInfo(tao, "Finalize simplex; minnorm = %10.9e\n", (double)minnorm));
657: /* Gather mpi vecs to one big local vec */
659: /* Begin serial code */
661: /* Disp[i] = Xi-xmin, i=1,..,mfqP->minindex-1,mfqP->minindex+1,..,n */
662: /* Fdiff[i] = (Fi-Fmin)', i=1,..,mfqP->minindex-1,mfqP->minindex+1,..,n */
663: /* (Column oriented for blas calls) */
664: ii = 0;
666: PetscCall(PetscInfo(tao, "Build matrix: %d\n", mfqP->size));
667: if (1 == mfqP->size) {
668: PetscCall(VecGetArrayRead(mfqP->Xhist[mfqP->minindex], &xmint));
669: for (i = 0; i < mfqP->n; i++) mfqP->xmin[i] = xmint[i];
670: PetscCall(VecRestoreArrayRead(mfqP->Xhist[mfqP->minindex], &xmint));
671: PetscCall(VecGetArrayRead(mfqP->Fhist[mfqP->minindex], &fmin));
672: for (i = 0; i < mfqP->n + 1; i++) {
673: if (i == mfqP->minindex) continue;
675: PetscCall(VecGetArray(mfqP->Xhist[i], &x));
676: for (j = 0; j < mfqP->n; j++) mfqP->Disp[ii + mfqP->npmax * j] = (x[j] - mfqP->xmin[j]) / mfqP->delta;
677: PetscCall(VecRestoreArray(mfqP->Xhist[i], &x));
679: PetscCall(VecGetArray(mfqP->Fhist[i], &f));
680: for (j = 0; j < mfqP->m; j++) mfqP->Fdiff[ii + mfqP->n * j] = f[j] - fmin[j];
681: PetscCall(VecRestoreArray(mfqP->Fhist[i], &f));
683: mfqP->model_indices[ii++] = i;
684: }
685: for (j = 0; j < mfqP->m; j++) mfqP->C[j] = fmin[j];
686: PetscCall(VecRestoreArrayRead(mfqP->Fhist[mfqP->minindex], &fmin));
687: } else {
688: PetscCall(VecSet(mfqP->localxmin, 0));
689: PetscCall(VecScatterBegin(mfqP->scatterx, mfqP->Xhist[mfqP->minindex], mfqP->localxmin, INSERT_VALUES, SCATTER_FORWARD));
690: PetscCall(VecScatterEnd(mfqP->scatterx, mfqP->Xhist[mfqP->minindex], mfqP->localxmin, INSERT_VALUES, SCATTER_FORWARD));
692: PetscCall(VecGetArrayRead(mfqP->localxmin, &xmint));
693: for (i = 0; i < mfqP->n; i++) mfqP->xmin[i] = xmint[i];
694: PetscCall(VecRestoreArrayRead(mfqP->localxmin, &xmint));
696: PetscCall(VecScatterBegin(mfqP->scatterf, mfqP->Fhist[mfqP->minindex], mfqP->localfmin, INSERT_VALUES, SCATTER_FORWARD));
697: PetscCall(VecScatterEnd(mfqP->scatterf, mfqP->Fhist[mfqP->minindex], mfqP->localfmin, INSERT_VALUES, SCATTER_FORWARD));
698: PetscCall(VecGetArrayRead(mfqP->localfmin, &fmin));
699: for (i = 0; i < mfqP->n + 1; i++) {
700: if (i == mfqP->minindex) continue;
702: PetscCall(VecScatterBegin(mfqP->scatterx, mfqP->Xhist[ii], mfqP->localx, INSERT_VALUES, SCATTER_FORWARD));
703: PetscCall(VecScatterEnd(mfqP->scatterx, mfqP->Xhist[ii], mfqP->localx, INSERT_VALUES, SCATTER_FORWARD));
704: PetscCall(VecGetArray(mfqP->localx, &x));
705: for (j = 0; j < mfqP->n; j++) mfqP->Disp[ii + mfqP->npmax * j] = (x[j] - mfqP->xmin[j]) / mfqP->delta;
706: PetscCall(VecRestoreArray(mfqP->localx, &x));
708: PetscCall(VecScatterBegin(mfqP->scatterf, mfqP->Fhist[ii], mfqP->localf, INSERT_VALUES, SCATTER_FORWARD));
709: PetscCall(VecScatterEnd(mfqP->scatterf, mfqP->Fhist[ii], mfqP->localf, INSERT_VALUES, SCATTER_FORWARD));
710: PetscCall(VecGetArray(mfqP->localf, &f));
711: for (j = 0; j < mfqP->m; j++) mfqP->Fdiff[ii + mfqP->n * j] = f[j] - fmin[j];
712: PetscCall(VecRestoreArray(mfqP->localf, &f));
714: mfqP->model_indices[ii++] = i;
715: }
716: for (j = 0; j < mfqP->m; j++) mfqP->C[j] = fmin[j];
717: PetscCall(VecRestoreArrayRead(mfqP->localfmin, &fmin));
718: }
720: /* Determine the initial quadratic models */
721: /* G = D(ModelIn,:) \ (F(ModelIn,1:m)-repmat(F(xkin,1:m),n,1)); */
722: /* D (nxn) Fdiff (nxm) => G (nxm) */
723: blasncopy = blasn;
724: PetscCallBLAS("LAPACKgesv", LAPACKgesv_(&blasn, &blasm, mfqP->Disp, &blasnpmax, mfqP->iwork, mfqP->Fdiff, &blasncopy, &info));
725: PetscCall(PetscInfo(tao, "Linear solve return: %" PetscBLASInt_FMT "\n", info));
727: PetscCall(pounders_update_res(tao));
729: valid = PETSC_TRUE;
731: PetscCall(VecSetValues(tao->gradient, mfqP->n, mfqP->indices, mfqP->Gres, INSERT_VALUES));
732: PetscCall(VecAssemblyBegin(tao->gradient));
733: PetscCall(VecAssemblyEnd(tao->gradient));
734: PetscCall(VecNorm(tao->gradient, NORM_2, &gnorm));
735: gnorm *= mfqP->delta;
736: PetscCall(VecCopy(mfqP->Xhist[mfqP->minindex], tao->solution));
738: tao->reason = TAO_CONTINUE_ITERATING;
739: PetscCall(TaoLogConvergenceHistory(tao, minnorm, gnorm, 0.0, tao->ksp_its));
740: PetscCall(TaoMonitor(tao, tao->niter, minnorm, gnorm, 0.0, step));
741: PetscUseTypeMethod(tao, convergencetest, tao->cnvP);
743: mfqP->nHist = mfqP->n + 1;
744: mfqP->nmodelpoints = mfqP->n + 1;
745: PetscCall(PetscInfo(tao, "Initial gradient: %20.19e\n", (double)gnorm));
747: while (tao->reason == TAO_CONTINUE_ITERATING) {
748: PetscReal gnm = 1e-4;
749: /* Call general purpose update function */
750: PetscTryTypeMethod(tao, update, tao->niter, tao->user_update);
751: tao->niter++;
752: /* Solve the subproblem min{Q(s): ||s|| <= 1.0} */
753: PetscCall(gqtwrap(tao, &gnm, &mdec));
754: /* Evaluate the function at the new point */
756: for (i = 0; i < mfqP->n; i++) mfqP->work[i] = mfqP->Xsubproblem[i] * mfqP->delta + mfqP->xmin[i];
757: PetscCall(VecDuplicate(tao->solution, &mfqP->Xhist[mfqP->nHist]));
758: PetscCall(VecDuplicate(tao->ls_res, &mfqP->Fhist[mfqP->nHist]));
759: PetscCall(VecSetValues(mfqP->Xhist[mfqP->nHist], mfqP->n, mfqP->indices, mfqP->work, INSERT_VALUES));
760: PetscCall(VecAssemblyBegin(mfqP->Xhist[mfqP->nHist]));
761: PetscCall(VecAssemblyEnd(mfqP->Xhist[mfqP->nHist]));
763: PetscCall(pounders_feval(tao, mfqP->Xhist[mfqP->nHist], mfqP->Fhist[mfqP->nHist], &mfqP->Fres[mfqP->nHist]));
765: rho = (mfqP->Fres[mfqP->minindex] - mfqP->Fres[mfqP->nHist]) / mdec;
766: mfqP->nHist++;
768: /* Update the center */
769: if ((rho >= mfqP->eta1) || (rho > mfqP->eta0 && valid == PETSC_TRUE)) {
770: /* Update model to reflect new base point */
771: for (i = 0; i < mfqP->n; i++) mfqP->work[i] = (mfqP->work[i] - mfqP->xmin[i]) / mfqP->delta;
772: for (j = 0; j < mfqP->m; j++) {
773: /* C(j) = C(j) + work*G(:,j) + .5*work*H(:,:,j)*work';
774: G(:,j) = G(:,j) + H(:,:,j)*work' */
775: for (k = 0; k < mfqP->n; k++) {
776: mfqP->work2[k] = 0.0;
777: for (l = 0; l < mfqP->n; l++) mfqP->work2[k] += mfqP->H[j + mfqP->m * (k + l * mfqP->n)] * mfqP->work[l];
778: }
779: for (i = 0; i < mfqP->n; i++) {
780: mfqP->C[j] += mfqP->work[i] * (mfqP->Fdiff[i + mfqP->n * j] + 0.5 * mfqP->work2[i]);
781: mfqP->Fdiff[i + mfqP->n * j] += mfqP->work2[i];
782: }
783: }
784: /* Cres += work*Gres + .5*work*Hres*work';
785: Gres += Hres*work'; */
787: PetscCallBLAS("BLASgemv", BLASgemv_("N", &blasn, &blasn, &one, mfqP->Hres, &blasn, mfqP->work, &ione, &zero, mfqP->work2, &ione));
788: for (i = 0; i < mfqP->n; i++) mfqP->Gres[i] += mfqP->work2[i];
789: mfqP->minindex = mfqP->nHist - 1;
790: minnorm = mfqP->Fres[mfqP->minindex];
791: PetscCall(VecCopy(mfqP->Fhist[mfqP->minindex], tao->ls_res));
792: /* Change current center */
793: PetscCall(VecGetArrayRead(mfqP->Xhist[mfqP->minindex], &xmint));
794: for (i = 0; i < mfqP->n; i++) mfqP->xmin[i] = xmint[i];
795: PetscCall(VecRestoreArrayRead(mfqP->Xhist[mfqP->minindex], &xmint));
796: }
798: /* Evaluate at a model-improving point if necessary */
799: if (valid == PETSC_FALSE) {
800: mfqP->q_is_I = 1;
801: mfqP->nmodelpoints = 0;
802: PetscCall(affpoints(mfqP, mfqP->xmin, mfqP->c1));
803: if (mfqP->nmodelpoints < mfqP->n) {
804: PetscCall(PetscInfo(tao, "Model not valid -- model-improving\n"));
805: PetscCall(modelimprove(tao, mfqP, 1));
806: }
807: }
809: /* Update the trust region radius */
810: deltaold = mfqP->delta;
811: normxsp = 0;
812: for (i = 0; i < mfqP->n; i++) normxsp += mfqP->Xsubproblem[i] * mfqP->Xsubproblem[i];
813: normxsp = PetscSqrtReal(normxsp);
814: if (rho >= mfqP->eta1 && normxsp > 0.5 * mfqP->delta) {
815: mfqP->delta = PetscMin(mfqP->delta * mfqP->gamma1, mfqP->deltamax);
816: } else if (valid == PETSC_TRUE) {
817: mfqP->delta = PetscMax(mfqP->delta * mfqP->gamma0, mfqP->deltamin);
818: }
820: /* Compute the next interpolation set */
821: mfqP->q_is_I = 1;
822: mfqP->nmodelpoints = 0;
823: PetscCall(PetscInfo(tao, "Affine Points: xmin = %20.19e, c1 = %20.19e\n", (double)*mfqP->xmin, (double)mfqP->c1));
824: PetscCall(affpoints(mfqP, mfqP->xmin, mfqP->c1));
825: if (mfqP->nmodelpoints == mfqP->n) {
826: valid = PETSC_TRUE;
827: } else {
828: valid = PETSC_FALSE;
829: PetscCall(PetscInfo(tao, "Affine Points: xmin = %20.19e, c2 = %20.19e\n", (double)*mfqP->xmin, (double)mfqP->c2));
830: PetscCall(affpoints(mfqP, mfqP->xmin, mfqP->c2));
831: if (mfqP->n > mfqP->nmodelpoints) {
832: PetscCall(PetscInfo(tao, "Model not valid -- adding geometry points\n"));
833: PetscCall(modelimprove(tao, mfqP, mfqP->n - mfqP->nmodelpoints));
834: }
835: }
836: for (i = mfqP->nmodelpoints; i > 0; i--) mfqP->model_indices[i] = mfqP->model_indices[i - 1];
837: mfqP->nmodelpoints++;
838: mfqP->model_indices[0] = mfqP->minindex;
839: PetscCall(morepoints(mfqP));
840: for (i = 0; i < mfqP->nmodelpoints; i++) {
841: PetscCall(VecGetArray(mfqP->Xhist[mfqP->model_indices[i]], &x));
842: for (j = 0; j < mfqP->n; j++) mfqP->Disp[i + mfqP->npmax * j] = (x[j] - mfqP->xmin[j]) / deltaold;
843: PetscCall(VecRestoreArray(mfqP->Xhist[mfqP->model_indices[i]], &x));
844: PetscCall(VecGetArray(mfqP->Fhist[mfqP->model_indices[i]], &f));
845: for (j = 0; j < mfqP->m; j++) {
846: for (k = 0; k < mfqP->n; k++) {
847: mfqP->work[k] = 0.0;
848: 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];
849: }
850: 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]);
851: }
852: PetscCall(VecRestoreArray(mfqP->Fhist[mfqP->model_indices[i]], &f));
853: }
855: /* Update the quadratic model */
856: PetscCall(PetscInfo(tao, "Get Quad, size: %" PetscInt_FMT ", points: %" PetscInt_FMT "\n", mfqP->n, mfqP->nmodelpoints));
857: PetscCall(getquadpounders(mfqP));
858: PetscCall(VecGetArrayRead(mfqP->Fhist[mfqP->minindex], &fmin));
859: PetscCallBLAS("BLAScopy", BLAScopy_(&blasm, fmin, &ione, mfqP->C, &ione));
860: /* G = G*(delta/deltaold) + Gdel */
861: ratio = mfqP->delta / deltaold;
862: iblas = blasm * blasn;
863: PetscCallBLAS("BLASscal", BLASscal_(&iblas, &ratio, mfqP->Fdiff, &ione));
864: PetscCallBLAS("BLASaxpy", BLASaxpy_(&iblas, &one, mfqP->Gdel, &ione, mfqP->Fdiff, &ione));
865: /* H = H*(delta/deltaold)^2 + Hdel */
866: iblas = blasm * blasn * blasn;
867: ratio *= ratio;
868: PetscCallBLAS("BLASscal", BLASscal_(&iblas, &ratio, mfqP->H, &ione));
869: PetscCallBLAS("BLASaxpy", BLASaxpy_(&iblas, &one, mfqP->Hdel, &ione, mfqP->H, &ione));
871: /* Get residuals */
872: PetscCall(pounders_update_res(tao));
874: /* Export solution and gradient residual to TAO */
875: PetscCall(VecCopy(mfqP->Xhist[mfqP->minindex], tao->solution));
876: PetscCall(VecSetValues(tao->gradient, mfqP->n, mfqP->indices, mfqP->Gres, INSERT_VALUES));
877: PetscCall(VecAssemblyBegin(tao->gradient));
878: PetscCall(VecAssemblyEnd(tao->gradient));
879: PetscCall(VecNorm(tao->gradient, NORM_2, &gnorm));
880: gnorm *= mfqP->delta;
881: /* final criticality test */
882: PetscCall(TaoLogConvergenceHistory(tao, minnorm, gnorm, 0.0, tao->ksp_its));
883: PetscCall(TaoMonitor(tao, tao->niter, minnorm, gnorm, 0.0, step));
884: PetscUseTypeMethod(tao, convergencetest, tao->cnvP);
885: /* test for repeated model */
886: if (mfqP->nmodelpoints == mfqP->last_nmodelpoints) {
887: same = PETSC_TRUE;
888: } else {
889: same = PETSC_FALSE;
890: }
891: for (i = 0; i < mfqP->nmodelpoints; i++) {
892: if (same) {
893: if (mfqP->model_indices[i] == mfqP->last_model_indices[i]) {
894: same = PETSC_TRUE;
895: } else {
896: same = PETSC_FALSE;
897: }
898: }
899: mfqP->last_model_indices[i] = mfqP->model_indices[i];
900: }
901: mfqP->last_nmodelpoints = mfqP->nmodelpoints;
902: if (same && mfqP->delta == deltaold) {
903: PetscCall(PetscInfo(tao, "Identical model used in successive iterations\n"));
904: tao->reason = TAO_CONVERGED_STEPTOL;
905: }
906: }
907: PetscFunctionReturn(PETSC_SUCCESS);
908: }
910: static PetscErrorCode TaoSetUp_POUNDERS(Tao tao)
911: {
912: TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;
913: IS isfloc, isfglob, isxloc, isxglob;
915: PetscFunctionBegin;
916: if (!tao->gradient) PetscCall(VecDuplicate(tao->solution, &tao->gradient));
917: if (!tao->stepdirection) PetscCall(VecDuplicate(tao->solution, &tao->stepdirection));
918: PetscCall(VecGetSize(tao->solution, &mfqP->n));
919: PetscCall(VecGetSize(tao->ls_res, &mfqP->m));
920: mfqP->c1 = PetscSqrtReal((PetscReal)mfqP->n);
921: if (mfqP->npmax == PETSC_CURRENT) mfqP->npmax = 2 * mfqP->n + 1;
922: mfqP->npmax = PetscMin((mfqP->n + 1) * (mfqP->n + 2) / 2, mfqP->npmax);
923: mfqP->npmax = PetscMax(mfqP->npmax, mfqP->n + 2);
925: PetscCall(PetscMalloc1(tao->max_funcs + 100, &mfqP->Xhist));
926: PetscCall(PetscMalloc1(tao->max_funcs + 100, &mfqP->Fhist));
927: for (PetscInt i = 0; i < mfqP->n + 1; i++) {
928: PetscCall(VecDuplicate(tao->solution, &mfqP->Xhist[i]));
929: PetscCall(VecDuplicate(tao->ls_res, &mfqP->Fhist[i]));
930: }
931: PetscCall(VecDuplicate(tao->solution, &mfqP->workxvec));
932: PetscCall(VecDuplicate(tao->ls_res, &mfqP->workfvec));
933: mfqP->nHist = 0;
935: PetscCall(PetscMalloc1(tao->max_funcs + 100, &mfqP->Fres));
936: PetscCall(PetscMalloc1(mfqP->npmax * mfqP->m, &mfqP->RES));
937: PetscCall(PetscMalloc1(mfqP->n, &mfqP->work));
938: PetscCall(PetscMalloc1(mfqP->n, &mfqP->work2));
939: PetscCall(PetscMalloc1(mfqP->n, &mfqP->work3));
940: PetscCall(PetscMalloc1(PetscMax(mfqP->m, mfqP->n + 1), &mfqP->mwork));
941: PetscCall(PetscMalloc1(mfqP->npmax - mfqP->n - 1, &mfqP->omega));
942: PetscCall(PetscMalloc1(mfqP->n * (mfqP->n + 1) / 2, &mfqP->beta));
943: PetscCall(PetscMalloc1(mfqP->n + 1, &mfqP->alpha));
945: PetscCall(PetscMalloc1(mfqP->n * mfqP->n * mfqP->m, &mfqP->H));
946: PetscCall(PetscMalloc1(mfqP->npmax * mfqP->npmax, &mfqP->Q));
947: PetscCall(PetscMalloc1(mfqP->npmax * mfqP->npmax, &mfqP->Q_tmp));
948: PetscCall(PetscMalloc1(mfqP->n * (mfqP->n + 1) / 2 * (mfqP->npmax), &mfqP->L));
949: PetscCall(PetscMalloc1(mfqP->n * (mfqP->n + 1) / 2 * (mfqP->npmax), &mfqP->L_tmp));
950: PetscCall(PetscMalloc1(mfqP->n * (mfqP->n + 1) / 2 * (mfqP->npmax), &mfqP->L_save));
951: PetscCall(PetscMalloc1(mfqP->n * (mfqP->n + 1) / 2 * (mfqP->npmax), &mfqP->N));
952: PetscCall(PetscMalloc1(mfqP->npmax * (mfqP->n + 1), &mfqP->M));
953: PetscCall(PetscMalloc1(mfqP->npmax * (mfqP->npmax - mfqP->n - 1), &mfqP->Z));
954: PetscCall(PetscMalloc1(mfqP->npmax, &mfqP->tau));
955: PetscCall(PetscMalloc1(mfqP->npmax, &mfqP->tau_tmp));
956: mfqP->nmax = PetscMax(5 * mfqP->npmax, mfqP->n * (mfqP->n + 1) / 2);
957: PetscCall(PetscMalloc1(mfqP->nmax, &mfqP->npmaxwork));
958: PetscCall(PetscMalloc1(mfqP->nmax, &mfqP->npmaxiwork));
959: PetscCall(PetscMalloc1(mfqP->n, &mfqP->xmin));
960: PetscCall(PetscMalloc1(mfqP->m, &mfqP->C));
961: PetscCall(PetscMalloc1(mfqP->m * mfqP->n, &mfqP->Fdiff));
962: PetscCall(PetscMalloc1(mfqP->npmax * mfqP->n, &mfqP->Disp));
963: PetscCall(PetscMalloc1(mfqP->n, &mfqP->Gres));
964: PetscCall(PetscMalloc1(mfqP->n * mfqP->n, &mfqP->Hres));
965: PetscCall(PetscMalloc1(mfqP->n * mfqP->n, &mfqP->Gpoints));
966: PetscCall(PetscMalloc1(mfqP->npmax, &mfqP->model_indices));
967: PetscCall(PetscMalloc1(mfqP->npmax, &mfqP->last_model_indices));
968: PetscCall(PetscMalloc1(mfqP->n, &mfqP->Xsubproblem));
969: PetscCall(PetscMalloc1(mfqP->m * mfqP->n, &mfqP->Gdel));
970: PetscCall(PetscMalloc1(mfqP->n * mfqP->n * mfqP->m, &mfqP->Hdel));
971: PetscCall(PetscMalloc1(PetscMax(mfqP->m, mfqP->n), &mfqP->indices));
972: PetscCall(PetscMalloc1(mfqP->n, &mfqP->iwork));
973: PetscCall(PetscMalloc1(mfqP->m * mfqP->m, &mfqP->w));
974: for (PetscInt i = 0; i < mfqP->m; i++) {
975: for (PetscInt j = 0; j < mfqP->m; j++) {
976: if (i == j) {
977: mfqP->w[i + mfqP->m * j] = 1.0;
978: } else {
979: mfqP->w[i + mfqP->m * j] = 0.0;
980: }
981: }
982: }
983: for (PetscInt i = 0; i < PetscMax(mfqP->m, mfqP->n); i++) mfqP->indices[i] = i;
984: PetscCallMPI(MPI_Comm_size(((PetscObject)tao)->comm, &mfqP->size));
985: if (mfqP->size > 1) {
986: PetscCall(VecCreateSeq(PETSC_COMM_SELF, mfqP->n, &mfqP->localx));
987: PetscCall(VecCreateSeq(PETSC_COMM_SELF, mfqP->n, &mfqP->localxmin));
988: PetscCall(VecCreateSeq(PETSC_COMM_SELF, mfqP->m, &mfqP->localf));
989: PetscCall(VecCreateSeq(PETSC_COMM_SELF, mfqP->m, &mfqP->localfmin));
990: PetscCall(ISCreateStride(MPI_COMM_SELF, mfqP->n, 0, 1, &isxloc));
991: PetscCall(ISCreateStride(MPI_COMM_SELF, mfqP->n, 0, 1, &isxglob));
992: PetscCall(ISCreateStride(MPI_COMM_SELF, mfqP->m, 0, 1, &isfloc));
993: PetscCall(ISCreateStride(MPI_COMM_SELF, mfqP->m, 0, 1, &isfglob));
995: PetscCall(VecScatterCreate(tao->solution, isxglob, mfqP->localx, isxloc, &mfqP->scatterx));
996: PetscCall(VecScatterCreate(tao->ls_res, isfglob, mfqP->localf, isfloc, &mfqP->scatterf));
998: PetscCall(ISDestroy(&isxloc));
999: PetscCall(ISDestroy(&isxglob));
1000: PetscCall(ISDestroy(&isfloc));
1001: PetscCall(ISDestroy(&isfglob));
1002: }
1004: if (!mfqP->usegqt) {
1005: KSP ksp;
1006: PC pc;
1007: PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, mfqP->n, mfqP->n, mfqP->Xsubproblem, &mfqP->subx));
1008: PetscCall(VecCreateSeq(PETSC_COMM_SELF, mfqP->n, &mfqP->subxl));
1009: PetscCall(VecDuplicate(mfqP->subxl, &mfqP->subb));
1010: PetscCall(VecDuplicate(mfqP->subxl, &mfqP->subxu));
1011: PetscCall(VecDuplicate(mfqP->subxl, &mfqP->subpdel));
1012: PetscCall(VecDuplicate(mfqP->subxl, &mfqP->subndel));
1013: PetscCall(TaoCreate(PETSC_COMM_SELF, &mfqP->subtao));
1014: PetscCall(PetscObjectIncrementTabLevel((PetscObject)mfqP->subtao, (PetscObject)tao, 1));
1015: PetscCall(TaoSetType(mfqP->subtao, TAOBNTR));
1016: PetscCall(TaoSetOptionsPrefix(mfqP->subtao, "pounders_subsolver_"));
1017: PetscCall(TaoSetSolution(mfqP->subtao, mfqP->subx));
1018: PetscCall(TaoSetObjectiveAndGradient(mfqP->subtao, NULL, pounders_fg, (void *)mfqP));
1019: PetscCall(TaoSetMaximumIterations(mfqP->subtao, mfqP->gqt_maxits));
1020: PetscCall(TaoSetFromOptions(mfqP->subtao));
1021: PetscCall(TaoGetKSP(mfqP->subtao, &ksp));
1022: if (ksp) {
1023: PetscCall(KSPGetPC(ksp, &pc));
1024: PetscCall(PCSetType(pc, PCNONE));
1025: }
1026: PetscCall(TaoSetVariableBounds(mfqP->subtao, mfqP->subxl, mfqP->subxu));
1027: PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, mfqP->n, mfqP->n, mfqP->Hres, &mfqP->subH));
1028: PetscCall(TaoSetHessian(mfqP->subtao, mfqP->subH, mfqP->subH, pounders_h, (void *)mfqP));
1029: }
1030: PetscFunctionReturn(PETSC_SUCCESS);
1031: }
1033: static PetscErrorCode TaoDestroy_POUNDERS(Tao tao)
1034: {
1035: TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;
1037: PetscFunctionBegin;
1038: if (!mfqP->usegqt) {
1039: PetscCall(TaoDestroy(&mfqP->subtao));
1040: PetscCall(VecDestroy(&mfqP->subx));
1041: PetscCall(VecDestroy(&mfqP->subxl));
1042: PetscCall(VecDestroy(&mfqP->subxu));
1043: PetscCall(VecDestroy(&mfqP->subb));
1044: PetscCall(VecDestroy(&mfqP->subpdel));
1045: PetscCall(VecDestroy(&mfqP->subndel));
1046: PetscCall(MatDestroy(&mfqP->subH));
1047: }
1048: PetscCall(PetscFree(mfqP->Fres));
1049: PetscCall(PetscFree(mfqP->RES));
1050: PetscCall(PetscFree(mfqP->work));
1051: PetscCall(PetscFree(mfqP->work2));
1052: PetscCall(PetscFree(mfqP->work3));
1053: PetscCall(PetscFree(mfqP->mwork));
1054: PetscCall(PetscFree(mfqP->omega));
1055: PetscCall(PetscFree(mfqP->beta));
1056: PetscCall(PetscFree(mfqP->alpha));
1057: PetscCall(PetscFree(mfqP->H));
1058: PetscCall(PetscFree(mfqP->Q));
1059: PetscCall(PetscFree(mfqP->Q_tmp));
1060: PetscCall(PetscFree(mfqP->L));
1061: PetscCall(PetscFree(mfqP->L_tmp));
1062: PetscCall(PetscFree(mfqP->L_save));
1063: PetscCall(PetscFree(mfqP->N));
1064: PetscCall(PetscFree(mfqP->M));
1065: PetscCall(PetscFree(mfqP->Z));
1066: PetscCall(PetscFree(mfqP->tau));
1067: PetscCall(PetscFree(mfqP->tau_tmp));
1068: PetscCall(PetscFree(mfqP->npmaxwork));
1069: PetscCall(PetscFree(mfqP->npmaxiwork));
1070: PetscCall(PetscFree(mfqP->xmin));
1071: PetscCall(PetscFree(mfqP->C));
1072: PetscCall(PetscFree(mfqP->Fdiff));
1073: PetscCall(PetscFree(mfqP->Disp));
1074: PetscCall(PetscFree(mfqP->Gres));
1075: PetscCall(PetscFree(mfqP->Hres));
1076: PetscCall(PetscFree(mfqP->Gpoints));
1077: PetscCall(PetscFree(mfqP->model_indices));
1078: PetscCall(PetscFree(mfqP->last_model_indices));
1079: PetscCall(PetscFree(mfqP->Xsubproblem));
1080: PetscCall(PetscFree(mfqP->Gdel));
1081: PetscCall(PetscFree(mfqP->Hdel));
1082: PetscCall(PetscFree(mfqP->indices));
1083: PetscCall(PetscFree(mfqP->iwork));
1084: PetscCall(PetscFree(mfqP->w));
1085: for (PetscInt i = 0; i < mfqP->nHist; i++) {
1086: PetscCall(VecDestroy(&mfqP->Xhist[i]));
1087: PetscCall(VecDestroy(&mfqP->Fhist[i]));
1088: }
1089: PetscCall(VecDestroy(&mfqP->workxvec));
1090: PetscCall(VecDestroy(&mfqP->workfvec));
1091: PetscCall(PetscFree(mfqP->Xhist));
1092: PetscCall(PetscFree(mfqP->Fhist));
1094: if (mfqP->size > 1) {
1095: PetscCall(VecDestroy(&mfqP->localx));
1096: PetscCall(VecDestroy(&mfqP->localxmin));
1097: PetscCall(VecDestroy(&mfqP->localf));
1098: PetscCall(VecDestroy(&mfqP->localfmin));
1099: }
1100: PetscCall(PetscFree(tao->data));
1101: PetscFunctionReturn(PETSC_SUCCESS);
1102: }
1104: static PetscErrorCode TaoSetFromOptions_POUNDERS(Tao tao, PetscOptionItems PetscOptionsObject)
1105: {
1106: TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;
1108: PetscFunctionBegin;
1109: PetscOptionsHeadBegin(PetscOptionsObject, "POUNDERS method for least-squares optimization");
1110: PetscCall(PetscOptionsReal("-tao_pounders_delta", "initial delta", "", mfqP->delta, &mfqP->delta0, NULL));
1111: mfqP->delta = mfqP->delta0;
1112: PetscCall(PetscOptionsInt("-tao_pounders_npmax", "max number of points in model", "", mfqP->npmax, &mfqP->npmax, NULL));
1113: PetscCall(PetscOptionsBool("-tao_pounders_gqt", "use gqt algorithm for subproblem", "", mfqP->usegqt, &mfqP->usegqt, NULL));
1114: PetscOptionsHeadEnd();
1115: PetscFunctionReturn(PETSC_SUCCESS);
1116: }
1118: static PetscErrorCode TaoView_POUNDERS(Tao tao, PetscViewer viewer)
1119: {
1120: TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;
1121: PetscBool isascii;
1123: PetscFunctionBegin;
1124: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1125: if (isascii) {
1126: PetscCall(PetscViewerASCIIPrintf(viewer, "initial delta: %g\n", (double)mfqP->delta0));
1127: PetscCall(PetscViewerASCIIPrintf(viewer, "final delta: %g\n", (double)mfqP->delta));
1128: PetscCall(PetscViewerASCIIPrintf(viewer, "model points: %" PetscInt_FMT "\n", mfqP->nmodelpoints));
1129: if (mfqP->usegqt) {
1130: PetscCall(PetscViewerASCIIPrintf(viewer, "subproblem solver: gqt\n"));
1131: } else {
1132: PetscCall(TaoView(mfqP->subtao, viewer));
1133: }
1134: }
1135: PetscFunctionReturn(PETSC_SUCCESS);
1136: }
1137: /*MC
1138: TAOPOUNDERS - POUNDERS derivate-free model-based algorithm for nonlinear least squares
1140: Options Database Keys:
1141: + -tao_pounders_delta - initial step length
1142: . -tao_pounders_npmax - maximum number of points in model
1143: - -tao_pounders_gqt - use gqt algorithm for subproblem instead of TRON
1145: Level: beginner
1147: .seealso: `Tao`, `TAONTR`, `TAONTL`, `TAONM`, `TaoType`, `TaoCreate()`
1148: M*/
1150: PETSC_EXTERN PetscErrorCode TaoCreate_POUNDERS(Tao tao)
1151: {
1152: TAO_POUNDERS *mfqP = (TAO_POUNDERS *)tao->data;
1154: PetscFunctionBegin;
1155: tao->ops->setup = TaoSetUp_POUNDERS;
1156: tao->ops->solve = TaoSolve_POUNDERS;
1157: tao->ops->view = TaoView_POUNDERS;
1158: tao->ops->setfromoptions = TaoSetFromOptions_POUNDERS;
1159: tao->ops->destroy = TaoDestroy_POUNDERS;
1161: PetscCall(PetscNew(&mfqP));
1162: tao->data = (void *)mfqP;
1164: /* Override default settings (unless already changed) */
1165: PetscCall(TaoParametersInitialize(tao));
1166: PetscObjectParameterSetDefault(tao, max_it, 2000);
1167: PetscObjectParameterSetDefault(tao, max_funcs, 4000);
1169: mfqP->npmax = PETSC_CURRENT;
1170: mfqP->delta0 = 0.1;
1171: mfqP->delta = 0.1;
1172: mfqP->deltamax = 1e3;
1173: mfqP->deltamin = 1e-6;
1174: mfqP->c2 = 10.0;
1175: mfqP->theta1 = 1e-5;
1176: mfqP->theta2 = 1e-4;
1177: mfqP->gamma0 = 0.5;
1178: mfqP->gamma1 = 2.0;
1179: mfqP->eta0 = 0.0;
1180: mfqP->eta1 = 0.1;
1181: mfqP->usegqt = PETSC_FALSE;
1182: mfqP->gqt_rtol = 0.001;
1183: mfqP->gqt_maxits = 50;
1184: mfqP->workxvec = NULL;
1185: PetscFunctionReturn(PETSC_SUCCESS);
1186: }