1be1d678aSKris Buschelman 2f7765cecSBarry Smith /* 3b4fd4287SBarry Smith Routines to project vectors out of null spaces. 4f7765cecSBarry Smith */ 5f7765cecSBarry Smith 6af0996ceSBarry Smith #include <petsc/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 275fa7ec2dSBarry Smith .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), MatSetNullSpace(), 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__ 39009ec7a5SJed Brown #define __FUNCT__ "MatNullSpaceGetVecs" 40009ec7a5SJed Brown /*@C 41009ec7a5SJed Brown MatNullSpaceGetVecs - get vectors defining the null space 42009ec7a5SJed Brown 43009ec7a5SJed Brown Not Collective 44009ec7a5SJed Brown 45009ec7a5SJed Brown Input Arguments: 46009ec7a5SJed Brown . sp - null space object 47009ec7a5SJed Brown 48009ec7a5SJed Brown Output Arguments: 49009ec7a5SJed Brown + has_cnst - PETSC_TRUE if the null space contains the constant vector, otherwise PETSC_FALSE 50009ec7a5SJed Brown . n - number of vectors (excluding constant vector) in null space 51009ec7a5SJed Brown - vecs - orthonormal vectors that span the null space (excluding the constant vector) 52009ec7a5SJed Brown 53009ec7a5SJed Brown Level: developer 54009ec7a5SJed Brown 552a7a6963SBarry Smith Notes: 562a7a6963SBarry Smith These vectors and the array are owned by the MatNullSpace and should not be destroyed or freeded by the caller 572a7a6963SBarry Smith 58009ec7a5SJed Brown .seealso: MatNullSpaceCreate(), MatGetNullSpace(), MatGetNearNullSpace() 59009ec7a5SJed Brown @*/ 60009ec7a5SJed Brown PetscErrorCode MatNullSpaceGetVecs(MatNullSpace sp,PetscBool *has_const,PetscInt *n,const Vec **vecs) 61009ec7a5SJed Brown { 62009ec7a5SJed Brown 63009ec7a5SJed Brown PetscFunctionBegin; 64009ec7a5SJed Brown PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1); 65009ec7a5SJed Brown if (has_const) *has_const = sp->has_cnst; 66009ec7a5SJed Brown if (n) *n = sp->n; 67009ec7a5SJed Brown if (vecs) *vecs = sp->vecs; 68009ec7a5SJed Brown PetscFunctionReturn(0); 69009ec7a5SJed Brown } 70009ec7a5SJed Brown 71009ec7a5SJed Brown #undef __FUNCT__ 72009ec7a5SJed Brown #define __FUNCT__ "MatNullSpaceCreateRigidBody" 73009ec7a5SJed Brown /*@ 74009ec7a5SJed Brown MatNullSpaceCreateRigidBody - create rigid body modes from coordinates 75009ec7a5SJed Brown 76009ec7a5SJed Brown Collective on Vec 77009ec7a5SJed Brown 78009ec7a5SJed Brown Input Argument: 79009ec7a5SJed Brown . coords - block of coordinates of each node, must have block size set 80009ec7a5SJed Brown 81009ec7a5SJed Brown Output Argument: 82009ec7a5SJed Brown . sp - the null space 83009ec7a5SJed Brown 84009ec7a5SJed Brown Level: advanced 85009ec7a5SJed Brown 86009ec7a5SJed Brown .seealso: MatNullSpaceCreate() 87009ec7a5SJed Brown @*/ 88009ec7a5SJed Brown PetscErrorCode MatNullSpaceCreateRigidBody(Vec coords,MatNullSpace *sp) 89009ec7a5SJed Brown { 90009ec7a5SJed Brown PetscErrorCode ierr; 91009ec7a5SJed Brown const PetscScalar *x; 92bee94d3eSJed Brown PetscScalar *v[6],dots[5]; 93009ec7a5SJed Brown Vec vec[6]; 94009ec7a5SJed Brown PetscInt n,N,dim,nmodes,i,j; 95*eb7a2786SBarry Smith PetscReal sN; 96009ec7a5SJed Brown 97009ec7a5SJed Brown PetscFunctionBegin; 98009ec7a5SJed Brown ierr = VecGetBlockSize(coords,&dim);CHKERRQ(ierr); 99009ec7a5SJed Brown ierr = VecGetLocalSize(coords,&n);CHKERRQ(ierr); 100009ec7a5SJed Brown ierr = VecGetSize(coords,&N);CHKERRQ(ierr); 101009ec7a5SJed Brown n /= dim; 102009ec7a5SJed Brown N /= dim; 103*eb7a2786SBarry Smith sN = 1./PetscSqrtReal((PetscReal)N); 104009ec7a5SJed Brown switch (dim) { 105009ec7a5SJed Brown case 1: 106ce94432eSBarry Smith ierr = MatNullSpaceCreate(PetscObjectComm((PetscObject)coords),PETSC_TRUE,0,NULL,sp);CHKERRQ(ierr); 107009ec7a5SJed Brown break; 108009ec7a5SJed Brown case 2: 109009ec7a5SJed Brown case 3: 110009ec7a5SJed Brown nmodes = (dim == 2) ? 3 : 6; 111ce94432eSBarry Smith ierr = VecCreate(PetscObjectComm((PetscObject)coords),&vec[0]);CHKERRQ(ierr); 112009ec7a5SJed Brown ierr = VecSetSizes(vec[0],dim*n,dim*N);CHKERRQ(ierr); 113009ec7a5SJed Brown ierr = VecSetBlockSize(vec[0],dim);CHKERRQ(ierr); 114009ec7a5SJed Brown ierr = VecSetUp(vec[0]);CHKERRQ(ierr); 115009ec7a5SJed Brown for (i=1; i<nmodes; i++) {ierr = VecDuplicate(vec[0],&vec[i]);CHKERRQ(ierr);} 116009ec7a5SJed Brown for (i=0; i<nmodes; i++) {ierr = VecGetArray(vec[i],&v[i]);CHKERRQ(ierr);} 117009ec7a5SJed Brown ierr = VecGetArrayRead(coords,&x);CHKERRQ(ierr); 118009ec7a5SJed Brown for (i=0; i<n; i++) { 119009ec7a5SJed Brown if (dim == 2) { 120*eb7a2786SBarry Smith v[0][i*2+0] = sN; 121009ec7a5SJed Brown v[0][i*2+1] = 0.; 122009ec7a5SJed Brown v[1][i*2+0] = 0.; 123*eb7a2786SBarry Smith v[1][i*2+1] = sN; 124009ec7a5SJed Brown /* Rotations */ 125009ec7a5SJed Brown v[2][i*2+0] = -x[i*2+1]; 126009ec7a5SJed Brown v[2][i*2+1] = x[i*2+0]; 127009ec7a5SJed Brown } else { 128*eb7a2786SBarry Smith v[0][i*3+0] = sN; 129009ec7a5SJed Brown v[0][i*3+1] = 0.; 130009ec7a5SJed Brown v[0][i*3+2] = 0.; 131009ec7a5SJed Brown v[1][i*3+0] = 0.; 132*eb7a2786SBarry Smith v[1][i*3+1] = sN; 133009ec7a5SJed Brown v[1][i*3+2] = 0.; 134009ec7a5SJed Brown v[2][i*3+0] = 0.; 135009ec7a5SJed Brown v[2][i*3+1] = 0.; 136*eb7a2786SBarry Smith v[2][i*3+2] = sN; 137009ec7a5SJed Brown 138009ec7a5SJed Brown v[3][i*3+0] = x[i*3+1]; 139009ec7a5SJed Brown v[3][i*3+1] = -x[i*3+0]; 140009ec7a5SJed Brown v[3][i*3+2] = 0.; 141009ec7a5SJed Brown v[4][i*3+0] = 0.; 142009ec7a5SJed Brown v[4][i*3+1] = -x[i*3+2]; 143009ec7a5SJed Brown v[4][i*3+2] = x[i*3+1]; 144009ec7a5SJed Brown v[5][i*3+0] = x[i*3+2]; 145009ec7a5SJed Brown v[5][i*3+1] = 0.; 146009ec7a5SJed Brown v[5][i*3+2] = -x[i*3+0]; 147009ec7a5SJed Brown } 148009ec7a5SJed Brown } 149009ec7a5SJed Brown for (i=0; i<nmodes; i++) {ierr = VecRestoreArray(vec[i],&v[i]);CHKERRQ(ierr);} 150009ec7a5SJed Brown ierr = VecRestoreArrayRead(coords,&x);CHKERRQ(ierr); 151009ec7a5SJed Brown for (i=dim; i<nmodes; i++) { 152bee94d3eSJed Brown /* Orthonormalize vec[i] against vec[0:i-1] */ 153009ec7a5SJed Brown ierr = VecMDot(vec[i],i,vec,dots);CHKERRQ(ierr); 154009ec7a5SJed Brown for (j=0; j<i; j++) dots[j] *= -1.; 155009ec7a5SJed Brown ierr = VecMAXPY(vec[i],i,dots,vec);CHKERRQ(ierr); 1560298fd71SBarry Smith ierr = VecNormalize(vec[i],NULL);CHKERRQ(ierr); 157009ec7a5SJed Brown } 158ce94432eSBarry Smith ierr = MatNullSpaceCreate(PetscObjectComm((PetscObject)coords),PETSC_FALSE,nmodes,vec,sp);CHKERRQ(ierr); 159009ec7a5SJed Brown for (i=0; i<nmodes; i++) {ierr = VecDestroy(&vec[i]);CHKERRQ(ierr);} 160009ec7a5SJed Brown } 161009ec7a5SJed Brown PetscFunctionReturn(0); 162009ec7a5SJed Brown } 163009ec7a5SJed Brown 164009ec7a5SJed Brown #undef __FUNCT__ 165f7357b39SLisandro Dalcin #define __FUNCT__ "MatNullSpaceView" 166b717e993SJed Brown /*@C 167b717e993SJed Brown MatNullSpaceView - Visualizes a null space object. 168b717e993SJed Brown 169b717e993SJed Brown Collective on MatNullSpace 170b717e993SJed Brown 171b717e993SJed Brown Input Parameters: 172b717e993SJed Brown + matnull - the null space 173b717e993SJed Brown - viewer - visualization context 174b717e993SJed Brown 175b717e993SJed Brown Level: advanced 176b717e993SJed Brown 177b717e993SJed Brown Fortran Note: 178b717e993SJed Brown This routine is not supported in Fortran. 179b717e993SJed Brown 180b717e993SJed Brown .seealso: MatNullSpaceCreate(), PetscViewerASCIIOpen() 181b717e993SJed Brown @*/ 182b717e993SJed Brown PetscErrorCode MatNullSpaceView(MatNullSpace sp,PetscViewer viewer) 183f7357b39SLisandro Dalcin { 184f7357b39SLisandro Dalcin PetscErrorCode ierr; 185f7357b39SLisandro Dalcin PetscBool iascii; 186f7357b39SLisandro Dalcin 187f7357b39SLisandro Dalcin PetscFunctionBegin; 188f7357b39SLisandro Dalcin PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1); 189ce94432eSBarry Smith if (!viewer) viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)sp)); 190f7357b39SLisandro Dalcin PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 191f7357b39SLisandro Dalcin PetscCheckSameComm(sp,1,viewer,2); 192f7357b39SLisandro Dalcin 193251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 194f7357b39SLisandro Dalcin if (iascii) { 19502cf292fSJed Brown PetscViewerFormat format; 19602cf292fSJed Brown PetscInt i; 19702cf292fSJed Brown ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 198dae58748SBarry Smith ierr = PetscObjectPrintClassNamePrefixType((PetscObject)sp,viewer);CHKERRQ(ierr); 19902cf292fSJed Brown ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 20002cf292fSJed Brown ierr = PetscViewerASCIIPrintf(viewer,"Contains %D vector%s%s\n",sp->n,sp->n==1 ? "" : "s",sp->has_cnst ? " and the constant" : "");CHKERRQ(ierr); 20102cf292fSJed Brown if (sp->remove) {ierr = PetscViewerASCIIPrintf(viewer,"Has user-provided removal function\n");CHKERRQ(ierr);} 20202cf292fSJed Brown if (!(format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL)) { 20302cf292fSJed Brown for (i=0; i<sp->n; i++) { 20402cf292fSJed Brown ierr = VecView(sp->vecs[i],viewer);CHKERRQ(ierr); 20502cf292fSJed Brown } 20602cf292fSJed Brown } 20702cf292fSJed Brown ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 208f7357b39SLisandro Dalcin } 209f7357b39SLisandro Dalcin PetscFunctionReturn(0); 210f7357b39SLisandro Dalcin } 211f7357b39SLisandro Dalcin 212f7357b39SLisandro Dalcin #undef __FUNCT__ 2134a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceCreate" 214f39d8e23SSatish Balay /*@ 2155cfeda75SBarry Smith MatNullSpaceCreate - Creates a data structure used to project vectors 216b4fd4287SBarry Smith out of null spaces. 217f7765cecSBarry Smith 2184e472627SLois Curfman McInnes Collective on MPI_Comm 2194e472627SLois Curfman McInnes 220f7765cecSBarry Smith Input Parameters: 22183c3bef8SLois Curfman McInnes + comm - the MPI communicator associated with the object 22283c3bef8SLois Curfman McInnes . has_cnst - PETSC_TRUE if the null space contains the constant vector; otherwise PETSC_FALSE 223b4fd4287SBarry Smith . n - number of vectors (excluding constant vector) in null space 22483c3bef8SLois Curfman McInnes - vecs - the vectors that span the null space (excluding the constant vector); 225f7a9e4ceSBarry Smith these vectors must be orthonormal. These vectors are NOT copied, so do not change them 22673141a14SBarry Smith after this call. You should free the array that you pass in and destroy the vectors (this will reduce the reference count 22773141a14SBarry Smith for them by one). 228f7765cecSBarry Smith 229f7765cecSBarry Smith Output Parameter: 230b4fd4287SBarry Smith . SP - the null space context 231f7765cecSBarry Smith 23283c3bef8SLois Curfman McInnes Level: advanced 23383c3bef8SLois Curfman McInnes 23480bf1014SBarry Smith Notes: See MatNullSpaceSetFunction() as an alternative way of providing the null space information instead of setting vecs. 23580bf1014SBarry Smith 23680bf1014SBarry 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 23780bf1014SBarry Smith need to pass in a function that eliminates the constant function into MatNullSpaceSetFunction(). 23880bf1014SBarry Smith 2396e1639daSBarry Smith Users manual sections: 2406e1639daSBarry Smith . sec_singular 2416e1639daSBarry Smith 24283c3bef8SLois Curfman McInnes .keywords: PC, null space, create 24341a59933SSatish Balay 2445fa7ec2dSBarry Smith .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), MatSetNullSpace(), MatNullSpace, MatNullSpaceSetFunction() 245f7765cecSBarry Smith @*/ 2467087cfbeSBarry Smith PetscErrorCode MatNullSpaceCreate(MPI_Comm comm,PetscBool has_cnst,PetscInt n,const Vec vecs[],MatNullSpace *SP) 247f7765cecSBarry Smith { 2485cfeda75SBarry Smith MatNullSpace sp; 249dfbe8321SBarry Smith PetscErrorCode ierr; 250c1ac3661SBarry Smith PetscInt i; 251f7765cecSBarry Smith 2523a40ed3dSBarry Smith PetscFunctionBegin; 253e32f2f54SBarry Smith if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of vectors (given %D) cannot be negative",n); 254574b3360SMatthew Knepley if (n) PetscValidPointer(vecs,4); 2550700a824SBarry Smith for (i=0; i<n; i++) PetscValidHeaderSpecific(vecs[i],VEC_CLASSID,4); 256574b3360SMatthew Knepley PetscValidPointer(SP,5); 257574b3360SMatthew Knepley 2580298fd71SBarry Smith *SP = NULL; 259607a6623SBarry Smith ierr = MatInitializePackage();CHKERRQ(ierr); 260574b3360SMatthew Knepley 26173107ff1SLisandro Dalcin ierr = PetscHeaderCreate(sp,MAT_NULLSPACE_CLASSID,"MatNullSpace","Null space","Mat",comm,MatNullSpaceDestroy,MatNullSpaceView);CHKERRQ(ierr); 262f7765cecSBarry Smith 263b4fd4287SBarry Smith sp->has_cnst = has_cnst; 264b4fd4287SBarry Smith sp->n = n; 2657850f3fbSLisandro Dalcin sp->vecs = 0; 2667850f3fbSLisandro Dalcin sp->alpha = 0; 2677850f3fbSLisandro Dalcin sp->remove = 0; 2687850f3fbSLisandro Dalcin sp->rmctx = 0; 2697850f3fbSLisandro Dalcin 270f7a9e4ceSBarry Smith if (n) { 271785e854fSJed Brown ierr = PetscMalloc1(n,&sp->vecs);CHKERRQ(ierr); 272785e854fSJed Brown ierr = PetscMalloc1(n,&sp->alpha);CHKERRQ(ierr); 2733bb1ff40SBarry Smith ierr = PetscLogObjectMemory((PetscObject)sp,n*(sizeof(Vec)+sizeof(PetscScalar)));CHKERRQ(ierr); 2747850f3fbSLisandro Dalcin for (i=0; i<n; i++) { 2757850f3fbSLisandro Dalcin ierr = PetscObjectReference((PetscObject)vecs[i]);CHKERRQ(ierr); 2767850f3fbSLisandro Dalcin sp->vecs[i] = vecs[i]; 2777850f3fbSLisandro Dalcin } 278f7a9e4ceSBarry Smith } 279b4fd4287SBarry Smith 280b4fd4287SBarry Smith *SP = sp; 2813a40ed3dSBarry Smith PetscFunctionReturn(0); 282f7765cecSBarry Smith } 283f7765cecSBarry Smith 2844a2ae208SSatish Balay #undef __FUNCT__ 2854a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceDestroy" 286f7765cecSBarry Smith /*@ 2875cfeda75SBarry Smith MatNullSpaceDestroy - Destroys a data structure used to project vectors 288b4fd4287SBarry Smith out of null spaces. 289b4fd4287SBarry Smith 2905cfeda75SBarry Smith Collective on MatNullSpace 2914e472627SLois Curfman McInnes 292b4fd4287SBarry Smith Input Parameter: 293b9756687SLois Curfman McInnes . sp - the null space context to be destroyed 294b9756687SLois Curfman McInnes 295b9756687SLois Curfman McInnes Level: advanced 296b4fd4287SBarry Smith 29783c3bef8SLois Curfman McInnes .keywords: PC, null space, destroy 29841a59933SSatish Balay 29972875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceRemove(), MatNullSpaceSetFunction() 300b4fd4287SBarry Smith @*/ 301d34fcf5fSBarry Smith PetscErrorCode MatNullSpaceDestroy(MatNullSpace *sp) 302b4fd4287SBarry Smith { 303dfbe8321SBarry Smith PetscErrorCode ierr; 30485614651SBarry Smith 3055cfeda75SBarry Smith PetscFunctionBegin; 3066bf464f9SBarry Smith if (!*sp) PetscFunctionReturn(0); 307d34fcf5fSBarry Smith PetscValidHeaderSpecific((*sp),MAT_NULLSPACE_CLASSID,1); 308d34fcf5fSBarry Smith if (--((PetscObject)(*sp))->refct > 0) {*sp = 0; PetscFunctionReturn(0);} 30985614651SBarry Smith 3106bf464f9SBarry Smith ierr = VecDestroyVecs((*sp)->n,&(*sp)->vecs);CHKERRQ(ierr); 311d34fcf5fSBarry Smith ierr = PetscFree((*sp)->alpha);CHKERRQ(ierr); 3126bf464f9SBarry Smith ierr = PetscHeaderDestroy(sp);CHKERRQ(ierr); 3133a40ed3dSBarry Smith PetscFunctionReturn(0); 314b4fd4287SBarry Smith } 315b4fd4287SBarry Smith 3164a2ae208SSatish Balay #undef __FUNCT__ 3174a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceRemove" 318812c3f48SMatthew Knepley /*@C 3195cfeda75SBarry Smith MatNullSpaceRemove - Removes all the components of a null space from a vector. 320f7765cecSBarry Smith 3215cfeda75SBarry Smith Collective on MatNullSpace 322f7765cecSBarry Smith 3234e472627SLois Curfman McInnes Input Parameters: 3244e472627SLois Curfman McInnes + sp - the null space context 325359a2de3SMatthew G. Knepley - vec - the vector from which the null space is to be removed 3264e472627SLois Curfman McInnes 327b9756687SLois Curfman McInnes Level: advanced 328b9756687SLois Curfman McInnes 32983c3bef8SLois Curfman McInnes .keywords: PC, null space, remove 33041a59933SSatish Balay 33172875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction() 332f7765cecSBarry Smith @*/ 333d0195637SJed Brown PetscErrorCode MatNullSpaceRemove(MatNullSpace sp,Vec vec) 334f7765cecSBarry Smith { 33587828ca2SBarry Smith PetscScalar sum; 3367850f3fbSLisandro Dalcin PetscInt i,N; 3376849ba73SBarry Smith PetscErrorCode ierr; 338f7765cecSBarry Smith 3393a40ed3dSBarry Smith PetscFunctionBegin; 3400700a824SBarry Smith PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1); 3410700a824SBarry Smith PetscValidHeaderSpecific(vec,VEC_CLASSID,2); 3423cd8ff7eSMatthew Knepley 343b4fd4287SBarry Smith if (sp->has_cnst) { 3447850f3fbSLisandro Dalcin ierr = VecGetSize(vec,&N);CHKERRQ(ierr); 3457850f3fbSLisandro Dalcin if (N > 0) { 3467850f3fbSLisandro Dalcin ierr = VecSum(vec,&sum);CHKERRQ(ierr); 347d4a378daSJed Brown sum = sum/((PetscScalar)(-1.0*N)); 3487850f3fbSLisandro Dalcin ierr = VecShift(vec,sum);CHKERRQ(ierr); 3497850f3fbSLisandro Dalcin } 350f7765cecSBarry Smith } 351b4fd4287SBarry Smith 3527850f3fbSLisandro Dalcin if (sp->n) { 3537850f3fbSLisandro Dalcin ierr = VecMDot(vec,sp->n,sp->vecs,sp->alpha);CHKERRQ(ierr); 3547850f3fbSLisandro Dalcin for (i=0; i<sp->n; i++) sp->alpha[i] = -sp->alpha[i]; 3557850f3fbSLisandro Dalcin ierr = VecMAXPY(vec,sp->n,sp->alpha,sp->vecs);CHKERRQ(ierr); 356f7765cecSBarry Smith } 357b4fd4287SBarry Smith 35872875594SBarry Smith if (sp->remove) { 3590c3c4d68SMatthew Knepley ierr = (*sp->remove)(sp,vec,sp->rmctx);CHKERRQ(ierr); 36072875594SBarry Smith } 3613a40ed3dSBarry Smith PetscFunctionReturn(0); 362f7765cecSBarry Smith } 363a2e34c3dSBarry Smith 3644a2ae208SSatish Balay #undef __FUNCT__ 3654a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceTest" 366a2e34c3dSBarry Smith /*@ 367a2e34c3dSBarry Smith MatNullSpaceTest - Tests if the claimed null space is really a 368a2e34c3dSBarry Smith null space of a matrix 369a2e34c3dSBarry Smith 370a2e34c3dSBarry Smith Collective on MatNullSpace 371a2e34c3dSBarry Smith 372a2e34c3dSBarry Smith Input Parameters: 373a2e34c3dSBarry Smith + sp - the null space context 374a2e34c3dSBarry Smith - mat - the matrix 375a2e34c3dSBarry Smith 37695902228SMatthew Knepley Output Parameters: 37795902228SMatthew Knepley . isNull - PETSC_TRUE if the nullspace is valid for this matrix 37895902228SMatthew Knepley 379a2e34c3dSBarry Smith Level: advanced 380a2e34c3dSBarry Smith 381a2e34c3dSBarry Smith .keywords: PC, null space, remove 382a2e34c3dSBarry Smith 38372875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction() 384a2e34c3dSBarry Smith @*/ 3857087cfbeSBarry Smith PetscErrorCode MatNullSpaceTest(MatNullSpace sp,Mat mat,PetscBool *isNull) 386a2e34c3dSBarry Smith { 38787828ca2SBarry Smith PetscScalar sum; 3888bb6bcc5SSatish Balay PetscReal nrm; 3890b12b109SJed Brown PetscInt j,n,N; 3906849ba73SBarry Smith PetscErrorCode ierr; 391a2e34c3dSBarry Smith Vec l,r; 392ace3abfcSBarry Smith PetscBool flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,consistent = PETSC_TRUE; 3933050cee2SBarry Smith PetscViewer viewer; 394a2e34c3dSBarry Smith 395a2e34c3dSBarry Smith PetscFunctionBegin; 3960700a824SBarry Smith PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1); 3970700a824SBarry Smith PetscValidHeaderSpecific(mat,MAT_CLASSID,2); 3983cfa8680SLisandro Dalcin n = sp->n; 3990298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-mat_null_space_test_view",&flg1,NULL);CHKERRQ(ierr); 4000298fd71SBarry Smith ierr = PetscOptionsGetBool(NULL,"-mat_null_space_test_view_draw",&flg2,NULL);CHKERRQ(ierr); 401a2e34c3dSBarry Smith 402a2e34c3dSBarry Smith if (n) { 403401b765aSJed Brown ierr = VecDuplicate(sp->vecs[0],&l);CHKERRQ(ierr); 404a2e34c3dSBarry Smith } else { 4052a7a6963SBarry Smith ierr = MatCreateVecs(mat,&l,NULL);CHKERRQ(ierr); 406a2e34c3dSBarry Smith } 407a2e34c3dSBarry Smith 408ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)sp),&viewer);CHKERRQ(ierr); 409a2e34c3dSBarry Smith if (sp->has_cnst) { 410a2e34c3dSBarry Smith ierr = VecDuplicate(l,&r);CHKERRQ(ierr); 411a2e34c3dSBarry Smith ierr = VecGetSize(l,&N);CHKERRQ(ierr); 412a2e34c3dSBarry Smith sum = 1.0/N; 4132dcb1b2aSMatthew Knepley ierr = VecSet(l,sum);CHKERRQ(ierr); 414a2e34c3dSBarry Smith ierr = MatMult(mat,l,r);CHKERRQ(ierr); 4158bb6bcc5SSatish Balay ierr = VecNorm(r,NORM_2,&nrm);CHKERRQ(ierr); 41618404f68SMatthew G Knepley if (nrm >= 1.e-7) consistent = PETSC_FALSE; 417874288d9SMatthew G Knepley if (flg1) { 41818404f68SMatthew G Knepley if (consistent) { 419ce94432eSBarry Smith ierr = PetscPrintf(PetscObjectComm((PetscObject)sp),"Constants are likely null vector");CHKERRQ(ierr); 42095902228SMatthew Knepley } else { 421ce94432eSBarry Smith ierr = PetscPrintf(PetscObjectComm((PetscObject)sp),"Constants are unlikely null vector ");CHKERRQ(ierr); 42295902228SMatthew Knepley } 42357622a8eSBarry Smith ierr = PetscPrintf(PetscObjectComm((PetscObject)sp),"|| A * 1/N || = %g\n",(double)nrm);CHKERRQ(ierr); 424874288d9SMatthew G Knepley } 42518404f68SMatthew G Knepley if (!consistent && flg1) {ierr = VecView(r,viewer);CHKERRQ(ierr);} 42618404f68SMatthew G Knepley if (!consistent && flg2) {ierr = VecView(r,viewer);CHKERRQ(ierr);} 4276bf464f9SBarry Smith ierr = VecDestroy(&r);CHKERRQ(ierr); 428a2e34c3dSBarry Smith } 429a2e34c3dSBarry Smith 430a2e34c3dSBarry Smith for (j=0; j<n; j++) { 431a2e34c3dSBarry Smith ierr = (*mat->ops->mult)(mat,sp->vecs[j],l);CHKERRQ(ierr); 4328bb6bcc5SSatish Balay ierr = VecNorm(l,NORM_2,&nrm);CHKERRQ(ierr); 43318404f68SMatthew G Knepley if (nrm >= 1.e-7) consistent = PETSC_FALSE; 434874288d9SMatthew G Knepley if (flg1) { 43518404f68SMatthew G Knepley if (consistent) { 436ce94432eSBarry Smith ierr = PetscPrintf(PetscObjectComm((PetscObject)sp),"Null vector %D is likely null vector",j);CHKERRQ(ierr); 43795902228SMatthew Knepley } else { 438ce94432eSBarry Smith ierr = PetscPrintf(PetscObjectComm((PetscObject)sp),"Null vector %D unlikely null vector ",j);CHKERRQ(ierr); 43995902228SMatthew Knepley consistent = PETSC_FALSE; 44095902228SMatthew Knepley } 44157622a8eSBarry Smith ierr = PetscPrintf(PetscObjectComm((PetscObject)sp),"|| A * v[%D] || = %g\n",j,(double)nrm);CHKERRQ(ierr); 442874288d9SMatthew G Knepley } 44318404f68SMatthew G Knepley if (!consistent && flg1) {ierr = VecView(l,viewer);CHKERRQ(ierr);} 44418404f68SMatthew G Knepley if (!consistent && flg2) {ierr = VecView(l,viewer);CHKERRQ(ierr);} 445a2e34c3dSBarry Smith } 446a2e34c3dSBarry Smith 447ce94432eSBarry Smith if (sp->remove) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot test a null space provided as a function with MatNullSpaceSetFunction()"); 448401b765aSJed Brown ierr = VecDestroy(&l);CHKERRQ(ierr); 44931980aa1SBarry Smith if (isNull) *isNull = consistent; 450a2e34c3dSBarry Smith PetscFunctionReturn(0); 451a2e34c3dSBarry Smith } 452a2e34c3dSBarry Smith 453