1be1d678aSKris Buschelman 2f7765cecSBarry Smith /* 3b4fd4287SBarry Smith Routines to project vectors out of null spaces. 4f7765cecSBarry Smith */ 5f7765cecSBarry Smith 6c6db04a5SJed Brown #include <private/matimpl.h> /*I "petscmat.h" I*/ 7f7765cecSBarry Smith 87087cfbeSBarry Smith PetscClassId MAT_NULLSPACE_CLASSID; 98ba1e511SMatthew Knepley 104a2ae208SSatish Balay #undef __FUNCT__ 1172875594SBarry Smith #define __FUNCT__ "MatNullSpaceSetFunction" 1272875594SBarry Smith /*@C 1372875594SBarry Smith MatNullSpaceSetFunction - set a function that removes a null space from a vector 1472875594SBarry Smith out of null spaces. 1572875594SBarry Smith 163f9fe445SBarry Smith Logically Collective on MatNullSpace 1772875594SBarry Smith 1872875594SBarry Smith Input Parameters: 1972875594SBarry Smith + sp - the null space object 209dbe9a8aSBarry Smith . rem - the function that removes the null space 219dbe9a8aSBarry Smith - ctx - context for the remove function 2272875594SBarry Smith 23658c74aaSSatish Balay Level: advanced 2472875594SBarry Smith 25658c74aaSSatish Balay .keywords: PC, null space, create 26b47fd4b1SSatish Balay 2772875594SBarry Smith .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), KSPSetNullSpace(), MatNullSpace, MatNullSpaceCreate() 2872875594SBarry Smith @*/ 297087cfbeSBarry Smith PetscErrorCode MatNullSpaceSetFunction(MatNullSpace sp, PetscErrorCode (*rem)(MatNullSpace,Vec,void*),void *ctx) 3072875594SBarry Smith { 3172875594SBarry Smith PetscFunctionBegin; 320700a824SBarry Smith PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1); 339dbe9a8aSBarry Smith sp->remove = rem; 349dbe9a8aSBarry Smith sp->rmctx = ctx; 3572875594SBarry Smith PetscFunctionReturn(0); 3672875594SBarry Smith } 3772875594SBarry Smith 3872875594SBarry Smith #undef __FUNCT__ 39f7357b39SLisandro Dalcin #define __FUNCT__ "MatNullSpaceView" 40*b717e993SJed Brown /*@C 41*b717e993SJed Brown MatNullSpaceView - Visualizes a null space object. 42*b717e993SJed Brown 43*b717e993SJed Brown Collective on MatNullSpace 44*b717e993SJed Brown 45*b717e993SJed Brown Input Parameters: 46*b717e993SJed Brown + matnull - the null space 47*b717e993SJed Brown - viewer - visualization context 48*b717e993SJed Brown 49*b717e993SJed Brown Level: advanced 50*b717e993SJed Brown 51*b717e993SJed Brown Fortran Note: 52*b717e993SJed Brown This routine is not supported in Fortran. 53*b717e993SJed Brown 54*b717e993SJed Brown .seealso: MatNullSpaceCreate(), PetscViewerASCIIOpen() 55*b717e993SJed Brown @*/ 56*b717e993SJed Brown PetscErrorCode MatNullSpaceView(MatNullSpace sp,PetscViewer viewer) 57f7357b39SLisandro Dalcin { 58f7357b39SLisandro Dalcin PetscErrorCode ierr; 59f7357b39SLisandro Dalcin PetscBool iascii; 60f7357b39SLisandro Dalcin 61f7357b39SLisandro Dalcin PetscFunctionBegin; 62f7357b39SLisandro Dalcin PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1); 63f7357b39SLisandro Dalcin if (!viewer) viewer = PETSC_VIEWER_STDOUT_(((PetscObject)sp)->comm); 64f7357b39SLisandro Dalcin PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 65f7357b39SLisandro Dalcin PetscCheckSameComm(sp,1,viewer,2); 66f7357b39SLisandro Dalcin 67f7357b39SLisandro Dalcin ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 68f7357b39SLisandro Dalcin if (iascii) { 69f7357b39SLisandro Dalcin ierr = PetscObjectPrintClassNamePrefixType((PetscObject)sp,viewer,"MatNullSpace Object");CHKERRQ(ierr); 70f7357b39SLisandro Dalcin } 71f7357b39SLisandro Dalcin PetscFunctionReturn(0); 72f7357b39SLisandro Dalcin } 73f7357b39SLisandro Dalcin 74f7357b39SLisandro Dalcin #undef __FUNCT__ 754a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceCreate" 76f39d8e23SSatish Balay /*@ 775cfeda75SBarry Smith MatNullSpaceCreate - Creates a data structure used to project vectors 78b4fd4287SBarry Smith out of null spaces. 79f7765cecSBarry Smith 804e472627SLois Curfman McInnes Collective on MPI_Comm 814e472627SLois Curfman McInnes 82f7765cecSBarry Smith Input Parameters: 8383c3bef8SLois Curfman McInnes + comm - the MPI communicator associated with the object 8483c3bef8SLois Curfman McInnes . has_cnst - PETSC_TRUE if the null space contains the constant vector; otherwise PETSC_FALSE 85b4fd4287SBarry Smith . n - number of vectors (excluding constant vector) in null space 8683c3bef8SLois Curfman McInnes - vecs - the vectors that span the null space (excluding the constant vector); 87f7a9e4ceSBarry Smith these vectors must be orthonormal. These vectors are NOT copied, so do not change them 8873141a14SBarry Smith after this call. You should free the array that you pass in and destroy the vectors (this will reduce the reference count 8973141a14SBarry Smith for them by one). 90f7765cecSBarry Smith 91f7765cecSBarry Smith Output Parameter: 92b4fd4287SBarry Smith . SP - the null space context 93f7765cecSBarry Smith 9483c3bef8SLois Curfman McInnes Level: advanced 9583c3bef8SLois Curfman McInnes 9680bf1014SBarry Smith Notes: See MatNullSpaceSetFunction() as an alternative way of providing the null space information instead of setting vecs. 9780bf1014SBarry Smith 9880bf1014SBarry Smith If has_cnst is PETSC_TRUE you do not need to pass a constant vector in as a fourth argument to this routine, nor do you 9980bf1014SBarry Smith need to pass in a function that eliminates the constant function into MatNullSpaceSetFunction(). 10080bf1014SBarry Smith 1016e1639daSBarry Smith Users manual sections: 1026e1639daSBarry Smith . sec_singular 1036e1639daSBarry Smith 10483c3bef8SLois Curfman McInnes .keywords: PC, null space, create 10541a59933SSatish Balay 10672875594SBarry Smith .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), KSPSetNullSpace(), MatNullSpace, MatNullSpaceSetFunction() 107f7765cecSBarry Smith @*/ 1087087cfbeSBarry Smith PetscErrorCode MatNullSpaceCreate(MPI_Comm comm,PetscBool has_cnst,PetscInt n,const Vec vecs[],MatNullSpace *SP) 109f7765cecSBarry Smith { 1105cfeda75SBarry Smith MatNullSpace sp; 111dfbe8321SBarry Smith PetscErrorCode ierr; 112c1ac3661SBarry Smith PetscInt i; 113f7765cecSBarry Smith 1143a40ed3dSBarry Smith PetscFunctionBegin; 115e32f2f54SBarry Smith if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of vectors (given %D) cannot be negative",n); 116574b3360SMatthew Knepley if (n) PetscValidPointer(vecs,4); 1170700a824SBarry Smith for (i=0; i<n; i++) PetscValidHeaderSpecific(vecs[i],VEC_CLASSID,4); 118574b3360SMatthew Knepley PetscValidPointer(SP,5); 119574b3360SMatthew Knepley 120574b3360SMatthew Knepley *SP = PETSC_NULL; 121574b3360SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 122574b3360SMatthew Knepley ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr); 123574b3360SMatthew Knepley #endif 124574b3360SMatthew Knepley 1255b9de0c1SLisandro Dalcin ierr = PetscHeaderCreate(sp,_p_MatNullSpace,int,MAT_NULLSPACE_CLASSID,0,"MatNullSpace",comm,MatNullSpaceDestroy,MatNullSpaceView);CHKERRQ(ierr); 126f7765cecSBarry Smith 127b4fd4287SBarry Smith sp->has_cnst = has_cnst; 128b4fd4287SBarry Smith sp->n = n; 1297850f3fbSLisandro Dalcin sp->vecs = 0; 1307850f3fbSLisandro Dalcin sp->alpha = 0; 1317850f3fbSLisandro Dalcin sp->vec = 0; 1327850f3fbSLisandro Dalcin sp->remove = 0; 1337850f3fbSLisandro Dalcin sp->rmctx = 0; 1347850f3fbSLisandro Dalcin 135f7a9e4ceSBarry Smith if (n) { 136f7a9e4ceSBarry Smith ierr = PetscMalloc(n*sizeof(Vec),&sp->vecs);CHKERRQ(ierr); 1377850f3fbSLisandro Dalcin ierr = PetscMalloc(n*sizeof(PetscScalar),&sp->alpha);CHKERRQ(ierr); 1387850f3fbSLisandro Dalcin ierr = PetscLogObjectMemory(sp,n*(sizeof(Vec)+sizeof(PetscScalar)));CHKERRQ(ierr); 1397850f3fbSLisandro Dalcin for (i=0; i<n; i++) { 1407850f3fbSLisandro Dalcin ierr = PetscObjectReference((PetscObject)vecs[i]);CHKERRQ(ierr); 1417850f3fbSLisandro Dalcin sp->vecs[i] = vecs[i]; 1427850f3fbSLisandro Dalcin } 143f7a9e4ceSBarry Smith } 144b4fd4287SBarry Smith 145b4fd4287SBarry Smith *SP = sp; 1463a40ed3dSBarry Smith PetscFunctionReturn(0); 147f7765cecSBarry Smith } 148f7765cecSBarry Smith 1494a2ae208SSatish Balay #undef __FUNCT__ 1504a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceDestroy" 151f7765cecSBarry Smith /*@ 1525cfeda75SBarry Smith MatNullSpaceDestroy - Destroys a data structure used to project vectors 153b4fd4287SBarry Smith out of null spaces. 154b4fd4287SBarry Smith 1555cfeda75SBarry Smith Collective on MatNullSpace 1564e472627SLois Curfman McInnes 157b4fd4287SBarry Smith Input Parameter: 158b9756687SLois Curfman McInnes . sp - the null space context to be destroyed 159b9756687SLois Curfman McInnes 160b9756687SLois Curfman McInnes Level: advanced 161b4fd4287SBarry Smith 16283c3bef8SLois Curfman McInnes .keywords: PC, null space, destroy 16341a59933SSatish Balay 16472875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceRemove(), MatNullSpaceSetFunction() 165b4fd4287SBarry Smith @*/ 166d34fcf5fSBarry Smith PetscErrorCode MatNullSpaceDestroy(MatNullSpace *sp) 167b4fd4287SBarry Smith { 168dfbe8321SBarry Smith PetscErrorCode ierr; 16985614651SBarry Smith 1705cfeda75SBarry Smith PetscFunctionBegin; 1716bf464f9SBarry Smith if (!*sp) PetscFunctionReturn(0); 172d34fcf5fSBarry Smith PetscValidHeaderSpecific((*sp),MAT_NULLSPACE_CLASSID,1); 173d34fcf5fSBarry Smith if (--((PetscObject)(*sp))->refct > 0) {*sp = 0; PetscFunctionReturn(0);} 17485614651SBarry Smith 1756bf464f9SBarry Smith ierr = VecDestroy(&(*sp)->vec);CHKERRQ(ierr); 1766bf464f9SBarry Smith ierr = VecDestroyVecs((*sp)->n,&(*sp)->vecs);CHKERRQ(ierr); 177d34fcf5fSBarry Smith ierr = PetscFree((*sp)->alpha);CHKERRQ(ierr); 1786bf464f9SBarry Smith ierr = PetscHeaderDestroy(sp);CHKERRQ(ierr); 1793a40ed3dSBarry Smith PetscFunctionReturn(0); 180b4fd4287SBarry Smith } 181b4fd4287SBarry Smith 1824a2ae208SSatish Balay #undef __FUNCT__ 1834a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceRemove" 184812c3f48SMatthew Knepley /*@C 1855cfeda75SBarry Smith MatNullSpaceRemove - Removes all the components of a null space from a vector. 186f7765cecSBarry Smith 1875cfeda75SBarry Smith Collective on MatNullSpace 188f7765cecSBarry Smith 1894e472627SLois Curfman McInnes Input Parameters: 1904e472627SLois Curfman McInnes + sp - the null space context 1914e7234bfSBarry Smith . vec - the vector from which the null space is to be removed 1925fcf39f4SBarry Smith - out - if this is requested (not PETSC_NULL) then this is a vector with the null space removed otherwise 1934e7234bfSBarry Smith the removal is done in-place (in vec) 1944e7234bfSBarry Smith 195db090513SMatthew Knepley Note: The user is not responsible for the vector returned and should not destroy it. 1964e472627SLois Curfman McInnes 197b9756687SLois Curfman McInnes Level: advanced 198b9756687SLois Curfman McInnes 19983c3bef8SLois Curfman McInnes .keywords: PC, null space, remove 20041a59933SSatish Balay 20172875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction() 202f7765cecSBarry Smith @*/ 2037087cfbeSBarry Smith PetscErrorCode MatNullSpaceRemove(MatNullSpace sp,Vec vec,Vec *out) 204f7765cecSBarry Smith { 20587828ca2SBarry Smith PetscScalar sum; 2067850f3fbSLisandro Dalcin PetscInt i,N; 2076849ba73SBarry Smith PetscErrorCode ierr; 208f7765cecSBarry Smith 2093a40ed3dSBarry Smith PetscFunctionBegin; 2100700a824SBarry Smith PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1); 2110700a824SBarry Smith PetscValidHeaderSpecific(vec,VEC_CLASSID,2); 2123cd8ff7eSMatthew Knepley 2135cfeda75SBarry Smith if (out) { 2143cd8ff7eSMatthew Knepley PetscValidPointer(out,3); 2155cfeda75SBarry Smith if (!sp->vec) { 2165cfeda75SBarry Smith ierr = VecDuplicate(vec,&sp->vec);CHKERRQ(ierr); 2177850f3fbSLisandro Dalcin ierr = PetscLogObjectParent(sp,sp->vec);CHKERRQ(ierr); 2185cfeda75SBarry Smith } 2197850f3fbSLisandro Dalcin ierr = VecCopy(vec,sp->vec);CHKERRQ(ierr); 2207850f3fbSLisandro Dalcin vec = *out = sp->vec; 2215cfeda75SBarry Smith } 2225cfeda75SBarry Smith 223b4fd4287SBarry Smith if (sp->has_cnst) { 2247850f3fbSLisandro Dalcin ierr = VecGetSize(vec,&N);CHKERRQ(ierr); 2257850f3fbSLisandro Dalcin if (N > 0) { 2267850f3fbSLisandro Dalcin ierr = VecSum(vec,&sum);CHKERRQ(ierr); 22718a7d68fSSatish Balay sum = sum/(-1.0*N); 2287850f3fbSLisandro Dalcin ierr = VecShift(vec,sum);CHKERRQ(ierr); 2297850f3fbSLisandro Dalcin } 230f7765cecSBarry Smith } 231b4fd4287SBarry Smith 2327850f3fbSLisandro Dalcin if (sp->n) { 2337850f3fbSLisandro Dalcin ierr = VecMDot(vec,sp->n,sp->vecs,sp->alpha);CHKERRQ(ierr); 2347850f3fbSLisandro Dalcin for (i=0; i<sp->n; i++) sp->alpha[i] = -sp->alpha[i]; 2357850f3fbSLisandro Dalcin ierr = VecMAXPY(vec,sp->n,sp->alpha,sp->vecs);CHKERRQ(ierr); 236f7765cecSBarry Smith } 237b4fd4287SBarry Smith 23872875594SBarry Smith if (sp->remove){ 2390c3c4d68SMatthew Knepley ierr = (*sp->remove)(sp,vec,sp->rmctx);CHKERRQ(ierr); 24072875594SBarry Smith } 2413a40ed3dSBarry Smith PetscFunctionReturn(0); 242f7765cecSBarry Smith } 243a2e34c3dSBarry Smith 2444a2ae208SSatish Balay #undef __FUNCT__ 2454a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceTest" 246a2e34c3dSBarry Smith /*@ 247a2e34c3dSBarry Smith MatNullSpaceTest - Tests if the claimed null space is really a 248a2e34c3dSBarry Smith null space of a matrix 249a2e34c3dSBarry Smith 250a2e34c3dSBarry Smith Collective on MatNullSpace 251a2e34c3dSBarry Smith 252a2e34c3dSBarry Smith Input Parameters: 253a2e34c3dSBarry Smith + sp - the null space context 254a2e34c3dSBarry Smith - mat - the matrix 255a2e34c3dSBarry Smith 25695902228SMatthew Knepley Output Parameters: 25795902228SMatthew Knepley . isNull - PETSC_TRUE if the nullspace is valid for this matrix 25895902228SMatthew Knepley 259a2e34c3dSBarry Smith Level: advanced 260a2e34c3dSBarry Smith 261a2e34c3dSBarry Smith .keywords: PC, null space, remove 262a2e34c3dSBarry Smith 26372875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction() 264a2e34c3dSBarry Smith @*/ 2657087cfbeSBarry Smith PetscErrorCode MatNullSpaceTest(MatNullSpace sp,Mat mat,PetscBool *isNull) 266a2e34c3dSBarry Smith { 26787828ca2SBarry Smith PetscScalar sum; 2688bb6bcc5SSatish Balay PetscReal nrm; 2690b12b109SJed Brown PetscInt j,n,N; 2706849ba73SBarry Smith PetscErrorCode ierr; 271a2e34c3dSBarry Smith Vec l,r; 272ace3abfcSBarry Smith PetscBool flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,consistent = PETSC_TRUE; 2733050cee2SBarry Smith PetscViewer viewer; 274a2e34c3dSBarry Smith 275a2e34c3dSBarry Smith PetscFunctionBegin; 2760700a824SBarry Smith PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1); 2770700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 2783cfa8680SLisandro Dalcin n = sp->n; 279acfcf0e5SJed Brown ierr = PetscOptionsGetBool(PETSC_NULL,"-mat_null_space_test_view",&flg1,PETSC_NULL);CHKERRQ(ierr); 280acfcf0e5SJed Brown ierr = PetscOptionsGetBool(PETSC_NULL,"-mat_null_space_test_view_draw",&flg2,PETSC_NULL);CHKERRQ(ierr); 281a2e34c3dSBarry Smith 282a2e34c3dSBarry Smith if (!sp->vec) { 283a2e34c3dSBarry Smith if (n) { 284a2e34c3dSBarry Smith ierr = VecDuplicate(sp->vecs[0],&sp->vec);CHKERRQ(ierr); 285a2e34c3dSBarry Smith } else { 2860b12b109SJed Brown ierr = MatGetVecs(mat,&sp->vec,PETSC_NULL);CHKERRQ(ierr); 287a2e34c3dSBarry Smith } 288a2e34c3dSBarry Smith } 289a2e34c3dSBarry Smith l = sp->vec; 290a2e34c3dSBarry Smith 2917adad957SLisandro Dalcin ierr = PetscViewerASCIIGetStdout(((PetscObject)sp)->comm,&viewer);CHKERRQ(ierr); 292a2e34c3dSBarry Smith if (sp->has_cnst) { 293a2e34c3dSBarry Smith ierr = VecDuplicate(l,&r);CHKERRQ(ierr); 294a2e34c3dSBarry Smith ierr = VecGetSize(l,&N);CHKERRQ(ierr); 295a2e34c3dSBarry Smith sum = 1.0/N; 2962dcb1b2aSMatthew Knepley ierr = VecSet(l,sum);CHKERRQ(ierr); 297a2e34c3dSBarry Smith ierr = MatMult(mat,l,r);CHKERRQ(ierr); 2988bb6bcc5SSatish Balay ierr = VecNorm(r,NORM_2,&nrm);CHKERRQ(ierr); 29995902228SMatthew Knepley if (nrm < 1.e-7) { 30095902228SMatthew Knepley ierr = PetscPrintf(((PetscObject)sp)->comm,"Constants are likely null vector");CHKERRQ(ierr); 30195902228SMatthew Knepley } else { 30295902228SMatthew Knepley ierr = PetscPrintf(((PetscObject)sp)->comm,"Constants are unlikely null vector ");CHKERRQ(ierr); 30395902228SMatthew Knepley consistent = PETSC_FALSE; 30495902228SMatthew Knepley } 30531980aa1SBarry Smith ierr = PetscPrintf(((PetscObject)sp)->comm,"|| A * 1/N || = %G\n",nrm);CHKERRQ(ierr); 3063050cee2SBarry Smith if (nrm > 1.e-7 && flg1) {ierr = VecView(r,viewer);CHKERRQ(ierr);} 3073050cee2SBarry Smith if (nrm > 1.e-7 && flg2) {ierr = VecView(r,viewer);CHKERRQ(ierr);} 3086bf464f9SBarry Smith ierr = VecDestroy(&r);CHKERRQ(ierr); 309a2e34c3dSBarry Smith } 310a2e34c3dSBarry Smith 311a2e34c3dSBarry Smith for (j=0; j<n; j++) { 312a2e34c3dSBarry Smith ierr = (*mat->ops->mult)(mat,sp->vecs[j],l);CHKERRQ(ierr); 3138bb6bcc5SSatish Balay ierr = VecNorm(l,NORM_2,&nrm);CHKERRQ(ierr); 31495902228SMatthew Knepley if (nrm < 1.e-7) { 31595902228SMatthew Knepley ierr = PetscPrintf(((PetscObject)sp)->comm,"Null vector %D is likely null vector",j);CHKERRQ(ierr); 31695902228SMatthew Knepley } else { 31795902228SMatthew Knepley ierr = PetscPrintf(((PetscObject)sp)->comm,"Null vector %D unlikely null vector ",j);CHKERRQ(ierr); 31895902228SMatthew Knepley consistent = PETSC_FALSE; 31995902228SMatthew Knepley } 3207adad957SLisandro Dalcin ierr = PetscPrintf(((PetscObject)sp)->comm,"|| A * v[%D] || = %G\n",j,nrm);CHKERRQ(ierr); 3213050cee2SBarry Smith if (nrm > 1.e-7 && flg1) {ierr = VecView(l,viewer);CHKERRQ(ierr);} 3223050cee2SBarry Smith if (nrm > 1.e-7 && flg2) {ierr = VecView(l,viewer);CHKERRQ(ierr);} 323a2e34c3dSBarry Smith } 324a2e34c3dSBarry Smith 325ac53348aSBarry Smith if (sp->remove) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Cannot test a null space provided as a function with MatNullSpaceSetFunction()"); 32631980aa1SBarry Smith if (isNull) *isNull = consistent; 327a2e34c3dSBarry Smith PetscFunctionReturn(0); 328a2e34c3dSBarry Smith } 329a2e34c3dSBarry Smith 330