xref: /petsc/src/mat/interface/matnull.c (revision fb04ea6f11ece59b316ec3a56ccfc350c010061c)
1be1d678aSKris Buschelman 
2f7765cecSBarry Smith /*
3b4fd4287SBarry Smith     Routines to project vectors out of null spaces.
4f7765cecSBarry Smith */
5f7765cecSBarry Smith 
6af0996ceSBarry Smith #include <petsc/private/matimpl.h>      /*I "petscmat.h" I*/
7f7765cecSBarry Smith 
87087cfbeSBarry Smith PetscClassId MAT_NULLSPACE_CLASSID;
98ba1e511SMatthew Knepley 
1072875594SBarry Smith /*@C
1172875594SBarry Smith    MatNullSpaceSetFunction - set a function that removes a null space from a vector
1272875594SBarry Smith    out of null spaces.
1372875594SBarry Smith 
143f9fe445SBarry Smith    Logically Collective on MatNullSpace
1572875594SBarry Smith 
1672875594SBarry Smith    Input Parameters:
1772875594SBarry Smith +  sp - the null space object
189dbe9a8aSBarry Smith .  rem - the function that removes the null space
199dbe9a8aSBarry Smith -  ctx - context for the remove function
2072875594SBarry Smith 
21658c74aaSSatish Balay    Level: advanced
2272875594SBarry Smith 
23658c74aaSSatish Balay .keywords: PC, null space, create
24b47fd4b1SSatish Balay 
255fa7ec2dSBarry Smith .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), MatSetNullSpace(), MatNullSpace, MatNullSpaceCreate()
2672875594SBarry Smith @*/
277087cfbeSBarry Smith PetscErrorCode  MatNullSpaceSetFunction(MatNullSpace sp, PetscErrorCode (*rem)(MatNullSpace,Vec,void*),void *ctx)
2872875594SBarry Smith {
2972875594SBarry Smith   PetscFunctionBegin;
300700a824SBarry Smith   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1);
319dbe9a8aSBarry Smith   sp->remove = rem;
329dbe9a8aSBarry Smith   sp->rmctx  = ctx;
3372875594SBarry Smith   PetscFunctionReturn(0);
3472875594SBarry Smith }
3572875594SBarry Smith 
36009ec7a5SJed Brown /*@C
37009ec7a5SJed Brown    MatNullSpaceGetVecs - get vectors defining the null space
38009ec7a5SJed Brown 
39009ec7a5SJed Brown    Not Collective
40009ec7a5SJed Brown 
41009ec7a5SJed Brown    Input Arguments:
42009ec7a5SJed Brown .  sp - null space object
43009ec7a5SJed Brown 
44009ec7a5SJed Brown    Output Arguments:
45009ec7a5SJed Brown +  has_cnst - PETSC_TRUE if the null space contains the constant vector, otherwise PETSC_FALSE
46009ec7a5SJed Brown .  n - number of vectors (excluding constant vector) in null space
47009ec7a5SJed Brown -  vecs - orthonormal vectors that span the null space (excluding the constant vector)
48009ec7a5SJed Brown 
49009ec7a5SJed Brown    Level: developer
50009ec7a5SJed Brown 
512a7a6963SBarry Smith    Notes:
522a7a6963SBarry Smith       These vectors and the array are owned by the MatNullSpace and should not be destroyed or freeded by the caller
532a7a6963SBarry Smith 
54009ec7a5SJed Brown .seealso: MatNullSpaceCreate(), MatGetNullSpace(), MatGetNearNullSpace()
55009ec7a5SJed Brown @*/
56009ec7a5SJed Brown PetscErrorCode MatNullSpaceGetVecs(MatNullSpace sp,PetscBool *has_const,PetscInt *n,const Vec **vecs)
57009ec7a5SJed Brown {
58009ec7a5SJed Brown 
59009ec7a5SJed Brown   PetscFunctionBegin;
60009ec7a5SJed Brown   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1);
61009ec7a5SJed Brown   if (has_const) *has_const = sp->has_cnst;
62009ec7a5SJed Brown   if (n) *n = sp->n;
63009ec7a5SJed Brown   if (vecs) *vecs = sp->vecs;
64009ec7a5SJed Brown   PetscFunctionReturn(0);
65009ec7a5SJed Brown }
66009ec7a5SJed Brown 
67009ec7a5SJed Brown /*@
68009ec7a5SJed Brown    MatNullSpaceCreateRigidBody - create rigid body modes from coordinates
69009ec7a5SJed Brown 
70009ec7a5SJed Brown    Collective on Vec
71009ec7a5SJed Brown 
72009ec7a5SJed Brown    Input Argument:
73009ec7a5SJed Brown .  coords - block of coordinates of each node, must have block size set
74009ec7a5SJed Brown 
75009ec7a5SJed Brown    Output Argument:
76009ec7a5SJed Brown .  sp - the null space
77009ec7a5SJed Brown 
78009ec7a5SJed Brown    Level: advanced
79009ec7a5SJed Brown 
8095452b02SPatrick Sanan    Notes:
8195452b02SPatrick Sanan     If you are solving an elasticity problems you should likely use this, in conjunction with ee MatSetNearNullspace(), to provide information that
8240220148SBarry Smith            the PCGAMG preconditioner can use to construct a much more efficient preconditioner.
8340220148SBarry Smith 
8440220148SBarry Smith            If you are solving an elasticity problem with pure Neumann boundary conditions you can use this in conjunction with MatSetNullspace() to
8540220148SBarry Smith            provide this information to the linear solver so it can handle the null space appropriately in the linear solution.
8640220148SBarry Smith 
8740220148SBarry Smith 
8840220148SBarry Smith .seealso: MatNullSpaceCreate(), MatSetNearNullspace(), MatSetNullspace()
89009ec7a5SJed Brown @*/
90009ec7a5SJed Brown PetscErrorCode MatNullSpaceCreateRigidBody(Vec coords,MatNullSpace *sp)
91009ec7a5SJed Brown {
92009ec7a5SJed Brown   PetscErrorCode    ierr;
93009ec7a5SJed Brown   const PetscScalar *x;
94bee94d3eSJed Brown   PetscScalar       *v[6],dots[5];
95009ec7a5SJed Brown   Vec               vec[6];
96009ec7a5SJed Brown   PetscInt          n,N,dim,nmodes,i,j;
97eb7a2786SBarry Smith   PetscReal         sN;
98009ec7a5SJed Brown 
99009ec7a5SJed Brown   PetscFunctionBegin;
100009ec7a5SJed Brown   ierr = VecGetBlockSize(coords,&dim);CHKERRQ(ierr);
101009ec7a5SJed Brown   ierr = VecGetLocalSize(coords,&n);CHKERRQ(ierr);
102009ec7a5SJed Brown   ierr = VecGetSize(coords,&N);CHKERRQ(ierr);
103009ec7a5SJed Brown   n   /= dim;
104009ec7a5SJed Brown   N   /= dim;
105eb7a2786SBarry Smith   sN = 1./PetscSqrtReal((PetscReal)N);
106009ec7a5SJed Brown   switch (dim) {
107009ec7a5SJed Brown   case 1:
108ce94432eSBarry Smith     ierr = MatNullSpaceCreate(PetscObjectComm((PetscObject)coords),PETSC_TRUE,0,NULL,sp);CHKERRQ(ierr);
109009ec7a5SJed Brown     break;
110009ec7a5SJed Brown   case 2:
111009ec7a5SJed Brown   case 3:
112009ec7a5SJed Brown     nmodes = (dim == 2) ? 3 : 6;
113ce94432eSBarry Smith     ierr   = VecCreate(PetscObjectComm((PetscObject)coords),&vec[0]);CHKERRQ(ierr);
114009ec7a5SJed Brown     ierr   = VecSetSizes(vec[0],dim*n,dim*N);CHKERRQ(ierr);
115009ec7a5SJed Brown     ierr   = VecSetBlockSize(vec[0],dim);CHKERRQ(ierr);
116009ec7a5SJed Brown     ierr   = VecSetUp(vec[0]);CHKERRQ(ierr);
117009ec7a5SJed Brown     for (i=1; i<nmodes; i++) {ierr = VecDuplicate(vec[0],&vec[i]);CHKERRQ(ierr);}
118009ec7a5SJed Brown     for (i=0; i<nmodes; i++) {ierr = VecGetArray(vec[i],&v[i]);CHKERRQ(ierr);}
119009ec7a5SJed Brown     ierr = VecGetArrayRead(coords,&x);CHKERRQ(ierr);
120009ec7a5SJed Brown     for (i=0; i<n; i++) {
121009ec7a5SJed Brown       if (dim == 2) {
122eb7a2786SBarry Smith         v[0][i*2+0] = sN;
123009ec7a5SJed Brown         v[0][i*2+1] = 0.;
124009ec7a5SJed Brown         v[1][i*2+0] = 0.;
125eb7a2786SBarry Smith         v[1][i*2+1] = sN;
126009ec7a5SJed Brown         /* Rotations */
127009ec7a5SJed Brown         v[2][i*2+0] = -x[i*2+1];
128009ec7a5SJed Brown         v[2][i*2+1] = x[i*2+0];
129009ec7a5SJed Brown       } else {
130eb7a2786SBarry Smith         v[0][i*3+0] = sN;
131009ec7a5SJed Brown         v[0][i*3+1] = 0.;
132009ec7a5SJed Brown         v[0][i*3+2] = 0.;
133009ec7a5SJed Brown         v[1][i*3+0] = 0.;
134eb7a2786SBarry Smith         v[1][i*3+1] = sN;
135009ec7a5SJed Brown         v[1][i*3+2] = 0.;
136009ec7a5SJed Brown         v[2][i*3+0] = 0.;
137009ec7a5SJed Brown         v[2][i*3+1] = 0.;
138eb7a2786SBarry Smith         v[2][i*3+2] = sN;
139009ec7a5SJed Brown 
140009ec7a5SJed Brown         v[3][i*3+0] = x[i*3+1];
141009ec7a5SJed Brown         v[3][i*3+1] = -x[i*3+0];
142009ec7a5SJed Brown         v[3][i*3+2] = 0.;
143009ec7a5SJed Brown         v[4][i*3+0] = 0.;
144009ec7a5SJed Brown         v[4][i*3+1] = -x[i*3+2];
145009ec7a5SJed Brown         v[4][i*3+2] = x[i*3+1];
146009ec7a5SJed Brown         v[5][i*3+0] = x[i*3+2];
147009ec7a5SJed Brown         v[5][i*3+1] = 0.;
148009ec7a5SJed Brown         v[5][i*3+2] = -x[i*3+0];
149009ec7a5SJed Brown       }
150009ec7a5SJed Brown     }
151009ec7a5SJed Brown     for (i=0; i<nmodes; i++) {ierr = VecRestoreArray(vec[i],&v[i]);CHKERRQ(ierr);}
152009ec7a5SJed Brown     ierr = VecRestoreArrayRead(coords,&x);CHKERRQ(ierr);
153009ec7a5SJed Brown     for (i=dim; i<nmodes; i++) {
154bee94d3eSJed Brown       /* Orthonormalize vec[i] against vec[0:i-1] */
155009ec7a5SJed Brown       ierr = VecMDot(vec[i],i,vec,dots);CHKERRQ(ierr);
156009ec7a5SJed Brown       for (j=0; j<i; j++) dots[j] *= -1.;
157009ec7a5SJed Brown       ierr = VecMAXPY(vec[i],i,dots,vec);CHKERRQ(ierr);
1580298fd71SBarry Smith       ierr = VecNormalize(vec[i],NULL);CHKERRQ(ierr);
159009ec7a5SJed Brown     }
160ce94432eSBarry Smith     ierr = MatNullSpaceCreate(PetscObjectComm((PetscObject)coords),PETSC_FALSE,nmodes,vec,sp);CHKERRQ(ierr);
161009ec7a5SJed Brown     for (i=0; i<nmodes; i++) {ierr = VecDestroy(&vec[i]);CHKERRQ(ierr);}
162009ec7a5SJed Brown   }
163009ec7a5SJed Brown   PetscFunctionReturn(0);
164009ec7a5SJed Brown }
165009ec7a5SJed Brown 
166b717e993SJed Brown /*@C
167b717e993SJed Brown    MatNullSpaceView - Visualizes a null space object.
168b717e993SJed Brown 
169b717e993SJed Brown    Collective on MatNullSpace
170b717e993SJed Brown 
171b717e993SJed Brown    Input Parameters:
172b717e993SJed Brown +  matnull - the null space
173b717e993SJed Brown -  viewer - visualization context
174b717e993SJed Brown 
175b717e993SJed Brown    Level: advanced
176b717e993SJed Brown 
177b717e993SJed Brown    Fortran Note:
178b717e993SJed Brown    This routine is not supported in Fortran.
179b717e993SJed Brown 
180b717e993SJed Brown .seealso: MatNullSpaceCreate(), PetscViewerASCIIOpen()
181b717e993SJed Brown @*/
182b717e993SJed Brown PetscErrorCode MatNullSpaceView(MatNullSpace sp,PetscViewer viewer)
183f7357b39SLisandro Dalcin {
184f7357b39SLisandro Dalcin   PetscErrorCode ierr;
185f7357b39SLisandro Dalcin   PetscBool      iascii;
186f7357b39SLisandro Dalcin 
187f7357b39SLisandro Dalcin   PetscFunctionBegin;
188f7357b39SLisandro Dalcin   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1);
189f55353a2SBarry Smith   if (!viewer) {
190f55353a2SBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)sp),&viewer);CHKERRQ(ierr);
191f55353a2SBarry Smith   }
192f7357b39SLisandro Dalcin   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
193f7357b39SLisandro Dalcin   PetscCheckSameComm(sp,1,viewer,2);
194f7357b39SLisandro Dalcin 
195251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
196f7357b39SLisandro Dalcin   if (iascii) {
19702cf292fSJed Brown     PetscViewerFormat format;
19802cf292fSJed Brown     PetscInt          i;
19902cf292fSJed Brown     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
200dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)sp,viewer);CHKERRQ(ierr);
20102cf292fSJed Brown     ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
20202cf292fSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"Contains %D vector%s%s\n",sp->n,sp->n==1 ? "" : "s",sp->has_cnst ? " and the constant" : "");CHKERRQ(ierr);
20302cf292fSJed Brown     if (sp->remove) {ierr = PetscViewerASCIIPrintf(viewer,"Has user-provided removal function\n");CHKERRQ(ierr);}
20402cf292fSJed Brown     if (!(format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL)) {
20502cf292fSJed Brown       for (i=0; i<sp->n; i++) {
20602cf292fSJed Brown         ierr = VecView(sp->vecs[i],viewer);CHKERRQ(ierr);
20702cf292fSJed Brown       }
20802cf292fSJed Brown     }
20902cf292fSJed Brown     ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
210f7357b39SLisandro Dalcin   }
211f7357b39SLisandro Dalcin   PetscFunctionReturn(0);
212f7357b39SLisandro Dalcin }
213f7357b39SLisandro Dalcin 
214c3c607ccSBarry Smith /*@C
2155cfeda75SBarry Smith    MatNullSpaceCreate - Creates a data structure used to project vectors
216b4fd4287SBarry Smith    out of null spaces.
217f7765cecSBarry Smith 
2184e472627SLois Curfman McInnes    Collective on MPI_Comm
2194e472627SLois Curfman McInnes 
220f7765cecSBarry Smith    Input Parameters:
22183c3bef8SLois Curfman McInnes +  comm - the MPI communicator associated with the object
22283c3bef8SLois Curfman McInnes .  has_cnst - PETSC_TRUE if the null space contains the constant vector; otherwise PETSC_FALSE
223b4fd4287SBarry Smith .  n - number of vectors (excluding constant vector) in null space
22483c3bef8SLois Curfman McInnes -  vecs - the vectors that span the null space (excluding the constant vector);
225f7a9e4ceSBarry Smith           these vectors must be orthonormal. These vectors are NOT copied, so do not change them
22673141a14SBarry Smith           after this call. You should free the array that you pass in and destroy the vectors (this will reduce the reference count
22773141a14SBarry Smith           for them by one).
228f7765cecSBarry Smith 
229f7765cecSBarry Smith    Output Parameter:
230b4fd4287SBarry Smith .  SP - the null space context
231f7765cecSBarry Smith 
23283c3bef8SLois Curfman McInnes    Level: advanced
23383c3bef8SLois Curfman McInnes 
23495452b02SPatrick Sanan    Notes:
23595452b02SPatrick Sanan     See MatNullSpaceSetFunction() as an alternative way of providing the null space information instead of setting vecs.
23680bf1014SBarry Smith 
23780bf1014SBarry 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
23880bf1014SBarry Smith        need to pass in a function that eliminates the constant function into MatNullSpaceSetFunction().
23980bf1014SBarry Smith 
2406e1639daSBarry Smith   Users manual sections:
2416e1639daSBarry Smith .   sec_singular
2426e1639daSBarry Smith 
24383c3bef8SLois Curfman McInnes .keywords: PC, null space, create
24441a59933SSatish Balay 
2455fa7ec2dSBarry Smith .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), MatSetNullSpace(), MatNullSpace, MatNullSpaceSetFunction()
246f7765cecSBarry Smith @*/
2477087cfbeSBarry Smith PetscErrorCode  MatNullSpaceCreate(MPI_Comm comm,PetscBool has_cnst,PetscInt n,const Vec vecs[],MatNullSpace *SP)
248f7765cecSBarry Smith {
2495cfeda75SBarry Smith   MatNullSpace   sp;
250dfbe8321SBarry Smith   PetscErrorCode ierr;
251c1ac3661SBarry Smith   PetscInt       i;
252f7765cecSBarry Smith 
2533a40ed3dSBarry Smith   PetscFunctionBegin;
254e32f2f54SBarry Smith   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of vectors (given %D) cannot be negative",n);
255574b3360SMatthew Knepley   if (n) PetscValidPointer(vecs,4);
2560700a824SBarry Smith   for (i=0; i<n; i++) PetscValidHeaderSpecific(vecs[i],VEC_CLASSID,4);
257574b3360SMatthew Knepley   PetscValidPointer(SP,5);
2589d2471e0SBarry Smith   if (n) {
2599d2471e0SBarry Smith     for (i=0; i<n; i++) {
2609d2471e0SBarry Smith       /* prevent the user from changes values in the vector */
2619d2471e0SBarry Smith       ierr = VecLockPush(vecs[i]);CHKERRQ(ierr);
2629d2471e0SBarry Smith     }
2639d2471e0SBarry Smith   }
26496ded551SBarry Smith #if defined(PETSC_USE_DEBUG)
26596ded551SBarry Smith   if (n) {
26696ded551SBarry Smith     PetscScalar *dots;
26796ded551SBarry Smith     for (i=0; i<n; i++) {
26896ded551SBarry Smith       PetscReal norm;
26996ded551SBarry Smith       ierr = VecNorm(vecs[i],NORM_2,&norm);CHKERRQ(ierr);
270c068d9bbSLisandro Dalcin       if (PetscAbsReal(norm - 1) > PETSC_SQRT_MACHINE_EPSILON) SETERRQ2(PetscObjectComm((PetscObject)vecs[i]),PETSC_ERR_ARG_WRONG,"Vector %D must have 2-norm of 1.0, it is %g",i,(double)norm);
27196ded551SBarry Smith     }
27296ded551SBarry Smith     if (has_cnst) {
27396ded551SBarry Smith       for (i=0; i<n; i++) {
27496ded551SBarry Smith         PetscScalar sum;
27596ded551SBarry Smith         ierr = VecSum(vecs[i],&sum);CHKERRQ(ierr);
2761cb85adbSBarry Smith         if (PetscAbsScalar(sum) > PETSC_SQRT_MACHINE_EPSILON) SETERRQ2(PetscObjectComm((PetscObject)vecs[i]),PETSC_ERR_ARG_WRONG,"Vector %D must be orthogonal to constant vector, inner product is %g",i,(double)PetscAbsScalar(sum));
27796ded551SBarry Smith       }
27896ded551SBarry Smith     }
27996ded551SBarry Smith     ierr = PetscMalloc1(n-1,&dots);CHKERRQ(ierr);
28096ded551SBarry Smith     for (i=0; i<n-1; i++) {
28196ded551SBarry Smith       PetscInt j;
28296ded551SBarry Smith       ierr = VecMDot(vecs[i],n-i-1,vecs+i+1,dots);CHKERRQ(ierr);
28396ded551SBarry Smith       for (j=0;j<n-i-1;j++) {
2841cb85adbSBarry Smith         if (PetscAbsScalar(dots[j]) > PETSC_SQRT_MACHINE_EPSILON) SETERRQ3(PetscObjectComm((PetscObject)vecs[i]),PETSC_ERR_ARG_WRONG,"Vector %D must be orthogonal to vector %D, inner product is %g",i,i+j+1,(double)PetscAbsScalar(dots[j]));
28596ded551SBarry Smith       }
28696ded551SBarry Smith     }
28796ded551SBarry Smith     PetscFree(dots);CHKERRQ(ierr);
28896ded551SBarry Smith   }
28996ded551SBarry Smith #endif
290574b3360SMatthew Knepley 
2910298fd71SBarry Smith   *SP = NULL;
292607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
293574b3360SMatthew Knepley 
29473107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(sp,MAT_NULLSPACE_CLASSID,"MatNullSpace","Null space","Mat",comm,MatNullSpaceDestroy,MatNullSpaceView);CHKERRQ(ierr);
295f7765cecSBarry Smith 
296b4fd4287SBarry Smith   sp->has_cnst = has_cnst;
297b4fd4287SBarry Smith   sp->n        = n;
2987850f3fbSLisandro Dalcin   sp->vecs     = 0;
2997850f3fbSLisandro Dalcin   sp->alpha    = 0;
3007850f3fbSLisandro Dalcin   sp->remove   = 0;
3017850f3fbSLisandro Dalcin   sp->rmctx    = 0;
3027850f3fbSLisandro Dalcin 
303f7a9e4ceSBarry Smith   if (n) {
304785e854fSJed Brown     ierr = PetscMalloc1(n,&sp->vecs);CHKERRQ(ierr);
305785e854fSJed Brown     ierr = PetscMalloc1(n,&sp->alpha);CHKERRQ(ierr);
3063bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)sp,n*(sizeof(Vec)+sizeof(PetscScalar)));CHKERRQ(ierr);
3077850f3fbSLisandro Dalcin     for (i=0; i<n; i++) {
3087850f3fbSLisandro Dalcin       ierr        = PetscObjectReference((PetscObject)vecs[i]);CHKERRQ(ierr);
3097850f3fbSLisandro Dalcin       sp->vecs[i] = vecs[i];
3107850f3fbSLisandro Dalcin     }
311f7a9e4ceSBarry Smith   }
312b4fd4287SBarry Smith 
313b4fd4287SBarry Smith   *SP = sp;
3143a40ed3dSBarry Smith   PetscFunctionReturn(0);
315f7765cecSBarry Smith }
316f7765cecSBarry Smith 
317f7765cecSBarry Smith /*@
3185cfeda75SBarry Smith    MatNullSpaceDestroy - Destroys a data structure used to project vectors
319b4fd4287SBarry Smith    out of null spaces.
320b4fd4287SBarry Smith 
3215cfeda75SBarry Smith    Collective on MatNullSpace
3224e472627SLois Curfman McInnes 
323b4fd4287SBarry Smith    Input Parameter:
324b9756687SLois Curfman McInnes .  sp - the null space context to be destroyed
325b9756687SLois Curfman McInnes 
326b9756687SLois Curfman McInnes    Level: advanced
327b4fd4287SBarry Smith 
32883c3bef8SLois Curfman McInnes .keywords: PC, null space, destroy
32941a59933SSatish Balay 
33072875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceRemove(), MatNullSpaceSetFunction()
331b4fd4287SBarry Smith @*/
332d34fcf5fSBarry Smith PetscErrorCode  MatNullSpaceDestroy(MatNullSpace *sp)
333b4fd4287SBarry Smith {
334dfbe8321SBarry Smith   PetscErrorCode ierr;
3359d2471e0SBarry Smith   PetscInt       i;
33685614651SBarry Smith 
3375cfeda75SBarry Smith   PetscFunctionBegin;
3386bf464f9SBarry Smith   if (!*sp) PetscFunctionReturn(0);
339d34fcf5fSBarry Smith   PetscValidHeaderSpecific((*sp),MAT_NULLSPACE_CLASSID,1);
340d34fcf5fSBarry Smith   if (--((PetscObject)(*sp))->refct > 0) {*sp = 0; PetscFunctionReturn(0);}
34185614651SBarry Smith 
3429d2471e0SBarry Smith   for (i=0; i < (*sp)->n; i++) {
3439d2471e0SBarry Smith     ierr = VecLockPop((*sp)->vecs[i]);CHKERRQ(ierr);
3449d2471e0SBarry Smith   }
3459d2471e0SBarry Smith 
3466bf464f9SBarry Smith   ierr = VecDestroyVecs((*sp)->n,&(*sp)->vecs);CHKERRQ(ierr);
347d34fcf5fSBarry Smith   ierr = PetscFree((*sp)->alpha);CHKERRQ(ierr);
3486bf464f9SBarry Smith   ierr = PetscHeaderDestroy(sp);CHKERRQ(ierr);
3493a40ed3dSBarry Smith   PetscFunctionReturn(0);
350b4fd4287SBarry Smith }
351b4fd4287SBarry Smith 
352812c3f48SMatthew Knepley /*@C
3535cfeda75SBarry Smith    MatNullSpaceRemove - Removes all the components of a null space from a vector.
354f7765cecSBarry Smith 
3555cfeda75SBarry Smith    Collective on MatNullSpace
356f7765cecSBarry Smith 
3574e472627SLois Curfman McInnes    Input Parameters:
358260663b8SBarry Smith +  sp - the null space context (if this is NULL then no null space is removed)
359359a2de3SMatthew G. Knepley -  vec - the vector from which the null space is to be removed
3604e472627SLois Curfman McInnes 
361b9756687SLois Curfman McInnes    Level: advanced
362b9756687SLois Curfman McInnes 
36383c3bef8SLois Curfman McInnes .keywords: PC, null space, remove
36441a59933SSatish Balay 
36572875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction()
366f7765cecSBarry Smith @*/
367d0195637SJed Brown PetscErrorCode  MatNullSpaceRemove(MatNullSpace sp,Vec vec)
368f7765cecSBarry Smith {
36987828ca2SBarry Smith   PetscScalar    sum;
3707850f3fbSLisandro Dalcin   PetscInt       i,N;
3716849ba73SBarry Smith   PetscErrorCode ierr;
372f7765cecSBarry Smith 
3733a40ed3dSBarry Smith   PetscFunctionBegin;
374260663b8SBarry Smith   if (!sp) PetscFunctionReturn(0);
3750700a824SBarry Smith   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1);
3760700a824SBarry Smith   PetscValidHeaderSpecific(vec,VEC_CLASSID,2);
3773cd8ff7eSMatthew Knepley 
378b4fd4287SBarry Smith   if (sp->has_cnst) {
3797850f3fbSLisandro Dalcin     ierr = VecGetSize(vec,&N);CHKERRQ(ierr);
3807850f3fbSLisandro Dalcin     if (N > 0) {
3817850f3fbSLisandro Dalcin       ierr = VecSum(vec,&sum);CHKERRQ(ierr);
382d4a378daSJed Brown       sum  = sum/((PetscScalar)(-1.0*N));
3837850f3fbSLisandro Dalcin       ierr = VecShift(vec,sum);CHKERRQ(ierr);
3847850f3fbSLisandro Dalcin     }
385f7765cecSBarry Smith   }
386b4fd4287SBarry Smith 
3877850f3fbSLisandro Dalcin   if (sp->n) {
3887850f3fbSLisandro Dalcin     ierr = VecMDot(vec,sp->n,sp->vecs,sp->alpha);CHKERRQ(ierr);
3897850f3fbSLisandro Dalcin     for (i=0; i<sp->n; i++) sp->alpha[i] = -sp->alpha[i];
3907850f3fbSLisandro Dalcin     ierr = VecMAXPY(vec,sp->n,sp->alpha,sp->vecs);CHKERRQ(ierr);
391f7765cecSBarry Smith   }
392b4fd4287SBarry Smith 
39372875594SBarry Smith   if (sp->remove) {
3940c3c4d68SMatthew Knepley     ierr = (*sp->remove)(sp,vec,sp->rmctx);CHKERRQ(ierr);
39572875594SBarry Smith   }
3963a40ed3dSBarry Smith   PetscFunctionReturn(0);
397f7765cecSBarry Smith }
398a2e34c3dSBarry Smith 
399a2e34c3dSBarry Smith /*@
400a2e34c3dSBarry Smith    MatNullSpaceTest  - Tests if the claimed null space is really a
401a2e34c3dSBarry Smith      null space of a matrix
402a2e34c3dSBarry Smith 
403a2e34c3dSBarry Smith    Collective on MatNullSpace
404a2e34c3dSBarry Smith 
405a2e34c3dSBarry Smith    Input Parameters:
406a2e34c3dSBarry Smith +  sp - the null space context
407a2e34c3dSBarry Smith -  mat - the matrix
408a2e34c3dSBarry Smith 
40995902228SMatthew Knepley    Output Parameters:
41095902228SMatthew Knepley .  isNull - PETSC_TRUE if the nullspace is valid for this matrix
41195902228SMatthew Knepley 
412a2e34c3dSBarry Smith    Level: advanced
413a2e34c3dSBarry Smith 
414a2e34c3dSBarry Smith .keywords: PC, null space, remove
415a2e34c3dSBarry Smith 
41672875594SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction()
417a2e34c3dSBarry Smith @*/
4187087cfbeSBarry Smith PetscErrorCode  MatNullSpaceTest(MatNullSpace sp,Mat mat,PetscBool  *isNull)
419a2e34c3dSBarry Smith {
42087828ca2SBarry Smith   PetscScalar    sum;
421a872bbdcSToby Isaac   PetscReal      nrm,tol = 10. * PETSC_SQRT_MACHINE_EPSILON;
4220b12b109SJed Brown   PetscInt       j,n,N;
4236849ba73SBarry Smith   PetscErrorCode ierr;
424a2e34c3dSBarry Smith   Vec            l,r;
425ace3abfcSBarry Smith   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,consistent = PETSC_TRUE;
4263050cee2SBarry Smith   PetscViewer    viewer;
427a2e34c3dSBarry Smith 
428a2e34c3dSBarry Smith   PetscFunctionBegin;
4290700a824SBarry Smith   PetscValidHeaderSpecific(sp,MAT_NULLSPACE_CLASSID,1);
4300700a824SBarry Smith   PetscValidHeaderSpecific(mat,MAT_CLASSID,2);
4313cfa8680SLisandro Dalcin   n    = sp->n;
432*fb04ea6fSLawrence Mitchell   ierr = PetscOptionsGetBool(((PetscObject)sp)->options,((PetscObject)mat)->prefix,"-mat_null_space_test_view",&flg1,NULL);CHKERRQ(ierr);
433*fb04ea6fSLawrence Mitchell   ierr = PetscOptionsGetBool(((PetscObject)sp)->options,((PetscObject)mat)->prefix,"-mat_null_space_test_view_draw",&flg2,NULL);CHKERRQ(ierr);
434a2e34c3dSBarry Smith 
435a2e34c3dSBarry Smith   if (n) {
436401b765aSJed Brown     ierr = VecDuplicate(sp->vecs[0],&l);CHKERRQ(ierr);
437a2e34c3dSBarry Smith   } else {
4382a7a6963SBarry Smith     ierr = MatCreateVecs(mat,&l,NULL);CHKERRQ(ierr);
439a2e34c3dSBarry Smith   }
440a2e34c3dSBarry Smith 
441ce94432eSBarry Smith   ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)sp),&viewer);CHKERRQ(ierr);
442a2e34c3dSBarry Smith   if (sp->has_cnst) {
443a2e34c3dSBarry Smith     ierr = VecDuplicate(l,&r);CHKERRQ(ierr);
444a2e34c3dSBarry Smith     ierr = VecGetSize(l,&N);CHKERRQ(ierr);
445a2e34c3dSBarry Smith     sum  = 1.0/N;
4462dcb1b2aSMatthew Knepley     ierr = VecSet(l,sum);CHKERRQ(ierr);
447a2e34c3dSBarry Smith     ierr = MatMult(mat,l,r);CHKERRQ(ierr);
4488bb6bcc5SSatish Balay     ierr = VecNorm(r,NORM_2,&nrm);CHKERRQ(ierr);
449a872bbdcSToby Isaac     if (nrm >= tol) consistent = PETSC_FALSE;
450874288d9SMatthew G Knepley     if (flg1) {
45118404f68SMatthew G Knepley       if (consistent) {
452ce94432eSBarry Smith         ierr = PetscPrintf(PetscObjectComm((PetscObject)sp),"Constants are likely null vector");CHKERRQ(ierr);
45395902228SMatthew Knepley       } else {
454ce94432eSBarry Smith         ierr = PetscPrintf(PetscObjectComm((PetscObject)sp),"Constants are unlikely null vector ");CHKERRQ(ierr);
45595902228SMatthew Knepley       }
45657622a8eSBarry Smith       ierr = PetscPrintf(PetscObjectComm((PetscObject)sp),"|| A * 1/N || = %g\n",(double)nrm);CHKERRQ(ierr);
457874288d9SMatthew G Knepley     }
45818404f68SMatthew G Knepley     if (!consistent && flg1) {ierr = VecView(r,viewer);CHKERRQ(ierr);}
45918404f68SMatthew G Knepley     if (!consistent && flg2) {ierr = VecView(r,viewer);CHKERRQ(ierr);}
4606bf464f9SBarry Smith     ierr = VecDestroy(&r);CHKERRQ(ierr);
461a2e34c3dSBarry Smith   }
462a2e34c3dSBarry Smith 
463a2e34c3dSBarry Smith   for (j=0; j<n; j++) {
464a2e34c3dSBarry Smith     ierr = (*mat->ops->mult)(mat,sp->vecs[j],l);CHKERRQ(ierr);
4658bb6bcc5SSatish Balay     ierr = VecNorm(l,NORM_2,&nrm);CHKERRQ(ierr);
466a872bbdcSToby Isaac     if (nrm >= tol) consistent = PETSC_FALSE;
467874288d9SMatthew G Knepley     if (flg1) {
46818404f68SMatthew G Knepley       if (consistent) {
469ce94432eSBarry Smith         ierr = PetscPrintf(PetscObjectComm((PetscObject)sp),"Null vector %D is likely null vector",j);CHKERRQ(ierr);
47095902228SMatthew Knepley       } else {
471ce94432eSBarry Smith         ierr       = PetscPrintf(PetscObjectComm((PetscObject)sp),"Null vector %D unlikely null vector ",j);CHKERRQ(ierr);
47295902228SMatthew Knepley         consistent = PETSC_FALSE;
47395902228SMatthew Knepley       }
47457622a8eSBarry Smith       ierr = PetscPrintf(PetscObjectComm((PetscObject)sp),"|| A * v[%D] || = %g\n",j,(double)nrm);CHKERRQ(ierr);
475874288d9SMatthew G Knepley     }
47618404f68SMatthew G Knepley     if (!consistent && flg1) {ierr = VecView(l,viewer);CHKERRQ(ierr);}
47718404f68SMatthew G Knepley     if (!consistent && flg2) {ierr = VecView(l,viewer);CHKERRQ(ierr);}
478a2e34c3dSBarry Smith   }
479a2e34c3dSBarry Smith 
480ce94432eSBarry Smith   if (sp->remove) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot test a null space provided as a function with MatNullSpaceSetFunction()");
481401b765aSJed Brown   ierr = VecDestroy(&l);CHKERRQ(ierr);
48231980aa1SBarry Smith   if (isNull) *isNull = consistent;
483a2e34c3dSBarry Smith   PetscFunctionReturn(0);
484a2e34c3dSBarry Smith }
485a2e34c3dSBarry Smith 
486