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