本文整理汇总了C++中DMGetLocalVector函数的典型用法代码示例。如果您正苦于以下问题:C++ DMGetLocalVector函数的具体用法?C++ DMGetLocalVector怎么用?C++ DMGetLocalVector使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DMGetLocalVector函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: DMPlexProjectFunction
/*@C
DMPlexProjectField - This projects the given function of the fields into the function space provided.
Input Parameters:
+ dm - The DM
. U - The input field vector
. funcs - The functions to evaluate, one per field
- mode - The insertion mode for values
Output Parameter:
. X - The output vector
Level: developer
.seealso: DMPlexProjectFunction(), DMPlexComputeL2Diff()
@*/
PetscErrorCode DMPlexProjectField(DM dm, Vec U, void (**funcs)(const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscReal [], PetscScalar []), InsertMode mode, Vec X)
{
Vec localX, localU;
PetscErrorCode ierr;
PetscFunctionBegin;
PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
ierr = DMGetLocalVector(dm, &localU);CHKERRQ(ierr);
ierr = DMGlobalToLocalBegin(dm, U, INSERT_VALUES, localU);CHKERRQ(ierr);
ierr = DMGlobalToLocalEnd(dm, U, INSERT_VALUES, localU);CHKERRQ(ierr);
ierr = DMPlexProjectFieldLocal(dm, localU, funcs, mode, localX);CHKERRQ(ierr);
ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
if (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES) {
Mat cMat;
ierr = DMGetDefaultConstraints(dm, NULL, &cMat);CHKERRQ(ierr);
if (cMat) {
ierr = DMGlobalToLocalSolve(dm, localX, X);CHKERRQ(ierr);
}
}
ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(dm, &localU);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例2: 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);
}
示例3: 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);
}
示例4: 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);
}
示例5: IFunction
PetscErrorCode IFunction(TS ts,PetscReal t,Vec X,Vec Xdot,Vec F,void *ctx)
{
PetscErrorCode ierr;
AppCtx *user=(AppCtx*)ctx;
DM cda;
DMDACoor2d **coors;
PetscScalar **p,**f,**pdot;
PetscInt i,j;
PetscInt xs,ys,xm,ym,M,N;
Vec localX,gc,localXdot;
PetscScalar p_adv1,p_adv2,p_diff;
PetscFunctionBeginUser;
ierr = DMDAGetInfo(user->da,NULL,&M,&N,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
ierr = DMGetCoordinateDM(user->da,&cda);CHKERRQ(ierr);
ierr = DMDAGetCorners(cda,&xs,&ys,0,&xm,&ym,0);CHKERRQ(ierr);
ierr = DMGetLocalVector(user->da,&localX);CHKERRQ(ierr);
ierr = DMGetLocalVector(user->da,&localXdot);CHKERRQ(ierr);
ierr = DMGlobalToLocalBegin(user->da,X,INSERT_VALUES,localX);CHKERRQ(ierr);
ierr = DMGlobalToLocalEnd(user->da,X,INSERT_VALUES,localX);CHKERRQ(ierr);
ierr = DMGlobalToLocalBegin(user->da,Xdot,INSERT_VALUES,localXdot);CHKERRQ(ierr);
ierr = DMGlobalToLocalEnd(user->da,Xdot,INSERT_VALUES,localXdot);CHKERRQ(ierr);
ierr = DMGetCoordinatesLocal(user->da,&gc);CHKERRQ(ierr);
ierr = DMDAVecGetArrayRead(cda,gc,&coors);CHKERRQ(ierr);
ierr = DMDAVecGetArrayRead(user->da,localX,&p);CHKERRQ(ierr);
ierr = DMDAVecGetArrayRead(user->da,localXdot,&pdot);CHKERRQ(ierr);
ierr = DMDAVecGetArray(user->da,F,&f);CHKERRQ(ierr);
PetscScalar diffuse1,gamma;
gamma = user->D*user->ws/(2*user->H);
diffuse1 = user->lambda*user->lambda*user->q/(user->lambda*gamma+1)*(1.0 - PetscExpScalar(-t*(gamma+1.0)/user->lambda));
user->disper_coe = user->ws*user->ws/(4*user->H*user->H)*diffuse1;
for (i=xs; i < xs+xm; i++) {
for (j=ys; j < ys+ym; j++) {
ierr = adv1(p,coors[j][i].y,i,j,M,&p_adv1,user);CHKERRQ(ierr);
ierr = adv2(p,coors[j][i].x,coors[j][i].y,i,j,N,&p_adv2,user);CHKERRQ(ierr);
ierr = diffuse(p,i,j,t,&p_diff,user);CHKERRQ(ierr);
f[j][i] = -p_adv1 - p_adv2 + p_diff - pdot[j][i];
}
}
ierr = DMDAVecRestoreArrayRead(user->da,localX,&p);CHKERRQ(ierr);
ierr = DMDAVecRestoreArrayRead(user->da,localX,&pdot);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(user->da,&localX);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(user->da,&localXdot);CHKERRQ(ierr);
ierr = DMDAVecRestoreArray(user->da,F,&f);CHKERRQ(ierr);
ierr = DMDAVecRestoreArrayRead(cda,gc,&coors);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例6: 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);
}
示例7: FormTangent
PetscErrorCode FormTangent(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat *A,Mat *B,MatStructure *flag,void *ctx)
{
PetscFunctionBegin;
PetscErrorCode ierr;
SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE, "FormTangent not implemented, use -snes_mf");
DMDALocalInfo info;
DM da_dof;
Vec localU,localUdot; // local versions
Field **h,**hdot;
/* get the da from the snes */
ierr = TSGetDM(ts,(DM*)&da_dof);CHKERRQ(ierr);
/* handle the vec U */
ierr = DMGetLocalVector(da_dof,&localU);CHKERRQ(ierr);
ierr = DMGlobalToLocalBegin(da_dof,U,INSERT_VALUES,localU);CHKERRQ(ierr);
ierr = DMGlobalToLocalEnd(da_dof,U,INSERT_VALUES,localU);CHKERRQ(ierr);
/* handle the vec Udot */
ierr = DMGetLocalVector(da_dof,&localUdot);CHKERRQ(ierr);
ierr = DMGlobalToLocalBegin(da_dof,Udot,INSERT_VALUES,localUdot);CHKERRQ(ierr);
ierr = DMGlobalToLocalEnd(da_dof,Udot,INSERT_VALUES,localUdot);CHKERRQ(ierr);
/* Get the arrays from the vectors */
ierr = DMDAVecGetArray(da_dof,localU,&h);CHKERRQ(ierr);
ierr = DMDAVecGetArray(da_dof,localUdot,&hdot);CHKERRQ(ierr);
/* Grab the local info and call the local tangent routine */
ierr = DMDAGetLocalInfo(da_dof,&info);CHKERRQ(ierr);CHKERRQ(ierr);
ierr = MatZeroEntries(*B);CHKERRQ(ierr); // pre-zero the matrix
ierr = FormTangentLocal(&info,t,h,hdot,shift,B,(AppCtx *) ctx);CHKERRQ(ierr);
ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
if (*A != *B) { // then we could be matrix free
ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
}
*flag = SAME_NONZERO_PATTERN; /* the sparsity pattern does not change */
ierr = DMDAVecRestoreArray(da_dof,localUdot,&hdot);CHKERRQ(ierr);
ierr = DMDAVecRestoreArray(da_dof,localU,&h);CHKERRQ(ierr);
/* Restore the arrays and local vectors */
ierr = DMRestoreLocalVector(da_dof,&localU);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(da_dof,&localUdot);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例8: 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);
}
示例9: SNESComputePicardJacobian_DMDA
static PetscErrorCode SNESComputePicardJacobian_DMDA(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *mstr,void *ctx)
{
PetscErrorCode ierr;
DM dm;
DMSNES_DA *dmdasnes = (DMSNES_DA*)ctx;
DMDALocalInfo info;
Vec Xloc;
void *x;
PetscFunctionBegin;
if (!dmdasnes->jacobianplocal) 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);
CHKMEMQ;
ierr = (*dmdasnes->jacobianplocal)(&info,x,*A,*B,mstr,dmdasnes->picardlocalctx);CHKERRQ(ierr);
CHKMEMQ;
ierr = DMDAVecRestoreArray(dm,Xloc,&x);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(dm,&Xloc);CHKERRQ(ierr);
*mstr = SAME_NONZERO_PATTERN;
if (*A != *B) {
ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
示例10: DMDAComputeFunction
/*
This function should eventually replace:
DMDAComputeFunction() and DMDAComputeFunction1()
*/
static PetscErrorCode SNESComputeObjective_DMDA(SNES snes,Vec X,PetscReal *ob,void *ctx)
{
PetscErrorCode ierr;
DM dm;
DMSNES_DA *dmdasnes = (DMSNES_DA*)ctx;
DMDALocalInfo info;
Vec Xloc;
void *x;
PetscFunctionBegin;
PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
PetscValidHeaderSpecific(X,VEC_CLASSID,2);
PetscValidPointer(ob,3);
if (!dmdasnes->objectivelocal) 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);
CHKMEMQ;
ierr = (*dmdasnes->objectivelocal)(&info,x,ob,dmdasnes->objectivelocalctx);CHKERRQ(ierr);
CHKMEMQ;
ierr = DMDAVecRestoreArray(dm,Xloc,&x);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(dm,&Xloc);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例11: CaculateLocalSourceTerm
/*
Caculate the source term of the equations, which includes all
terms except convetion term, diffusion term, and timedependent term.
*/
PetscErrorCode CaculateLocalSourceTerm(DM dm, Vec locX, Vec F, User user)
{
PetscErrorCode ierr;
DM dmGrad = user->dmGrad;
const PetscScalar *x;
PetscScalar *f;
PetscInt cStart, cell;
const PetscScalar *cellgeom;
const CellGeom *cg;
Vec locGrad, Grad;
const PetscScalar *grad;
DM dmCell;
PetscFunctionBeginUser;
ierr = VecGetDM(user->cellgeom,&dmCell);CHKERRQ(ierr);
ierr = VecGetArrayRead(locX,&x);CHKERRQ(ierr);
ierr = VecGetArray(F,&f);CHKERRQ(ierr);
ierr = DMPlexGetHeightStratum(dm, 0, &cStart, NULL);CHKERRQ(ierr);
ierr = VecGetArrayRead(user->cellgeom,&cellgeom);CHKERRQ(ierr);
ierr = DMGetGlobalVector(dmGrad,&Grad);CHKERRQ(ierr);
ierr = DMGetLocalVector(dmGrad,&locGrad);CHKERRQ(ierr);
ierr = DMGlobalToLocalBegin(dmGrad,Grad,INSERT_VALUES,locGrad);CHKERRQ(ierr);
ierr = DMGlobalToLocalEnd(dmGrad,Grad,INSERT_VALUES,locGrad);CHKERRQ(ierr);
ierr = DMRestoreGlobalVector(dmGrad,&Grad);CHKERRQ(ierr);
ierr = VecGetArrayRead(locGrad,&grad);CHKERRQ(ierr);
for (cell = cStart; cell < user->cEndInterior; cell++) {
PetscScalar *fref;
const PetscScalar *xref;
PetscScalar *cgrad;
ierr = DMPlexPointLocalRead(dmCell,cell,cellgeom,&cg);CHKERRQ(ierr);
ierr = DMPlexPointLocalRead(dm,cell,x,&xref);CHKERRQ(ierr); /*For the unkown variables*/
ierr = DMPlexPointGlobalRef(dm,cell,f,&fref);CHKERRQ(ierr);
ierr = DMPlexPointLocalRead(dmGrad,cell,grad,&cgrad);CHKERRQ(ierr);
// if (!fref){ PetscPrintf(PETSC_COMM_WORLD,"%d, %d\n", cell, user->cEndInterior);}
if (fref){
fref[0] += SourceRho(user, cgrad, xref, cg->centroid);/*the continuity equation*/
fref[1] += SourceU(user, cgrad, xref, cg->centroid); /*Momentum U*/
fref[2] += SourceV(user, cgrad, xref, cg->centroid); /*Momentum V*/
fref[3] += SourceW(user, cgrad, xref, cg->centroid); /*Momentum W*/
fref[4] += SourceE(user, cgrad, xref, cg->centroid);/*Energy*/
}
}
ierr = VecRestoreArrayRead(locX,&x);CHKERRQ(ierr);
ierr = VecRestoreArray(F,&f);CHKERRQ(ierr);
ierr = VecRestoreArrayRead(user->cellgeom,&cellgeom);CHKERRQ(ierr);
ierr = VecRestoreArrayRead(locGrad,&grad);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(dmGrad,&locGrad);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例12: FormDiffusionFunction
/*
Applies the second order centered difference diffusion operator on a one dimensional periodic domain
*/
static PetscErrorCode FormDiffusionFunction(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,j,dof;
Vec Xlocal;
PetscReal idx;
PetscFunctionBeginUser;
ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
ierr = DMDAGetInfo(dm,NULL,NULL,NULL,NULL,NULL,NULL,NULL,&dof,NULL,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
ierr = DMGetLocalVector(dm,&Xlocal);CHKERRQ(ierr);
ierr = DMGlobalToLocalBegin(dm,X,INSERT_VALUES,Xlocal);CHKERRQ(ierr);
ierr = DMGlobalToLocalEnd(dm,X,INSERT_VALUES,Xlocal);CHKERRQ(ierr);
ierr = DMDAVecGetArrayDOFRead(dm,Xlocal,&x);CHKERRQ(ierr);
ierr = DMDAVecGetArrayDOF(dm,F,&f);CHKERRQ(ierr);
ierr = DMDAGetCorners(dm,&xs,NULL,NULL,&xm,NULL,NULL);CHKERRQ(ierr);
idx = 1.0*user->diffus/user->dx;
for (i=xs; i<xs+xm; i++) {
for (j=0; j<dof; j++) {
f[i][j] += idx*(x[i+1][j] - 2.0*x[i][j] + x[i-1][j]);
}
}
ierr = DMDAVecRestoreArrayDOFRead(dm,Xlocal,&x);CHKERRQ(ierr);
ierr = DMDAVecRestoreArrayDOF(dm,F,&f);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(dm,&Xlocal);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例13: ComputeRHS
/*
We integrate over each cell
(i, j+1)----(i+1, j+1)
| \ |
| \ |
| \ |
| \ |
| \ |
| \ |
| \ |
(i, j)----(i+1, j)
*/
PetscErrorCode ComputeRHS(KSP ksp, Vec b, void *ctx)
{
UserContext *user = (UserContext*)ctx;
PetscScalar phi = user->phi;
PetscScalar *array;
PetscInt ne,nc,i;
const PetscInt *e;
PetscErrorCode ierr;
Vec blocal;
DM da;
PetscFunctionBeginUser;
ierr = KSPGetDM(ksp,&da);CHKERRQ(ierr);
/* access a local vector with room for the ghost points */
ierr = DMGetLocalVector(da,&blocal);CHKERRQ(ierr);
ierr = VecGetArray(blocal, (PetscScalar**) &array);CHKERRQ(ierr);
/* access the list of elements on this processor and loop over them */
ierr = DMDAGetElements(da,&ne,&nc,&e);CHKERRQ(ierr);
for (i=0; i<ne; i++) {
/* this is nonsense, but set each nodal value to phi (will actually do integration over element */
array[e[3*i]] = phi;
array[e[3*i+1]] = phi;
array[e[3*i+2]] = phi;
}
ierr = VecRestoreArray(blocal, (PetscScalar**) &array);CHKERRQ(ierr);
ierr = DMDARestoreElements(da,&ne,&nc,&e);CHKERRQ(ierr);
/* add our partial sums over all processors into b */
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);
}
示例14: TSComputeRHSJacobian_DMDA
static PetscErrorCode TSComputeRHSJacobian_DMDA(TS ts,PetscReal ptime,Vec X,Mat *A,Mat *B,MatStructure *mstr,void *ctx)
{
PetscErrorCode ierr;
DM dm;
DMTS_DA *dmdats = (DMTS_DA*)ctx;
DMDALocalInfo info;
Vec Xloc;
void *x;
PetscFunctionBegin;
if (!dmdats->rhsfunctionlocal) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_PLIB,"Corrupt context");
ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
if (dmdats->rhsjacobianlocal) {
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);
CHKMEMQ;
ierr = (*dmdats->rhsjacobianlocal)(&info,ptime,x,*A,*B,mstr,dmdats->rhsjacobianlocalctx);CHKERRQ(ierr);
CHKMEMQ;
ierr = DMDAVecRestoreArray(dm,Xloc,&x);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(dm,&Xloc);CHKERRQ(ierr);
} else SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_PLIB,"TSComputeRHSJacobian_DMDA() called without calling DMDATSSetRHSJacobian()");
/* This will be redundant if the user called both, but it's too common to forget. */
if (*A != *B) {
ierr = MatAssemblyBegin(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(*A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
示例15: FormMassTimeStepFunction
/**
Form the time step function for the mass term
@param Vec in: (y^(n+1))
out @param Vec out: My^(n+1) - M y^(n) if timestep = 1, backward euler
will do the BDF2
**/
PetscErrorCode FormMassTimeStepFunction(User user, Algebra algebra, Vec in, Vec out, PetscBool rebuild)
{
PetscErrorCode ierr;
PetscMPIInt rank;
Vec inLocal;
PetscFunctionBegin;
ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
ierr = VecSet(out, 0.0);CHKERRQ(ierr);
ierr = DMGetLocalVector(user->dm, &inLocal);CHKERRQ(ierr);
ierr = DMGlobalToLocalBegin(user->dm, in, INSERT_VALUES, inLocal);CHKERRQ(ierr);
ierr = DMGlobalToLocalEnd(user->dm, in, INSERT_VALUES, inLocal);CHKERRQ(ierr);
ierr = ApplyBC(user->dm, user->current_time, inLocal, user);CHKERRQ(ierr);
ierr = CaculateLocalMassFunction(user->dm, inLocal, out, user);CHKERRQ(ierr);
ierr = DMLocalToGlobalBegin(user->dm, inLocal, INSERT_VALUES, in);CHKERRQ(ierr);
ierr = DMLocalToGlobalEnd(user->dm, inLocal, INSERT_VALUES, in);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(user->dm, &inLocal);CHKERRQ(ierr);
PetscFunctionReturn(0);
}