xref: /petsc/src/mat/interface/matnull.c (revision 0700a8246d308f50502909ba325e6169d3ee27eb)
1be1d678aSKris Buschelman #define PETSCMAT_DLL
2be1d678aSKris Buschelman 
3f7765cecSBarry Smith /*
4b4fd4287SBarry Smith     Routines to project vectors out of null spaces.
5f7765cecSBarry Smith */
6f7765cecSBarry Smith 
77c4f633dSBarry Smith #include "private/matimpl.h"      /*I "petscmat.h" I*/
8f7765cecSBarry Smith 
9*0700a824SBarry Smith PetscClassId PETSCMAT_DLLEXPORT MAT_NULLSPACE_CLASSID;
108ba1e511SMatthew Knepley 
114a2ae208SSatish Balay #undef __FUNCT__
1272875594SBarry Smith #define __FUNCT__ "MatNullSpaceSetFunction"
1372875594SBarry Smith /*@C
1472875594SBarry Smith    MatNullSpaceSetFunction - set a function that removes a null space from a vector
1572875594SBarry Smith    out of null spaces.
1672875594SBarry Smith 
1772875594SBarry Smith    Collective on MatNullSpace
1872875594SBarry Smith 
1972875594SBarry Smith    Input Parameters:
2072875594SBarry Smith +  sp - the null space object
219dbe9a8aSBarry Smith .  rem - the function that removes the null space
229dbe9a8aSBarry Smith -  ctx - context for the remove function
2372875594SBarry Smith 
24658c74aaSSatish Balay    Level: advanced
2572875594SBarry Smith 
26658c74aaSSatish Balay .keywords: PC, null space, create
27b47fd4b1SSatish Balay 
2872875594SBarry Smith .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), KSPSetNullSpace(), MatNullSpace, MatNullSpaceCreate()
2972875594SBarry Smith @*/
30b22b330cSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceSetFunction(MatNullSpace sp, PetscErrorCode (*rem)(MatNullSpace,Vec,void*),void *ctx)
3172875594SBarry Smith {
3272875594SBarry Smith   PetscFunctionBegin;
33*0700a824SBarry Smith   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1);
349dbe9a8aSBarry Smith   sp->remove = rem;
359dbe9a8aSBarry Smith   sp->rmctx  = ctx;
3672875594SBarry Smith   PetscFunctionReturn(0);
3772875594SBarry Smith }
3872875594SBarry Smith 
3972875594SBarry Smith #undef __FUNCT__
404a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceCreate"
41f39d8e23SSatish Balay /*@
425cfeda75SBarry Smith    MatNullSpaceCreate - Creates a data structure used to project vectors
43b4fd4287SBarry Smith    out of null spaces.
44f7765cecSBarry Smith 
454e472627SLois Curfman McInnes    Collective on MPI_Comm
464e472627SLois Curfman McInnes 
47f7765cecSBarry Smith    Input Parameters:
4883c3bef8SLois Curfman McInnes +  comm - the MPI communicator associated with the object
4983c3bef8SLois Curfman McInnes .  has_cnst - PETSC_TRUE if the null space contains the constant vector; otherwise PETSC_FALSE
50b4fd4287SBarry Smith .  n - number of vectors (excluding constant vector) in null space
5183c3bef8SLois Curfman McInnes -  vecs - the vectors that span the null space (excluding the constant vector);
52f7a9e4ceSBarry Smith           these vectors must be orthonormal. These vectors are NOT copied, so do not change them
5373141a14SBarry Smith           after this call. You should free the array that you pass in and destroy the vectors (this will reduce the reference count
5473141a14SBarry Smith           for them by one).
55f7765cecSBarry Smith 
56f7765cecSBarry Smith    Output Parameter:
57b4fd4287SBarry Smith .  SP - the null space context
58f7765cecSBarry Smith 
5983c3bef8SLois Curfman McInnes    Level: advanced
6083c3bef8SLois Curfman McInnes 
6180bf1014SBarry Smith    Notes: See MatNullSpaceSetFunction() as an alternative way of providing the null space information instead of setting vecs.
6280bf1014SBarry Smith 
6380bf1014SBarry 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
6480bf1014SBarry Smith        need to pass in a function that eliminates the constant function into MatNullSpaceSetFunction().
6580bf1014SBarry Smith 
666e1639daSBarry Smith   Users manual sections:
676e1639daSBarry Smith .   sec_singular
686e1639daSBarry Smith 
6983c3bef8SLois Curfman McInnes .keywords: PC, null space, create
7041a59933SSatish Balay 
7172875594SBarry Smith .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), KSPSetNullSpace(), MatNullSpace, MatNullSpaceSetFunction()
72f7765cecSBarry Smith @*/
73be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceCreate(MPI_Comm comm,PetscTruth has_cnst,PetscInt n,const Vec vecs[],MatNullSpace *SP)
74f7765cecSBarry Smith {
755cfeda75SBarry Smith   MatNullSpace   sp;
76dfbe8321SBarry Smith   PetscErrorCode ierr;
77c1ac3661SBarry Smith   PetscInt       i;
78f7765cecSBarry Smith 
793a40ed3dSBarry Smith   PetscFunctionBegin;
807850f3fbSLisandro Dalcin   if (n < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Number of vectors (given %D) cannot be negative",n);
81574b3360SMatthew Knepley   if (n) PetscValidPointer(vecs,4);
82*0700a824SBarry Smith   for (i=0; i<n; i++) PetscValidHeaderSpecific(vecs[i],VEC_CLASSID,4);
83574b3360SMatthew Knepley   PetscValidPointer(SP,5);
84574b3360SMatthew Knepley 
85574b3360SMatthew Knepley   *SP = PETSC_NULL;
86574b3360SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
87574b3360SMatthew Knepley   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
88574b3360SMatthew Knepley #endif
89574b3360SMatthew Knepley 
90*0700a824SBarry Smith   ierr = PetscHeaderCreate(sp,_p_MatNullSpace,int,MAT_NULLSPACE_CLASSID,0,"MatNullSpace",comm,MatNullSpaceDestroy,0);CHKERRQ(ierr);
91f7765cecSBarry Smith 
92b4fd4287SBarry Smith   sp->has_cnst = has_cnst;
93b4fd4287SBarry Smith   sp->n        = n;
947850f3fbSLisandro Dalcin   sp->vecs     = 0;
957850f3fbSLisandro Dalcin   sp->alpha    = 0;
967850f3fbSLisandro Dalcin   sp->vec      = 0;
977850f3fbSLisandro Dalcin   sp->remove   = 0;
987850f3fbSLisandro Dalcin   sp->rmctx    = 0;
997850f3fbSLisandro Dalcin 
100f7a9e4ceSBarry Smith   if (n) {
101f7a9e4ceSBarry Smith     ierr = PetscMalloc(n*sizeof(Vec),&sp->vecs);CHKERRQ(ierr);
1027850f3fbSLisandro Dalcin     ierr = PetscMalloc(n*sizeof(PetscScalar),&sp->alpha);CHKERRQ(ierr);
1037850f3fbSLisandro Dalcin     ierr = PetscLogObjectMemory(sp,n*(sizeof(Vec)+sizeof(PetscScalar)));CHKERRQ(ierr);
1047850f3fbSLisandro Dalcin     for (i=0; i<n; i++) {
1057850f3fbSLisandro Dalcin       ierr = PetscObjectReference((PetscObject)vecs[i]);CHKERRQ(ierr);
1067850f3fbSLisandro Dalcin       sp->vecs[i] = vecs[i];
1077850f3fbSLisandro Dalcin     }
108f7a9e4ceSBarry Smith   }
109b4fd4287SBarry Smith 
110b4fd4287SBarry Smith   *SP          = sp;
1113a40ed3dSBarry Smith   PetscFunctionReturn(0);
112f7765cecSBarry Smith }
113f7765cecSBarry Smith 
1144a2ae208SSatish Balay #undef __FUNCT__
1154a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceDestroy"
116f7765cecSBarry Smith /*@
1175cfeda75SBarry Smith    MatNullSpaceDestroy - Destroys a data structure used to project vectors
118b4fd4287SBarry Smith    out of null spaces.
119b4fd4287SBarry Smith 
1205cfeda75SBarry Smith    Collective on MatNullSpace
1214e472627SLois Curfman McInnes 
122b4fd4287SBarry Smith    Input Parameter:
123b9756687SLois Curfman McInnes .  sp - the null space context to be destroyed
124b9756687SLois Curfman McInnes 
125b9756687SLois Curfman McInnes    Level: advanced
126b4fd4287SBarry Smith 
12783c3bef8SLois Curfman McInnes .keywords: PC, null space, destroy
12841a59933SSatish Balay 
12972875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceRemove(), MatNullSpaceSetFunction()
130b4fd4287SBarry Smith @*/
131be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceDestroy(MatNullSpace sp)
132b4fd4287SBarry Smith {
133dfbe8321SBarry Smith   PetscErrorCode ierr;
13485614651SBarry Smith 
1355cfeda75SBarry Smith   PetscFunctionBegin;
136*0700a824SBarry Smith   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1);
1377adad957SLisandro Dalcin   if (--((PetscObject)sp)->refct > 0) PetscFunctionReturn(0);
13885614651SBarry Smith 
1395cfeda75SBarry Smith   if (sp->vec)  { ierr = VecDestroy(sp->vec);CHKERRQ(ierr); }
1407850f3fbSLisandro Dalcin   if (sp->vecs) { ierr = VecDestroyVecs(sp->vecs,sp->n);CHKERRQ(ierr); }
1417850f3fbSLisandro Dalcin   ierr = PetscFree(sp->alpha);CHKERRQ(ierr);
142d38fa0fbSBarry Smith   ierr = PetscHeaderDestroy(sp);CHKERRQ(ierr);
1433a40ed3dSBarry Smith   PetscFunctionReturn(0);
144b4fd4287SBarry Smith }
145b4fd4287SBarry Smith 
1464a2ae208SSatish Balay #undef __FUNCT__
1474a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceRemove"
148812c3f48SMatthew Knepley /*@C
1495cfeda75SBarry Smith    MatNullSpaceRemove - Removes all the components of a null space from a vector.
150f7765cecSBarry Smith 
1515cfeda75SBarry Smith    Collective on MatNullSpace
152f7765cecSBarry Smith 
1534e472627SLois Curfman McInnes    Input Parameters:
1544e472627SLois Curfman McInnes +  sp - the null space context
1554e7234bfSBarry Smith .  vec - the vector from which the null space is to be removed
1565fcf39f4SBarry Smith -  out - if this is requested (not PETSC_NULL) then this is a vector with the null space removed otherwise
1574e7234bfSBarry Smith          the removal is done in-place (in vec)
1584e7234bfSBarry Smith 
159db090513SMatthew Knepley    Note: The user is not responsible for the vector returned and should not destroy it.
1604e472627SLois Curfman McInnes 
161b9756687SLois Curfman McInnes    Level: advanced
162b9756687SLois Curfman McInnes 
16383c3bef8SLois Curfman McInnes .keywords: PC, null space, remove
16441a59933SSatish Balay 
16572875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction()
166f7765cecSBarry Smith @*/
167be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceRemove(MatNullSpace sp,Vec vec,Vec *out)
168f7765cecSBarry Smith {
16987828ca2SBarry Smith   PetscScalar    sum;
1707850f3fbSLisandro Dalcin   PetscInt       i,N;
1716849ba73SBarry Smith   PetscErrorCode ierr;
172f7765cecSBarry Smith 
1733a40ed3dSBarry Smith   PetscFunctionBegin;
174*0700a824SBarry Smith   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1);
175*0700a824SBarry Smith   PetscValidHeaderSpecific(vec,VEC_CLASSID,2);
1763cd8ff7eSMatthew Knepley 
1775cfeda75SBarry Smith   if (out) {
1783cd8ff7eSMatthew Knepley     PetscValidPointer(out,3);
1795cfeda75SBarry Smith     if (!sp->vec) {
1805cfeda75SBarry Smith       ierr = VecDuplicate(vec,&sp->vec);CHKERRQ(ierr);
1817850f3fbSLisandro Dalcin       ierr = PetscLogObjectParent(sp,sp->vec);CHKERRQ(ierr);
1825cfeda75SBarry Smith     }
1837850f3fbSLisandro Dalcin     ierr = VecCopy(vec,sp->vec);CHKERRQ(ierr);
1847850f3fbSLisandro Dalcin     vec = *out = sp->vec;
1855cfeda75SBarry Smith   }
1865cfeda75SBarry Smith 
187b4fd4287SBarry Smith   if (sp->has_cnst) {
1887850f3fbSLisandro Dalcin     ierr = VecGetSize(vec,&N);CHKERRQ(ierr);
1897850f3fbSLisandro Dalcin     if (N > 0) {
1907850f3fbSLisandro Dalcin       ierr = VecSum(vec,&sum);CHKERRQ(ierr);
19118a7d68fSSatish Balay       sum  = sum/(-1.0*N);
1927850f3fbSLisandro Dalcin       ierr = VecShift(vec,sum);CHKERRQ(ierr);
1937850f3fbSLisandro Dalcin     }
194f7765cecSBarry Smith   }
195b4fd4287SBarry Smith 
1967850f3fbSLisandro Dalcin   if (sp->n) {
1977850f3fbSLisandro Dalcin     ierr = VecMDot(vec,sp->n,sp->vecs,sp->alpha);CHKERRQ(ierr);
1987850f3fbSLisandro Dalcin     for (i=0; i<sp->n; i++) sp->alpha[i] = -sp->alpha[i];
1997850f3fbSLisandro Dalcin     ierr = VecMAXPY(vec,sp->n,sp->alpha,sp->vecs);CHKERRQ(ierr);
200f7765cecSBarry Smith   }
201b4fd4287SBarry Smith 
20272875594SBarry Smith   if (sp->remove){
2030c3c4d68SMatthew Knepley     ierr = (*sp->remove)(sp,vec,sp->rmctx);CHKERRQ(ierr);
20472875594SBarry Smith   }
2053a40ed3dSBarry Smith   PetscFunctionReturn(0);
206f7765cecSBarry Smith }
207a2e34c3dSBarry Smith 
2084a2ae208SSatish Balay #undef __FUNCT__
2094a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceTest"
210a2e34c3dSBarry Smith /*@
211a2e34c3dSBarry Smith    MatNullSpaceTest  - Tests if the claimed null space is really a
212a2e34c3dSBarry Smith      null space of a matrix
213a2e34c3dSBarry Smith 
214a2e34c3dSBarry Smith    Collective on MatNullSpace
215a2e34c3dSBarry Smith 
216a2e34c3dSBarry Smith    Input Parameters:
217a2e34c3dSBarry Smith +  sp - the null space context
218a2e34c3dSBarry Smith -  mat - the matrix
219a2e34c3dSBarry Smith 
22095902228SMatthew Knepley    Output Parameters:
22195902228SMatthew Knepley .  isNull - PETSC_TRUE if the nullspace is valid for this matrix
22295902228SMatthew Knepley 
223a2e34c3dSBarry Smith    Level: advanced
224a2e34c3dSBarry Smith 
225a2e34c3dSBarry Smith .keywords: PC, null space, remove
226a2e34c3dSBarry Smith 
22772875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction()
228a2e34c3dSBarry Smith @*/
22995902228SMatthew Knepley PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceTest(MatNullSpace sp,Mat mat,PetscTruth *isNull)
230a2e34c3dSBarry Smith {
23187828ca2SBarry Smith   PetscScalar    sum;
2328bb6bcc5SSatish Balay   PetscReal      nrm;
2330b12b109SJed Brown   PetscInt       j,n,N;
2346849ba73SBarry Smith   PetscErrorCode ierr;
235a2e34c3dSBarry Smith   Vec            l,r;
23690d69ab7SBarry Smith   PetscTruth     flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,consistent = PETSC_TRUE;
2373050cee2SBarry Smith   PetscViewer    viewer;
238a2e34c3dSBarry Smith 
239a2e34c3dSBarry Smith   PetscFunctionBegin;
240*0700a824SBarry Smith   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1);
241*0700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,2);
2423cfa8680SLisandro Dalcin   n = sp->n;
24390d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(PETSC_NULL,"-mat_null_space_test_view",&flg1,PETSC_NULL);CHKERRQ(ierr);
24490d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(PETSC_NULL,"-mat_null_space_test_view_draw",&flg2,PETSC_NULL);CHKERRQ(ierr);
245a2e34c3dSBarry Smith 
246a2e34c3dSBarry Smith   if (!sp->vec) {
247a2e34c3dSBarry Smith     if (n) {
248a2e34c3dSBarry Smith       ierr = VecDuplicate(sp->vecs[0],&sp->vec);CHKERRQ(ierr);
249a2e34c3dSBarry Smith     } else {
2500b12b109SJed Brown       ierr = MatGetVecs(mat,&sp->vec,PETSC_NULL);CHKERRQ(ierr);
251a2e34c3dSBarry Smith     }
252a2e34c3dSBarry Smith   }
253a2e34c3dSBarry Smith   l    = sp->vec;
254a2e34c3dSBarry Smith 
2557adad957SLisandro Dalcin   ierr = PetscViewerASCIIGetStdout(((PetscObject)sp)->comm,&viewer);CHKERRQ(ierr);
256a2e34c3dSBarry Smith   if (sp->has_cnst) {
257a2e34c3dSBarry Smith     ierr = VecDuplicate(l,&r);CHKERRQ(ierr);
258a2e34c3dSBarry Smith     ierr = VecGetSize(l,&N);CHKERRQ(ierr);
259a2e34c3dSBarry Smith     sum  = 1.0/N;
2602dcb1b2aSMatthew Knepley     ierr = VecSet(l,sum);CHKERRQ(ierr);
261a2e34c3dSBarry Smith     ierr = MatMult(mat,l,r);CHKERRQ(ierr);
2628bb6bcc5SSatish Balay     ierr = VecNorm(r,NORM_2,&nrm);CHKERRQ(ierr);
26395902228SMatthew Knepley     if (nrm < 1.e-7) {
26495902228SMatthew Knepley       ierr = PetscPrintf(((PetscObject)sp)->comm,"Constants are likely null vector");CHKERRQ(ierr);
26595902228SMatthew Knepley     } else {
26695902228SMatthew Knepley       ierr = PetscPrintf(((PetscObject)sp)->comm,"Constants are unlikely null vector ");CHKERRQ(ierr);
26795902228SMatthew Knepley       consistent = PETSC_FALSE;
26895902228SMatthew Knepley     }
2697adad957SLisandro Dalcin     ierr = PetscPrintf(((PetscObject)sp)->comm,"|| A * 1 || = %G\n",nrm);CHKERRQ(ierr);
2703050cee2SBarry Smith     if (nrm > 1.e-7 && flg1) {ierr = VecView(r,viewer);CHKERRQ(ierr);}
2713050cee2SBarry Smith     if (nrm > 1.e-7 && flg2) {ierr = VecView(r,viewer);CHKERRQ(ierr);}
272a2e34c3dSBarry Smith     ierr = VecDestroy(r);CHKERRQ(ierr);
273a2e34c3dSBarry Smith   }
274a2e34c3dSBarry Smith 
275a2e34c3dSBarry Smith   for (j=0; j<n; j++) {
276a2e34c3dSBarry Smith     ierr = (*mat->ops->mult)(mat,sp->vecs[j],l);CHKERRQ(ierr);
2778bb6bcc5SSatish Balay     ierr = VecNorm(l,NORM_2,&nrm);CHKERRQ(ierr);
27895902228SMatthew Knepley     if (nrm < 1.e-7) {
27995902228SMatthew Knepley       ierr = PetscPrintf(((PetscObject)sp)->comm,"Null vector %D is likely null vector",j);CHKERRQ(ierr);
28095902228SMatthew Knepley     } else {
28195902228SMatthew Knepley       ierr = PetscPrintf(((PetscObject)sp)->comm,"Null vector %D unlikely null vector ",j);CHKERRQ(ierr);
28295902228SMatthew Knepley       consistent = PETSC_FALSE;
28395902228SMatthew Knepley     }
2847adad957SLisandro Dalcin     ierr = PetscPrintf(((PetscObject)sp)->comm,"|| A * v[%D] || = %G\n",j,nrm);CHKERRQ(ierr);
2853050cee2SBarry Smith     if (nrm > 1.e-7 && flg1) {ierr = VecView(l,viewer);CHKERRQ(ierr);}
2863050cee2SBarry Smith     if (nrm > 1.e-7 && flg2) {ierr = VecView(l,viewer);CHKERRQ(ierr);}
287a2e34c3dSBarry Smith   }
288a2e34c3dSBarry Smith 
28972875594SBarry Smith   if (sp->remove){
29072875594SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Cannot test a null space provided as a function with MatNullSpaceSetFunction()");
29172875594SBarry Smith   }
29295902228SMatthew Knepley   *isNull = consistent;
293a2e34c3dSBarry Smith   PetscFunctionReturn(0);
294a2e34c3dSBarry Smith }
295a2e34c3dSBarry Smith 
296