xref: /petsc/src/mat/interface/matnull.c (revision 0713fee8a27481b0deeccc792613f84c28abcf5e)
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: pcnull.c,v 1.16 1998/04/24 21:21:12 curfman Exp balay $";
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    Output Parameter:
30 .  SP - the null space context
31 
32 .keywords: PC, Null space
33 @*/
34 int PCNullSpaceCreate(MPI_Comm comm, int has_cnst, int n, Vec *vecs,PCNullSpace *SP)
35 {
36   PCNullSpace sp;
37 
38   PetscFunctionBegin;
39   PetscHeaderCreate(sp,_p_PCNullSpace,int,PCNULLSPACE_COOKIE,0,comm,PCNullSpaceDestroy,0);
40   PLogObjectCreate(sp);
41   PLogObjectMemory(sp,sizeof(struct _p_PCNullSpace));
42 
43   sp->has_cnst = has_cnst;
44   sp->n        = n;
45   sp->vecs     = vecs;
46 
47   *SP          = sp;
48   PetscFunctionReturn(0);
49 }
50 
51 #undef __FUNC__
52 #define __FUNC__ "PCNullSpaceDestroy"
53 /*@
54    PCNullSpaceDestroy - Destroys a data-structure used to project vectors
55    out of null spaces.
56 
57    Collective on PCNullSpace
58 
59    Input Parameter:
60 .  SP - the null space context to be destroyed
61 
62 .keywords: PC, Null space
63 @*/
64 int PCNullSpaceDestroy(PCNullSpace sp)
65 {
66   PetscFunctionBegin;
67   PLogObjectDestroy(sp);
68   PetscHeaderDestroy(sp);
69   PetscFunctionReturn(0);
70 }
71 
72 #undef __FUNC__
73 #define __FUNC__ "PCNullSpaceRemove"
74 /*@
75    PCNullSpaceRemove - Removes all the components of a null space from a vector.
76 
77    Collective on PCNullSpace
78 
79    Input Parameters:
80 +  sp - the null space context
81 -  vec - the vector you want the null space removed from
82 
83 .keywords: PC, Null space
84 @*/
85 int PCNullSpaceRemove(PCNullSpace sp,Vec vec)
86 {
87   Scalar sum;
88   int    j, n = sp->n, N,ierr;
89 
90   PetscFunctionBegin;
91   if (sp->has_cnst) {
92     ierr = VecSum(vec,&sum); CHKERRQ(ierr);
93     ierr = VecGetSize(vec,&N); CHKERRQ(ierr);
94     sum  = sum/(-1.0*N);
95     ierr = VecShift(&sum,vec); CHKERRQ(ierr);
96   }
97 
98   for ( j=0; j<n; j++ ) {
99     ierr = VecDot(vec,sp->vecs[j],&sum);CHKERRQ(ierr);
100     sum  = -sum;
101     ierr = VecAYPX(&sum,sp->vecs[j],vec); CHKERRQ(ierr);
102   }
103 
104   PetscFunctionReturn(0);
105 }
106