xref: /petsc/src/mat/interface/matnull.c (revision b45d2f2cb7e031d9c0de5873eca80614ca7b863b)
1be1d678aSKris Buschelman 
2f7765cecSBarry Smith /*
3b4fd4287SBarry Smith     Routines to project vectors out of null spaces.
4f7765cecSBarry Smith */
5f7765cecSBarry Smith 
6*b45d2f2cSJed Brown #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 
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"
40b717e993SJed Brown /*@C
41b717e993SJed Brown    MatNullSpaceView - Visualizes a null space object.
42b717e993SJed Brown 
43b717e993SJed Brown    Collective on MatNullSpace
44b717e993SJed Brown 
45b717e993SJed Brown    Input Parameters:
46b717e993SJed Brown +  matnull - the null space
47b717e993SJed Brown -  viewer - visualization context
48b717e993SJed Brown 
49b717e993SJed Brown    Level: advanced
50b717e993SJed Brown 
51b717e993SJed Brown    Fortran Note:
52b717e993SJed Brown    This routine is not supported in Fortran.
53b717e993SJed Brown 
54b717e993SJed Brown .seealso: MatNullSpaceCreate(), PetscViewerASCIIOpen()
55b717e993SJed Brown @*/
56b717e993SJed 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) {
6902cf292fSJed Brown     PetscViewerFormat format;
7002cf292fSJed Brown     PetscInt i;
7102cf292fSJed Brown     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
72f7357b39SLisandro Dalcin     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)sp,viewer,"MatNullSpace Object");CHKERRQ(ierr);
7302cf292fSJed Brown     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
7402cf292fSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"Contains %D vector%s%s\n",sp->n,sp->n==1?"":"s",sp->has_cnst?" and the constant":"");CHKERRQ(ierr);
7502cf292fSJed Brown     if (sp->remove) {ierr = PetscViewerASCIIPrintf(viewer,"Has user-provided removal function\n");CHKERRQ(ierr);}
7602cf292fSJed Brown     if (!(format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL)) {
7702cf292fSJed Brown       for (i=0; i<sp->n; i++) {
7802cf292fSJed Brown         ierr = VecView(sp->vecs[i],viewer);CHKERRQ(ierr);
7902cf292fSJed Brown       }
8002cf292fSJed Brown     }
8102cf292fSJed Brown     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
82f7357b39SLisandro Dalcin   }
83f7357b39SLisandro Dalcin   PetscFunctionReturn(0);
84f7357b39SLisandro Dalcin }
85f7357b39SLisandro Dalcin 
86f7357b39SLisandro Dalcin #undef __FUNCT__
874a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceCreate"
88f39d8e23SSatish Balay /*@
895cfeda75SBarry Smith    MatNullSpaceCreate - Creates a data structure used to project vectors
90b4fd4287SBarry Smith    out of null spaces.
91f7765cecSBarry Smith 
924e472627SLois Curfman McInnes    Collective on MPI_Comm
934e472627SLois Curfman McInnes 
94f7765cecSBarry Smith    Input Parameters:
9583c3bef8SLois Curfman McInnes +  comm - the MPI communicator associated with the object
9683c3bef8SLois Curfman McInnes .  has_cnst - PETSC_TRUE if the null space contains the constant vector; otherwise PETSC_FALSE
97b4fd4287SBarry Smith .  n - number of vectors (excluding constant vector) in null space
9883c3bef8SLois Curfman McInnes -  vecs - the vectors that span the null space (excluding the constant vector);
99f7a9e4ceSBarry Smith           these vectors must be orthonormal. These vectors are NOT copied, so do not change them
10073141a14SBarry Smith           after this call. You should free the array that you pass in and destroy the vectors (this will reduce the reference count
10173141a14SBarry Smith           for them by one).
102f7765cecSBarry Smith 
103f7765cecSBarry Smith    Output Parameter:
104b4fd4287SBarry Smith .  SP - the null space context
105f7765cecSBarry Smith 
10683c3bef8SLois Curfman McInnes    Level: advanced
10783c3bef8SLois Curfman McInnes 
10880bf1014SBarry Smith    Notes: See MatNullSpaceSetFunction() as an alternative way of providing the null space information instead of setting vecs.
10980bf1014SBarry Smith 
11080bf1014SBarry 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
11180bf1014SBarry Smith        need to pass in a function that eliminates the constant function into MatNullSpaceSetFunction().
11280bf1014SBarry Smith 
1136e1639daSBarry Smith   Users manual sections:
1146e1639daSBarry Smith .   sec_singular
1156e1639daSBarry Smith 
11683c3bef8SLois Curfman McInnes .keywords: PC, null space, create
11741a59933SSatish Balay 
11872875594SBarry Smith .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), KSPSetNullSpace(), MatNullSpace, MatNullSpaceSetFunction()
119f7765cecSBarry Smith @*/
1207087cfbeSBarry Smith PetscErrorCode  MatNullSpaceCreate(MPI_Comm comm,PetscBool  has_cnst,PetscInt n,const Vec vecs[],MatNullSpace *SP)
121f7765cecSBarry Smith {
1225cfeda75SBarry Smith   MatNullSpace   sp;
123dfbe8321SBarry Smith   PetscErrorCode ierr;
124c1ac3661SBarry Smith   PetscInt       i;
125f7765cecSBarry Smith 
1263a40ed3dSBarry Smith   PetscFunctionBegin;
127e32f2f54SBarry Smith   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of vectors (given %D) cannot be negative",n);
128574b3360SMatthew Knepley   if (n) PetscValidPointer(vecs,4);
1290700a824SBarry Smith   for (i=0; i<n; i++) PetscValidHeaderSpecific(vecs[i],VEC_CLASSID,4);
130574b3360SMatthew Knepley   PetscValidPointer(SP,5);
131574b3360SMatthew Knepley 
132574b3360SMatthew Knepley   *SP = PETSC_NULL;
133574b3360SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
134574b3360SMatthew Knepley   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
135574b3360SMatthew Knepley #endif
136574b3360SMatthew Knepley 
1373194b578SJed Brown   ierr = PetscHeaderCreate(sp,_p_MatNullSpace,int,MAT_NULLSPACE_CLASSID,0,"MatNullSpace","Null space","Mat",comm,MatNullSpaceDestroy,MatNullSpaceView);CHKERRQ(ierr);
138f7765cecSBarry Smith 
139b4fd4287SBarry Smith   sp->has_cnst = has_cnst;
140b4fd4287SBarry Smith   sp->n        = n;
1417850f3fbSLisandro Dalcin   sp->vecs     = 0;
1427850f3fbSLisandro Dalcin   sp->alpha    = 0;
1437850f3fbSLisandro Dalcin   sp->vec      = 0;
1447850f3fbSLisandro Dalcin   sp->remove   = 0;
1457850f3fbSLisandro Dalcin   sp->rmctx    = 0;
1467850f3fbSLisandro Dalcin 
147f7a9e4ceSBarry Smith   if (n) {
148f7a9e4ceSBarry Smith     ierr = PetscMalloc(n*sizeof(Vec),&sp->vecs);CHKERRQ(ierr);
1497850f3fbSLisandro Dalcin     ierr = PetscMalloc(n*sizeof(PetscScalar),&sp->alpha);CHKERRQ(ierr);
1507850f3fbSLisandro Dalcin     ierr = PetscLogObjectMemory(sp,n*(sizeof(Vec)+sizeof(PetscScalar)));CHKERRQ(ierr);
1517850f3fbSLisandro Dalcin     for (i=0; i<n; i++) {
1527850f3fbSLisandro Dalcin       ierr = PetscObjectReference((PetscObject)vecs[i]);CHKERRQ(ierr);
1537850f3fbSLisandro Dalcin       sp->vecs[i] = vecs[i];
1547850f3fbSLisandro Dalcin     }
155f7a9e4ceSBarry Smith   }
156b4fd4287SBarry Smith 
157b4fd4287SBarry Smith   *SP          = sp;
1583a40ed3dSBarry Smith   PetscFunctionReturn(0);
159f7765cecSBarry Smith }
160f7765cecSBarry Smith 
1614a2ae208SSatish Balay #undef __FUNCT__
1624a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceDestroy"
163f7765cecSBarry Smith /*@
1645cfeda75SBarry Smith    MatNullSpaceDestroy - Destroys a data structure used to project vectors
165b4fd4287SBarry Smith    out of null spaces.
166b4fd4287SBarry Smith 
1675cfeda75SBarry Smith    Collective on MatNullSpace
1684e472627SLois Curfman McInnes 
169b4fd4287SBarry Smith    Input Parameter:
170b9756687SLois Curfman McInnes .  sp - the null space context to be destroyed
171b9756687SLois Curfman McInnes 
172b9756687SLois Curfman McInnes    Level: advanced
173b4fd4287SBarry Smith 
17483c3bef8SLois Curfman McInnes .keywords: PC, null space, destroy
17541a59933SSatish Balay 
17672875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceRemove(), MatNullSpaceSetFunction()
177b4fd4287SBarry Smith @*/
178d34fcf5fSBarry Smith PetscErrorCode  MatNullSpaceDestroy(MatNullSpace *sp)
179b4fd4287SBarry Smith {
180dfbe8321SBarry Smith   PetscErrorCode ierr;
18185614651SBarry Smith 
1825cfeda75SBarry Smith   PetscFunctionBegin;
1836bf464f9SBarry Smith   if (!*sp) PetscFunctionReturn(0);
184d34fcf5fSBarry Smith   PetscValidHeaderSpecific((*sp),MAT_NULLSPACE_CLASSID,1);
185d34fcf5fSBarry Smith   if (--((PetscObject)(*sp))->refct > 0) {*sp = 0; PetscFunctionReturn(0);}
18685614651SBarry Smith 
1876bf464f9SBarry Smith   ierr = VecDestroy(&(*sp)->vec);CHKERRQ(ierr);
1886bf464f9SBarry Smith   ierr = VecDestroyVecs((*sp)->n,&(*sp)->vecs);CHKERRQ(ierr);
189d34fcf5fSBarry Smith   ierr = PetscFree((*sp)->alpha);CHKERRQ(ierr);
1906bf464f9SBarry Smith   ierr = PetscHeaderDestroy(sp);CHKERRQ(ierr);
1913a40ed3dSBarry Smith   PetscFunctionReturn(0);
192b4fd4287SBarry Smith }
193b4fd4287SBarry Smith 
1944a2ae208SSatish Balay #undef __FUNCT__
1954a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceRemove"
196812c3f48SMatthew Knepley /*@C
1975cfeda75SBarry Smith    MatNullSpaceRemove - Removes all the components of a null space from a vector.
198f7765cecSBarry Smith 
1995cfeda75SBarry Smith    Collective on MatNullSpace
200f7765cecSBarry Smith 
2014e472627SLois Curfman McInnes    Input Parameters:
2024e472627SLois Curfman McInnes +  sp - the null space context
2034e7234bfSBarry Smith .  vec - the vector from which the null space is to be removed
2045fcf39f4SBarry Smith -  out - if this is requested (not PETSC_NULL) then this is a vector with the null space removed otherwise
2054e7234bfSBarry Smith          the removal is done in-place (in vec)
2064e7234bfSBarry Smith 
207db090513SMatthew Knepley    Note: The user is not responsible for the vector returned and should not destroy it.
2084e472627SLois Curfman McInnes 
209b9756687SLois Curfman McInnes    Level: advanced
210b9756687SLois Curfman McInnes 
21183c3bef8SLois Curfman McInnes .keywords: PC, null space, remove
21241a59933SSatish Balay 
21372875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction()
214f7765cecSBarry Smith @*/
2157087cfbeSBarry Smith PetscErrorCode  MatNullSpaceRemove(MatNullSpace sp,Vec vec,Vec *out)
216f7765cecSBarry Smith {
21787828ca2SBarry Smith   PetscScalar    sum;
2187850f3fbSLisandro Dalcin   PetscInt       i,N;
2196849ba73SBarry Smith   PetscErrorCode ierr;
220f7765cecSBarry Smith 
2213a40ed3dSBarry Smith   PetscFunctionBegin;
2220700a824SBarry Smith   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1);
2230700a824SBarry Smith   PetscValidHeaderSpecific(vec,VEC_CLASSID,2);
2243cd8ff7eSMatthew Knepley 
2255cfeda75SBarry Smith   if (out) {
2263cd8ff7eSMatthew Knepley     PetscValidPointer(out,3);
2275cfeda75SBarry Smith     if (!sp->vec) {
2285cfeda75SBarry Smith       ierr = VecDuplicate(vec,&sp->vec);CHKERRQ(ierr);
2297850f3fbSLisandro Dalcin       ierr = PetscLogObjectParent(sp,sp->vec);CHKERRQ(ierr);
2305cfeda75SBarry Smith     }
2317850f3fbSLisandro Dalcin     ierr = VecCopy(vec,sp->vec);CHKERRQ(ierr);
2327850f3fbSLisandro Dalcin     vec = *out = sp->vec;
2335cfeda75SBarry Smith   }
2345cfeda75SBarry Smith 
235b4fd4287SBarry Smith   if (sp->has_cnst) {
2367850f3fbSLisandro Dalcin     ierr = VecGetSize(vec,&N);CHKERRQ(ierr);
2377850f3fbSLisandro Dalcin     if (N > 0) {
2387850f3fbSLisandro Dalcin       ierr = VecSum(vec,&sum);CHKERRQ(ierr);
239d4a378daSJed Brown       sum  = sum/((PetscScalar)(-1.0*N));
2407850f3fbSLisandro Dalcin       ierr = VecShift(vec,sum);CHKERRQ(ierr);
2417850f3fbSLisandro Dalcin     }
242f7765cecSBarry Smith   }
243b4fd4287SBarry Smith 
2447850f3fbSLisandro Dalcin   if (sp->n) {
2457850f3fbSLisandro Dalcin     ierr = VecMDot(vec,sp->n,sp->vecs,sp->alpha);CHKERRQ(ierr);
2467850f3fbSLisandro Dalcin     for (i=0; i<sp->n; i++) sp->alpha[i] = -sp->alpha[i];
2477850f3fbSLisandro Dalcin     ierr = VecMAXPY(vec,sp->n,sp->alpha,sp->vecs);CHKERRQ(ierr);
248f7765cecSBarry Smith   }
249b4fd4287SBarry Smith 
25072875594SBarry Smith   if (sp->remove){
2510c3c4d68SMatthew Knepley     ierr = (*sp->remove)(sp,vec,sp->rmctx);CHKERRQ(ierr);
25272875594SBarry Smith   }
2533a40ed3dSBarry Smith   PetscFunctionReturn(0);
254f7765cecSBarry Smith }
255a2e34c3dSBarry Smith 
2564a2ae208SSatish Balay #undef __FUNCT__
2574a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceTest"
258a2e34c3dSBarry Smith /*@
259a2e34c3dSBarry Smith    MatNullSpaceTest  - Tests if the claimed null space is really a
260a2e34c3dSBarry Smith      null space of a matrix
261a2e34c3dSBarry Smith 
262a2e34c3dSBarry Smith    Collective on MatNullSpace
263a2e34c3dSBarry Smith 
264a2e34c3dSBarry Smith    Input Parameters:
265a2e34c3dSBarry Smith +  sp - the null space context
266a2e34c3dSBarry Smith -  mat - the matrix
267a2e34c3dSBarry Smith 
26895902228SMatthew Knepley    Output Parameters:
26995902228SMatthew Knepley .  isNull - PETSC_TRUE if the nullspace is valid for this matrix
27095902228SMatthew Knepley 
271a2e34c3dSBarry Smith    Level: advanced
272a2e34c3dSBarry Smith 
273a2e34c3dSBarry Smith .keywords: PC, null space, remove
274a2e34c3dSBarry Smith 
27572875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction()
276a2e34c3dSBarry Smith @*/
2777087cfbeSBarry Smith PetscErrorCode  MatNullSpaceTest(MatNullSpace sp,Mat mat,PetscBool  *isNull)
278a2e34c3dSBarry Smith {
27987828ca2SBarry Smith   PetscScalar    sum;
2808bb6bcc5SSatish Balay   PetscReal      nrm;
2810b12b109SJed Brown   PetscInt       j,n,N;
2826849ba73SBarry Smith   PetscErrorCode ierr;
283a2e34c3dSBarry Smith   Vec            l,r;
284ace3abfcSBarry Smith   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,consistent = PETSC_TRUE;
2853050cee2SBarry Smith   PetscViewer    viewer;
286a2e34c3dSBarry Smith 
287a2e34c3dSBarry Smith   PetscFunctionBegin;
2880700a824SBarry Smith   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1);
2890700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,2);
2903cfa8680SLisandro Dalcin   n = sp->n;
291acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-mat_null_space_test_view",&flg1,PETSC_NULL);CHKERRQ(ierr);
292acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-mat_null_space_test_view_draw",&flg2,PETSC_NULL);CHKERRQ(ierr);
293a2e34c3dSBarry Smith 
294a2e34c3dSBarry Smith   if (!sp->vec) {
295a2e34c3dSBarry Smith     if (n) {
296a2e34c3dSBarry Smith       ierr = VecDuplicate(sp->vecs[0],&sp->vec);CHKERRQ(ierr);
297a2e34c3dSBarry Smith     } else {
2980b12b109SJed Brown       ierr = MatGetVecs(mat,&sp->vec,PETSC_NULL);CHKERRQ(ierr);
299a2e34c3dSBarry Smith     }
300a2e34c3dSBarry Smith   }
301a2e34c3dSBarry Smith   l    = sp->vec;
302a2e34c3dSBarry Smith 
3037adad957SLisandro Dalcin   ierr = PetscViewerASCIIGetStdout(((PetscObject)sp)->comm,&viewer);CHKERRQ(ierr);
304a2e34c3dSBarry Smith   if (sp->has_cnst) {
305a2e34c3dSBarry Smith     ierr = VecDuplicate(l,&r);CHKERRQ(ierr);
306a2e34c3dSBarry Smith     ierr = VecGetSize(l,&N);CHKERRQ(ierr);
307a2e34c3dSBarry Smith     sum  = 1.0/N;
3082dcb1b2aSMatthew Knepley     ierr = VecSet(l,sum);CHKERRQ(ierr);
309a2e34c3dSBarry Smith     ierr = MatMult(mat,l,r);CHKERRQ(ierr);
3108bb6bcc5SSatish Balay     ierr = VecNorm(r,NORM_2,&nrm);CHKERRQ(ierr);
311874288d9SMatthew G Knepley     if (flg1) {
31295902228SMatthew Knepley       if (nrm < 1.e-7) {
31395902228SMatthew Knepley         ierr = PetscPrintf(((PetscObject)sp)->comm,"Constants are likely null vector");CHKERRQ(ierr);
31495902228SMatthew Knepley       } else {
31595902228SMatthew Knepley         ierr = PetscPrintf(((PetscObject)sp)->comm,"Constants are unlikely null vector ");CHKERRQ(ierr);
31695902228SMatthew Knepley         consistent = PETSC_FALSE;
31795902228SMatthew Knepley       }
31831980aa1SBarry Smith       ierr = PetscPrintf(((PetscObject)sp)->comm,"|| A * 1/N || = %G\n",nrm);CHKERRQ(ierr);
319874288d9SMatthew G Knepley     }
3203050cee2SBarry Smith     if (nrm > 1.e-7 && flg1) {ierr = VecView(r,viewer);CHKERRQ(ierr);}
3213050cee2SBarry Smith     if (nrm > 1.e-7 && flg2) {ierr = VecView(r,viewer);CHKERRQ(ierr);}
3226bf464f9SBarry Smith     ierr = VecDestroy(&r);CHKERRQ(ierr);
323a2e34c3dSBarry Smith   }
324a2e34c3dSBarry Smith 
325a2e34c3dSBarry Smith   for (j=0; j<n; j++) {
326a2e34c3dSBarry Smith     ierr = (*mat->ops->mult)(mat,sp->vecs[j],l);CHKERRQ(ierr);
3278bb6bcc5SSatish Balay     ierr = VecNorm(l,NORM_2,&nrm);CHKERRQ(ierr);
328874288d9SMatthew G Knepley     if (flg1) {
32995902228SMatthew Knepley       if (nrm < 1.e-7) {
33095902228SMatthew Knepley         ierr = PetscPrintf(((PetscObject)sp)->comm,"Null vector %D is likely null vector",j);CHKERRQ(ierr);
33195902228SMatthew Knepley       } else {
33295902228SMatthew Knepley         ierr = PetscPrintf(((PetscObject)sp)->comm,"Null vector %D unlikely null vector ",j);CHKERRQ(ierr);
33395902228SMatthew Knepley         consistent = PETSC_FALSE;
33495902228SMatthew Knepley       }
3357adad957SLisandro Dalcin       ierr = PetscPrintf(((PetscObject)sp)->comm,"|| A * v[%D] || = %G\n",j,nrm);CHKERRQ(ierr);
336874288d9SMatthew G Knepley     }
3373050cee2SBarry Smith     if (nrm > 1.e-7 && flg1) {ierr = VecView(l,viewer);CHKERRQ(ierr);}
3383050cee2SBarry Smith     if (nrm > 1.e-7 && flg2) {ierr = VecView(l,viewer);CHKERRQ(ierr);}
339a2e34c3dSBarry Smith   }
340a2e34c3dSBarry Smith 
341ac53348aSBarry Smith   if (sp->remove) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Cannot test a null space provided as a function with MatNullSpaceSetFunction()");
34231980aa1SBarry Smith   if (isNull) *isNull = consistent;
343a2e34c3dSBarry Smith   PetscFunctionReturn(0);
344a2e34c3dSBarry Smith }
345a2e34c3dSBarry Smith 
346