本文整理汇总了C++中VecSetValues函数的典型用法代码示例。如果您正苦于以下问题:C++ VecSetValues函数的具体用法?C++ VecSetValues怎么用?C++ VecSetValues使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了VecSetValues函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: malloc_with_check
Vector<Scalar>* PetscVector<Scalar>::change_sign()
{
PetscScalar* y = malloc_with_check(this->size, this);
int *idx = malloc_with_check(this->size, this);
for (unsigned int i = 0; i < this->size; i++) idx[i] = i;
VecGetValues(vec, this->size, idx, y);
for (unsigned int i = 0; i < this->size; i++) y[i] *= -1.;
VecSetValues(vec, this->size, idx, y, INSERT_VALUES);
free_with_check(y);
free_with_check(idx);
return this;
}
示例2: main
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;
}
示例3: VecMerge
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;
}
示例4: MatConvert_Shell
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);
}
示例5: main
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;
}
示例6: main
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;
}
示例7: preAllocateEntries
void linearSystemPETSc<fullMatrix<double> >::addToRightHandSide(int row,
const fullMatrix<double> &val)
{
if (!_entriesPreAllocated)
preAllocateEntries();
int blockSize;
_try(MatGetBlockSize(_a, &blockSize));
for (int ii = 0; ii < blockSize; ii++) {
PetscInt i = row * blockSize + ii;
PetscScalar v = val(ii, 0);
VecSetValues(_b, 1, &i, &v, ADD_VALUES);
}
}
示例8: reInitializeCalcBC
PetscErrorCode reInitializeCalcBC(PetscScalar tc, PetscInt Iterc, PetscScalar tf, PetscInt Iterf, PetscInt numTracers, Vec *v, Vec *bcc, Vec *bcf)
{
PetscErrorCode ierr;
PetscScalar myTime;
/* Recompute BC */
if (periodicBiogeochemForcing) {
ierr = interpPeriodicVector(tc,&Tss,biogeochemTimer.cyclePeriod,biogeochemTimer.numPerPeriod,biogeochemTimer.tdp,&Tssp,"Tss_");
ierr = interpPeriodicVector(tc,&Sss,biogeochemTimer.cyclePeriod,biogeochemTimer.numPerPeriod,biogeochemTimer.tdp,&Sssp,"Sss_");
ierr = interpPeriodicVector(tc,&atmosp,biogeochemTimer.cyclePeriod,biogeochemTimer.numPerPeriod,biogeochemTimer.tdp,&atmospp,"atmosp_");
myTime = DeltaT*Iterc; /* Iter should start at 0 */
inert_gas_bc_(&lBCSize,&Iterc,&myTime,&localTss[0],&localSss[0],
&localatmosp[0],&gasID,
&localBCc[0]);
ierr = interpPeriodicVector(tf,&Tss,biogeochemTimer.cyclePeriod,biogeochemTimer.numPerPeriod,biogeochemTimer.tdp,&Tssp,"Tss_");
ierr = interpPeriodicVector(tf,&Sss,biogeochemTimer.cyclePeriod,biogeochemTimer.numPerPeriod,biogeochemTimer.tdp,&Sssp,"Sss_");
ierr = interpPeriodicVector(tf,&atmosp,biogeochemTimer.cyclePeriod,biogeochemTimer.numPerPeriod,biogeochemTimer.tdp,&atmospp,"atmosp_");
myTime = DeltaT*Iterf; /* Iter should start at 0 */
inert_gas_bc_(&lBCSize,&Iterf,&myTime,&localTss[0],&localSss[0],
&localatmosp[0],&gasID,
&localBCf[0]);
ierr = VecSetValues(BCc,lBCSize,gBCIndices,localBCc,INSERT_VALUES);CHKERRQ(ierr);
ierr = VecAssemblyBegin(BCc);CHKERRQ(ierr);
ierr = VecAssemblyEnd(BCc);CHKERRQ(ierr);
ierr = VecSetValues(BCf,lBCSize,gBCIndices,localBCf,INSERT_VALUES);CHKERRQ(ierr);
ierr = VecAssemblyBegin(BCf);CHKERRQ(ierr);
ierr = VecAssemblyEnd(BCf);CHKERRQ(ierr);
}
return 0;
}
示例9: setPetsc
/*! \brief Set the Eigen internal vector
*
*
*/
void setPetsc() const
{
PETSC_SAFE_CALL(VecSetFromOptions(v));
// set the vector
if (row_val.size() != 0)
PETSC_SAFE_CALL(VecSetValues(v,row_val.size(),&row_val.template get<row_id>(0),&row_val.template get<val_id>(0),INSERT_VALUES))
PETSC_SAFE_CALL(VecAssemblyBegin(v));
PETSC_SAFE_CALL(VecAssemblyEnd(v));
}
示例10: CalculateSolution
PetscErrorCode CalculateSolution(PetscInt n,Vec *solution)
{
PetscErrorCode ierr;
PetscInt i;
PetscReal h,x = 0.0;
PetscScalar uu;
PetscFunctionBegin;
ierr = VecCreateSeq(PETSC_COMM_SELF,n,solution);CHKERRQ(ierr);
h = 1.0/((PetscReal)(n+1));
for (i=0; i<n; i++) {
x += h; uu = x*(1.-x);
ierr = VecSetValues(*solution,1,&i,&uu,INSERT_VALUES);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
示例11: CalculateRhs
PetscErrorCode CalculateRhs(Vec u)
{
PetscErrorCode ierr;
PetscInt i,n;
PetscReal h,x = 0.0;
PetscScalar uu;
PetscFunctionBegin;
ierr = VecGetSize(u,&n);CHKERRQ(ierr);
h = 1.0/((PetscReal)(n+1));
for (i=0; i<n; i++) {
x += h; uu = 2.0*h*h;
ierr = VecSetValues(u,1,&i,&uu,INSERT_VALUES);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
示例12: PCGAMGGetDataWithGhosts
PetscErrorCode PCGAMGGetDataWithGhosts(const Mat Gmat,const PetscInt data_sz,const PetscReal data_in[],PetscInt *a_stride,PetscReal **a_data_out)
{
PetscErrorCode ierr;
PetscMPIInt rank,size;
MPI_Comm comm;
Vec tmp_crds;
Mat_MPIAIJ *mpimat = (Mat_MPIAIJ*)Gmat->data;
PetscInt nnodes,num_ghosts,dir,kk,jj,my0,Iend,nloc;
PetscScalar *data_arr;
PetscReal *datas;
PetscBool isMPIAIJ;
PetscFunctionBegin;
ierr = PetscObjectGetComm((PetscObject)Gmat,&comm);CHKERRQ(ierr);
ierr = PetscObjectTypeCompare((PetscObject)Gmat, MATMPIAIJ, &isMPIAIJ);CHKERRQ(ierr);
ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
ierr = MatGetOwnershipRange(Gmat, &my0, &Iend);CHKERRQ(ierr);
nloc = Iend - my0;
ierr = VecGetLocalSize(mpimat->lvec, &num_ghosts);CHKERRQ(ierr);
nnodes = num_ghosts + nloc;
*a_stride = nnodes;
ierr = MatGetVecs(Gmat, &tmp_crds, 0);CHKERRQ(ierr);
ierr = PetscMalloc1(data_sz*nnodes, &datas);CHKERRQ(ierr);
for (dir=0; dir<data_sz; dir++) {
/* set local, and global */
for (kk=0; kk<nloc; kk++) {
PetscInt gid = my0 + kk;
PetscScalar crd = (PetscScalar)data_in[dir*nloc + kk]; /* col oriented */
datas[dir*nnodes + kk] = PetscRealPart(crd);
ierr = VecSetValues(tmp_crds, 1, &gid, &crd, INSERT_VALUES);CHKERRQ(ierr);
}
ierr = VecAssemblyBegin(tmp_crds);CHKERRQ(ierr);
ierr = VecAssemblyEnd(tmp_crds);CHKERRQ(ierr);
/* get ghost datas */
ierr = VecScatterBegin(mpimat->Mvctx,tmp_crds,mpimat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
ierr = VecScatterEnd(mpimat->Mvctx,tmp_crds,mpimat->lvec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
ierr = VecGetArray(mpimat->lvec, &data_arr);CHKERRQ(ierr);
for (kk=nloc,jj=0;jj<num_ghosts;kk++,jj++) datas[dir*nnodes + kk] = PetscRealPart(data_arr[jj]);
ierr = VecRestoreArray(mpimat->lvec, &data_arr);CHKERRQ(ierr);
}
ierr = VecDestroy(&tmp_crds);CHKERRQ(ierr);
*a_data_out = datas;
PetscFunctionReturn(0);
}
示例13: ComputeRHS
static PetscErrorCode ComputeRHS(KSP ksp,Vec b,void *ctx)
{
PetscErrorCode ierr;
PetscInt mx,idx[2];
PetscScalar h,v[2];
DM da;
PetscFunctionBeginUser;
ierr = KSPGetDM(ksp,&da);CHKERRQ(ierr);
ierr = DMDAGetInfo(da,0,&mx,0,0,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr);
h = 1.0/((mx-1));
ierr = VecSet(b,h);CHKERRQ(ierr);
idx[0] = 0; idx[1] = mx -1;
v[0] = v[1] = 0.0;
ierr = VecSetValues(b,2,idx,v,INSERT_VALUES);CHKERRQ(ierr);
ierr = VecAssemblyBegin(b);CHKERRQ(ierr);
ierr = VecAssemblyEnd(b);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例14: ComputeInitialSolution
PetscErrorCode ComputeInitialSolution(DM da,Vec x)
{
PetscErrorCode ierr;
PetscInt mx,col[2],xs,xm,i;
PetscScalar Hx,val[2];
PetscFunctionBeginUser;
ierr = DMDAGetInfo(da,0,&mx,0,0,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr);
Hx = 2.0*PETSC_PI / (PetscReal)(mx);
ierr = DMDAGetCorners(da,&xs,0,0,&xm,0,0);CHKERRQ(ierr);
for (i=xs; i<xs+xm; i++) {
col[0] = 2*i; col[1] = 2*i + 1;
val[0] = val[1] = PetscSinScalar(((PetscScalar)i)*Hx);
ierr = VecSetValues(x,2,col,val,INSERT_VALUES);CHKERRQ(ierr);
}
ierr = VecAssemblyBegin(x);CHKERRQ(ierr);
ierr = VecAssemblyEnd(x);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例15: _fill_vector
/**
* Fills in a PETSc vector from a JNI double array. They must be the same size.
*
* @param env a pointer to the JNI environment
* @param b a pointer to the the PETSc vector to fill
* @param values a pointer to the JNI double array to use
* @return 0 on success, PetscErrorCode on failure
*/
PetscErrorCode _fill_vector(JNIEnv *env, Vec *b, jdoubleArray *values) {
PetscErrorCode ierr;
// Get size and base pointer for array of values
jsize value_length = (*env) -> GetArrayLength(env, *values);
PetscScalar *value_array = (*env) -> GetDoubleArrayElements(env,
*values, 0);
// Make array of indices
int indices[value_length];
for (int i = 0; i < value_length; i++) {
indices[i] = i;
}
// Fill with values
ierr = VecSetValues(*b, value_length, indices, value_array, INSERT_VALUES);
CHKERRQ(ierr);
(*env) -> ReleaseDoubleArrayElements(env, *values, value_array, 0);
return 0;
}