本文整理汇总了C++中VecZeroEntries函数的典型用法代码示例。如果您正苦于以下问题:C++ VecZeroEntries函数的具体用法?C++ VecZeroEntries怎么用?C++ VecZeroEntries使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了VecZeroEntries函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: SNESComputeFunction_DMLocal
static PetscErrorCode SNESComputeFunction_DMLocal(SNES snes,Vec X,Vec F,void *ctx)
{
PetscErrorCode ierr;
DM dm;
DMSNES_Local *dmlocalsnes = (DMSNES_Local*)ctx;
Vec Xloc,Floc;
PetscFunctionBegin;
PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
PetscValidHeaderSpecific(X,VEC_CLASSID,2);
PetscValidHeaderSpecific(F,VEC_CLASSID,3);
ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr);
ierr = DMGetLocalVector(dm,&Xloc);CHKERRQ(ierr);
ierr = DMGetLocalVector(dm,&Floc);CHKERRQ(ierr);
ierr = VecZeroEntries(Xloc);CHKERRQ(ierr);
ierr = VecZeroEntries(Floc);CHKERRQ(ierr);
ierr = DMGlobalToLocalBegin(dm,X,INSERT_VALUES,Xloc);CHKERRQ(ierr);
ierr = DMGlobalToLocalEnd(dm,X,INSERT_VALUES,Xloc);CHKERRQ(ierr);
CHKMEMQ;
ierr = (*dmlocalsnes->residuallocal)(dm,Xloc,Floc,dmlocalsnes->residuallocalctx);CHKERRQ(ierr);
CHKMEMQ;
ierr = VecZeroEntries(F);CHKERRQ(ierr);
ierr = DMLocalToGlobalBegin(dm,Floc,ADD_VALUES,F);CHKERRQ(ierr);
ierr = DMLocalToGlobalEnd(dm,Floc,ADD_VALUES,F);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(dm,&Floc);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(dm,&Xloc);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例2: f
/*
Evaluates \integral_{-1}^{1} f*v_i where v_i is the ith basis polynomial via the GLL nodes and weights, since the v_i
basis function is zero at all nodes except the ith one the integral is simply the weight_i * f(node_i)
*/
PetscErrorCode ComputeRhs(DM da,PetscGLL *gll,Vec b)
{
PetscErrorCode ierr;
PetscInt i,j,xs,xn,n = gll->n;
PetscScalar *bb,*xx;
PetscReal xd;
Vec blocal,xlocal;
PetscFunctionBegin;
ierr = DMDAGetCorners(da,&xs,NULL,NULL,&xn,NULL,NULL);CHKERRQ(ierr);
xs = xs/(n-1);
xn = xn/(n-1);
ierr = DMGetLocalVector(da,&blocal);CHKERRQ(ierr);
ierr = VecZeroEntries(blocal);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da,blocal,&bb);CHKERRQ(ierr);
ierr = DMGetCoordinatesLocal(da,&xlocal);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da,xlocal,&xx);CHKERRQ(ierr);
/* loop over local spectral elements */
for (j=xs; j<xs+xn; j++) {
/* loop over GLL points in each element */
for (i=0; i<n; i++) {
xd = xx[j*(n-1) + i];
bb[j*(n-1) + i] += -gll->weights[i]*(-20.*PETSC_PI*xd*PetscSinReal(5.*PETSC_PI*xd) + (2. - (5.*PETSC_PI)*(5.*PETSC_PI)*(xd*xd - 1.))*PetscCosReal(5.*PETSC_PI*xd));
}
}
ierr = DMDAVecRestoreArray(da,xlocal,&xx);CHKERRQ(ierr);
ierr = DMDAVecRestoreArray(da,blocal,&bb);CHKERRQ(ierr);
ierr = VecZeroEntries(b);CHKERRQ(ierr);
ierr = DMLocalToGlobalBegin(da,blocal,ADD_VALUES,b);CHKERRQ(ierr);
ierr = DMLocalToGlobalEnd(da,blocal,ADD_VALUES,b);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(da,&blocal);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例3: DMCreateLocalVector_Shell
PetscErrorCode DMCreateLocalVector_Shell(DM dm,Vec *gvec)
{
PetscErrorCode ierr;
DM_Shell *shell = (DM_Shell*)dm->data;
Vec X;
PetscFunctionBegin;
PetscValidHeaderSpecific(dm,DM_CLASSID,1);
PetscValidPointer(gvec,2);
*gvec = 0;
X = shell->Xlocal;
if (!X) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Must call DMShellSetLocalVector() or DMShellSetCreateLocalVector()");
if (((PetscObject)X)->refct < 2) { /* We have an exclusive reference so we can give it out */
ierr = PetscObjectReference((PetscObject)X);
CHKERRQ(ierr);
ierr = VecZeroEntries(X);
CHKERRQ(ierr);
*gvec = X;
} else { /* Need to create a copy, could use MAT_SHARE_NONZERO_PATTERN in most cases */
ierr = VecDuplicate(X,gvec);
CHKERRQ(ierr);
ierr = VecZeroEntries(*gvec);
CHKERRQ(ierr);
}
ierr = VecSetDM(*gvec,dm);
CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例4: OpForcing
PetscErrorCode OpForcing(Op op,DM dm,Vec F) {
PetscErrorCode ierr;
Vec X,Floc;
DM dmx;
const PetscScalar *x;
PetscScalar *f;
const PetscReal *B,*D,*w3;
PetscReal L[3];
PetscInt nelem,ne = op->ne,P,Q,P3,Q3;
PetscFunctionBegin;
ierr = PetscLogEventBegin(OP_Forcing,dm,F,0,0);CHKERRQ(ierr);
ierr = DMFEGetTensorEval(dm,&P,&Q,&B,&D,NULL,NULL,&w3);CHKERRQ(ierr);
P3 = P*P*P;
Q3 = Q*Q*Q;
ierr = DMFEGetUniformCoordinates(dm,L);CHKERRQ(ierr);
ierr = DMGetLocalVector(dm,&Floc);CHKERRQ(ierr);
ierr = DMGetCoordinateDM(dm,&dmx);CHKERRQ(ierr);
ierr = DMGetCoordinatesLocal(dm,&X);CHKERRQ(ierr);
ierr = DMFEGetNumElements(dm,&nelem);CHKERRQ(ierr);
ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr);
ierr = VecZeroEntries(Floc);CHKERRQ(ierr);
ierr = VecGetArray(Floc,&f);CHKERRQ(ierr);
for (PetscInt e=0; e<nelem; e+=ne) {
PetscScalar fe[op->dof*P3*ne]_align,fq[op->dof][Q3][ne]_align,xe[3*P3*ne]_align,xq[3][Q3][ne]_align,dx[3][3][Q3][ne]_align,wdxdet[Q3][ne]_align;
ierr = DMFEExtractElements(dmx,x,e,ne,xe);CHKERRQ(ierr);
ierr = PetscMemzero(xq,sizeof xq);CHKERRQ(ierr);
ierr = TensorContract(op->Tensor3,B,B,B,TENSOR_EVAL,xe,xq[0][0]);CHKERRQ(ierr);
ierr = PetscMemzero(dx,sizeof dx);CHKERRQ(ierr);
ierr = TensorContract(op->Tensor3,D,B,B,TENSOR_EVAL,xe,dx[0][0][0]);CHKERRQ(ierr);
ierr = TensorContract(op->Tensor3,B,D,B,TENSOR_EVAL,xe,dx[1][0][0]);CHKERRQ(ierr);
ierr = TensorContract(op->Tensor3,B,B,D,TENSOR_EVAL,xe,dx[2][0][0]);CHKERRQ(ierr);
ierr = PointwiseJacobianInvert(ne,Q*Q*Q,w3,dx,wdxdet);CHKERRQ(ierr);
for (PetscInt i=0; i<Q3; i++) {
for (PetscInt l=0; l<ne; l++) {
PetscReal xx[] = {xq[0][i][l],xq[1][i][l],xq[2][i][l]};
PetscScalar fql[op->dof];
ierr = (op->PointwiseForcing)(op,xx,L,fql);CHKERRQ(ierr);
for (PetscInt d=0; d<op->dof; d++) fq[d][i][l] = wdxdet[i][l] * fql[d];
}
}
ierr = PetscMemzero(fe,sizeof fe);CHKERRQ(ierr);
ierr = TensorContract(op->TensorDOF,B,B,B,TENSOR_TRANSPOSE,fq[0][0],fe);CHKERRQ(ierr);
ierr = DMFESetElements(dm,f,e,ne,ADD_VALUES,DOMAIN_INTERIOR,fe);CHKERRQ(ierr);
}
ierr = VecRestoreArrayRead(X,&x);CHKERRQ(ierr);
ierr = VecRestoreArray(Floc,&f);CHKERRQ(ierr);
ierr = VecZeroEntries(F);CHKERRQ(ierr);
ierr = DMLocalToGlobalBegin(dm,Floc,ADD_VALUES,F);CHKERRQ(ierr);
ierr = DMLocalToGlobalEnd(dm,Floc,ADD_VALUES,F);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(dm,&Floc);CHKERRQ(ierr);
ierr = PetscLogEventEnd(OP_Forcing,dm,F,0,0);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例5: SNESComputeFunction_DMDA
static PetscErrorCode SNESComputeFunction_DMDA(SNES snes,Vec X,Vec F,void *ctx)
{
PetscErrorCode ierr;
DM dm;
DMSNES_DA *dmdasnes = (DMSNES_DA*)ctx;
DMDALocalInfo info;
Vec Xloc;
void *x,*f;
PetscFunctionBegin;
PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
PetscValidHeaderSpecific(X,VEC_CLASSID,2);
PetscValidHeaderSpecific(F,VEC_CLASSID,3);
if (!dmdasnes->residuallocal) SETERRQ(PetscObjectComm((PetscObject)snes),PETSC_ERR_PLIB,"Corrupt context");
ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr);
ierr = DMGetLocalVector(dm,&Xloc);CHKERRQ(ierr);
ierr = DMGlobalToLocalBegin(dm,X,INSERT_VALUES,Xloc);CHKERRQ(ierr);
ierr = DMGlobalToLocalEnd(dm,X,INSERT_VALUES,Xloc);CHKERRQ(ierr);
ierr = DMDAGetLocalInfo(dm,&info);CHKERRQ(ierr);
ierr = DMDAVecGetArray(dm,Xloc,&x);CHKERRQ(ierr);
switch (dmdasnes->residuallocalimode) {
case INSERT_VALUES: {
ierr = DMDAVecGetArray(dm,F,&f);CHKERRQ(ierr);
ierr = PetscLogEventBegin(SNES_FunctionEval,snes,X,F,0);CHKERRQ(ierr);
CHKMEMQ;
ierr = (*dmdasnes->residuallocal)(&info,x,f,dmdasnes->residuallocalctx);CHKERRQ(ierr);
CHKMEMQ;
ierr = PetscLogEventEnd(SNES_FunctionEval,snes,X,F,0);CHKERRQ(ierr);
ierr = DMDAVecRestoreArray(dm,F,&f);CHKERRQ(ierr);
} break;
case ADD_VALUES: {
Vec Floc;
ierr = DMGetLocalVector(dm,&Floc);CHKERRQ(ierr);
ierr = VecZeroEntries(Floc);CHKERRQ(ierr);
ierr = DMDAVecGetArray(dm,Floc,&f);CHKERRQ(ierr);
ierr = PetscLogEventBegin(SNES_FunctionEval,snes,X,F,0);CHKERRQ(ierr);
CHKMEMQ;
ierr = (*dmdasnes->residuallocal)(&info,x,f,dmdasnes->residuallocalctx);CHKERRQ(ierr);
CHKMEMQ;
ierr = PetscLogEventEnd(SNES_FunctionEval,snes,X,F,0);CHKERRQ(ierr);
ierr = DMDAVecRestoreArray(dm,Floc,&f);CHKERRQ(ierr);
ierr = VecZeroEntries(F);CHKERRQ(ierr);
ierr = DMLocalToGlobalBegin(dm,Floc,ADD_VALUES,F);CHKERRQ(ierr);
ierr = DMLocalToGlobalEnd(dm,Floc,ADD_VALUES,F);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(dm,&Floc);CHKERRQ(ierr);
} break;
default: SETERRQ1(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_INCOMP,"Cannot use imode=%d",(int)dmdasnes->residuallocalimode);
}
ierr = DMDAVecRestoreArray(dm,Xloc,&x);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(dm,&Xloc);CHKERRQ(ierr);
if (snes->domainerror) {
ierr = VecSetInf(F);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
示例6: FormResidual
PetscErrorCode FormResidual(TS ts,PetscReal t,Vec U,Vec Udot,Vec R,void *user)
{
PetscFunctionBegin;
PetscErrorCode ierr;
DMDALocalInfo info;
DM dm;
Vec localU,localUdot,localR; // local versions
Field **h,**hdot,**r;
/* get the da from the snes */
ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
/* handle the vec U */
ierr = DMGetLocalVector(dm,&localU);CHKERRQ(ierr);
ierr = DMGlobalToLocalBegin(dm,U,INSERT_VALUES,localU);CHKERRQ(ierr);
ierr = DMGlobalToLocalEnd(dm,U,INSERT_VALUES,localU);CHKERRQ(ierr);
/* handle the vec Udot */
ierr = DMGetLocalVector(dm,&localUdot);CHKERRQ(ierr);
ierr = DMGlobalToLocalBegin(dm,Udot,INSERT_VALUES,localUdot);CHKERRQ(ierr);
ierr = DMGlobalToLocalEnd(dm,Udot,INSERT_VALUES,localUdot);CHKERRQ(ierr);
/* handle the vec R */
ierr = DMGetLocalVector(dm,&localR);CHKERRQ(ierr);
ierr = VecZeroEntries(localR);CHKERRQ(ierr);
/* Get the arrays from the vectors */
ierr = DMIGAVecGetArray(dm,localU,&h);CHKERRQ(ierr);
ierr = DMIGAVecGetArray(dm,localUdot,&hdot);CHKERRQ(ierr);
ierr = DMIGAVecGetArray(dm,localR,&r);CHKERRQ(ierr);
/* Grab the local info and call the local residual routine */
ierr = DMIGAGetLocalInfo(dm,&info);CHKERRQ(ierr);CHKERRQ(ierr);
ierr = FormResidualLocal(&info,t,h,hdot,r,(AppCtx *) user);CHKERRQ(ierr);
/* Restore the arrays */
ierr = DMIGAVecRestoreArray(dm,localR,&r);CHKERRQ(ierr);
ierr = DMIGAVecRestoreArray(dm,localUdot,&hdot);CHKERRQ(ierr);
ierr = DMIGAVecRestoreArray(dm,localU,&h);CHKERRQ(ierr);
/* Add contributions back to global R */
ierr = VecZeroEntries(R);CHKERRQ(ierr);
ierr = DMLocalToGlobalBegin(dm,localR,ADD_VALUES,R);CHKERRQ(ierr); // this one adds the values
ierr = DMLocalToGlobalEnd(dm,localR,ADD_VALUES,R);CHKERRQ(ierr);
/* Restore the local vectors */
ierr = DMRestoreLocalVector(dm,&localU);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(dm,&localUdot);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(dm,&localR);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例7: DMDAComputeFunction
/*
This function should eventually replace:
DMDAComputeFunction() and DMDAComputeFunction1()
*/
static PetscErrorCode TSComputeIFunction_DMDA(TS ts,PetscReal ptime,Vec X,Vec Xdot,Vec F,void *ctx)
{
PetscErrorCode ierr;
DM dm;
DMTS_DA *dmdats = (DMTS_DA*)ctx;
DMDALocalInfo info;
Vec Xloc;
void *x,*f,*xdot;
PetscFunctionBegin;
PetscValidHeaderSpecific(ts,TS_CLASSID,1);
PetscValidHeaderSpecific(X,VEC_CLASSID,2);
PetscValidHeaderSpecific(F,VEC_CLASSID,3);
if (!dmdats->ifunctionlocal) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_PLIB,"Corrupt context");
ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
ierr = DMGetLocalVector(dm,&Xloc);CHKERRQ(ierr);
ierr = DMGlobalToLocalBegin(dm,X,INSERT_VALUES,Xloc);CHKERRQ(ierr);
ierr = DMGlobalToLocalEnd(dm,X,INSERT_VALUES,Xloc);CHKERRQ(ierr);
ierr = DMDAGetLocalInfo(dm,&info);CHKERRQ(ierr);
ierr = DMDAVecGetArray(dm,Xloc,&x);CHKERRQ(ierr);
ierr = DMDAVecGetArray(dm,Xdot,&xdot);CHKERRQ(ierr);
switch (dmdats->ifunctionlocalimode) {
case INSERT_VALUES: {
ierr = DMDAVecGetArray(dm,F,&f);CHKERRQ(ierr);
CHKMEMQ;
ierr = (*dmdats->ifunctionlocal)(&info,ptime,x,xdot,f,dmdats->ifunctionlocalctx);CHKERRQ(ierr);
CHKMEMQ;
ierr = DMDAVecRestoreArray(dm,F,&f);CHKERRQ(ierr);
} break;
case ADD_VALUES: {
Vec Floc;
ierr = DMGetLocalVector(dm,&Floc);CHKERRQ(ierr);
ierr = VecZeroEntries(Floc);CHKERRQ(ierr);
ierr = DMDAVecGetArray(dm,Floc,&f);CHKERRQ(ierr);
CHKMEMQ;
ierr = (*dmdats->ifunctionlocal)(&info,ptime,x,xdot,f,dmdats->ifunctionlocalctx);CHKERRQ(ierr);
CHKMEMQ;
ierr = DMDAVecRestoreArray(dm,Floc,&f);CHKERRQ(ierr);
ierr = VecZeroEntries(F);CHKERRQ(ierr);
ierr = DMLocalToGlobalBegin(dm,Floc,ADD_VALUES,F);CHKERRQ(ierr);
ierr = DMLocalToGlobalEnd(dm,Floc,ADD_VALUES,F);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(dm,&Floc);CHKERRQ(ierr);
} break;
default: SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_INCOMP,"Cannot use imode=%d",(int)dmdats->ifunctionlocalimode);
}
ierr = DMDAVecRestoreArray(dm,Xloc,&x);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(dm,&Xloc);CHKERRQ(ierr);
ierr = DMDAVecRestoreArray(dm,Xdot,&xdot);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例8: TSInterpolate_EIMEX
static PetscErrorCode TSInterpolate_EIMEX(TS ts,PetscReal itime,Vec X)
{
TS_EIMEX *ext = (TS_EIMEX*)ts->data;
PetscReal t,a,b;
Vec Y0=ext->VecSolPrev,Y1=ext->Y,Ydot=ext->Ydot,YdotI=ext->YdotI;
const PetscReal h = ts->ptime - ts->ptime_prev;
PetscErrorCode ierr;
PetscFunctionBegin;
t = (itime -ts->ptime + h)/h;
/* YdotI = -f(x)-g(x) */
ierr = VecZeroEntries(Ydot);CHKERRQ(ierr);
ierr = TSComputeIFunction(ts,ts->ptime-h,Y0,Ydot,YdotI,PETSC_FALSE);CHKERRQ(ierr);
a = 2.0*t*t*t - 3.0*t*t + 1.0;
b = -(t*t*t - 2.0*t*t + t)*h;
ierr = VecAXPBYPCZ(X,a,b,0.0,Y0,YdotI);CHKERRQ(ierr);
ierr = TSComputeIFunction(ts,ts->ptime,Y1,Ydot,YdotI,PETSC_FALSE);CHKERRQ(ierr);
a = -2.0*t*t*t+3.0*t*t;
b = -(t*t*t - t*t)*h;
ierr = VecAXPBYPCZ(X,a,b,1.0,Y1,YdotI);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例9: SFieldSolveFor
double SFieldSolveFor(SField sfv, double *Y, unsigned int yCount) {
mySField sf = static_cast<mySField>(sfv);
assert(yCount <= sf->maxN);
assert(Y);
assert(sf->running);
sf->Y = Y;
sf->curN = yCount;
// -------------- SOLVE
PetscErrorCode ierr;
PetscLogDouble tic,toc;
PetscTime(&tic);
int pt[sf->d];
ierr = MatZeroEntries(sf->J); CHKERRQ(ierr);
JacobianOnD(sf->J, sf->F, 0, pt, sf);
ierr = MatAssemblyBegin(sf->J,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
ierr = MatAssemblyEnd(sf->J,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
PetscTime(&toc);
sf->timeAssembly += toc-tic;
PetscTime(&tic);
ierr = VecZeroEntries(sf->U); CHKERRQ(ierr);
ierr = KSPSetOperators(sf->ksp, sf->J, sf->J); CHKERRQ(ierr);
ierr = KSPSetUp(sf->ksp); CHKERRQ(ierr);
ierr = KSPSolve(sf->ksp,sf->F,sf->U); CHKERRQ(ierr);
PetscTime(&toc);
sf->timeSolver += toc-tic;
return Integrate(sf->U,pt,0,sf);
}
示例10: F
/*
FormIFunction - Evaluates nonlinear function, F(X,Xdot) = Xdot + grad(objective(X))
Input Parameters:
+ ts - the TS context
. t - time
. X - input vector
. Xdot - time derivative
- ctx - optional user-defined context
Output Parameter:
. F - function vector
*/
static PetscErrorCode FormIFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ictx)
{
PetscErrorCode ierr;
const PetscScalar *x;
PetscScalar *f;
PetscInt i;
Ctx *ctx = (Ctx*)ictx;
PetscFunctionBeginUser;
/*
Get pointers to vector data.
- For default PETSc vectors, VecGetArray() returns a pointer to
the data array. Otherwise, the routine is implementation dependent.
- You MUST call VecRestoreArray() when you no longer need access to
the array.
*/
ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr);
ierr = VecZeroEntries(F);CHKERRQ(ierr);
ierr = VecGetArray(F,&f);CHKERRQ(ierr);
/* Compute gradient of objective */
for (i=0; i<ctx->n-1; i++) {
PetscScalar a,a0,a1;
a = x[i+1] - PetscSqr(x[i]);
a0 = -2.*x[i];
a1 = 1.;
f[i] += -2.*(1. - x[i]) + 200.*a*a0;
f[i+1] += 200.*a*a1;
}
/* Restore vectors */
ierr = VecRestoreArrayRead(X,&x);CHKERRQ(ierr);
ierr = VecRestoreArray(F,&f);CHKERRQ(ierr);
ierr = VecAXPY(F,1.0,Xdot);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例11: TSFunction_Sundials
int TSFunction_Sundials(realtype t,N_Vector y,N_Vector ydot,void *ctx)
{
TS ts = (TS) ctx;
DM dm;
DMTS tsdm;
TSIFunction ifunction;
MPI_Comm comm;
TS_Sundials *cvode = (TS_Sundials*)ts->data;
Vec yy = cvode->w1,yyd = cvode->w2,yydot = cvode->ydot;
PetscScalar *y_data,*ydot_data;
PetscErrorCode ierr;
PetscFunctionBegin;
ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
/* Make the PETSc work vectors yy and yyd point to the arrays in the SUNDIALS vectors y and ydot respectively*/
y_data = (PetscScalar*) N_VGetArrayPointer(y);
ydot_data = (PetscScalar*) N_VGetArrayPointer(ydot);
ierr = VecPlaceArray(yy,y_data);CHKERRABORT(comm,ierr);
ierr = VecPlaceArray(yyd,ydot_data);CHKERRABORT(comm,ierr);
/* Now compute the right hand side function, via IFunction unless only the more efficient RHSFunction is set */
ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
if (!ifunction) {
ierr = TSComputeRHSFunction(ts,t,yy,yyd);CHKERRQ(ierr);
} else { /* If rhsfunction is also set, this computes both parts and shifts them to the right */
ierr = VecZeroEntries(yydot);CHKERRQ(ierr);
ierr = TSComputeIFunction(ts,t,yy,yydot,yyd,PETSC_FALSE);CHKERRABORT(comm,ierr);
ierr = VecScale(yyd,-1.);CHKERRQ(ierr);
}
ierr = VecResetArray(yy);CHKERRABORT(comm,ierr);
ierr = VecResetArray(yyd);CHKERRABORT(comm,ierr);
PetscFunctionReturn(0);
}
示例12: FormRHSFunction
static PetscErrorCode FormRHSFunction(TS ts,PetscReal t,Vec X,Vec F,void *ptr)
{
User user = (User)ptr;
PetscErrorCode ierr;
PetscScalar **f;
const PetscScalar **x;
DM dm;
PetscInt i,xs,xm;
PetscFunctionBeginUser;
if (user->reactions) {
ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
ierr = DMDAVecGetArrayDOFRead(dm,X,&x);CHKERRQ(ierr);
ierr = DMDAVecGetArrayDOF(dm,F,&f);CHKERRQ(ierr);
ierr = DMDAGetCorners(dm,&xs,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
for (i=xs; i<xs+xm; i++) {
ierr = PetscMemcpy(user->tchemwork,x[i],(user->Nspec+1)*sizeof(x[xs][0]));CHKERRQ(ierr);
user->tchemwork[0] *= user->Tini; /* Dimensionalize */
ierr = TC_getSrc(user->tchemwork,user->Nspec+1,f[i]);TCCHKERRQ(ierr);
f[i][0] /= user->Tini; /* Non-dimensionalize */
}
ierr = DMDAVecRestoreArrayDOFRead(dm,X,&x);CHKERRQ(ierr);
ierr = DMDAVecRestoreArrayDOF(dm,F,&f);CHKERRQ(ierr);
} else {
ierr = VecZeroEntries(F);CHKERRQ(ierr);
}
if (user->diffusion) {
ierr = FormDiffusionFunction(ts,t,X,F,ptr);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
示例13: 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);
}
示例14: TSPrecond_Sundials
PetscErrorCode TSPrecond_Sundials(realtype tn,N_Vector y,N_Vector fy,booleantype jok,booleantype *jcurPtr,
realtype _gamma,void *P_data,N_Vector vtemp1,N_Vector vtemp2,N_Vector vtemp3)
{
TS ts = (TS) P_data;
TS_Sundials *cvode = (TS_Sundials*)ts->data;
PC pc;
PetscErrorCode ierr;
Mat J,P;
Vec yy = cvode->w1,yydot = cvode->ydot;
PetscReal gm = (PetscReal)_gamma;
MatStructure str = DIFFERENT_NONZERO_PATTERN;
PetscScalar *y_data;
PetscFunctionBegin;
ierr = TSGetIJacobian(ts,&J,&P,NULL,NULL);CHKERRQ(ierr);
y_data = (PetscScalar*) N_VGetArrayPointer(y);
ierr = VecPlaceArray(yy,y_data);CHKERRQ(ierr);
ierr = VecZeroEntries(yydot);CHKERRQ(ierr); /* The Jacobian is independent of Ydot for ODE which is all that CVode works for */
/* compute the shifted Jacobian (1/gm)*I + Jrest */
ierr = TSComputeIJacobian(ts,ts->ptime,yy,yydot,1/gm,&J,&P,&str,PETSC_FALSE);CHKERRQ(ierr);
ierr = VecResetArray(yy);CHKERRQ(ierr);
ierr = MatScale(P,gm);CHKERRQ(ierr); /* turn into I-gm*Jrest, J is not used by Sundials */
*jcurPtr = TRUE;
ierr = TSSundialsGetPC(ts,&pc);CHKERRQ(ierr);
ierr = PCSetOperators(pc,J,P,str);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例15: MatMultTransposeAdd_SubMatrix
static PetscErrorCode MatMultTransposeAdd_SubMatrix(Mat N,Vec v1,Vec v2,Vec v3)
{
Mat_SubMatrix *Na = (Mat_SubMatrix*)N->data;
Vec xx = 0;
PetscErrorCode ierr;
PetscFunctionBegin;
ierr = PreScaleLeft(N,v1,&xx);CHKERRQ(ierr);
ierr = VecZeroEntries(Na->lwork);CHKERRQ(ierr);
ierr = VecScatterBegin(Na->lrestrict,xx,Na->lwork,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
ierr = VecScatterEnd (Na->lrestrict,xx,Na->lwork,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
ierr = MatMultTranspose(Na->A,Na->lwork,Na->rwork);CHKERRQ(ierr);
if (v2 == v3) {
if (Na->scale == (PetscScalar)1.0 && !Na->right) {
ierr = VecScatterBegin(Na->rprolong,Na->rwork,v3,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
ierr = VecScatterEnd (Na->rprolong,Na->rwork,v3,ADD_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
} else {
if (!Na->orwork) {ierr = VecDuplicate(v3,&Na->orwork);CHKERRQ(ierr);}
ierr = VecScatterBegin(Na->rprolong,Na->rwork,Na->orwork,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
ierr = VecScatterEnd (Na->rprolong,Na->rwork,Na->orwork,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
ierr = PostScaleRight(N,Na->orwork);CHKERRQ(ierr);
ierr = VecAXPY(v3,Na->scale,Na->orwork);CHKERRQ(ierr);
}
} else {
ierr = VecScatterBegin(Na->rprolong,Na->rwork,v3,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
ierr = VecScatterEnd (Na->rprolong,Na->rwork,v3,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
ierr = PostScaleRight(N,v3);CHKERRQ(ierr);
ierr = VecAYPX(v3,Na->scale,v2);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}