xref: /petsc/src/mat/interface/matnull.c (revision 401b765a623475a82daee76c8a15a334bad9bc2f)
1be1d678aSKris Buschelman 
2f7765cecSBarry Smith /*
3b4fd4287SBarry Smith     Routines to project vectors out of null spaces.
4f7765cecSBarry Smith */
5f7765cecSBarry Smith 
6b45d2f2cSJed 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__
39009ec7a5SJed Brown #define __FUNCT__ "MatNullSpaceGetVecs"
40009ec7a5SJed Brown /*@C
41009ec7a5SJed Brown    MatNullSpaceGetVecs - get vectors defining the null space
42009ec7a5SJed Brown 
43009ec7a5SJed Brown    Not Collective
44009ec7a5SJed Brown 
45009ec7a5SJed Brown    Input Arguments:
46009ec7a5SJed Brown .  sp - null space object
47009ec7a5SJed Brown 
48009ec7a5SJed Brown    Output Arguments:
49009ec7a5SJed Brown +  has_cnst - PETSC_TRUE if the null space contains the constant vector, otherwise PETSC_FALSE
50009ec7a5SJed Brown .  n - number of vectors (excluding constant vector) in null space
51009ec7a5SJed Brown -  vecs - orthonormal vectors that span the null space (excluding the constant vector)
52009ec7a5SJed Brown 
53009ec7a5SJed Brown    Level: developer
54009ec7a5SJed Brown 
55009ec7a5SJed Brown .seealso: MatNullSpaceCreate(), MatGetNullSpace(), MatGetNearNullSpace()
56009ec7a5SJed Brown @*/
57009ec7a5SJed Brown PetscErrorCode MatNullSpaceGetVecs(MatNullSpace sp,PetscBool *has_const,PetscInt *n,const Vec **vecs)
58009ec7a5SJed Brown {
59009ec7a5SJed Brown 
60009ec7a5SJed Brown   PetscFunctionBegin;
61009ec7a5SJed Brown   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1);
62009ec7a5SJed Brown   if (has_const) *has_const = sp->has_cnst;
63009ec7a5SJed Brown   if (n) *n = sp->n;
64009ec7a5SJed Brown   if (vecs) *vecs = sp->vecs;
65009ec7a5SJed Brown   PetscFunctionReturn(0);
66009ec7a5SJed Brown }
67009ec7a5SJed Brown 
68009ec7a5SJed Brown #undef __FUNCT__
69009ec7a5SJed Brown #define __FUNCT__ "MatNullSpaceCreateRigidBody"
70009ec7a5SJed Brown /*@
71009ec7a5SJed Brown    MatNullSpaceCreateRigidBody - create rigid body modes from coordinates
72009ec7a5SJed Brown 
73009ec7a5SJed Brown    Collective on Vec
74009ec7a5SJed Brown 
75009ec7a5SJed Brown    Input Argument:
76009ec7a5SJed Brown .  coords - block of coordinates of each node, must have block size set
77009ec7a5SJed Brown 
78009ec7a5SJed Brown    Output Argument:
79009ec7a5SJed Brown .  sp - the null space
80009ec7a5SJed Brown 
81009ec7a5SJed Brown    Level: advanced
82009ec7a5SJed Brown 
83009ec7a5SJed Brown .seealso: MatNullSpaceCreate()
84009ec7a5SJed Brown @*/
85009ec7a5SJed Brown PetscErrorCode MatNullSpaceCreateRigidBody(Vec coords,MatNullSpace *sp)
86009ec7a5SJed Brown {
87009ec7a5SJed Brown   PetscErrorCode    ierr;
88009ec7a5SJed Brown   const PetscScalar *x;
89009ec7a5SJed Brown   PetscScalar       *v[6],dots[3];
90009ec7a5SJed Brown   Vec               vec[6];
91009ec7a5SJed Brown   PetscInt          n,N,dim,nmodes,i,j;
92009ec7a5SJed Brown 
93009ec7a5SJed Brown   PetscFunctionBegin;
94009ec7a5SJed Brown   ierr = VecGetBlockSize(coords,&dim);CHKERRQ(ierr);
95009ec7a5SJed Brown   ierr = VecGetLocalSize(coords,&n);CHKERRQ(ierr);
96009ec7a5SJed Brown   ierr = VecGetSize(coords,&N);CHKERRQ(ierr);
97009ec7a5SJed Brown   n   /= dim;
98009ec7a5SJed Brown   N   /= dim;
99009ec7a5SJed Brown   switch (dim) {
100009ec7a5SJed Brown   case 1:
101ce94432eSBarry Smith     ierr = MatNullSpaceCreate(PetscObjectComm((PetscObject)coords),PETSC_TRUE,0,NULL,sp);CHKERRQ(ierr);
102009ec7a5SJed Brown     break;
103009ec7a5SJed Brown   case 2:
104009ec7a5SJed Brown   case 3:
105009ec7a5SJed Brown     nmodes = (dim == 2) ? 3 : 6;
106ce94432eSBarry Smith     ierr   = VecCreate(PetscObjectComm((PetscObject)coords),&vec[0]);CHKERRQ(ierr);
107009ec7a5SJed Brown     ierr   = VecSetSizes(vec[0],dim*n,dim*N);CHKERRQ(ierr);
108009ec7a5SJed Brown     ierr   = VecSetBlockSize(vec[0],dim);CHKERRQ(ierr);
109009ec7a5SJed Brown     ierr   = VecSetUp(vec[0]);CHKERRQ(ierr);
110009ec7a5SJed Brown     for (i=1; i<nmodes; i++) {ierr = VecDuplicate(vec[0],&vec[i]);CHKERRQ(ierr);}
111009ec7a5SJed Brown     for (i=0; i<nmodes; i++) {ierr = VecGetArray(vec[i],&v[i]);CHKERRQ(ierr);}
112009ec7a5SJed Brown     ierr = VecGetArrayRead(coords,&x);CHKERRQ(ierr);
113009ec7a5SJed Brown     for (i=0; i<n; i++) {
114009ec7a5SJed Brown       if (dim == 2) {
115009ec7a5SJed Brown         v[0][i*2+0] = 1./N;
116009ec7a5SJed Brown         v[0][i*2+1] = 0.;
117009ec7a5SJed Brown         v[1][i*2+0] = 0.;
118009ec7a5SJed Brown         v[1][i*2+1] = 1./N;
119009ec7a5SJed Brown         /* Rotations */
120009ec7a5SJed Brown         v[2][i*2+0] = -x[i*2+1];
121009ec7a5SJed Brown         v[2][i*2+1] = x[i*2+0];
122009ec7a5SJed Brown       } else {
123009ec7a5SJed Brown         v[0][i*3+0] = 1./N;
124009ec7a5SJed Brown         v[0][i*3+1] = 0.;
125009ec7a5SJed Brown         v[0][i*3+2] = 0.;
126009ec7a5SJed Brown         v[1][i*3+0] = 0.;
127009ec7a5SJed Brown         v[1][i*3+1] = 1./N;
128009ec7a5SJed Brown         v[1][i*3+2] = 0.;
129009ec7a5SJed Brown         v[2][i*3+0] = 0.;
130009ec7a5SJed Brown         v[2][i*3+1] = 0.;
131009ec7a5SJed Brown         v[2][i*3+2] = 1./N;
132009ec7a5SJed Brown 
133009ec7a5SJed Brown         v[3][i*3+0] = x[i*3+1];
134009ec7a5SJed Brown         v[3][i*3+1] = -x[i*3+0];
135009ec7a5SJed Brown         v[3][i*3+2] = 0.;
136009ec7a5SJed Brown         v[4][i*3+0] = 0.;
137009ec7a5SJed Brown         v[4][i*3+1] = -x[i*3+2];
138009ec7a5SJed Brown         v[4][i*3+2] = x[i*3+1];
139009ec7a5SJed Brown         v[5][i*3+0] = x[i*3+2];
140009ec7a5SJed Brown         v[5][i*3+1] = 0.;
141009ec7a5SJed Brown         v[5][i*3+2] = -x[i*3+0];
142009ec7a5SJed Brown       }
143009ec7a5SJed Brown     }
144009ec7a5SJed Brown     for (i=0; i<nmodes; i++) {ierr = VecRestoreArray(vec[i],&v[i]);CHKERRQ(ierr);}
145009ec7a5SJed Brown     ierr = VecRestoreArrayRead(coords,&x);CHKERRQ(ierr);
146009ec7a5SJed Brown     for (i=dim; i<nmodes; i++) {
147009ec7a5SJed Brown       /* Orthonormalize vec[i] against vec[0:dim] */
148009ec7a5SJed Brown       ierr = VecMDot(vec[i],i,vec,dots);CHKERRQ(ierr);
149009ec7a5SJed Brown       for (j=0; j<i; j++) dots[j] *= -1.;
150009ec7a5SJed Brown       ierr = VecMAXPY(vec[i],i,dots,vec);CHKERRQ(ierr);
1510298fd71SBarry Smith       ierr = VecNormalize(vec[i],NULL);CHKERRQ(ierr);
152009ec7a5SJed Brown     }
153ce94432eSBarry Smith     ierr = MatNullSpaceCreate(PetscObjectComm((PetscObject)coords),PETSC_FALSE,nmodes,vec,sp);CHKERRQ(ierr);
154009ec7a5SJed Brown     for (i=0; i<nmodes; i++) {ierr = VecDestroy(&vec[i]);CHKERRQ(ierr);}
155009ec7a5SJed Brown   }
156009ec7a5SJed Brown   PetscFunctionReturn(0);
157009ec7a5SJed Brown }
158009ec7a5SJed Brown 
159009ec7a5SJed Brown #undef __FUNCT__
160f7357b39SLisandro Dalcin #define __FUNCT__ "MatNullSpaceView"
161b717e993SJed Brown /*@C
162b717e993SJed Brown    MatNullSpaceView - Visualizes a null space object.
163b717e993SJed Brown 
164b717e993SJed Brown    Collective on MatNullSpace
165b717e993SJed Brown 
166b717e993SJed Brown    Input Parameters:
167b717e993SJed Brown +  matnull - the null space
168b717e993SJed Brown -  viewer - visualization context
169b717e993SJed Brown 
170b717e993SJed Brown    Level: advanced
171b717e993SJed Brown 
172b717e993SJed Brown    Fortran Note:
173b717e993SJed Brown    This routine is not supported in Fortran.
174b717e993SJed Brown 
175b717e993SJed Brown .seealso: MatNullSpaceCreate(), PetscViewerASCIIOpen()
176b717e993SJed Brown @*/
177b717e993SJed Brown PetscErrorCode MatNullSpaceView(MatNullSpace sp,PetscViewer viewer)
178f7357b39SLisandro Dalcin {
179f7357b39SLisandro Dalcin   PetscErrorCode ierr;
180f7357b39SLisandro Dalcin   PetscBool      iascii;
181f7357b39SLisandro Dalcin 
182f7357b39SLisandro Dalcin   PetscFunctionBegin;
183f7357b39SLisandro Dalcin   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1);
184ce94432eSBarry Smith   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)sp));
185f7357b39SLisandro Dalcin   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
186f7357b39SLisandro Dalcin   PetscCheckSameComm(sp,1,viewer,2);
187f7357b39SLisandro Dalcin 
188251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
189f7357b39SLisandro Dalcin   if (iascii) {
19002cf292fSJed Brown     PetscViewerFormat format;
19102cf292fSJed Brown     PetscInt          i;
19202cf292fSJed Brown     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
193f7357b39SLisandro Dalcin     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)sp,viewer,"MatNullSpace Object");CHKERRQ(ierr);
19402cf292fSJed Brown     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
19502cf292fSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"Contains %D vector%s%s\n",sp->n,sp->n==1 ? "" : "s",sp->has_cnst ? " and the constant" : "");CHKERRQ(ierr);
19602cf292fSJed Brown     if (sp->remove) {ierr = PetscViewerASCIIPrintf(viewer,"Has user-provided removal function\n");CHKERRQ(ierr);}
19702cf292fSJed Brown     if (!(format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL)) {
19802cf292fSJed Brown       for (i=0; i<sp->n; i++) {
19902cf292fSJed Brown         ierr = VecView(sp->vecs[i],viewer);CHKERRQ(ierr);
20002cf292fSJed Brown       }
20102cf292fSJed Brown     }
20202cf292fSJed Brown     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
203f7357b39SLisandro Dalcin   }
204f7357b39SLisandro Dalcin   PetscFunctionReturn(0);
205f7357b39SLisandro Dalcin }
206f7357b39SLisandro Dalcin 
207f7357b39SLisandro Dalcin #undef __FUNCT__
2084a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceCreate"
209f39d8e23SSatish Balay /*@
2105cfeda75SBarry Smith    MatNullSpaceCreate - Creates a data structure used to project vectors
211b4fd4287SBarry Smith    out of null spaces.
212f7765cecSBarry Smith 
2134e472627SLois Curfman McInnes    Collective on MPI_Comm
2144e472627SLois Curfman McInnes 
215f7765cecSBarry Smith    Input Parameters:
21683c3bef8SLois Curfman McInnes +  comm - the MPI communicator associated with the object
21783c3bef8SLois Curfman McInnes .  has_cnst - PETSC_TRUE if the null space contains the constant vector; otherwise PETSC_FALSE
218b4fd4287SBarry Smith .  n - number of vectors (excluding constant vector) in null space
21983c3bef8SLois Curfman McInnes -  vecs - the vectors that span the null space (excluding the constant vector);
220f7a9e4ceSBarry Smith           these vectors must be orthonormal. These vectors are NOT copied, so do not change them
22173141a14SBarry Smith           after this call. You should free the array that you pass in and destroy the vectors (this will reduce the reference count
22273141a14SBarry Smith           for them by one).
223f7765cecSBarry Smith 
224f7765cecSBarry Smith    Output Parameter:
225b4fd4287SBarry Smith .  SP - the null space context
226f7765cecSBarry Smith 
22783c3bef8SLois Curfman McInnes    Level: advanced
22883c3bef8SLois Curfman McInnes 
22980bf1014SBarry Smith    Notes: See MatNullSpaceSetFunction() as an alternative way of providing the null space information instead of setting vecs.
23080bf1014SBarry Smith 
23180bf1014SBarry 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
23280bf1014SBarry Smith        need to pass in a function that eliminates the constant function into MatNullSpaceSetFunction().
23380bf1014SBarry Smith 
2346e1639daSBarry Smith   Users manual sections:
2356e1639daSBarry Smith .   sec_singular
2366e1639daSBarry Smith 
23783c3bef8SLois Curfman McInnes .keywords: PC, null space, create
23841a59933SSatish Balay 
23972875594SBarry Smith .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), KSPSetNullSpace(), MatNullSpace, MatNullSpaceSetFunction()
240f7765cecSBarry Smith @*/
2417087cfbeSBarry Smith PetscErrorCode  MatNullSpaceCreate(MPI_Comm comm,PetscBool has_cnst,PetscInt n,const Vec vecs[],MatNullSpace *SP)
242f7765cecSBarry Smith {
2435cfeda75SBarry Smith   MatNullSpace   sp;
244dfbe8321SBarry Smith   PetscErrorCode ierr;
245c1ac3661SBarry Smith   PetscInt       i;
246f7765cecSBarry Smith 
2473a40ed3dSBarry Smith   PetscFunctionBegin;
248e32f2f54SBarry Smith   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of vectors (given %D) cannot be negative",n);
249574b3360SMatthew Knepley   if (n) PetscValidPointer(vecs,4);
2500700a824SBarry Smith   for (i=0; i<n; i++) PetscValidHeaderSpecific(vecs[i],VEC_CLASSID,4);
251574b3360SMatthew Knepley   PetscValidPointer(SP,5);
252574b3360SMatthew Knepley 
2530298fd71SBarry Smith   *SP = NULL;
254519f805aSKarl Rupp #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
255607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
256574b3360SMatthew Knepley #endif
257574b3360SMatthew Knepley 
25867c2884eSBarry Smith   ierr = PetscHeaderCreate(sp,_p_MatNullSpace,int,MAT_NULLSPACE_CLASSID,"MatNullSpace","Null space","Mat",comm,MatNullSpaceDestroy,MatNullSpaceView);CHKERRQ(ierr);
259f7765cecSBarry Smith 
260b4fd4287SBarry Smith   sp->has_cnst = has_cnst;
261b4fd4287SBarry Smith   sp->n        = n;
2627850f3fbSLisandro Dalcin   sp->vecs     = 0;
2637850f3fbSLisandro Dalcin   sp->alpha    = 0;
2647850f3fbSLisandro Dalcin   sp->remove   = 0;
2657850f3fbSLisandro Dalcin   sp->rmctx    = 0;
2667850f3fbSLisandro Dalcin 
267f7a9e4ceSBarry Smith   if (n) {
268f7a9e4ceSBarry Smith     ierr = PetscMalloc(n*sizeof(Vec),&sp->vecs);CHKERRQ(ierr);
2697850f3fbSLisandro Dalcin     ierr = PetscMalloc(n*sizeof(PetscScalar),&sp->alpha);CHKERRQ(ierr);
2707850f3fbSLisandro Dalcin     ierr = PetscLogObjectMemory(sp,n*(sizeof(Vec)+sizeof(PetscScalar)));CHKERRQ(ierr);
2717850f3fbSLisandro Dalcin     for (i=0; i<n; i++) {
2727850f3fbSLisandro Dalcin       ierr        = PetscObjectReference((PetscObject)vecs[i]);CHKERRQ(ierr);
2737850f3fbSLisandro Dalcin       sp->vecs[i] = vecs[i];
2747850f3fbSLisandro Dalcin     }
275f7a9e4ceSBarry Smith   }
276b4fd4287SBarry Smith 
277b4fd4287SBarry Smith   *SP = sp;
2783a40ed3dSBarry Smith   PetscFunctionReturn(0);
279f7765cecSBarry Smith }
280f7765cecSBarry Smith 
2814a2ae208SSatish Balay #undef __FUNCT__
2824a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceDestroy"
283f7765cecSBarry Smith /*@
2845cfeda75SBarry Smith    MatNullSpaceDestroy - Destroys a data structure used to project vectors
285b4fd4287SBarry Smith    out of null spaces.
286b4fd4287SBarry Smith 
2875cfeda75SBarry Smith    Collective on MatNullSpace
2884e472627SLois Curfman McInnes 
289b4fd4287SBarry Smith    Input Parameter:
290b9756687SLois Curfman McInnes .  sp - the null space context to be destroyed
291b9756687SLois Curfman McInnes 
292b9756687SLois Curfman McInnes    Level: advanced
293b4fd4287SBarry Smith 
29483c3bef8SLois Curfman McInnes .keywords: PC, null space, destroy
29541a59933SSatish Balay 
29672875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceRemove(), MatNullSpaceSetFunction()
297b4fd4287SBarry Smith @*/
298d34fcf5fSBarry Smith PetscErrorCode  MatNullSpaceDestroy(MatNullSpace *sp)
299b4fd4287SBarry Smith {
300dfbe8321SBarry Smith   PetscErrorCode ierr;
30185614651SBarry Smith 
3025cfeda75SBarry Smith   PetscFunctionBegin;
3036bf464f9SBarry Smith   if (!*sp) PetscFunctionReturn(0);
304d34fcf5fSBarry Smith   PetscValidHeaderSpecific((*sp),MAT_NULLSPACE_CLASSID,1);
305d34fcf5fSBarry Smith   if (--((PetscObject)(*sp))->refct > 0) {*sp = 0; PetscFunctionReturn(0);}
30685614651SBarry Smith 
3076bf464f9SBarry Smith   ierr = VecDestroyVecs((*sp)->n,&(*sp)->vecs);CHKERRQ(ierr);
308d34fcf5fSBarry Smith   ierr = PetscFree((*sp)->alpha);CHKERRQ(ierr);
3096bf464f9SBarry Smith   ierr = PetscHeaderDestroy(sp);CHKERRQ(ierr);
3103a40ed3dSBarry Smith   PetscFunctionReturn(0);
311b4fd4287SBarry Smith }
312b4fd4287SBarry Smith 
3134a2ae208SSatish Balay #undef __FUNCT__
3144a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceRemove"
315812c3f48SMatthew Knepley /*@C
3165cfeda75SBarry Smith    MatNullSpaceRemove - Removes all the components of a null space from a vector.
317f7765cecSBarry Smith 
3185cfeda75SBarry Smith    Collective on MatNullSpace
319f7765cecSBarry Smith 
3204e472627SLois Curfman McInnes    Input Parameters:
3214e472627SLois Curfman McInnes +  sp - the null space context
3224e7234bfSBarry Smith .  vec - the vector from which the null space is to be removed
3230298fd71SBarry Smith -  out - if this is requested (not NULL) then this is a vector with the null space removed otherwise
3244e7234bfSBarry Smith          the removal is done in-place (in vec)
3254e7234bfSBarry Smith 
326db090513SMatthew Knepley    Note: The user is not responsible for the vector returned and should not destroy it.
3274e472627SLois Curfman McInnes 
328b9756687SLois Curfman McInnes    Level: advanced
329b9756687SLois Curfman McInnes 
33083c3bef8SLois Curfman McInnes .keywords: PC, null space, remove
33141a59933SSatish Balay 
33272875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction()
333f7765cecSBarry Smith @*/
334d0195637SJed Brown PetscErrorCode  MatNullSpaceRemove(MatNullSpace sp,Vec vec)
335f7765cecSBarry Smith {
33687828ca2SBarry Smith   PetscScalar    sum;
3377850f3fbSLisandro Dalcin   PetscInt       i,N;
3386849ba73SBarry Smith   PetscErrorCode ierr;
339f7765cecSBarry Smith 
3403a40ed3dSBarry Smith   PetscFunctionBegin;
3410700a824SBarry Smith   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1);
3420700a824SBarry Smith   PetscValidHeaderSpecific(vec,VEC_CLASSID,2);
3433cd8ff7eSMatthew Knepley 
344b4fd4287SBarry Smith   if (sp->has_cnst) {
3457850f3fbSLisandro Dalcin     ierr = VecGetSize(vec,&N);CHKERRQ(ierr);
3467850f3fbSLisandro Dalcin     if (N > 0) {
3477850f3fbSLisandro Dalcin       ierr = VecSum(vec,&sum);CHKERRQ(ierr);
348d4a378daSJed Brown       sum  = sum/((PetscScalar)(-1.0*N));
3497850f3fbSLisandro Dalcin       ierr = VecShift(vec,sum);CHKERRQ(ierr);
3507850f3fbSLisandro Dalcin     }
351f7765cecSBarry Smith   }
352b4fd4287SBarry Smith 
3537850f3fbSLisandro Dalcin   if (sp->n) {
3547850f3fbSLisandro Dalcin     ierr = VecMDot(vec,sp->n,sp->vecs,sp->alpha);CHKERRQ(ierr);
3557850f3fbSLisandro Dalcin     for (i=0; i<sp->n; i++) sp->alpha[i] = -sp->alpha[i];
3567850f3fbSLisandro Dalcin     ierr = VecMAXPY(vec,sp->n,sp->alpha,sp->vecs);CHKERRQ(ierr);
357f7765cecSBarry Smith   }
358b4fd4287SBarry Smith 
35972875594SBarry Smith   if (sp->remove) {
3600c3c4d68SMatthew Knepley     ierr = (*sp->remove)(sp,vec,sp->rmctx);CHKERRQ(ierr);
36172875594SBarry Smith   }
3623a40ed3dSBarry Smith   PetscFunctionReturn(0);
363f7765cecSBarry Smith }
364a2e34c3dSBarry Smith 
3654a2ae208SSatish Balay #undef __FUNCT__
3664a2ae208SSatish Balay #define __FUNCT__ "MatNullSpaceTest"
367a2e34c3dSBarry Smith /*@
368a2e34c3dSBarry Smith    MatNullSpaceTest  - Tests if the claimed null space is really a
369a2e34c3dSBarry Smith      null space of a matrix
370a2e34c3dSBarry Smith 
371a2e34c3dSBarry Smith    Collective on MatNullSpace
372a2e34c3dSBarry Smith 
373a2e34c3dSBarry Smith    Input Parameters:
374a2e34c3dSBarry Smith +  sp - the null space context
375a2e34c3dSBarry Smith -  mat - the matrix
376a2e34c3dSBarry Smith 
37795902228SMatthew Knepley    Output Parameters:
37895902228SMatthew Knepley .  isNull - PETSC_TRUE if the nullspace is valid for this matrix
37995902228SMatthew Knepley 
380a2e34c3dSBarry Smith    Level: advanced
381a2e34c3dSBarry Smith 
382a2e34c3dSBarry Smith .keywords: PC, null space, remove
383a2e34c3dSBarry Smith 
38472875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction()
385a2e34c3dSBarry Smith @*/
3867087cfbeSBarry Smith PetscErrorCode  MatNullSpaceTest(MatNullSpace sp,Mat mat,PetscBool  *isNull)
387a2e34c3dSBarry Smith {
38887828ca2SBarry Smith   PetscScalar    sum;
3898bb6bcc5SSatish Balay   PetscReal      nrm;
3900b12b109SJed Brown   PetscInt       j,n,N;
3916849ba73SBarry Smith   PetscErrorCode ierr;
392a2e34c3dSBarry Smith   Vec            l,r;
393ace3abfcSBarry Smith   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,consistent = PETSC_TRUE;
3943050cee2SBarry Smith   PetscViewer    viewer;
395a2e34c3dSBarry Smith 
396a2e34c3dSBarry Smith   PetscFunctionBegin;
3970700a824SBarry Smith   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1);
3980700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,2);
3993cfa8680SLisandro Dalcin   n    = sp->n;
4000298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-mat_null_space_test_view",&flg1,NULL);CHKERRQ(ierr);
4010298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-mat_null_space_test_view_draw",&flg2,NULL);CHKERRQ(ierr);
402a2e34c3dSBarry Smith 
403a2e34c3dSBarry Smith   if (n) {
404*401b765aSJed Brown     ierr = VecDuplicate(sp->vecs[0],&l);CHKERRQ(ierr);
405a2e34c3dSBarry Smith   } else {
406*401b765aSJed Brown     ierr = MatGetVecs(mat,&l,NULL);CHKERRQ(ierr);
407a2e34c3dSBarry Smith   }
408a2e34c3dSBarry Smith 
409ce94432eSBarry Smith   ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)sp),&viewer);CHKERRQ(ierr);
410a2e34c3dSBarry Smith   if (sp->has_cnst) {
411a2e34c3dSBarry Smith     ierr = VecDuplicate(l,&r);CHKERRQ(ierr);
412a2e34c3dSBarry Smith     ierr = VecGetSize(l,&N);CHKERRQ(ierr);
413a2e34c3dSBarry Smith     sum  = 1.0/N;
4142dcb1b2aSMatthew Knepley     ierr = VecSet(l,sum);CHKERRQ(ierr);
415a2e34c3dSBarry Smith     ierr = MatMult(mat,l,r);CHKERRQ(ierr);
4168bb6bcc5SSatish Balay     ierr = VecNorm(r,NORM_2,&nrm);CHKERRQ(ierr);
41718404f68SMatthew G Knepley     if (nrm >= 1.e-7) consistent = PETSC_FALSE;
418874288d9SMatthew G Knepley     if (flg1) {
41918404f68SMatthew G Knepley       if (consistent) {
420ce94432eSBarry Smith         ierr = PetscPrintf(PetscObjectComm((PetscObject)sp),"Constants are likely null vector");CHKERRQ(ierr);
42195902228SMatthew Knepley       } else {
422ce94432eSBarry Smith         ierr = PetscPrintf(PetscObjectComm((PetscObject)sp),"Constants are unlikely null vector ");CHKERRQ(ierr);
42395902228SMatthew Knepley       }
424ce94432eSBarry Smith       ierr = PetscPrintf(PetscObjectComm((PetscObject)sp),"|| A * 1/N || = %G\n",nrm);CHKERRQ(ierr);
425874288d9SMatthew G Knepley     }
42618404f68SMatthew G Knepley     if (!consistent && flg1) {ierr = VecView(r,viewer);CHKERRQ(ierr);}
42718404f68SMatthew G Knepley     if (!consistent && flg2) {ierr = VecView(r,viewer);CHKERRQ(ierr);}
4286bf464f9SBarry Smith     ierr = VecDestroy(&r);CHKERRQ(ierr);
429a2e34c3dSBarry Smith   }
430a2e34c3dSBarry Smith 
431a2e34c3dSBarry Smith   for (j=0; j<n; j++) {
432a2e34c3dSBarry Smith     ierr = (*mat->ops->mult)(mat,sp->vecs[j],l);CHKERRQ(ierr);
4338bb6bcc5SSatish Balay     ierr = VecNorm(l,NORM_2,&nrm);CHKERRQ(ierr);
43418404f68SMatthew G Knepley     if (nrm >= 1.e-7) consistent = PETSC_FALSE;
435874288d9SMatthew G Knepley     if (flg1) {
43618404f68SMatthew G Knepley       if (consistent) {
437ce94432eSBarry Smith         ierr = PetscPrintf(PetscObjectComm((PetscObject)sp),"Null vector %D is likely null vector",j);CHKERRQ(ierr);
43895902228SMatthew Knepley       } else {
439ce94432eSBarry Smith         ierr       = PetscPrintf(PetscObjectComm((PetscObject)sp),"Null vector %D unlikely null vector ",j);CHKERRQ(ierr);
44095902228SMatthew Knepley         consistent = PETSC_FALSE;
44195902228SMatthew Knepley       }
442ce94432eSBarry Smith       ierr = PetscPrintf(PetscObjectComm((PetscObject)sp),"|| A * v[%D] || = %G\n",j,nrm);CHKERRQ(ierr);
443874288d9SMatthew G Knepley     }
44418404f68SMatthew G Knepley     if (!consistent && flg1) {ierr = VecView(l,viewer);CHKERRQ(ierr);}
44518404f68SMatthew G Knepley     if (!consistent && flg2) {ierr = VecView(l,viewer);CHKERRQ(ierr);}
446a2e34c3dSBarry Smith   }
447a2e34c3dSBarry Smith 
448ce94432eSBarry Smith   if (sp->remove) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot test a null space provided as a function with MatNullSpaceSetFunction()");
449*401b765aSJed Brown   ierr = VecDestroy(&l);CHKERRQ(ierr);
45031980aa1SBarry Smith   if (isNull) *isNull = consistent;
451a2e34c3dSBarry Smith   PetscFunctionReturn(0);
452a2e34c3dSBarry Smith }
453a2e34c3dSBarry Smith 
454