xref: /petsc/src/snes/tutorials/ex2.c (revision 5f80ce2ab25dff0f4601e710601cbbcecf323266)
1c4762a1bSJed Brown 
2c4762a1bSJed Brown static char help[] = "Newton method to solve u'' + u^{2} = f, sequentially.\n\
3c4762a1bSJed Brown This example employs a user-defined monitoring routine.\n\n";
4c4762a1bSJed Brown 
5c4762a1bSJed Brown /*T
6c4762a1bSJed Brown    Concepts: SNES^basic uniprocessor example
7c4762a1bSJed Brown    Concepts: SNES^setting a user-defined monitoring routine
8c4762a1bSJed Brown    Processors: 1
9c4762a1bSJed Brown T*/
10c4762a1bSJed Brown 
11c4762a1bSJed Brown /*
12c4762a1bSJed Brown    Include "petscdraw.h" so that we can use PETSc drawing routines.
13c4762a1bSJed Brown    Include "petscsnes.h" so that we can use SNES solvers.  Note that this
14c4762a1bSJed Brown    file automatically includes:
15c4762a1bSJed Brown      petscsys.h       - base PETSc routines   petscvec.h - vectors
16c4762a1bSJed Brown      petscmat.h - matrices
17c4762a1bSJed Brown      petscis.h     - index sets            petscksp.h - Krylov subspace methods
18c4762a1bSJed Brown      petscviewer.h - viewers               petscpc.h  - preconditioners
19c4762a1bSJed Brown      petscksp.h   - linear solvers
20c4762a1bSJed Brown */
21c4762a1bSJed Brown 
22c4762a1bSJed Brown #include <petscsnes.h>
23c4762a1bSJed Brown 
24c4762a1bSJed Brown /*
25c4762a1bSJed Brown    User-defined routines
26c4762a1bSJed Brown */
27c4762a1bSJed Brown extern PetscErrorCode FormJacobian(SNES,Vec,Mat,Mat,void*);
28c4762a1bSJed Brown extern PetscErrorCode FormFunction(SNES,Vec,Vec,void*);
29c4762a1bSJed Brown extern PetscErrorCode FormInitialGuess(Vec);
30c4762a1bSJed Brown extern PetscErrorCode Monitor(SNES,PetscInt,PetscReal,void*);
31c4762a1bSJed Brown 
32c4762a1bSJed Brown /*
33c4762a1bSJed Brown    User-defined context for monitoring
34c4762a1bSJed Brown */
35c4762a1bSJed Brown typedef struct {
36c4762a1bSJed Brown   PetscViewer viewer;
37c4762a1bSJed Brown } MonitorCtx;
38c4762a1bSJed Brown 
39c4762a1bSJed Brown int main(int argc,char **argv)
40c4762a1bSJed Brown {
41c4762a1bSJed Brown   SNES           snes;                   /* SNES context */
42c4762a1bSJed Brown   Vec            x,r,F,U;             /* vectors */
43c4762a1bSJed Brown   Mat            J;                      /* Jacobian matrix */
44c4762a1bSJed Brown   MonitorCtx     monP;                   /* monitoring context */
45c4762a1bSJed Brown   PetscErrorCode ierr;
46c4762a1bSJed Brown   PetscInt       its,n = 5,i,maxit,maxf;
47c4762a1bSJed Brown   PetscMPIInt    size;
48c4762a1bSJed Brown   PetscScalar    h,xp,v,none = -1.0;
49c4762a1bSJed Brown   PetscReal      abstol,rtol,stol,norm;
50c4762a1bSJed Brown 
51c4762a1bSJed Brown   ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
52*5f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
532c71b3e2SJacob Faibussowitsch   PetscCheckFalse(size != 1,PETSC_COMM_SELF,PETSC_ERR_SUP,"This is a uniprocessor example only!");
54*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
55c4762a1bSJed Brown   h    = 1.0/(n-1);
56c4762a1bSJed Brown 
57c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
58c4762a1bSJed Brown      Create nonlinear solver context
59c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
60c4762a1bSJed Brown 
61*5f80ce2aSJacob Faibussowitsch   CHKERRQ(SNESCreate(PETSC_COMM_WORLD,&snes));
62c4762a1bSJed Brown 
63c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
64c4762a1bSJed Brown      Create vector data structures; set function evaluation routine
65c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
66c4762a1bSJed Brown   /*
67c4762a1bSJed Brown      Note that we form 1 vector from scratch and then duplicate as needed.
68c4762a1bSJed Brown   */
69*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecCreate(PETSC_COMM_WORLD,&x));
70*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecSetSizes(x,PETSC_DECIDE,n));
71*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecSetFromOptions(x));
72*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDuplicate(x,&r));
73*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDuplicate(x,&F));
74*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDuplicate(x,&U));
75c4762a1bSJed Brown 
76c4762a1bSJed Brown   /*
77c4762a1bSJed Brown      Set function evaluation routine and vector
78c4762a1bSJed Brown   */
79*5f80ce2aSJacob Faibussowitsch   CHKERRQ(SNESSetFunction(snes,r,FormFunction,(void*)F));
80c4762a1bSJed Brown 
81c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
82c4762a1bSJed Brown      Create matrix data structure; set Jacobian evaluation routine
83c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
84c4762a1bSJed Brown 
85*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatCreate(PETSC_COMM_WORLD,&J));
86*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatSetSizes(J,PETSC_DECIDE,PETSC_DECIDE,n,n));
87*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatSetFromOptions(J));
88*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatSeqAIJSetPreallocation(J,3,NULL));
89c4762a1bSJed Brown 
90c4762a1bSJed Brown   /*
91c4762a1bSJed Brown      Set Jacobian matrix data structure and default Jacobian evaluation
92c4762a1bSJed Brown      routine. User can override with:
93c4762a1bSJed Brown      -snes_fd : default finite differencing approximation of Jacobian
94c4762a1bSJed Brown      -snes_mf : matrix-free Newton-Krylov method with no preconditioning
95c4762a1bSJed Brown                 (unless user explicitly sets preconditioner)
96c4762a1bSJed Brown      -snes_mf_operator : form preconditioning matrix as set by the user,
97c4762a1bSJed Brown                          but use matrix-free approx for Jacobian-vector
98c4762a1bSJed Brown                          products within Newton-Krylov method
99c4762a1bSJed Brown   */
100c4762a1bSJed Brown 
101*5f80ce2aSJacob Faibussowitsch   CHKERRQ(SNESSetJacobian(snes,J,J,FormJacobian,NULL));
102c4762a1bSJed Brown 
103c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
104c4762a1bSJed Brown      Customize nonlinear solver; set runtime options
105c4762a1bSJed Brown    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
106c4762a1bSJed Brown 
107c4762a1bSJed Brown   /*
108c4762a1bSJed Brown      Set an optional user-defined monitoring routine
109c4762a1bSJed Brown   */
110*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerDrawOpen(PETSC_COMM_WORLD,0,0,0,0,400,400,&monP.viewer));
111*5f80ce2aSJacob Faibussowitsch   CHKERRQ(SNESMonitorSet(snes,Monitor,&monP,0));
112c4762a1bSJed Brown 
113c4762a1bSJed Brown   /*
114c4762a1bSJed Brown      Set names for some vectors to facilitate monitoring (optional)
115c4762a1bSJed Brown   */
116*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject)x,"Approximate Solution"));
117*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscObjectSetName((PetscObject)U,"Exact Solution"));
118c4762a1bSJed Brown 
119c4762a1bSJed Brown   /*
120c4762a1bSJed Brown      Set SNES/KSP/KSP/PC runtime options, e.g.,
121c4762a1bSJed Brown          -snes_view -snes_monitor -ksp_type <ksp> -pc_type <pc>
122c4762a1bSJed Brown   */
123*5f80ce2aSJacob Faibussowitsch   CHKERRQ(SNESSetFromOptions(snes));
124c4762a1bSJed Brown 
125c4762a1bSJed Brown   /*
126c4762a1bSJed Brown      Print parameters used for convergence testing (optional) ... just
127c4762a1bSJed Brown      to demonstrate this routine; this information is also printed with
128c4762a1bSJed Brown      the option -snes_view
129c4762a1bSJed Brown   */
130*5f80ce2aSJacob Faibussowitsch   CHKERRQ(SNESGetTolerances(snes,&abstol,&rtol,&stol,&maxit,&maxf));
131*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"atol=%g, rtol=%g, stol=%g, maxit=%D, maxf=%D\n",(double)abstol,(double)rtol,(double)stol,maxit,maxf));
132c4762a1bSJed Brown 
133c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
134c4762a1bSJed Brown      Initialize application:
135c4762a1bSJed Brown      Store right-hand-side of PDE and exact solution
136c4762a1bSJed Brown    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
137c4762a1bSJed Brown 
138c4762a1bSJed Brown   xp = 0.0;
139c4762a1bSJed Brown   for (i=0; i<n; i++) {
140c4762a1bSJed Brown     v    = 6.0*xp + PetscPowScalar(xp+1.e-12,6.0); /* +1.e-12 is to prevent 0^6 */
141*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecSetValues(F,1,&i,&v,INSERT_VALUES));
142c4762a1bSJed Brown     v    = xp*xp*xp;
143*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecSetValues(U,1,&i,&v,INSERT_VALUES));
144c4762a1bSJed Brown     xp  += h;
145c4762a1bSJed Brown   }
146c4762a1bSJed Brown 
147c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
148c4762a1bSJed Brown      Evaluate initial guess; then solve nonlinear system
149c4762a1bSJed Brown    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
150c4762a1bSJed Brown   /*
151c4762a1bSJed Brown      Note: The user should initialize the vector, x, with the initial guess
152c4762a1bSJed Brown      for the nonlinear solver prior to calling SNESSolve().  In particular,
153c4762a1bSJed Brown      to employ an initial guess of zero, the user should explicitly set
154c4762a1bSJed Brown      this vector to zero by calling VecSet().
155c4762a1bSJed Brown   */
156*5f80ce2aSJacob Faibussowitsch   CHKERRQ(FormInitialGuess(x));
157*5f80ce2aSJacob Faibussowitsch   CHKERRQ(SNESSolve(snes,NULL,x));
158*5f80ce2aSJacob Faibussowitsch   CHKERRQ(SNESGetIterationNumber(snes,&its));
159*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"number of SNES iterations = %D\n\n",its));
160c4762a1bSJed Brown 
161c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
162c4762a1bSJed Brown      Check solution and clean up
163c4762a1bSJed Brown    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
164c4762a1bSJed Brown 
165c4762a1bSJed Brown   /*
166c4762a1bSJed Brown      Check the error
167c4762a1bSJed Brown   */
168*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecAXPY(x,none,U));
169*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecNorm(x,NORM_2,&norm));
170*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"Norm of error %g, Iterations %D\n",(double)norm,its));
171c4762a1bSJed Brown 
172c4762a1bSJed Brown   /*
173c4762a1bSJed Brown      Free work space.  All PETSc objects should be destroyed when they
174c4762a1bSJed Brown      are no longer needed.
175c4762a1bSJed Brown   */
176*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&x));  CHKERRQ(VecDestroy(&r));
177*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&U));  CHKERRQ(VecDestroy(&F));
178*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatDestroy(&J));  CHKERRQ(SNESDestroy(&snes));
179*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerDestroy(&monP.viewer));
180c4762a1bSJed Brown   ierr = PetscFinalize();
181c4762a1bSJed Brown   return ierr;
182c4762a1bSJed Brown }
183c4762a1bSJed Brown /* ------------------------------------------------------------------- */
184c4762a1bSJed Brown /*
185c4762a1bSJed Brown    FormInitialGuess - Computes initial guess.
186c4762a1bSJed Brown 
187c4762a1bSJed Brown    Input/Output Parameter:
188c4762a1bSJed Brown .  x - the solution vector
189c4762a1bSJed Brown */
190c4762a1bSJed Brown PetscErrorCode FormInitialGuess(Vec x)
191c4762a1bSJed Brown {
192c4762a1bSJed Brown   PetscScalar    pfive = .50;
193*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecSet(x,pfive));
194c4762a1bSJed Brown   return 0;
195c4762a1bSJed Brown }
196c4762a1bSJed Brown /* ------------------------------------------------------------------- */
197c4762a1bSJed Brown /*
198c4762a1bSJed Brown    FormFunction - Evaluates nonlinear function, F(x).
199c4762a1bSJed Brown 
200c4762a1bSJed Brown    Input Parameters:
201c4762a1bSJed Brown .  snes - the SNES context
202c4762a1bSJed Brown .  x - input vector
203c4762a1bSJed Brown .  ctx - optional user-defined context, as set by SNESSetFunction()
204c4762a1bSJed Brown 
205c4762a1bSJed Brown    Output Parameter:
206c4762a1bSJed Brown .  f - function vector
207c4762a1bSJed Brown 
208c4762a1bSJed Brown    Note:
209c4762a1bSJed Brown    The user-defined context can contain any application-specific data
210c4762a1bSJed Brown    needed for the function evaluation (such as various parameters, work
211c4762a1bSJed Brown    vectors, and grid information).  In this program the context is just
212c4762a1bSJed Brown    a vector containing the right-hand-side of the discretized PDE.
213c4762a1bSJed Brown  */
214c4762a1bSJed Brown 
215c4762a1bSJed Brown PetscErrorCode FormFunction(SNES snes,Vec x,Vec f,void *ctx)
216c4762a1bSJed Brown {
217c4762a1bSJed Brown   Vec               g = (Vec)ctx;
218c4762a1bSJed Brown   const PetscScalar *xx,*gg;
219c4762a1bSJed Brown   PetscScalar       *ff,d;
220c4762a1bSJed Brown   PetscInt          i,n;
221c4762a1bSJed Brown 
222c4762a1bSJed Brown   /*
223c4762a1bSJed Brown      Get pointers to vector data.
224c4762a1bSJed Brown        - For default PETSc vectors, VecGetArray() returns a pointer to
225c4762a1bSJed Brown          the data array.  Otherwise, the routine is implementation dependent.
226c4762a1bSJed Brown        - You MUST call VecRestoreArray() when you no longer need access to
227c4762a1bSJed Brown          the array.
228c4762a1bSJed Brown   */
229*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArrayRead(x,&xx));
230*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArray(f,&ff));
231*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArrayRead(g,&gg));
232c4762a1bSJed Brown 
233c4762a1bSJed Brown   /*
234c4762a1bSJed Brown      Compute function
235c4762a1bSJed Brown   */
236*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetSize(x,&n));
237c4762a1bSJed Brown   d     = (PetscReal)(n - 1); d = d*d;
238c4762a1bSJed Brown   ff[0] = xx[0];
239c4762a1bSJed Brown   for (i=1; i<n-1; i++) ff[i] = d*(xx[i-1] - 2.0*xx[i] + xx[i+1]) + xx[i]*xx[i] - gg[i];
240c4762a1bSJed Brown   ff[n-1] = xx[n-1] - 1.0;
241c4762a1bSJed Brown 
242c4762a1bSJed Brown   /*
243c4762a1bSJed Brown      Restore vectors
244c4762a1bSJed Brown   */
245*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArrayRead(x,&xx));
246*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArray(f,&ff));
247*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArrayRead(g,&gg));
248c4762a1bSJed Brown   return 0;
249c4762a1bSJed Brown }
250c4762a1bSJed Brown /* ------------------------------------------------------------------- */
251c4762a1bSJed Brown /*
252c4762a1bSJed Brown    FormJacobian - Evaluates Jacobian matrix.
253c4762a1bSJed Brown 
254c4762a1bSJed Brown    Input Parameters:
255c4762a1bSJed Brown .  snes - the SNES context
256c4762a1bSJed Brown .  x - input vector
257c4762a1bSJed Brown .  dummy - optional user-defined context (not used here)
258c4762a1bSJed Brown 
259c4762a1bSJed Brown    Output Parameters:
260c4762a1bSJed Brown .  jac - Jacobian matrix
261c4762a1bSJed Brown .  B - optionally different preconditioning matrix
262c4762a1bSJed Brown 
263c4762a1bSJed Brown */
264c4762a1bSJed Brown 
265c4762a1bSJed Brown PetscErrorCode FormJacobian(SNES snes,Vec x,Mat jac,Mat B,void *dummy)
266c4762a1bSJed Brown {
267c4762a1bSJed Brown   const PetscScalar *xx;
268c4762a1bSJed Brown   PetscScalar       A[3],d;
269c4762a1bSJed Brown   PetscInt          i,n,j[3];
270c4762a1bSJed Brown 
271c4762a1bSJed Brown   /*
272c4762a1bSJed Brown      Get pointer to vector data
273c4762a1bSJed Brown   */
274*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArrayRead(x,&xx));
275c4762a1bSJed Brown 
276c4762a1bSJed Brown   /*
277c4762a1bSJed Brown      Compute Jacobian entries and insert into matrix.
278c4762a1bSJed Brown       - Note that in this case we set all elements for a particular
279c4762a1bSJed Brown         row at once.
280c4762a1bSJed Brown   */
281*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetSize(x,&n));
282c4762a1bSJed Brown   d    = (PetscReal)(n - 1); d = d*d;
283c4762a1bSJed Brown 
284c4762a1bSJed Brown   /*
285c4762a1bSJed Brown      Interior grid points
286c4762a1bSJed Brown   */
287c4762a1bSJed Brown   for (i=1; i<n-1; i++) {
288c4762a1bSJed Brown     j[0] = i - 1; j[1] = i; j[2] = i + 1;
289c4762a1bSJed Brown     A[0] = A[2] = d; A[1] = -2.0*d + 2.0*xx[i];
290*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatSetValues(B,1,&i,3,j,A,INSERT_VALUES));
291c4762a1bSJed Brown   }
292c4762a1bSJed Brown 
293c4762a1bSJed Brown   /*
294c4762a1bSJed Brown      Boundary points
295c4762a1bSJed Brown   */
296c4762a1bSJed Brown   i = 0;   A[0] = 1.0;
297c4762a1bSJed Brown 
298*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatSetValues(B,1,&i,1,&i,A,INSERT_VALUES));
299c4762a1bSJed Brown 
300c4762a1bSJed Brown   i = n-1; A[0] = 1.0;
301c4762a1bSJed Brown 
302*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatSetValues(B,1,&i,1,&i,A,INSERT_VALUES));
303c4762a1bSJed Brown 
304c4762a1bSJed Brown   /*
305c4762a1bSJed Brown      Restore vector
306c4762a1bSJed Brown   */
307*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArrayRead(x,&xx));
308c4762a1bSJed Brown 
309c4762a1bSJed Brown   /*
310c4762a1bSJed Brown      Assemble matrix
311c4762a1bSJed Brown   */
312*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
313*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));
314c4762a1bSJed Brown   if (jac != B) {
315*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatAssemblyBegin(jac,MAT_FINAL_ASSEMBLY));
316*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatAssemblyEnd(jac,MAT_FINAL_ASSEMBLY));
317c4762a1bSJed Brown   }
318c4762a1bSJed Brown   return 0;
319c4762a1bSJed Brown }
320c4762a1bSJed Brown /* ------------------------------------------------------------------- */
321c4762a1bSJed Brown /*
322c4762a1bSJed Brown    Monitor - User-defined monitoring routine that views the
323c4762a1bSJed Brown    current iterate with an x-window plot.
324c4762a1bSJed Brown 
325c4762a1bSJed Brown    Input Parameters:
326c4762a1bSJed Brown    snes - the SNES context
327c4762a1bSJed Brown    its - iteration number
328c4762a1bSJed Brown    norm - 2-norm function value (may be estimated)
329c4762a1bSJed Brown    ctx - optional user-defined context for private data for the
330c4762a1bSJed Brown          monitor routine, as set by SNESMonitorSet()
331c4762a1bSJed Brown 
332c4762a1bSJed Brown    Note:
333c4762a1bSJed Brown    See the manpage for PetscViewerDrawOpen() for useful runtime options,
334c4762a1bSJed Brown    such as -nox to deactivate all x-window output.
335c4762a1bSJed Brown  */
336c4762a1bSJed Brown PetscErrorCode Monitor(SNES snes,PetscInt its,PetscReal fnorm,void *ctx)
337c4762a1bSJed Brown {
338c4762a1bSJed Brown   MonitorCtx     *monP = (MonitorCtx*) ctx;
339c4762a1bSJed Brown   Vec            x;
340c4762a1bSJed Brown 
341*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"iter = %D, SNES Function norm %g\n",its,(double)fnorm));
342*5f80ce2aSJacob Faibussowitsch   CHKERRQ(SNESGetSolution(snes,&x));
343*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecView(x,monP->viewer));
344c4762a1bSJed Brown   return 0;
345c4762a1bSJed Brown }
346c4762a1bSJed Brown 
347c4762a1bSJed Brown /*TEST
348c4762a1bSJed Brown 
349c4762a1bSJed Brown    test:
350c4762a1bSJed Brown       args: -nox -snes_monitor_cancel -snes_monitor_short -snes_view -pc_type jacobi -ksp_gmres_cgs_refinement_type refine_always
351c4762a1bSJed Brown 
352c4762a1bSJed Brown    test:
353c4762a1bSJed Brown       suffix: 2
354c4762a1bSJed Brown       args: -nox -snes_monitor_cancel -snes_monitor_short -snes_type newtontr -snes_view
355c4762a1bSJed Brown       requires: !single
356c4762a1bSJed Brown 
357c4762a1bSJed Brown    test:
358c4762a1bSJed Brown       suffix: 3
359c4762a1bSJed Brown       args: -nox -malloc no -options_left no -snes_monitor_cancel -snes_monitor_short -snes_view -pc_type jacobi -ksp_gmres_cgs_refinement_type refine_always
360c4762a1bSJed Brown 
36141ba4c6cSHeeho Park    test:
36241ba4c6cSHeeho Park       suffix: 4
36341ba4c6cSHeeho Park       args: -nox -snes_monitor_cancel -snes_monitor_short -snes_type newtontrdc -snes_view
36441ba4c6cSHeeho Park       requires: !single
36541ba4c6cSHeeho Park 
366c4762a1bSJed Brown TEST*/
367