PetscErrorCode MatMult_SMF(Mat mat,Vec a,Vec y) { MatSubMatFreeCtx ctx; PetscErrorCode ierr; PetscFunctionBegin; ierr = MatShellGetContext(mat,(void **)&ctx);CHKERRQ(ierr); ierr = VecCopy(a,ctx->VR);CHKERRQ(ierr); ierr = VecISSet(ctx->VR,ctx->Cols,0.0);CHKERRQ(ierr); ierr = MatMult(ctx->A,ctx->VR,y);CHKERRQ(ierr); ierr = VecISSet(y,ctx->Rows,0.0);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode TaoSolve_BNTR(Tao tao) { PetscErrorCode ierr; TAO_BNK *bnk = (TAO_BNK *)tao->data; KSPConvergedReason ksp_reason; PetscReal oldTrust, prered, actred, steplen, resnorm; PetscBool cgTerminate, needH = PETSC_TRUE, stepAccepted, shift = PETSC_FALSE; PetscInt stepType, nDiff; PetscFunctionBegin; /* Initialize the preconditioner, KSP solver and trust radius/line search */ tao->reason = TAO_CONTINUE_ITERATING; ierr = TaoBNKInitialize(tao, bnk->init_type, &needH);CHKERRQ(ierr); if (tao->reason != TAO_CONTINUE_ITERATING) PetscFunctionReturn(0); /* Have not converged; continue with Newton method */ while (tao->reason == TAO_CONTINUE_ITERATING) { /* Call general purpose update function */ if (tao->ops->update) { ierr = (*tao->ops->update)(tao, tao->niter, tao->user_update);CHKERRQ(ierr); } ++tao->niter; if (needH && bnk->inactive_idx) { /* Take BNCG steps (if enabled) to trade-off Hessian evaluations for more gradient evaluations */ ierr = TaoBNKTakeCGSteps(tao, &cgTerminate);CHKERRQ(ierr); if (cgTerminate) { tao->reason = bnk->bncg->reason; PetscFunctionReturn(0); } /* Compute the hessian and update the BFGS preconditioner at the new iterate */ ierr = (*bnk->computehessian)(tao);CHKERRQ(ierr); needH = PETSC_FALSE; } /* Store current solution before it changes */ bnk->fold = bnk->f; ierr = VecCopy(tao->solution, bnk->Xold);CHKERRQ(ierr); ierr = VecCopy(tao->gradient, bnk->Gold);CHKERRQ(ierr); ierr = VecCopy(bnk->unprojected_gradient, bnk->unprojected_gradient_old);CHKERRQ(ierr); /* Enter into trust region loops */ stepAccepted = PETSC_FALSE; while (!stepAccepted && tao->reason == TAO_CONTINUE_ITERATING) { tao->ksp_its=0; /* Use the common BNK kernel to compute the Newton step (for inactive variables only) */ ierr = (*bnk->computestep)(tao, shift, &ksp_reason, &stepType);CHKERRQ(ierr); /* Temporarily accept the step and project it into the bounds */ ierr = VecAXPY(tao->solution, 1.0, tao->stepdirection);CHKERRQ(ierr); ierr = TaoBoundSolution(tao->solution, tao->XL,tao->XU, 0.0, &nDiff, tao->solution);CHKERRQ(ierr); /* Check if the projection changed the step direction */ if (nDiff > 0) { /* Projection changed the step, so we have to recompute the step and the predicted reduction. Leave the trust radius unchanged. */ ierr = VecCopy(tao->solution, tao->stepdirection);CHKERRQ(ierr); ierr = VecAXPY(tao->stepdirection, -1.0, bnk->Xold);CHKERRQ(ierr); ierr = TaoBNKRecomputePred(tao, tao->stepdirection, &prered);CHKERRQ(ierr); } else { /* Step did not change, so we can just recover the pre-computed prediction */ ierr = KSPCGGetObjFcn(tao->ksp, &prered);CHKERRQ(ierr); } prered = -prered; /* Compute the actual reduction and update the trust radius */ ierr = TaoComputeObjective(tao, tao->solution, &bnk->f);CHKERRQ(ierr); if (PetscIsInfOrNanReal(bnk->f)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN"); actred = bnk->fold - bnk->f; oldTrust = tao->trust; ierr = TaoBNKUpdateTrustRadius(tao, prered, actred, bnk->update_type, stepType, &stepAccepted);CHKERRQ(ierr); if (stepAccepted) { /* Step is good, evaluate the gradient and flip the need-Hessian switch */ steplen = 1.0; needH = PETSC_TRUE; ++bnk->newt; ierr = TaoComputeGradient(tao, tao->solution, bnk->unprojected_gradient);CHKERRQ(ierr); ierr = TaoBNKEstimateActiveSet(tao, bnk->as_type);CHKERRQ(ierr); ierr = VecCopy(bnk->unprojected_gradient, tao->gradient);CHKERRQ(ierr); ierr = VecISSet(tao->gradient, bnk->active_idx, 0.0);CHKERRQ(ierr); ierr = TaoGradientNorm(tao, tao->gradient, NORM_2, &bnk->gnorm);CHKERRQ(ierr); } else { /* Step is bad, revert old solution and re-solve with new radius*/ steplen = 0.0; needH = PETSC_FALSE; bnk->f = bnk->fold; ierr = VecCopy(bnk->Xold, tao->solution);CHKERRQ(ierr); ierr = VecCopy(bnk->Gold, tao->gradient);CHKERRQ(ierr); ierr = VecCopy(bnk->unprojected_gradient_old, bnk->unprojected_gradient);CHKERRQ(ierr); if (oldTrust == tao->trust) { /* Can't change the radius anymore so just terminate */ tao->reason = TAO_DIVERGED_TR_REDUCTION; } } /* Check for termination */ ierr = VecFischer(tao->solution, bnk->unprojected_gradient, tao->XL, tao->XU, bnk->W);CHKERRQ(ierr); ierr = VecNorm(bnk->W, NORM_2, &resnorm);CHKERRQ(ierr); if (PetscIsInfOrNanReal(resnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN"); ierr = TaoLogConvergenceHistory(tao, bnk->f, resnorm, 0.0, tao->ksp_its);CHKERRQ(ierr); ierr = TaoMonitor(tao, tao->niter, bnk->f, resnorm, 0.0, steplen);CHKERRQ(ierr); ierr = (*tao->ops->convergencetest)(tao, tao->cnvP);CHKERRQ(ierr); } } PetscFunctionReturn(0); }
PETSC_EXTERN void PETSC_STDCALL vecisset_(Vec V,IS S,PetscScalar *c, int *__ierr ){ *__ierr = VecISSet( (Vec)PetscToPointer((V) ), (IS)PetscToPointer((S) ),*c); }