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