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