static PetscErrorCode TaoSolve_SSILS(Tao tao) { TAO_SSLS *ssls = (TAO_SSLS *)tao->data; PetscReal psi, ndpsi, normd, innerd, t=0; PetscReal delta, rho; PetscInt iter=0,kspits; TaoConvergedReason reason; TaoLineSearchConvergedReason ls_reason; PetscErrorCode ierr; PetscFunctionBegin; /* Assume that Setup has been called! Set the structure for the Jacobian and create a linear solver. */ delta = ssls->delta; rho = ssls->rho; ierr = TaoComputeVariableBounds(tao);CHKERRQ(ierr); ierr = VecMedian(tao->XL,tao->solution,tao->XU,tao->solution);CHKERRQ(ierr); ierr = TaoLineSearchSetObjectiveAndGradientRoutine(tao->linesearch,Tao_SSLS_FunctionGradient,tao);CHKERRQ(ierr); ierr = TaoLineSearchSetObjectiveRoutine(tao->linesearch,Tao_SSLS_Function,tao);CHKERRQ(ierr); /* Calculate the function value and fischer function value at the current iterate */ ierr = TaoLineSearchComputeObjectiveAndGradient(tao->linesearch,tao->solution,&psi,ssls->dpsi);CHKERRQ(ierr); ierr = VecNorm(ssls->dpsi,NORM_2,&ndpsi);CHKERRQ(ierr); while (1) { ierr=PetscInfo3(tao, "iter: %D, merit: %g, ndpsi: %g\n",iter, (double)ssls->merit, (double)ndpsi);CHKERRQ(ierr); /* Check the termination criteria */ ierr = TaoMonitor(tao,iter++,ssls->merit,ndpsi,0.0,t,&reason);CHKERRQ(ierr); if (reason!=TAO_CONTINUE_ITERATING) break; /* Calculate direction. (Really negative of newton direction. Therefore, rest of the code uses -d.) */ ierr = KSPSetOperators(tao->ksp,tao->jacobian,tao->jacobian_pre);CHKERRQ(ierr); ierr = KSPSolve(tao->ksp,ssls->ff,tao->stepdirection);CHKERRQ(ierr); ierr = KSPGetIterationNumber(tao->ksp,&kspits);CHKERRQ(ierr); tao->ksp_its+=kspits; ierr = VecNorm(tao->stepdirection,NORM_2,&normd);CHKERRQ(ierr); ierr = VecDot(tao->stepdirection,ssls->dpsi,&innerd);CHKERRQ(ierr); /* Make sure that we have a descent direction */ if (innerd <= delta*pow(normd, rho)) { ierr = PetscInfo(tao, "newton direction not descent\n");CHKERRQ(ierr); ierr = VecCopy(ssls->dpsi,tao->stepdirection);CHKERRQ(ierr); ierr = VecDot(tao->stepdirection,ssls->dpsi,&innerd);CHKERRQ(ierr); } ierr = VecScale(tao->stepdirection, -1.0);CHKERRQ(ierr); innerd = -innerd; ierr = TaoLineSearchSetInitialStepLength(tao->linesearch,1.0); ierr = TaoLineSearchApply(tao->linesearch,tao->solution,&psi,ssls->dpsi,tao->stepdirection,&t,&ls_reason);CHKERRQ(ierr); ierr = VecNorm(ssls->dpsi,NORM_2,&ndpsi);CHKERRQ(ierr); } PetscFunctionReturn(0); }
/* Push initial point away from bounds */ PetscErrorCode IPMPushInitialPoint(Tao tao) { TAO_IPM *ipmP = (TAO_IPM *)tao->data; PetscErrorCode ierr; PetscFunctionBegin; ierr = TaoComputeVariableBounds(tao);CHKERRQ(ierr); if (tao->XL && tao->XU) { ierr = VecMedian(tao->XL, tao->solution, tao->XU, tao->solution);CHKERRQ(ierr); } if (ipmP->nb > 0) { ierr = VecSet(ipmP->s,ipmP->pushs);CHKERRQ(ierr); ierr = VecSet(ipmP->lamdai,ipmP->pushnu);CHKERRQ(ierr); if (ipmP->mi > 0) { ierr = VecSet(tao->DI,ipmP->pushnu);CHKERRQ(ierr); } } if (ipmP->me > 0) { ierr = VecSet(tao->DE,1.0);CHKERRQ(ierr); ierr = VecSet(ipmP->lamdae,1.0);CHKERRQ(ierr); } PetscFunctionReturn(0); }
static PetscErrorCode TaoSolve_TRON(Tao tao) { TAO_TRON *tron = (TAO_TRON *)tao->data; PetscErrorCode ierr; PetscInt its; TaoConvergedReason reason = TAO_CONTINUE_ITERATING; TaoLineSearchConvergedReason ls_reason = TAOLINESEARCH_CONTINUE_ITERATING; PetscReal prered,actred,delta,f,f_new,rhok,gdx,xdiff,stepsize; PetscFunctionBegin; tron->pgstepsize=1.0; tao->trust = tao->trust0; /* Project the current point onto the feasible set */ ierr = TaoComputeVariableBounds(tao);CHKERRQ(ierr); ierr = VecMedian(tao->XL,tao->solution,tao->XU,tao->solution);CHKERRQ(ierr); ierr = TaoLineSearchSetVariableBounds(tao->linesearch,tao->XL,tao->XU);CHKERRQ(ierr); ierr = TaoComputeObjectiveAndGradient(tao,tao->solution,&tron->f,tao->gradient);CHKERRQ(ierr); ierr = ISDestroy(&tron->Free_Local);CHKERRQ(ierr); ierr = VecWhichBetween(tao->XL,tao->solution,tao->XU,&tron->Free_Local);CHKERRQ(ierr); /* Project the gradient and calculate the norm */ ierr = VecBoundGradientProjection(tao->gradient,tao->solution, tao->XL, tao->XU, tao->gradient);CHKERRQ(ierr); ierr = VecNorm(tao->gradient,NORM_2,&tron->gnorm);CHKERRQ(ierr); if (PetscIsInfOrNanReal(tron->f) || PetscIsInfOrNanReal(tron->gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf pr NaN"); if (tao->trust <= 0) { tao->trust=PetscMax(tron->gnorm*tron->gnorm,1.0); } tron->stepsize=tao->trust; ierr = TaoMonitor(tao, tao->niter, tron->f, tron->gnorm, 0.0, tron->stepsize, &reason);CHKERRQ(ierr); while (reason==TAO_CONTINUE_ITERATING){ tao->ksp_its=0; ierr = TronGradientProjections(tao,tron);CHKERRQ(ierr); f=tron->f; delta=tao->trust; tron->n_free_last = tron->n_free; ierr = TaoComputeHessian(tao,tao->solution,tao->hessian,tao->hessian_pre);CHKERRQ(ierr); ierr = ISGetSize(tron->Free_Local, &tron->n_free);CHKERRQ(ierr); /* If no free variables */ if (tron->n_free == 0) { actred=0; ierr = PetscInfo(tao,"No free variables in tron iteration.\n");CHKERRQ(ierr); ierr = VecNorm(tao->gradient,NORM_2,&tron->gnorm);CHKERRQ(ierr); ierr = TaoMonitor(tao, tao->niter, tron->f, tron->gnorm, 0.0, delta, &reason);CHKERRQ(ierr); if (!reason) { reason = TAO_CONVERGED_STEPTOL; ierr = TaoSetConvergedReason(tao,reason);CHKERRQ(ierr); } break; } /* use free_local to mask/submat gradient, hessian, stepdirection */ ierr = TaoVecGetSubVec(tao->gradient,tron->Free_Local,tao->subset_type,0.0,&tron->R);CHKERRQ(ierr); ierr = TaoVecGetSubVec(tao->gradient,tron->Free_Local,tao->subset_type,0.0,&tron->DXFree);CHKERRQ(ierr); ierr = VecSet(tron->DXFree,0.0);CHKERRQ(ierr); ierr = VecScale(tron->R, -1.0);CHKERRQ(ierr); ierr = TaoMatGetSubMat(tao->hessian, tron->Free_Local, tron->diag, tao->subset_type, &tron->H_sub);CHKERRQ(ierr); if (tao->hessian == tao->hessian_pre) { ierr = MatDestroy(&tron->Hpre_sub);CHKERRQ(ierr); ierr = PetscObjectReference((PetscObject)(tron->H_sub));CHKERRQ(ierr); tron->Hpre_sub = tron->H_sub; } else { ierr = TaoMatGetSubMat(tao->hessian_pre, tron->Free_Local, tron->diag, tao->subset_type,&tron->Hpre_sub);CHKERRQ(ierr); } ierr = KSPReset(tao->ksp);CHKERRQ(ierr); ierr = KSPSetOperators(tao->ksp, tron->H_sub, tron->Hpre_sub);CHKERRQ(ierr); while (1) { /* Approximately solve the reduced linear system */ ierr = KSPSTCGSetRadius(tao->ksp,delta);CHKERRQ(ierr); ierr = KSPSolve(tao->ksp, tron->R, tron->DXFree);CHKERRQ(ierr); ierr = KSPGetIterationNumber(tao->ksp,&its);CHKERRQ(ierr); tao->ksp_its+=its; tao->ksp_tot_its+=its; ierr = VecSet(tao->stepdirection,0.0);CHKERRQ(ierr); /* Add dxfree matrix to compute step direction vector */ ierr = VecISAXPY(tao->stepdirection,tron->Free_Local,1.0,tron->DXFree);CHKERRQ(ierr); if (0) { PetscReal rhs,stepnorm; ierr = VecNorm(tron->R,NORM_2,&rhs);CHKERRQ(ierr); ierr = VecNorm(tron->DXFree,NORM_2,&stepnorm);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"|rhs|=%g\t|s|=%g\n",(double)rhs,(double)stepnorm);CHKERRQ(ierr); } ierr = VecDot(tao->gradient, tao->stepdirection, &gdx);CHKERRQ(ierr); ierr = PetscInfo1(tao,"Expected decrease in function value: %14.12e\n",(double)gdx);CHKERRQ(ierr); ierr = VecCopy(tao->solution, tron->X_New);CHKERRQ(ierr); ierr = VecCopy(tao->gradient, tron->G_New);CHKERRQ(ierr); stepsize=1.0;f_new=f; ierr = TaoLineSearchSetInitialStepLength(tao->linesearch,1.0);CHKERRQ(ierr); ierr = TaoLineSearchApply(tao->linesearch, tron->X_New, &f_new, tron->G_New, tao->stepdirection,&stepsize,&ls_reason);CHKERRQ(ierr);CHKERRQ(ierr); ierr = TaoAddLineSearchCounts(tao);CHKERRQ(ierr); ierr = MatMult(tao->hessian, tao->stepdirection, tron->Work);CHKERRQ(ierr); ierr = VecAYPX(tron->Work, 0.5, tao->gradient);CHKERRQ(ierr); ierr = VecDot(tao->stepdirection, tron->Work, &prered);CHKERRQ(ierr); actred = f_new - f; if (actred<0) { rhok=PetscAbs(-actred/prered); } else { rhok=0.0; } /* Compare actual improvement to the quadratic model */ if (rhok > tron->eta1) { /* Accept the point */ /* d = x_new - x */ ierr = VecCopy(tron->X_New, tao->stepdirection);CHKERRQ(ierr); ierr = VecAXPY(tao->stepdirection, -1.0, tao->solution);CHKERRQ(ierr); ierr = VecNorm(tao->stepdirection, NORM_2, &xdiff);CHKERRQ(ierr); xdiff *= stepsize; /* Adjust trust region size */ if (rhok < tron->eta2 ){ delta = PetscMin(xdiff,delta)*tron->sigma1; } else if (rhok > tron->eta4 ){ delta= PetscMin(xdiff,delta)*tron->sigma3; } else if (rhok > tron->eta3 ){ delta=PetscMin(xdiff,delta)*tron->sigma2; } ierr = VecBoundGradientProjection(tron->G_New,tron->X_New, tao->XL, tao->XU, tao->gradient);CHKERRQ(ierr); if (tron->Free_Local) { ierr = ISDestroy(&tron->Free_Local);CHKERRQ(ierr); } ierr = VecWhichBetween(tao->XL, tron->X_New, tao->XU, &tron->Free_Local);CHKERRQ(ierr); f=f_new; ierr = VecNorm(tao->gradient,NORM_2,&tron->gnorm);CHKERRQ(ierr); ierr = VecCopy(tron->X_New, tao->solution);CHKERRQ(ierr); ierr = VecCopy(tron->G_New, tao->gradient);CHKERRQ(ierr); break; } else if (delta <= 1e-30) { break; } else { delta /= 4.0; } } /* end linear solve loop */ tron->f=f; tron->actred=actred; tao->trust=delta; tao->niter++; ierr = TaoMonitor(tao, tao->niter, tron->f, tron->gnorm, 0.0, delta, &reason);CHKERRQ(ierr); } /* END MAIN LOOP */ PetscFunctionReturn(0); }
static PetscErrorCode IPMInitializeBounds(Tao tao) { TAO_IPM *ipmP = (TAO_IPM*)tao->data; Vec xtmp; PetscInt xstart,xend; PetscInt ucstart,ucend; /* user ci */ PetscInt ucestart,uceend; /* user ce */ PetscInt sstart,send; PetscInt bigsize; PetscInt i,counter,nloc; PetscInt *cind,*xind,*ucind,*uceind,*stepind; VecType vtype; const PetscInt *xli,*xui; PetscInt xl_offset,xu_offset; IS bigxl,bigxu,isuc,isc,isx,sis,is1; PetscErrorCode ierr; MPI_Comm comm; PetscFunctionBegin; cind=xind=ucind=uceind=stepind=0; ipmP->mi=0; ipmP->nxlb=0; ipmP->nxub=0; ipmP->nb=0; ipmP->nslack=0; ierr = VecDuplicate(tao->solution,&xtmp);CHKERRQ(ierr); if (!tao->XL && !tao->XU && tao->ops->computebounds) { ierr = TaoComputeVariableBounds(tao);CHKERRQ(ierr); } if (tao->XL) { ierr = VecSet(xtmp,PETSC_NINFINITY);CHKERRQ(ierr); ierr = VecWhichGreaterThan(tao->XL,xtmp,&ipmP->isxl);CHKERRQ(ierr); ierr = ISGetSize(ipmP->isxl,&ipmP->nxlb);CHKERRQ(ierr); } else { ipmP->nxlb=0; } if (tao->XU) { ierr = VecSet(xtmp,PETSC_INFINITY);CHKERRQ(ierr); ierr = VecWhichLessThan(tao->XU,xtmp,&ipmP->isxu);CHKERRQ(ierr); ierr = ISGetSize(ipmP->isxu,&ipmP->nxub);CHKERRQ(ierr); } else { ipmP->nxub=0; } ierr = VecDestroy(&xtmp);CHKERRQ(ierr); if (tao->constraints_inequality) { ierr = VecGetSize(tao->constraints_inequality,&ipmP->mi);CHKERRQ(ierr); } else { ipmP->mi = 0; } ipmP->nb = ipmP->nxlb + ipmP->nxub + ipmP->mi; comm = ((PetscObject)(tao->solution))->comm; bigsize = ipmP->n+2*ipmP->nb+ipmP->me; ierr = PetscMalloc1(bigsize,&stepind);CHKERRQ(ierr); ierr = PetscMalloc1(ipmP->n,&xind);CHKERRQ(ierr); ierr = PetscMalloc1(ipmP->me,&uceind);CHKERRQ(ierr); ierr = VecGetOwnershipRange(tao->solution,&xstart,&xend);CHKERRQ(ierr); if (ipmP->nb > 0) { ierr = VecCreate(comm,&ipmP->s);CHKERRQ(ierr); ierr = VecSetSizes(ipmP->s,PETSC_DECIDE,ipmP->nb);CHKERRQ(ierr); ierr = VecSetFromOptions(ipmP->s);CHKERRQ(ierr); ierr = VecDuplicate(ipmP->s,&ipmP->ds);CHKERRQ(ierr); ierr = VecDuplicate(ipmP->s,&ipmP->rhs_s);CHKERRQ(ierr); ierr = VecDuplicate(ipmP->s,&ipmP->complementarity);CHKERRQ(ierr); ierr = VecDuplicate(ipmP->s,&ipmP->ci);CHKERRQ(ierr); ierr = VecDuplicate(ipmP->s,&ipmP->lamdai);CHKERRQ(ierr); ierr = VecDuplicate(ipmP->s,&ipmP->dlamdai);CHKERRQ(ierr); ierr = VecDuplicate(ipmP->s,&ipmP->rhs_lamdai);CHKERRQ(ierr); ierr = VecDuplicate(ipmP->s,&ipmP->save_lamdai);CHKERRQ(ierr); ierr = VecDuplicate(ipmP->s,&ipmP->save_s);CHKERRQ(ierr); ierr = VecDuplicate(ipmP->s,&ipmP->rpi);CHKERRQ(ierr); ierr = VecDuplicate(ipmP->s,&ipmP->Zero_nb);CHKERRQ(ierr); ierr = VecSet(ipmP->Zero_nb,0.0);CHKERRQ(ierr); ierr = VecDuplicate(ipmP->s,&ipmP->One_nb);CHKERRQ(ierr); ierr = VecSet(ipmP->One_nb,1.0);CHKERRQ(ierr); ierr = VecDuplicate(ipmP->s,&ipmP->Inf_nb);CHKERRQ(ierr); ierr = VecSet(ipmP->Inf_nb,PETSC_INFINITY);CHKERRQ(ierr); ierr = PetscMalloc1(ipmP->nb,&cind);CHKERRQ(ierr); ierr = PetscMalloc1(ipmP->mi,&ucind);CHKERRQ(ierr); ierr = VecGetOwnershipRange(ipmP->s,&sstart,&send);CHKERRQ(ierr); if (ipmP->mi > 0) { ierr = VecGetOwnershipRange(tao->constraints_inequality,&ucstart,&ucend);CHKERRQ(ierr); counter=0; for (i=ucstart;i<ucend;i++) { cind[counter++] = i; } ierr = ISCreateGeneral(comm,counter,cind,PETSC_COPY_VALUES,&isuc);CHKERRQ(ierr); ierr = ISCreateGeneral(comm,counter,cind,PETSC_COPY_VALUES,&isc);CHKERRQ(ierr); ierr = VecScatterCreate(tao->constraints_inequality,isuc,ipmP->ci,isc,&ipmP->ci_scat);CHKERRQ(ierr); ierr = ISDestroy(&isuc);CHKERRQ(ierr); ierr = ISDestroy(&isc);CHKERRQ(ierr); } /* need to know how may xbound indices are on each process */ /* TODO better way */ if (ipmP->nxlb) { ierr = ISAllGather(ipmP->isxl,&bigxl);CHKERRQ(ierr); ierr = ISGetIndices(bigxl,&xli);CHKERRQ(ierr); /* find offsets for this processor */ xl_offset = ipmP->mi; for (i=0;i<ipmP->nxlb;i++) { if (xli[i] < xstart) { xl_offset++; } else break; } ierr = ISRestoreIndices(bigxl,&xli);CHKERRQ(ierr); ierr = ISGetIndices(ipmP->isxl,&xli);CHKERRQ(ierr); ierr = ISGetLocalSize(ipmP->isxl,&nloc);CHKERRQ(ierr); for (i=0;i<nloc;i++) { xind[i] = xli[i]; cind[i] = xl_offset+i; } ierr = ISCreateGeneral(comm,nloc,xind,PETSC_COPY_VALUES,&isx);CHKERRQ(ierr); ierr = ISCreateGeneral(comm,nloc,cind,PETSC_COPY_VALUES,&isc);CHKERRQ(ierr); ierr = VecScatterCreate(tao->XL,isx,ipmP->ci,isc,&ipmP->xl_scat);CHKERRQ(ierr); ierr = ISDestroy(&isx);CHKERRQ(ierr); ierr = ISDestroy(&isc);CHKERRQ(ierr); ierr = ISDestroy(&bigxl);CHKERRQ(ierr); } if (ipmP->nxub) { ierr = ISAllGather(ipmP->isxu,&bigxu);CHKERRQ(ierr); ierr = ISGetIndices(bigxu,&xui);CHKERRQ(ierr); /* find offsets for this processor */ xu_offset = ipmP->mi + ipmP->nxlb; for (i=0;i<ipmP->nxub;i++) { if (xui[i] < xstart) { xu_offset++; } else break; } ierr = ISRestoreIndices(bigxu,&xui);CHKERRQ(ierr); ierr = ISGetIndices(ipmP->isxu,&xui);CHKERRQ(ierr); ierr = ISGetLocalSize(ipmP->isxu,&nloc);CHKERRQ(ierr); for (i=0;i<nloc;i++) { xind[i] = xui[i]; cind[i] = xu_offset+i; } ierr = ISCreateGeneral(comm,nloc,xind,PETSC_COPY_VALUES,&isx);CHKERRQ(ierr); ierr = ISCreateGeneral(comm,nloc,cind,PETSC_COPY_VALUES,&isc);CHKERRQ(ierr); ierr = VecScatterCreate(tao->XU,isx,ipmP->ci,isc,&ipmP->xu_scat);CHKERRQ(ierr); ierr = ISDestroy(&isx);CHKERRQ(ierr); ierr = ISDestroy(&isc);CHKERRQ(ierr); ierr = ISDestroy(&bigxu);CHKERRQ(ierr); } } ierr = VecCreate(comm,&ipmP->bigrhs);CHKERRQ(ierr); ierr = VecGetType(tao->solution,&vtype);CHKERRQ(ierr); ierr = VecSetType(ipmP->bigrhs,vtype);CHKERRQ(ierr); ierr = VecSetSizes(ipmP->bigrhs,PETSC_DECIDE,bigsize);CHKERRQ(ierr); ierr = VecSetFromOptions(ipmP->bigrhs);CHKERRQ(ierr); ierr = VecDuplicate(ipmP->bigrhs,&ipmP->bigstep);CHKERRQ(ierr); /* create scatters for step->x and x->rhs */ for (i=xstart;i<xend;i++) { stepind[i-xstart] = i; xind[i-xstart] = i; } ierr = ISCreateGeneral(comm,xend-xstart,stepind,PETSC_COPY_VALUES,&sis);CHKERRQ(ierr); ierr = ISCreateGeneral(comm,xend-xstart,xind,PETSC_COPY_VALUES,&is1);CHKERRQ(ierr); ierr = VecScatterCreate(ipmP->bigstep,sis,tao->solution,is1,&ipmP->step1);CHKERRQ(ierr); ierr = VecScatterCreate(tao->solution,is1,ipmP->bigrhs,sis,&ipmP->rhs1);CHKERRQ(ierr); ierr = ISDestroy(&sis);CHKERRQ(ierr); ierr = ISDestroy(&is1);CHKERRQ(ierr); if (ipmP->nb > 0) { for (i=sstart;i<send;i++) { stepind[i-sstart] = i+ipmP->n; cind[i-sstart] = i; } ierr = ISCreateGeneral(comm,send-sstart,stepind,PETSC_COPY_VALUES,&sis);CHKERRQ(ierr); ierr = ISCreateGeneral(comm,send-sstart,cind,PETSC_COPY_VALUES,&is1);CHKERRQ(ierr); ierr = VecScatterCreate(ipmP->bigstep,sis,ipmP->s,is1,&ipmP->step2);CHKERRQ(ierr); ierr = ISDestroy(&sis);CHKERRQ(ierr); for (i=sstart;i<send;i++) { stepind[i-sstart] = i+ipmP->n+ipmP->me; cind[i-sstart] = i; } ierr = ISCreateGeneral(comm,send-sstart,stepind,PETSC_COPY_VALUES,&sis);CHKERRQ(ierr); ierr = VecScatterCreate(ipmP->s,is1,ipmP->bigrhs,sis,&ipmP->rhs3);CHKERRQ(ierr); ierr = ISDestroy(&sis);CHKERRQ(ierr); ierr = ISDestroy(&is1);CHKERRQ(ierr); } if (ipmP->me > 0) { ierr = VecGetOwnershipRange(tao->constraints_equality,&ucestart,&uceend);CHKERRQ(ierr); for (i=ucestart;i<uceend;i++) { stepind[i-ucestart] = i + ipmP->n+ipmP->nb; uceind[i-ucestart] = i; } ierr = ISCreateGeneral(comm,uceend-ucestart,stepind,PETSC_COPY_VALUES,&sis);CHKERRQ(ierr); ierr = ISCreateGeneral(comm,uceend-ucestart,uceind,PETSC_COPY_VALUES,&is1);CHKERRQ(ierr); ierr = VecScatterCreate(ipmP->bigstep,sis,tao->constraints_equality,is1,&ipmP->step3);CHKERRQ(ierr); ierr = ISDestroy(&sis);CHKERRQ(ierr); for (i=ucestart;i<uceend;i++) { stepind[i-ucestart] = i + ipmP->n; } ierr = ISCreateGeneral(comm,uceend-ucestart,stepind,PETSC_COPY_VALUES,&sis);CHKERRQ(ierr); ierr = VecScatterCreate(tao->constraints_equality,is1,ipmP->bigrhs,sis,&ipmP->rhs2);CHKERRQ(ierr); ierr = ISDestroy(&sis);CHKERRQ(ierr); ierr = ISDestroy(&is1);CHKERRQ(ierr); } if (ipmP->nb > 0) { for (i=sstart;i<send;i++) { stepind[i-sstart] = i + ipmP->n + ipmP->nb + ipmP->me; cind[i-sstart] = i; } ierr = ISCreateGeneral(comm,send-sstart,cind,PETSC_COPY_VALUES,&is1);CHKERRQ(ierr); ierr = ISCreateGeneral(comm,send-sstart,stepind,PETSC_COPY_VALUES,&sis);CHKERRQ(ierr); ierr = VecScatterCreate(ipmP->bigstep,sis,ipmP->s,is1,&ipmP->step4);CHKERRQ(ierr); ierr = VecScatterCreate(ipmP->s,is1,ipmP->bigrhs,sis,&ipmP->rhs4);CHKERRQ(ierr); ierr = ISDestroy(&sis);CHKERRQ(ierr); ierr = ISDestroy(&is1);CHKERRQ(ierr); } ierr = PetscFree(stepind);CHKERRQ(ierr); ierr = PetscFree(cind);CHKERRQ(ierr); ierr = PetscFree(ucind);CHKERRQ(ierr); ierr = PetscFree(uceind);CHKERRQ(ierr); ierr = PetscFree(xind);CHKERRQ(ierr); PetscFunctionReturn(0); }
static PetscErrorCode TaoSolve_GPCG(Tao tao) { TAO_GPCG *gpcg = (TAO_GPCG *)tao->data; PetscErrorCode ierr; PetscInt its; PetscReal actred,f,f_new,gnorm,gdx,stepsize,xtb; PetscReal xtHx; TaoConvergedReason reason = TAO_CONTINUE_ITERATING; TaoLineSearchConvergedReason ls_status = TAOLINESEARCH_CONTINUE_ITERATING; PetscFunctionBegin; ierr = TaoComputeVariableBounds(tao);CHKERRQ(ierr); ierr = VecMedian(tao->XL,tao->solution,tao->XU,tao->solution);CHKERRQ(ierr); ierr = TaoLineSearchSetVariableBounds(tao->linesearch,tao->XL,tao->XU);CHKERRQ(ierr); /* Using f = .5*x'Hx + x'b + c and g=Hx + b, compute b,c */ ierr = TaoComputeHessian(tao,tao->solution,tao->hessian,tao->hessian_pre);CHKERRQ(ierr); ierr = TaoComputeObjectiveAndGradient(tao,tao->solution,&f,tao->gradient);CHKERRQ(ierr); ierr = VecCopy(tao->gradient, gpcg->B);CHKERRQ(ierr); ierr = MatMult(tao->hessian,tao->solution,gpcg->Work);CHKERRQ(ierr); ierr = VecDot(gpcg->Work, tao->solution, &xtHx);CHKERRQ(ierr); ierr = VecAXPY(gpcg->B,-1.0,gpcg->Work);CHKERRQ(ierr); ierr = VecDot(gpcg->B,tao->solution,&xtb);CHKERRQ(ierr); gpcg->c=f-xtHx/2.0-xtb; if (gpcg->Free_Local) { ierr = ISDestroy(&gpcg->Free_Local);CHKERRQ(ierr); } ierr = VecWhichBetween(tao->XL,tao->solution,tao->XU,&gpcg->Free_Local);CHKERRQ(ierr); /* Project the gradient and calculate the norm */ ierr = VecCopy(tao->gradient,gpcg->G_New);CHKERRQ(ierr); ierr = VecBoundGradientProjection(tao->gradient,tao->solution,tao->XL,tao->XU,gpcg->PG);CHKERRQ(ierr); ierr = VecNorm(gpcg->PG,NORM_2,&gpcg->gnorm);CHKERRQ(ierr); tao->step=1.0; gpcg->f = f; /* Check Stopping Condition */ ierr=TaoMonitor(tao,tao->niter,f,gpcg->gnorm,0.0,tao->step,&reason);CHKERRQ(ierr); while (reason == TAO_CONTINUE_ITERATING){ tao->ksp_its=0; ierr = GPCGGradProjections(tao);CHKERRQ(ierr); ierr = ISGetSize(gpcg->Free_Local,&gpcg->n_free);CHKERRQ(ierr); f=gpcg->f; gnorm=gpcg->gnorm; ierr = KSPReset(tao->ksp);CHKERRQ(ierr); if (gpcg->n_free > 0){ /* Create a reduced linear system */ ierr = VecDestroy(&gpcg->R);CHKERRQ(ierr); ierr = VecDestroy(&gpcg->DXFree);CHKERRQ(ierr); ierr = TaoVecGetSubVec(tao->gradient,gpcg->Free_Local, tao->subset_type, 0.0, &gpcg->R);CHKERRQ(ierr); ierr = VecScale(gpcg->R, -1.0);CHKERRQ(ierr); ierr = TaoVecGetSubVec(tao->stepdirection,gpcg->Free_Local,tao->subset_type, 0.0, &gpcg->DXFree);CHKERRQ(ierr); ierr = VecSet(gpcg->DXFree,0.0);CHKERRQ(ierr); ierr = TaoMatGetSubMat(tao->hessian, gpcg->Free_Local, gpcg->Work, tao->subset_type, &gpcg->Hsub);CHKERRQ(ierr); if (tao->hessian_pre == tao->hessian) { ierr = MatDestroy(&gpcg->Hsub_pre);CHKERRQ(ierr); ierr = PetscObjectReference((PetscObject)gpcg->Hsub);CHKERRQ(ierr); gpcg->Hsub_pre = gpcg->Hsub; } else { ierr = TaoMatGetSubMat(tao->hessian, gpcg->Free_Local, gpcg->Work, tao->subset_type, &gpcg->Hsub_pre);CHKERRQ(ierr); } ierr = KSPReset(tao->ksp);CHKERRQ(ierr); ierr = KSPSetOperators(tao->ksp,gpcg->Hsub,gpcg->Hsub_pre);CHKERRQ(ierr); ierr = KSPSolve(tao->ksp,gpcg->R,gpcg->DXFree);CHKERRQ(ierr); ierr = KSPGetIterationNumber(tao->ksp,&its);CHKERRQ(ierr); tao->ksp_its+=its; tao->ksp_tot_its+=its; ierr = VecSet(tao->stepdirection,0.0);CHKERRQ(ierr); ierr = VecISAXPY(tao->stepdirection,gpcg->Free_Local,1.0,gpcg->DXFree);CHKERRQ(ierr); ierr = VecDot(tao->stepdirection,tao->gradient,&gdx);CHKERRQ(ierr); ierr = TaoLineSearchSetInitialStepLength(tao->linesearch,1.0);CHKERRQ(ierr); f_new=f; ierr = TaoLineSearchApply(tao->linesearch,tao->solution,&f_new,tao->gradient,tao->stepdirection,&stepsize,&ls_status);CHKERRQ(ierr); actred = f_new - f; /* Evaluate the function and gradient at the new point */ ierr = VecBoundGradientProjection(tao->gradient,tao->solution,tao->XL,tao->XU, gpcg->PG);CHKERRQ(ierr); ierr = VecNorm(gpcg->PG, NORM_2, &gnorm);CHKERRQ(ierr); f=f_new; ierr = ISDestroy(&gpcg->Free_Local);CHKERRQ(ierr); ierr = VecWhichBetween(tao->XL,tao->solution,tao->XU,&gpcg->Free_Local);CHKERRQ(ierr); } else { actred = 0; gpcg->step=1.0; /* if there were no free variables, no cg method */ } tao->niter++; ierr = TaoMonitor(tao,tao->niter,f,gnorm,0.0,gpcg->step,&reason);CHKERRQ(ierr); gpcg->f=f;gpcg->gnorm=gnorm; gpcg->actred=actred; if (reason!=TAO_CONTINUE_ITERATING) break; } /* END MAIN LOOP */ PetscFunctionReturn(0); }
static PetscErrorCode QPIPSetInitialPoint(TAO_BQPIP *qp, Tao tao) { PetscErrorCode ierr; PetscReal two=2.0,p01=1; PetscReal gap1,gap2,fff,mu; PetscFunctionBegin; /* Compute function, Gradient R=Hx+b, and Hessian */ ierr = TaoComputeVariableBounds(tao);CHKERRQ(ierr); ierr = VecMedian(qp->XL, tao->solution, qp->XU, tao->solution);CHKERRQ(ierr); ierr = MatMult(tao->hessian, tao->solution, tao->gradient);CHKERRQ(ierr); ierr = VecCopy(qp->C0, qp->Work);CHKERRQ(ierr); ierr = VecAXPY(qp->Work, 0.5, tao->gradient);CHKERRQ(ierr); ierr = VecAXPY(tao->gradient, 1.0, qp->C0);CHKERRQ(ierr); ierr = VecDot(tao->solution, qp->Work, &fff);CHKERRQ(ierr); qp->pobj = fff + qp->c; /* Initialize Primal Vectors */ /* T = XU - X; G = X - XL */ ierr = VecCopy(qp->XU, qp->T);CHKERRQ(ierr); ierr = VecAXPY(qp->T, -1.0, tao->solution);CHKERRQ(ierr); ierr = VecCopy(tao->solution, qp->G);CHKERRQ(ierr); ierr = VecAXPY(qp->G, -1.0, qp->XL);CHKERRQ(ierr); ierr = VecSet(qp->GZwork, p01);CHKERRQ(ierr); ierr = VecSet(qp->TSwork, p01);CHKERRQ(ierr); ierr = VecPointwiseMax(qp->G, qp->G, qp->GZwork);CHKERRQ(ierr); ierr = VecPointwiseMax(qp->T, qp->T, qp->TSwork);CHKERRQ(ierr); /* Initialize Dual Variable Vectors */ ierr = VecCopy(qp->G, qp->Z);CHKERRQ(ierr); ierr = VecReciprocal(qp->Z);CHKERRQ(ierr); ierr = VecCopy(qp->T, qp->S);CHKERRQ(ierr); ierr = VecReciprocal(qp->S);CHKERRQ(ierr); ierr = MatMult(tao->hessian, qp->Work, qp->RHS);CHKERRQ(ierr); ierr = VecAbs(qp->RHS);CHKERRQ(ierr); ierr = VecSet(qp->Work, p01);CHKERRQ(ierr); ierr = VecPointwiseMax(qp->RHS, qp->RHS, qp->Work);CHKERRQ(ierr); ierr = VecPointwiseDivide(qp->RHS, tao->gradient, qp->RHS);CHKERRQ(ierr); ierr = VecNorm(qp->RHS, NORM_1, &gap1);CHKERRQ(ierr); mu = PetscMin(10.0,(gap1+10.0)/qp->m); ierr = VecScale(qp->S, mu);CHKERRQ(ierr); ierr = VecScale(qp->Z, mu);CHKERRQ(ierr); ierr = VecSet(qp->TSwork, p01);CHKERRQ(ierr); ierr = VecSet(qp->GZwork, p01);CHKERRQ(ierr); ierr = VecPointwiseMax(qp->S, qp->S, qp->TSwork);CHKERRQ(ierr); ierr = VecPointwiseMax(qp->Z, qp->Z, qp->GZwork);CHKERRQ(ierr); qp->mu=0;qp->dinfeas=1.0;qp->pinfeas=1.0; while ( (qp->dinfeas+qp->pinfeas)/(qp->m+qp->n) >= qp->mu ){ ierr = VecScale(qp->G, two);CHKERRQ(ierr); ierr = VecScale(qp->Z, two);CHKERRQ(ierr); ierr = VecScale(qp->S, two);CHKERRQ(ierr); ierr = VecScale(qp->T, two);CHKERRQ(ierr); ierr = QPIPComputeResidual(qp,tao);CHKERRQ(ierr); ierr = VecCopy(tao->solution, qp->R3);CHKERRQ(ierr); ierr = VecAXPY(qp->R3, -1.0, qp->G);CHKERRQ(ierr); ierr = VecAXPY(qp->R3, -1.0, qp->XL);CHKERRQ(ierr); ierr = VecCopy(tao->solution, qp->R5);CHKERRQ(ierr); ierr = VecAXPY(qp->R5, 1.0, qp->T);CHKERRQ(ierr); ierr = VecAXPY(qp->R5, -1.0, qp->XU);CHKERRQ(ierr); ierr = VecNorm(qp->R3, NORM_INFINITY, &gap1);CHKERRQ(ierr); ierr = VecNorm(qp->R5, NORM_INFINITY, &gap2);CHKERRQ(ierr); qp->pinfeas=PetscMax(gap1,gap2); /* Compute the duality gap */ ierr = VecDot(qp->G, qp->Z, &gap1);CHKERRQ(ierr); ierr = VecDot(qp->T, qp->S, &gap2);CHKERRQ(ierr); qp->gap = (gap1+gap2); qp->dobj = qp->pobj - qp->gap; if (qp->m>0) qp->mu=qp->gap/(qp->m); else qp->mu=0.0; qp->rgap=qp->gap/( PetscAbsReal(qp->dobj) + PetscAbsReal(qp->pobj) + 1.0 ); } PetscFunctionReturn(0); }
static PetscErrorCode TaoSolve_BLMVM(Tao tao) { PetscErrorCode ierr; TAO_BLMVM *blmP = (TAO_BLMVM *)tao->data; TaoConvergedReason reason = TAO_CONTINUE_ITERATING; TaoLineSearchConvergedReason ls_status = TAOLINESEARCH_CONTINUE_ITERATING; PetscReal f, fold, gdx, gnorm; PetscReal stepsize = 1.0,delta; PetscFunctionBegin; /* Project initial point onto bounds */ ierr = TaoComputeVariableBounds(tao);CHKERRQ(ierr); ierr = VecMedian(tao->XL,tao->solution,tao->XU,tao->solution);CHKERRQ(ierr); ierr = TaoLineSearchSetVariableBounds(tao->linesearch,tao->XL,tao->XU);CHKERRQ(ierr); /* Check convergence criteria */ ierr = TaoComputeObjectiveAndGradient(tao, tao->solution,&f,blmP->unprojected_gradient);CHKERRQ(ierr); ierr = VecBoundGradientProjection(blmP->unprojected_gradient,tao->solution, tao->XL,tao->XU,tao->gradient);CHKERRQ(ierr); ierr = TaoGradientNorm(tao, tao->gradient,NORM_2,&gnorm);CHKERRQ(ierr); if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf pr NaN"); ierr = TaoMonitor(tao, tao->niter, f, gnorm, 0.0, stepsize, &reason);CHKERRQ(ierr); if (reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(0); /* Set initial scaling for the function */ if (f != 0.0) { delta = 2.0*PetscAbsScalar(f) / (gnorm*gnorm); } else { delta = 2.0 / (gnorm*gnorm); } ierr = MatLMVMSetDelta(blmP->M,delta);CHKERRQ(ierr); /* Set counter for gradient/reset steps */ blmP->grad = 0; blmP->reset = 0; /* Have not converged; continue with Newton method */ while (reason == TAO_CONTINUE_ITERATING) { /* Compute direction */ ierr = MatLMVMUpdate(blmP->M, tao->solution, tao->gradient);CHKERRQ(ierr); ierr = MatLMVMSolve(blmP->M, blmP->unprojected_gradient, tao->stepdirection);CHKERRQ(ierr); ierr = VecBoundGradientProjection(tao->stepdirection,tao->solution,tao->XL,tao->XU,tao->gradient);CHKERRQ(ierr); /* Check for success (descent direction) */ ierr = VecDot(blmP->unprojected_gradient, tao->gradient, &gdx);CHKERRQ(ierr); if (gdx <= 0) { /* Step is not descent or solve was not successful Use steepest descent direction (scaled) */ ++blmP->grad; if (f != 0.0) { delta = 2.0*PetscAbsScalar(f) / (gnorm*gnorm); } else { delta = 2.0 / (gnorm*gnorm); } ierr = MatLMVMSetDelta(blmP->M,delta);CHKERRQ(ierr); ierr = MatLMVMReset(blmP->M);CHKERRQ(ierr); ierr = MatLMVMUpdate(blmP->M, tao->solution, blmP->unprojected_gradient);CHKERRQ(ierr); ierr = MatLMVMSolve(blmP->M,blmP->unprojected_gradient, tao->stepdirection);CHKERRQ(ierr); } ierr = VecScale(tao->stepdirection,-1.0);CHKERRQ(ierr); /* Perform the linesearch */ fold = f; ierr = VecCopy(tao->solution, blmP->Xold);CHKERRQ(ierr); ierr = VecCopy(blmP->unprojected_gradient, blmP->Gold);CHKERRQ(ierr); ierr = TaoLineSearchSetInitialStepLength(tao->linesearch,1.0);CHKERRQ(ierr); ierr = TaoLineSearchApply(tao->linesearch, tao->solution, &f, blmP->unprojected_gradient, tao->stepdirection, &stepsize, &ls_status);CHKERRQ(ierr); ierr = TaoAddLineSearchCounts(tao);CHKERRQ(ierr); if (ls_status != TAOLINESEARCH_SUCCESS && ls_status != TAOLINESEARCH_SUCCESS_USER) { /* Linesearch failed Reset factors and use scaled (projected) gradient step */ ++blmP->reset; f = fold; ierr = VecCopy(blmP->Xold, tao->solution);CHKERRQ(ierr); ierr = VecCopy(blmP->Gold, blmP->unprojected_gradient);CHKERRQ(ierr); if (f != 0.0) { delta = 2.0* PetscAbsScalar(f) / (gnorm*gnorm); } else { delta = 2.0/ (gnorm*gnorm); } ierr = MatLMVMSetDelta(blmP->M,delta);CHKERRQ(ierr); ierr = MatLMVMReset(blmP->M);CHKERRQ(ierr); ierr = MatLMVMUpdate(blmP->M, tao->solution, blmP->unprojected_gradient);CHKERRQ(ierr); ierr = MatLMVMSolve(blmP->M, blmP->unprojected_gradient, tao->stepdirection);CHKERRQ(ierr); ierr = VecScale(tao->stepdirection, -1.0);CHKERRQ(ierr); /* This may be incorrect; linesearch has values fo stepmax and stepmin that should be reset. */ ierr = TaoLineSearchSetInitialStepLength(tao->linesearch,1.0);CHKERRQ(ierr); ierr = TaoLineSearchApply(tao->linesearch,tao->solution,&f, blmP->unprojected_gradient, tao->stepdirection, &stepsize, &ls_status);CHKERRQ(ierr); ierr = TaoAddLineSearchCounts(tao);CHKERRQ(ierr); if (ls_status != TAOLINESEARCH_SUCCESS && ls_status != TAOLINESEARCH_SUCCESS_USER) { tao->reason = TAO_DIVERGED_LS_FAILURE; break; } } /* Check for converged */ ierr = VecBoundGradientProjection(blmP->unprojected_gradient, tao->solution, tao->XL, tao->XU, tao->gradient);CHKERRQ(ierr); ierr = TaoGradientNorm(tao, tao->gradient, NORM_2, &gnorm);CHKERRQ(ierr); if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Not-a-Number"); tao->niter++; ierr = TaoMonitor(tao, tao->niter, f, gnorm, 0.0, stepsize, &reason);CHKERRQ(ierr); } PetscFunctionReturn(0); }
static PetscErrorCode TaoSolve_ASILS(Tao tao) { TAO_SSLS *asls = (TAO_SSLS *)tao->data; PetscReal psi,ndpsi, normd, innerd, t=0; PetscInt iter=0, nf; PetscErrorCode ierr; TaoConvergedReason reason; TaoLineSearchConvergedReason ls_reason; PetscFunctionBegin; /* Assume that Setup has been called! Set the structure for the Jacobian and create a linear solver. */ ierr = TaoComputeVariableBounds(tao);CHKERRQ(ierr); ierr = TaoLineSearchSetObjectiveAndGradientRoutine(tao->linesearch,Tao_ASLS_FunctionGradient,tao);CHKERRQ(ierr); ierr = TaoLineSearchSetObjectiveRoutine(tao->linesearch,Tao_SSLS_Function,tao);CHKERRQ(ierr); /* Calculate the function value and fischer function value at the current iterate */ ierr = TaoLineSearchComputeObjectiveAndGradient(tao->linesearch,tao->solution,&psi,asls->dpsi);CHKERRQ(ierr); ierr = VecNorm(asls->dpsi,NORM_2,&ndpsi);CHKERRQ(ierr); while (1) { /* Check the termination criteria */ ierr = PetscInfo3(tao,"iter %D, merit: %g, ||dpsi||: %g\n",iter, (double)asls->merit, (double)ndpsi);CHKERRQ(ierr); ierr = TaoMonitor(tao, iter++, asls->merit, ndpsi, 0.0, t, &reason);CHKERRQ(ierr); if (TAO_CONTINUE_ITERATING != reason) break; /* We are going to solve a linear system of equations. We need to set the tolerances for the solve so that we maintain an asymptotic rate of convergence that is superlinear. Note: these tolerances are for the reduced system. We really need to make sure that the full system satisfies the full-space conditions. This rule gives superlinear asymptotic convergence asls->atol = min(0.5, asls->merit*sqrt(asls->merit)); asls->rtol = 0.0; This rule gives quadratic asymptotic convergence asls->atol = min(0.5, asls->merit*asls->merit); asls->rtol = 0.0; Calculate a free and fixed set of variables. The fixed set of variables are those for the d_b is approximately equal to zero. The definition of approximately changes as we approach the solution to the problem. No one rule is guaranteed to work in all cases. The following definition is based on the norm of the Jacobian matrix. If the norm is large, the tolerance becomes smaller. */ ierr = MatNorm(tao->jacobian,NORM_1,&asls->identifier);CHKERRQ(ierr); asls->identifier = PetscMin(asls->merit, 1e-2) / (1 + asls->identifier); ierr = VecSet(asls->t1,-asls->identifier);CHKERRQ(ierr); ierr = VecSet(asls->t2, asls->identifier);CHKERRQ(ierr); ierr = ISDestroy(&asls->fixed);CHKERRQ(ierr); ierr = ISDestroy(&asls->free);CHKERRQ(ierr); ierr = VecWhichBetweenOrEqual(asls->t1, asls->db, asls->t2, &asls->fixed);CHKERRQ(ierr); ierr = ISComplementVec(asls->fixed,asls->t1, &asls->free);CHKERRQ(ierr); ierr = ISGetSize(asls->fixed,&nf);CHKERRQ(ierr); ierr = PetscInfo1(tao,"Number of fixed variables: %D\n", nf);CHKERRQ(ierr); /* We now have our partition. Now calculate the direction in the fixed variable space. */ ierr = TaoVecGetSubVec(asls->ff, asls->fixed, tao->subset_type, 0.0, &asls->r1); ierr = TaoVecGetSubVec(asls->da, asls->fixed, tao->subset_type, 1.0, &asls->r2); ierr = VecPointwiseDivide(asls->r1,asls->r1,asls->r2);CHKERRQ(ierr); ierr = VecSet(tao->stepdirection,0.0);CHKERRQ(ierr); ierr = VecISAXPY(tao->stepdirection, asls->fixed,1.0,asls->r1);CHKERRQ(ierr); /* Our direction in the Fixed Variable Set is fixed. Calculate the information needed for the step in the Free Variable Set. To do this, we need to know the diagonal perturbation and the right hand side. */ ierr = TaoVecGetSubVec(asls->da, asls->free, tao->subset_type, 0.0, &asls->r1);CHKERRQ(ierr); ierr = TaoVecGetSubVec(asls->ff, asls->free, tao->subset_type, 0.0, &asls->r2);CHKERRQ(ierr); ierr = TaoVecGetSubVec(asls->db, asls->free, tao->subset_type, 1.0, &asls->r3);CHKERRQ(ierr); ierr = VecPointwiseDivide(asls->r1,asls->r1, asls->r3);CHKERRQ(ierr); ierr = VecPointwiseDivide(asls->r2,asls->r2, asls->r3);CHKERRQ(ierr); /* r1 is the diagonal perturbation r2 is the right hand side r3 is no longer needed Now need to modify r2 for our direction choice in the fixed variable set: calculate t1 = J*d, take the reduced vector of t1 and modify r2. */ ierr = MatMult(tao->jacobian, tao->stepdirection, asls->t1);CHKERRQ(ierr); ierr = TaoVecGetSubVec(asls->t1,asls->free,tao->subset_type,0.0,&asls->r3);CHKERRQ(ierr); ierr = VecAXPY(asls->r2, -1.0, asls->r3);CHKERRQ(ierr); /* Calculate the reduced problem matrix and the direction */ if (!asls->w && (tao->subset_type == TAO_SUBSET_MASK || tao->subset_type == TAO_SUBSET_MATRIXFREE)) { ierr = VecDuplicate(tao->solution, &asls->w);CHKERRQ(ierr); } ierr = TaoMatGetSubMat(tao->jacobian, asls->free, asls->w, tao->subset_type,&asls->J_sub);CHKERRQ(ierr); if (tao->jacobian != tao->jacobian_pre) { ierr = TaoMatGetSubMat(tao->jacobian_pre, asls->free, asls->w, tao->subset_type, &asls->Jpre_sub);CHKERRQ(ierr); } else { ierr = MatDestroy(&asls->Jpre_sub);CHKERRQ(ierr); asls->Jpre_sub = asls->J_sub; ierr = PetscObjectReference((PetscObject)(asls->Jpre_sub));CHKERRQ(ierr); } ierr = MatDiagonalSet(asls->J_sub, asls->r1,ADD_VALUES);CHKERRQ(ierr); ierr = TaoVecGetSubVec(tao->stepdirection, asls->free, tao->subset_type, 0.0, &asls->dxfree);CHKERRQ(ierr); ierr = VecSet(asls->dxfree, 0.0);CHKERRQ(ierr); /* Calculate the reduced direction. (Really negative of Newton direction. Therefore, rest of the code uses -d.) */ ierr = KSPReset(tao->ksp); ierr = KSPSetOperators(tao->ksp, asls->J_sub, asls->Jpre_sub);CHKERRQ(ierr); ierr = KSPSolve(tao->ksp, asls->r2, asls->dxfree);CHKERRQ(ierr); /* Add the direction in the free variables back into the real direction. */ ierr = VecISAXPY(tao->stepdirection, asls->free, 1.0,asls->dxfree);CHKERRQ(ierr); /* Check the real direction for descent and if not, use the negative gradient direction. */ ierr = VecNorm(tao->stepdirection, NORM_2, &normd);CHKERRQ(ierr); ierr = VecDot(tao->stepdirection, asls->dpsi, &innerd);CHKERRQ(ierr); if (innerd <= asls->delta*pow(normd, asls->rho)) { ierr = PetscInfo1(tao,"Gradient direction: %5.4e.\n", (double)innerd);CHKERRQ(ierr); ierr = PetscInfo1(tao, "Iteration %D: newton direction not descent\n", iter);CHKERRQ(ierr); ierr = VecCopy(asls->dpsi, tao->stepdirection);CHKERRQ(ierr); ierr = VecDot(asls->dpsi, tao->stepdirection, &innerd);CHKERRQ(ierr); } ierr = VecScale(tao->stepdirection, -1.0);CHKERRQ(ierr); innerd = -innerd; /* We now have a correct descent direction. Apply a linesearch to find the new iterate. */ ierr = TaoLineSearchSetInitialStepLength(tao->linesearch, 1.0);CHKERRQ(ierr); ierr = TaoLineSearchApply(tao->linesearch, tao->solution, &psi,asls->dpsi, tao->stepdirection, &t, &ls_reason);CHKERRQ(ierr); ierr = VecNorm(asls->dpsi, NORM_2, &ndpsi);CHKERRQ(ierr); } PetscFunctionReturn(0); }