Actual source code: matnull.c

petsc-3.13.6 2020-09-29
Report Typos and Errors

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

  6:  #include <petsc/private/matimpl.h>

  8: PetscClassId MAT_NULLSPACE_CLASSID;

 10: /*@C
 11:    MatNullSpaceSetFunction - set a function that removes a null space from a vector
 12:    out of null spaces.

 14:    Logically Collective on MatNullSpace

 16:    Input Parameters:
 17: +  sp - the null space object
 18: .  rem - the function that removes the null space
 19: -  ctx - context for the remove function

 21:    Level: advanced

 23: .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), MatSetNullSpace(), MatNullSpace, MatNullSpaceCreate()
 24: @*/
 25: PetscErrorCode  MatNullSpaceSetFunction(MatNullSpace sp, PetscErrorCode (*rem)(MatNullSpace,Vec,void*),void *ctx)
 26: {
 29:   sp->remove = rem;
 30:   sp->rmctx  = ctx;
 31:   return(0);
 32: }

 34: /*@C
 35:    MatNullSpaceGetVecs - get vectors defining the null space

 37:    Not Collective

 39:    Input Arguments:
 40: .  sp - null space object

 42:    Output Arguments:
 43: +  has_cnst - PETSC_TRUE if the null space contains the constant vector, otherwise PETSC_FALSE
 44: .  n - number of vectors (excluding constant vector) in null space
 45: -  vecs - orthonormal vectors that span the null space (excluding the constant vector)

 47:    Level: developer

 49:    Notes:
 50:       These vectors and the array are owned by the MatNullSpace and should not be destroyed or freeded by the caller

 52: .seealso: MatNullSpaceCreate(), MatGetNullSpace(), MatGetNearNullSpace()
 53: @*/
 54: PetscErrorCode MatNullSpaceGetVecs(MatNullSpace sp,PetscBool *has_const,PetscInt *n,const Vec **vecs)
 55: {

 59:   if (has_const) *has_const = sp->has_cnst;
 60:   if (n) *n = sp->n;
 61:   if (vecs) *vecs = sp->vecs;
 62:   return(0);
 63: }

 65: /*@
 66:    MatNullSpaceCreateRigidBody - create rigid body modes from coordinates

 68:    Collective on Vec

 70:    Input Argument:
 71: .  coords - block of coordinates of each node, must have block size set

 73:    Output Argument:
 74: .  sp - the null space

 76:    Level: advanced

 78:    Notes:
 79:      If you are solving an elasticity problem you should likely use this, in conjunction with MatSetNearNullspace(), to provide information that 
 80:      the PCGAMG preconditioner can use to construct a much more efficient preconditioner.

 82:      If you are solving an elasticity problem with pure Neumann boundary conditions you can use this in conjunction with MatSetNullspace() to
 83:      provide this information to the linear solver so it can handle the null space appropriately in the linear solution.


 86: .seealso: MatNullSpaceCreate(), MatSetNearNullspace(), MatSetNullspace()
 87: @*/
 88: PetscErrorCode MatNullSpaceCreateRigidBody(Vec coords,MatNullSpace *sp)
 89: {
 90:   PetscErrorCode    ierr;
 91:   const PetscScalar *x;
 92:   PetscScalar       *v[6],dots[5];
 93:   Vec               vec[6];
 94:   PetscInt          n,N,dim,nmodes,i,j;
 95:   PetscReal         sN;

 98:   VecGetBlockSize(coords,&dim);
 99:   VecGetLocalSize(coords,&n);
100:   VecGetSize(coords,&N);
101:   n   /= dim;
102:   N   /= dim;
103:   sN = 1./PetscSqrtReal((PetscReal)N);
104:   switch (dim) {
105:   case 1:
106:     MatNullSpaceCreate(PetscObjectComm((PetscObject)coords),PETSC_TRUE,0,NULL,sp);
107:     break;
108:   case 2:
109:   case 3:
110:     nmodes = (dim == 2) ? 3 : 6;
111:     VecCreate(PetscObjectComm((PetscObject)coords),&vec[0]);
112:     VecSetSizes(vec[0],dim*n,dim*N);
113:     VecSetBlockSize(vec[0],dim);
114:     VecSetUp(vec[0]);
115:     for (i=1; i<nmodes; i++) {VecDuplicate(vec[0],&vec[i]);}
116:     for (i=0; i<nmodes; i++) {VecGetArray(vec[i],&v[i]);}
117:     VecGetArrayRead(coords,&x);
118:     for (i=0; i<n; i++) {
119:       if (dim == 2) {
120:         v[0][i*2+0] = sN;
121:         v[0][i*2+1] = 0.;
122:         v[1][i*2+0] = 0.;
123:         v[1][i*2+1] = sN;
124:         /* Rotations */
125:         v[2][i*2+0] = -x[i*2+1];
126:         v[2][i*2+1] = x[i*2+0];
127:       } else {
128:         v[0][i*3+0] = sN;
129:         v[0][i*3+1] = 0.;
130:         v[0][i*3+2] = 0.;
131:         v[1][i*3+0] = 0.;
132:         v[1][i*3+1] = sN;
133:         v[1][i*3+2] = 0.;
134:         v[2][i*3+0] = 0.;
135:         v[2][i*3+1] = 0.;
136:         v[2][i*3+2] = sN;

138:         v[3][i*3+0] = x[i*3+1];
139:         v[3][i*3+1] = -x[i*3+0];
140:         v[3][i*3+2] = 0.;
141:         v[4][i*3+0] = 0.;
142:         v[4][i*3+1] = -x[i*3+2];
143:         v[4][i*3+2] = x[i*3+1];
144:         v[5][i*3+0] = x[i*3+2];
145:         v[5][i*3+1] = 0.;
146:         v[5][i*3+2] = -x[i*3+0];
147:       }
148:     }
149:     for (i=0; i<nmodes; i++) {VecRestoreArray(vec[i],&v[i]);}
150:     VecRestoreArrayRead(coords,&x);
151:     for (i=dim; i<nmodes; i++) {
152:       /* Orthonormalize vec[i] against vec[0:i-1] */
153:       VecMDot(vec[i],i,vec,dots);
154:       for (j=0; j<i; j++) dots[j] *= -1.;
155:       VecMAXPY(vec[i],i,dots,vec);
156:       VecNormalize(vec[i],NULL);
157:     }
158:     MatNullSpaceCreate(PetscObjectComm((PetscObject)coords),PETSC_FALSE,nmodes,vec,sp);
159:     for (i=0; i<nmodes; i++) {VecDestroy(&vec[i]);}
160:   }
161:   return(0);
162: }

