PetscErrorCode StokesCalcResidual(Stokes *s) { PetscReal val; Vec b0, b1; PetscErrorCode ierr; PetscFunctionBeginUser; /* residual Ax-b (*warning* overwrites b) */ ierr = VecScale(s->b, -1.0);CHKERRQ(ierr); ierr = MatMultAdd(s->A, s->x, s->b, s->b);CHKERRQ(ierr); /* ierr = VecView(s->b, (PetscViewer)PETSC_VIEWER_DEFAULT);CHKERRQ(ierr); */ /* residual velocity */ ierr = VecGetSubVector(s->b, s->isg[0], &b0);CHKERRQ(ierr); ierr = VecNorm(b0, NORM_2, &val);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," residual u = %g\n",(double)val);CHKERRQ(ierr); ierr = VecRestoreSubVector(s->b, s->isg[0], &b0);CHKERRQ(ierr); /* residual pressure */ ierr = VecGetSubVector(s->b, s->isg[1], &b1);CHKERRQ(ierr); ierr = VecNorm(b1, NORM_2, &val);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," residual p = %g\n",(double)val);CHKERRQ(ierr); ierr = VecRestoreSubVector(s->b, s->isg[1], &b1);CHKERRQ(ierr); /* total residual */ ierr = VecNorm(s->b, NORM_2, &val);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," residual [u,p] = %g\n", (double)val);CHKERRQ(ierr); PetscFunctionReturn(0); }
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); }
PetscErrorCode StokesRhs(Stokes *s) { PetscInt row, start, end, i, j; PetscScalar val; Vec b0,b1; PetscErrorCode ierr; PetscFunctionBeginUser; /* velocity part */ ierr = VecGetSubVector(s->b, s->isg[0], &b0);CHKERRQ(ierr); ierr = VecGetOwnershipRange(b0, &start, &end);CHKERRQ(ierr); for (row = start; row < end; row++) { ierr = StokesGetPosition(s, row, &i, &j);CHKERRQ(ierr); if (row < s->nx*s->ny) { ierr = StokesRhsMomX(s, i, j, &val);CHKERRQ(ierr); } else if (row < 2*s->nx*s->ny) { ierr = StokesRhsMomY(s, i, j, &val);CHKERRQ(ierr); } ierr = VecSetValue(b0, row, val, INSERT_VALUES);CHKERRQ(ierr); } ierr = VecRestoreSubVector(s->b, s->isg[0], &b0);CHKERRQ(ierr); /* pressure part */ ierr = VecGetSubVector(s->b, s->isg[1], &b1);CHKERRQ(ierr); ierr = VecGetOwnershipRange(b1, &start, &end);CHKERRQ(ierr); for (row = start; row < end; row++) { ierr = StokesGetPosition(s, row, &i, &j);CHKERRQ(ierr); ierr = StokesRhsMass(s, i, j, &val);CHKERRQ(ierr); ierr = VecSetValue(b1, row, val, INSERT_VALUES);CHKERRQ(ierr); } ierr = VecRestoreSubVector(s->b, s->isg[1], &b1);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode StokesExactSolution(Stokes *s) { PetscInt row, start, end, i, j; PetscScalar val; Vec y0,y1; PetscErrorCode ierr; PetscFunctionBeginUser; /* velocity part */ ierr = VecGetSubVector(s->y, s->isg[0], &y0);CHKERRQ(ierr); ierr = VecGetOwnershipRange(y0, &start, &end);CHKERRQ(ierr); for (row = start; row < end; row++) { ierr = StokesGetPosition(s, row,&i,&j);CHKERRQ(ierr); if (row < s->nx*s->ny) { val = StokesExactVelocityX(j*s->hy+s->hy/2); } else { val = 0; } ierr = VecSetValue(y0, row, val, INSERT_VALUES);CHKERRQ(ierr); } ierr = VecRestoreSubVector(s->y, s->isg[0], &y0);CHKERRQ(ierr); /* pressure part */ ierr = VecGetSubVector(s->y, s->isg[1], &y1);CHKERRQ(ierr); ierr = VecGetOwnershipRange(y1, &start, &end);CHKERRQ(ierr); for (row = start; row < end; row++) { ierr = StokesGetPosition(s, row, &i, &j);CHKERRQ(ierr); val = StokesExactPressure(i*s->hx+s->hx/2); ierr = VecSetValue(y1, row, val, INSERT_VALUES);CHKERRQ(ierr); } ierr = VecRestoreSubVector(s->y, s->isg[1], &y1);CHKERRQ(ierr); PetscFunctionReturn(0); }
int main(int argc, char *argv[]) { MPI_Comm comm; Vec X,Y,Z; PetscMPIInt rank,size; PetscInt i,rstart,rend; PetscScalar *x; PetscViewer viewer; IS is0,is1; PetscErrorCode ierr; ierr = PetscInitialize(&argc,&argv,0,help);if (ierr) return ierr; comm = PETSC_COMM_WORLD; viewer = PETSC_VIEWER_STDOUT_WORLD; ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); ierr = VecCreate(comm,&X);CHKERRQ(ierr); ierr = VecSetSizes(X,10,PETSC_DETERMINE);CHKERRQ(ierr); ierr = VecSetFromOptions(X);CHKERRQ(ierr); ierr = VecGetOwnershipRange(X,&rstart,&rend);CHKERRQ(ierr); ierr = VecGetArray(X,&x);CHKERRQ(ierr); for (i=0; i<rend-rstart; i++) x[i] = rstart+i; ierr = VecRestoreArray(X,&x);CHKERRQ(ierr); ierr = ISCreateStride(comm,(rend-rstart)/3+3*(rank>size/2),rstart,1,&is0);CHKERRQ(ierr); ierr = ISComplement(is0,rstart,rend,&is1);CHKERRQ(ierr); ierr = ISView(is0,viewer);CHKERRQ(ierr); ierr = ISView(is1,viewer);CHKERRQ(ierr); ierr = VecGetSubVector(X,is0,&Y);CHKERRQ(ierr); ierr = VecGetSubVector(X,is1,&Z);CHKERRQ(ierr); ierr = VecView(Y,viewer);CHKERRQ(ierr); ierr = VecView(Z,viewer);CHKERRQ(ierr); ierr = VecRestoreSubVector(X,is0,&Y);CHKERRQ(ierr); ierr = VecRestoreSubVector(X,is1,&Z);CHKERRQ(ierr); ierr = ISDestroy(&is0);CHKERRQ(ierr); ierr = ISDestroy(&is1);CHKERRQ(ierr); ierr = VecDestroy(&X);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
PetscErrorCode PetscSectionRestoreField_Internal(PetscSection section, PetscSection sectionGlobal, Vec v, PetscInt field, PetscInt pStart, PetscInt pEnd, IS *is, Vec *subv) { PetscErrorCode ierr; PetscFunctionBegin; ierr = VecRestoreSubVector(v, *is, subv);CHKERRQ(ierr); ierr = ISDestroy(is);CHKERRQ(ierr); PetscFunctionReturn(0); }
// Monitor a sub-region of the full-domain PETSc Vec object. PetscErrorCode ProbeVolume::monitorVec(const DM &da, const Vec &fvec, const PetscInt &n, const PetscReal &t) { PetscErrorCode ierr; PetscFunctionBeginUser; // grab the part of the vector that corresponds to the sub-volume Vec svec; ierr = VecGetSubVector(fvec, is, &svec); CHKERRQ(ierr); if (n_sum != 0) // we accumulate the data over the time-steps { if (dvec == PETSC_NULL) { ierr = VecDuplicate(svec, &dvec); CHKERRQ(ierr); ierr = VecSet(dvec, 0.0); CHKERRQ(ierr); } ierr = VecAXPY(dvec, 1.0, svec); CHKERRQ(ierr); count++; if (count % n_sum == 0) // output the time-averaged data { ierr = VecScale(dvec, 1.0 / count); CHKERRQ(ierr); ierr = writeVec(dvec, t); CHKERRQ(ierr); // reset for the next accumulation cycle ierr = VecSet(dvec, 0.0); CHKERRQ(ierr); count = 0; } } else // output the time-step sub-volume data to file { ierr = writeVec(svec, t); CHKERRQ(ierr); } ierr = VecRestoreSubVector(fvec, is, &svec); CHKERRQ(ierr); PetscFunctionReturn(0); } // ProbeVolume::monitorVec