Actual source code: lmemon.c
slepc-3.16.3 2022-04-11
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: LME routines related to monitors
12: */
14: #include <slepc/private/lmeimpl.h>
15: #include <petscdraw.h>
17: PetscErrorCode LMEMonitorLGCreate(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 LMEMonitor(LME lme,PetscInt it,PetscReal errest)
41: {
43: PetscInt i,n = lme->numbermonitors;
46: for (i=0;i<n;i++) {
47: (*lme->monitor[i])(lme,it,errest,lme->monitorcontext[i]);
48: }
49: return(0);
50: }
52: /*@C
53: LMEMonitorSet - Sets an ADDITIONAL function to be called at every
54: iteration to monitor convergence.
56: Logically Collective on lme
58: Input Parameters:
59: + lme - linear matrix equation solver context obtained from LMECreate()
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(LME lme,int its,PetscReal errest,void *mctx)
68: + lme - linear matrix equation solver context obtained from LMECreate()
69: . its - iteration number
70: . errest - error estimate
71: - mctx - optional monitoring context, as set by LMEMonitorSet()
73: Options Database Keys:
74: + -lme_monitor - print the error estimate
75: . -lme_monitor draw::draw_lg - sets line graph monitor for the error estimate
76: - -lme_monitor_cancel - cancels all monitors that have been hardwired into
77: a code by calls to LMEMonitorSet(), but does not cancel those set via
78: the options database.
80: Notes:
81: Several different monitoring routines may be set by calling
82: LMEMonitorSet() multiple times; all will be called in the
83: order in which they were set.
85: Level: intermediate
87: .seealso: LMEMonitorCancel()
88: @*/
89: PetscErrorCode LMEMonitorSet(LME lme,PetscErrorCode (*monitor)(LME,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
90: {
93: if (lme->numbermonitors >= MAXLMEMONITORS) SETERRQ(PetscObjectComm((PetscObject)lme),PETSC_ERR_ARG_OUTOFRANGE,"Too many LME monitors set");
94: lme->monitor[lme->numbermonitors] = monitor;
95: lme->monitorcontext[lme->numbermonitors] = (void*)mctx;
96: lme->monitordestroy[lme->numbermonitors++] = monitordestroy;
97: return(0);
98: }
100: /*@
101: LMEMonitorCancel - Clears all monitors for an LME object.
103: Logically Collective on lme
105: Input Parameters:
106: . lme - linear matrix equation solver context obtained from LMECreate()
108: Options Database Key:
109: . -lme_monitor_cancel - cancels all monitors that have been hardwired
110: into a code by calls to LMEMonitorSet(),
111: but does not cancel those set via the options database.
113: Level: intermediate
115: .seealso: LMEMonitorSet()
116: @*/
117: PetscErrorCode LMEMonitorCancel(LME lme)
118: {
120: PetscInt i;
124: for (i=0; i<lme->numbermonitors; i++) {
125: if (lme->monitordestroy[i]) {
126: (*lme->monitordestroy[i])(&lme->monitorcontext[i]);
127: }
128: }
129: lme->numbermonitors = 0;
130: return(0);
131: }
133: /*@C
134: LMEGetMonitorContext - Gets the monitor context, as set by
135: LMEMonitorSet() for the FIRST monitor only.
137: Not Collective
139: Input Parameter:
140: . lme - linear matrix equation solver context obtained from LMECreate()
142: Output Parameter:
143: . ctx - monitor context
145: Level: intermediate
147: .seealso: LMEMonitorSet()
148: @*/
149: PetscErrorCode LMEGetMonitorContext(LME lme,void *ctx)
150: {
153: *(void**)ctx = lme->monitorcontext[0];
154: return(0);
155: }
157: /*@C
158: LMEMonitorDefault - Print the error estimate of the current approximation at each
159: iteration of the linear matrix equation solver.
161: Collective on lme
163: Input Parameters:
164: + lme - linear matrix equation solver context
165: . its - iteration number
166: . errest - error estimate
167: - vf - viewer and format for monitoring
169: Options Database Key:
170: . -lme_monitor - activates LMEMonitorDefault()
172: Level: intermediate
174: .seealso: LMEMonitorSet()
175: @*/
176: PetscErrorCode LMEMonitorDefault(LME lme,PetscInt its,PetscReal errest,PetscViewerAndFormat *vf)
177: {
179: PetscViewer viewer = vf->viewer;
184: PetscViewerPushFormat(viewer,vf->format);
185: PetscViewerASCIIAddTab(viewer,((PetscObject)lme)->tablevel);
186: if (its == 1 && ((PetscObject)lme)->prefix) {
187: PetscViewerASCIIPrintf(viewer," Error estimates for %s solve.\n",((PetscObject)lme)->prefix);
188: }
189: PetscViewerASCIIPrintf(viewer,"%3D LME Error estimate %14.12e\n",its,(double)errest);
190: PetscViewerASCIISubtractTab(viewer,((PetscObject)lme)->tablevel);
191: PetscViewerPopFormat(viewer);
192: return(0);
193: }
195: /*@C
196: LMEMonitorDefaultDrawLG - Plots the error estimate of the current approximation at each
197: iteration of the linear matrix equation solver.
199: Collective on lme
201: Input Parameters:
202: + lme - linear matrix equation solver context
203: . its - iteration number
204: . errest - error estimate
205: - vf - viewer and format for monitoring
207: Options Database Key:
208: . -lme_monitor draw::draw_lg - activates LMEMonitorDefaultDrawLG()
210: Level: intermediate
212: .seealso: LMEMonitorSet()
213: @*/
214: PetscErrorCode LMEMonitorDefaultDrawLG(LME lme,PetscInt its,PetscReal errest,PetscViewerAndFormat *vf)
215: {
217: PetscViewer viewer = vf->viewer;
218: PetscDrawLG lg = vf->lg;
219: PetscReal x,y;
225: PetscViewerPushFormat(viewer,vf->format);
226: if (its==1) {
227: PetscDrawLGReset(lg);
228: PetscDrawLGSetDimension(lg,1);
229: PetscDrawLGSetLimits(lg,1,1.0,PetscLog10Real(lme->tol)-2,0.0);
230: }
231: x = (PetscReal)its;
232: if (errest > 0.0) y = PetscLog10Real(errest);
233: else y = 0.0;
234: PetscDrawLGAddPoint(lg,&x,&y);
235: if (its <= 20 || !(its % 5) || lme->reason) {
236: PetscDrawLGDraw(lg);
237: PetscDrawLGSave(lg);
238: }
239: PetscViewerPopFormat(viewer);
240: return(0);
241: }
243: /*@C
244: LMEMonitorDefaultDrawLGCreate - Creates the plotter for the error estimate.
246: Collective on viewer
248: Input Parameters:
249: + viewer - the viewer
250: . format - the viewer format
251: - ctx - an optional user context
253: Output Parameter:
254: . vf - the viewer and format context
256: Level: intermediate
258: .seealso: LMEMonitorSet()
259: @*/
260: PetscErrorCode LMEMonitorDefaultDrawLGCreate(PetscViewer viewer,PetscViewerFormat format,void *ctx,PetscViewerAndFormat **vf)
261: {
265: PetscViewerAndFormatCreate(viewer,format,vf);
266: (*vf)->data = ctx;
267: LMEMonitorLGCreate(PetscObjectComm((PetscObject)viewer),NULL,"Error Estimate","Log Error Estimate",1,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,&(*vf)->lg);
268: return(0);
269: }