xref: /petsc/src/mat/interface/matnull.c (revision d79d66985a41d08ce931896262adbe590aad7dff)
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: pcnull.c,v 1.20 1999/01/31 16:08:03 bsmith Exp curfman $";
3 #endif
4 /*
5     Routines to project vectors out of null spaces.
6 */
7 
8 #include "petsc.h"
9 #include "src/sles/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 - PETSC_TRUE if the null space contains the constant vector; otherwise 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    Output Parameter:
30 .  SP - the null space context
31 
32    Level: advanced
33 
34 .keywords: PC, null space, create
35 .seealso: PCNullSpaceDestroy(), PCNullSpaceRemove()
36 @*/
37 int PCNullSpaceCreate(MPI_Comm comm, int has_cnst, int n, Vec *vecs,PCNullSpace *SP)
38 {
39   PCNullSpace sp;
40 
41   PetscFunctionBegin;
42   PetscHeaderCreate(sp,_p_PCNullSpace,int,PCNULLSPACE_COOKIE,0,"PCNullSpace",comm,PCNullSpaceDestroy,0);
43   PLogObjectCreate(sp);
44   PLogObjectMemory(sp,sizeof(struct _p_PCNullSpace));
45 
46   sp->has_cnst = has_cnst;
47   sp->n        = n;
48   sp->vecs     = vecs;
49 
50   *SP          = sp;
51   PetscFunctionReturn(0);
52 }
53 
54 #undef __FUNC__
55 #define __FUNC__ "PCNullSpaceDestroy"
56 /*@
57    PCNullSpaceDestroy - Destroys a data-structure used to project vectors
58    out of null spaces.
59 
60    Collective on PCNullSpace
61 
62    Input Parameter:
63 .  sp - the null space context to be destroyed
64 
65    Level: advanced
66 
67 .keywords: PC, null space, destroy
68 .seealso: PCNullSpaceDestroy(), PCNullSpaceRemove()
69 @*/
70 int PCNullSpaceDestroy(PCNullSpace sp)
71 {
72   PetscFunctionBegin;
73   PLogObjectDestroy(sp);
74   PetscHeaderDestroy(sp);
75   PetscFunctionReturn(0);
76 }
77 
78 #undef __FUNC__
79 #define __FUNC__ "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 .seealso: PCNullSpaceCreate(), PCNullSpaceDestroy()
93 @*/
94 int PCNullSpaceRemove(PCNullSpace sp,Vec vec)
95 {
96   Scalar sum;
97   int    j, n = sp->n, N,ierr;
98 
99   PetscFunctionBegin;
100   if (sp->has_cnst) {
101     ierr = VecSum(vec,&sum); CHKERRQ(ierr);
102     ierr = VecGetSize(vec,&N); CHKERRQ(ierr);
103     sum  = sum/(-1.0*N);
104     ierr = VecShift(&sum,vec); CHKERRQ(ierr);
105   }
106 
107   for ( j=0; j<n; j++ ) {
108     ierr = VecDot(vec,sp->vecs[j],&sum);CHKERRQ(ierr);
109     sum  = -sum;
110     ierr = VecAYPX(&sum,sp->vecs[j],vec); CHKERRQ(ierr);
111   }
112 
113   PetscFunctionReturn(0);
114 }
115