164: /*@C
165:    MatNullSpaceView - Visualizes a null space object.

167:    Collective on MatNullSpace

169:    Input Parameters:
170: +  matnull - the null space
171: -  viewer - visualization context

173:    Level: advanced

175:    Fortran Note:
176:    This routine is not supported in Fortran.

178: .seealso: MatNullSpaceCreate(), PetscViewerASCIIOpen()
179: @*/
180: PetscErrorCode MatNullSpaceView(MatNullSpace sp,PetscViewer viewer)
181: {
183:   PetscBool      iascii;

187:   if (!viewer) {
188:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)sp),&viewer);
189:   }

193:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
194:   if (iascii) {
195:     PetscViewerFormat format;
196:     PetscInt          i;
197:     PetscViewerGetFormat(viewer,&format);
198:     PetscObjectPrintClassNamePrefixType((PetscObject)sp,viewer);
199:     PetscViewerASCIIPushTab(viewer);
200:     PetscViewerASCIIPrintf(viewer,"Contains %D vector%s%s\n",sp->n,sp->n==1 ? "" : "s",sp->has_cnst ? " and the constant" : "");
201:     if (sp->remove) {PetscViewerASCIIPrintf(viewer,"Has user-provided removal function\n");}
202:     if (!(format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL)) {
203:       for (i=0; i<sp->n; i++) {
204:         VecView(sp->vecs[i],viewer);
205:       }
206:     }
207:     PetscViewerASCIIPopTab(viewer);
208:   }
209:   return(0);
210: }

