xref: /petsc/src/ts/interface/ts.c (revision 648b50de1df4efe77e6f62dd35de1702f4663c9b)
163dd3a1aSKris Buschelman 
2af0996ceSBarry Smith #include <petsc/private/tsimpl.h>        /*I "petscts.h"  I*/
3496e6a7aSJed Brown #include <petscdmshell.h>
41e25c274SJed Brown #include <petscdmda.h>
52d5ee99bSBarry Smith #include <petscviewer.h>
62d5ee99bSBarry Smith #include <petscdraw.h>
7d763cef2SBarry Smith 
8d5ba7fb7SMatthew Knepley /* Logging support */
9d74926cbSBarry Smith PetscClassId  TS_CLASSID, DMTS_CLASSID;
10166c7f25SBarry Smith PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval;
11d405a339SMatthew Knepley 
1249354f04SShri Abhyankar const char *const TSExactFinalTimeOptions[] = {"STEPOVER","INTERPOLATE","MATCHSTEP","TSExactFinalTimeOption","TS_EXACTFINALTIME_",0};
1349354f04SShri Abhyankar 
142d5ee99bSBarry Smith struct _n_TSMonitorDrawCtx {
152d5ee99bSBarry Smith   PetscViewer   viewer;
164b363babSBarry Smith   PetscDrawAxis axis;
172d5ee99bSBarry Smith   Vec           initialsolution;
182d5ee99bSBarry Smith   PetscBool     showinitial;
192d5ee99bSBarry Smith   PetscInt      howoften;  /* when > 0 uses step % howoften, when negative only final solution plotted */
202d5ee99bSBarry Smith   PetscBool     showtimestepandtime;
212d5ee99bSBarry Smith   int           color;
222d5ee99bSBarry Smith };
232d5ee99bSBarry Smith 
24bdad233fSMatthew Knepley #undef __FUNCT__
25bdad233fSMatthew Knepley #define __FUNCT__ "TSSetFromOptions"
26bdad233fSMatthew Knepley /*@
27bdad233fSMatthew Knepley    TSSetFromOptions - Sets various TS parameters from user options.
28bdad233fSMatthew Knepley 
29bdad233fSMatthew Knepley    Collective on TS
30bdad233fSMatthew Knepley 
31bdad233fSMatthew Knepley    Input Parameter:
32bdad233fSMatthew Knepley .  ts - the TS context obtained from TSCreate()
33bdad233fSMatthew Knepley 
34bdad233fSMatthew Knepley    Options Database Keys:
354d91e141SJed Brown +  -ts_type <type> - TSEULER, TSBEULER, TSSUNDIALS, TSPSEUDO, TSCN, TSRK, TSTHETA, TSGL, TSSSP
36ef222394SBarry Smith .  -ts_save_trajectory - checkpoint the solution at each time-step
373e4cdcaaSBarry Smith .  -ts_max_steps <maxsteps> - maximum number of time-steps to take
383e4cdcaaSBarry Smith .  -ts_final_time <time> - maximum time to compute to
393e4cdcaaSBarry Smith .  -ts_dt <dt> - initial time step
40a3cdaa26SBarry Smith .  -ts_exact_final_time <stepover,interpolate,matchstep> whether to stop at the exact given final time and how to compute the solution at that ti,e
41a3cdaa26SBarry Smith .  -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed
42a3cdaa26SBarry Smith .  -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails
43a3cdaa26SBarry Smith .  -ts_error_if_step_fails <true,false> - Error if no step succeeds
44a3cdaa26SBarry Smith .  -ts_rtol <rtol> - relative tolerance for local truncation error
45a3cdaa26SBarry Smith .  -ts_atol <atol> Absolute tolerance for local truncation error
46ef222394SBarry Smith .  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
47847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
48bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
49de06c3feSJed Brown .  -ts_monitor_lg_timestep - Monitor timestep size graphically
50de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
51de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
52de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
53de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
54de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
55de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
563e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
573e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
5891b97e58SBarry Smith .  -ts_monitor_solution_binary <filename> - Save each solution to a binary file
59b3d3934dSBarry Smith .  -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
60b3d3934dSBarry Smith -  -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time
6191b97e58SBarry Smith 
6291b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
63bdad233fSMatthew Knepley 
64bdad233fSMatthew Knepley    Level: beginner
65bdad233fSMatthew Knepley 
66bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
67bdad233fSMatthew Knepley 
68a313700dSBarry Smith .seealso: TSGetType()
69bdad233fSMatthew Knepley @*/
707087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
71bdad233fSMatthew Knepley {
72bc952696SBarry Smith   PetscBool              opt,flg,tflg;
73dfbe8321SBarry Smith   PetscErrorCode         ierr;
74649052a6SBarry Smith   PetscViewer            monviewer;
75eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
76089b2837SJed Brown   SNES                   snes;
771c3436cfSJed Brown   TSAdapt                adapt;
7831748224SBarry Smith   PetscReal              time_step;
7949354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
80d1212d36SBarry Smith   char                   dir[16];
816991f827SBarry Smith   const char             *defaultType;
826991f827SBarry Smith   char                   typeName[256];
83bdad233fSMatthew Knepley 
84bdad233fSMatthew Knepley   PetscFunctionBegin;
850700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
863194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
876991f827SBarry Smith   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
886991f827SBarry Smith   else defaultType = TSEULER;
896991f827SBarry Smith 
906991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
916991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
926991f827SBarry Smith   if (opt) {
936991f827SBarry Smith     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
946991f827SBarry Smith   } else {
956991f827SBarry Smith     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
966991f827SBarry Smith   }
97bdad233fSMatthew Knepley 
98bdad233fSMatthew Knepley   /* Handle generic TS options */
99844594baSBarry Smith   if (ts->trajectory) tflg = PETSC_TRUE;
100844594baSBarry Smith   else tflg = PETSC_FALSE;
101920aabf2SBarry Smith   ierr = PetscOptionsBool("-ts_save_trajectory","Save the solution at each timestep","TSSetSaveTrajectory",tflg,&tflg,NULL);CHKERRQ(ierr);
102bc952696SBarry Smith   if (tflg) {ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);}
103ef222394SBarry Smith   if (ts->adjoint_solve) tflg = PETSC_TRUE;
104ef222394SBarry Smith   else tflg = PETSC_FALSE;
105ef222394SBarry Smith   ierr = PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&flg);CHKERRQ(ierr);
106ef222394SBarry Smith   if (flg) {
107ef222394SBarry Smith     ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);
108e87fd094SBarry Smith     ts->adjoint_solve = tflg;
109ef222394SBarry Smith   }
1100298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1110298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
1120298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
11331748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
11431748224SBarry Smith   if (flg) {
11531748224SBarry Smith     ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
11631748224SBarry Smith   }
11749354f04SShri Abhyankar   ierr = PetscOptionsEnum("-ts_exact_final_time","Option for handling of final time step","TSSetExactFinalTime",TSExactFinalTimeOptions,(PetscEnum)ts->exact_final_time,(PetscEnum*)&eftopt,&flg);CHKERRQ(ierr);
11849354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1190298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_snes_failures","Maximum number of nonlinear solve failures","TSSetMaxSNESFailures",ts->max_snes_failures,&ts->max_snes_failures,NULL);CHKERRQ(ierr);
1200298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1210298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1220298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1230298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
124bdad233fSMatthew Knepley 
12556f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
12656f85f32SBarry Smith   {
12756f85f32SBarry Smith   PetscBool set;
12856f85f32SBarry Smith   flg  = PETSC_FALSE;
12956f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
13056f85f32SBarry Smith   if (set) {
13156f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
13256f85f32SBarry Smith   }
13356f85f32SBarry Smith   }
13456f85f32SBarry Smith #endif
13556f85f32SBarry Smith 
136bdad233fSMatthew Knepley   /* Monitor options */
137a6570f20SBarry Smith   ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
138eabae89aSBarry Smith   if (flg) {
139ce94432eSBarry Smith     ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr);
140649052a6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
141bdad233fSMatthew Knepley   }
1425180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1435180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1445180491cSLisandro Dalcin 
1454f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
146a7cc72afSBarry Smith   if (opt) {
1470b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1483923b477SBarry Smith     PetscInt       howoften = 1;
149a80ad3e0SBarry Smith 
1500298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
151ce94432eSBarry Smith     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
1524f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
153b3603a34SBarry Smith   }
1544f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
155b3603a34SBarry Smith   if (opt) {
1560b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1573923b477SBarry Smith     PetscInt       howoften = 1;
158b3603a34SBarry Smith 
1590298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
16022d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1614f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
162bdad233fSMatthew Knepley   }
1634f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
164ef20d060SBarry Smith   if (opt) {
1650b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1663923b477SBarry Smith     PetscInt       howoften = 1;
167ef20d060SBarry Smith 
1680298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
16922d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1704f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
171ef20d060SBarry Smith   }
172201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
173201da799SBarry Smith   if (opt) {
174201da799SBarry Smith     TSMonitorLGCtx ctx;
175201da799SBarry Smith     PetscInt       howoften = 1;
176201da799SBarry Smith 
1770298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
17822d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
179201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
180201da799SBarry Smith   }
181201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
182201da799SBarry Smith   if (opt) {
183201da799SBarry Smith     TSMonitorLGCtx ctx;
184201da799SBarry Smith     PetscInt       howoften = 1;
185201da799SBarry Smith 
1860298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
18722d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
188201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
189201da799SBarry Smith   }
1908189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
1918189c53fSBarry Smith   if (opt) {
1928189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
1938189c53fSBarry Smith     PetscInt          howoften = 1;
1948189c53fSBarry Smith 
1950298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
19622d28d08SBarry Smith     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1978189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
1988189c53fSBarry Smith   }
199ef20d060SBarry Smith   opt  = PETSC_FALSE;
2000dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
201a7cc72afSBarry Smith   if (opt) {
20283a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
20383a4ac43SBarry Smith     PetscInt         howoften = 1;
204a80ad3e0SBarry Smith 
2050298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
206ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
20783a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
208bdad233fSMatthew Knepley   }
209fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2102d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2112d5ee99bSBarry Smith   if (opt) {
2122d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2132d5ee99bSBarry Smith     PetscReal        bounds[4];
2142d5ee99bSBarry Smith     PetscInt         n = 4;
2152d5ee99bSBarry Smith     PetscDraw        draw;
2162d5ee99bSBarry Smith 
2172d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2182d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2192d5ee99bSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr);
2202d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2212d5ee99bSBarry Smith     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
2224b363babSBarry Smith     ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr);
2234b363babSBarry Smith     ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
224f54adfc0SBarry Smith     ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2254b363babSBarry Smith     ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr);
22607995780SPeter Brune     /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */
2272d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2282d5ee99bSBarry Smith   }
2292d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2300dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2313a471f94SBarry Smith   if (opt) {
23283a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
23383a4ac43SBarry Smith     PetscInt         howoften = 1;
2343a471f94SBarry Smith 
2350298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
236ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
23783a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2383a471f94SBarry Smith   }
2393a471f94SBarry Smith   opt  = PETSC_FALSE;
24091b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
241fb1732b5SBarry Smith   if (flg) {
242fb1732b5SBarry Smith     PetscViewer ctx;
243fb1732b5SBarry Smith     if (monfilename[0]) {
244ce94432eSBarry Smith       ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
245c2fbc07fSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
246fb1732b5SBarry Smith     } else {
247ce94432eSBarry Smith       ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts));
2480298fd71SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr);
249fb1732b5SBarry Smith     }
250fb1732b5SBarry Smith   }
251ed81e22dSJed Brown   opt  = PETSC_FALSE;
25291b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_vtk","Save each time step to a binary file, use filename-%%03D.vts","TSMonitorSolutionVTK",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
253ed81e22dSJed Brown   if (flg) {
254ed81e22dSJed Brown     const char *ptr,*ptr2;
255ed81e22dSJed Brown     char       *filetemplate;
256ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
257ed81e22dSJed Brown     /* Do some cursory validation of the input. */
258ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
259ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
260ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
261ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
262ce94432eSBarry Smith       if (!ptr2 && (*ptr < '0' || '9' < *ptr)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Invalid file template argument to -ts_monitor_solution_vtk, should look like filename-%%03D.vts");
263ed81e22dSJed Brown       if (ptr2) break;
264ed81e22dSJed Brown     }
265ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
266ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
267ed81e22dSJed Brown   }
268bdad233fSMatthew Knepley 
269d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
270d1212d36SBarry Smith   if (flg) {
271d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
272d1212d36SBarry Smith     int                  ray = 0;
273d1212d36SBarry Smith     DMDADirection        ddir;
274d1212d36SBarry Smith     DM                   da;
275d1212d36SBarry Smith     PetscMPIInt          rank;
276d1212d36SBarry Smith 
277ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
278d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
279d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
280ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
281d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
282d1212d36SBarry Smith 
283d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
284b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
285d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
286d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
287ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
288d1212d36SBarry Smith     if (!rank) {
289d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
290d1212d36SBarry Smith     }
29151b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
292d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
293d1212d36SBarry Smith   }
29451b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
29551b4a12fSMatthew G. Knepley   if (flg) {
29651b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
29751b4a12fSMatthew G. Knepley     int                 ray = 0;
29851b4a12fSMatthew G. Knepley     DMDADirection       ddir;
29951b4a12fSMatthew G. Knepley     DM                  da;
30051b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
301d1212d36SBarry Smith 
30251b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
30351b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
30451b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
30551b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
30651b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
3071c3436cfSJed Brown 
30851b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
309b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
31051b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
31151b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
31251b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
31351b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
31451b4a12fSMatthew G. Knepley   }
315a7a1495cSBarry Smith 
316b3d3934dSBarry Smith   ierr = PetscOptionsName("-ts_monitor_envelope","Monitor maximum and minimum value of each component of the solution","TSMonitorEnvelope",&opt);CHKERRQ(ierr);
317b3d3934dSBarry Smith   if (opt) {
318b3d3934dSBarry Smith     TSMonitorEnvelopeCtx ctx;
319b3d3934dSBarry Smith 
320b3d3934dSBarry Smith     ierr = TSMonitorEnvelopeCtxCreate(ts,&ctx);CHKERRQ(ierr);
321b3d3934dSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorEnvelope,ctx,(PetscErrorCode (*)(void**))TSMonitorEnvelopeCtxDestroy);CHKERRQ(ierr);
322b3d3934dSBarry Smith   }
323b3d3934dSBarry Smith 
324847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
325847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
326847ff0e1SMatthew G. Knepley   if (flg) {
327847ff0e1SMatthew G. Knepley     DM   dm;
328847ff0e1SMatthew G. Knepley     DMTS tdm;
329847ff0e1SMatthew G. Knepley 
330847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
331847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
332847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
333847ff0e1SMatthew G. Knepley     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr);
334847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
335847ff0e1SMatthew G. Knepley   }
336847ff0e1SMatthew G. Knepley 
3377781c08eSBarry Smith   /*
3387781c08eSBarry Smith      This code is all wrong. One is creating objects inside the TSSetFromOptions() so if run with the options gui
3397781c08eSBarry Smith      will bleed memory. Also one is using a PetscOptionsBegin() inside a PetscOptionsBegin()
3407781c08eSBarry Smith   */
341552698daSJed Brown   ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
342e55864a3SBarry Smith   ierr = TSAdaptSetFromOptions(PetscOptionsObject,adapt);CHKERRQ(ierr);
343d763cef2SBarry Smith 
344d763cef2SBarry Smith     /* Handle specific TS options */
345d763cef2SBarry Smith   if (ts->ops->setfromoptions) {
3466991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
347d763cef2SBarry Smith   }
3486991f827SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
349d763cef2SBarry Smith 
350d763cef2SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
351d763cef2SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
352d763cef2SBarry Smith 
353bc952696SBarry Smith   if (ts->trajectory) {
354bc952696SBarry Smith     ierr = TSTrajectorySetFromOptions(ts->trajectory);CHKERRQ(ierr);
355bc952696SBarry Smith   }
356bc952696SBarry Smith 
3576991f827SBarry Smith   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3586991f827SBarry Smith   if (snes) {
3596991f827SBarry Smith     if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
3606991f827SBarry Smith     ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
361d763cef2SBarry Smith   }
362d763cef2SBarry Smith   PetscFunctionReturn(0);
363d763cef2SBarry Smith }
364d763cef2SBarry Smith 
365d763cef2SBarry Smith #undef __FUNCT__
366bc952696SBarry Smith #define __FUNCT__ "TSSetSaveTrajectory"
367d2daff3dSHong Zhang /*@
368bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
369d2daff3dSHong Zhang 
370d2daff3dSHong Zhang    Collective on TS
371d2daff3dSHong Zhang 
372d2daff3dSHong Zhang    Input Parameters:
373bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
374bc952696SBarry Smith 
375d2daff3dSHong Zhang 
376d2daff3dSHong Zhang    Level: intermediate
377d2daff3dSHong Zhang 
378bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve()
379d2daff3dSHong Zhang 
380bc952696SBarry Smith .keywords: TS, set, checkpoint,
381d2daff3dSHong Zhang @*/
382bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
383d2daff3dSHong Zhang {
384bc952696SBarry Smith   PetscErrorCode ierr;
385bc952696SBarry Smith 
386d2daff3dSHong Zhang   PetscFunctionBegin;
387d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
388bc952696SBarry Smith   if (!ts->trajectory) {
389bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
390920aabf2SBarry Smith     ierr = TSTrajectorySetType(ts->trajectory,TSTRAJECTORYBASIC);CHKERRQ(ierr);
391*648b50deSBarry Smith     ierr = TSTrajectorySetFromOptions(ts->trajectory);CHKERRQ(ierr);
392bc952696SBarry Smith   }
393d2daff3dSHong Zhang   PetscFunctionReturn(0);
394d2daff3dSHong Zhang }
395d2daff3dSHong Zhang 
396a7a1495cSBarry Smith #undef __FUNCT__
397a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian"
398a7a1495cSBarry Smith /*@
399a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
400a7a1495cSBarry Smith       set with TSSetRHSJacobian().
401a7a1495cSBarry Smith 
402a7a1495cSBarry Smith    Collective on TS and Vec
403a7a1495cSBarry Smith 
404a7a1495cSBarry Smith    Input Parameters:
405316643e7SJed Brown +  ts - the TS context
406a7a1495cSBarry Smith .  t - current timestep
4070910c330SBarry Smith -  U - input vector
408a7a1495cSBarry Smith 
409a7a1495cSBarry Smith    Output Parameters:
410a7a1495cSBarry Smith +  A - Jacobian matrix
411a7a1495cSBarry Smith .  B - optional preconditioning matrix
412a7a1495cSBarry Smith -  flag - flag indicating matrix structure
413a7a1495cSBarry Smith 
414a7a1495cSBarry Smith    Notes:
415a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
416a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
417a7a1495cSBarry Smith 
41894b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
419a7a1495cSBarry Smith    flag parameter.
420a7a1495cSBarry Smith 
421a7a1495cSBarry Smith    Level: developer
422a7a1495cSBarry Smith 
423a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
424a7a1495cSBarry Smith 
42594b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
426a7a1495cSBarry Smith @*/
427d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
428a7a1495cSBarry Smith {
429dfbe8321SBarry Smith   PetscErrorCode ierr;
430270bf2e7SJed Brown   PetscObjectState Ustate;
43124989b8cSPeter Brune   DM             dm;
432942e3340SBarry Smith   DMTS           tsdm;
43324989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
43424989b8cSPeter Brune   void           *ctx;
43524989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
436b2df71adSDebojyoti Ghosh   TSRHSFunction  rhsfunction;
437a7a1495cSBarry Smith 
438a7a1495cSBarry Smith   PetscFunctionBegin;
4390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4400910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4410910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
44224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
443942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
44424989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
4450298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
446b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
44759e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
448b2df71adSDebojyoti Ghosh   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
4490e4ef248SJed Brown     PetscFunctionReturn(0);
450f8ede8e7SJed Brown   }
451d90be118SSean Farley 
452ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
453d90be118SSean Farley 
454e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
45594ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
45694ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
45794ab13aaSBarry Smith     if (A != B) {
45894ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
45994ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
460e1244c69SJed Brown     }
461e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
462e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
463e1244c69SJed Brown   }
464e1244c69SJed Brown 
46524989b8cSPeter Brune   if (rhsjacobianfunc) {
46694ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
467a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
468d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
469a7a1495cSBarry Smith     PetscStackPop;
47094ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
471a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
47294ab13aaSBarry Smith     PetscValidHeaderSpecific(A,MAT_CLASSID,4);
47394ab13aaSBarry Smith     PetscValidHeaderSpecific(B,MAT_CLASSID,5);
474ef66eb69SBarry Smith   } else {
47594ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
47694ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
477ef66eb69SBarry Smith   }
4780e4ef248SJed Brown   ts->rhsjacobian.time       = t;
4790910c330SBarry Smith   ts->rhsjacobian.X          = U;
48059e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
481a7a1495cSBarry Smith   PetscFunctionReturn(0);
482a7a1495cSBarry Smith }
483a7a1495cSBarry Smith 
4844a2ae208SSatish Balay #undef __FUNCT__
4854a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
486316643e7SJed Brown /*@
487d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
488d763cef2SBarry Smith 
489316643e7SJed Brown    Collective on TS and Vec
490316643e7SJed Brown 
491316643e7SJed Brown    Input Parameters:
492316643e7SJed Brown +  ts - the TS context
493316643e7SJed Brown .  t - current time
4940910c330SBarry Smith -  U - state vector
495316643e7SJed Brown 
496316643e7SJed Brown    Output Parameter:
497316643e7SJed Brown .  y - right hand side
498316643e7SJed Brown 
499316643e7SJed Brown    Note:
500316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
501316643e7SJed Brown    is used internally within the nonlinear solvers.
502316643e7SJed Brown 
503316643e7SJed Brown    Level: developer
504316643e7SJed Brown 
505316643e7SJed Brown .keywords: TS, compute
506316643e7SJed Brown 
507316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
508316643e7SJed Brown @*/
5090910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
510d763cef2SBarry Smith {
511dfbe8321SBarry Smith   PetscErrorCode ierr;
51224989b8cSPeter Brune   TSRHSFunction  rhsfunction;
51324989b8cSPeter Brune   TSIFunction    ifunction;
51424989b8cSPeter Brune   void           *ctx;
51524989b8cSPeter Brune   DM             dm;
51624989b8cSPeter Brune 
517d763cef2SBarry Smith   PetscFunctionBegin;
5180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5190910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5200700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
52124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
52224989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
5230298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
524d763cef2SBarry Smith 
525ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
526d763cef2SBarry Smith 
5270910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
52824989b8cSPeter Brune   if (rhsfunction) {
529d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
5300910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
531d763cef2SBarry Smith     PetscStackPop;
532214bc6a2SJed Brown   } else {
533214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
534b2cd27e8SJed Brown   }
53544a41b28SSean Farley 
5360910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
537d763cef2SBarry Smith   PetscFunctionReturn(0);
538d763cef2SBarry Smith }
539d763cef2SBarry Smith 
5404a2ae208SSatish Balay #undef __FUNCT__
541ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
542ef20d060SBarry Smith /*@
543ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
544ef20d060SBarry Smith 
545ef20d060SBarry Smith    Collective on TS and Vec
546ef20d060SBarry Smith 
547ef20d060SBarry Smith    Input Parameters:
548ef20d060SBarry Smith +  ts - the TS context
549ef20d060SBarry Smith -  t - current time
550ef20d060SBarry Smith 
551ef20d060SBarry Smith    Output Parameter:
5520910c330SBarry Smith .  U - the solution
553ef20d060SBarry Smith 
554ef20d060SBarry Smith    Note:
555ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
556ef20d060SBarry Smith    is used internally within the nonlinear solvers.
557ef20d060SBarry Smith 
558ef20d060SBarry Smith    Level: developer
559ef20d060SBarry Smith 
560ef20d060SBarry Smith .keywords: TS, compute
561ef20d060SBarry Smith 
562abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
563ef20d060SBarry Smith @*/
5640910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
565ef20d060SBarry Smith {
566ef20d060SBarry Smith   PetscErrorCode     ierr;
567ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
568ef20d060SBarry Smith   void               *ctx;
569ef20d060SBarry Smith   DM                 dm;
570ef20d060SBarry Smith 
571ef20d060SBarry Smith   PetscFunctionBegin;
572ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5730910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
574ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
575ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
576ef20d060SBarry Smith 
577ef20d060SBarry Smith   if (solutionfunction) {
5789b7cd975SBarry Smith     PetscStackPush("TS user solution function");
5790910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
580ef20d060SBarry Smith     PetscStackPop;
581ef20d060SBarry Smith   }
582ef20d060SBarry Smith   PetscFunctionReturn(0);
583ef20d060SBarry Smith }
5849b7cd975SBarry Smith #undef __FUNCT__
5859b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
5869b7cd975SBarry Smith /*@
5879b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
5889b7cd975SBarry Smith 
5899b7cd975SBarry Smith    Collective on TS and Vec
5909b7cd975SBarry Smith 
5919b7cd975SBarry Smith    Input Parameters:
5929b7cd975SBarry Smith +  ts - the TS context
5939b7cd975SBarry Smith -  t - current time
5949b7cd975SBarry Smith 
5959b7cd975SBarry Smith    Output Parameter:
5969b7cd975SBarry Smith .  U - the function value
5979b7cd975SBarry Smith 
5989b7cd975SBarry Smith    Note:
5999b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
6009b7cd975SBarry Smith    is used internally within the nonlinear solvers.
6019b7cd975SBarry Smith 
6029b7cd975SBarry Smith    Level: developer
6039b7cd975SBarry Smith 
6049b7cd975SBarry Smith .keywords: TS, compute
6059b7cd975SBarry Smith 
6069b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
6079b7cd975SBarry Smith @*/
6089b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
6099b7cd975SBarry Smith {
6109b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
6119b7cd975SBarry Smith   void               *ctx;
6129b7cd975SBarry Smith   DM                 dm;
6139b7cd975SBarry Smith 
6149b7cd975SBarry Smith   PetscFunctionBegin;
6159b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6169b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6179b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
6189b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
6199b7cd975SBarry Smith 
6209b7cd975SBarry Smith   if (forcing) {
6219b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
6229b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
6239b7cd975SBarry Smith     PetscStackPop;
6249b7cd975SBarry Smith   }
6259b7cd975SBarry Smith   PetscFunctionReturn(0);
6269b7cd975SBarry Smith }
627ef20d060SBarry Smith 
628ef20d060SBarry Smith #undef __FUNCT__
629214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
630214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
631214bc6a2SJed Brown {
6322dd45cf8SJed Brown   Vec            F;
633214bc6a2SJed Brown   PetscErrorCode ierr;
634214bc6a2SJed Brown 
635214bc6a2SJed Brown   PetscFunctionBegin;
6360298fd71SBarry Smith   *Frhs = NULL;
6370298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
638214bc6a2SJed Brown   if (!ts->Frhs) {
6392dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
640214bc6a2SJed Brown   }
641214bc6a2SJed Brown   *Frhs = ts->Frhs;
642214bc6a2SJed Brown   PetscFunctionReturn(0);
643214bc6a2SJed Brown }
644214bc6a2SJed Brown 
645214bc6a2SJed Brown #undef __FUNCT__
646214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
647214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
648214bc6a2SJed Brown {
649214bc6a2SJed Brown   Mat            A,B;
6502dd45cf8SJed Brown   PetscErrorCode ierr;
651214bc6a2SJed Brown 
652214bc6a2SJed Brown   PetscFunctionBegin;
653c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
654c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
6550298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
656214bc6a2SJed Brown   if (Arhs) {
657214bc6a2SJed Brown     if (!ts->Arhs) {
658214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
659214bc6a2SJed Brown     }
660214bc6a2SJed Brown     *Arhs = ts->Arhs;
661214bc6a2SJed Brown   }
662214bc6a2SJed Brown   if (Brhs) {
663214bc6a2SJed Brown     if (!ts->Brhs) {
664bdb70873SJed Brown       if (A != B) {
665214bc6a2SJed Brown         ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
666bdb70873SJed Brown       } else {
667bdb70873SJed Brown         ts->Brhs = ts->Arhs;
668bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
669bdb70873SJed Brown       }
670214bc6a2SJed Brown     }
671214bc6a2SJed Brown     *Brhs = ts->Brhs;
672214bc6a2SJed Brown   }
673214bc6a2SJed Brown   PetscFunctionReturn(0);
674214bc6a2SJed Brown }
675214bc6a2SJed Brown 
676214bc6a2SJed Brown #undef __FUNCT__
677316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
678316643e7SJed Brown /*@
6790910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
680316643e7SJed Brown 
681316643e7SJed Brown    Collective on TS and Vec
682316643e7SJed Brown 
683316643e7SJed Brown    Input Parameters:
684316643e7SJed Brown +  ts - the TS context
685316643e7SJed Brown .  t - current time
6860910c330SBarry Smith .  U - state vector
6870910c330SBarry Smith .  Udot - time derivative of state vector
688214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
689316643e7SJed Brown 
690316643e7SJed Brown    Output Parameter:
691316643e7SJed Brown .  Y - right hand side
692316643e7SJed Brown 
693316643e7SJed Brown    Note:
694316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
695316643e7SJed Brown    is used internally within the nonlinear solvers.
696316643e7SJed Brown 
697316643e7SJed Brown    If the user did did not write their equations in implicit form, this
698316643e7SJed Brown    function recasts them in implicit form.
699316643e7SJed Brown 
700316643e7SJed Brown    Level: developer
701316643e7SJed Brown 
702316643e7SJed Brown .keywords: TS, compute
703316643e7SJed Brown 
704316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
705316643e7SJed Brown @*/
7060910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
707316643e7SJed Brown {
708316643e7SJed Brown   PetscErrorCode ierr;
70924989b8cSPeter Brune   TSIFunction    ifunction;
71024989b8cSPeter Brune   TSRHSFunction  rhsfunction;
71124989b8cSPeter Brune   void           *ctx;
71224989b8cSPeter Brune   DM             dm;
713316643e7SJed Brown 
714316643e7SJed Brown   PetscFunctionBegin;
7150700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7160910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7170910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
7180700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
719316643e7SJed Brown 
72024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
72124989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
7220298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
72324989b8cSPeter Brune 
724ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
725d90be118SSean Farley 
7260910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
72724989b8cSPeter Brune   if (ifunction) {
728316643e7SJed Brown     PetscStackPush("TS user implicit function");
7290910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
730316643e7SJed Brown     PetscStackPop;
731214bc6a2SJed Brown   }
732214bc6a2SJed Brown   if (imex) {
73324989b8cSPeter Brune     if (!ifunction) {
7340910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
7352dd45cf8SJed Brown     }
73624989b8cSPeter Brune   } else if (rhsfunction) {
73724989b8cSPeter Brune     if (ifunction) {
738214bc6a2SJed Brown       Vec Frhs;
739214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
7400910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
741214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
7422dd45cf8SJed Brown     } else {
7430910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
7440910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
745316643e7SJed Brown     }
7464a6899ffSJed Brown   }
7470910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
748316643e7SJed Brown   PetscFunctionReturn(0);
749316643e7SJed Brown }
750316643e7SJed Brown 
751316643e7SJed Brown #undef __FUNCT__
752316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
753316643e7SJed Brown /*@
754316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
755316643e7SJed Brown 
756316643e7SJed Brown    Collective on TS and Vec
757316643e7SJed Brown 
758316643e7SJed Brown    Input
759316643e7SJed Brown       Input Parameters:
760316643e7SJed Brown +  ts - the TS context
761316643e7SJed Brown .  t - current timestep
7620910c330SBarry Smith .  U - state vector
7630910c330SBarry Smith .  Udot - time derivative of state vector
764214bc6a2SJed Brown .  shift - shift to apply, see note below
765214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
766316643e7SJed Brown 
767316643e7SJed Brown    Output Parameters:
768316643e7SJed Brown +  A - Jacobian matrix
769316643e7SJed Brown .  B - optional preconditioning matrix
770316643e7SJed Brown -  flag - flag indicating matrix structure
771316643e7SJed Brown 
772316643e7SJed Brown    Notes:
7730910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
774316643e7SJed Brown 
7750910c330SBarry Smith    dF/dU + shift*dF/dUdot
776316643e7SJed Brown 
777316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
778316643e7SJed Brown    is used internally within the nonlinear solvers.
779316643e7SJed Brown 
780316643e7SJed Brown    Level: developer
781316643e7SJed Brown 
782316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
783316643e7SJed Brown 
784316643e7SJed Brown .seealso:  TSSetIJacobian()
78563495f91SJed Brown @*/
786d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
787316643e7SJed Brown {
788316643e7SJed Brown   PetscErrorCode ierr;
78924989b8cSPeter Brune   TSIJacobian    ijacobian;
79024989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
79124989b8cSPeter Brune   DM             dm;
79224989b8cSPeter Brune   void           *ctx;
793316643e7SJed Brown 
794316643e7SJed Brown   PetscFunctionBegin;
7950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7960910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7970910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
798316643e7SJed Brown   PetscValidPointer(A,6);
79994ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
800316643e7SJed Brown   PetscValidPointer(B,7);
80194ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
80224989b8cSPeter Brune 
80324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
80424989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
8050298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
80624989b8cSPeter Brune 
807ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
808316643e7SJed Brown 
80994ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
81024989b8cSPeter Brune   if (ijacobian) {
811316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
812d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
813316643e7SJed Brown     PetscStackPop;
814214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
81594ab13aaSBarry Smith     PetscValidHeaderSpecific(A,MAT_CLASSID,4);
81694ab13aaSBarry Smith     PetscValidHeaderSpecific(B,MAT_CLASSID,5);
8174a6899ffSJed Brown   }
818214bc6a2SJed Brown   if (imex) {
819b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
82094ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
82194ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
82294ab13aaSBarry Smith       if (A != B) {
82394ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
82494ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
825214bc6a2SJed Brown       }
826214bc6a2SJed Brown     }
827214bc6a2SJed Brown   } else {
828e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
829e1244c69SJed Brown     if (rhsjacobian) {
830bd373395SBarry Smith       if (ijacobian) {
831214bc6a2SJed Brown         ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
832bd373395SBarry Smith       } else {
833bd373395SBarry Smith         ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr);
834214bc6a2SJed Brown       }
835d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
836e1244c69SJed Brown     }
83794ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
838e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
839e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
84094ab13aaSBarry Smith       ierr = MatScale(A,-1);CHKERRQ(ierr);
84194ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
84294ab13aaSBarry Smith       if (A != B) {
84394ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
84494ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
845316643e7SJed Brown       }
846e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
847e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
848e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
84994ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
85094ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
85194ab13aaSBarry Smith         if (A != B) {
85294ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
85394ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
854214bc6a2SJed Brown         }
855316643e7SJed Brown       }
85694ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
85794ab13aaSBarry Smith       if (A != B) {
85894ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
859316643e7SJed Brown       }
860316643e7SJed Brown     }
861316643e7SJed Brown   }
86294ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
863316643e7SJed Brown   PetscFunctionReturn(0);
864316643e7SJed Brown }
865316643e7SJed Brown 
866316643e7SJed Brown #undef __FUNCT__
8674a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
868d763cef2SBarry Smith /*@C
869d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
870b5abc632SBarry Smith     where U_t = G(t,u).
871d763cef2SBarry Smith 
8723f9fe445SBarry Smith     Logically Collective on TS
873d763cef2SBarry Smith 
874d763cef2SBarry Smith     Input Parameters:
875d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
8760298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
877d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
878d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
8790298fd71SBarry Smith           function evaluation routine (may be NULL)
880d763cef2SBarry Smith 
881d763cef2SBarry Smith     Calling sequence of func:
88287828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
883d763cef2SBarry Smith 
884d763cef2SBarry Smith +   t - current timestep
885d763cef2SBarry Smith .   u - input vector
886d763cef2SBarry Smith .   F - function vector
887d763cef2SBarry Smith -   ctx - [optional] user-defined function context
888d763cef2SBarry Smith 
889d763cef2SBarry Smith     Level: beginner
890d763cef2SBarry Smith 
8912bbac0d3SBarry Smith     Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
8922bbac0d3SBarry Smith 
893d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
894d763cef2SBarry Smith 
895ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
896d763cef2SBarry Smith @*/
897089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
898d763cef2SBarry Smith {
899089b2837SJed Brown   PetscErrorCode ierr;
900089b2837SJed Brown   SNES           snes;
9010298fd71SBarry Smith   Vec            ralloc = NULL;
90224989b8cSPeter Brune   DM             dm;
903d763cef2SBarry Smith 
904089b2837SJed Brown   PetscFunctionBegin;
9050700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
906ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
90724989b8cSPeter Brune 
90824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
90924989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
910089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
911e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
912e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
913e856ceecSJed Brown     r    = ralloc;
914e856ceecSJed Brown   }
915089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
916e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
917d763cef2SBarry Smith   PetscFunctionReturn(0);
918d763cef2SBarry Smith }
919d763cef2SBarry Smith 
9204a2ae208SSatish Balay #undef __FUNCT__
921ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
922ef20d060SBarry Smith /*@C
923abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
924ef20d060SBarry Smith 
925ef20d060SBarry Smith     Logically Collective on TS
926ef20d060SBarry Smith 
927ef20d060SBarry Smith     Input Parameters:
928ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
929ef20d060SBarry Smith .   f - routine for evaluating the solution
930ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
9310298fd71SBarry Smith           function evaluation routine (may be NULL)
932ef20d060SBarry Smith 
933ef20d060SBarry Smith     Calling sequence of func:
934ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
935ef20d060SBarry Smith 
936ef20d060SBarry Smith +   t - current timestep
937ef20d060SBarry Smith .   u - output vector
938ef20d060SBarry Smith -   ctx - [optional] user-defined function context
939ef20d060SBarry Smith 
940abd5a294SJed Brown     Notes:
941abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
942abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
943abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
944abd5a294SJed Brown 
9454f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
946abd5a294SJed Brown 
947ef20d060SBarry Smith     Level: beginner
948ef20d060SBarry Smith 
949ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
950ef20d060SBarry Smith 
9519b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
952ef20d060SBarry Smith @*/
953ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
954ef20d060SBarry Smith {
955ef20d060SBarry Smith   PetscErrorCode ierr;
956ef20d060SBarry Smith   DM             dm;
957ef20d060SBarry Smith 
958ef20d060SBarry Smith   PetscFunctionBegin;
959ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
960ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
961ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
962ef20d060SBarry Smith   PetscFunctionReturn(0);
963ef20d060SBarry Smith }
964ef20d060SBarry Smith 
965ef20d060SBarry Smith #undef __FUNCT__
9669b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
9679b7cd975SBarry Smith /*@C
9689b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
9699b7cd975SBarry Smith 
9709b7cd975SBarry Smith     Logically Collective on TS
9719b7cd975SBarry Smith 
9729b7cd975SBarry Smith     Input Parameters:
9739b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
9749b7cd975SBarry Smith .   f - routine for evaluating the forcing function
9759b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
9760298fd71SBarry Smith           function evaluation routine (may be NULL)
9779b7cd975SBarry Smith 
9789b7cd975SBarry Smith     Calling sequence of func:
9799b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
9809b7cd975SBarry Smith 
9819b7cd975SBarry Smith +   t - current timestep
9829b7cd975SBarry Smith .   u - output vector
9839b7cd975SBarry Smith -   ctx - [optional] user-defined function context
9849b7cd975SBarry Smith 
9859b7cd975SBarry Smith     Notes:
9869b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
9879b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
9889b7cd975SBarry Smith 
9899b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
9909b7cd975SBarry Smith 
9919b7cd975SBarry Smith     Level: beginner
9929b7cd975SBarry Smith 
9939b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
9949b7cd975SBarry Smith 
9959b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
9969b7cd975SBarry Smith @*/
9979b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
9989b7cd975SBarry Smith {
9999b7cd975SBarry Smith   PetscErrorCode ierr;
10009b7cd975SBarry Smith   DM             dm;
10019b7cd975SBarry Smith 
10029b7cd975SBarry Smith   PetscFunctionBegin;
10039b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10049b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
10059b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
10069b7cd975SBarry Smith   PetscFunctionReturn(0);
10079b7cd975SBarry Smith }
10089b7cd975SBarry Smith 
10099b7cd975SBarry Smith #undef __FUNCT__
10104a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
1011d763cef2SBarry Smith /*@C
1012f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1013b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1014d763cef2SBarry Smith 
10153f9fe445SBarry Smith    Logically Collective on TS
1016d763cef2SBarry Smith 
1017d763cef2SBarry Smith    Input Parameters:
1018d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1019e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1020e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1021d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1022d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
10230298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1024d763cef2SBarry Smith 
1025f7ab8db6SBarry Smith    Calling sequence of f:
1026ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1027d763cef2SBarry Smith 
1028d763cef2SBarry Smith +  t - current timestep
1029d763cef2SBarry Smith .  u - input vector
1030e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1031e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1032d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1033d763cef2SBarry Smith 
1034d763cef2SBarry Smith 
1035d763cef2SBarry Smith    Level: beginner
1036d763cef2SBarry Smith 
1037d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1038d763cef2SBarry Smith 
1039ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1040d763cef2SBarry Smith 
1041d763cef2SBarry Smith @*/
1042e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1043d763cef2SBarry Smith {
1044277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1045089b2837SJed Brown   SNES           snes;
104624989b8cSPeter Brune   DM             dm;
104724989b8cSPeter Brune   TSIJacobian    ijacobian;
1048277b19d0SLisandro Dalcin 
1049d763cef2SBarry Smith   PetscFunctionBegin;
10500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1051e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1052e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1053e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1054e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1055d763cef2SBarry Smith 
105624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
105724989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1058e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1059e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1060e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1061e1244c69SJed Brown   }
10620298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1063089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10645f659677SPeter Brune   if (!ijacobian) {
1065e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
10660e4ef248SJed Brown   }
1067e5d3d808SBarry Smith   if (Amat) {
1068e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
10690e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1070bbd56ea5SKarl Rupp 
1071e5d3d808SBarry Smith     ts->Arhs = Amat;
10720e4ef248SJed Brown   }
1073e5d3d808SBarry Smith   if (Pmat) {
1074e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
10750e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1076bbd56ea5SKarl Rupp 
1077e5d3d808SBarry Smith     ts->Brhs = Pmat;
10780e4ef248SJed Brown   }
1079d763cef2SBarry Smith   PetscFunctionReturn(0);
1080d763cef2SBarry Smith }
1081d763cef2SBarry Smith 
1082316643e7SJed Brown 
1083316643e7SJed Brown #undef __FUNCT__
1084316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
1085316643e7SJed Brown /*@C
1086b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1087316643e7SJed Brown 
10883f9fe445SBarry Smith    Logically Collective on TS
1089316643e7SJed Brown 
1090316643e7SJed Brown    Input Parameters:
1091316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
10920298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1093316643e7SJed Brown .  f   - the function evaluation routine
10940298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1095316643e7SJed Brown 
1096316643e7SJed Brown    Calling sequence of f:
1097316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1098316643e7SJed Brown 
1099316643e7SJed Brown +  t   - time at step/stage being solved
1100316643e7SJed Brown .  u   - state vector
1101316643e7SJed Brown .  u_t - time derivative of state vector
1102316643e7SJed Brown .  F   - function vector
1103316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1104316643e7SJed Brown 
1105316643e7SJed Brown    Important:
11062bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1107316643e7SJed Brown 
1108316643e7SJed Brown    Level: beginner
1109316643e7SJed Brown 
1110316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1111316643e7SJed Brown 
1112d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1113316643e7SJed Brown @*/
1114089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
1115316643e7SJed Brown {
1116089b2837SJed Brown   PetscErrorCode ierr;
1117089b2837SJed Brown   SNES           snes;
11180298fd71SBarry Smith   Vec            resalloc = NULL;
111924989b8cSPeter Brune   DM             dm;
1120316643e7SJed Brown 
1121316643e7SJed Brown   PetscFunctionBegin;
11220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1123ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
112424989b8cSPeter Brune 
112524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
112624989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
112724989b8cSPeter Brune 
1128089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1129e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
1130e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
1131e856ceecSJed Brown     res  = resalloc;
1132e856ceecSJed Brown   }
1133089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
1134e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
1135089b2837SJed Brown   PetscFunctionReturn(0);
1136089b2837SJed Brown }
1137089b2837SJed Brown 
1138089b2837SJed Brown #undef __FUNCT__
1139089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1140089b2837SJed Brown /*@C
1141089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1142089b2837SJed Brown 
1143089b2837SJed Brown    Not Collective
1144089b2837SJed Brown 
1145089b2837SJed Brown    Input Parameter:
1146089b2837SJed Brown .  ts - the TS context
1147089b2837SJed Brown 
1148089b2837SJed Brown    Output Parameter:
11490298fd71SBarry Smith +  r - vector to hold residual (or NULL)
11500298fd71SBarry Smith .  func - the function to compute residual (or NULL)
11510298fd71SBarry Smith -  ctx - the function context (or NULL)
1152089b2837SJed Brown 
1153089b2837SJed Brown    Level: advanced
1154089b2837SJed Brown 
1155089b2837SJed Brown .keywords: TS, nonlinear, get, function
1156089b2837SJed Brown 
1157089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1158089b2837SJed Brown @*/
1159089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1160089b2837SJed Brown {
1161089b2837SJed Brown   PetscErrorCode ierr;
1162089b2837SJed Brown   SNES           snes;
116324989b8cSPeter Brune   DM             dm;
1164089b2837SJed Brown 
1165089b2837SJed Brown   PetscFunctionBegin;
1166089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1167089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11680298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
116924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
117024989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1171089b2837SJed Brown   PetscFunctionReturn(0);
1172089b2837SJed Brown }
1173089b2837SJed Brown 
1174089b2837SJed Brown #undef __FUNCT__
1175089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1176089b2837SJed Brown /*@C
1177089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1178089b2837SJed Brown 
1179089b2837SJed Brown    Not Collective
1180089b2837SJed Brown 
1181089b2837SJed Brown    Input Parameter:
1182089b2837SJed Brown .  ts - the TS context
1183089b2837SJed Brown 
1184089b2837SJed Brown    Output Parameter:
11850298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
11860298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
11870298fd71SBarry Smith -  ctx - the function context (or NULL)
1188089b2837SJed Brown 
1189089b2837SJed Brown    Level: advanced
1190089b2837SJed Brown 
1191089b2837SJed Brown .keywords: TS, nonlinear, get, function
1192089b2837SJed Brown 
11932bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1194089b2837SJed Brown @*/
1195089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1196089b2837SJed Brown {
1197089b2837SJed Brown   PetscErrorCode ierr;
1198089b2837SJed Brown   SNES           snes;
119924989b8cSPeter Brune   DM             dm;
1200089b2837SJed Brown 
1201089b2837SJed Brown   PetscFunctionBegin;
1202089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1203089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12040298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
120524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
120624989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1207316643e7SJed Brown   PetscFunctionReturn(0);
1208316643e7SJed Brown }
1209316643e7SJed Brown 
1210316643e7SJed Brown #undef __FUNCT__
1211316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1212316643e7SJed Brown /*@C
1213a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1214ae8867d6SBarry Smith         provided with TSSetIFunction().
1215316643e7SJed Brown 
12163f9fe445SBarry Smith    Logically Collective on TS
1217316643e7SJed Brown 
1218316643e7SJed Brown    Input Parameters:
1219316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1220e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1221e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1222316643e7SJed Brown .  f   - the Jacobian evaluation routine
12230298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1224316643e7SJed Brown 
1225316643e7SJed Brown    Calling sequence of f:
1226ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1227316643e7SJed Brown 
1228316643e7SJed Brown +  t    - time at step/stage being solved
12291b4a444bSJed Brown .  U    - state vector
12301b4a444bSJed Brown .  U_t  - time derivative of state vector
1231316643e7SJed Brown .  a    - shift
1232e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1233e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1234316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1235316643e7SJed Brown 
1236316643e7SJed Brown    Notes:
1237e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1238316643e7SJed Brown 
1239895c21f2SBarry Smith    If you know the operator Amat has a null space you can use MatSetNullSpace() and MatSetTransposeNullSpace() to supply the null
1240895c21f2SBarry Smith    space to Amat and the KSP solvers will automatically use that null space as needed during the solution process.
1241895c21f2SBarry Smith 
1242a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1243b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1244a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1245a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1246a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1247a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1248a4f0a591SBarry Smith 
1249316643e7SJed Brown    Level: beginner
1250316643e7SJed Brown 
1251316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1252316643e7SJed Brown 
1253ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1254316643e7SJed Brown 
1255316643e7SJed Brown @*/
1256e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1257316643e7SJed Brown {
1258316643e7SJed Brown   PetscErrorCode ierr;
1259089b2837SJed Brown   SNES           snes;
126024989b8cSPeter Brune   DM             dm;
1261316643e7SJed Brown 
1262316643e7SJed Brown   PetscFunctionBegin;
12630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1264e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1265e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1266e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1267e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
126824989b8cSPeter Brune 
126924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
127024989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
127124989b8cSPeter Brune 
1272089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1273e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1274316643e7SJed Brown   PetscFunctionReturn(0);
1275316643e7SJed Brown }
1276316643e7SJed Brown 
12774a2ae208SSatish Balay #undef __FUNCT__
1278e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse"
1279e1244c69SJed Brown /*@
1280e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1281e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1282e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1283e1244c69SJed Brown    not been changed by the TS.
1284e1244c69SJed Brown 
1285e1244c69SJed Brown    Logically Collective
1286e1244c69SJed Brown 
1287e1244c69SJed Brown    Input Arguments:
1288e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1289e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1290e1244c69SJed Brown 
1291e1244c69SJed Brown    Level: intermediate
1292e1244c69SJed Brown 
1293e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1294e1244c69SJed Brown @*/
1295e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1296e1244c69SJed Brown {
1297e1244c69SJed Brown   PetscFunctionBegin;
1298e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1299e1244c69SJed Brown   PetscFunctionReturn(0);
1300e1244c69SJed Brown }
1301e1244c69SJed Brown 
1302e1244c69SJed Brown #undef __FUNCT__
130355849f57SBarry Smith #define __FUNCT__ "TSLoad"
130455849f57SBarry Smith /*@C
130555849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
130655849f57SBarry Smith 
130755849f57SBarry Smith   Collective on PetscViewer
130855849f57SBarry Smith 
130955849f57SBarry Smith   Input Parameters:
131055849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
131155849f57SBarry Smith            some related function before a call to TSLoad().
131255849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
131355849f57SBarry Smith 
131455849f57SBarry Smith    Level: intermediate
131555849f57SBarry Smith 
131655849f57SBarry Smith   Notes:
131755849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
131855849f57SBarry Smith 
131955849f57SBarry Smith   Notes for advanced users:
132055849f57SBarry Smith   Most users should not need to know the details of the binary storage
132155849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
132255849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
132355849f57SBarry Smith   format is
132455849f57SBarry Smith .vb
132555849f57SBarry Smith      has not yet been determined
132655849f57SBarry Smith .ve
132755849f57SBarry Smith 
132855849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
132955849f57SBarry Smith @*/
1330f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
133155849f57SBarry Smith {
133255849f57SBarry Smith   PetscErrorCode ierr;
133355849f57SBarry Smith   PetscBool      isbinary;
133455849f57SBarry Smith   PetscInt       classid;
133555849f57SBarry Smith   char           type[256];
13362d53ad75SBarry Smith   DMTS           sdm;
1337ad6bc421SBarry Smith   DM             dm;
133855849f57SBarry Smith 
133955849f57SBarry Smith   PetscFunctionBegin;
1340f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
134155849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
134255849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
134355849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
134455849f57SBarry Smith 
1345060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
1346ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
1347060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
1348f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1349f2c2a1b9SBarry Smith   if (ts->ops->load) {
1350f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1351f2c2a1b9SBarry Smith   }
1352ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1353ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1354ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1355f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1356f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
13572d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13582d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
135955849f57SBarry Smith   PetscFunctionReturn(0);
136055849f57SBarry Smith }
136155849f57SBarry Smith 
13629804daf3SBarry Smith #include <petscdraw.h>
1363e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1364e04113cfSBarry Smith #include <petscviewersaws.h>
1365f05ece33SBarry Smith #endif
136655849f57SBarry Smith #undef __FUNCT__
13674a2ae208SSatish Balay #define __FUNCT__ "TSView"
13687e2c5f70SBarry Smith /*@C
1369d763cef2SBarry Smith     TSView - Prints the TS data structure.
1370d763cef2SBarry Smith 
13714c49b128SBarry Smith     Collective on TS
1372d763cef2SBarry Smith 
1373d763cef2SBarry Smith     Input Parameters:
1374d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1375d763cef2SBarry Smith -   viewer - visualization context
1376d763cef2SBarry Smith 
1377d763cef2SBarry Smith     Options Database Key:
1378d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1379d763cef2SBarry Smith 
1380d763cef2SBarry Smith     Notes:
1381d763cef2SBarry Smith     The available visualization contexts include
1382b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1383b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1384d763cef2SBarry Smith          output where only the first processor opens
1385d763cef2SBarry Smith          the file.  All other processors send their
1386d763cef2SBarry Smith          data to the first processor to print.
1387d763cef2SBarry Smith 
1388d763cef2SBarry Smith     The user can open an alternative visualization context with
1389b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1390d763cef2SBarry Smith 
1391d763cef2SBarry Smith     Level: beginner
1392d763cef2SBarry Smith 
1393d763cef2SBarry Smith .keywords: TS, timestep, view
1394d763cef2SBarry Smith 
1395b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1396d763cef2SBarry Smith @*/
13977087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1398d763cef2SBarry Smith {
1399dfbe8321SBarry Smith   PetscErrorCode ierr;
140019fd82e9SBarry Smith   TSType         type;
14012b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
14022d53ad75SBarry Smith   DMTS           sdm;
1403e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1404536b137fSBarry Smith   PetscBool      issaws;
1405f05ece33SBarry Smith #endif
1406d763cef2SBarry Smith 
1407d763cef2SBarry Smith   PetscFunctionBegin;
14080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14093050cee2SBarry Smith   if (!viewer) {
1410ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
14113050cee2SBarry Smith   }
14120700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1413c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1414fd16b177SBarry Smith 
1415251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1416251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
141755849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
14182b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1419e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1420536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1421f05ece33SBarry Smith #endif
142232077d6dSBarry Smith   if (iascii) {
1423dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
142477431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
14257c8652ddSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1426d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
14275ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1428c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1429d763cef2SBarry Smith     }
14305ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1431193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
14322d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14332d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1434d52bd9f3SBarry Smith     if (ts->ops->view) {
1435d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1436d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1437d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1438d52bd9f3SBarry Smith     }
14390f5bd95cSBarry Smith   } else if (isstring) {
1440a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1441b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
144255849f57SBarry Smith   } else if (isbinary) {
144355849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
144455849f57SBarry Smith     MPI_Comm    comm;
144555849f57SBarry Smith     PetscMPIInt rank;
144655849f57SBarry Smith     char        type[256];
144755849f57SBarry Smith 
144855849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
144955849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
145055849f57SBarry Smith     if (!rank) {
145155849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
145255849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
145355849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
145455849f57SBarry Smith     }
145555849f57SBarry Smith     if (ts->ops->view) {
145655849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
145755849f57SBarry Smith     }
1458f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1459f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
14602d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14612d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
14622b0a91c0SBarry Smith   } else if (isdraw) {
14632b0a91c0SBarry Smith     PetscDraw draw;
14642b0a91c0SBarry Smith     char      str[36];
146589fd9fafSBarry Smith     PetscReal x,y,bottom,h;
14662b0a91c0SBarry Smith 
14672b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
14682b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
14692b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
14702b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
147151fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
147289fd9fafSBarry Smith     bottom = y - h;
14732b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
14742b0a91c0SBarry Smith     if (ts->ops->view) {
14752b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14762b0a91c0SBarry Smith     }
14772b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1478e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1479536b137fSBarry Smith   } else if (issaws) {
1480d45a07a7SBarry Smith     PetscMPIInt rank;
14812657e9d9SBarry Smith     const char  *name;
14822657e9d9SBarry Smith 
14832657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1484d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1485d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1486d45a07a7SBarry Smith       char       dir[1024];
1487d45a07a7SBarry Smith 
1488e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
1489a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
14902657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
14912657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
14922657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
1493d763cef2SBarry Smith     }
14940acecf5bSBarry Smith     if (ts->ops->view) {
14950acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14960acecf5bSBarry Smith     }
1497f05ece33SBarry Smith #endif
1498f05ece33SBarry Smith   }
1499f05ece33SBarry Smith 
1500b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1501251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1502b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1503d763cef2SBarry Smith   PetscFunctionReturn(0);
1504d763cef2SBarry Smith }
1505d763cef2SBarry Smith 
1506d763cef2SBarry Smith 
15074a2ae208SSatish Balay #undef __FUNCT__
15084a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1509b07ff414SBarry Smith /*@
1510d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1511d763cef2SBarry Smith    the timesteppers.
1512d763cef2SBarry Smith 
15133f9fe445SBarry Smith    Logically Collective on TS
1514d763cef2SBarry Smith 
1515d763cef2SBarry Smith    Input Parameters:
1516d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1517d763cef2SBarry Smith -  usrP - optional user context
1518d763cef2SBarry Smith 
1519d763cef2SBarry Smith    Level: intermediate
1520d763cef2SBarry Smith 
1521d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1522d763cef2SBarry Smith 
1523d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1524d763cef2SBarry Smith @*/
15257087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1526d763cef2SBarry Smith {
1527d763cef2SBarry Smith   PetscFunctionBegin;
15280700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1529d763cef2SBarry Smith   ts->user = usrP;
1530d763cef2SBarry Smith   PetscFunctionReturn(0);
1531d763cef2SBarry Smith }
1532d763cef2SBarry Smith 
15334a2ae208SSatish Balay #undef __FUNCT__
15344a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1535b07ff414SBarry Smith /*@
1536d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1537d763cef2SBarry Smith     timestepper.
1538d763cef2SBarry Smith 
1539d763cef2SBarry Smith     Not Collective
1540d763cef2SBarry Smith 
1541d763cef2SBarry Smith     Input Parameter:
1542d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1543d763cef2SBarry Smith 
1544d763cef2SBarry Smith     Output Parameter:
1545d763cef2SBarry Smith .   usrP - user context
1546d763cef2SBarry Smith 
1547d763cef2SBarry Smith     Level: intermediate
1548d763cef2SBarry Smith 
1549d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1550d763cef2SBarry Smith 
1551d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1552d763cef2SBarry Smith @*/
1553e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1554d763cef2SBarry Smith {
1555d763cef2SBarry Smith   PetscFunctionBegin;
15560700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1557e71120c6SJed Brown   *(void**)usrP = ts->user;
1558d763cef2SBarry Smith   PetscFunctionReturn(0);
1559d763cef2SBarry Smith }
1560d763cef2SBarry Smith 
15614a2ae208SSatish Balay #undef __FUNCT__
15624a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1563d763cef2SBarry Smith /*@
1564b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1565d763cef2SBarry Smith 
1566d763cef2SBarry Smith    Not Collective
1567d763cef2SBarry Smith 
1568d763cef2SBarry Smith    Input Parameter:
1569d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1570d763cef2SBarry Smith 
1571d763cef2SBarry Smith    Output Parameter:
1572b8123daeSJed Brown .  iter - number of steps completed so far
1573d763cef2SBarry Smith 
1574d763cef2SBarry Smith    Level: intermediate
1575d763cef2SBarry Smith 
1576d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
15779be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
1578d763cef2SBarry Smith @*/
15797087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1580d763cef2SBarry Smith {
1581d763cef2SBarry Smith   PetscFunctionBegin;
15820700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15834482741eSBarry Smith   PetscValidIntPointer(iter,2);
1584d763cef2SBarry Smith   *iter = ts->steps;
1585d763cef2SBarry Smith   PetscFunctionReturn(0);
1586d763cef2SBarry Smith }
1587d763cef2SBarry Smith 
15884a2ae208SSatish Balay #undef __FUNCT__
15894a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1590d763cef2SBarry Smith /*@
1591d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1592d763cef2SBarry Smith    as well as the initial time.
1593d763cef2SBarry Smith 
15943f9fe445SBarry Smith    Logically Collective on TS
1595d763cef2SBarry Smith 
1596d763cef2SBarry Smith    Input Parameters:
1597d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1598d763cef2SBarry Smith .  initial_time - the initial time
1599d763cef2SBarry Smith -  time_step - the size of the timestep
1600d763cef2SBarry Smith 
1601d763cef2SBarry Smith    Level: intermediate
1602d763cef2SBarry Smith 
1603d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1604d763cef2SBarry Smith 
1605d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1606d763cef2SBarry Smith @*/
16077087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1608d763cef2SBarry Smith {
1609e144a568SJed Brown   PetscErrorCode ierr;
1610e144a568SJed Brown 
1611d763cef2SBarry Smith   PetscFunctionBegin;
16120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1613e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1614d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1615d763cef2SBarry Smith   PetscFunctionReturn(0);
1616d763cef2SBarry Smith }
1617d763cef2SBarry Smith 
16184a2ae208SSatish Balay #undef __FUNCT__
16194a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1620d763cef2SBarry Smith /*@
1621d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1622d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1623d763cef2SBarry Smith 
16243f9fe445SBarry Smith    Logically Collective on TS
1625d763cef2SBarry Smith 
1626d763cef2SBarry Smith    Input Parameters:
1627d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1628d763cef2SBarry Smith -  time_step - the size of the timestep
1629d763cef2SBarry Smith 
1630d763cef2SBarry Smith    Level: intermediate
1631d763cef2SBarry Smith 
1632d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1633d763cef2SBarry Smith 
1634d763cef2SBarry Smith .keywords: TS, set, timestep
1635d763cef2SBarry Smith @*/
16367087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1637d763cef2SBarry Smith {
1638d763cef2SBarry Smith   PetscFunctionBegin;
16390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1640c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1641d763cef2SBarry Smith   ts->time_step      = time_step;
164231748224SBarry Smith   ts->time_step_orig = time_step;
1643d763cef2SBarry Smith   PetscFunctionReturn(0);
1644d763cef2SBarry Smith }
1645d763cef2SBarry Smith 
16464a2ae208SSatish Balay #undef __FUNCT__
1647a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1648a43b19c4SJed Brown /*@
164949354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
165049354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
165149354f04SShri Abhyankar      or just return at the final time TS computed.
1652a43b19c4SJed Brown 
1653a43b19c4SJed Brown   Logically Collective on TS
1654a43b19c4SJed Brown 
1655a43b19c4SJed Brown    Input Parameter:
1656a43b19c4SJed Brown +   ts - the time-step context
165749354f04SShri Abhyankar -   eftopt - exact final time option
1658a43b19c4SJed Brown 
1659a43b19c4SJed Brown    Level: beginner
1660a43b19c4SJed Brown 
1661a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1662a43b19c4SJed Brown @*/
166349354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1664a43b19c4SJed Brown {
1665a43b19c4SJed Brown   PetscFunctionBegin;
1666a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
166749354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
166849354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1669a43b19c4SJed Brown   PetscFunctionReturn(0);
1670a43b19c4SJed Brown }
1671a43b19c4SJed Brown 
1672a43b19c4SJed Brown #undef __FUNCT__
16734a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1674d763cef2SBarry Smith /*@
1675d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1676d763cef2SBarry Smith 
1677d763cef2SBarry Smith    Not Collective
1678d763cef2SBarry Smith 
1679d763cef2SBarry Smith    Input Parameter:
1680d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1681d763cef2SBarry Smith 
1682d763cef2SBarry Smith    Output Parameter:
1683d763cef2SBarry Smith .  dt - the current timestep size
1684d763cef2SBarry Smith 
1685d763cef2SBarry Smith    Level: intermediate
1686d763cef2SBarry Smith 
1687d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1688d763cef2SBarry Smith 
1689d763cef2SBarry Smith .keywords: TS, get, timestep
1690d763cef2SBarry Smith @*/
16917087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1692d763cef2SBarry Smith {
1693d763cef2SBarry Smith   PetscFunctionBegin;
16940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1695f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1696d763cef2SBarry Smith   *dt = ts->time_step;
1697d763cef2SBarry Smith   PetscFunctionReturn(0);
1698d763cef2SBarry Smith }
1699d763cef2SBarry Smith 
17004a2ae208SSatish Balay #undef __FUNCT__
17014a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1702d8e5e3e6SSatish Balay /*@
1703d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1704d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1705d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1706d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1707d763cef2SBarry Smith 
1708d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1709d763cef2SBarry Smith 
1710d763cef2SBarry Smith    Input Parameter:
1711d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1712d763cef2SBarry Smith 
1713d763cef2SBarry Smith    Output Parameter:
1714d763cef2SBarry Smith .  v - the vector containing the solution
1715d763cef2SBarry Smith 
1716d763cef2SBarry Smith    Level: intermediate
1717d763cef2SBarry Smith 
1718d763cef2SBarry Smith .seealso: TSGetTimeStep()
1719d763cef2SBarry Smith 
1720d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1721d763cef2SBarry Smith @*/
17227087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1723d763cef2SBarry Smith {
1724d763cef2SBarry Smith   PetscFunctionBegin;
17250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17264482741eSBarry Smith   PetscValidPointer(v,2);
17278737fe31SLisandro Dalcin   *v = ts->vec_sol;
1728d763cef2SBarry Smith   PetscFunctionReturn(0);
1729d763cef2SBarry Smith }
1730d763cef2SBarry Smith 
1731c235aa8dSHong Zhang #undef __FUNCT__
1732dfb21088SHong Zhang #define __FUNCT__ "TSGetCostGradients"
1733c235aa8dSHong Zhang /*@
1734dfb21088SHong Zhang    TSGetCostGradients - Returns the gradients from the TSAdjointSolve()
1735c235aa8dSHong Zhang 
1736c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
1737c235aa8dSHong Zhang 
1738c235aa8dSHong Zhang    Input Parameter:
1739c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
1740c235aa8dSHong Zhang 
1741c235aa8dSHong Zhang    Output Parameter:
1742abc2977eSBarry Smith +  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
1743abc2977eSBarry Smith -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
1744c235aa8dSHong Zhang 
1745c235aa8dSHong Zhang    Level: intermediate
1746c235aa8dSHong Zhang 
1747c235aa8dSHong Zhang .seealso: TSGetTimeStep()
1748c235aa8dSHong Zhang 
1749c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
1750c235aa8dSHong Zhang @*/
1751dfb21088SHong Zhang PetscErrorCode  TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu)
1752c235aa8dSHong Zhang {
1753c235aa8dSHong Zhang   PetscFunctionBegin;
1754c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1755abc2977eSBarry Smith   if (numcost) *numcost = ts->numcost;
1756abc2977eSBarry Smith   if (lambda)  *lambda  = ts->vecs_sensi;
1757abc2977eSBarry Smith   if (mu)      *mu      = ts->vecs_sensip;
1758c235aa8dSHong Zhang   PetscFunctionReturn(0);
1759c235aa8dSHong Zhang }
1760c235aa8dSHong Zhang 
1761bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
17624a2ae208SSatish Balay #undef __FUNCT__
1763bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1764d8e5e3e6SSatish Balay /*@
1765bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1766d763cef2SBarry Smith 
1767bdad233fSMatthew Knepley   Not collective
1768d763cef2SBarry Smith 
1769bdad233fSMatthew Knepley   Input Parameters:
1770bdad233fSMatthew Knepley + ts   - The TS
1771bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1772d763cef2SBarry Smith .vb
17730910c330SBarry Smith          U_t - A U = 0      (linear)
17740910c330SBarry Smith          U_t - A(t) U = 0   (linear)
17750910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1776d763cef2SBarry Smith .ve
1777d763cef2SBarry Smith 
1778d763cef2SBarry Smith    Level: beginner
1779d763cef2SBarry Smith 
1780bdad233fSMatthew Knepley .keywords: TS, problem type
1781bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1782d763cef2SBarry Smith @*/
17837087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1784a7cc72afSBarry Smith {
17859e2a6581SJed Brown   PetscErrorCode ierr;
17869e2a6581SJed Brown 
1787d763cef2SBarry Smith   PetscFunctionBegin;
17880700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1789bdad233fSMatthew Knepley   ts->problem_type = type;
17909e2a6581SJed Brown   if (type == TS_LINEAR) {
17919e2a6581SJed Brown     SNES snes;
17929e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
17939e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
17949e2a6581SJed Brown   }
1795d763cef2SBarry Smith   PetscFunctionReturn(0);
1796d763cef2SBarry Smith }
1797d763cef2SBarry Smith 
1798bdad233fSMatthew Knepley #undef __FUNCT__
1799bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1800bdad233fSMatthew Knepley /*@C
1801bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1802bdad233fSMatthew Knepley 
1803bdad233fSMatthew Knepley   Not collective
1804bdad233fSMatthew Knepley 
1805bdad233fSMatthew Knepley   Input Parameter:
1806bdad233fSMatthew Knepley . ts   - The TS
1807bdad233fSMatthew Knepley 
1808bdad233fSMatthew Knepley   Output Parameter:
1809bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1810bdad233fSMatthew Knepley .vb
1811089b2837SJed Brown          M U_t = A U
1812089b2837SJed Brown          M(t) U_t = A(t) U
1813b5abc632SBarry Smith          F(t,U,U_t)
1814bdad233fSMatthew Knepley .ve
1815bdad233fSMatthew Knepley 
1816bdad233fSMatthew Knepley    Level: beginner
1817bdad233fSMatthew Knepley 
1818bdad233fSMatthew Knepley .keywords: TS, problem type
1819bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1820bdad233fSMatthew Knepley @*/
18217087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1822a7cc72afSBarry Smith {
1823bdad233fSMatthew Knepley   PetscFunctionBegin;
18240700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
18254482741eSBarry Smith   PetscValidIntPointer(type,2);
1826bdad233fSMatthew Knepley   *type = ts->problem_type;
1827bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1828bdad233fSMatthew Knepley }
1829d763cef2SBarry Smith 
18304a2ae208SSatish Balay #undef __FUNCT__
18314a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1832d763cef2SBarry Smith /*@
1833d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1834d763cef2SBarry Smith    of a timestepper.
1835d763cef2SBarry Smith 
1836d763cef2SBarry Smith    Collective on TS
1837d763cef2SBarry Smith 
1838d763cef2SBarry Smith    Input Parameter:
1839d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1840d763cef2SBarry Smith 
1841d763cef2SBarry Smith    Notes:
1842d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1843d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1844d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1845d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1846d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1847d763cef2SBarry Smith 
1848d763cef2SBarry Smith    Level: advanced
1849d763cef2SBarry Smith 
1850d763cef2SBarry Smith .keywords: TS, timestep, setup
1851d763cef2SBarry Smith 
1852d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1853d763cef2SBarry Smith @*/
18547087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1855d763cef2SBarry Smith {
1856dfbe8321SBarry Smith   PetscErrorCode ierr;
18576c6b9e74SPeter Brune   DM             dm;
18586c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
1859d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
18606c6b9e74SPeter Brune   TSIJacobian    ijac;
18616c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1862d763cef2SBarry Smith 
1863d763cef2SBarry Smith   PetscFunctionBegin;
18640700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1865277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1866277b19d0SLisandro Dalcin 
18672c18e0fdSBarry Smith   ts->total_steps = 0;
18687adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
18699596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1870d763cef2SBarry Smith   }
1871277b19d0SLisandro Dalcin 
1872277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1873277b19d0SLisandro Dalcin 
18741c3436cfSJed Brown 
1875552698daSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
1876277b19d0SLisandro Dalcin 
1877e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
1878e1244c69SJed Brown     Mat Amat,Pmat;
1879e1244c69SJed Brown     SNES snes;
1880e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1881e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
1882e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
1883e1244c69SJed Brown      * have displaced the RHS matrix */
1884e1244c69SJed Brown     if (Amat == ts->Arhs) {
1885e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
1886e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
1887e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
1888e1244c69SJed Brown     }
1889e1244c69SJed Brown     if (Pmat == ts->Brhs) {
1890e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
1891e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
1892e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
1893e1244c69SJed Brown     }
1894e1244c69SJed Brown   }
1895277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1896000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1897277b19d0SLisandro Dalcin   }
1898277b19d0SLisandro Dalcin 
18996c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
19006c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
19016c6b9e74SPeter Brune    */
19026c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
19030298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
19046c6b9e74SPeter Brune   if (!func) {
19056c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
19066c6b9e74SPeter Brune   }
19076c6b9e74SPeter Brune   /* if the SNES doesn't have a jacobian set and the TS has an ijacobian or rhsjacobian set, set the SNES to use it.
19086c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
19096c6b9e74SPeter Brune    */
19100298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
19110298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
19120298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
19136c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
19146c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
19156c6b9e74SPeter Brune   }
1916277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1917277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1918277b19d0SLisandro Dalcin }
1919277b19d0SLisandro Dalcin 
1920277b19d0SLisandro Dalcin #undef __FUNCT__
1921f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp"
1922f6a906c0SBarry Smith /*@
1923f6a906c0SBarry Smith    TSAdjointSetUp - Sets up the internal data structures for the later use
1924f6a906c0SBarry Smith    of an adjoint solver
1925f6a906c0SBarry Smith 
1926f6a906c0SBarry Smith    Collective on TS
1927f6a906c0SBarry Smith 
1928f6a906c0SBarry Smith    Input Parameter:
1929f6a906c0SBarry Smith .  ts - the TS context obtained from TSCreate()
1930f6a906c0SBarry Smith 
1931f6a906c0SBarry Smith    Level: advanced
1932f6a906c0SBarry Smith 
1933f6a906c0SBarry Smith .keywords: TS, timestep, setup
1934f6a906c0SBarry Smith 
1935947abb85SHong Zhang .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients()
1936f6a906c0SBarry Smith @*/
1937f6a906c0SBarry Smith PetscErrorCode  TSAdjointSetUp(TS ts)
1938f6a906c0SBarry Smith {
1939f6a906c0SBarry Smith   PetscErrorCode ierr;
1940f6a906c0SBarry Smith 
1941f6a906c0SBarry Smith   PetscFunctionBegin;
1942f6a906c0SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1943f6a906c0SBarry Smith   if (ts->adjointsetupcalled) PetscFunctionReturn(0);
1944dfb21088SHong Zhang   if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first");
1945d8151eeaSBarry Smith 
1946947abb85SHong Zhang   if (ts->vec_costintegral) { /* if there is integral in the cost function*/
1947d8151eeaSBarry Smith     ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
1948d8151eeaSBarry Smith     if (ts->vecs_sensip){
1949d8151eeaSBarry Smith       ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
1950d8151eeaSBarry Smith     }
1951947abb85SHong Zhang   }
1952d8151eeaSBarry Smith 
195342f2b339SBarry Smith   if (ts->ops->adjointsetup) {
195442f2b339SBarry Smith     ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr);
1955f6a906c0SBarry Smith   }
1956f6a906c0SBarry Smith   ts->adjointsetupcalled = PETSC_TRUE;
1957f6a906c0SBarry Smith   PetscFunctionReturn(0);
1958f6a906c0SBarry Smith }
1959f6a906c0SBarry Smith 
1960f6a906c0SBarry Smith #undef __FUNCT__
1961277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1962277b19d0SLisandro Dalcin /*@
1963277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1964277b19d0SLisandro Dalcin 
1965277b19d0SLisandro Dalcin    Collective on TS
1966277b19d0SLisandro Dalcin 
1967277b19d0SLisandro Dalcin    Input Parameter:
1968277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1969277b19d0SLisandro Dalcin 
1970277b19d0SLisandro Dalcin    Level: beginner
1971277b19d0SLisandro Dalcin 
1972277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1973277b19d0SLisandro Dalcin 
1974277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1975277b19d0SLisandro Dalcin @*/
1976277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1977277b19d0SLisandro Dalcin {
1978277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1979277b19d0SLisandro Dalcin 
1980277b19d0SLisandro Dalcin   PetscFunctionBegin;
1981277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1982b18ea86cSHong Zhang 
1983277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1984277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1985277b19d0SLisandro Dalcin   }
1986277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1987e27a82bcSLisandro Dalcin   if (ts->adapt) {ierr = TSAdaptReset(ts->adapt);CHKERRQ(ierr);}
1988bbd56ea5SKarl Rupp 
19894e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
19904e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1991214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
19926bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1993e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1994e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
199538637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1996bbd56ea5SKarl Rupp 
1997947abb85SHong Zhang  if (ts->vec_costintegral) {
1998d8151eeaSBarry Smith     ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
1999d8151eeaSBarry Smith     if (ts->vecs_drdp){
2000d8151eeaSBarry Smith       ierr = VecDestroyVecs(ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
2001d8151eeaSBarry Smith     }
2002947abb85SHong Zhang   }
2003d8151eeaSBarry Smith   ts->vecs_sensi  = NULL;
2004d8151eeaSBarry Smith   ts->vecs_sensip = NULL;
2005ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
20062c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
200736eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2008277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2009d763cef2SBarry Smith   PetscFunctionReturn(0);
2010d763cef2SBarry Smith }
2011d763cef2SBarry Smith 
20124a2ae208SSatish Balay #undef __FUNCT__
20134a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
2014d8e5e3e6SSatish Balay /*@
2015d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2016d763cef2SBarry Smith    with TSCreate().
2017d763cef2SBarry Smith 
2018d763cef2SBarry Smith    Collective on TS
2019d763cef2SBarry Smith 
2020d763cef2SBarry Smith    Input Parameter:
2021d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2022d763cef2SBarry Smith 
2023d763cef2SBarry Smith    Level: beginner
2024d763cef2SBarry Smith 
2025d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2026d763cef2SBarry Smith 
2027d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2028d763cef2SBarry Smith @*/
20296bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2030d763cef2SBarry Smith {
20316849ba73SBarry Smith   PetscErrorCode ierr;
2032d763cef2SBarry Smith 
2033d763cef2SBarry Smith   PetscFunctionBegin;
20346bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
20356bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
20366bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2037d763cef2SBarry Smith 
20386bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2039277b19d0SLisandro Dalcin 
2040e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2041e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
20426bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
20436d4c513bSLisandro Dalcin 
2044bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2045bc952696SBarry Smith 
204684df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
2047aeb4809dSShri Abhyankar   if ((*ts)->event) {
2048aeb4809dSShri Abhyankar     ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr);
2049aeb4809dSShri Abhyankar   }
20506bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
20516bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
20526bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
20536d4c513bSLisandro Dalcin 
2054a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2055d763cef2SBarry Smith   PetscFunctionReturn(0);
2056d763cef2SBarry Smith }
2057d763cef2SBarry Smith 
20584a2ae208SSatish Balay #undef __FUNCT__
20594a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
2060d8e5e3e6SSatish Balay /*@
2061d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2062d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2063d763cef2SBarry Smith 
2064d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2065d763cef2SBarry Smith 
2066d763cef2SBarry Smith    Input Parameter:
2067d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2068d763cef2SBarry Smith 
2069d763cef2SBarry Smith    Output Parameter:
2070d763cef2SBarry Smith .  snes - the nonlinear solver context
2071d763cef2SBarry Smith 
2072d763cef2SBarry Smith    Notes:
2073d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2074d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
207594b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2076d763cef2SBarry Smith 
2077d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
20780298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2079d763cef2SBarry Smith 
2080d763cef2SBarry Smith    Level: beginner
2081d763cef2SBarry Smith 
2082d763cef2SBarry Smith .keywords: timestep, get, SNES
2083d763cef2SBarry Smith @*/
20847087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2085d763cef2SBarry Smith {
2086d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2087d372ba47SLisandro Dalcin 
2088d763cef2SBarry Smith   PetscFunctionBegin;
20890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20904482741eSBarry Smith   PetscValidPointer(snes,2);
2091d372ba47SLisandro Dalcin   if (!ts->snes) {
2092ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
20930298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
20943bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2095d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2096496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
20979e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
20989e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
20999e2a6581SJed Brown     }
2100d372ba47SLisandro Dalcin   }
2101d763cef2SBarry Smith   *snes = ts->snes;
2102d763cef2SBarry Smith   PetscFunctionReturn(0);
2103d763cef2SBarry Smith }
2104d763cef2SBarry Smith 
21054a2ae208SSatish Balay #undef __FUNCT__
2106deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
2107deb2cd25SJed Brown /*@
2108deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2109deb2cd25SJed Brown 
2110deb2cd25SJed Brown    Collective
2111deb2cd25SJed Brown 
2112deb2cd25SJed Brown    Input Parameter:
2113deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2114deb2cd25SJed Brown -  snes - the nonlinear solver context
2115deb2cd25SJed Brown 
2116deb2cd25SJed Brown    Notes:
2117deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2118deb2cd25SJed Brown 
2119deb2cd25SJed Brown    Level: developer
2120deb2cd25SJed Brown 
2121deb2cd25SJed Brown .keywords: timestep, set, SNES
2122deb2cd25SJed Brown @*/
2123deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2124deb2cd25SJed Brown {
2125deb2cd25SJed Brown   PetscErrorCode ierr;
2126d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2127deb2cd25SJed Brown 
2128deb2cd25SJed Brown   PetscFunctionBegin;
2129deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2130deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2131deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2132deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2133bbd56ea5SKarl Rupp 
2134deb2cd25SJed Brown   ts->snes = snes;
2135bbd56ea5SKarl Rupp 
21360298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
21370298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2138740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
21390298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2140740132f1SEmil Constantinescu   }
2141deb2cd25SJed Brown   PetscFunctionReturn(0);
2142deb2cd25SJed Brown }
2143deb2cd25SJed Brown 
2144deb2cd25SJed Brown #undef __FUNCT__
214594b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
2146d8e5e3e6SSatish Balay /*@
214794b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2148d763cef2SBarry Smith    a TS (timestepper) context.
2149d763cef2SBarry Smith 
215094b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2151d763cef2SBarry Smith 
2152d763cef2SBarry Smith    Input Parameter:
2153d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2154d763cef2SBarry Smith 
2155d763cef2SBarry Smith    Output Parameter:
215694b7f48cSBarry Smith .  ksp - the nonlinear solver context
2157d763cef2SBarry Smith 
2158d763cef2SBarry Smith    Notes:
215994b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2160d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2161d763cef2SBarry Smith    KSP and PC contexts as well.
2162d763cef2SBarry Smith 
216394b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
21640298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2165d763cef2SBarry Smith 
2166d763cef2SBarry Smith    Level: beginner
2167d763cef2SBarry Smith 
216894b7f48cSBarry Smith .keywords: timestep, get, KSP
2169d763cef2SBarry Smith @*/
21707087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2171d763cef2SBarry Smith {
2172d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2173089b2837SJed Brown   SNES           snes;
2174d372ba47SLisandro Dalcin 
2175d763cef2SBarry Smith   PetscFunctionBegin;
21760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21774482741eSBarry Smith   PetscValidPointer(ksp,2);
217817186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2179e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2180089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2181089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2182d763cef2SBarry Smith   PetscFunctionReturn(0);
2183d763cef2SBarry Smith }
2184d763cef2SBarry Smith 
2185d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2186d763cef2SBarry Smith 
21874a2ae208SSatish Balay #undef __FUNCT__
2188adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
2189adb62b0dSMatthew Knepley /*@
2190adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2191adb62b0dSMatthew Knepley    maximum time for iteration.
2192adb62b0dSMatthew Knepley 
21933f9fe445SBarry Smith    Not Collective
2194adb62b0dSMatthew Knepley 
2195adb62b0dSMatthew Knepley    Input Parameters:
2196adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
21970298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
21980298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2199adb62b0dSMatthew Knepley 
2200adb62b0dSMatthew Knepley    Level: intermediate
2201adb62b0dSMatthew Knepley 
2202adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2203adb62b0dSMatthew Knepley @*/
22047087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2205adb62b0dSMatthew Knepley {
2206adb62b0dSMatthew Knepley   PetscFunctionBegin;
22070700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2208abc0a331SBarry Smith   if (maxsteps) {
22094482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2210adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2211adb62b0dSMatthew Knepley   }
2212abc0a331SBarry Smith   if (maxtime) {
22134482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2214adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2215adb62b0dSMatthew Knepley   }
2216adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2217adb62b0dSMatthew Knepley }
2218adb62b0dSMatthew Knepley 
2219adb62b0dSMatthew Knepley #undef __FUNCT__
22204a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
2221d763cef2SBarry Smith /*@
2222d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2223d763cef2SBarry Smith    maximum time for iteration.
2224d763cef2SBarry Smith 
22253f9fe445SBarry Smith    Logically Collective on TS
2226d763cef2SBarry Smith 
2227d763cef2SBarry Smith    Input Parameters:
2228d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2229d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2230d763cef2SBarry Smith -  maxtime - final time to iterate to
2231d763cef2SBarry Smith 
2232d763cef2SBarry Smith    Options Database Keys:
2233d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
22343bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2235d763cef2SBarry Smith 
2236d763cef2SBarry Smith    Notes:
2237d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2238d763cef2SBarry Smith 
2239d763cef2SBarry Smith    Level: intermediate
2240d763cef2SBarry Smith 
2241d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2242a43b19c4SJed Brown 
2243a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2244d763cef2SBarry Smith @*/
22457087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2246d763cef2SBarry Smith {
2247d763cef2SBarry Smith   PetscFunctionBegin;
22480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2249c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2250c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
225139b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
225239b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2253d763cef2SBarry Smith   PetscFunctionReturn(0);
2254d763cef2SBarry Smith }
2255d763cef2SBarry Smith 
22564a2ae208SSatish Balay #undef __FUNCT__
22574a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2258d763cef2SBarry Smith /*@
2259d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2260d763cef2SBarry Smith    for use by the TS routines.
2261d763cef2SBarry Smith 
22623f9fe445SBarry Smith    Logically Collective on TS and Vec
2263d763cef2SBarry Smith 
2264d763cef2SBarry Smith    Input Parameters:
2265d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
22660910c330SBarry Smith -  u - the solution vector
2267d763cef2SBarry Smith 
2268d763cef2SBarry Smith    Level: beginner
2269d763cef2SBarry Smith 
2270d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2271d763cef2SBarry Smith @*/
22720910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2273d763cef2SBarry Smith {
22748737fe31SLisandro Dalcin   PetscErrorCode ierr;
2275496e6a7aSJed Brown   DM             dm;
22768737fe31SLisandro Dalcin 
2277d763cef2SBarry Smith   PetscFunctionBegin;
22780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22790910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
22800910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
22816bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2282bbd56ea5SKarl Rupp 
22830910c330SBarry Smith   ts->vec_sol = u;
2284bbd56ea5SKarl Rupp 
2285496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
22860910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2287d763cef2SBarry Smith   PetscFunctionReturn(0);
2288d763cef2SBarry Smith }
2289d763cef2SBarry Smith 
2290e74ef692SMatthew Knepley #undef __FUNCT__
229173b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps"
229273b18844SBarry Smith /*@
229373b18844SBarry Smith    TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time
229473b18844SBarry Smith 
229573b18844SBarry Smith    Logically Collective on TS
229673b18844SBarry Smith 
229773b18844SBarry Smith    Input Parameters:
229873b18844SBarry Smith +  ts - the TS context obtained from TSCreate()
229973b18844SBarry Smith .  steps - number of steps to use
230073b18844SBarry Smith 
230173b18844SBarry Smith    Level: intermediate
230273b18844SBarry Smith 
230373b18844SBarry Smith    Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this
230473b18844SBarry Smith           so as to integrate back to less than the original timestep
230573b18844SBarry Smith 
230673b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations
230773b18844SBarry Smith 
230873b18844SBarry Smith .seealso: TSSetExactFinalTime()
230973b18844SBarry Smith @*/
231073b18844SBarry Smith PetscErrorCode  TSAdjointSetSteps(TS ts,PetscInt steps)
231173b18844SBarry Smith {
231273b18844SBarry Smith   PetscFunctionBegin;
231373b18844SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
231473b18844SBarry Smith   PetscValidLogicalCollectiveInt(ts,steps,2);
231573b18844SBarry Smith   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps");
231673b18844SBarry Smith   if (steps > ts->total_steps) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back more than the total number of forward steps");
231773b18844SBarry Smith   ts->adjoint_max_steps = steps;
231873b18844SBarry Smith   PetscFunctionReturn(0);
231973b18844SBarry Smith }
232073b18844SBarry Smith 
232173b18844SBarry Smith #undef __FUNCT__
2322dfb21088SHong Zhang #define __FUNCT__ "TSSetCostGradients"
2323c235aa8dSHong Zhang /*@
2324dfb21088SHong Zhang    TSSetCostGradients - Sets the initial value of the gradients of the cost function w.r.t. initial conditions and w.r.t. the problem parameters
23250cf82b59SBarry Smith       for use by the TSAdjoint routines.
2326c235aa8dSHong Zhang 
2327c235aa8dSHong Zhang    Logically Collective on TS and Vec
2328c235aa8dSHong Zhang 
2329c235aa8dSHong Zhang    Input Parameters:
2330c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
2331abc2977eSBarry Smith .  lambda - gradients with respect to the initial condition variables, the dimension and parallel layout of these vectors is the same as the ODE solution vector
2332abc2977eSBarry Smith -  mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters
2333c235aa8dSHong Zhang 
2334c235aa8dSHong Zhang    Level: beginner
2335c235aa8dSHong Zhang 
2336abc2977eSBarry Smith    Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime  mu_i = df/dp|finaltime
23370cf82b59SBarry Smith 
2338c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2339c235aa8dSHong Zhang @*/
2340dfb21088SHong Zhang PetscErrorCode  TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu)
2341c235aa8dSHong Zhang {
2342c235aa8dSHong Zhang   PetscFunctionBegin;
2343c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2344abc2977eSBarry Smith   PetscValidPointer(lambda,2);
2345abc2977eSBarry Smith   ts->vecs_sensi  = lambda;
2346abc2977eSBarry Smith   ts->vecs_sensip = mu;
2347dfb21088SHong Zhang   if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostIntegrand");
2348abc2977eSBarry Smith   ts->numcost  = numcost;
2349ad8e2604SHong Zhang   PetscFunctionReturn(0);
2350ad8e2604SHong Zhang }
2351ad8e2604SHong Zhang 
2352ad8e2604SHong Zhang #undef __FUNCT__
23535bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian"
2354ad8e2604SHong Zhang /*@C
23550cf82b59SBarry Smith   TSAdjointSetRHSJacobian - Sets the function that computes the Jacobian of G w.r.t. the parameters p where y_t = G(y,p,t), as well as the location to store the matrix.
2356ad8e2604SHong Zhang 
2357ad8e2604SHong Zhang   Logically Collective on TS
2358ad8e2604SHong Zhang 
2359ad8e2604SHong Zhang   Input Parameters:
2360ad8e2604SHong Zhang + ts   - The TS context obtained from TSCreate()
2361ad8e2604SHong Zhang - func - The function
2362ad8e2604SHong Zhang 
2363ad8e2604SHong Zhang   Calling sequence of func:
23640cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx);
236505755b9cSHong Zhang +   t - current timestep
23660cf82b59SBarry Smith .   y - input vector (current ODE solution)
236705755b9cSHong Zhang .   A - output matrix
236805755b9cSHong Zhang -   ctx - [optional] user-defined function context
2369ad8e2604SHong Zhang 
2370ad8e2604SHong Zhang   Level: intermediate
2371ad8e2604SHong Zhang 
23720cf82b59SBarry Smith   Notes: Amat has the same number of rows and the same row parallel layout as u, Amat has the same number of columns and parallel layout as p
23730cf82b59SBarry Smith 
2374ad8e2604SHong Zhang .keywords: TS, sensitivity
2375ad8e2604SHong Zhang .seealso:
2376ad8e2604SHong Zhang @*/
23775bf8c567SBarry Smith PetscErrorCode  TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
2378ad8e2604SHong Zhang {
2379ad8e2604SHong Zhang   PetscErrorCode ierr;
2380ad8e2604SHong Zhang 
2381ad8e2604SHong Zhang   PetscFunctionBegin;
2382ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2383ad8e2604SHong Zhang   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
2384ad8e2604SHong Zhang 
2385ad8e2604SHong Zhang   ts->rhsjacobianp    = func;
2386ad8e2604SHong Zhang   ts->rhsjacobianpctx = ctx;
2387ad8e2604SHong Zhang   if(Amat) {
2388ad8e2604SHong Zhang     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
2389ad8e2604SHong Zhang     ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2390ad8e2604SHong Zhang     ts->Jacp = Amat;
2391ad8e2604SHong Zhang   }
2392ad8e2604SHong Zhang   PetscFunctionReturn(0);
2393ad8e2604SHong Zhang }
2394ad8e2604SHong Zhang 
2395ad8e2604SHong Zhang #undef __FUNCT__
23965bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian"
23970cf82b59SBarry Smith /*@C
23985bf8c567SBarry Smith   TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function.
2399ad8e2604SHong Zhang 
2400ad8e2604SHong Zhang   Collective on TS
2401ad8e2604SHong Zhang 
2402ad8e2604SHong Zhang   Input Parameters:
2403ad8e2604SHong Zhang . ts   - The TS context obtained from TSCreate()
2404ad8e2604SHong Zhang 
2405ad8e2604SHong Zhang   Level: developer
2406ad8e2604SHong Zhang 
2407ad8e2604SHong Zhang .keywords: TS, sensitivity
24085bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian()
2409ad8e2604SHong Zhang @*/
24105bf8c567SBarry Smith PetscErrorCode  TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat)
2411ad8e2604SHong Zhang {
2412ad8e2604SHong Zhang   PetscErrorCode ierr;
2413ad8e2604SHong Zhang 
2414ad8e2604SHong Zhang   PetscFunctionBegin;
2415ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2416ad8e2604SHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
2417ad8e2604SHong Zhang   PetscValidPointer(Amat,4);
2418ad8e2604SHong Zhang 
2419ad8e2604SHong Zhang   PetscStackPush("TS user JacobianP function for sensitivity analysis");
2420ad8e2604SHong Zhang   ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr);
2421ad8e2604SHong Zhang   PetscStackPop;
2422c235aa8dSHong Zhang   PetscFunctionReturn(0);
2423c235aa8dSHong Zhang }
2424c235aa8dSHong Zhang 
2425c235aa8dSHong Zhang #undef __FUNCT__
2426dfb21088SHong Zhang #define __FUNCT__ "TSSetCostIntegrand"
24276fd0887fSHong Zhang /*@C
2428dfb21088SHong Zhang     TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions
24296fd0887fSHong Zhang 
24306fd0887fSHong Zhang     Logically Collective on TS
24316fd0887fSHong Zhang 
24326fd0887fSHong Zhang     Input Parameters:
24336fd0887fSHong Zhang +   ts - the TS context obtained from TSCreate()
2434abc2977eSBarry Smith .   numcost - number of gradients to be computed, this is the number of cost functions
24350cf82b59SBarry Smith .   rf - routine for evaluating the integrand function
2436b612ec54SBarry Smith .   drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y
2437b612ec54SBarry Smith .   drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p
2438b612ec54SBarry Smith -   ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL)
24396fd0887fSHong Zhang 
24400cf82b59SBarry Smith     Calling sequence of rf:
24410cf82b59SBarry Smith $     rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx);
24426fd0887fSHong Zhang 
24436fd0887fSHong Zhang +   t - current timestep
24440cf82b59SBarry Smith .   y - input vector
24450cf82b59SBarry Smith .   f - function result; one vector entry for each cost function
24466fd0887fSHong Zhang -   ctx - [optional] user-defined function context
24476fd0887fSHong Zhang 
2448b612ec54SBarry Smith    Calling sequence of drdyf:
24490cf82b59SBarry Smith $    PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx);
2450b612ec54SBarry Smith 
2451b612ec54SBarry Smith    Calling sequence of drdpf:
24520cf82b59SBarry Smith $    PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx);
2453b612ec54SBarry Smith 
2454b612ec54SBarry Smith     Level: intermediate
24556fd0887fSHong Zhang 
2456abc2977eSBarry Smith     Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions
24570cf82b59SBarry Smith 
24586fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function
24596fd0887fSHong Zhang 
2460dfb21088SHong Zhang .seealso: TSAdjointSetRHSJacobian(),TSGetCostGradients(), TSSetCostGradients()
24616fd0887fSHong Zhang @*/
2462dfb21088SHong Zhang PetscErrorCode  TSSetCostIntegrand(TS ts,PetscInt numcost, PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*),
2463d8151eeaSBarry Smith                                                                   PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*),
2464d8151eeaSBarry Smith                                                                   PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),void *ctx)
24656fd0887fSHong Zhang {
24666fd0887fSHong Zhang   PetscErrorCode ierr;
24676fd0887fSHong Zhang 
24686fd0887fSHong Zhang   PetscFunctionBegin;
24696fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2470dfb21088SHong Zhang   if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostGradients()");
2471c72ed514SHong Zhang   if (!ts->numcost) ts->numcost=numcost;
24726fd0887fSHong Zhang 
2473abc2977eSBarry Smith   ierr                 = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr);
24742c39e106SBarry Smith   ierr                 = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr);
24750cf82b59SBarry Smith   ts->costintegrand    = rf;
247605755b9cSHong Zhang   ts->costintegrandctx = ctx;
2477b612ec54SBarry Smith   ts->drdyfunction     = drdyf;
2478b612ec54SBarry Smith   ts->drdpfunction     = drdpf;
24796fd0887fSHong Zhang   PetscFunctionReturn(0);
24806fd0887fSHong Zhang }
24816fd0887fSHong Zhang 
24826fd0887fSHong Zhang #undef __FUNCT__
2483dfb21088SHong Zhang #define __FUNCT__ "TSGetCostIntegral"
248436eaed60SHong Zhang /*@
2485dfb21088SHong Zhang    TSGetCostIntegral - Returns the values of the integral term in the cost functions.
248636eaed60SHong Zhang    It is valid to call the routine after a backward run.
248736eaed60SHong Zhang 
248836eaed60SHong Zhang    Not Collective
248936eaed60SHong Zhang 
249036eaed60SHong Zhang    Input Parameter:
249136eaed60SHong Zhang .  ts - the TS context obtained from TSCreate()
249236eaed60SHong Zhang 
249336eaed60SHong Zhang    Output Parameter:
24942c39e106SBarry Smith .  v - the vector containing the integrals for each cost function
249536eaed60SHong Zhang 
249636eaed60SHong Zhang    Level: intermediate
249736eaed60SHong Zhang 
2498dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
249936eaed60SHong Zhang 
250036eaed60SHong Zhang .keywords: TS, sensitivity analysis
250136eaed60SHong Zhang @*/
2502dfb21088SHong Zhang PetscErrorCode  TSGetCostIntegral(TS ts,Vec *v)
250336eaed60SHong Zhang {
250436eaed60SHong Zhang   PetscFunctionBegin;
250536eaed60SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
250636eaed60SHong Zhang   PetscValidPointer(v,2);
25072c39e106SBarry Smith   *v = ts->vec_costintegral;
250836eaed60SHong Zhang   PetscFunctionReturn(0);
250936eaed60SHong Zhang }
251036eaed60SHong Zhang 
251136eaed60SHong Zhang #undef __FUNCT__
2512d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand"
25136fd0887fSHong Zhang /*@
25142c39e106SBarry Smith    TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions.
25156fd0887fSHong Zhang 
25166fd0887fSHong Zhang    Input Parameters:
25176fd0887fSHong Zhang +  ts - the TS context
25186fd0887fSHong Zhang .  t - current time
25190cf82b59SBarry Smith -  y - state vector, i.e. current solution
25206fd0887fSHong Zhang 
25216fd0887fSHong Zhang    Output Parameter:
2522abc2977eSBarry Smith .  q - vector of size numcost to hold the outputs
25236fd0887fSHong Zhang 
25246fd0887fSHong Zhang    Note:
25256fd0887fSHong Zhang    Most users should not need to explicitly call this routine, as it
25266fd0887fSHong Zhang    is used internally within the sensitivity analysis context.
25276fd0887fSHong Zhang 
25286fd0887fSHong Zhang    Level: developer
25296fd0887fSHong Zhang 
25306fd0887fSHong Zhang .keywords: TS, compute
25316fd0887fSHong Zhang 
2532dfb21088SHong Zhang .seealso: TSSetCostIntegrand()
25336fd0887fSHong Zhang @*/
25340cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q)
25356fd0887fSHong Zhang {
25366fd0887fSHong Zhang   PetscErrorCode ierr;
25376fd0887fSHong Zhang 
25386fd0887fSHong Zhang   PetscFunctionBegin;
25396fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25400cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
25416fd0887fSHong Zhang   PetscValidHeaderSpecific(q,VEC_CLASSID,4);
25426fd0887fSHong Zhang 
25430cf82b59SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
2544e4680132SHong Zhang   if (ts->costintegrand) {
2545e4680132SHong Zhang     PetscStackPush("TS user integrand in the cost function");
25460cf82b59SBarry Smith     ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr);
25476fd0887fSHong Zhang     PetscStackPop;
25486fd0887fSHong Zhang   } else {
25496fd0887fSHong Zhang     ierr = VecZeroEntries(q);CHKERRQ(ierr);
25506fd0887fSHong Zhang   }
25516fd0887fSHong Zhang 
25520cf82b59SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
25536fd0887fSHong Zhang   PetscFunctionReturn(0);
25546fd0887fSHong Zhang }
25556fd0887fSHong Zhang 
25566fd0887fSHong Zhang #undef __FUNCT__
2557d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction"
255805755b9cSHong Zhang /*@
2559d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.
256005755b9cSHong Zhang 
256105755b9cSHong Zhang   Collective on TS
256205755b9cSHong Zhang 
256305755b9cSHong Zhang   Input Parameters:
256405755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
256505755b9cSHong Zhang 
256605755b9cSHong Zhang   Notes:
2567d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
256805755b9cSHong Zhang   so most users would not generally call this routine themselves.
256905755b9cSHong Zhang 
257005755b9cSHong Zhang   Level: developer
257105755b9cSHong Zhang 
257205755b9cSHong Zhang .keywords: TS, sensitivity
2573d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction()
257405755b9cSHong Zhang @*/
25750cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy)
257605755b9cSHong Zhang {
257705755b9cSHong Zhang   PetscErrorCode ierr;
257805755b9cSHong Zhang 
257905755b9cSHong Zhang   PetscFunctionBegin;
258005755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25810cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
258205755b9cSHong Zhang 
258305755b9cSHong Zhang   PetscStackPush("TS user DRDY function for sensitivity analysis");
25840cf82b59SBarry Smith   ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr);
258505755b9cSHong Zhang   PetscStackPop;
258605755b9cSHong Zhang   PetscFunctionReturn(0);
258705755b9cSHong Zhang }
258805755b9cSHong Zhang 
258905755b9cSHong Zhang #undef __FUNCT__
2590d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction"
259105755b9cSHong Zhang /*@
2592d4aa0a58SBarry Smith   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function.
259305755b9cSHong Zhang 
259405755b9cSHong Zhang   Collective on TS
259505755b9cSHong Zhang 
259605755b9cSHong Zhang   Input Parameters:
259705755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
259805755b9cSHong Zhang 
259905755b9cSHong Zhang   Notes:
260005755b9cSHong Zhang   TSDRDPFunction() is typically used for sensitivity implementation,
260105755b9cSHong Zhang   so most users would not generally call this routine themselves.
260205755b9cSHong Zhang 
260305755b9cSHong Zhang   Level: developer
260405755b9cSHong Zhang 
260505755b9cSHong Zhang .keywords: TS, sensitivity
2606d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction()
260705755b9cSHong Zhang @*/
26080cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp)
260905755b9cSHong Zhang {
261005755b9cSHong Zhang   PetscErrorCode ierr;
261105755b9cSHong Zhang 
261205755b9cSHong Zhang   PetscFunctionBegin;
261305755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
26140cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
261505755b9cSHong Zhang 
261605755b9cSHong Zhang   PetscStackPush("TS user DRDP function for sensitivity analysis");
26170cf82b59SBarry Smith   ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr);
261805755b9cSHong Zhang   PetscStackPop;
261905755b9cSHong Zhang   PetscFunctionReturn(0);
262005755b9cSHong Zhang }
262105755b9cSHong Zhang 
262205755b9cSHong Zhang #undef __FUNCT__
2623e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2624ac226902SBarry Smith /*@C
2625000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
26263f2090d5SJed Brown   called once at the beginning of each time step.
2627000e7ae3SMatthew Knepley 
26283f9fe445SBarry Smith   Logically Collective on TS
2629000e7ae3SMatthew Knepley 
2630000e7ae3SMatthew Knepley   Input Parameters:
2631000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2632000e7ae3SMatthew Knepley - func - The function
2633000e7ae3SMatthew Knepley 
2634000e7ae3SMatthew Knepley   Calling sequence of func:
2635000e7ae3SMatthew Knepley . func (TS ts);
2636000e7ae3SMatthew Knepley 
2637000e7ae3SMatthew Knepley   Level: intermediate
2638000e7ae3SMatthew Knepley 
2639b8123daeSJed Brown   Note:
2640b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2641b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2642b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2643b8123daeSJed Brown 
2644000e7ae3SMatthew Knepley .keywords: TS, timestep
26459be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
2646000e7ae3SMatthew Knepley @*/
26477087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2648000e7ae3SMatthew Knepley {
2649000e7ae3SMatthew Knepley   PetscFunctionBegin;
26500700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2651ae60f76fSBarry Smith   ts->prestep = func;
2652000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2653000e7ae3SMatthew Knepley }
2654000e7ae3SMatthew Knepley 
2655e74ef692SMatthew Knepley #undef __FUNCT__
26563f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
265709ee8438SJed Brown /*@
26583f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
26593f2090d5SJed Brown 
26603f2090d5SJed Brown   Collective on TS
26613f2090d5SJed Brown 
26623f2090d5SJed Brown   Input Parameters:
26633f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
26643f2090d5SJed Brown 
26653f2090d5SJed Brown   Notes:
26663f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
26673f2090d5SJed Brown   so most users would not generally call this routine themselves.
26683f2090d5SJed Brown 
26693f2090d5SJed Brown   Level: developer
26703f2090d5SJed Brown 
26713f2090d5SJed Brown .keywords: TS, timestep
26729be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
26733f2090d5SJed Brown @*/
26747087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
26753f2090d5SJed Brown {
26763f2090d5SJed Brown   PetscErrorCode ierr;
26773f2090d5SJed Brown 
26783f2090d5SJed Brown   PetscFunctionBegin;
26790700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2680ae60f76fSBarry Smith   if (ts->prestep) {
2681ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
2682312ce896SJed Brown   }
26833f2090d5SJed Brown   PetscFunctionReturn(0);
26843f2090d5SJed Brown }
26853f2090d5SJed Brown 
26863f2090d5SJed Brown #undef __FUNCT__
2687b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2688b8123daeSJed Brown /*@C
2689b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2690b8123daeSJed Brown   called once at the beginning of each stage.
2691b8123daeSJed Brown 
2692b8123daeSJed Brown   Logically Collective on TS
2693b8123daeSJed Brown 
2694b8123daeSJed Brown   Input Parameters:
2695b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2696b8123daeSJed Brown - func - The function
2697b8123daeSJed Brown 
2698b8123daeSJed Brown   Calling sequence of func:
2699b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2700b8123daeSJed Brown 
2701b8123daeSJed Brown   Level: intermediate
2702b8123daeSJed Brown 
2703b8123daeSJed Brown   Note:
2704b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2705b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2706b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2707b8123daeSJed Brown 
2708b8123daeSJed Brown .keywords: TS, timestep
27099be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2710b8123daeSJed Brown @*/
2711b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2712b8123daeSJed Brown {
2713b8123daeSJed Brown   PetscFunctionBegin;
2714b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2715ae60f76fSBarry Smith   ts->prestage = func;
2716b8123daeSJed Brown   PetscFunctionReturn(0);
2717b8123daeSJed Brown }
2718b8123daeSJed Brown 
2719b8123daeSJed Brown #undef __FUNCT__
27209be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage"
27219be3e283SDebojyoti Ghosh /*@C
27229be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
27239be3e283SDebojyoti Ghosh   called once at the end of each stage.
27249be3e283SDebojyoti Ghosh 
27259be3e283SDebojyoti Ghosh   Logically Collective on TS
27269be3e283SDebojyoti Ghosh 
27279be3e283SDebojyoti Ghosh   Input Parameters:
27289be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
27299be3e283SDebojyoti Ghosh - func - The function
27309be3e283SDebojyoti Ghosh 
27319be3e283SDebojyoti Ghosh   Calling sequence of func:
27329be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
27339be3e283SDebojyoti Ghosh 
27349be3e283SDebojyoti Ghosh   Level: intermediate
27359be3e283SDebojyoti Ghosh 
27369be3e283SDebojyoti Ghosh   Note:
27379be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
27389be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
27399be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
27409be3e283SDebojyoti Ghosh 
27419be3e283SDebojyoti Ghosh .keywords: TS, timestep
27429be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
27439be3e283SDebojyoti Ghosh @*/
27449be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
27459be3e283SDebojyoti Ghosh {
27469be3e283SDebojyoti Ghosh   PetscFunctionBegin;
27479be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
27489be3e283SDebojyoti Ghosh   ts->poststage = func;
27499be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
27509be3e283SDebojyoti Ghosh }
27519be3e283SDebojyoti Ghosh 
27529be3e283SDebojyoti Ghosh #undef __FUNCT__
2753b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2754b8123daeSJed Brown /*@
2755b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2756b8123daeSJed Brown 
2757b8123daeSJed Brown   Collective on TS
2758b8123daeSJed Brown 
2759b8123daeSJed Brown   Input Parameters:
2760b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
27619be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
2762b8123daeSJed Brown 
2763b8123daeSJed Brown   Notes:
2764b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2765b8123daeSJed Brown   most users would not generally call this routine themselves.
2766b8123daeSJed Brown 
2767b8123daeSJed Brown   Level: developer
2768b8123daeSJed Brown 
2769b8123daeSJed Brown .keywords: TS, timestep
27709be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
2771b8123daeSJed Brown @*/
2772b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2773b8123daeSJed Brown {
2774b8123daeSJed Brown   PetscErrorCode ierr;
2775b8123daeSJed Brown 
2776b8123daeSJed Brown   PetscFunctionBegin;
2777b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2778ae60f76fSBarry Smith   if (ts->prestage) {
2779ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
2780b8123daeSJed Brown   }
2781b8123daeSJed Brown   PetscFunctionReturn(0);
2782b8123daeSJed Brown }
2783b8123daeSJed Brown 
2784b8123daeSJed Brown #undef __FUNCT__
27859be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage"
27869be3e283SDebojyoti Ghosh /*@
27879be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
27889be3e283SDebojyoti Ghosh 
27899be3e283SDebojyoti Ghosh   Collective on TS
27909be3e283SDebojyoti Ghosh 
27919be3e283SDebojyoti Ghosh   Input Parameters:
27929be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
27939be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
27949be3e283SDebojyoti Ghosh   stageindex  - Stage number
27959be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
27969be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
27979be3e283SDebojyoti Ghosh 
27989be3e283SDebojyoti Ghosh   Notes:
27999be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
28009be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
28019be3e283SDebojyoti Ghosh 
28029be3e283SDebojyoti Ghosh   Level: developer
28039be3e283SDebojyoti Ghosh 
28049be3e283SDebojyoti Ghosh .keywords: TS, timestep
28059be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
28069be3e283SDebojyoti Ghosh @*/
28079be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
28089be3e283SDebojyoti Ghosh {
28099be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
28109be3e283SDebojyoti Ghosh 
28119be3e283SDebojyoti Ghosh   PetscFunctionBegin;
28129be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28134beae5d8SLisandro Dalcin   if (ts->poststage) {
28149be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
28159be3e283SDebojyoti Ghosh   }
28169be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
28179be3e283SDebojyoti Ghosh }
28189be3e283SDebojyoti Ghosh 
28199be3e283SDebojyoti Ghosh #undef __FUNCT__
2820e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2821ac226902SBarry Smith /*@C
2822000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
28233f2090d5SJed Brown   called once at the end of each time step.
2824000e7ae3SMatthew Knepley 
28253f9fe445SBarry Smith   Logically Collective on TS
2826000e7ae3SMatthew Knepley 
2827000e7ae3SMatthew Knepley   Input Parameters:
2828000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2829000e7ae3SMatthew Knepley - func - The function
2830000e7ae3SMatthew Knepley 
2831000e7ae3SMatthew Knepley   Calling sequence of func:
2832b8123daeSJed Brown $ func (TS ts);
2833000e7ae3SMatthew Knepley 
2834000e7ae3SMatthew Knepley   Level: intermediate
2835000e7ae3SMatthew Knepley 
2836000e7ae3SMatthew Knepley .keywords: TS, timestep
2837b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2838000e7ae3SMatthew Knepley @*/
28397087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2840000e7ae3SMatthew Knepley {
2841000e7ae3SMatthew Knepley   PetscFunctionBegin;
28420700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2843ae60f76fSBarry Smith   ts->poststep = func;
2844000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2845000e7ae3SMatthew Knepley }
2846000e7ae3SMatthew Knepley 
2847e74ef692SMatthew Knepley #undef __FUNCT__
28483f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
284909ee8438SJed Brown /*@
28503f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
28513f2090d5SJed Brown 
28523f2090d5SJed Brown   Collective on TS
28533f2090d5SJed Brown 
28543f2090d5SJed Brown   Input Parameters:
28553f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
28563f2090d5SJed Brown 
28573f2090d5SJed Brown   Notes:
28583f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
28593f2090d5SJed Brown   so most users would not generally call this routine themselves.
28603f2090d5SJed Brown 
28613f2090d5SJed Brown   Level: developer
28623f2090d5SJed Brown 
28633f2090d5SJed Brown .keywords: TS, timestep
28643f2090d5SJed Brown @*/
28657087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
28663f2090d5SJed Brown {
28673f2090d5SJed Brown   PetscErrorCode ierr;
28683f2090d5SJed Brown 
28693f2090d5SJed Brown   PetscFunctionBegin;
28700700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2871ae60f76fSBarry Smith   if (ts->poststep) {
2872ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
287372ac3e02SJed Brown   }
28743f2090d5SJed Brown   PetscFunctionReturn(0);
28753f2090d5SJed Brown }
28763f2090d5SJed Brown 
2877d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2878d763cef2SBarry Smith 
28794a2ae208SSatish Balay #undef __FUNCT__
2880a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2881d763cef2SBarry Smith /*@C
2882a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2883d763cef2SBarry Smith    timestep to display the iteration's  progress.
2884d763cef2SBarry Smith 
28853f9fe445SBarry Smith    Logically Collective on TS
2886d763cef2SBarry Smith 
2887d763cef2SBarry Smith    Input Parameters:
2888d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2889e213d8f1SJed Brown .  monitor - monitoring routine
2890329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
28910298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
2892b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
28930298fd71SBarry Smith           (may be NULL)
2894d763cef2SBarry Smith 
2895e213d8f1SJed Brown    Calling sequence of monitor:
28960910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2897d763cef2SBarry Smith 
2898d763cef2SBarry Smith +    ts - the TS context
289988c05cc5SBarry Smith .    steps - iteration number (after the final time step the monitor routine is called with a step of -1, this is at the final time which may have
290088c05cc5SBarry Smith                                been interpolated to)
29011f06c33eSBarry Smith .    time - current time
29020910c330SBarry Smith .    u - current iterate
2903d763cef2SBarry Smith -    mctx - [optional] monitoring context
2904d763cef2SBarry Smith 
2905d763cef2SBarry Smith    Notes:
2906d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2907d763cef2SBarry Smith    already has been loaded.
2908d763cef2SBarry Smith 
2909025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2910025f1a04SBarry Smith 
2911d763cef2SBarry Smith    Level: intermediate
2912d763cef2SBarry Smith 
2913d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2914d763cef2SBarry Smith 
2915a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2916d763cef2SBarry Smith @*/
2917c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2918d763cef2SBarry Smith {
2919d763cef2SBarry Smith   PetscFunctionBegin;
29200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
292117186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2922d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
29238704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2924d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2925d763cef2SBarry Smith   PetscFunctionReturn(0);
2926d763cef2SBarry Smith }
2927d763cef2SBarry Smith 
29284a2ae208SSatish Balay #undef __FUNCT__
2929a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2930d763cef2SBarry Smith /*@C
2931a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2932d763cef2SBarry Smith 
29333f9fe445SBarry Smith    Logically Collective on TS
2934d763cef2SBarry Smith 
2935d763cef2SBarry Smith    Input Parameters:
2936d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2937d763cef2SBarry Smith 
2938d763cef2SBarry Smith    Notes:
2939d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2940d763cef2SBarry Smith 
2941d763cef2SBarry Smith    Level: intermediate
2942d763cef2SBarry Smith 
2943d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2944d763cef2SBarry Smith 
2945a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2946d763cef2SBarry Smith @*/
29477087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2948d763cef2SBarry Smith {
2949d952e501SBarry Smith   PetscErrorCode ierr;
2950d952e501SBarry Smith   PetscInt       i;
2951d952e501SBarry Smith 
2952d763cef2SBarry Smith   PetscFunctionBegin;
29530700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2954d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
29558704b422SBarry Smith     if (ts->monitordestroy[i]) {
29568704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2957d952e501SBarry Smith     }
2958d952e501SBarry Smith   }
2959d763cef2SBarry Smith   ts->numbermonitors = 0;
2960d763cef2SBarry Smith   PetscFunctionReturn(0);
2961d763cef2SBarry Smith }
2962d763cef2SBarry Smith 
29634a2ae208SSatish Balay #undef __FUNCT__
2964a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2965d8e5e3e6SSatish Balay /*@
2966a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
29675516499fSSatish Balay 
29685516499fSSatish Balay    Level: intermediate
296941251cbbSSatish Balay 
29705516499fSSatish Balay .keywords: TS, set, monitor
29715516499fSSatish Balay 
297241251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
297341251cbbSSatish Balay @*/
2974649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2975d763cef2SBarry Smith {
2976dfbe8321SBarry Smith   PetscErrorCode ierr;
2977ce94432eSBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts));
2978d132466eSBarry Smith 
2979d763cef2SBarry Smith   PetscFunctionBegin;
2980649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
29818392e04aSShri Abhyankar   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");CHKERRQ(ierr);
2982649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2983d763cef2SBarry Smith   PetscFunctionReturn(0);
2984d763cef2SBarry Smith }
2985d763cef2SBarry Smith 
29864a2ae208SSatish Balay #undef __FUNCT__
2987cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2988cd652676SJed Brown /*@
2989cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2990cd652676SJed Brown 
2991cd652676SJed Brown    Logically Collective on TS
2992cd652676SJed Brown 
2993cd652676SJed Brown    Input Argument:
2994cd652676SJed Brown .  ts - time stepping context
2995cd652676SJed Brown 
2996cd652676SJed Brown    Output Argument:
2997cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2998cd652676SJed Brown 
2999cd652676SJed Brown    Level: intermediate
3000cd652676SJed Brown 
3001cd652676SJed Brown .keywords: TS, set
3002cd652676SJed Brown 
3003cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
3004cd652676SJed Brown @*/
3005cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
3006cd652676SJed Brown {
3007cd652676SJed Brown   PetscFunctionBegin;
3008cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3009cd652676SJed Brown   ts->retain_stages = flg;
3010cd652676SJed Brown   PetscFunctionReturn(0);
3011cd652676SJed Brown }
3012cd652676SJed Brown 
3013cd652676SJed Brown #undef __FUNCT__
3014cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
3015cd652676SJed Brown /*@
3016cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3017cd652676SJed Brown 
3018cd652676SJed Brown    Collective on TS
3019cd652676SJed Brown 
3020cd652676SJed Brown    Input Argument:
3021cd652676SJed Brown +  ts - time stepping context
3022cd652676SJed Brown -  t - time to interpolate to
3023cd652676SJed Brown 
3024cd652676SJed Brown    Output Argument:
30250910c330SBarry Smith .  U - state at given time
3026cd652676SJed Brown 
3027cd652676SJed Brown    Notes:
3028cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
3029cd652676SJed Brown 
3030cd652676SJed Brown    Level: intermediate
3031cd652676SJed Brown 
3032cd652676SJed Brown    Developer Notes:
3033cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3034cd652676SJed Brown 
3035cd652676SJed Brown .keywords: TS, set
3036cd652676SJed Brown 
3037cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
3038cd652676SJed Brown @*/
30390910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3040cd652676SJed Brown {
3041cd652676SJed Brown   PetscErrorCode ierr;
3042cd652676SJed Brown 
3043cd652676SJed Brown   PetscFunctionBegin;
3044cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3045b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
30466712e2f1SBarry Smith   if (t < ts->ptime - ts->time_step_prev || t > ts->ptime) SETERRQ3(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Requested time %g not in last time steps [%g,%g]",t,(double)(ts->ptime-ts->time_step_prev),(double)ts->ptime);
3047ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
30480910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3049cd652676SJed Brown   PetscFunctionReturn(0);
3050cd652676SJed Brown }
3051cd652676SJed Brown 
3052cd652676SJed Brown #undef __FUNCT__
30534a2ae208SSatish Balay #define __FUNCT__ "TSStep"
3054d763cef2SBarry Smith /*@
30556d9e5789SSean Farley    TSStep - Steps one time step
3056d763cef2SBarry Smith 
3057d763cef2SBarry Smith    Collective on TS
3058d763cef2SBarry Smith 
3059d763cef2SBarry Smith    Input Parameter:
3060d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3061d763cef2SBarry Smith 
306227829d71SBarry Smith    Level: developer
3063d763cef2SBarry Smith 
3064b8123daeSJed Brown    Notes:
306527829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
306627829d71SBarry Smith 
3067b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3068b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3069b8123daeSJed Brown 
307025cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
307125cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
307225cb2221SBarry Smith 
3073d763cef2SBarry Smith .keywords: TS, timestep, solve
3074d763cef2SBarry Smith 
30759be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3076d763cef2SBarry Smith @*/
3077193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3078d763cef2SBarry Smith {
30792fb8446cSMatthew G. Knepley   DM               dm;
3080dfbe8321SBarry Smith   PetscErrorCode   ierr;
3081fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3082d763cef2SBarry Smith 
3083d763cef2SBarry Smith   PetscFunctionBegin;
30840700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3085fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3086fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3087fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3088fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3089fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3090fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3091302440fdSBarry Smith                                 "  year        = {2014}\n}\n",&cite);CHKERRQ(ierr);
3092fffbeea8SBarry Smith 
30932fb8446cSMatthew G. Knepley   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
3094d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
3095d405a339SMatthew Knepley 
3096362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
309724655328SShri   ts->ptime_prev = ts->ptime;
3098cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3099bbd56ea5SKarl Rupp 
3100e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3101d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3102193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3103d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3104bbd56ea5SKarl Rupp 
310524655328SShri   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3106cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3107362cd11cSLisandro Dalcin 
3108362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3109cef5090cSJed Brown     if (ts->errorifstepfailed) {
311008c7845fSBarry Smith       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
311108c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3112d2daff3dSHong Zhang     }
311308c7845fSBarry Smith   } else if (!ts->reason) {
311408c7845fSBarry Smith     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
311508c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
311608c7845fSBarry Smith   }
31172c18e0fdSBarry Smith   ts->total_steps++;
31188392e04aSShri Abhyankar   ts->steprollback = PETSC_FALSE;
311908c7845fSBarry Smith   PetscFunctionReturn(0);
312008c7845fSBarry Smith }
312108c7845fSBarry Smith 
312208c7845fSBarry Smith #undef __FUNCT__
312308c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep"
312408c7845fSBarry Smith /*@
3125b957a604SHong Zhang    TSAdjointStep - Steps one time step backward in the adjoint run
312608c7845fSBarry Smith 
312708c7845fSBarry Smith    Collective on TS
312808c7845fSBarry Smith 
312908c7845fSBarry Smith    Input Parameter:
313008c7845fSBarry Smith .  ts - the TS context obtained from TSCreate()
313108c7845fSBarry Smith 
31326a4d4014SLisandro Dalcin    Level: intermediate
31336a4d4014SLisandro Dalcin 
3134b957a604SHong Zhang .keywords: TS, adjoint, step
31356a4d4014SLisandro Dalcin 
3136b957a604SHong Zhang .seealso: TSAdjointSetUp(), TSAdjointSolve()
31376a4d4014SLisandro Dalcin @*/
313808c7845fSBarry Smith PetscErrorCode  TSAdjointStep(TS ts)
31396a4d4014SLisandro Dalcin {
314008c7845fSBarry Smith   DM               dm;
31416a4d4014SLisandro Dalcin   PetscErrorCode   ierr;
31426a4d4014SLisandro Dalcin 
31436a4d4014SLisandro Dalcin   PetscFunctionBegin;
31444a2ae208SSatish Balay   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
314508c7845fSBarry Smith   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
314608c7845fSBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
3147d763cef2SBarry Smith 
3148d763cef2SBarry Smith   ts->reason = TS_CONVERGED_ITERATING;
314908c7845fSBarry Smith   ts->ptime_prev = ts->ptime;
315008c7845fSBarry Smith   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3151685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vec_sol,(PetscObject)ts, "-ts_view_solution");CHKERRQ(ierr);
3152d763cef2SBarry Smith 
31536849ba73SBarry Smith   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
315442f2b339SBarry Smith   if (!ts->ops->adjointstep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed because the adjoint of  %s has not been implemented, try other time stepping methods for adjoint sensitivity analysis",((PetscObject)ts)->type_name);
315542f2b339SBarry Smith   ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr);
3156a6570f20SBarry Smith   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3157d763cef2SBarry Smith 
3158cef5090cSJed Brown   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3159cef5090cSJed Brown   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3160362cd11cSLisandro Dalcin 
3161362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3162cef5090cSJed Brown     if (ts->errorifstepfailed) {
3163cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
3164ce94432eSBarry Smith         SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_snes_failures or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
31652a3a10ceSLisandro Dalcin       } else if (ts->reason == TS_DIVERGED_STEP_REJECTED) {
31662a3a10ceSLisandro Dalcin         SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s, increase -ts_max_reject or make negative to attempt recovery",TSConvergedReasons[ts->reason]);
3167ce94432eSBarry Smith       } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3168cef5090cSJed Brown     }
3169362cd11cSLisandro Dalcin   } else if (!ts->reason) {
317073b18844SBarry Smith     if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
3171db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time)         ts->reason = TS_CONVERGED_TIME;
3172362cd11cSLisandro Dalcin   }
31732c18e0fdSBarry Smith   ts->total_steps--;
3174d763cef2SBarry Smith   PetscFunctionReturn(0);
3175d763cef2SBarry Smith }
3176d763cef2SBarry Smith 
3177d763cef2SBarry Smith #undef __FUNCT__
317805175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
317905175c85SJed Brown /*@
318005175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
318105175c85SJed Brown 
31821c3436cfSJed Brown    Collective on TS
318305175c85SJed Brown 
318405175c85SJed Brown    Input Arguments:
31851c3436cfSJed Brown +  ts - time stepping context
31861c3436cfSJed Brown .  order - desired order of accuracy
31870298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
318805175c85SJed Brown 
318905175c85SJed Brown    Output Arguments:
31900910c330SBarry Smith .  U - state at the end of the current step
319105175c85SJed Brown 
319205175c85SJed Brown    Level: advanced
319305175c85SJed Brown 
3194108c343cSJed Brown    Notes:
3195108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3196108c343cSJed Brown    It is normally called by adaptive controllers before a step has been accepted and may also be called by the user after TSStep() has returned.
3197108c343cSJed Brown 
31981c3436cfSJed Brown .seealso: TSStep(), TSAdapt
319905175c85SJed Brown @*/
32000910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
320105175c85SJed Brown {
320205175c85SJed Brown   PetscErrorCode ierr;
320305175c85SJed Brown 
320405175c85SJed Brown   PetscFunctionBegin;
320505175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
320605175c85SJed Brown   PetscValidType(ts,1);
32070910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3208ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
32090910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
321005175c85SJed Brown   PetscFunctionReturn(0);
321105175c85SJed Brown }
321205175c85SJed Brown 
3213d67e68b7SBarry Smith 
321405175c85SJed Brown #undef __FUNCT__
3215d763cef2SBarry Smith #define __FUNCT__ "TSSolve"
3216d763cef2SBarry Smith /*@
3217d763cef2SBarry Smith    TSSolve - Steps the requested number of timesteps.
3218d763cef2SBarry Smith 
3219d763cef2SBarry Smith    Collective on TS
3220d763cef2SBarry Smith 
3221d763cef2SBarry Smith    Input Parameter:
3222d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3223cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
32245a3a76d0SJed Brown 
3225d763cef2SBarry Smith    Level: beginner
3226d763cef2SBarry Smith 
32275a3a76d0SJed Brown    Notes:
32285a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
32295a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
32305a3a76d0SJed Brown    stepped over the final time.
32315a3a76d0SJed Brown 
3232d763cef2SBarry Smith .keywords: TS, timestep, solve
3233d763cef2SBarry Smith 
3234d763cef2SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep()
3235d763cef2SBarry Smith @*/
3236cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
3237d763cef2SBarry Smith {
3238b06615a5SLisandro Dalcin   Vec               solution;
3239d763cef2SBarry Smith   PetscErrorCode    ierr;
3240d763cef2SBarry Smith 
3241d763cef2SBarry Smith   PetscFunctionBegin;
3242d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3243f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
324449354f04SShri Abhyankar   if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE) {   /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */
3245b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
32460910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3247b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3248b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3249b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
32505a3a76d0SJed Brown     }
32510910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3252bbd56ea5SKarl Rupp   } else if (u) {
32530910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
32545a3a76d0SJed Brown   }
3255b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
3256d763cef2SBarry Smith   /* reset time step and iteration counters */
3257193ac0bcSJed Brown   ts->steps             = 0;
32585ef26d82SJed Brown   ts->ksp_its           = 0;
32595ef26d82SJed Brown   ts->snes_its          = 0;
3260c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
3261c610991cSLisandro Dalcin   ts->reject            = 0;
3262193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
3263193ac0bcSJed Brown 
3264ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3265ec8db0baSMatthew G. Knepley   {
3266ec8db0baSMatthew G. Knepley     DM dm;
3267ec8db0baSMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
3268ec8db0baSMatthew G. Knepley     ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3269ec8db0baSMatthew G. Knepley   }
3270f05ece33SBarry Smith 
3271193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
3272193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
32730910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
3274cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3275193ac0bcSJed Brown   } else {
3276d763cef2SBarry Smith     /* steps the requested number of timesteps. */
3277db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
3278db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3279cdf0de3dSShri Abhyankar     ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
328011b05f00SHong Zhang     if (ts->vec_costintegral) ts->costintegralfwd=PETSC_TRUE;
32816fea3669SShri Abhyankar     if(ts->event) {
32826fea3669SShri Abhyankar       ierr = TSEventMonitorInitialize(ts);CHKERRQ(ierr);
32836fea3669SShri Abhyankar     }
3284e1a7a14fSJed Brown     while (!ts->reason) {
3285193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
3286193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
3287aeb4809dSShri Abhyankar       if (ts->event) {
3288aeb4809dSShri Abhyankar 	ierr = TSEventMonitor(ts);CHKERRQ(ierr);
32891eda64f1SShri Abhyankar       }
32908392e04aSShri Abhyankar       if(!ts->steprollback) {
3291cdf0de3dSShri Abhyankar 	ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
32921eda64f1SShri Abhyankar 	ierr = TSPostStep(ts);CHKERRQ(ierr);
3293aeb4809dSShri Abhyankar       }
3294193ac0bcSJed Brown     }
329549354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
32960910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3297cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3298b06615a5SLisandro Dalcin       solution = u;
32990574a7fbSJed Brown     } else {
3300ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3301cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3302b06615a5SLisandro Dalcin       solution = ts->vec_sol;
33030574a7fbSJed Brown     }
3304b06615a5SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr);
3305685405a1SBarry Smith     ierr = VecViewFromOptions(solution,(PetscObject) ts,"-ts_view_solution");CHKERRQ(ierr);
3306193ac0bcSJed Brown   }
3307d2daff3dSHong Zhang 
3308ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
3309dc1cb503SEmil Constantinescu   ierr = VecViewFromOptions(ts->vec_sol,NULL,"-ts_view_solution");CHKERRQ(ierr);
331056f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
3311ef222394SBarry Smith   if (ts->adjoint_solve) {
3312ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
33132b0a91c0SBarry Smith   }
3314d763cef2SBarry Smith   PetscFunctionReturn(0);
3315d763cef2SBarry Smith }
3316d763cef2SBarry Smith 
3317d763cef2SBarry Smith #undef __FUNCT__
3318c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve"
3319c88f2d44SBarry Smith /*@
3320c88f2d44SBarry Smith    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE
3321c88f2d44SBarry Smith 
3322c88f2d44SBarry Smith    Collective on TS
3323c88f2d44SBarry Smith 
3324c88f2d44SBarry Smith    Input Parameter:
33252c39e106SBarry Smith .  ts - the TS context obtained from TSCreate()
3326c88f2d44SBarry Smith 
3327e87fd094SBarry Smith    Options Database:
3328e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions
3329e87fd094SBarry Smith 
3330c88f2d44SBarry Smith    Level: intermediate
3331c88f2d44SBarry Smith 
3332c88f2d44SBarry Smith    Notes:
3333c88f2d44SBarry Smith    This must be called after a call to TSSolve() that solves the forward problem
3334c88f2d44SBarry Smith 
333573b18844SBarry Smith    By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time
333673b18844SBarry Smith 
3337c88f2d44SBarry Smith .keywords: TS, timestep, solve
3338c88f2d44SBarry Smith 
3339b957a604SHong Zhang .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep()
3340c88f2d44SBarry Smith @*/
33412c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts)
3342c88f2d44SBarry Smith {
3343c88f2d44SBarry Smith   PetscErrorCode    ierr;
3344c88f2d44SBarry Smith 
3345c88f2d44SBarry Smith   PetscFunctionBegin;
3346c88f2d44SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3347c88f2d44SBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
3348c88f2d44SBarry Smith   /* reset time step and iteration counters */
3349c88f2d44SBarry Smith   ts->steps             = 0;
3350c88f2d44SBarry Smith   ts->ksp_its           = 0;
3351c88f2d44SBarry Smith   ts->snes_its          = 0;
3352c88f2d44SBarry Smith   ts->num_snes_failures = 0;
3353c88f2d44SBarry Smith   ts->reject            = 0;
3354c88f2d44SBarry Smith   ts->reason            = TS_CONVERGED_ITERATING;
3355c88f2d44SBarry Smith 
335673b18844SBarry Smith   if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps;
3357c88f2d44SBarry Smith 
335873b18844SBarry Smith   if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
3359c88f2d44SBarry Smith   while (!ts->reason) {
3360c679fc15SHong Zhang     ierr = TSTrajectoryGet(ts->trajectory,ts,ts->adjoint_max_steps-ts->steps,&ts->ptime);CHKERRQ(ierr);
336173b18844SBarry Smith     ierr = TSMonitor(ts,ts->adjoint_max_steps-ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
336208c7845fSBarry Smith     ierr = TSAdjointStep(ts);CHKERRQ(ierr);
3363c88f2d44SBarry Smith     if (ts->event) {
3364d0578d90SShri Abhyankar       ierr = TSAdjointEventMonitor(ts);CHKERRQ(ierr);
3365d0578d90SShri Abhyankar     }
3366c88f2d44SBarry Smith   }
3367c88f2d44SBarry Smith   ts->solvetime = ts->ptime;
3368685405a1SBarry Smith   ierr = VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");CHKERRQ(ierr);
3369c88f2d44SBarry Smith   PetscFunctionReturn(0);
3370c88f2d44SBarry Smith }
3371c88f2d44SBarry Smith 
3372c88f2d44SBarry Smith #undef __FUNCT__
3373d763cef2SBarry Smith #define __FUNCT__ "TSMonitor"
3374228d79bcSJed Brown /*@
3375228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3376228d79bcSJed Brown 
3377228d79bcSJed Brown    Collective on TS
3378228d79bcSJed Brown 
3379228d79bcSJed Brown    Input Parameters:
3380228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3381228d79bcSJed Brown .  step - step number that has just completed
3382228d79bcSJed Brown .  ptime - model time of the state
33830910c330SBarry Smith -  u - state at the current model time
3384228d79bcSJed Brown 
3385228d79bcSJed Brown    Notes:
3386228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
3387228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
3388228d79bcSJed Brown 
3389228d79bcSJed Brown    Level: advanced
3390228d79bcSJed Brown 
3391228d79bcSJed Brown .keywords: TS, timestep
3392228d79bcSJed Brown @*/
33930910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3394d763cef2SBarry Smith {
3395d763cef2SBarry Smith   PetscErrorCode ierr;
3396d763cef2SBarry Smith   PetscInt       i,n = ts->numbermonitors;
3397d763cef2SBarry Smith 
3398d763cef2SBarry Smith   PetscFunctionBegin;
3399b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3400b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
34015edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
3402d763cef2SBarry Smith   for (i=0; i<n; i++) {
34030910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
3404d763cef2SBarry Smith   }
34055edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
3406d763cef2SBarry Smith   PetscFunctionReturn(0);
3407d763cef2SBarry Smith }
3408d763cef2SBarry Smith 
3409d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
3410d763cef2SBarry Smith #undef __FUNCT__
3411a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
3412d763cef2SBarry Smith /*@C
3413a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
3414a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
3415d763cef2SBarry Smith 
3416d763cef2SBarry Smith    Collective on TS
3417d763cef2SBarry Smith 
3418d763cef2SBarry Smith    Input Parameters:
3419d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
3420d763cef2SBarry Smith .  label - the title to put in the title bar
34217c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
3422a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
3423a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
3424d763cef2SBarry Smith 
3425d763cef2SBarry Smith    Output Parameter:
34260b039ecaSBarry Smith .  ctx - the context
3427d763cef2SBarry Smith 
3428d763cef2SBarry Smith    Options Database Key:
34294f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
34304f09c107SBarry Smith .  -ts_monitor_lg_solution -
343199fdda47SBarry Smith .  -ts_monitor_lg_error -
343299fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
343399fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
3434b6fe0379SLisandro Dalcin -  -lg_use_markers <true,false> - mark the data points (at each time step) on the plot; default is true
3435d763cef2SBarry Smith 
3436d763cef2SBarry Smith    Notes:
3437a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
3438d763cef2SBarry Smith 
3439d763cef2SBarry Smith    Level: intermediate
3440d763cef2SBarry Smith 
34417c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
3442d763cef2SBarry Smith 
34434f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
34447c922b88SBarry Smith 
3445d763cef2SBarry Smith @*/
3446a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
3447d763cef2SBarry Smith {
3448b0a32e0cSBarry Smith   PetscDraw      win;
3449dfbe8321SBarry Smith   PetscErrorCode ierr;
3450d763cef2SBarry Smith 
3451d763cef2SBarry Smith   PetscFunctionBegin;
3452b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
3453a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
34543fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
34550b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
34563bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)(*ctx)->lg,(PetscObject)win);CHKERRQ(ierr);
3457b6fe0379SLisandro Dalcin   ierr = PetscDrawLGSetUseMarkers((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr);
3458287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
3459a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
3460d763cef2SBarry Smith   PetscFunctionReturn(0);
3461d763cef2SBarry Smith }
3462d763cef2SBarry Smith 
34634a2ae208SSatish Balay #undef __FUNCT__
34644f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
3465b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
3466d763cef2SBarry Smith {
34670b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
3468c32365f1SBarry Smith   PetscReal      x   = ptime,y;
3469dfbe8321SBarry Smith   PetscErrorCode ierr;
3470d763cef2SBarry Smith 
3471d763cef2SBarry Smith   PetscFunctionBegin;
3472b06615a5SLisandro Dalcin   if (!step) {
3473a9f9c1f6SBarry Smith     PetscDrawAxis axis;
3474a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
3475a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
3476a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
3477a9f9c1f6SBarry Smith   }
3478c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
34790b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
3480b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
34810b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
34823923b477SBarry Smith   }
3483d763cef2SBarry Smith   PetscFunctionReturn(0);
3484d763cef2SBarry Smith }
3485d763cef2SBarry Smith 
34864a2ae208SSatish Balay #undef __FUNCT__
3487a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
3488d763cef2SBarry Smith /*@C
3489a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
3490a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
3491d763cef2SBarry Smith 
34920b039ecaSBarry Smith    Collective on TSMonitorLGCtx
3493d763cef2SBarry Smith 
3494d763cef2SBarry Smith    Input Parameter:
34950b039ecaSBarry Smith .  ctx - the monitor context
3496d763cef2SBarry Smith 
3497d763cef2SBarry Smith    Level: intermediate
3498d763cef2SBarry Smith 
3499d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
3500d763cef2SBarry Smith 
35014f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
3502d763cef2SBarry Smith @*/
3503a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
3504d763cef2SBarry Smith {
3505b0a32e0cSBarry Smith   PetscDraw      draw;
3506dfbe8321SBarry Smith   PetscErrorCode ierr;
3507d763cef2SBarry Smith 
3508d763cef2SBarry Smith   PetscFunctionBegin;
35097684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
35107684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
35117684fa3eSBarry Smith   }
35120b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
35136bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
35140b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
351531152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
3516387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
3517387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
3518387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
35190b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
3520d763cef2SBarry Smith   PetscFunctionReturn(0);
3521d763cef2SBarry Smith }
3522d763cef2SBarry Smith 
35234a2ae208SSatish Balay #undef __FUNCT__
35244a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
3525d763cef2SBarry Smith /*@
3526b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
3527d763cef2SBarry Smith 
3528d763cef2SBarry Smith    Not Collective
3529d763cef2SBarry Smith 
3530d763cef2SBarry Smith    Input Parameter:
3531d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3532d763cef2SBarry Smith 
3533d763cef2SBarry Smith    Output Parameter:
3534d763cef2SBarry Smith .  t  - the current time
3535d763cef2SBarry Smith 
3536d763cef2SBarry Smith    Level: beginner
3537d763cef2SBarry Smith 
3538b8123daeSJed Brown    Note:
3539b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
35409be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
3541b8123daeSJed Brown 
3542d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3543d763cef2SBarry Smith 
3544d763cef2SBarry Smith .keywords: TS, get, time
3545d763cef2SBarry Smith @*/
35467087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
3547d763cef2SBarry Smith {
3548d763cef2SBarry Smith   PetscFunctionBegin;
35490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3550f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
3551d763cef2SBarry Smith   *t = ts->ptime;
3552d763cef2SBarry Smith   PetscFunctionReturn(0);
3553d763cef2SBarry Smith }
3554d763cef2SBarry Smith 
35554a2ae208SSatish Balay #undef __FUNCT__
3556e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime"
3557e5e524a1SHong Zhang /*@
3558e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
3559e5e524a1SHong Zhang 
3560e5e524a1SHong Zhang    Not Collective
3561e5e524a1SHong Zhang 
3562e5e524a1SHong Zhang    Input Parameter:
3563e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
3564e5e524a1SHong Zhang 
3565e5e524a1SHong Zhang    Output Parameter:
3566e5e524a1SHong Zhang .  t  - the previous time
3567e5e524a1SHong Zhang 
3568e5e524a1SHong Zhang    Level: beginner
3569e5e524a1SHong Zhang 
3570e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3571e5e524a1SHong Zhang 
3572e5e524a1SHong Zhang .keywords: TS, get, time
3573e5e524a1SHong Zhang @*/
3574e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
3575e5e524a1SHong Zhang {
3576e5e524a1SHong Zhang   PetscFunctionBegin;
3577e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3578e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
3579e5e524a1SHong Zhang   *t = ts->ptime_prev;
3580e5e524a1SHong Zhang   PetscFunctionReturn(0);
3581e5e524a1SHong Zhang }
3582e5e524a1SHong Zhang 
3583e5e524a1SHong Zhang #undef __FUNCT__
35846a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
35856a4d4014SLisandro Dalcin /*@
35866a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
35876a4d4014SLisandro Dalcin 
35883f9fe445SBarry Smith    Logically Collective on TS
35896a4d4014SLisandro Dalcin 
35906a4d4014SLisandro Dalcin    Input Parameters:
35916a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
35926a4d4014SLisandro Dalcin -  time - the time
35936a4d4014SLisandro Dalcin 
35946a4d4014SLisandro Dalcin    Level: intermediate
35956a4d4014SLisandro Dalcin 
35966a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
35976a4d4014SLisandro Dalcin 
35986a4d4014SLisandro Dalcin .keywords: TS, set, time
35996a4d4014SLisandro Dalcin @*/
36007087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
36016a4d4014SLisandro Dalcin {
36026a4d4014SLisandro Dalcin   PetscFunctionBegin;
36030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3604c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
36056a4d4014SLisandro Dalcin   ts->ptime = t;
36066a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
36076a4d4014SLisandro Dalcin }
36086a4d4014SLisandro Dalcin 
36096a4d4014SLisandro Dalcin #undef __FUNCT__
36104a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
3611d763cef2SBarry Smith /*@C
3612d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
3613d763cef2SBarry Smith    TS options in the database.
3614d763cef2SBarry Smith 
36153f9fe445SBarry Smith    Logically Collective on TS
3616d763cef2SBarry Smith 
3617d763cef2SBarry Smith    Input Parameter:
3618d763cef2SBarry Smith +  ts     - The TS context
3619d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3620d763cef2SBarry Smith 
3621d763cef2SBarry Smith    Notes:
3622d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3623d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3624d763cef2SBarry Smith    hyphen.
3625d763cef2SBarry Smith 
3626d763cef2SBarry Smith    Level: advanced
3627d763cef2SBarry Smith 
3628d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
3629d763cef2SBarry Smith 
3630d763cef2SBarry Smith .seealso: TSSetFromOptions()
3631d763cef2SBarry Smith 
3632d763cef2SBarry Smith @*/
36337087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
3634d763cef2SBarry Smith {
3635dfbe8321SBarry Smith   PetscErrorCode ierr;
3636089b2837SJed Brown   SNES           snes;
3637d763cef2SBarry Smith 
3638d763cef2SBarry Smith   PetscFunctionBegin;
36390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3640d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3641089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3642089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3643d763cef2SBarry Smith   PetscFunctionReturn(0);
3644d763cef2SBarry Smith }
3645d763cef2SBarry Smith 
3646d763cef2SBarry Smith 
36474a2ae208SSatish Balay #undef __FUNCT__
36484a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
3649d763cef2SBarry Smith /*@C
3650d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
3651d763cef2SBarry Smith    TS options in the database.
3652d763cef2SBarry Smith 
36533f9fe445SBarry Smith    Logically Collective on TS
3654d763cef2SBarry Smith 
3655d763cef2SBarry Smith    Input Parameter:
3656d763cef2SBarry Smith +  ts     - The TS context
3657d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3658d763cef2SBarry Smith 
3659d763cef2SBarry Smith    Notes:
3660d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3661d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3662d763cef2SBarry Smith    hyphen.
3663d763cef2SBarry Smith 
3664d763cef2SBarry Smith    Level: advanced
3665d763cef2SBarry Smith 
3666d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
3667d763cef2SBarry Smith 
3668d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
3669d763cef2SBarry Smith 
3670d763cef2SBarry Smith @*/
36717087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
3672d763cef2SBarry Smith {
3673dfbe8321SBarry Smith   PetscErrorCode ierr;
3674089b2837SJed Brown   SNES           snes;
3675d763cef2SBarry Smith 
3676d763cef2SBarry Smith   PetscFunctionBegin;
36770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3678d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3679089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3680089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3681d763cef2SBarry Smith   PetscFunctionReturn(0);
3682d763cef2SBarry Smith }
3683d763cef2SBarry Smith 
36844a2ae208SSatish Balay #undef __FUNCT__
36854a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
3686d763cef2SBarry Smith /*@C
3687d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
3688d763cef2SBarry Smith    TS options in the database.
3689d763cef2SBarry Smith 
3690d763cef2SBarry Smith    Not Collective
3691d763cef2SBarry Smith 
3692d763cef2SBarry Smith    Input Parameter:
3693d763cef2SBarry Smith .  ts - The TS context
3694d763cef2SBarry Smith 
3695d763cef2SBarry Smith    Output Parameter:
3696d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
3697d763cef2SBarry Smith 
3698d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
3699d763cef2SBarry Smith    sufficient length to hold the prefix.
3700d763cef2SBarry Smith 
3701d763cef2SBarry Smith    Level: intermediate
3702d763cef2SBarry Smith 
3703d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
3704d763cef2SBarry Smith 
3705d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
3706d763cef2SBarry Smith @*/
37077087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
3708d763cef2SBarry Smith {
3709dfbe8321SBarry Smith   PetscErrorCode ierr;
3710d763cef2SBarry Smith 
3711d763cef2SBarry Smith   PetscFunctionBegin;
37120700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37134482741eSBarry Smith   PetscValidPointer(prefix,2);
3714d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3715d763cef2SBarry Smith   PetscFunctionReturn(0);
3716d763cef2SBarry Smith }
3717d763cef2SBarry Smith 
37184a2ae208SSatish Balay #undef __FUNCT__
37194a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
3720d763cef2SBarry Smith /*@C
3721d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
3722d763cef2SBarry Smith 
3723d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
3724d763cef2SBarry Smith 
3725d763cef2SBarry Smith    Input Parameter:
3726d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
3727d763cef2SBarry Smith 
3728d763cef2SBarry Smith    Output Parameters:
3729e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
3730e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
3731e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
3732e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
3733d763cef2SBarry Smith 
37340298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
3735d763cef2SBarry Smith 
3736d763cef2SBarry Smith    Level: intermediate
3737d763cef2SBarry Smith 
373826d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
3739d763cef2SBarry Smith 
3740d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
3741d763cef2SBarry Smith @*/
3742e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
3743d763cef2SBarry Smith {
3744089b2837SJed Brown   PetscErrorCode ierr;
3745089b2837SJed Brown   SNES           snes;
374624989b8cSPeter Brune   DM             dm;
3747089b2837SJed Brown 
3748d763cef2SBarry Smith   PetscFunctionBegin;
3749089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3750e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
375124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
375224989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
3753d763cef2SBarry Smith   PetscFunctionReturn(0);
3754d763cef2SBarry Smith }
3755d763cef2SBarry Smith 
37561713a123SBarry Smith #undef __FUNCT__
37572eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
37582eca1d9cSJed Brown /*@C
37592eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
37602eca1d9cSJed Brown 
37612eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
37622eca1d9cSJed Brown 
37632eca1d9cSJed Brown    Input Parameter:
37642eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
37652eca1d9cSJed Brown 
37662eca1d9cSJed Brown    Output Parameters:
3767e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
3768e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
37692eca1d9cSJed Brown .  f   - The function to compute the matrices
37702eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
37712eca1d9cSJed Brown 
37720298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
37732eca1d9cSJed Brown 
37742eca1d9cSJed Brown    Level: advanced
37752eca1d9cSJed Brown 
37762eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
37772eca1d9cSJed Brown 
37782eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
37792eca1d9cSJed Brown @*/
3780e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
37812eca1d9cSJed Brown {
3782089b2837SJed Brown   PetscErrorCode ierr;
3783089b2837SJed Brown   SNES           snes;
378424989b8cSPeter Brune   DM             dm;
37850910c330SBarry Smith 
37862eca1d9cSJed Brown   PetscFunctionBegin;
3787089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3788f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
3789e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
379024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
379124989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
37922eca1d9cSJed Brown   PetscFunctionReturn(0);
37932eca1d9cSJed Brown }
37942eca1d9cSJed Brown 
37956083293cSBarry Smith 
37962eca1d9cSJed Brown #undef __FUNCT__
379783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
37981713a123SBarry Smith /*@C
379983a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
38001713a123SBarry Smith    VecView() for the solution at each timestep
38011713a123SBarry Smith 
38021713a123SBarry Smith    Collective on TS
38031713a123SBarry Smith 
38041713a123SBarry Smith    Input Parameters:
38051713a123SBarry Smith +  ts - the TS context
38061713a123SBarry Smith .  step - current time-step
3807142b95e3SSatish Balay .  ptime - current time
38080298fd71SBarry Smith -  dummy - either a viewer or NULL
38091713a123SBarry Smith 
381099fdda47SBarry Smith    Options Database:
381199fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
381299fdda47SBarry Smith 
3813387f4636SBarry Smith    Notes: the initial solution and current solution are not display with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
381499fdda47SBarry Smith        will look bad
381599fdda47SBarry Smith 
38161713a123SBarry Smith    Level: intermediate
38171713a123SBarry Smith 
38181713a123SBarry Smith .keywords: TS,  vector, monitor, view
38191713a123SBarry Smith 
3820a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
38211713a123SBarry Smith @*/
38220910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
38231713a123SBarry Smith {
3824dfbe8321SBarry Smith   PetscErrorCode   ierr;
382583a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
3826473a3ab2SBarry Smith   PetscDraw        draw;
38271713a123SBarry Smith 
38281713a123SBarry Smith   PetscFunctionBegin;
38296083293cSBarry Smith   if (!step && ictx->showinitial) {
38306083293cSBarry Smith     if (!ictx->initialsolution) {
38310910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
38321713a123SBarry Smith     }
38330910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
38346083293cSBarry Smith   }
3835b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
38360dcf80beSBarry Smith 
38376083293cSBarry Smith   if (ictx->showinitial) {
38386083293cSBarry Smith     PetscReal pause;
38396083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
38406083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
38416083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
38426083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
38436083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
38446083293cSBarry Smith   }
38450910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
3846473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
384751fa3d41SBarry Smith     PetscReal xl,yl,xr,yr,h;
3848473a3ab2SBarry Smith     char      time[32];
3849473a3ab2SBarry Smith 
3850473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
385185b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
3852473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
3853473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
385451fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
3855473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
3856473a3ab2SBarry Smith   }
3857473a3ab2SBarry Smith 
38586083293cSBarry Smith   if (ictx->showinitial) {
38596083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
38606083293cSBarry Smith   }
38611713a123SBarry Smith   PetscFunctionReturn(0);
38621713a123SBarry Smith }
38631713a123SBarry Smith 
38642d5ee99bSBarry Smith #undef __FUNCT__
38652d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
38662d5ee99bSBarry Smith /*@C
38672d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
38682d5ee99bSBarry Smith 
38692d5ee99bSBarry Smith    Collective on TS
38702d5ee99bSBarry Smith 
38712d5ee99bSBarry Smith    Input Parameters:
38722d5ee99bSBarry Smith +  ts - the TS context
38732d5ee99bSBarry Smith .  step - current time-step
38742d5ee99bSBarry Smith .  ptime - current time
38752d5ee99bSBarry Smith -  dummy - either a viewer or NULL
38762d5ee99bSBarry Smith 
38772d5ee99bSBarry Smith    Level: intermediate
38782d5ee99bSBarry Smith 
38792d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
38802d5ee99bSBarry Smith 
38812d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
38822d5ee99bSBarry Smith @*/
38832d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
38842d5ee99bSBarry Smith {
38852d5ee99bSBarry Smith   PetscErrorCode    ierr;
38862d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
38872d5ee99bSBarry Smith   PetscDraw         draw;
38882d5ee99bSBarry Smith   MPI_Comm          comm;
38892d5ee99bSBarry Smith   PetscInt          n;
38902d5ee99bSBarry Smith   PetscMPIInt       size;
389151fa3d41SBarry Smith   PetscReal         xl,yl,xr,yr,h;
38922d5ee99bSBarry Smith   char              time[32];
38932d5ee99bSBarry Smith   const PetscScalar *U;
38942d5ee99bSBarry Smith 
38952d5ee99bSBarry Smith   PetscFunctionBegin;
38962d5ee99bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
38972d5ee99bSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
38982d5ee99bSBarry Smith   if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs");
38992d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
39002d5ee99bSBarry Smith   if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns");
39012d5ee99bSBarry Smith 
39022d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
39032d5ee99bSBarry Smith 
39042d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
39054b363babSBarry Smith   ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
3906ba5783adSMatthew G Knepley   if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) {
3907a4494fc1SBarry Smith       ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
3908a4494fc1SBarry Smith       PetscFunctionReturn(0);
3909a4494fc1SBarry Smith   }
39102d5ee99bSBarry Smith   if (!step) ictx->color++;
39112d5ee99bSBarry Smith   ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr);
39122d5ee99bSBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
39132d5ee99bSBarry Smith 
39142d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
39154b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
391685b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
39172d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
391851fa3d41SBarry Smith     ierr = PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
39192d5ee99bSBarry Smith   }
39202d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
39212d5ee99bSBarry Smith   PetscFunctionReturn(0);
39222d5ee99bSBarry Smith }
39232d5ee99bSBarry Smith 
39241713a123SBarry Smith 
39256c699258SBarry Smith #undef __FUNCT__
392683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
39276083293cSBarry Smith /*@C
392883a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
39296083293cSBarry Smith 
39306083293cSBarry Smith    Collective on TS
39316083293cSBarry Smith 
39326083293cSBarry Smith    Input Parameters:
39336083293cSBarry Smith .    ctx - the monitor context
39346083293cSBarry Smith 
39356083293cSBarry Smith    Level: intermediate
39366083293cSBarry Smith 
39376083293cSBarry Smith .keywords: TS,  vector, monitor, view
39386083293cSBarry Smith 
393983a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
39406083293cSBarry Smith @*/
394183a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
39426083293cSBarry Smith {
39436083293cSBarry Smith   PetscErrorCode ierr;
39446083293cSBarry Smith 
39456083293cSBarry Smith   PetscFunctionBegin;
39464b363babSBarry Smith   ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr);
394783a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
394883a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
394983a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
39506083293cSBarry Smith   PetscFunctionReturn(0);
39516083293cSBarry Smith }
39526083293cSBarry Smith 
39536083293cSBarry Smith #undef __FUNCT__
395483a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
39556083293cSBarry Smith /*@C
395683a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
39576083293cSBarry Smith 
39586083293cSBarry Smith    Collective on TS
39596083293cSBarry Smith 
39606083293cSBarry Smith    Input Parameter:
39616083293cSBarry Smith .    ts - time-step context
39626083293cSBarry Smith 
39636083293cSBarry Smith    Output Patameter:
39646083293cSBarry Smith .    ctx - the monitor context
39656083293cSBarry Smith 
396699fdda47SBarry Smith    Options Database:
396799fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
396899fdda47SBarry Smith 
39696083293cSBarry Smith    Level: intermediate
39706083293cSBarry Smith 
39716083293cSBarry Smith .keywords: TS,  vector, monitor, view
39726083293cSBarry Smith 
397383a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
39746083293cSBarry Smith @*/
397583a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
39766083293cSBarry Smith {
39776083293cSBarry Smith   PetscErrorCode   ierr;
39786083293cSBarry Smith 
39796083293cSBarry Smith   PetscFunctionBegin;
3980b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
398183a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
3982e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
3983bbd56ea5SKarl Rupp 
398483a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
3985473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
39860298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
3987473a3ab2SBarry Smith 
3988473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
39890298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
39902d5ee99bSBarry Smith   (*ctx)->color = PETSC_DRAW_WHITE;
39916083293cSBarry Smith   PetscFunctionReturn(0);
39926083293cSBarry Smith }
39936083293cSBarry Smith 
39946083293cSBarry Smith #undef __FUNCT__
399583a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
39963a471f94SBarry Smith /*@C
399783a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
39983a471f94SBarry Smith    VecView() for the error at each timestep
39993a471f94SBarry Smith 
40003a471f94SBarry Smith    Collective on TS
40013a471f94SBarry Smith 
40023a471f94SBarry Smith    Input Parameters:
40033a471f94SBarry Smith +  ts - the TS context
40043a471f94SBarry Smith .  step - current time-step
40053a471f94SBarry Smith .  ptime - current time
40060298fd71SBarry Smith -  dummy - either a viewer or NULL
40073a471f94SBarry Smith 
40083a471f94SBarry Smith    Level: intermediate
40093a471f94SBarry Smith 
40103a471f94SBarry Smith .keywords: TS,  vector, monitor, view
40113a471f94SBarry Smith 
40123a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
40133a471f94SBarry Smith @*/
40140910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
40153a471f94SBarry Smith {
40163a471f94SBarry Smith   PetscErrorCode   ierr;
401783a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
401883a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
40193a471f94SBarry Smith   Vec              work;
40203a471f94SBarry Smith 
40213a471f94SBarry Smith   PetscFunctionBegin;
4022b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
40230910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
40243a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
40250910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
40263a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
40273a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
40283a471f94SBarry Smith   PetscFunctionReturn(0);
40293a471f94SBarry Smith }
40303a471f94SBarry Smith 
4031af0996ceSBarry Smith #include <petsc/private/dmimpl.h>
40323a471f94SBarry Smith #undef __FUNCT__
40336c699258SBarry Smith #define __FUNCT__ "TSSetDM"
40346c699258SBarry Smith /*@
40356c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
40366c699258SBarry Smith 
40373f9fe445SBarry Smith    Logically Collective on TS and DM
40386c699258SBarry Smith 
40396c699258SBarry Smith    Input Parameters:
40406c699258SBarry Smith +  ts - the preconditioner context
40416c699258SBarry Smith -  dm - the dm
40426c699258SBarry Smith 
40436c699258SBarry Smith    Level: intermediate
40446c699258SBarry Smith 
40456c699258SBarry Smith 
40466c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
40476c699258SBarry Smith @*/
40487087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
40496c699258SBarry Smith {
40506c699258SBarry Smith   PetscErrorCode ierr;
4051089b2837SJed Brown   SNES           snes;
4052942e3340SBarry Smith   DMTS           tsdm;
40536c699258SBarry Smith 
40546c699258SBarry Smith   PetscFunctionBegin;
40550700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
405670663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4057942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
40582a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4059942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4060942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
406124989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
406224989b8cSPeter Brune         tsdm->originaldm = dm;
406324989b8cSPeter Brune       }
406424989b8cSPeter Brune     }
40656bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
406624989b8cSPeter Brune   }
40676c699258SBarry Smith   ts->dm = dm;
4068bbd56ea5SKarl Rupp 
4069089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4070089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
40716c699258SBarry Smith   PetscFunctionReturn(0);
40726c699258SBarry Smith }
40736c699258SBarry Smith 
40746c699258SBarry Smith #undef __FUNCT__
40756c699258SBarry Smith #define __FUNCT__ "TSGetDM"
40766c699258SBarry Smith /*@
40776c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
40786c699258SBarry Smith 
40793f9fe445SBarry Smith    Not Collective
40806c699258SBarry Smith 
40816c699258SBarry Smith    Input Parameter:
40826c699258SBarry Smith . ts - the preconditioner context
40836c699258SBarry Smith 
40846c699258SBarry Smith    Output Parameter:
40856c699258SBarry Smith .  dm - the dm
40866c699258SBarry Smith 
40876c699258SBarry Smith    Level: intermediate
40886c699258SBarry Smith 
40896c699258SBarry Smith 
40906c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
40916c699258SBarry Smith @*/
40927087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
40936c699258SBarry Smith {
4094496e6a7aSJed Brown   PetscErrorCode ierr;
4095496e6a7aSJed Brown 
40966c699258SBarry Smith   PetscFunctionBegin;
40970700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4098496e6a7aSJed Brown   if (!ts->dm) {
4099ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4100496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4101496e6a7aSJed Brown   }
41026c699258SBarry Smith   *dm = ts->dm;
41036c699258SBarry Smith   PetscFunctionReturn(0);
41046c699258SBarry Smith }
41051713a123SBarry Smith 
41060f5c6efeSJed Brown #undef __FUNCT__
41070f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
41080f5c6efeSJed Brown /*@
41090f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
41100f5c6efeSJed Brown 
41113f9fe445SBarry Smith    Logically Collective on SNES
41120f5c6efeSJed Brown 
41130f5c6efeSJed Brown    Input Parameter:
4114d42a1c89SJed Brown + snes - nonlinear solver
41150910c330SBarry Smith . U - the current state at which to evaluate the residual
4116d42a1c89SJed Brown - ctx - user context, must be a TS
41170f5c6efeSJed Brown 
41180f5c6efeSJed Brown    Output Parameter:
41190f5c6efeSJed Brown . F - the nonlinear residual
41200f5c6efeSJed Brown 
41210f5c6efeSJed Brown    Notes:
41220f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
41230f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
41240f5c6efeSJed Brown 
41250f5c6efeSJed Brown    Level: advanced
41260f5c6efeSJed Brown 
41270f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
41280f5c6efeSJed Brown @*/
41290910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
41300f5c6efeSJed Brown {
41310f5c6efeSJed Brown   TS             ts = (TS)ctx;
41320f5c6efeSJed Brown   PetscErrorCode ierr;
41330f5c6efeSJed Brown 
41340f5c6efeSJed Brown   PetscFunctionBegin;
41350f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
41360910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
41370f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
41380f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
41390910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
41400f5c6efeSJed Brown   PetscFunctionReturn(0);
41410f5c6efeSJed Brown }
41420f5c6efeSJed Brown 
41430f5c6efeSJed Brown #undef __FUNCT__
41440f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
41450f5c6efeSJed Brown /*@
41460f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
41470f5c6efeSJed Brown 
41480f5c6efeSJed Brown    Collective on SNES
41490f5c6efeSJed Brown 
41500f5c6efeSJed Brown    Input Parameter:
41510f5c6efeSJed Brown + snes - nonlinear solver
41520910c330SBarry Smith . U - the current state at which to evaluate the residual
41530f5c6efeSJed Brown - ctx - user context, must be a TS
41540f5c6efeSJed Brown 
41550f5c6efeSJed Brown    Output Parameter:
41560f5c6efeSJed Brown + A - the Jacobian
41570f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
41580f5c6efeSJed Brown - flag - indicates any structure change in the matrix
41590f5c6efeSJed Brown 
41600f5c6efeSJed Brown    Notes:
41610f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
41620f5c6efeSJed Brown 
41630f5c6efeSJed Brown    Level: developer
41640f5c6efeSJed Brown 
41650f5c6efeSJed Brown .seealso: SNESSetJacobian()
41660f5c6efeSJed Brown @*/
4167d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
41680f5c6efeSJed Brown {
41690f5c6efeSJed Brown   TS             ts = (TS)ctx;
41700f5c6efeSJed Brown   PetscErrorCode ierr;
41710f5c6efeSJed Brown 
41720f5c6efeSJed Brown   PetscFunctionBegin;
41730f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
41740910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
41750f5c6efeSJed Brown   PetscValidPointer(A,3);
417694ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
41770f5c6efeSJed Brown   PetscValidPointer(B,4);
417894ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
41790f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4180d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
41810f5c6efeSJed Brown   PetscFunctionReturn(0);
41820f5c6efeSJed Brown }
4183325fc9f4SBarry Smith 
41840e4ef248SJed Brown #undef __FUNCT__
41850e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
41860e4ef248SJed Brown /*@C
41870e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
41880e4ef248SJed Brown 
41890e4ef248SJed Brown    Collective on TS
41900e4ef248SJed Brown 
41910e4ef248SJed Brown    Input Arguments:
41920e4ef248SJed Brown +  ts - time stepping context
41930e4ef248SJed Brown .  t - time at which to evaluate
41940910c330SBarry Smith .  U - state at which to evaluate
41950e4ef248SJed Brown -  ctx - context
41960e4ef248SJed Brown 
41970e4ef248SJed Brown    Output Arguments:
41980e4ef248SJed Brown .  F - right hand side
41990e4ef248SJed Brown 
42000e4ef248SJed Brown    Level: intermediate
42010e4ef248SJed Brown 
42020e4ef248SJed Brown    Notes:
42030e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
42040e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
42050e4ef248SJed Brown 
42060e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
42070e4ef248SJed Brown @*/
42080910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
42090e4ef248SJed Brown {
42100e4ef248SJed Brown   PetscErrorCode ierr;
42110e4ef248SJed Brown   Mat            Arhs,Brhs;
42120e4ef248SJed Brown 
42130e4ef248SJed Brown   PetscFunctionBegin;
42140e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4215d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
42160910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
42170e4ef248SJed Brown   PetscFunctionReturn(0);
42180e4ef248SJed Brown }
42190e4ef248SJed Brown 
42200e4ef248SJed Brown #undef __FUNCT__
42210e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
42220e4ef248SJed Brown /*@C
42230e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
42240e4ef248SJed Brown 
42250e4ef248SJed Brown    Collective on TS
42260e4ef248SJed Brown 
42270e4ef248SJed Brown    Input Arguments:
42280e4ef248SJed Brown +  ts - time stepping context
42290e4ef248SJed Brown .  t - time at which to evaluate
42300910c330SBarry Smith .  U - state at which to evaluate
42310e4ef248SJed Brown -  ctx - context
42320e4ef248SJed Brown 
42330e4ef248SJed Brown    Output Arguments:
42340e4ef248SJed Brown +  A - pointer to operator
42350e4ef248SJed Brown .  B - pointer to preconditioning matrix
42360e4ef248SJed Brown -  flg - matrix structure flag
42370e4ef248SJed Brown 
42380e4ef248SJed Brown    Level: intermediate
42390e4ef248SJed Brown 
42400e4ef248SJed Brown    Notes:
42410e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
42420e4ef248SJed Brown 
42430e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
42440e4ef248SJed Brown @*/
4245d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
42460e4ef248SJed Brown {
42470e4ef248SJed Brown   PetscFunctionBegin;
42480e4ef248SJed Brown   PetscFunctionReturn(0);
42490e4ef248SJed Brown }
42500e4ef248SJed Brown 
42510026cea9SSean Farley #undef __FUNCT__
42520026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
42530026cea9SSean Farley /*@C
42540026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
42550026cea9SSean Farley 
42560026cea9SSean Farley    Collective on TS
42570026cea9SSean Farley 
42580026cea9SSean Farley    Input Arguments:
42590026cea9SSean Farley +  ts - time stepping context
42600026cea9SSean Farley .  t - time at which to evaluate
42610910c330SBarry Smith .  U - state at which to evaluate
42620910c330SBarry Smith .  Udot - time derivative of state vector
42630026cea9SSean Farley -  ctx - context
42640026cea9SSean Farley 
42650026cea9SSean Farley    Output Arguments:
42660026cea9SSean Farley .  F - left hand side
42670026cea9SSean Farley 
42680026cea9SSean Farley    Level: intermediate
42690026cea9SSean Farley 
42700026cea9SSean Farley    Notes:
42710910c330SBarry Smith    The assumption here is that the left hand side is of the form A*Udot (and not A*Udot + B*U). For other cases, the
42720026cea9SSean Farley    user is required to write their own TSComputeIFunction.
42730026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
42740026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
42750026cea9SSean Farley 
42760026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
42770026cea9SSean Farley @*/
42780910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
42790026cea9SSean Farley {
42800026cea9SSean Farley   PetscErrorCode ierr;
42810026cea9SSean Farley   Mat            A,B;
42820026cea9SSean Farley 
42830026cea9SSean Farley   PetscFunctionBegin;
42840298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4285d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
42860910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
42870026cea9SSean Farley   PetscFunctionReturn(0);
42880026cea9SSean Farley }
42890026cea9SSean Farley 
42900026cea9SSean Farley #undef __FUNCT__
42910026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
42920026cea9SSean Farley /*@C
4293b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
42940026cea9SSean Farley 
42950026cea9SSean Farley    Collective on TS
42960026cea9SSean Farley 
42970026cea9SSean Farley    Input Arguments:
42980026cea9SSean Farley +  ts - time stepping context
42990026cea9SSean Farley .  t - time at which to evaluate
43000910c330SBarry Smith .  U - state at which to evaluate
43010910c330SBarry Smith .  Udot - time derivative of state vector
43020026cea9SSean Farley .  shift - shift to apply
43030026cea9SSean Farley -  ctx - context
43040026cea9SSean Farley 
43050026cea9SSean Farley    Output Arguments:
43060026cea9SSean Farley +  A - pointer to operator
43070026cea9SSean Farley .  B - pointer to preconditioning matrix
43080026cea9SSean Farley -  flg - matrix structure flag
43090026cea9SSean Farley 
4310b41af12eSJed Brown    Level: advanced
43110026cea9SSean Farley 
43120026cea9SSean Farley    Notes:
43130026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
43140026cea9SSean Farley 
4315b41af12eSJed Brown    It is only appropriate for problems of the form
4316b41af12eSJed Brown 
4317b41af12eSJed Brown $     M Udot = F(U,t)
4318b41af12eSJed Brown 
4319b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4320b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4321b41af12eSJed Brown   an implicit operator of the form
4322b41af12eSJed Brown 
4323b41af12eSJed Brown $    shift*M + J
4324b41af12eSJed Brown 
4325b41af12eSJed Brown   where J is the Jacobian of -F(U).  Support may be added in a future version of PETSc, but for now, the user must store
4326b41af12eSJed Brown   a copy of M or reassemble it when requested.
4327b41af12eSJed Brown 
43280026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
43290026cea9SSean Farley @*/
4330d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
43310026cea9SSean Farley {
4332b41af12eSJed Brown   PetscErrorCode ierr;
4333b41af12eSJed Brown 
43340026cea9SSean Farley   PetscFunctionBegin;
433594ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4336b41af12eSJed Brown   ts->ijacobian.shift = shift;
43370026cea9SSean Farley   PetscFunctionReturn(0);
43380026cea9SSean Farley }
4339b41af12eSJed Brown 
4340e817cc15SEmil Constantinescu #undef __FUNCT__
4341e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
4342e817cc15SEmil Constantinescu /*@
4343e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4344e817cc15SEmil Constantinescu 
4345e817cc15SEmil Constantinescu    Not Collective
4346e817cc15SEmil Constantinescu 
4347e817cc15SEmil Constantinescu    Input Parameter:
4348e817cc15SEmil Constantinescu .  ts - the TS context
4349e817cc15SEmil Constantinescu 
4350e817cc15SEmil Constantinescu    Output Parameter:
43514e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4352e817cc15SEmil Constantinescu 
4353e817cc15SEmil Constantinescu    Level: beginner
4354e817cc15SEmil Constantinescu 
4355e817cc15SEmil Constantinescu .keywords: TS, equation type
4356e817cc15SEmil Constantinescu 
4357e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4358e817cc15SEmil Constantinescu @*/
4359e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4360e817cc15SEmil Constantinescu {
4361e817cc15SEmil Constantinescu   PetscFunctionBegin;
4362e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4363e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4364e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4365e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4366e817cc15SEmil Constantinescu }
4367e817cc15SEmil Constantinescu 
4368e817cc15SEmil Constantinescu #undef __FUNCT__
4369e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
4370e817cc15SEmil Constantinescu /*@
4371e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4372e817cc15SEmil Constantinescu 
4373e817cc15SEmil Constantinescu    Not Collective
4374e817cc15SEmil Constantinescu 
4375e817cc15SEmil Constantinescu    Input Parameter:
4376e817cc15SEmil Constantinescu +  ts - the TS context
43771297b224SEmil Constantinescu -  equation_type - see TSEquationType
4378e817cc15SEmil Constantinescu 
4379e817cc15SEmil Constantinescu    Level: advanced
4380e817cc15SEmil Constantinescu 
4381e817cc15SEmil Constantinescu .keywords: TS, equation type
4382e817cc15SEmil Constantinescu 
4383e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4384e817cc15SEmil Constantinescu @*/
4385e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4386e817cc15SEmil Constantinescu {
4387e817cc15SEmil Constantinescu   PetscFunctionBegin;
4388e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4389e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4390e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4391e817cc15SEmil Constantinescu }
43920026cea9SSean Farley 
43934af1b03aSJed Brown #undef __FUNCT__
43944af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
43954af1b03aSJed Brown /*@
43964af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
43974af1b03aSJed Brown 
43984af1b03aSJed Brown    Not Collective
43994af1b03aSJed Brown 
44004af1b03aSJed Brown    Input Parameter:
44014af1b03aSJed Brown .  ts - the TS context
44024af1b03aSJed Brown 
44034af1b03aSJed Brown    Output Parameter:
44044af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
44054af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
44064af1b03aSJed Brown 
4407487e0bb9SJed Brown    Level: beginner
44084af1b03aSJed Brown 
4409cd652676SJed Brown    Notes:
4410cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
44114af1b03aSJed Brown 
44124af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
44134af1b03aSJed Brown 
44144af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
44154af1b03aSJed Brown @*/
44164af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
44174af1b03aSJed Brown {
44184af1b03aSJed Brown   PetscFunctionBegin;
44194af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44204af1b03aSJed Brown   PetscValidPointer(reason,2);
44214af1b03aSJed Brown   *reason = ts->reason;
44224af1b03aSJed Brown   PetscFunctionReturn(0);
44234af1b03aSJed Brown }
44244af1b03aSJed Brown 
4425fb1732b5SBarry Smith #undef __FUNCT__
4426d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
4427d6ad946cSShri Abhyankar /*@
4428d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4429d6ad946cSShri Abhyankar 
4430d6ad946cSShri Abhyankar    Not Collective
4431d6ad946cSShri Abhyankar 
4432d6ad946cSShri Abhyankar    Input Parameter:
4433d6ad946cSShri Abhyankar +  ts - the TS context
4434d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4435d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4436d6ad946cSShri Abhyankar 
4437f5abba47SShri Abhyankar    Level: advanced
4438d6ad946cSShri Abhyankar 
4439d6ad946cSShri Abhyankar    Notes:
4440d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
4441d6ad946cSShri Abhyankar 
4442d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
4443d6ad946cSShri Abhyankar 
4444d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4445d6ad946cSShri Abhyankar @*/
4446d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4447d6ad946cSShri Abhyankar {
4448d6ad946cSShri Abhyankar   PetscFunctionBegin;
4449d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4450d6ad946cSShri Abhyankar   ts->reason = reason;
4451d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4452d6ad946cSShri Abhyankar }
4453d6ad946cSShri Abhyankar 
4454d6ad946cSShri Abhyankar #undef __FUNCT__
4455cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
4456cc708dedSBarry Smith /*@
4457cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4458cc708dedSBarry Smith 
4459cc708dedSBarry Smith    Not Collective
4460cc708dedSBarry Smith 
4461cc708dedSBarry Smith    Input Parameter:
4462cc708dedSBarry Smith .  ts - the TS context
4463cc708dedSBarry Smith 
4464cc708dedSBarry Smith    Output Parameter:
4465cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
4466cc708dedSBarry Smith 
4467487e0bb9SJed Brown    Level: beginner
4468cc708dedSBarry Smith 
4469cc708dedSBarry Smith    Notes:
4470cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4471cc708dedSBarry Smith 
4472cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
4473cc708dedSBarry Smith 
4474cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4475cc708dedSBarry Smith @*/
4476cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4477cc708dedSBarry Smith {
4478cc708dedSBarry Smith   PetscFunctionBegin;
4479cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4480cc708dedSBarry Smith   PetscValidPointer(ftime,2);
4481cc708dedSBarry Smith   *ftime = ts->solvetime;
4482cc708dedSBarry Smith   PetscFunctionReturn(0);
4483cc708dedSBarry Smith }
4484cc708dedSBarry Smith 
4485cc708dedSBarry Smith #undef __FUNCT__
44862c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps"
44872c18e0fdSBarry Smith /*@
44882c18e0fdSBarry Smith    TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate()
44892c18e0fdSBarry Smith 
44902c18e0fdSBarry Smith    Not Collective
44912c18e0fdSBarry Smith 
44922c18e0fdSBarry Smith    Input Parameter:
44932c18e0fdSBarry Smith .  ts - the TS context
44942c18e0fdSBarry Smith 
44952c18e0fdSBarry Smith    Output Parameter:
44962c18e0fdSBarry Smith .  steps - the number of steps
44972c18e0fdSBarry Smith 
44982c18e0fdSBarry Smith    Level: beginner
44992c18e0fdSBarry Smith 
45002c18e0fdSBarry Smith    Notes:
45012c18e0fdSBarry Smith    Includes the number of steps for all calls to TSSolve() since TSSetUp() was called
45022c18e0fdSBarry Smith 
45032c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test
45042c18e0fdSBarry Smith 
45052c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
45062c18e0fdSBarry Smith @*/
45072c18e0fdSBarry Smith PetscErrorCode  TSGetTotalSteps(TS ts,PetscInt *steps)
45082c18e0fdSBarry Smith {
45092c18e0fdSBarry Smith   PetscFunctionBegin;
45102c18e0fdSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45112c18e0fdSBarry Smith   PetscValidPointer(steps,2);
45122c18e0fdSBarry Smith   *steps = ts->total_steps;
45132c18e0fdSBarry Smith   PetscFunctionReturn(0);
45142c18e0fdSBarry Smith }
45152c18e0fdSBarry Smith 
45162c18e0fdSBarry Smith #undef __FUNCT__
45175ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
45189f67acb7SJed Brown /*@
45195ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
45209f67acb7SJed Brown    used by the time integrator.
45219f67acb7SJed Brown 
45229f67acb7SJed Brown    Not Collective
45239f67acb7SJed Brown 
45249f67acb7SJed Brown    Input Parameter:
45259f67acb7SJed Brown .  ts - TS context
45269f67acb7SJed Brown 
45279f67acb7SJed Brown    Output Parameter:
45289f67acb7SJed Brown .  nits - number of nonlinear iterations
45299f67acb7SJed Brown 
45309f67acb7SJed Brown    Notes:
45319f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
45329f67acb7SJed Brown 
45339f67acb7SJed Brown    Level: intermediate
45349f67acb7SJed Brown 
45359f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
45369f67acb7SJed Brown 
45375ef26d82SJed Brown .seealso:  TSGetKSPIterations()
45389f67acb7SJed Brown @*/
45395ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
45409f67acb7SJed Brown {
45419f67acb7SJed Brown   PetscFunctionBegin;
45429f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45439f67acb7SJed Brown   PetscValidIntPointer(nits,2);
45445ef26d82SJed Brown   *nits = ts->snes_its;
45459f67acb7SJed Brown   PetscFunctionReturn(0);
45469f67acb7SJed Brown }
45479f67acb7SJed Brown 
45489f67acb7SJed Brown #undef __FUNCT__
45495ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
45509f67acb7SJed Brown /*@
45515ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
45529f67acb7SJed Brown    used by the time integrator.
45539f67acb7SJed Brown 
45549f67acb7SJed Brown    Not Collective
45559f67acb7SJed Brown 
45569f67acb7SJed Brown    Input Parameter:
45579f67acb7SJed Brown .  ts - TS context
45589f67acb7SJed Brown 
45599f67acb7SJed Brown    Output Parameter:
45609f67acb7SJed Brown .  lits - number of linear iterations
45619f67acb7SJed Brown 
45629f67acb7SJed Brown    Notes:
45639f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
45649f67acb7SJed Brown 
45659f67acb7SJed Brown    Level: intermediate
45669f67acb7SJed Brown 
45679f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
45689f67acb7SJed Brown 
45695ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
45709f67acb7SJed Brown @*/
45715ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
45729f67acb7SJed Brown {
45739f67acb7SJed Brown   PetscFunctionBegin;
45749f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45759f67acb7SJed Brown   PetscValidIntPointer(lits,2);
45765ef26d82SJed Brown   *lits = ts->ksp_its;
45779f67acb7SJed Brown   PetscFunctionReturn(0);
45789f67acb7SJed Brown }
45799f67acb7SJed Brown 
45809f67acb7SJed Brown #undef __FUNCT__
4581cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
4582cef5090cSJed Brown /*@
4583cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
4584cef5090cSJed Brown 
4585cef5090cSJed Brown    Not Collective
4586cef5090cSJed Brown 
4587cef5090cSJed Brown    Input Parameter:
4588cef5090cSJed Brown .  ts - TS context
4589cef5090cSJed Brown 
4590cef5090cSJed Brown    Output Parameter:
4591cef5090cSJed Brown .  rejects - number of steps rejected
4592cef5090cSJed Brown 
4593cef5090cSJed Brown    Notes:
4594cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4595cef5090cSJed Brown 
4596cef5090cSJed Brown    Level: intermediate
4597cef5090cSJed Brown 
4598cef5090cSJed Brown .keywords: TS, get, number
4599cef5090cSJed Brown 
46005ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
4601cef5090cSJed Brown @*/
4602cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
4603cef5090cSJed Brown {
4604cef5090cSJed Brown   PetscFunctionBegin;
4605cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4606cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
4607cef5090cSJed Brown   *rejects = ts->reject;
4608cef5090cSJed Brown   PetscFunctionReturn(0);
4609cef5090cSJed Brown }
4610cef5090cSJed Brown 
4611cef5090cSJed Brown #undef __FUNCT__
4612cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
4613cef5090cSJed Brown /*@
4614cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
4615cef5090cSJed Brown 
4616cef5090cSJed Brown    Not Collective
4617cef5090cSJed Brown 
4618cef5090cSJed Brown    Input Parameter:
4619cef5090cSJed Brown .  ts - TS context
4620cef5090cSJed Brown 
4621cef5090cSJed Brown    Output Parameter:
4622cef5090cSJed Brown .  fails - number of failed nonlinear solves
4623cef5090cSJed Brown 
4624cef5090cSJed Brown    Notes:
4625cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4626cef5090cSJed Brown 
4627cef5090cSJed Brown    Level: intermediate
4628cef5090cSJed Brown 
4629cef5090cSJed Brown .keywords: TS, get, number
4630cef5090cSJed Brown 
46315ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
4632cef5090cSJed Brown @*/
4633cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
4634cef5090cSJed Brown {
4635cef5090cSJed Brown   PetscFunctionBegin;
4636cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4637cef5090cSJed Brown   PetscValidIntPointer(fails,2);
4638cef5090cSJed Brown   *fails = ts->num_snes_failures;
4639cef5090cSJed Brown   PetscFunctionReturn(0);
4640cef5090cSJed Brown }
4641cef5090cSJed Brown 
4642cef5090cSJed Brown #undef __FUNCT__
4643cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
4644cef5090cSJed Brown /*@
4645cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
4646cef5090cSJed Brown 
4647cef5090cSJed Brown    Not Collective
4648cef5090cSJed Brown 
4649cef5090cSJed Brown    Input Parameter:
4650cef5090cSJed Brown +  ts - TS context
4651cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
4652cef5090cSJed Brown 
4653cef5090cSJed Brown    Notes:
4654cef5090cSJed Brown    The counter is reset to zero for each step
4655cef5090cSJed Brown 
4656cef5090cSJed Brown    Options Database Key:
4657cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
4658cef5090cSJed Brown 
4659cef5090cSJed Brown    Level: intermediate
4660cef5090cSJed Brown 
4661cef5090cSJed Brown .keywords: TS, set, maximum, number
4662cef5090cSJed Brown 
46635ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4664cef5090cSJed Brown @*/
4665cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
4666cef5090cSJed Brown {
4667cef5090cSJed Brown   PetscFunctionBegin;
4668cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4669cef5090cSJed Brown   ts->max_reject = rejects;
4670cef5090cSJed Brown   PetscFunctionReturn(0);
4671cef5090cSJed Brown }
4672cef5090cSJed Brown 
4673cef5090cSJed Brown #undef __FUNCT__
4674cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
4675cef5090cSJed Brown /*@
4676cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
4677cef5090cSJed Brown 
4678cef5090cSJed Brown    Not Collective
4679cef5090cSJed Brown 
4680cef5090cSJed Brown    Input Parameter:
4681cef5090cSJed Brown +  ts - TS context
4682cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
4683cef5090cSJed Brown 
4684cef5090cSJed Brown    Notes:
4685cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
4686cef5090cSJed Brown 
4687cef5090cSJed Brown    Options Database Key:
4688cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
4689cef5090cSJed Brown 
4690cef5090cSJed Brown    Level: intermediate
4691cef5090cSJed Brown 
4692cef5090cSJed Brown .keywords: TS, set, maximum, number
4693cef5090cSJed Brown 
46945ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
4695cef5090cSJed Brown @*/
4696cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
4697cef5090cSJed Brown {
4698cef5090cSJed Brown   PetscFunctionBegin;
4699cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4700cef5090cSJed Brown   ts->max_snes_failures = fails;
4701cef5090cSJed Brown   PetscFunctionReturn(0);
4702cef5090cSJed Brown }
4703cef5090cSJed Brown 
4704cef5090cSJed Brown #undef __FUNCT__
47054e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails"
4706cef5090cSJed Brown /*@
4707cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
4708cef5090cSJed Brown 
4709cef5090cSJed Brown    Not Collective
4710cef5090cSJed Brown 
4711cef5090cSJed Brown    Input Parameter:
4712cef5090cSJed Brown +  ts - TS context
4713cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
4714cef5090cSJed Brown 
4715cef5090cSJed Brown    Options Database Key:
4716cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
4717cef5090cSJed Brown 
4718cef5090cSJed Brown    Level: intermediate
4719cef5090cSJed Brown 
4720cef5090cSJed Brown .keywords: TS, set, error
4721cef5090cSJed Brown 
47225ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4723cef5090cSJed Brown @*/
4724cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
4725cef5090cSJed Brown {
4726cef5090cSJed Brown   PetscFunctionBegin;
4727cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4728cef5090cSJed Brown   ts->errorifstepfailed = err;
4729cef5090cSJed Brown   PetscFunctionReturn(0);
4730cef5090cSJed Brown }
4731cef5090cSJed Brown 
4732cef5090cSJed Brown #undef __FUNCT__
4733fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
4734fb1732b5SBarry Smith /*@C
4735fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
4736fb1732b5SBarry Smith 
4737fb1732b5SBarry Smith    Collective on TS
4738fb1732b5SBarry Smith 
4739fb1732b5SBarry Smith    Input Parameters:
4740fb1732b5SBarry Smith +  ts - the TS context
4741fb1732b5SBarry Smith .  step - current time-step
4742fb1732b5SBarry Smith .  ptime - current time
47430910c330SBarry Smith .  u - current state
4744fb1732b5SBarry Smith -  viewer - binary viewer
4745fb1732b5SBarry Smith 
4746fb1732b5SBarry Smith    Level: intermediate
4747fb1732b5SBarry Smith 
4748fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
4749fb1732b5SBarry Smith 
4750fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4751fb1732b5SBarry Smith @*/
47520910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
4753fb1732b5SBarry Smith {
4754fb1732b5SBarry Smith   PetscErrorCode ierr;
4755ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
4756fb1732b5SBarry Smith 
4757fb1732b5SBarry Smith   PetscFunctionBegin;
47580910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
4759ed81e22dSJed Brown   PetscFunctionReturn(0);
4760ed81e22dSJed Brown }
4761ed81e22dSJed Brown 
4762ed81e22dSJed Brown #undef __FUNCT__
4763ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
4764ed81e22dSJed Brown /*@C
4765ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
4766ed81e22dSJed Brown 
4767ed81e22dSJed Brown    Collective on TS
4768ed81e22dSJed Brown 
4769ed81e22dSJed Brown    Input Parameters:
4770ed81e22dSJed Brown +  ts - the TS context
4771ed81e22dSJed Brown .  step - current time-step
4772ed81e22dSJed Brown .  ptime - current time
47730910c330SBarry Smith .  u - current state
4774ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4775ed81e22dSJed Brown 
4776ed81e22dSJed Brown    Level: intermediate
4777ed81e22dSJed Brown 
4778ed81e22dSJed Brown    Notes:
4779ed81e22dSJed Brown    The VTK format does not allow writing multiple time steps in the same file, therefore a different file will be written for each time step.
4780ed81e22dSJed Brown    These are named according to the file name template.
4781ed81e22dSJed Brown 
4782ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
4783ed81e22dSJed Brown 
4784ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4785ed81e22dSJed Brown 
4786ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4787ed81e22dSJed Brown @*/
47880910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
4789ed81e22dSJed Brown {
4790ed81e22dSJed Brown   PetscErrorCode ierr;
4791ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
4792ed81e22dSJed Brown   PetscViewer    viewer;
4793ed81e22dSJed Brown 
4794ed81e22dSJed Brown   PetscFunctionBegin;
47958caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
4796ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
47970910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
4798ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4799ed81e22dSJed Brown   PetscFunctionReturn(0);
4800ed81e22dSJed Brown }
4801ed81e22dSJed Brown 
4802ed81e22dSJed Brown #undef __FUNCT__
4803ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
4804ed81e22dSJed Brown /*@C
4805ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
4806ed81e22dSJed Brown 
4807ed81e22dSJed Brown    Collective on TS
4808ed81e22dSJed Brown 
4809ed81e22dSJed Brown    Input Parameters:
4810ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4811ed81e22dSJed Brown 
4812ed81e22dSJed Brown    Level: intermediate
4813ed81e22dSJed Brown 
4814ed81e22dSJed Brown    Note:
4815ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
4816ed81e22dSJed Brown 
4817ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4818ed81e22dSJed Brown 
4819ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
4820ed81e22dSJed Brown @*/
4821ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
4822ed81e22dSJed Brown {
4823ed81e22dSJed Brown   PetscErrorCode ierr;
4824ed81e22dSJed Brown 
4825ed81e22dSJed Brown   PetscFunctionBegin;
4826ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
4827fb1732b5SBarry Smith   PetscFunctionReturn(0);
4828fb1732b5SBarry Smith }
4829fb1732b5SBarry Smith 
483084df9cb4SJed Brown #undef __FUNCT__
4831552698daSJed Brown #define __FUNCT__ "TSGetAdapt"
483284df9cb4SJed Brown /*@
4833552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
483484df9cb4SJed Brown 
4835ed81e22dSJed Brown    Collective on TS if controller has not been created yet
483684df9cb4SJed Brown 
483784df9cb4SJed Brown    Input Arguments:
4838ed81e22dSJed Brown .  ts - time stepping context
483984df9cb4SJed Brown 
484084df9cb4SJed Brown    Output Arguments:
4841ed81e22dSJed Brown .  adapt - adaptive controller
484284df9cb4SJed Brown 
484384df9cb4SJed Brown    Level: intermediate
484484df9cb4SJed Brown 
4845ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
484684df9cb4SJed Brown @*/
4847552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
484884df9cb4SJed Brown {
484984df9cb4SJed Brown   PetscErrorCode ierr;
485084df9cb4SJed Brown 
485184df9cb4SJed Brown   PetscFunctionBegin;
485284df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
485384df9cb4SJed Brown   PetscValidPointer(adapt,2);
485484df9cb4SJed Brown   if (!ts->adapt) {
4855ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
48563bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
48571c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
485884df9cb4SJed Brown   }
485984df9cb4SJed Brown   *adapt = ts->adapt;
486084df9cb4SJed Brown   PetscFunctionReturn(0);
486184df9cb4SJed Brown }
4862d6ebe24aSShri Abhyankar 
4863d6ebe24aSShri Abhyankar #undef __FUNCT__
48641c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
48651c3436cfSJed Brown /*@
48661c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
48671c3436cfSJed Brown 
48681c3436cfSJed Brown    Logically Collective
48691c3436cfSJed Brown 
48701c3436cfSJed Brown    Input Arguments:
48711c3436cfSJed Brown +  ts - time integration context
48721c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
48730298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
48741c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
48750298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
48761c3436cfSJed Brown 
4877a3cdaa26SBarry Smith    Options Database keys:
4878a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
4879a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
4880a3cdaa26SBarry Smith 
48813ff766beSShri Abhyankar    Notes:
48823ff766beSShri Abhyankar    With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error
48833ff766beSShri Abhyankar    (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be
48843ff766beSShri Abhyankar    computed only for the differential or the algebraic part then this can be done using the vector of
48853ff766beSShri Abhyankar    tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the
48863ff766beSShri Abhyankar    differential part and infinity for the algebraic part, the LTE calculation will include only the
48873ff766beSShri Abhyankar    differential variables.
48883ff766beSShri Abhyankar 
48891c3436cfSJed Brown    Level: beginner
48901c3436cfSJed Brown 
4891c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
48921c3436cfSJed Brown @*/
48931c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
48941c3436cfSJed Brown {
48951c3436cfSJed Brown   PetscErrorCode ierr;
48961c3436cfSJed Brown 
48971c3436cfSJed Brown   PetscFunctionBegin;
4898c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
48991c3436cfSJed Brown   if (vatol) {
49001c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
49011c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
4902bbd56ea5SKarl Rupp 
49031c3436cfSJed Brown     ts->vatol = vatol;
49041c3436cfSJed Brown   }
4905c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
49061c3436cfSJed Brown   if (vrtol) {
49071c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
49081c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
4909bbd56ea5SKarl Rupp 
49101c3436cfSJed Brown     ts->vrtol = vrtol;
49111c3436cfSJed Brown   }
49121c3436cfSJed Brown   PetscFunctionReturn(0);
49131c3436cfSJed Brown }
49141c3436cfSJed Brown 
49151c3436cfSJed Brown #undef __FUNCT__
4916c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
4917c5033834SJed Brown /*@
4918c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
4919c5033834SJed Brown 
4920c5033834SJed Brown    Logically Collective
4921c5033834SJed Brown 
4922c5033834SJed Brown    Input Arguments:
4923c5033834SJed Brown .  ts - time integration context
4924c5033834SJed Brown 
4925c5033834SJed Brown    Output Arguments:
49260298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
49270298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
49280298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
49290298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
4930c5033834SJed Brown 
4931c5033834SJed Brown    Level: beginner
4932c5033834SJed Brown 
4933c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
4934c5033834SJed Brown @*/
4935c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
4936c5033834SJed Brown {
4937c5033834SJed Brown   PetscFunctionBegin;
4938c5033834SJed Brown   if (atol)  *atol  = ts->atol;
4939c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
4940c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
4941c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
4942c5033834SJed Brown   PetscFunctionReturn(0);
4943c5033834SJed Brown }
4944c5033834SJed Brown 
4945c5033834SJed Brown #undef __FUNCT__
49469c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNorm2"
49479c6b16b5SShri Abhyankar /*@
4948a4868fbcSLisandro Dalcin    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between two state vectors
49499c6b16b5SShri Abhyankar 
49509c6b16b5SShri Abhyankar    Collective on TS
49519c6b16b5SShri Abhyankar 
49529c6b16b5SShri Abhyankar    Input Arguments:
49539c6b16b5SShri Abhyankar +  ts - time stepping context
4954a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
4955a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
49569c6b16b5SShri Abhyankar 
49579c6b16b5SShri Abhyankar    Output Arguments:
49589c6b16b5SShri Abhyankar .  norm - weighted norm, a value of 1.0 is considered small
49599c6b16b5SShri Abhyankar 
49609c6b16b5SShri Abhyankar    Level: developer
49619c6b16b5SShri Abhyankar 
4962deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
49639c6b16b5SShri Abhyankar @*/
4964a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec U,Vec Y,PetscReal *norm)
49659c6b16b5SShri Abhyankar {
49669c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
49679c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
49689c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
49699c6b16b5SShri Abhyankar   PetscReal         sum,gsum;
49709c6b16b5SShri Abhyankar   PetscReal         tol;
49719c6b16b5SShri Abhyankar 
49729c6b16b5SShri Abhyankar   PetscFunctionBegin;
49739c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4974a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
4975a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
4976a4868fbcSLisandro Dalcin   PetscValidType(U,2);
4977a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
4978a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
4979a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
4980a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
49819c6b16b5SShri Abhyankar 
49829c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
49839c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
49849c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
49859c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
49869c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
49879c6b16b5SShri Abhyankar   sum  = 0.;
49889c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
49899c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
49909c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
49919c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
49929c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
49939c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
49949c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
49959c6b16b5SShri Abhyankar     }
49969c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
49979c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
49989c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
49999c6b16b5SShri Abhyankar     const PetscScalar *atol;
50009c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
50019c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
50029c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
50039c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
50049c6b16b5SShri Abhyankar     }
50059c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
50069c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
50079c6b16b5SShri Abhyankar     const PetscScalar *rtol;
50089c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
50099c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
50109c6b16b5SShri Abhyankar       tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
50119c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
50129c6b16b5SShri Abhyankar     }
50139c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
50149c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
50159c6b16b5SShri Abhyankar     for (i=0; i<n; i++) {
50169c6b16b5SShri Abhyankar       tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
50179c6b16b5SShri Abhyankar       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
50189c6b16b5SShri Abhyankar     }
50199c6b16b5SShri Abhyankar   }
50209c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
50219c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
50229c6b16b5SShri Abhyankar 
50239c6b16b5SShri Abhyankar   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
50249c6b16b5SShri Abhyankar   *norm = PetscSqrtReal(gsum / N);
50259c6b16b5SShri Abhyankar 
50269c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
50279c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
50289c6b16b5SShri Abhyankar }
50299c6b16b5SShri Abhyankar 
50309c6b16b5SShri Abhyankar #undef __FUNCT__
50319c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNormInfinity"
50329c6b16b5SShri Abhyankar /*@
5033a4868fbcSLisandro Dalcin    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between two state vectors
50349c6b16b5SShri Abhyankar 
50359c6b16b5SShri Abhyankar    Collective on TS
50369c6b16b5SShri Abhyankar 
50379c6b16b5SShri Abhyankar    Input Arguments:
50389c6b16b5SShri Abhyankar +  ts - time stepping context
5039a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5040a4868fbcSLisandro Dalcin -  Y - state vector to be compared to U
50419c6b16b5SShri Abhyankar 
50429c6b16b5SShri Abhyankar    Output Arguments:
50439c6b16b5SShri Abhyankar .  norm - weighted norm, a value of 1.0 is considered small
50449c6b16b5SShri Abhyankar 
50459c6b16b5SShri Abhyankar    Level: developer
50469c6b16b5SShri Abhyankar 
5047deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
50489c6b16b5SShri Abhyankar @*/
5049a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec U,Vec Y,PetscReal *norm)
50509c6b16b5SShri Abhyankar {
50519c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
50529c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart,k;
50539c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
50549c6b16b5SShri Abhyankar   PetscReal         max,gmax;
50559c6b16b5SShri Abhyankar   PetscReal         tol;
50569c6b16b5SShri Abhyankar 
50579c6b16b5SShri Abhyankar   PetscFunctionBegin;
50589c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5059a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
5060a4868fbcSLisandro Dalcin   PetscValidHeaderSpecific(Y,VEC_CLASSID,3);
5061a4868fbcSLisandro Dalcin   PetscValidType(U,2);
5062a4868fbcSLisandro Dalcin   PetscValidType(Y,3);
5063a4868fbcSLisandro Dalcin   PetscCheckSameComm(U,2,Y,3);
5064a4868fbcSLisandro Dalcin   PetscValidPointer(norm,4);
5065a4868fbcSLisandro Dalcin   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"U and Y cannot be the same vector");
50669c6b16b5SShri Abhyankar 
50679c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
50689c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
50699c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
50709c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
50719c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
50729c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
50739c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
50749c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
50759c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
50769c6b16b5SShri Abhyankar     k = 0;
50779c6b16b5SShri Abhyankar     tol = PetscRealPart(atol[k]) + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
50789c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
50799c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
50809c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
50819c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
50829c6b16b5SShri Abhyankar     }
50839c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
50849c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
50859c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
50869c6b16b5SShri Abhyankar     const PetscScalar *atol;
50879c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
50889c6b16b5SShri Abhyankar     k = 0;
50899c6b16b5SShri Abhyankar     tol = PetscRealPart(atol[k]) + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
50909c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
50919c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
50929c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
50939c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
50949c6b16b5SShri Abhyankar     }
50959c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
50969c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
50979c6b16b5SShri Abhyankar     const PetscScalar *rtol;
50989c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
50999c6b16b5SShri Abhyankar     k = 0;
51009c6b16b5SShri Abhyankar     tol = ts->atol + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
51019c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
51029c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
51039c6b16b5SShri Abhyankar       tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
51049c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
51059c6b16b5SShri Abhyankar     }
51069c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
51079c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
51089c6b16b5SShri Abhyankar     k = 0;
51099c6b16b5SShri Abhyankar     tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
51109c6b16b5SShri Abhyankar     max = PetscAbsScalar(y[k] - u[k]) / tol;
51119c6b16b5SShri Abhyankar     for (i=1; i<n; i++) {
51129c6b16b5SShri Abhyankar       tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
51139c6b16b5SShri Abhyankar       max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
51149c6b16b5SShri Abhyankar     }
51159c6b16b5SShri Abhyankar   }
51169c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
51179c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
51189c6b16b5SShri Abhyankar 
51199c6b16b5SShri Abhyankar   ierr  = MPI_Allreduce(&max,&gmax,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
51209c6b16b5SShri Abhyankar   *norm = gmax;
51219c6b16b5SShri Abhyankar 
51229c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
51239c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
51249c6b16b5SShri Abhyankar }
51259c6b16b5SShri Abhyankar 
51269c6b16b5SShri Abhyankar #undef __FUNCT__
51277619abb3SShri #define __FUNCT__ "TSErrorWeightedNorm"
51281c3436cfSJed Brown /*@
5129a4868fbcSLisandro Dalcin    TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors
51301c3436cfSJed Brown 
51311c3436cfSJed Brown    Collective on TS
51321c3436cfSJed Brown 
51331c3436cfSJed Brown    Input Arguments:
51341c3436cfSJed Brown +  ts - time stepping context
5135a4868fbcSLisandro Dalcin .  U - state vector, usually ts->vec_sol
5136a4868fbcSLisandro Dalcin .  Y - state vector to be compared to U
5137a4868fbcSLisandro Dalcin -  wnormtype - norm type, either NORM_2 or NORM_INFINITY
51387619abb3SShri 
51391c3436cfSJed Brown    Output Arguments:
51401c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
51411c3436cfSJed Brown 
5142a4868fbcSLisandro Dalcin 
5143a4868fbcSLisandro Dalcin    Options Database Keys:
5144a4868fbcSLisandro Dalcin .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
5145a4868fbcSLisandro Dalcin 
51461c3436cfSJed Brown    Level: developer
51471c3436cfSJed Brown 
5148deea92deSShri .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
51491c3436cfSJed Brown @*/
5150a4868fbcSLisandro Dalcin PetscErrorCode TSErrorWeightedNorm(TS ts,Vec U,Vec Y,NormType wnormtype,PetscReal *norm)
51511c3436cfSJed Brown {
51528beabaa1SBarry Smith   PetscErrorCode ierr;
51531c3436cfSJed Brown 
51541c3436cfSJed Brown   PetscFunctionBegin;
5155a4868fbcSLisandro Dalcin   if (wnormtype == NORM_2) {
5156a4868fbcSLisandro Dalcin     ierr = TSErrorWeightedNorm2(ts,U,Y,norm);CHKERRQ(ierr);
5157a4868fbcSLisandro Dalcin   } else if(wnormtype == NORM_INFINITY) {
5158a4868fbcSLisandro Dalcin     ierr = TSErrorWeightedNormInfinity(ts,U,Y,norm);CHKERRQ(ierr);
5159a4868fbcSLisandro Dalcin   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for norm type %s",NormTypes[wnormtype]);
51601c3436cfSJed Brown   PetscFunctionReturn(0);
51611c3436cfSJed Brown }
51621c3436cfSJed Brown 
51631c3436cfSJed Brown #undef __FUNCT__
51648d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
51658d59e960SJed Brown /*@
51668d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
51678d59e960SJed Brown 
51688d59e960SJed Brown    Logically Collective on TS
51698d59e960SJed Brown 
51708d59e960SJed Brown    Input Arguments:
51718d59e960SJed Brown +  ts - time stepping context
51728d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
51738d59e960SJed Brown 
51748d59e960SJed Brown    Note:
51758d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
51768d59e960SJed Brown 
51778d59e960SJed Brown    Level: intermediate
51788d59e960SJed Brown 
51798d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
51808d59e960SJed Brown @*/
51818d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
51828d59e960SJed Brown {
51838d59e960SJed Brown   PetscFunctionBegin;
51848d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
51858d59e960SJed Brown   ts->cfltime_local = cfltime;
51868d59e960SJed Brown   ts->cfltime       = -1.;
51878d59e960SJed Brown   PetscFunctionReturn(0);
51888d59e960SJed Brown }
51898d59e960SJed Brown 
51908d59e960SJed Brown #undef __FUNCT__
51918d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
51928d59e960SJed Brown /*@
51938d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
51948d59e960SJed Brown 
51958d59e960SJed Brown    Collective on TS
51968d59e960SJed Brown 
51978d59e960SJed Brown    Input Arguments:
51988d59e960SJed Brown .  ts - time stepping context
51998d59e960SJed Brown 
52008d59e960SJed Brown    Output Arguments:
52018d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
52028d59e960SJed Brown 
52038d59e960SJed Brown    Level: advanced
52048d59e960SJed Brown 
52058d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
52068d59e960SJed Brown @*/
52078d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
52088d59e960SJed Brown {
52098d59e960SJed Brown   PetscErrorCode ierr;
52108d59e960SJed Brown 
52118d59e960SJed Brown   PetscFunctionBegin;
52128d59e960SJed Brown   if (ts->cfltime < 0) {
5213ce94432eSBarry Smith     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
52148d59e960SJed Brown   }
52158d59e960SJed Brown   *cfltime = ts->cfltime;
52168d59e960SJed Brown   PetscFunctionReturn(0);
52178d59e960SJed Brown }
52188d59e960SJed Brown 
52198d59e960SJed Brown #undef __FUNCT__
5220d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
5221d6ebe24aSShri Abhyankar /*@
5222d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
5223d6ebe24aSShri Abhyankar 
5224d6ebe24aSShri Abhyankar    Input Parameters:
5225d6ebe24aSShri Abhyankar .  ts   - the TS context.
5226d6ebe24aSShri Abhyankar .  xl   - lower bound.
5227d6ebe24aSShri Abhyankar .  xu   - upper bound.
5228d6ebe24aSShri Abhyankar 
5229d6ebe24aSShri Abhyankar    Notes:
5230d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
5231e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
5232d6ebe24aSShri Abhyankar 
52332bd2b0e6SSatish Balay    Level: advanced
52342bd2b0e6SSatish Balay 
5235d6ebe24aSShri Abhyankar @*/
5236d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
5237d6ebe24aSShri Abhyankar {
5238d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
5239d6ebe24aSShri Abhyankar   SNES           snes;
5240d6ebe24aSShri Abhyankar 
5241d6ebe24aSShri Abhyankar   PetscFunctionBegin;
5242d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
5243d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
5244d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
5245d6ebe24aSShri Abhyankar }
5246d6ebe24aSShri Abhyankar 
5247325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
5248c6db04a5SJed Brown #include <mex.h>
5249325fc9f4SBarry Smith 
5250325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
5251325fc9f4SBarry Smith 
5252325fc9f4SBarry Smith #undef __FUNCT__
5253325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
5254325fc9f4SBarry Smith /*
5255325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
5256325fc9f4SBarry Smith                          TSSetFunctionMatlab().
5257325fc9f4SBarry Smith 
5258325fc9f4SBarry Smith    Collective on TS
5259325fc9f4SBarry Smith 
5260325fc9f4SBarry Smith    Input Parameters:
5261325fc9f4SBarry Smith +  snes - the TS context
52620910c330SBarry Smith -  u - input vector
5263325fc9f4SBarry Smith 
5264325fc9f4SBarry Smith    Output Parameter:
5265325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
5266325fc9f4SBarry Smith 
5267325fc9f4SBarry Smith    Notes:
5268325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
5269325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
5270325fc9f4SBarry Smith    themselves.
5271325fc9f4SBarry Smith 
5272325fc9f4SBarry Smith    Level: developer
5273325fc9f4SBarry Smith 
5274325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5275325fc9f4SBarry Smith 
5276325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5277325fc9f4SBarry Smith */
52780910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
5279325fc9f4SBarry Smith {
5280325fc9f4SBarry Smith   PetscErrorCode  ierr;
5281325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5282325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
5283325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
5284325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
5285325fc9f4SBarry Smith 
5286325fc9f4SBarry Smith   PetscFunctionBegin;
5287325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
52880910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
52890910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
5290325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
52910910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
5292325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
5293325fc9f4SBarry Smith 
5294325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
52950910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
52960910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
52970910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
5298bbd56ea5SKarl Rupp 
5299325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5300325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
5301325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5302325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5303325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
5304325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
5305325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
5306325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
5307325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5308325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5309325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5310325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5311325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5312325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5313325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5314325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5315325fc9f4SBarry Smith   PetscFunctionReturn(0);
5316325fc9f4SBarry Smith }
5317325fc9f4SBarry Smith 
5318325fc9f4SBarry Smith 
5319325fc9f4SBarry Smith #undef __FUNCT__
5320325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
5321325fc9f4SBarry Smith /*
5322325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
5323325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
5324e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
5325325fc9f4SBarry Smith 
5326325fc9f4SBarry Smith    Logically Collective on TS
5327325fc9f4SBarry Smith 
5328325fc9f4SBarry Smith    Input Parameters:
5329325fc9f4SBarry Smith +  ts - the TS context
5330325fc9f4SBarry Smith -  func - function evaluation routine
5331325fc9f4SBarry Smith 
5332325fc9f4SBarry Smith    Calling sequence of func:
53330910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
5334325fc9f4SBarry Smith 
5335325fc9f4SBarry Smith    Level: beginner
5336325fc9f4SBarry Smith 
5337325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5338325fc9f4SBarry Smith 
5339325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5340325fc9f4SBarry Smith */
5341cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
5342325fc9f4SBarry Smith {
5343325fc9f4SBarry Smith   PetscErrorCode  ierr;
5344325fc9f4SBarry Smith   TSMatlabContext *sctx;
5345325fc9f4SBarry Smith 
5346325fc9f4SBarry Smith   PetscFunctionBegin;
5347325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5348325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5349325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5350325fc9f4SBarry Smith   /*
5351325fc9f4SBarry Smith      This should work, but it doesn't
5352325fc9f4SBarry Smith   sctx->ctx = ctx;
5353325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5354325fc9f4SBarry Smith   */
5355325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5356bbd56ea5SKarl Rupp 
53570298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
5358325fc9f4SBarry Smith   PetscFunctionReturn(0);
5359325fc9f4SBarry Smith }
5360325fc9f4SBarry Smith 
5361325fc9f4SBarry Smith #undef __FUNCT__
5362325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
5363325fc9f4SBarry Smith /*
5364325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
5365325fc9f4SBarry Smith                          TSSetJacobianMatlab().
5366325fc9f4SBarry Smith 
5367325fc9f4SBarry Smith    Collective on TS
5368325fc9f4SBarry Smith 
5369325fc9f4SBarry Smith    Input Parameters:
5370cdcf91faSSean Farley +  ts - the TS context
53710910c330SBarry Smith .  u - input vector
5372325fc9f4SBarry Smith .  A, B - the matrices
5373325fc9f4SBarry Smith -  ctx - user context
5374325fc9f4SBarry Smith 
5375325fc9f4SBarry Smith    Level: developer
5376325fc9f4SBarry Smith 
5377325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5378325fc9f4SBarry Smith 
5379325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5380325fc9f4SBarry Smith @*/
5381f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
5382325fc9f4SBarry Smith {
5383325fc9f4SBarry Smith   PetscErrorCode  ierr;
5384325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5385325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
5386325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
5387325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
5388325fc9f4SBarry Smith 
5389325fc9f4SBarry Smith   PetscFunctionBegin;
5390cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53910910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
5392325fc9f4SBarry Smith 
53930910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
5394325fc9f4SBarry Smith 
5395cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
53960910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
53970910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
53980910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
53990910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
5400bbd56ea5SKarl Rupp 
5401325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5402325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
5403325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5404325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5405325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
5406325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
5407325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
5408325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
5409325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
5410325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
5411325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5412325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5413325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5414325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5415325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5416325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5417325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5418325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
5419325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
5420325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5421325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
5422325fc9f4SBarry Smith   PetscFunctionReturn(0);
5423325fc9f4SBarry Smith }
5424325fc9f4SBarry Smith 
5425325fc9f4SBarry Smith 
5426325fc9f4SBarry Smith #undef __FUNCT__
5427325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
5428325fc9f4SBarry Smith /*
5429325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
5430e3c5b3baSBarry Smith    vector for use by the TS routines in solving ODEs from MATLAB. Here the function is a string containing the name of a MATLAB function
5431325fc9f4SBarry Smith 
5432325fc9f4SBarry Smith    Logically Collective on TS
5433325fc9f4SBarry Smith 
5434325fc9f4SBarry Smith    Input Parameters:
5435cdcf91faSSean Farley +  ts - the TS context
5436325fc9f4SBarry Smith .  A,B - Jacobian matrices
5437325fc9f4SBarry Smith .  func - function evaluation routine
5438325fc9f4SBarry Smith -  ctx - user context
5439325fc9f4SBarry Smith 
5440325fc9f4SBarry Smith    Calling sequence of func:
54410910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
5442325fc9f4SBarry Smith 
5443325fc9f4SBarry Smith 
5444325fc9f4SBarry Smith    Level: developer
5445325fc9f4SBarry Smith 
5446325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5447325fc9f4SBarry Smith 
5448325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5449325fc9f4SBarry Smith */
5450cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
5451325fc9f4SBarry Smith {
5452325fc9f4SBarry Smith   PetscErrorCode  ierr;
5453325fc9f4SBarry Smith   TSMatlabContext *sctx;
5454325fc9f4SBarry Smith 
5455325fc9f4SBarry Smith   PetscFunctionBegin;
5456325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5457325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5458325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5459325fc9f4SBarry Smith   /*
5460325fc9f4SBarry Smith      This should work, but it doesn't
5461325fc9f4SBarry Smith   sctx->ctx = ctx;
5462325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5463325fc9f4SBarry Smith   */
5464325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5465bbd56ea5SKarl Rupp 
5466cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
5467325fc9f4SBarry Smith   PetscFunctionReturn(0);
5468325fc9f4SBarry Smith }
5469325fc9f4SBarry Smith 
5470b5b1a830SBarry Smith #undef __FUNCT__
5471b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
5472b5b1a830SBarry Smith /*
5473b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
5474b5b1a830SBarry Smith 
5475b5b1a830SBarry Smith    Collective on TS
5476b5b1a830SBarry Smith 
5477b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5478b5b1a830SBarry Smith @*/
54790910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
5480b5b1a830SBarry Smith {
5481b5b1a830SBarry Smith   PetscErrorCode  ierr;
5482b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5483a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
5484b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
5485b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
5486b5b1a830SBarry Smith 
5487b5b1a830SBarry Smith   PetscFunctionBegin;
5488cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
54890910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
5490b5b1a830SBarry Smith 
5491cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
54920910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
5493bbd56ea5SKarl Rupp 
5494b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5495b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
5496b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
5497b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
5498b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
5499b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
5500b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
5501b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5502b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
5503b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
5504b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
5505b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
5506b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
5507b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
5508b5b1a830SBarry Smith   PetscFunctionReturn(0);
5509b5b1a830SBarry Smith }
5510b5b1a830SBarry Smith 
5511b5b1a830SBarry Smith 
5512b5b1a830SBarry Smith #undef __FUNCT__
5513b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
5514b5b1a830SBarry Smith /*
5515b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
5516b5b1a830SBarry Smith 
5517b5b1a830SBarry Smith    Level: developer
5518b5b1a830SBarry Smith 
5519b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
5520b5b1a830SBarry Smith 
5521b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5522b5b1a830SBarry Smith */
5523cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
5524b5b1a830SBarry Smith {
5525b5b1a830SBarry Smith   PetscErrorCode  ierr;
5526b5b1a830SBarry Smith   TSMatlabContext *sctx;
5527b5b1a830SBarry Smith 
5528b5b1a830SBarry Smith   PetscFunctionBegin;
5529b5b1a830SBarry Smith   /* currently sctx is memory bleed */
5530b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5531b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5532b5b1a830SBarry Smith   /*
5533b5b1a830SBarry Smith      This should work, but it doesn't
5534b5b1a830SBarry Smith   sctx->ctx = ctx;
5535b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5536b5b1a830SBarry Smith   */
5537b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5538bbd56ea5SKarl Rupp 
55390298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
5540b5b1a830SBarry Smith   PetscFunctionReturn(0);
5541b5b1a830SBarry Smith }
5542325fc9f4SBarry Smith #endif
5543b3603a34SBarry Smith 
5544b3603a34SBarry Smith #undef __FUNCT__
55454f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
5546b3603a34SBarry Smith /*@C
55474f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
5548b3603a34SBarry Smith        in a time based line graph
5549b3603a34SBarry Smith 
5550b3603a34SBarry Smith    Collective on TS
5551b3603a34SBarry Smith 
5552b3603a34SBarry Smith    Input Parameters:
5553b3603a34SBarry Smith +  ts - the TS context
5554b3603a34SBarry Smith .  step - current time-step
5555b3603a34SBarry Smith .  ptime - current time
5556b3603a34SBarry Smith -  lg - a line graph object
5557b3603a34SBarry Smith 
5558b3d3934dSBarry Smith    Options Database:
55599ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
5560b3d3934dSBarry Smith 
5561b3603a34SBarry Smith    Level: intermediate
5562b3603a34SBarry Smith 
55630b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
5564b3603a34SBarry Smith 
5565b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
5566b3603a34SBarry Smith 
5567b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5568b3603a34SBarry Smith @*/
55690910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
5570b3603a34SBarry Smith {
5571b3603a34SBarry Smith   PetscErrorCode    ierr;
55720b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
5573b3603a34SBarry Smith   const PetscScalar *yy;
557458ff32f7SBarry Smith   PetscInt          dim;
557580666b62SBarry Smith   Vec               v;
5576b3603a34SBarry Smith 
5577b3603a34SBarry Smith   PetscFunctionBegin;
557858ff32f7SBarry Smith   if (!step) {
5579a9f9c1f6SBarry Smith     PetscDrawAxis axis;
5580a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5581a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
5582387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
5583387f4636SBarry Smith       char      **displaynames;
5584387f4636SBarry Smith       PetscBool flg;
5585387f4636SBarry Smith 
5586387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
5587387f4636SBarry Smith       ierr = PetscMalloc((dim+1)*sizeof(char*),&displaynames);CHKERRQ(ierr);
5588387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
55899ae14b6eSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
5590387f4636SBarry Smith       if (flg) {
5591a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
5592387f4636SBarry Smith       }
5593387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
5594387f4636SBarry Smith     }
5595387f4636SBarry Smith     if (ctx->displaynames) {
5596387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
5597387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
5598387f4636SBarry Smith     } else if (ctx->names) {
55990910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
56000b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
5601387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
5602387f4636SBarry Smith     }
56030b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
560458ff32f7SBarry Smith   }
560580666b62SBarry Smith   if (ctx->transform) {
5606e673d494SBarry Smith     ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);
560780666b62SBarry Smith   } else {
560880666b62SBarry Smith     v = u;
560980666b62SBarry Smith   }
561080666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
5611e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
5612e3efe391SJed Brown   {
5613e3efe391SJed Brown     PetscReal *yreal;
5614e3efe391SJed Brown     PetscInt  i,n;
561580666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
5616785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
5617e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
56180b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
5619e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
5620e3efe391SJed Brown   }
5621e3efe391SJed Brown #else
5622387f4636SBarry Smith   if (ctx->displaynames) {
5623387f4636SBarry Smith     PetscInt i;
5624387f4636SBarry Smith     for (i=0; i<ctx->ndisplayvariables; i++) {
5625387f4636SBarry Smith       ctx->displayvalues[i] = yy[ctx->displayvariables[i]];
5626387f4636SBarry Smith     }
5627387f4636SBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
5628387f4636SBarry Smith   } else {
56290b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
5630387f4636SBarry Smith   }
5631e3efe391SJed Brown #endif
563280666b62SBarry Smith   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
563380666b62SBarry Smith   if (ctx->transform) {
563480666b62SBarry Smith     ierr = VecDestroy(&v);CHKERRQ(ierr);
563580666b62SBarry Smith   }
5636b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
56370b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
56383923b477SBarry Smith   }
5639b3603a34SBarry Smith   PetscFunctionReturn(0);
5640b3603a34SBarry Smith }
5641b3603a34SBarry Smith 
5642387f4636SBarry Smith 
5643b3603a34SBarry Smith #undef __FUNCT__
564431152f8aSBarry Smith #define __FUNCT__ "TSMonitorLGSetVariableNames"
5645b037adc7SBarry Smith /*@C
564631152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
5647b037adc7SBarry Smith 
5648b037adc7SBarry Smith    Collective on TS
5649b037adc7SBarry Smith 
5650b037adc7SBarry Smith    Input Parameters:
5651b037adc7SBarry Smith +  ts - the TS context
5652b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
5653b037adc7SBarry Smith 
5654b037adc7SBarry Smith    Level: intermediate
5655b037adc7SBarry Smith 
5656b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
5657b037adc7SBarry Smith 
5658a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
5659b037adc7SBarry Smith @*/
566031152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
5661b037adc7SBarry Smith {
5662b037adc7SBarry Smith   PetscErrorCode    ierr;
5663b037adc7SBarry Smith   PetscInt          i;
5664b037adc7SBarry Smith 
5665b037adc7SBarry Smith   PetscFunctionBegin;
5666b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
5667b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
56685537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
5669b3d3934dSBarry Smith       break;
5670b3d3934dSBarry Smith     }
5671b3d3934dSBarry Smith   }
5672b3d3934dSBarry Smith   PetscFunctionReturn(0);
5673b3d3934dSBarry Smith }
5674b3d3934dSBarry Smith 
5675b3d3934dSBarry Smith #undef __FUNCT__
5676e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetVariableNames"
5677e673d494SBarry Smith /*@C
5678e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
5679e673d494SBarry Smith 
5680e673d494SBarry Smith    Collective on TS
5681e673d494SBarry Smith 
5682e673d494SBarry Smith    Input Parameters:
5683e673d494SBarry Smith +  ts - the TS context
5684e673d494SBarry Smith -  names - the names of the components, final string must be NULL
5685e673d494SBarry Smith 
5686e673d494SBarry Smith    Level: intermediate
5687e673d494SBarry Smith 
5688e673d494SBarry Smith .keywords: TS,  vector, monitor, view
5689e673d494SBarry Smith 
5690a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
5691e673d494SBarry Smith @*/
5692e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
5693e673d494SBarry Smith {
5694e673d494SBarry Smith   PetscErrorCode    ierr;
5695e673d494SBarry Smith 
5696e673d494SBarry Smith   PetscFunctionBegin;
5697e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
5698e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
5699e673d494SBarry Smith   PetscFunctionReturn(0);
5700e673d494SBarry Smith }
5701e673d494SBarry Smith 
5702e673d494SBarry Smith #undef __FUNCT__
5703b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorLGGetVariableNames"
5704b3d3934dSBarry Smith /*@C
5705b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
5706b3d3934dSBarry Smith 
5707b3d3934dSBarry Smith    Collective on TS
5708b3d3934dSBarry Smith 
5709b3d3934dSBarry Smith    Input Parameter:
5710b3d3934dSBarry Smith .  ts - the TS context
5711b3d3934dSBarry Smith 
5712b3d3934dSBarry Smith    Output Parameter:
5713b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
5714b3d3934dSBarry Smith 
5715b3d3934dSBarry Smith    Level: intermediate
5716b3d3934dSBarry Smith 
5717b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
5718b3d3934dSBarry Smith 
5719b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
5720b3d3934dSBarry Smith @*/
5721b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
5722b3d3934dSBarry Smith {
5723b3d3934dSBarry Smith   PetscInt       i;
5724b3d3934dSBarry Smith 
5725b3d3934dSBarry Smith   PetscFunctionBegin;
5726b3d3934dSBarry Smith   *names = NULL;
5727b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
5728b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
57295537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
5730b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
5731b3d3934dSBarry Smith       break;
5732387f4636SBarry Smith     }
5733387f4636SBarry Smith   }
5734387f4636SBarry Smith   PetscFunctionReturn(0);
5735387f4636SBarry Smith }
5736387f4636SBarry Smith 
5737387f4636SBarry Smith #undef __FUNCT__
5738a66092f1SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetDisplayVariables"
5739a66092f1SBarry Smith /*@C
5740a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
5741a66092f1SBarry Smith 
5742a66092f1SBarry Smith    Collective on TS
5743a66092f1SBarry Smith 
5744a66092f1SBarry Smith    Input Parameters:
5745a66092f1SBarry Smith +  ctx - the TSMonitorLG context
5746a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
5747a66092f1SBarry Smith 
5748a66092f1SBarry Smith    Level: intermediate
5749a66092f1SBarry Smith 
5750a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
5751a66092f1SBarry Smith 
5752a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
5753a66092f1SBarry Smith @*/
5754a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
5755a66092f1SBarry Smith {
5756a66092f1SBarry Smith   PetscInt          j = 0,k;
5757a66092f1SBarry Smith   PetscErrorCode    ierr;
5758a66092f1SBarry Smith 
5759a66092f1SBarry Smith   PetscFunctionBegin;
5760a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
5761a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
5762a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
5763a66092f1SBarry Smith   while (displaynames[j]) j++;
5764a66092f1SBarry Smith   ctx->ndisplayvariables = j;
5765a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
5766a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
5767a66092f1SBarry Smith   j = 0;
5768a66092f1SBarry Smith   while (displaynames[j]) {
5769a66092f1SBarry Smith     k = 0;
5770a66092f1SBarry Smith     while (ctx->names[k]) {
5771a66092f1SBarry Smith       PetscBool flg;
5772a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
5773a66092f1SBarry Smith       if (flg) {
5774a66092f1SBarry Smith         ctx->displayvariables[j] = k;
5775a66092f1SBarry Smith         break;
5776a66092f1SBarry Smith       }
5777a66092f1SBarry Smith       k++;
5778a66092f1SBarry Smith     }
5779a66092f1SBarry Smith     j++;
5780a66092f1SBarry Smith   }
5781a66092f1SBarry Smith   PetscFunctionReturn(0);
5782a66092f1SBarry Smith }
5783a66092f1SBarry Smith 
5784a66092f1SBarry Smith 
5785a66092f1SBarry Smith #undef __FUNCT__
5786387f4636SBarry Smith #define __FUNCT__ "TSMonitorLGSetDisplayVariables"
5787387f4636SBarry Smith /*@C
5788387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
5789387f4636SBarry Smith 
5790387f4636SBarry Smith    Collective on TS
5791387f4636SBarry Smith 
5792387f4636SBarry Smith    Input Parameters:
5793387f4636SBarry Smith +  ts - the TS context
5794387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
5795387f4636SBarry Smith 
5796387f4636SBarry Smith    Level: intermediate
5797387f4636SBarry Smith 
5798387f4636SBarry Smith .keywords: TS,  vector, monitor, view
5799387f4636SBarry Smith 
5800387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
5801387f4636SBarry Smith @*/
5802387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
5803387f4636SBarry Smith {
5804a66092f1SBarry Smith   PetscInt          i;
5805387f4636SBarry Smith   PetscErrorCode    ierr;
5806387f4636SBarry Smith 
5807387f4636SBarry Smith   PetscFunctionBegin;
5808387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
5809387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
58105537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
5811b3d3934dSBarry Smith       break;
5812b037adc7SBarry Smith     }
5813b037adc7SBarry Smith   }
5814b037adc7SBarry Smith   PetscFunctionReturn(0);
5815b037adc7SBarry Smith }
5816b037adc7SBarry Smith 
5817b037adc7SBarry Smith #undef __FUNCT__
581880666b62SBarry Smith #define __FUNCT__ "TSMonitorLGSetTransform"
581980666b62SBarry Smith /*@C
582080666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
582180666b62SBarry Smith 
582280666b62SBarry Smith    Collective on TS
582380666b62SBarry Smith 
582480666b62SBarry Smith    Input Parameters:
582580666b62SBarry Smith +  ts - the TS context
582680666b62SBarry Smith .  transform - the transform function
58277684fa3eSBarry Smith .  destroy - function to destroy the optional context
582880666b62SBarry Smith -  ctx - optional context used by transform function
582980666b62SBarry Smith 
583080666b62SBarry Smith    Level: intermediate
583180666b62SBarry Smith 
583280666b62SBarry Smith .keywords: TS,  vector, monitor, view
583380666b62SBarry Smith 
5834a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
583580666b62SBarry Smith @*/
58367684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
583780666b62SBarry Smith {
583880666b62SBarry Smith   PetscInt          i;
5839a66092f1SBarry Smith   PetscErrorCode    ierr;
584080666b62SBarry Smith 
584180666b62SBarry Smith   PetscFunctionBegin;
584280666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
584380666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
58445537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
584580666b62SBarry Smith     }
584680666b62SBarry Smith   }
584780666b62SBarry Smith   PetscFunctionReturn(0);
584880666b62SBarry Smith }
584980666b62SBarry Smith 
585080666b62SBarry Smith #undef __FUNCT__
5851e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetTransform"
5852e673d494SBarry Smith /*@C
5853e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
5854e673d494SBarry Smith 
5855e673d494SBarry Smith    Collective on TSLGCtx
5856e673d494SBarry Smith 
5857e673d494SBarry Smith    Input Parameters:
5858e673d494SBarry Smith +  ts - the TS context
5859e673d494SBarry Smith .  transform - the transform function
58607684fa3eSBarry Smith .  destroy - function to destroy the optional context
5861e673d494SBarry Smith -  ctx - optional context used by transform function
5862e673d494SBarry Smith 
5863e673d494SBarry Smith    Level: intermediate
5864e673d494SBarry Smith 
5865e673d494SBarry Smith .keywords: TS,  vector, monitor, view
5866e673d494SBarry Smith 
5867a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
5868e673d494SBarry Smith @*/
58697684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
5870e673d494SBarry Smith {
5871e673d494SBarry Smith   PetscFunctionBegin;
5872e673d494SBarry Smith   ctx->transform    = transform;
58737684fa3eSBarry Smith   ctx->transformdestroy = destroy;
5874e673d494SBarry Smith   ctx->transformctx = tctx;
5875e673d494SBarry Smith   PetscFunctionReturn(0);
5876e673d494SBarry Smith }
5877e673d494SBarry Smith 
5878b3603a34SBarry Smith #undef __FUNCT__
58794f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
5880ef20d060SBarry Smith /*@C
58814f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
5882ef20d060SBarry Smith        in a time based line graph
5883ef20d060SBarry Smith 
5884ef20d060SBarry Smith    Collective on TS
5885ef20d060SBarry Smith 
5886ef20d060SBarry Smith    Input Parameters:
5887ef20d060SBarry Smith +  ts - the TS context
5888ef20d060SBarry Smith .  step - current time-step
5889ef20d060SBarry Smith .  ptime - current time
5890ef20d060SBarry Smith -  lg - a line graph object
5891ef20d060SBarry Smith 
5892ef20d060SBarry Smith    Level: intermediate
5893ef20d060SBarry Smith 
5894abd5a294SJed Brown    Notes:
5895abd5a294SJed Brown    Only for sequential solves.
5896abd5a294SJed Brown 
5897abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
5898abd5a294SJed Brown 
5899abd5a294SJed Brown    Options Database Keys:
59004f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
5901ef20d060SBarry Smith 
5902ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
5903ef20d060SBarry Smith 
5904abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
5905ef20d060SBarry Smith @*/
59060910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
5907ef20d060SBarry Smith {
5908ef20d060SBarry Smith   PetscErrorCode    ierr;
59090b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
5910ef20d060SBarry Smith   const PetscScalar *yy;
5911ef20d060SBarry Smith   Vec               y;
5912a9f9c1f6SBarry Smith   PetscInt          dim;
5913ef20d060SBarry Smith 
5914ef20d060SBarry Smith   PetscFunctionBegin;
5915a9f9c1f6SBarry Smith   if (!step) {
5916a9f9c1f6SBarry Smith     PetscDrawAxis axis;
5917a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
59181ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
59190910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
5920a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
5921a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5922a9f9c1f6SBarry Smith   }
59230910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
5924ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
59250910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
5926ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
5927e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
5928e3efe391SJed Brown   {
5929e3efe391SJed Brown     PetscReal *yreal;
5930e3efe391SJed Brown     PetscInt  i,n;
5931e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
5932785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
5933e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
59340b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
5935e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
5936e3efe391SJed Brown   }
5937e3efe391SJed Brown #else
59380b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
5939e3efe391SJed Brown #endif
5940ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
5941ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
5942b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
59430b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
59443923b477SBarry Smith   }
5945ef20d060SBarry Smith   PetscFunctionReturn(0);
5946ef20d060SBarry Smith }
5947ef20d060SBarry Smith 
5948201da799SBarry Smith #undef __FUNCT__
5949201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
5950201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
5951201da799SBarry Smith {
5952201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
5953201da799SBarry Smith   PetscReal      x   = ptime,y;
5954201da799SBarry Smith   PetscErrorCode ierr;
5955201da799SBarry Smith   PetscInt       its;
5956201da799SBarry Smith 
5957201da799SBarry Smith   PetscFunctionBegin;
5958201da799SBarry Smith   if (!n) {
5959201da799SBarry Smith     PetscDrawAxis axis;
5960bbd56ea5SKarl Rupp 
5961201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5962201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
5963201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5964bbd56ea5SKarl Rupp 
5965201da799SBarry Smith     ctx->snes_its = 0;
5966201da799SBarry Smith   }
5967201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
5968201da799SBarry Smith   y    = its - ctx->snes_its;
5969201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
59703fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
5971201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
5972201da799SBarry Smith   }
5973201da799SBarry Smith   ctx->snes_its = its;
5974201da799SBarry Smith   PetscFunctionReturn(0);
5975201da799SBarry Smith }
5976201da799SBarry Smith 
5977201da799SBarry Smith #undef __FUNCT__
5978201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
5979201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
5980201da799SBarry Smith {
5981201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
5982201da799SBarry Smith   PetscReal      x   = ptime,y;
5983201da799SBarry Smith   PetscErrorCode ierr;
5984201da799SBarry Smith   PetscInt       its;
5985201da799SBarry Smith 
5986201da799SBarry Smith   PetscFunctionBegin;
5987201da799SBarry Smith   if (!n) {
5988201da799SBarry Smith     PetscDrawAxis axis;
5989bbd56ea5SKarl Rupp 
5990201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5991201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
5992201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5993bbd56ea5SKarl Rupp 
5994201da799SBarry Smith     ctx->ksp_its = 0;
5995201da799SBarry Smith   }
5996201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
5997201da799SBarry Smith   y    = its - ctx->ksp_its;
5998201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
599999fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6000201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
6001201da799SBarry Smith   }
6002201da799SBarry Smith   ctx->ksp_its = its;
6003201da799SBarry Smith   PetscFunctionReturn(0);
6004201da799SBarry Smith }
6005f9c1d6abSBarry Smith 
6006f9c1d6abSBarry Smith #undef __FUNCT__
6007f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
6008f9c1d6abSBarry Smith /*@
6009f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
6010f9c1d6abSBarry Smith 
6011f9c1d6abSBarry Smith    Collective on TS and Vec
6012f9c1d6abSBarry Smith 
6013f9c1d6abSBarry Smith    Input Parameters:
6014f9c1d6abSBarry Smith +  ts - the TS context
6015f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
6016f9c1d6abSBarry Smith 
6017f9c1d6abSBarry Smith    Output Parameters:
6018f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
6019f9c1d6abSBarry Smith 
6020f9c1d6abSBarry Smith    Level: developer
6021f9c1d6abSBarry Smith 
6022f9c1d6abSBarry Smith .keywords: TS, compute
6023f9c1d6abSBarry Smith 
6024f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
6025f9c1d6abSBarry Smith @*/
6026f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
6027f9c1d6abSBarry Smith {
6028f9c1d6abSBarry Smith   PetscErrorCode ierr;
6029f9c1d6abSBarry Smith 
6030f9c1d6abSBarry Smith   PetscFunctionBegin;
6031f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6032ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
6033f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
6034f9c1d6abSBarry Smith   PetscFunctionReturn(0);
6035f9c1d6abSBarry Smith }
603624655328SShri 
6037b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
6038b3d3934dSBarry Smith #undef __FUNCT__
6039b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxCreate"
6040b3d3934dSBarry Smith /*@C
6041b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
6042b3d3934dSBarry Smith 
6043b3d3934dSBarry Smith    Collective on TS
6044b3d3934dSBarry Smith 
6045b3d3934dSBarry Smith    Input Parameters:
6046b3d3934dSBarry Smith .  ts  - the ODE solver object
6047b3d3934dSBarry Smith 
6048b3d3934dSBarry Smith    Output Parameter:
6049b3d3934dSBarry Smith .  ctx - the context
6050b3d3934dSBarry Smith 
6051b3d3934dSBarry Smith    Level: intermediate
6052b3d3934dSBarry Smith 
6053b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
6054b3d3934dSBarry Smith 
6055b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
6056b3d3934dSBarry Smith 
6057b3d3934dSBarry Smith @*/
6058b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
6059b3d3934dSBarry Smith {
6060b3d3934dSBarry Smith   PetscErrorCode ierr;
6061b3d3934dSBarry Smith 
6062b3d3934dSBarry Smith   PetscFunctionBegin;
6063a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
6064b3d3934dSBarry Smith   PetscFunctionReturn(0);
6065b3d3934dSBarry Smith }
6066b3d3934dSBarry Smith 
6067b3d3934dSBarry Smith #undef __FUNCT__
6068b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelope"
6069b3d3934dSBarry Smith /*@C
6070b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
6071b3d3934dSBarry Smith 
6072b3d3934dSBarry Smith    Collective on TS
6073b3d3934dSBarry Smith 
6074b3d3934dSBarry Smith    Input Parameters:
6075b3d3934dSBarry Smith +  ts - the TS context
6076b3d3934dSBarry Smith .  step - current time-step
6077b3d3934dSBarry Smith .  ptime - current time
6078b3d3934dSBarry Smith -  ctx - the envelope context
6079b3d3934dSBarry Smith 
6080b3d3934dSBarry Smith    Options Database:
6081b3d3934dSBarry Smith .  -ts_monitor_envelope
6082b3d3934dSBarry Smith 
6083b3d3934dSBarry Smith    Level: intermediate
6084b3d3934dSBarry Smith 
6085b3d3934dSBarry Smith    Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
6086b3d3934dSBarry Smith 
6087b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6088b3d3934dSBarry Smith 
6089b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds()
6090b3d3934dSBarry Smith @*/
6091b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6092b3d3934dSBarry Smith {
6093b3d3934dSBarry Smith   PetscErrorCode       ierr;
6094b3d3934dSBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dummy;
6095b3d3934dSBarry Smith 
6096b3d3934dSBarry Smith   PetscFunctionBegin;
6097b3d3934dSBarry Smith   if (!ctx->max) {
6098b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
6099b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
6100b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
6101b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
6102b3d3934dSBarry Smith   } else {
6103b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
6104b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
6105b3d3934dSBarry Smith   }
6106b3d3934dSBarry Smith   PetscFunctionReturn(0);
6107b3d3934dSBarry Smith }
6108b3d3934dSBarry Smith 
6109b3d3934dSBarry Smith 
6110b3d3934dSBarry Smith #undef __FUNCT__
6111b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeGetBounds"
6112b3d3934dSBarry Smith /*@C
6113b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
6114b3d3934dSBarry Smith 
6115b3d3934dSBarry Smith    Collective on TS
6116b3d3934dSBarry Smith 
6117b3d3934dSBarry Smith    Input Parameter:
6118b3d3934dSBarry Smith .  ts - the TS context
6119b3d3934dSBarry Smith 
6120b3d3934dSBarry Smith    Output Parameter:
6121b3d3934dSBarry Smith +  max - the maximum values
6122b3d3934dSBarry Smith -  min - the minimum values
6123b3d3934dSBarry Smith 
6124b3d3934dSBarry Smith    Level: intermediate
6125b3d3934dSBarry Smith 
6126b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6127b3d3934dSBarry Smith 
6128b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6129b3d3934dSBarry Smith @*/
6130b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
6131b3d3934dSBarry Smith {
6132b3d3934dSBarry Smith   PetscInt i;
6133b3d3934dSBarry Smith 
6134b3d3934dSBarry Smith   PetscFunctionBegin;
6135b3d3934dSBarry Smith   if (max) *max = NULL;
6136b3d3934dSBarry Smith   if (min) *min = NULL;
6137b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6138b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
61395537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
6140b3d3934dSBarry Smith       if (max) *max = ctx->max;
6141b3d3934dSBarry Smith       if (min) *min = ctx->min;
6142b3d3934dSBarry Smith       break;
6143b3d3934dSBarry Smith     }
6144b3d3934dSBarry Smith   }
6145b3d3934dSBarry Smith   PetscFunctionReturn(0);
6146b3d3934dSBarry Smith }
6147b3d3934dSBarry Smith 
6148b3d3934dSBarry Smith #undef __FUNCT__
6149b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy"
6150b3d3934dSBarry Smith /*@C
6151b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
6152b3d3934dSBarry Smith 
6153b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
6154b3d3934dSBarry Smith 
6155b3d3934dSBarry Smith    Input Parameter:
6156b3d3934dSBarry Smith .  ctx - the monitor context
6157b3d3934dSBarry Smith 
6158b3d3934dSBarry Smith    Level: intermediate
6159b3d3934dSBarry Smith 
6160b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
6161b3d3934dSBarry Smith 
6162b3d3934dSBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
6163b3d3934dSBarry Smith @*/
6164b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
6165b3d3934dSBarry Smith {
6166b3d3934dSBarry Smith   PetscErrorCode ierr;
6167b3d3934dSBarry Smith 
6168b3d3934dSBarry Smith   PetscFunctionBegin;
6169b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
6170b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
6171b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
6172b3d3934dSBarry Smith   PetscFunctionReturn(0);
6173b3d3934dSBarry Smith }
6174f2dee214SBarry Smith 
617524655328SShri #undef __FUNCT__
617624655328SShri #define __FUNCT__ "TSRollBack"
617724655328SShri /*@
617824655328SShri    TSRollBack - Rolls back one time step
617924655328SShri 
618024655328SShri    Collective on TS
618124655328SShri 
618224655328SShri    Input Parameter:
618324655328SShri .  ts - the TS context obtained from TSCreate()
618424655328SShri 
618524655328SShri    Level: advanced
618624655328SShri 
618724655328SShri .keywords: TS, timestep, rollback
618824655328SShri 
618924655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
619024655328SShri @*/
619124655328SShri PetscErrorCode  TSRollBack(TS ts)
619224655328SShri {
619324655328SShri   PetscErrorCode ierr;
619424655328SShri 
619524655328SShri   PetscFunctionBegin;
619624655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
619724655328SShri 
619824655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
619924655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
620024655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
620124655328SShri   ts->ptime = ts->ptime_prev;
62028392e04aSShri Abhyankar   ts->steprollback = PETSC_TRUE; /* Flag to indicate that the step is rollbacked */
620324655328SShri   PetscFunctionReturn(0);
620424655328SShri }
6205aeb4809dSShri Abhyankar 
6206ff22ae23SHong Zhang #undef __FUNCT__
6207ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages"
6208ff22ae23SHong Zhang /*@
6209ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
6210ff22ae23SHong Zhang 
6211ff22ae23SHong Zhang    Input Parameter:
6212ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
6213ff22ae23SHong Zhang 
6214ff22ae23SHong Zhang    Level: advanced
6215ff22ae23SHong Zhang 
6216ff22ae23SHong Zhang .keywords: TS, getstages
6217ff22ae23SHong Zhang 
6218ff22ae23SHong Zhang .seealso: TSCreate()
6219ff22ae23SHong Zhang @*/
6220ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns, Vec **Y)
6221ff22ae23SHong Zhang {
6222ff22ae23SHong Zhang   PetscErrorCode ierr;
6223ff22ae23SHong Zhang 
6224ff22ae23SHong Zhang   PetscFunctionBegin;
6225ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
6226ff22ae23SHong Zhang   PetscValidPointer(ns,2);
6227ff22ae23SHong Zhang 
6228ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
6229ff22ae23SHong Zhang   else {
6230ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
6231ff22ae23SHong Zhang   }
6232ff22ae23SHong Zhang   PetscFunctionReturn(0);
6233ff22ae23SHong Zhang }
6234ff22ae23SHong Zhang 
6235847ff0e1SMatthew G. Knepley #undef __FUNCT__
6236847ff0e1SMatthew G. Knepley #define __FUNCT__ "TSComputeIJacobianDefaultColor"
6237847ff0e1SMatthew G. Knepley /*@C
6238847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
6239847ff0e1SMatthew G. Knepley 
6240847ff0e1SMatthew G. Knepley   Collective on SNES
6241847ff0e1SMatthew G. Knepley 
6242847ff0e1SMatthew G. Knepley   Input Parameters:
6243847ff0e1SMatthew G. Knepley + ts - the TS context
6244847ff0e1SMatthew G. Knepley . t - current timestep
6245847ff0e1SMatthew G. Knepley . U - state vector
6246847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
6247847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
6248847ff0e1SMatthew G. Knepley - ctx - an optional user context
6249847ff0e1SMatthew G. Knepley 
6250847ff0e1SMatthew G. Knepley   Output Parameters:
6251847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
6252847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
6253847ff0e1SMatthew G. Knepley 
6254847ff0e1SMatthew G. Knepley   Level: intermediate
6255847ff0e1SMatthew G. Knepley 
6256847ff0e1SMatthew G. Knepley   Notes:
6257847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
6258847ff0e1SMatthew G. Knepley 
6259847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
6260847ff0e1SMatthew G. Knepley 
6261847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
6262847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
6263847ff0e1SMatthew G. Knepley 
6264847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
6265847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
6266847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
6267847ff0e1SMatthew G. Knepley 
6268847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
6269847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
6270847ff0e1SMatthew G. Knepley @*/
6271847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
6272847ff0e1SMatthew G. Knepley {
6273847ff0e1SMatthew G. Knepley   SNES           snes;
6274847ff0e1SMatthew G. Knepley   MatFDColoring  color;
6275847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
6276847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
6277847ff0e1SMatthew G. Knepley 
6278847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
6279847ff0e1SMatthew G. Knepley   ierr = PetscOptionsGetBool(((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
6280847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
6281847ff0e1SMatthew G. Knepley   if (!color) {
6282847ff0e1SMatthew G. Knepley     DM         dm;
6283847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
6284847ff0e1SMatthew G. Knepley 
6285847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
6286847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
6287847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
6288847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
6289847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
6290847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
6291847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
6292847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
6293847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
6294847ff0e1SMatthew G. Knepley     } else {
6295847ff0e1SMatthew G. Knepley       MatColoring mc;
6296847ff0e1SMatthew G. Knepley 
6297847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
6298847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
6299847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
6300847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
6301847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
6302847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
6303847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
6304847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
6305847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
6306847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
6307847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
6308847ff0e1SMatthew G. Knepley     }
6309847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
6310847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
6311847ff0e1SMatthew G. Knepley   }
6312847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
6313847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
6314847ff0e1SMatthew G. Knepley   if (J != B) {
6315847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
6316847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
6317847ff0e1SMatthew G. Knepley   }
6318847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
6319847ff0e1SMatthew G. Knepley }
632093b34091SDebojyoti Ghosh 
632193b34091SDebojyoti Ghosh #undef  __FUNCT__
6322e5168f73SEmil Constantinescu #define __FUNCT__ "TSClone"
632393b34091SDebojyoti Ghosh /*@C
6324e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
632593b34091SDebojyoti Ghosh 
632693b34091SDebojyoti Ghosh   Collective on MPI_Comm
632793b34091SDebojyoti Ghosh 
632893b34091SDebojyoti Ghosh   Input Parameter:
632993b34091SDebojyoti Ghosh . tsin    - The input TS
633093b34091SDebojyoti Ghosh 
633193b34091SDebojyoti Ghosh   Output Parameter:
6332e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
633393b34091SDebojyoti Ghosh 
63345eca1a21SEmil Constantinescu   Notes:
63355eca1a21SEmil Constantinescu   This function is used to create a clone of a TS object. It is used in ARKIMEX for initializing the slope for first stage explicit methods. It will likely be replaced in the future with a mechanism of switching methods on the fly.
63365eca1a21SEmil Constantinescu 
6337baa10174SEmil Constantinescu   When using TSDestroy() on a clone the user has to first reset the correct TS reference in the embedded SNES object: e.g.: by running SNES snes_dup=NULL; TSGetSNES(ts,&snes_dup); ierr = TSSetSNES(ts,snes_dup);
63385eca1a21SEmil Constantinescu 
63395eca1a21SEmil Constantinescu   Level: developer
634093b34091SDebojyoti Ghosh 
6341e5168f73SEmil Constantinescu .keywords: TS, clone
6342e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
634393b34091SDebojyoti Ghosh @*/
6344baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
634593b34091SDebojyoti Ghosh {
634693b34091SDebojyoti Ghosh   TS             t;
634793b34091SDebojyoti Ghosh   PetscErrorCode ierr;
6348dc846ba4SSatish Balay   SNES           snes_start;
6349dc846ba4SSatish Balay   DM             dm;
6350dc846ba4SSatish Balay   TSType         type;
635193b34091SDebojyoti Ghosh 
635293b34091SDebojyoti Ghosh   PetscFunctionBegin;
635393b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
635493b34091SDebojyoti Ghosh   *tsout = NULL;
635593b34091SDebojyoti Ghosh 
63567a37829fSSatish Balay   ierr = PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
635793b34091SDebojyoti Ghosh 
635893b34091SDebojyoti Ghosh   /* General TS description */
635993b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
636093b34091SDebojyoti Ghosh   t->setupcalled       = 0;
636193b34091SDebojyoti Ghosh   t->ksp_its           = 0;
636293b34091SDebojyoti Ghosh   t->snes_its          = 0;
636393b34091SDebojyoti Ghosh   t->nwork             = 0;
636493b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
636593b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
636693b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
636793b34091SDebojyoti Ghosh 
636834561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);                   CHKERRQ(ierr);
636934561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);                       CHKERRQ(ierr);
6370d15a3a53SEmil Constantinescu 
637193b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);                             CHKERRQ(ierr);
637293b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);                                 CHKERRQ(ierr);
637393b34091SDebojyoti Ghosh 
637493b34091SDebojyoti Ghosh   t->adapt=tsin->adapt;
637593b34091SDebojyoti Ghosh   PetscObjectReference((PetscObject)t->adapt);
637693b34091SDebojyoti Ghosh 
637793b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
637893b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
637993b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
638093b34091SDebojyoti Ghosh   t->time_step_orig    = tsin->time_step_orig;
638193b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
638293b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
638393b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
638493b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
638593b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
638693b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
638793b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
638893b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
638993b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
639093b34091SDebojyoti Ghosh 
639193b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type); CHKERRQ(ierr);
639293b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);     CHKERRQ(ierr);
639393b34091SDebojyoti Ghosh 
639493b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
639593b34091SDebojyoti Ghosh 
639693b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
639793b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
639893b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
639993b34091SDebojyoti Ghosh 
640093b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
640193b34091SDebojyoti Ghosh 
640293b34091SDebojyoti Ghosh   *tsout = t;
640393b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
640493b34091SDebojyoti Ghosh }
6405