xref: /petsc/src/mat/interface/matnull.c (revision 70f55243aafb320636e2a54ff30cab5d1e8d3d7b)
1f7765cecSBarry Smith #ifndef lint
2*70f55243SBarry Smith static char vcid[] = "$Id: pcnull.c,v 1.5 1996/04/25 23:59:27 balay 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"
9*70f55243SBarry 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 
15112a2221SBarry Smith /*@C
16b4fd4287SBarry Smith   PCNullSpaceCreate - Creates a data-structure used to project vectors
17b4fd4287SBarry Smith        out of null spaces.
18f7765cecSBarry Smith 
19f7765cecSBarry Smith   Input Parameters:
20b4fd4287SBarry Smith .  comm - the MPI communicator associated with the object.
21b4fd4287SBarry Smith .  has_cnst - if the null spaces contains the constant vector, PETSC_TRUE or PETSC_FALSE
22b4fd4287SBarry Smith .  n - number of vectors (excluding constant vector) in null space
23b4fd4287SBarry Smith .  vecs - the vectors that span the null space (excluding the constant vector)
24b4fd4287SBarry Smith .         these vectors must be orthonormal
25f7765cecSBarry Smith 
26f7765cecSBarry Smith   Output Parameter:
27b4fd4287SBarry Smith .  SP - the null space context
28f7765cecSBarry Smith 
29f7765cecSBarry Smith 
30b4fd4287SBarry Smith .keywords: PC, Null space
31f7765cecSBarry Smith @*/
32b4fd4287SBarry Smith int PCNullSpaceCreate(MPI_Comm comm, int has_cnst, int n, Vec *vecs,PCNullSpace *SP)
33f7765cecSBarry Smith {
34b4fd4287SBarry Smith   PCNullSpace sp;
35f7765cecSBarry Smith 
36b4fd4287SBarry Smith   PetscHeaderCreate(sp,_PCNullSpace,PCNULLSPACE_COOKIE,0,comm);
37b4fd4287SBarry Smith   PLogObjectCreate(sp);
38b4fd4287SBarry Smith   PLogObjectMemory(sp,sizeof(struct _PCNullSpace));
39f7765cecSBarry Smith 
40b4fd4287SBarry Smith   sp->has_cnst = has_cnst;
41b4fd4287SBarry Smith   sp->n        = n;
42b4fd4287SBarry Smith   sp->vecs     = vecs;
43b4fd4287SBarry Smith 
44b4fd4287SBarry Smith   *SP          = sp;
45f7765cecSBarry Smith   return 0;
46f7765cecSBarry Smith }
47f7765cecSBarry Smith 
48f7765cecSBarry Smith /*@
49b4fd4287SBarry Smith   PCNullSpaceDestroy - Destroys a data-structure used to project vectors
50b4fd4287SBarry Smith        out of null spaces.
51b4fd4287SBarry Smith 
52b4fd4287SBarry Smith   Input Parameter:
53b4fd4287SBarry Smith .    SP - the null space context to be destroyed
54b4fd4287SBarry Smith 
55b4fd4287SBarry Smith .keywords: PC, Null space
56b4fd4287SBarry Smith @*/
57b4fd4287SBarry Smith int PCNullSpaceDestroy(PCNullSpace sp)
58b4fd4287SBarry Smith {
59b4fd4287SBarry Smith   PLogObjectDestroy(sp);
60b4fd4287SBarry Smith   PetscHeaderDestroy(sp);
61b4fd4287SBarry Smith   return 0;
62b4fd4287SBarry Smith }
63b4fd4287SBarry Smith 
64b4fd4287SBarry Smith /*@
65b4fd4287SBarry Smith   PCNullSpaceRemove - Removes all the components of a null space from a vector.
66f7765cecSBarry Smith 
67f7765cecSBarry Smith   Input Parameters:
68b4fd4287SBarry Smith .    sp - the null space context
69b4fd4287SBarry Smith .    vec - the vector you want the null space removed from
70f7765cecSBarry Smith 
71f7765cecSBarry Smith 
72b4fd4287SBarry Smith .keywords: PC, Null space
73f7765cecSBarry Smith @*/
74b4fd4287SBarry Smith int PCNullSpaceRemove(PCNullSpace sp,Vec vec)
75f7765cecSBarry Smith {
76b4fd4287SBarry Smith   Scalar sum;
77b4fd4287SBarry Smith   int    j, n = sp->n, N,ierr;
78f7765cecSBarry Smith 
79b4fd4287SBarry Smith   if (sp->has_cnst) {
80b4fd4287SBarry Smith     ierr = VecSum(vec,&sum); CHKERRQ(ierr);
81b4fd4287SBarry Smith     ierr = VecGetSize(vec,&N); CHKERRQ(ierr);
82b4fd4287SBarry Smith     sum  = -sum/N;
83b4fd4287SBarry Smith     ierr = VecShift(&sum,vec); CHKERRQ(ierr);
84f7765cecSBarry Smith   }
85b4fd4287SBarry Smith 
86b4fd4287SBarry Smith   for ( j=0; j<n; j++ ) {
87b4fd4287SBarry Smith     ierr = VecDot(vec,sp->vecs[j],&sum);CHKERRQ(ierr);
88b4fd4287SBarry Smith     sum  = -sum;
89b4fd4287SBarry Smith     ierr = VecAYPX(&sum,sp->vecs[j],vec); CHKERRQ(ierr);
90f7765cecSBarry Smith   }
91b4fd4287SBarry Smith 
92f7765cecSBarry Smith   return 0;
93f7765cecSBarry Smith }
94