xref: /petsc/src/mat/interface/matnull.c (revision 7adad9577d7f34aa90d02da3975546c0571d04a7)
1be1d678aSKris Buschelman #define PETSCMAT_DLL
2be1d678aSKris Buschelman 
3f7765cecSBarry Smith /*
4b4fd4287SBarry Smith     Routines to project vectors out of null spaces.
5f7765cecSBarry Smith */
6f7765cecSBarry Smith 
7b9147fbbSdalcinl #include "include/private/matimpl.h"      /*I "petscmat.h" I*/
8e090d566SSatish Balay #include "petscsys.h"
9f7765cecSBarry Smith 
10be1d678aSKris Buschelman PetscCookie PETSCMAT_DLLEXPORT MAT_NULLSPACE_COOKIE = 0;
118ba1e511SMatthew Knepley 
124a2ae208SSatish Balay #undef __FUNCT__
1372875594SBarry Smith #define __FUNCT__ "MatNullSpaceSetFunction"
1472875594SBarry Smith /*@C
1572875594SBarry Smith    MatNullSpaceSetFunction - set a function that removes a null space from a vector
1672875594SBarry Smith    out of null spaces.
1772875594SBarry Smith 
1872875594SBarry Smith    Collective on MatNullSpace
1972875594SBarry Smith 
2072875594SBarry Smith    Input Parameters:
2172875594SBarry Smith +  sp - the null space object
229dbe9a8aSBarry Smith .  rem - the function that removes the null space
239dbe9a8aSBarry Smith -  ctx - context for the remove function
2472875594SBarry Smith 
25658c74aaSSatish Balay    Level: advanced
2672875594SBarry Smith 
27658c74aaSSatish Balay .keywords: PC, null space, create
28b47fd4b1SSatish Balay 
2972875594SBarry Smith .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), KSPSetNullSpace(), MatNullSpace, MatNullSpaceCreate()
3072875594SBarry Smith @*/
319dbe9a8aSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceSetFunction(MatNullSpace sp, PetscErrorCode (*rem)(Vec,void*),void *ctx)
3272875594SBarry Smith {
3372875594SBarry Smith   PetscFunctionBegin;
343cfa8680SLisandro Dalcin   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_COOKIE,1);
359dbe9a8aSBarry Smith   sp->remove = rem;
369dbe9a8aSBarry Smith   sp->rmctx  = ctx;
3772875594SBarry Smith   PetscFunctionReturn(0);
3872875594SBarry Smith }
3972875594SBarry Smith 
4072875594SBarry Smith #undef __FUNCT__
414a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceCreate"
42f39d8e23SSatish Balay /*@
435cfeda75SBarry Smith    MatNullSpaceCreate - Creates a data structure used to project vectors
44b4fd4287SBarry Smith    out of null spaces.
45f7765cecSBarry Smith 
464e472627SLois Curfman McInnes    Collective on MPI_Comm
474e472627SLois Curfman McInnes 
48f7765cecSBarry Smith    Input Parameters:
4983c3bef8SLois Curfman McInnes +  comm - the MPI communicator associated with the object
5083c3bef8SLois Curfman McInnes .  has_cnst - PETSC_TRUE if the null space contains the constant vector; otherwise PETSC_FALSE
51b4fd4287SBarry Smith .  n - number of vectors (excluding constant vector) in null space
5283c3bef8SLois Curfman McInnes -  vecs - the vectors that span the null space (excluding the constant vector);
53f7a9e4ceSBarry Smith           these vectors must be orthonormal. These vectors are NOT copied, so do not change them
54f7a9e4ceSBarry Smith           after this call. You should free the array that you pass in.
55f7765cecSBarry Smith 
56f7765cecSBarry Smith    Output Parameter:
57b4fd4287SBarry Smith .  SP - the null space context
58f7765cecSBarry Smith 
5983c3bef8SLois Curfman McInnes    Level: advanced
6083c3bef8SLois Curfman McInnes 
616e1639daSBarry Smith   Users manual sections:
626e1639daSBarry Smith .   sec_singular
636e1639daSBarry Smith 
6483c3bef8SLois Curfman McInnes .keywords: PC, null space, create
6541a59933SSatish Balay 
6672875594SBarry Smith .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), KSPSetNullSpace(), MatNullSpace, MatNullSpaceSetFunction()
67f7765cecSBarry Smith @*/
68be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceCreate(MPI_Comm comm,PetscTruth has_cnst,PetscInt n,const Vec vecs[],MatNullSpace *SP)
69f7765cecSBarry Smith {
705cfeda75SBarry Smith   MatNullSpace   sp;
71dfbe8321SBarry Smith   PetscErrorCode ierr;
72c1ac3661SBarry Smith   PetscInt       i;
73f7765cecSBarry Smith 
743a40ed3dSBarry Smith   PetscFunctionBegin;
757850f3fbSLisandro Dalcin   if (n < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Number of vectors (given %D) cannot be negative",n);
76574b3360SMatthew Knepley   if (n) PetscValidPointer(vecs,4);
77574b3360SMatthew Knepley   for (i=0; i<n; i++) PetscValidHeaderSpecific(vecs[i],VEC_COOKIE,4);
78574b3360SMatthew Knepley   PetscValidPointer(SP,5);
79574b3360SMatthew Knepley 
80574b3360SMatthew Knepley   *SP = PETSC_NULL;
81574b3360SMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
82574b3360SMatthew Knepley   ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr);
83574b3360SMatthew Knepley #endif
84574b3360SMatthew Knepley 
8552e6d16bSBarry Smith   ierr = PetscHeaderCreate(sp,_p_MatNullSpace,int,MAT_NULLSPACE_COOKIE,0,"MatNullSpace",comm,MatNullSpaceDestroy,0);CHKERRQ(ierr);
86f7765cecSBarry Smith 
87b4fd4287SBarry Smith   sp->has_cnst = has_cnst;
88b4fd4287SBarry Smith   sp->n        = n;
897850f3fbSLisandro Dalcin   sp->vecs     = 0;
907850f3fbSLisandro Dalcin   sp->alpha    = 0;
917850f3fbSLisandro Dalcin   sp->vec      = 0;
927850f3fbSLisandro Dalcin   sp->remove   = 0;
937850f3fbSLisandro Dalcin   sp->rmctx    = 0;
947850f3fbSLisandro Dalcin 
95f7a9e4ceSBarry Smith   if (n) {
96f7a9e4ceSBarry Smith     ierr = PetscMalloc(n*sizeof(Vec),&sp->vecs);CHKERRQ(ierr);
977850f3fbSLisandro Dalcin     ierr = PetscMalloc(n*sizeof(PetscScalar),&sp->alpha);CHKERRQ(ierr);
987850f3fbSLisandro Dalcin     ierr = PetscLogObjectMemory(sp,n*(sizeof(Vec)+sizeof(PetscScalar)));CHKERRQ(ierr);
997850f3fbSLisandro Dalcin     for (i=0; i<n; i++) {
1007850f3fbSLisandro Dalcin       ierr = PetscObjectReference((PetscObject)vecs[i]);CHKERRQ(ierr);
1017850f3fbSLisandro Dalcin       sp->vecs[i] = vecs[i];
1027850f3fbSLisandro Dalcin     }
103f7a9e4ceSBarry Smith   }
104b4fd4287SBarry Smith 
105b4fd4287SBarry Smith   *SP          = sp;
1063a40ed3dSBarry Smith   PetscFunctionReturn(0);
107f7765cecSBarry Smith }
108f7765cecSBarry Smith 
1094a2ae208SSatish Balay #undef __FUNCT__
1104a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceDestroy"
111f7765cecSBarry Smith /*@
1125cfeda75SBarry Smith    MatNullSpaceDestroy - Destroys a data structure used to project vectors
113b4fd4287SBarry Smith    out of null spaces.
114b4fd4287SBarry Smith 
1155cfeda75SBarry Smith    Collective on MatNullSpace
1164e472627SLois Curfman McInnes 
117b4fd4287SBarry Smith    Input Parameter:
118b9756687SLois Curfman McInnes .  sp - the null space context to be destroyed
119b9756687SLois Curfman McInnes 
120b9756687SLois Curfman McInnes    Level: advanced
121b4fd4287SBarry Smith 
12283c3bef8SLois Curfman McInnes .keywords: PC, null space, destroy
12341a59933SSatish Balay 
12472875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceRemove(), MatNullSpaceSetFunction()
125b4fd4287SBarry Smith @*/
126be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceDestroy(MatNullSpace sp)
127b4fd4287SBarry Smith {
128dfbe8321SBarry Smith   PetscErrorCode ierr;
12985614651SBarry Smith 
1305cfeda75SBarry Smith   PetscFunctionBegin;
1313cfa8680SLisandro Dalcin   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_COOKIE,1);
132*7adad957SLisandro Dalcin   if (--((PetscObject)sp)->refct > 0) PetscFunctionReturn(0);
13385614651SBarry Smith 
1345cfeda75SBarry Smith   if (sp->vec)  { ierr = VecDestroy(sp->vec);CHKERRQ(ierr); }
1357850f3fbSLisandro Dalcin   if (sp->vecs) { ierr = VecDestroyVecs(sp->vecs,sp->n);CHKERRQ(ierr); }
1367850f3fbSLisandro Dalcin   ierr = PetscFree(sp->alpha);CHKERRQ(ierr);
137d38fa0fbSBarry Smith   ierr = PetscHeaderDestroy(sp);CHKERRQ(ierr);
1383a40ed3dSBarry Smith   PetscFunctionReturn(0);
139b4fd4287SBarry Smith }
140b4fd4287SBarry Smith 
1414a2ae208SSatish Balay #undef __FUNCT__
1424a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceRemove"
143b4fd4287SBarry Smith /*@
1445cfeda75SBarry Smith    MatNullSpaceRemove - Removes all the components of a null space from a vector.
145f7765cecSBarry Smith 
1465cfeda75SBarry Smith    Collective on MatNullSpace
147f7765cecSBarry Smith 
1484e472627SLois Curfman McInnes    Input Parameters:
1494e472627SLois Curfman McInnes +  sp - the null space context
1504e7234bfSBarry Smith .  vec - the vector from which the null space is to be removed
1515fcf39f4SBarry Smith -  out - if this is requested (not PETSC_NULL) then this is a vector with the null space removed otherwise
1524e7234bfSBarry Smith          the removal is done in-place (in vec)
1534e7234bfSBarry Smith 
154db090513SMatthew Knepley    Note: The user is not responsible for the vector returned and should not destroy it.
1554e472627SLois Curfman McInnes 
156b9756687SLois Curfman McInnes    Level: advanced
157b9756687SLois Curfman McInnes 
15883c3bef8SLois Curfman McInnes .keywords: PC, null space, remove
15941a59933SSatish Balay 
16072875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction()
161f7765cecSBarry Smith @*/
162be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceRemove(MatNullSpace sp,Vec vec,Vec *out)
163f7765cecSBarry Smith {
16487828ca2SBarry Smith   PetscScalar    sum;
1657850f3fbSLisandro Dalcin   PetscInt       i,N;
1666849ba73SBarry Smith   PetscErrorCode ierr;
167f7765cecSBarry Smith 
1683a40ed3dSBarry Smith   PetscFunctionBegin;
1693cd8ff7eSMatthew Knepley   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_COOKIE,1);
1703cd8ff7eSMatthew Knepley   PetscValidHeaderSpecific(vec,VEC_COOKIE,2);
1713cd8ff7eSMatthew Knepley 
1725cfeda75SBarry Smith   if (out) {
1733cd8ff7eSMatthew Knepley     PetscValidPointer(out,3);
1745cfeda75SBarry Smith     if (!sp->vec) {
1755cfeda75SBarry Smith       ierr = VecDuplicate(vec,&sp->vec);CHKERRQ(ierr);
1767850f3fbSLisandro Dalcin       ierr = PetscLogObjectParent(sp,sp->vec);CHKERRQ(ierr);
1775cfeda75SBarry Smith     }
1787850f3fbSLisandro Dalcin     ierr = VecCopy(vec,sp->vec);CHKERRQ(ierr);
1797850f3fbSLisandro Dalcin     vec = *out = sp->vec;
1805cfeda75SBarry Smith   }
1815cfeda75SBarry Smith 
182b4fd4287SBarry Smith   if (sp->has_cnst) {
1837850f3fbSLisandro Dalcin     ierr = VecGetSize(vec,&N);CHKERRQ(ierr);
1847850f3fbSLisandro Dalcin     if (N > 0) {
1857850f3fbSLisandro Dalcin       ierr = VecSum(vec,&sum);CHKERRQ(ierr);
18618a7d68fSSatish Balay       sum  = sum/(-1.0*N);
1877850f3fbSLisandro Dalcin       ierr = VecShift(vec,sum);CHKERRQ(ierr);
1887850f3fbSLisandro Dalcin     }
189f7765cecSBarry Smith   }
190b4fd4287SBarry Smith 
1917850f3fbSLisandro Dalcin   if (sp->n) {
1927850f3fbSLisandro Dalcin     ierr = VecMDot(vec,sp->n,sp->vecs,sp->alpha);CHKERRQ(ierr);
1937850f3fbSLisandro Dalcin     for (i=0; i<sp->n; i++) sp->alpha[i] = -sp->alpha[i];
1947850f3fbSLisandro Dalcin     ierr = VecMAXPY(vec,sp->n,sp->alpha,sp->vecs);CHKERRQ(ierr);
195f7765cecSBarry Smith   }
196b4fd4287SBarry Smith 
19772875594SBarry Smith   if (sp->remove){
1987850f3fbSLisandro Dalcin     ierr = (*sp->remove)(vec,sp->rmctx);
19972875594SBarry Smith   }
2003a40ed3dSBarry Smith   PetscFunctionReturn(0);
201f7765cecSBarry Smith }
202a2e34c3dSBarry Smith 
2034a2ae208SSatish Balay #undef __FUNCT__
2044a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceTest"
205a2e34c3dSBarry Smith /*@
206a2e34c3dSBarry Smith    MatNullSpaceTest  - Tests if the claimed null space is really a
207a2e34c3dSBarry Smith      null space of a matrix
208a2e34c3dSBarry Smith 
209a2e34c3dSBarry Smith    Collective on MatNullSpace
210a2e34c3dSBarry Smith 
211a2e34c3dSBarry Smith    Input Parameters:
212a2e34c3dSBarry Smith +  sp - the null space context
213a2e34c3dSBarry Smith -  mat - the matrix
214a2e34c3dSBarry Smith 
215a2e34c3dSBarry Smith    Level: advanced
216a2e34c3dSBarry Smith 
217a2e34c3dSBarry Smith .keywords: PC, null space, remove
218a2e34c3dSBarry Smith 
21972875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction()
220a2e34c3dSBarry Smith @*/
221be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceTest(MatNullSpace sp,Mat mat)
222a2e34c3dSBarry Smith {
22387828ca2SBarry Smith   PetscScalar    sum;
2248bb6bcc5SSatish Balay   PetscReal      nrm;
2253cfa8680SLisandro Dalcin   PetscInt       j,n,N,m;
2266849ba73SBarry Smith   PetscErrorCode ierr;
227a2e34c3dSBarry Smith   Vec            l,r;
228a2e34c3dSBarry Smith   PetscTruth     flg1,flg2;
2293050cee2SBarry Smith   PetscViewer    viewer;
230a2e34c3dSBarry Smith 
231a2e34c3dSBarry Smith   PetscFunctionBegin;
2323cfa8680SLisandro Dalcin   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_COOKIE,1);
2333cfa8680SLisandro Dalcin   PetscValidHeaderSpecific(mat,MAT_COOKIE,2);
2343cfa8680SLisandro Dalcin   n = sp->n;
235b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_null_space_test_view",&flg1);CHKERRQ(ierr);
236b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_null_space_test_view_draw",&flg2);CHKERRQ(ierr);
237a2e34c3dSBarry Smith 
238a2e34c3dSBarry Smith   if (!sp->vec) {
239a2e34c3dSBarry Smith     if (n) {
240a2e34c3dSBarry Smith       ierr = VecDuplicate(sp->vecs[0],&sp->vec);CHKERRQ(ierr);
241a2e34c3dSBarry Smith     } else {
242a2e34c3dSBarry Smith       ierr = MatGetLocalSize(mat,&m,PETSC_NULL);CHKERRQ(ierr);
243*7adad957SLisandro Dalcin       ierr = VecCreateMPI(((PetscObject)sp)->comm,m,PETSC_DETERMINE,&sp->vec);CHKERRQ(ierr);
244a2e34c3dSBarry Smith     }
245a2e34c3dSBarry Smith   }
246a2e34c3dSBarry Smith   l    = sp->vec;
247a2e34c3dSBarry Smith 
248*7adad957SLisandro Dalcin   ierr = PetscViewerASCIIGetStdout(((PetscObject)sp)->comm,&viewer);CHKERRQ(ierr);
249a2e34c3dSBarry Smith   if (sp->has_cnst) {
250a2e34c3dSBarry Smith     ierr = VecDuplicate(l,&r);CHKERRQ(ierr);
251a2e34c3dSBarry Smith     ierr = VecGetSize(l,&N);CHKERRQ(ierr);
252a2e34c3dSBarry Smith     sum  = 1.0/N;
2532dcb1b2aSMatthew Knepley     ierr = VecSet(l,sum);CHKERRQ(ierr);
254a2e34c3dSBarry Smith     ierr = MatMult(mat,l,r);CHKERRQ(ierr);
2558bb6bcc5SSatish Balay     ierr = VecNorm(r,NORM_2,&nrm);CHKERRQ(ierr);
256*7adad957SLisandro Dalcin     if (nrm < 1.e-7) {ierr = PetscPrintf(((PetscObject)sp)->comm,"Constants are likely null vector");CHKERRQ(ierr);}
257*7adad957SLisandro Dalcin     else {ierr = PetscPrintf(((PetscObject)sp)->comm,"Constants are unlikely null vector ");CHKERRQ(ierr);}
258*7adad957SLisandro Dalcin     ierr = PetscPrintf(((PetscObject)sp)->comm,"|| A * 1 || = %G\n",nrm);CHKERRQ(ierr);
2593050cee2SBarry Smith     if (nrm > 1.e-7 && flg1) {ierr = VecView(r,viewer);CHKERRQ(ierr);}
2603050cee2SBarry Smith     if (nrm > 1.e-7 && flg2) {ierr = VecView(r,viewer);CHKERRQ(ierr);}
261a2e34c3dSBarry Smith     ierr = VecDestroy(r);CHKERRQ(ierr);
262a2e34c3dSBarry Smith   }
263a2e34c3dSBarry Smith 
264a2e34c3dSBarry Smith   for (j=0; j<n; j++) {
265a2e34c3dSBarry Smith     ierr = (*mat->ops->mult)(mat,sp->vecs[j],l);CHKERRQ(ierr);
2668bb6bcc5SSatish Balay     ierr = VecNorm(l,NORM_2,&nrm);CHKERRQ(ierr);
267*7adad957SLisandro Dalcin     if (nrm < 1.e-7) {ierr = PetscPrintf(((PetscObject)sp)->comm,"Null vector %D is likely null vector",j);CHKERRQ(ierr);}
268*7adad957SLisandro Dalcin     else {ierr = PetscPrintf(((PetscObject)sp)->comm,"Null vector %D unlikely null vector ",j);CHKERRQ(ierr);}
269*7adad957SLisandro Dalcin     ierr = PetscPrintf(((PetscObject)sp)->comm,"|| A * v[%D] || = %G\n",j,nrm);CHKERRQ(ierr);
2703050cee2SBarry Smith     if (nrm > 1.e-7 && flg1) {ierr = VecView(l,viewer);CHKERRQ(ierr);}
2713050cee2SBarry Smith     if (nrm > 1.e-7 && flg2) {ierr = VecView(l,viewer);CHKERRQ(ierr);}
272a2e34c3dSBarry Smith   }
273a2e34c3dSBarry Smith 
27472875594SBarry Smith   if (sp->remove){
27572875594SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Cannot test a null space provided as a function with MatNullSpaceSetFunction()");
27672875594SBarry Smith   }
27772875594SBarry Smith 
278a2e34c3dSBarry Smith   PetscFunctionReturn(0);
279a2e34c3dSBarry Smith }
280a2e34c3dSBarry Smith 
281