xref: /petsc/src/mat/interface/matnull.c (revision 5cfeda75ab3ff5a1a1e46140f14dc3304ce71702)
1*5cfeda75SBarry Smith /*$Id: pcnull.c,v 1.31 2000/05/05 22:16:59 balay Exp bsmith $*/
2f7765cecSBarry Smith /*
3b4fd4287SBarry Smith     Routines to project vectors out of null spaces.
4f7765cecSBarry Smith */
5f7765cecSBarry Smith 
6*5cfeda75SBarry Smith #include "src/mat/matimpl.h"      /*I "petscmat.h" I*/
7e090d566SSatish Balay #include "petscsys.h"
8f7765cecSBarry Smith 
95615d1e5SSatish Balay #undef __FUNC__
10*5cfeda75SBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatNullSpaceCreate"
11112a2221SBarry Smith /*@C
12*5cfeda75SBarry Smith    MatNullSpaceCreate - Creates a data structure used to project vectors
13b4fd4287SBarry Smith    out of null spaces.
14f7765cecSBarry Smith 
154e472627SLois Curfman McInnes    Collective on MPI_Comm
164e472627SLois Curfman McInnes 
17f7765cecSBarry Smith    Input Parameters:
1883c3bef8SLois Curfman McInnes +  comm - the MPI communicator associated with the object
1983c3bef8SLois Curfman McInnes .  has_cnst - PETSC_TRUE if the null space contains the constant vector; otherwise PETSC_FALSE
20b4fd4287SBarry Smith .  n - number of vectors (excluding constant vector) in null space
2183c3bef8SLois Curfman McInnes -  vecs - the vectors that span the null space (excluding the constant vector);
224e472627SLois Curfman McInnes           these vectors must be orthonormal
23f7765cecSBarry Smith 
24f7765cecSBarry Smith    Output Parameter:
25b4fd4287SBarry Smith .  SP - the null space context
26f7765cecSBarry Smith 
2783c3bef8SLois Curfman McInnes    Level: advanced
2883c3bef8SLois Curfman McInnes 
2983c3bef8SLois Curfman McInnes .keywords: PC, null space, create
3041a59933SSatish Balay 
31*5cfeda75SBarry Smith .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove()
32f7765cecSBarry Smith @*/
33*5cfeda75SBarry Smith int MatNullSpaceCreate(MPI_Comm comm,int has_cnst,int n,Vec *vecs,MatNullSpace *SP)
34f7765cecSBarry Smith {
35*5cfeda75SBarry Smith   MatNullSpace sp;
36f7765cecSBarry Smith 
373a40ed3dSBarry Smith   PetscFunctionBegin;
38*5cfeda75SBarry Smith   PetscHeaderCreate(sp,_p_MatNullSpace,int,MATNULLSPACE_COOKIE,0,"MatNullSpace",comm,MatNullSpaceDestroy,0);
39b4fd4287SBarry Smith   PLogObjectCreate(sp);
40*5cfeda75SBarry Smith   PLogObjectMemory(sp,sizeof(struct _p_MatNullSpace));
41f7765cecSBarry Smith 
42b4fd4287SBarry Smith   sp->has_cnst = has_cnst;
43b4fd4287SBarry Smith   sp->n        = n;
44b4fd4287SBarry Smith   sp->vecs     = vecs;
45*5cfeda75SBarry Smith   sp->vec      = PETSC_NULL;
46b4fd4287SBarry Smith 
47b4fd4287SBarry Smith   *SP          = sp;
483a40ed3dSBarry Smith   PetscFunctionReturn(0);
49f7765cecSBarry Smith }
50f7765cecSBarry Smith 
515615d1e5SSatish Balay #undef __FUNC__
52*5cfeda75SBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatNullSpaceDestroy"
53f7765cecSBarry Smith /*@
54*5cfeda75SBarry Smith    MatNullSpaceDestroy - Destroys a data structure used to project vectors
55b4fd4287SBarry Smith    out of null spaces.
56b4fd4287SBarry Smith 
57*5cfeda75SBarry Smith    Collective on MatNullSpace
584e472627SLois Curfman McInnes 
59b4fd4287SBarry Smith    Input Parameter:
60b9756687SLois Curfman McInnes .  sp - the null space context to be destroyed
61b9756687SLois Curfman McInnes 
62b9756687SLois Curfman McInnes    Level: advanced
63b4fd4287SBarry Smith 
6483c3bef8SLois Curfman McInnes .keywords: PC, null space, destroy
6541a59933SSatish Balay 
66*5cfeda75SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceRemove()
67b4fd4287SBarry Smith @*/
68*5cfeda75SBarry Smith int MatNullSpaceDestroy(MatNullSpace sp)
69b4fd4287SBarry Smith {
70*5cfeda75SBarry Smith   int ierr;
7185614651SBarry Smith 
72*5cfeda75SBarry Smith   PetscFunctionBegin;
7385614651SBarry Smith   if (--sp->refct > 0) PetscFunctionReturn(0);
7485614651SBarry Smith 
75*5cfeda75SBarry Smith   if (sp->vec) {ierr = VecDestroy(sp->vec);CHKERRQ(ierr);}
76*5cfeda75SBarry Smith 
77b4fd4287SBarry Smith   PLogObjectDestroy(sp);
78b4fd4287SBarry Smith   PetscHeaderDestroy(sp);
793a40ed3dSBarry Smith   PetscFunctionReturn(0);
80b4fd4287SBarry Smith }
81b4fd4287SBarry Smith 
825615d1e5SSatish Balay #undef __FUNC__
83*5cfeda75SBarry Smith #define __FUNC__ /*<a name=""></a>*/"MatNullSpaceRemove"
84b4fd4287SBarry Smith /*@
85*5cfeda75SBarry Smith    MatNullSpaceRemove - Removes all the components of a null space from a vector.
86f7765cecSBarry Smith 
87*5cfeda75SBarry Smith    Collective on MatNullSpace
88f7765cecSBarry Smith 
894e472627SLois Curfman McInnes    Input Parameters:
904e472627SLois Curfman McInnes +  sp - the null space context
9183c3bef8SLois Curfman McInnes -  vec - the vector from which the null space is to be removed
924e472627SLois Curfman McInnes 
93b9756687SLois Curfman McInnes    Level: advanced
94b9756687SLois Curfman McInnes 
9583c3bef8SLois Curfman McInnes .keywords: PC, null space, remove
9641a59933SSatish Balay 
97*5cfeda75SBarry Smith .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy()
98f7765cecSBarry Smith @*/
99*5cfeda75SBarry Smith int MatNullSpaceRemove(MatNullSpace sp,Vec vec,Vec *out)
100f7765cecSBarry Smith {
101b4fd4287SBarry Smith   Scalar sum;
102b4fd4287SBarry Smith   int    j,n = sp->n,N,ierr;
103*5cfeda75SBarry Smith   Vec    l = vec;
104f7765cecSBarry Smith 
1053a40ed3dSBarry Smith   PetscFunctionBegin;
106*5cfeda75SBarry Smith   if (out) {
107*5cfeda75SBarry Smith     if (!sp->vec) {
108*5cfeda75SBarry Smith       ierr = VecDuplicate(vec,&sp->vec);CHKERRQ(ierr);
109*5cfeda75SBarry Smith     }
110*5cfeda75SBarry Smith     *out = sp->vec;
111*5cfeda75SBarry Smith     ierr = VecCopy(vec,*out);CHKERRQ(ierr);
112*5cfeda75SBarry Smith     l    = *out;
113*5cfeda75SBarry Smith   }
114*5cfeda75SBarry Smith 
115b4fd4287SBarry Smith   if (sp->has_cnst) {
116*5cfeda75SBarry Smith     ierr = VecSum(l,&sum);CHKERRQ(ierr);
117*5cfeda75SBarry Smith     ierr = VecGetSize(l,&N);CHKERRQ(ierr);
11818a7d68fSSatish Balay     sum  = sum/(-1.0*N);
119*5cfeda75SBarry Smith     ierr = VecShift(&sum,l);CHKERRQ(ierr);
120f7765cecSBarry Smith   }
121b4fd4287SBarry Smith 
122b4fd4287SBarry Smith   for (j=0; j<n; j++) {
123*5cfeda75SBarry Smith     ierr = VecDot(l,sp->vecs[j],&sum);CHKERRQ(ierr);
124b4fd4287SBarry Smith     sum  = -sum;
125*5cfeda75SBarry Smith     ierr = VecAXPY(&sum,sp->vecs[j],l);CHKERRQ(ierr);
126f7765cecSBarry Smith   }
127b4fd4287SBarry Smith 
1283a40ed3dSBarry Smith   PetscFunctionReturn(0);
129f7765cecSBarry Smith }
130