212: /*@C
213:    MatNullSpaceCreate - Creates a data structure used to project vectors
214:    out of null spaces.

216:    Collective

218:    Input Parameters:
219: +  comm - the MPI communicator associated with the object
220: .  has_cnst - PETSC_TRUE if the null space contains the constant vector; otherwise PETSC_FALSE
221: .  n - number of vectors (excluding constant vector) in null space
222: -  vecs - the vectors that span the null space (excluding the constant vector);
223:           these vectors must be orthonormal. These vectors are NOT copied, so do not change them
224:           after this call. You should free the array that you pass in and destroy the vectors (this will reduce the reference count
225:           for them by one).

227:    Output Parameter:
228: .  SP - the null space context

230:    Level: advanced

232:    Notes:
233:     See MatNullSpaceSetFunction() as an alternative way of providing the null space information instead of setting vecs.

235:       If has_cnst is PETSC_TRUE you do not need to pass a constant vector in as a fourth argument to this routine, nor do you
236:        need to pass in a function that eliminates the constant function into MatNullSpaceSetFunction().

238:   Users manual sections:
239: .   Section 4.19 Solving Singular Systems

241: .seealso: MatNullSpaceDestroy(), MatNullSpaceRemove(), MatSetNullSpace(), MatNullSpace, MatNullSpaceSetFunction()
242: @*/
243: PetscErrorCode  MatNullSpaceCreate(MPI_Comm comm,PetscBool has_cnst,PetscInt n,const Vec vecs[],MatNullSpace *SP)
244: {
245:   MatNullSpace   sp;
247:   PetscInt       i;

250:   if (n < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of vectors (given %D) cannot be negative",n);
254:   if (n) {
255:     for (i=0; i<n; i++) {
256:       /* prevent the user from changes values in the vector */
257:       VecLockReadPush(vecs[i]);
258:     }
259:   }
260: #if defined(PETSC_USE_DEBUG)
261:   if (n) {
262:     PetscScalar *dots;
263:     for (i=0; i<n; i++) {
264:       PetscReal norm;
265:       VecNorm(vecs[i],NORM_2,&norm);
266:       if (PetscAbsReal(norm - 1) > PETSC_SQRT_MACHINE_EPSILON) SETERRQ2(PetscObjectComm((PetscObject)vecs[i]),PETSC_ERR_ARG_WRONG,"Vector %D must have 2-norm of 1.0, it is %g",i,(double)norm);
267:     }
268:     if (has_cnst) {
269:       for (i=0; i<n; i++) {
270:         PetscScalar sum;
271:         VecSum(vecs[i],&sum);
272:         if (PetscAbsScalar(sum) > PETSC_SQRT_MACHINE_EPSILON) SETERRQ2(PetscObjectComm((PetscObject)vecs[i]),PETSC_ERR_ARG_WRONG,"Vector %D must be orthogonal to constant vector, inner product is %g",i,(double)PetscAbsScalar(sum));
273:       }
274:     }
275:     PetscMalloc1(n-1,&dots);
276:     for (i=0; i<n-1; i++) {
277:       PetscInt j;
278:       VecMDot(vecs[i],n-i-1,vecs+i+1,dots);
279:       for (j=0;j<n-i-1;j++) {
280:         if (PetscAbsScalar(dots[j]) > PETSC_SQRT_MACHINE_EPSILON) SETERRQ3(PetscObjectComm((PetscObject)vecs[i]),PETSC_ERR_ARG_WRONG,"Vector %D must be orthogonal to vector %D, inner product is %g",i,i+j+1,(double)PetscAbsScalar(dots[j]));
281:       }
282:     }
283:     PetscFree(dots);
284:   }
285: #endif

287:   *SP = NULL;
288:   MatInitializePackage();

290:   PetscHeaderCreate(sp,MAT_NULLSPACE_CLASSID,"MatNullSpace","Null space","Mat",comm,MatNullSpaceDestroy,MatNullSpaceView);

292:   sp->has_cnst = has_cnst;
293:   sp->n        = n;
294:   sp->vecs     = 0;
295:   sp->alpha    = 0;
296:   sp->remove   = 0;
297:   sp->rmctx    = 0;

299:   if (n) {
300:     PetscMalloc1(n,&sp->vecs);
301:     PetscMalloc1(n,&sp->alpha);
302:     PetscLogObjectMemory((PetscObject)sp,n*(sizeof(Vec)+sizeof(PetscScalar)));
303:     for (i=0; i<n; i++) {
304:       PetscObjectReference((PetscObject)vecs[i]);
305:       sp->vecs[i] = vecs[i];
306:     }
307:   }

309:   *SP = sp;
310:   return(0);
311: }

313: /*@
314:    MatNullSpaceDestroy - Destroys a data structure used to project vectors
315:    out of null spaces.

317:    Collective on MatNullSpace

319:    Input Parameter:
320: .  sp - the null space context to be destroyed

322:    Level: advanced

324: .seealso: MatNullSpaceCreate(), MatNullSpaceRemove(), MatNullSpaceSetFunction()
325: @*/
326: PetscErrorCode  MatNullSpaceDestroy(MatNullSpace *sp)
327: {
329:   PetscInt       i;

332:   if (!*sp) return(0);
334:   if (--((PetscObject)(*sp))->refct > 0) {*sp = 0; return(0);}

336:   for (i=0; i < (*sp)->n; i++) {
337:     VecLockReadPop((*sp)->vecs[i]);
338:   }

340:   VecDestroyVecs((*sp)->n,&(*sp)->vecs);
341:   PetscFree((*sp)->alpha);
342:   PetscHeaderDestroy(sp);
343:   return(0);
344: }

346: /*@C
347:    MatNullSpaceRemove - Removes all the components of a null space from a vector.

349:    Collective on MatNullSpace

351:    Input Parameters:
352: +  sp - the null space context (if this is NULL then no null space is removed)
353: -  vec - the vector from which the null space is to be removed

355:    Level: advanced

357: .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction()
358: @*/
359: PetscErrorCode  MatNullSpaceRemove(MatNullSpace sp,Vec vec)
360: {
361:   PetscScalar    sum;
362:   PetscInt       i,N;

366:   if (!sp) return(0);

370:   if (sp->has_cnst) {
371:     VecGetSize(vec,&N);
372:     if (N > 0) {
373:       VecSum(vec,&sum);
374:       sum  = sum/((PetscScalar)(-1.0*N));
375:       VecShift(vec,sum);
376:     }
377:   }

379:   if (sp->n) {
380:     VecMDot(vec,sp->n,sp->vecs,sp->alpha);
381:     for (i=0; i<sp->n; i++) sp->alpha[i] = -sp->alpha[i];
382:     VecMAXPY(vec,sp->n,sp->alpha,sp->vecs);
383:   }

385:   if (sp->remove) {
386:     (*sp->remove)(sp,vec,sp->rmctx);
387:   }
388:   return(0);
389: }

391: /*@
392:    MatNullSpaceTest  - Tests if the claimed null space is really a
393:      null space of a matrix

395:    Collective on MatNullSpace

397:    Input Parameters:
398: +  sp - the null space context
399: -  mat - the matrix

401:    Output Parameters:
402: .  isNull - PETSC_TRUE if the nullspace is valid for this matrix

404:    Level: advanced

406: .seealso: MatNullSpaceCreate(), MatNullSpaceDestroy(), MatNullSpaceSetFunction()
407: @*/
408: PetscErrorCode  MatNullSpaceTest(MatNullSpace sp,Mat mat,PetscBool  *isNull)
409: {
410:   PetscScalar    sum;
411:   PetscReal      nrm,tol = 10. * PETSC_SQRT_MACHINE_EPSILON;
412:   PetscInt       j,n,N;
414:   Vec            l,r;
415:   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,consistent = PETSC_TRUE;
416:   PetscViewer    viewer;

421:   n    = sp->n;
422:   PetscOptionsGetBool(((PetscObject)sp)->options,((PetscObject)mat)->prefix,"-mat_null_space_test_view",&flg1,NULL);
423:   PetscOptionsGetBool(((PetscObject)sp)->options,((PetscObject)mat)->prefix,"-mat_null_space_test_view_draw",&flg2,NULL);

425:   if (n) {
426:     VecDuplicate(sp->vecs[0],&l);
427:   } else {
428:     MatCreateVecs(mat,&l,NULL);
429:   }

431:   PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)sp),&viewer);
432:   if (sp->has_cnst) {
433:     VecDuplicate(l,&r);
434:     VecGetSize(l,&N);
435:     sum  = 1.0/N;
436:     VecSet(l,sum);
437:     MatMult(mat,l,r);
438:     VecNorm(r,NORM_2,&nrm);
439:     if (nrm >= tol) consistent = PETSC_FALSE;
440:     if (flg1) {
441:       if (consistent) {
442:         PetscPrintf(PetscObjectComm((PetscObject)sp),"Constants are likely null vector");
443:       } else {
444:         PetscPrintf(PetscObjectComm((PetscObject)sp),"Constants are unlikely null vector ");
445:       }
446:       PetscPrintf(PetscObjectComm((PetscObject)sp),"|| A * 1/N || = %g\n",(double)nrm);
447:     }
448:     if (!consistent && flg1) {VecView(r,viewer);}
449:     if (!consistent && flg2) {VecView(r,viewer);}
450:     VecDestroy(&r);
451:   }

453:   for (j=0; j<n; j++) {
454:     (*mat->ops->mult)(mat,sp->vecs[j],l);
455:     VecNorm(l,NORM_2,&nrm);
456:     if (nrm >= tol) consistent = PETSC_FALSE;
457:     if (flg1) {
458:       if (consistent) {
459:         PetscPrintf(PetscObjectComm((PetscObject)sp),"Null vector %D is likely null vector",j);
460:       } else {
461:         PetscPrintf(PetscObjectComm((PetscObject)sp),"Null vector %D unlikely null vector ",j);
462:         consistent = PETSC_FALSE;
463:       }
464:       PetscPrintf(PetscObjectComm((PetscObject)sp),"|| A * v[%D] || = %g\n",j,(double)nrm);
465:     }
466:     if (!consistent && flg1) {VecView(l,viewer);}
467:     if (!consistent && flg2) {VecView(l,viewer);}
468:   }

470:   if (sp->remove) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"Cannot test a null space provided as a function with MatNullSpaceSetFunction()");
471:   VecDestroy(&l);
472:   if (isNull) *isNull = consistent;
473:   return(0);
474: }