Actual source code: slepcutil.c

slepc-3.16.1 2021-11-17
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2021, 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>

 13: /*
 14:    Internal functions used to register monitors.
 15:  */
 16: PetscErrorCode SlepcMonitorMakeKey_Internal(const char name[],PetscViewerType vtype,PetscViewerFormat format,char key[])
 17: {

 21:   PetscStrncpy(key,name,PETSC_MAX_PATH_LEN);
 22:   PetscStrlcat(key,":",PETSC_MAX_PATH_LEN);
 23:   PetscStrlcat(key,vtype,PETSC_MAX_PATH_LEN);
 24:   PetscStrlcat(key,":",PETSC_MAX_PATH_LEN);
 25:   PetscStrlcat(key,PetscViewerFormats[format],PETSC_MAX_PATH_LEN);
 26:   return(0);
 27: }

 29: PetscErrorCode PetscViewerAndFormatCreate_Internal(PetscViewer viewer,PetscViewerFormat format,void *ctx,PetscViewerAndFormat **vf)
 30: {

 34:   PetscViewerAndFormatCreate(viewer,format,vf);
 35:   (*vf)->data = ctx;
 36:   return(0);
 37: }

 39: /*
 40:    Given n vectors in V, this function gets references of them into W.
 41:    If m<0 then some previous non-processed vectors remain in W and must be freed.
 42:  */
 43: PetscErrorCode SlepcBasisReference_Private(PetscInt n,Vec *V,PetscInt *m,Vec **W)
 44: {
 46:   PetscInt       i;

 49:   for (i=0;i<n;i++) {
 50:     PetscObjectReference((PetscObject)V[i]);
 51:   }
 52:   SlepcBasisDestroy_Private(m,W);
 53:   if (n>0) {
 54:     PetscMalloc1(n,W);
 55:     for (i=0;i<n;i++) (*W)[i] = V[i];
 56:     *m = -n;
 57:   }
 58:   return(0);
 59: }

 61: /*
 62:    Destroys a set of vectors.
 63:    A negative value of m indicates that W contains vectors to be destroyed.
 64:  */
 65: PetscErrorCode SlepcBasisDestroy_Private(PetscInt *m,Vec **W)
 66: {
 68:   PetscInt       i;

 71:   if (*m<0) {
 72:     for (i=0;i<-(*m);i++) {
 73:       VecDestroy(&(*W)[i]);
 74:     }
 75:     PetscFree(*W);
 76:   }
 77:   *m = 0;
 78:   return(0);
 79: }

 81: /*@C
 82:    SlepcSNPrintfScalar - Prints a PetscScalar variable to a string of
 83:    given length.

 85:    Not Collective

 87:    Input Parameters:
 88: +  str - the string to print to
 89: .  len - the length of str
 90: .  val - scalar value to be printed
 91: -  exp - to be used within an expression, print leading sign and parentheses
 92:          in case of nonzero imaginary part

 94:    Level: developer
 95: @*/
 96: PetscErrorCode SlepcSNPrintfScalar(char *str,size_t len,PetscScalar val,PetscBool exp)
 97: {
 99: #if defined(PETSC_USE_COMPLEX)
100:   PetscReal      re,im;
101: #endif

104: #if !defined(PETSC_USE_COMPLEX)
105:   if (exp) {
106:     PetscSNPrintf(str,len,"%+g",(double)val);
107:   } else {
108:     PetscSNPrintf(str,len,"%g",(double)val);
109:   }
110: #else
111:   re = PetscRealPart(val);
112:   im = PetscImaginaryPart(val);
113:   if (im!=0.0) {
114:     if (exp) {
115:       PetscSNPrintf(str,len,"+(%g%+gi)",(double)re,(double)im);
116:     } else {
117:       PetscSNPrintf(str,len,"%g%+gi",(double)re,(double)im);
118:     }
119:   } else {
120:     if (exp) {
121:       PetscSNPrintf(str,len,"%+g",(double)re);
122:     } else {
123:       PetscSNPrintf(str,len,"%g",(double)re);
124:     }
125:   }
126: #endif
127:   return(0);
128: }

