xref: /petsc/src/mat/interface/matnull.c (revision b97566877d6edecffda61a6401a48b65d1220eea)
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: pcnull.c,v 1.18 1998/12/17 22:09:36 bsmith Exp curfman $";
3 #endif
4 /*
5     Routines to project vectors out of null spaces.
6 */
7 
8 #include "petsc.h"
9 #include "src/pc/pcimpl.h"      /*I "pc.h" I*/
10 #include "src/sys/nreg.h"
11 #include "sys.h"
12 
13 
14 #undef __FUNC__
15 #define __FUNC__ "PCNullSpaceCreate"
16 /*@C
17    PCNullSpaceCreate - Creates a data-structure used to project vectors
18    out of null spaces.
19 
20    Collective on MPI_Comm
21 
22    Input Parameters:
23 +  comm - the MPI communicator associated with the object.
24 .  has_cnst - if the null spaces contains the constant vector, PETSC_TRUE or PETSC_FALSE
25 .  n - number of vectors (excluding constant vector) in null space
26 -  vecs - the vectors that span the null space (excluding the constant vector)
27           these vectors must be orthonormal
28 
29    Level: advanced
30 
31    Output Parameter:
32 .  SP - the null space context
33 
34 .keywords: PC, Null space
35 @*/
36 int PCNullSpaceCreate(MPI_Comm comm, int has_cnst, int n, Vec *vecs,PCNullSpace *SP)
37 {
38   PCNullSpace sp;
39 
40   PetscFunctionBegin;
41   PetscHeaderCreate(sp,_p_PCNullSpace,int,PCNULLSPACE_COOKIE,0,"PCNullSpace",comm,PCNullSpaceDestroy,0);
42   PLogObjectCreate(sp);
43   PLogObjectMemory(sp,sizeof(struct _p_PCNullSpace));
44 
45   sp->has_cnst = has_cnst;
46   sp->n        = n;
47   sp->vecs     = vecs;
48 
49   *SP          = sp;
50   PetscFunctionReturn(0);
51 }
52 
53 #undef __FUNC__
54 #define __FUNC__ "PCNullSpaceDestroy"
55 /*@
56    PCNullSpaceDestroy - Destroys a data-structure used to project vectors
57    out of null spaces.
58 
59    Collective on PCNullSpace
60 
61    Input Parameter:
62 .  sp - the null space context to be destroyed
63 
64    Level: advanced
65 
66 .keywords: PC, Null space
67 @*/
68 int PCNullSpaceDestroy(PCNullSpace sp)
69 {
70   PetscFunctionBegin;
71   PLogObjectDestroy(sp);
72   PetscHeaderDestroy(sp);
73   PetscFunctionReturn(0);
74 }
75 
76 #undef __FUNC__
77 #define __FUNC__ "PCNullSpaceRemove"
78 /*@
79    PCNullSpaceRemove - Removes all the components of a null space from a vector.
80 
81    Collective on PCNullSpace
82 
83    Input Parameters:
84 +  sp - the null space context
85 -  vec - the vector you want the null space removed from
86 
87    Level: advanced
88 
89 .keywords: PC, Null space
90 @*/
91 int PCNullSpaceRemove(PCNullSpace sp,Vec vec)
92 {
93   Scalar sum;
94   int    j, n = sp->n, N,ierr;
95 
96   PetscFunctionBegin;
97   if (sp->has_cnst) {
98     ierr = VecSum(vec,&sum); CHKERRQ(ierr);
99     ierr = VecGetSize(vec,&N); CHKERRQ(ierr);
100     sum  = sum/(-1.0*N);
101     ierr = VecShift(&sum,vec); CHKERRQ(ierr);
102   }
103 
104   for ( j=0; j<n; j++ ) {
105     ierr = VecDot(vec,sp->vecs[j],&sum);CHKERRQ(ierr);
106     sum  = -sum;
107     ierr = VecAYPX(&sum,sp->vecs[j],vec); CHKERRQ(ierr);
108   }
109 
110   PetscFunctionReturn(0);
111 }
112