xref: /petsc/src/mat/interface/matnull.c (revision 18a7d68fc2f5cf5dd06b272efb89dfa4051d0411)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*18a7d68fSSatish Balay static char vcid[] = "$Id: pcnull.c,v 1.16 1998/04/24 21:21:12 curfman Exp balay $";
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 
204e472627SLois Curfman McInnes    Collective on MPI_Comm
214e472627SLois Curfman McInnes 
22f7765cecSBarry Smith    Input Parameters:
234e472627SLois Curfman McInnes +  comm - the MPI communicator associated with the object.
24b4fd4287SBarry Smith .  has_cnst - if the null spaces contains the constant vector, PETSC_TRUE or PETSC_FALSE
25b4fd4287SBarry Smith .  n - number of vectors (excluding constant vector) in null space
264e472627SLois Curfman McInnes -  vecs - the vectors that span the null space (excluding the constant vector)
274e472627SLois Curfman McInnes           these vectors must be orthonormal
28f7765cecSBarry Smith 
29f7765cecSBarry Smith    Output Parameter:
30b4fd4287SBarry Smith .  SP - the null space context
31f7765cecSBarry Smith 
32b4fd4287SBarry Smith .keywords: PC, Null space
33f7765cecSBarry Smith @*/
34b4fd4287SBarry Smith int PCNullSpaceCreate(MPI_Comm comm, int has_cnst, int n, Vec *vecs,PCNullSpace *SP)
35f7765cecSBarry Smith {
36b4fd4287SBarry Smith   PCNullSpace sp;
37f7765cecSBarry Smith 
383a40ed3dSBarry Smith   PetscFunctionBegin;
39f830108cSBarry Smith   PetscHeaderCreate(sp,_p_PCNullSpace,int,PCNULLSPACE_COOKIE,0,comm,PCNullSpaceDestroy,0);
40b4fd4287SBarry Smith   PLogObjectCreate(sp);
41f09e8eb9SSatish Balay   PLogObjectMemory(sp,sizeof(struct _p_PCNullSpace));
42f7765cecSBarry Smith 
43b4fd4287SBarry Smith   sp->has_cnst = has_cnst;
44b4fd4287SBarry Smith   sp->n        = n;
45b4fd4287SBarry Smith   sp->vecs     = vecs;
46b4fd4287SBarry Smith 
47b4fd4287SBarry Smith   *SP          = sp;
483a40ed3dSBarry Smith   PetscFunctionReturn(0);
49f7765cecSBarry Smith }
50f7765cecSBarry Smith 
515615d1e5SSatish Balay #undef __FUNC__
52d4bb536fSBarry Smith #define __FUNC__ "PCNullSpaceDestroy"
53f7765cecSBarry Smith /*@
54b4fd4287SBarry Smith    PCNullSpaceDestroy - Destroys a data-structure used to project vectors
55b4fd4287SBarry Smith    out of null spaces.
56b4fd4287SBarry Smith 
574e472627SLois Curfman McInnes    Collective on PCNullSpace
584e472627SLois Curfman McInnes 
59b4fd4287SBarry Smith    Input Parameter:
60b4fd4287SBarry Smith .  SP - the null space context to be destroyed
61b4fd4287SBarry Smith 
62b4fd4287SBarry Smith .keywords: PC, Null space
63b4fd4287SBarry Smith @*/
64b4fd4287SBarry Smith int PCNullSpaceDestroy(PCNullSpace sp)
65b4fd4287SBarry Smith {
663a40ed3dSBarry Smith   PetscFunctionBegin;
67b4fd4287SBarry Smith   PLogObjectDestroy(sp);
68b4fd4287SBarry Smith   PetscHeaderDestroy(sp);
693a40ed3dSBarry Smith   PetscFunctionReturn(0);
70b4fd4287SBarry Smith }
71b4fd4287SBarry Smith 
725615d1e5SSatish Balay #undef __FUNC__
735615d1e5SSatish Balay #define __FUNC__ "PCNullSpaceRemove"
74b4fd4287SBarry Smith /*@
75b4fd4287SBarry Smith    PCNullSpaceRemove - Removes all the components of a null space from a vector.
76f7765cecSBarry Smith 
77fee21e36SBarry Smith    Collective on PCNullSpace
78f7765cecSBarry Smith 
794e472627SLois Curfman McInnes    Input Parameters:
804e472627SLois Curfman McInnes +  sp - the null space context
814e472627SLois Curfman McInnes -  vec - the vector you want the null space removed from
824e472627SLois Curfman McInnes 
83b4fd4287SBarry Smith .keywords: PC, Null space
84f7765cecSBarry Smith @*/
85b4fd4287SBarry Smith int PCNullSpaceRemove(PCNullSpace sp,Vec vec)
86f7765cecSBarry Smith {
87b4fd4287SBarry Smith   Scalar sum;
88b4fd4287SBarry Smith   int    j, n = sp->n, N,ierr;
89f7765cecSBarry Smith 
903a40ed3dSBarry Smith   PetscFunctionBegin;
91b4fd4287SBarry Smith   if (sp->has_cnst) {
92b4fd4287SBarry Smith     ierr = VecSum(vec,&sum); CHKERRQ(ierr);
93b4fd4287SBarry Smith     ierr = VecGetSize(vec,&N); CHKERRQ(ierr);
94*18a7d68fSSatish Balay     sum  = sum/(-1.0*N);
95b4fd4287SBarry Smith     ierr = VecShift(&sum,vec); CHKERRQ(ierr);
96f7765cecSBarry Smith   }
97b4fd4287SBarry Smith 
98b4fd4287SBarry Smith   for ( j=0; j<n; j++ ) {
99b4fd4287SBarry Smith     ierr = VecDot(vec,sp->vecs[j],&sum);CHKERRQ(ierr);
100b4fd4287SBarry Smith     sum  = -sum;
101b4fd4287SBarry Smith     ierr = VecAYPX(&sum,sp->vecs[j],vec); CHKERRQ(ierr);
102f7765cecSBarry Smith   }
103b4fd4287SBarry Smith 
1043a40ed3dSBarry Smith   PetscFunctionReturn(0);
105f7765cecSBarry Smith }
106