本文整理汇总了C++中PetscSqrtReal函数的典型用法代码示例。如果您正苦于以下问题:C++ PetscSqrtReal函数的具体用法?C++ PetscSqrtReal怎么用?C++ PetscSqrtReal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PetscSqrtReal函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: step
/*
Monitor - User-provided routine to monitor the solution computed at
each timestep. This example plots the solution and computes the
error in two different norms.
Input Parameters:
ts - the timestep context
step - the count of the current step (with 0 meaning the
initial condition)
time - the current time
u - the solution at this timestep
ctx - the user-provided context for this monitoring routine.
In this case we use the application context which contains
information about the problem size, workspace and the exact
solution.
*/
PetscErrorCode Monitor(TS ts,PetscInt step,PetscReal time,Vec u,void *ctx)
{
AppCtx *appctx = (AppCtx*) ctx; /* user-defined application context */
PetscErrorCode ierr;
PetscReal en2,en2s,enmax;
PetscDraw draw;
/*
We use the default X windows viewer
PETSC_VIEWER_DRAW_(appctx->comm)
that is associated with the current communicator. This saves
the effort of calling PetscViewerDrawOpen() to create the window.
Note that if we wished to plot several items in separate windows we
would create each viewer with PetscViewerDrawOpen() and store them in
the application context, appctx.
PetscReal buffering makes graphics look better.
*/
ierr = PetscViewerDrawGetDraw(PETSC_VIEWER_DRAW_(appctx->comm),0,&draw);CHKERRQ(ierr);
ierr = PetscDrawSetDoubleBuffer(draw);CHKERRQ(ierr);
ierr = VecView(u,PETSC_VIEWER_DRAW_(appctx->comm));CHKERRQ(ierr);
/*
Compute the exact solution at this timestep
*/
ierr = ExactSolution(time,appctx->solution,appctx);CHKERRQ(ierr);
/*
Print debugging information if desired
*/
if (appctx->debug) {
ierr = PetscPrintf(appctx->comm,"Computed solution vector\n");CHKERRQ(ierr);
ierr = VecView(u,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
ierr = PetscPrintf(appctx->comm,"Exact solution vector\n");CHKERRQ(ierr);
ierr = VecView(appctx->solution,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
}
/*
Compute the 2-norm and max-norm of the error
*/
ierr = VecAXPY(appctx->solution,-1.0,u);CHKERRQ(ierr);
ierr = VecNorm(appctx->solution,NORM_2,&en2);CHKERRQ(ierr);
en2s = PetscSqrtReal(appctx->h)*en2; /* scale the 2-norm by the grid spacing */
ierr = VecNorm(appctx->solution,NORM_MAX,&enmax);CHKERRQ(ierr);
/*
PetscPrintf() causes only the first processor in this
communicator to print the timestep information.
*/
ierr = PetscPrintf(appctx->comm,"Timestep %D: time = %g,2-norm error = %g, max norm error = %g\n",step,(double)time,(double)en2s,(double)enmax);CHKERRQ(ierr);
/*
Print debugging information if desired
*/
/* if (appctx->debug) {
ierr = PetscPrintf(appctx->comm,"Error vector\n");CHKERRQ(ierr);
ierr = VecView(appctx->solution,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
} */
return 0;
}
示例2: FormInitialGuess
PetscErrorCode FormInitialGuess(Vec X,AppCtx *user)
{
PetscInt i,j,row,mx,my;
PetscErrorCode ierr;
PetscReal one = 1.0,lambda;
PetscReal temp1,temp,hx,hy;
PetscScalar *x;
mx = user->mx;
my = user->my;
lambda = user->param;
hx = one / (PetscReal)(mx-1);
hy = one / (PetscReal)(my-1);
ierr = VecGetArray(X,&x);CHKERRQ(ierr);
temp1 = lambda/(lambda + one);
for (j=0; j<my; j++) {
temp = (PetscReal)(PetscMin(j,my-j-1))*hy;
for (i=0; i<mx; i++) {
row = i + j*mx;
if (i == 0 || j == 0 || i == mx-1 || j == my-1) {
x[row] = 0.0;
continue;
}
x[row] = temp1*PetscSqrtReal(PetscMin((PetscReal)(PetscMin(i,mx-i-1))*hx,temp));
}
}
ierr = VecRestoreArray(X,&x);CHKERRQ(ierr);
return 0;
}
示例3: StokesCalcError
PetscErrorCode StokesCalcError(Stokes *s)
{
PetscScalar scale = PetscSqrtReal((double)s->nx*s->ny);
PetscReal val;
Vec y0, y1;
PetscErrorCode ierr;
PetscFunctionBeginUser;
/* error y-x */
ierr = VecAXPY(s->y, -1.0, s->x);CHKERRQ(ierr);
/* ierr = VecView(s->y, (PetscViewer)PETSC_VIEWER_DEFAULT);CHKERRQ(ierr); */
/* error in velocity */
ierr = VecGetSubVector(s->y, s->isg[0], &y0);CHKERRQ(ierr);
ierr = VecNorm(y0, NORM_2, &val);CHKERRQ(ierr);
ierr = PetscPrintf(PETSC_COMM_WORLD," discretization error u = %g\n",(double)(PetscRealPart(val/scale)));CHKERRQ(ierr);
ierr = VecRestoreSubVector(s->y, s->isg[0], &y0);CHKERRQ(ierr);
/* error in pressure */
ierr = VecGetSubVector(s->y, s->isg[1], &y1);CHKERRQ(ierr);
ierr = VecNorm(y1, NORM_2, &val);CHKERRQ(ierr);
ierr = PetscPrintf(PETSC_COMM_WORLD," discretization error p = %g\n",(double)(PetscRealPart(val/scale)));CHKERRQ(ierr);
ierr = VecRestoreSubVector(s->y, s->isg[1], &y1);CHKERRQ(ierr);
/* total error */
ierr = VecNorm(s->y, NORM_2, &val);CHKERRQ(ierr);
ierr = PetscPrintf(PETSC_COMM_WORLD," discretization error [u,p] = %g\n", (double)PetscRealPart((val/scale)));CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例4: interval
/*@
PetscDTGaussQuadrature - create Gauss quadrature
Not Collective
Input Arguments:
+ npoints - number of points
. a - left end of interval (often-1)
- b - right end of interval (often +1)
Output Arguments:
+ x - quadrature points
- w - quadrature weights
Level: intermediate
References:
Golub and Welsch, Calculation of Quadrature Rules, Math. Comp. 23(106), 221--230, 1969.
.seealso: PetscDTLegendreEval()
@*/
PetscErrorCode PetscDTGaussQuadrature(PetscInt npoints,PetscReal a,PetscReal b,PetscReal *x,PetscReal *w)
{
PetscErrorCode ierr;
PetscInt i;
PetscReal *work;
PetscScalar *Z;
PetscBLASInt N,LDZ,info;
PetscFunctionBegin;
ierr = PetscCitationsRegister(GaussCitation, &GaussCite);CHKERRQ(ierr);
/* Set up the Golub-Welsch system */
for (i=0; i<npoints; i++) {
x[i] = 0; /* diagonal is 0 */
if (i) w[i-1] = 0.5 / PetscSqrtReal(1 - 1./PetscSqr(2*i));
}
ierr = PetscMalloc2(npoints*npoints,&Z,PetscMax(1,2*npoints-2),&work);CHKERRQ(ierr);
ierr = PetscBLASIntCast(npoints,&N);CHKERRQ(ierr);
LDZ = N;
ierr = PetscFPTrapPush(PETSC_FP_TRAP_OFF);CHKERRQ(ierr);
PetscStackCallBLAS("LAPACKsteqr",LAPACKsteqr_("I",&N,x,w,Z,&LDZ,work,&info));
ierr = PetscFPTrapPop();CHKERRQ(ierr);
if (info) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"xSTEQR error");
for (i=0; i<(npoints+1)/2; i++) {
PetscReal y = 0.5 * (-x[i] + x[npoints-i-1]); /* enforces symmetry */
x[i] = (a+b)/2 - y*(b-a)/2;
x[npoints-i-1] = (a+b)/2 + y*(b-a)/2;
w[i] = w[npoints-1-i] = 0.5*(b-a)*(PetscSqr(PetscAbsScalar(Z[i*npoints])) + PetscSqr(PetscAbsScalar(Z[(npoints-i-1)*npoints])));
}
ierr = PetscFree2(Z,work);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例5: FormInitialGuess
/*
FormInitialGuess - Forms initial approximation.
Input Parameters:
user - user-defined application context
X - vector
Output Parameter:
X - vector
*/
PetscErrorCode FormInitialGuess(AppCtx *user,Vec X)
{
PetscInt i,j,row,mx,my,xs,ys,xm,ym,gxm,gym,gxs,gys;
PetscErrorCode ierr;
PetscReal one = 1.0,lambda,temp1,temp,hx,hy,hxdhy,hydhx,sc;
PetscScalar *x;
Vec localX = user->localX;
mx = user->mx; my = user->my; lambda = user->param;
hx = one/(PetscReal)(mx-1); hy = one/(PetscReal)(my-1);
sc = hx*hy*lambda; hxdhy = hx/hy; hydhx = hy/hx;
temp1 = lambda/(lambda + one);
/*
Get a pointer 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 = VecGetArray(localX,&x);CHKERRQ(ierr);
/*
Get local grid boundaries (for 2-dimensional DMDA):
xs, ys - starting grid indices (no ghost points)
xm, ym - widths of local grid (no ghost points)
gxs, gys - starting grid indices (including ghost points)
gxm, gym - widths of local grid (including ghost points)
*/
ierr = DMDAGetCorners(user->da,&xs,&ys,PETSC_NULL,&xm,&ym,PETSC_NULL);CHKERRQ(ierr);
ierr = DMDAGetGhostCorners(user->da,&gxs,&gys,PETSC_NULL,&gxm,&gym,PETSC_NULL);CHKERRQ(ierr);
/*
Compute initial guess over the locally owned part of the grid
*/
for (j=ys; j<ys+ym; j++) {
temp = (PetscReal)(PetscMin(j,my-j-1))*hy;
for (i=xs; i<xs+xm; i++) {
row = i - gxs + (j - gys)*gxm;
if (i == 0 || j == 0 || i == mx-1 || j == my-1) {
x[row] = 0.0;
continue;
}
x[row] = temp1*PetscSqrtReal(PetscMin((PetscReal)(PetscMin(i,mx-i-1))*hx,temp));
}
}
/*
Restore vector
*/
ierr = VecRestoreArray(localX,&x);CHKERRQ(ierr);
/*
Insert values into global vector
*/
ierr = DMLocalToGlobalBegin(user->da,localX,INSERT_VALUES,X);CHKERRQ(ierr);
ierr = DMLocalToGlobalEnd(user->da,localX,INSERT_VALUES,X);CHKERRQ(ierr);
return 0;
}
示例6: step
/*
Monitor - User-provided routine to monitor the solution computed at
each timestep. This example plots the solution and computes the
error in two different norms.
Input Parameters:
ts - the timestep context
step - the count of the current step (with 0 meaning the
initial condition)
time - the current time
u - the solution at this timestep
ctx - the user-provided context for this monitoring routine.
In this case we use the application context which contains
information about the problem size, workspace and the exact
solution.
*/
PetscErrorCode Monitor(TS ts,PetscInt step,PetscReal time,Vec u,void *ctx)
{
AppCtx *appctx = (AppCtx*) ctx; /* user-defined application context */
PetscErrorCode ierr;
PetscReal norm_2,norm_max;
/*
View a graph of the current iterate
*/
ierr = VecView(u,appctx->viewer2);CHKERRQ(ierr);
/*
Compute the exact solution
*/
ierr = ExactSolution(time,appctx->solution,appctx);CHKERRQ(ierr);
/*
Print debugging information if desired
*/
if (appctx->debug) {
ierr = PetscPrintf(appctx->comm,"Computed solution vector\n");CHKERRQ(ierr);
ierr = VecView(u,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
ierr = PetscPrintf(appctx->comm,"Exact solution vector\n");CHKERRQ(ierr);
ierr = VecView(appctx->solution,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
}
/*
Compute the 2-norm and max-norm of the error
*/
ierr = VecAXPY(appctx->solution,-1.0,u);CHKERRQ(ierr);
ierr = VecNorm(appctx->solution,NORM_2,&norm_2);CHKERRQ(ierr);
norm_2 = PetscSqrtReal(appctx->h)*norm_2;
ierr = VecNorm(appctx->solution,NORM_MAX,&norm_max);CHKERRQ(ierr);
if (norm_2 < 1e-14) norm_2 = 0;
if (norm_max < 1e-14) norm_max = 0;
/*
PetscPrintf() causes only the first processor in this
communicator to print the timestep information.
*/
ierr = PetscPrintf(appctx->comm,"Timestep %D: time = %g 2-norm error = %g max norm error = %g\n",step,(double)time,(double)norm_2,(double)norm_max);CHKERRQ(ierr);
appctx->norm_2 += norm_2;
appctx->norm_max += norm_max;
/*
View a graph of the error
*/
ierr = VecView(appctx->solution,appctx->viewer1);CHKERRQ(ierr);
/*
Print debugging information if desired
*/
if (appctx->debug) {
ierr = PetscPrintf(appctx->comm,"Error vector\n");CHKERRQ(ierr);
ierr = VecView(appctx->solution,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
}
return 0;
}
示例7: context
/*@C
PetscDrawViewPortsCreate - Splits a window into smaller view ports. Each processor shares all the viewports.
Collective on PetscDraw
Input Parameters:
+ draw - the drawing context
- nports - the number of ports
Output Parameter:
. ports - a PetscDrawViewPorts context (C structure)
Options Database:
. -draw_ports - display multiple fields in the same window with PetscDrawPorts instead of in seperate windows
Level: advanced
Concepts: drawing^in subset of window
.seealso: PetscDrawSplitViewPort(), PetscDrawSetViewPort(), PetscDrawViewPortsSet(), PetscDrawViewPortsDestroy()
@*/
PetscErrorCode PetscDrawViewPortsCreate(PetscDraw draw,PetscInt nports,PetscDrawViewPorts **newports)
{
PetscDrawViewPorts *ports;
PetscInt i,n;
PetscBool isnull;
PetscMPIInt rank;
PetscReal *xl,*xr,*yl,*yr,h;
PetscErrorCode ierr;
PetscFunctionBegin;
PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
if (nports < 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Number of divisions must be positive: %d", nports);
PetscValidPointer(newports,3);
ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
if (isnull) {*newports = NULL; PetscFunctionReturn(0);}
ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
ierr = PetscNew(&ports);CHKERRQ(ierr); *newports = ports;
ports->draw = draw;
ports->nports = nports;
ierr = PetscObjectReference((PetscObject)draw);CHKERRQ(ierr);
/* save previous drawport of window */
ierr = PetscDrawGetViewPort(draw,&ports->port_xl,&ports->port_yl,&ports->port_xr,&ports->port_yr);CHKERRQ(ierr);
n = (PetscInt)(.1 + PetscSqrtReal((PetscReal)nports));
while (n*n < nports) n++;
h = 1.0/n;
ierr = PetscMalloc4(n*n,&xl,n*n,&xr,n*n,&yl,n*n,&yr);CHKERRQ(ierr);
ports->xl = xl;
ports->xr = xr;
ports->yl = yl;
ports->yr = yr;
ierr = PetscDrawSetCoordinates(draw,0.0,0.0,1.0,1.0);CHKERRQ(ierr);
ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
for (i=0; i<n*n; i++) {
xl[i] = (i % n)*h;
xr[i] = xl[i] + h;
yl[i] = (i / n)*h;
yr[i] = yl[i] + h;
if (!rank) {
ierr = PetscDrawLine(draw,xl[i],yl[i],xl[i],yr[i],PETSC_DRAW_BLACK);CHKERRQ(ierr);
ierr = PetscDrawLine(draw,xl[i],yr[i],xr[i],yr[i],PETSC_DRAW_BLACK);CHKERRQ(ierr);
ierr = PetscDrawLine(draw,xr[i],yr[i],xr[i],yl[i],PETSC_DRAW_BLACK);CHKERRQ(ierr);
ierr = PetscDrawLine(draw,xr[i],yl[i],xl[i],yl[i],PETSC_DRAW_BLACK);CHKERRQ(ierr);
}
xl[i] += .05*h;
xr[i] -= .05*h;
yl[i] += .05*h;
yr[i] -= .05*h;
}
ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例8: FormInitialGuess
/*
FormInitialGuess - Forms initial approximation.
Input Parameters:
user - user-defined application context
X - vector
Output Parameter:
X - vector
*/
PetscErrorCode FormInitialGuess(AppCtx *user,Vec X)
{
PetscInt i,j,k,Mx,My,Mz,xs,ys,zs,xm,ym,zm;
PetscErrorCode ierr;
PetscReal lambda,temp1,hx,hy,hz,tempk,tempj;
PetscScalar ***x;
PetscFunctionBeginUser;
ierr = DMDAGetInfo(user->da,PETSC_IGNORE,&Mx,&My,&Mz,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE);CHKERRQ(ierr);
lambda = user->param;
hx = 1.0/(PetscReal)(Mx-1);
hy = 1.0/(PetscReal)(My-1);
hz = 1.0/(PetscReal)(Mz-1);
temp1 = lambda/(lambda + 1.0);
/*
Get a pointer 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 = DMDAVecGetArray(user->da,X,&x);CHKERRQ(ierr);
/*
Get local grid boundaries (for 3-dimensional DMDA):
xs, ys, zs - starting grid indices (no ghost points)
xm, ym, zm - widths of local grid (no ghost points)
*/
ierr = DMDAGetCorners(user->da,&xs,&ys,&zs,&xm,&ym,&zm);CHKERRQ(ierr);
/*
Compute initial guess over the locally owned part of the grid
*/
for (k=zs; k<zs+zm; k++) {
tempk = (PetscReal)(PetscMin(k,Mz-k-1))*hz;
for (j=ys; j<ys+ym; j++) {
tempj = PetscMin((PetscReal)(PetscMin(j,My-j-1))*hy,tempk);
for (i=xs; i<xs+xm; i++) {
if (i == 0 || j == 0 || k == 0 || i == Mx-1 || j == My-1 || k == Mz-1) {
/* boundary conditions are all zero Dirichlet */
x[k][j][i] = 0.0;
} else {
x[k][j][i] = temp1*PetscSqrtReal(PetscMin((PetscReal)(PetscMin(i,Mx-i-1))*hx,tempj));
}
}
}
}
/*
Restore vector
*/
ierr = DMDAVecRestoreArray(user->da,X,&x);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例9: step
/*
Monitor - User-provided routine to monitor the solution computed at
each timestep. This example plots the solution and computes the
error in two different norms.
Input Parameters:
ts - the timestep context
step - the count of the current step (with 0 meaning the
initial condition)
time - the current time
u - the solution at this timestep
ctx - the user-provided context for this monitoring routine.
In this case we use the application context which contains
information about the problem size, workspace and the exact
solution.
*/
PetscErrorCode Monitor(TS ts,PetscInt step,PetscReal time,Vec u,void *ctx)
{
AppCtx *appctx = (AppCtx*) ctx; /* user-defined application context */
PetscErrorCode ierr;
PetscReal norm_2,norm_max;
/*
View a graph of the current iterate
*/
ierr = VecView(u,appctx->viewer2);CHKERRQ(ierr);
/*
Compute the exact solution
*/
ierr = ExactSolution(time,appctx->solution,appctx);CHKERRQ(ierr);
/*
Print debugging information if desired
*/
if (appctx->debug) {
printf("Computed solution vector\n");
ierr = VecView(u,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
printf("Exact solution vector\n");
ierr = VecView(appctx->solution,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
}
/*
Compute the 2-norm and max-norm of the error
*/
ierr = VecAXPY(appctx->solution,-1.0,u);CHKERRQ(ierr);
ierr = VecNorm(appctx->solution,NORM_2,&norm_2);CHKERRQ(ierr);
norm_2 = PetscSqrtReal(appctx->h)*norm_2;
ierr = VecNorm(appctx->solution,NORM_MAX,&norm_max);CHKERRQ(ierr);
ierr = PetscPrintf(PETSC_COMM_WORLD,"Timestep %D: time = %G, 2-norm error = %G, max norm error = %G\n",
step,time,norm_2,norm_max);CHKERRQ(ierr);
appctx->norm_2 += norm_2;
appctx->norm_max += norm_max;
/*
View a graph of the error
*/
ierr = VecView(appctx->solution,appctx->viewer1);CHKERRQ(ierr);
/*
Print debugging information if desired
*/
if (appctx->debug) {
printf("Error vector\n");
ierr = VecView(appctx->solution,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
}
return 0;
}
示例10: KSPAGMRESLejaOrdering
PetscErrorCode KSPAGMRESLejaOrdering(PetscScalar *re, PetscScalar *im, PetscScalar *rre, PetscScalar *rim, PetscInt m)
{
PetscInt *spos;
PetscScalar *n_cmpl,temp;
PetscErrorCode ierr;
PetscInt i, pos, j;
ierr = PetscMalloc(m*sizeof(PetscScalar), &n_cmpl);CHKERRQ(ierr);
ierr = PetscMalloc(m*sizeof(PetscInt), &spos);CHKERRQ(ierr);
PetscFunctionBegin;
/* Check the proper order of complex conjugate pairs */
j = 0;
while (j < m ) {
if (im[j] != 0.0) {/* complex eigenvalue */
if (im[j] < 0.0) { /* change the order */
temp = im[j+1];
im[j+1] = im[j];
im[j] = temp;
}
j += 2;
} else j++;
}
for (i = 0;i < m;i++)
n_cmpl[i] = PetscSqrtReal(re[i]*re[i]+im[i]*im[i]);
KSPAGMRESLejafmaxarray(n_cmpl, 0, m, &pos);
j = 0;
if (im[pos] >= 0.0) {
rre[0] = re[pos];
rim[0] = im[pos];
j++;
spos[0] = pos;
}
while (j < (m)) {
if (im[pos] > 0) {
rre[j] = re[pos+1];
rim[j] = im[pos+1];
spos[j] = pos + 1;
j++;
}
KSPAGMRESLejaCfpdMax(re, im, spos, j, m, &pos);
if (im[pos] < 0) pos--;
if ((im[pos] >= 0) && (j < m)) {
rre[j] = re[pos];
rim[j] = im[pos];
spos[j] = pos;
j++;
}
}
ierr = PetscFree(spos);CHKERRQ(ierr);
ierr = PetscFree(n_cmpl);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例11: context
/*@C
PetscDrawViewPortsCreate - Splits a window into smaller
view ports. Each processor shares all the viewports.
Collective on PetscDraw
Input Parameters:
+ draw - the drawing context
- nports - the number of ports
Output Parameter:
. ports - a PetscDrawViewPorts context (C structure)
Level: advanced
Concepts: drawing^in subset of window
.seealso: PetscDrawSplitViewPort(), PetscDrawSetViewPort(), PetscDrawViewPortsSet(), PetscDrawViewPortsDestroy()
@*/
PetscErrorCode PetscDrawViewPortsCreate(PetscDraw draw,PetscInt nports,PetscDrawViewPorts **ports)
{
PetscInt i,n;
PetscErrorCode ierr;
PetscBool isnull;
PetscReal *xl,*xr,*yl,*yr,h;
PetscFunctionBegin;
PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
PetscValidPointer(ports,3);
ierr = PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);CHKERRQ(ierr);
if (isnull) {
*ports = NULL;
PetscFunctionReturn(0);
}
ierr = PetscNew(ports);CHKERRQ(ierr);
(*ports)->draw = draw;
(*ports)->nports = nports;
ierr = PetscObjectReference((PetscObject)draw);CHKERRQ(ierr);
n = (PetscInt)(.1 + PetscSqrtReal((PetscReal)nports));
while (n*n < nports) n++;
ierr = PetscMalloc1(n*n,&xl);CHKERRQ(ierr);(*ports)->xl = xl;
ierr = PetscMalloc1(n*n,&xr);CHKERRQ(ierr);(*ports)->xr = xr;
ierr = PetscMalloc1(n*n,&yl);CHKERRQ(ierr);(*ports)->yl = yl;
ierr = PetscMalloc1(n*n,&yr);CHKERRQ(ierr);(*ports)->yr = yr;
h = 1.0/n;
for (i=0; i<n*n; i++) {
xl[i] = (i % n)*h;
xr[i] = xl[i] + h;
yl[i] = (i/n)*h;
yr[i] = yl[i] + h;
ierr = PetscDrawLine(draw,xl[i],yl[i],xl[i],yr[i],PETSC_DRAW_BLACK);CHKERRQ(ierr);
ierr = PetscDrawLine(draw,xl[i],yr[i],xr[i],yr[i],PETSC_DRAW_BLACK);CHKERRQ(ierr);
ierr = PetscDrawLine(draw,xr[i],yr[i],xr[i],yl[i],PETSC_DRAW_BLACK);CHKERRQ(ierr);
ierr = PetscDrawLine(draw,xr[i],yl[i],xl[i],yl[i],PETSC_DRAW_BLACK);CHKERRQ(ierr);
xl[i] += .1*h;
xr[i] -= .1*h;
yl[i] += .1*h;
yr[i] -= .1*h;
}
/* save previous drawport of window */
ierr = PetscDrawGetViewPort(draw,&(*ports)->port_xl,&(*ports)->port_yl,&(*ports)->port_xr,&(*ports)->port_yr);CHKERRQ(ierr);
/* ierr = PetscDrawSynchronizedFlush(draw);CHKERRQ(ierr);*/ /* this causes flicker */
PetscFunctionReturn(0);
}
示例12: SVDOrthogonalizeCGS
/*
Custom CGS orthogonalization, preprocess after first orthogonalization
*/
static PetscErrorCode SVDOrthogonalizeCGS(BV V,PetscInt i,PetscScalar* h,PetscReal a,BVOrthogRefineType refine,PetscReal eta,PetscReal *norm)
{
PetscErrorCode ierr;
PetscReal sum,onorm;
PetscScalar dot;
PetscInt j;
PetscFunctionBegin;
switch (refine) {
case BV_ORTHOG_REFINE_NEVER:
ierr = BVNormColumn(V,i,NORM_2,norm);CHKERRQ(ierr);
break;
case BV_ORTHOG_REFINE_ALWAYS:
ierr = BVSetActiveColumns(V,0,i);CHKERRQ(ierr);
ierr = BVDotColumn(V,i,h);CHKERRQ(ierr);
ierr = BVMultColumn(V,-1.0,1.0,i,h);CHKERRQ(ierr);
ierr = BVNormColumn(V,i,NORM_2,norm);CHKERRQ(ierr);
break;
case BV_ORTHOG_REFINE_IFNEEDED:
dot = h[i];
onorm = PetscSqrtReal(PetscRealPart(dot)) / a;
sum = 0.0;
for (j=0;j<i;j++) {
sum += PetscRealPart(h[j] * PetscConj(h[j]));
}
*norm = PetscRealPart(dot)/(a*a) - sum;
if (*norm>0.0) *norm = PetscSqrtReal(*norm);
else {
ierr = BVNormColumn(V,i,NORM_2,norm);CHKERRQ(ierr);
}
if (*norm < eta*onorm) {
ierr = BVSetActiveColumns(V,0,i);CHKERRQ(ierr);
ierr = BVDotColumn(V,i,h);CHKERRQ(ierr);
ierr = BVMultColumn(V,-1.0,1.0,i,h);CHKERRQ(ierr);
ierr = BVNormColumn(V,i,NORM_2,norm);CHKERRQ(ierr);
}
break;
}
PetscFunctionReturn(0);
}
示例13: coordinate
/*@C
DMDAGetLogicalCoordinate - Returns a the i,j,k logical coordinate for the closest mesh point to a x,y,z point in the coordinates of the DMDA
Collective on DMDA
Input Parameters:
+ da - the distributed array
- x,y,z - the physical coordinates
Output Parameters:
+ II, JJ, KK - the logical coordinate (-1 on processes that do not contain that point)
- X, Y, Z, - (optional) the coordinates of the located grid point
Level: advanced
Notes:
All processors that share the DMDA must call this with the same coordinate value
.keywords: distributed array, get, processor subset
@*/
PetscErrorCode DMDAGetLogicalCoordinate(DM da,PetscScalar x,PetscScalar y,PetscScalar z,PetscInt *II,PetscInt *JJ,PetscInt *KK,PetscScalar *X,PetscScalar *Y,PetscScalar *Z)
{
DM_DA *dd = (DM_DA*)da->data;
PetscErrorCode ierr;
Vec coors;
DM dacoors;
DMDACoor2d **c;
PetscInt i,j,xs,xm,ys,ym;
PetscReal d,D = PETSC_MAX_REAL,Dv;
PetscMPIInt rank,root;
PetscFunctionBegin;
if (dd->dim == 1) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_SUP,"Cannot get point from 1d DMDA");
if (dd->dim == 3) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_SUP,"Cannot get point from 3d DMDA");
*II = -1;
*JJ = -1;
ierr = DMGetCoordinateDM(da,&dacoors);CHKERRQ(ierr);
ierr = DMDAGetCorners(dacoors,&xs,&ys,NULL,&xm,&ym,NULL);CHKERRQ(ierr);
ierr = DMGetCoordinates(da,&coors);CHKERRQ(ierr);
ierr = DMDAVecGetArray(dacoors,coors,&c);CHKERRQ(ierr);
for (j=ys; j<ys+ym; j++) {
for (i=xs; i<xs+xm; i++) {
d = PetscSqrtReal(PetscRealPart( (c[j][i].x - x)*(c[j][i].x - x) + (c[j][i].y - y)*(c[j][i].y - y) ));
if (d < D) {
D = d;
*II = i;
*JJ = j;
}
}
}
ierr = MPI_Allreduce(&D,&Dv,1,MPIU_REAL,MPI_MIN,PetscObjectComm((PetscObject)da));CHKERRQ(ierr);
if (D != Dv) {
*II = -1;
*JJ = -1;
rank = 0;
} else {
*X = c[*JJ][*II].x;
*Y = c[*JJ][*II].y;
ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)da),&rank);CHKERRQ(ierr);
rank++;
}
ierr = MPI_Allreduce(&rank,&root,1,MPI_INT,MPI_SUM,PetscObjectComm((PetscObject)da));CHKERRQ(ierr);
root--;
ierr = MPI_Bcast(X,1,MPIU_SCALAR,root,PetscObjectComm((PetscObject)da));CHKERRQ(ierr);
ierr = MPI_Bcast(Y,1,MPIU_SCALAR,root,PetscObjectComm((PetscObject)da));CHKERRQ(ierr);
ierr = DMDAVecRestoreArray(dacoors,coors,&c);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例14: SNESNGMRESSelectRestart_Private
PetscErrorCode SNESNGMRESSelectRestart_Private(SNES snes,PetscInt l,PetscReal fAnorm,PetscReal dnorm,PetscReal fminnorm,PetscReal dminnorm,PetscBool *selectRestart)
{
SNES_NGMRES *ngmres = (SNES_NGMRES*)snes->data;
PetscErrorCode ierr;
PetscFunctionBegin;
*selectRestart = PETSC_FALSE;
/* difference stagnation restart */
if ((ngmres->epsilonB*dnorm > dminnorm) && (PetscSqrtReal(fAnorm) > ngmres->deltaB*PetscSqrtReal(fminnorm)) && l > 0) {
if (ngmres->monitor) {
ierr = PetscViewerASCIIPrintf(ngmres->monitor,"difference restart: %e > %e\n",ngmres->epsilonB*dnorm,dminnorm);CHKERRQ(ierr);
}
*selectRestart = PETSC_TRUE;
}
/* residual stagnation restart */
if (PetscSqrtReal(fAnorm) > ngmres->gammaC*PetscSqrtReal(fminnorm)) {
if (ngmres->monitor) {
ierr = PetscViewerASCIIPrintf(ngmres->monitor,"residual restart: %e > %e\n",PetscSqrtReal(fAnorm),ngmres->gammaC*PetscSqrtReal(fminnorm));CHKERRQ(ierr);
}
*selectRestart = PETSC_TRUE;
}
PetscFunctionReturn(0);
}
示例15: VecNorm_MPI
PetscErrorCode VecNorm_MPI(Vec xin,NormType type,PetscReal *z)
{
PetscReal sum,work = 0.0;
const PetscScalar *xx;
PetscErrorCode ierr;
PetscInt n = xin->map->n;
PetscBLASInt one = 1,bn;
PetscFunctionBegin;
ierr = PetscBLASIntCast(n,&bn);CHKERRQ(ierr);
if (type == NORM_2 || type == NORM_FROBENIUS) {
ierr = VecGetArrayRead(xin,&xx);CHKERRQ(ierr);
work = PetscRealPart(BLASdot_(&bn,xx,&one,xx,&one));
ierr = VecRestoreArrayRead(xin,&xx);CHKERRQ(ierr);
ierr = MPIU_Allreduce(&work,&sum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)xin));CHKERRQ(ierr);
*z = PetscSqrtReal(sum);
ierr = PetscLogFlops(2.0*xin->map->n);CHKERRQ(ierr);
} else if (type == NORM_1) {
/* Find the local part */
ierr = VecNorm_Seq(xin,NORM_1,&work);CHKERRQ(ierr);
/* Find the global max */
ierr = MPIU_Allreduce(&work,z,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)xin));CHKERRQ(ierr);
} else if (type == NORM_INFINITY) {
/* Find the local max */
ierr = VecNorm_Seq(xin,NORM_INFINITY,&work);CHKERRQ(ierr);
/* Find the global max */
ierr = MPIU_Allreduce(&work,z,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)xin));CHKERRQ(ierr);
} else if (type == NORM_1_AND_2) {
PetscReal temp[2];
ierr = VecNorm_Seq(xin,NORM_1,temp);CHKERRQ(ierr);
ierr = VecNorm_Seq(xin,NORM_2,temp+1);CHKERRQ(ierr);
temp[1] = temp[1]*temp[1];
ierr = MPIU_Allreduce(temp,z,2,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)xin));CHKERRQ(ierr);
z[1] = PetscSqrtReal(z[1]);
}
PetscFunctionReturn(0);
}