Actual source code: matnull.c

  1: #define PETSCMAT_DLL

  3: /*
  4:     Routines to project vectors out of null spaces.
  5: */

 7:  #include include/private/matimpl.h
 8:  #include petscsys.h

 10: PetscCookie  MAT_NULLSPACE_COOKIE = 0;

 14: /*@C
 15:    MatNullSpaceSetFunction - set a function that removes a null space from a vector
 16:    out of null spaces.

 18:    Collective on MatNullSpace

 20:    Input Parameters:
 21: +  sp - the null space object
 22: .  rem - the function that removes the null space
 23: -  ctx - context for the remove function

 25:    Level: advanced

 27: .keywords: PC, null space, create

 29: .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), KSPSetNullSpace(), MatNullSpace, MatNullSpaceCreate()
 30: @*/
 31: PetscErrorCode  MatNullSpaceSetFunction(MatNullSpace sp, PetscErrorCode (*rem)(Vec,void*),void *ctx)
 32: {
 35:   sp->remove = rem;
 36:   sp->rmctx  = ctx;
 37:   return(0);
 38: }

 42: /*@
 43:    MatNullSpaceCreate - Creates a data structure used to project vectors 
 44:    out of null spaces.

 46:    Collective on MPI_Comm

 48:    Input Parameters:
 49: +  comm - the MPI communicator associated with the object
 50: .  has_cnst - PETSC_TRUE if the null space contains the constant vector; otherwise PETSC_FALSE
 51: .  n - number of vectors (excluding constant vector) in null space
 52: -  vecs - the vectors that span the null space (excluding the constant vector);
 53:           these vectors must be orthonormal. These vectors are NOT copied, so do not change them
 54:           after this call. You should free the array that you pass in.

 56:    Output Parameter:
 57: .  SP - the null space context

 59:    Level: advanced

 61:   Users manual sections:
 62: .   Section 4.15 Solving Singular Systems

 64: .keywords: PC, null space, create

 66: .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), KSPSetNullSpace(), MatNullSpace, MatNullSpaceSetFunction()
 67: @*/
 68: PetscErrorCode  MatNullSpaceCreate(MPI_Comm comm,PetscTruth has_cnst,PetscInt n,const Vec vecs[],MatNullSpace *SP)
 69: {
 70:   MatNullSpace   sp;
 72:   PetscInt       i;

 75:   if (n < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Number of vectors (given %D) cannot be negative",n);
 79: 
 80:   *SP = PETSC_NULL;
 81: #ifndef PETSC_USE_DYNAMIC_LIBRARIES 
 82:   MatInitializePackage(PETSC_NULL);
 83: #endif 

 85:   PetscHeaderCreate(sp,_p_MatNullSpace,int,MAT_NULLSPACE_COOKIE,0,"MatNullSpace",comm,MatNullSpaceDestroy,0);
 86:   PetscLogObjectMemory(sp,sizeof(struct _p_MatNullSpace));

 88:   sp->has_cnst = has_cnst;
 89:   sp->n        = n;
 90:   sp->vecs     = 0;
 91:   sp->alpha    = 0;
 92:   sp->vec      = 0;
 93:   sp->remove   = 0;
 94:   sp->rmctx    = 0;

 96:   if (n) {
 97:     PetscMalloc(n*sizeof(Vec),&sp->vecs);
 98:     PetscMalloc(n*sizeof(PetscScalar),&sp->alpha);
 99:     PetscLogObjectMemory(sp,n*(sizeof(Vec)+sizeof(PetscScalar)));
100:     for (i=0; i<n; i++) {
101:       PetscObjectReference((PetscObject)vecs[i]);
102:       sp->vecs[i] = vecs[i];
103:     }
104:   }

106:   *SP          = sp;
107:   return(0);
108: }

112: /*@
113:    MatNullSpaceDestroy - Destroys a data structure used to project vectors 
114:    out of null spaces.

116:    Collective on MatNullSpace

118:    Input Parameter:
119: .  sp - the null space context to be destroyed

121:    Level: advanced

123: .keywords: PC, null space, destroy

125: .seealso: MatNullSpaceCreate(), MatNullSpaceRemove(), MatNullSpaceSetFunction()
126: @*/
127: PetscErrorCode  MatNullSpaceDestroy(MatNullSpace sp)
128: {

133:   if (--sp->refct > 0) return(0);

135:   if (sp->vec)  { VecDestroy(sp->vec); }
136:   if (sp->vecs) { VecDestroyVecs(sp->vecs,sp->n); }
137:   PetscFree(sp->alpha);
138:   PetscHeaderDestroy(sp);
139:   return(0);
140: }

144: /*@
145:    MatNullSpaceRemove - Removes all the components of a null space from a vector.

147:    Collective on MatNullSpace

149:    Input Parameters:
150: +  sp - the null space context
151: .  vec - the vector from which the null space is to be removed 
152: -  out - if this is requested (not PETSC_NULL) then this is a vector with the null space removed otherwise
153:          the removal is done in-place (in vec)

155:    Note: The user is not responsible for the vector returned and should not destroy it.

157:    Level: advanced

159: .keywords: PC, null space, remove

161: .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction()
162: @*/
163: PetscErrorCode  MatNullSpaceRemove(MatNullSpace sp,Vec vec,Vec *out)
164: {
165:   PetscScalar    sum;
166:   PetscInt       i,N;


173:   if (out) {
175:     if (!sp->vec) {
176:       VecDuplicate(vec,&sp->vec);
177:       PetscLogObjectParent(sp,sp->vec);
178:     }
179:     VecCopy(vec,sp->vec);
180:     vec = *out = sp->vec;
181:   }
182: 
183:   if (sp->has_cnst) {
184:     VecGetSize(vec,&N);
185:     if (N > 0) {
186:       VecSum(vec,&sum);
187:       sum  = sum/(-1.0*N);
188:       VecShift(vec,sum);
189:     }
190:   }
191: 
192:   if (sp->n) {
193:     VecMDot(vec,sp->n,sp->vecs,sp->alpha);
194:     for (i=0; i<sp->n; i++) sp->alpha[i] = -sp->alpha[i];
195:     VecMAXPY(vec,sp->n,sp->alpha,sp->vecs);
196:   }

198:   if (sp->remove){
199:     (*sp->remove)(vec,sp->rmctx);
200:   }
201:   return(0);
202: }

206: /*@
207:    MatNullSpaceTest  - Tests if the claimed null space is really a
208:      null space of a matrix

210:    Collective on MatNullSpace

212:    Input Parameters:
213: +  sp - the null space context
214: -  mat - the matrix

216:    Level: advanced

218: .keywords: PC, null space, remove

220: .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction()
221: @*/
222: PetscErrorCode  MatNullSpaceTest(MatNullSpace sp,Mat mat)
223: {
224:   PetscScalar    sum;
225:   PetscReal      nrm;
226:   PetscInt       j,n,N,m;
228:   Vec            l,r;
229:   PetscTruth     flg1,flg2;
230:   PetscViewer    viewer;

235:   n = sp->n;
236:   PetscOptionsHasName(PETSC_NULL,"-mat_null_space_test_view",&flg1);
237:   PetscOptionsHasName(PETSC_NULL,"-mat_null_space_test_view_draw",&flg2);

239:   if (!sp->vec) {
240:     if (n) {
241:       VecDuplicate(sp->vecs[0],&sp->vec);
242:     } else {
243:       MatGetLocalSize(mat,&m,PETSC_NULL);
244:       VecCreateMPI(sp->comm,m,PETSC_DETERMINE,&sp->vec);
245:     }
246:   }
247:   l    = sp->vec;

249:   PetscViewerASCIIGetStdout(sp->comm,&viewer);
250:   if (sp->has_cnst) {
251:     VecDuplicate(l,&r);
252:     VecGetSize(l,&N);
253:     sum  = 1.0/N;
254:     VecSet(l,sum);
255:     MatMult(mat,l,r);
256:     VecNorm(r,NORM_2,&nrm);
257:     if (nrm < 1.e-7) {PetscPrintf(sp->comm,"Constants are likely null vector");}
258:     else {PetscPrintf(sp->comm,"Constants are unlikely null vector ");}
259:     PetscPrintf(sp->comm,"|| A * 1 || = %G\n",nrm);
260:     if (nrm > 1.e-7 && flg1) {VecView(r,viewer);}
261:     if (nrm > 1.e-7 && flg2) {VecView(r,viewer);}
262:     VecDestroy(r);
263:   }

265:   for (j=0; j<n; j++) {
266:     (*mat->ops->mult)(mat,sp->vecs[j],l);
267:     VecNorm(l,NORM_2,&nrm);
268:     if (nrm < 1.e-7) {PetscPrintf(sp->comm,"Null vector %D is likely null vector",j);}
269:     else {PetscPrintf(sp->comm,"Null vector %D unlikely null vector ",j);}
270:     PetscPrintf(sp->comm,"|| A * v[%D] || = %G\n",j,nrm);
271:     if (nrm > 1.e-7 && flg1) {VecView(l,viewer);}
272:     if (nrm > 1.e-7 && flg2) {VecView(l,viewer);}
273:   }

275:   if (sp->remove){
276:     SETERRQ(PETSC_ERR_SUP,"Cannot test a null space provided as a function with MatNullSpaceSetFunction()");
277:   }
278: 
279:   return(0);
280: }