Actual source code: slepcutil.c

slepc-3.11.2 2019-07-30
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2019, Universitat Politecnica de Valencia, Spain

  6:    This file is part of SLEPc.
  7:    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
  8:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  9: */

 11: #include <slepc/private/slepcimpl.h>            /*I "slepcsys.h" I*/

 13: /*@C
 14:    SlepcConvMonitorCreate - Creates a SlepcConvMonitor context.

 16:    Collective on PetscViewer

 18:    Input Parameters:
 19: +  viewer - the viewer where the monitor must send data
 20: -  format - the format

 22:    Output Parameter:
 23: .  ctx - the created context

 25:    Notes:
 26:    The created context is used for EPS, SVD, PEP, and NEP monitor functions that just
 27:    print the iteration numbers at which convergence takes place (XXXMonitorConverged).

 29:    This function increases the reference count of the viewer so you can destroy the
 30:    viewer object after this call.

 32:    Level: developer

 34: .seealso: SlepcConvMonitorDestroy()
 35: @*/
 36: PetscErrorCode SlepcConvMonitorCreate(PetscViewer viewer,PetscViewerFormat format,SlepcConvMonitor *ctx)
 37: {

 41:   PetscObjectReference((PetscObject)viewer);
 42:   PetscNew(ctx);
 43:   (*ctx)->viewer = viewer;
 44:   (*ctx)->format = format;
 45:   return(0);
 46: }

 48: /*@C
 49:    SlepcConvMonitorDestroy - Destroys a SlepcConvMonitor context.

 51:    Collective on PetscViewer

 53:    Input Parameters:
 54: .  ctx - the SlepcConvMonitor context to be destroyed.

 56:    Level: developer

 58: .seealso: SlepcConvMonitorCreate()
 59: @*/
 60: PetscErrorCode SlepcConvMonitorDestroy(SlepcConvMonitor *ctx)
 61: {

 65:   if (!*ctx) return(0);
 66:   PetscViewerDestroy(&(*ctx)->viewer);
 67:   PetscFree(*ctx);
 68:   return(0);
 69: }

 71: /*
 72:    Given n vectors in V, this function gets references of them into W.
 73:    If m<0 then some previous non-processed vectors remain in W and must be freed.
 74:  */
 75: PetscErrorCode SlepcBasisReference_Private(PetscInt n,Vec *V,PetscInt *m,Vec **W)
 76: {
 78:   PetscInt       i;

 81:   for (i=0;i<n;i++) {
 82:     PetscObjectReference((PetscObject)V[i]);
 83:   }
 84:   SlepcBasisDestroy_Private(m,W);
 85:   if (n>0) {
 86:     PetscMalloc1(n,W);
 87:     for (i=0;i<n;i++) (*W)[i] = V[i];
 88:     *m = -n;
 89:   }
 90:   return(0);
 91: }

 93: /*
 94:    Destroys a set of vectors.
 95:    A negative value of m indicates that W contains vectors to be destroyed.
 96:  */
 97: PetscErrorCode SlepcBasisDestroy_Private(PetscInt *m,Vec **W)
 98: {
100:   PetscInt       i;

103:   if (*m<0) {
104:     for (i=0;i<-(*m);i++) {
105:       VecDestroy(&(*W)[i]);
106:     }
107:     PetscFree(*W);
108:   }
109:   *m = 0;
110:   return(0);
111: }

113: /*@C
114:    SlepcSNPrintfScalar - Prints a PetscScalar variable to a string of
115:    given length.

117:    Not Collective

119:    Input Parameters:
120: +  str - the string to print to
121: .  len - the length of str
122: .  val - scalar value to be printed
123: -  exp - to be used within an expression, print leading sign and parentheses
124:          in case of nonzero imaginary part

126:    Level: developer
127: @*/
128: PetscErrorCode SlepcSNPrintfScalar(char *str,size_t len,PetscScalar val,PetscBool exp)
129: {
131: #if defined(PETSC_USE_COMPLEX)
132:   PetscReal      re,im;
133: #endif

136: #if !defined(PETSC_USE_COMPLEX)
137:   if (exp) {
138:     PetscSNPrintf(str,len,"%+g",(double)val);
139:   } else {
140:     PetscSNPrintf(str,len,"%g",(double)val);
141:   }
142: #else
143:   re = PetscRealPart(val);
144:   im = PetscImaginaryPart(val);
145:   if (im!=0.0) {
146:     if (exp) {
147:       PetscSNPrintf(str,len,"+(%g%+gi)",(double)re,(double)im);
148:     } else {
149:       PetscSNPrintf(str,len,"%g%+gi",(double)re,(double)im);
150:     }
151:   } else {
152:     if (exp) {
153:       PetscSNPrintf(str,len,"%+g",(double)re);
154:     } else {
155:       PetscSNPrintf(str,len,"%g",(double)re);
156:     }
157:   }
158: #endif
159:   return(0);
160: }

162: /*
163:    SlepcDebugViewMatrix - prints an array as a matrix, to be used from within a debugger.
164:    Output can be pasted to Matlab.

166:      nrows, ncols: size of printed matrix
167:      Xr, Xi: array to be printed (Xi not referenced in complex scalars)
168:      ldx: leading dimension
169:      s: name of Matlab variable
170:      filename: optionally write output to a file
171:  */
172: #if defined(PETSC_USE_DEBUG)
173: PetscErrorCode SlepcDebugViewMatrix(PetscInt nrows,PetscInt ncols,PetscScalar *Xr,PetscScalar *Xi,PetscInt ldx,const char *s,const char *filename)
174: {
176:   PetscInt       i,j;
177:   PetscViewer    viewer;

180:   if (filename) {
181:     PetscViewerASCIIOpen(PETSC_COMM_WORLD,filename,&viewer);
182:   } else {
183:     PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
184:   }
185:   PetscViewerASCIIPrintf(viewer,"%s = [\n",s);
186:   for (i=0;i<nrows;i++) {
187:     for (j=0;j<ncols;j++) {
188: #if defined(PETSC_USE_COMPLEX)
189:       PetscViewerASCIIPrintf(viewer,"%.18g+%.18gi ",PetscRealPart(Xr[i+j*ldx]),PetscImaginaryPart(Xr[i+j*ldx]));
190: #else
191:       if (Xi) { PetscViewerASCIIPrintf(viewer,"%.18g+%.18gi ",Xr[i+j*ldx],Xi[i+j*ldx]); }
192:       else { PetscViewerASCIIPrintf(viewer,"%.18g ",Xr[i+j*ldx]); }
193: #endif
194:     }
195:     PetscViewerASCIIPrintf(viewer,"\n");
196:   }
197:   PetscViewerASCIIPrintf(viewer,"];\n");
198:   if (filename) { PetscViewerDestroy(&viewer); }
199:   return(0);
200: }
201: #endif

203: /*
204:    SlepcDebugSetMatlabStdout - sets Matlab format in stdout, to be used from within a debugger.
205:  */
206: #if defined(PETSC_USE_DEBUG)
207: PETSC_UNUSED PetscErrorCode SlepcDebugSetMatlabStdout(void)
208: {
210:   PetscViewer    viewer;

213:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
214:   PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
215:   return(0);
216: }
217: #endif