xref: /petsc/src/mat/interface/matnull.c (revision e0e703c18709862a0d755d6cf34ba2e20a8376a1)
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: pcnull.c,v 1.14 1998/03/12 23:17:20 bsmith Exp bsmith $";
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   Input Parameters:
21 .  comm - the MPI communicator associated with the object.
22 .  has_cnst - if the null spaces contains the constant vector, PETSC_TRUE or PETSC_FALSE
23 .  n - number of vectors (excluding constant vector) in null space
24 .  vecs - the vectors that span the null space (excluding the constant vector)
25 .         these vectors must be orthonormal
26 
27   Output Parameter:
28 .  SP - the null space context
29 
30   Collective on MPI_Comm
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   Input Parameter:
58 .    SP - the null space context to be destroyed
59 
60    Collective on PCNullSpace
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   Input Parameters:
78 .    sp - the null space context
79 .    vec - the vector you want the null space removed from
80 
81    Collective on PCNullSpace
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/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