xref: /petsc/src/mat/interface/matnull.c (revision ce0145b1747f2f9feb84b0d3a9d89d9b7fa16aac)
1be1d678aSKris Buschelman #define PETSCMAT_DLL
2be1d678aSKris Buschelman 
3f7765cecSBarry Smith /*
4b4fd4287SBarry Smith     Routines to project vectors out of null spaces.
5f7765cecSBarry Smith */
6f7765cecSBarry Smith 
75cfeda75SBarry Smith #include "src/mat/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
2272875594SBarry Smith -  remove - the function that removes the null space
2372875594SBarry Smith 
2472875594SBarry Smith .keywords: PC, null space, create
2572875594SBarry Smith 
2672875594SBarry Smith .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), KSPSetNullSpace(), MatNullSpace, MatNullSpaceCreate()
2772875594SBarry Smith @*/
28281d1b2eSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceSetFunction(MatNullSpace sp, PetscErrorCode (*remove)(Vec,void *ctx))
2972875594SBarry Smith {
3072875594SBarry Smith   PetscFunctionBegin;
3172875594SBarry Smith   sp->remove = remove;
32281d1b2eSBarry Smith   sp->rmctx  = ctx;
3372875594SBarry Smith   PetscFunctionReturn(0);
3472875594SBarry Smith }
3572875594SBarry Smith 
3672875594SBarry Smith #undef __FUNCT__
374a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceCreate"
38112a2221SBarry Smith /*@C
395cfeda75SBarry Smith    MatNullSpaceCreate - Creates a data structure used to project vectors
40b4fd4287SBarry Smith    out of null spaces.
41f7765cecSBarry Smith 
424e472627SLois Curfman McInnes    Collective on MPI_Comm
434e472627SLois Curfman McInnes 
44f7765cecSBarry Smith    Input Parameters:
4583c3bef8SLois Curfman McInnes +  comm - the MPI communicator associated with the object
4683c3bef8SLois Curfman McInnes .  has_cnst - PETSC_TRUE if the null space contains the constant vector; otherwise PETSC_FALSE
47b4fd4287SBarry Smith .  n - number of vectors (excluding constant vector) in null space
4883c3bef8SLois Curfman McInnes -  vecs - the vectors that span the null space (excluding the constant vector);
49f7a9e4ceSBarry Smith           these vectors must be orthonormal. These vectors are NOT copied, so do not change them
50f7a9e4ceSBarry Smith           after this call. You should free the array that you pass in.
51f7765cecSBarry Smith 
52f7765cecSBarry Smith    Output Parameter:
53b4fd4287SBarry Smith .  SP - the null space context
54f7765cecSBarry Smith 
5583c3bef8SLois Curfman McInnes    Level: advanced
5683c3bef8SLois Curfman McInnes 
576e1639daSBarry Smith   Users manual sections:
586e1639daSBarry Smith .   sec_singular
596e1639daSBarry Smith 
6083c3bef8SLois Curfman McInnes .keywords: PC, null space, create
6141a59933SSatish Balay 
6272875594SBarry Smith .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), KSPSetNullSpace(), MatNullSpace, MatNullSpaceSetFunction()
63f7765cecSBarry Smith @*/
64be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceCreate(MPI_Comm comm,PetscTruth has_cnst,PetscInt n,const Vec vecs[],MatNullSpace *SP)
65f7765cecSBarry Smith {
665cfeda75SBarry Smith   MatNullSpace   sp;
67dfbe8321SBarry Smith   PetscErrorCode ierr;
68c1ac3661SBarry Smith   PetscInt       i;
69f7765cecSBarry Smith 
703a40ed3dSBarry Smith   PetscFunctionBegin;
7152e6d16bSBarry Smith   ierr = PetscHeaderCreate(sp,_p_MatNullSpace,int,MAT_NULLSPACE_COOKIE,0,"MatNullSpace",comm,MatNullSpaceDestroy,0);CHKERRQ(ierr);
7252e6d16bSBarry Smith   ierr = PetscLogObjectMemory(sp,sizeof(struct _p_MatNullSpace));CHKERRQ(ierr);
73f7765cecSBarry Smith 
74b4fd4287SBarry Smith   sp->has_cnst = has_cnst;
75b4fd4287SBarry Smith   sp->n        = n;
765cfeda75SBarry Smith   sp->vec      = PETSC_NULL;
77f7a9e4ceSBarry Smith   if (n) {
78f7a9e4ceSBarry Smith     ierr = PetscMalloc(n*sizeof(Vec),&sp->vecs);CHKERRQ(ierr);
79f7a9e4ceSBarry Smith     for (i=0; i<n; i++) sp->vecs[i] = vecs[i];
80f7a9e4ceSBarry Smith   } else {
81f7a9e4ceSBarry Smith     sp->vecs = 0;
82f7a9e4ceSBarry Smith   }
83b4fd4287SBarry Smith 
84f7a9e4ceSBarry Smith   for (i=0; i<n; i++) {
85f7a9e4ceSBarry Smith     ierr = PetscObjectReference((PetscObject)sp->vecs[i]);CHKERRQ(ierr);
86f7a9e4ceSBarry Smith   }
87b4fd4287SBarry Smith   *SP          = sp;
883a40ed3dSBarry Smith   PetscFunctionReturn(0);
89f7765cecSBarry Smith }
90f7765cecSBarry Smith 
914a2ae208SSatish Balay #undef __FUNCT__
924a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceDestroy"
93f7765cecSBarry Smith /*@
945cfeda75SBarry Smith    MatNullSpaceDestroy - Destroys a data structure used to project vectors
95b4fd4287SBarry Smith    out of null spaces.
96b4fd4287SBarry Smith 
975cfeda75SBarry Smith    Collective on MatNullSpace
984e472627SLois Curfman McInnes 
99b4fd4287SBarry Smith    Input Parameter:
100b9756687SLois Curfman McInnes .  sp - the null space context to be destroyed
101b9756687SLois Curfman McInnes 
102b9756687SLois Curfman McInnes    Level: advanced
103b4fd4287SBarry Smith 
10483c3bef8SLois Curfman McInnes .keywords: PC, null space, destroy
10541a59933SSatish Balay 
10672875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceRemove(), MatNullSpaceSetFunction()
107b4fd4287SBarry Smith @*/
108be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceDestroy(MatNullSpace sp)
109b4fd4287SBarry Smith {
110dfbe8321SBarry Smith   PetscErrorCode ierr;
11185614651SBarry Smith 
1125cfeda75SBarry Smith   PetscFunctionBegin;
11385614651SBarry Smith   if (--sp->refct > 0) PetscFunctionReturn(0);
11485614651SBarry Smith 
1155cfeda75SBarry Smith   if (sp->vec) {ierr = VecDestroy(sp->vec);CHKERRQ(ierr);}
116f7a9e4ceSBarry Smith   if (sp->vecs) {
117f7a9e4ceSBarry Smith     ierr = VecDestroyVecs(sp->vecs,sp->n);CHKERRQ(ierr);
118f7a9e4ceSBarry Smith   }
119d38fa0fbSBarry Smith   ierr = PetscHeaderDestroy(sp);CHKERRQ(ierr);
1203a40ed3dSBarry Smith   PetscFunctionReturn(0);
121b4fd4287SBarry Smith }
122b4fd4287SBarry Smith 
1234a2ae208SSatish Balay #undef __FUNCT__
1244a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceRemove"
125b4fd4287SBarry Smith /*@
1265cfeda75SBarry Smith    MatNullSpaceRemove - Removes all the components of a null space from a vector.
127f7765cecSBarry Smith 
1285cfeda75SBarry Smith    Collective on MatNullSpace
129f7765cecSBarry Smith 
1304e472627SLois Curfman McInnes    Input Parameters:
1314e472627SLois Curfman McInnes +  sp - the null space context
1324e7234bfSBarry Smith .  vec - the vector from which the null space is to be removed
1335fcf39f4SBarry Smith -  out - if this is requested (not PETSC_NULL) then this is a vector with the null space removed otherwise
1344e7234bfSBarry Smith          the removal is done in-place (in vec)
1354e7234bfSBarry Smith 
1364e7234bfSBarry Smith 
1374e472627SLois Curfman McInnes 
138b9756687SLois Curfman McInnes    Level: advanced
139b9756687SLois Curfman McInnes 
14083c3bef8SLois Curfman McInnes .keywords: PC, null space, remove
14141a59933SSatish Balay 
14272875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction()
143f7765cecSBarry Smith @*/
144be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceRemove(MatNullSpace sp,Vec vec,Vec *out)
145f7765cecSBarry Smith {
14687828ca2SBarry Smith   PetscScalar    sum;
1476849ba73SBarry Smith   PetscErrorCode ierr;
148c1ac3661SBarry Smith   PetscInt       j,n = sp->n,N;
1495cfeda75SBarry Smith   Vec            l = vec;
150f7765cecSBarry Smith 
1513a40ed3dSBarry Smith   PetscFunctionBegin;
1525cfeda75SBarry Smith   if (out) {
1535cfeda75SBarry Smith     if (!sp->vec) {
1545cfeda75SBarry Smith       ierr = VecDuplicate(vec,&sp->vec);CHKERRQ(ierr);
1555cfeda75SBarry Smith     }
1565cfeda75SBarry Smith     *out = sp->vec;
1575cfeda75SBarry Smith     ierr = VecCopy(vec,*out);CHKERRQ(ierr);
1585cfeda75SBarry Smith     l    = *out;
1595cfeda75SBarry Smith   }
1605cfeda75SBarry Smith 
161b4fd4287SBarry Smith   if (sp->has_cnst) {
1625cfeda75SBarry Smith     ierr = VecSum(l,&sum);CHKERRQ(ierr);
1635cfeda75SBarry Smith     ierr = VecGetSize(l,&N);CHKERRQ(ierr);
16418a7d68fSSatish Balay     sum  = sum/(-1.0*N);
1652dcb1b2aSMatthew Knepley     ierr = VecShift(l,sum);CHKERRQ(ierr);
166f7765cecSBarry Smith   }
167b4fd4287SBarry Smith 
168b4fd4287SBarry Smith   for (j=0; j<n; j++) {
1695cfeda75SBarry Smith     ierr = VecDot(l,sp->vecs[j],&sum);CHKERRQ(ierr);
170b4fd4287SBarry Smith     sum  = -sum;
1712dcb1b2aSMatthew Knepley     ierr = VecAXPY(l,sum,sp->vecs[j]);CHKERRQ(ierr);
172f7765cecSBarry Smith   }
173b4fd4287SBarry Smith 
17472875594SBarry Smith   if (sp->remove){
175*ce0145b1SBarry Smith     ierr = (*sp->remove)(l,sp->rmctx);
17672875594SBarry Smith   }
1773a40ed3dSBarry Smith   PetscFunctionReturn(0);
178f7765cecSBarry Smith }
179a2e34c3dSBarry Smith 
1804a2ae208SSatish Balay #undef __FUNCT__
1814a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceTest"
182a2e34c3dSBarry Smith /*@
183a2e34c3dSBarry Smith    MatNullSpaceTest  - Tests if the claimed null space is really a
184a2e34c3dSBarry Smith      null space of a matrix
185a2e34c3dSBarry Smith 
186a2e34c3dSBarry Smith    Collective on MatNullSpace
187a2e34c3dSBarry Smith 
188a2e34c3dSBarry Smith    Input Parameters:
189a2e34c3dSBarry Smith +  sp - the null space context
190a2e34c3dSBarry Smith -  mat - the matrix
191a2e34c3dSBarry Smith 
192a2e34c3dSBarry Smith    Level: advanced
193a2e34c3dSBarry Smith 
194a2e34c3dSBarry Smith .keywords: PC, null space, remove
195a2e34c3dSBarry Smith 
19672875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction()
197a2e34c3dSBarry Smith @*/
198be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatNullSpaceTest(MatNullSpace sp,Mat mat)
199a2e34c3dSBarry Smith {
20087828ca2SBarry Smith   PetscScalar    sum;
2018bb6bcc5SSatish Balay   PetscReal      nrm;
202c1ac3661SBarry Smith   PetscInt       j,n = sp->n,N,m;
2036849ba73SBarry Smith   PetscErrorCode ierr;
204a2e34c3dSBarry Smith   Vec            l,r;
205a2e34c3dSBarry Smith   MPI_Comm       comm = sp->comm;
206a2e34c3dSBarry Smith   PetscTruth     flg1,flg2;
207a2e34c3dSBarry Smith 
208a2e34c3dSBarry Smith   PetscFunctionBegin;
209b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_null_space_test_view",&flg1);CHKERRQ(ierr);
210b0a32e0cSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_null_space_test_view_draw",&flg2);CHKERRQ(ierr);
211a2e34c3dSBarry Smith 
212a2e34c3dSBarry Smith   if (!sp->vec) {
213a2e34c3dSBarry Smith     if (n) {
214a2e34c3dSBarry Smith       ierr = VecDuplicate(sp->vecs[0],&sp->vec);CHKERRQ(ierr);
215a2e34c3dSBarry Smith     } else {
216a2e34c3dSBarry Smith       ierr = MatGetLocalSize(mat,&m,PETSC_NULL);CHKERRQ(ierr);
217a2e34c3dSBarry Smith       ierr = VecCreateMPI(sp->comm,m,PETSC_DETERMINE,&sp->vec);CHKERRQ(ierr);
218a2e34c3dSBarry Smith     }
219a2e34c3dSBarry Smith   }
220a2e34c3dSBarry Smith   l    = sp->vec;
221a2e34c3dSBarry Smith 
222a2e34c3dSBarry Smith   if (sp->has_cnst) {
223a2e34c3dSBarry Smith     ierr = VecDuplicate(l,&r);CHKERRQ(ierr);
224a2e34c3dSBarry Smith     ierr = VecGetSize(l,&N);CHKERRQ(ierr);
225a2e34c3dSBarry Smith     sum  = 1.0/N;
2262dcb1b2aSMatthew Knepley     ierr = VecSet(l,sum);CHKERRQ(ierr);
227a2e34c3dSBarry Smith     ierr = MatMult(mat,l,r);CHKERRQ(ierr);
2288bb6bcc5SSatish Balay     ierr = VecNorm(r,NORM_2,&nrm);CHKERRQ(ierr);
2298bb6bcc5SSatish Balay     if (nrm < 1.e-7) {ierr = PetscPrintf(comm,"Constants are likely null vector");CHKERRQ(ierr);}
230a2e34c3dSBarry Smith     else {ierr = PetscPrintf(comm,"Constants are unlikely null vector ");CHKERRQ(ierr);}
2318bb6bcc5SSatish Balay     ierr = PetscPrintf(comm,"|| A * 1 || = %g\n",nrm);CHKERRQ(ierr);
232b0a32e0cSBarry Smith     if (nrm > 1.e-7 && flg1) {ierr = VecView(r,PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr);}
233b0a32e0cSBarry Smith     if (nrm > 1.e-7 && flg2) {ierr = VecView(r,PETSC_VIEWER_DRAW_(comm));CHKERRQ(ierr);}
234a2e34c3dSBarry Smith     ierr = VecDestroy(r);CHKERRQ(ierr);
235a2e34c3dSBarry Smith   }
236a2e34c3dSBarry Smith 
237a2e34c3dSBarry Smith   for (j=0; j<n; j++) {
238a2e34c3dSBarry Smith     ierr = (*mat->ops->mult)(mat,sp->vecs[j],l);CHKERRQ(ierr);
2398bb6bcc5SSatish Balay     ierr = VecNorm(l,NORM_2,&nrm);CHKERRQ(ierr);
24077431f27SBarry Smith     if (nrm < 1.e-7) {ierr = PetscPrintf(comm,"Null vector %D is likely null vector",j);CHKERRQ(ierr);}
24177431f27SBarry Smith     else {ierr = PetscPrintf(comm,"Null vector %D unlikely null vector ",j);CHKERRQ(ierr);}
24277431f27SBarry Smith     ierr = PetscPrintf(comm,"|| A * v[%D] || = %g\n",j,nrm);CHKERRQ(ierr);
243b0a32e0cSBarry Smith     if (nrm > 1.e-7 && flg1) {ierr = VecView(l,PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr);}
244b0a32e0cSBarry Smith     if (nrm > 1.e-7 && flg2) {ierr = VecView(l,PETSC_VIEWER_DRAW_(comm));CHKERRQ(ierr);}
245a2e34c3dSBarry Smith   }
246a2e34c3dSBarry Smith 
24772875594SBarry Smith   if (sp->remove){
24872875594SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Cannot test a null space provided as a function with MatNullSpaceSetFunction()");
24972875594SBarry Smith   }
25072875594SBarry Smith 
251a2e34c3dSBarry Smith   PetscFunctionReturn(0);
252a2e34c3dSBarry Smith }
253a2e34c3dSBarry Smith 
254