xref: /petsc/src/ts/tutorials/ex4.c (revision 5f80ce2ab25dff0f4601e710601cbbcecf323266)
1c4762a1bSJed Brown 
2c4762a1bSJed Brown static char help[] ="Solves a simple time-dependent linear PDE (the heat equation).\n\
3c4762a1bSJed Brown Input parameters include:\n\
4c4762a1bSJed Brown   -m <points>, where <points> = number of grid points\n\
5c4762a1bSJed Brown   -time_dependent_rhs : Treat the problem as having a time-dependent right-hand side\n\
6c4762a1bSJed Brown   -debug              : Activate debugging printouts\n\
7c4762a1bSJed Brown   -nox                : Deactivate x-window graphics\n\n";
8c4762a1bSJed Brown 
9c4762a1bSJed Brown /*
10c4762a1bSJed Brown    Concepts: TS^time-dependent linear problems
11c4762a1bSJed Brown    Concepts: TS^heat equation
12c4762a1bSJed Brown    Concepts: TS^diffusion equation
13c4762a1bSJed Brown    Processors: n
14c4762a1bSJed Brown */
15c4762a1bSJed Brown 
16c4762a1bSJed Brown /* ------------------------------------------------------------------------
17c4762a1bSJed Brown 
18c4762a1bSJed Brown    This program solves the one-dimensional heat equation (also called the
19c4762a1bSJed Brown    diffusion equation),
20c4762a1bSJed Brown        u_t = u_xx,
21c4762a1bSJed Brown    on the domain 0 <= x <= 1, with the boundary conditions
22c4762a1bSJed Brown        u(t,0) = 0, u(t,1) = 0,
23c4762a1bSJed Brown    and the initial condition
24c4762a1bSJed Brown        u(0,x) = sin(6*pi*x) + 3*sin(2*pi*x).
25c4762a1bSJed Brown    This is a linear, second-order, parabolic equation.
26c4762a1bSJed Brown 
27c4762a1bSJed Brown    We discretize the right-hand side using finite differences with
28c4762a1bSJed Brown    uniform grid spacing h:
29c4762a1bSJed Brown        u_xx = (u_{i+1} - 2u_{i} + u_{i-1})/(h^2)
30c4762a1bSJed Brown    We then demonstrate time evolution using the various TS methods by
31c4762a1bSJed Brown    running the program via
32c4762a1bSJed Brown        mpiexec -n <procs> ex3 -ts_type <timestepping solver>
33c4762a1bSJed Brown 
34c4762a1bSJed Brown    We compare the approximate solution with the exact solution, given by
35c4762a1bSJed Brown        u_exact(x,t) = exp(-36*pi*pi*t) * sin(6*pi*x) +
36c4762a1bSJed Brown                       3*exp(-4*pi*pi*t) * sin(2*pi*x)
37c4762a1bSJed Brown 
38c4762a1bSJed Brown    Notes:
39c4762a1bSJed Brown    This code demonstrates the TS solver interface to two variants of
40c4762a1bSJed Brown    linear problems, u_t = f(u,t), namely
41c4762a1bSJed Brown      - time-dependent f:   f(u,t) is a function of t
42c4762a1bSJed Brown      - time-independent f: f(u,t) is simply f(u)
43c4762a1bSJed Brown 
44c4762a1bSJed Brown     The uniprocessor version of this code is ts/tutorials/ex3.c
45c4762a1bSJed Brown 
46c4762a1bSJed Brown   ------------------------------------------------------------------------- */
47c4762a1bSJed Brown 
48c4762a1bSJed Brown /*
49c4762a1bSJed Brown    Include "petscdmda.h" so that we can use distributed arrays (DMDAs) to manage
50c4762a1bSJed Brown    the parallel grid.  Include "petscts.h" so that we can use TS solvers.
51c4762a1bSJed Brown    Note that this file automatically includes:
52c4762a1bSJed Brown      petscsys.h       - base PETSc routines   petscvec.h  - vectors
53c4762a1bSJed Brown      petscmat.h  - matrices
54c4762a1bSJed Brown      petscis.h     - index sets            petscksp.h  - Krylov subspace methods
55c4762a1bSJed Brown      petscviewer.h - viewers               petscpc.h   - preconditioners
56c4762a1bSJed Brown      petscksp.h   - linear solvers        petscsnes.h - nonlinear solvers
57c4762a1bSJed Brown */
58c4762a1bSJed Brown 
59c4762a1bSJed Brown #include <petscdm.h>
60c4762a1bSJed Brown #include <petscdmda.h>
61c4762a1bSJed Brown #include <petscts.h>
62c4762a1bSJed Brown #include <petscdraw.h>
63c4762a1bSJed Brown 
64c4762a1bSJed Brown /*
65c4762a1bSJed Brown    User-defined application context - contains data needed by the
66c4762a1bSJed Brown    application-provided call-back routines.
67c4762a1bSJed Brown */
68c4762a1bSJed Brown typedef struct {
69c4762a1bSJed Brown   MPI_Comm    comm;              /* communicator */
70c4762a1bSJed Brown   DM          da;                /* distributed array data structure */
71c4762a1bSJed Brown   Vec         localwork;         /* local ghosted work vector */
72c4762a1bSJed Brown   Vec         u_local;           /* local ghosted approximate solution vector */
73c4762a1bSJed Brown   Vec         solution;          /* global exact solution vector */
74c4762a1bSJed Brown   PetscInt    m;                 /* total number of grid points */
75c4762a1bSJed Brown   PetscReal   h;                 /* mesh width h = 1/(m-1) */
76c4762a1bSJed Brown   PetscBool   debug;             /* flag (1 indicates activation of debugging printouts) */
77c4762a1bSJed Brown   PetscViewer viewer1,viewer2;  /* viewers for the solution and error */
78c4762a1bSJed Brown   PetscReal   norm_2,norm_max;  /* error norms */
79c4762a1bSJed Brown } AppCtx;
80c4762a1bSJed Brown 
81c4762a1bSJed Brown /*
82c4762a1bSJed Brown    User-defined routines
83c4762a1bSJed Brown */
84c4762a1bSJed Brown extern PetscErrorCode InitialConditions(Vec,AppCtx*);
85c4762a1bSJed Brown extern PetscErrorCode RHSMatrixHeat(TS,PetscReal,Vec,Mat,Mat,void*);
86c4762a1bSJed Brown extern PetscErrorCode RHSFunctionHeat(TS,PetscReal,Vec,Vec,void*);
87c4762a1bSJed Brown extern PetscErrorCode Monitor(TS,PetscInt,PetscReal,Vec,void*);
88c4762a1bSJed Brown extern PetscErrorCode ExactSolution(PetscReal,Vec,AppCtx*);
89c4762a1bSJed Brown 
90c4762a1bSJed Brown int main(int argc,char **argv)
91c4762a1bSJed Brown {
92c4762a1bSJed Brown   AppCtx         appctx;                 /* user-defined application context */
93c4762a1bSJed Brown   TS             ts;                     /* timestepping context */
94c4762a1bSJed Brown   Mat            A;                      /* matrix data structure */
95c4762a1bSJed Brown   Vec            u;                      /* approximate solution vector */
96c4762a1bSJed Brown   PetscReal      time_total_max = 1.0;   /* default max total time */
97c4762a1bSJed Brown   PetscInt       time_steps_max = 100;   /* default max timesteps */
98c4762a1bSJed Brown   PetscDraw      draw;                   /* drawing context */
99c4762a1bSJed Brown   PetscErrorCode ierr;
100c4762a1bSJed Brown   PetscInt       steps,m;
101c4762a1bSJed Brown   PetscMPIInt    size;
102c4762a1bSJed Brown   PetscReal      dt,ftime;
103c4762a1bSJed Brown   PetscBool      flg;
104c4762a1bSJed Brown   TSProblemType  tsproblem = TS_LINEAR;
105c4762a1bSJed Brown 
106c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
107c4762a1bSJed Brown      Initialize program and set problem parameters
108c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
109c4762a1bSJed Brown 
110c4762a1bSJed Brown   ierr        = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
111c4762a1bSJed Brown   appctx.comm = PETSC_COMM_WORLD;
112c4762a1bSJed Brown 
113c4762a1bSJed Brown   m               = 60;
114*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL));
115*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsHasName(NULL,NULL,"-debug",&appctx.debug));
116c4762a1bSJed Brown   appctx.m        = m;
117c4762a1bSJed Brown   appctx.h        = 1.0/(m-1.0);
118c4762a1bSJed Brown   appctx.norm_2   = 0.0;
119c4762a1bSJed Brown   appctx.norm_max = 0.0;
120c4762a1bSJed Brown 
121*5f80ce2aSJacob Faibussowitsch   CHKERRMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
122*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"Solving a linear TS problem, number of processors = %d\n",size));
123c4762a1bSJed Brown 
124c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
125c4762a1bSJed Brown      Create vector data structures
126c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
127c4762a1bSJed Brown   /*
128c4762a1bSJed Brown      Create distributed array (DMDA) to manage parallel grid and vectors
129c4762a1bSJed Brown      and to set up the ghost point communication pattern.  There are M
130c4762a1bSJed Brown      total grid values spread equally among all the processors.
131c4762a1bSJed Brown   */
132c4762a1bSJed Brown 
133*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,m,1,1,NULL,&appctx.da));
134*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetFromOptions(appctx.da));
135*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMSetUp(appctx.da));
136c4762a1bSJed Brown 
137c4762a1bSJed Brown   /*
138c4762a1bSJed Brown      Extract global and local vectors from DMDA; we use these to store the
139c4762a1bSJed Brown      approximate solution.  Then duplicate these for remaining vectors that
140c4762a1bSJed Brown      have the same types.
141c4762a1bSJed Brown   */
142*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreateGlobalVector(appctx.da,&u));
143*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMCreateLocalVector(appctx.da,&appctx.u_local));
144c4762a1bSJed Brown 
145c4762a1bSJed Brown   /*
146c4762a1bSJed Brown      Create local work vector for use in evaluating right-hand-side function;
147c4762a1bSJed Brown      create global work vector for storing exact solution.
148c4762a1bSJed Brown   */
149*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDuplicate(appctx.u_local,&appctx.localwork));
150*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDuplicate(u,&appctx.solution));
151c4762a1bSJed Brown 
152c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
153c4762a1bSJed Brown      Set up displays to show graphs of the solution and error
154c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
155c4762a1bSJed Brown 
156*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",80,380,400,160,&appctx.viewer1));
157*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerDrawGetDraw(appctx.viewer1,0,&draw));
158*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDrawSetDoubleBuffer(draw));
159*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",80,0,400,160,&appctx.viewer2));
160*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerDrawGetDraw(appctx.viewer2,0,&draw));
161*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscDrawSetDoubleBuffer(draw));
162c4762a1bSJed Brown 
163c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
164c4762a1bSJed Brown      Create timestepping solver context
165c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
166c4762a1bSJed Brown 
167*5f80ce2aSJacob Faibussowitsch   CHKERRQ(TSCreate(PETSC_COMM_WORLD,&ts));
168c4762a1bSJed Brown 
169c4762a1bSJed Brown   flg  = PETSC_FALSE;
170*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-nonlinear",&flg,NULL));
171*5f80ce2aSJacob Faibussowitsch   CHKERRQ(TSSetProblemType(ts,flg ? TS_NONLINEAR : TS_LINEAR));
172c4762a1bSJed Brown 
173c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
174c4762a1bSJed Brown      Set optional user-defined monitoring routine
175c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
176*5f80ce2aSJacob Faibussowitsch   CHKERRQ(TSMonitorSet(ts,Monitor,&appctx,NULL));
177c4762a1bSJed Brown 
178c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
179c4762a1bSJed Brown 
180c4762a1bSJed Brown      Create matrix data structure; set matrix evaluation routine.
181c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
182c4762a1bSJed Brown 
183*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatCreate(PETSC_COMM_WORLD,&A));
184*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m,m));
185*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatSetFromOptions(A));
186*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatSetUp(A));
187c4762a1bSJed Brown 
188c4762a1bSJed Brown   flg  = PETSC_FALSE;
189*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-time_dependent_rhs",&flg,NULL));
190c4762a1bSJed Brown   if (flg) {
191c4762a1bSJed Brown     /*
192c4762a1bSJed Brown        For linear problems with a time-dependent f(u,t) in the equation
193c4762a1bSJed Brown        u_t = f(u,t), the user provides the discretized right-hand-side
194c4762a1bSJed Brown        as a time-dependent matrix.
195c4762a1bSJed Brown     */
196*5f80ce2aSJacob Faibussowitsch     CHKERRQ(TSSetRHSFunction(ts,NULL,TSComputeRHSFunctionLinear,&appctx));
197*5f80ce2aSJacob Faibussowitsch     CHKERRQ(TSSetRHSJacobian(ts,A,A,RHSMatrixHeat,&appctx));
198c4762a1bSJed Brown   } else {
199c4762a1bSJed Brown     /*
200c4762a1bSJed Brown        For linear problems with a time-independent f(u) in the equation
201c4762a1bSJed Brown        u_t = f(u), the user provides the discretized right-hand-side
202c4762a1bSJed Brown        as a matrix only once, and then sets a null matrix evaluation
203c4762a1bSJed Brown        routine.
204c4762a1bSJed Brown     */
205*5f80ce2aSJacob Faibussowitsch     CHKERRQ(RHSMatrixHeat(ts,0.0,u,A,A,&appctx));
206*5f80ce2aSJacob Faibussowitsch     CHKERRQ(TSSetRHSFunction(ts,NULL,TSComputeRHSFunctionLinear,&appctx));
207*5f80ce2aSJacob Faibussowitsch     CHKERRQ(TSSetRHSJacobian(ts,A,A,TSComputeRHSJacobianConstant,&appctx));
208c4762a1bSJed Brown   }
209c4762a1bSJed Brown 
210c4762a1bSJed Brown   if (tsproblem == TS_NONLINEAR) {
211c4762a1bSJed Brown     SNES snes;
212*5f80ce2aSJacob Faibussowitsch     CHKERRQ(TSSetRHSFunction(ts,NULL,RHSFunctionHeat,&appctx));
213*5f80ce2aSJacob Faibussowitsch     CHKERRQ(TSGetSNES(ts,&snes));
214*5f80ce2aSJacob Faibussowitsch     CHKERRQ(SNESSetJacobian(snes,NULL,NULL,SNESComputeJacobianDefault,NULL));
215c4762a1bSJed Brown   }
216c4762a1bSJed Brown 
217c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
218c4762a1bSJed Brown      Set solution vector and initial timestep
219c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
220c4762a1bSJed Brown 
221c4762a1bSJed Brown   dt   = appctx.h*appctx.h/2.0;
222*5f80ce2aSJacob Faibussowitsch   CHKERRQ(TSSetTimeStep(ts,dt));
223*5f80ce2aSJacob Faibussowitsch   CHKERRQ(TSSetSolution(ts,u));
224c4762a1bSJed Brown 
225c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
226c4762a1bSJed Brown      Customize timestepping solver:
227c4762a1bSJed Brown        - Set the solution method to be the Backward Euler method.
228c4762a1bSJed Brown        - Set timestepping duration info
229c4762a1bSJed Brown      Then set runtime options, which can override these defaults.
230c4762a1bSJed Brown      For example,
231c4762a1bSJed Brown           -ts_max_steps <maxsteps> -ts_max_time <maxtime>
232c4762a1bSJed Brown      to override the defaults set by TSSetMaxSteps()/TSSetMaxTime().
233c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
234c4762a1bSJed Brown 
235*5f80ce2aSJacob Faibussowitsch   CHKERRQ(TSSetMaxSteps(ts,time_steps_max));
236*5f80ce2aSJacob Faibussowitsch   CHKERRQ(TSSetMaxTime(ts,time_total_max));
237*5f80ce2aSJacob Faibussowitsch   CHKERRQ(TSSetExactFinalTime(ts,TS_EXACTFINALTIME_STEPOVER));
238*5f80ce2aSJacob Faibussowitsch   CHKERRQ(TSSetFromOptions(ts));
239c4762a1bSJed Brown 
240c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
241c4762a1bSJed Brown      Solve the problem
242c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
243c4762a1bSJed Brown 
244c4762a1bSJed Brown   /*
245c4762a1bSJed Brown      Evaluate initial conditions
246c4762a1bSJed Brown   */
247*5f80ce2aSJacob Faibussowitsch   CHKERRQ(InitialConditions(u,&appctx));
248c4762a1bSJed Brown 
249c4762a1bSJed Brown   /*
250c4762a1bSJed Brown      Run the timestepping solver
251c4762a1bSJed Brown   */
252*5f80ce2aSJacob Faibussowitsch   CHKERRQ(TSSolve(ts,u));
253*5f80ce2aSJacob Faibussowitsch   CHKERRQ(TSGetSolveTime(ts,&ftime));
254*5f80ce2aSJacob Faibussowitsch   CHKERRQ(TSGetStepNumber(ts,&steps));
255c4762a1bSJed Brown 
256c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
257c4762a1bSJed Brown      View timestepping solver info
258c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
259*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"Total timesteps %D, Final time %g\n",steps,(double)ftime));
260*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscPrintf(PETSC_COMM_WORLD,"Avg. error (2 norm) = %g Avg. error (max norm) = %g\n",(double)(appctx.norm_2/steps),(double)(appctx.norm_max/steps)));
261c4762a1bSJed Brown 
262c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
263c4762a1bSJed Brown      Free work space.  All PETSc objects should be destroyed when they
264c4762a1bSJed Brown      are no longer needed.
265c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
266c4762a1bSJed Brown 
267*5f80ce2aSJacob Faibussowitsch   CHKERRQ(TSDestroy(&ts));
268*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatDestroy(&A));
269*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&u));
270*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerDestroy(&appctx.viewer1));
271*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscViewerDestroy(&appctx.viewer2));
272*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&appctx.localwork));
273*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&appctx.solution));
274*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecDestroy(&appctx.u_local));
275*5f80ce2aSJacob Faibussowitsch   CHKERRQ(DMDestroy(&appctx.da));
276c4762a1bSJed Brown 
277c4762a1bSJed Brown   /*
278c4762a1bSJed Brown      Always call PetscFinalize() before exiting a program.  This routine
279c4762a1bSJed Brown        - finalizes the PETSc libraries as well as MPI
280c4762a1bSJed Brown        - provides summary and diagnostic information if certain runtime
281c4762a1bSJed Brown          options are chosen (e.g., -log_view).
282c4762a1bSJed Brown   */
283c4762a1bSJed Brown   ierr = PetscFinalize();
284c4762a1bSJed Brown   return ierr;
285c4762a1bSJed Brown }
286c4762a1bSJed Brown /* --------------------------------------------------------------------- */
287c4762a1bSJed Brown /*
288c4762a1bSJed Brown    InitialConditions - Computes the solution at the initial time.
289c4762a1bSJed Brown 
290c4762a1bSJed Brown    Input Parameter:
291c4762a1bSJed Brown    u - uninitialized solution vector (global)
292c4762a1bSJed Brown    appctx - user-defined application context
293c4762a1bSJed Brown 
294c4762a1bSJed Brown    Output Parameter:
295c4762a1bSJed Brown    u - vector with solution at initial time (global)
296c4762a1bSJed Brown */
297c4762a1bSJed Brown PetscErrorCode InitialConditions(Vec u,AppCtx *appctx)
298c4762a1bSJed Brown {
299c4762a1bSJed Brown   PetscScalar    *u_localptr,h = appctx->h;
300c4762a1bSJed Brown   PetscInt       i,mybase,myend;
301c4762a1bSJed Brown 
302c4762a1bSJed Brown   /*
303c4762a1bSJed Brown      Determine starting point of each processor's range of
304c4762a1bSJed Brown      grid values.
305c4762a1bSJed Brown   */
306*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetOwnershipRange(u,&mybase,&myend));
307c4762a1bSJed Brown 
308c4762a1bSJed Brown   /*
309c4762a1bSJed Brown     Get a pointer to vector data.
310c4762a1bSJed Brown     - For default PETSc vectors, VecGetArray() returns a pointer to
311c4762a1bSJed Brown       the data array.  Otherwise, the routine is implementation dependent.
312c4762a1bSJed Brown     - You MUST call VecRestoreArray() when you no longer need access to
313c4762a1bSJed Brown       the array.
314c4762a1bSJed Brown     - Note that the Fortran interface to VecGetArray() differs from the
315c4762a1bSJed Brown       C version.  See the users manual for details.
316c4762a1bSJed Brown   */
317*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArray(u,&u_localptr));
318c4762a1bSJed Brown 
319c4762a1bSJed Brown   /*
320c4762a1bSJed Brown      We initialize the solution array by simply writing the solution
321c4762a1bSJed Brown      directly into the array locations.  Alternatively, we could use
322c4762a1bSJed Brown      VecSetValues() or VecSetValuesLocal().
323c4762a1bSJed Brown   */
324c4762a1bSJed Brown   for (i=mybase; i<myend; i++) u_localptr[i-mybase] = PetscSinScalar(PETSC_PI*i*6.*h) + 3.*PetscSinScalar(PETSC_PI*i*2.*h);
325c4762a1bSJed Brown 
326c4762a1bSJed Brown   /*
327c4762a1bSJed Brown      Restore vector
328c4762a1bSJed Brown   */
329*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArray(u,&u_localptr));
330c4762a1bSJed Brown 
331c4762a1bSJed Brown   /*
332c4762a1bSJed Brown      Print debugging information if desired
333c4762a1bSJed Brown   */
334c4762a1bSJed Brown   if (appctx->debug) {
335*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscPrintf(appctx->comm,"initial guess vector\n"));
336*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecView(u,PETSC_VIEWER_STDOUT_WORLD));
337c4762a1bSJed Brown   }
338c4762a1bSJed Brown 
339c4762a1bSJed Brown   return 0;
340c4762a1bSJed Brown }
341c4762a1bSJed Brown /* --------------------------------------------------------------------- */
342c4762a1bSJed Brown /*
343c4762a1bSJed Brown    ExactSolution - Computes the exact solution at a given time.
344c4762a1bSJed Brown 
345c4762a1bSJed Brown    Input Parameters:
346c4762a1bSJed Brown    t - current time
347c4762a1bSJed Brown    solution - vector in which exact solution will be computed
348c4762a1bSJed Brown    appctx - user-defined application context
349c4762a1bSJed Brown 
350c4762a1bSJed Brown    Output Parameter:
351c4762a1bSJed Brown    solution - vector with the newly computed exact solution
352c4762a1bSJed Brown */
353c4762a1bSJed Brown PetscErrorCode ExactSolution(PetscReal t,Vec solution,AppCtx *appctx)
354c4762a1bSJed Brown {
355c4762a1bSJed Brown   PetscScalar    *s_localptr,h = appctx->h,ex1,ex2,sc1,sc2;
356c4762a1bSJed Brown   PetscInt       i,mybase,myend;
357c4762a1bSJed Brown 
358c4762a1bSJed Brown   /*
359c4762a1bSJed Brown      Determine starting and ending points of each processor's
360c4762a1bSJed Brown      range of grid values
361c4762a1bSJed Brown   */
362*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetOwnershipRange(solution,&mybase,&myend));
363c4762a1bSJed Brown 
364c4762a1bSJed Brown   /*
365c4762a1bSJed Brown      Get a pointer to vector data.
366c4762a1bSJed Brown   */
367*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecGetArray(solution,&s_localptr));
368c4762a1bSJed Brown 
369c4762a1bSJed Brown   /*
370c4762a1bSJed Brown      Simply write the solution directly into the array locations.
371c4762a1bSJed Brown      Alternatively, we culd use VecSetValues() or VecSetValuesLocal().
372c4762a1bSJed Brown   */
373c4762a1bSJed Brown   ex1 = PetscExpReal(-36.*PETSC_PI*PETSC_PI*t); ex2 = PetscExpReal(-4.*PETSC_PI*PETSC_PI*t);
374c4762a1bSJed Brown   sc1 = PETSC_PI*6.*h;                 sc2 = PETSC_PI*2.*h;
375c4762a1bSJed Brown   for (i=mybase; i<myend; i++) s_localptr[i-mybase] = PetscSinScalar(sc1*(PetscReal)i)*ex1 + 3.*PetscSinScalar(sc2*(PetscReal)i)*ex2;
376c4762a1bSJed Brown 
377c4762a1bSJed Brown   /*
378c4762a1bSJed Brown      Restore vector
379c4762a1bSJed Brown   */
380*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecRestoreArray(solution,&s_localptr));
381c4762a1bSJed Brown   return 0;
382c4762a1bSJed Brown }
383c4762a1bSJed Brown /* --------------------------------------------------------------------- */
384c4762a1bSJed Brown /*
385c4762a1bSJed Brown    Monitor - User-provided routine to monitor the solution computed at
386c4762a1bSJed Brown    each timestep.  This example plots the solution and computes the
387c4762a1bSJed Brown    error in two different norms.
388c4762a1bSJed Brown 
389c4762a1bSJed Brown    Input Parameters:
390c4762a1bSJed Brown    ts     - the timestep context
391c4762a1bSJed Brown    step   - the count of the current step (with 0 meaning the
392c4762a1bSJed Brown              initial condition)
393c4762a1bSJed Brown    time   - the current time
394c4762a1bSJed Brown    u      - the solution at this timestep
395c4762a1bSJed Brown    ctx    - the user-provided context for this monitoring routine.
396c4762a1bSJed Brown             In this case we use the application context which contains
397c4762a1bSJed Brown             information about the problem size, workspace and the exact
398c4762a1bSJed Brown             solution.
399c4762a1bSJed Brown */
400c4762a1bSJed Brown PetscErrorCode Monitor(TS ts,PetscInt step,PetscReal time,Vec u,void *ctx)
401c4762a1bSJed Brown {
402c4762a1bSJed Brown   AppCtx         *appctx = (AppCtx*) ctx;   /* user-defined application context */
403c4762a1bSJed Brown   PetscReal      norm_2,norm_max;
404c4762a1bSJed Brown 
405c4762a1bSJed Brown   /*
406c4762a1bSJed Brown      View a graph of the current iterate
407c4762a1bSJed Brown   */
408*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecView(u,appctx->viewer2));
409c4762a1bSJed Brown 
410c4762a1bSJed Brown   /*
411c4762a1bSJed Brown      Compute the exact solution
412c4762a1bSJed Brown   */
413*5f80ce2aSJacob Faibussowitsch   CHKERRQ(ExactSolution(time,appctx->solution,appctx));
414c4762a1bSJed Brown 
415c4762a1bSJed Brown   /*
416c4762a1bSJed Brown      Print debugging information if desired
417c4762a1bSJed Brown   */
418c4762a1bSJed Brown   if (appctx->debug) {
419*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscPrintf(appctx->comm,"Computed solution vector\n"));
420*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecView(u,PETSC_VIEWER_STDOUT_WORLD));
421*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscPrintf(appctx->comm,"Exact solution vector\n"));
422*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecView(appctx->solution,PETSC_VIEWER_STDOUT_WORLD));
423c4762a1bSJed Brown   }
424c4762a1bSJed Brown 
425c4762a1bSJed Brown   /*
426c4762a1bSJed Brown      Compute the 2-norm and max-norm of the error
427c4762a1bSJed Brown   */
428*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecAXPY(appctx->solution,-1.0,u));
429*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecNorm(appctx->solution,NORM_2,&norm_2));
430c4762a1bSJed Brown   norm_2 = PetscSqrtReal(appctx->h)*norm_2;
431*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecNorm(appctx->solution,NORM_MAX,&norm_max));
432c4762a1bSJed Brown   if (norm_2   < 1e-14) norm_2   = 0;
433c4762a1bSJed Brown   if (norm_max < 1e-14) norm_max = 0;
434c4762a1bSJed Brown 
435c4762a1bSJed Brown   /*
436c4762a1bSJed Brown      PetscPrintf() causes only the first processor in this
437c4762a1bSJed Brown      communicator to print the timestep information.
438c4762a1bSJed Brown   */
439*5f80ce2aSJacob Faibussowitsch   CHKERRQ(PetscPrintf(appctx->comm,"Timestep %D: time = %g 2-norm error = %g max norm error = %g\n",step,(double)time,(double)norm_2,(double)norm_max));
440c4762a1bSJed Brown   appctx->norm_2   += norm_2;
441c4762a1bSJed Brown   appctx->norm_max += norm_max;
442c4762a1bSJed Brown 
443c4762a1bSJed Brown   /*
444c4762a1bSJed Brown      View a graph of the error
445c4762a1bSJed Brown   */
446*5f80ce2aSJacob Faibussowitsch   CHKERRQ(VecView(appctx->solution,appctx->viewer1));
447c4762a1bSJed Brown 
448c4762a1bSJed Brown   /*
449c4762a1bSJed Brown      Print debugging information if desired
450c4762a1bSJed Brown   */
451c4762a1bSJed Brown   if (appctx->debug) {
452*5f80ce2aSJacob Faibussowitsch     CHKERRQ(PetscPrintf(appctx->comm,"Error vector\n"));
453*5f80ce2aSJacob Faibussowitsch     CHKERRQ(VecView(appctx->solution,PETSC_VIEWER_STDOUT_WORLD));
454c4762a1bSJed Brown   }
455c4762a1bSJed Brown 
456c4762a1bSJed Brown   return 0;
457c4762a1bSJed Brown }
458c4762a1bSJed Brown 
459c4762a1bSJed Brown /* --------------------------------------------------------------------- */
460c4762a1bSJed Brown /*
461c4762a1bSJed Brown    RHSMatrixHeat - User-provided routine to compute the right-hand-side
462c4762a1bSJed Brown    matrix for the heat equation.
463c4762a1bSJed Brown 
464c4762a1bSJed Brown    Input Parameters:
465c4762a1bSJed Brown    ts - the TS context
466c4762a1bSJed Brown    t - current time
467c4762a1bSJed Brown    global_in - global input vector
468c4762a1bSJed Brown    dummy - optional user-defined context, as set by TSetRHSJacobian()
469c4762a1bSJed Brown 
470c4762a1bSJed Brown    Output Parameters:
471c4762a1bSJed Brown    AA - Jacobian matrix
472c4762a1bSJed Brown    BB - optionally different preconditioning matrix
473c4762a1bSJed Brown    str - flag indicating matrix structure
474c4762a1bSJed Brown 
475c4762a1bSJed Brown   Notes:
476c4762a1bSJed Brown   RHSMatrixHeat computes entries for the locally owned part of the system.
477c4762a1bSJed Brown    - Currently, all PETSc parallel matrix formats are partitioned by
478c4762a1bSJed Brown      contiguous chunks of rows across the processors.
479c4762a1bSJed Brown    - Each processor needs to insert only elements that it owns
480c4762a1bSJed Brown      locally (but any non-local elements will be sent to the
481c4762a1bSJed Brown      appropriate processor during matrix assembly).
482c4762a1bSJed Brown    - Always specify global row and columns of matrix entries when
483c4762a1bSJed Brown      using MatSetValues(); we could alternatively use MatSetValuesLocal().
484c4762a1bSJed Brown    - Here, we set all entries for a particular row at once.
485c4762a1bSJed Brown    - Note that MatSetValues() uses 0-based row and column numbers
486c4762a1bSJed Brown      in Fortran as well as in C.
487c4762a1bSJed Brown */
488c4762a1bSJed Brown PetscErrorCode RHSMatrixHeat(TS ts,PetscReal t,Vec X,Mat AA,Mat BB,void *ctx)
489c4762a1bSJed Brown {
490c4762a1bSJed Brown   Mat            A       = AA;              /* Jacobian matrix */
491c4762a1bSJed Brown   AppCtx         *appctx = (AppCtx*)ctx;     /* user-defined application context */
492c4762a1bSJed Brown   PetscInt       i,mstart,mend,idx[3];
493c4762a1bSJed Brown   PetscScalar    v[3],stwo = -2./(appctx->h*appctx->h),sone = -.5*stwo;
494c4762a1bSJed Brown 
495c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
496c4762a1bSJed Brown      Compute entries for the locally owned part of the matrix
497c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
498c4762a1bSJed Brown 
499*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatGetOwnershipRange(A,&mstart,&mend));
500c4762a1bSJed Brown 
501c4762a1bSJed Brown   /*
502c4762a1bSJed Brown      Set matrix rows corresponding to boundary data
503c4762a1bSJed Brown   */
504c4762a1bSJed Brown 
505c4762a1bSJed Brown   if (mstart == 0) {  /* first processor only */
506c4762a1bSJed Brown     v[0] = 1.0;
507*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatSetValues(A,1,&mstart,1,&mstart,v,INSERT_VALUES));
508c4762a1bSJed Brown     mstart++;
509c4762a1bSJed Brown   }
510c4762a1bSJed Brown 
511c4762a1bSJed Brown   if (mend == appctx->m) { /* last processor only */
512c4762a1bSJed Brown     mend--;
513c4762a1bSJed Brown     v[0] = 1.0;
514*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatSetValues(A,1,&mend,1,&mend,v,INSERT_VALUES));
515c4762a1bSJed Brown   }
516c4762a1bSJed Brown 
517c4762a1bSJed Brown   /*
518c4762a1bSJed Brown      Set matrix rows corresponding to interior data.  We construct the
519c4762a1bSJed Brown      matrix one row at a time.
520c4762a1bSJed Brown   */
521c4762a1bSJed Brown   v[0] = sone; v[1] = stwo; v[2] = sone;
522c4762a1bSJed Brown   for (i=mstart; i<mend; i++) {
523c4762a1bSJed Brown     idx[0] = i-1; idx[1] = i; idx[2] = i+1;
524*5f80ce2aSJacob Faibussowitsch     CHKERRQ(MatSetValues(A,1,&i,3,idx,v,INSERT_VALUES));
525c4762a1bSJed Brown   }
526c4762a1bSJed Brown 
527c4762a1bSJed Brown   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
528c4762a1bSJed Brown      Complete the matrix assembly process and set some options
529c4762a1bSJed Brown      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
530c4762a1bSJed Brown   /*
531c4762a1bSJed Brown      Assemble matrix, using the 2-step process:
532c4762a1bSJed Brown        MatAssemblyBegin(), MatAssemblyEnd()
533c4762a1bSJed Brown      Computations can be done while messages are in transition
534c4762a1bSJed Brown      by placing code between these two statements.
535c4762a1bSJed Brown   */
536*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
537*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
538c4762a1bSJed Brown 
539c4762a1bSJed Brown   /*
540c4762a1bSJed Brown      Set and option to indicate that we will never add a new nonzero location
541c4762a1bSJed Brown      to the matrix. If we do, it will generate an error.
542c4762a1bSJed Brown   */
543*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE));
544c4762a1bSJed Brown 
545c4762a1bSJed Brown   return 0;
546c4762a1bSJed Brown }
547c4762a1bSJed Brown 
548c4762a1bSJed Brown PetscErrorCode RHSFunctionHeat(TS ts,PetscReal t,Vec globalin,Vec globalout,void *ctx)
549c4762a1bSJed Brown {
550c4762a1bSJed Brown   Mat            A;
551c4762a1bSJed Brown 
552c4762a1bSJed Brown   PetscFunctionBeginUser;
553*5f80ce2aSJacob Faibussowitsch   CHKERRQ(TSGetRHSJacobian(ts,&A,NULL,NULL,&ctx));
554*5f80ce2aSJacob Faibussowitsch   CHKERRQ(RHSMatrixHeat(ts,t,globalin,A,NULL,ctx));
555*5f80ce2aSJacob Faibussowitsch   /* CHKERRQ(MatView(A,PETSC_VIEWER_STDOUT_WORLD)); */
556*5f80ce2aSJacob Faibussowitsch   CHKERRQ(MatMult(A,globalin,globalout));
557c4762a1bSJed Brown   PetscFunctionReturn(0);
558c4762a1bSJed Brown }
559c4762a1bSJed Brown 
560c4762a1bSJed Brown /*TEST
561c4762a1bSJed Brown 
562c4762a1bSJed Brown     test:
563c4762a1bSJed Brown       args: -ts_view -nox
564c4762a1bSJed Brown 
565c4762a1bSJed Brown     test:
566c4762a1bSJed Brown       suffix: 2
567c4762a1bSJed Brown       args: -ts_view -nox
568c4762a1bSJed Brown       nsize: 3
569c4762a1bSJed Brown 
570c4762a1bSJed Brown     test:
571c4762a1bSJed Brown       suffix: 3
572c4762a1bSJed Brown       args: -ts_view -nox -nonlinear
573c4762a1bSJed Brown 
574c4762a1bSJed Brown     test:
575c4762a1bSJed Brown       suffix: 4
576c4762a1bSJed Brown       args: -ts_view -nox -nonlinear
577c4762a1bSJed Brown       nsize: 3
578c4762a1bSJed Brown       timeoutfactor: 3
579c4762a1bSJed Brown 
580c4762a1bSJed Brown     test:
581c4762a1bSJed Brown       suffix: sundials
582e808b789SPatrick Sanan       requires: sundials2
583c4762a1bSJed Brown       args: -nox -ts_type sundials -ts_max_steps 5 -nonlinear
584c4762a1bSJed Brown       nsize: 4
585c4762a1bSJed Brown 
5867324063eSPatrick Sanan     test:
5877324063eSPatrick Sanan       suffix: sundials_dense
5887324063eSPatrick Sanan       requires: sundials2
5897324063eSPatrick Sanan       args: -nox -ts_type sundials -ts_sundials_use_dense -ts_max_steps 5 -nonlinear
5907324063eSPatrick Sanan       nsize: 1
5917324063eSPatrick Sanan 
592c4762a1bSJed Brown TEST*/
593