本文整理汇总了C++中MatSetUp函数的典型用法代码示例。如果您正苦于以下问题:C++ MatSetUp函数的具体用法?C++ MatSetUp怎么用?C++ MatSetUp使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MatSetUp函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc,char **args)
{
const PetscInt inds[] = {0,1};
PetscScalar avals[] = {2,3,5,7};
Mat S;
User user;
PetscErrorCode ierr;
Vec base;
ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
ierr = PetscNew(&user);CHKERRQ(ierr);
ierr = MatCreateSeqAIJ(PETSC_COMM_WORLD,2,2,2,NULL,&user->B);CHKERRQ(ierr);
ierr = MatSetUp(user->B);CHKERRQ(ierr);
ierr = MatSetValues(user->B,2,inds,2,inds,avals,INSERT_VALUES);CHKERRQ(ierr);
ierr = MatAssemblyBegin(user->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(user->B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatCreateVecs(user->B,&base,NULL);CHKERRQ(ierr);
ierr = MatCreateShell(PETSC_COMM_WORLD,2,2,2,2,user,&S);CHKERRQ(ierr);
ierr = MatSetUp(S);CHKERRQ(ierr);
ierr = MatShellSetOperation(S,MATOP_MULT,(void (*)(void))MatMult_User);CHKERRQ(ierr);
ierr = MatShellSetOperation(S,MATOP_MULT_TRANSPOSE,(void (*)(void))MatMultTranspose_User);CHKERRQ(ierr);
ierr = MatShellTestMult(S,MyFunction,base,user,NULL);CHKERRQ(ierr);
ierr = MatShellTestMultTranspose(S,MyFunction,base,user,NULL);CHKERRQ(ierr);
ierr = VecDestroy(&base);CHKERRQ(ierr);
ierr = MatDestroy(&user->B);CHKERRQ(ierr);
ierr = MatDestroy(&S);CHKERRQ(ierr);
ierr = PetscFree(user);CHKERRQ(ierr);
ierr = PetscFinalize();
return ierr;
}
示例2: main
int main(int argc,char **args)
{
const PetscScalar xvals[] = {11,13},yvals[] = {17,19};
const PetscInt inds[] = {0,1};
PetscScalar avals[] = {2,3,5,7};
Mat S1,S2;
Vec X,Y;
User user;
PetscErrorCode ierr;
ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
ierr = PetscNew(&user);CHKERRQ(ierr);
ierr = MatCreateSeqAIJ(PETSC_COMM_WORLD,2,2,2,NULL,&user->A);CHKERRQ(ierr);
ierr = MatSetUp(user->A);CHKERRQ(ierr);
ierr = MatSetValues(user->A,2,inds,2,inds,avals,INSERT_VALUES);CHKERRQ(ierr);
ierr = MatAssemblyBegin(user->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(user->A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = VecCreateSeq(PETSC_COMM_WORLD,2,&X);CHKERRQ(ierr);
ierr = VecSetValues(X,2,inds,xvals,INSERT_VALUES);CHKERRQ(ierr);
ierr = VecAssemblyBegin(X);CHKERRQ(ierr);
ierr = VecAssemblyEnd(X);CHKERRQ(ierr);
ierr = VecDuplicate(X,&Y);CHKERRQ(ierr);
ierr = VecSetValues(Y,2,inds,yvals,INSERT_VALUES);CHKERRQ(ierr);
ierr = VecAssemblyBegin(Y);CHKERRQ(ierr);
ierr = VecAssemblyEnd(Y);CHKERRQ(ierr);
ierr = MatCreateShell(PETSC_COMM_WORLD,2,2,2,2,user,&S1);CHKERRQ(ierr);
ierr = MatSetUp(S1);CHKERRQ(ierr);
ierr = MatShellSetOperation(S1,MATOP_MULT,(void (*)(void))MatMult_User);CHKERRQ(ierr);
ierr = MatShellSetOperation(S1,MATOP_COPY,(void (*)(void))MatCopy_User);CHKERRQ(ierr);
ierr = MatShellSetOperation(S1,MATOP_DESTROY,(void (*)(void))MatDestroy_User);CHKERRQ(ierr);
ierr = MatCreateShell(PETSC_COMM_WORLD,2,2,2,2,NULL,&S2);CHKERRQ(ierr);
ierr = MatSetUp(S2);CHKERRQ(ierr);
ierr = MatShellSetOperation(S2,MATOP_MULT,(void (*)(void))MatMult_User);CHKERRQ(ierr);
ierr = MatShellSetOperation(S2,MATOP_COPY,(void (*)(void))MatCopy_User);CHKERRQ(ierr);
ierr = MatShellSetOperation(S2,MATOP_DESTROY,(void (*)(void))MatDestroy_User);CHKERRQ(ierr);
ierr = MatScale(S1,31);CHKERRQ(ierr);
ierr = MatShift(S1,37);CHKERRQ(ierr);
ierr = MatDiagonalScale(S1,X,Y);CHKERRQ(ierr);
ierr = MatCopy(S1,S2,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
ierr = MatMult(S1,X,Y);CHKERRQ(ierr);
ierr = VecView(Y,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
ierr = MatMult(S2,X,Y);CHKERRQ(ierr);
ierr = VecView(Y,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
ierr = MatDestroy(&S1);CHKERRQ(ierr);
ierr = MatDestroy(&S2);CHKERRQ(ierr);
ierr = VecDestroy(&X);CHKERRQ(ierr);
ierr = VecDestroy(&Y);CHKERRQ(ierr);
ierr = PetscFinalize();
return ierr;
}
示例3: main
int main(int argc,char **args)
{
Mat A1,A2,A3,A4,nest;
Mat mata[4];
Mat aij;
MPI_Comm comm;
PetscInt m,n,istart,iend,ii,i,J,j;
PetscScalar v;
PetscMPIInt size;
PetscErrorCode ierr;
ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
comm = PETSC_COMM_WORLD;
ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
/*
Assemble the matrix for the five point stencil, YET AGAIN
*/
ierr = MatCreate(comm,&A1);CHKERRQ(ierr);
m=2,n=2;
ierr = MatSetSizes(A1,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);CHKERRQ(ierr);
ierr = MatSetFromOptions(A1);CHKERRQ(ierr);
ierr = MatSetUp(A1);CHKERRQ(ierr);
ierr = MatGetOwnershipRange(A1,&istart,&iend);CHKERRQ(ierr);
for (ii=istart; ii<iend; ii++) {
v = -1.0; i = ii/n; j = ii - i*n;
if (i>0) {J = ii - n; ierr = MatSetValues(A1,1,&ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);}
if (i<m-1) {J = ii + n; ierr = MatSetValues(A1,1,&ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);}
if (j>0) {J = ii - 1; ierr = MatSetValues(A1,1,&ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);}
if (j<n-1) {J = ii + 1; ierr = MatSetValues(A1,1,&ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);}
v = 4.0; ierr = MatSetValues(A1,1,&ii,1,&ii,&v,INSERT_VALUES);CHKERRQ(ierr);
}
ierr = MatAssemblyBegin(A1,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(A1,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatView(A1,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
ierr = MatDuplicate(A1,MAT_COPY_VALUES,&A2);CHKERRQ(ierr);
ierr = MatDuplicate(A1,MAT_COPY_VALUES,&A3);CHKERRQ(ierr);
ierr = MatDuplicate(A1,MAT_COPY_VALUES,&A4);CHKERRQ(ierr);
/*create a nest matrix */
ierr = MatCreate(comm,&nest);CHKERRQ(ierr);
ierr = MatSetType(nest,MATNEST);CHKERRQ(ierr);
mata[0]=A1,mata[1]=A2,mata[2]=A3,mata[3]=A4;
ierr = MatNestSetSubMats(nest,2,NULL,2,NULL,mata);CHKERRQ(ierr);
ierr = MatSetUp(nest);CHKERRQ(ierr);
ierr = MatConvert(nest,MATAIJ,MAT_INITIAL_MATRIX,&aij);CHKERRQ(ierr);
ierr = MatView(aij,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
ierr = MatDestroy(&nest);CHKERRQ(ierr);
ierr = MatDestroy(&aij);CHKERRQ(ierr);
ierr = MatDestroy(&A1);CHKERRQ(ierr);
ierr = MatDestroy(&A2);CHKERRQ(ierr);
ierr = MatDestroy(&A3);CHKERRQ(ierr);
ierr = MatDestroy(&A4);CHKERRQ(ierr);
ierr = PetscFinalize();
return ierr;
}
示例4: main
int main(int argc,char **args)
{
PetscErrorCode ierr;
Mat A,B,C;
PetscBool different=PETSC_FALSE,skip=PETSC_FALSE;
PetscInt m0,m1,n=128,i;
PetscInitialize(&argc,&args,(char*)0,help);
ierr = PetscOptionsGetBool(NULL,"-different",&different,NULL);CHKERRQ(ierr);
ierr = PetscOptionsGetBool(NULL,"-skip",&skip,NULL);CHKERRQ(ierr);
/*
Create matrices
A = tridiag(1,-2,1) and B = diag(7);
*/
ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
ierr = MatCreate(PETSC_COMM_WORLD,&B);CHKERRQ(ierr);
ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);CHKERRQ(ierr);
ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,n,n);CHKERRQ(ierr);
ierr = MatSetFromOptions(A);CHKERRQ(ierr);
ierr = MatSetFromOptions(B);CHKERRQ(ierr);
ierr = MatSetUp(A);CHKERRQ(ierr);
ierr = MatSetUp(B);CHKERRQ(ierr);
ierr = MatGetOwnershipRange(A,&m0,&m1);CHKERRQ(ierr);
for (i=m0;i<m1;i++) {
if (i>0) { ierr = MatSetValue(A,i,i-1,-1.0,INSERT_VALUES);CHKERRQ(ierr); }
if (i<n-1) { ierr = MatSetValue(A,i,i+1,-1.0,INSERT_VALUES);CHKERRQ(ierr); }
ierr = MatSetValue(A,i,i,2.0,INSERT_VALUES);CHKERRQ(ierr);
ierr = MatSetValue(B,i,i,7.0,INSERT_VALUES);CHKERRQ(ierr);
}
ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatDuplicate(A,MAT_COPY_VALUES,&C);CHKERRQ(ierr);
/* Add B */
ierr = MatAXPY(C,1.0,B,(different)?DIFFERENT_NONZERO_PATTERN:SUBSET_NONZERO_PATTERN);CHKERRQ(ierr);
/* Add A */
if (!skip) { ierr = MatAXPY(C,1.0,A,SUBSET_NONZERO_PATTERN);CHKERRQ(ierr); }
/*
Free memory
*/
ierr = MatDestroy(&A);CHKERRQ(ierr);
ierr = MatDestroy(&B);CHKERRQ(ierr);
ierr = MatDestroy(&C);CHKERRQ(ierr);
ierr = PetscFinalize();
return 0;
}
示例5: MatGetFactor_seqaij_essl
PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_essl(Mat A,MatFactorType ftype,Mat *F)
{
Mat B;
PetscErrorCode ierr;
Mat_Essl *essl;
PetscFunctionBegin;
if (A->cmap->N != A->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"matrix must be square");
ierr = MatCreate(PetscObjectComm((PetscObject)A),&B);CHKERRQ(ierr);
ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,A->rmap->n,A->cmap->n);CHKERRQ(ierr);
ierr = PetscStrallocpy("essl",&((PetscObject)B)->type_name);CHKERRQ(ierr);
ierr = MatSetUp(B);CHKERRQ(ierr);
ierr = PetscNewLog(B,&essl);CHKERRQ(ierr);
B->data = essl;
B->ops->lufactorsymbolic = MatLUFactorSymbolic_Essl;
B->ops->destroy = MatDestroy_Essl;
B->ops->getinfo = MatGetInfo_External;
ierr = PetscObjectComposeFunction((PetscObject)B,"MatFactorGetSolverType_C",MatFactorGetSolverType_essl);CHKERRQ(ierr);
B->factortype = MAT_FACTOR_LU;
ierr = PetscFree(B->solvertype);CHKERRQ(ierr);
ierr = PetscStrallocpy(MATSOLVERESSL,&B->solvertype);CHKERRQ(ierr);
*F = B;
PetscFunctionReturn(0);
}
示例6: SparseGp_KGradient
int
SparseGp_KGradient (SparseGp *gp, HyperParam hp, int lengthInd)
{
PetscErrorCode ierr;
(void) ierr;
int N = gp->trainObs->size2;
/* create sparse K matrix */
ierr = MatCreate (PETSC_COMM_WORLD, &(gp->_KGradient)); CHKERRQ(ierr);
ierr = MatSetSizes (gp->_KGradient, gp->nlocal, gp->nlocal, N, N); CHKERRQ(ierr);
ierr = MatSetFromOptions(gp->_KGradient); CHKERRQ(ierr);
ierr = MatSetUp (gp->_KGradient); CHKERRQ(ierr);
/* assemble matrix */
switch (hp) {
case GP_HYPERPARAM_SIGMA0: {
petsc_util_fillSigma0GradMatFromKernel (gp->kern, gp->trainObs, &(gp->_KGradient), 0, N, gp->rstart, gp->rend);
break;
}
case GP_HYPERPARAM_LENGTH_I: {
petsc_util_fillLenghtGradMatFromKernel (gp->kern, gp->trainObs, &(gp->_KGradient), 0, N, gp->rstart, gp->rend, lengthInd);
break;
}
default:
printf ("Unknown hyperparameter type\n");
exit (EXIT_FAILURE);
}
ierr = MatAssemblyBegin(gp->_KGradient, MAT_FINAL_ASSEMBLY); CHKERRQ (ierr);
ierr = MatAssemblyEnd(gp->_KGradient, MAT_FINAL_ASSEMBLY); CHKERRQ (ierr);
return EXIT_SUCCESS;
}
示例7: MatGetFactor_seqaij_matlab
PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_matlab(Mat A,MatFactorType ftype,Mat *F)
{
PetscErrorCode ierr;
PetscFunctionBegin;
if (A->cmap->N != A->rmap->N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"matrix must be square");
ierr = MatCreate(PetscObjectComm((PetscObject)A),F);
CHKERRQ(ierr);
ierr = MatSetSizes(*F,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);
CHKERRQ(ierr);
ierr = PetscStrallocpy("matlab",&((PetscObject)*F)->type_name);
CHKERRQ(ierr);
ierr = MatSetUp(*F);
CHKERRQ(ierr);
(*F)->ops->destroy = MatDestroy_matlab;
(*F)->ops->getinfo = MatGetInfo_External;
(*F)->ops->lufactorsymbolic = MatLUFactorSymbolic_Matlab;
(*F)->ops->ilufactorsymbolic = MatLUFactorSymbolic_Matlab;
ierr = PetscObjectComposeFunction((PetscObject)(*F),"MatFactorGetSolverPackage_C",MatFactorGetSolverPackage_seqaij_matlab);
CHKERRQ(ierr);
(*F)->factortype = ftype;
ierr = PetscFree((*F)->solvertype);
CHKERRQ(ierr);
ierr = PetscStrallocpy(MATSOLVERMATLAB,&(*F)->solvertype);
CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例8: PCSetUp_Eisenstat
static PetscErrorCode PCSetUp_Eisenstat(PC pc)
{
PetscErrorCode ierr;
PetscInt M,N,m,n;
PC_Eisenstat *eis = (PC_Eisenstat*)pc->data;
PetscFunctionBegin;
if (!pc->setupcalled) {
ierr = MatGetSize(pc->mat,&M,&N);CHKERRQ(ierr);
ierr = MatGetLocalSize(pc->mat,&m,&n);CHKERRQ(ierr);
ierr = MatCreate(((PetscObject)pc)->comm,&eis->shell);CHKERRQ(ierr);
ierr = MatSetSizes(eis->shell,m,n,M,N);CHKERRQ(ierr);
ierr = MatSetType(eis->shell,MATSHELL);CHKERRQ(ierr);
ierr = MatSetUp(eis->shell);CHKERRQ(ierr);
ierr = MatShellSetContext(eis->shell,(void*)pc);CHKERRQ(ierr);
ierr = PetscLogObjectParent(pc,eis->shell);CHKERRQ(ierr);
ierr = MatShellSetOperation(eis->shell,MATOP_MULT,(void(*)(void))PCMult_Eisenstat);CHKERRQ(ierr);
}
if (!eis->usediag) PetscFunctionReturn(0);
if (!pc->setupcalled) {
ierr = MatGetVecs(pc->pmat,&eis->diag,0);CHKERRQ(ierr);
ierr = PetscLogObjectParent(pc,eis->diag);CHKERRQ(ierr);
}
ierr = MatGetDiagonal(pc->pmat,eis->diag);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例9: ComputeRHSMatrix
PetscErrorCode ComputeRHSMatrix(PetscInt m,PetscInt nrhs,Mat* C)
{
PetscErrorCode ierr;
PetscRandom rand;
Mat RHS;
PetscScalar *array,rval;
PetscInt i,k;
PetscFunctionBegin;
ierr = MatCreate(PETSC_COMM_WORLD,&RHS);CHKERRQ(ierr);
ierr = MatSetSizes(RHS,m,PETSC_DECIDE,PETSC_DECIDE,nrhs);CHKERRQ(ierr);
ierr = MatSetType(RHS,MATSEQDENSE);CHKERRQ(ierr);
ierr = MatSetUp(RHS);CHKERRQ(ierr);
ierr = PetscRandomCreate(PETSC_COMM_WORLD,&rand);CHKERRQ(ierr);
ierr = PetscRandomSetFromOptions(rand);CHKERRQ(ierr);
ierr = MatDenseGetArray(RHS,&array);CHKERRQ(ierr);
for (i=0; i<m; i++){
ierr = PetscRandomGetValue(rand,&rval);CHKERRQ(ierr);
array[i] = rval;
}
if (nrhs > 1){
for (k=1; k<nrhs; k++){
for (i=0; i<m; i++){
array[m*k+i] = array[i];
}
}
}
ierr = MatDenseRestoreArray(RHS,&array);CHKERRQ(ierr);
ierr = MatAssemblyBegin(RHS,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(RHS,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
*C = RHS;
ierr = PetscRandomDestroy(&rand);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例10: main
int main(int argc, char **argv)
{
Mat A;
KSP ksp;
DM shell;
Vec *left, *right;
MPI_Comm c;
PetscErrorCode ierr;
ierr = PetscInitialize(&argc, &argv, NULL, NULL);if (ierr) return ierr;
c = PETSC_COMM_WORLD;
ierr = MatCreate(c, &A); CHKERRQ(ierr);
ierr = MatSetSizes(A, 1, 1, PETSC_DECIDE, PETSC_DECIDE); CHKERRQ(ierr);
ierr = MatSetFromOptions(A); CHKERRQ(ierr);
ierr = MatSetUp(A); CHKERRQ(ierr);
ierr = KSPCreate(c, &ksp); CHKERRQ(ierr);
ierr = KSPSetOperators(ksp, A, A); CHKERRQ(ierr);
ierr = KSPSetFromOptions(ksp); CHKERRQ(ierr);
ierr = DMShellCreate(c, &shell); CHKERRQ(ierr);
ierr = DMSetFromOptions(shell); CHKERRQ(ierr);
ierr = DMSetUp(shell); CHKERRQ(ierr);
ierr = KSPSetDM(ksp, shell); CHKERRQ(ierr);
ierr = KSPCreateVecs(ksp, 1, &right, 1, &left); CHKERRQ(ierr);
ierr = VecView(right[0], PETSC_VIEWER_STDOUT_(c));CHKERRQ(ierr);
ierr = VecDestroyVecs(1,&right); CHKERRQ(ierr);
ierr = VecDestroyVecs(1,&left); CHKERRQ(ierr);
ierr = DMDestroy(&shell); CHKERRQ(ierr);
ierr = KSPDestroy(&ksp); CHKERRQ(ierr);
ierr = MatDestroy(&A); CHKERRQ(ierr);
PetscFinalize();
return 0;
}
示例11: KSPSetUp_TSIRM
static PetscErrorCode KSPSetUp_TSIRM(KSP ksp)
{
PetscErrorCode ierr;
KSP_TSIRM *tsirm = (KSP_TSIRM*)ksp->data;
PetscFunctionBegin;
/* Initialization */
tsirm->tol_ls = 1e-40;
tsirm->size_ls = 12;
tsirm->maxiter_ls = 15;
tsirm->cgls = 0;
/* Matrix of the system */
ierr = KSPGetOperators(ksp,&tsirm->A,NULL);
CHKERRQ(ierr); /* Matrix of the system */
ierr = MatGetSize(tsirm->A,&tsirm->size,NULL);
CHKERRQ(ierr); /* Size of the system */
ierr = MatGetOwnershipRange(tsirm->A,&tsirm->Istart,&tsirm->Iend);
CHKERRQ(ierr);
/* Matrix S of residuals */
ierr = MatCreate(PETSC_COMM_WORLD,&tsirm->S);
CHKERRQ(ierr);
ierr = MatSetSizes(tsirm->S,tsirm->Iend-tsirm->Istart,PETSC_DECIDE,tsirm->size,tsirm->size_ls);
CHKERRQ(ierr);
ierr = MatSetType(tsirm->S,MATDENSE);
CHKERRQ(ierr);
ierr = MatSetUp(tsirm->S);
CHKERRQ(ierr);
/* Residual and vector Alpha computed in the minimization step */
ierr = MatCreateVecs(tsirm->S,&tsirm->Alpha,&tsirm->r);
CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例12: the
/*@
MatCreateTranspose - Creates a new matrix object that behaves like A'
Collective on Mat
Input Parameter:
. A - the (possibly rectangular) matrix
Output Parameter:
. N - the matrix that represents A'
Level: intermediate
Notes:
The transpose A' is NOT actually formed! Rather the new matrix
object performs the matrix-vector product by using the MatMultTranspose() on
the original matrix
.seealso: MatCreateNormal(), MatMult(), MatMultTranspose(), MatCreate()
@*/
PetscErrorCode MatCreateTranspose(Mat A,Mat *N)
{
PetscErrorCode ierr;
PetscInt m,n;
Mat_Transpose *Na;
PetscFunctionBegin;
ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr);
ierr = MatCreate(PetscObjectComm((PetscObject)A),N);CHKERRQ(ierr);
ierr = MatSetSizes(*N,n,m,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr);
ierr = PetscLayoutSetUp((*N)->rmap);CHKERRQ(ierr);
ierr = PetscLayoutSetUp((*N)->cmap);CHKERRQ(ierr);
ierr = PetscObjectChangeTypeName((PetscObject)*N,MATTRANSPOSEMAT);CHKERRQ(ierr);
ierr = PetscNewLog(*N,&Na);CHKERRQ(ierr);
(*N)->data = (void*) Na;
ierr = PetscObjectReference((PetscObject)A);CHKERRQ(ierr);
Na->A = A;
(*N)->ops->destroy = MatDestroy_Transpose;
(*N)->ops->mult = MatMult_Transpose;
(*N)->ops->multadd = MatMultAdd_Transpose;
(*N)->ops->multtranspose = MatMultTranspose_Transpose;
(*N)->ops->multtransposeadd = MatMultTransposeAdd_Transpose;
(*N)->ops->duplicate = MatDuplicate_Transpose;
(*N)->ops->getvecs = MatCreateVecs_Transpose;
(*N)->ops->axpy = MatAXPY_Transpose;
(*N)->assembled = PETSC_TRUE;
ierr = PetscObjectComposeFunction((PetscObject)(*N),"MatTransposeGetMat_C",MatTransposeGetMat_Transpose);CHKERRQ(ierr);
ierr = MatSetBlockSizes(*N,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));CHKERRQ(ierr);
ierr = MatSetUp(*N);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
示例13: main
int main(int argc,char **argv)
{
PetscErrorCode ierr;
PetscInt time_steps = 100,steps;
PetscMPIInt size;
Vec global;
PetscReal dt,ftime;
TS ts;
MatStructure A_structure;
Mat A = 0;
ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr);
ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
ierr = PetscOptionsGetInt(NULL,"-time",&time_steps,NULL);CHKERRQ(ierr);
/* set initial conditions */
ierr = VecCreate(PETSC_COMM_WORLD,&global);CHKERRQ(ierr);
ierr = VecSetSizes(global,PETSC_DECIDE,3);CHKERRQ(ierr);
ierr = VecSetFromOptions(global);CHKERRQ(ierr);
ierr = Initial(global,NULL);CHKERRQ(ierr);
/* make timestep context */
ierr = TSCreate(PETSC_COMM_WORLD,&ts);CHKERRQ(ierr);
ierr = TSSetProblemType(ts,TS_NONLINEAR);CHKERRQ(ierr);
ierr = TSMonitorSet(ts,Monitor,NULL,NULL);CHKERRQ(ierr);
dt = 0.1;
/*
The user provides the RHS and Jacobian
*/
ierr = TSSetRHSFunction(ts,NULL,RHSFunction,NULL);CHKERRQ(ierr);
ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,3,3);CHKERRQ(ierr);
ierr = MatSetFromOptions(A);CHKERRQ(ierr);
ierr = MatSetUp(A);CHKERRQ(ierr);
ierr = RHSJacobian(ts,0.0,global,&A,&A,&A_structure,NULL);CHKERRQ(ierr);
ierr = TSSetRHSJacobian(ts,A,A,RHSJacobian,NULL);CHKERRQ(ierr);
ierr = TSSetFromOptions(ts);CHKERRQ(ierr);
ierr = TSSetInitialTimeStep(ts,0.0,dt);CHKERRQ(ierr);
ierr = TSSetDuration(ts,time_steps,1);CHKERRQ(ierr);
ierr = TSSetSolution(ts,global);CHKERRQ(ierr);
ierr = TSSolve(ts,global);CHKERRQ(ierr);
ierr = TSGetSolveTime(ts,&ftime);CHKERRQ(ierr);
ierr = TSGetTimeStepNumber(ts,&steps);CHKERRQ(ierr);
/* free the memories */
ierr = TSDestroy(&ts);CHKERRQ(ierr);
ierr = VecDestroy(&global);CHKERRQ(ierr);
ierr = MatDestroy(&A);CHKERRQ(ierr);
ierr = PetscFinalize();
return 0;
}
示例14: main
int main(int argc, char **argv)
{
Mat mat;
MatNullSpace nsp;
PetscBool prefix = PETSC_FALSE, flg;
PetscErrorCode ierr;
PetscInt zero = 0;
PetscScalar value = 0;
ierr = PetscInitialize(&argc, &argv, NULL, help); if (ierr) return ierr;
ierr = PetscOptionsGetBool(NULL, NULL, "-with_prefix",&prefix,NULL);CHKERRQ(ierr);
ierr = MatCreateDense(PETSC_COMM_WORLD, 1, 1, 1, 1, NULL, &mat);CHKERRQ(ierr);
ierr = MatSetOptionsPrefix(mat, prefix ? "prefix_" : NULL);CHKERRQ(ierr);
ierr = MatSetUp(mat);CHKERRQ(ierr);
ierr = MatSetValues(mat, 1, &zero, 1, &zero, &value, INSERT_VALUES);CHKERRQ(ierr);
ierr = MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatNullSpaceCreate(PETSC_COMM_WORLD, PETSC_TRUE, 0, NULL, &nsp);CHKERRQ(ierr);
ierr = MatNullSpaceTest(nsp, mat, &flg);CHKERRQ(ierr);
if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Null space test failed!");
ierr = MatNullSpaceDestroy(&nsp);CHKERRQ(ierr);
ierr = MatDestroy(&mat);CHKERRQ(ierr);
ierr = PetscFinalize();
return ierr;
}
示例15: DMCreateMatrix_Shell
static PetscErrorCode DMCreateMatrix_Shell(DM dm,Mat *J)
{
PetscErrorCode ierr;
DM_Shell *shell = (DM_Shell*)dm->data;
Mat A;
PetscFunctionBegin;
PetscValidHeaderSpecific(dm,DM_CLASSID,1);
PetscValidPointer(J,3);
if (!shell->A) {
if (shell->Xglobal) {
PetscInt m,M;
ierr = PetscInfo(dm,"Naively creating matrix using global vector distribution without preallocation\n");
CHKERRQ(ierr);
ierr = VecGetSize(shell->Xglobal,&M);
CHKERRQ(ierr);
ierr = VecGetLocalSize(shell->Xglobal,&m);
CHKERRQ(ierr);
ierr = MatCreate(PetscObjectComm((PetscObject)dm),&shell->A);
CHKERRQ(ierr);
ierr = MatSetSizes(shell->A,m,m,M,M);
CHKERRQ(ierr);
ierr = MatSetType(shell->A,dm->mattype);
CHKERRQ(ierr);
ierr = MatSetUp(shell->A);
CHKERRQ(ierr);
} else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Must call DMShellSetMatrix(), DMShellSetCreateMatrix(), or provide a vector");
}
A = shell->A;
/* the check below is tacky and incomplete */
if (dm->mattype) {
PetscBool flg,aij,seqaij,mpiaij;
ierr = PetscObjectTypeCompare((PetscObject)A,dm->mattype,&flg);
CHKERRQ(ierr);
ierr = PetscObjectTypeCompare((PetscObject)A,MATSEQAIJ,&seqaij);
CHKERRQ(ierr);
ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&mpiaij);
CHKERRQ(ierr);
ierr = PetscStrcmp(dm->mattype,MATAIJ,&aij);
CHKERRQ(ierr);
if (!flg) {
if (!(aij && (seqaij || mpiaij))) SETERRQ2(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_NOTSAMETYPE,"Requested matrix of type %s, but only %s available",dm->mattype,((PetscObject)A)->type_name);
}
}
if (((PetscObject)A)->refct < 2) { /* We have an exclusive reference so we can give it out */
ierr = PetscObjectReference((PetscObject)A);
CHKERRQ(ierr);
ierr = MatZeroEntries(A);
CHKERRQ(ierr);
*J = A;
} else { /* Need to create a copy, could use MAT_SHARE_NONZERO_PATTERN in most cases */
ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,J);
CHKERRQ(ierr);
ierr = MatZeroEntries(*J);
CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}