Actual source code: lmemon.c
slepc-3.17.0 2022-03-31
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-, 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;
23: PetscDrawCreate(comm,host,label,x,y,m,n,&draw);
24: PetscDrawSetFromOptions(draw);
25: PetscDrawLGCreate(draw,l,&lg);
26: if (names) PetscDrawLGSetLegend(lg,names);
27: PetscDrawLGSetFromOptions(lg);
28: PetscDrawLGGetAxis(lg,&axis);
29: PetscDrawAxisSetLabels(axis,"Convergence","Iteration",metric);
30: PetscDrawDestroy(&draw);
31: *lgctx = lg;
32: PetscFunctionReturn(0);
33: }
35: /*
36: Runs the user provided monitor routines, if any.
37: */
38: PetscErrorCode LMEMonitor(LME lme,PetscInt it,PetscReal errest)
39: {
40: PetscInt i,n = lme->numbermonitors;
42: for (i=0;i<n;i++) (*lme->monitor[i])(lme,it,errest,lme->monitorcontext[i]);
43: PetscFunctionReturn(0);
44: }
46: /*@C
47: LMEMonitorSet - Sets an ADDITIONAL function to be called at every
48: iteration to monitor convergence.
50: Logically Collective on lme
52: Input Parameters:
53: + lme - linear matrix equation solver context obtained from LMECreate()
54: . monitor - pointer to function (if this is NULL, it turns off monitoring)
55: . mctx - [optional] context for private data for the
56: monitor routine (use NULL if no context is desired)
57: - monitordestroy - [optional] routine that frees monitor context (may be NULL)
59: Calling Sequence of monitor:
60: $ monitor(LME lme,int its,PetscReal errest,void *mctx)
62: + lme - linear matrix equation solver context obtained from LMECreate()
63: . its - iteration number
64: . errest - error estimate
65: - mctx - optional monitoring context, as set by LMEMonitorSet()
67: Options Database Keys:
68: + -lme_monitor - print the error estimate
69: . -lme_monitor draw::draw_lg - sets line graph monitor for the error estimate
70: - -lme_monitor_cancel - cancels all monitors that have been hardwired into
71: a code by calls to LMEMonitorSet(), but does not cancel those set via
72: the options database.
74: Notes:
75: Several different monitoring routines may be set by calling
76: LMEMonitorSet() multiple times; all will be called in the
77: order in which they were set.
79: Level: intermediate
81: .seealso: LMEMonitorCancel()
82: @*/
83: PetscErrorCode LMEMonitorSet(LME lme,PetscErrorCode (*monitor)(LME,PetscInt,PetscReal,void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
84: {
87: lme->monitor[lme->numbermonitors] = monitor;
88: lme->monitorcontext[lme->numbermonitors] = (void*)mctx;
89: lme->monitordestroy[lme->numbermonitors++] = monitordestroy;
90: PetscFunctionReturn(0);
91: }
93: /*@
94: LMEMonitorCancel - Clears all monitors for an LME object.
96: Logically Collective on lme
98: Input Parameters:
99: . lme - linear matrix equation solver context obtained from LMECreate()
101: Options Database Key:
102: . -lme_monitor_cancel - cancels all monitors that have been hardwired
103: into a code by calls to LMEMonitorSet(),
104: but does not cancel those set via the options database.
106: Level: intermediate
108: .seealso: LMEMonitorSet()
109: @*/
110: PetscErrorCode LMEMonitorCancel(LME lme)
111: {
112: PetscInt i;
115: for (i=0; i<lme->numbermonitors; i++) {
116: if (lme->monitordestroy[i]) (*lme->monitordestroy[i])(&lme->monitorcontext[i]);
117: }
118: lme->numbermonitors = 0;
119: PetscFunctionReturn(0);
120: }
122: /*@C
123: LMEGetMonitorContext - Gets the monitor context, as set by
124: LMEMonitorSet() for the FIRST monitor only.
126: Not Collective
128: Input Parameter:
129: . lme - linear matrix equation solver context obtained from LMECreate()
131: Output Parameter:
132: . ctx - monitor context
134: Level: intermediate
136: .seealso: LMEMonitorSet()
137: @*/
138: PetscErrorCode LMEGetMonitorContext(LME lme,void *ctx)
139: {
141: *(void**)ctx = lme->monitorcontext[0];
142: PetscFunctionReturn(0);
143: }
145: /*@C
146: LMEMonitorDefault - Print the error estimate of the current approximation at each
147: iteration of the linear matrix equation solver.
149: Collective on lme
151: Input Parameters:
152: + lme - linear matrix equation solver context
153: . its - iteration number
154: . errest - error estimate
155: - vf - viewer and format for monitoring
157: Options Database Key:
158: . -lme_monitor - activates LMEMonitorDefault()
160: Level: intermediate
162: .seealso: LMEMonitorSet()
163: @*/
164: PetscErrorCode LMEMonitorDefault(LME lme,PetscInt its,PetscReal errest,PetscViewerAndFormat *vf)
165: {
166: PetscViewer viewer = vf->viewer;
170: PetscViewerPushFormat(viewer,vf->format);
171: PetscViewerASCIIAddTab(viewer,((PetscObject)lme)->tablevel);
172: if (its == 1 && ((PetscObject)lme)->prefix) PetscViewerASCIIPrintf(viewer," Error estimates for %s solve.\n",((PetscObject)lme)->prefix);
173: PetscViewerASCIIPrintf(viewer,"%3" PetscInt_FMT " LME Error estimate %14.12e\n",its,(double)errest);
174: PetscViewerASCIISubtractTab(viewer,((PetscObject)lme)->tablevel);
175: PetscViewerPopFormat(viewer);
176: PetscFunctionReturn(0);
177: }
179: /*@C
180: LMEMonitorDefaultDrawLG - Plots the error estimate of the current approximation at each
181: iteration of the linear matrix equation solver.
183: Collective on lme
185: Input Parameters:
186: + lme - linear matrix equation solver context
187: . its - iteration number
188: . errest - error estimate
189: - vf - viewer and format for monitoring
191: Options Database Key:
192: . -lme_monitor draw::draw_lg - activates LMEMonitorDefaultDrawLG()
194: Level: intermediate
196: .seealso: LMEMonitorSet()
197: @*/
198: PetscErrorCode LMEMonitorDefaultDrawLG(LME lme,PetscInt its,PetscReal errest,PetscViewerAndFormat *vf)
199: {
200: PetscViewer viewer = vf->viewer;
201: PetscDrawLG lg = vf->lg;
202: PetscReal x,y;
207: PetscViewerPushFormat(viewer,vf->format);
208: if (its==1) {
209: PetscDrawLGReset(lg);
210: PetscDrawLGSetDimension(lg,1);
211: PetscDrawLGSetLimits(lg,1,1.0,PetscLog10Real(lme->tol)-2,0.0);
212: }
213: x = (PetscReal)its;
214: if (errest > 0.0) y = PetscLog10Real(errest);
215: else y = 0.0;
216: PetscDrawLGAddPoint(lg,&x,&y);
217: if (its <= 20 || !(its % 5) || lme->reason) {
218: PetscDrawLGDraw(lg);
219: PetscDrawLGSave(lg);
220: }
221: PetscViewerPopFormat(viewer);
222: PetscFunctionReturn(0);
223: }
225: /*@C
226: LMEMonitorDefaultDrawLGCreate - Creates the plotter for the error estimate.
228: Collective on viewer
230: Input Parameters:
231: + viewer - the viewer
232: . format - the viewer format
233: - ctx - an optional user context
235: Output Parameter:
236: . vf - the viewer and format context
238: Level: intermediate
240: .seealso: LMEMonitorSet()
241: @*/
242: PetscErrorCode LMEMonitorDefaultDrawLGCreate(PetscViewer viewer,PetscViewerFormat format,void *ctx,PetscViewerAndFormat **vf)
243: {
244: PetscViewerAndFormatCreate(viewer,format,vf);
245: (*vf)->data = ctx;
246: LMEMonitorLGCreate(PetscObjectComm((PetscObject)viewer),NULL,"Error Estimate","Log Error Estimate",1,NULL,PETSC_DECIDE,PETSC_DECIDE,400,300,&(*vf)->lg);
247: PetscFunctionReturn(0);
248: }