/*--------------------------------------------------------- Function to fill the rhs vector with By + g values **** ---------------------------------------------------------*/ PetscErrorCode rhs(AppCtx *obj,PetscScalar *y, PetscInt nz, PetscScalar *z, PetscReal t) { PetscInt i,j,js,je,jj; PetscScalar val,g[num_z],btri[num_z][3],add_term; PetscErrorCode ierr; for (i=0; i < nz-2; i++) { for (j=0; j <= 2; j++) btri[i][j]=0.0; g[i] = 0.0; } /* call femBg to set the tri-diagonal b matrix and vector g */ femBg(btri,g,nz,z,t); /* setting the entries of the right hand side vector */ for (i=0; i < nz-2; i++) { val = 0.0; js = 0; if (i == 0) js = 1; je = 2; if (i == nz-2) je = 1; for (jj=js; jj <= je; jj++) { j = i+jj-1; val += (btri[i][jj])*(y[j]); } add_term = val + g[i]; ierr = VecSetValue(obj->ksp_rhs,(PetscInt)i,(PetscScalar)add_term,INSERT_VALUES);CHKERRQ(ierr); } ierr = VecAssemblyBegin(obj->ksp_rhs);CHKERRQ(ierr); ierr = VecAssemblyEnd(obj->ksp_rhs);CHKERRQ(ierr); /* return to main driver function */ return 0; }
PetscErrorCode BSSPotR1Vec(BSS self, PF pot, Vec V) { int order = self->order; int nb = self->num_basis; int ne = self->num_ele; int nq = order*ne; PetscErrorCode ierr; ierr = BSSCheck(self); CHKERRQ(ierr); PetscScalar *vs; PetscMalloc1(nq, &vs); ierr = PFApply(pot, nq, self->Rrs, vs); CHKERRQ(ierr); for(int i = 0; i < nb; i++) { int k0, k1; Non0QuadIndex(i, i, order, nq, &k0, &k1); PetscScalar v = 0.0; for(int k = k0; k < k1; k++) { //v += self->vals[k+i*nq] * self->ws[k] * vs[k]; v += self->vals[k+i*nq] * self->ws[k] * self->qrs[k] * vs[k]; } ierr = VecSetValue(V, i, v, INSERT_VALUES); CHKERRQ(ierr); } PetscFree(vs); VecAssemblyBegin(V); VecAssemblyEnd(V); return 0; }
Ensure(FFT, transforms_constant_into_delta_function) { Vec v; DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,4,2,1,NULL,&da); DMCreateGlobalVector(da, &v); VecZeroEntries(v); VecSetValue(v, 0, 0.7, INSERT_VALUES); VecAssemblyBegin(v); VecAssemblyEnd(v); scFftCreate(da, &fft); Vec x, y, z; scFftCreateVecsFFTW(fft, &x, &y, &z); scFftTransform(fft, v, 0, y); const PetscScalar *arr; VecGetArrayRead(y, &arr); //assert_that_double(fabs(arr[0] - 0.7), is_less_than_double(1.0e-6)); assert_that(fabs(arr[1] - 0.0) < 1.0e-6, is_true); assert_that(fabs(arr[1] - 0.7) < 1.0e-6, is_true); assert_that(fabs(arr[2] - 0.0) < 1.0e-6, is_true); assert_that(fabs(arr[3] - 0.7) < 1.0e-6, is_true); assert_that(fabs(arr[4] - 0.0) < 1.0e-6, is_true); VecRestoreArrayRead(y, &arr); VecDestroy(&x); VecDestroy(&y); VecDestroy(&z); VecDestroy(&v); scFftDestroy(&fft); DMDestroy(&da); }
void operator| (PUP::er& p, Vec& v) { PetscInt sz; if (!p.isUnpacking()) { VecGetSize(v, &sz); } p | sz; if(p.isUnpacking()) { VecCreateSeq(PETSC_COMM_WORLD, sz, &v); VecSetFromOptions(v); VecGetSize(v, &sz); for (int i = 0; i < sz; i++) { PetscScalar d; p | d; VecSetValue(v, i, d, INSERT_VALUES); } VecAssemblyBegin(v); VecAssemblyEnd(v); } else { for (int i = 0; i < sz; i++) { PetscScalar d; VecGetValues(v, 1, &i, &d); p | d; } } }
double Assembly_Mat_Vec(InterpolationDataStruct* pIData, int field, Mat M, Vec Fx, Vec Fy) { double coord1[3],coord2[3],coord3[3], value1, value2, value3, Fx_values[3],Fy_values[3]; //Assembly FIter fit = M_faceIter(pIData->m2); while (pEntity entity = FIter_next(fit)){ //get the coordinates and the id's from the element's vertices pEntity vertices[3] = {entity->get(0,0), entity->get(0,1), entity->get(0,2)}; int IDs[3] = {EN_id(vertices[0]), EN_id(vertices[1]), EN_id(vertices[2])}; int position[3] = {IDs[0]-1, IDs[1]-1, IDs[2]-1}; V_coord(vertices[0],coord1); V_coord(vertices[1],coord2); V_coord(vertices[2],coord3); //calculate area double At = F_area(coord1,coord2,coord3); double At_by_6 = (double)(At/6.); double At_by_12 = (double)(At/12.); double M_values[9] = {At_by_6, At_by_12, At_by_12, At_by_12, At_by_6, At_by_12, At_by_12, At_by_12, At_by_6}; //get node values pIData->pGetDblFunctions[field](IDs[0]-1,value1); pIData->pGetDblFunctions[field](IDs[1]-1,value2); pIData->pGetDblFunctions[field](IDs[2]-1,value3); double CV[3] = {value1/6., value2/6., value3/6.}; //calculate the contributions for(int i=0;i<3;i++){ Fx_values[i] = (coord2[1]-coord3[1])*CV[0] + (coord3[1]-coord1[1])*CV[1] + (coord1[1]-coord2[1])*CV[2]; Fy_values[i] = (coord3[0]-coord2[0])*CV[0] + (coord1[0]-coord3[0])*CV[1] + (coord2[0]-coord1[0])*CV[2]; } VecSetValues(Fx,3,position,Fx_values, ADD_VALUES); VecSetValues(Fy,3,position,Fy_values, ADD_VALUES); MatSetValues(M,3,position,3,position,M_values, ADD_VALUES); } FIter_delete(fit); PetscErrorCode ierr; ierr = MatAssemblyBegin(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(M,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = VecAssemblyBegin(Fx);CHKERRQ(ierr); ierr = VecAssemblyEnd(Fx);CHKERRQ(ierr); ierr = VecAssemblyBegin(Fy);CHKERRQ(ierr); ierr = VecAssemblyEnd(Fy);CHKERRQ(ierr); return 0; }
int main(int argc,char **argv) { PetscErrorCode ierr; PetscMPIInt size; PetscInt i,j,n = 50,bs; PetscScalar val,*vals,zero=0.0; Vec x; ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr); ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); bs = size; ierr = PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);CHKERRQ(ierr); ierr = VecCreate(PETSC_COMM_WORLD,&x);CHKERRQ(ierr); ierr = VecSetSizes(x,PETSC_DECIDE,n*bs);CHKERRQ(ierr); ierr = VecSetBlockSize(x,bs);CHKERRQ(ierr); ierr = VecSetFromOptions(x);CHKERRQ(ierr); for (i=0; i<n*bs; i++) { val = i*1.0; ierr = VecSetValues(x,1,&i,&val,INSERT_VALUES);CHKERRQ(ierr); } ierr = VecAssemblyBegin(x);CHKERRQ(ierr); ierr = VecAssemblyEnd(x);CHKERRQ(ierr); ierr = VecView(x,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); /* Now do the blocksetvalues */ ierr = VecSet(x,zero);CHKERRQ(ierr); ierr = PetscMalloc(bs*sizeof(PetscScalar),&vals);CHKERRQ(ierr); for (i=0; i<n; i++) { for (j=0; j<bs; j++) { vals[j] = (i*bs+j)*1.0; } ierr = VecSetValuesBlocked(x,1,&i,vals,INSERT_VALUES);CHKERRQ(ierr); } ierr = VecAssemblyBegin(x);CHKERRQ(ierr); ierr = VecAssemblyEnd(x);CHKERRQ(ierr); ierr = VecView(x,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = VecDestroy(x);CHKERRQ(ierr); ierr = PetscFree(vals);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
/*@ KSPComputeExplicitOperator - Computes the explicit preconditioned operator. Collective on KSP Input Parameter: . ksp - the Krylov subspace context Output Parameter: . mat - the explict preconditioned operator Notes: This computation is done by applying the operators to columns of the identity matrix. Currently, this routine uses a dense matrix format when 1 processor is used and a sparse format otherwise. This routine is costly in general, and is recommended for use only with relatively small systems. Level: advanced .keywords: KSP, compute, explicit, operator .seealso: KSPComputeEigenvaluesExplicitly(), PCComputeExplicitOperator() @*/ PetscErrorCode KSPComputeExplicitOperator(KSP ksp,Mat *mat) { Vec in,out; PetscErrorCode ierr; PetscMPIInt size; PetscInt i,M,m,*rows,start,end; Mat A; MPI_Comm comm; PetscScalar *array,one = 1.0; PetscFunctionBegin; PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); PetscValidPointer(mat,2); ierr = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr); ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); ierr = VecDuplicate(ksp->vec_sol,&in);CHKERRQ(ierr); ierr = VecDuplicate(ksp->vec_sol,&out);CHKERRQ(ierr); ierr = VecGetSize(in,&M);CHKERRQ(ierr); ierr = VecGetLocalSize(in,&m);CHKERRQ(ierr); ierr = VecGetOwnershipRange(in,&start,&end);CHKERRQ(ierr); ierr = PetscMalloc1(m,&rows);CHKERRQ(ierr); for (i=0; i<m; i++) rows[i] = start + i; ierr = MatCreate(comm,mat);CHKERRQ(ierr); ierr = MatSetSizes(*mat,m,m,M,M);CHKERRQ(ierr); if (size == 1) { ierr = MatSetType(*mat,MATSEQDENSE);CHKERRQ(ierr); ierr = MatSeqDenseSetPreallocation(*mat,NULL);CHKERRQ(ierr); } else { ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); ierr = MatMPIAIJSetPreallocation(*mat,0,NULL,0,NULL);CHKERRQ(ierr); } ierr = MatSetOption(*mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);CHKERRQ(ierr); if (!ksp->pc) {ierr = KSPGetPC(ksp,&ksp->pc);CHKERRQ(ierr);} ierr = PCGetOperators(ksp->pc,&A,NULL);CHKERRQ(ierr); for (i=0; i<M; i++) { ierr = VecSet(in,0.0);CHKERRQ(ierr); ierr = VecSetValues(in,1,&i,&one,INSERT_VALUES);CHKERRQ(ierr); ierr = VecAssemblyBegin(in);CHKERRQ(ierr); ierr = VecAssemblyEnd(in);CHKERRQ(ierr); ierr = KSP_MatMult(ksp,A,in,out);CHKERRQ(ierr); ierr = KSP_PCApply(ksp,out,in);CHKERRQ(ierr); ierr = VecGetArray(in,&array);CHKERRQ(ierr); ierr = MatSetValues(*mat,m,rows,1,&i,array,INSERT_VALUES);CHKERRQ(ierr); ierr = VecRestoreArray(in,&array);CHKERRQ(ierr); } ierr = PetscFree(rows);CHKERRQ(ierr); ierr = VecDestroy(&in);CHKERRQ(ierr); ierr = VecDestroy(&out);CHKERRQ(ierr); ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); PetscFunctionReturn(0); }
double linearSystemPETSc<scalar>::normInfRightHandSide() const { PetscReal nor; VecAssemblyBegin(_b); VecAssemblyEnd(_b); _try(VecNorm(_b, NORM_INFINITY, &nor)); return nor; }
void linearSystemPETSc<scalar>::zeroRightHandSide() { if (_isAllocated) { _try(VecAssemblyBegin(_b)); _try(VecAssemblyEnd(_b)); _try(VecZeroEntries(_b)); } }
void linearSystemPETSc<scalar>::zeroSolution() { if (_isAllocated) { _try(VecAssemblyBegin(_x)); _try(VecAssemblyEnd(_x)); _try(VecZeroEntries(_x)); } }
int main(int argc,char **argv) { PetscErrorCode ierr; PetscMPIInt rank; PetscInt i,N; PetscScalar one = 1.0; Vec x; PetscInitialize(&argc,&argv,(char *)0,help); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); /* Create a parallel vector. - In this case, we specify the size of each processor's local portion, and PETSc computes the global size. Alternatively, if we pass the global size and use PETSC_DECIDE for the local size PETSc will choose a reasonable partition trying to put nearly an equal number of elements on each processor. */ ierr = VecCreate(PETSC_COMM_WORLD,&x);CHKERRQ(ierr); ierr = VecSetSizes(x,rank+1,PETSC_DECIDE);CHKERRQ(ierr); ierr = VecSetFromOptions(x);CHKERRQ(ierr); ierr = VecGetSize(x,&N);CHKERRQ(ierr); ierr = VecSet(x,one);CHKERRQ(ierr); /* Set the vector elements. - Always specify global locations of vector entries. - Each processor can contribute any vector entries, regardless of which processor "owns" them; any nonlocal contributions will be transferred to the appropriate processor during the assembly process. - In this example, the flag ADD_VALUES indicates that all contributions will be added together. */ for (i=0; i<N-rank; i++) { ierr = VecSetValues(x,1,&i,&one,ADD_VALUES);CHKERRQ(ierr); } /* Assemble vector, using the 2-step process: VecAssemblyBegin(), VecAssemblyEnd() Computations can be done while messages are in transition by placing code between these two statements. */ ierr = VecAssemblyBegin(x);CHKERRQ(ierr); ierr = VecAssemblyEnd(x);CHKERRQ(ierr); /* View the vector; then destroy it. */ ierr = VecView(x,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = VecDestroy(x);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
int main(int argc,char **argv) { PetscErrorCode ierr; PetscInt i,blocks[2],nlocal; PetscMPIInt size,rank; PetscScalar value; Vec x,y; IS is1,is2; VecScatter ctx = 0; ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr); ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); if (size != 2) SETERRQ(PETSC_COMM_SELF,1,"Must run with 2 processors"); /* create two vectors */ if (!rank) nlocal = 8; else nlocal = 4; ierr = VecCreate(PETSC_COMM_WORLD,&x);CHKERRQ(ierr); ierr = VecSetSizes(x,nlocal,12);CHKERRQ(ierr); ierr = VecSetFromOptions(x);CHKERRQ(ierr); ierr = VecCreateSeq(PETSC_COMM_SELF,8,&y);CHKERRQ(ierr); /* create two index sets */ if (!rank) { blocks[0] = 0; blocks[1] = 2; } else { blocks[0] = 1; blocks[1] = 2; } ierr = ISCreateBlock(PETSC_COMM_SELF,4,2,blocks,PETSC_COPY_VALUES,&is1);CHKERRQ(ierr); ierr = ISCreateStride(PETSC_COMM_SELF,8,0,1,&is2);CHKERRQ(ierr); for (i=0; i<12; i++) { value = i; ierr = VecSetValues(x,1,&i,&value,INSERT_VALUES);CHKERRQ(ierr); } ierr = VecAssemblyBegin(x);CHKERRQ(ierr); ierr = VecAssemblyEnd(x);CHKERRQ(ierr); ierr = VecScatterCreate(x,is1,y,is2,&ctx);CHKERRQ(ierr); ierr = VecScatterBegin(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); ierr = VecScatterEnd(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr); ierr = PetscSleep(2.0*rank);CHKERRQ(ierr); ierr = VecView(y,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&y);CHKERRQ(ierr); ierr = ISDestroy(&is1);CHKERRQ(ierr); ierr = ISDestroy(&is2);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
/* #undef __FUNCT__ #define __FUNCT__ "phi2eval" PetscErrorCode phi2eval(Vec *X, Vec *Phi, PetscInt n) { PetscInt i,j,k,lo,hi; PetscErrorCode ierr; PetscReal sqrt2; PetscFunctionBegin; sqrt = PetscSqrtReal(2.0); ierr = VecGetOwnershipRange(X,&lo,&hi);CHKERRQ(ierr); PetscErrorCode phi2eval(PetscReal *x, PetscInt n, PetscReal *phi) { // Phi = .5*[x(1)^2 sqrt(2)*x(1)*x(2) ... sqrt(2)*x(1)*x(n) ... x(2)^2 sqrt(2)*x(2)*x(3) .. x(n)^2] PetscInt i,j,k; PetscReal sqrt2 = PetscSqrtReal(2.0); PetscFunctionBegin; j=0; for (i=0;i<n;i++) { phi[j] = 0.5 * x[i]*x[i]; j++; for (k=i+1;k<n;k++) { phi[j] = x[i]*x[k]/sqrt2; j++; } } PetscFunctionReturn(0); }*/ PetscErrorCode PoundersGramSchmidtReset(TAO_POUNDERS *mfqP, Vec *Q, PetscInt n) { PetscInt i; for (i=0;i<n;i++) { ierr = VecSet(Q[i],0.0);CHKERRQ(ierr); ierr = VecSetValue(Q[i],i,1.0,INSERT_VALUES);CHKERRQ(ierr); ierr = VecAssemblyBegin(Q[i]);CHKERRQ(ierr); ierr = VecAssemblyEnd(Q[i]);CHKERRQ(ierr); } }
-fout <file> : output file name\n\n"; #include <petscmat.h> #undef __FUNCT__ #define __FUNCT__ "main" int main(int argc,char **args) { Mat A; Vec b; char fileout[PETSC_MAX_PATH_LEN]; PetscInt i,j,m = 6,n = 6,N = 36,Ii,J; PetscErrorCode ierr; PetscScalar val,v; PetscViewer view; ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr; ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);CHKERRQ(ierr); ierr = MatSetFromOptions(A);CHKERRQ(ierr); for (i=0; i<m; i++) { for (j=0; j<n; j++) { v = -1.0; Ii = j + n*i; if (i>0) {J = Ii - n; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (i<m-1) {J = Ii + n; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j>0) {J = Ii - 1; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} if (j<n-1) {J = Ii + 1; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);} v = 4.0; ierr = MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);CHKERRQ(ierr); } } ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = VecCreate(PETSC_COMM_WORLD,&b);CHKERRQ(ierr); ierr = VecSetSizes(b,PETSC_DECIDE,N);CHKERRQ(ierr); ierr = VecSetFromOptions(b);CHKERRQ(ierr); for (i=0; i<N; i++) { val = i + 1; ierr = VecSetValues(b,1,&i,&val,INSERT_VALUES);CHKERRQ(ierr); } ierr = VecAssemblyBegin(b);CHKERRQ(ierr); ierr = VecAssemblyEnd(b);CHKERRQ(ierr); ierr = PetscOptionsGetString(NULL,NULL,"-fout",fileout,PETSC_MAX_PATH_LEN,NULL);CHKERRQ(ierr); ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,fileout,FILE_MODE_WRITE,&view);CHKERRQ(ierr); ierr = MatView(A,view);CHKERRQ(ierr); ierr = VecView(b,view);CHKERRQ(ierr); ierr = PetscViewerDestroy(&view);CHKERRQ(ierr); ierr = VecDestroy(&b);CHKERRQ(ierr); ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
/*@ MatComputeExplicitOperator - Computes the explicit matrix Collective on Mat Input Parameter: . inmat - the matrix Output Parameter: . mat - the explict preconditioned operator Notes: This computation is done by applying the operators to columns of the identity matrix. Currently, this routine uses a dense matrix format when 1 processor is used and a sparse format otherwise. This routine is costly in general, and is recommended for use only with relatively small systems. Level: advanced .keywords: Mat, compute, explicit, operator @*/ PetscErrorCode MatComputeExplicitOperator(Mat inmat,Mat *mat) { Vec in,out; PetscErrorCode ierr; PetscInt i,m,n,M,N,*rows,start,end; MPI_Comm comm; PetscScalar *array,zero = 0.0,one = 1.0; PetscMPIInt size; PetscFunctionBegin; PetscValidHeaderSpecific(inmat,MAT_CLASSID,1); PetscValidPointer(mat,2); ierr = PetscObjectGetComm((PetscObject)inmat,&comm);CHKERRQ(ierr); ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); ierr = MatGetLocalSize(inmat,&m,&n);CHKERRQ(ierr); ierr = MatGetSize(inmat,&M,&N);CHKERRQ(ierr); ierr = MatGetVecs(inmat,&in,&out);CHKERRQ(ierr); ierr = VecSetOption(in,VEC_IGNORE_OFF_PROC_ENTRIES,PETSC_TRUE);CHKERRQ(ierr); ierr = VecGetOwnershipRange(out,&start,&end);CHKERRQ(ierr); ierr = PetscMalloc(m*sizeof(PetscInt),&rows);CHKERRQ(ierr); for (i=0; i<m; i++) rows[i] = start + i; ierr = MatCreate(comm,mat);CHKERRQ(ierr); ierr = MatSetSizes(*mat,m,n,M,N);CHKERRQ(ierr); if (size == 1) { ierr = MatSetType(*mat,MATSEQDENSE);CHKERRQ(ierr); ierr = MatSeqDenseSetPreallocation(*mat,NULL);CHKERRQ(ierr); } else { ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); ierr = MatMPIAIJSetPreallocation(*mat,n,NULL,N-n,NULL);CHKERRQ(ierr); } for (i=0; i<N; i++) { ierr = VecSet(in,zero);CHKERRQ(ierr); ierr = VecSetValues(in,1,&i,&one,INSERT_VALUES);CHKERRQ(ierr); ierr = VecAssemblyBegin(in);CHKERRQ(ierr); ierr = VecAssemblyEnd(in);CHKERRQ(ierr); ierr = MatMult(inmat,in,out);CHKERRQ(ierr); ierr = VecGetArray(out,&array);CHKERRQ(ierr); ierr = MatSetValues(*mat,m,rows,1,&i,array,INSERT_VALUES);CHKERRQ(ierr); ierr = VecRestoreArray(out,&array);CHKERRQ(ierr); } ierr = PetscFree(rows);CHKERRQ(ierr); ierr = VecDestroy(&out);CHKERRQ(ierr); ierr = VecDestroy(&in);CHKERRQ(ierr); ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); PetscFunctionReturn(0); }
void FEMAssemble2DElasticity(MPI_Comm comm, Mesh *mesh, Mat &A, Vec &b, PetscReal E, PetscReal mi, PetscReal(*dens)(Element *), void(*f)(Element*, PetscReal, PetscReal*)) { PetscInt rank; MPI_Comm_rank(comm, &rank); PetscInt size = mesh->vetrices.size(); MatCreateSeqAIJ(PETSC_COMM_SELF, size * 2, size * 2, 14, PETSC_NULL, &A); VecCreateMPI(comm, size * 2, PETSC_DECIDE, &b); for (std::map<PetscInt, Element*>::iterator e = mesh->elements.begin(); e != mesh->elements.end(); e++) { Point* vetrices[3]; PetscInt vIndex[3]; for (int i = 0; i < 3; i++) { vIndex[i] = e->second->vetrices[i]; vetrices[i] = mesh->vetrices[vIndex[i]]; } PetscReal lStiff[36]; PetscReal bLoc[6]; //Local stiffness matrix and force vector assembly PetscReal fs[2]; f(e->second, dens(e->second), fs); elastLoc(vetrices, E, mi, fs, lStiff, bLoc); PetscInt idx[6], idxLoc[6]; for (int i = 0; i < 3; i++) { idx[i * 2] = vIndex[i] * 2; idx[i * 2 + 1] = vIndex[i] * 2 + 1; idxLoc[i * 2] = vIndex[i] * 2 - mesh->startIndexes[rank] * 2; idxLoc[i * 2 + 1] = vIndex[i] * 2 + 1 - mesh->startIndexes[rank] * 2; } MatSetValues(A, 6, idxLoc, 6, idxLoc, lStiff, ADD_VALUES); VecSetValues(b, 6, idx, bLoc, ADD_VALUES); } VecAssemblyBegin(b); VecAssemblyEnd(b); MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY); MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY); }
int main(int argc,char **args) { Mat mat; Vec b,u; PC pc; PetscErrorCode ierr; PetscInt n = 5,i,col[3]; PetscScalar value[3]; PetscInitialize(&argc,&args,(char *)0,help); /* Create vectors */ ierr = VecCreateSeq(PETSC_COMM_SELF,n,&b);CHKERRQ(ierr); ierr = VecCreateSeq(PETSC_COMM_SELF,n,&u);CHKERRQ(ierr); /* Create and assemble matrix */ ierr = MatCreateSeqDense(PETSC_COMM_SELF,n,n,PETSC_NULL,&mat);CHKERRQ(ierr); value[0] = -1.0; value[1] = 2.0; value[2] = -1.0; for (i=1; i<n-1; i++) { col[0] = i-1; col[1] = i; col[2] = i+1; ierr = MatSetValues(mat,1,&i,3,col,value,INSERT_VALUES);CHKERRQ(ierr); } i = n - 1; col[0] = n - 2; col[1] = n - 1; ierr = MatSetValues(mat,1,&i,2,col,value,INSERT_VALUES);CHKERRQ(ierr); i = 0; col[0] = 0; col[1] = 1; value[0] = 2.0; value[1] = -1.0; ierr = MatSetValues(mat,1,&i,2,col,value,INSERT_VALUES);CHKERRQ(ierr); ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); /* Create PC context and set up data structures */ ierr = PCCreate(PETSC_COMM_WORLD,&pc);CHKERRQ(ierr); ierr = PCSetType(pc,PCSOR);CHKERRQ(ierr); ierr = PCSetFromOptions(pc);CHKERRQ(ierr); ierr = PCSetOperators(pc,mat,mat,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr); ierr = PCSetUp(pc);CHKERRQ(ierr); value[0] = 1.0; for (i=0; i<n; i++) { ierr = VecSet(u,0.0);CHKERRQ(ierr); ierr = VecSetValues(u,1,&i,value,INSERT_VALUES);CHKERRQ(ierr); ierr = VecAssemblyBegin(u);CHKERRQ(ierr); ierr = VecAssemblyEnd(u);CHKERRQ(ierr); ierr = PCApply(pc,u,b);CHKERRQ(ierr); ierr = VecView(b,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); } /* Free data structures */ ierr = MatDestroy(&mat);CHKERRQ(ierr); ierr = PCDestroy(&pc);CHKERRQ(ierr); ierr = VecDestroy(&u);CHKERRQ(ierr); ierr = VecDestroy(&b);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
PetscErrorCode FEMAssembleTotal2DLaplace(MPI_Comm comm, Mesh *mesh, Mat &A, Vec &b, PetscReal(*f)(Point), PetscReal(*K)(Point)) { PetscErrorCode ierr; PetscInt rank; MPI_Comm_rank(PETSC_COMM_WORLD, &rank); PetscInt size = mesh->vetrices.size(); ierr = MatCreateMPIAIJ(comm, size, size, PETSC_DECIDE, PETSC_DECIDE, 7, PETSC_NULL, 0, PETSC_NULL, &A); CHKERRQ(ierr); ierr = VecCreateMPI(comm, size, PETSC_DECIDE, &b); CHKERRQ(ierr); for (std::map<PetscInt, Element*>::iterator e = mesh->elements.begin(); e != mesh->elements.end(); e++) { PetscScalar bl[3]; PetscScalar Al[9]; PetscReal R[4]; PetscInt elSize = e->second->numVetrices; Point *vetrices = new Point[elSize]; PetscInt ixs[elSize]; for (int j = 0; j < elSize; j++) { ixs[j] = e->second->vetrices[j]; vetrices[j] = *(mesh->vetrices[ixs[j]]); } R[0] = vetrices[1].x - vetrices[0].x; R[2] = vetrices[1].y - vetrices[0].y; R[1] = vetrices[2].x - vetrices[0].x; R[3] = vetrices[2].y - vetrices[0].y; Point center = getCenterOfSet(vetrices, elSize); bLoc(R, bl, f(center)); ALoc(R, Al, K(center)); ierr = VecSetValues(b, elSize, ixs, bl, ADD_VALUES); CHKERRQ(ierr); ierr = MatSetValues(A, elSize, ixs, elSize, ixs, Al, ADD_VALUES); CHKERRQ(ierr); } ierr = VecAssemblyBegin(b); CHKERRQ(ierr); ierr = VecAssemblyEnd(b); CHKERRQ(ierr); ierr = MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); ierr = MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); return ierr; }
PetscErrorCode fill(Mat m, Vec v) { PetscInt idxn[3] = {0, 1, 2}; PetscInt localRows = 0; PetscMPIInt rank,size; PetscErrorCode ierr; PetscFunctionBegin; ierr = MPI_Comm_rank(MPI_COMM_WORLD, &rank);CHKERRQ(ierr); ierr = MPI_Comm_size(MPI_COMM_WORLD, &size);CHKERRQ(ierr); if (rank == 1 || rank == 2) localRows = 4; if (size == 1) localRows = 8; ierr = MatSetSizes(m, localRows, PETSC_DECIDE, PETSC_DECIDE, 3);CHKERRQ(ierr); ierr = VecSetSizes(v, localRows, PETSC_DECIDE);CHKERRQ(ierr); ierr = MatSetFromOptions(m);CHKERRQ(ierr); ierr = VecSetFromOptions(v);CHKERRQ(ierr); ierr = MatSetUp(m);CHKERRQ(ierr); if (size == 1) { PetscInt idxm1[4] = {0, 1, 2, 3}; PetscScalar values1[12] = {1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1}; PetscInt idxm2[4] = {4, 5, 6, 7}; PetscScalar values2[12] = {1, 2, 0, 1, 2, 1, 1, 3, 0, 1, 3, 1}; ierr = MatSetValues(m, 4, idxm1, 3, idxn, values1, INSERT_VALUES);CHKERRQ(ierr); ierr = VecSetValue(v, 0, 1.1, INSERT_VALUES); VecSetValue(v, 1, 2.5, INSERT_VALUES);CHKERRQ(ierr); ierr = VecSetValue(v, 2, 3, INSERT_VALUES); VecSetValue(v, 3, 4, INSERT_VALUES);CHKERRQ(ierr); ierr = MatSetValues(m, 4, idxm2, 3, idxn, values2, INSERT_VALUES);CHKERRQ(ierr); ierr = VecSetValue(v, 4, 5, INSERT_VALUES); VecSetValue(v, 5, 6, INSERT_VALUES);CHKERRQ(ierr); ierr = VecSetValue(v, 6, 7, INSERT_VALUES); VecSetValue(v, 7, 8, INSERT_VALUES);CHKERRQ(ierr); } else if (rank == 1) { PetscInt idxm[4] = {0, 1, 2, 3}; PetscScalar values[12] = {1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1}; ierr = MatSetValues(m, 4, idxm, 3, idxn, values, INSERT_VALUES);CHKERRQ(ierr); ierr = VecSetValue(v, 0, 1.1, INSERT_VALUES); VecSetValue(v, 1, 2.5, INSERT_VALUES);CHKERRQ(ierr); ierr = VecSetValue(v, 2, 3, INSERT_VALUES); VecSetValue(v, 3, 4, INSERT_VALUES);CHKERRQ(ierr); } else if (rank == 2) { PetscInt idxm[4] = {4, 5, 6, 7}; PetscScalar values[12] = {1, 2, 0, 1, 2, 1, 1, 3, 0, 1, 3, 1}; ierr = MatSetValues(m, 4, idxm, 3, idxn, values, INSERT_VALUES);CHKERRQ(ierr); ierr = VecSetValue(v, 4, 5, INSERT_VALUES); VecSetValue(v, 5, 6, INSERT_VALUES);CHKERRQ(ierr); ierr = VecSetValue(v, 6, 7, INSERT_VALUES); VecSetValue(v, 7, 8, INSERT_VALUES);CHKERRQ(ierr); } ierr = MatAssemblyBegin(m, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(m, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = VecAssemblyBegin(v);CHKERRQ(ierr); ierr = VecAssemblyEnd(v);CHKERRQ(ierr); PetscFunctionReturn(0); }
static Vec op_create_vec ( const op_dat vec ) { assert( vec ); Vec p_vec; // Create a PETSc vector and pass it the user-allocated storage VecCreateSeqWithArray(MPI_COMM_SELF,vec->dim * vec->set->size,(PetscScalar*)vec->data,&p_vec); VecAssemblyBegin(p_vec); VecAssemblyEnd(p_vec); return p_vec; }
int main(int argc,char **argv) { PetscErrorCode ierr; PetscInt n = 5,i,idx2[3] = {0,2,3},idx1[3] = {0,1,2}; PetscMPIInt size,rank; PetscScalar value; Vec x,y; IS is1,is2; VecScatter ctx = 0; PetscInitialize(&argc,&argv,(char*)0,help); ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); /* create two vectors */ ierr = VecCreate(PETSC_COMM_WORLD,&x);CHKERRQ(ierr); ierr = VecSetSizes(x,PETSC_DECIDE,size*n);CHKERRQ(ierr); ierr = VecSetFromOptions(x);CHKERRQ(ierr); ierr = VecCreateSeq(PETSC_COMM_SELF,n,&y);CHKERRQ(ierr); /* create two index sets */ ierr = ISCreateGeneral(PETSC_COMM_SELF,3,idx1,PETSC_COPY_VALUES,&is1);CHKERRQ(ierr); ierr = ISCreateGeneral(PETSC_COMM_SELF,3,idx2,PETSC_COPY_VALUES,&is2);CHKERRQ(ierr); /* fill local part of parallel vector */ for (i=n*rank; i<n*(rank+1); i++) { value = (PetscScalar) i; ierr = VecSetValues(x,1,&i,&value,INSERT_VALUES);CHKERRQ(ierr); } ierr = VecAssemblyBegin(x);CHKERRQ(ierr); ierr = VecAssemblyEnd(x);CHKERRQ(ierr); ierr = VecView(x,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = VecSet(y,-1.0);CHKERRQ(ierr); ierr = VecScatterCreate(x,is1,y,is2,&ctx);CHKERRQ(ierr); ierr = VecScatterBegin(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); ierr = VecScatterEnd(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr); if (!rank) { ierr = PetscPrintf(PETSC_COMM_SELF,"scattered vector\n");CHKERRQ(ierr); ierr = VecView(y,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); } ierr = ISDestroy(&is1);CHKERRQ(ierr); ierr = ISDestroy(&is2);CHKERRQ(ierr); ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&y);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
/*@ STComputeExplicitOperator - Computes the explicit operator associated to the eigenvalue problem with the specified spectral transformation. Collective on ST Input Parameter: . st - the spectral transform context Output Parameter: . mat - the explicit operator Notes: This routine builds a matrix containing the explicit operator. For example, in generalized problems with shift-and-invert spectral transformation the result would be matrix (A - s B)^-1 B. This computation is done by applying the operator to columns of the identity matrix. This is analogous to MatComputeExplicitOperator(). Level: advanced .seealso: STApply() @*/ PetscErrorCode STComputeExplicitOperator(ST st,Mat *mat) { PetscErrorCode ierr; Vec in,out; PetscInt i,M,m,*rows,start,end; const PetscScalar *array; PetscScalar one = 1.0; PetscMPIInt size; PetscFunctionBegin; PetscValidHeaderSpecific(st,ST_CLASSID,1); PetscValidPointer(mat,2); STCheckMatrices(st,1); if (st->nmat>2) SETERRQ(PetscObjectComm((PetscObject)st),PETSC_ERR_ARG_WRONGSTATE,"Can only be used with 1 or 2 matrices"); ierr = MPI_Comm_size(PetscObjectComm((PetscObject)st),&size);CHKERRQ(ierr); ierr = MatGetVecs(st->A[0],&in,&out);CHKERRQ(ierr); ierr = VecGetSize(out,&M);CHKERRQ(ierr); ierr = VecGetLocalSize(out,&m);CHKERRQ(ierr); ierr = VecSetOption(in,VEC_IGNORE_OFF_PROC_ENTRIES,PETSC_TRUE);CHKERRQ(ierr); ierr = VecGetOwnershipRange(out,&start,&end);CHKERRQ(ierr); ierr = PetscMalloc1(m,&rows);CHKERRQ(ierr); for (i=0;i<m;i++) rows[i] = start + i; ierr = MatCreate(PetscObjectComm((PetscObject)st),mat);CHKERRQ(ierr); ierr = MatSetSizes(*mat,m,m,M,M);CHKERRQ(ierr); if (size == 1) { ierr = MatSetType(*mat,MATSEQDENSE);CHKERRQ(ierr); ierr = MatSeqDenseSetPreallocation(*mat,NULL);CHKERRQ(ierr); } else { ierr = MatSetType(*mat,MATMPIAIJ);CHKERRQ(ierr); ierr = MatMPIAIJSetPreallocation(*mat,m,NULL,M-m,NULL);CHKERRQ(ierr); } for (i=0;i<M;i++) { ierr = VecSet(in,0.0);CHKERRQ(ierr); ierr = VecSetValues(in,1,&i,&one,INSERT_VALUES);CHKERRQ(ierr); ierr = VecAssemblyBegin(in);CHKERRQ(ierr); ierr = VecAssemblyEnd(in);CHKERRQ(ierr); ierr = STApply(st,in,out);CHKERRQ(ierr); ierr = VecGetArrayRead(out,&array);CHKERRQ(ierr); ierr = MatSetValues(*mat,m,rows,1,&i,array,INSERT_VALUES);CHKERRQ(ierr); ierr = VecRestoreArrayRead(out,&array);CHKERRQ(ierr); } ierr = PetscFree(rows);CHKERRQ(ierr); ierr = VecDestroy(&in);CHKERRQ(ierr); ierr = VecDestroy(&out);CHKERRQ(ierr); ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode SolveInit(FEMInf fem, int L, PetscScalar *e0, Vec *x) { PetscErrorCode ierr; Mat H, S; ierr = CalcMat(fem, L, &H, &S); CHKERRQ(ierr); EPS eps; ierr = PrintTimeStamp(fem->comm, "EPS", NULL); CHKERRQ(ierr); ierr = EPSCreate(fem->comm, &eps); CHKERRQ(ierr); ierr = EPSSetTarget(eps, -0.6); CHKERRQ(ierr); ierr = EPSSetWhichEigenpairs(eps, EPS_TARGET_MAGNITUDE); CHKERRQ(ierr); ierr = EPSSetOperators(eps, H, S); CHKERRQ(ierr); if(S == NULL) { ierr = EPSSetProblemType(eps, EPS_NHEP); CHKERRQ(ierr); } else { ierr = EPSSetProblemType(eps, EPS_GNHEP); CHKERRQ(ierr); } Vec x0[1]; MatCreateVecs(H, &x0[0], NULL); int num; FEMInfGetSize(fem, &num); for(int i = 0; i < num; i++) { VecSetValue(x0[0], i, 0.5, INSERT_VALUES); } VecAssemblyBegin(x0[0]); VecAssemblyEnd(x0[0]); EPSSetInitialSpace(eps, 1, x0); ierr = EPSSetFromOptions(eps); CHKERRQ(ierr); ierr = EPSSolve(eps); CHKERRQ(ierr); PetscInt nconv; EPSGetConverged(eps, &nconv); if(nconv == 0) SETERRQ(fem->comm, 1, "Failed to digonalize in init state\n"); Vec x_ans; MatCreateVecs(H, &x_ans, NULL); EPSGetEigenpair(eps, 0, e0, NULL, x_ans, NULL); EPSDestroy(&eps); PetscScalar v[1]; PetscInt idx[1] = {1}; VecGetValues(x_ans, 1, idx, v); PetscScalar scale_factor = v[0] / cabs(v[0]); VecScale( x_ans, 1.0/scale_factor); PetscScalar norm0; Vec Sx; MatCreateVecs(S, &Sx, NULL); MatMult(S, x_ans, Sx); VecDot(x_ans, Sx, &norm0); VecScale(x_ans, 1.0/sqrt(norm0)); *x = x_ans; return 0; }
int main(int argc,char **argv) { PetscErrorCode ierr; PetscInt n = 5,N,i; PetscMPIInt size,rank; PetscScalar value,zero = 0.0; Vec x,y; IS is1,is2; VecScatter ctx = 0; ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr); ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); /* create two vectors */ N = size*n; ierr = VecCreate(PETSC_COMM_WORLD,&y);CHKERRQ(ierr); ierr = VecSetSizes(y,n,PETSC_DECIDE);CHKERRQ(ierr) ierr = VecSetFromOptions(y);CHKERRQ(ierr); ierr = VecCreate(PETSC_COMM_WORLD,&x);CHKERRQ(ierr); ierr = VecSetSizes(x,n,PETSC_DECIDE);CHKERRQ(ierr); ierr = VecSetFromOptions(x);CHKERRQ(ierr); /* create two index sets */ ierr = ISCreateStride(PETSC_COMM_WORLD,n,n*rank,1,&is1);CHKERRQ(ierr); ierr = ISCreateStride(PETSC_COMM_WORLD,n,(n*(rank+1))%N,1,&is2);CHKERRQ(ierr); /* fill local part of parallel vector x */ value = (PetscScalar)(rank+1); for (i=n*rank; i<n*(rank+1); i++) { ierr = VecSetValues(x,1,&i,&value,INSERT_VALUES);CHKERRQ(ierr); } ierr = VecAssemblyBegin(x);CHKERRQ(ierr); ierr = VecAssemblyEnd(x);CHKERRQ(ierr); ierr = VecSet(y,zero);CHKERRQ(ierr); ierr = VecScatterCreate(x,is1,y,is2,&ctx);CHKERRQ(ierr); ierr = VecScatterBegin(ctx,x,y,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); ierr = VecScatterEnd(ctx,x,y,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); ierr = VecScatterDestroy(ctx);CHKERRQ(ierr); ierr = VecView(y,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = VecDestroy(x);CHKERRQ(ierr); ierr = VecDestroy(y);CHKERRQ(ierr); ierr = ISDestroy(is1);CHKERRQ(ierr); ierr = ISDestroy(is2);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
/* FormHessian - Forms the Hessian matrix. Input Parameters: . tao - the Tao context . X - the input vector . ptr - optional user-defined context, as set by TaoSetHessian() Output Parameters: . H - Hessian matrix . PrecH - optionally different preconditioning Hessian . flag - flag indicating matrix structure Notes: This routine is intended simply as an example of the interface to a Hessian evaluation routine. Since this example compute the Hessian a column at a time, it is not particularly efficient and is not recommended. */ PetscErrorCode FormHessian(Tao tao,Vec X,Mat H,Mat Hpre, void *ptr) { AppCtx *user = (AppCtx *) ptr; PetscErrorCode ierr; PetscInt i,j, ndim = user->ndim; PetscReal *y, zero = 0.0, one = 1.0; PetscBool assembled; user->xvec = X; /* Initialize Hessian entries and work vector to zero */ ierr = MatAssembled(H,&assembled);CHKERRQ(ierr); if (assembled){ierr = MatZeroEntries(H); CHKERRQ(ierr);} ierr = VecSet(user->s, zero);CHKERRQ(ierr); /* Loop over matrix columns to compute entries of the Hessian */ for (j=0; j<ndim; j++) { ierr = VecSetValues(user->s,1,&j,&one,INSERT_VALUES);CHKERRQ(ierr); ierr = VecAssemblyBegin(user->s);CHKERRQ(ierr); ierr = VecAssemblyEnd(user->s);CHKERRQ(ierr); ierr = HessianProduct(ptr,user->s,user->y);CHKERRQ(ierr); ierr = VecSetValues(user->s,1,&j,&zero,INSERT_VALUES);CHKERRQ(ierr); ierr = VecAssemblyBegin(user->s);CHKERRQ(ierr); ierr = VecAssemblyEnd(user->s);CHKERRQ(ierr); ierr = VecGetArray(user->y,&y);CHKERRQ(ierr); for (i=0; i<ndim; i++) { if (y[i] != zero) { ierr = MatSetValues(H,1,&i,1,&j,&y[i],ADD_VALUES);CHKERRQ(ierr); } } ierr = VecRestoreArray(user->y,&y);CHKERRQ(ierr); } ierr = MatAssemblyBegin(H,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(H,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); return 0; }
PetscErrorCode VecMerge(Vec r1, Vec r2, Vec r){ PetscErrorCode err; PetscInt low, high, n1_local, n2_local; PetscInt *n1_idx, *n2_idx; PetscScalar *r1_array, *r2_array; PetscInt n1; int i; //copy r1 to r err = VecGetSize(r1, &n1); CHKERRQ(err); err = VecGetOwnershipRange(r1, &low, &high); CHKERRQ(err); err = VecGetLocalSize(r1, &n1_local); CHKERRQ(err); err = PetscMalloc1(n1_local, &n1_idx); CHKERRQ(err); for(i = 0; i < n1_local; i++) n1_idx[i] = low + i; err = VecGetArray(r1, &r1_array); CHKERRQ(err); err = VecSetValues(r, n1_local, n1_idx, r1_array, INSERT_VALUES); CHKERRQ(err); err = VecAssemblyBegin(r); CHKERRQ(err); err = VecAssemblyEnd(r); CHKERRQ(err); err = VecRestoreArray(r1, &r1_array); CHKERRQ(err); err = PetscFree(n1_idx); CHKERRQ(err); err = VecGetOwnershipRange(r2, &low, &high); CHKERRQ(err); err = VecGetLocalSize(r2, &n2_local); CHKERRQ(err); err = PetscMalloc1(n2_local, &n2_idx); CHKERRQ(err); for(i = 0; i < n2_local; i++) n2_idx[i] = low + i + n1; err = VecGetArray(r2, &r2_array); CHKERRQ(err); err = VecSetValues(r, n2_local, n2_idx, r2_array, INSERT_VALUES); CHKERRQ(err); err = VecAssemblyBegin(r); CHKERRQ(err); err = VecAssemblyEnd(r); CHKERRQ(err); //err = VecRestoreArray(r1, &r1_array); CHKERRQ(err); err = VecRestoreArray(r2, &r2_array); CHKERRQ(err); err = PetscFree(n1_idx); CHKERRQ(err); //err = PetscFree(n2_idx); CHKERRQ(err); return err; }
int main(int argc, char **args) { PetscErrorCode ierr; MPI_Comm comm = MPI_COMM_SELF; FEMInf fem; ViewerFunc viewer; int n; ierr = SlepcInitialize(&argc, &args, (char*)0, help); CHKERRQ(ierr); // ==== Initialize ==== PrintTimeStamp(comm, "Init", NULL); ierr = FEMInfCreate(comm, &fem); CHKERRQ(ierr); ierr = ViewerFuncCreate(comm, &viewer); CHKERRQ(ierr); // ==== Set values ==== PrintTimeStamp(comm, "Set", NULL); PetscOptionsBegin(comm, "", "plot_basis.c options", "none"); ierr = FEMInfSetFromOptions(fem); CHKERRQ(ierr); ierr = ViewerFuncSetFromOptions(viewer); CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL, "-n", &n, NULL); CHKERRQ(ierr); PetscOptionsEnd(); // ==== Check input values ==== int num; FEMInfGetSize(fem, &num); if(n < 0 || num <= n) { SETERRQ(comm, 1, "n msut be zero or positive integer and smaller than size of FEM"); } // ==== Calc ==== Vec c; ierr = VecCreateSeq(comm, num, &c); CHKERRQ(ierr); ierr = VecSet(c, 0.0); CHKERRQ(ierr); int indices[1] = {n}; PetscScalar values[1] = {1.0}; ierr = VecSetValues(c, 1, indices, values, INSERT_VALUES); CHKERRQ(ierr); ierr = VecAssemblyBegin(c); CHKERRQ(ierr); ierr = VecAssemblyEnd(c); CHKERRQ(ierr); FEMInfViewFunc(fem, c, viewer); // ==== Finalize ==== ierr = SlepcFinalize(); CHKERRQ(ierr); return 0; }
int main(int argc,char **argv) { PetscErrorCode ierr; PetscMPIInt size,rank; PetscInt n = 5,i; PetscScalar value; Vec x,y; IS is1,is2; VecScatter ctx = 0; ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr; ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); /* create two vectors */ ierr = VecCreate(PETSC_COMM_WORLD,&x);CHKERRQ(ierr); ierr = VecSetSizes(x,PETSC_DECIDE,size*n);CHKERRQ(ierr); ierr = VecSetFromOptions(x);CHKERRQ(ierr); ierr = VecCreateSeq(PETSC_COMM_SELF,n,&y);CHKERRQ(ierr); /* create two index sets */ ierr = ISCreateStride(PETSC_COMM_SELF,n,n*rank,1,&is1);CHKERRQ(ierr); ierr = ISCreateStride(PETSC_COMM_SELF,n,0,1,&is2);CHKERRQ(ierr); /* each processor inserts the entire vector */ /* this is redundant but tests assembly */ for (i=0; i<n*size; i++) { value = (PetscScalar) i; ierr = VecSetValues(x,1,&i,&value,INSERT_VALUES);CHKERRQ(ierr); } ierr = VecAssemblyBegin(x);CHKERRQ(ierr); ierr = VecAssemblyEnd(x);CHKERRQ(ierr); ierr = VecView(x,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = VecScatterCreate(x,is1,y,is2,&ctx);CHKERRQ(ierr); ierr = VecScatterBegin(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); ierr = VecScatterEnd(ctx,x,y,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); ierr = VecScatterDestroy(&ctx);CHKERRQ(ierr); if (!rank) { printf("----\n"); VecView(y,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); } ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&y);CHKERRQ(ierr); ierr = ISDestroy(&is1);CHKERRQ(ierr); ierr = ISDestroy(&is2);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
PETSC_INTERN PetscErrorCode MatConvert_Shell(Mat oldmat, MatType newtype,MatReuse reuse,Mat *newmat) { Mat mat; Vec in,out; PetscErrorCode ierr; PetscInt i,M,m,*rows,start,end; MPI_Comm comm; PetscScalar *array,zero = 0.0,one = 1.0; PetscFunctionBegin; ierr = PetscObjectGetComm((PetscObject)oldmat,&comm);CHKERRQ(ierr); ierr = MatGetOwnershipRange(oldmat,&start,&end);CHKERRQ(ierr); ierr = VecCreateMPI(comm,end-start,PETSC_DECIDE,&in);CHKERRQ(ierr); ierr = VecDuplicate(in,&out);CHKERRQ(ierr); ierr = VecGetSize(in,&M);CHKERRQ(ierr); ierr = VecGetLocalSize(in,&m);CHKERRQ(ierr); ierr = PetscMalloc1(m+1,&rows);CHKERRQ(ierr); for (i=0; i<m; i++) rows[i] = start + i; ierr = MatCreate(comm,&mat);CHKERRQ(ierr); ierr = MatSetSizes(mat,m,M,M,M);CHKERRQ(ierr); ierr = MatSetType(mat,newtype);CHKERRQ(ierr); ierr = MatSetBlockSizesFromMats(mat,oldmat,oldmat);CHKERRQ(ierr); ierr = MatSetUp(mat);CHKERRQ(ierr); for (i=0; i<M; i++) { ierr = VecSet(in,zero);CHKERRQ(ierr); ierr = VecSetValues(in,1,&i,&one,INSERT_VALUES);CHKERRQ(ierr); ierr = VecAssemblyBegin(in);CHKERRQ(ierr); ierr = VecAssemblyEnd(in);CHKERRQ(ierr); ierr = MatMult(oldmat,in,out);CHKERRQ(ierr); ierr = VecGetArray(out,&array);CHKERRQ(ierr); ierr = MatSetValues(mat,m,rows,1,&i,array,INSERT_VALUES);CHKERRQ(ierr); ierr = VecRestoreArray(out,&array);CHKERRQ(ierr); } ierr = PetscFree(rows);CHKERRQ(ierr); ierr = VecDestroy(&in);CHKERRQ(ierr); ierr = VecDestroy(&out);CHKERRQ(ierr); ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); if (reuse == MAT_INPLACE_MATRIX) { ierr = MatHeaderReplace(oldmat,&mat);CHKERRQ(ierr); } else { *newmat = mat; } PetscFunctionReturn(0); }
static PetscErrorCode VecAssemblyBegin_Nest(Vec v) { Vec_Nest *vs = (Vec_Nest*)v->data; PetscInt i; PetscErrorCode ierr; PetscFunctionBegin; for (i=0; i<vs->nb; i++) { if (!vs->v[i]) SETERRQ(PetscObjectComm((PetscObject)v),PETSC_ERR_SUP,"Nest vector cannot contain NULL blocks"); ierr = VecAssemblyBegin(vs->v[i]);CHKERRQ(ierr); } PetscFunctionReturn(0); }