1 #define PETSCMAT_DLL 2 3 /* 4 Routines to project vectors out of null spaces. 5 */ 6 7 #include "src/mat/matimpl.h" /*I "petscmat.h" I*/ 8 #include "petscsys.h" 9 10 PetscCookie PETSCMAT_DLLEXPORT MAT_NULLSPACE_COOKIE = 0; 11 12 #undef __FUNCT__ 13 #define __FUNCT__ "MatNullSpaceCreate" 14 /*@ 15 MatNullSpaceCreate - Creates a data structure used to project vectors 16 out of null spaces. 17 18 Collective on MPI_Comm 19 20 Input Parameters: 21 + comm - the MPI communicator associated with the object 22 . has_cnst - PETSC_TRUE if the null space contains the constant vector; otherwise 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. These vectors are NOT copied, so do not change them 26 after this call. You should free the array that you pass in. 27 28 Output Parameter: 29 . SP - the null space context 30 31 Level: advanced 32 33 Users manual sections: 34 . sec_singular 35 36 .keywords: PC, null space, create 37 38 .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), KSPSetNullSpace(), MatNullSpace 39 @*/ 40 PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceCreate(MPI_Comm comm,PetscTruth has_cnst,PetscInt n,const Vec vecs[],MatNullSpace *SP) 41 { 42 MatNullSpace sp; 43 PetscErrorCode ierr; 44 PetscInt i; 45 46 PetscFunctionBegin; 47 if (n) PetscValidPointer(vecs,4); 48 for (i=0; i<n; i++) PetscValidHeaderSpecific(vecs[i],VEC_COOKIE,4); 49 PetscValidPointer(SP,5); 50 51 *SP = PETSC_NULL; 52 #ifndef PETSC_USE_DYNAMIC_LIBRARIES 53 ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr); 54 #endif 55 56 ierr = PetscHeaderCreate(sp,_p_MatNullSpace,int,MAT_NULLSPACE_COOKIE,0,"MatNullSpace",comm,MatNullSpaceDestroy,0);CHKERRQ(ierr); 57 ierr = PetscLogObjectMemory(sp,sizeof(struct _p_MatNullSpace));CHKERRQ(ierr); 58 59 sp->has_cnst = has_cnst; 60 sp->n = n; 61 sp->vec = PETSC_NULL; 62 if (n) { 63 ierr = PetscMalloc(n*sizeof(Vec),&sp->vecs);CHKERRQ(ierr); 64 for (i=0; i<n; i++) sp->vecs[i] = vecs[i]; 65 } else { 66 sp->vecs = 0; 67 } 68 69 for (i=0; i<n; i++) { 70 ierr = PetscObjectReference((PetscObject)sp->vecs[i]);CHKERRQ(ierr); 71 } 72 *SP = sp; 73 PetscFunctionReturn(0); 74 } 75 76 #undef __FUNCT__ 77 #define __FUNCT__ "MatNullSpaceDestroy" 78 /*@ 79 MatNullSpaceDestroy - Destroys a data structure used to project vectors 80 out of null spaces. 81 82 Collective on MatNullSpace 83 84 Input Parameter: 85 . sp - the null space context to be destroyed 86 87 Level: advanced 88 89 .keywords: PC, null space, destroy 90 91 .seealso: MatNullSpaceCreate(), MatNullSpaceRemove() 92 @*/ 93 PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceDestroy(MatNullSpace sp) 94 { 95 PetscErrorCode ierr; 96 97 PetscFunctionBegin; 98 if (--sp->refct > 0) PetscFunctionReturn(0); 99 100 if (sp->vec) {ierr = VecDestroy(sp->vec);CHKERRQ(ierr);} 101 if (sp->vecs) { 102 ierr = VecDestroyVecs(sp->vecs,sp->n);CHKERRQ(ierr); 103 } 104 ierr = PetscHeaderDestroy(sp);CHKERRQ(ierr); 105 PetscFunctionReturn(0); 106 } 107 108 #undef __FUNCT__ 109 #define __FUNCT__ "MatNullSpaceRemove" 110 /*@ 111 MatNullSpaceRemove - Removes all the components of a null space from a vector. 112 113 Collective on MatNullSpace 114 115 Input Parameters: 116 + sp - the null space context 117 . vec - the vector from which the null space is to be removed 118 - out - if this is requested (not PETSC_NULL) then this is a vector with the null space removed otherwise 119 the removal is done in-place (in vec) 120 121 Note: The user is not responsible for the vector returned and should not destroy it. 122 123 Level: advanced 124 125 .keywords: PC, null space, remove 126 127 .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy() 128 @*/ 129 PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceRemove(MatNullSpace sp,Vec vec,Vec *out) 130 { 131 PetscScalar sum; 132 PetscErrorCode ierr; 133 PetscInt j,n = sp->n,N; 134 Vec l = vec; 135 136 PetscFunctionBegin; 137 PetscValidHeaderSpecific(sp,MAT_NULLSPACE_COOKIE,1); 138 PetscValidHeaderSpecific(vec,VEC_COOKIE,2); 139 140 if (out) { 141 PetscValidPointer(out,3); 142 if (!sp->vec) { 143 ierr = VecDuplicate(vec,&sp->vec);CHKERRQ(ierr); 144 } 145 *out = sp->vec; 146 ierr = VecCopy(vec,*out);CHKERRQ(ierr); 147 l = *out; 148 } 149 150 if (sp->has_cnst) { 151 ierr = VecSum(l,&sum);CHKERRQ(ierr); 152 ierr = VecGetSize(l,&N);CHKERRQ(ierr); 153 sum = sum/(-1.0*N); 154 ierr = VecShift(l,sum);CHKERRQ(ierr); 155 } 156 157 for (j=0; j<n; j++) { 158 ierr = VecDot(l,sp->vecs[j],&sum);CHKERRQ(ierr); 159 sum = -sum; 160 ierr = VecAXPY(l,sum,sp->vecs[j]);CHKERRQ(ierr); 161 } 162 163 PetscFunctionReturn(0); 164 } 165 166 #undef __FUNCT__ 167 #define __FUNCT__ "MatNullSpaceTest" 168 /*@ 169 MatNullSpaceTest - Tests if the claimed null space is really a 170 null space of a matrix 171 172 Collective on MatNullSpace 173 174 Input Parameters: 175 + sp - the null space context 176 - mat - the matrix 177 178 Level: advanced 179 180 .keywords: PC, null space, remove 181 182 .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy() 183 @*/ 184 PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceTest(MatNullSpace sp,Mat mat) 185 { 186 PetscScalar sum; 187 PetscReal nrm; 188 PetscInt j,n = sp->n,N,m; 189 PetscErrorCode ierr; 190 Vec l,r; 191 MPI_Comm comm = sp->comm; 192 PetscTruth flg1,flg2; 193 194 PetscFunctionBegin; 195 ierr = PetscOptionsHasName(PETSC_NULL,"-mat_null_space_test_view",&flg1);CHKERRQ(ierr); 196 ierr = PetscOptionsHasName(PETSC_NULL,"-mat_null_space_test_view_draw",&flg2);CHKERRQ(ierr); 197 198 if (!sp->vec) { 199 if (n) { 200 ierr = VecDuplicate(sp->vecs[0],&sp->vec);CHKERRQ(ierr); 201 } else { 202 ierr = MatGetLocalSize(mat,&m,PETSC_NULL);CHKERRQ(ierr); 203 ierr = VecCreateMPI(sp->comm,m,PETSC_DETERMINE,&sp->vec);CHKERRQ(ierr); 204 } 205 } 206 l = sp->vec; 207 208 if (sp->has_cnst) { 209 ierr = VecDuplicate(l,&r);CHKERRQ(ierr); 210 ierr = VecGetSize(l,&N);CHKERRQ(ierr); 211 sum = 1.0/N; 212 ierr = VecSet(l,sum);CHKERRQ(ierr); 213 ierr = MatMult(mat,l,r);CHKERRQ(ierr); 214 ierr = VecNorm(r,NORM_2,&nrm);CHKERRQ(ierr); 215 if (nrm < 1.e-7) {ierr = PetscPrintf(comm,"Constants are likely null vector");CHKERRQ(ierr);} 216 else {ierr = PetscPrintf(comm,"Constants are unlikely null vector ");CHKERRQ(ierr);} 217 ierr = PetscPrintf(comm,"|| A * 1 || = %g\n",nrm);CHKERRQ(ierr); 218 if (nrm > 1.e-7 && flg1) {ierr = VecView(r,PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr);} 219 if (nrm > 1.e-7 && flg2) {ierr = VecView(r,PETSC_VIEWER_DRAW_(comm));CHKERRQ(ierr);} 220 ierr = VecDestroy(r);CHKERRQ(ierr); 221 } 222 223 for (j=0; j<n; j++) { 224 ierr = (*mat->ops->mult)(mat,sp->vecs[j],l);CHKERRQ(ierr); 225 ierr = VecNorm(l,NORM_2,&nrm);CHKERRQ(ierr); 226 if (nrm < 1.e-7) {ierr = PetscPrintf(comm,"Null vector %D is likely null vector",j);CHKERRQ(ierr);} 227 else {ierr = PetscPrintf(comm,"Null vector %D unlikely null vector ",j);CHKERRQ(ierr);} 228 ierr = PetscPrintf(comm,"|| A * v[%D] || = %g\n",j,nrm);CHKERRQ(ierr); 229 if (nrm > 1.e-7 && flg1) {ierr = VecView(l,PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr);} 230 if (nrm > 1.e-7 && flg2) {ierr = VecView(l,PETSC_VIEWER_DRAW_(comm));CHKERRQ(ierr);} 231 } 232 233 PetscFunctionReturn(0); 234 } 235 236