xref: /petsc/src/mat/interface/matnull.c (revision c6db04a5321582041def2b1e244c75985478b3ef)
1be1d678aSKris Buschelman 
2f7765cecSBarry Smith /*
3b4fd4287SBarry Smith     Routines to project vectors out of null spaces.
4f7765cecSBarry Smith */
5f7765cecSBarry Smith 
6*c6db04a5SJed 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__
394a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceCreate"
40f39d8e23SSatish Balay /*@
415cfeda75SBarry Smith    MatNullSpaceCreate - Creates a data structure used to project vectors
42b4fd4287SBarry Smith    out of null spaces.
43f7765cecSBarry Smith 
444e472627SLois Curfman McInnes    Collective on MPI_Comm
454e472627SLois Curfman McInnes 
46f7765cecSBarry Smith    Input Parameters:
4783c3bef8SLois Curfman McInnes +  comm - the MPI communicator associated with the object
4883c3bef8SLois Curfman McInnes .  has_cnst - PETSC_TRUE if the null space contains the constant vector; otherwise PETSC_FALSE
49b4fd4287SBarry Smith .  n - number of vectors (excluding constant vector) in null space
5083c3bef8SLois Curfman McInnes -  vecs - the vectors that span the null space (excluding the constant vector);
51f7a9e4ceSBarry Smith           these vectors must be orthonormal. These vectors are NOT copied, so do not change them
5273141a14SBarry Smith           after this call. You should free the array that you pass in and destroy the vectors (this will reduce the reference count
5373141a14SBarry Smith           for them by one).
54f7765cecSBarry Smith 
55f7765cecSBarry Smith    Output Parameter:
56b4fd4287SBarry Smith .  SP - the null space context
57f7765cecSBarry Smith 
5883c3bef8SLois Curfman McInnes    Level: advanced
5983c3bef8SLois Curfman McInnes 
6080bf1014SBarry Smith    Notes: See MatNullSpaceSetFunction() as an alternative way of providing the null space information instead of setting vecs.
6180bf1014SBarry Smith 
6280bf1014SBarry 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
6380bf1014SBarry Smith        need to pass in a function that eliminates the constant function into MatNullSpaceSetFunction().
6480bf1014SBarry Smith 
656e1639daSBarry Smith   Users manual sections:
666e1639daSBarry Smith .   sec_singular
676e1639daSBarry Smith 
6883c3bef8SLois Curfman McInnes .keywords: PC, null space, create
6941a59933SSatish Balay 
7072875594SBarry Smith .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), KSPSetNullSpace(), MatNullSpace, MatNullSpaceSetFunction()
71f7765cecSBarry Smith @*/
727087cfbeSBarry Smith PetscErrorCode  MatNullSpaceCreate(MPI_Comm comm,PetscBool  has_cnst,PetscInt n,const Vec vecs[],MatNullSpace *SP)
73f7765cecSBarry Smith {
745cfeda75SBarry Smith   MatNullSpace   sp;
75dfbe8321SBarry Smith   PetscErrorCode ierr;
76c1ac3661SBarry Smith   PetscInt       i;
77f7765cecSBarry Smith 
783a40ed3dSBarry Smith   PetscFunctionBegin;
79e32f2f54SBarry Smith   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of vectors (given %D) cannot be negative",n);
80574b3360SMatthew Knepley   if (n) PetscValidPointer(vecs,4);
810700a824SBarry Smith   for (i=0; i<n; i++) PetscValidHeaderSpecific(vecs[i],VEC_CLASSID,4);
82574b3360SMatthew Knepley   PetscValidPointer(SP,5);
83574b3360SMatthew Knepley 
84574b3360SMatthew Knepley   *SP = PETSC_NULL;
85574b3360SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
86574b3360SMatthew Knepley   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
87574b3360SMatthew Knepley #endif
88574b3360SMatthew Knepley 
890700a824SBarry Smith   ierr = PetscHeaderCreate(sp,_p_MatNullSpace,int,MAT_NULLSPACE_CLASSID,0,"MatNullSpace",comm,MatNullSpaceDestroy,0);CHKERRQ(ierr);
90f7765cecSBarry Smith 
91b4fd4287SBarry Smith   sp->has_cnst = has_cnst;
92b4fd4287SBarry Smith   sp->n        = n;
937850f3fbSLisandro Dalcin   sp->vecs     = 0;
947850f3fbSLisandro Dalcin   sp->alpha    = 0;
957850f3fbSLisandro Dalcin   sp->vec      = 0;
967850f3fbSLisandro Dalcin   sp->remove   = 0;
977850f3fbSLisandro Dalcin   sp->rmctx    = 0;
987850f3fbSLisandro Dalcin 
99f7a9e4ceSBarry Smith   if (n) {
100f7a9e4ceSBarry Smith     ierr = PetscMalloc(n*sizeof(Vec),&sp->vecs);CHKERRQ(ierr);
1017850f3fbSLisandro Dalcin     ierr = PetscMalloc(n*sizeof(PetscScalar),&sp->alpha);CHKERRQ(ierr);
1027850f3fbSLisandro Dalcin     ierr = PetscLogObjectMemory(sp,n*(sizeof(Vec)+sizeof(PetscScalar)));CHKERRQ(ierr);
1037850f3fbSLisandro Dalcin     for (i=0; i<n; i++) {
1047850f3fbSLisandro Dalcin       ierr = PetscObjectReference((PetscObject)vecs[i]);CHKERRQ(ierr);
1057850f3fbSLisandro Dalcin       sp->vecs[i] = vecs[i];
1067850f3fbSLisandro Dalcin     }
107f7a9e4ceSBarry Smith   }
108b4fd4287SBarry Smith 
109b4fd4287SBarry Smith   *SP          = sp;
1103a40ed3dSBarry Smith   PetscFunctionReturn(0);
111f7765cecSBarry Smith }
112f7765cecSBarry Smith 
1134a2ae208SSatish Balay #undef __FUNCT__
1144a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceDestroy"
115f7765cecSBarry Smith /*@
1165cfeda75SBarry Smith    MatNullSpaceDestroy - Destroys a data structure used to project vectors
117b4fd4287SBarry Smith    out of null spaces.
118b4fd4287SBarry Smith 
1195cfeda75SBarry Smith    Collective on MatNullSpace
1204e472627SLois Curfman McInnes 
121b4fd4287SBarry Smith    Input Parameter:
122b9756687SLois Curfman McInnes .  sp - the null space context to be destroyed
123b9756687SLois Curfman McInnes 
124b9756687SLois Curfman McInnes    Level: advanced
125b4fd4287SBarry Smith 
12683c3bef8SLois Curfman McInnes .keywords: PC, null space, destroy
12741a59933SSatish Balay 
12872875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceRemove(), MatNullSpaceSetFunction()
129b4fd4287SBarry Smith @*/
1307087cfbeSBarry Smith PetscErrorCode  MatNullSpaceDestroy(MatNullSpace sp)
131b4fd4287SBarry Smith {
132dfbe8321SBarry Smith   PetscErrorCode ierr;
13385614651SBarry Smith 
1345cfeda75SBarry Smith   PetscFunctionBegin;
1350700a824SBarry Smith   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1);
1367adad957SLisandro Dalcin   if (--((PetscObject)sp)->refct > 0) PetscFunctionReturn(0);
13785614651SBarry Smith 
1385cfeda75SBarry Smith   if (sp->vec)  { ierr = VecDestroy(sp->vec);CHKERRQ(ierr); }
139574deadeSBarry Smith   if (sp->vecs) { ierr = VecDestroyVecs(sp->n,&sp->vecs);CHKERRQ(ierr); }
1407850f3fbSLisandro Dalcin   ierr = PetscFree(sp->alpha);CHKERRQ(ierr);
141d38fa0fbSBarry Smith   ierr = PetscHeaderDestroy(sp);CHKERRQ(ierr);
1423a40ed3dSBarry Smith   PetscFunctionReturn(0);
143b4fd4287SBarry Smith }
144b4fd4287SBarry Smith 
1454a2ae208SSatish Balay #undef __FUNCT__
1464a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceRemove"
147812c3f48SMatthew Knepley /*@C
1485cfeda75SBarry Smith    MatNullSpaceRemove - Removes all the components of a null space from a vector.
149f7765cecSBarry Smith 
1505cfeda75SBarry Smith    Collective on MatNullSpace
151f7765cecSBarry Smith 
1524e472627SLois Curfman McInnes    Input Parameters:
1534e472627SLois Curfman McInnes +  sp - the null space context
1544e7234bfSBarry Smith .  vec - the vector from which the null space is to be removed
1555fcf39f4SBarry Smith -  out - if this is requested (not PETSC_NULL) then this is a vector with the null space removed otherwise
1564e7234bfSBarry Smith          the removal is done in-place (in vec)
1574e7234bfSBarry Smith 
158db090513SMatthew Knepley    Note: The user is not responsible for the vector returned and should not destroy it.
1594e472627SLois Curfman McInnes 
160b9756687SLois Curfman McInnes    Level: advanced
161b9756687SLois Curfman McInnes 
16283c3bef8SLois Curfman McInnes .keywords: PC, null space, remove
16341a59933SSatish Balay 
16472875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction()
165f7765cecSBarry Smith @*/
1667087cfbeSBarry Smith PetscErrorCode  MatNullSpaceRemove(MatNullSpace sp,Vec vec,Vec *out)
167f7765cecSBarry Smith {
16887828ca2SBarry Smith   PetscScalar    sum;
1697850f3fbSLisandro Dalcin   PetscInt       i,N;
1706849ba73SBarry Smith   PetscErrorCode ierr;
171f7765cecSBarry Smith 
1723a40ed3dSBarry Smith   PetscFunctionBegin;
1730700a824SBarry Smith   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1);
1740700a824SBarry Smith   PetscValidHeaderSpecific(vec,VEC_CLASSID,2);
1753cd8ff7eSMatthew Knepley 
1765cfeda75SBarry Smith   if (out) {
1773cd8ff7eSMatthew Knepley     PetscValidPointer(out,3);
1785cfeda75SBarry Smith     if (!sp->vec) {
1795cfeda75SBarry Smith       ierr = VecDuplicate(vec,&sp->vec);CHKERRQ(ierr);
1807850f3fbSLisandro Dalcin       ierr = PetscLogObjectParent(sp,sp->vec);CHKERRQ(ierr);
1815cfeda75SBarry Smith     }
1827850f3fbSLisandro Dalcin     ierr = VecCopy(vec,sp->vec);CHKERRQ(ierr);
1837850f3fbSLisandro Dalcin     vec = *out = sp->vec;
1845cfeda75SBarry Smith   }
1855cfeda75SBarry Smith 
186b4fd4287SBarry Smith   if (sp->has_cnst) {
1877850f3fbSLisandro Dalcin     ierr = VecGetSize(vec,&N);CHKERRQ(ierr);
1887850f3fbSLisandro Dalcin     if (N > 0) {
1897850f3fbSLisandro Dalcin       ierr = VecSum(vec,&sum);CHKERRQ(ierr);
19018a7d68fSSatish Balay       sum  = sum/(-1.0*N);
1917850f3fbSLisandro Dalcin       ierr = VecShift(vec,sum);CHKERRQ(ierr);
1927850f3fbSLisandro Dalcin     }
193f7765cecSBarry Smith   }
194b4fd4287SBarry Smith 
1957850f3fbSLisandro Dalcin   if (sp->n) {
1967850f3fbSLisandro Dalcin     ierr = VecMDot(vec,sp->n,sp->vecs,sp->alpha);CHKERRQ(ierr);
1977850f3fbSLisandro Dalcin     for (i=0; i<sp->n; i++) sp->alpha[i] = -sp->alpha[i];
1987850f3fbSLisandro Dalcin     ierr = VecMAXPY(vec,sp->n,sp->alpha,sp->vecs);CHKERRQ(ierr);
199f7765cecSBarry Smith   }
200b4fd4287SBarry Smith 
20172875594SBarry Smith   if (sp->remove){
2020c3c4d68SMatthew Knepley     ierr = (*sp->remove)(sp,vec,sp->rmctx);CHKERRQ(ierr);
20372875594SBarry Smith   }
2043a40ed3dSBarry Smith   PetscFunctionReturn(0);
205f7765cecSBarry Smith }
206a2e34c3dSBarry Smith 
2074a2ae208SSatish Balay #undef __FUNCT__
2084a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceTest"
209a2e34c3dSBarry Smith /*@
210a2e34c3dSBarry Smith    MatNullSpaceTest  - Tests if the claimed null space is really a
211a2e34c3dSBarry Smith      null space of a matrix
212a2e34c3dSBarry Smith 
213a2e34c3dSBarry Smith    Collective on MatNullSpace
214a2e34c3dSBarry Smith 
215a2e34c3dSBarry Smith    Input Parameters:
216a2e34c3dSBarry Smith +  sp - the null space context
217a2e34c3dSBarry Smith -  mat - the matrix
218a2e34c3dSBarry Smith 
21995902228SMatthew Knepley    Output Parameters:
22095902228SMatthew Knepley .  isNull - PETSC_TRUE if the nullspace is valid for this matrix
22195902228SMatthew Knepley 
222a2e34c3dSBarry Smith    Level: advanced
223a2e34c3dSBarry Smith 
224a2e34c3dSBarry Smith .keywords: PC, null space, remove
225a2e34c3dSBarry Smith 
22672875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction()
227a2e34c3dSBarry Smith @*/
2287087cfbeSBarry Smith PetscErrorCode  MatNullSpaceTest(MatNullSpace sp,Mat mat,PetscBool  *isNull)
229a2e34c3dSBarry Smith {
23087828ca2SBarry Smith   PetscScalar    sum;
2318bb6bcc5SSatish Balay   PetscReal      nrm;
2320b12b109SJed Brown   PetscInt       j,n,N;
2336849ba73SBarry Smith   PetscErrorCode ierr;
234a2e34c3dSBarry Smith   Vec            l,r;
235ace3abfcSBarry Smith   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,consistent = PETSC_TRUE;
2363050cee2SBarry Smith   PetscViewer    viewer;
237a2e34c3dSBarry Smith 
238a2e34c3dSBarry Smith   PetscFunctionBegin;
2390700a824SBarry Smith   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1);
2400700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,2);
2413cfa8680SLisandro Dalcin   n = sp->n;
242acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-mat_null_space_test_view",&flg1,PETSC_NULL);CHKERRQ(ierr);
243acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-mat_null_space_test_view_draw",&flg2,PETSC_NULL);CHKERRQ(ierr);
244a2e34c3dSBarry Smith 
245a2e34c3dSBarry Smith   if (!sp->vec) {
246a2e34c3dSBarry Smith     if (n) {
247a2e34c3dSBarry Smith       ierr = VecDuplicate(sp->vecs[0],&sp->vec);CHKERRQ(ierr);
248a2e34c3dSBarry Smith     } else {
2490b12b109SJed Brown       ierr = MatGetVecs(mat,&sp->vec,PETSC_NULL);CHKERRQ(ierr);
250a2e34c3dSBarry Smith     }
251a2e34c3dSBarry Smith   }
252a2e34c3dSBarry Smith   l    = sp->vec;
253a2e34c3dSBarry Smith 
2547adad957SLisandro Dalcin   ierr = PetscViewerASCIIGetStdout(((PetscObject)sp)->comm,&viewer);CHKERRQ(ierr);
255a2e34c3dSBarry Smith   if (sp->has_cnst) {
256a2e34c3dSBarry Smith     ierr = VecDuplicate(l,&r);CHKERRQ(ierr);
257a2e34c3dSBarry Smith     ierr = VecGetSize(l,&N);CHKERRQ(ierr);
258a2e34c3dSBarry Smith     sum  = 1.0/N;
2592dcb1b2aSMatthew Knepley     ierr = VecSet(l,sum);CHKERRQ(ierr);
260a2e34c3dSBarry Smith     ierr = MatMult(mat,l,r);CHKERRQ(ierr);
2618bb6bcc5SSatish Balay     ierr = VecNorm(r,NORM_2,&nrm);CHKERRQ(ierr);
26295902228SMatthew Knepley     if (nrm < 1.e-7) {
26395902228SMatthew Knepley       ierr = PetscPrintf(((PetscObject)sp)->comm,"Constants are likely null vector");CHKERRQ(ierr);
26495902228SMatthew Knepley     } else {
26595902228SMatthew Knepley       ierr = PetscPrintf(((PetscObject)sp)->comm,"Constants are unlikely null vector ");CHKERRQ(ierr);
26695902228SMatthew Knepley       consistent = PETSC_FALSE;
26795902228SMatthew Knepley     }
26831980aa1SBarry Smith     ierr = PetscPrintf(((PetscObject)sp)->comm,"|| A * 1/N || = %G\n",nrm);CHKERRQ(ierr);
2693050cee2SBarry Smith     if (nrm > 1.e-7 && flg1) {ierr = VecView(r,viewer);CHKERRQ(ierr);}
2703050cee2SBarry Smith     if (nrm > 1.e-7 && flg2) {ierr = VecView(r,viewer);CHKERRQ(ierr);}
271a2e34c3dSBarry Smith     ierr = VecDestroy(r);CHKERRQ(ierr);
272a2e34c3dSBarry Smith   }
273a2e34c3dSBarry Smith 
274a2e34c3dSBarry Smith   for (j=0; j<n; j++) {
275a2e34c3dSBarry Smith     ierr = (*mat->ops->mult)(mat,sp->vecs[j],l);CHKERRQ(ierr);
2768bb6bcc5SSatish Balay     ierr = VecNorm(l,NORM_2,&nrm);CHKERRQ(ierr);
27795902228SMatthew Knepley     if (nrm < 1.e-7) {
27895902228SMatthew Knepley       ierr = PetscPrintf(((PetscObject)sp)->comm,"Null vector %D is likely null vector",j);CHKERRQ(ierr);
27995902228SMatthew Knepley     } else {
28095902228SMatthew Knepley       ierr = PetscPrintf(((PetscObject)sp)->comm,"Null vector %D unlikely null vector ",j);CHKERRQ(ierr);
28195902228SMatthew Knepley       consistent = PETSC_FALSE;
28295902228SMatthew Knepley     }
2837adad957SLisandro Dalcin     ierr = PetscPrintf(((PetscObject)sp)->comm,"|| A * v[%D] || = %G\n",j,nrm);CHKERRQ(ierr);
2843050cee2SBarry Smith     if (nrm > 1.e-7 && flg1) {ierr = VecView(l,viewer);CHKERRQ(ierr);}
2853050cee2SBarry Smith     if (nrm > 1.e-7 && flg2) {ierr = VecView(l,viewer);CHKERRQ(ierr);}
286a2e34c3dSBarry Smith   }
287a2e34c3dSBarry Smith 
288ac53348aSBarry Smith   if (sp->remove) SETERRQ(((PetscObject)mat)->comm,PETSC_ERR_SUP,"Cannot test a null space provided as a function with MatNullSpaceSetFunction()");
28931980aa1SBarry Smith   if (isNull) *isNull = consistent;
290a2e34c3dSBarry Smith   PetscFunctionReturn(0);
291a2e34c3dSBarry Smith }
292a2e34c3dSBarry Smith 
293