本文整理汇总了C++中VecMAXPY函数的典型用法代码示例。如果您正苦于以下问题:C++ VecMAXPY函数的具体用法?C++ VecMAXPY怎么用?C++ VecMAXPY使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了VecMAXPY函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: TSAdjointStep_RK
static PetscErrorCode TSAdjointStep_RK(TS ts)
{
TS_RK *rk = (TS_RK*)ts->data;
RKTableau tab = rk->tableau;
const PetscInt s = tab->s;
const PetscReal *A = tab->A,*b = tab->b,*c = tab->c;
PetscScalar *w = rk->work;
Vec *Y = rk->Y,*VecDeltaLam = rk->VecDeltaLam,*VecDeltaMu = rk->VecDeltaMu,*VecSensiTemp = rk->VecSensiTemp;
PetscInt i,j,nadj;
PetscReal t;
PetscErrorCode ierr;
PetscReal h = ts->time_step;
Mat J,Jp;
PetscFunctionBegin;
t = ts->ptime;
rk->status = TS_STEP_INCOMPLETE;
h = ts->time_step;
ierr = TSPreStep(ts);CHKERRQ(ierr);
for (i=s-1; i>=0; i--) {
rk->stage_time = t + h*(1.0-c[i]);
for (nadj=0; nadj<ts->numcost; nadj++) {
ierr = VecCopy(ts->vecs_sensi[nadj],VecSensiTemp[nadj]);CHKERRQ(ierr);
ierr = VecScale(VecSensiTemp[nadj],-h*b[i]);
for (j=i+1; j<s; j++) {
ierr = VecAXPY(VecSensiTemp[nadj],-h*A[j*s+i],VecDeltaLam[nadj*s+j]);
}
}
/* Stage values of lambda */
ierr = TSGetRHSJacobian(ts,&J,&Jp,NULL,NULL);CHKERRQ(ierr);
ierr = TSComputeRHSJacobian(ts,rk->stage_time,Y[i],J,Jp);CHKERRQ(ierr);
for (nadj=0; nadj<ts->numcost; nadj++) {
ierr = MatMultTranspose(J,VecSensiTemp[nadj],VecDeltaLam[nadj*s+i]);CHKERRQ(ierr);
}
/* Stage values of mu */
if(ts->vecs_sensip) {
ierr = TSAdjointComputeRHSJacobian(ts,rk->stage_time,Y[i],ts->Jacp);CHKERRQ(ierr);
for (nadj=0; nadj<ts->numcost; nadj++) {
ierr = MatMultTranspose(ts->Jacp,VecSensiTemp[nadj],VecDeltaMu[nadj*s+i]);CHKERRQ(ierr);
}
}
}
for (j=0; j<s; j++) w[j] = 1.0;
for (nadj=0; nadj<ts->numcost; nadj++) {
ierr = VecMAXPY(ts->vecs_sensi[nadj],s,w,&VecDeltaLam[nadj*s]);CHKERRQ(ierr);
if(ts->vecs_sensip) {
ierr = VecMAXPY(ts->vecs_sensip[nadj],s,w,&VecDeltaMu[nadj*s]);CHKERRQ(ierr);
}
}
ts->ptime += ts->time_step;
ts->steps++;
rk->status = TS_STEP_COMPLETE;
PetscFunctionReturn(0);
}
示例2: SNESMSStep_3Sstar
/*
X - initial state, updated in-place.
F - residual, computed at the initial X on input
*/
static PetscErrorCode SNESMSStep_3Sstar(SNES snes,Vec X,Vec F)
{
PetscErrorCode ierr;
SNES_MS *ms = (SNES_MS*)snes->data;
SNESMSTableau t = ms->tableau;
const PetscReal *gamma = t->gamma,*delta = t->delta,*betasub = t->betasub;
Vec S1,S2,S3,Y;
PetscInt i,nstages = t->nstages;;
PetscFunctionBegin;
Y = snes->work[0];
S1 = X;
S2 = snes->work[1];
S3 = snes->work[2];
ierr = VecZeroEntries(S2);CHKERRQ(ierr);
ierr = VecCopy(X,S3);CHKERRQ(ierr);
for (i=0; i<nstages; i++) {
Vec Ss[4] = {S1,S2,S3,Y};
PetscScalar scoeff[4] = {gamma[0*nstages+i]-1.0,gamma[1*nstages+i],gamma[2*nstages+i],-betasub[i]*ms->damping};
ierr = VecAXPY(S2,delta[i],S1);CHKERRQ(ierr);
if (i>0) {
ierr = SNESComputeFunction(snes,S1,F);CHKERRQ(ierr);
if (snes->domainerror) {
snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN;
PetscFunctionReturn(0);
}
}
ierr = SNES_KSPSolve(snes,snes->ksp,F,Y);CHKERRQ(ierr);
ierr = VecMAXPY(S1,4,scoeff,Ss);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
示例3: context
/*@C
MatNullSpaceRemove - Removes all the components of a null space from a vector.
Collective on MatNullSpace
Input Parameters:
+ sp - the null space context (if this is NULL then no null space is removed)
- vec - the vector from which the null space is to be removed
Level: advanced
.keywords: PC, null space, remove
.seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction()
@*/
PetscErrorCode MatNullSpaceRemove(MatNullSpace sp,Vec vec)
{
PetscScalar sum;
PetscInt i,N;
PetscErrorCode ierr;
PetscFunctionBegin;
if (!sp) PetscFunctionReturn(0);
PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1);
PetscValidHeaderSpecific(vec,VEC_CLASSID,2);
if (sp->has_cnst) {
ierr = VecGetSize(vec,&N);CHKERRQ(ierr);
if (N > 0) {
ierr = VecSum(vec,&sum);CHKERRQ(ierr);
sum = sum/((PetscScalar)(-1.0*N));
ierr = VecShift(vec,sum);CHKERRQ(ierr);
}
}
if (sp->n) {
ierr = VecMDot(vec,sp->n,sp->vecs,sp->alpha);CHKERRQ(ierr);
for (i=0; i<sp->n; i++) sp->alpha[i] = -sp->alpha[i];
ierr = VecMAXPY(vec,sp->n,sp->alpha,sp->vecs);CHKERRQ(ierr);
}
if (sp->remove) {
ierr = (*sp->remove)(sp,vec,sp->rmctx);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
示例4: TSEvaluateWLTE_Alpha
static PetscErrorCode TSEvaluateWLTE_Alpha(TS ts,NormType wnormtype,PetscInt *order,PetscReal *wlte)
{
TS_Alpha *th = (TS_Alpha*)ts->data;
Vec X = th->X1; /* X = solution */
Vec V = th->V1; /* V = solution */
Vec Y = th->vec_lte_work[0]; /* Y = X + LTE */
Vec Z = th->vec_lte_work[1]; /* Z = V + LTE */
PetscReal enormX,enormV;
PetscErrorCode ierr;
PetscFunctionBegin;
if (ts->steprestart) {
/* th->vec_{sol|dot}_prev is set to the LTE in TSAlpha_Restart() */
ierr = VecAXPY(Y,1,X);CHKERRQ(ierr);
ierr = VecAXPY(Z,1,V);CHKERRQ(ierr);
} else {
/* Compute LTE using backward differences with non-constant time step */
PetscReal h = ts->time_step, h_prev = ts->ptime - ts->ptime_prev;
PetscReal a = 1 + h_prev/h;
PetscScalar scal[3]; Vec vecX[3],vecV[3];
scal[0] = +1/a; scal[1] = -1/(a-1); scal[2] = +1/(a*(a-1));
vecX[0] = th->X1; vecX[1] = th->X0; vecX[2] = th->vec_sol_prev;
vecV[0] = th->V1; vecV[1] = th->V0; vecV[2] = th->vec_dot_prev;
ierr = VecCopy(X,Y);CHKERRQ(ierr);
ierr = VecMAXPY(Y,3,scal,vecX);CHKERRQ(ierr);
ierr = VecCopy(V,Z);CHKERRQ(ierr);
ierr = VecMAXPY(Z,3,scal,vecV);CHKERRQ(ierr);
}
/* XXX ts->atol and ts->vatol are not appropriate for computing enormV */
ierr = TSErrorWeightedNorm(ts,X,Y,wnormtype,&enormX);CHKERRQ(ierr);
ierr = TSErrorWeightedNorm(ts,V,Z,wnormtype,&enormV);CHKERRQ(ierr);
if (wnormtype == NORM_2)
*wlte = PetscSqrtReal(PetscSqr(enormX)/2 + PetscSqr(enormV)/2);
else
*wlte = PetscMax(enormX,enormV);
if (order) *order = 2;
PetscFunctionReturn(0);
}
示例5: KSPFischerGuessUpdate_Method1
PetscErrorCode KSPFischerGuessUpdate_Method1(KSPFischerGuess_Method1 *itg,Vec x)
{
PetscReal norm;
PetscErrorCode ierr;
int curl = itg->curl,i;
PetscFunctionBegin;
PetscValidHeaderSpecific(x,VEC_CLASSID,2);
PetscValidPointer(itg,3);
if (curl == itg->maxl) {
ierr = KSP_MatMult(itg->ksp,itg->mat,x,itg->btilde[0]);CHKERRQ(ierr);
ierr = VecNormalize(itg->btilde[0],&norm);CHKERRQ(ierr);
ierr = VecCopy(x,itg->xtilde[0]);CHKERRQ(ierr);
ierr = VecScale(itg->xtilde[0],1.0/norm);CHKERRQ(ierr);
itg->curl = 1;
} else {
if (!curl) {
ierr = VecCopy(x,itg->xtilde[curl]);CHKERRQ(ierr);
} else {
ierr = VecWAXPY(itg->xtilde[curl],-1.0,itg->guess,x);CHKERRQ(ierr);
}
ierr = KSP_MatMult(itg->ksp,itg->mat,itg->xtilde[curl],itg->btilde[curl]);CHKERRQ(ierr);
ierr = VecMDot(itg->btilde[curl],curl,itg->btilde,itg->alpha);CHKERRQ(ierr);
for (i=0; i<curl; i++) itg->alpha[i] = -itg->alpha[i];
ierr = VecMAXPY(itg->btilde[curl],curl,itg->alpha,itg->btilde);CHKERRQ(ierr);
ierr = VecMAXPY(itg->xtilde[curl],curl,itg->alpha,itg->xtilde);CHKERRQ(ierr);
ierr = VecNormalize(itg->btilde[curl],&norm);CHKERRQ(ierr);
if (norm) {
ierr = VecScale(itg->xtilde[curl],1.0/norm);CHKERRQ(ierr);
itg->curl++;
} else {
ierr = PetscInfo(itg->ksp,"Not increasing dimension of Fischer space because new direction is identical to previous\n");CHKERRQ(ierr);
}
}
PetscFunctionReturn(0);
}
示例6: KSPGMRESBuildSoln
static PetscErrorCode KSPGMRESBuildSoln(PetscScalar *nrs,Vec vs,Vec vdest,KSP ksp,PetscInt it)
{
PetscScalar tt;
PetscErrorCode ierr;
PetscInt ii,k,j;
KSP_GMRES *gmres = (KSP_GMRES*)(ksp->data);
PetscFunctionBegin;
/* Solve for solution vector that minimizes the residual */
/* If it is < 0, no gmres steps have been performed */
if (it < 0) {
ierr = VecCopy(vs,vdest);CHKERRQ(ierr); /* VecCopy() is smart, exists immediately if vguess == vdest */
PetscFunctionReturn(0);
}
if (*HH(it,it) != 0.0) {
nrs[it] = *GRS(it) / *HH(it,it);
} else {
ksp->reason = KSP_DIVERGED_BREAKDOWN;
ierr = PetscInfo2(ksp,"Likely your matrix or preconditioner is singular. HH(it,it) is identically zero; it = %D GRS(it) = %G",it,PetscAbsScalar(*GRS(it)));CHKERRQ(ierr);
PetscFunctionReturn(0);
}
for (ii=1; ii<=it; ii++) {
k = it - ii;
tt = *GRS(k);
for (j=k+1; j<=it; j++) tt = tt - *HH(k,j) * nrs[j];
if (*HH(k,k) == 0.0) {
ksp->reason = KSP_DIVERGED_BREAKDOWN;
ierr = PetscInfo1(ksp,"Likely your matrix or preconditioner is singular. HH(k,k) is identically zero; k = %D",k);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
nrs[k] = tt / *HH(k,k);
}
/* Accumulate the correction to the solution of the preconditioned problem in TEMP */
ierr = VecSet(VEC_TEMP,0.0);CHKERRQ(ierr);
ierr = VecMAXPY(VEC_TEMP,it+1,nrs,&VEC_VV(0));CHKERRQ(ierr);
ierr = KSPUnwindPreconditioner(ksp,VEC_TEMP,VEC_TEMP_MATOP);CHKERRQ(ierr);
/* add solution to previous solution */
if (vdest != vs) {
ierr = VecCopy(vs,vdest);CHKERRQ(ierr);
}
ierr = VecAXPY(vdest,1.0,VEC_TEMP);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例7: KSPFGMRESBuildSoln
static PetscErrorCode KSPFGMRESBuildSoln(PetscScalar *nrs,Vec vguess,Vec vdest,KSP ksp,PetscInt it)
{
PetscScalar tt;
PetscErrorCode ierr;
PetscInt ii,k,j;
KSP_FGMRES *fgmres = (KSP_FGMRES*)(ksp->data);
PetscFunctionBegin;
/* Solve for solution vector that minimizes the residual */
/* If it is < 0, no fgmres steps have been performed */
if (it < 0) {
ierr = VecCopy(vguess,vdest);CHKERRQ(ierr); /* VecCopy() is smart, exists immediately if vguess == vdest */
PetscFunctionReturn(0);
}
/* so fgmres steps HAVE been performed */
/* solve the upper triangular system - RS is the right side and HH is
the upper triangular matrix - put soln in nrs */
if (*HH(it,it) != 0.0) {
nrs[it] = *RS(it) / *HH(it,it);
} else {
nrs[it] = 0.0;
}
for (ii=1; ii<=it; ii++) {
k = it - ii;
tt = *RS(k);
for (j=k+1; j<=it; j++) tt = tt - *HH(k,j) * nrs[j];
nrs[k] = tt / *HH(k,k);
}
/* Accumulate the correction to the soln of the preconditioned prob. in
VEC_TEMP - note that we use the preconditioned vectors */
ierr = VecSet(VEC_TEMP,0.0);CHKERRQ(ierr); /* set VEC_TEMP components to 0 */
ierr = VecMAXPY(VEC_TEMP,it+1,nrs,&PREVEC(0));CHKERRQ(ierr);
/* put updated solution into vdest.*/
if (vdest != vguess) {
ierr = VecCopy(VEC_TEMP,vdest);CHKERRQ(ierr);
ierr = VecAXPY(vdest,1.0,vguess);CHKERRQ(ierr);
} else { /* replace guess with solution */
ierr = VecAXPY(vdest,1.0,VEC_TEMP);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
示例8: homemade_assert_msg
void FETI_Operations::apply_RB_projection(Vec vec_in, Vec vec_out)
{
homemade_assert_msg(m_bNullVecsSet,"Null space vectors not set yet!");
homemade_assert_msg(m_binvRITRIMatSet,"Null space matrices not set yet!");
// vec_out = [ I - RC * (inv_RITRI_mat) * RC^t ] * vec_in
// Declaration of Vecs with size 'm_null_nb_vecs'
Vec dummy_seq_vec;
Vec dummy_seq_vec_bis;
VecCreateSeq(PETSC_COMM_SELF,m_null_nb_vecs,&dummy_seq_vec);
VecZeroEntries(dummy_seq_vec);
VecDuplicate(dummy_seq_vec,&dummy_seq_vec_bis);
// dummy_seq_vec = RC^t * vec_in
// -> All the communications are done here!
PetscScalar *dummy_seq_array;
VecGetArray(dummy_seq_vec,&dummy_seq_array);
VecMDot(vec_in,m_null_nb_vecs,m_null_coupled_vecs,dummy_seq_array);
VecRestoreArray(dummy_seq_vec,&dummy_seq_array);
// dummy_seq_vec_bis = - inv_RITRI_mat * dummy_seq_vec
// -> Calculate dummy_seq_vec_bis on the first proc, and then broadcast the value
/*
* Originally, this operation was done locally, but due to a syncing issue,
* we have to do it this way to avoid a "Value must the same in all processors" error
* when calling VecMAXPY below.
*/
PETSC_MatMultScale_Bcast(m_inv_RITRI_mat,dummy_seq_vec,dummy_seq_vec_bis,-1);
m_comm.barrier();
// vec_out = vec_in + sum ( dummy_seq_vec_bis[i] * vec_RC[i])
// -> This should have no communications at all!
VecCopy(vec_in,vec_out);
VecGetArray(dummy_seq_vec_bis,&dummy_seq_array);
VecMAXPY(vec_out,m_null_nb_vecs,dummy_seq_array,m_null_coupled_vecs);
VecRestoreArray(dummy_seq_vec_bis,&dummy_seq_array);
// Cleanup
VecDestroy(&dummy_seq_vec);
VecDestroy(&dummy_seq_vec_bis);
}
示例9: KSPFischerGuessFormGuess_Method1
PetscErrorCode KSPFischerGuessFormGuess_Method1(KSPFischerGuess_Method1 *itg,Vec b,Vec x)
{
PetscErrorCode ierr;
PetscInt i;
PetscFunctionBegin;
PetscValidPointer(itg,2);
PetscValidHeaderSpecific(x,VEC_CLASSID,3);
ierr = VecSet(x,0.0);CHKERRQ(ierr);
ierr = VecMDot(b,itg->curl,itg->btilde,itg->alpha);CHKERRQ(ierr);
if (itg->monitor) {
ierr = PetscPrintf(((PetscObject)itg->ksp)->comm,"KSPFischerGuess alphas = ");CHKERRQ(ierr);
for (i=0; i<itg->curl; i++) {
ierr = PetscPrintf(((PetscObject)itg->ksp)->comm,"%g ",(double)PetscAbsScalar(itg->alpha[i]));CHKERRQ(ierr);
}
ierr = PetscPrintf(((PetscObject)itg->ksp)->comm,"\n");CHKERRQ(ierr);
}
ierr = VecMAXPY(x,itg->curl,itg->alpha,itg->xtilde);CHKERRQ(ierr);
ierr = VecCopy(x,itg->guess);CHKERRQ(ierr);
/* Note: do not change the b right hand side as is done in the publication */
PetscFunctionReturn(0);
}
示例10: KSPPGMRESBuildSoln
static PetscErrorCode KSPPGMRESBuildSoln(PetscScalar *nrs,Vec vguess,Vec vdest,KSP ksp,PetscInt it)
{
PetscScalar tt;
PetscErrorCode ierr;
PetscInt k,j;
KSP_PGMRES *pgmres = (KSP_PGMRES*)(ksp->data);
PetscFunctionBegin;
/* Solve for solution vector that minimizes the residual */
if (it < 0) { /* no pgmres steps have been performed */
ierr = VecCopy(vguess,vdest);CHKERRQ(ierr); /* VecCopy() is smart, exits immediately if vguess == vdest */
PetscFunctionReturn(0);
}
/* solve the upper triangular system - RS is the right side and HH is
the upper triangular matrix - put soln in nrs */
if (*HH(it,it) != 0.0) nrs[it] = *RS(it) / *HH(it,it);
else nrs[it] = 0.0;
for (k=it-1; k>=0; k--) {
tt = *RS(k);
for (j=k+1; j<=it; j++) tt -= *HH(k,j) * nrs[j];
nrs[k] = tt / *HH(k,k);
}
/* Accumulate the correction to the solution of the preconditioned problem in TEMP */
ierr = VecZeroEntries(VEC_TEMP);CHKERRQ(ierr);
ierr = VecMAXPY(VEC_TEMP,it+1,nrs,&VEC_VV(0));CHKERRQ(ierr);
ierr = KSPUnwindPreconditioner(ksp,VEC_TEMP,VEC_TEMP_MATOP);CHKERRQ(ierr);
/* add solution to previous solution */
if (vdest == vguess) {
ierr = VecAXPY(vdest,1.0,VEC_TEMP);CHKERRQ(ierr);
} else {
ierr = VecWAXPY(vdest,1.0,VEC_TEMP,vguess);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
示例11: SNESMSStep_3Sstar
/*
X - initial state, updated in-place.
F - residual, computed at the initial X on input
*/
static PetscErrorCode SNESMSStep_3Sstar(SNES snes,Vec X,Vec F)
{
PetscErrorCode ierr;
SNES_MS *ms = (SNES_MS*)snes->data;
SNESMSTableau t = ms->tableau;
const PetscReal *gamma = t->gamma,*delta = t->delta,*betasub = t->betasub;
Vec S1,S2,S3,Y;
PetscInt i,nstages = t->nstages;;
PetscFunctionBegin;
Y = snes->work[0];
S1 = X;
S2 = snes->work[1];
S3 = snes->work[2];
ierr = VecZeroEntries(S2);CHKERRQ(ierr);
ierr = VecCopy(X,S3);CHKERRQ(ierr);
for (i=0; i<nstages; i++) {
Vec Ss[4];
PetscScalar scoeff[4];
Ss[0] = S1; Ss[1] = S2; Ss[2] = S3; Ss[3] = Y;
scoeff[0] = gamma[0*nstages+i]-(PetscReal)1.0;
scoeff[1] = gamma[1*nstages+i];
scoeff[2] = gamma[2*nstages+i];
scoeff[3] = -betasub[i]*ms->damping;
ierr = VecAXPY(S2,delta[i],S1);CHKERRQ(ierr);
if (i>0) {
ierr = SNESComputeFunction(snes,S1,F);CHKERRQ(ierr);
}
ierr = KSPSolve(snes->ksp,F,Y);CHKERRQ(ierr);
ierr = VecMAXPY(S1,4,scoeff,Ss);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
示例12: interpTimeDependentVector
PetscErrorCode interpTimeDependentVector(PetscScalar tc, Vec *u, PetscInt numTracers,
PetscInt nt, PetscScalar *t, Vec **ut)
{
PetscInt itf, itr;
PetscErrorCode ierr;
PetscScalar alpha[2];
PetscScalar zero=0.0;
for (itr=0; itr<numTracers; itr++) {
ierr = VecSet(u[itr],zero); CHKERRQ(ierr);
}
if (tc>=t[0]) {
ierr = calcInterpFactor(nt,tc,t,&itf,&alpha[0]); CHKERRQ(ierr);
alpha[1]=1.0-alpha[0];
for (itr=0; itr<numTracers; itr++) {
VecMAXPY(u[itr],2,alpha,&ut[itr][itf]);
}
} else {
ierr = PetscPrintf(PETSC_COMM_WORLD,"Warning: time < %10.5f. Assuming u=0\n", t[0]);CHKERRQ(ierr);
}
return 0;
}
示例13: KSPSolve_GCR_cycle
PetscErrorCode KSPSolve_GCR_cycle( KSP ksp )
{
KSP_GCR *ctx = (KSP_GCR*)ksp->data;
PetscErrorCode ierr;
PetscScalar r_dot_v;
Mat A, B;
PC pc;
Vec s,v,r;
PetscReal norm_r,nrm;
PetscInt k, i, restart;
Vec x;
PetscReal res;
PetscFunctionBegin;
restart = ctx->restart;
ierr = KSPGetPC( ksp, &pc );CHKERRQ(ierr);
ierr = KSPGetOperators( ksp, &A, &B, 0 );CHKERRQ(ierr);
x = ksp->vec_sol;
r = ctx->R;
for ( k=0; k<restart; k++ ) {
v = ctx->VV[k];
s = ctx->SS[k];
if (ctx->modifypc) {
ierr = (*ctx->modifypc)(ksp,ksp->its,ksp->rnorm,ctx->modifypc_ctx);CHKERRQ(ierr);
}
ierr = PCApply( pc, r, s );CHKERRQ(ierr); /* s = B^{-1} r */
ierr = MatMult( A, s, v );CHKERRQ(ierr); /* v = A s */
ierr = VecMDot( v,k, ctx->VV, ctx->val );CHKERRQ(ierr);
for (i=0; i<k; i++) ctx->val[i] = -ctx->val[i];
ierr = VecMAXPY(v,k,ctx->val,ctx->VV);CHKERRQ(ierr); /* v = v - sum_{i=0}^{k-1} alpha_i v_i */
ierr = VecMAXPY(s,k,ctx->val,ctx->SS);CHKERRQ(ierr); /* s = s - sum_{i=0}^{k-1} alpha_i s_i */
ierr = VecDotNorm2(r,v,&r_dot_v,&nrm);CHKERRQ(ierr);
nrm = PetscSqrtReal(nrm);
r_dot_v = r_dot_v/nrm;
ierr = VecScale( v, 1.0/nrm );CHKERRQ(ierr);
ierr = VecScale( s, 1.0/nrm );CHKERRQ(ierr);
ierr = VecAXPY( x, r_dot_v, s );CHKERRQ(ierr);
ierr = VecAXPY( r, -r_dot_v, v );CHKERRQ(ierr);
if (ksp->its > ksp->chknorm ) {
ierr = VecNorm( r, NORM_2, &norm_r );CHKERRQ(ierr);
}
/* update the local counter and the global counter */
ksp->its++;
res = norm_r;
ksp->rnorm = res;
KSPLogResidualHistory(ksp,res);
ierr = KSPMonitor(ksp,ksp->its,res);CHKERRQ(ierr);
if ( ksp->its > ksp->chknorm ) {
ierr = (*ksp->converged)(ksp,ksp->its,res,&ksp->reason,ksp->cnvP);CHKERRQ(ierr);
if (ksp->reason) break;
}
if ( ksp->its >= ksp->max_it ) {
ksp->reason = KSP_CONVERGED_ITS;
break;
}
}
ctx->n_restarts++;
PetscFunctionReturn(0);
}
示例14: F
//.........这里部分代码省略.........
PetscFunctionReturn(0);
}
}
while (next->next) {
i++;
next = next->next;
ierr = VecCopy(X,Xes[i]);CHKERRQ(ierr);
ierr = SNESSolve(next->snes,B,Xes[i]);CHKERRQ(ierr);
ierr = SNESGetConvergedReason(next->snes,&reason);CHKERRQ(ierr);
if (reason < 0 && reason != SNES_DIVERGED_MAX_IT) {
jac->innerFailures++;
if (jac->innerFailures >= snes->maxFailures) {
snes->reason = SNES_DIVERGED_INNER;
PetscFunctionReturn(0);
}
}
}
/* all the solutions are collected; combine optimally */
for (i=0;i<jac->n;i++) {
for (j=0;j<i+1;j++) {
ierr = VecDotBegin(Fes[i],Fes[j],&jac->h[i + j*jac->n]);CHKERRQ(ierr);
}
ierr = VecDotBegin(Fes[i],F,&jac->g[i]);CHKERRQ(ierr);
}
for (i=0;i<jac->n;i++) {
for (j=0;j<i+1;j++) {
ierr = VecDotEnd(Fes[i],Fes[j],&jac->h[i + j*jac->n]);CHKERRQ(ierr);
if (i == j) jac->fnorms[i] = PetscSqrtReal(PetscRealPart(jac->h[i + j*jac->n]));
}
ierr = VecDotEnd(Fes[i],F,&jac->g[i]);CHKERRQ(ierr);
}
ftf = (*fnorm)*(*fnorm);
for (i=0; i<jac->n; i++) {
for (j=i+1;j<jac->n;j++) {
jac->h[i + j*jac->n] = jac->h[j + i*jac->n];
}
}
for (i=0; i<jac->n; i++) {
for (j=0; j<jac->n; j++) {
jac->h[i + j*jac->n] = jac->h[i + j*jac->n] - jac->g[j] - jac->g[i] + ftf;
}
jac->beta[i] = ftf - jac->g[i];
}
#if defined(PETSC_MISSING_LAPACK_GELSS)
SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_SUP,"SNESCOMPOSITE with ADDITIVEOPTIMAL requires the LAPACK GELSS routine.");
#else
jac->info = 0;
jac->rcond = -1.;
ierr = PetscFPTrapPush(PETSC_FP_TRAP_OFF);CHKERRQ(ierr);
#if defined(PETSC_USE_COMPLEX)
PetscStackCall("LAPACKgelss",LAPACKgelss_(&jac->n,&jac->n,&jac->nrhs,jac->h,&jac->lda,jac->beta,&jac->lda,jac->s,&jac->rcond,&jac->rank,jac->work,&jac->lwork,jac->rwork,&jac->info));
#else
PetscStackCall("LAPACKgelss",LAPACKgelss_(&jac->n,&jac->n,&jac->nrhs,jac->h,&jac->lda,jac->beta,&jac->lda,jac->s,&jac->rcond,&jac->rank,jac->work,&jac->lwork,&jac->info));
#endif
ierr = PetscFPTrapPop();CHKERRQ(ierr);
if (jac->info < 0) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_LIB,"Bad argument to GELSS");
if (jac->info > 0) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_LIB,"SVD failed to converge");
#endif
tot = 0.;
total = 0.;
for (i=0; i<jac->n; i++) {
if (snes->errorifnotconverged && PetscIsInfOrNanScalar(jac->beta[i])) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_LIB,"SVD generated inconsistent output");
ierr = PetscInfo2(snes,"%D: %g\n",i,(double)PetscRealPart(jac->beta[i]));CHKERRQ(ierr);
tot += jac->beta[i];
total += PetscAbsScalar(jac->beta[i]);
}
ierr = VecScale(X,(1. - tot));CHKERRQ(ierr);
ierr = VecMAXPY(X,jac->n,jac->beta,Xes);CHKERRQ(ierr);
ierr = SNESComputeFunction(snes,X,F);CHKERRQ(ierr);
if (snes->xl && snes->xu) {
ierr = SNESVIComputeInactiveSetFnorm(snes, F, X, fnorm);CHKERRQ(ierr);
} else {
ierr = VecNorm(F, NORM_2, fnorm);CHKERRQ(ierr);
}
/* take the minimum-normed candidate if it beats the combination by a factor of rtol or the combination has stagnated */
min_fnorm = jac->fnorms[0];
min_i = 0;
for (i=0; i<jac->n; i++) {
if (jac->fnorms[i] < min_fnorm) {
min_fnorm = jac->fnorms[i];
min_i = i;
}
}
/* stagnation or divergence restart to the solution of the solver that failed the least */
if (PetscRealPart(total) < jac->stol || min_fnorm*jac->rtol < *fnorm) {
ierr = VecCopy(jac->Xes[min_i],X);CHKERRQ(ierr);
ierr = VecCopy(jac->Fes[min_i],F);CHKERRQ(ierr);
*fnorm = min_fnorm;
}
PetscFunctionReturn(0);
}
示例15: SNESNGMRESFormCombinedSolution_Private
PetscErrorCode SNESNGMRESFormCombinedSolution_Private(SNES snes,PetscInt l,Vec XM,Vec FM,PetscReal fMnorm,Vec X,Vec XA,Vec FA)
{
SNES_NGMRES *ngmres = (SNES_NGMRES*) snes->data;
PetscInt i,j;
Vec *Fdot = ngmres->Fdot;
Vec *Xdot = ngmres->Xdot;
PetscScalar *beta = ngmres->beta;
PetscScalar *xi = ngmres->xi;
PetscScalar alph_total = 0.;
PetscErrorCode ierr;
PetscReal nu;
Vec Y = snes->work[2];
PetscBool changed_y,changed_w;
PetscFunctionBegin;
nu = fMnorm*fMnorm;
/* construct the right hand side and xi factors */
ierr = VecMDot(FM,l,Fdot,xi);CHKERRQ(ierr);
for (i = 0; i < l; i++) beta[i] = nu - xi[i];
/* construct h */
for (j = 0; j < l; j++) {
for (i = 0; i < l; i++) {
H(i,j) = Q(i,j)-xi[i]-xi[j]+nu;
}
}
if (l == 1) {
/* simply set alpha[0] = beta[0] / H[0, 0] */
if (H(0,0) != 0.) beta[0] = beta[0]/H(0,0);
else beta[0] = 0.;
} else {
#if defined(PETSC_MISSING_LAPACK_GELSS)
SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_SUP,"NGMRES with LS requires the LAPACK GELSS routine.");
#else
ierr = PetscBLASIntCast(l,&ngmres->m);CHKERRQ(ierr);
ierr = PetscBLASIntCast(l,&ngmres->n);CHKERRQ(ierr);
ngmres->info = 0;
ngmres->rcond = -1.;
ierr = PetscFPTrapPush(PETSC_FP_TRAP_OFF);CHKERRQ(ierr);
#if defined(PETSC_USE_COMPLEX)
PetscStackCall("LAPACKgelss",LAPACKgelss_(&ngmres->m,&ngmres->n,&ngmres->nrhs,ngmres->h,&ngmres->lda,ngmres->beta,&ngmres->ldb,ngmres->s,&ngmres->rcond,&ngmres->rank,ngmres->work,&ngmres->lwork,ngmres->rwork,&ngmres->info));
#else
PetscStackCall("LAPACKgelss",LAPACKgelss_(&ngmres->m,&ngmres->n,&ngmres->nrhs,ngmres->h,&ngmres->lda,ngmres->beta,&ngmres->ldb,ngmres->s,&ngmres->rcond,&ngmres->rank,ngmres->work,&ngmres->lwork,&ngmres->info));
#endif
ierr = PetscFPTrapPop();CHKERRQ(ierr);
if (ngmres->info < 0) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_LIB,"Bad argument to GELSS");
if (ngmres->info > 0) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_LIB,"SVD failed to converge");
#endif
}
for (i=0; i<l; i++) {
if (PetscIsInfOrNanScalar(beta[i])) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_LIB,"SVD generated inconsistent output");
}
alph_total = 0.;
for (i = 0; i < l; i++) alph_total += beta[i];
ierr = VecCopy(XM,XA);CHKERRQ(ierr);
ierr = VecScale(XA,1.-alph_total);CHKERRQ(ierr);
ierr = VecMAXPY(XA,l,beta,Xdot);CHKERRQ(ierr);
/* check the validity of the step */
ierr = VecCopy(XA,Y);CHKERRQ(ierr);
ierr = VecAXPY(Y,-1.0,X);CHKERRQ(ierr);
ierr = SNESLineSearchPostCheck(snes->linesearch,X,Y,XA,&changed_y,&changed_w);CHKERRQ(ierr);
if (!ngmres->approxfunc) {ierr = SNESComputeFunction(snes,XA,FA);CHKERRQ(ierr);}
else {
ierr = VecCopy(FM,FA);CHKERRQ(ierr);
ierr = VecScale(FA,1.-alph_total);CHKERRQ(ierr);
ierr = VecMAXPY(FA,l,beta,Fdot);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}