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