本文整理汇总了C++中VecCreateSeq函数的典型用法代码示例。如果您正苦于以下问题:C++ VecCreateSeq函数的具体用法?C++ VecCreateSeq怎么用?C++ VecCreateSeq使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了VecCreateSeq函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: mlgeq
bool SAMpatchPETSc::expandSolution(const SystemVector& solVec,
Vector& dofVec, Real scaleSD) const
{
PETScVector* Bptr = const_cast<PETScVector*>(dynamic_cast<const PETScVector*>(&solVec));
if (!Bptr)
return false;
#ifdef HAVE_MPI
if (adm.isParallel()) {
if (!glob2LocEq) {
std::vector<int> mlgeq(adm.dd.getMLGEQ());
for (auto& it : mlgeq)
--it;
ISCreateGeneral(*adm.getCommunicator(),adm.dd.getMLGEQ().size(),
mlgeq.data(), PETSC_COPY_VALUES, &glob2LocEq);
}
Vec solution;
VecCreateSeq(PETSC_COMM_SELF, Bptr->dim(), &solution);
VecScatter ctx;
VecScatterCreate(Bptr->getVector(), glob2LocEq, solution, nullptr, &ctx);
VecScatterBegin(ctx, Bptr->getVector(), solution, INSERT_VALUES, SCATTER_FORWARD);
VecScatterEnd(ctx, Bptr->getVector(),solution,INSERT_VALUES,SCATTER_FORWARD);
VecScatterDestroy(&ctx);
PetscScalar* data;
VecGetArray(solution, &data);
std::copy(data, data + Bptr->dim(), Bptr->getPtr());
VecDestroy(&solution);
} else
#endif
{
PetscScalar* data;
VecGetArray(Bptr->getVector(), &data);
std::copy(data, data + Bptr->dim(), Bptr->getPtr());
VecRestoreArray(Bptr->getVector(), &data);
}
return this->SAMpatch::expandSolution(solVec, dofVec, scaleSD);
}
示例2: Monitor
PetscErrorCode Monitor(TS ts,PetscInt step,PetscReal time,Vec global,void *ctx)
{
VecScatter scatter;
IS from,to;
PetscInt i,n,*idx;
Vec tmp_vec;
PetscErrorCode ierr;
PetscScalar *tmp;
/* Get the size of the vector */
ierr = VecGetSize(global,&n);CHKERRQ(ierr);
/* Set the index sets */
ierr = PetscMalloc(n*sizeof(PetscInt),&idx);CHKERRQ(ierr);
for(i=0; i<n; i++) idx[i]=i;
/* Create local sequential vectors */
ierr = VecCreateSeq(PETSC_COMM_SELF,n,&tmp_vec);CHKERRQ(ierr);
/* Create scatter context */
ierr = ISCreateGeneral(PETSC_COMM_SELF,n,idx,PETSC_COPY_VALUES,&from);CHKERRQ(ierr);
ierr = ISCreateGeneral(PETSC_COMM_SELF,n,idx,PETSC_COPY_VALUES,&to);CHKERRQ(ierr);
ierr = VecScatterCreate(global,from,tmp_vec,to,&scatter);CHKERRQ(ierr);
ierr = VecScatterBegin(scatter,global,tmp_vec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
ierr = VecScatterEnd(scatter,global,tmp_vec,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
ierr = VecGetArray(tmp_vec,&tmp);CHKERRQ(ierr);
ierr = PetscPrintf(PETSC_COMM_WORLD,"At t =%14.6e u = %14.6e %14.6e %14.6e \n",
time,PetscRealPart(tmp[0]),PetscRealPart(tmp[1]),PetscRealPart(tmp[2]));CHKERRQ(ierr);
ierr = PetscPrintf(PETSC_COMM_WORLD,"At t =%14.6e errors = %14.6e %14.6e %14.6e \n",
time,PetscRealPart(tmp[0]-solx(time)),PetscRealPart(tmp[1]-soly(time)),PetscRealPart(tmp[2]-solz(time)));CHKERRQ(ierr);
ierr = VecRestoreArray(tmp_vec,&tmp);CHKERRQ(ierr);
ierr = VecScatterDestroy(&scatter);CHKERRQ(ierr);
ierr = ISDestroy(&from);CHKERRQ(ierr);
ierr = ISDestroy(&to);CHKERRQ(ierr);
ierr = PetscFree(idx);CHKERRQ(ierr);
ierr = VecDestroy(&tmp_vec);CHKERRQ(ierr);
return 0;
}
示例3: MPI_Comm_rank
void ProfilerTest::test_petsc_memory() {
int ierr, mpi_rank;
ierr = MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
EXPECT_EQ( ierr, 0 );
Profiler::initialize(); {
PetscLogDouble mem;
START_TIMER("A");
PetscInt size = 100*1000;
PetscScalar value = 0.1;
Vec tmp_vector;
VecCreateSeq(PETSC_COMM_SELF, size, &tmp_vector);
VecSet(tmp_vector, value);
// VecSetRandom(tmp_vector, NULL);
END_TIMER("A");
START_TIMER("A");
// allocated memory MUST be greater or equal to size * size of double
EXPECT_GE(AN.petsc_memory_difference, size*sizeof(double));
END_TIMER("A");
START_TIMER("B");
PetscScalar sum;
VecSum(tmp_vector, &sum);
END_TIMER("B");
START_TIMER("C");
VecDestroy(&tmp_vector);
END_TIMER("C");
START_TIMER("C");
// since we are destroying vector, we expect to see negative memory difference
EXPECT_LE(AN.petsc_memory_difference, 0);
END_TIMER("C");
}
PI->output(MPI_COMM_WORLD, cout);
Profiler::uninitialize();
}
示例4: assert
inline void PetscVector::init (const int n,
const int n_local,
const bool fast,
const ParallelType type) {
int ierr=0;
int petsc_n=static_cast<int>(n);
int petsc_n_local=static_cast<int>(n_local);
// Clear initialized vectors
if (this->initialized()) this->clear();
if (type == AUTOMATIC) {
if (n == n_local) this->_type = SERIAL;
else this->_type = PARALLEL;
} else this->_type = type;
assert ((this->_type==SERIAL && n==n_local) || this->_type==PARALLEL);
// create a sequential vector if on only 1 processor
if (this->_type == SERIAL) {
ierr = VecCreateSeq (PETSC_COMM_SELF, petsc_n, &_vec);
CHKERRABORT(PETSC_COMM_SELF,ierr);
ierr = VecSetFromOptions (_vec);
CHKERRABORT(PETSC_COMM_SELF,ierr);
}
// otherwise create an MPI-enabled vector
else if (this->_type == PARALLEL) {
assert (n_local <= n);
ierr = VecCreateMPI (MPI_COMM_WORLD, petsc_n_local, petsc_n, &_vec);
CHKERRABORT(MPI_COMM_WORLD,ierr);
ierr = VecSetFromOptions (_vec);
CHKERRABORT(MPI_COMM_WORLD,ierr);
} else {
std::cout << "Not good" <<std::endl;
abort();
}
this->_is_initialized = true;
this->_is_closed = true;
if (fast == false) this->zero ();
}
示例5: main
int main(int argc,char **argv)
{
KSP ksp;
PC pc;
Mat A,M;
Vec X,B,D;
MPI_Comm comm;
PetscScalar v;
KSPConvergedReason reason;
PetscInt i,j,its;
PetscErrorCode ierr;
PetscFunctionBegin;
ierr = PetscInitialize(&argc,&argv,0,help);CHKERRQ(ierr);
ierr = PetscOptionsSetValue("-options_left",PETSC_NULL);CHKERRQ(ierr);
comm = MPI_COMM_SELF;
/*
* Construct the Kershaw matrix
* and a suitable rhs / initial guess
*/
ierr = MatCreateSeqAIJ(comm,4,4,4,0,&A);CHKERRQ(ierr);
ierr = VecCreateSeq(comm,4,&B);CHKERRQ(ierr);
ierr = VecDuplicate(B,&X);CHKERRQ(ierr);
for (i=0; i<4; i++) {
v=3;
ierr = MatSetValues(A,1,&i,1,&i,&v,INSERT_VALUES);CHKERRQ(ierr);
v=1;
ierr = VecSetValues(B,1,&i,&v,INSERT_VALUES);CHKERRQ(ierr);
ierr = VecSetValues(X,1,&i,&v,INSERT_VALUES);CHKERRQ(ierr);
}
i=0; v=0;
ierr = VecSetValues(X,1,&i,&v,INSERT_VALUES);CHKERRQ(ierr);
for (i=0; i<3; i++) {
v=-2; j=i+1;
ierr = MatSetValues(A,1,&i,1,&j,&v,INSERT_VALUES);CHKERRQ(ierr);
ierr = MatSetValues(A,1,&j,1,&i,&v,INSERT_VALUES);CHKERRQ(ierr);
}
i=0; j=3; v=2;
ierr = MatSetValues(A,1,&i,1,&j,&v,INSERT_VALUES);CHKERRQ(ierr);
ierr = MatSetValues(A,1,&j,1,&i,&v,INSERT_VALUES);CHKERRQ(ierr);
ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = VecAssemblyBegin(B);CHKERRQ(ierr);
ierr = VecAssemblyEnd(B);CHKERRQ(ierr);
printf("\nThe Kershaw matrix:\n\n"); MatView(A,0);
/*
* A Conjugate Gradient method
* with ILU(0) preconditioning
*/
ierr = KSPCreate(comm,&ksp);CHKERRQ(ierr);
ierr = KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
ierr = KSPSetType(ksp,KSPCG);CHKERRQ(ierr);
ierr = KSPSetInitialGuessNonzero(ksp,PETSC_TRUE);CHKERRQ(ierr);
/*
* ILU preconditioner;
* The iterative method will break down unless you comment in the SetShift
* line below, or use the -pc_factor_shift_positive_definite option.
* Run the code twice: once as given to see the negative pivot and the
* divergence behaviour, then comment in the Shift line, or add the
* command line option, and see that the pivots are all positive and
* the method converges.
*/
ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
ierr = PCSetType(pc,PCICC);CHKERRQ(ierr);
/* ierr = PCFactorSetShiftType(prec,MAT_SHIFT_POSITIVE_DEFINITE);CHKERRQ(ierr); */
ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
ierr = KSPSetUp(ksp);CHKERRQ(ierr);
/*
* Now that the factorisation is done, show the pivots;
* note that the last one is negative. This in itself is not an error,
* but it will make the iterative method diverge.
*/
ierr = PCFactorGetMatrix(pc,&M);CHKERRQ(ierr);
ierr = VecDuplicate(B,&D);CHKERRQ(ierr);
ierr = MatGetDiagonal(M,D);CHKERRQ(ierr);
printf("\nPivots:\n\n"); VecView(D,0);
/*
* Solve the system;
* without the shift this will diverge with
* an indefinite preconditioner
*/
ierr = KSPSolve(ksp,B,X);CHKERRQ(ierr);
ierr = KSPGetConvergedReason(ksp,&reason);CHKERRQ(ierr);
if (reason==KSP_DIVERGED_INDEFINITE_PC) {
printf("\nDivergence because of indefinite preconditioner;\n");
printf("Run the executable again but with -pc_factor_shift_positive_definite option.\n");
} else if (reason<0) {
printf("\nOther kind of divergence: this should not happen.\n");
} else {
ierr = KSPGetIterationNumber(ksp,&its);CHKERRQ(ierr);
printf("\nConvergence in %d iterations.\n",(int)its);
//.........这里部分代码省略.........
示例6: MatSetUpMultiply_MPIAIJ
//.........这里部分代码省略.........
lid--;
garray[lid] = gid;
}
ierr = PetscSortInt(ec,garray);CHKERRQ(ierr); /* sort, and rebuild */
ierr = PetscTableRemoveAll(gid1_lid1);CHKERRQ(ierr);
for (i=0; i<ec; i++) {
ierr = PetscTableAdd(gid1_lid1,garray[i]+1,i+1,INSERT_VALUES);CHKERRQ(ierr);
}
/* compact out the extra columns in B */
for (i=0; i<aij->B->rmap->n; i++) {
for (j=0; j<B->ilen[i]; j++) {
PetscInt gid1 = aj[B->i[i] + j] + 1;
ierr = PetscTableFind(gid1_lid1,gid1,&lid);CHKERRQ(ierr);
lid--;
aj[B->i[i] + j] = lid;
}
}
aij->B->cmap->n = aij->B->cmap->N = ec;
aij->B->cmap->bs = 1;
ierr = PetscLayoutSetUp((aij->B->cmap));CHKERRQ(ierr);
ierr = PetscTableDestroy(&gid1_lid1);CHKERRQ(ierr);
#else
/* Make an array as long as the number of columns */
/* mark those columns that are in aij->B */
ierr = PetscCalloc1(N+1,&indices);CHKERRQ(ierr);
for (i=0; i<aij->B->rmap->n; i++) {
for (j=0; j<B->ilen[i]; j++) {
if (!indices[aj[B->i[i] + j]]) ec++;
indices[aj[B->i[i] + j]] = 1;
}
}
/* form array of columns we need */
ierr = PetscMalloc1(ec+1,&garray);CHKERRQ(ierr);
ec = 0;
for (i=0; i<N; i++) {
if (indices[i]) garray[ec++] = i;
}
/* make indices now point into garray */
for (i=0; i<ec; i++) {
indices[garray[i]] = i;
}
/* compact out the extra columns in B */
for (i=0; i<aij->B->rmap->n; i++) {
for (j=0; j<B->ilen[i]; j++) {
aj[B->i[i] + j] = indices[aj[B->i[i] + j]];
}
}
aij->B->cmap->n = aij->B->cmap->N = ec;
aij->B->cmap->bs = 1;
ierr = PetscLayoutSetUp((aij->B->cmap));CHKERRQ(ierr);
ierr = PetscFree(indices);CHKERRQ(ierr);
#endif
} else {
garray = aij->garray;
}
if (!aij->lvec) {
/* create local vector that is used to scatter into */
ierr = VecCreateSeq(PETSC_COMM_SELF,ec,&aij->lvec);CHKERRQ(ierr);
} else {
ierr = VecGetSize(aij->lvec,&ec);CHKERRQ(ierr);
}
/* create two temporary Index sets for build scatter gather */
ierr = ISCreateGeneral(((PetscObject)mat)->comm,ec,garray,PETSC_COPY_VALUES,&from);CHKERRQ(ierr);
ierr = ISCreateStride(PETSC_COMM_SELF,ec,0,1,&to);CHKERRQ(ierr);
/* create temporary global vector to generate scatter context */
/* This does not allocate the array's memory so is efficient */
ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject)mat),1,mat->cmap->n,mat->cmap->N,NULL,&gvec);CHKERRQ(ierr);
/* generate the scatter context */
if (aij->Mvctx_mpi1_flg) {
ierr = VecScatterDestroy(&aij->Mvctx_mpi1);CHKERRQ(ierr);
ierr = VecScatterCreate(gvec,from,aij->lvec,to,&aij->Mvctx_mpi1);CHKERRQ(ierr);
ierr = VecScatterSetType(aij->Mvctx_mpi1,VECSCATTERMPI1);CHKERRQ(ierr);
ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)aij->Mvctx_mpi1);CHKERRQ(ierr);
} else {
ierr = VecScatterDestroy(&aij->Mvctx);CHKERRQ(ierr);
ierr = VecScatterCreate(gvec,from,aij->lvec,to,&aij->Mvctx);CHKERRQ(ierr);
ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)aij->Mvctx);CHKERRQ(ierr);
ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)aij->lvec);CHKERRQ(ierr);
ierr = PetscLogObjectMemory((PetscObject)mat,(ec+1)*sizeof(PetscInt));CHKERRQ(ierr);
}
aij->garray = garray;
ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)from);CHKERRQ(ierr);
ierr = PetscLogObjectParent((PetscObject)mat,(PetscObject)to);CHKERRQ(ierr);
ierr = ISDestroy(&from);CHKERRQ(ierr);
ierr = ISDestroy(&to);CHKERRQ(ierr);
ierr = VecDestroy(&gvec);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例7: main
int main(int argc,char **args)
{
Mat C;
PetscInt i,j,m = 3,n = 3,Ii,J;
PetscErrorCode ierr;
PetscBool flg;
PetscScalar v;
IS perm,iperm;
Vec x,u,b,y;
PetscReal norm,tol=PETSC_SMALL;
MatFactorInfo info;
PetscMPIInt size;
ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
if (size != 1) SETERRQ(PETSC_COMM_WORLD,1,"This is a uniprocessor example only!");
ierr = MatCreate(PETSC_COMM_WORLD,&C);CHKERRQ(ierr);
ierr = MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);CHKERRQ(ierr);
ierr = MatSetFromOptions(C);CHKERRQ(ierr);
ierr = MatSetUp(C);CHKERRQ(ierr);
ierr = PetscOptionsHasName(NULL,NULL,"-symmetric",&flg);CHKERRQ(ierr);
if (flg) { /* Treat matrix as symmetric only if we set this flag */
ierr = MatSetOption(C,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
ierr = MatSetOption(C,MAT_SYMMETRY_ETERNAL,PETSC_TRUE);CHKERRQ(ierr);
}
/* Create the matrix for the five point stencil, YET AGAIN */
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(C,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);}
if (i<m-1) {J = Ii + n; ierr = MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);}
if (j>0) {J = Ii - 1; ierr = MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);}
if (j<n-1) {J = Ii + 1; ierr = MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);}
v = 4.0; ierr = MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);CHKERRQ(ierr);
}
}
ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatGetOrdering(C,MATORDERINGRCM,&perm,&iperm);CHKERRQ(ierr);
ierr = MatView(C,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
ierr = ISView(perm,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
ierr = VecCreateSeq(PETSC_COMM_SELF,m*n,&u);CHKERRQ(ierr);
ierr = VecSet(u,1.0);CHKERRQ(ierr);
ierr = VecDuplicate(u,&x);CHKERRQ(ierr);
ierr = VecDuplicate(u,&b);CHKERRQ(ierr);
ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
ierr = MatMult(C,u,b);CHKERRQ(ierr);
ierr = VecCopy(b,y);CHKERRQ(ierr);
ierr = VecScale(y,2.0);CHKERRQ(ierr);
ierr = MatNorm(C,NORM_FROBENIUS,&norm);CHKERRQ(ierr);
ierr = PetscPrintf(PETSC_COMM_SELF,"Frobenius norm of matrix %g\n",(double)norm);CHKERRQ(ierr);
ierr = MatNorm(C,NORM_1,&norm);CHKERRQ(ierr);
ierr = PetscPrintf(PETSC_COMM_SELF,"One norm of matrix %g\n",(double)norm);CHKERRQ(ierr);
ierr = MatNorm(C,NORM_INFINITY,&norm);CHKERRQ(ierr);
ierr = PetscPrintf(PETSC_COMM_SELF,"Infinity norm of matrix %g\n",(double)norm);CHKERRQ(ierr);
ierr = MatFactorInfoInitialize(&info);CHKERRQ(ierr);
info.fill = 2.0;
info.dtcol = 0.0;
info.zeropivot = 1.e-14;
info.pivotinblocks = 1.0;
ierr = MatLUFactor(C,perm,iperm,&info);CHKERRQ(ierr);
/* Test MatSolve */
ierr = MatSolve(C,b,x);CHKERRQ(ierr);
ierr = VecView(b,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
ierr = VecView(x,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
ierr = VecAXPY(x,-1.0,u);CHKERRQ(ierr);
ierr = VecNorm(x,NORM_2,&norm);CHKERRQ(ierr);
if (norm > tol) {
ierr = PetscPrintf(PETSC_COMM_SELF,"MatSolve: Norm of error %g\n",(double)norm);CHKERRQ(ierr);
}
/* Test MatSolveAdd */
ierr = MatSolveAdd(C,b,y,x);CHKERRQ(ierr);
ierr = VecAXPY(x,-1.0,y);CHKERRQ(ierr);
ierr = VecAXPY(x,-1.0,u);CHKERRQ(ierr);
ierr = VecNorm(x,NORM_2,&norm);CHKERRQ(ierr);
if (norm > tol) {
ierr = PetscPrintf(PETSC_COMM_SELF,"MatSolveAdd(): Norm of error %g\n",(double)norm);CHKERRQ(ierr);
}
ierr = ISDestroy(&perm);CHKERRQ(ierr);
ierr = ISDestroy(&iperm);CHKERRQ(ierr);
ierr = VecDestroy(&u);CHKERRQ(ierr);
ierr = VecDestroy(&y);CHKERRQ(ierr);
ierr = VecDestroy(&b);CHKERRQ(ierr);
ierr = VecDestroy(&x);CHKERRQ(ierr);
ierr = MatDestroy(&C);CHKERRQ(ierr);
ierr = PetscFinalize();
return ierr;
}
示例8: main
PetscInt main(PetscInt argc,char **args)
{
typedef enum {RANDOM, CONSTANT, TANH, NUM_FUNCS} FuncType;
const char *funcNames[NUM_FUNCS] = {"random", "constant", "tanh"};
PetscMPIInt size;
PetscInt n = 10,N,Ny,ndim=4,dim[4],DIM,i;
Vec x,y,z;
PetscScalar s;
PetscRandom rdm;
PetscReal enorm;
PetscInt func=RANDOM;
FuncType function = RANDOM;
PetscBool view = PETSC_FALSE;
PetscErrorCode ierr;
PetscScalar *x_array,*y_array,*z_array;
fftw_plan fplan,bplan;
const ptrdiff_t N0 = 20, N1 = 20;
ptrdiff_t alloc_local, local_n0, local_0_start;
ierr = PetscInitialize(&argc,&args,(char *)0,help);CHKERRQ(ierr);
#if defined(PETSC_USE_COMPLEX)
SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP, "This example requires real numbers");
#endif
ierr = MPI_Comm_size(PETSC_COMM_WORLD, &size);CHKERRQ(ierr);
alloc_local=fftw_mpi_local_size_2d(N0, N1, PETSC_COMM_WORLD,
&local_n0, &local_0_start);
if (size != 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP, "This is a uniprocessor example only!");
ierr = PetscOptionsBegin(PETSC_COMM_WORLD, PETSC_NULL, "FFTW Options", "ex142");CHKERRQ(ierr);
ierr = PetscOptionsEList("-function", "Function type", "ex142", funcNames, NUM_FUNCS, funcNames[function], &func, PETSC_NULL);CHKERRQ(ierr);
ierr = PetscOptionsBool("-vec_view draw", "View the functions", "ex112", view, &view, PETSC_NULL);CHKERRQ(ierr);
function = (FuncType) func;
ierr = PetscOptionsEnd();CHKERRQ(ierr);
for (DIM = 0; DIM < ndim; DIM++){
dim[DIM] = n; /* size of real space vector in DIM-dimension */
}
ierr = PetscRandomCreate(PETSC_COMM_SELF, &rdm);CHKERRQ(ierr);
ierr = PetscRandomSetFromOptions(rdm);CHKERRQ(ierr);
for (DIM = 1; DIM < 5; DIM++){
/* create vectors of length N=dim[0]*dim[1]* ...*dim[DIM-1] */
/*----------------------------------------------------------*/
N = Ny = 1;
for (i = 0; i < DIM-1; i++) {
N *= dim[i];
}
Ny = N; Ny *= 2*(dim[DIM-1]/2 + 1); /* add padding elements to output vector y */
N *= dim[DIM-1];
ierr = PetscPrintf(PETSC_COMM_SELF, "\n %d-D: FFTW on vector of size %d \n",DIM,N);CHKERRQ(ierr);
ierr = VecCreateSeq(PETSC_COMM_SELF,N,&x);CHKERRQ(ierr);
ierr = PetscObjectSetName((PetscObject) x, "Real space vector");CHKERRQ(ierr);
ierr = VecCreateSeq(PETSC_COMM_SELF,Ny,&y);CHKERRQ(ierr);
ierr = PetscObjectSetName((PetscObject) y, "Frequency space vector");CHKERRQ(ierr);
ierr = VecDuplicate(x,&z);CHKERRQ(ierr);
ierr = PetscObjectSetName((PetscObject) z, "Reconstructed vector");CHKERRQ(ierr);
/* Set fftw plan */
/*----------------------------------*/
ierr = VecGetArray(x,&x_array);CHKERRQ(ierr);
ierr = VecGetArray(y,&y_array);CHKERRQ(ierr);
ierr = VecGetArray(z,&z_array);CHKERRQ(ierr);
unsigned int flags = FFTW_ESTIMATE; //or FFTW_MEASURE
/* The data in the in/out arrays is overwritten during FFTW_MEASURE planning, so such planning
should be done before the input is initialized by the user. */
printf("DIM: %d, N %d, Ny %d\n",DIM,N,Ny);
switch (DIM){
case 1:
fplan = fftw_plan_dft_r2c_1d(dim[0], (double *)x_array, (fftw_complex*)y_array, flags);
bplan = fftw_plan_dft_c2r_1d(dim[0], (fftw_complex*)y_array, (double *)z_array, flags);
break;
case 2:
fplan = fftw_plan_dft_r2c_2d(dim[0],dim[1],(double *)x_array, (fftw_complex*)y_array,flags);
bplan = fftw_plan_dft_c2r_2d(dim[0],dim[1],(fftw_complex*)y_array,(double *)z_array,flags);
break;
case 3:
fplan = fftw_plan_dft_r2c_3d(dim[0],dim[1],dim[2],(double *)x_array, (fftw_complex*)y_array,flags);
bplan = fftw_plan_dft_c2r_3d(dim[0],dim[1],dim[2],(fftw_complex*)y_array,(double *)z_array,flags);
break;
default:
fplan = fftw_plan_dft_r2c(DIM,dim,(double *)x_array, (fftw_complex*)y_array,flags);
bplan = fftw_plan_dft_c2r(DIM,dim,(fftw_complex*)y_array,(double *)z_array,flags);
break;
}
ierr = VecRestoreArray(x,&x_array);CHKERRQ(ierr);
ierr = VecRestoreArray(y,&y_array);CHKERRQ(ierr);
ierr = VecRestoreArray(z,&z_array);CHKERRQ(ierr);
/* Initialize Real space vector x:
The data in the in/out arrays is overwritten during FFTW_MEASURE planning, so planning
should be done before the input is initialized by the user.
--------------------------------------------------------*/
//.........这里部分代码省略.........
示例9: main
PetscInt main(PetscInt argc,char **args)
{
typedef enum {RANDOM, CONSTANT, TANH, NUM_FUNCS} FuncType;
const char *funcNames[NUM_FUNCS] = {"random", "constant", "tanh"};
Mat A;
PetscMPIInt size;
PetscInt n = 10,N,ndim=4,dim[4],DIM,i;
Vec x,y,z;
PetscScalar s;
PetscRandom rdm;
PetscReal enorm;
PetscInt func;
FuncType function = RANDOM;
PetscBool view = PETSC_FALSE;
PetscErrorCode ierr;
ierr = PetscInitialize(&argc,&args,(char *)0,help);CHKERRQ(ierr);
#if !defined(PETSC_USE_COMPLEX)
SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP, "This example requires complex numbers");
#endif
ierr = MPI_Comm_size(PETSC_COMM_WORLD, &size);CHKERRQ(ierr);
if (size != 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP, "This is a uniprocessor example only!");
ierr = PetscOptionsBegin(PETSC_COMM_WORLD, PETSC_NULL, "FFTW Options", "ex112");CHKERRQ(ierr);
ierr = PetscOptionsEList("-function", "Function type", "ex112", funcNames, NUM_FUNCS, funcNames[function], &func, PETSC_NULL);CHKERRQ(ierr);
ierr = PetscOptionsBool("-vec_view draw", "View the functions", "ex112", view, &view, PETSC_NULL);CHKERRQ(ierr);
function = (FuncType) func;
ierr = PetscOptionsEnd();CHKERRQ(ierr);
for (DIM = 0; DIM < ndim; DIM++){
dim[DIM] = n; /* size of transformation in DIM-dimension */
}
ierr = PetscRandomCreate(PETSC_COMM_SELF, &rdm);CHKERRQ(ierr);
ierr = PetscRandomSetFromOptions(rdm);CHKERRQ(ierr);
for (DIM = 1; DIM < 5; DIM++){
for (i = 0, N = 1; i < DIM; i++) N *= dim[i];
ierr = PetscPrintf(PETSC_COMM_SELF, "\n %d-D: FFTW on vector of size %d \n",DIM,N);CHKERRQ(ierr);
/* create FFTW object */
ierr = MatCreateFFT(PETSC_COMM_SELF,DIM,dim,MATFFTW,&A);CHKERRQ(ierr);
/* create vectors of length N=n^DIM */
ierr = MatGetVecs(A,&x,&y);CHKERRQ(ierr);
ierr = MatGetVecs(A,&z,PETSC_NULL);CHKERRQ(ierr);
ierr = PetscObjectSetName((PetscObject) x, "Real space vector");CHKERRQ(ierr);
ierr = PetscObjectSetName((PetscObject) y, "Frequency space vector");CHKERRQ(ierr);
ierr = PetscObjectSetName((PetscObject) z, "Reconstructed vector");CHKERRQ(ierr);
/* set values of space vector x */
if (function == RANDOM) {
ierr = VecSetRandom(x, rdm);CHKERRQ(ierr);
} else if (function == CONSTANT) {
ierr = VecSet(x, 1.0);CHKERRQ(ierr);
} else if (function == TANH) {
PetscScalar *a;
ierr = VecGetArray(x, &a);CHKERRQ(ierr);
for (i = 0; i < N; ++i) {
a[i] = tanh((i - N/2.0)*(10.0/N));
}
ierr = VecRestoreArray(x, &a);CHKERRQ(ierr);
}
if (view) {ierr = VecView(x, PETSC_VIEWER_DRAW_WORLD);CHKERRQ(ierr);}
/* apply FFTW_FORWARD and FFTW_BACKWARD several times on same x, y, and z */
for (i=0; i<3; i++){
ierr = MatMult(A,x,y);CHKERRQ(ierr);
if (view && i == 0) {ierr = VecView(y, PETSC_VIEWER_DRAW_WORLD);CHKERRQ(ierr);}
ierr = MatMultTranspose(A,y,z);CHKERRQ(ierr);
/* compare x and z. FFTW computes an unnormalized DFT, thus z = N*x */
s = 1.0/(PetscReal)N;
ierr = VecScale(z,s);CHKERRQ(ierr);
if (view && i == 0) {ierr = VecView(z, PETSC_VIEWER_DRAW_WORLD);CHKERRQ(ierr);}
ierr = VecAXPY(z,-1.0,x);CHKERRQ(ierr);
ierr = VecNorm(z,NORM_1,&enorm);CHKERRQ(ierr);
if (enorm > 1.e-11){
ierr = PetscPrintf(PETSC_COMM_SELF," Error norm of |x - z| %G\n",enorm);CHKERRQ(ierr);
}
}
/* apply FFTW_FORWARD and FFTW_BACKWARD several times on different x */
for (i=0; i<3; i++){
ierr = VecDestroy(&x);CHKERRQ(ierr);
ierr = VecCreateSeq(PETSC_COMM_SELF,N,&x);CHKERRQ(ierr);
ierr = VecSetRandom(x, rdm);CHKERRQ(ierr);
ierr = MatMult(A,x,y);CHKERRQ(ierr);
ierr = MatMultTranspose(A,y,z);CHKERRQ(ierr);
/* compare x and z. FFTW computes an unnormalized DFT, thus z = N*x */
s = 1.0/(PetscReal)N;
ierr = VecScale(z,s);CHKERRQ(ierr);
if (view && i == 0) {ierr = VecView(z, PETSC_VIEWER_DRAW_WORLD);CHKERRQ(ierr);}
ierr = VecAXPY(z,-1.0,x);CHKERRQ(ierr);
ierr = VecNorm(z,NORM_1,&enorm);CHKERRQ(ierr);
if (enorm > 1.e-11){
ierr = PetscPrintf(PETSC_COMM_SELF," Error norm of new |x - z| %G\n",enorm);CHKERRQ(ierr);
}
}
//.........这里部分代码省略.........
示例10: main
int main(int argc, char** args){
PetscErrorCode err;
PetscViewer fd = NULL;
Mat invL1 = NULL, invU1 = NULL, invL2 = NULL, invU2 = NULL, H12 = NULL, H21 = NULL;
Vec order = NULL, r = NULL; //, or = NULL; //dimension: n: n1 + n2
Vec seeds = NULL;
// Vec r1 = NULL, q1 = NULL, t1_1 = NULL, t1_2 = NULL, t1_3 = NULL, t1_4 = NULL, t1_5 = NULL; // dimension: n1
// Vec r2 = NULL, q2 = NULL, q_tilda = NULL, t2_1 = NULL, t2_2 = NULL, t2_3 = NULL; // dimension: n2
PetscRandom rand;
PetscLogDouble tic, toc, total_time, time;
PetscInt n, i;
PetscMPIInt rank, size;
PetscInt seed;
PetscScalar c, val;
PetscInt QN = 100;
// Initialize PETSC and MPI
err = PetscInitialize(&argc, &args, (char*) 0, help); CHKERRQ(err);
err = MPI_Comm_size(PETSC_COMM_WORLD, &size); CHKERRQ(err);
err = MPI_Comm_rank(PETSC_COMM_WORLD, &rank); CHKERRQ(err);
err = PetscPrintf(PETSC_COMM_WORLD, "mpi size: %d\n", size); CHKERRQ(err);
// Read matrices and an ordering vector
err = PetscPrintf(PETSC_COMM_WORLD, "Read inputs (invL1, invU1, invL2, invU2, H12, H21, order)\n"); CHKERRQ(err);
err = loadMat("./data/invL1.dat", &invL1, PETSC_COMM_WORLD, MATMPIAIJ, &fd); CHKERRQ(err);
err = checkMat("invL1", invL1); CHKERRQ(err);
err = loadMat("./data/invU1.dat", &invU1, PETSC_COMM_WORLD, MATMPIAIJ, &fd); CHKERRQ(err);
err = checkMat("invU1", invU1); CHKERRQ(err);
err = loadMat("./data/invL2.dat", &invL2, PETSC_COMM_WORLD, MATMPIAIJ, &fd); CHKERRQ(err);
err = checkMat("invL2", invL2); CHKERRQ(err);
err = loadMat("./data/invU2.dat", &invU2, PETSC_COMM_WORLD, MATMPIAIJ, &fd); CHKERRQ(err);
err = checkMat("invU2", invU2); CHKERRQ(err);
err = loadMat("./data/H12.dat", &H12, PETSC_COMM_WORLD, MATMPIAIJ, &fd); CHKERRQ(err);
err = checkMat("H12", H12); CHKERRQ(err);
err = loadMat("./data/H21.dat", &H21, PETSC_COMM_WORLD, MATMPIAIJ, &fd); CHKERRQ(err);
err = checkMat("H21", H21); CHKERRQ(err);
err = loadVec("./data/order.dat", &order, PETSC_COMM_SELF, &fd); CHKERRQ(err); //all processes must have this vector for ordering the result vector.
err = checkVec("order", order); CHKERRQ(err);
// shift -1 for zero-based index
err = VecShift(order, -1); CHKERRQ(err);
err = VecGetSize(order, &n); CHKERRQ(err);
seed = 5;
c = 0.05;
err = PetscTime(&tic); CHKERRQ(err);
//err = BearQueryMat(seed, c, invL1, invU1, invL2, invU2, H12, H21, order); CHKERRQ(err);
//err = PetscTime(&toc); CHKERRQ(err);
//time = toc - tic;
//err = PetscPrintf(PETSC_COMM_WORLD, "running time: %f sec\n", time); CHKERRQ(err);
///* 100 times querying
err = VecCreateSeq(PETSC_COMM_SELF, QN, &seeds); CHKERRQ(err);
err = VecSetFromOptions(seeds); CHKERRQ(err);
err = PetscRandomCreate(PETSC_COMM_WORLD, &rand); CHKERRQ(err);
err = PetscRandomSetSeed(rand, 100); CHKERRQ(err);
err = PetscRandomSetInterval(rand, (PetscScalar) 0, (PetscScalar) n); CHKERRQ(err);
err = PetscRandomSetFromOptions(rand); CHKERRQ(err);
err = VecSetRandom(seeds, rand); CHKERRQ(err);
err = PetscRandomDestroy(&rand); CHKERRQ(err);
seed = 5; //seed is give by user on one-based index
c = 0.05;
i = 0;
err = VecDuplicate(order, &r); CHKERRQ(err);
for(i = 0; i < QN; i++){
err = VecGetValues(seeds, 1, &i, &val);
seed = (PetscInt) val;
//err = PetscPrintf(PETSC_COMM_SELF, "rank: %d, seed: %d\n", rank, seed);
err = PetscTime(&tic); CHKERRQ(err);
err = BearQuery(seed, c, invL1, invU1, invL2, invU2, H12, H21, order, r); CHKERRQ(err);
err = PetscTime(&toc); CHKERRQ(err);
time = toc - tic;
err = PetscPrintf(PETSC_COMM_WORLD, "running time: %f sec\n", time); CHKERRQ(err);
total_time += time;
}
err = PetscPrintf(PETSC_COMM_WORLD, "average running time: %f sec\n", total_time/QN); CHKERRQ(err);
err = VecDestroy(&r);
/* err = MatGetSize(H12, &n1, &n2); CHKERRQ(err);
n = n1 + n2;
err = PetscPrintf(PETSC_COMM_WORLD, "n1: %d, n2: %d\n", n1, n2); CHKERRQ(err);
err = VecCreateMPI(PETSC_COMM_WORLD, PETSC_DECIDE, n, &r); CHKERRQ(err);
err = VecCreateMPI(PETSC_COMM_WORLD, PETSC_DECIDE, n1, &q1); CHKERRQ(err);
err = VecCreateMPI(PETSC_COMM_WORLD, PETSC_DECIDE, n2, &q2); CHKERRQ(err);
err = VecSet(q1, 0); CHKERRQ(err);
err = VecSet(q2, 0); CHKERRQ(err);
//.........这里部分代码省略.........
示例11: main
int main(int argc,char **argv)
{
Vec p;
PetscScalar *x_ptr;
PetscErrorCode ierr;
PetscMPIInt size;
AppCtx ctx;
Vec lowerb,upperb;
Tao tao;
TaoConvergedReason reason;
KSP ksp;
PC pc;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Initialize program
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
PetscInitialize(&argc,&argv,NULL,help);
PetscFunctionBeginUser;
ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
if (size != 1) SETERRQ(PETSC_COMM_SELF,1,"This is a uniprocessor example only!");
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Set runtime options
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"Swing equation options","");CHKERRQ(ierr);
{
ctx.beta = 2;
ctx.c = 10000.0;
ctx.u_s = 1.0;
ctx.omega_s = 1.0;
ctx.omega_b = 120.0*PETSC_PI;
ctx.H = 5.0;
ierr = PetscOptionsScalar("-Inertia","","",ctx.H,&ctx.H,NULL);CHKERRQ(ierr);
ctx.D = 5.0;
ierr = PetscOptionsScalar("-D","","",ctx.D,&ctx.D,NULL);CHKERRQ(ierr);
ctx.E = 1.1378;
ctx.V = 1.0;
ctx.X = 0.545;
ctx.Pmax = ctx.E*ctx.V/ctx.X;;
ierr = PetscOptionsScalar("-Pmax","","",ctx.Pmax,&ctx.Pmax,NULL);CHKERRQ(ierr);
ctx.Pm = 0.4;
ierr = PetscOptionsScalar("-Pm","","",ctx.Pm,&ctx.Pm,NULL);CHKERRQ(ierr);
ctx.tf = 0.1;
ctx.tcl = 0.2;
ierr = PetscOptionsReal("-tf","Time to start fault","",ctx.tf,&ctx.tf,NULL);CHKERRQ(ierr);
ierr = PetscOptionsReal("-tcl","Time to end fault","",ctx.tcl,&ctx.tcl,NULL);CHKERRQ(ierr);
}
ierr = PetscOptionsEnd();CHKERRQ(ierr);
/* Create TAO solver and set desired solution method */
ierr = TaoCreate(PETSC_COMM_WORLD,&tao);CHKERRQ(ierr);
ierr = TaoSetType(tao,TAOBLMVM);CHKERRQ(ierr);
/*
Optimization starts
*/
/* Set initial solution guess */
ierr = VecCreateSeq(PETSC_COMM_WORLD,1,&p);CHKERRQ(ierr);
ierr = VecGetArray(p,&x_ptr);CHKERRQ(ierr);
x_ptr[0] = ctx.Pm;
ierr = VecRestoreArray(p,&x_ptr);CHKERRQ(ierr);
ierr = TaoSetInitialVector(tao,p);CHKERRQ(ierr);
/* Set routine for function and gradient evaluation */
ierr = TaoSetObjectiveRoutine(tao,FormFunction,(void *)&ctx);CHKERRQ(ierr);
ierr = TaoSetGradientRoutine(tao,TaoDefaultComputeGradient,(void *)&ctx);CHKERRQ(ierr);
/* Set bounds for the optimization */
ierr = VecDuplicate(p,&lowerb);CHKERRQ(ierr);
ierr = VecDuplicate(p,&upperb);CHKERRQ(ierr);
ierr = VecGetArray(lowerb,&x_ptr);CHKERRQ(ierr);
x_ptr[0] = 0.;
ierr = VecRestoreArray(lowerb,&x_ptr);CHKERRQ(ierr);
ierr = VecGetArray(upperb,&x_ptr);CHKERRQ(ierr);
x_ptr[0] = 1.1;;
ierr = VecRestoreArray(upperb,&x_ptr);CHKERRQ(ierr);
ierr = TaoSetVariableBounds(tao,lowerb,upperb);
/* Check for any TAO command line options */
ierr = TaoSetFromOptions(tao);CHKERRQ(ierr);
ierr = TaoGetKSP(tao,&ksp);CHKERRQ(ierr);
if (ksp) {
ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
}
ierr = TaoSetTolerances(tao,1e-15,1e-15,1e-15,1e-15,1e-15);
/* SOLVE THE APPLICATION */
ierr = TaoSolve(tao); CHKERRQ(ierr);
/* Get information on termination */
ierr = TaoGetConvergedReason(tao,&reason);CHKERRQ(ierr);
if (reason <= 0){
ierr=PetscPrintf(MPI_COMM_WORLD, "Try another method! \n");CHKERRQ(ierr);
}
ierr = VecView(p,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
/* Free TAO data structures */
ierr = TaoDestroy(&tao);CHKERRQ(ierr);
//.........这里部分代码省略.........
示例12: main
int main(int argc,char **argv)
{
SNES snes; /* nonlinear solver context */
KSP ksp; /* linear solver context */
PC pc; /* preconditioner context */
Vec x,r; /* solution, residual vectors */
Mat J; /* Jacobian matrix */
PetscErrorCode ierr;
PetscInt its;
PetscMPIInt size,rank;
PetscScalar pfive = .5,*xx;
PetscBool flg;
AppCtx user; /* user-defined work context */
IS isglobal,islocal;
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 nonlinear solver context
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = SNESCreate(PETSC_COMM_WORLD,&snes);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Create matrix and vector data structures; set corresponding routines
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*
Create vectors for solution and nonlinear function
*/
ierr = VecCreate(PETSC_COMM_WORLD,&x);CHKERRQ(ierr);
ierr = VecSetSizes(x,PETSC_DECIDE,2);CHKERRQ(ierr);
ierr = VecSetFromOptions(x);CHKERRQ(ierr);
ierr = VecDuplicate(x,&r);CHKERRQ(ierr);
if (size > 1){
ierr = VecCreateSeq(PETSC_COMM_SELF,2,&user.xloc);CHKERRQ(ierr);
ierr = VecDuplicate(user.xloc,&user.rloc);CHKERRQ(ierr);
/* Create the scatter between the global x and local xloc */
ierr = ISCreateStride(MPI_COMM_SELF,2,0,1,&islocal);CHKERRQ(ierr);
ierr = ISCreateStride(MPI_COMM_SELF,2,0,1,&isglobal);CHKERRQ(ierr);
ierr = VecScatterCreate(x,isglobal,user.xloc,islocal,&user.scatter);CHKERRQ(ierr);
ierr = ISDestroy(&isglobal);CHKERRQ(ierr);
ierr = ISDestroy(&islocal);CHKERRQ(ierr);
}
/*
Create Jacobian matrix data structure
*/
ierr = MatCreate(PETSC_COMM_WORLD,&J);CHKERRQ(ierr);
ierr = MatSetSizes(J,PETSC_DECIDE,PETSC_DECIDE,2,2);CHKERRQ(ierr);
ierr = MatSetFromOptions(J);CHKERRQ(ierr);
ierr = MatSetUp(J);CHKERRQ(ierr);
ierr = PetscOptionsHasName(PETSC_NULL,"-hard",&flg);CHKERRQ(ierr);
if (!flg) {
/*
Set function evaluation routine and vector.
*/
ierr = SNESSetFunction(snes,r,FormFunction1,&user);CHKERRQ(ierr);
/*
Set Jacobian matrix data structure and Jacobian evaluation routine
*/
ierr = SNESSetJacobian(snes,J,J,FormJacobian1,PETSC_NULL);CHKERRQ(ierr);
} else {
if (size != 1) SETERRQ(PETSC_COMM_SELF,1,"This case is a uniprocessor example only!");
ierr = SNESSetFunction(snes,r,FormFunction2,PETSC_NULL);CHKERRQ(ierr);
ierr = SNESSetJacobian(snes,J,J,FormJacobian2,PETSC_NULL);CHKERRQ(ierr);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Customize nonlinear solver; set runtime options
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*
Set linear solver defaults for this problem. By extracting the
KSP, KSP, and PC contexts from the SNES context, we can then
directly call any KSP, KSP, and PC routines to set various options.
*/
ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
ierr = KSPSetTolerances(ksp,1.e-4,PETSC_DEFAULT,PETSC_DEFAULT,20);CHKERRQ(ierr);
/*
Set SNES/KSP/KSP/PC runtime options, e.g.,
-snes_view -snes_monitor -ksp_type <ksp> -pc_type <pc>
These options will override those specified above as long as
SNESSetFromOptions() is called _after_ any other customization
routines.
*/
ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Evaluate initial guess; then solve nonlinear system
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
if (!flg) {
ierr = VecSet(x,pfive);CHKERRQ(ierr);
} else {
//.........这里部分代码省略.........
示例13: main
//.........这里部分代码省略.........
tauArray[k][j][i] = sin(M_PI*newTime)*cos(2*M_PI*acx)*cos(2*M_PI*acy)*cos(2*M_PI*acz);
}
}
}
CHKERRQ( DAVecRestoreArray ( da, tauVec, &tauArray ) );
}
tau.push_back(tauVec);
}
fBasis.push_back(tau);
}
*/
// std::cout << "Finished setting basis" << std::endl;
/*
// Set initial velocity ...
CHKERRQ(DAVecGetArray(da, initialVelocity, &solArray));
for (int k = z; k < z + p ; k++) {
for (int j = y; j < y + n; j++) {
for (int i = x; i < x + m; i++) {
acx = (i)*hx; acy = (j)*hx; acz = (k)*hx;
solArray[k][j][i] = M_PI*cos(2*M_PI*acx)*cos(2*M_PI*acy)*cos(2*M_PI*acz);
}
}
}
CHKERRQ( DAVecRestoreArray ( da, initialVelocity, &solArray ) );
*/
std::vector<Vec> newF;
Vec alpha;
PetscScalar *avec;
VecCreateSeq(PETSC_COMM_SELF, numParams, &alpha);
/*
VecCreate(PETSC_COMM_WORLD, &alpha);
VecSetSizes(alpha, numParams, PETSC_DECIDE);
VecSetFromOptions(alpha);
*/
VecGetArray(alpha, &avec);
for (int j=0; j<numParams; j++)
avec[j] = 0.5 + 0.5*j;
VecRestoreArray(alpha, &avec);
// getForces(alpha, fBasis, newF);
getForces(alpha, newF, da, ti, numParams);
// Setup Matrices and Force Vector ...
Mass->setProblemDimensions(1.0, 1.0, 1.0);
Mass->setDA(da);
Mass->setDof(dof);
Mass->setNuVec(rho);
Stiffness->setProblemDimensions(1.0, 1.0, 1.0);
Stiffness->setDA(da);
Stiffness->setDof(dof);
Stiffness->setNuVec(nu);
Damping->setAlpha(0.0);
Damping->setBeta(0.00075);
Damping->setMassMatrix(Mass);
Damping->setStiffnessMatrix(Stiffness);
Damping->setDA(da);
示例14: MatMPIBAIJDiagonalScaleLocalSetUp
PetscErrorCode MatMPIBAIJDiagonalScaleLocalSetUp(Mat inA,Vec scale)
{
Mat_MPIBAIJ *ina = (Mat_MPIBAIJ*) inA->data; /*access private part of matrix */
Mat_SeqBAIJ *B = (Mat_SeqBAIJ*)ina->B->data;
PetscErrorCode ierr;
PetscInt bs = inA->rmap->bs,i,n,nt,j,cstart,cend,no,*garray = ina->garray,*lindices;
PetscInt *r_rmapd,*r_rmapo;
PetscFunctionBegin;
ierr = MatGetOwnershipRange(inA,&cstart,&cend);CHKERRQ(ierr);
ierr = MatGetSize(ina->A,NULL,&n);CHKERRQ(ierr);
ierr = PetscMalloc((inA->rmap->bmapping->n+1)*sizeof(PetscInt),&r_rmapd);CHKERRQ(ierr);
ierr = PetscMemzero(r_rmapd,inA->rmap->bmapping->n*sizeof(PetscInt));CHKERRQ(ierr);
nt = 0;
for (i=0; i<inA->rmap->bmapping->n; i++) {
if (inA->rmap->bmapping->indices[i]*bs >= cstart && inA->rmap->bmapping->indices[i]*bs < cend) {
nt++;
r_rmapd[i] = inA->rmap->bmapping->indices[i] + 1;
}
}
if (nt*bs != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Hmm nt*bs %D n %D",nt*bs,n);
ierr = PetscMalloc((n+1)*sizeof(PetscInt),&uglyrmapd);CHKERRQ(ierr);
for (i=0; i<inA->rmap->bmapping->n; i++) {
if (r_rmapd[i]) {
for (j=0; j<bs; j++) {
uglyrmapd[(r_rmapd[i]-1)*bs+j-cstart] = i*bs + j;
}
}
}
ierr = PetscFree(r_rmapd);CHKERRQ(ierr);
ierr = VecCreateSeq(PETSC_COMM_SELF,n,&uglydd);CHKERRQ(ierr);
ierr = PetscMalloc((ina->Nbs+1)*sizeof(PetscInt),&lindices);CHKERRQ(ierr);
ierr = PetscMemzero(lindices,ina->Nbs*sizeof(PetscInt));CHKERRQ(ierr);
for (i=0; i<B->nbs; i++) {
lindices[garray[i]] = i+1;
}
no = inA->rmap->bmapping->n - nt;
ierr = PetscMalloc((inA->rmap->bmapping->n+1)*sizeof(PetscInt),&r_rmapo);CHKERRQ(ierr);
ierr = PetscMemzero(r_rmapo,inA->rmap->bmapping->n*sizeof(PetscInt));CHKERRQ(ierr);
nt = 0;
for (i=0; i<inA->rmap->bmapping->n; i++) {
if (lindices[inA->rmap->bmapping->indices[i]]) {
nt++;
r_rmapo[i] = lindices[inA->rmap->bmapping->indices[i]];
}
}
if (nt > no) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Hmm nt %D no %D",nt,n);
ierr = PetscFree(lindices);CHKERRQ(ierr);
ierr = PetscMalloc((nt*bs+1)*sizeof(PetscInt),&uglyrmapo);CHKERRQ(ierr);
for (i=0; i<inA->rmap->bmapping->n; i++) {
if (r_rmapo[i]) {
for (j=0; j<bs; j++) {
uglyrmapo[(r_rmapo[i]-1)*bs+j] = i*bs + j;
}
}
}
ierr = PetscFree(r_rmapo);CHKERRQ(ierr);
ierr = VecCreateSeq(PETSC_COMM_SELF,nt*bs,&uglyoo);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例15: MatSetUpMultiply_MPIBAIJ
PetscErrorCode MatSetUpMultiply_MPIBAIJ(Mat mat)
{
Mat_MPIBAIJ *baij = (Mat_MPIBAIJ*)mat->data;
Mat_SeqBAIJ *B = (Mat_SeqBAIJ*)(baij->B->data);
PetscErrorCode ierr;
PetscInt i,j,*aj = B->j,ec = 0,*garray;
PetscInt bs = mat->rmap->bs,*stmp;
IS from,to;
Vec gvec;
#if defined(PETSC_USE_CTABLE)
PetscTable gid1_lid1;
PetscTablePosition tpos;
PetscInt gid,lid;
#else
PetscInt Nbs = baij->Nbs,*indices;
#endif
PetscFunctionBegin;
#if defined(PETSC_USE_CTABLE)
/* use a table - Mark Adams */
ierr = PetscTableCreate(B->mbs,baij->Nbs+1,&gid1_lid1);CHKERRQ(ierr);
for (i=0; i<B->mbs; i++) {
for (j=0; j<B->ilen[i]; j++) {
PetscInt data,gid1 = aj[B->i[i]+j] + 1;
ierr = PetscTableFind(gid1_lid1,gid1,&data);CHKERRQ(ierr);
if (!data) {
/* one based table */
ierr = PetscTableAdd(gid1_lid1,gid1,++ec,INSERT_VALUES);CHKERRQ(ierr);
}
}
}
/* form array of columns we need */
ierr = PetscMalloc((ec+1)*sizeof(PetscInt),&garray);CHKERRQ(ierr);
ierr = PetscTableGetHeadPosition(gid1_lid1,&tpos);CHKERRQ(ierr);
while (tpos) {
ierr = PetscTableGetNext(gid1_lid1,&tpos,&gid,&lid);CHKERRQ(ierr);
gid--; lid--;
garray[lid] = gid;
}
ierr = PetscSortInt(ec,garray);CHKERRQ(ierr);
ierr = PetscTableRemoveAll(gid1_lid1);CHKERRQ(ierr);
for (i=0; i<ec; i++) {
ierr = PetscTableAdd(gid1_lid1,garray[i]+1,i+1,INSERT_VALUES);CHKERRQ(ierr);
}
/* compact out the extra columns in B */
for (i=0; i<B->mbs; i++) {
for (j=0; j<B->ilen[i]; j++) {
PetscInt gid1 = aj[B->i[i] + j] + 1;
ierr = PetscTableFind(gid1_lid1,gid1,&lid);CHKERRQ(ierr);
lid--;
aj[B->i[i]+j] = lid;
}
}
B->nbs = ec;
baij->B->cmap->n = baij->B->cmap->N = ec*mat->rmap->bs;
ierr = PetscLayoutSetUp((baij->B->cmap));CHKERRQ(ierr);
ierr = PetscTableDestroy(&gid1_lid1);CHKERRQ(ierr);
#else
/* Make an array as long as the number of columns */
/* mark those columns that are in baij->B */
ierr = PetscMalloc((Nbs+1)*sizeof(PetscInt),&indices);CHKERRQ(ierr);
ierr = PetscMemzero(indices,Nbs*sizeof(PetscInt));CHKERRQ(ierr);
for (i=0; i<B->mbs; i++) {
for (j=0; j<B->ilen[i]; j++) {
if (!indices[aj[B->i[i] + j]]) ec++;
indices[aj[B->i[i] + j]] = 1;
}
}
/* form array of columns we need */
ierr = PetscMalloc((ec+1)*sizeof(PetscInt),&garray);CHKERRQ(ierr);
ec = 0;
for (i=0; i<Nbs; i++) {
if (indices[i]) {
garray[ec++] = i;
}
}
/* make indices now point into garray */
for (i=0; i<ec; i++) {
indices[garray[i]] = i;
}
/* compact out the extra columns in B */
for (i=0; i<B->mbs; i++) {
for (j=0; j<B->ilen[i]; j++) {
aj[B->i[i] + j] = indices[aj[B->i[i] + j]];
}
}
B->nbs = ec;
baij->B->cmap->n = baij->B->cmap->N = ec*mat->rmap->bs;
ierr = PetscLayoutSetUp((baij->B->cmap));CHKERRQ(ierr);
ierr = PetscFree(indices);CHKERRQ(ierr);
#endif
/* create local vector that is used to scatter into */
ierr = VecCreateSeq(PETSC_COMM_SELF,ec*bs,&baij->lvec);CHKERRQ(ierr);
//.........这里部分代码省略.........