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