static PetscErrorCode CreateSpectralPlanes(DM dm, PetscInt numPlanes, const PetscInt planeDir[], const PetscReal planeCoord[], AppCtx *user) { PetscSection coordSection; Vec coordinates; const PetscScalar *coords; PetscInt dim, p, vStart, vEnd, v; PetscErrorCode ierr; PetscFunctionBeginUser; ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr); ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr); ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr); ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr); ierr = VecGetArrayRead(coordinates, &coords);CHKERRQ(ierr); for (p = 0; p < numPlanes; ++p) { DMLabel label; char name[PETSC_MAX_PATH_LEN]; ierr = PetscSNPrintf(name, PETSC_MAX_PATH_LEN, "spectral_plane_%D", p);CHKERRQ(ierr); ierr = DMCreateLabel(dm, name);CHKERRQ(ierr); ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr); ierr = DMLabelAddStratum(label, 1);CHKERRQ(ierr); for (v = vStart; v < vEnd; ++v) { PetscInt off; ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr); if (PetscAbsReal(planeCoord[p] - PetscRealPart(coords[off+planeDir[p]])) < PETSC_SMALL) { ierr = DMLabelSetValue(label, v, 1);CHKERRQ(ierr); } } } ierr = VecRestoreArrayRead(coordinates, &coords);CHKERRQ(ierr); PetscFunctionReturn(0); }
static PetscErrorCode RHSJacobian(TS ts,PetscReal t,Vec X,Mat A,Mat B,void *ctx) { PetscErrorCode ierr; User user = (User)ctx; PetscReal mu = user->mu; PetscInt rowcol[] = {0,1}; PetscScalar J[2][2]; const PetscScalar *x; PetscFunctionBeginUser; ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr); J[0][0] = 0; J[1][0] = -2.*mu*x[1]*x[0]-1; J[0][1] = 1.0; J[1][1] = mu*(1.0-x[0]*x[0]); ierr = MatSetValues(A,2,rowcol,2,rowcol,&J[0][0],INSERT_VALUES);CHKERRQ(ierr); ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); if (A != B) { ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); } ierr = VecRestoreArrayRead(X,&x);CHKERRQ(ierr); PetscFunctionReturn(0); }
static PetscErrorCode PCApply_Redistribute(PC pc,Vec b,Vec x) { PC_Redistribute *red = (PC_Redistribute*)pc->data; PetscErrorCode ierr; PetscInt dcnt = red->dcnt,i; const PetscInt *drows = red->drows; PetscScalar *xwork; const PetscScalar *bwork,*diag = red->diag; PetscFunctionBegin; if (!red->work) { ierr = VecDuplicate(b,&red->work);CHKERRQ(ierr); } /* compute the rows of solution that have diagonal entries only */ ierr = VecSet(x,0.0);CHKERRQ(ierr); /* x = diag(A)^{-1} b */ ierr = VecGetArray(x,&xwork);CHKERRQ(ierr); ierr = VecGetArrayRead(b,&bwork);CHKERRQ(ierr); for (i=0; i<dcnt; i++) { xwork[drows[i]] = diag[i]*bwork[drows[i]]; } ierr = PetscLogFlops(dcnt);CHKERRQ(ierr); ierr = VecRestoreArray(red->work,&xwork);CHKERRQ(ierr); ierr = VecRestoreArrayRead(b,&bwork);CHKERRQ(ierr); /* update the right hand side for the reduced system with diagonal rows (and corresponding columns) removed */ ierr = MatMult(pc->pmat,x,red->work);CHKERRQ(ierr); ierr = VecAYPX(red->work,-1.0,b);CHKERRQ(ierr); /* red->work = b - A x */ ierr = VecScatterBegin(red->scatter,red->work,red->b,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); ierr = VecScatterEnd(red->scatter,red->work,red->b,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); ierr = KSPSolve(red->ksp,red->b,red->x);CHKERRQ(ierr); ierr = VecScatterBegin(red->scatter,red->x,x,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); ierr = VecScatterEnd(red->scatter,red->x,x,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode StokesWriteSolution(Stokes *s) { PetscMPIInt size; PetscInt n,i,j; const PetscScalar *array; PetscErrorCode ierr; PetscFunctionBeginUser; /* write data (*warning* only works sequential) */ MPI_Comm_size(MPI_COMM_WORLD,&size); /*ierr = PetscPrintf(PETSC_COMM_WORLD," number of processors = %D\n",size);CHKERRQ(ierr);*/ if (size == 1) { PetscViewer viewer; ierr = VecGetArrayRead(s->x, &array);CHKERRQ(ierr); ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD, "solution.dat", &viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer, "# x, y, u, v, p\n");CHKERRQ(ierr); for (j = 0; j < s->ny; j++) { for (i = 0; i < s->nx; i++) { n = j*s->nx+i; ierr = PetscViewerASCIIPrintf(viewer, "%.12g %.12g %.12g %.12g %.12g\n", (double)(i*s->hx+s->hx/2),(double)(j*s->hy+s->hy/2), (double)PetscRealPart(array[n]), (double)PetscRealPart(array[n+s->nx*s->ny]),(double)PetscRealPart(array[n+2*s->nx*s->ny]));CHKERRQ(ierr); } } ierr = VecRestoreArrayRead(s->x, &array);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } PetscFunctionReturn(0); }
PetscErrorCode MatSolve_LUSOL(Mat A,Vec b,Vec x) { Mat_LUSOL *lusol=(Mat_LUSOL*)A->spptr; double *xx; const double *bb; int mode=5; PetscErrorCode ierr; int i,m,n,nnz,status; PetscFunctionBegin; ierr = VecGetArray(x, &xx);CHKERRQ(ierr); ierr = VecGetArrayRead(b, &bb);CHKERRQ(ierr); m = n = lusol->n; nnz = lusol->nnz; for (i = 0; i < m; i++) lusol->mnsv[i] = bb[i]; LU6SOL(&mode, &m, &n, lusol->mnsv, xx, &nnz, lusol->luparm, lusol->parmlu, lusol->data, lusol->indc, lusol->indr, lusol->ip, lusol->iq, lusol->lenc, lusol->lenr, lusol->locc, lusol->locr, &status); if (status) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"solve failed, error code %d",status); ierr = VecRestoreArray(x, &xx);CHKERRQ(ierr); ierr = VecRestoreArrayRead(b, &bb);CHKERRQ(ierr); PetscFunctionReturn(0); }
static PetscErrorCode CostIntegrand(TS ts,PetscReal t,Vec U,Vec R,Userctx *user) { PetscErrorCode ierr; PetscScalar *r; const PetscScalar *u; PetscInt idx; Vec Xgen,Xnet; PetscScalar *xgen; PetscInt i; PetscFunctionBegin; ierr = DMCompositeGetLocalVectors(user->dmpgrid,&Xgen,&Xnet);CHKERRQ(ierr); ierr = DMCompositeScatter(user->dmpgrid,U,Xgen,Xnet);CHKERRQ(ierr); ierr = VecGetArray(Xgen,&xgen);CHKERRQ(ierr); ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); ierr = VecGetArray(R,&r);CHKERRQ(ierr); r[0] = 0.; idx = 0; for (i=0;i<ngen;i++) { r[0] += PetscPowScalarInt(PetscMax(0.,PetscMax(xgen[idx+3]/(2.*PETSC_PI)-user->freq_u,user->freq_l-xgen[idx+3]/(2.*PETSC_PI))),user->pow); idx += 9; } ierr = VecRestoreArray(R,&r);CHKERRQ(ierr); ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); ierr = DMCompositeRestoreLocalVectors(user->dmpgrid,&Xgen,&Xnet);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode VecMax_Seq(Vec xin,PetscInt* idx,PetscReal * z) { PetscInt i,j=0,n = xin->map->n; PetscReal max,tmp; const PetscScalar *xx; PetscErrorCode ierr; PetscFunctionBegin; ierr = VecGetArrayRead(xin,&xx);CHKERRQ(ierr); if (!n) { max = PETSC_MIN_REAL; j = -1; } else { #if defined(PETSC_USE_COMPLEX) max = PetscRealPart(*xx++); j = 0; #else max = *xx++; j = 0; #endif for (i=1; i<n; i++) { #if defined(PETSC_USE_COMPLEX) if ((tmp = PetscRealPart(*xx++)) > max) { j = i; max = tmp;} #else if ((tmp = *xx++) > max) { j = i; max = tmp; } #endif } } *z = max; if (idx) *idx = j; ierr = VecRestoreArrayRead(xin,&xx);CHKERRQ(ierr); PetscFunctionReturn(0); }
void SetJacobian(Geometry geo, Mat J, Vec v, int jc, int jr, int jh){ // use jc = -1 for last 2 columns, and jc = -2 for blocks (all jc) AssembleVec(v); int ns, ne, i; VecGetOwnershipRange(v, &ns, &ne); const double *vals; VecGetArrayRead(v, &vals); for(i=ns; i<ne; i++){ if( Last2(geo, i) || vals[i-ns] == 0.0) continue; int col, offset = jh*(Nxyzcr(geo)+2), ij = i%(Nxyzcr(geo)+2); Point p; CreatePoint_i(&p, ij, &geo->gN); if(jc == -1) //columns col = Nxyzcr(geo)+jr; else if(jc == -2) //blocks col = jr*Nxyzc(geo) + xyzc(&p); else // tensors col = jr*Nxyzc(geo) + jc*Nxyz(geo) + xyz(&p); MatSetValue(J, i, col+offset, vals[i-ns], ADD_VALUES); } VecRestoreArrayRead(v, &vals); }
void CollectVec(Geometry geo, Vec vN, Vec vM){ VecSet(vM, 0.0); Vecfun f; CreateVecfun(&f, geo->vf); const double *vals; VecGetArrayRead(vN, &vals); int ns, ne; VecGetOwnershipRange(vN, &ns, &ne); if( ne > Nxyzcr(geo)-2) ne = Nxyzcr(geo)-2; int i; for(i=ns; i<ne; i++){ if( valr(&f, i) == 0.0) continue; // skip if gain profile zero here Point p; CreatePoint_i(&p, i, &geo->gN); if( projectmedium(&p, &geo->gM, geo->LowerPML) ) VecSetValue(vM, xyz(&p), vals[i-ns], ADD_VALUES); } VecRestoreArrayRead(vN, &vals); DestroyVecfun(&f); AssembleVec(vM); }
PetscErrorCode MatBackwardSolve_SeqBAIJ_1_NaturalOrdering(Mat A,Vec bb,Vec xx) { Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data; PetscErrorCode ierr; const PetscInt n = a->mbs,*aj = a->j,*adiag = a->diag,*vi; PetscScalar *x,sum; const PetscScalar *b; const MatScalar *aa = a->a,*v; PetscInt i,nz; PetscFunctionBegin; if (!n) PetscFunctionReturn(0); ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr); ierr = VecGetArray(xx,&x);CHKERRQ(ierr); /* backward solve the upper triangular */ for (i=n-1; i>=0; i--) { v = aa + adiag[i+1] + 1; vi = aj + adiag[i+1] + 1; nz = adiag[i] - adiag[i+1]-1; sum = b[i]; PetscSparseDenseMinusDot(sum,x,v,vi,nz); x[i] = sum*v[nz]; /* x[i]=aa[adiag[i]]*sum; v++; */ } ierr = PetscLogFlops(2.0*a->nz - A->cmap->n);CHKERRQ(ierr); ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr); ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); PetscFunctionReturn(0); }
static PetscErrorCode IJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal a,Mat A,Mat B,void *ptr) { PetscErrorCode ierr; User user = (User)ptr; PetscReal mu = user->mu; PetscInt rowcol[] = {0,1}; PetscScalar J[2][2]; const PetscScalar *x; PetscFunctionBegin; ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr); J[0][0] = a; J[0][1] = 0; J[1][0] = (2.*x[0]*x[1]+1.)/mu; J[1][1] = a - (1. - x[0]*x[0])/mu; ierr = MatSetValues(B,2,rowcol,2,rowcol,&J[0][0],INSERT_VALUES);CHKERRQ(ierr); ierr = VecRestoreArrayRead(X,&x);CHKERRQ(ierr); ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); if (A != B) { ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); } PetscFunctionReturn(0); }
PetscErrorCode MatForwardSolve_SeqBAIJ_1_NaturalOrdering(Mat A,Vec bb,Vec xx) { Mat_SeqBAIJ *a = (Mat_SeqBAIJ*)A->data; PetscErrorCode ierr; const PetscInt n = a->mbs,*ai = a->i,*aj = a->j,*vi; PetscScalar *x,sum; const PetscScalar *b; const MatScalar *aa = a->a,*v; PetscInt i,nz; PetscFunctionBegin; if (!n) PetscFunctionReturn(0); ierr = VecGetArrayRead(bb,&b);CHKERRQ(ierr); ierr = VecGetArray(xx,&x);CHKERRQ(ierr); /* forward solve the lower triangular */ x[0] = b[0]; v = aa; vi = aj; for (i=1; i<n; i++) { nz = ai[i+1] - ai[i]; sum = b[i]; PetscSparseDenseMinusDot(sum,x,v,vi,nz); v += nz; vi += nz; x[i] = sum; } ierr = PetscLogFlops(a->nz - A->cmap->n);CHKERRQ(ierr); ierr = VecRestoreArrayRead(bb,&b);CHKERRQ(ierr); ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr); PetscFunctionReturn(0); }
static PetscErrorCode FormIJacobian(TS ts,PetscReal t,Vec U, Vec Udot, PetscReal a, Mat J,Mat Jpre,void *ctx) { PetscErrorCode ierr; AppCtx *user = (AppCtx *)ctx; PetscScalar v; const PetscScalar *x; PetscInt i,col; PetscFunctionBegin; ierr = VecGetArrayRead(U,&x); CHKERRQ(ierr); for(i=0; i < user->mx; i++) { v = a - 1. + 3.*x[i]*x[i]; col = i; ierr = MatSetValues(J,1,&i,1,&col,&v,INSERT_VALUES); CHKERRQ(ierr); } ierr = VecRestoreArrayRead(U,&x); CHKERRQ(ierr); ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); if (J != Jpre) { ierr = MatAssemblyBegin(Jpre,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); ierr = MatAssemblyEnd(Jpre,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); } /* MatView(J,PETSC_VIEWER_STDOUT_WORLD);*/ PetscFunctionReturn(0); }
static PetscErrorCode RHSFunction(TS ts,PetscReal t,Vec X,Vec F,void *ptr) { PetscErrorCode ierr; AppCtx *user = (AppCtx*)ptr; PetscScalar *f; const PetscScalar *x; PetscInt i,mx; PetscReal hx,eps; PetscFunctionBegin; mx = user->mx; eps = user->param; hx = (user->xright-user->xleft)/(mx-1); ierr = VecGetArrayRead(X,&x); CHKERRQ(ierr); ierr = VecGetArray(F,&f); CHKERRQ(ierr); f[0] = 2.*eps*(x[1]-x[0])/(hx*hx); /*boundary*/ for(i=1; i<mx-1; i++) { f[i]= eps*(x[i+1]-2.*x[i]+x[i-1])/(hx*hx); } f[mx-1] = 2.*eps*(x[mx-2]- x[mx-1])/(hx*hx); /*boundary*/ ierr = VecRestoreArrayRead(X,&x); CHKERRQ(ierr); ierr = VecRestoreArray(F,&f); CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode KSPMonitorRange_Private(KSP ksp,PetscInt it,PetscReal *per) { PetscErrorCode ierr; Vec resid; PetscReal rmax,pwork; PetscInt i,n,N; const PetscScalar *r; PetscFunctionBegin; ierr = KSPBuildResidual(ksp,NULL,NULL,&resid); CHKERRQ(ierr); ierr = VecNorm(resid,NORM_INFINITY,&rmax); CHKERRQ(ierr); ierr = VecGetLocalSize(resid,&n); CHKERRQ(ierr); ierr = VecGetSize(resid,&N); CHKERRQ(ierr); ierr = VecGetArrayRead(resid,&r); CHKERRQ(ierr); pwork = 0.0; for (i=0; i<n; i++) pwork += (PetscAbsScalar(r[i]) > .20*rmax); ierr = VecRestoreArrayRead(resid,&r); CHKERRQ(ierr); ierr = VecDestroy(&resid); CHKERRQ(ierr); ierr = MPIU_Allreduce(&pwork,per,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ksp)); CHKERRQ(ierr); *per = *per/N; PetscFunctionReturn(0); }
/* FormFunctionFortran - Evaluates nonlinear function, F(x) in Fortran. */ int FormFunctionFortran(SNES snes,Vec X,Vec F,void *ptr) { AppCtx *user = (AppCtx*)ptr; int ierr; PetscScalar *f; const 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)snes);CHKERRQ(ierr); if (!user->rank) { ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr); ierr = VecGetArray(F,&f);CHKERRQ(ierr); applicationfunctionfortran_(&user->param,&user->mx,&user->my,x,f,&ierr); ierr = VecRestoreArrayRead(X,&x);CHKERRQ(ierr); ierr = VecRestoreArray(F,&f);CHKERRQ(ierr); ierr = PetscLogFlops(11.0*(user->mx-2)*(user->my-2))CHKERRQ(ierr); } /* 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)snes);CHKERRQ(ierr); return 0; }
PetscErrorCode IFunction(TS ts,PetscReal t, Vec X, Vec Xdot, Vec F, Userctx *user) { PetscErrorCode ierr; SNES snes; PetscScalar *f; const PetscScalar *xdot; PetscInt i; PetscFunctionBegin; user->t = t; ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); ierr = ResidualFunction(snes,X,F,user);CHKERRQ(ierr); ierr = VecGetArray(F,&f); ierr = VecGetArrayRead(Xdot,&xdot);CHKERRQ(ierr); for (i=0;i < ngen;i++) { f[9*i] += xdot[9*i]; f[9*i+1] += xdot[9*i+1]; f[9*i+2] += xdot[9*i+2]; f[9*i+3] += xdot[9*i+3]; f[9*i+6] += xdot[9*i+6]; f[9*i+7] += xdot[9*i+7]; f[9*i+8] += xdot[9*i+8]; } ierr = VecRestoreArray(F,&f); ierr = VecRestoreArrayRead(Xdot,&xdot);CHKERRQ(ierr); PetscFunctionReturn(0); }
void Save(Vec u, const char* filename) { int n, rank; MPI_Comm_rank(PETSC_COMM_WORLD, &rank); VecGetSize(u, &n); Vec uloc; VecCreateSeq(PETSC_COMM_SELF, n, &uloc); VecScatter ctx; VecScatterCreateToZero(u, &ctx, &uloc); VecScatterBegin(ctx, u, uloc, INSERT_VALUES, SCATTER_FORWARD); VecScatterEnd(ctx, u, uloc, INSERT_VALUES, SCATTER_FORWARD); const PetscScalar *pu; VecGetArrayRead(uloc, &pu); if (rank==0) { int sq = sqrt(n); std::ofstream out(filename); for (size_t i=0; i<n; i++) { out << pu[i] << std::endl; if ((i+1)%sq == 0) out << std::endl; } } VecRestoreArrayRead(uloc, &pu); VecScatterDestroy(&ctx); VecDestroy(&uloc); }
static PetscErrorCode krylov_petsc_apply_aij( Mat A, Vec x, Vec y ) { void *ctx; PetscErrorCode ierr; krylov_pc_ctx_t* kct; const double* px; double* py; /* PetscFunctionBegin; */ ierr = MatShellGetContext( A, &ctx ); CHKERRQ(ierr); kct = (krylov_pc_ctx_t *)ctx; ierr = VecGetArrayRead( x, &px ); CHKERRQ(ierr); ierr = VecGetArray( y, &py ); CHKERRQ(ierr); problem_data_t* vecs = kct->vecs; weakeqn_ptrs_t* fcns = kct->fcns; p4est_t* p4est = kct->p4est; p4est_ghost_t* ghost = *kct->ghost; element_data_t* ghost_data = *kct->ghost_data; dgmath_jit_dbase_t* dgmath_jit_dbase = kct->dgmath_jit_dbase; problem_data_t vecs_for_aij; problem_data_copy_ptrs(vecs, &vecs_for_aij); vecs_for_aij.u = (double*)px; vecs_for_aij.Au = py; fcns->apply_lhs(p4est, ghost, ghost_data, &vecs_for_aij, dgmath_jit_dbase); ierr = VecRestoreArrayRead( x, &px ); CHKERRQ(ierr); ierr = VecRestoreArray( y, &py ); CHKERRQ(ierr); return ierr; }
static PetscErrorCode IJacobian(TS ts,PetscReal t,Vec X,Vec Xdot,PetscReal a,Mat A,Mat B,void *ctx) { PetscErrorCode ierr; User user = (User)ctx; PetscInt rowcol[] = {0,1}; PetscScalar J[2][2]; const PetscScalar *x; PetscFunctionBeginUser; ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr); J[0][0] = a; J[0][1] = -1.0; J[1][0] = c21*a + user->mu*(1.0 + 2.0*x[0]*x[1]); J[1][1] = -c21 + a - user->mu*(1.0-x[0]*x[0]); ierr = MatSetValues(B,2,rowcol,2,rowcol,&J[0][0],INSERT_VALUES);CHKERRQ(ierr); ierr = VecRestoreArrayRead(X,&x);CHKERRQ(ierr); ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); if (B && A != B) { ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); } PetscFunctionReturn(0); }
/** divide the the solution by the density */ PetscErrorCode ReformatSolution(Vec solution, Vec solution_unscaled, User user) { PetscScalar *x; const PetscScalar *xold; PetscInt cStart, cEnd, cEndInterior = user->cEndInterior, c; PetscErrorCode ierr; PetscFunctionBeginUser; ierr = DMPlexGetHeightStratum(user->dm, 0, &cStart, &cEnd);CHKERRQ(ierr); ierr = VecGetArrayRead(solution, &xold);CHKERRQ(ierr); ierr = VecGetArray(solution_unscaled, &x);CHKERRQ(ierr); for (c = cStart; c < cEndInterior; ++c) { PetscScalar *xc, *xcold; ierr = DMPlexPointGlobalRef(user->dm,c,x,&xc);CHKERRQ(ierr); ierr = DMPlexPointGlobalRead(user->dm,c,xold,&xcold);CHKERRQ(ierr); if (xc) { xc[0] = xcold[0]; // Density xc[1] = xcold[1]/xcold[0]; // Velocity u (the x-direction) xc[2] = xcold[2]/xcold[0]; // Velocity v (the y-direction) xc[3] = xcold[3]/xcold[0]; // Velocity w (the z-direction) xc[4] = xcold[4]/xcold[0]; // Total energy E } } ierr = VecRestoreArrayRead(solution, &xold);CHKERRQ(ierr); ierr = VecRestoreArray(solution_unscaled, &x);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode FormJacobian(SNES snes,Vec x,Mat jac,Mat B,void *ctx){ Solver_ctx *sol_ctx = (Solver_ctx*)ctx; ASL *asl=(ASL*)(sol_ctx->asl); const PetscScalar *xx; /* Variable vector*/ PetscScalar A[nzc]; /* Temporary storage for Jacobian Calc. */ PetscErrorCode ierr; /* PETSc Error code */ int err; /* ASL Error code */ unsigned long int i; /* Constraint index */ cgrad *cg; /* Constraint gradient information */ /* Compute Jacobian entries and insert into matrix. */ ierr = VecGetArrayRead(x,&xx);CHKERRQ(ierr); jacval((real*)xx,(real*)A,&err); ierr = VecRestoreArrayRead(x,&xx);CHKERRQ(ierr); for(i=n_conjac[0];i<n_conjac[1]; ++i){ /*i is constraint index */ cg = Cgrad[i]; while(cg!=NULL){ MatSetValue(B, i, cg->varno, A[cg->goff],INSERT_VALUES); cg=cg->next; } } /* Assemble matrix */ ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); return 0; }
/* Use the first order upwind scheme to compute the flux */ PetscErrorCode CaculateLocalMassFunction(DM dm, Vec locX, Vec F, User user) { PetscErrorCode ierr; const PetscScalar *x; PetscScalar *f; PetscInt cStart, cell; // Physics phys = user->model->physics; PetscFunctionBeginUser; ierr = VecGetArrayRead(locX,&x);CHKERRQ(ierr); ierr = VecGetArray(F,&f);CHKERRQ(ierr); ierr = DMPlexGetHeightStratum(dm, 0, &cStart, NULL);CHKERRQ(ierr); for (cell = cStart; cell < user->cEndInterior; cell++) { PetscInt i; PetscScalar *fref; const PetscScalar *xref; ierr = DMPlexPointGlobalRead(dm,cell,x,&xref);CHKERRQ(ierr); /*For the unkown variables*/ ierr = DMPlexPointGlobalRef(dm,cell,f,&fref);CHKERRQ(ierr); // if (!fref){ PetscPrintf(PETSC_COMM_WORLD,"%d, %d\n", cell, user->cEndInterior);} if (fref){ fref[0] = xref[0];/*the density*/ for (i=1; i<DIM+1; i++) { fref[i] = xref[0]*xref[i]; }/*viscosity*/ fref[DIM+1] = user->R/(user->adiabatic - 1)*xref[0]*xref[DIM+1];/*Energy*/ } } ierr = VecRestoreArrayRead(locX,&x);CHKERRQ(ierr); ierr = VecRestoreArray(F,&f);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode RHSfunction(TS ts,PetscReal t,Vec globalin,Vec globalout,void *ctx) { PetscErrorCode ierr; AppCtx *obj = (AppCtx*)ctx; PetscScalar soln[num_z]; const PetscScalar *soln_ptr; PetscInt i,nz=obj->nz; PetscReal time; /* get the previous solution to compute updated system */ ierr = VecGetArrayRead(globalin,&soln_ptr);CHKERRQ(ierr); for (i=0; i < num_z-2; i++) soln[i] = soln_ptr[i]; ierr = VecRestoreArrayRead(globalin,&soln_ptr);CHKERRQ(ierr); soln[num_z-1] = 0.0; soln[num_z-2] = 0.0; /* clear out the matrix and rhs for ksp to keep things straight */ ierr = VecSet(obj->ksp_rhs,(PetscScalar)0.0);CHKERRQ(ierr); time = t; /* get the updated system */ rhs(obj,soln,nz,obj->z,time); /* setup of the By+g rhs */ /* do a ksp solve to get the rhs for the ts problem */ if (obj->useAlhs) { /* ksp_sol = ksp_rhs */ ierr = VecCopy(obj->ksp_rhs,globalout);CHKERRQ(ierr); } else { /* ksp_sol = inv(Amat)*ksp_rhs */ ierr = Petsc_KSPSolve(obj);CHKERRQ(ierr);CHKERRQ(ierr); ierr = VecCopy(obj->ksp_sol,globalout);CHKERRQ(ierr); } return 0; }
/* Monitor timesteps and use interpolation to output at integer multiples of 0.1 */ static PetscErrorCode Monitor(TS ts,PetscInt step,PetscReal t,Vec X,void *ctx) { PetscErrorCode ierr; const PetscScalar *x; PetscReal tfinal, dt; User user = (User)ctx; Vec interpolatedX; PetscFunctionBeginUser; ierr = TSGetTimeStep(ts,&dt);CHKERRQ(ierr); ierr = TSGetDuration(ts,NULL,&tfinal);CHKERRQ(ierr); while (user->next_output <= t && user->next_output <= tfinal) { ierr = VecDuplicate(X,&interpolatedX);CHKERRQ(ierr); ierr = TSInterpolate(ts,user->next_output,interpolatedX);CHKERRQ(ierr); ierr = VecGetArrayRead(interpolatedX,&x);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"[%.1f] %D TS %.6f (dt = %.6f) X % 12.6e % 12.6e\n", user->next_output,step,t,dt,(double)PetscRealPart(x[0]), (double)PetscRealPart(x[1]));CHKERRQ(ierr); ierr = VecRestoreArrayRead(interpolatedX,&x);CHKERRQ(ierr); ierr = VecDestroy(&interpolatedX);CHKERRQ(ierr); user->next_output += 0.1; } PetscFunctionReturn(0); }
/* Defines the Jacobian of the ODE passed to the ODE solver. See TSSetIJacobian() for the meaning of a and the Jacobian. */ static PetscErrorCode RHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B,AppCtx *ctx) { PetscErrorCode ierr; PetscInt rowcol[] = {0,1}; PetscScalar J[2][2],Pmax; const PetscScalar *u; PetscFunctionBegin; ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr); if ((t > ctx->tf) && (t < ctx->tcl)) Pmax = 0.0; /* A short-circuit on the generator terminal that drives the electrical power output (Pmax*sin(delta)) to 0 */ else Pmax = ctx->Pmax; J[0][0] = 0; J[0][1] = ctx->omega_b; J[1][1] = -ctx->D*ctx->omega_s/(2.0*ctx->H); J[1][0] = -Pmax*PetscCosScalar(u[0])*ctx->omega_s/(2.0*ctx->H); ierr = MatSetValues(B,2,rowcol,2,rowcol,&J[0][0],INSERT_VALUES);CHKERRQ(ierr); ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr); ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); if (A != B) { ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); } PetscFunctionReturn(0); }
PetscErrorCode ComputeExactSolution(DM dm, PetscReal time, Vec X, User user) { DM dmCell; const PetscScalar *cellgeom; PetscScalar *x; PetscInt cStart, cEnd, cEndInterior = user->cEndInterior, c; PetscErrorCode ierr; PetscFunctionBeginUser; ierr = VecGetDM(user->cellgeom, &dmCell);CHKERRQ(ierr); ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd);CHKERRQ(ierr); ierr = VecGetArrayRead(user->cellgeom, &cellgeom);CHKERRQ(ierr); ierr = VecGetArray(X, &x);CHKERRQ(ierr); for (c = cStart; c < cEndInterior; ++c) { const CellGeom *cg; PetscScalar *xc; ierr = DMPlexPointLocalRead(dmCell,c,cellgeom,&cg);CHKERRQ(ierr); ierr = DMPlexPointGlobalRef(dm,c,x,&xc);CHKERRQ(ierr); if (xc) {ierr = ExactSolution(time, cg->centroid, xc, user);CHKERRQ(ierr);} } ierr = VecRestoreArrayRead(user->cellgeom, &cellgeom);CHKERRQ(ierr); ierr = VecRestoreArray(X, &x);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode VecView_Seq_Draw_LG(Vec xin,PetscViewer v) { PetscDraw draw; PetscBool isnull; PetscDrawLG lg; PetscErrorCode ierr; PetscInt i,c,bs = PetscAbs(xin->map->bs),n = xin->map->n/bs; const PetscScalar *xv; PetscReal *xx,*yy; int colors[] = {PETSC_DRAW_RED}; PetscFunctionBegin; ierr = PetscViewerDrawGetDraw(v,0,&draw);CHKERRQ(ierr); ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); ierr = PetscMalloc2(n,&xx,n,&yy);CHKERRQ(ierr); ierr = VecGetArrayRead(xin,&xv);CHKERRQ(ierr); for (c=0; c<bs; c++) { ierr = PetscViewerDrawGetDrawLG(v,c,&lg);CHKERRQ(ierr); ierr = PetscDrawLGReset(lg);CHKERRQ(ierr); ierr = PetscDrawLGSetDimension(lg,1);CHKERRQ(ierr); ierr = PetscDrawLGSetColors(lg,colors);CHKERRQ(ierr); for (i=0; i<n; i++) { xx[i] = (PetscReal)i; yy[i] = PetscRealPart(xv[c + i*bs]); } ierr = PetscDrawLGAddPoints(lg,n,&xx,&yy);CHKERRQ(ierr); ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr); ierr = PetscDrawLGSave(lg);CHKERRQ(ierr); } ierr = VecRestoreArrayRead(xin,&xv);CHKERRQ(ierr); ierr = PetscFree2(xx,yy);CHKERRQ(ierr); PetscFunctionReturn(0); }
static PetscErrorCode VecGetArray_Nest(Vec X,PetscScalar **x) { Vec_Nest *bx = (Vec_Nest*)X->data; PetscInt i,m,rstart,rend; PetscErrorCode ierr; PetscFunctionBegin; ierr = VecGetOwnershipRange(X,&rstart,&rend);CHKERRQ(ierr); ierr = VecGetLocalSize(X,&m);CHKERRQ(ierr); ierr = PetscMalloc1(m,x);CHKERRQ(ierr); for (i=0; i<bx->nb; i++) { Vec subvec = bx->v[i]; IS isy = bx->is[i]; PetscInt j,sm; const PetscInt *ixy; const PetscScalar *y; ierr = VecGetLocalSize(subvec,&sm);CHKERRQ(ierr); ierr = VecGetArrayRead(subvec,&y);CHKERRQ(ierr); ierr = ISGetIndices(isy,&ixy);CHKERRQ(ierr); for (j=0; j<sm; j++) { PetscInt ix = ixy[j]; if (ix < rstart || rend <= ix) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for getting array from nonlocal subvec"); (*x)[ix-rstart] = y[j]; } ierr = ISRestoreIndices(isy,&ixy);CHKERRQ(ierr); ierr = VecRestoreArrayRead(subvec,&y);CHKERRQ(ierr); } PetscFunctionReturn(0); }
PetscErrorCode OpSolution(Op op,DM dm,Vec U) { PetscErrorCode ierr; Vec X; const PetscScalar *x; PetscScalar *u; PetscReal L[3]; PetscInt i,m,bs; PetscFunctionBegin; ierr = PetscLogEventBegin(OP_Solution,dm,U,0,0);CHKERRQ(ierr); ierr = DMGetCoordinates(dm,&X);CHKERRQ(ierr); ierr = VecStrideMax(X,0,NULL,&L[0]);CHKERRQ(ierr); ierr = VecStrideMax(X,1,NULL,&L[1]);CHKERRQ(ierr); ierr = VecStrideMax(X,2,NULL,&L[2]);CHKERRQ(ierr); ierr = VecGetLocalSize(U,&m);CHKERRQ(ierr); ierr = VecGetBlockSize(U,&bs);CHKERRQ(ierr); ierr = VecGetArrayRead(X,&x);CHKERRQ(ierr); ierr = VecGetArray(U,&u);CHKERRQ(ierr); for (i=0; i<m/bs; i++) { ierr = (op->PointwiseSolution)(op,&x[i*3],L,&u[i*bs]);CHKERRQ(ierr); } ierr = VecRestoreArrayRead(X,&x);CHKERRQ(ierr); ierr = VecRestoreArray(U,&u);CHKERRQ(ierr); ierr = PetscLogEventEnd(OP_Solution,dm,U,0,0);CHKERRQ(ierr); PetscFunctionReturn(0); }