130: /*@C
131:    SlepcHasExternalPackage - Determine whether SLEPc has been configured with the
132:    given package.

134:    Not Collective

136:    Input Parameter:
137: .  pkg - external package name

139:    Output Parameter:
140: .  has - PETSC_TRUE if SLEPc is configured with the given package, else PETSC_FALSE

142:    Level: intermediate

144:    Notes:
145:    This is basically an alternative for SLEPC_HAVE_XXX whenever a preprocessor macro
146:    is not available/desirable, e.g. in Python.

148:    The external package name pkg is e.g. "arpack", "primme".
149:    It should correspond to the name listed in  ./configure --help

151:    The lookup is case insensitive, i.e. looking for "ARPACK" or "arpack" is the same.
152: @*/
153: PetscErrorCode SlepcHasExternalPackage(const char pkg[], PetscBool *has)
154: {
155:   char           pkgstr[128],*loc;
156:   size_t         cnt;

160:   PetscSNPrintfCount(pkgstr,sizeof(pkgstr),":%s:",&cnt,pkg);
161:   if (cnt >= sizeof(pkgstr)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Package name is too long: \"%s\"",pkg);
162:   PetscStrtolower(pkgstr);
163: #if defined(SLEPC_HAVE_PACKAGES)
164:   PetscStrstr(SLEPC_HAVE_PACKAGES,pkgstr,&loc);
165: #else
166: #error "SLEPC_HAVE_PACKAGES macro undefined. Please reconfigure"
167: #endif
168:   *has = loc? PETSC_TRUE: PETSC_FALSE;
169:   return(0);
170: }

172: /*
173:    SlepcDebugViewMatrix - prints an array as a matrix, to be used from within a debugger.
174:    Output can be pasted to Matlab.

176:      nrows, ncols: size of printed matrix
177:      Xr, Xi: array to be printed (Xi not referenced in complex scalars)
178:      ldx: leading dimension
179:      s: name of Matlab variable
180:      filename: optionally write output to a file
181:  */
182: #if defined(PETSC_USE_DEBUG)
183: PetscErrorCode SlepcDebugViewMatrix(PetscInt nrows,PetscInt ncols,PetscScalar *Xr,PetscScalar *Xi,PetscInt ldx,const char *s,const char *filename)
184: {
186:   PetscInt       i,j;
187:   PetscViewer    viewer;

190:   if (filename) {
191:     PetscViewerASCIIOpen(PETSC_COMM_WORLD,filename,&viewer);
192:   } else {
193:     PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
194:   }
195:   PetscViewerASCIIPrintf(viewer,"%s = [\n",s);
196:   for (i=0;i<nrows;i++) {
197:     for (j=0;j<ncols;j++) {
198: #if defined(PETSC_USE_COMPLEX)
199:       PetscViewerASCIIPrintf(viewer,"%.18g+%.18gi ",PetscRealPart(Xr[i+j*ldx]),PetscImaginaryPart(Xr[i+j*ldx]));
200: #else
201:       if (Xi) { PetscViewerASCIIPrintf(viewer,"%.18g+%.18gi ",Xr[i+j*ldx],Xi[i+j*ldx]); }
202:       else { PetscViewerASCIIPrintf(viewer,"%.18g ",Xr[i+j*ldx]); }
203: #endif
204:     }
205:     PetscViewerASCIIPrintf(viewer,"\n");
206:   }
207:   PetscViewerASCIIPrintf(viewer,"];\n");
208:   if (filename) { PetscViewerDestroy(&viewer); }
209:   return(0);
210: }
211: #endif

213: /*
214:    SlepcDebugSetMatlabStdout - sets Matlab format in stdout, to be used from within a debugger.
215:  */
216: #if defined(PETSC_USE_DEBUG)
217: PETSC_UNUSED PetscErrorCode SlepcDebugSetMatlabStdout(void)
218: {
220:   PetscViewer    viewer;

223:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
224:   PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
225:   return(0);
226: }
227: #endif