Actual source code: slepcutil.c
slepc-3.17.2 2022-08-09
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-, 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: {
18: PetscStrncpy(key,name,PETSC_MAX_PATH_LEN);
19: PetscStrlcat(key,":",PETSC_MAX_PATH_LEN);
20: PetscStrlcat(key,vtype,PETSC_MAX_PATH_LEN);
21: PetscStrlcat(key,":",PETSC_MAX_PATH_LEN);
22: PetscStrlcat(key,PetscViewerFormats[format],PETSC_MAX_PATH_LEN);
23: PetscFunctionReturn(0);
24: }
26: PetscErrorCode PetscViewerAndFormatCreate_Internal(PetscViewer viewer,PetscViewerFormat format,void *ctx,PetscViewerAndFormat **vf)
27: {
28: PetscViewerAndFormatCreate(viewer,format,vf);
29: (*vf)->data = ctx;
30: PetscFunctionReturn(0);
31: }
33: /*
34: Given n vectors in V, this function gets references of them into W.
35: If m<0 then some previous non-processed vectors remain in W and must be freed.
36: */
37: PetscErrorCode SlepcBasisReference_Private(PetscInt n,Vec *V,PetscInt *m,Vec **W)
38: {
39: PetscInt i;
41: for (i=0;i<n;i++) PetscObjectReference((PetscObject)V[i]);
42: SlepcBasisDestroy_Private(m,W);
43: if (n>0) {
44: PetscMalloc1(n,W);
45: for (i=0;i<n;i++) (*W)[i] = V[i];
46: *m = -n;
47: }
48: PetscFunctionReturn(0);
49: }
51: /*
52: Destroys a set of vectors.
53: A negative value of m indicates that W contains vectors to be destroyed.
54: */
55: PetscErrorCode SlepcBasisDestroy_Private(PetscInt *m,Vec **W)
56: {
57: PetscInt i;
59: if (*m<0) {
60: for (i=0;i<-(*m);i++) VecDestroy(&(*W)[i]);
61: PetscFree(*W);
62: }
63: *m = 0;
64: PetscFunctionReturn(0);
65: }
67: /*@C
68: SlepcSNPrintfScalar - Prints a PetscScalar variable to a string of
69: given length.
71: Not Collective
73: Input Parameters:
74: + str - the string to print to
75: . len - the length of str
76: . val - scalar value to be printed
77: - exp - to be used within an expression, print leading sign and parentheses
78: in case of nonzero imaginary part
80: Level: developer
82: .seealso: PetscSNPrintf()
83: @*/
84: PetscErrorCode SlepcSNPrintfScalar(char *str,size_t len,PetscScalar val,PetscBool exp)
85: {
86: #if defined(PETSC_USE_COMPLEX)
87: PetscReal re,im;
88: #endif
90: #if !defined(PETSC_USE_COMPLEX)
91: if (exp) PetscSNPrintf(str,len,"%+g",(double)val);
92: else PetscSNPrintf(str,len,"%g",(double)val);
93: #else
94: re = PetscRealPart(val);
95: im = PetscImaginaryPart(val);
96: if (im!=0.0) {
97: if (exp) PetscSNPrintf(str,len,"+(%g%+gi)",(double)re,(double)im);
98: else PetscSNPrintf(str,len,"%g%+gi",(double)re,(double)im);
99: } else {
100: if (exp) PetscSNPrintf(str,len,"%+g",(double)re);
101: else PetscSNPrintf(str,len,"%g",(double)re);
102: }
103: #endif
104: PetscFunctionReturn(0);
105: }
107: /*@C
108: SlepcHasExternalPackage - Determine whether SLEPc has been configured with the
109: given package.
111: Not Collective
113: Input Parameter:
114: . pkg - external package name
116: Output Parameter:
117: . has - PETSC_TRUE if SLEPc is configured with the given package, else PETSC_FALSE
119: Level: intermediate
121: Notes:
122: This is basically an alternative for SLEPC_HAVE_XXX whenever a preprocessor macro
123: is not available/desirable, e.g. in Python.
125: The external package name pkg is e.g. "arpack", "primme".
126: It should correspond to the name listed in ./configure --help
128: The lookup is case insensitive, i.e. looking for "ARPACK" or "arpack" is the same.
130: .seealso: EPSType, SVDType
131: @*/
132: PetscErrorCode SlepcHasExternalPackage(const char pkg[], PetscBool *has)
133: {
134: char pkgstr[128],*loc;
135: size_t cnt;
137: PetscSNPrintfCount(pkgstr,sizeof(pkgstr),":%s:",&cnt,pkg);
139: PetscStrtolower(pkgstr);
140: #if defined(SLEPC_HAVE_PACKAGES)
141: PetscStrstr(SLEPC_HAVE_PACKAGES,pkgstr,&loc);
142: #else
143: #error "SLEPC_HAVE_PACKAGES macro undefined. Please reconfigure"
144: #endif
145: *has = loc? PETSC_TRUE: PETSC_FALSE;
146: PetscFunctionReturn(0);
147: }
149: /*
150: SlepcDebugViewMatrix - prints an array as a matrix, to be used from within a debugger.
151: Output can be pasted to Matlab.
153: nrows, ncols: size of printed matrix
154: Xr, Xi: array to be printed (Xi not referenced in complex scalars)
155: ldx: leading dimension
156: s: name of Matlab variable
157: filename: optionally write output to a file
158: */
159: #if defined(PETSC_USE_DEBUG)
160: PetscErrorCode SlepcDebugViewMatrix(PetscInt nrows,PetscInt ncols,PetscScalar *Xr,PetscScalar *Xi,PetscInt ldx,const char *s,const char *filename)
161: {
162: PetscInt i,j;
163: PetscViewer viewer;
165: if (filename) PetscViewerASCIIOpen(PETSC_COMM_WORLD,filename,&viewer);
166: else PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
167: PetscViewerASCIIPrintf(viewer,"%s = [\n",s);
168: for (i=0;i<nrows;i++) {
169: for (j=0;j<ncols;j++) {
170: #if defined(PETSC_USE_COMPLEX)
171: PetscViewerASCIIPrintf(viewer,"%.18g+%.18gi ",PetscRealPart(Xr[i+j*ldx]),PetscImaginaryPart(Xr[i+j*ldx]));
172: #else
173: if (Xi) PetscViewerASCIIPrintf(viewer,"%.18g+%.18gi ",Xr[i+j*ldx],Xi[i+j*ldx]);
174: else PetscViewerASCIIPrintf(viewer,"%.18g ",Xr[i+j*ldx]);
175: #endif
176: }
177: PetscViewerASCIIPrintf(viewer,"\n");
178: }
179: PetscViewerASCIIPrintf(viewer,"];\n");
180: if (filename) PetscViewerDestroy(&viewer);
181: PetscFunctionReturn(0);
182: }
183: #endif
185: /*
186: SlepcDebugSetMatlabStdout - sets Matlab format in stdout, to be used from within a debugger.
187: */
188: #if defined(PETSC_USE_DEBUG)
189: PETSC_UNUSED PetscErrorCode SlepcDebugSetMatlabStdout(void)
190: {
191: PetscViewer viewer;
193: PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
194: PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
195: PetscFunctionReturn(0);
196: }
197: #endif