1 #ifdef PETSC_RCS_HEADER 2 static char vcid[] = "$Id: pcnull.c,v 1.24 1999/05/04 20:33:53 balay Exp bsmith $"; 3 #endif 4 /* 5 Routines to project vectors out of null spaces. 6 */ 7 8 #include "petsc.h" 9 #include "src/sles/pc/pcimpl.h" /*I "pc.h" I*/ 10 #include "sys.h" 11 12 13 #undef __FUNC__ 14 #define __FUNC__ "PCNullSpaceCreate" 15 /*@C 16 PCNullSpaceCreate - Creates a data-structure used to project vectors 17 out of null spaces. 18 19 Collective on MPI_Comm 20 21 Input Parameters: 22 + comm - the MPI communicator associated with the object 23 . has_cnst - PETSC_TRUE if the null space contains the constant vector; otherwise PETSC_FALSE 24 . n - number of vectors (excluding constant vector) in null space 25 - vecs - the vectors that span the null space (excluding the constant vector); 26 these vectors must be orthonormal 27 28 Output Parameter: 29 . SP - the null space context 30 31 Level: advanced 32 33 .keywords: PC, null space, create 34 35 .seealso: PCNullSpaceDestroy(), PCNullSpaceRemove() 36 @*/ 37 int PCNullSpaceCreate(MPI_Comm comm, int has_cnst, int n, Vec *vecs,PCNullSpace *SP) 38 { 39 PCNullSpace sp; 40 41 PetscFunctionBegin; 42 PetscHeaderCreate(sp,_p_PCNullSpace,int,PCNULLSPACE_COOKIE,0,"PCNullSpace",comm,PCNullSpaceDestroy,0); 43 PLogObjectCreate(sp); 44 PLogObjectMemory(sp,sizeof(struct _p_PCNullSpace)); 45 46 sp->has_cnst = has_cnst; 47 sp->n = n; 48 sp->vecs = vecs; 49 50 *SP = sp; 51 PetscFunctionReturn(0); 52 } 53 54 #undef __FUNC__ 55 #define __FUNC__ "PCNullSpaceDestroy" 56 /*@ 57 PCNullSpaceDestroy - Destroys a data-structure used to project vectors 58 out of null spaces. 59 60 Collective on PCNullSpace 61 62 Input Parameter: 63 . sp - the null space context to be destroyed 64 65 Level: advanced 66 67 .keywords: PC, null space, destroy 68 69 .seealso: PCNullSpaceDestroy(), PCNullSpaceRemove() 70 @*/ 71 int PCNullSpaceDestroy(PCNullSpace sp) 72 { 73 PetscFunctionBegin; 74 75 if (--sp->refct > 0) PetscFunctionReturn(0); 76 77 PLogObjectDestroy(sp); 78 PetscHeaderDestroy(sp); 79 PetscFunctionReturn(0); 80 } 81 82 #undef __FUNC__ 83 #define __FUNC__ "PCNullSpaceRemove" 84 /*@ 85 PCNullSpaceRemove - Removes all the components of a null space from a vector. 86 87 Collective on PCNullSpace 88 89 Input Parameters: 90 + sp - the null space context 91 - vec - the vector from which the null space is to be removed 92 93 Level: advanced 94 95 .keywords: PC, null space, remove 96 97 .seealso: PCNullSpaceCreate(), PCNullSpaceDestroy() 98 @*/ 99 int PCNullSpaceRemove(PCNullSpace sp,Vec vec) 100 { 101 Scalar sum; 102 int j, n = sp->n, N,ierr; 103 104 PetscFunctionBegin; 105 if (sp->has_cnst) { 106 ierr = VecSum(vec,&sum);CHKERRQ(ierr); 107 ierr = VecGetSize(vec,&N);CHKERRQ(ierr); 108 sum = sum/(-1.0*N); 109 ierr = VecShift(&sum,vec);CHKERRQ(ierr); 110 } 111 112 for ( j=0; j<n; j++ ) { 113 ierr = VecDot(vec,sp->vecs[j],&sum);CHKERRQ(ierr); 114 sum = -sum; 115 ierr = VecAXPY(&sum,sp->vecs[j],vec);CHKERRQ(ierr); 116 } 117 118 PetscFunctionReturn(0); 119 } 120