Actual source code: mfnmon.c
slepc-3.15.2 2021-09-20
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-2021, 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: */
10: /*
11: MFN routines related to monitors
12: */
14: #include <slepc/private/mfnimpl.h>
15: #include <petscdraw.h>
17: 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: PetscDraw draw;
20: PetscDrawAxis axis;
21: PetscDrawLG lg;
25: PetscDrawCreate(comm,host,label,x,y,m,n,&draw);
26: PetscDrawSetFromOptions(draw);
27: PetscDrawLGCreate(draw,l,&lg);
28: if (names) { PetscDrawLGSetLegend(lg,names); }
29: PetscDrawLGSetFromOptions(lg);
30: PetscDrawLGGetAxis(lg,&axis);
31: PetscDrawAxisSetLabels(axis,"Convergence","Iteration",metric);
32: PetscDrawDestroy(&draw);
33: *lgctx = lg;
34: return(0);
35: }
37: /*
38: Runs the user provided monitor routines, if any.
39: */
40: PetscErrorCode MFNMonitor(MFN mfn,PetscInt it,PetscReal errest)
41: {
43: PetscInt i,n = mfn->numbermonitors;
46: for (i=0;i<n;i++) {
47: (*mfn->monitor[i])(mfn,it,errest,mfn->monitorcontext[i]);
48: }
49: return(0);
50: }
52: /*@C
53: MFNMonitorSet - Sets an ADDITIONAL function to be called at every
54: iteration to monitor convergence.
56: Logically Collective on mfn
58: Input Parameters:
59: + mfn - matrix function context obtained from MFNCreate()
60: . monitor - pointer to function (if this is NULL, it turns off monitoring)
61: . mctx - [optional] context for private data for the
62: monitor routine (use NULL if no context is desired)
63: - monitordestroy - [optional] routine that frees monitor context (may be NULL)
65: Calling Sequence of monitor:
66: $ monitor(MFN mfn,int its,PetscReal errest,void *mctx)
68: + mfn - matrix function context obtained from MFNCreate()
69: . its - iteration number
70: . errest - error estimate
71: - mctx - optional monitoring context, as set by MFNMonitorSet()
73: Options Database Keys:
74: + -mfn_monitor - print the error estimate
75: . -mfn_monitor draw::draw_lg - sets line graph monitor for the error estimate
76: - -mfn_monitor_cancel - cancels all monitors that have been hardwired into
77: a code by calls to MFNMonitorSet(), but does not cancel those set via
78: the options database.
80: Notes:
81: Several different monitoring routines may be set by calling
82: MFNMonitorSet() multiple times; all will be called in the
83: order in which they were set.
85: Level: intermediate
87: .seealso: MFNMonitorCancel()
88: @*/
89: PetscErrorCode MFNMonitorSet(MFN mfn,PetscErrorCode (*monitor)(MFN,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
90: {
93: if (mfn->numbermonitors >= MAXMFNMONITORS) SETERRQ(PetscObjectComm((PetscObject)mfn),PETSC_ERR_ARG_OUTOFRANGE,"Too many MFN monitors set");
94: mfn->monitor[mfn->numbermonitors] = monitor;
95: mfn->monitorcontext[mfn->numbermonitors] = (void*)mctx;
96: mfn->monitordestroy[mfn->numbermonitors++] = monitordestroy;
97: return(0);
98: }
100: /*@
101: MFNMonitorCancel - Clears all monitors for an MFN object.
103: Logically Collective on mfn
105: Input Parameters:
106: . mfn - matrix function context obtained from MFNCreate()
108: Options Database Key:
109: . -mfn_monitor_cancel - cancels all monitors that have been hardwired
110: into a code by calls to MFNMonitorSet(),
111: but does not cancel those set via the options database.
113: Level: intermediate
115: .seealso: MFNMonitorSet()
116: @*/
117: PetscErrorCode MFNMonitorCancel(MFN mfn)
118: {
120: PetscInt i;
124: for (i=0; i<mfn->numbermonitors; i++) {
125: if (mfn->monitordestroy[i]) {
126: (*mfn->monitordestroy[i])(&mfn->monitorcontext[i]);
127: }
128: }
129: mfn->numbermonitors = 0;
130: return(0);
131: }
133: /*@C
134: MFNGetMonitorContext - Gets the monitor context, as set by
135: MFNMonitorSet() for the FIRST monitor only.
137: Not Collective
139: Input Parameter:
140: . mfn - matrix function context obtained from MFNCreate()
142: Output Parameter:
143: . ctx - monitor context
145: Level: intermediate
147: .seealso: MFNMonitorSet()
148: @*/
149: PetscErrorCode MFNGetMonitorContext(MFN mfn,void **ctx)
150: {
153: *ctx = mfn->monitorcontext[0];
154: return(0);
155: }
157: /*@C
158: MFNMonitorDefault - Print the error estimate of the current approximation at each
159: iteration of the matrix function solver.
161: Collective on mfn
163: Input Parameters:
164: + mfn - matrix function context
165: . its - iteration number
166: . errest - error estimate
167: - vf - viewer and format for monitoring
169: Options Database Key:
170: . -mfn_monitor - activates MFNMonitorDefault()
172: Level: intermediate
174: .seealso: MFNMonitorSet()
175: @*/
176: PetscErrorCode MFNMonitorDefault(MFN mfn,PetscInt its,PetscReal errest,PetscViewerAndFormat *vf)
177: {
179: PetscViewer viewer;
184: viewer = vf->viewer;
186: PetscViewerPushFormat(viewer,vf->format);
187: PetscViewerASCIIAddTab(viewer,((PetscObject)mfn)->tablevel);
188: if (its == 1 && ((PetscObject)mfn)->prefix) {
189: PetscViewerASCIIPrintf(viewer," Error estimates for %s solve.\n",((PetscObject)mfn)->prefix);
190: }
191: PetscViewerASCIIPrintf(viewer,"%3D MFN Error estimate %14.12e\n",its,(double)errest);
192: PetscViewerASCIISubtractTab(viewer,((PetscObject)mfn)->tablevel);
193: PetscViewerPopFormat(viewer);
194: return(0);
195: }
197: /*@C
198: MFNMonitorDefaultDrawLG - Plots the error estimate of the current approximation at each
199: iteration of the matrix function solver.
201: Collective on mfn
203: Input Parameters:
204: + mfn - matrix function context
205: . its - iteration number
206: . errest - error estimate
207: - vf - viewer and format for monitoring
209: Options Database Key:
210: . -mfn_monitor draw::draw_lg - activates MFNMonitorDefaultDrawLG()
212: Level: intermediate
214: .seealso: MFNMonitorSet()
215: @*/
216: PetscErrorCode MFNMonitorDefaultDrawLG(MFN mfn,PetscInt its,PetscReal errest,PetscViewerAndFormat *vf)
217: {
219: PetscViewer viewer;
220: PetscDrawLG lg;
221: PetscReal x,y;
226: viewer = vf->viewer;
228: lg = vf->lg;
230: PetscViewerPushFormat(viewer,vf->format);
231: if (its==1) {
232: PetscDrawLGReset(lg);
233: PetscDrawLGSetDimension(lg,1);
234: PetscDrawLGSetLimits(lg,1,1.0,PetscLog10Real(mfn->tol)-2,0.0);
235: }
236: x = (PetscReal)its;
237: if (errest > 0.0) y = PetscLog10Real(errest);
238: else y = 0.0;
239: PetscDrawLGAddPoint(lg,&x,&y);
240: if (its <= 20 || !(its % 5) || mfn->reason) {
241: PetscDrawLGDraw(lg);
242: PetscDrawLGSave(lg);
243: }
244: PetscViewerPopFormat(viewer);
245: return(0);
246: }
248: /*@C
249: MFNMonitorDefaultDrawLGCreate - Creates the plotter for the error estimate.
251: Collective on viewer
253: Input Parameters:
254: + viewer - the viewer
255: . format - the viewer format
256: - ctx - an optional user context
258: Output Parameter:
259: . vf - the viewer and format context
261: Level: intermediate
263: .seealso: MFNMonitorSet()
264: @*/
265: PetscErrorCode MFNMonitorDefaultDrawLGCreate(PetscViewer viewer,PetscViewerFormat format,void *ctx,PetscViewerAndFormat **vf)
266: {
270: PetscViewerAndFormatCreate(viewer,format,vf);
271: (*vf)->data = ctx;
272: MFNMonitorLGCreate(PetscObjectComm((PetscObject)viewer),NULL,"Error Estimate","Log Error Estimate",1,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,&(*vf)->lg);
273: return(0);
274: }