本文整理汇总了C++中PetscMin函数的典型用法代码示例。如果您正苦于以下问题:C++ PetscMin函数的具体用法?C++ PetscMin怎么用?C++ PetscMin使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PetscMin函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: LINPACKcgpthy
static PetscReal LINPACKcgpthy(PetscReal *a,PetscReal *b)
{
/* System generated locals */
PetscReal ret_val,d__1,d__2,d__3;
/* Local variables */
PetscReal p,r,s,t,u;
PetscFunctionBegin;
/* FINDS DSQRT(A**2+B**2) WITHOUT OVERFLOW OR DESTRUCTIVE UNDERFLOW */
/* Computing MAX */
d__1 = PetscAbsReal(*a);
d__2 = PetscAbsReal(*b);
p = PetscMax(d__1,d__2);
if (!p) goto L20;
/* Computing MIN */
d__2 = PetscAbsReal(*a);
d__3 = PetscAbsReal(*b);
/* Computing 2nd power */
d__1 = PetscMin(d__2,d__3) / p;
r = d__1 * d__1;
L10:
t = r + 4.;
if (t == 4.) goto L20;
s = r / t;
u = s * 2. + 1.;
p = u * p;
/* Computing 2nd power */
d__1 = s / u;
r = d__1 * d__1 * r;
goto L10;
L20:
ret_val = p;
PetscFunctionReturn(ret_val);
} /* cgpthy_ */
示例2: KSPView_FCG
PetscErrorCode KSPView_FCG(KSP ksp,PetscViewer viewer)
{
KSP_FCG *fcg = (KSP_FCG*)ksp->data;
PetscErrorCode ierr;
PetscBool iascii,isstring;
const char *truncstr;
PetscFunctionBegin;
ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
if (fcg->truncstrat == KSP_FCG_TRUNC_TYPE_STANDARD) truncstr = "Using standard truncation strategy";
else if (fcg->truncstrat == KSP_FCG_TRUNC_TYPE_NOTAY) truncstr = "Using Notay's truncation strategy";
else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Undefined FCG truncation strategy");
if (iascii) {
ierr = PetscViewerASCIIPrintf(viewer," FCG: m_max=%D\n",fcg->mmax);CHKERRQ(ierr);
ierr = PetscViewerASCIIPrintf(viewer," FCG: preallocated %D directions\n",PetscMin(fcg->nprealloc,fcg->mmax+1));CHKERRQ(ierr);
ierr = PetscViewerASCIIPrintf(viewer," FCG: %s\n",truncstr);CHKERRQ(ierr);
} else if (isstring) {
ierr = PetscViewerStringSPrintf(viewer,"m_max %D nprealloc %D %s",fcg->mmax,fcg->nprealloc,truncstr);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
示例3: main
int main( int argc, char **argv )
{
PetscErrorCode ierr;
Mat A; /* Grcar matrix */
SVD svd; /* singular value solver context */
PetscInt N=30, Istart, Iend, i, col[5], nconv1, nconv2;
PetscScalar value[] = { -1, 1, 1, 1, 1 };
PetscReal sigma_1, sigma_n;
SlepcInitialize(&argc,&argv,(char*)0,help);
ierr = PetscOptionsGetInt(PETSC_NULL,"-n",&N,PETSC_NULL);CHKERRQ(ierr);
ierr = PetscPrintf(PETSC_COMM_WORLD,"\nEstimate the condition number of a Grcar matrix, n=%d\n\n",N);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Generate the matrix
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);CHKERRQ(ierr);
ierr = MatSetFromOptions(A);CHKERRQ(ierr);
ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr);
for( i=Istart; i<Iend; i++ ) {
col[0]=i-1; col[1]=i; col[2]=i+1; col[3]=i+2; col[4]=i+3;
if (i==0) {
ierr = MatSetValues(A,1,&i,4,col+1,value+1,INSERT_VALUES);CHKERRQ(ierr);
}
else {
ierr = MatSetValues(A,1,&i,PetscMin(5,N-i+1),col,value,INSERT_VALUES);CHKERRQ(ierr);
}
}
ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Create the singular value solver and set the solution method
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*
Create singular value context
*/
ierr = SVDCreate(PETSC_COMM_WORLD,&svd);CHKERRQ(ierr);
/*
Set operator
*/
ierr = SVDSetOperator(svd,A);CHKERRQ(ierr);
/*
Set solver parameters at runtime
*/
ierr = SVDSetFromOptions(svd);CHKERRQ(ierr);
ierr = SVDSetDimensions(svd,1,PETSC_IGNORE,PETSC_IGNORE);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Solve the eigensystem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*
First request an eigenvalue from one end of the spectrum
*/
ierr = SVDSetWhichSingularTriplets(svd,SVD_LARGEST);CHKERRQ(ierr);
ierr = SVDSolve(svd);CHKERRQ(ierr);
/*
Get number of converged singular values
*/
ierr = SVDGetConverged(svd,&nconv1);CHKERRQ(ierr);
/*
Get converged singular values: largest singular value is stored in sigma_1.
In this example, we are not interested in the singular vectors
*/
if (nconv1 > 0) {
ierr = SVDGetSingularTriplet(svd,0,&sigma_1,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
} else {
ierr = PetscPrintf(PETSC_COMM_WORLD," Unable to compute large singular value!\n\n");CHKERRQ(ierr);
}
/*
Request an eigenvalue from the other end of the spectrum
*/
ierr = SVDSetWhichSingularTriplets(svd,SVD_SMALLEST);CHKERRQ(ierr);
ierr = SVDSolve(svd);CHKERRQ(ierr);
/*
Get number of converged eigenpairs
*/
ierr = SVDGetConverged(svd,&nconv2);CHKERRQ(ierr);
/*
Get converged singular values: smallest singular value is stored in sigma_n.
As before, we are not interested in the singular vectors
*/
if (nconv2 > 0) {
ierr = SVDGetSingularTriplet(svd,0,&sigma_n,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
} else {
ierr = PetscPrintf(PETSC_COMM_WORLD," Unable to compute small singular value!\n\n");CHKERRQ(ierr);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Display solution and clean up
//.........这里部分代码省略.........
示例4: KSPFGMRESCycle
PetscErrorCode KSPFGMRESCycle(PetscInt *itcount,KSP ksp)
{
KSP_FGMRES *fgmres = (KSP_FGMRES*)(ksp->data);
PetscReal res_norm;
PetscReal hapbnd,tt;
PetscBool hapend = PETSC_FALSE; /* indicates happy breakdown ending */
PetscErrorCode ierr;
PetscInt loc_it; /* local count of # of dir. in Krylov space */
PetscInt max_k = fgmres->max_k; /* max # of directions Krylov space */
Mat Amat,Pmat;
MatStructure pflag;
PetscFunctionBegin;
/* Number of pseudo iterations since last restart is the number
of prestart directions */
loc_it = 0;
/* note: (fgmres->it) is always set one less than (loc_it) It is used in
KSPBUILDSolution_FGMRES, where it is passed to KSPFGMRESBuildSoln.
Note that when KSPFGMRESBuildSoln is called from this function,
(loc_it -1) is passed, so the two are equivalent */
fgmres->it = (loc_it - 1);
/* initial residual is in VEC_VV(0) - compute its norm*/
ierr = VecNorm(VEC_VV(0),NORM_2,&res_norm);CHKERRQ(ierr);
/* first entry in right-hand-side of hessenberg system is just
the initial residual norm */
*RS(0) = res_norm;
ksp->rnorm = res_norm;
ierr = KSPLogResidualHistory(ksp,res_norm);CHKERRQ(ierr);
ierr = KSPMonitor(ksp,ksp->its,res_norm);CHKERRQ(ierr);
/* check for the convergence - maybe the current guess is good enough */
ierr = (*ksp->converged)(ksp,ksp->its,res_norm,&ksp->reason,ksp->cnvP);CHKERRQ(ierr);
if (ksp->reason) {
if (itcount) *itcount = 0;
PetscFunctionReturn(0);
}
/* scale VEC_VV (the initial residual) */
ierr = VecScale(VEC_VV(0),1.0/res_norm);CHKERRQ(ierr);
/* MAIN ITERATION LOOP BEGINNING*/
/* keep iterating until we have converged OR generated the max number
of directions OR reached the max number of iterations for the method */
while (!ksp->reason && loc_it < max_k && ksp->its < ksp->max_it) {
if (loc_it) {
ierr = KSPLogResidualHistory(ksp,res_norm);CHKERRQ(ierr);
ierr = KSPMonitor(ksp,ksp->its,res_norm);CHKERRQ(ierr);
}
fgmres->it = (loc_it - 1);
/* see if more space is needed for work vectors */
if (fgmres->vv_allocated <= loc_it + VEC_OFFSET + 1) {
ierr = KSPFGMRESGetNewVectors(ksp,loc_it+1);CHKERRQ(ierr);
/* (loc_it+1) is passed in as number of the first vector that should
be allocated */
}
/* CHANGE THE PRECONDITIONER? */
/* ModifyPC is the callback function that can be used to
change the PC or its attributes before its applied */
(*fgmres->modifypc)(ksp,ksp->its,loc_it,res_norm,fgmres->modifyctx);
/* apply PRECONDITIONER to direction vector and store with
preconditioned vectors in prevec */
ierr = KSP_PCApply(ksp,VEC_VV(loc_it),PREVEC(loc_it));CHKERRQ(ierr);
ierr = PCGetOperators(ksp->pc,&Amat,&Pmat,&pflag);CHKERRQ(ierr);
/* Multiply preconditioned vector by operator - put in VEC_VV(loc_it+1) */
ierr = MatMult(Amat,PREVEC(loc_it),VEC_VV(1+loc_it));CHKERRQ(ierr);
/* update hessenberg matrix and do Gram-Schmidt - new direction is in
VEC_VV(1+loc_it)*/
ierr = (*fgmres->orthog)(ksp,loc_it);CHKERRQ(ierr);
/* new entry in hessenburg is the 2-norm of our new direction */
ierr = VecNorm(VEC_VV(loc_it+1),NORM_2,&tt);CHKERRQ(ierr);
*HH(loc_it+1,loc_it) = tt;
*HES(loc_it+1,loc_it) = tt;
/* Happy Breakdown Check */
hapbnd = PetscAbsScalar((tt) / *RS(loc_it));
/* RS(loc_it) contains the res_norm from the last iteration */
hapbnd = PetscMin(fgmres->haptol,hapbnd);
if (tt > hapbnd) {
/* scale new direction by its norm */
ierr = VecScale(VEC_VV(loc_it+1),1.0/tt);CHKERRQ(ierr);
} else {
/* This happens when the solution is exactly reached. */
/* So there is no new direction... */
ierr = VecSet(VEC_TEMP,0.0);CHKERRQ(ierr); /* set VEC_TEMP to 0 */
hapend = PETSC_TRUE;
}
//.........这里部分代码省略.........
示例5: FormInitialGuess
/*
FormInitialGuess - Forms initial approximation.
Input Parameters:
user - user-defined application context
X - vector
Output Parameter:
X - vector
*/
int FormInitialGuess(AppCtx *user,Vec X)
{
int i,j,row,mx,my,ierr;
PetscReal one = 1.0,lambda,temp1,temp,hx,hy,hxdhy,hydhx,sc;
PetscScalar *x;
/*
Process 0 has to wait for all other processes to get here
before proceeding to write in the shared vector
*/
ierr = PetscBarrier((PetscObject)X);CHKERRQ(ierr);
if (user->rank) {
/*
All the non-busy processors have to wait here for process 0 to finish
evaluating the function; otherwise they will start using the vector values
before they have been computed
*/
ierr = PetscBarrier((PetscObject)X);CHKERRQ(ierr);
return 0;
}
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(X,&x);CHKERRQ(ierr);
/*
Compute initial guess over the locally owned part of the grid
*/
#pragma arl(4)
#pragma distinct (*x,*f)
#pragma no side effects (sqrt)
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));
}
}
/*
Restore vector
*/
ierr = VecRestoreArray(X,&x);CHKERRQ(ierr);
ierr = PetscBarrier((PetscObject)X);CHKERRQ(ierr);
return 0;
}
示例6: VecView_MPI_Draw_DA1d
PetscErrorCode VecView_MPI_Draw_DA1d(Vec xin,PetscViewer v)
{
DM da;
PetscErrorCode ierr;
PetscMPIInt rank,size,tag1,tag2;
PetscInt i,n,N,step,istart,isize,j,nbounds;
MPI_Status status;
PetscReal coors[4],ymin,ymax,min,max,xmin = 0.0,xmax = 0.0,tmp = 0.0,xgtmp = 0.0;
const PetscScalar *array,*xg;
PetscDraw draw;
PetscBool isnull,showpoints = PETSC_FALSE;
MPI_Comm comm;
PetscDrawAxis axis;
Vec xcoor;
DMBoundaryType bx;
const PetscReal *bounds;
PetscInt *displayfields;
PetscInt k,ndisplayfields;
PetscBool hold;
PetscFunctionBegin;
ierr = PetscViewerDrawGetDraw(v,0,&draw);CHKERRQ(ierr);
ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0);
ierr = PetscViewerDrawGetBounds(v,&nbounds,&bounds);CHKERRQ(ierr);
ierr = VecGetDM(xin,&da);CHKERRQ(ierr);
if (!da) SETERRQ(PetscObjectComm((PetscObject)xin),PETSC_ERR_ARG_WRONG,"Vector not generated from a DMDA");
ierr = PetscOptionsGetBool(NULL,"-draw_vec_mark_points",&showpoints,NULL);CHKERRQ(ierr);
ierr = DMDAGetInfo(da,0,&N,0,0,0,0,0,&step,0,&bx,0,0,0);CHKERRQ(ierr);
ierr = DMDAGetCorners(da,&istart,0,0,&isize,0,0);CHKERRQ(ierr);
ierr = VecGetArrayRead(xin,&array);CHKERRQ(ierr);
ierr = VecGetLocalSize(xin,&n);CHKERRQ(ierr);
n = n/step;
/* get coordinates of nodes */
ierr = DMGetCoordinates(da,&xcoor);CHKERRQ(ierr);
if (!xcoor) {
ierr = DMDASetUniformCoordinates(da,0.0,1.0,0.0,0.0,0.0,0.0);CHKERRQ(ierr);
ierr = DMGetCoordinates(da,&xcoor);CHKERRQ(ierr);
}
ierr = VecGetArrayRead(xcoor,&xg);CHKERRQ(ierr);
ierr = PetscObjectGetComm((PetscObject)xin,&comm);CHKERRQ(ierr);
ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
/*
Determine the min and max x coordinate in plot
*/
if (!rank) {
xmin = PetscRealPart(xg[0]);
}
if (rank == size-1) {
xmax = PetscRealPart(xg[n-1]);
}
ierr = MPI_Bcast(&xmin,1,MPIU_REAL,0,comm);CHKERRQ(ierr);
ierr = MPI_Bcast(&xmax,1,MPIU_REAL,size-1,comm);CHKERRQ(ierr);
ierr = DMDASelectFields(da,&ndisplayfields,&displayfields);CHKERRQ(ierr);
for (k=0; k<ndisplayfields; k++) {
j = displayfields[k];
ierr = PetscViewerDrawGetDraw(v,k,&draw);CHKERRQ(ierr);
ierr = PetscDrawCheckResizedWindow(draw);CHKERRQ(ierr);
/*
Determine the min and max y coordinate in plot
*/
min = 1.e20; max = -1.e20;
for (i=0; i<n; i++) {
if (PetscRealPart(array[j+i*step]) < min) min = PetscRealPart(array[j+i*step]);
if (PetscRealPart(array[j+i*step]) > max) max = PetscRealPart(array[j+i*step]);
}
if (min + 1.e-10 > max) {
min -= 1.e-5;
max += 1.e-5;
}
if (j < nbounds) {
min = PetscMin(min,bounds[2*j]);
max = PetscMax(max,bounds[2*j+1]);
}
ierr = MPI_Reduce(&min,&ymin,1,MPIU_REAL,MPIU_MIN,0,comm);CHKERRQ(ierr);
ierr = MPI_Reduce(&max,&ymax,1,MPIU_REAL,MPIU_MAX,0,comm);CHKERRQ(ierr);
ierr = PetscViewerDrawGetHold(v,&hold);CHKERRQ(ierr);
if (!hold) {
ierr = PetscDrawSynchronizedClear(draw);CHKERRQ(ierr);
}
ierr = PetscViewerDrawGetDrawAxis(v,k,&axis);CHKERRQ(ierr);
ierr = PetscLogObjectParent((PetscObject)draw,(PetscObject)axis);CHKERRQ(ierr);
if (!rank) {
const char *title;
ierr = PetscDrawAxisSetLimits(axis,xmin,xmax,ymin,ymax);CHKERRQ(ierr);
ierr = PetscDrawAxisDraw(axis);CHKERRQ(ierr);
ierr = PetscDrawGetCoordinates(draw,coors,coors+1,coors+2,coors+3);CHKERRQ(ierr);
ierr = DMDAGetFieldName(da,j,&title);CHKERRQ(ierr);
if (title) {ierr = PetscDrawSetTitle(draw,title);CHKERRQ(ierr);}
//.........这里部分代码省略.........
示例7: DMPlexVTKWriteCells_ASCII
PetscErrorCode DMPlexVTKWriteCells_ASCII(DM dm, FILE *fp, PetscInt *totalCells)
{
MPI_Comm comm;
DMLabel label;
IS globalVertexNumbers = NULL;
const PetscInt *gvertex;
PetscInt dim;
PetscInt numCorners = 0, totCorners = 0, maxCorners, *corners;
PetscInt numCells = 0, totCells = 0, maxCells, cellHeight;
PetscInt numLabelCells, maxLabelCells, cMax, cStart, cEnd, c, vStart, vEnd, v;
PetscMPIInt numProcs, rank, proc, tag;
PetscErrorCode ierr;
PetscFunctionBegin;
ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
ierr = PetscCommGetNewTag(comm, &tag);CHKERRQ(ierr);
ierr = MPI_Comm_size(comm, &numProcs);CHKERRQ(ierr);
ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, NULL);CHKERRQ(ierr);
if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
ierr = DMPlexGetLabel(dm, "vtk", &label);CHKERRQ(ierr);
ierr = DMPlexGetStratumSize(dm, "vtk", 1, &numLabelCells);CHKERRQ(ierr);
ierr = MPI_Allreduce(&numLabelCells, &maxLabelCells, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
if (!maxLabelCells) label = NULL;
for (c = cStart; c < cEnd; ++c) {
PetscInt *closure = NULL;
PetscInt closureSize, value;
if (label) {
ierr = DMLabelGetValue(label, c, &value);CHKERRQ(ierr);
if (value != 1) continue;
}
ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
for (v = 0; v < closureSize*2; v += 2) {
if ((closure[v] >= vStart) && (closure[v] < vEnd)) ++numCorners;
}
ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
++numCells;
}
maxCells = numCells;
ierr = MPI_Reduce(&numCells, &totCells, 1, MPIU_INT, MPI_SUM, 0, comm);CHKERRQ(ierr);
ierr = MPI_Reduce(&numCells, &maxCells, 1, MPIU_INT, MPI_MAX, 0, comm);CHKERRQ(ierr);
ierr = MPI_Reduce(&numCorners, &totCorners, 1, MPIU_INT, MPI_SUM, 0, comm);CHKERRQ(ierr);
ierr = MPI_Reduce(&numCorners, &maxCorners, 1, MPIU_INT, MPI_MAX, 0, comm);CHKERRQ(ierr);
ierr = DMPlexGetVertexNumbering(dm, &globalVertexNumbers);CHKERRQ(ierr);
ierr = ISGetIndices(globalVertexNumbers, &gvertex);CHKERRQ(ierr);
ierr = PetscMalloc1(maxCells, &corners);CHKERRQ(ierr);
ierr = PetscFPrintf(comm, fp, "CELLS %d %d\n", totCells, totCorners+totCells);CHKERRQ(ierr);
if (!rank) {
PetscInt *remoteVertices;
int *vertices;
ierr = PetscMalloc1(maxCorners, &vertices);CHKERRQ(ierr);
for (c = cStart, numCells = 0; c < cEnd; ++c) {
PetscInt *closure = NULL;
PetscInt closureSize, value, nC = 0;
if (label) {
ierr = DMLabelGetValue(label, c, &value);CHKERRQ(ierr);
if (value != 1) continue;
}
ierr = DMPlexGetTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
for (v = 0; v < closureSize*2; v += 2) {
if ((closure[v] >= vStart) && (closure[v] < vEnd)) {
const PetscInt gv = gvertex[closure[v] - vStart];
vertices[nC++] = gv < 0 ? -(gv+1) : gv;
}
}
ierr = DMPlexRestoreTransitiveClosure(dm, c, PETSC_TRUE, &closureSize, &closure);CHKERRQ(ierr);
corners[numCells++] = nC;
ierr = PetscFPrintf(comm, fp, "%d ", nC);CHKERRQ(ierr);
ierr = DMPlexInvertCell(dim, nC, vertices);CHKERRQ(ierr);
for (v = 0; v < nC; ++v) {
ierr = PetscFPrintf(comm, fp, " %d", vertices[v]);CHKERRQ(ierr);
}
ierr = PetscFPrintf(comm, fp, "\n");CHKERRQ(ierr);
}
if (numProcs > 1) {ierr = PetscMalloc1(maxCorners+maxCells, &remoteVertices);CHKERRQ(ierr);}
for (proc = 1; proc < numProcs; ++proc) {
MPI_Status status;
ierr = MPI_Recv(&numCorners, 1, MPIU_INT, proc, tag, comm, &status);CHKERRQ(ierr);
ierr = MPI_Recv(remoteVertices, numCorners, MPIU_INT, proc, tag, comm, &status);CHKERRQ(ierr);
for (c = 0; c < numCorners;) {
PetscInt nC = remoteVertices[c++];
for (v = 0; v < nC; ++v, ++c) {
vertices[v] = remoteVertices[c];
}
ierr = DMPlexInvertCell(dim, nC, vertices);CHKERRQ(ierr);
ierr = PetscFPrintf(comm, fp, "%d ", nC);CHKERRQ(ierr);
for (v = 0; v < nC; ++v) {
ierr = PetscFPrintf(comm, fp, " %d", vertices[v]);CHKERRQ(ierr);
}
ierr = PetscFPrintf(comm, fp, "\n");CHKERRQ(ierr);
}
//.........这里部分代码省略.........
示例8: MatISSetMPIXAIJPreallocation_Private
//.........这里部分代码省略.........
ierr = MatGetOwnershipRanges(A,(const PetscInt**)&mat_ranges);CHKERRQ(ierr);
for (i=0;i<nsubdomains;i++) {
for (j=mat_ranges[i];j<mat_ranges[i+1];j++) {
row_ownership[j] = i;
}
}
/*
my_dnz and my_onz contains exact contribution to preallocation from each local mat
then, they will be summed up properly. This way, preallocation is always sufficient
*/
ierr = PetscCalloc2(local_rows,&my_dnz,local_rows,&my_onz);CHKERRQ(ierr);
/* preallocation as a MATAIJ */
if (isdense) { /* special case for dense local matrices */
for (i=0;i<local_rows;i++) {
PetscInt index_row = global_indices_r[i];
for (j=i;j<local_rows;j++) {
PetscInt owner = row_ownership[index_row];
PetscInt index_col = global_indices_c[j];
if (index_col > mat_ranges[owner]-1 && index_col < mat_ranges[owner+1] ) { /* diag block */
my_dnz[i] += 1;
} else { /* offdiag block */
my_onz[i] += 1;
}
/* same as before, interchanging rows and cols */
if (i != j) {
owner = row_ownership[index_col];
if (index_row > mat_ranges[owner]-1 && index_row < mat_ranges[owner+1] ) {
my_dnz[j] += 1;
} else {
my_onz[j] += 1;
}
}
}
}
} else { /* TODO: this could be optimized using MatGetRowIJ */
for (i=0;i<local_rows;i++) {
const PetscInt *cols;
PetscInt ncols,index_row = global_indices_r[i];
ierr = MatGetRow(matis->A,i,&ncols,&cols,NULL);CHKERRQ(ierr);
for (j=0;j<ncols;j++) {
PetscInt owner = row_ownership[index_row];
PetscInt index_col = global_indices_c[cols[j]];
if (index_col > mat_ranges[owner]-1 && index_col < mat_ranges[owner+1] ) { /* diag block */
my_dnz[i] += 1;
} else { /* offdiag block */
my_onz[i] += 1;
}
/* same as before, interchanging rows and cols */
if (issbaij && index_col != index_row) {
owner = row_ownership[index_col];
if (index_row > mat_ranges[owner]-1 && index_row < mat_ranges[owner+1] ) {
my_dnz[cols[j]] += 1;
} else {
my_onz[cols[j]] += 1;
}
}
}
ierr = MatRestoreRow(matis->A,i,&ncols,&cols,NULL);CHKERRQ(ierr);
}
}
ierr = ISLocalToGlobalMappingRestoreIndices(A->rmap->mapping,&global_indices_r);CHKERRQ(ierr);
if (global_indices_c != global_indices_r) {
ierr = ISLocalToGlobalMappingRestoreIndices(A->rmap->mapping,&global_indices_c);CHKERRQ(ierr);
}
ierr = PetscFree(row_ownership);CHKERRQ(ierr);
/* Reduce my_dnz and my_onz */
if (maxreduce) {
ierr = PetscSFReduceBegin(matis->sf,MPIU_INT,my_dnz,dnz,MPI_MAX);CHKERRQ(ierr);
ierr = PetscSFReduceEnd(matis->sf,MPIU_INT,my_dnz,dnz,MPI_MAX);CHKERRQ(ierr);
ierr = PetscSFReduceBegin(matis->sf,MPIU_INT,my_onz,onz,MPI_MAX);CHKERRQ(ierr);
ierr = PetscSFReduceEnd(matis->sf,MPIU_INT,my_onz,onz,MPI_MAX);CHKERRQ(ierr);
} else {
ierr = PetscSFReduceBegin(matis->sf,MPIU_INT,my_dnz,dnz,MPI_SUM);CHKERRQ(ierr);
ierr = PetscSFReduceEnd(matis->sf,MPIU_INT,my_dnz,dnz,MPI_SUM);CHKERRQ(ierr);
ierr = PetscSFReduceBegin(matis->sf,MPIU_INT,my_onz,onz,MPI_SUM);CHKERRQ(ierr);
ierr = PetscSFReduceEnd(matis->sf,MPIU_INT,my_onz,onz,MPI_SUM);CHKERRQ(ierr);
}
ierr = PetscFree2(my_dnz,my_onz);CHKERRQ(ierr);
/* Resize preallocation if overestimated */
for (i=0;i<lrows;i++) {
dnz[i] = PetscMin(dnz[i],lcols);
onz[i] = PetscMin(onz[i],cols-lcols);
}
/* set preallocation */
ierr = MatMPIAIJSetPreallocation(B,0,dnz,0,onz);CHKERRQ(ierr);
for (i=0;i<lrows/bs;i++) {
dnz[i] = dnz[i*bs]/bs;
onz[i] = onz[i*bs]/bs;
}
ierr = MatMPIBAIJSetPreallocation(B,bs,0,dnz,0,onz);CHKERRQ(ierr);
ierr = MatMPISBAIJSetPreallocation(B,bs,0,dnz,0,onz);CHKERRQ(ierr);
ierr = MatPreallocateFinalize(dnz,onz);CHKERRQ(ierr);
if (issbaij) {
ierr = MatRestoreRowUpperTriangular(matis->A);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
示例9: PCTFS_ivec_min
/***********************************ivec.c*************************************/
PetscErrorCode PCTFS_ivec_min( PetscInt *arg1, PetscInt *arg2, PetscInt n)
{
PetscFunctionBegin;
while (n--) {*(arg1) = PetscMin(*arg1,*arg2); arg1++; arg2++;}
PetscFunctionReturn(0);
}
示例10: EPSSolve_Arnoldi
PetscErrorCode EPSSolve_Arnoldi(EPS eps)
{
PetscErrorCode ierr;
PetscInt k,nv,ld;
Mat U;
PetscScalar *H,*X;
PetscReal beta,gamma=1.0;
PetscBool breakdown,harmonic,refined;
BVOrthogRefineType orthog_ref;
EPS_ARNOLDI *arnoldi = (EPS_ARNOLDI*)eps->data;
PetscFunctionBegin;
ierr = DSGetLeadingDimension(eps->ds,&ld);CHKERRQ(ierr);
ierr = DSGetRefined(eps->ds,&refined);CHKERRQ(ierr);
harmonic = (eps->extraction==EPS_HARMONIC || eps->extraction==EPS_REFINED_HARMONIC)?PETSC_TRUE:PETSC_FALSE;
ierr = BVGetOrthogonalization(eps->V,NULL,&orthog_ref,NULL);CHKERRQ(ierr);
/* Get the starting Arnoldi vector */
ierr = EPSGetStartVector(eps,0,NULL);CHKERRQ(ierr);
/* Restart loop */
while (eps->reason == EPS_CONVERGED_ITERATING) {
eps->its++;
/* Compute an nv-step Arnoldi factorization */
nv = PetscMin(eps->nconv+eps->mpd,eps->ncv);
ierr = DSSetDimensions(eps->ds,nv,0,eps->nconv,0);CHKERRQ(ierr);
ierr = DSGetArray(eps->ds,DS_MAT_A,&H);CHKERRQ(ierr);
if (!arnoldi->delayed) {
ierr = EPSBasicArnoldi(eps,PETSC_FALSE,H,ld,eps->nconv,&nv,&beta,&breakdown);CHKERRQ(ierr);
} else SETERRQ(PetscObjectComm((PetscObject)eps),1,"Not implemented");
/*if (orthog_ref == BV_ORTHOG_REFINE_NEVER) {
ierr = EPSDelayedArnoldi1(eps,H,ld,eps->V,eps->nconv,&nv,f,&beta,&breakdown);CHKERRQ(ierr);
} else {
ierr = EPSDelayedArnoldi(eps,H,ld,eps->V,eps->nconv,&nv,f,&beta,&breakdown);CHKERRQ(ierr);
}*/
ierr = DSRestoreArray(eps->ds,DS_MAT_A,&H);CHKERRQ(ierr);
ierr = DSSetState(eps->ds,DS_STATE_INTERMEDIATE);CHKERRQ(ierr);
ierr = BVSetActiveColumns(eps->V,eps->nconv,nv);CHKERRQ(ierr);
/* Compute translation of Krylov decomposition if harmonic extraction used */
if (harmonic) {
ierr = DSTranslateHarmonic(eps->ds,eps->target,beta,PETSC_FALSE,NULL,&gamma);CHKERRQ(ierr);
}
/* Solve projected problem */
ierr = DSSolve(eps->ds,eps->eigr,eps->eigi);CHKERRQ(ierr);
ierr = DSSort(eps->ds,eps->eigr,eps->eigi,NULL,NULL,NULL);CHKERRQ(ierr);
ierr = DSUpdateExtraRow(eps->ds);CHKERRQ(ierr);
/* Check convergence */
ierr = EPSKrylovConvergence(eps,PETSC_FALSE,eps->nconv,nv-eps->nconv,beta,gamma,&k);CHKERRQ(ierr);
if (refined) {
ierr = DSGetArray(eps->ds,DS_MAT_X,&X);CHKERRQ(ierr);
ierr = BVMultColumn(eps->V,1.0,0.0,k,X+k*ld);CHKERRQ(ierr);
ierr = DSRestoreArray(eps->ds,DS_MAT_X,&X);CHKERRQ(ierr);
ierr = DSGetMat(eps->ds,DS_MAT_Q,&U);CHKERRQ(ierr);
ierr = BVMultInPlace(eps->V,U,eps->nconv,nv);CHKERRQ(ierr);
ierr = MatDestroy(&U);CHKERRQ(ierr);
ierr = BVOrthogonalizeColumn(eps->V,k,NULL,NULL,NULL);CHKERRQ(ierr);
} else {
ierr = DSGetMat(eps->ds,DS_MAT_Q,&U);CHKERRQ(ierr);
ierr = BVMultInPlace(eps->V,U,eps->nconv,nv);CHKERRQ(ierr);
ierr = MatDestroy(&U);CHKERRQ(ierr);
}
eps->nconv = k;
ierr = EPSMonitor(eps,eps->its,eps->nconv,eps->eigr,eps->eigi,eps->errest,nv);CHKERRQ(ierr);
if (breakdown && k<eps->nev) {
ierr = PetscInfo2(eps,"Breakdown in Arnoldi method (it=%D norm=%g)\n",eps->its,(double)beta);CHKERRQ(ierr);
ierr = EPSGetStartVector(eps,k,&breakdown);CHKERRQ(ierr);
if (breakdown) {
eps->reason = EPS_DIVERGED_BREAKDOWN;
ierr = PetscInfo(eps,"Unable to generate more start vectors\n");CHKERRQ(ierr);
}
}
if (eps->its >= eps->max_it) eps->reason = EPS_DIVERGED_ITS;
if (eps->nconv >= eps->nev) eps->reason = EPS_CONVERGED_TOL;
}
/* truncate Schur decomposition and change the state to raw so that
PSVectors() computes eigenvectors from scratch */
ierr = DSSetDimensions(eps->ds,eps->nconv,0,0,0);CHKERRQ(ierr);
ierr = DSSetState(eps->ds,DS_STATE_RAW);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例11: SNESDiffParameterCompute_More
PetscErrorCode SNESDiffParameterCompute_More(SNES snes,void *nePv,Vec x,Vec p,double *fnoise,double *hopt)
{
DIFFPAR_MORE *neP = (DIFFPAR_MORE*)nePv;
Vec w, xp, fvec; /* work vectors to use in computing h */
double zero = 0.0, hl, hu, h, fnoise_s, fder2_s;
PetscScalar alpha;
PetscScalar fval[7], tab[7][7], eps[7], f = -1;
double rerrf = -1., fder2;
PetscErrorCode ierr;
PetscInt iter, k, i, j, info;
PetscInt nf = 7; /* number of function evaluations */
PetscInt fcount;
MPI_Comm comm;
FILE *fp;
PetscBool noise_test = PETSC_FALSE;
PetscFunctionBegin;
ierr = PetscObjectGetComm((PetscObject)snes,&comm);CHKERRQ(ierr);
/* Call to SNESSetUp() just to set data structures in SNES context */
if (!snes->setupcalled) {ierr = SNESSetUp(snes);CHKERRQ(ierr);}
w = neP->workv[0];
xp = neP->workv[1];
fvec = neP->workv[2];
fp = neP->fp;
/* Initialize parameters */
hl = zero;
hu = zero;
h = neP->h_first_try;
fnoise_s = zero;
fder2_s = zero;
fcount = neP->function_count;
/* We have 5 tries to attempt to compute a good hopt value */
ierr = SNESGetIterationNumber(snes,&i);CHKERRQ(ierr);
ierr = PetscFPrintf(comm,fp,"\n ------- SNES iteration %D ---------\n",i);CHKERRQ(ierr);
for (iter=0; iter<5; iter++) {
neP->h_first_try = h;
/* Compute the nf function values needed to estimate the noise from
the difference table */
for (k=0; k<nf; k++) {
alpha = h * (k+1 - (nf+1)/2);
ierr = VecWAXPY(xp,alpha,p,x);CHKERRQ(ierr);
ierr = SNESComputeFunction(snes,xp,fvec);CHKERRQ(ierr);
neP->function_count++;
ierr = VecDot(fvec,w,&fval[k]);CHKERRQ(ierr);
}
f = fval[(nf+1)/2 - 1];
/* Construct the difference table */
for (i=0; i<nf; i++) tab[i][0] = fval[i];
for (j=0; j<6; j++) {
for (i=0; i<nf-j; i++) {
tab[i][j+1] = tab[i+1][j] - tab[i][j];
}
}
/* Print the difference table */
ierr = PetscFPrintf(comm,fp,"Difference Table: iter = %D\n",iter);CHKERRQ(ierr);
for (i=0; i<nf; i++) {
for (j=0; j<nf-i; j++) {
ierr = PetscFPrintf(comm,fp," %10.2e ",tab[i][j]);CHKERRQ(ierr);
}
ierr = PetscFPrintf(comm,fp,"\n");CHKERRQ(ierr);
}
/* Call the noise estimator */
ierr = SNESNoise_dnest_(&nf,fval,&h,fnoise,&fder2,hopt,&info,eps);CHKERRQ(ierr);
/* Output statements */
rerrf = *fnoise/PetscAbsScalar(f);
if (info == 1) {ierr = PetscFPrintf(comm,fp,"%s\n","Noise detected");CHKERRQ(ierr);}
if (info == 2) {ierr = PetscFPrintf(comm,fp,"%s\n","Noise not detected; h is too small");CHKERRQ(ierr);}
if (info == 3) {ierr = PetscFPrintf(comm,fp,"%s\n","Noise not detected; h is too large");CHKERRQ(ierr);}
if (info == 4) {ierr = PetscFPrintf(comm,fp,"%s\n","Noise detected, but unreliable hopt");CHKERRQ(ierr);}
ierr = PetscFPrintf(comm,fp,"Approximate epsfcn %g %g %g %g %g %g\n",(double)eps[0],(double)eps[1],(double)eps[2],(double)eps[3],(double)eps[4],(double)eps[5]);CHKERRQ(ierr);
ierr = PetscFPrintf(comm,fp,"h = %g, fnoise = %g, fder2 = %g, rerrf = %g, hopt = %g\n\n",(double)h, (double)*fnoise, (double)fder2, (double)rerrf, (double)*hopt);CHKERRQ(ierr);
/* Save fnoise and fder2. */
if (*fnoise) fnoise_s = *fnoise;
if (fder2) fder2_s = fder2;
/* Check for noise detection. */
if (fnoise_s && fder2_s) {
*fnoise = fnoise_s;
fder2 = fder2_s;
*hopt = 1.68*sqrt(*fnoise/PetscAbsScalar(fder2));
goto theend;
} else {
/* Update hl and hu, and determine new h */
if (info == 2 || info == 4) {
hl = h;
if (hu == zero) h = 100*h;
else h = PetscMin(100*h,0.1*hu);
} else if (info == 3) {
hu = h;
//.........这里部分代码省略.........
示例12: DMPlexVTKWriteSection_ASCII
PetscErrorCode DMPlexVTKWriteSection_ASCII(DM dm, PetscSection section, PetscSection globalSection, Vec v, FILE *fp, PetscInt enforceDof, PetscInt precision, PetscReal scale)
{
MPI_Comm comm;
const MPI_Datatype mpiType = MPIU_SCALAR;
PetscScalar *array;
PetscInt numDof = 0, maxDof;
PetscInt numLabelCells, cellHeight, cMax, cStart, cEnd, numLabelVertices, vMax, vStart, vEnd, pStart, pEnd, p;
PetscMPIInt numProcs, rank, proc, tag;
PetscBool hasLabel;
PetscErrorCode ierr;
PetscFunctionBegin;
ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
PetscValidHeaderSpecific(dm,DM_CLASSID,1);
PetscValidHeaderSpecific(v,VEC_CLASSID,4);
if (precision < 0) precision = 6;
ierr = PetscCommGetNewTag(comm, &tag);CHKERRQ(ierr);
ierr = MPI_Comm_size(comm, &numProcs);CHKERRQ(ierr);
ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
/* VTK only wants the values at cells or vertices */
ierr = DMPlexGetVTKCellHeight(dm, &cellHeight);CHKERRQ(ierr);
ierr = DMPlexGetHeightStratum(dm, cellHeight, &cStart, &cEnd);CHKERRQ(ierr);
ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
ierr = DMPlexGetHybridBounds(dm, &cMax, NULL, NULL, &vMax);CHKERRQ(ierr);
if (cMax >= 0) cEnd = PetscMin(cEnd, cMax);
if (vMax >= 0) vEnd = PetscMin(vEnd, vMax);
pStart = PetscMax(PetscMin(cStart, vStart), pStart);
pEnd = PetscMin(PetscMax(cEnd, vEnd), pEnd);
ierr = DMPlexGetStratumSize(dm, "vtk", 1, &numLabelCells);CHKERRQ(ierr);
ierr = DMPlexGetStratumSize(dm, "vtk", 2, &numLabelVertices);CHKERRQ(ierr);
hasLabel = numLabelCells > 0 || numLabelVertices > 0 ? PETSC_TRUE : PETSC_FALSE;
for (p = pStart; p < pEnd; ++p) {
/* Reject points not either cells or vertices */
if (((p < cStart) || (p >= cEnd)) && ((p < vStart) || (p >= vEnd))) continue;
if (hasLabel) {
PetscInt value;
if (((p >= cStart) && (p < cEnd) && numLabelCells) ||
((p >= vStart) && (p < vEnd) && numLabelVertices)) {
ierr = DMPlexGetLabelValue(dm, "vtk", p, &value);CHKERRQ(ierr);
if (value != 1) continue;
}
}
ierr = PetscSectionGetDof(section, p, &numDof);CHKERRQ(ierr);
if (numDof) break;
}
ierr = MPI_Allreduce(&numDof, &maxDof, 1, MPIU_INT, MPI_MAX, comm);CHKERRQ(ierr);
enforceDof = PetscMax(enforceDof, maxDof);
ierr = VecGetArray(v, &array);CHKERRQ(ierr);
if (!rank) {
char formatString[8];
ierr = PetscSNPrintf(formatString, 8, "%%.%de", precision);CHKERRQ(ierr);
for (p = pStart; p < pEnd; ++p) {
/* Here we lose a way to filter points by keeping them out of the Numbering */
PetscInt dof, off, goff, d;
/* Reject points not either cells or vertices */
if (((p < cStart) || (p >= cEnd)) && ((p < vStart) || (p >= vEnd))) continue;
if (hasLabel) {
PetscInt value;
if (((p >= cStart) && (p < cEnd) && numLabelCells) ||
((p >= vStart) && (p < vEnd) && numLabelVertices)) {
ierr = DMPlexGetLabelValue(dm, "vtk", p, &value);CHKERRQ(ierr);
if (value != 1) continue;
}
}
ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
ierr = PetscSectionGetOffset(section, p, &off);CHKERRQ(ierr);
ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
if (dof && goff >= 0) {
for (d = 0; d < dof; d++) {
if (d > 0) {
ierr = PetscFPrintf(comm, fp, " ");CHKERRQ(ierr);
}
ierr = PetscFPrintf(comm, fp, formatString, PetscRealPart(array[off+d])*scale);CHKERRQ(ierr);
}
for (d = dof; d < enforceDof; d++) {
ierr = PetscFPrintf(comm, fp, " 0.0");CHKERRQ(ierr);
}
ierr = PetscFPrintf(comm, fp, "\n");CHKERRQ(ierr);
}
}
for (proc = 1; proc < numProcs; ++proc) {
PetscScalar *remoteValues;
PetscInt size = 0, d;
MPI_Status status;
ierr = MPI_Recv(&size, 1, MPIU_INT, proc, tag, comm, &status);CHKERRQ(ierr);
ierr = PetscMalloc1(size, &remoteValues);CHKERRQ(ierr);
ierr = MPI_Recv(remoteValues, size, mpiType, proc, tag, comm, &status);CHKERRQ(ierr);
for (p = 0; p < size/maxDof; ++p) {
for (d = 0; d < maxDof; ++d) {
if (d > 0) {
ierr = PetscFPrintf(comm, fp, " ");CHKERRQ(ierr);
}
ierr = PetscFPrintf(comm, fp, formatString, PetscRealPart(remoteValues[p*maxDof+d])*scale);CHKERRQ(ierr);
}
//.........这里部分代码省略.........
示例13: DMPlexVTKWriteAll_ASCII
static PetscErrorCode DMPlexVTKWriteAll_ASCII(DM dm, PetscViewer viewer)
{
MPI_Comm comm;
PetscViewer_VTK *vtk = (PetscViewer_VTK*) viewer->data;
FILE *fp;
PetscViewerVTKObjectLink link;
PetscSection coordSection, globalCoordSection;
PetscLayout vLayout;
Vec coordinates;
PetscReal lengthScale;
PetscInt vMax, totVertices, totCells;
PetscBool hasPoint = PETSC_FALSE, hasCell = PETSC_FALSE, writePartition = PETSC_FALSE;
PetscErrorCode ierr;
PetscFunctionBegin;
ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
ierr = PetscFOpen(comm, vtk->filename, "wb", &fp);CHKERRQ(ierr);
ierr = PetscFPrintf(comm, fp, "# vtk DataFile Version 2.0\n");CHKERRQ(ierr);
ierr = PetscFPrintf(comm, fp, "Simplicial Mesh Example\n");CHKERRQ(ierr);
ierr = PetscFPrintf(comm, fp, "ASCII\n");CHKERRQ(ierr);
ierr = PetscFPrintf(comm, fp, "DATASET UNSTRUCTURED_GRID\n");CHKERRQ(ierr);
/* Vertices */
ierr = DMPlexGetScale(dm, PETSC_UNIT_LENGTH, &lengthScale);CHKERRQ(ierr);
ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
ierr = PetscSectionCreateGlobalSection(coordSection, dm->sf, PETSC_FALSE, PETSC_FALSE, &globalCoordSection);CHKERRQ(ierr);
ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
ierr = DMPlexGetHybridBounds(dm, NULL, NULL, NULL, &vMax);CHKERRQ(ierr);
if (vMax >= 0) {
PetscInt pStart, pEnd, p, localSize = 0;
ierr = PetscSectionGetChart(globalCoordSection, &pStart, &pEnd);CHKERRQ(ierr);
pEnd = PetscMin(pEnd, vMax);
for (p = pStart; p < pEnd; ++p) {
PetscInt dof;
ierr = PetscSectionGetDof(globalCoordSection, p, &dof);CHKERRQ(ierr);
if (dof > 0) ++localSize;
}
ierr = PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &vLayout);CHKERRQ(ierr);
ierr = PetscLayoutSetLocalSize(vLayout, localSize);CHKERRQ(ierr);
ierr = PetscLayoutSetBlockSize(vLayout, 1);CHKERRQ(ierr);
ierr = PetscLayoutSetUp(vLayout);CHKERRQ(ierr);
} else {
ierr = PetscSectionGetPointLayout(PetscObjectComm((PetscObject)dm), globalCoordSection, &vLayout);CHKERRQ(ierr);
}
ierr = PetscLayoutGetSize(vLayout, &totVertices);CHKERRQ(ierr);
ierr = PetscFPrintf(comm, fp, "POINTS %d double\n", totVertices);CHKERRQ(ierr);
ierr = DMPlexVTKWriteSection_ASCII(dm, coordSection, globalCoordSection, coordinates, fp, 3, PETSC_DETERMINE, lengthScale);CHKERRQ(ierr);
/* Cells */
ierr = DMPlexVTKWriteCells_ASCII(dm, fp, &totCells);CHKERRQ(ierr);
/* Vertex fields */
for (link = vtk->link; link; link = link->next) {
if ((link->ft == PETSC_VTK_POINT_FIELD) || (link->ft == PETSC_VTK_POINT_VECTOR_FIELD)) hasPoint = PETSC_TRUE;
if ((link->ft == PETSC_VTK_CELL_FIELD) || (link->ft == PETSC_VTK_CELL_VECTOR_FIELD)) hasCell = PETSC_TRUE;
}
if (hasPoint) {
ierr = PetscFPrintf(comm, fp, "POINT_DATA %d\n", totVertices);CHKERRQ(ierr);
for (link = vtk->link; link; link = link->next) {
Vec X = (Vec) link->vec;
DM dmX;
PetscSection section, globalSection, newSection = NULL;
const char *name;
PetscInt enforceDof = PETSC_DETERMINE;
if ((link->ft != PETSC_VTK_POINT_FIELD) && (link->ft != PETSC_VTK_POINT_VECTOR_FIELD)) continue;
if (link->ft == PETSC_VTK_POINT_VECTOR_FIELD) enforceDof = 3;
ierr = PetscObjectGetName(link->vec, &name);CHKERRQ(ierr);
ierr = VecGetDM(X, &dmX);CHKERRQ(ierr);
if (dmX) {
DMLabel subpointMap, subpointMapX;
PetscInt dim, dimX, pStart, pEnd, qStart, qEnd;
ierr = DMGetDefaultSection(dmX, §ion);CHKERRQ(ierr);
/* Here is where we check whether dmX is a submesh of dm */
ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
ierr = DMGetDimension(dmX, &dimX);CHKERRQ(ierr);
ierr = DMPlexGetChart(dm, &pStart, &pEnd);CHKERRQ(ierr);
ierr = DMPlexGetChart(dmX, &qStart, &qEnd);CHKERRQ(ierr);
ierr = DMPlexGetSubpointMap(dm, &subpointMap);CHKERRQ(ierr);
ierr = DMPlexGetSubpointMap(dmX, &subpointMapX);CHKERRQ(ierr);
if (((dim != dimX) || ((pEnd-pStart) < (qEnd-qStart))) && subpointMap && !subpointMapX) {
const PetscInt *ind = NULL;
IS subpointIS;
PetscInt n = 0, q;
ierr = PetscSectionGetChart(section, &qStart, &qEnd);CHKERRQ(ierr);
ierr = DMPlexCreateSubpointIS(dm, &subpointIS);CHKERRQ(ierr);
if (subpointIS) {
ierr = ISGetLocalSize(subpointIS, &n);CHKERRQ(ierr);
ierr = ISGetIndices(subpointIS, &ind);CHKERRQ(ierr);
}
ierr = PetscSectionCreate(comm, &newSection);CHKERRQ(ierr);
ierr = PetscSectionSetChart(newSection, pStart, pEnd);CHKERRQ(ierr);
for (q = qStart; q < qEnd; ++q) {
PetscInt dof, off, p;
ierr = PetscSectionGetDof(section, q, &dof);CHKERRQ(ierr);
if (dof) {
ierr = PetscFindInt(q, n, ind, &p);CHKERRQ(ierr);
if (p >= pStart) {
//.........这里部分代码省略.........
示例14: graph
/*
PCGAMGCreateGraph - create simple scaled scalar graph from matrix
Input Parameter:
. Amat - matrix
Output Parameter:
. a_Gmaat - eoutput scalar graph (symmetric?)
*/
PetscErrorCode PCGAMGCreateGraph(Mat Amat, Mat *a_Gmat)
{
PetscErrorCode ierr;
PetscInt Istart,Iend,Ii,jj,kk,ncols,nloc,NN,MM,bs;
MPI_Comm comm;
Mat Gmat;
MatType mtype;
PetscFunctionBegin;
ierr = PetscObjectGetComm((PetscObject)Amat,&comm);CHKERRQ(ierr);
ierr = MatGetOwnershipRange(Amat, &Istart, &Iend);CHKERRQ(ierr);
ierr = MatGetSize(Amat, &MM, &NN);CHKERRQ(ierr);
ierr = MatGetBlockSize(Amat, &bs);CHKERRQ(ierr);
nloc = (Iend-Istart)/bs;
#if defined PETSC_GAMG_USE_LOG
ierr = PetscLogEventBegin(petsc_gamg_setup_events[GRAPH],0,0,0,0);CHKERRQ(ierr);
#endif
if (bs > 1) {
const PetscScalar *vals;
const PetscInt *idx;
PetscInt *d_nnz, *o_nnz,*w0,*w1,*w2;
PetscBool ismpiaij,isseqaij;
/*
Determine the preallocation needed for the scalar matrix derived from the vector matrix.
*/
ierr = PetscObjectBaseTypeCompare((PetscObject)Amat,MATSEQAIJ,&isseqaij);CHKERRQ(ierr);
ierr = PetscObjectBaseTypeCompare((PetscObject)Amat,MATMPIAIJ,&ismpiaij);CHKERRQ(ierr);
ierr = PetscMalloc2(nloc, &d_nnz,isseqaij ? 0 : nloc, &o_nnz);CHKERRQ(ierr);
if (isseqaij) {
PetscInt max_d_nnz;
/*
Determine exact preallocation count for (sequential) scalar matrix
*/
ierr = MatSeqAIJGetMaxRowNonzeros(Amat,&max_d_nnz);CHKERRQ(ierr);
max_d_nnz = PetscMin(nloc,bs*max_d_nnz);CHKERRQ(ierr);
ierr = PetscMalloc3(max_d_nnz, &w0,max_d_nnz, &w1,max_d_nnz, &w2);CHKERRQ(ierr);
for (Ii = 0, jj = 0; Ii < Iend; Ii += bs, jj++) {
ierr = MatCollapseRows(Amat,Ii,bs,w0,w1,w2,&d_nnz[jj],NULL);CHKERRQ(ierr);
}
ierr = PetscFree3(w0,w1,w2);CHKERRQ(ierr);
} else if (ismpiaij) {
Mat Daij,Oaij;
const PetscInt *garray;
PetscInt max_d_nnz;
ierr = MatMPIAIJGetSeqAIJ(Amat,&Daij,&Oaij,&garray);CHKERRQ(ierr);
/*
Determine exact preallocation count for diagonal block portion of scalar matrix
*/
ierr = MatSeqAIJGetMaxRowNonzeros(Daij,&max_d_nnz);CHKERRQ(ierr);
max_d_nnz = PetscMin(nloc,bs*max_d_nnz);CHKERRQ(ierr);
ierr = PetscMalloc3(max_d_nnz, &w0,max_d_nnz, &w1,max_d_nnz, &w2);CHKERRQ(ierr);
for (Ii = 0, jj = 0; Ii < Iend - Istart; Ii += bs, jj++) {
ierr = MatCollapseRows(Daij,Ii,bs,w0,w1,w2,&d_nnz[jj],NULL);CHKERRQ(ierr);
}
ierr = PetscFree3(w0,w1,w2);CHKERRQ(ierr);
/*
Over estimate (usually grossly over), preallocation count for off-diagonal portion of scalar matrix
*/
for (Ii = 0, jj = 0; Ii < Iend - Istart; Ii += bs, jj++) {
o_nnz[jj] = 0;
for (kk=0; kk<bs; kk++) { /* rows that get collapsed to a single row */
ierr = MatGetRow(Oaij,Ii+kk,&ncols,0,0);CHKERRQ(ierr);
o_nnz[jj] += ncols;
ierr = MatRestoreRow(Oaij,Ii+kk,&ncols,0,0);CHKERRQ(ierr);
}
if (o_nnz[jj] > (NN/bs-nloc)) o_nnz[jj] = NN/bs-nloc;
}
} else SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"Require AIJ matrix type");
/* get scalar copy (norms) of matrix */
ierr = MatGetType(Amat,&mtype);CHKERRQ(ierr);
ierr = MatCreate(comm, &Gmat);CHKERRQ(ierr);
ierr = MatSetSizes(Gmat,nloc,nloc,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
ierr = MatSetBlockSizes(Gmat, 1, 1);CHKERRQ(ierr);
ierr = MatSetType(Gmat, mtype);CHKERRQ(ierr);
ierr = MatSeqAIJSetPreallocation(Gmat,0,d_nnz);CHKERRQ(ierr);
ierr = MatMPIAIJSetPreallocation(Gmat,0,d_nnz,0,o_nnz);CHKERRQ(ierr);
ierr = PetscFree2(d_nnz,o_nnz);CHKERRQ(ierr);
for (Ii = Istart; Ii < Iend; Ii++) {
PetscInt dest_row = Ii/bs;
//.........这里部分代码省略.........
示例15: TestCellShape
static PetscErrorCode TestCellShape(DM dm)
{
PetscMPIInt rank;
PetscInt dim, c, cStart, cEnd, count = 0;
ex1_stats_t stats, globalStats;
PetscReal *J, *invJ, min = 0, max = 0, mean = 0, stdev = 0;
MPI_Comm comm = PetscObjectComm((PetscObject)dm);
DM dmCoarse;
PetscErrorCode ierr;
PetscFunctionBegin;
stats.min = PETSC_MAX_REAL;
stats.max = PETSC_MIN_REAL;
stats.sum = stats.squaresum = 0.;
stats.count = 0;
ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
ierr = PetscMalloc2(dim * dim, &J, dim * dim, &invJ);CHKERRQ(ierr);
ierr = DMPlexGetHeightStratum(dm,0,&cStart,&cEnd);CHKERRQ(ierr);
for (c = cStart; c < cEnd; c++) {
PetscInt i;
PetscReal frobJ = 0., frobInvJ = 0., cond2, cond, detJ;
ierr = DMPlexComputeCellGeometryAffineFEM(dm,c,NULL,J,invJ,&detJ);CHKERRQ(ierr);
for (i = 0; i < dim * dim; i++) {
frobJ += J[i] * J[i];
frobInvJ += invJ[i] * invJ[i];
}
cond2 = frobJ * frobInvJ;
cond = PetscSqrtReal(cond2);
stats.min = PetscMin(stats.min,cond);
stats.max = PetscMax(stats.max,cond);
stats.sum += cond;
stats.squaresum += cond2;
stats.count++;
}
{
PetscMPIInt blockLengths[2] = {4,1};
MPI_Aint blockOffsets[2] = {offsetof(ex1_stats_t,min),offsetof(ex1_stats_t,count)};
MPI_Datatype blockTypes[2] = {MPIU_REAL,MPIU_INT}, statType;
MPI_Op statReduce;
ierr = MPI_Type_create_struct(2,blockLengths,blockOffsets,blockTypes,&statType);CHKERRQ(ierr);
ierr = MPI_Type_commit(&statType);CHKERRQ(ierr);
ierr = MPI_Op_create(ex1_stats_reduce, PETSC_TRUE, &statReduce);CHKERRQ(ierr);
ierr = MPI_Reduce(&stats,&globalStats,1,statType,statReduce,0,comm);CHKERRQ(ierr);
ierr = MPI_Op_free(&statReduce);CHKERRQ(ierr);
ierr = MPI_Type_free(&statType);CHKERRQ(ierr);
}
ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
if (!rank) {
count = globalStats.count;
min = globalStats.min;
max = globalStats.max;
mean = globalStats.sum / globalStats.count;
stdev = PetscSqrtReal(globalStats.squaresum / globalStats.count - mean * mean);
}
ierr = PetscPrintf(comm,"Mesh with %d cells, shape condition numbers: min = %g, max = %g, mean = %g, stddev = %g\n", count, (double) min, (double) max, (double) mean, (double) stdev);
ierr = PetscFree2(J,invJ);CHKERRQ(ierr);
ierr = DMGetCoarseDM(dm,&dmCoarse);CHKERRQ(ierr);
if (dmCoarse) {
ierr = TestCellShape(dmCoarse);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}