xref: /petsc/src/mat/interface/matnull.c (revision f09e8eb94a771781a812a8d81a9ca3d36ec35eba)
1f7765cecSBarry Smith #ifndef lint
2*f09e8eb9SSatish Balay static char vcid[] = "$Id: pcnull.c,v 1.9 1997/02/22 02:23:59 bsmith 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*/
10f7765cecSBarry Smith #include <stdio.h>
11f5eb4b81SSatish Balay #include "src/sys/nreg.h"
12f7765cecSBarry Smith #include "sys.h"
13f7765cecSBarry Smith 
14f7765cecSBarry Smith 
155615d1e5SSatish Balay #undef __FUNC__
165615d1e5SSatish Balay #define __FUNC__ "PCNullSpaceCreate"
17112a2221SBarry Smith /*@C
18b4fd4287SBarry Smith   PCNullSpaceCreate - Creates a data-structure used to project vectors
19b4fd4287SBarry Smith        out of null spaces.
20f7765cecSBarry Smith 
21f7765cecSBarry Smith   Input Parameters:
22b4fd4287SBarry Smith .  comm - the MPI communicator associated with the object.
23b4fd4287SBarry Smith .  has_cnst - if the null spaces contains the constant vector, PETSC_TRUE or PETSC_FALSE
24b4fd4287SBarry Smith .  n - number of vectors (excluding constant vector) in null space
25b4fd4287SBarry Smith .  vecs - the vectors that span the null space (excluding the constant vector)
26b4fd4287SBarry Smith .         these vectors must be orthonormal
27f7765cecSBarry Smith 
28f7765cecSBarry Smith   Output Parameter:
29b4fd4287SBarry Smith .  SP - the null space context
30f7765cecSBarry Smith 
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 
38*f09e8eb9SSatish Balay   PetscHeaderCreate(sp,_p_PCNullSpace,PCNULLSPACE_COOKIE,0,comm);
39b4fd4287SBarry Smith   PLogObjectCreate(sp);
40*f09e8eb9SSatish 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;
47f7765cecSBarry Smith   return 0;
48f7765cecSBarry Smith }
49f7765cecSBarry Smith 
505615d1e5SSatish Balay #undef __FUNC__
515eea60f9SBarry Smith #define __FUNC__ "PCNullSpaceDestroy" /* ADIC Ignore */
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 {
63b4fd4287SBarry Smith   PLogObjectDestroy(sp);
64b4fd4287SBarry Smith   PetscHeaderDestroy(sp);
65b4fd4287SBarry Smith   return 0;
66b4fd4287SBarry Smith }
67b4fd4287SBarry Smith 
685615d1e5SSatish Balay #undef __FUNC__
695615d1e5SSatish Balay #define __FUNC__ "PCNullSpaceRemove"
70b4fd4287SBarry Smith /*@
71b4fd4287SBarry Smith   PCNullSpaceRemove - Removes all the components of a null space from a vector.
72f7765cecSBarry Smith 
73f7765cecSBarry Smith   Input Parameters:
74b4fd4287SBarry Smith .    sp - the null space context
75b4fd4287SBarry Smith .    vec - the vector you want the null space removed from
76f7765cecSBarry Smith 
77f7765cecSBarry Smith 
78b4fd4287SBarry Smith .keywords: PC, Null space
79f7765cecSBarry Smith @*/
80b4fd4287SBarry Smith int PCNullSpaceRemove(PCNullSpace sp,Vec vec)
81f7765cecSBarry Smith {
82b4fd4287SBarry Smith   Scalar sum;
83b4fd4287SBarry Smith   int    j, n = sp->n, N,ierr;
84f7765cecSBarry Smith 
85b4fd4287SBarry Smith   if (sp->has_cnst) {
86b4fd4287SBarry Smith     ierr = VecSum(vec,&sum); CHKERRQ(ierr);
87b4fd4287SBarry Smith     ierr = VecGetSize(vec,&N); CHKERRQ(ierr);
88b4fd4287SBarry Smith     sum  = -sum/N;
89b4fd4287SBarry Smith     ierr = VecShift(&sum,vec); CHKERRQ(ierr);
90f7765cecSBarry Smith   }
91b4fd4287SBarry Smith 
92b4fd4287SBarry Smith   for ( j=0; j<n; j++ ) {
93b4fd4287SBarry Smith     ierr = VecDot(vec,sp->vecs[j],&sum);CHKERRQ(ierr);
94b4fd4287SBarry Smith     sum  = -sum;
95b4fd4287SBarry Smith     ierr = VecAYPX(&sum,sp->vecs[j],vec); CHKERRQ(ierr);
96f7765cecSBarry Smith   }
97b4fd4287SBarry Smith 
98f7765cecSBarry Smith   return 0;
99f7765cecSBarry Smith }
100