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 : MFN routines related to monitors
12 : */
13 :
14 : #include <slepc/private/mfnimpl.h> /*I "slepcmfn.h" I*/
15 : #include <petscdraw.h>
16 :
17 0 : PetscErrorCode MFNMonitorLGCreate(MPI_Comm comm,const char host[],const char label[],const char metric[],PetscInt l,const char *names[],int x,int y,int m,int n,PetscDrawLG *lgctx)
18 : {
19 0 : PetscDraw draw;
20 0 : PetscDrawAxis axis;
21 0 : PetscDrawLG lg;
22 :
23 0 : PetscFunctionBegin;
24 0 : PetscCall(PetscDrawCreate(comm,host,label,x,y,m,n,&draw));
25 0 : PetscCall(PetscDrawSetFromOptions(draw));
26 0 : PetscCall(PetscDrawLGCreate(draw,l,&lg));
27 0 : if (names) PetscCall(PetscDrawLGSetLegend(lg,names));
28 0 : PetscCall(PetscDrawLGSetFromOptions(lg));
29 0 : PetscCall(PetscDrawLGGetAxis(lg,&axis));
30 0 : PetscCall(PetscDrawAxisSetLabels(axis,"Convergence","Iteration",metric));
31 0 : PetscCall(PetscDrawDestroy(&draw));
32 0 : *lgctx = lg;
33 0 : PetscFunctionReturn(PETSC_SUCCESS);
34 : }
35 :
36 : /*
37 : Runs the user provided monitor routines, if any.
38 : */
39 230 : PetscErrorCode MFNMonitor(MFN mfn,PetscInt it,PetscReal errest)
40 : {
41 230 : PetscInt i,n = mfn->numbermonitors;
42 :
43 230 : PetscFunctionBegin;
44 236 : for (i=0;i<n;i++) PetscCall((*mfn->monitor[i])(mfn,it,errest,mfn->monitorcontext[i]));
45 230 : PetscFunctionReturn(PETSC_SUCCESS);
46 : }
47 :
48 : /*@C
49 : MFNMonitorSet - Sets an ADDITIONAL function to be called at every
50 : iteration to monitor convergence.
51 :
52 : Logically Collective
53 :
54 : Input Parameters:
55 : + mfn - matrix function context obtained from MFNCreate()
56 : . monitor - pointer to function (if this is NULL, it turns off monitoring)
57 : . mctx - [optional] context for private data for the
58 : monitor routine (use NULL if no context is desired)
59 : - monitordestroy - [optional] routine that frees monitor context (may be NULL),
60 : see PetscCtxDestroyFn for the calling sequence
61 :
62 : Calling sequence of monitor:
63 : $ PetscErrorCode monitor(MFN mfn,PetscInt its,PetscReal errest,void *mctx)
64 : + mfn - matrix function context obtained from MFNCreate()
65 : . its - iteration number
66 : . errest - error estimate
67 : - mctx - optional monitoring context, as set by MFNMonitorSet()
68 :
69 : Options Database Keys:
70 : + -mfn_monitor - print the error estimate
71 : . -mfn_monitor draw::draw_lg - sets line graph monitor for the error estimate
72 : - -mfn_monitor_cancel - cancels all monitors that have been hardwired into
73 : a code by calls to MFNMonitorSet(), but does not cancel those set via
74 : the options database.
75 :
76 : Notes:
77 : Several different monitoring routines may be set by calling
78 : MFNMonitorSet() multiple times; all will be called in the
79 : order in which they were set.
80 :
81 : Level: intermediate
82 :
83 : .seealso: MFNMonitorCancel()
84 : @*/
85 3 : PetscErrorCode MFNMonitorSet(MFN mfn,PetscErrorCode (*monitor)(MFN mfn,PetscInt its,PetscReal errest,void *mctx),void *mctx,PetscCtxDestroyFn *monitordestroy)
86 : {
87 3 : PetscFunctionBegin;
88 3 : PetscValidHeaderSpecific(mfn,MFN_CLASSID,1);
89 3 : PetscCheck(mfn->numbermonitors<MAXMFNMONITORS,PetscObjectComm((PetscObject)mfn),PETSC_ERR_ARG_OUTOFRANGE,"Too many MFN monitors set");
90 3 : mfn->monitor[mfn->numbermonitors] = monitor;
91 3 : mfn->monitorcontext[mfn->numbermonitors] = (void*)mctx;
92 3 : mfn->monitordestroy[mfn->numbermonitors++] = monitordestroy;
93 3 : PetscFunctionReturn(PETSC_SUCCESS);
94 : }
95 :
96 : /*@
97 : MFNMonitorCancel - Clears all monitors for an MFN object.
98 :
99 : Logically Collective
100 :
101 : Input Parameters:
102 : . mfn - matrix function context obtained from MFNCreate()
103 :
104 : Options Database Key:
105 : . -mfn_monitor_cancel - cancels all monitors that have been hardwired
106 : into a code by calls to MFNMonitorSet(),
107 : but does not cancel those set via the options database.
108 :
109 : Level: intermediate
110 :
111 : .seealso: MFNMonitorSet()
112 : @*/
113 18 : PetscErrorCode MFNMonitorCancel(MFN mfn)
114 : {
115 18 : PetscInt i;
116 :
117 18 : PetscFunctionBegin;
118 18 : PetscValidHeaderSpecific(mfn,MFN_CLASSID,1);
119 21 : for (i=0; i<mfn->numbermonitors; i++) {
120 3 : if (mfn->monitordestroy[i]) PetscCall((*mfn->monitordestroy[i])(&mfn->monitorcontext[i]));
121 : }
122 18 : mfn->numbermonitors = 0;
123 18 : PetscFunctionReturn(PETSC_SUCCESS);
124 : }
125 :
126 : /*@C
127 : MFNGetMonitorContext - Gets the monitor context, as set by
128 : MFNMonitorSet() for the FIRST monitor only.
129 :
130 : Not Collective
131 :
132 : Input Parameter:
133 : . mfn - matrix function context obtained from MFNCreate()
134 :
135 : Output Parameter:
136 : . ctx - monitor context
137 :
138 : Level: intermediate
139 :
140 : .seealso: MFNMonitorSet()
141 : @*/
142 0 : PetscErrorCode MFNGetMonitorContext(MFN mfn,void *ctx)
143 : {
144 0 : PetscFunctionBegin;
145 0 : PetscValidHeaderSpecific(mfn,MFN_CLASSID,1);
146 0 : *(void**)ctx = mfn->monitorcontext[0];
147 0 : PetscFunctionReturn(PETSC_SUCCESS);
148 : }
149 :
150 : /*@C
151 : MFNMonitorDefault - Print the error estimate of the current approximation at each
152 : iteration of the matrix function solver.
153 :
154 : Collective
155 :
156 : Input Parameters:
157 : + mfn - matrix function context
158 : . its - iteration number
159 : . errest - error estimate
160 : - vf - viewer and format for monitoring
161 :
162 : Options Database Key:
163 : . -mfn_monitor - activates MFNMonitorDefault()
164 :
165 : Level: intermediate
166 :
167 : .seealso: MFNMonitorSet()
168 : @*/
169 6 : PetscErrorCode MFNMonitorDefault(MFN mfn,PetscInt its,PetscReal errest,PetscViewerAndFormat *vf)
170 : {
171 6 : PetscViewer viewer = vf->viewer;
172 :
173 6 : PetscFunctionBegin;
174 6 : PetscValidHeaderSpecific(mfn,MFN_CLASSID,1);
175 6 : PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
176 6 : PetscCall(PetscViewerPushFormat(viewer,vf->format));
177 6 : PetscCall(PetscViewerASCIIAddTab(viewer,((PetscObject)mfn)->tablevel));
178 6 : if (its == 1 && ((PetscObject)mfn)->prefix) PetscCall(PetscViewerASCIIPrintf(viewer," Error estimates for %s solve.\n",((PetscObject)mfn)->prefix));
179 6 : PetscCall(PetscViewerASCIIPrintf(viewer,"%3" PetscInt_FMT " MFN Error estimate %14.12e\n",its,(double)errest));
180 6 : PetscCall(PetscViewerASCIISubtractTab(viewer,((PetscObject)mfn)->tablevel));
181 6 : PetscCall(PetscViewerPopFormat(viewer));
182 6 : PetscFunctionReturn(PETSC_SUCCESS);
183 : }
184 :
185 : /*@C
186 : MFNMonitorDefaultDrawLG - Plots the error estimate of the current approximation at each
187 : iteration of the matrix function solver.
188 :
189 : Collective
190 :
191 : Input Parameters:
192 : + mfn - matrix function context
193 : . its - iteration number
194 : . errest - error estimate
195 : - vf - viewer and format for monitoring
196 :
197 : Options Database Key:
198 : . -mfn_monitor draw::draw_lg - activates MFNMonitorDefaultDrawLG()
199 :
200 : Level: intermediate
201 :
202 : .seealso: MFNMonitorSet()
203 : @*/
204 0 : PetscErrorCode MFNMonitorDefaultDrawLG(MFN mfn,PetscInt its,PetscReal errest,PetscViewerAndFormat *vf)
205 : {
206 0 : PetscViewer viewer = vf->viewer;
207 0 : PetscDrawLG lg = vf->lg;
208 0 : PetscReal x,y;
209 :
210 0 : PetscFunctionBegin;
211 0 : PetscValidHeaderSpecific(mfn,MFN_CLASSID,1);
212 0 : PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4);
213 0 : PetscValidHeaderSpecific(lg,PETSC_DRAWLG_CLASSID,4);
214 0 : PetscCall(PetscViewerPushFormat(viewer,vf->format));
215 0 : if (its==1) {
216 0 : PetscCall(PetscDrawLGReset(lg));
217 0 : PetscCall(PetscDrawLGSetDimension(lg,1));
218 0 : PetscCall(PetscDrawLGSetLimits(lg,1,1.0,PetscLog10Real(mfn->tol)-2,0.0));
219 : }
220 0 : x = (PetscReal)its;
221 0 : if (errest > 0.0) y = PetscLog10Real(errest);
222 0 : else y = 0.0;
223 0 : PetscCall(PetscDrawLGAddPoint(lg,&x,&y));
224 0 : if (its <= 20 || !(its % 5) || mfn->reason) {
225 0 : PetscCall(PetscDrawLGDraw(lg));
226 0 : PetscCall(PetscDrawLGSave(lg));
227 : }
228 0 : PetscCall(PetscViewerPopFormat(viewer));
229 0 : PetscFunctionReturn(PETSC_SUCCESS);
230 : }
231 :
232 : /*@C
233 : MFNMonitorDefaultDrawLGCreate - Creates the plotter for the error estimate.
234 :
235 : Collective
236 :
237 : Input Parameters:
238 : + viewer - the viewer
239 : . format - the viewer format
240 : - ctx - an optional user context
241 :
242 : Output Parameter:
243 : . vf - the viewer and format context
244 :
245 : Level: intermediate
246 :
247 : .seealso: MFNMonitorSet()
248 : @*/
249 0 : PetscErrorCode MFNMonitorDefaultDrawLGCreate(PetscViewer viewer,PetscViewerFormat format,void *ctx,PetscViewerAndFormat **vf)
250 : {
251 0 : PetscFunctionBegin;
252 0 : PetscCall(PetscViewerAndFormatCreate(viewer,format,vf));
253 0 : (*vf)->data = ctx;
254 0 : PetscCall(MFNMonitorLGCreate(PetscObjectComm((PetscObject)viewer),NULL,"Error Estimate","Log Error Estimate",1,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,&(*vf)->lg));
255 0 : PetscFunctionReturn(PETSC_SUCCESS);
256 : }
|