Actual source code: slepcutil.c

slepc-3.10.1 2018-10-23
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2018, 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: }