Actual source code: slepcutil.c

slepc-3.15.1 2021-05-28
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: /*
131:    SlepcDebugViewMatrix - prints an array as a matrix, to be used from within a debugger.
132:    Output can be pasted to Matlab.

134:      nrows, ncols: size of printed matrix
135:      Xr, Xi: array to be printed (Xi not referenced in complex scalars)
136:      ldx: leading dimension
137:      s: name of Matlab variable
138:      filename: optionally write output to a file
139:  */
140: #if defined(PETSC_USE_DEBUG)
141: PetscErrorCode SlepcDebugViewMatrix(PetscInt nrows,PetscInt ncols,PetscScalar *Xr,PetscScalar *Xi,PetscInt ldx,const char *s,const char *filename)
142: {
144:   PetscInt       i,j;
145:   PetscViewer    viewer;

148:   if (filename) {
149:     PetscViewerASCIIOpen(PETSC_COMM_WORLD,filename,&viewer);
150:   } else {
151:     PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
152:   }
153:   PetscViewerASCIIPrintf(viewer,"%s = [\n",s);
154:   for (i=0;i<nrows;i++) {
155:     for (j=0;j<ncols;j++) {
156: #if defined(PETSC_USE_COMPLEX)
157:       PetscViewerASCIIPrintf(viewer,"%.18g+%.18gi ",PetscRealPart(Xr[i+j*ldx]),PetscImaginaryPart(Xr[i+j*ldx]));
158: #else
159:       if (Xi) { PetscViewerASCIIPrintf(viewer,"%.18g+%.18gi ",Xr[i+j*ldx],Xi[i+j*ldx]); }
160:       else { PetscViewerASCIIPrintf(viewer,"%.18g ",Xr[i+j*ldx]); }
161: #endif
162:     }
163:     PetscViewerASCIIPrintf(viewer,"\n");
164:   }
165:   PetscViewerASCIIPrintf(viewer,"];\n");
166:   if (filename) { PetscViewerDestroy(&viewer); }
167:   return(0);
168: }
169: #endif

171: /*
172:    SlepcDebugSetMatlabStdout - sets Matlab format in stdout, to be used from within a debugger.
173:  */
174: #if defined(PETSC_USE_DEBUG)
175: PETSC_UNUSED PetscErrorCode SlepcDebugSetMatlabStdout(void)
176: {
178:   PetscViewer    viewer;

181:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
182:   PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
183:   return(0);
184: }
185: #endif