xref: /petsc/src/ksp/ksp/interface/xmon.c (revision be1d678a52e6eff2808b2fa31ae986cdbf03c9fe)
1 
2 #include "src/ksp/ksp/kspimpl.h"              /*I  "petscksp.h"   I*/
3 
4 #undef __FUNCT__
5 #define __FUNCT__ "KSPLGMonitorCreate"
6 /*@C
7    KSPLGMonitorCreate - Creates a line graph context for use with
8    KSP to monitor convergence of preconditioned residual norms.
9 
10    Collective on KSP
11 
12    Input Parameters:
13 +  host - the X display to open, or null for the local machine
14 .  label - the title to put in the title bar
15 .  x, y - the screen coordinates of the upper left coordinate of
16           the window
17 -  m, n - the screen width and height in pixels
18 
19    Output Parameter:
20 .  draw - the drawing context
21 
22    Options Database Key:
23 .  -ksp_xmonitor - Sets line graph monitor
24 
25    Notes:
26    Use KSPLGMonitorDestroy() to destroy this line graph; do not use PetscDrawLGDestroy().
27 
28    Level: intermediate
29 
30 .keywords: KSP, monitor, line graph, residual, create
31 
32 .seealso: KSPLGMonitorDestroy(), KSPSetMonitor(), KSPLGTrueMonitorCreate()
33 @*/
34 PetscErrorCode KSPLGMonitorCreate(const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
35 {
36   PetscDraw      win;
37   PetscErrorCode ierr;
38 
39   PetscFunctionBegin;
40   ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr);
41   ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr);
42   ierr = PetscDrawLGCreate(win,1,draw);CHKERRQ(ierr);
43   ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr);
44   PetscFunctionReturn(0);
45 }
46 
47 #undef __FUNCT__
48 #define __FUNCT__ "KSPLGMonitor"
49 PetscErrorCode KSPLGMonitor(KSP ksp,PetscInt n,PetscReal rnorm,void *monctx)
50 {
51   PetscDrawLG    lg = (PetscDrawLG) monctx;
52   PetscErrorCode ierr;
53   PetscReal      x,y;
54 
55   PetscFunctionBegin;
56   if (!monctx) {
57     MPI_Comm    comm;
58     PetscViewer viewer;
59 
60     ierr   = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr);
61     viewer = PETSC_VIEWER_DRAW_(comm);
62     ierr   = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
63   }
64 
65   if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
66   x = (PetscReal) n;
67   if (rnorm > 0.0) y = log10(rnorm); else y = -15.0;
68   ierr = PetscDrawLGAddPoint(lg,&x,&y);CHKERRQ(ierr);
69   if (n < 20 || !(n % 5)) {
70     ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
71   }
72   PetscFunctionReturn(0);
73 }
74 
75 #undef __FUNCT__
76 #define __FUNCT__ "KSPLGMonitorDestroy"
77 /*@C
78    KSPLGMonitorDestroy - Destroys a line graph context that was created
79    with KSPLGMonitorCreate().
80 
81    Collective on KSP
82 
83    Input Parameter:
84 .  draw - the drawing context
85 
86    Level: intermediate
87 
88 .keywords: KSP, monitor, line graph, destroy
89 
90 .seealso: KSPLGMonitorCreate(), KSPLGTrueMonitorDestroy(), KSPSetMonitor()
91 @*/
92 PetscErrorCode KSPLGMonitorDestroy(PetscDrawLG drawlg)
93 {
94   PetscDraw      draw;
95   PetscErrorCode ierr;
96 
97   PetscFunctionBegin;
98   ierr = PetscDrawLGGetDraw(drawlg,&draw);CHKERRQ(ierr);
99   if (draw) { ierr = PetscDrawDestroy(draw);CHKERRQ(ierr);}
100   ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr);
101   PetscFunctionReturn(0);
102 }
103 
104 #undef __FUNCT__
105 #define __FUNCT__ "KSPLGTrueMonitorCreate"
106 /*@C
107    KSPLGTrueMonitorCreate - Creates a line graph context for use with
108    KSP to monitor convergence of true residual norms (as opposed to
109    preconditioned residual norms).
110 
111    Collective on KSP
112 
113    Input Parameters:
114 +  host - the X display to open, or null for the local machine
115 .  label - the title to put in the title bar
116 .  x, y - the screen coordinates of the upper left coordinate of
117           the window
118 -  m, n - the screen width and height in pixels
119 
120    Output Parameter:
121 .  draw - the drawing context
122 
123    Options Database Key:
124 .  -ksp_xtruemonitor - Sets true line graph monitor
125 
126    Notes:
127    Use KSPLGTrueMonitorDestroy() to destroy this line graph, not
128    PetscDrawLGDestroy().
129 
130    Level: intermediate
131 
132 .keywords: KSP, monitor, line graph, residual, create, true
133 
134 .seealso: KSPLGMonitorDestroy(), KSPSetMonitor(), KSPDefaultMonitor()
135 @*/
136 PetscErrorCode KSPLGTrueMonitorCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscDrawLG *draw)
137 {
138   PetscDraw      win;
139   PetscErrorCode ierr;
140   PetscMPIInt    rank;
141 
142   PetscFunctionBegin;
143   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
144   if (rank) { *draw = 0; PetscFunctionReturn(0);}
145 
146   ierr = PetscDrawCreate(PETSC_COMM_SELF,host,label,x,y,m,n,&win);CHKERRQ(ierr);
147   ierr = PetscDrawSetType(win,PETSC_DRAW_X);CHKERRQ(ierr);
148   ierr = PetscDrawLGCreate(win,2,draw);CHKERRQ(ierr);
149   ierr = PetscLogObjectParent(*draw,win);CHKERRQ(ierr);
150   PetscFunctionReturn(0);
151 }
152 
153 #undef __FUNCT__
154 #define __FUNCT__ "KSPLGTrueMonitor"
155 PetscErrorCode KSPLGTrueMonitor(KSP ksp,PetscInt n,PetscReal rnorm,void *monctx)
156 {
157   PetscDrawLG    lg = (PetscDrawLG) monctx;
158   PetscReal      x[2],y[2],scnorm;
159   PetscErrorCode ierr;
160   PetscMPIInt    rank;
161   Vec            resid,work;
162 
163   PetscFunctionBegin;
164   if (!monctx) {
165     MPI_Comm    comm;
166     PetscViewer viewer;
167 
168     ierr   = PetscObjectGetComm((PetscObject)ksp,&comm);CHKERRQ(ierr);
169     viewer = PETSC_VIEWER_DRAW_(comm);
170     ierr   = PetscViewerDrawGetDrawLG(viewer,0,&lg);CHKERRQ(ierr);
171   }
172 
173   ierr = MPI_Comm_rank(ksp->comm,&rank);CHKERRQ(ierr);
174   if (!rank) {
175     if (!n) {ierr = PetscDrawLGReset(lg);CHKERRQ(ierr);}
176     x[0] = x[1] = (PetscReal) n;
177     if (rnorm > 0.0) y[0] = log10(rnorm); else y[0] = -15.0;
178   }
179 
180   ierr = VecDuplicate(ksp->vec_rhs,&work);CHKERRQ(ierr);
181   ierr = KSPBuildResidual(ksp,0,work,&resid);CHKERRQ(ierr);
182   ierr = VecNorm(resid,NORM_2,&scnorm);CHKERRQ(ierr);
183   ierr = VecDestroy(work);CHKERRQ(ierr);
184 
185   if (!rank) {
186     if (scnorm > 0.0) y[1] = log10(scnorm); else y[1] = -15.0;
187     ierr = PetscDrawLGAddPoint(lg,x,y);CHKERRQ(ierr);
188     if (n <= 20 || (n % 3)) {
189       ierr = PetscDrawLGDraw(lg);CHKERRQ(ierr);
190     }
191   }
192   PetscFunctionReturn(0);
193 }
194 
195 #undef __FUNCT__
196 #define __FUNCT__ "KSPLGTrueMonitorDestroy"
197 /*@C
198    KSPLGTrueMonitorDestroy - Destroys a line graph context that was created
199    with KSPLGTrueMonitorCreate().
200 
201    Collective on KSP
202 
203    Input Parameter:
204 .  draw - the drawing context
205 
206    Level: intermediate
207 
208 .keywords: KSP, monitor, line graph, destroy, true
209 
210 .seealso: KSPLGTrueMonitorCreate(), KSPSetMonitor()
211 @*/
212 PetscErrorCode KSPLGTrueMonitorDestroy(PetscDrawLG drawlg)
213 {
214   PetscErrorCode ierr;
215   PetscDraw      draw;
216 
217   PetscFunctionBegin;
218   ierr = PetscDrawLGGetDraw(drawlg,&draw);CHKERRQ(ierr);
219   ierr = PetscDrawDestroy(draw);CHKERRQ(ierr);
220   ierr = PetscDrawLGDestroy(drawlg);CHKERRQ(ierr);
221   PetscFunctionReturn(0);
222 }
223 
224 
225