xref: /petsc/src/ts/interface/ts.c (revision baa1017425a7bea96d3f46c1bd30e5d2a06d100b)
163dd3a1aSKris Buschelman 
2b45d2f2cSJed Brown #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);
391bc952696SBarry Smith   }
392d2daff3dSHong Zhang   PetscFunctionReturn(0);
393d2daff3dSHong Zhang }
394d2daff3dSHong Zhang 
395a7a1495cSBarry Smith #undef __FUNCT__
396a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian"
397a7a1495cSBarry Smith /*@
398a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
399a7a1495cSBarry Smith       set with TSSetRHSJacobian().
400a7a1495cSBarry Smith 
401a7a1495cSBarry Smith    Collective on TS and Vec
402a7a1495cSBarry Smith 
403a7a1495cSBarry Smith    Input Parameters:
404316643e7SJed Brown +  ts - the TS context
405a7a1495cSBarry Smith .  t - current timestep
4060910c330SBarry Smith -  U - input vector
407a7a1495cSBarry Smith 
408a7a1495cSBarry Smith    Output Parameters:
409a7a1495cSBarry Smith +  A - Jacobian matrix
410a7a1495cSBarry Smith .  B - optional preconditioning matrix
411a7a1495cSBarry Smith -  flag - flag indicating matrix structure
412a7a1495cSBarry Smith 
413a7a1495cSBarry Smith    Notes:
414a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
415a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
416a7a1495cSBarry Smith 
41794b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
418a7a1495cSBarry Smith    flag parameter.
419a7a1495cSBarry Smith 
420a7a1495cSBarry Smith    Level: developer
421a7a1495cSBarry Smith 
422a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
423a7a1495cSBarry Smith 
42494b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
425a7a1495cSBarry Smith @*/
426d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
427a7a1495cSBarry Smith {
428dfbe8321SBarry Smith   PetscErrorCode ierr;
429270bf2e7SJed Brown   PetscObjectState Ustate;
43024989b8cSPeter Brune   DM             dm;
431942e3340SBarry Smith   DMTS           tsdm;
43224989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
43324989b8cSPeter Brune   void           *ctx;
43424989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
435b2df71adSDebojyoti Ghosh   TSRHSFunction  rhsfunction;
436a7a1495cSBarry Smith 
437a7a1495cSBarry Smith   PetscFunctionBegin;
4380700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4390910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4400910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
44124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
442942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
44324989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
4440298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
445b2df71adSDebojyoti Ghosh   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
44659e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
447b2df71adSDebojyoti Ghosh   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) {
4480e4ef248SJed Brown     PetscFunctionReturn(0);
449f8ede8e7SJed Brown   }
450d90be118SSean Farley 
451ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
452d90be118SSean Farley 
453e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
45494ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
45594ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
45694ab13aaSBarry Smith     if (A != B) {
45794ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
45894ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
459e1244c69SJed Brown     }
460e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
461e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
462e1244c69SJed Brown   }
463e1244c69SJed Brown 
46424989b8cSPeter Brune   if (rhsjacobianfunc) {
46594ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
466a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
467d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
468a7a1495cSBarry Smith     PetscStackPop;
46994ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
470a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
47194ab13aaSBarry Smith     PetscValidHeaderSpecific(A,MAT_CLASSID,4);
47294ab13aaSBarry Smith     PetscValidHeaderSpecific(B,MAT_CLASSID,5);
473ef66eb69SBarry Smith   } else {
47494ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
47594ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
476ef66eb69SBarry Smith   }
4770e4ef248SJed Brown   ts->rhsjacobian.time       = t;
4780910c330SBarry Smith   ts->rhsjacobian.X          = U;
47959e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
480a7a1495cSBarry Smith   PetscFunctionReturn(0);
481a7a1495cSBarry Smith }
482a7a1495cSBarry Smith 
4834a2ae208SSatish Balay #undef __FUNCT__
4844a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
485316643e7SJed Brown /*@
486d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
487d763cef2SBarry Smith 
488316643e7SJed Brown    Collective on TS and Vec
489316643e7SJed Brown 
490316643e7SJed Brown    Input Parameters:
491316643e7SJed Brown +  ts - the TS context
492316643e7SJed Brown .  t - current time
4930910c330SBarry Smith -  U - state vector
494316643e7SJed Brown 
495316643e7SJed Brown    Output Parameter:
496316643e7SJed Brown .  y - right hand side
497316643e7SJed Brown 
498316643e7SJed Brown    Note:
499316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
500316643e7SJed Brown    is used internally within the nonlinear solvers.
501316643e7SJed Brown 
502316643e7SJed Brown    Level: developer
503316643e7SJed Brown 
504316643e7SJed Brown .keywords: TS, compute
505316643e7SJed Brown 
506316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
507316643e7SJed Brown @*/
5080910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
509d763cef2SBarry Smith {
510dfbe8321SBarry Smith   PetscErrorCode ierr;
51124989b8cSPeter Brune   TSRHSFunction  rhsfunction;
51224989b8cSPeter Brune   TSIFunction    ifunction;
51324989b8cSPeter Brune   void           *ctx;
51424989b8cSPeter Brune   DM             dm;
51524989b8cSPeter Brune 
516d763cef2SBarry Smith   PetscFunctionBegin;
5170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5180910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5190700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
52024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
52124989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
5220298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
523d763cef2SBarry Smith 
524ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
525d763cef2SBarry Smith 
5260910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
52724989b8cSPeter Brune   if (rhsfunction) {
528d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
5290910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
530d763cef2SBarry Smith     PetscStackPop;
531214bc6a2SJed Brown   } else {
532214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
533b2cd27e8SJed Brown   }
53444a41b28SSean Farley 
5350910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
536d763cef2SBarry Smith   PetscFunctionReturn(0);
537d763cef2SBarry Smith }
538d763cef2SBarry Smith 
5394a2ae208SSatish Balay #undef __FUNCT__
540ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
541ef20d060SBarry Smith /*@
542ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
543ef20d060SBarry Smith 
544ef20d060SBarry Smith    Collective on TS and Vec
545ef20d060SBarry Smith 
546ef20d060SBarry Smith    Input Parameters:
547ef20d060SBarry Smith +  ts - the TS context
548ef20d060SBarry Smith -  t - current time
549ef20d060SBarry Smith 
550ef20d060SBarry Smith    Output Parameter:
5510910c330SBarry Smith .  U - the solution
552ef20d060SBarry Smith 
553ef20d060SBarry Smith    Note:
554ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
555ef20d060SBarry Smith    is used internally within the nonlinear solvers.
556ef20d060SBarry Smith 
557ef20d060SBarry Smith    Level: developer
558ef20d060SBarry Smith 
559ef20d060SBarry Smith .keywords: TS, compute
560ef20d060SBarry Smith 
561abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
562ef20d060SBarry Smith @*/
5630910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
564ef20d060SBarry Smith {
565ef20d060SBarry Smith   PetscErrorCode     ierr;
566ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
567ef20d060SBarry Smith   void               *ctx;
568ef20d060SBarry Smith   DM                 dm;
569ef20d060SBarry Smith 
570ef20d060SBarry Smith   PetscFunctionBegin;
571ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5720910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
573ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
574ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
575ef20d060SBarry Smith 
576ef20d060SBarry Smith   if (solutionfunction) {
5779b7cd975SBarry Smith     PetscStackPush("TS user solution function");
5780910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
579ef20d060SBarry Smith     PetscStackPop;
580ef20d060SBarry Smith   }
581ef20d060SBarry Smith   PetscFunctionReturn(0);
582ef20d060SBarry Smith }
5839b7cd975SBarry Smith #undef __FUNCT__
5849b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
5859b7cd975SBarry Smith /*@
5869b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
5879b7cd975SBarry Smith 
5889b7cd975SBarry Smith    Collective on TS and Vec
5899b7cd975SBarry Smith 
5909b7cd975SBarry Smith    Input Parameters:
5919b7cd975SBarry Smith +  ts - the TS context
5929b7cd975SBarry Smith -  t - current time
5939b7cd975SBarry Smith 
5949b7cd975SBarry Smith    Output Parameter:
5959b7cd975SBarry Smith .  U - the function value
5969b7cd975SBarry Smith 
5979b7cd975SBarry Smith    Note:
5989b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
5999b7cd975SBarry Smith    is used internally within the nonlinear solvers.
6009b7cd975SBarry Smith 
6019b7cd975SBarry Smith    Level: developer
6029b7cd975SBarry Smith 
6039b7cd975SBarry Smith .keywords: TS, compute
6049b7cd975SBarry Smith 
6059b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
6069b7cd975SBarry Smith @*/
6079b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
6089b7cd975SBarry Smith {
6099b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
6109b7cd975SBarry Smith   void               *ctx;
6119b7cd975SBarry Smith   DM                 dm;
6129b7cd975SBarry Smith 
6139b7cd975SBarry Smith   PetscFunctionBegin;
6149b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6159b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6169b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
6179b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
6189b7cd975SBarry Smith 
6199b7cd975SBarry Smith   if (forcing) {
6209b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
6219b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
6229b7cd975SBarry Smith     PetscStackPop;
6239b7cd975SBarry Smith   }
6249b7cd975SBarry Smith   PetscFunctionReturn(0);
6259b7cd975SBarry Smith }
626ef20d060SBarry Smith 
627ef20d060SBarry Smith #undef __FUNCT__
628214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
629214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
630214bc6a2SJed Brown {
6312dd45cf8SJed Brown   Vec            F;
632214bc6a2SJed Brown   PetscErrorCode ierr;
633214bc6a2SJed Brown 
634214bc6a2SJed Brown   PetscFunctionBegin;
6350298fd71SBarry Smith   *Frhs = NULL;
6360298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
637214bc6a2SJed Brown   if (!ts->Frhs) {
6382dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
639214bc6a2SJed Brown   }
640214bc6a2SJed Brown   *Frhs = ts->Frhs;
641214bc6a2SJed Brown   PetscFunctionReturn(0);
642214bc6a2SJed Brown }
643214bc6a2SJed Brown 
644214bc6a2SJed Brown #undef __FUNCT__
645214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
646214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
647214bc6a2SJed Brown {
648214bc6a2SJed Brown   Mat            A,B;
6492dd45cf8SJed Brown   PetscErrorCode ierr;
650214bc6a2SJed Brown 
651214bc6a2SJed Brown   PetscFunctionBegin;
652c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
653c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
6540298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
655214bc6a2SJed Brown   if (Arhs) {
656214bc6a2SJed Brown     if (!ts->Arhs) {
657214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
658214bc6a2SJed Brown     }
659214bc6a2SJed Brown     *Arhs = ts->Arhs;
660214bc6a2SJed Brown   }
661214bc6a2SJed Brown   if (Brhs) {
662214bc6a2SJed Brown     if (!ts->Brhs) {
663bdb70873SJed Brown       if (A != B) {
664214bc6a2SJed Brown         ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
665bdb70873SJed Brown       } else {
666bdb70873SJed Brown         ts->Brhs = ts->Arhs;
667bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
668bdb70873SJed Brown       }
669214bc6a2SJed Brown     }
670214bc6a2SJed Brown     *Brhs = ts->Brhs;
671214bc6a2SJed Brown   }
672214bc6a2SJed Brown   PetscFunctionReturn(0);
673214bc6a2SJed Brown }
674214bc6a2SJed Brown 
675214bc6a2SJed Brown #undef __FUNCT__
676316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
677316643e7SJed Brown /*@
6780910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
679316643e7SJed Brown 
680316643e7SJed Brown    Collective on TS and Vec
681316643e7SJed Brown 
682316643e7SJed Brown    Input Parameters:
683316643e7SJed Brown +  ts - the TS context
684316643e7SJed Brown .  t - current time
6850910c330SBarry Smith .  U - state vector
6860910c330SBarry Smith .  Udot - time derivative of state vector
687214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
688316643e7SJed Brown 
689316643e7SJed Brown    Output Parameter:
690316643e7SJed Brown .  Y - right hand side
691316643e7SJed Brown 
692316643e7SJed Brown    Note:
693316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
694316643e7SJed Brown    is used internally within the nonlinear solvers.
695316643e7SJed Brown 
696316643e7SJed Brown    If the user did did not write their equations in implicit form, this
697316643e7SJed Brown    function recasts them in implicit form.
698316643e7SJed Brown 
699316643e7SJed Brown    Level: developer
700316643e7SJed Brown 
701316643e7SJed Brown .keywords: TS, compute
702316643e7SJed Brown 
703316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
704316643e7SJed Brown @*/
7050910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
706316643e7SJed Brown {
707316643e7SJed Brown   PetscErrorCode ierr;
70824989b8cSPeter Brune   TSIFunction    ifunction;
70924989b8cSPeter Brune   TSRHSFunction  rhsfunction;
71024989b8cSPeter Brune   void           *ctx;
71124989b8cSPeter Brune   DM             dm;
712316643e7SJed Brown 
713316643e7SJed Brown   PetscFunctionBegin;
7140700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7150910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7160910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
7170700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
718316643e7SJed Brown 
71924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
72024989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
7210298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
72224989b8cSPeter Brune 
723ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
724d90be118SSean Farley 
7250910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
72624989b8cSPeter Brune   if (ifunction) {
727316643e7SJed Brown     PetscStackPush("TS user implicit function");
7280910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
729316643e7SJed Brown     PetscStackPop;
730214bc6a2SJed Brown   }
731214bc6a2SJed Brown   if (imex) {
73224989b8cSPeter Brune     if (!ifunction) {
7330910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
7342dd45cf8SJed Brown     }
73524989b8cSPeter Brune   } else if (rhsfunction) {
73624989b8cSPeter Brune     if (ifunction) {
737214bc6a2SJed Brown       Vec Frhs;
738214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
7390910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
740214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
7412dd45cf8SJed Brown     } else {
7420910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
7430910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
744316643e7SJed Brown     }
7454a6899ffSJed Brown   }
7460910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
747316643e7SJed Brown   PetscFunctionReturn(0);
748316643e7SJed Brown }
749316643e7SJed Brown 
750316643e7SJed Brown #undef __FUNCT__
751316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
752316643e7SJed Brown /*@
753316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
754316643e7SJed Brown 
755316643e7SJed Brown    Collective on TS and Vec
756316643e7SJed Brown 
757316643e7SJed Brown    Input
758316643e7SJed Brown       Input Parameters:
759316643e7SJed Brown +  ts - the TS context
760316643e7SJed Brown .  t - current timestep
7610910c330SBarry Smith .  U - state vector
7620910c330SBarry Smith .  Udot - time derivative of state vector
763214bc6a2SJed Brown .  shift - shift to apply, see note below
764214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
765316643e7SJed Brown 
766316643e7SJed Brown    Output Parameters:
767316643e7SJed Brown +  A - Jacobian matrix
768316643e7SJed Brown .  B - optional preconditioning matrix
769316643e7SJed Brown -  flag - flag indicating matrix structure
770316643e7SJed Brown 
771316643e7SJed Brown    Notes:
7720910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
773316643e7SJed Brown 
7740910c330SBarry Smith    dF/dU + shift*dF/dUdot
775316643e7SJed Brown 
776316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
777316643e7SJed Brown    is used internally within the nonlinear solvers.
778316643e7SJed Brown 
779316643e7SJed Brown    Level: developer
780316643e7SJed Brown 
781316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
782316643e7SJed Brown 
783316643e7SJed Brown .seealso:  TSSetIJacobian()
78463495f91SJed Brown @*/
785d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
786316643e7SJed Brown {
787316643e7SJed Brown   PetscErrorCode ierr;
78824989b8cSPeter Brune   TSIJacobian    ijacobian;
78924989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
79024989b8cSPeter Brune   DM             dm;
79124989b8cSPeter Brune   void           *ctx;
792316643e7SJed Brown 
793316643e7SJed Brown   PetscFunctionBegin;
7940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7950910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7960910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
797316643e7SJed Brown   PetscValidPointer(A,6);
79894ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
799316643e7SJed Brown   PetscValidPointer(B,7);
80094ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
80124989b8cSPeter Brune 
80224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
80324989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
8040298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
80524989b8cSPeter Brune 
806ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
807316643e7SJed Brown 
80894ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
80924989b8cSPeter Brune   if (ijacobian) {
810316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
811d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
812316643e7SJed Brown     PetscStackPop;
813214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
81494ab13aaSBarry Smith     PetscValidHeaderSpecific(A,MAT_CLASSID,4);
81594ab13aaSBarry Smith     PetscValidHeaderSpecific(B,MAT_CLASSID,5);
8164a6899ffSJed Brown   }
817214bc6a2SJed Brown   if (imex) {
818b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
81994ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
82094ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
82194ab13aaSBarry Smith       if (A != B) {
82294ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
82394ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
824214bc6a2SJed Brown       }
825214bc6a2SJed Brown     }
826214bc6a2SJed Brown   } else {
827e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
828e1244c69SJed Brown     if (rhsjacobian) {
829bd373395SBarry Smith       if (ijacobian) {
830214bc6a2SJed Brown         ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
831bd373395SBarry Smith       } else {
832bd373395SBarry Smith         ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr);
833214bc6a2SJed Brown       }
834d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
835e1244c69SJed Brown     }
83694ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
837e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
838e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
83994ab13aaSBarry Smith       ierr = MatScale(A,-1);CHKERRQ(ierr);
84094ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
84194ab13aaSBarry Smith       if (A != B) {
84294ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
84394ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
844316643e7SJed Brown       }
845e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
846e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
847e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
84894ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
84994ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
85094ab13aaSBarry Smith         if (A != B) {
85194ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
85294ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
853214bc6a2SJed Brown         }
854316643e7SJed Brown       }
85594ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
85694ab13aaSBarry Smith       if (A != B) {
85794ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
858316643e7SJed Brown       }
859316643e7SJed Brown     }
860316643e7SJed Brown   }
86194ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
862316643e7SJed Brown   PetscFunctionReturn(0);
863316643e7SJed Brown }
864316643e7SJed Brown 
865316643e7SJed Brown #undef __FUNCT__
8664a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
867d763cef2SBarry Smith /*@C
868d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
869b5abc632SBarry Smith     where U_t = G(t,u).
870d763cef2SBarry Smith 
8713f9fe445SBarry Smith     Logically Collective on TS
872d763cef2SBarry Smith 
873d763cef2SBarry Smith     Input Parameters:
874d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
8750298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
876d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
877d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
8780298fd71SBarry Smith           function evaluation routine (may be NULL)
879d763cef2SBarry Smith 
880d763cef2SBarry Smith     Calling sequence of func:
88187828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
882d763cef2SBarry Smith 
883d763cef2SBarry Smith +   t - current timestep
884d763cef2SBarry Smith .   u - input vector
885d763cef2SBarry Smith .   F - function vector
886d763cef2SBarry Smith -   ctx - [optional] user-defined function context
887d763cef2SBarry Smith 
888d763cef2SBarry Smith     Level: beginner
889d763cef2SBarry Smith 
8902bbac0d3SBarry Smith     Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
8912bbac0d3SBarry Smith 
892d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
893d763cef2SBarry Smith 
894ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
895d763cef2SBarry Smith @*/
896089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
897d763cef2SBarry Smith {
898089b2837SJed Brown   PetscErrorCode ierr;
899089b2837SJed Brown   SNES           snes;
9000298fd71SBarry Smith   Vec            ralloc = NULL;
90124989b8cSPeter Brune   DM             dm;
902d763cef2SBarry Smith 
903089b2837SJed Brown   PetscFunctionBegin;
9040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
905ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
90624989b8cSPeter Brune 
90724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
90824989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
909089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
910e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
911e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
912e856ceecSJed Brown     r    = ralloc;
913e856ceecSJed Brown   }
914089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
915e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
916d763cef2SBarry Smith   PetscFunctionReturn(0);
917d763cef2SBarry Smith }
918d763cef2SBarry Smith 
9194a2ae208SSatish Balay #undef __FUNCT__
920ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
921ef20d060SBarry Smith /*@C
922abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
923ef20d060SBarry Smith 
924ef20d060SBarry Smith     Logically Collective on TS
925ef20d060SBarry Smith 
926ef20d060SBarry Smith     Input Parameters:
927ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
928ef20d060SBarry Smith .   f - routine for evaluating the solution
929ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
9300298fd71SBarry Smith           function evaluation routine (may be NULL)
931ef20d060SBarry Smith 
932ef20d060SBarry Smith     Calling sequence of func:
933ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
934ef20d060SBarry Smith 
935ef20d060SBarry Smith +   t - current timestep
936ef20d060SBarry Smith .   u - output vector
937ef20d060SBarry Smith -   ctx - [optional] user-defined function context
938ef20d060SBarry Smith 
939abd5a294SJed Brown     Notes:
940abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
941abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
942abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
943abd5a294SJed Brown 
9444f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
945abd5a294SJed Brown 
946ef20d060SBarry Smith     Level: beginner
947ef20d060SBarry Smith 
948ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
949ef20d060SBarry Smith 
9509b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
951ef20d060SBarry Smith @*/
952ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
953ef20d060SBarry Smith {
954ef20d060SBarry Smith   PetscErrorCode ierr;
955ef20d060SBarry Smith   DM             dm;
956ef20d060SBarry Smith 
957ef20d060SBarry Smith   PetscFunctionBegin;
958ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
959ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
960ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
961ef20d060SBarry Smith   PetscFunctionReturn(0);
962ef20d060SBarry Smith }
963ef20d060SBarry Smith 
964ef20d060SBarry Smith #undef __FUNCT__
9659b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
9669b7cd975SBarry Smith /*@C
9679b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
9689b7cd975SBarry Smith 
9699b7cd975SBarry Smith     Logically Collective on TS
9709b7cd975SBarry Smith 
9719b7cd975SBarry Smith     Input Parameters:
9729b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
9739b7cd975SBarry Smith .   f - routine for evaluating the forcing function
9749b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
9750298fd71SBarry Smith           function evaluation routine (may be NULL)
9769b7cd975SBarry Smith 
9779b7cd975SBarry Smith     Calling sequence of func:
9789b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
9799b7cd975SBarry Smith 
9809b7cd975SBarry Smith +   t - current timestep
9819b7cd975SBarry Smith .   u - output vector
9829b7cd975SBarry Smith -   ctx - [optional] user-defined function context
9839b7cd975SBarry Smith 
9849b7cd975SBarry Smith     Notes:
9859b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
9869b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
9879b7cd975SBarry Smith 
9889b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
9899b7cd975SBarry Smith 
9909b7cd975SBarry Smith     Level: beginner
9919b7cd975SBarry Smith 
9929b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
9939b7cd975SBarry Smith 
9949b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
9959b7cd975SBarry Smith @*/
9969b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
9979b7cd975SBarry Smith {
9989b7cd975SBarry Smith   PetscErrorCode ierr;
9999b7cd975SBarry Smith   DM             dm;
10009b7cd975SBarry Smith 
10019b7cd975SBarry Smith   PetscFunctionBegin;
10029b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
10039b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
10049b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
10059b7cd975SBarry Smith   PetscFunctionReturn(0);
10069b7cd975SBarry Smith }
10079b7cd975SBarry Smith 
10089b7cd975SBarry Smith #undef __FUNCT__
10094a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
1010d763cef2SBarry Smith /*@C
1011f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
1012b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
1013d763cef2SBarry Smith 
10143f9fe445SBarry Smith    Logically Collective on TS
1015d763cef2SBarry Smith 
1016d763cef2SBarry Smith    Input Parameters:
1017d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
1018e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1019e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1020d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1021d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
10220298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1023d763cef2SBarry Smith 
1024f7ab8db6SBarry Smith    Calling sequence of f:
1025ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1026d763cef2SBarry Smith 
1027d763cef2SBarry Smith +  t - current timestep
1028d763cef2SBarry Smith .  u - input vector
1029e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1030e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1031d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1032d763cef2SBarry Smith 
1033d763cef2SBarry Smith 
1034d763cef2SBarry Smith    Level: beginner
1035d763cef2SBarry Smith 
1036d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1037d763cef2SBarry Smith 
1038ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1039d763cef2SBarry Smith 
1040d763cef2SBarry Smith @*/
1041e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1042d763cef2SBarry Smith {
1043277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1044089b2837SJed Brown   SNES           snes;
104524989b8cSPeter Brune   DM             dm;
104624989b8cSPeter Brune   TSIJacobian    ijacobian;
1047277b19d0SLisandro Dalcin 
1048d763cef2SBarry Smith   PetscFunctionBegin;
10490700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1050e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1051e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1052e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1053e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1054d763cef2SBarry Smith 
105524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
105624989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1057e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1058e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1059e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1060e1244c69SJed Brown   }
10610298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1062089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10635f659677SPeter Brune   if (!ijacobian) {
1064e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
10650e4ef248SJed Brown   }
1066e5d3d808SBarry Smith   if (Amat) {
1067e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
10680e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1069bbd56ea5SKarl Rupp 
1070e5d3d808SBarry Smith     ts->Arhs = Amat;
10710e4ef248SJed Brown   }
1072e5d3d808SBarry Smith   if (Pmat) {
1073e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
10740e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1075bbd56ea5SKarl Rupp 
1076e5d3d808SBarry Smith     ts->Brhs = Pmat;
10770e4ef248SJed Brown   }
1078d763cef2SBarry Smith   PetscFunctionReturn(0);
1079d763cef2SBarry Smith }
1080d763cef2SBarry Smith 
1081316643e7SJed Brown 
1082316643e7SJed Brown #undef __FUNCT__
1083316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
1084316643e7SJed Brown /*@C
1085b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1086316643e7SJed Brown 
10873f9fe445SBarry Smith    Logically Collective on TS
1088316643e7SJed Brown 
1089316643e7SJed Brown    Input Parameters:
1090316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
10910298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1092316643e7SJed Brown .  f   - the function evaluation routine
10930298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1094316643e7SJed Brown 
1095316643e7SJed Brown    Calling sequence of f:
1096316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1097316643e7SJed Brown 
1098316643e7SJed Brown +  t   - time at step/stage being solved
1099316643e7SJed Brown .  u   - state vector
1100316643e7SJed Brown .  u_t - time derivative of state vector
1101316643e7SJed Brown .  F   - function vector
1102316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1103316643e7SJed Brown 
1104316643e7SJed Brown    Important:
11052bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1106316643e7SJed Brown 
1107316643e7SJed Brown    Level: beginner
1108316643e7SJed Brown 
1109316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1110316643e7SJed Brown 
1111d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1112316643e7SJed Brown @*/
1113089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
1114316643e7SJed Brown {
1115089b2837SJed Brown   PetscErrorCode ierr;
1116089b2837SJed Brown   SNES           snes;
11170298fd71SBarry Smith   Vec            resalloc = NULL;
111824989b8cSPeter Brune   DM             dm;
1119316643e7SJed Brown 
1120316643e7SJed Brown   PetscFunctionBegin;
11210700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1122ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
112324989b8cSPeter Brune 
112424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
112524989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
112624989b8cSPeter Brune 
1127089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1128e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
1129e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
1130e856ceecSJed Brown     res  = resalloc;
1131e856ceecSJed Brown   }
1132089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
1133e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
1134089b2837SJed Brown   PetscFunctionReturn(0);
1135089b2837SJed Brown }
1136089b2837SJed Brown 
1137089b2837SJed Brown #undef __FUNCT__
1138089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1139089b2837SJed Brown /*@C
1140089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1141089b2837SJed Brown 
1142089b2837SJed Brown    Not Collective
1143089b2837SJed Brown 
1144089b2837SJed Brown    Input Parameter:
1145089b2837SJed Brown .  ts - the TS context
1146089b2837SJed Brown 
1147089b2837SJed Brown    Output Parameter:
11480298fd71SBarry Smith +  r - vector to hold residual (or NULL)
11490298fd71SBarry Smith .  func - the function to compute residual (or NULL)
11500298fd71SBarry Smith -  ctx - the function context (or NULL)
1151089b2837SJed Brown 
1152089b2837SJed Brown    Level: advanced
1153089b2837SJed Brown 
1154089b2837SJed Brown .keywords: TS, nonlinear, get, function
1155089b2837SJed Brown 
1156089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1157089b2837SJed Brown @*/
1158089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1159089b2837SJed Brown {
1160089b2837SJed Brown   PetscErrorCode ierr;
1161089b2837SJed Brown   SNES           snes;
116224989b8cSPeter Brune   DM             dm;
1163089b2837SJed Brown 
1164089b2837SJed Brown   PetscFunctionBegin;
1165089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1166089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11670298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
116824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
116924989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1170089b2837SJed Brown   PetscFunctionReturn(0);
1171089b2837SJed Brown }
1172089b2837SJed Brown 
1173089b2837SJed Brown #undef __FUNCT__
1174089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1175089b2837SJed Brown /*@C
1176089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1177089b2837SJed Brown 
1178089b2837SJed Brown    Not Collective
1179089b2837SJed Brown 
1180089b2837SJed Brown    Input Parameter:
1181089b2837SJed Brown .  ts - the TS context
1182089b2837SJed Brown 
1183089b2837SJed Brown    Output Parameter:
11840298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
11850298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
11860298fd71SBarry Smith -  ctx - the function context (or NULL)
1187089b2837SJed Brown 
1188089b2837SJed Brown    Level: advanced
1189089b2837SJed Brown 
1190089b2837SJed Brown .keywords: TS, nonlinear, get, function
1191089b2837SJed Brown 
11922bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1193089b2837SJed Brown @*/
1194089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1195089b2837SJed Brown {
1196089b2837SJed Brown   PetscErrorCode ierr;
1197089b2837SJed Brown   SNES           snes;
119824989b8cSPeter Brune   DM             dm;
1199089b2837SJed Brown 
1200089b2837SJed Brown   PetscFunctionBegin;
1201089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1202089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
12030298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
120424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
120524989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1206316643e7SJed Brown   PetscFunctionReturn(0);
1207316643e7SJed Brown }
1208316643e7SJed Brown 
1209316643e7SJed Brown #undef __FUNCT__
1210316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1211316643e7SJed Brown /*@C
1212a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1213ae8867d6SBarry Smith         provided with TSSetIFunction().
1214316643e7SJed Brown 
12153f9fe445SBarry Smith    Logically Collective on TS
1216316643e7SJed Brown 
1217316643e7SJed Brown    Input Parameters:
1218316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1219e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1220e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1221316643e7SJed Brown .  f   - the Jacobian evaluation routine
12220298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1223316643e7SJed Brown 
1224316643e7SJed Brown    Calling sequence of f:
1225ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1226316643e7SJed Brown 
1227316643e7SJed Brown +  t    - time at step/stage being solved
12281b4a444bSJed Brown .  U    - state vector
12291b4a444bSJed Brown .  U_t  - time derivative of state vector
1230316643e7SJed Brown .  a    - shift
1231e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1232e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1233316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1234316643e7SJed Brown 
1235316643e7SJed Brown    Notes:
1236e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1237316643e7SJed Brown 
1238a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1239b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1240a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1241a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1242a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1243a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1244a4f0a591SBarry Smith 
1245316643e7SJed Brown    Level: beginner
1246316643e7SJed Brown 
1247316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1248316643e7SJed Brown 
1249ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1250316643e7SJed Brown 
1251316643e7SJed Brown @*/
1252e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1253316643e7SJed Brown {
1254316643e7SJed Brown   PetscErrorCode ierr;
1255089b2837SJed Brown   SNES           snes;
125624989b8cSPeter Brune   DM             dm;
1257316643e7SJed Brown 
1258316643e7SJed Brown   PetscFunctionBegin;
12590700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1260e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1261e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1262e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1263e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
126424989b8cSPeter Brune 
126524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
126624989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
126724989b8cSPeter Brune 
1268089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1269e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1270316643e7SJed Brown   PetscFunctionReturn(0);
1271316643e7SJed Brown }
1272316643e7SJed Brown 
12734a2ae208SSatish Balay #undef __FUNCT__
1274e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse"
1275e1244c69SJed Brown /*@
1276e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1277e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1278e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1279e1244c69SJed Brown    not been changed by the TS.
1280e1244c69SJed Brown 
1281e1244c69SJed Brown    Logically Collective
1282e1244c69SJed Brown 
1283e1244c69SJed Brown    Input Arguments:
1284e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1285e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1286e1244c69SJed Brown 
1287e1244c69SJed Brown    Level: intermediate
1288e1244c69SJed Brown 
1289e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1290e1244c69SJed Brown @*/
1291e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1292e1244c69SJed Brown {
1293e1244c69SJed Brown   PetscFunctionBegin;
1294e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1295e1244c69SJed Brown   PetscFunctionReturn(0);
1296e1244c69SJed Brown }
1297e1244c69SJed Brown 
1298e1244c69SJed Brown #undef __FUNCT__
129955849f57SBarry Smith #define __FUNCT__ "TSLoad"
130055849f57SBarry Smith /*@C
130155849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
130255849f57SBarry Smith 
130355849f57SBarry Smith   Collective on PetscViewer
130455849f57SBarry Smith 
130555849f57SBarry Smith   Input Parameters:
130655849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
130755849f57SBarry Smith            some related function before a call to TSLoad().
130855849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
130955849f57SBarry Smith 
131055849f57SBarry Smith    Level: intermediate
131155849f57SBarry Smith 
131255849f57SBarry Smith   Notes:
131355849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
131455849f57SBarry Smith 
131555849f57SBarry Smith   Notes for advanced users:
131655849f57SBarry Smith   Most users should not need to know the details of the binary storage
131755849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
131855849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
131955849f57SBarry Smith   format is
132055849f57SBarry Smith .vb
132155849f57SBarry Smith      has not yet been determined
132255849f57SBarry Smith .ve
132355849f57SBarry Smith 
132455849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
132555849f57SBarry Smith @*/
1326f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
132755849f57SBarry Smith {
132855849f57SBarry Smith   PetscErrorCode ierr;
132955849f57SBarry Smith   PetscBool      isbinary;
133055849f57SBarry Smith   PetscInt       classid;
133155849f57SBarry Smith   char           type[256];
13322d53ad75SBarry Smith   DMTS           sdm;
1333ad6bc421SBarry Smith   DM             dm;
133455849f57SBarry Smith 
133555849f57SBarry Smith   PetscFunctionBegin;
1336f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
133755849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
133855849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
133955849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
134055849f57SBarry Smith 
134155849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1342ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
134355849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1344f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1345f2c2a1b9SBarry Smith   if (ts->ops->load) {
1346f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1347f2c2a1b9SBarry Smith   }
1348ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1349ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1350ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1351f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1352f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
13532d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13542d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
135555849f57SBarry Smith   PetscFunctionReturn(0);
135655849f57SBarry Smith }
135755849f57SBarry Smith 
13589804daf3SBarry Smith #include <petscdraw.h>
1359e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1360e04113cfSBarry Smith #include <petscviewersaws.h>
1361f05ece33SBarry Smith #endif
136255849f57SBarry Smith #undef __FUNCT__
13634a2ae208SSatish Balay #define __FUNCT__ "TSView"
13647e2c5f70SBarry Smith /*@C
1365d763cef2SBarry Smith     TSView - Prints the TS data structure.
1366d763cef2SBarry Smith 
13674c49b128SBarry Smith     Collective on TS
1368d763cef2SBarry Smith 
1369d763cef2SBarry Smith     Input Parameters:
1370d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1371d763cef2SBarry Smith -   viewer - visualization context
1372d763cef2SBarry Smith 
1373d763cef2SBarry Smith     Options Database Key:
1374d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1375d763cef2SBarry Smith 
1376d763cef2SBarry Smith     Notes:
1377d763cef2SBarry Smith     The available visualization contexts include
1378b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1379b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1380d763cef2SBarry Smith          output where only the first processor opens
1381d763cef2SBarry Smith          the file.  All other processors send their
1382d763cef2SBarry Smith          data to the first processor to print.
1383d763cef2SBarry Smith 
1384d763cef2SBarry Smith     The user can open an alternative visualization context with
1385b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1386d763cef2SBarry Smith 
1387d763cef2SBarry Smith     Level: beginner
1388d763cef2SBarry Smith 
1389d763cef2SBarry Smith .keywords: TS, timestep, view
1390d763cef2SBarry Smith 
1391b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1392d763cef2SBarry Smith @*/
13937087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1394d763cef2SBarry Smith {
1395dfbe8321SBarry Smith   PetscErrorCode ierr;
139619fd82e9SBarry Smith   TSType         type;
13972b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
13982d53ad75SBarry Smith   DMTS           sdm;
1399e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1400536b137fSBarry Smith   PetscBool      issaws;
1401f05ece33SBarry Smith #endif
1402d763cef2SBarry Smith 
1403d763cef2SBarry Smith   PetscFunctionBegin;
14040700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
14053050cee2SBarry Smith   if (!viewer) {
1406ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
14073050cee2SBarry Smith   }
14080700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1409c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1410fd16b177SBarry Smith 
1411251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1412251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
141355849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
14142b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1415e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1416536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1417f05ece33SBarry Smith #endif
141832077d6dSBarry Smith   if (iascii) {
1419dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
142077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
14217c8652ddSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1422d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
14235ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1424c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1425d763cef2SBarry Smith     }
14265ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1427193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
14282d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14292d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1430d52bd9f3SBarry Smith     if (ts->ops->view) {
1431d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1432d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1433d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1434d52bd9f3SBarry Smith     }
14350f5bd95cSBarry Smith   } else if (isstring) {
1436a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1437b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
143855849f57SBarry Smith   } else if (isbinary) {
143955849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
144055849f57SBarry Smith     MPI_Comm    comm;
144155849f57SBarry Smith     PetscMPIInt rank;
144255849f57SBarry Smith     char        type[256];
144355849f57SBarry Smith 
144455849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
144555849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
144655849f57SBarry Smith     if (!rank) {
144755849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
144855849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
144955849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
145055849f57SBarry Smith     }
145155849f57SBarry Smith     if (ts->ops->view) {
145255849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
145355849f57SBarry Smith     }
1454f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1455f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
14562d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14572d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
14582b0a91c0SBarry Smith   } else if (isdraw) {
14592b0a91c0SBarry Smith     PetscDraw draw;
14602b0a91c0SBarry Smith     char      str[36];
146189fd9fafSBarry Smith     PetscReal x,y,bottom,h;
14622b0a91c0SBarry Smith 
14632b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
14642b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
14652b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
14662b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
14670298fd71SBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
146889fd9fafSBarry Smith     bottom = y - h;
14692b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
14702b0a91c0SBarry Smith     if (ts->ops->view) {
14712b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14722b0a91c0SBarry Smith     }
14732b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1474e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1475536b137fSBarry Smith   } else if (issaws) {
1476d45a07a7SBarry Smith     PetscMPIInt rank;
14772657e9d9SBarry Smith     const char  *name;
14782657e9d9SBarry Smith 
14792657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1480d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1481d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1482d45a07a7SBarry Smith       char       dir[1024];
1483d45a07a7SBarry Smith 
1484e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
1485a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
14862657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
14872657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
14882657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
1489d763cef2SBarry Smith     }
14900acecf5bSBarry Smith     if (ts->ops->view) {
14910acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14920acecf5bSBarry Smith     }
1493f05ece33SBarry Smith #endif
1494f05ece33SBarry Smith   }
1495f05ece33SBarry Smith 
1496b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1497251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1498b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1499d763cef2SBarry Smith   PetscFunctionReturn(0);
1500d763cef2SBarry Smith }
1501d763cef2SBarry Smith 
1502d763cef2SBarry Smith 
15034a2ae208SSatish Balay #undef __FUNCT__
15044a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1505b07ff414SBarry Smith /*@
1506d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1507d763cef2SBarry Smith    the timesteppers.
1508d763cef2SBarry Smith 
15093f9fe445SBarry Smith    Logically Collective on TS
1510d763cef2SBarry Smith 
1511d763cef2SBarry Smith    Input Parameters:
1512d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1513d763cef2SBarry Smith -  usrP - optional user context
1514d763cef2SBarry Smith 
1515d763cef2SBarry Smith    Level: intermediate
1516d763cef2SBarry Smith 
1517d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1518d763cef2SBarry Smith 
1519d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1520d763cef2SBarry Smith @*/
15217087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1522d763cef2SBarry Smith {
1523d763cef2SBarry Smith   PetscFunctionBegin;
15240700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1525d763cef2SBarry Smith   ts->user = usrP;
1526d763cef2SBarry Smith   PetscFunctionReturn(0);
1527d763cef2SBarry Smith }
1528d763cef2SBarry Smith 
15294a2ae208SSatish Balay #undef __FUNCT__
15304a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1531b07ff414SBarry Smith /*@
1532d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1533d763cef2SBarry Smith     timestepper.
1534d763cef2SBarry Smith 
1535d763cef2SBarry Smith     Not Collective
1536d763cef2SBarry Smith 
1537d763cef2SBarry Smith     Input Parameter:
1538d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1539d763cef2SBarry Smith 
1540d763cef2SBarry Smith     Output Parameter:
1541d763cef2SBarry Smith .   usrP - user context
1542d763cef2SBarry Smith 
1543d763cef2SBarry Smith     Level: intermediate
1544d763cef2SBarry Smith 
1545d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1546d763cef2SBarry Smith 
1547d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1548d763cef2SBarry Smith @*/
1549e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1550d763cef2SBarry Smith {
1551d763cef2SBarry Smith   PetscFunctionBegin;
15520700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1553e71120c6SJed Brown   *(void**)usrP = ts->user;
1554d763cef2SBarry Smith   PetscFunctionReturn(0);
1555d763cef2SBarry Smith }
1556d763cef2SBarry Smith 
15574a2ae208SSatish Balay #undef __FUNCT__
15584a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1559d763cef2SBarry Smith /*@
1560b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1561d763cef2SBarry Smith 
1562d763cef2SBarry Smith    Not Collective
1563d763cef2SBarry Smith 
1564d763cef2SBarry Smith    Input Parameter:
1565d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1566d763cef2SBarry Smith 
1567d763cef2SBarry Smith    Output Parameter:
1568b8123daeSJed Brown .  iter - number of steps completed so far
1569d763cef2SBarry Smith 
1570d763cef2SBarry Smith    Level: intermediate
1571d763cef2SBarry Smith 
1572d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
15739be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
1574d763cef2SBarry Smith @*/
15757087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1576d763cef2SBarry Smith {
1577d763cef2SBarry Smith   PetscFunctionBegin;
15780700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15794482741eSBarry Smith   PetscValidIntPointer(iter,2);
1580d763cef2SBarry Smith   *iter = ts->steps;
1581d763cef2SBarry Smith   PetscFunctionReturn(0);
1582d763cef2SBarry Smith }
1583d763cef2SBarry Smith 
15844a2ae208SSatish Balay #undef __FUNCT__
15854a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1586d763cef2SBarry Smith /*@
1587d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1588d763cef2SBarry Smith    as well as the initial time.
1589d763cef2SBarry Smith 
15903f9fe445SBarry Smith    Logically Collective on TS
1591d763cef2SBarry Smith 
1592d763cef2SBarry Smith    Input Parameters:
1593d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1594d763cef2SBarry Smith .  initial_time - the initial time
1595d763cef2SBarry Smith -  time_step - the size of the timestep
1596d763cef2SBarry Smith 
1597d763cef2SBarry Smith    Level: intermediate
1598d763cef2SBarry Smith 
1599d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1600d763cef2SBarry Smith 
1601d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1602d763cef2SBarry Smith @*/
16037087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1604d763cef2SBarry Smith {
1605e144a568SJed Brown   PetscErrorCode ierr;
1606e144a568SJed Brown 
1607d763cef2SBarry Smith   PetscFunctionBegin;
16080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1609e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1610d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1611d763cef2SBarry Smith   PetscFunctionReturn(0);
1612d763cef2SBarry Smith }
1613d763cef2SBarry Smith 
16144a2ae208SSatish Balay #undef __FUNCT__
16154a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1616d763cef2SBarry Smith /*@
1617d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1618d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1619d763cef2SBarry Smith 
16203f9fe445SBarry Smith    Logically Collective on TS
1621d763cef2SBarry Smith 
1622d763cef2SBarry Smith    Input Parameters:
1623d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1624d763cef2SBarry Smith -  time_step - the size of the timestep
1625d763cef2SBarry Smith 
1626d763cef2SBarry Smith    Level: intermediate
1627d763cef2SBarry Smith 
1628d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1629d763cef2SBarry Smith 
1630d763cef2SBarry Smith .keywords: TS, set, timestep
1631d763cef2SBarry Smith @*/
16327087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1633d763cef2SBarry Smith {
1634d763cef2SBarry Smith   PetscFunctionBegin;
16350700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1636c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1637d763cef2SBarry Smith   ts->time_step      = time_step;
163831748224SBarry Smith   ts->time_step_orig = time_step;
1639d763cef2SBarry Smith   PetscFunctionReturn(0);
1640d763cef2SBarry Smith }
1641d763cef2SBarry Smith 
16424a2ae208SSatish Balay #undef __FUNCT__
1643a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1644a43b19c4SJed Brown /*@
164549354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
164649354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
164749354f04SShri Abhyankar      or just return at the final time TS computed.
1648a43b19c4SJed Brown 
1649a43b19c4SJed Brown   Logically Collective on TS
1650a43b19c4SJed Brown 
1651a43b19c4SJed Brown    Input Parameter:
1652a43b19c4SJed Brown +   ts - the time-step context
165349354f04SShri Abhyankar -   eftopt - exact final time option
1654a43b19c4SJed Brown 
1655a43b19c4SJed Brown    Level: beginner
1656a43b19c4SJed Brown 
1657a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1658a43b19c4SJed Brown @*/
165949354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1660a43b19c4SJed Brown {
1661a43b19c4SJed Brown   PetscFunctionBegin;
1662a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
166349354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
166449354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1665a43b19c4SJed Brown   PetscFunctionReturn(0);
1666a43b19c4SJed Brown }
1667a43b19c4SJed Brown 
1668a43b19c4SJed Brown #undef __FUNCT__
16694a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1670d763cef2SBarry Smith /*@
1671d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1672d763cef2SBarry Smith 
1673d763cef2SBarry Smith    Not Collective
1674d763cef2SBarry Smith 
1675d763cef2SBarry Smith    Input Parameter:
1676d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1677d763cef2SBarry Smith 
1678d763cef2SBarry Smith    Output Parameter:
1679d763cef2SBarry Smith .  dt - the current timestep size
1680d763cef2SBarry Smith 
1681d763cef2SBarry Smith    Level: intermediate
1682d763cef2SBarry Smith 
1683d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1684d763cef2SBarry Smith 
1685d763cef2SBarry Smith .keywords: TS, get, timestep
1686d763cef2SBarry Smith @*/
16877087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1688d763cef2SBarry Smith {
1689d763cef2SBarry Smith   PetscFunctionBegin;
16900700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1691f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1692d763cef2SBarry Smith   *dt = ts->time_step;
1693d763cef2SBarry Smith   PetscFunctionReturn(0);
1694d763cef2SBarry Smith }
1695d763cef2SBarry Smith 
16964a2ae208SSatish Balay #undef __FUNCT__
16974a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1698d8e5e3e6SSatish Balay /*@
1699d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1700d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1701d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1702d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1703d763cef2SBarry Smith 
1704d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1705d763cef2SBarry Smith 
1706d763cef2SBarry Smith    Input Parameter:
1707d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1708d763cef2SBarry Smith 
1709d763cef2SBarry Smith    Output Parameter:
1710d763cef2SBarry Smith .  v - the vector containing the solution
1711d763cef2SBarry Smith 
1712d763cef2SBarry Smith    Level: intermediate
1713d763cef2SBarry Smith 
1714d763cef2SBarry Smith .seealso: TSGetTimeStep()
1715d763cef2SBarry Smith 
1716d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1717d763cef2SBarry Smith @*/
17187087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1719d763cef2SBarry Smith {
1720d763cef2SBarry Smith   PetscFunctionBegin;
17210700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17224482741eSBarry Smith   PetscValidPointer(v,2);
17238737fe31SLisandro Dalcin   *v = ts->vec_sol;
1724d763cef2SBarry Smith   PetscFunctionReturn(0);
1725d763cef2SBarry Smith }
1726d763cef2SBarry Smith 
1727c235aa8dSHong Zhang #undef __FUNCT__
17280cf82b59SBarry Smith #define __FUNCT__ "TSAdjointGetCostGradients"
1729c235aa8dSHong Zhang /*@
17300cf82b59SBarry Smith    TSAdjointGetCostGradients - Returns the gradients from the TSAdjointSolve()
1731c235aa8dSHong Zhang 
1732c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
1733c235aa8dSHong Zhang 
1734c235aa8dSHong Zhang    Input Parameter:
1735c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
1736c235aa8dSHong Zhang 
1737c235aa8dSHong Zhang    Output Parameter:
1738abc2977eSBarry Smith +  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
1739abc2977eSBarry Smith -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters
1740c235aa8dSHong Zhang 
1741c235aa8dSHong Zhang    Level: intermediate
1742c235aa8dSHong Zhang 
1743c235aa8dSHong Zhang .seealso: TSGetTimeStep()
1744c235aa8dSHong Zhang 
1745c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
1746c235aa8dSHong Zhang @*/
1747abc2977eSBarry Smith PetscErrorCode  TSAdjointGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu)
1748c235aa8dSHong Zhang {
1749c235aa8dSHong Zhang   PetscFunctionBegin;
1750c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1751abc2977eSBarry Smith   if (numcost) *numcost = ts->numcost;
1752abc2977eSBarry Smith   if (lambda)  *lambda  = ts->vecs_sensi;
1753abc2977eSBarry Smith   if (mu)      *mu      = ts->vecs_sensip;
1754c235aa8dSHong Zhang   PetscFunctionReturn(0);
1755c235aa8dSHong Zhang }
1756c235aa8dSHong Zhang 
1757bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
17584a2ae208SSatish Balay #undef __FUNCT__
1759bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1760d8e5e3e6SSatish Balay /*@
1761bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1762d763cef2SBarry Smith 
1763bdad233fSMatthew Knepley   Not collective
1764d763cef2SBarry Smith 
1765bdad233fSMatthew Knepley   Input Parameters:
1766bdad233fSMatthew Knepley + ts   - The TS
1767bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1768d763cef2SBarry Smith .vb
17690910c330SBarry Smith          U_t - A U = 0      (linear)
17700910c330SBarry Smith          U_t - A(t) U = 0   (linear)
17710910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1772d763cef2SBarry Smith .ve
1773d763cef2SBarry Smith 
1774d763cef2SBarry Smith    Level: beginner
1775d763cef2SBarry Smith 
1776bdad233fSMatthew Knepley .keywords: TS, problem type
1777bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1778d763cef2SBarry Smith @*/
17797087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1780a7cc72afSBarry Smith {
17819e2a6581SJed Brown   PetscErrorCode ierr;
17829e2a6581SJed Brown 
1783d763cef2SBarry Smith   PetscFunctionBegin;
17840700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1785bdad233fSMatthew Knepley   ts->problem_type = type;
17869e2a6581SJed Brown   if (type == TS_LINEAR) {
17879e2a6581SJed Brown     SNES snes;
17889e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
17899e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
17909e2a6581SJed Brown   }
1791d763cef2SBarry Smith   PetscFunctionReturn(0);
1792d763cef2SBarry Smith }
1793d763cef2SBarry Smith 
1794bdad233fSMatthew Knepley #undef __FUNCT__
1795bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1796bdad233fSMatthew Knepley /*@C
1797bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1798bdad233fSMatthew Knepley 
1799bdad233fSMatthew Knepley   Not collective
1800bdad233fSMatthew Knepley 
1801bdad233fSMatthew Knepley   Input Parameter:
1802bdad233fSMatthew Knepley . ts   - The TS
1803bdad233fSMatthew Knepley 
1804bdad233fSMatthew Knepley   Output Parameter:
1805bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1806bdad233fSMatthew Knepley .vb
1807089b2837SJed Brown          M U_t = A U
1808089b2837SJed Brown          M(t) U_t = A(t) U
1809b5abc632SBarry Smith          F(t,U,U_t)
1810bdad233fSMatthew Knepley .ve
1811bdad233fSMatthew Knepley 
1812bdad233fSMatthew Knepley    Level: beginner
1813bdad233fSMatthew Knepley 
1814bdad233fSMatthew Knepley .keywords: TS, problem type
1815bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1816bdad233fSMatthew Knepley @*/
18177087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1818a7cc72afSBarry Smith {
1819bdad233fSMatthew Knepley   PetscFunctionBegin;
18200700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
18214482741eSBarry Smith   PetscValidIntPointer(type,2);
1822bdad233fSMatthew Knepley   *type = ts->problem_type;
1823bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1824bdad233fSMatthew Knepley }
1825d763cef2SBarry Smith 
18264a2ae208SSatish Balay #undef __FUNCT__
18274a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1828d763cef2SBarry Smith /*@
1829d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1830d763cef2SBarry Smith    of a timestepper.
1831d763cef2SBarry Smith 
1832d763cef2SBarry Smith    Collective on TS
1833d763cef2SBarry Smith 
1834d763cef2SBarry Smith    Input Parameter:
1835d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1836d763cef2SBarry Smith 
1837d763cef2SBarry Smith    Notes:
1838d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1839d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1840d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1841d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1842d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1843d763cef2SBarry Smith 
1844d763cef2SBarry Smith    Level: advanced
1845d763cef2SBarry Smith 
1846d763cef2SBarry Smith .keywords: TS, timestep, setup
1847d763cef2SBarry Smith 
1848d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1849d763cef2SBarry Smith @*/
18507087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1851d763cef2SBarry Smith {
1852dfbe8321SBarry Smith   PetscErrorCode ierr;
18536c6b9e74SPeter Brune   DM             dm;
18546c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
1855d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
18566c6b9e74SPeter Brune   TSIJacobian    ijac;
18576c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1858d763cef2SBarry Smith 
1859d763cef2SBarry Smith   PetscFunctionBegin;
18600700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1861277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1862277b19d0SLisandro Dalcin 
18632c18e0fdSBarry Smith   ts->total_steps = 0;
18647adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
18659596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1866d763cef2SBarry Smith   }
1867277b19d0SLisandro Dalcin 
1868277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1869277b19d0SLisandro Dalcin 
18701c3436cfSJed Brown 
1871552698daSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
1872277b19d0SLisandro Dalcin 
1873e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
1874e1244c69SJed Brown     Mat Amat,Pmat;
1875e1244c69SJed Brown     SNES snes;
1876e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1877e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
1878e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
1879e1244c69SJed Brown      * have displaced the RHS matrix */
1880e1244c69SJed Brown     if (Amat == ts->Arhs) {
1881e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
1882e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
1883e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
1884e1244c69SJed Brown     }
1885e1244c69SJed Brown     if (Pmat == ts->Brhs) {
1886e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
1887e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
1888e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
1889e1244c69SJed Brown     }
1890e1244c69SJed Brown   }
1891277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1892000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1893277b19d0SLisandro Dalcin   }
1894277b19d0SLisandro Dalcin 
18956c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
18966c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
18976c6b9e74SPeter Brune    */
18986c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
18990298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
19006c6b9e74SPeter Brune   if (!func) {
19016c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
19026c6b9e74SPeter Brune   }
19036c6b9e74SPeter 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.
19046c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
19056c6b9e74SPeter Brune    */
19060298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
19070298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
19080298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
19096c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
19106c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
19116c6b9e74SPeter Brune   }
1912277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1913277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1914277b19d0SLisandro Dalcin }
1915277b19d0SLisandro Dalcin 
1916277b19d0SLisandro Dalcin #undef __FUNCT__
1917f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp"
1918f6a906c0SBarry Smith /*@
1919f6a906c0SBarry Smith    TSAdjointSetUp - Sets up the internal data structures for the later use
1920f6a906c0SBarry Smith    of an adjoint solver
1921f6a906c0SBarry Smith 
1922f6a906c0SBarry Smith    Collective on TS
1923f6a906c0SBarry Smith 
1924f6a906c0SBarry Smith    Input Parameter:
1925f6a906c0SBarry Smith .  ts - the TS context obtained from TSCreate()
1926f6a906c0SBarry Smith 
1927f6a906c0SBarry Smith    Notes:
1928f6a906c0SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1929f6a906c0SBarry Smith    TSSetUp(), since these actions will automatically occur during
1930f6a906c0SBarry Smith    the call to TSStep().  However, if one wishes to control this
1931f6a906c0SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1932f6a906c0SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1933f6a906c0SBarry Smith 
1934f6a906c0SBarry Smith    Level: advanced
1935f6a906c0SBarry Smith 
1936f6a906c0SBarry Smith .keywords: TS, timestep, setup
1937f6a906c0SBarry Smith 
1938f6a906c0SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1939f6a906c0SBarry Smith @*/
1940f6a906c0SBarry Smith PetscErrorCode  TSAdjointSetUp(TS ts)
1941f6a906c0SBarry Smith {
1942f6a906c0SBarry Smith   PetscErrorCode ierr;
1943f6a906c0SBarry Smith 
1944f6a906c0SBarry Smith   PetscFunctionBegin;
1945f6a906c0SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1946f6a906c0SBarry Smith   if (ts->adjointsetupcalled) PetscFunctionReturn(0);
19470cf82b59SBarry Smith   if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSAdjointSetCostGradients() first");
1948d8151eeaSBarry Smith 
1949d8151eeaSBarry Smith   ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&ts->vecs_drdy);CHKERRQ(ierr);
1950d8151eeaSBarry Smith   if (ts->vecs_sensip){
1951d8151eeaSBarry Smith     ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&ts->vecs_drdp);CHKERRQ(ierr);
1952d8151eeaSBarry Smith   }
1953d8151eeaSBarry Smith 
195442f2b339SBarry Smith   if (ts->ops->adjointsetup) {
195542f2b339SBarry Smith     ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr);
1956f6a906c0SBarry Smith   }
1957f6a906c0SBarry Smith   ts->adjointsetupcalled = PETSC_TRUE;
1958f6a906c0SBarry Smith   PetscFunctionReturn(0);
1959f6a906c0SBarry Smith }
1960f6a906c0SBarry Smith 
1961f6a906c0SBarry Smith #undef __FUNCT__
1962277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1963277b19d0SLisandro Dalcin /*@
1964277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1965277b19d0SLisandro Dalcin 
1966277b19d0SLisandro Dalcin    Collective on TS
1967277b19d0SLisandro Dalcin 
1968277b19d0SLisandro Dalcin    Input Parameter:
1969277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1970277b19d0SLisandro Dalcin 
1971277b19d0SLisandro Dalcin    Level: beginner
1972277b19d0SLisandro Dalcin 
1973277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1974277b19d0SLisandro Dalcin 
1975277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1976277b19d0SLisandro Dalcin @*/
1977277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1978277b19d0SLisandro Dalcin {
1979277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1980277b19d0SLisandro Dalcin 
1981277b19d0SLisandro Dalcin   PetscFunctionBegin;
1982277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1983b18ea86cSHong Zhang 
1984277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1985277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1986277b19d0SLisandro Dalcin   }
1987277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);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);
1996b29392e1SShri   ierr = ISDestroy(&ts->is_diff);CHKERRQ(ierr);
1997bbd56ea5SKarl Rupp 
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   }
2002d8151eeaSBarry Smith   ts->vecs_sensi  = NULL;
2003d8151eeaSBarry Smith   ts->vecs_sensip = NULL;
2004ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
20052c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
200636eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
2007277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
2008d763cef2SBarry Smith   PetscFunctionReturn(0);
2009d763cef2SBarry Smith }
2010d763cef2SBarry Smith 
20114a2ae208SSatish Balay #undef __FUNCT__
20124a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
2013d8e5e3e6SSatish Balay /*@
2014d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
2015d763cef2SBarry Smith    with TSCreate().
2016d763cef2SBarry Smith 
2017d763cef2SBarry Smith    Collective on TS
2018d763cef2SBarry Smith 
2019d763cef2SBarry Smith    Input Parameter:
2020d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2021d763cef2SBarry Smith 
2022d763cef2SBarry Smith    Level: beginner
2023d763cef2SBarry Smith 
2024d763cef2SBarry Smith .keywords: TS, timestepper, destroy
2025d763cef2SBarry Smith 
2026d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
2027d763cef2SBarry Smith @*/
20286bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
2029d763cef2SBarry Smith {
20306849ba73SBarry Smith   PetscErrorCode ierr;
2031d763cef2SBarry Smith 
2032d763cef2SBarry Smith   PetscFunctionBegin;
20336bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
20346bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
20356bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2036d763cef2SBarry Smith 
20376bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2038277b19d0SLisandro Dalcin 
2039e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2040e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
20416bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
20426d4c513bSLisandro Dalcin 
2043bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2044bc952696SBarry Smith 
204584df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
2046aeb4809dSShri Abhyankar   if ((*ts)->event) {
2047aeb4809dSShri Abhyankar     ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr);
2048aeb4809dSShri Abhyankar   }
20496bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
20506bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
20516bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
20526d4c513bSLisandro Dalcin 
2053a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2054d763cef2SBarry Smith   PetscFunctionReturn(0);
2055d763cef2SBarry Smith }
2056d763cef2SBarry Smith 
20574a2ae208SSatish Balay #undef __FUNCT__
20584a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
2059d8e5e3e6SSatish Balay /*@
2060d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2061d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2062d763cef2SBarry Smith 
2063d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2064d763cef2SBarry Smith 
2065d763cef2SBarry Smith    Input Parameter:
2066d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2067d763cef2SBarry Smith 
2068d763cef2SBarry Smith    Output Parameter:
2069d763cef2SBarry Smith .  snes - the nonlinear solver context
2070d763cef2SBarry Smith 
2071d763cef2SBarry Smith    Notes:
2072d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2073d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
207494b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2075d763cef2SBarry Smith 
2076d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
20770298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2078d763cef2SBarry Smith 
2079d763cef2SBarry Smith    Level: beginner
2080d763cef2SBarry Smith 
2081d763cef2SBarry Smith .keywords: timestep, get, SNES
2082d763cef2SBarry Smith @*/
20837087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2084d763cef2SBarry Smith {
2085d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2086d372ba47SLisandro Dalcin 
2087d763cef2SBarry Smith   PetscFunctionBegin;
20880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20894482741eSBarry Smith   PetscValidPointer(snes,2);
2090d372ba47SLisandro Dalcin   if (!ts->snes) {
2091ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
20920298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
20933bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2094d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2095496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
20969e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
20979e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
20989e2a6581SJed Brown     }
2099d372ba47SLisandro Dalcin   }
2100d763cef2SBarry Smith   *snes = ts->snes;
2101d763cef2SBarry Smith   PetscFunctionReturn(0);
2102d763cef2SBarry Smith }
2103d763cef2SBarry Smith 
21044a2ae208SSatish Balay #undef __FUNCT__
2105deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
2106deb2cd25SJed Brown /*@
2107deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2108deb2cd25SJed Brown 
2109deb2cd25SJed Brown    Collective
2110deb2cd25SJed Brown 
2111deb2cd25SJed Brown    Input Parameter:
2112deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2113deb2cd25SJed Brown -  snes - the nonlinear solver context
2114deb2cd25SJed Brown 
2115deb2cd25SJed Brown    Notes:
2116deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2117deb2cd25SJed Brown 
2118deb2cd25SJed Brown    Level: developer
2119deb2cd25SJed Brown 
2120deb2cd25SJed Brown .keywords: timestep, set, SNES
2121deb2cd25SJed Brown @*/
2122deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2123deb2cd25SJed Brown {
2124deb2cd25SJed Brown   PetscErrorCode ierr;
2125d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2126deb2cd25SJed Brown 
2127deb2cd25SJed Brown   PetscFunctionBegin;
2128deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2129deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2130deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2131deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2132bbd56ea5SKarl Rupp 
2133deb2cd25SJed Brown   ts->snes = snes;
2134bbd56ea5SKarl Rupp 
21350298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
21360298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2137740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
21380298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2139740132f1SEmil Constantinescu   }
2140deb2cd25SJed Brown   PetscFunctionReturn(0);
2141deb2cd25SJed Brown }
2142deb2cd25SJed Brown 
2143deb2cd25SJed Brown #undef __FUNCT__
214494b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
2145d8e5e3e6SSatish Balay /*@
214694b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2147d763cef2SBarry Smith    a TS (timestepper) context.
2148d763cef2SBarry Smith 
214994b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2150d763cef2SBarry Smith 
2151d763cef2SBarry Smith    Input Parameter:
2152d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2153d763cef2SBarry Smith 
2154d763cef2SBarry Smith    Output Parameter:
215594b7f48cSBarry Smith .  ksp - the nonlinear solver context
2156d763cef2SBarry Smith 
2157d763cef2SBarry Smith    Notes:
215894b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2159d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2160d763cef2SBarry Smith    KSP and PC contexts as well.
2161d763cef2SBarry Smith 
216294b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
21630298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2164d763cef2SBarry Smith 
2165d763cef2SBarry Smith    Level: beginner
2166d763cef2SBarry Smith 
216794b7f48cSBarry Smith .keywords: timestep, get, KSP
2168d763cef2SBarry Smith @*/
21697087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2170d763cef2SBarry Smith {
2171d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2172089b2837SJed Brown   SNES           snes;
2173d372ba47SLisandro Dalcin 
2174d763cef2SBarry Smith   PetscFunctionBegin;
21750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21764482741eSBarry Smith   PetscValidPointer(ksp,2);
217717186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2178e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2179089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2180089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2181d763cef2SBarry Smith   PetscFunctionReturn(0);
2182d763cef2SBarry Smith }
2183d763cef2SBarry Smith 
2184d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2185d763cef2SBarry Smith 
21864a2ae208SSatish Balay #undef __FUNCT__
2187adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
2188adb62b0dSMatthew Knepley /*@
2189adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2190adb62b0dSMatthew Knepley    maximum time for iteration.
2191adb62b0dSMatthew Knepley 
21923f9fe445SBarry Smith    Not Collective
2193adb62b0dSMatthew Knepley 
2194adb62b0dSMatthew Knepley    Input Parameters:
2195adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
21960298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
21970298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2198adb62b0dSMatthew Knepley 
2199adb62b0dSMatthew Knepley    Level: intermediate
2200adb62b0dSMatthew Knepley 
2201adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2202adb62b0dSMatthew Knepley @*/
22037087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2204adb62b0dSMatthew Knepley {
2205adb62b0dSMatthew Knepley   PetscFunctionBegin;
22060700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2207abc0a331SBarry Smith   if (maxsteps) {
22084482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2209adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2210adb62b0dSMatthew Knepley   }
2211abc0a331SBarry Smith   if (maxtime) {
22124482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2213adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2214adb62b0dSMatthew Knepley   }
2215adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2216adb62b0dSMatthew Knepley }
2217adb62b0dSMatthew Knepley 
2218adb62b0dSMatthew Knepley #undef __FUNCT__
22194a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
2220d763cef2SBarry Smith /*@
2221d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2222d763cef2SBarry Smith    maximum time for iteration.
2223d763cef2SBarry Smith 
22243f9fe445SBarry Smith    Logically Collective on TS
2225d763cef2SBarry Smith 
2226d763cef2SBarry Smith    Input Parameters:
2227d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2228d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2229d763cef2SBarry Smith -  maxtime - final time to iterate to
2230d763cef2SBarry Smith 
2231d763cef2SBarry Smith    Options Database Keys:
2232d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
22333bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2234d763cef2SBarry Smith 
2235d763cef2SBarry Smith    Notes:
2236d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2237d763cef2SBarry Smith 
2238d763cef2SBarry Smith    Level: intermediate
2239d763cef2SBarry Smith 
2240d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2241a43b19c4SJed Brown 
2242a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2243d763cef2SBarry Smith @*/
22447087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2245d763cef2SBarry Smith {
2246d763cef2SBarry Smith   PetscFunctionBegin;
22470700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2248c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2249c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
225039b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
225139b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2252d763cef2SBarry Smith   PetscFunctionReturn(0);
2253d763cef2SBarry Smith }
2254d763cef2SBarry Smith 
22554a2ae208SSatish Balay #undef __FUNCT__
22564a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2257d763cef2SBarry Smith /*@
2258d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2259d763cef2SBarry Smith    for use by the TS routines.
2260d763cef2SBarry Smith 
22613f9fe445SBarry Smith    Logically Collective on TS and Vec
2262d763cef2SBarry Smith 
2263d763cef2SBarry Smith    Input Parameters:
2264d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
22650910c330SBarry Smith -  u - the solution vector
2266d763cef2SBarry Smith 
2267d763cef2SBarry Smith    Level: beginner
2268d763cef2SBarry Smith 
2269d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2270d763cef2SBarry Smith @*/
22710910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2272d763cef2SBarry Smith {
22738737fe31SLisandro Dalcin   PetscErrorCode ierr;
2274496e6a7aSJed Brown   DM             dm;
22758737fe31SLisandro Dalcin 
2276d763cef2SBarry Smith   PetscFunctionBegin;
22770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22780910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
22790910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
22806bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2281bbd56ea5SKarl Rupp 
22820910c330SBarry Smith   ts->vec_sol = u;
2283bbd56ea5SKarl Rupp 
2284496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
22850910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2286d763cef2SBarry Smith   PetscFunctionReturn(0);
2287d763cef2SBarry Smith }
2288d763cef2SBarry Smith 
2289e74ef692SMatthew Knepley #undef __FUNCT__
229073b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps"
229173b18844SBarry Smith /*@
229273b18844SBarry Smith    TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time
229373b18844SBarry Smith 
229473b18844SBarry Smith    Logically Collective on TS
229573b18844SBarry Smith 
229673b18844SBarry Smith    Input Parameters:
229773b18844SBarry Smith +  ts - the TS context obtained from TSCreate()
229873b18844SBarry Smith .  steps - number of steps to use
229973b18844SBarry Smith 
230073b18844SBarry Smith    Level: intermediate
230173b18844SBarry Smith 
230273b18844SBarry Smith    Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this
230373b18844SBarry Smith           so as to integrate back to less than the original timestep
230473b18844SBarry Smith 
230573b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations
230673b18844SBarry Smith 
230773b18844SBarry Smith .seealso: TSSetExactFinalTime()
230873b18844SBarry Smith @*/
230973b18844SBarry Smith PetscErrorCode  TSAdjointSetSteps(TS ts,PetscInt steps)
231073b18844SBarry Smith {
231173b18844SBarry Smith   PetscFunctionBegin;
231273b18844SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
231373b18844SBarry Smith   PetscValidLogicalCollectiveInt(ts,steps,2);
231473b18844SBarry Smith   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps");
231573b18844SBarry 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");
231673b18844SBarry Smith   ts->adjoint_max_steps = steps;
231773b18844SBarry Smith   PetscFunctionReturn(0);
231873b18844SBarry Smith }
231973b18844SBarry Smith 
232073b18844SBarry Smith #undef __FUNCT__
23210cf82b59SBarry Smith #define __FUNCT__ "TSAdjointSetCostGradients"
2322c235aa8dSHong Zhang /*@
23230cf82b59SBarry Smith    TSAdjointSetCostGradients - Sets the initial value of the gradients of the cost function w.r.t. initial conditions and w.r.t. the problem parameters
23240cf82b59SBarry Smith       for use by the TSAdjoint routines.
2325c235aa8dSHong Zhang 
2326c235aa8dSHong Zhang    Logically Collective on TS and Vec
2327c235aa8dSHong Zhang 
2328c235aa8dSHong Zhang    Input Parameters:
2329c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
2330abc2977eSBarry 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
2331abc2977eSBarry Smith -  mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters
2332c235aa8dSHong Zhang 
2333c235aa8dSHong Zhang    Level: beginner
2334c235aa8dSHong Zhang 
2335abc2977eSBarry Smith    Notes: the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime  mu_i = df/dp|finaltime
23360cf82b59SBarry Smith 
2337c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2338c235aa8dSHong Zhang @*/
2339abc2977eSBarry Smith PetscErrorCode  TSAdjointSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu)
2340c235aa8dSHong Zhang {
2341c235aa8dSHong Zhang   PetscFunctionBegin;
2342c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2343abc2977eSBarry Smith   PetscValidPointer(lambda,2);
2344abc2977eSBarry Smith   ts->vecs_sensi  = lambda;
2345abc2977eSBarry Smith   ts->vecs_sensip = mu;
2346abc2977eSBarry Smith   ts->numcost  = numcost;
2347ad8e2604SHong Zhang   PetscFunctionReturn(0);
2348ad8e2604SHong Zhang }
2349ad8e2604SHong Zhang 
2350ad8e2604SHong Zhang #undef __FUNCT__
23515bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian"
2352ad8e2604SHong Zhang /*@C
23530cf82b59SBarry 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.
2354ad8e2604SHong Zhang 
2355ad8e2604SHong Zhang   Logically Collective on TS
2356ad8e2604SHong Zhang 
2357ad8e2604SHong Zhang   Input Parameters:
2358ad8e2604SHong Zhang + ts   - The TS context obtained from TSCreate()
2359ad8e2604SHong Zhang - func - The function
2360ad8e2604SHong Zhang 
2361ad8e2604SHong Zhang   Calling sequence of func:
23620cf82b59SBarry Smith $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx);
236305755b9cSHong Zhang +   t - current timestep
23640cf82b59SBarry Smith .   y - input vector (current ODE solution)
236505755b9cSHong Zhang .   A - output matrix
236605755b9cSHong Zhang -   ctx - [optional] user-defined function context
2367ad8e2604SHong Zhang 
2368ad8e2604SHong Zhang   Level: intermediate
2369ad8e2604SHong Zhang 
23700cf82b59SBarry 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
23710cf82b59SBarry Smith 
2372ad8e2604SHong Zhang .keywords: TS, sensitivity
2373ad8e2604SHong Zhang .seealso:
2374ad8e2604SHong Zhang @*/
23755bf8c567SBarry Smith PetscErrorCode  TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
2376ad8e2604SHong Zhang {
2377ad8e2604SHong Zhang   PetscErrorCode ierr;
2378ad8e2604SHong Zhang 
2379ad8e2604SHong Zhang   PetscFunctionBegin;
2380ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2381ad8e2604SHong Zhang   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
2382ad8e2604SHong Zhang 
2383ad8e2604SHong Zhang   ts->rhsjacobianp    = func;
2384ad8e2604SHong Zhang   ts->rhsjacobianpctx = ctx;
2385ad8e2604SHong Zhang   if(Amat) {
2386ad8e2604SHong Zhang     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
2387ad8e2604SHong Zhang     ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2388ad8e2604SHong Zhang     ts->Jacp = Amat;
2389ad8e2604SHong Zhang   }
2390ad8e2604SHong Zhang   PetscFunctionReturn(0);
2391ad8e2604SHong Zhang }
2392ad8e2604SHong Zhang 
2393ad8e2604SHong Zhang #undef __FUNCT__
23945bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian"
23950cf82b59SBarry Smith /*@C
23965bf8c567SBarry Smith   TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function.
2397ad8e2604SHong Zhang 
2398ad8e2604SHong Zhang   Collective on TS
2399ad8e2604SHong Zhang 
2400ad8e2604SHong Zhang   Input Parameters:
2401ad8e2604SHong Zhang . ts   - The TS context obtained from TSCreate()
2402ad8e2604SHong Zhang 
2403ad8e2604SHong Zhang   Level: developer
2404ad8e2604SHong Zhang 
2405ad8e2604SHong Zhang .keywords: TS, sensitivity
24065bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian()
2407ad8e2604SHong Zhang @*/
24085bf8c567SBarry Smith PetscErrorCode  TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat)
2409ad8e2604SHong Zhang {
2410ad8e2604SHong Zhang   PetscErrorCode ierr;
2411ad8e2604SHong Zhang 
2412ad8e2604SHong Zhang   PetscFunctionBegin;
2413ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2414ad8e2604SHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
2415ad8e2604SHong Zhang   PetscValidPointer(Amat,4);
2416ad8e2604SHong Zhang 
2417ad8e2604SHong Zhang   PetscStackPush("TS user JacobianP function for sensitivity analysis");
2418ad8e2604SHong Zhang   ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr);
2419ad8e2604SHong Zhang   PetscStackPop;
2420c235aa8dSHong Zhang   PetscFunctionReturn(0);
2421c235aa8dSHong Zhang }
2422c235aa8dSHong Zhang 
2423c235aa8dSHong Zhang #undef __FUNCT__
2424d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetCostIntegrand"
24256fd0887fSHong Zhang /*@C
24260cf82b59SBarry Smith     TSAdjointSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions
24276fd0887fSHong Zhang 
24286fd0887fSHong Zhang     Logically Collective on TS
24296fd0887fSHong Zhang 
24306fd0887fSHong Zhang     Input Parameters:
24316fd0887fSHong Zhang +   ts - the TS context obtained from TSCreate()
2432abc2977eSBarry Smith .   numcost - number of gradients to be computed, this is the number of cost functions
24330cf82b59SBarry Smith .   rf - routine for evaluating the integrand function
2434b612ec54SBarry Smith .   drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y
2435b612ec54SBarry Smith .   drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p
2436b612ec54SBarry Smith -   ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL)
24376fd0887fSHong Zhang 
24380cf82b59SBarry Smith     Calling sequence of rf:
24390cf82b59SBarry Smith $     rf(TS ts,PetscReal t,Vec y,Vec f[],void *ctx);
24406fd0887fSHong Zhang 
24416fd0887fSHong Zhang +   t - current timestep
24420cf82b59SBarry Smith .   y - input vector
24430cf82b59SBarry Smith .   f - function result; one vector entry for each cost function
24446fd0887fSHong Zhang -   ctx - [optional] user-defined function context
24456fd0887fSHong Zhang 
2446b612ec54SBarry Smith    Calling sequence of drdyf:
24470cf82b59SBarry Smith $    PetscErroCode drdyf(TS ts,PetscReal t,Vec y,Vec *drdy,void *ctx);
2448b612ec54SBarry Smith 
2449b612ec54SBarry Smith    Calling sequence of drdpf:
24500cf82b59SBarry Smith $    PetscErroCode drdpf(TS ts,PetscReal t,Vec y,Vec *drdp,void *ctx);
2451b612ec54SBarry Smith 
2452b612ec54SBarry Smith     Level: intermediate
24536fd0887fSHong Zhang 
2454abc2977eSBarry Smith     Notes: For optimization there is generally a single cost function, numcost = 1. For sensitivities there may be multiple cost functions
24550cf82b59SBarry Smith 
24566fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function
24576fd0887fSHong Zhang 
24580cf82b59SBarry Smith .seealso: TSAdjointSetRHSJacobian(),TSAdjointGetCostGradients(), TSAdjointSetCostGradients()
24596fd0887fSHong Zhang @*/
2460abc2977eSBarry Smith PetscErrorCode  TSAdjointSetCostIntegrand(TS ts,PetscInt numcost, PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*),
2461d8151eeaSBarry Smith                                                                   PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*),
2462d8151eeaSBarry Smith                                                                   PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),void *ctx)
24636fd0887fSHong Zhang {
24646fd0887fSHong Zhang   PetscErrorCode ierr;
24656fd0887fSHong Zhang 
24666fd0887fSHong Zhang   PetscFunctionBegin;
24676fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2468abc2977eSBarry Smith   if (!ts->numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Call TSAdjointSetCostGradients() first so that the number of cost functions can be determined.");
2469abc2977eSBarry Smith   if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSAdjointSetCostIntegrand()) is inconsistent with the one set by TSAdjointSetCostGradients()");
24706fd0887fSHong Zhang 
2471abc2977eSBarry Smith   ierr                  = VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);CHKERRQ(ierr);
24722c39e106SBarry Smith   ierr                  = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr);
24730cf82b59SBarry Smith   ts->costintegrand     = rf;
247405755b9cSHong Zhang   ts->costintegrandctx  = ctx;
2475b612ec54SBarry Smith   ts->drdyfunction    = drdyf;
2476b612ec54SBarry Smith   ts->drdpfunction    = drdpf;
24776fd0887fSHong Zhang   PetscFunctionReturn(0);
24786fd0887fSHong Zhang }
24796fd0887fSHong Zhang 
24806fd0887fSHong Zhang #undef __FUNCT__
24812c39e106SBarry Smith #define __FUNCT__ "TSAdjointGetCostIntegral"
248236eaed60SHong Zhang /*@
24832c39e106SBarry Smith    TSAdjointGetCostIntegral - Returns the values of the integral term in the cost functions.
248436eaed60SHong Zhang    It is valid to call the routine after a backward run.
248536eaed60SHong Zhang 
248636eaed60SHong Zhang    Not Collective
248736eaed60SHong Zhang 
248836eaed60SHong Zhang    Input Parameter:
248936eaed60SHong Zhang .  ts - the TS context obtained from TSCreate()
249036eaed60SHong Zhang 
249136eaed60SHong Zhang    Output Parameter:
24922c39e106SBarry Smith .  v - the vector containing the integrals for each cost function
249336eaed60SHong Zhang 
249436eaed60SHong Zhang    Level: intermediate
249536eaed60SHong Zhang 
2496d4aa0a58SBarry Smith .seealso: TSAdjointSetCostIntegrand()
249736eaed60SHong Zhang 
249836eaed60SHong Zhang .keywords: TS, sensitivity analysis
249936eaed60SHong Zhang @*/
25002c39e106SBarry Smith PetscErrorCode  TSAdjointGetCostIntegral(TS ts,Vec *v)
250136eaed60SHong Zhang {
250236eaed60SHong Zhang   PetscFunctionBegin;
250336eaed60SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
250436eaed60SHong Zhang   PetscValidPointer(v,2);
25052c39e106SBarry Smith   *v = ts->vec_costintegral;
250636eaed60SHong Zhang   PetscFunctionReturn(0);
250736eaed60SHong Zhang }
250836eaed60SHong Zhang 
250936eaed60SHong Zhang #undef __FUNCT__
2510d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand"
25116fd0887fSHong Zhang /*@
25122c39e106SBarry Smith    TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions.
25136fd0887fSHong Zhang 
25146fd0887fSHong Zhang    Input Parameters:
25156fd0887fSHong Zhang +  ts - the TS context
25166fd0887fSHong Zhang .  t - current time
25170cf82b59SBarry Smith -  y - state vector, i.e. current solution
25186fd0887fSHong Zhang 
25196fd0887fSHong Zhang    Output Parameter:
2520abc2977eSBarry Smith .  q - vector of size numcost to hold the outputs
25216fd0887fSHong Zhang 
25226fd0887fSHong Zhang    Note:
25236fd0887fSHong Zhang    Most users should not need to explicitly call this routine, as it
25246fd0887fSHong Zhang    is used internally within the sensitivity analysis context.
25256fd0887fSHong Zhang 
25266fd0887fSHong Zhang    Level: developer
25276fd0887fSHong Zhang 
25286fd0887fSHong Zhang .keywords: TS, compute
25296fd0887fSHong Zhang 
2530d4aa0a58SBarry Smith .seealso: TSAdjointSetCostIntegrand()
25316fd0887fSHong Zhang @*/
25320cf82b59SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec y,Vec q)
25336fd0887fSHong Zhang {
25346fd0887fSHong Zhang   PetscErrorCode ierr;
25356fd0887fSHong Zhang 
25366fd0887fSHong Zhang   PetscFunctionBegin;
25376fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25380cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
25396fd0887fSHong Zhang   PetscValidHeaderSpecific(q,VEC_CLASSID,4);
25406fd0887fSHong Zhang 
25410cf82b59SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
2542e4680132SHong Zhang   if (ts->costintegrand) {
2543e4680132SHong Zhang     PetscStackPush("TS user integrand in the cost function");
25440cf82b59SBarry Smith     ierr = (*ts->costintegrand)(ts,t,y,q,ts->costintegrandctx);CHKERRQ(ierr);
25456fd0887fSHong Zhang     PetscStackPop;
25466fd0887fSHong Zhang   } else {
25476fd0887fSHong Zhang     ierr = VecZeroEntries(q);CHKERRQ(ierr);
25486fd0887fSHong Zhang   }
25496fd0887fSHong Zhang 
25500cf82b59SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,y,q,0);CHKERRQ(ierr);
25516fd0887fSHong Zhang   PetscFunctionReturn(0);
25526fd0887fSHong Zhang }
25536fd0887fSHong Zhang 
25546fd0887fSHong Zhang #undef __FUNCT__
2555d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction"
255605755b9cSHong Zhang /*@
2557d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.
255805755b9cSHong Zhang 
255905755b9cSHong Zhang   Collective on TS
256005755b9cSHong Zhang 
256105755b9cSHong Zhang   Input Parameters:
256205755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
256305755b9cSHong Zhang 
256405755b9cSHong Zhang   Notes:
2565d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
256605755b9cSHong Zhang   so most users would not generally call this routine themselves.
256705755b9cSHong Zhang 
256805755b9cSHong Zhang   Level: developer
256905755b9cSHong Zhang 
257005755b9cSHong Zhang .keywords: TS, sensitivity
2571d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction()
257205755b9cSHong Zhang @*/
25730cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec y,Vec *drdy)
257405755b9cSHong Zhang {
257505755b9cSHong Zhang   PetscErrorCode ierr;
257605755b9cSHong Zhang 
257705755b9cSHong Zhang   PetscFunctionBegin;
257805755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25790cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
258005755b9cSHong Zhang 
258105755b9cSHong Zhang   PetscStackPush("TS user DRDY function for sensitivity analysis");
25820cf82b59SBarry Smith   ierr = (*ts->drdyfunction)(ts,t,y,drdy,ts->costintegrandctx); CHKERRQ(ierr);
258305755b9cSHong Zhang   PetscStackPop;
258405755b9cSHong Zhang   PetscFunctionReturn(0);
258505755b9cSHong Zhang }
258605755b9cSHong Zhang 
258705755b9cSHong Zhang #undef __FUNCT__
2588d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction"
258905755b9cSHong Zhang /*@
2590d4aa0a58SBarry Smith   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function.
259105755b9cSHong Zhang 
259205755b9cSHong Zhang   Collective on TS
259305755b9cSHong Zhang 
259405755b9cSHong Zhang   Input Parameters:
259505755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
259605755b9cSHong Zhang 
259705755b9cSHong Zhang   Notes:
259805755b9cSHong Zhang   TSDRDPFunction() is typically used for sensitivity implementation,
259905755b9cSHong Zhang   so most users would not generally call this routine themselves.
260005755b9cSHong Zhang 
260105755b9cSHong Zhang   Level: developer
260205755b9cSHong Zhang 
260305755b9cSHong Zhang .keywords: TS, sensitivity
2604d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction()
260505755b9cSHong Zhang @*/
26060cf82b59SBarry Smith PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec y,Vec *drdp)
260705755b9cSHong Zhang {
260805755b9cSHong Zhang   PetscErrorCode ierr;
260905755b9cSHong Zhang 
261005755b9cSHong Zhang   PetscFunctionBegin;
261105755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
26120cf82b59SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,3);
261305755b9cSHong Zhang 
261405755b9cSHong Zhang   PetscStackPush("TS user DRDP function for sensitivity analysis");
26150cf82b59SBarry Smith   ierr = (*ts->drdpfunction)(ts,t,y,drdp,ts->costintegrandctx); CHKERRQ(ierr);
261605755b9cSHong Zhang   PetscStackPop;
261705755b9cSHong Zhang   PetscFunctionReturn(0);
261805755b9cSHong Zhang }
261905755b9cSHong Zhang 
262005755b9cSHong Zhang #undef __FUNCT__
2621e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2622ac226902SBarry Smith /*@C
2623000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
26243f2090d5SJed Brown   called once at the beginning of each time step.
2625000e7ae3SMatthew Knepley 
26263f9fe445SBarry Smith   Logically Collective on TS
2627000e7ae3SMatthew Knepley 
2628000e7ae3SMatthew Knepley   Input Parameters:
2629000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2630000e7ae3SMatthew Knepley - func - The function
2631000e7ae3SMatthew Knepley 
2632000e7ae3SMatthew Knepley   Calling sequence of func:
2633000e7ae3SMatthew Knepley . func (TS ts);
2634000e7ae3SMatthew Knepley 
2635000e7ae3SMatthew Knepley   Level: intermediate
2636000e7ae3SMatthew Knepley 
2637b8123daeSJed Brown   Note:
2638b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2639b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2640b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2641b8123daeSJed Brown 
2642000e7ae3SMatthew Knepley .keywords: TS, timestep
26439be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
2644000e7ae3SMatthew Knepley @*/
26457087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2646000e7ae3SMatthew Knepley {
2647000e7ae3SMatthew Knepley   PetscFunctionBegin;
26480700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2649ae60f76fSBarry Smith   ts->prestep = func;
2650000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2651000e7ae3SMatthew Knepley }
2652000e7ae3SMatthew Knepley 
2653e74ef692SMatthew Knepley #undef __FUNCT__
26543f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
265509ee8438SJed Brown /*@
26563f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
26573f2090d5SJed Brown 
26583f2090d5SJed Brown   Collective on TS
26593f2090d5SJed Brown 
26603f2090d5SJed Brown   Input Parameters:
26613f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
26623f2090d5SJed Brown 
26633f2090d5SJed Brown   Notes:
26643f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
26653f2090d5SJed Brown   so most users would not generally call this routine themselves.
26663f2090d5SJed Brown 
26673f2090d5SJed Brown   Level: developer
26683f2090d5SJed Brown 
26693f2090d5SJed Brown .keywords: TS, timestep
26709be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
26713f2090d5SJed Brown @*/
26727087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
26733f2090d5SJed Brown {
26743f2090d5SJed Brown   PetscErrorCode ierr;
26753f2090d5SJed Brown 
26763f2090d5SJed Brown   PetscFunctionBegin;
26770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2678ae60f76fSBarry Smith   if (ts->prestep) {
2679ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
2680312ce896SJed Brown   }
26813f2090d5SJed Brown   PetscFunctionReturn(0);
26823f2090d5SJed Brown }
26833f2090d5SJed Brown 
26843f2090d5SJed Brown #undef __FUNCT__
2685b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2686b8123daeSJed Brown /*@C
2687b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2688b8123daeSJed Brown   called once at the beginning of each stage.
2689b8123daeSJed Brown 
2690b8123daeSJed Brown   Logically Collective on TS
2691b8123daeSJed Brown 
2692b8123daeSJed Brown   Input Parameters:
2693b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2694b8123daeSJed Brown - func - The function
2695b8123daeSJed Brown 
2696b8123daeSJed Brown   Calling sequence of func:
2697b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2698b8123daeSJed Brown 
2699b8123daeSJed Brown   Level: intermediate
2700b8123daeSJed Brown 
2701b8123daeSJed Brown   Note:
2702b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2703b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2704b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2705b8123daeSJed Brown 
2706b8123daeSJed Brown .keywords: TS, timestep
27079be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2708b8123daeSJed Brown @*/
2709b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2710b8123daeSJed Brown {
2711b8123daeSJed Brown   PetscFunctionBegin;
2712b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2713ae60f76fSBarry Smith   ts->prestage = func;
2714b8123daeSJed Brown   PetscFunctionReturn(0);
2715b8123daeSJed Brown }
2716b8123daeSJed Brown 
2717b8123daeSJed Brown #undef __FUNCT__
27189be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage"
27199be3e283SDebojyoti Ghosh /*@C
27209be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
27219be3e283SDebojyoti Ghosh   called once at the end of each stage.
27229be3e283SDebojyoti Ghosh 
27239be3e283SDebojyoti Ghosh   Logically Collective on TS
27249be3e283SDebojyoti Ghosh 
27259be3e283SDebojyoti Ghosh   Input Parameters:
27269be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
27279be3e283SDebojyoti Ghosh - func - The function
27289be3e283SDebojyoti Ghosh 
27299be3e283SDebojyoti Ghosh   Calling sequence of func:
27309be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
27319be3e283SDebojyoti Ghosh 
27329be3e283SDebojyoti Ghosh   Level: intermediate
27339be3e283SDebojyoti Ghosh 
27349be3e283SDebojyoti Ghosh   Note:
27359be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
27369be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
27379be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
27389be3e283SDebojyoti Ghosh 
27399be3e283SDebojyoti Ghosh .keywords: TS, timestep
27409be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
27419be3e283SDebojyoti Ghosh @*/
27429be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
27439be3e283SDebojyoti Ghosh {
27449be3e283SDebojyoti Ghosh   PetscFunctionBegin;
27459be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
27469be3e283SDebojyoti Ghosh   ts->poststage = func;
27479be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
27489be3e283SDebojyoti Ghosh }
27499be3e283SDebojyoti Ghosh 
27509be3e283SDebojyoti Ghosh #undef __FUNCT__
2751b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2752b8123daeSJed Brown /*@
2753b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2754b8123daeSJed Brown 
2755b8123daeSJed Brown   Collective on TS
2756b8123daeSJed Brown 
2757b8123daeSJed Brown   Input Parameters:
2758b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
27599be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
2760b8123daeSJed Brown 
2761b8123daeSJed Brown   Notes:
2762b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2763b8123daeSJed Brown   most users would not generally call this routine themselves.
2764b8123daeSJed Brown 
2765b8123daeSJed Brown   Level: developer
2766b8123daeSJed Brown 
2767b8123daeSJed Brown .keywords: TS, timestep
27689be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
2769b8123daeSJed Brown @*/
2770b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2771b8123daeSJed Brown {
2772b8123daeSJed Brown   PetscErrorCode ierr;
2773b8123daeSJed Brown 
2774b8123daeSJed Brown   PetscFunctionBegin;
2775b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2776ae60f76fSBarry Smith   if (ts->prestage) {
2777ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
2778b8123daeSJed Brown   }
2779b8123daeSJed Brown   PetscFunctionReturn(0);
2780b8123daeSJed Brown }
2781b8123daeSJed Brown 
2782b8123daeSJed Brown #undef __FUNCT__
27839be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage"
27849be3e283SDebojyoti Ghosh /*@
27859be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
27869be3e283SDebojyoti Ghosh 
27879be3e283SDebojyoti Ghosh   Collective on TS
27889be3e283SDebojyoti Ghosh 
27899be3e283SDebojyoti Ghosh   Input Parameters:
27909be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
27919be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
27929be3e283SDebojyoti Ghosh   stageindex  - Stage number
27939be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
27949be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
27959be3e283SDebojyoti Ghosh 
27969be3e283SDebojyoti Ghosh   Notes:
27979be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
27989be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
27999be3e283SDebojyoti Ghosh 
28009be3e283SDebojyoti Ghosh   Level: developer
28019be3e283SDebojyoti Ghosh 
28029be3e283SDebojyoti Ghosh .keywords: TS, timestep
28039be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
28049be3e283SDebojyoti Ghosh @*/
28059be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
28069be3e283SDebojyoti Ghosh {
28079be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
28089be3e283SDebojyoti Ghosh 
28099be3e283SDebojyoti Ghosh   PetscFunctionBegin;
28109be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
28114beae5d8SLisandro Dalcin   if (ts->poststage) {
28129be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
28139be3e283SDebojyoti Ghosh   }
28149be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
28159be3e283SDebojyoti Ghosh }
28169be3e283SDebojyoti Ghosh 
28179be3e283SDebojyoti Ghosh #undef __FUNCT__
2818e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2819ac226902SBarry Smith /*@C
2820000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
28213f2090d5SJed Brown   called once at the end of each time step.
2822000e7ae3SMatthew Knepley 
28233f9fe445SBarry Smith   Logically Collective on TS
2824000e7ae3SMatthew Knepley 
2825000e7ae3SMatthew Knepley   Input Parameters:
2826000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2827000e7ae3SMatthew Knepley - func - The function
2828000e7ae3SMatthew Knepley 
2829000e7ae3SMatthew Knepley   Calling sequence of func:
2830b8123daeSJed Brown $ func (TS ts);
2831000e7ae3SMatthew Knepley 
2832000e7ae3SMatthew Knepley   Level: intermediate
2833000e7ae3SMatthew Knepley 
2834000e7ae3SMatthew Knepley .keywords: TS, timestep
2835b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2836000e7ae3SMatthew Knepley @*/
28377087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2838000e7ae3SMatthew Knepley {
2839000e7ae3SMatthew Knepley   PetscFunctionBegin;
28400700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2841ae60f76fSBarry Smith   ts->poststep = func;
2842000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2843000e7ae3SMatthew Knepley }
2844000e7ae3SMatthew Knepley 
2845e74ef692SMatthew Knepley #undef __FUNCT__
28463f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
284709ee8438SJed Brown /*@
28483f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
28493f2090d5SJed Brown 
28503f2090d5SJed Brown   Collective on TS
28513f2090d5SJed Brown 
28523f2090d5SJed Brown   Input Parameters:
28533f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
28543f2090d5SJed Brown 
28553f2090d5SJed Brown   Notes:
28563f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
28573f2090d5SJed Brown   so most users would not generally call this routine themselves.
28583f2090d5SJed Brown 
28593f2090d5SJed Brown   Level: developer
28603f2090d5SJed Brown 
28613f2090d5SJed Brown .keywords: TS, timestep
28623f2090d5SJed Brown @*/
28637087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
28643f2090d5SJed Brown {
28653f2090d5SJed Brown   PetscErrorCode ierr;
28663f2090d5SJed Brown 
28673f2090d5SJed Brown   PetscFunctionBegin;
28680700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2869ae60f76fSBarry Smith   if (ts->poststep) {
2870ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
287172ac3e02SJed Brown   }
28723f2090d5SJed Brown   PetscFunctionReturn(0);
28733f2090d5SJed Brown }
28743f2090d5SJed Brown 
2875d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2876d763cef2SBarry Smith 
28774a2ae208SSatish Balay #undef __FUNCT__
2878a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2879d763cef2SBarry Smith /*@C
2880a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2881d763cef2SBarry Smith    timestep to display the iteration's  progress.
2882d763cef2SBarry Smith 
28833f9fe445SBarry Smith    Logically Collective on TS
2884d763cef2SBarry Smith 
2885d763cef2SBarry Smith    Input Parameters:
2886d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2887e213d8f1SJed Brown .  monitor - monitoring routine
2888329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
28890298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
2890b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
28910298fd71SBarry Smith           (may be NULL)
2892d763cef2SBarry Smith 
2893e213d8f1SJed Brown    Calling sequence of monitor:
28940910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2895d763cef2SBarry Smith 
2896d763cef2SBarry Smith +    ts - the TS context
289788c05cc5SBarry 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
289888c05cc5SBarry Smith                                been interpolated to)
28991f06c33eSBarry Smith .    time - current time
29000910c330SBarry Smith .    u - current iterate
2901d763cef2SBarry Smith -    mctx - [optional] monitoring context
2902d763cef2SBarry Smith 
2903d763cef2SBarry Smith    Notes:
2904d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2905d763cef2SBarry Smith    already has been loaded.
2906d763cef2SBarry Smith 
2907025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2908025f1a04SBarry Smith 
2909d763cef2SBarry Smith    Level: intermediate
2910d763cef2SBarry Smith 
2911d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2912d763cef2SBarry Smith 
2913a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2914d763cef2SBarry Smith @*/
2915c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2916d763cef2SBarry Smith {
2917d763cef2SBarry Smith   PetscFunctionBegin;
29180700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
291917186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2920d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
29218704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2922d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2923d763cef2SBarry Smith   PetscFunctionReturn(0);
2924d763cef2SBarry Smith }
2925d763cef2SBarry Smith 
29264a2ae208SSatish Balay #undef __FUNCT__
2927a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2928d763cef2SBarry Smith /*@C
2929a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2930d763cef2SBarry Smith 
29313f9fe445SBarry Smith    Logically Collective on TS
2932d763cef2SBarry Smith 
2933d763cef2SBarry Smith    Input Parameters:
2934d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2935d763cef2SBarry Smith 
2936d763cef2SBarry Smith    Notes:
2937d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2938d763cef2SBarry Smith 
2939d763cef2SBarry Smith    Level: intermediate
2940d763cef2SBarry Smith 
2941d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2942d763cef2SBarry Smith 
2943a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2944d763cef2SBarry Smith @*/
29457087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2946d763cef2SBarry Smith {
2947d952e501SBarry Smith   PetscErrorCode ierr;
2948d952e501SBarry Smith   PetscInt       i;
2949d952e501SBarry Smith 
2950d763cef2SBarry Smith   PetscFunctionBegin;
29510700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2952d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
29538704b422SBarry Smith     if (ts->monitordestroy[i]) {
29548704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2955d952e501SBarry Smith     }
2956d952e501SBarry Smith   }
2957d763cef2SBarry Smith   ts->numbermonitors = 0;
2958d763cef2SBarry Smith   PetscFunctionReturn(0);
2959d763cef2SBarry Smith }
2960d763cef2SBarry Smith 
29614a2ae208SSatish Balay #undef __FUNCT__
2962a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2963d8e5e3e6SSatish Balay /*@
2964a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
29655516499fSSatish Balay 
29665516499fSSatish Balay    Level: intermediate
296741251cbbSSatish Balay 
29685516499fSSatish Balay .keywords: TS, set, monitor
29695516499fSSatish Balay 
297041251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
297141251cbbSSatish Balay @*/
2972649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2973d763cef2SBarry Smith {
2974dfbe8321SBarry Smith   PetscErrorCode ierr;
2975ce94432eSBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts));
2976d132466eSBarry Smith 
2977d763cef2SBarry Smith   PetscFunctionBegin;
2978649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
29798392e04aSShri 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);
2980649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2981d763cef2SBarry Smith   PetscFunctionReturn(0);
2982d763cef2SBarry Smith }
2983d763cef2SBarry Smith 
29844a2ae208SSatish Balay #undef __FUNCT__
2985cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2986cd652676SJed Brown /*@
2987cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2988cd652676SJed Brown 
2989cd652676SJed Brown    Logically Collective on TS
2990cd652676SJed Brown 
2991cd652676SJed Brown    Input Argument:
2992cd652676SJed Brown .  ts - time stepping context
2993cd652676SJed Brown 
2994cd652676SJed Brown    Output Argument:
2995cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2996cd652676SJed Brown 
2997cd652676SJed Brown    Level: intermediate
2998cd652676SJed Brown 
2999cd652676SJed Brown .keywords: TS, set
3000cd652676SJed Brown 
3001cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
3002cd652676SJed Brown @*/
3003cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
3004cd652676SJed Brown {
3005cd652676SJed Brown   PetscFunctionBegin;
3006cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3007cd652676SJed Brown   ts->retain_stages = flg;
3008cd652676SJed Brown   PetscFunctionReturn(0);
3009cd652676SJed Brown }
3010cd652676SJed Brown 
3011cd652676SJed Brown #undef __FUNCT__
3012cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
3013cd652676SJed Brown /*@
3014cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
3015cd652676SJed Brown 
3016cd652676SJed Brown    Collective on TS
3017cd652676SJed Brown 
3018cd652676SJed Brown    Input Argument:
3019cd652676SJed Brown +  ts - time stepping context
3020cd652676SJed Brown -  t - time to interpolate to
3021cd652676SJed Brown 
3022cd652676SJed Brown    Output Argument:
30230910c330SBarry Smith .  U - state at given time
3024cd652676SJed Brown 
3025cd652676SJed Brown    Notes:
3026cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
3027cd652676SJed Brown 
3028cd652676SJed Brown    Level: intermediate
3029cd652676SJed Brown 
3030cd652676SJed Brown    Developer Notes:
3031cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3032cd652676SJed Brown 
3033cd652676SJed Brown .keywords: TS, set
3034cd652676SJed Brown 
3035cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
3036cd652676SJed Brown @*/
30370910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3038cd652676SJed Brown {
3039cd652676SJed Brown   PetscErrorCode ierr;
3040cd652676SJed Brown 
3041cd652676SJed Brown   PetscFunctionBegin;
3042cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3043b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
30446712e2f1SBarry 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);
3045ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
30460910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3047cd652676SJed Brown   PetscFunctionReturn(0);
3048cd652676SJed Brown }
3049cd652676SJed Brown 
3050cd652676SJed Brown #undef __FUNCT__
30514a2ae208SSatish Balay #define __FUNCT__ "TSStep"
3052d763cef2SBarry Smith /*@
30536d9e5789SSean Farley    TSStep - Steps one time step
3054d763cef2SBarry Smith 
3055d763cef2SBarry Smith    Collective on TS
3056d763cef2SBarry Smith 
3057d763cef2SBarry Smith    Input Parameter:
3058d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3059d763cef2SBarry Smith 
306027829d71SBarry Smith    Level: developer
3061d763cef2SBarry Smith 
3062b8123daeSJed Brown    Notes:
306327829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
306427829d71SBarry Smith 
3065b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3066b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3067b8123daeSJed Brown 
306825cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
306925cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
307025cb2221SBarry Smith 
3071d763cef2SBarry Smith .keywords: TS, timestep, solve
3072d763cef2SBarry Smith 
30739be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3074d763cef2SBarry Smith @*/
3075193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3076d763cef2SBarry Smith {
30772fb8446cSMatthew G. Knepley   DM               dm;
3078dfbe8321SBarry Smith   PetscErrorCode   ierr;
3079fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3080d763cef2SBarry Smith 
3081d763cef2SBarry Smith   PetscFunctionBegin;
30820700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3083fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3084fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3085fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3086fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3087fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3088fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3089fffbeea8SBarry Smith                                 "  year        = {2014}\n}\n",&cite);
3090fffbeea8SBarry Smith 
30912fb8446cSMatthew G. Knepley   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
3092d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
3093d405a339SMatthew Knepley 
3094362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
309524655328SShri   ts->ptime_prev = ts->ptime;
3096cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3097bbd56ea5SKarl Rupp 
3098e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3099d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3100193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3101d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3102bbd56ea5SKarl Rupp 
310324655328SShri   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3104cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3105362cd11cSLisandro Dalcin 
3106362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3107cef5090cSJed Brown     if (ts->errorifstepfailed) {
310808c7845fSBarry 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]);
310908c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3110d2daff3dSHong Zhang     }
311108c7845fSBarry Smith   } else if (!ts->reason) {
311208c7845fSBarry Smith     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
311308c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
311408c7845fSBarry Smith   }
31152c18e0fdSBarry Smith   ts->total_steps++;
31168392e04aSShri Abhyankar   ts->steprollback = PETSC_FALSE;
311708c7845fSBarry Smith   PetscFunctionReturn(0);
311808c7845fSBarry Smith }
311908c7845fSBarry Smith 
312008c7845fSBarry Smith #undef __FUNCT__
312108c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep"
312208c7845fSBarry Smith /*@
312308c7845fSBarry Smith    TSAdjointStep - Steps one time step
312408c7845fSBarry Smith 
312508c7845fSBarry Smith    Collective on TS
312608c7845fSBarry Smith 
312708c7845fSBarry Smith    Input Parameter:
312808c7845fSBarry Smith .  ts - the TS context obtained from TSCreate()
312908c7845fSBarry Smith 
31306a4d4014SLisandro Dalcin    Level: intermediate
31316a4d4014SLisandro Dalcin 
31327087cfbeSBarry Smith    Notes:
31336a4d4014SLisandro Dalcin    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
31346a4d4014SLisandro Dalcin    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
31356a4d4014SLisandro Dalcin 
31366a4d4014SLisandro Dalcin    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
3137f22f69f0SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
31386a4d4014SLisandro Dalcin 
31390700a824SBarry Smith .keywords: TS, timestep, solve
31406a4d4014SLisandro Dalcin 
314108c7845fSBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
31426a4d4014SLisandro Dalcin @*/
314308c7845fSBarry Smith PetscErrorCode  TSAdjointStep(TS ts)
31446a4d4014SLisandro Dalcin {
314508c7845fSBarry Smith   DM               dm;
31466a4d4014SLisandro Dalcin   PetscErrorCode   ierr;
31476a4d4014SLisandro Dalcin 
31486a4d4014SLisandro Dalcin   PetscFunctionBegin;
31494a2ae208SSatish Balay   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
315008c7845fSBarry Smith   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
315108c7845fSBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
3152d763cef2SBarry Smith 
3153d763cef2SBarry Smith   ts->reason = TS_CONVERGED_ITERATING;
315408c7845fSBarry Smith   ts->ptime_prev = ts->ptime;
315508c7845fSBarry Smith   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
315608c7845fSBarry Smith   ierr = VecViewFromOptions(ts->vec_sol, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr);
3157d763cef2SBarry Smith 
31586849ba73SBarry Smith   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
315942f2b339SBarry 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);
316042f2b339SBarry Smith   ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr);
3161a6570f20SBarry Smith   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3162d763cef2SBarry Smith 
3163cef5090cSJed Brown   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3164cef5090cSJed Brown   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3165362cd11cSLisandro Dalcin 
3166362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3167cef5090cSJed Brown     if (ts->errorifstepfailed) {
3168cef5090cSJed Brown       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
3169ce94432eSBarry 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]);
31702a3a10ceSLisandro Dalcin       } else if (ts->reason == TS_DIVERGED_STEP_REJECTED) {
31712a3a10ceSLisandro 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]);
3172ce94432eSBarry Smith       } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3173cef5090cSJed Brown     }
3174362cd11cSLisandro Dalcin   } else if (!ts->reason) {
317573b18844SBarry Smith     if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
3176db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time)         ts->reason = TS_CONVERGED_TIME;
3177362cd11cSLisandro Dalcin   }
31782c18e0fdSBarry Smith   ts->total_steps--;
3179d763cef2SBarry Smith   PetscFunctionReturn(0);
3180d763cef2SBarry Smith }
3181d763cef2SBarry Smith 
3182d763cef2SBarry Smith #undef __FUNCT__
318305175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
318405175c85SJed Brown /*@
318505175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
318605175c85SJed Brown 
31871c3436cfSJed Brown    Collective on TS
318805175c85SJed Brown 
318905175c85SJed Brown    Input Arguments:
31901c3436cfSJed Brown +  ts - time stepping context
31911c3436cfSJed Brown .  order - desired order of accuracy
31920298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
319305175c85SJed Brown 
319405175c85SJed Brown    Output Arguments:
31950910c330SBarry Smith .  U - state at the end of the current step
319605175c85SJed Brown 
319705175c85SJed Brown    Level: advanced
319805175c85SJed Brown 
3199108c343cSJed Brown    Notes:
3200108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3201108c343cSJed 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.
3202108c343cSJed Brown 
32031c3436cfSJed Brown .seealso: TSStep(), TSAdapt
320405175c85SJed Brown @*/
32050910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
320605175c85SJed Brown {
320705175c85SJed Brown   PetscErrorCode ierr;
320805175c85SJed Brown 
320905175c85SJed Brown   PetscFunctionBegin;
321005175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
321105175c85SJed Brown   PetscValidType(ts,1);
32120910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3213ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
32140910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
321505175c85SJed Brown   PetscFunctionReturn(0);
321605175c85SJed Brown }
321705175c85SJed Brown 
3218d67e68b7SBarry Smith 
321905175c85SJed Brown #undef __FUNCT__
3220d763cef2SBarry Smith #define __FUNCT__ "TSSolve"
3221d763cef2SBarry Smith /*@
3222d763cef2SBarry Smith    TSSolve - Steps the requested number of timesteps.
3223d763cef2SBarry Smith 
3224d763cef2SBarry Smith    Collective on TS
3225d763cef2SBarry Smith 
3226d763cef2SBarry Smith    Input Parameter:
3227d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
3228cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
32295a3a76d0SJed Brown 
3230d763cef2SBarry Smith    Level: beginner
3231d763cef2SBarry Smith 
32325a3a76d0SJed Brown    Notes:
32335a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
32345a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
32355a3a76d0SJed Brown    stepped over the final time.
32365a3a76d0SJed Brown 
3237d763cef2SBarry Smith .keywords: TS, timestep, solve
3238d763cef2SBarry Smith 
3239d763cef2SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep()
3240d763cef2SBarry Smith @*/
3241cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
3242d763cef2SBarry Smith {
3243b06615a5SLisandro Dalcin   Vec               solution;
3244d763cef2SBarry Smith   PetscErrorCode    ierr;
3245d763cef2SBarry Smith 
3246d763cef2SBarry Smith   PetscFunctionBegin;
3247d763cef2SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3248f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
324949354f04SShri 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 */
3250b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
32510910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3252b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3253b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3254b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
32555a3a76d0SJed Brown     }
32560910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3257bbd56ea5SKarl Rupp   } else if (u) {
32580910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
32595a3a76d0SJed Brown   }
3260b5d403baSSean Farley   ierr = TSSetUp(ts);CHKERRQ(ierr);
3261d763cef2SBarry Smith   /* reset time step and iteration counters */
3262193ac0bcSJed Brown   ts->steps             = 0;
32635ef26d82SJed Brown   ts->ksp_its           = 0;
32645ef26d82SJed Brown   ts->snes_its          = 0;
3265c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
3266c610991cSLisandro Dalcin   ts->reject            = 0;
3267193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
3268193ac0bcSJed Brown 
3269ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3270ec8db0baSMatthew G. Knepley   {
3271ec8db0baSMatthew G. Knepley     DM dm;
3272ec8db0baSMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
3273ec8db0baSMatthew G. Knepley     ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3274ec8db0baSMatthew G. Knepley   }
3275f05ece33SBarry Smith 
3276193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
3277193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
32780910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
3279cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3280193ac0bcSJed Brown   } else {
3281d763cef2SBarry Smith     /* steps the requested number of timesteps. */
3282db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
3283db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3284cdf0de3dSShri Abhyankar     ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
32856fea3669SShri Abhyankar     if(ts->event) {
32866fea3669SShri Abhyankar       ierr = TSEventMonitorInitialize(ts);CHKERRQ(ierr);
32876fea3669SShri Abhyankar     }
3288e1a7a14fSJed Brown     while (!ts->reason) {
3289193ac0bcSJed Brown       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
3290193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
3291aeb4809dSShri Abhyankar       if (ts->event) {
3292aeb4809dSShri Abhyankar 	ierr = TSEventMonitor(ts);CHKERRQ(ierr);
32931eda64f1SShri Abhyankar       }
32948392e04aSShri Abhyankar       if(!ts->steprollback) {
3295cdf0de3dSShri Abhyankar 	ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
32961eda64f1SShri Abhyankar 	ierr = TSPostStep(ts);CHKERRQ(ierr);
3297aeb4809dSShri Abhyankar       }
3298193ac0bcSJed Brown     }
329949354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
33000910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3301cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3302b06615a5SLisandro Dalcin       solution = u;
33030574a7fbSJed Brown     } else {
3304ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3305cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3306b06615a5SLisandro Dalcin       solution = ts->vec_sol;
33070574a7fbSJed Brown     }
3308b06615a5SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr);
330927829d71SBarry Smith     ierr = VecViewFromOptions(solution, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr);
3310193ac0bcSJed Brown   }
3311d2daff3dSHong Zhang 
3312ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
3313dc1cb503SEmil Constantinescu   ierr = VecViewFromOptions(ts->vec_sol,NULL,"-ts_view_solution");CHKERRQ(ierr);
331456f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
3315ef222394SBarry Smith   if (ts->adjoint_solve) {
3316ef222394SBarry Smith     ierr = TSAdjointSolve(ts);CHKERRQ(ierr);
33172b0a91c0SBarry Smith   }
3318d763cef2SBarry Smith   PetscFunctionReturn(0);
3319d763cef2SBarry Smith }
3320d763cef2SBarry Smith 
3321d763cef2SBarry Smith #undef __FUNCT__
3322c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve"
3323c88f2d44SBarry Smith /*@
3324c88f2d44SBarry Smith    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE
3325c88f2d44SBarry Smith 
3326c88f2d44SBarry Smith    Collective on TS
3327c88f2d44SBarry Smith 
3328c88f2d44SBarry Smith    Input Parameter:
33292c39e106SBarry Smith .  ts - the TS context obtained from TSCreate()
3330c88f2d44SBarry Smith 
3331e87fd094SBarry Smith    Options Database:
3332e87fd094SBarry Smith . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial conditions
3333e87fd094SBarry Smith 
3334c88f2d44SBarry Smith    Level: intermediate
3335c88f2d44SBarry Smith 
3336c88f2d44SBarry Smith    Notes:
3337c88f2d44SBarry Smith    This must be called after a call to TSSolve() that solves the forward problem
3338c88f2d44SBarry Smith 
333973b18844SBarry Smith    By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time
334073b18844SBarry Smith 
3341c88f2d44SBarry Smith .keywords: TS, timestep, solve
3342c88f2d44SBarry Smith 
3343c88f2d44SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep()
3344c88f2d44SBarry Smith @*/
33452c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts)
3346c88f2d44SBarry Smith {
3347c88f2d44SBarry Smith   PetscErrorCode    ierr;
3348c88f2d44SBarry Smith 
3349c88f2d44SBarry Smith   PetscFunctionBegin;
3350c88f2d44SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3351c88f2d44SBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
3352c88f2d44SBarry Smith   /* reset time step and iteration counters */
3353c88f2d44SBarry Smith   ts->steps             = 0;
3354c88f2d44SBarry Smith   ts->ksp_its           = 0;
3355c88f2d44SBarry Smith   ts->snes_its          = 0;
3356c88f2d44SBarry Smith   ts->num_snes_failures = 0;
3357c88f2d44SBarry Smith   ts->reject            = 0;
3358c88f2d44SBarry Smith   ts->reason            = TS_CONVERGED_ITERATING;
3359c88f2d44SBarry Smith 
336073b18844SBarry Smith   if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps;
3361c88f2d44SBarry Smith 
336273b18844SBarry Smith   if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
3363c88f2d44SBarry Smith   while (!ts->reason) {
336473b18844SBarry Smith     ierr = TSTrajectoryGet(ts->trajectory,ts,ts->adjoint_max_steps-ts->steps,ts->ptime);CHKERRQ(ierr);
336573b18844SBarry Smith     ierr = TSMonitor(ts,ts->adjoint_max_steps-ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
336608c7845fSBarry Smith     ierr = TSAdjointStep(ts);CHKERRQ(ierr);
3367c88f2d44SBarry Smith     if (ts->event) {
3368d0578d90SShri Abhyankar       ierr = TSAdjointEventMonitor(ts);CHKERRQ(ierr);
3369d0578d90SShri Abhyankar     }
3370d0578d90SShri Abhyankar 
3371d0578d90SShri Abhyankar #if 0 /* I don't think PostStep is needed in AdjointSolve */
3372c88f2d44SBarry Smith       if (ts->event->status != TSEVENT_PROCESSING) {
3373c88f2d44SBarry Smith         ierr = TSPostStep(ts);CHKERRQ(ierr);
3374c88f2d44SBarry Smith       }
3375c88f2d44SBarry Smith     } else {
3376c88f2d44SBarry Smith       ierr = TSPostStep(ts);CHKERRQ(ierr);
3377c88f2d44SBarry Smith     }
3378d0578d90SShri Abhyankar #endif
3379c88f2d44SBarry Smith   }
3380c88f2d44SBarry Smith   ts->solvetime = ts->ptime;
3381e87fd094SBarry Smith   ierr = VecViewFromOptions(ts->vecs_sensi[0], ((PetscObject) ts)->prefix, "-ts_adjoint_view_solution");CHKERRQ(ierr);
3382c88f2d44SBarry Smith   PetscFunctionReturn(0);
3383c88f2d44SBarry Smith }
3384c88f2d44SBarry Smith 
3385c88f2d44SBarry Smith #undef __FUNCT__
3386d763cef2SBarry Smith #define __FUNCT__ "TSMonitor"
3387228d79bcSJed Brown /*@
3388228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3389228d79bcSJed Brown 
3390228d79bcSJed Brown    Collective on TS
3391228d79bcSJed Brown 
3392228d79bcSJed Brown    Input Parameters:
3393228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3394228d79bcSJed Brown .  step - step number that has just completed
3395228d79bcSJed Brown .  ptime - model time of the state
33960910c330SBarry Smith -  u - state at the current model time
3397228d79bcSJed Brown 
3398228d79bcSJed Brown    Notes:
3399228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
3400228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
3401228d79bcSJed Brown 
3402228d79bcSJed Brown    Level: advanced
3403228d79bcSJed Brown 
3404228d79bcSJed Brown .keywords: TS, timestep
3405228d79bcSJed Brown @*/
34060910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3407d763cef2SBarry Smith {
3408d763cef2SBarry Smith   PetscErrorCode ierr;
3409d763cef2SBarry Smith   PetscInt       i,n = ts->numbermonitors;
3410d763cef2SBarry Smith 
3411d763cef2SBarry Smith   PetscFunctionBegin;
3412b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3413b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
34145edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
3415d763cef2SBarry Smith   for (i=0; i<n; i++) {
34160910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
3417d763cef2SBarry Smith   }
34185edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
3419d763cef2SBarry Smith   PetscFunctionReturn(0);
3420d763cef2SBarry Smith }
3421d763cef2SBarry Smith 
3422d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
3423d763cef2SBarry Smith #undef __FUNCT__
3424a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
3425d763cef2SBarry Smith /*@C
3426a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
3427a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
3428d763cef2SBarry Smith 
3429d763cef2SBarry Smith    Collective on TS
3430d763cef2SBarry Smith 
3431d763cef2SBarry Smith    Input Parameters:
3432d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
3433d763cef2SBarry Smith .  label - the title to put in the title bar
34347c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
3435a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
3436a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
3437d763cef2SBarry Smith 
3438d763cef2SBarry Smith    Output Parameter:
34390b039ecaSBarry Smith .  ctx - the context
3440d763cef2SBarry Smith 
3441d763cef2SBarry Smith    Options Database Key:
34424f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
34434f09c107SBarry Smith .  -ts_monitor_lg_solution -
344499fdda47SBarry Smith .  -ts_monitor_lg_error -
344599fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
344699fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
344799fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
3448d763cef2SBarry Smith 
3449d763cef2SBarry Smith    Notes:
3450a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
3451d763cef2SBarry Smith 
3452d763cef2SBarry Smith    Level: intermediate
3453d763cef2SBarry Smith 
34547c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
3455d763cef2SBarry Smith 
34564f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
34577c922b88SBarry Smith 
3458d763cef2SBarry Smith @*/
3459a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
3460d763cef2SBarry Smith {
3461b0a32e0cSBarry Smith   PetscDraw      win;
3462dfbe8321SBarry Smith   PetscErrorCode ierr;
3463d763cef2SBarry Smith 
3464d763cef2SBarry Smith   PetscFunctionBegin;
3465b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
3466a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
34673fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
34680b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
34693bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)(*ctx)->lg,(PetscObject)win);CHKERRQ(ierr);
3470287de1a7SBarry Smith   ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr);
3471287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
3472a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
3473d763cef2SBarry Smith   PetscFunctionReturn(0);
3474d763cef2SBarry Smith }
3475d763cef2SBarry Smith 
34764a2ae208SSatish Balay #undef __FUNCT__
34774f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
3478b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
3479d763cef2SBarry Smith {
34800b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
3481c32365f1SBarry Smith   PetscReal      x   = ptime,y;
3482dfbe8321SBarry Smith   PetscErrorCode ierr;
3483d763cef2SBarry Smith 
3484d763cef2SBarry Smith   PetscFunctionBegin;
3485b06615a5SLisandro Dalcin   if (!step) {
3486a9f9c1f6SBarry Smith     PetscDrawAxis axis;
3487a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
3488a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
3489a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
3490287de1a7SBarry Smith     ierr = PetscDrawLGIndicateDataPoints(ctx->lg,PETSC_TRUE);CHKERRQ(ierr);
3491a9f9c1f6SBarry Smith   }
3492c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
34930b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
3494b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
34950b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
34963923b477SBarry Smith   }
3497d763cef2SBarry Smith   PetscFunctionReturn(0);
3498d763cef2SBarry Smith }
3499d763cef2SBarry Smith 
35004a2ae208SSatish Balay #undef __FUNCT__
3501a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
3502d763cef2SBarry Smith /*@C
3503a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
3504a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
3505d763cef2SBarry Smith 
35060b039ecaSBarry Smith    Collective on TSMonitorLGCtx
3507d763cef2SBarry Smith 
3508d763cef2SBarry Smith    Input Parameter:
35090b039ecaSBarry Smith .  ctx - the monitor context
3510d763cef2SBarry Smith 
3511d763cef2SBarry Smith    Level: intermediate
3512d763cef2SBarry Smith 
3513d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
3514d763cef2SBarry Smith 
35154f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
3516d763cef2SBarry Smith @*/
3517a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
3518d763cef2SBarry Smith {
3519b0a32e0cSBarry Smith   PetscDraw      draw;
3520dfbe8321SBarry Smith   PetscErrorCode ierr;
3521d763cef2SBarry Smith 
3522d763cef2SBarry Smith   PetscFunctionBegin;
35237684fa3eSBarry Smith   if ((*ctx)->transformdestroy) {
35247684fa3eSBarry Smith     ierr = ((*ctx)->transformdestroy)((*ctx)->transformctx);CHKERRQ(ierr);
35257684fa3eSBarry Smith   }
35260b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
35276bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
35280b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
352931152f8aSBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->names);CHKERRQ(ierr);
3530387f4636SBarry Smith   ierr = PetscStrArrayDestroy(&(*ctx)->displaynames);CHKERRQ(ierr);
3531387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvariables);CHKERRQ(ierr);
3532387f4636SBarry Smith   ierr = PetscFree((*ctx)->displayvalues);CHKERRQ(ierr);
35330b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
3534d763cef2SBarry Smith   PetscFunctionReturn(0);
3535d763cef2SBarry Smith }
3536d763cef2SBarry Smith 
35374a2ae208SSatish Balay #undef __FUNCT__
35384a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
3539d763cef2SBarry Smith /*@
3540b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
3541d763cef2SBarry Smith 
3542d763cef2SBarry Smith    Not Collective
3543d763cef2SBarry Smith 
3544d763cef2SBarry Smith    Input Parameter:
3545d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3546d763cef2SBarry Smith 
3547d763cef2SBarry Smith    Output Parameter:
3548d763cef2SBarry Smith .  t  - the current time
3549d763cef2SBarry Smith 
3550d763cef2SBarry Smith    Level: beginner
3551d763cef2SBarry Smith 
3552b8123daeSJed Brown    Note:
3553b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
35549be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
3555b8123daeSJed Brown 
3556d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3557d763cef2SBarry Smith 
3558d763cef2SBarry Smith .keywords: TS, get, time
3559d763cef2SBarry Smith @*/
35607087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
3561d763cef2SBarry Smith {
3562d763cef2SBarry Smith   PetscFunctionBegin;
35630700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3564f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
3565d763cef2SBarry Smith   *t = ts->ptime;
3566d763cef2SBarry Smith   PetscFunctionReturn(0);
3567d763cef2SBarry Smith }
3568d763cef2SBarry Smith 
35694a2ae208SSatish Balay #undef __FUNCT__
3570e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime"
3571e5e524a1SHong Zhang /*@
3572e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
3573e5e524a1SHong Zhang 
3574e5e524a1SHong Zhang    Not Collective
3575e5e524a1SHong Zhang 
3576e5e524a1SHong Zhang    Input Parameter:
3577e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
3578e5e524a1SHong Zhang 
3579e5e524a1SHong Zhang    Output Parameter:
3580e5e524a1SHong Zhang .  t  - the previous time
3581e5e524a1SHong Zhang 
3582e5e524a1SHong Zhang    Level: beginner
3583e5e524a1SHong Zhang 
3584e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3585e5e524a1SHong Zhang 
3586e5e524a1SHong Zhang .keywords: TS, get, time
3587e5e524a1SHong Zhang @*/
3588e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
3589e5e524a1SHong Zhang {
3590e5e524a1SHong Zhang   PetscFunctionBegin;
3591e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3592e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
3593e5e524a1SHong Zhang   *t = ts->ptime_prev;
3594e5e524a1SHong Zhang   PetscFunctionReturn(0);
3595e5e524a1SHong Zhang }
3596e5e524a1SHong Zhang 
3597e5e524a1SHong Zhang #undef __FUNCT__
35986a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
35996a4d4014SLisandro Dalcin /*@
36006a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
36016a4d4014SLisandro Dalcin 
36023f9fe445SBarry Smith    Logically Collective on TS
36036a4d4014SLisandro Dalcin 
36046a4d4014SLisandro Dalcin    Input Parameters:
36056a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
36066a4d4014SLisandro Dalcin -  time - the time
36076a4d4014SLisandro Dalcin 
36086a4d4014SLisandro Dalcin    Level: intermediate
36096a4d4014SLisandro Dalcin 
36106a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
36116a4d4014SLisandro Dalcin 
36126a4d4014SLisandro Dalcin .keywords: TS, set, time
36136a4d4014SLisandro Dalcin @*/
36147087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
36156a4d4014SLisandro Dalcin {
36166a4d4014SLisandro Dalcin   PetscFunctionBegin;
36170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3618c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
36196a4d4014SLisandro Dalcin   ts->ptime = t;
36206a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
36216a4d4014SLisandro Dalcin }
36226a4d4014SLisandro Dalcin 
36236a4d4014SLisandro Dalcin #undef __FUNCT__
36244a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
3625d763cef2SBarry Smith /*@C
3626d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
3627d763cef2SBarry Smith    TS options in the database.
3628d763cef2SBarry Smith 
36293f9fe445SBarry Smith    Logically Collective on TS
3630d763cef2SBarry Smith 
3631d763cef2SBarry Smith    Input Parameter:
3632d763cef2SBarry Smith +  ts     - The TS context
3633d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3634d763cef2SBarry Smith 
3635d763cef2SBarry Smith    Notes:
3636d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3637d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3638d763cef2SBarry Smith    hyphen.
3639d763cef2SBarry Smith 
3640d763cef2SBarry Smith    Level: advanced
3641d763cef2SBarry Smith 
3642d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
3643d763cef2SBarry Smith 
3644d763cef2SBarry Smith .seealso: TSSetFromOptions()
3645d763cef2SBarry Smith 
3646d763cef2SBarry Smith @*/
36477087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
3648d763cef2SBarry Smith {
3649dfbe8321SBarry Smith   PetscErrorCode ierr;
3650089b2837SJed Brown   SNES           snes;
3651d763cef2SBarry Smith 
3652d763cef2SBarry Smith   PetscFunctionBegin;
36530700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3654d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3655089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3656089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3657d763cef2SBarry Smith   PetscFunctionReturn(0);
3658d763cef2SBarry Smith }
3659d763cef2SBarry Smith 
3660d763cef2SBarry Smith 
36614a2ae208SSatish Balay #undef __FUNCT__
36624a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
3663d763cef2SBarry Smith /*@C
3664d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
3665d763cef2SBarry Smith    TS options in the database.
3666d763cef2SBarry Smith 
36673f9fe445SBarry Smith    Logically Collective on TS
3668d763cef2SBarry Smith 
3669d763cef2SBarry Smith    Input Parameter:
3670d763cef2SBarry Smith +  ts     - The TS context
3671d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3672d763cef2SBarry Smith 
3673d763cef2SBarry Smith    Notes:
3674d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3675d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3676d763cef2SBarry Smith    hyphen.
3677d763cef2SBarry Smith 
3678d763cef2SBarry Smith    Level: advanced
3679d763cef2SBarry Smith 
3680d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
3681d763cef2SBarry Smith 
3682d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
3683d763cef2SBarry Smith 
3684d763cef2SBarry Smith @*/
36857087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
3686d763cef2SBarry Smith {
3687dfbe8321SBarry Smith   PetscErrorCode ierr;
3688089b2837SJed Brown   SNES           snes;
3689d763cef2SBarry Smith 
3690d763cef2SBarry Smith   PetscFunctionBegin;
36910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3692d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3693089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3694089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3695d763cef2SBarry Smith   PetscFunctionReturn(0);
3696d763cef2SBarry Smith }
3697d763cef2SBarry Smith 
36984a2ae208SSatish Balay #undef __FUNCT__
36994a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
3700d763cef2SBarry Smith /*@C
3701d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
3702d763cef2SBarry Smith    TS options in the database.
3703d763cef2SBarry Smith 
3704d763cef2SBarry Smith    Not Collective
3705d763cef2SBarry Smith 
3706d763cef2SBarry Smith    Input Parameter:
3707d763cef2SBarry Smith .  ts - The TS context
3708d763cef2SBarry Smith 
3709d763cef2SBarry Smith    Output Parameter:
3710d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
3711d763cef2SBarry Smith 
3712d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
3713d763cef2SBarry Smith    sufficient length to hold the prefix.
3714d763cef2SBarry Smith 
3715d763cef2SBarry Smith    Level: intermediate
3716d763cef2SBarry Smith 
3717d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
3718d763cef2SBarry Smith 
3719d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
3720d763cef2SBarry Smith @*/
37217087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
3722d763cef2SBarry Smith {
3723dfbe8321SBarry Smith   PetscErrorCode ierr;
3724d763cef2SBarry Smith 
3725d763cef2SBarry Smith   PetscFunctionBegin;
37260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
37274482741eSBarry Smith   PetscValidPointer(prefix,2);
3728d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3729d763cef2SBarry Smith   PetscFunctionReturn(0);
3730d763cef2SBarry Smith }
3731d763cef2SBarry Smith 
37324a2ae208SSatish Balay #undef __FUNCT__
37334a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
3734d763cef2SBarry Smith /*@C
3735d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
3736d763cef2SBarry Smith 
3737d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
3738d763cef2SBarry Smith 
3739d763cef2SBarry Smith    Input Parameter:
3740d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
3741d763cef2SBarry Smith 
3742d763cef2SBarry Smith    Output Parameters:
3743e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
3744e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
3745e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
3746e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
3747d763cef2SBarry Smith 
37480298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
3749d763cef2SBarry Smith 
3750d763cef2SBarry Smith    Level: intermediate
3751d763cef2SBarry Smith 
375226d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
3753d763cef2SBarry Smith 
3754d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
3755d763cef2SBarry Smith @*/
3756e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
3757d763cef2SBarry Smith {
3758089b2837SJed Brown   PetscErrorCode ierr;
3759089b2837SJed Brown   SNES           snes;
376024989b8cSPeter Brune   DM             dm;
3761089b2837SJed Brown 
3762d763cef2SBarry Smith   PetscFunctionBegin;
3763089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3764e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
376524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
376624989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
3767d763cef2SBarry Smith   PetscFunctionReturn(0);
3768d763cef2SBarry Smith }
3769d763cef2SBarry Smith 
37701713a123SBarry Smith #undef __FUNCT__
37712eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
37722eca1d9cSJed Brown /*@C
37732eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
37742eca1d9cSJed Brown 
37752eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
37762eca1d9cSJed Brown 
37772eca1d9cSJed Brown    Input Parameter:
37782eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
37792eca1d9cSJed Brown 
37802eca1d9cSJed Brown    Output Parameters:
3781e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
3782e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
37832eca1d9cSJed Brown .  f   - The function to compute the matrices
37842eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
37852eca1d9cSJed Brown 
37860298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
37872eca1d9cSJed Brown 
37882eca1d9cSJed Brown    Level: advanced
37892eca1d9cSJed Brown 
37902eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
37912eca1d9cSJed Brown 
37922eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
37932eca1d9cSJed Brown @*/
3794e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
37952eca1d9cSJed Brown {
3796089b2837SJed Brown   PetscErrorCode ierr;
3797089b2837SJed Brown   SNES           snes;
379824989b8cSPeter Brune   DM             dm;
37990910c330SBarry Smith 
38002eca1d9cSJed Brown   PetscFunctionBegin;
3801089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3802f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
3803e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
380424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
380524989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
38062eca1d9cSJed Brown   PetscFunctionReturn(0);
38072eca1d9cSJed Brown }
38082eca1d9cSJed Brown 
38096083293cSBarry Smith 
38102eca1d9cSJed Brown #undef __FUNCT__
381183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
38121713a123SBarry Smith /*@C
381383a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
38141713a123SBarry Smith    VecView() for the solution at each timestep
38151713a123SBarry Smith 
38161713a123SBarry Smith    Collective on TS
38171713a123SBarry Smith 
38181713a123SBarry Smith    Input Parameters:
38191713a123SBarry Smith +  ts - the TS context
38201713a123SBarry Smith .  step - current time-step
3821142b95e3SSatish Balay .  ptime - current time
38220298fd71SBarry Smith -  dummy - either a viewer or NULL
38231713a123SBarry Smith 
382499fdda47SBarry Smith    Options Database:
382599fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
382699fdda47SBarry Smith 
3827387f4636SBarry 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
382899fdda47SBarry Smith        will look bad
382999fdda47SBarry Smith 
38301713a123SBarry Smith    Level: intermediate
38311713a123SBarry Smith 
38321713a123SBarry Smith .keywords: TS,  vector, monitor, view
38331713a123SBarry Smith 
3834a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
38351713a123SBarry Smith @*/
38360910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
38371713a123SBarry Smith {
3838dfbe8321SBarry Smith   PetscErrorCode   ierr;
383983a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
3840473a3ab2SBarry Smith   PetscDraw        draw;
38411713a123SBarry Smith 
38421713a123SBarry Smith   PetscFunctionBegin;
38436083293cSBarry Smith   if (!step && ictx->showinitial) {
38446083293cSBarry Smith     if (!ictx->initialsolution) {
38450910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
38461713a123SBarry Smith     }
38470910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
38486083293cSBarry Smith   }
3849b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
38500dcf80beSBarry Smith 
38516083293cSBarry Smith   if (ictx->showinitial) {
38526083293cSBarry Smith     PetscReal pause;
38536083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
38546083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
38556083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
38566083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
38576083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
38586083293cSBarry Smith   }
38590910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
3860473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
3861473a3ab2SBarry Smith     PetscReal xl,yl,xr,yr,tw,w,h;
3862473a3ab2SBarry Smith     char      time[32];
3863473a3ab2SBarry Smith     size_t    len;
3864473a3ab2SBarry Smith 
3865473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
386685b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
3867473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
3868473a3ab2SBarry Smith     ierr = PetscStrlen(time,&len);CHKERRQ(ierr);
38690298fd71SBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
3870473a3ab2SBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
3871473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
3872473a3ab2SBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
3873473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
3874473a3ab2SBarry Smith   }
3875473a3ab2SBarry Smith 
38766083293cSBarry Smith   if (ictx->showinitial) {
38776083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
38786083293cSBarry Smith   }
38791713a123SBarry Smith   PetscFunctionReturn(0);
38801713a123SBarry Smith }
38811713a123SBarry Smith 
38822d5ee99bSBarry Smith #undef __FUNCT__
38832d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
38842d5ee99bSBarry Smith /*@C
38852d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
38862d5ee99bSBarry Smith 
38872d5ee99bSBarry Smith    Collective on TS
38882d5ee99bSBarry Smith 
38892d5ee99bSBarry Smith    Input Parameters:
38902d5ee99bSBarry Smith +  ts - the TS context
38912d5ee99bSBarry Smith .  step - current time-step
38922d5ee99bSBarry Smith .  ptime - current time
38932d5ee99bSBarry Smith -  dummy - either a viewer or NULL
38942d5ee99bSBarry Smith 
38952d5ee99bSBarry Smith    Level: intermediate
38962d5ee99bSBarry Smith 
38972d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
38982d5ee99bSBarry Smith 
38992d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
39002d5ee99bSBarry Smith @*/
39012d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
39022d5ee99bSBarry Smith {
39032d5ee99bSBarry Smith   PetscErrorCode    ierr;
39042d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
39052d5ee99bSBarry Smith   PetscDraw         draw;
39062d5ee99bSBarry Smith   MPI_Comm          comm;
39072d5ee99bSBarry Smith   PetscInt          n;
39082d5ee99bSBarry Smith   PetscMPIInt       size;
39092d5ee99bSBarry Smith   PetscReal         xl,yl,xr,yr,tw,w,h;
39102d5ee99bSBarry Smith   char              time[32];
39112d5ee99bSBarry Smith   size_t            len;
39122d5ee99bSBarry Smith   const PetscScalar *U;
39132d5ee99bSBarry Smith 
39142d5ee99bSBarry Smith   PetscFunctionBegin;
39152d5ee99bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
39162d5ee99bSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
39172d5ee99bSBarry Smith   if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs");
39182d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
39192d5ee99bSBarry Smith   if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns");
39202d5ee99bSBarry Smith 
39212d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
39222d5ee99bSBarry Smith 
39232d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
39244b363babSBarry Smith   ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
3925ba5783adSMatthew G Knepley   if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) {
3926a4494fc1SBarry Smith       ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
3927a4494fc1SBarry Smith       PetscFunctionReturn(0);
3928a4494fc1SBarry Smith   }
39292d5ee99bSBarry Smith   if (!step) ictx->color++;
39302d5ee99bSBarry Smith   ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr);
39312d5ee99bSBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
39322d5ee99bSBarry Smith 
39332d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
39344b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
393585b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
39362d5ee99bSBarry Smith     ierr = PetscStrlen(time,&len);CHKERRQ(ierr);
39372d5ee99bSBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
39382d5ee99bSBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
39392d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
39402d5ee99bSBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
39412d5ee99bSBarry Smith   }
39422d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
39432d5ee99bSBarry Smith   PetscFunctionReturn(0);
39442d5ee99bSBarry Smith }
39452d5ee99bSBarry Smith 
39461713a123SBarry Smith 
39476c699258SBarry Smith #undef __FUNCT__
394883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
39496083293cSBarry Smith /*@C
395083a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
39516083293cSBarry Smith 
39526083293cSBarry Smith    Collective on TS
39536083293cSBarry Smith 
39546083293cSBarry Smith    Input Parameters:
39556083293cSBarry Smith .    ctx - the monitor context
39566083293cSBarry Smith 
39576083293cSBarry Smith    Level: intermediate
39586083293cSBarry Smith 
39596083293cSBarry Smith .keywords: TS,  vector, monitor, view
39606083293cSBarry Smith 
396183a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
39626083293cSBarry Smith @*/
396383a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
39646083293cSBarry Smith {
39656083293cSBarry Smith   PetscErrorCode ierr;
39666083293cSBarry Smith 
39676083293cSBarry Smith   PetscFunctionBegin;
39684b363babSBarry Smith   ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr);
396983a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
397083a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
397183a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
39726083293cSBarry Smith   PetscFunctionReturn(0);
39736083293cSBarry Smith }
39746083293cSBarry Smith 
39756083293cSBarry Smith #undef __FUNCT__
397683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
39776083293cSBarry Smith /*@C
397883a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
39796083293cSBarry Smith 
39806083293cSBarry Smith    Collective on TS
39816083293cSBarry Smith 
39826083293cSBarry Smith    Input Parameter:
39836083293cSBarry Smith .    ts - time-step context
39846083293cSBarry Smith 
39856083293cSBarry Smith    Output Patameter:
39866083293cSBarry Smith .    ctx - the monitor context
39876083293cSBarry Smith 
398899fdda47SBarry Smith    Options Database:
398999fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
399099fdda47SBarry Smith 
39916083293cSBarry Smith    Level: intermediate
39926083293cSBarry Smith 
39936083293cSBarry Smith .keywords: TS,  vector, monitor, view
39946083293cSBarry Smith 
399583a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
39966083293cSBarry Smith @*/
399783a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
39986083293cSBarry Smith {
39996083293cSBarry Smith   PetscErrorCode   ierr;
40006083293cSBarry Smith 
40016083293cSBarry Smith   PetscFunctionBegin;
4002b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
400383a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
4004e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
4005bbd56ea5SKarl Rupp 
400683a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
4007473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
40080298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
4009473a3ab2SBarry Smith 
4010473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
40110298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
40122d5ee99bSBarry Smith   (*ctx)->color = PETSC_DRAW_WHITE;
40136083293cSBarry Smith   PetscFunctionReturn(0);
40146083293cSBarry Smith }
40156083293cSBarry Smith 
40166083293cSBarry Smith #undef __FUNCT__
401783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
40183a471f94SBarry Smith /*@C
401983a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
40203a471f94SBarry Smith    VecView() for the error at each timestep
40213a471f94SBarry Smith 
40223a471f94SBarry Smith    Collective on TS
40233a471f94SBarry Smith 
40243a471f94SBarry Smith    Input Parameters:
40253a471f94SBarry Smith +  ts - the TS context
40263a471f94SBarry Smith .  step - current time-step
40273a471f94SBarry Smith .  ptime - current time
40280298fd71SBarry Smith -  dummy - either a viewer or NULL
40293a471f94SBarry Smith 
40303a471f94SBarry Smith    Level: intermediate
40313a471f94SBarry Smith 
40323a471f94SBarry Smith .keywords: TS,  vector, monitor, view
40333a471f94SBarry Smith 
40343a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
40353a471f94SBarry Smith @*/
40360910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
40373a471f94SBarry Smith {
40383a471f94SBarry Smith   PetscErrorCode   ierr;
403983a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
404083a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
40413a471f94SBarry Smith   Vec              work;
40423a471f94SBarry Smith 
40433a471f94SBarry Smith   PetscFunctionBegin;
4044b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
40450910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
40463a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
40470910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
40483a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
40493a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
40503a471f94SBarry Smith   PetscFunctionReturn(0);
40513a471f94SBarry Smith }
40523a471f94SBarry Smith 
40532a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
40543a471f94SBarry Smith #undef __FUNCT__
40556c699258SBarry Smith #define __FUNCT__ "TSSetDM"
40566c699258SBarry Smith /*@
40576c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
40586c699258SBarry Smith 
40593f9fe445SBarry Smith    Logically Collective on TS and DM
40606c699258SBarry Smith 
40616c699258SBarry Smith    Input Parameters:
40626c699258SBarry Smith +  ts - the preconditioner context
40636c699258SBarry Smith -  dm - the dm
40646c699258SBarry Smith 
40656c699258SBarry Smith    Level: intermediate
40666c699258SBarry Smith 
40676c699258SBarry Smith 
40686c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
40696c699258SBarry Smith @*/
40707087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
40716c699258SBarry Smith {
40726c699258SBarry Smith   PetscErrorCode ierr;
4073089b2837SJed Brown   SNES           snes;
4074942e3340SBarry Smith   DMTS           tsdm;
40756c699258SBarry Smith 
40766c699258SBarry Smith   PetscFunctionBegin;
40770700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
407870663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4079942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
40802a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4081942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4082942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
408324989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
408424989b8cSPeter Brune         tsdm->originaldm = dm;
408524989b8cSPeter Brune       }
408624989b8cSPeter Brune     }
40876bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
408824989b8cSPeter Brune   }
40896c699258SBarry Smith   ts->dm = dm;
4090bbd56ea5SKarl Rupp 
4091089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4092089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
40936c699258SBarry Smith   PetscFunctionReturn(0);
40946c699258SBarry Smith }
40956c699258SBarry Smith 
40966c699258SBarry Smith #undef __FUNCT__
40976c699258SBarry Smith #define __FUNCT__ "TSGetDM"
40986c699258SBarry Smith /*@
40996c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
41006c699258SBarry Smith 
41013f9fe445SBarry Smith    Not Collective
41026c699258SBarry Smith 
41036c699258SBarry Smith    Input Parameter:
41046c699258SBarry Smith . ts - the preconditioner context
41056c699258SBarry Smith 
41066c699258SBarry Smith    Output Parameter:
41076c699258SBarry Smith .  dm - the dm
41086c699258SBarry Smith 
41096c699258SBarry Smith    Level: intermediate
41106c699258SBarry Smith 
41116c699258SBarry Smith 
41126c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
41136c699258SBarry Smith @*/
41147087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
41156c699258SBarry Smith {
4116496e6a7aSJed Brown   PetscErrorCode ierr;
4117496e6a7aSJed Brown 
41186c699258SBarry Smith   PetscFunctionBegin;
41190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4120496e6a7aSJed Brown   if (!ts->dm) {
4121ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4122496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4123496e6a7aSJed Brown   }
41246c699258SBarry Smith   *dm = ts->dm;
41256c699258SBarry Smith   PetscFunctionReturn(0);
41266c699258SBarry Smith }
41271713a123SBarry Smith 
41280f5c6efeSJed Brown #undef __FUNCT__
41290f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
41300f5c6efeSJed Brown /*@
41310f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
41320f5c6efeSJed Brown 
41333f9fe445SBarry Smith    Logically Collective on SNES
41340f5c6efeSJed Brown 
41350f5c6efeSJed Brown    Input Parameter:
4136d42a1c89SJed Brown + snes - nonlinear solver
41370910c330SBarry Smith . U - the current state at which to evaluate the residual
4138d42a1c89SJed Brown - ctx - user context, must be a TS
41390f5c6efeSJed Brown 
41400f5c6efeSJed Brown    Output Parameter:
41410f5c6efeSJed Brown . F - the nonlinear residual
41420f5c6efeSJed Brown 
41430f5c6efeSJed Brown    Notes:
41440f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
41450f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
41460f5c6efeSJed Brown 
41470f5c6efeSJed Brown    Level: advanced
41480f5c6efeSJed Brown 
41490f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
41500f5c6efeSJed Brown @*/
41510910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
41520f5c6efeSJed Brown {
41530f5c6efeSJed Brown   TS             ts = (TS)ctx;
41540f5c6efeSJed Brown   PetscErrorCode ierr;
41550f5c6efeSJed Brown 
41560f5c6efeSJed Brown   PetscFunctionBegin;
41570f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
41580910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
41590f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
41600f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
41610910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
41620f5c6efeSJed Brown   PetscFunctionReturn(0);
41630f5c6efeSJed Brown }
41640f5c6efeSJed Brown 
41650f5c6efeSJed Brown #undef __FUNCT__
41660f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
41670f5c6efeSJed Brown /*@
41680f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
41690f5c6efeSJed Brown 
41700f5c6efeSJed Brown    Collective on SNES
41710f5c6efeSJed Brown 
41720f5c6efeSJed Brown    Input Parameter:
41730f5c6efeSJed Brown + snes - nonlinear solver
41740910c330SBarry Smith . U - the current state at which to evaluate the residual
41750f5c6efeSJed Brown - ctx - user context, must be a TS
41760f5c6efeSJed Brown 
41770f5c6efeSJed Brown    Output Parameter:
41780f5c6efeSJed Brown + A - the Jacobian
41790f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
41800f5c6efeSJed Brown - flag - indicates any structure change in the matrix
41810f5c6efeSJed Brown 
41820f5c6efeSJed Brown    Notes:
41830f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
41840f5c6efeSJed Brown 
41850f5c6efeSJed Brown    Level: developer
41860f5c6efeSJed Brown 
41870f5c6efeSJed Brown .seealso: SNESSetJacobian()
41880f5c6efeSJed Brown @*/
4189d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
41900f5c6efeSJed Brown {
41910f5c6efeSJed Brown   TS             ts = (TS)ctx;
41920f5c6efeSJed Brown   PetscErrorCode ierr;
41930f5c6efeSJed Brown 
41940f5c6efeSJed Brown   PetscFunctionBegin;
41950f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
41960910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
41970f5c6efeSJed Brown   PetscValidPointer(A,3);
419894ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
41990f5c6efeSJed Brown   PetscValidPointer(B,4);
420094ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
42010f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4202d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
42030f5c6efeSJed Brown   PetscFunctionReturn(0);
42040f5c6efeSJed Brown }
4205325fc9f4SBarry Smith 
42060e4ef248SJed Brown #undef __FUNCT__
42070e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
42080e4ef248SJed Brown /*@C
42090e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
42100e4ef248SJed Brown 
42110e4ef248SJed Brown    Collective on TS
42120e4ef248SJed Brown 
42130e4ef248SJed Brown    Input Arguments:
42140e4ef248SJed Brown +  ts - time stepping context
42150e4ef248SJed Brown .  t - time at which to evaluate
42160910c330SBarry Smith .  U - state at which to evaluate
42170e4ef248SJed Brown -  ctx - context
42180e4ef248SJed Brown 
42190e4ef248SJed Brown    Output Arguments:
42200e4ef248SJed Brown .  F - right hand side
42210e4ef248SJed Brown 
42220e4ef248SJed Brown    Level: intermediate
42230e4ef248SJed Brown 
42240e4ef248SJed Brown    Notes:
42250e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
42260e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
42270e4ef248SJed Brown 
42280e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
42290e4ef248SJed Brown @*/
42300910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
42310e4ef248SJed Brown {
42320e4ef248SJed Brown   PetscErrorCode ierr;
42330e4ef248SJed Brown   Mat            Arhs,Brhs;
42340e4ef248SJed Brown 
42350e4ef248SJed Brown   PetscFunctionBegin;
42360e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4237d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
42380910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
42390e4ef248SJed Brown   PetscFunctionReturn(0);
42400e4ef248SJed Brown }
42410e4ef248SJed Brown 
42420e4ef248SJed Brown #undef __FUNCT__
42430e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
42440e4ef248SJed Brown /*@C
42450e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
42460e4ef248SJed Brown 
42470e4ef248SJed Brown    Collective on TS
42480e4ef248SJed Brown 
42490e4ef248SJed Brown    Input Arguments:
42500e4ef248SJed Brown +  ts - time stepping context
42510e4ef248SJed Brown .  t - time at which to evaluate
42520910c330SBarry Smith .  U - state at which to evaluate
42530e4ef248SJed Brown -  ctx - context
42540e4ef248SJed Brown 
42550e4ef248SJed Brown    Output Arguments:
42560e4ef248SJed Brown +  A - pointer to operator
42570e4ef248SJed Brown .  B - pointer to preconditioning matrix
42580e4ef248SJed Brown -  flg - matrix structure flag
42590e4ef248SJed Brown 
42600e4ef248SJed Brown    Level: intermediate
42610e4ef248SJed Brown 
42620e4ef248SJed Brown    Notes:
42630e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
42640e4ef248SJed Brown 
42650e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
42660e4ef248SJed Brown @*/
4267d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
42680e4ef248SJed Brown {
42690e4ef248SJed Brown   PetscFunctionBegin;
42700e4ef248SJed Brown   PetscFunctionReturn(0);
42710e4ef248SJed Brown }
42720e4ef248SJed Brown 
42730026cea9SSean Farley #undef __FUNCT__
42740026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
42750026cea9SSean Farley /*@C
42760026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
42770026cea9SSean Farley 
42780026cea9SSean Farley    Collective on TS
42790026cea9SSean Farley 
42800026cea9SSean Farley    Input Arguments:
42810026cea9SSean Farley +  ts - time stepping context
42820026cea9SSean Farley .  t - time at which to evaluate
42830910c330SBarry Smith .  U - state at which to evaluate
42840910c330SBarry Smith .  Udot - time derivative of state vector
42850026cea9SSean Farley -  ctx - context
42860026cea9SSean Farley 
42870026cea9SSean Farley    Output Arguments:
42880026cea9SSean Farley .  F - left hand side
42890026cea9SSean Farley 
42900026cea9SSean Farley    Level: intermediate
42910026cea9SSean Farley 
42920026cea9SSean Farley    Notes:
42930910c330SBarry 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
42940026cea9SSean Farley    user is required to write their own TSComputeIFunction.
42950026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
42960026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
42970026cea9SSean Farley 
42980026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
42990026cea9SSean Farley @*/
43000910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
43010026cea9SSean Farley {
43020026cea9SSean Farley   PetscErrorCode ierr;
43030026cea9SSean Farley   Mat            A,B;
43040026cea9SSean Farley 
43050026cea9SSean Farley   PetscFunctionBegin;
43060298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4307d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
43080910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
43090026cea9SSean Farley   PetscFunctionReturn(0);
43100026cea9SSean Farley }
43110026cea9SSean Farley 
43120026cea9SSean Farley #undef __FUNCT__
43130026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
43140026cea9SSean Farley /*@C
4315b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
43160026cea9SSean Farley 
43170026cea9SSean Farley    Collective on TS
43180026cea9SSean Farley 
43190026cea9SSean Farley    Input Arguments:
43200026cea9SSean Farley +  ts - time stepping context
43210026cea9SSean Farley .  t - time at which to evaluate
43220910c330SBarry Smith .  U - state at which to evaluate
43230910c330SBarry Smith .  Udot - time derivative of state vector
43240026cea9SSean Farley .  shift - shift to apply
43250026cea9SSean Farley -  ctx - context
43260026cea9SSean Farley 
43270026cea9SSean Farley    Output Arguments:
43280026cea9SSean Farley +  A - pointer to operator
43290026cea9SSean Farley .  B - pointer to preconditioning matrix
43300026cea9SSean Farley -  flg - matrix structure flag
43310026cea9SSean Farley 
4332b41af12eSJed Brown    Level: advanced
43330026cea9SSean Farley 
43340026cea9SSean Farley    Notes:
43350026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
43360026cea9SSean Farley 
4337b41af12eSJed Brown    It is only appropriate for problems of the form
4338b41af12eSJed Brown 
4339b41af12eSJed Brown $     M Udot = F(U,t)
4340b41af12eSJed Brown 
4341b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4342b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4343b41af12eSJed Brown   an implicit operator of the form
4344b41af12eSJed Brown 
4345b41af12eSJed Brown $    shift*M + J
4346b41af12eSJed Brown 
4347b41af12eSJed 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
4348b41af12eSJed Brown   a copy of M or reassemble it when requested.
4349b41af12eSJed Brown 
43500026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
43510026cea9SSean Farley @*/
4352d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
43530026cea9SSean Farley {
4354b41af12eSJed Brown   PetscErrorCode ierr;
4355b41af12eSJed Brown 
43560026cea9SSean Farley   PetscFunctionBegin;
435794ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4358b41af12eSJed Brown   ts->ijacobian.shift = shift;
43590026cea9SSean Farley   PetscFunctionReturn(0);
43600026cea9SSean Farley }
4361b41af12eSJed Brown 
4362e817cc15SEmil Constantinescu #undef __FUNCT__
4363e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
4364e817cc15SEmil Constantinescu /*@
4365e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4366e817cc15SEmil Constantinescu 
4367e817cc15SEmil Constantinescu    Not Collective
4368e817cc15SEmil Constantinescu 
4369e817cc15SEmil Constantinescu    Input Parameter:
4370e817cc15SEmil Constantinescu .  ts - the TS context
4371e817cc15SEmil Constantinescu 
4372e817cc15SEmil Constantinescu    Output Parameter:
43734e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4374e817cc15SEmil Constantinescu 
4375e817cc15SEmil Constantinescu    Level: beginner
4376e817cc15SEmil Constantinescu 
4377e817cc15SEmil Constantinescu .keywords: TS, equation type
4378e817cc15SEmil Constantinescu 
4379e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4380e817cc15SEmil Constantinescu @*/
4381e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4382e817cc15SEmil Constantinescu {
4383e817cc15SEmil Constantinescu   PetscFunctionBegin;
4384e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4385e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4386e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4387e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4388e817cc15SEmil Constantinescu }
4389e817cc15SEmil Constantinescu 
4390e817cc15SEmil Constantinescu #undef __FUNCT__
4391e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
4392e817cc15SEmil Constantinescu /*@
4393e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4394e817cc15SEmil Constantinescu 
4395e817cc15SEmil Constantinescu    Not Collective
4396e817cc15SEmil Constantinescu 
4397e817cc15SEmil Constantinescu    Input Parameter:
4398e817cc15SEmil Constantinescu +  ts - the TS context
43991297b224SEmil Constantinescu -  equation_type - see TSEquationType
4400e817cc15SEmil Constantinescu 
4401e817cc15SEmil Constantinescu    Level: advanced
4402e817cc15SEmil Constantinescu 
4403e817cc15SEmil Constantinescu .keywords: TS, equation type
4404e817cc15SEmil Constantinescu 
4405e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4406e817cc15SEmil Constantinescu @*/
4407e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4408e817cc15SEmil Constantinescu {
4409e817cc15SEmil Constantinescu   PetscFunctionBegin;
4410e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4411e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4412e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4413e817cc15SEmil Constantinescu }
44140026cea9SSean Farley 
44154af1b03aSJed Brown #undef __FUNCT__
44164af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
44174af1b03aSJed Brown /*@
44184af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
44194af1b03aSJed Brown 
44204af1b03aSJed Brown    Not Collective
44214af1b03aSJed Brown 
44224af1b03aSJed Brown    Input Parameter:
44234af1b03aSJed Brown .  ts - the TS context
44244af1b03aSJed Brown 
44254af1b03aSJed Brown    Output Parameter:
44264af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
44274af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
44284af1b03aSJed Brown 
4429487e0bb9SJed Brown    Level: beginner
44304af1b03aSJed Brown 
4431cd652676SJed Brown    Notes:
4432cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
44334af1b03aSJed Brown 
44344af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
44354af1b03aSJed Brown 
44364af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
44374af1b03aSJed Brown @*/
44384af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
44394af1b03aSJed Brown {
44404af1b03aSJed Brown   PetscFunctionBegin;
44414af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44424af1b03aSJed Brown   PetscValidPointer(reason,2);
44434af1b03aSJed Brown   *reason = ts->reason;
44444af1b03aSJed Brown   PetscFunctionReturn(0);
44454af1b03aSJed Brown }
44464af1b03aSJed Brown 
4447fb1732b5SBarry Smith #undef __FUNCT__
4448d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
4449d6ad946cSShri Abhyankar /*@
4450d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4451d6ad946cSShri Abhyankar 
4452d6ad946cSShri Abhyankar    Not Collective
4453d6ad946cSShri Abhyankar 
4454d6ad946cSShri Abhyankar    Input Parameter:
4455d6ad946cSShri Abhyankar +  ts - the TS context
4456d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4457d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4458d6ad946cSShri Abhyankar 
4459f5abba47SShri Abhyankar    Level: advanced
4460d6ad946cSShri Abhyankar 
4461d6ad946cSShri Abhyankar    Notes:
4462d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
4463d6ad946cSShri Abhyankar 
4464d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
4465d6ad946cSShri Abhyankar 
4466d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4467d6ad946cSShri Abhyankar @*/
4468d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4469d6ad946cSShri Abhyankar {
4470d6ad946cSShri Abhyankar   PetscFunctionBegin;
4471d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4472d6ad946cSShri Abhyankar   ts->reason = reason;
4473d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4474d6ad946cSShri Abhyankar }
4475d6ad946cSShri Abhyankar 
4476d6ad946cSShri Abhyankar #undef __FUNCT__
4477cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
4478cc708dedSBarry Smith /*@
4479cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4480cc708dedSBarry Smith 
4481cc708dedSBarry Smith    Not Collective
4482cc708dedSBarry Smith 
4483cc708dedSBarry Smith    Input Parameter:
4484cc708dedSBarry Smith .  ts - the TS context
4485cc708dedSBarry Smith 
4486cc708dedSBarry Smith    Output Parameter:
4487cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
4488cc708dedSBarry Smith 
4489487e0bb9SJed Brown    Level: beginner
4490cc708dedSBarry Smith 
4491cc708dedSBarry Smith    Notes:
4492cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4493cc708dedSBarry Smith 
4494cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
4495cc708dedSBarry Smith 
4496cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4497cc708dedSBarry Smith @*/
4498cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4499cc708dedSBarry Smith {
4500cc708dedSBarry Smith   PetscFunctionBegin;
4501cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4502cc708dedSBarry Smith   PetscValidPointer(ftime,2);
4503cc708dedSBarry Smith   *ftime = ts->solvetime;
4504cc708dedSBarry Smith   PetscFunctionReturn(0);
4505cc708dedSBarry Smith }
4506cc708dedSBarry Smith 
4507cc708dedSBarry Smith #undef __FUNCT__
45082c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps"
45092c18e0fdSBarry Smith /*@
45102c18e0fdSBarry Smith    TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate()
45112c18e0fdSBarry Smith 
45122c18e0fdSBarry Smith    Not Collective
45132c18e0fdSBarry Smith 
45142c18e0fdSBarry Smith    Input Parameter:
45152c18e0fdSBarry Smith .  ts - the TS context
45162c18e0fdSBarry Smith 
45172c18e0fdSBarry Smith    Output Parameter:
45182c18e0fdSBarry Smith .  steps - the number of steps
45192c18e0fdSBarry Smith 
45202c18e0fdSBarry Smith    Level: beginner
45212c18e0fdSBarry Smith 
45222c18e0fdSBarry Smith    Notes:
45232c18e0fdSBarry Smith    Includes the number of steps for all calls to TSSolve() since TSSetUp() was called
45242c18e0fdSBarry Smith 
45252c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test
45262c18e0fdSBarry Smith 
45272c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
45282c18e0fdSBarry Smith @*/
45292c18e0fdSBarry Smith PetscErrorCode  TSGetTotalSteps(TS ts,PetscInt *steps)
45302c18e0fdSBarry Smith {
45312c18e0fdSBarry Smith   PetscFunctionBegin;
45322c18e0fdSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45332c18e0fdSBarry Smith   PetscValidPointer(steps,2);
45342c18e0fdSBarry Smith   *steps = ts->total_steps;
45352c18e0fdSBarry Smith   PetscFunctionReturn(0);
45362c18e0fdSBarry Smith }
45372c18e0fdSBarry Smith 
45382c18e0fdSBarry Smith #undef __FUNCT__
45395ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
45409f67acb7SJed Brown /*@
45415ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
45429f67acb7SJed Brown    used by the time integrator.
45439f67acb7SJed Brown 
45449f67acb7SJed Brown    Not Collective
45459f67acb7SJed Brown 
45469f67acb7SJed Brown    Input Parameter:
45479f67acb7SJed Brown .  ts - TS context
45489f67acb7SJed Brown 
45499f67acb7SJed Brown    Output Parameter:
45509f67acb7SJed Brown .  nits - number of nonlinear iterations
45519f67acb7SJed Brown 
45529f67acb7SJed Brown    Notes:
45539f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
45549f67acb7SJed Brown 
45559f67acb7SJed Brown    Level: intermediate
45569f67acb7SJed Brown 
45579f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
45589f67acb7SJed Brown 
45595ef26d82SJed Brown .seealso:  TSGetKSPIterations()
45609f67acb7SJed Brown @*/
45615ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
45629f67acb7SJed Brown {
45639f67acb7SJed Brown   PetscFunctionBegin;
45649f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45659f67acb7SJed Brown   PetscValidIntPointer(nits,2);
45665ef26d82SJed Brown   *nits = ts->snes_its;
45679f67acb7SJed Brown   PetscFunctionReturn(0);
45689f67acb7SJed Brown }
45699f67acb7SJed Brown 
45709f67acb7SJed Brown #undef __FUNCT__
45715ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
45729f67acb7SJed Brown /*@
45735ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
45749f67acb7SJed Brown    used by the time integrator.
45759f67acb7SJed Brown 
45769f67acb7SJed Brown    Not Collective
45779f67acb7SJed Brown 
45789f67acb7SJed Brown    Input Parameter:
45799f67acb7SJed Brown .  ts - TS context
45809f67acb7SJed Brown 
45819f67acb7SJed Brown    Output Parameter:
45829f67acb7SJed Brown .  lits - number of linear iterations
45839f67acb7SJed Brown 
45849f67acb7SJed Brown    Notes:
45859f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
45869f67acb7SJed Brown 
45879f67acb7SJed Brown    Level: intermediate
45889f67acb7SJed Brown 
45899f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
45909f67acb7SJed Brown 
45915ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
45929f67acb7SJed Brown @*/
45935ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
45949f67acb7SJed Brown {
45959f67acb7SJed Brown   PetscFunctionBegin;
45969f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45979f67acb7SJed Brown   PetscValidIntPointer(lits,2);
45985ef26d82SJed Brown   *lits = ts->ksp_its;
45999f67acb7SJed Brown   PetscFunctionReturn(0);
46009f67acb7SJed Brown }
46019f67acb7SJed Brown 
46029f67acb7SJed Brown #undef __FUNCT__
4603cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
4604cef5090cSJed Brown /*@
4605cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
4606cef5090cSJed Brown 
4607cef5090cSJed Brown    Not Collective
4608cef5090cSJed Brown 
4609cef5090cSJed Brown    Input Parameter:
4610cef5090cSJed Brown .  ts - TS context
4611cef5090cSJed Brown 
4612cef5090cSJed Brown    Output Parameter:
4613cef5090cSJed Brown .  rejects - number of steps rejected
4614cef5090cSJed Brown 
4615cef5090cSJed Brown    Notes:
4616cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4617cef5090cSJed Brown 
4618cef5090cSJed Brown    Level: intermediate
4619cef5090cSJed Brown 
4620cef5090cSJed Brown .keywords: TS, get, number
4621cef5090cSJed Brown 
46225ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
4623cef5090cSJed Brown @*/
4624cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
4625cef5090cSJed Brown {
4626cef5090cSJed Brown   PetscFunctionBegin;
4627cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4628cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
4629cef5090cSJed Brown   *rejects = ts->reject;
4630cef5090cSJed Brown   PetscFunctionReturn(0);
4631cef5090cSJed Brown }
4632cef5090cSJed Brown 
4633cef5090cSJed Brown #undef __FUNCT__
4634cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
4635cef5090cSJed Brown /*@
4636cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
4637cef5090cSJed Brown 
4638cef5090cSJed Brown    Not Collective
4639cef5090cSJed Brown 
4640cef5090cSJed Brown    Input Parameter:
4641cef5090cSJed Brown .  ts - TS context
4642cef5090cSJed Brown 
4643cef5090cSJed Brown    Output Parameter:
4644cef5090cSJed Brown .  fails - number of failed nonlinear solves
4645cef5090cSJed Brown 
4646cef5090cSJed Brown    Notes:
4647cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4648cef5090cSJed Brown 
4649cef5090cSJed Brown    Level: intermediate
4650cef5090cSJed Brown 
4651cef5090cSJed Brown .keywords: TS, get, number
4652cef5090cSJed Brown 
46535ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
4654cef5090cSJed Brown @*/
4655cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
4656cef5090cSJed Brown {
4657cef5090cSJed Brown   PetscFunctionBegin;
4658cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4659cef5090cSJed Brown   PetscValidIntPointer(fails,2);
4660cef5090cSJed Brown   *fails = ts->num_snes_failures;
4661cef5090cSJed Brown   PetscFunctionReturn(0);
4662cef5090cSJed Brown }
4663cef5090cSJed Brown 
4664cef5090cSJed Brown #undef __FUNCT__
4665cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
4666cef5090cSJed Brown /*@
4667cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
4668cef5090cSJed Brown 
4669cef5090cSJed Brown    Not Collective
4670cef5090cSJed Brown 
4671cef5090cSJed Brown    Input Parameter:
4672cef5090cSJed Brown +  ts - TS context
4673cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
4674cef5090cSJed Brown 
4675cef5090cSJed Brown    Notes:
4676cef5090cSJed Brown    The counter is reset to zero for each step
4677cef5090cSJed Brown 
4678cef5090cSJed Brown    Options Database Key:
4679cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
4680cef5090cSJed Brown 
4681cef5090cSJed Brown    Level: intermediate
4682cef5090cSJed Brown 
4683cef5090cSJed Brown .keywords: TS, set, maximum, number
4684cef5090cSJed Brown 
46855ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4686cef5090cSJed Brown @*/
4687cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
4688cef5090cSJed Brown {
4689cef5090cSJed Brown   PetscFunctionBegin;
4690cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4691cef5090cSJed Brown   ts->max_reject = rejects;
4692cef5090cSJed Brown   PetscFunctionReturn(0);
4693cef5090cSJed Brown }
4694cef5090cSJed Brown 
4695cef5090cSJed Brown #undef __FUNCT__
4696cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
4697cef5090cSJed Brown /*@
4698cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
4699cef5090cSJed Brown 
4700cef5090cSJed Brown    Not Collective
4701cef5090cSJed Brown 
4702cef5090cSJed Brown    Input Parameter:
4703cef5090cSJed Brown +  ts - TS context
4704cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
4705cef5090cSJed Brown 
4706cef5090cSJed Brown    Notes:
4707cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
4708cef5090cSJed Brown 
4709cef5090cSJed Brown    Options Database Key:
4710cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
4711cef5090cSJed Brown 
4712cef5090cSJed Brown    Level: intermediate
4713cef5090cSJed Brown 
4714cef5090cSJed Brown .keywords: TS, set, maximum, number
4715cef5090cSJed Brown 
47165ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
4717cef5090cSJed Brown @*/
4718cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
4719cef5090cSJed Brown {
4720cef5090cSJed Brown   PetscFunctionBegin;
4721cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4722cef5090cSJed Brown   ts->max_snes_failures = fails;
4723cef5090cSJed Brown   PetscFunctionReturn(0);
4724cef5090cSJed Brown }
4725cef5090cSJed Brown 
4726cef5090cSJed Brown #undef __FUNCT__
47274e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails"
4728cef5090cSJed Brown /*@
4729cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
4730cef5090cSJed Brown 
4731cef5090cSJed Brown    Not Collective
4732cef5090cSJed Brown 
4733cef5090cSJed Brown    Input Parameter:
4734cef5090cSJed Brown +  ts - TS context
4735cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
4736cef5090cSJed Brown 
4737cef5090cSJed Brown    Options Database Key:
4738cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
4739cef5090cSJed Brown 
4740cef5090cSJed Brown    Level: intermediate
4741cef5090cSJed Brown 
4742cef5090cSJed Brown .keywords: TS, set, error
4743cef5090cSJed Brown 
47445ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4745cef5090cSJed Brown @*/
4746cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
4747cef5090cSJed Brown {
4748cef5090cSJed Brown   PetscFunctionBegin;
4749cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4750cef5090cSJed Brown   ts->errorifstepfailed = err;
4751cef5090cSJed Brown   PetscFunctionReturn(0);
4752cef5090cSJed Brown }
4753cef5090cSJed Brown 
4754cef5090cSJed Brown #undef __FUNCT__
4755fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
4756fb1732b5SBarry Smith /*@C
4757fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
4758fb1732b5SBarry Smith 
4759fb1732b5SBarry Smith    Collective on TS
4760fb1732b5SBarry Smith 
4761fb1732b5SBarry Smith    Input Parameters:
4762fb1732b5SBarry Smith +  ts - the TS context
4763fb1732b5SBarry Smith .  step - current time-step
4764fb1732b5SBarry Smith .  ptime - current time
47650910c330SBarry Smith .  u - current state
4766fb1732b5SBarry Smith -  viewer - binary viewer
4767fb1732b5SBarry Smith 
4768fb1732b5SBarry Smith    Level: intermediate
4769fb1732b5SBarry Smith 
4770fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
4771fb1732b5SBarry Smith 
4772fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4773fb1732b5SBarry Smith @*/
47740910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
4775fb1732b5SBarry Smith {
4776fb1732b5SBarry Smith   PetscErrorCode ierr;
4777ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
4778fb1732b5SBarry Smith 
4779fb1732b5SBarry Smith   PetscFunctionBegin;
47800910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
4781ed81e22dSJed Brown   PetscFunctionReturn(0);
4782ed81e22dSJed Brown }
4783ed81e22dSJed Brown 
4784ed81e22dSJed Brown #undef __FUNCT__
4785ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
4786ed81e22dSJed Brown /*@C
4787ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
4788ed81e22dSJed Brown 
4789ed81e22dSJed Brown    Collective on TS
4790ed81e22dSJed Brown 
4791ed81e22dSJed Brown    Input Parameters:
4792ed81e22dSJed Brown +  ts - the TS context
4793ed81e22dSJed Brown .  step - current time-step
4794ed81e22dSJed Brown .  ptime - current time
47950910c330SBarry Smith .  u - current state
4796ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4797ed81e22dSJed Brown 
4798ed81e22dSJed Brown    Level: intermediate
4799ed81e22dSJed Brown 
4800ed81e22dSJed Brown    Notes:
4801ed81e22dSJed 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.
4802ed81e22dSJed Brown    These are named according to the file name template.
4803ed81e22dSJed Brown 
4804ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
4805ed81e22dSJed Brown 
4806ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4807ed81e22dSJed Brown 
4808ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4809ed81e22dSJed Brown @*/
48100910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
4811ed81e22dSJed Brown {
4812ed81e22dSJed Brown   PetscErrorCode ierr;
4813ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
4814ed81e22dSJed Brown   PetscViewer    viewer;
4815ed81e22dSJed Brown 
4816ed81e22dSJed Brown   PetscFunctionBegin;
48178caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
4818ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
48190910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
4820ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4821ed81e22dSJed Brown   PetscFunctionReturn(0);
4822ed81e22dSJed Brown }
4823ed81e22dSJed Brown 
4824ed81e22dSJed Brown #undef __FUNCT__
4825ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
4826ed81e22dSJed Brown /*@C
4827ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
4828ed81e22dSJed Brown 
4829ed81e22dSJed Brown    Collective on TS
4830ed81e22dSJed Brown 
4831ed81e22dSJed Brown    Input Parameters:
4832ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4833ed81e22dSJed Brown 
4834ed81e22dSJed Brown    Level: intermediate
4835ed81e22dSJed Brown 
4836ed81e22dSJed Brown    Note:
4837ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
4838ed81e22dSJed Brown 
4839ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4840ed81e22dSJed Brown 
4841ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
4842ed81e22dSJed Brown @*/
4843ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
4844ed81e22dSJed Brown {
4845ed81e22dSJed Brown   PetscErrorCode ierr;
4846ed81e22dSJed Brown 
4847ed81e22dSJed Brown   PetscFunctionBegin;
4848ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
4849fb1732b5SBarry Smith   PetscFunctionReturn(0);
4850fb1732b5SBarry Smith }
4851fb1732b5SBarry Smith 
485284df9cb4SJed Brown #undef __FUNCT__
4853552698daSJed Brown #define __FUNCT__ "TSGetAdapt"
485484df9cb4SJed Brown /*@
4855552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
485684df9cb4SJed Brown 
4857ed81e22dSJed Brown    Collective on TS if controller has not been created yet
485884df9cb4SJed Brown 
485984df9cb4SJed Brown    Input Arguments:
4860ed81e22dSJed Brown .  ts - time stepping context
486184df9cb4SJed Brown 
486284df9cb4SJed Brown    Output Arguments:
4863ed81e22dSJed Brown .  adapt - adaptive controller
486484df9cb4SJed Brown 
486584df9cb4SJed Brown    Level: intermediate
486684df9cb4SJed Brown 
4867ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
486884df9cb4SJed Brown @*/
4869552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
487084df9cb4SJed Brown {
487184df9cb4SJed Brown   PetscErrorCode ierr;
487284df9cb4SJed Brown 
487384df9cb4SJed Brown   PetscFunctionBegin;
487484df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
487584df9cb4SJed Brown   PetscValidPointer(adapt,2);
487684df9cb4SJed Brown   if (!ts->adapt) {
4877ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
48783bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
48791c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
488084df9cb4SJed Brown   }
488184df9cb4SJed Brown   *adapt = ts->adapt;
488284df9cb4SJed Brown   PetscFunctionReturn(0);
488384df9cb4SJed Brown }
4884d6ebe24aSShri Abhyankar 
4885d6ebe24aSShri Abhyankar #undef __FUNCT__
48861c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
48871c3436cfSJed Brown /*@
48881c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
48891c3436cfSJed Brown 
48901c3436cfSJed Brown    Logically Collective
48911c3436cfSJed Brown 
48921c3436cfSJed Brown    Input Arguments:
48931c3436cfSJed Brown +  ts - time integration context
48941c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
48950298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
48961c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
48970298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
48981c3436cfSJed Brown 
4899a3cdaa26SBarry Smith    Options Database keys:
4900a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
4901a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
4902a3cdaa26SBarry Smith 
49031c3436cfSJed Brown    Level: beginner
49041c3436cfSJed Brown 
4905c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
49061c3436cfSJed Brown @*/
49071c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
49081c3436cfSJed Brown {
49091c3436cfSJed Brown   PetscErrorCode ierr;
49101c3436cfSJed Brown 
49111c3436cfSJed Brown   PetscFunctionBegin;
4912c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
49131c3436cfSJed Brown   if (vatol) {
49141c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
49151c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
4916bbd56ea5SKarl Rupp 
49171c3436cfSJed Brown     ts->vatol = vatol;
49181c3436cfSJed Brown   }
4919c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
49201c3436cfSJed Brown   if (vrtol) {
49211c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
49221c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
4923bbd56ea5SKarl Rupp 
49241c3436cfSJed Brown     ts->vrtol = vrtol;
49251c3436cfSJed Brown   }
49261c3436cfSJed Brown   PetscFunctionReturn(0);
49271c3436cfSJed Brown }
49281c3436cfSJed Brown 
49291c3436cfSJed Brown #undef __FUNCT__
4930c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
4931c5033834SJed Brown /*@
4932c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
4933c5033834SJed Brown 
4934c5033834SJed Brown    Logically Collective
4935c5033834SJed Brown 
4936c5033834SJed Brown    Input Arguments:
4937c5033834SJed Brown .  ts - time integration context
4938c5033834SJed Brown 
4939c5033834SJed Brown    Output Arguments:
49400298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
49410298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
49420298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
49430298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
4944c5033834SJed Brown 
4945c5033834SJed Brown    Level: beginner
4946c5033834SJed Brown 
4947c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
4948c5033834SJed Brown @*/
4949c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
4950c5033834SJed Brown {
4951c5033834SJed Brown   PetscFunctionBegin;
4952c5033834SJed Brown   if (atol)  *atol  = ts->atol;
4953c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
4954c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
4955c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
4956c5033834SJed Brown   PetscFunctionReturn(0);
4957c5033834SJed Brown }
4958c5033834SJed Brown 
4959c5033834SJed Brown #undef __FUNCT__
4960b29392e1SShri #define __FUNCT__ "TSSetDifferentialEquationsIS"
4961b29392e1SShri /*@
4962b29392e1SShri    TSSetDifferentialEquationsIS - Sets an IS containing locations of differential equations in a DAE
4963b29392e1SShri 
4964b29392e1SShri    Not Collective
4965b29392e1SShri 
4966b29392e1SShri    Input Arguments:
4967b29392e1SShri +  ts - time stepping context
4968b29392e1SShri -  is_diff - Index set for differential equations
4969b29392e1SShri 
4970b29392e1SShri    Level: advanced
4971b29392e1SShri 
4972b29392e1SShri @*/
4973b29392e1SShri PetscErrorCode TSSetDifferentialEquationsIS(TS ts, IS is_diff)
4974b29392e1SShri {
4975b29392e1SShri   PetscErrorCode ierr;
4976b29392e1SShri 
4977b29392e1SShri   PetscFunctionBegin;
4978b29392e1SShri   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4979b29392e1SShri   PetscValidHeaderSpecific(is_diff,IS_CLASSID,2);
4980b29392e1SShri   PetscCheckSameComm(ts,1,is_diff,2);
4981b29392e1SShri   ierr = PetscObjectReference((PetscObject)is_diff);CHKERRQ(ierr);
4982b29392e1SShri   ierr = ISDestroy(&ts->is_diff);CHKERRQ(ierr);
4983b29392e1SShri   ts->is_diff = is_diff;
4984b29392e1SShri   PetscFunctionReturn(0);
4985b29392e1SShri }
4986b29392e1SShri 
4987b29392e1SShri #undef __FUNCT__
49889c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNorm2"
49899c6b16b5SShri Abhyankar /*@
49909c6b16b5SShri Abhyankar    TSErrorWeightedNorm2 - compute a weighted 2-norm of the difference between a vector and the current state
49919c6b16b5SShri Abhyankar 
49929c6b16b5SShri Abhyankar    Collective on TS
49939c6b16b5SShri Abhyankar 
49949c6b16b5SShri Abhyankar    Input Arguments:
49959c6b16b5SShri Abhyankar +  ts - time stepping context
49969c6b16b5SShri Abhyankar -  Y - state vector to be compared to ts->vec_sol
49979c6b16b5SShri Abhyankar 
49989c6b16b5SShri Abhyankar    Output Arguments:
49999c6b16b5SShri Abhyankar .  norm - weighted norm, a value of 1.0 is considered small
50009c6b16b5SShri Abhyankar 
50019c6b16b5SShri Abhyankar    Level: developer
50029c6b16b5SShri Abhyankar 
5003deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNormInfinity()
50049c6b16b5SShri Abhyankar @*/
50059c6b16b5SShri Abhyankar PetscErrorCode TSErrorWeightedNorm2(TS ts,Vec Y,PetscReal *norm)
50069c6b16b5SShri Abhyankar {
50079c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
50089c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart;
50099c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
50109c6b16b5SShri Abhyankar   Vec               U;
50119c6b16b5SShri Abhyankar   PetscReal         sum,gsum;
50129c6b16b5SShri Abhyankar   PetscReal         tol;
50139c6b16b5SShri Abhyankar 
50149c6b16b5SShri Abhyankar   PetscFunctionBegin;
50159c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50169c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
50179c6b16b5SShri Abhyankar   PetscValidPointer(norm,3);
50189c6b16b5SShri Abhyankar   U = ts->vec_sol;
50199c6b16b5SShri Abhyankar   PetscCheckSameTypeAndComm(U,1,Y,2);
50209c6b16b5SShri Abhyankar   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
50219c6b16b5SShri Abhyankar 
50229c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
50239c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
50249c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
50259c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
50269c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
50279c6b16b5SShri Abhyankar   sum  = 0.;
50289c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
50299c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
50309c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
50319c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
50329c6b16b5SShri Abhyankar     if(ts->is_diff) {
50339c6b16b5SShri Abhyankar       const PetscInt *idx;
50349c6b16b5SShri Abhyankar       PetscInt k;
50359c6b16b5SShri Abhyankar       ierr = ISGetIndices(ts->is_diff,&idx);CHKERRQ(ierr);
50369c6b16b5SShri Abhyankar       ierr = ISGetLocalSize(ts->is_diff,&n);CHKERRQ(ierr);
50379c6b16b5SShri Abhyankar       for(i=0; i < n; i++) {
50389c6b16b5SShri Abhyankar 	k = idx[i] - rstart;
50399c6b16b5SShri Abhyankar 	tol = PetscRealPart(atol[k]) + PetscRealPart(rtol[k])*PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
50409c6b16b5SShri Abhyankar 	sum += PetscSqr(PetscAbsScalar(y[k] - u[k]) / tol);
50419c6b16b5SShri Abhyankar       }
50429c6b16b5SShri Abhyankar       ierr = ISRestoreIndices(ts->is_diff,&idx);CHKERRQ(ierr);
50439c6b16b5SShri Abhyankar     } else {
50449c6b16b5SShri Abhyankar       for (i=0; i<n; i++) {
50459c6b16b5SShri Abhyankar 	tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
50469c6b16b5SShri Abhyankar 	sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
50479c6b16b5SShri Abhyankar       }
50489c6b16b5SShri Abhyankar     }
50499c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
50509c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
50519c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
50529c6b16b5SShri Abhyankar     const PetscScalar *atol;
50539c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
50549c6b16b5SShri Abhyankar     if(ts->is_diff) {
50559c6b16b5SShri Abhyankar       const PetscInt *idx;
50569c6b16b5SShri Abhyankar       PetscInt k;
50579c6b16b5SShri Abhyankar       ierr = ISGetIndices(ts->is_diff,&idx);CHKERRQ(ierr);
50589c6b16b5SShri Abhyankar       ierr = ISGetLocalSize(ts->is_diff,&n);CHKERRQ(ierr);
50599c6b16b5SShri Abhyankar       for(i=0; i < n; i++) {
50609c6b16b5SShri Abhyankar 	k = idx[i] - rstart;
50619c6b16b5SShri Abhyankar 	tol = PetscRealPart(atol[k]) + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
50629c6b16b5SShri Abhyankar 	sum += PetscSqr(PetscAbsScalar(y[k] - u[k]) / tol);
50639c6b16b5SShri Abhyankar       }
50649c6b16b5SShri Abhyankar       ierr = ISRestoreIndices(ts->is_diff,&idx);CHKERRQ(ierr);
50659c6b16b5SShri Abhyankar     } else {
50669c6b16b5SShri Abhyankar       for (i=0; i<n; i++) {
50679c6b16b5SShri Abhyankar 	tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
50689c6b16b5SShri Abhyankar 	sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
50699c6b16b5SShri Abhyankar       }
50709c6b16b5SShri Abhyankar     }
50719c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
50729c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
50739c6b16b5SShri Abhyankar     const PetscScalar *rtol;
50749c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
50759c6b16b5SShri Abhyankar     if(ts->is_diff) {
50769c6b16b5SShri Abhyankar       const PetscInt *idx;
50779c6b16b5SShri Abhyankar       PetscInt k;
50789c6b16b5SShri Abhyankar       ierr = ISGetIndices(ts->is_diff,&idx);CHKERRQ(ierr);
50799c6b16b5SShri Abhyankar       ierr = ISGetLocalSize(ts->is_diff,&n);CHKERRQ(ierr);
50809c6b16b5SShri Abhyankar       for(i=0; i < n; i++) {
50819c6b16b5SShri Abhyankar 	k = idx[i] - rstart;
50829c6b16b5SShri Abhyankar 	tol = ts->atol + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
50839c6b16b5SShri Abhyankar 	sum += PetscSqr(PetscAbsScalar(y[k] - u[k]) / tol);
50849c6b16b5SShri Abhyankar       }
50859c6b16b5SShri Abhyankar       ierr = ISRestoreIndices(ts->is_diff,&idx);CHKERRQ(ierr);
50869c6b16b5SShri Abhyankar     } else {
50879c6b16b5SShri Abhyankar       for (i=0; i<n; i++) {
50889c6b16b5SShri Abhyankar 	tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
50899c6b16b5SShri Abhyankar 	sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
50909c6b16b5SShri Abhyankar       }
50919c6b16b5SShri Abhyankar     }
50929c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
50939c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
50949c6b16b5SShri Abhyankar     if (ts->is_diff) {
50959c6b16b5SShri Abhyankar       const PetscInt *idx;
50969c6b16b5SShri Abhyankar       PetscInt k;
50979c6b16b5SShri Abhyankar       ierr = ISGetIndices(ts->is_diff,&idx);CHKERRQ(ierr);
50989c6b16b5SShri Abhyankar       ierr = ISGetLocalSize(ts->is_diff,&n);CHKERRQ(ierr);
50999c6b16b5SShri Abhyankar       for (i=0; i<n; i++) {
51009c6b16b5SShri Abhyankar 	k = idx[i] - rstart;
51019c6b16b5SShri Abhyankar 	tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
51029c6b16b5SShri Abhyankar 	sum += PetscSqr(PetscAbsScalar(y[k] - u[k]) / tol);
51039c6b16b5SShri Abhyankar       }
51049c6b16b5SShri Abhyankar     } else {
51059c6b16b5SShri Abhyankar       for (i=0; i<n; i++) {
51069c6b16b5SShri Abhyankar 	tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
51079c6b16b5SShri Abhyankar 	sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
51089c6b16b5SShri Abhyankar       }
51099c6b16b5SShri Abhyankar     }
51109c6b16b5SShri Abhyankar   }
51119c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
51129c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
51139c6b16b5SShri Abhyankar 
51149c6b16b5SShri Abhyankar   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
51159c6b16b5SShri Abhyankar   *norm = PetscSqrtReal(gsum / N);
51169c6b16b5SShri Abhyankar 
51179c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
51189c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
51199c6b16b5SShri Abhyankar }
51209c6b16b5SShri Abhyankar 
51219c6b16b5SShri Abhyankar #undef __FUNCT__
51229c6b16b5SShri Abhyankar #define __FUNCT__ "TSErrorWeightedNormInfinity"
51239c6b16b5SShri Abhyankar /*@
51249c6b16b5SShri Abhyankar    TSErrorWeightedNormInfinity - compute a weighted infinity-norm of the difference between a vector and the current state
51259c6b16b5SShri Abhyankar 
51269c6b16b5SShri Abhyankar    Collective on TS
51279c6b16b5SShri Abhyankar 
51289c6b16b5SShri Abhyankar    Input Arguments:
51299c6b16b5SShri Abhyankar +  ts - time stepping context
51309c6b16b5SShri Abhyankar -  Y - state vector to be compared to ts->vec_sol
51319c6b16b5SShri Abhyankar 
51329c6b16b5SShri Abhyankar    Output Arguments:
51339c6b16b5SShri Abhyankar .  norm - weighted norm, a value of 1.0 is considered small
51349c6b16b5SShri Abhyankar 
51359c6b16b5SShri Abhyankar    Level: developer
51369c6b16b5SShri Abhyankar 
5137deea92deSShri .seealso: TSErrorWeightedNorm(), TSErrorWeightedNorm2()
51389c6b16b5SShri Abhyankar @*/
51399c6b16b5SShri Abhyankar PetscErrorCode TSErrorWeightedNormInfinity(TS ts,Vec Y,PetscReal *norm)
51409c6b16b5SShri Abhyankar {
51419c6b16b5SShri Abhyankar   PetscErrorCode    ierr;
51429c6b16b5SShri Abhyankar   PetscInt          i,n,N,rstart,k;
51439c6b16b5SShri Abhyankar   const PetscScalar *u,*y;
51449c6b16b5SShri Abhyankar   Vec               U;
51459c6b16b5SShri Abhyankar   PetscReal         max,gmax;
51469c6b16b5SShri Abhyankar   PetscReal         tol;
51479c6b16b5SShri Abhyankar 
51489c6b16b5SShri Abhyankar   PetscFunctionBegin;
51499c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
51509c6b16b5SShri Abhyankar   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
51519c6b16b5SShri Abhyankar   PetscValidPointer(norm,3);
51529c6b16b5SShri Abhyankar   U = ts->vec_sol;
51539c6b16b5SShri Abhyankar   PetscCheckSameTypeAndComm(U,1,Y,2);
51549c6b16b5SShri Abhyankar   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
51559c6b16b5SShri Abhyankar 
51569c6b16b5SShri Abhyankar   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
51579c6b16b5SShri Abhyankar   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
51589c6b16b5SShri Abhyankar   ierr = VecGetOwnershipRange(U,&rstart,NULL);CHKERRQ(ierr);
51599c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
51609c6b16b5SShri Abhyankar   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
51619c6b16b5SShri Abhyankar   if (ts->vatol && ts->vrtol) {
51629c6b16b5SShri Abhyankar     const PetscScalar *atol,*rtol;
51639c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
51649c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
51659c6b16b5SShri Abhyankar     if(ts->is_diff) {
51669c6b16b5SShri Abhyankar       const PetscInt *idx;
51679c6b16b5SShri Abhyankar       ierr = ISGetIndices(ts->is_diff,&idx);CHKERRQ(ierr);
51689c6b16b5SShri Abhyankar       ierr = ISGetLocalSize(ts->is_diff,&n);CHKERRQ(ierr);
51699c6b16b5SShri Abhyankar 
51709c6b16b5SShri Abhyankar       k = idx[0];
51719c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[k]) + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
51729c6b16b5SShri Abhyankar       max = PetscAbsScalar(y[k] - u[k]) / tol;
51739c6b16b5SShri Abhyankar       for(i=1; i < n; i++) {
51749c6b16b5SShri Abhyankar 	k = idx[i] - rstart;
51759c6b16b5SShri Abhyankar 	tol = PetscRealPart(atol[k]) + PetscRealPart(rtol[k])*PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
51769c6b16b5SShri Abhyankar 	max = PetscMax(max,PetscAbsScalar(y[k] - u[k]) / tol);
51779c6b16b5SShri Abhyankar       }
51789c6b16b5SShri Abhyankar       ierr = ISRestoreIndices(ts->is_diff,&idx);CHKERRQ(ierr);
51799c6b16b5SShri Abhyankar     } else {
51809c6b16b5SShri Abhyankar       k = 0;
51819c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[k]) + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
51829c6b16b5SShri Abhyankar       max = PetscAbsScalar(y[k] - u[k]) / tol;
51839c6b16b5SShri Abhyankar       for (i=1; i<n; i++) {
51849c6b16b5SShri Abhyankar 	tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
51859c6b16b5SShri Abhyankar 	max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
51869c6b16b5SShri Abhyankar       }
51879c6b16b5SShri Abhyankar     }
51889c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
51899c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
51909c6b16b5SShri Abhyankar   } else if (ts->vatol) {       /* vector atol, scalar rtol */
51919c6b16b5SShri Abhyankar     const PetscScalar *atol;
51929c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
51939c6b16b5SShri Abhyankar     if(ts->is_diff) {
51949c6b16b5SShri Abhyankar       const PetscInt *idx;
51959c6b16b5SShri Abhyankar       ierr = ISGetIndices(ts->is_diff,&idx);CHKERRQ(ierr);
51969c6b16b5SShri Abhyankar       ierr = ISGetLocalSize(ts->is_diff,&n);CHKERRQ(ierr);
51979c6b16b5SShri Abhyankar 
51989c6b16b5SShri Abhyankar       k = idx[0];
51999c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[k]) + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
52009c6b16b5SShri Abhyankar       max = PetscAbsScalar(y[k] - u[k]) / tol;
52019c6b16b5SShri Abhyankar       for(i=1; i < n; i++) {
52029c6b16b5SShri Abhyankar 	k = idx[i] - rstart;
52039c6b16b5SShri Abhyankar 	tol = PetscRealPart(atol[k]) + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
52049c6b16b5SShri Abhyankar 	max = PetscMax(max,PetscAbsScalar(y[k] - u[k]) / tol);
52059c6b16b5SShri Abhyankar       }
52069c6b16b5SShri Abhyankar       ierr = ISRestoreIndices(ts->is_diff,&idx);CHKERRQ(ierr);
52079c6b16b5SShri Abhyankar     } else {
52089c6b16b5SShri Abhyankar       k = 0;
52099c6b16b5SShri Abhyankar       tol = PetscRealPart(atol[k]) + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
52109c6b16b5SShri Abhyankar       max = PetscAbsScalar(y[k] - u[k]) / tol;
52119c6b16b5SShri Abhyankar       for (i=1; i<n; i++) {
52129c6b16b5SShri Abhyankar 	tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
52139c6b16b5SShri Abhyankar         max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
52149c6b16b5SShri Abhyankar       }
52159c6b16b5SShri Abhyankar     }
52169c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
52179c6b16b5SShri Abhyankar   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
52189c6b16b5SShri Abhyankar     const PetscScalar *rtol;
52199c6b16b5SShri Abhyankar     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
52209c6b16b5SShri Abhyankar     if(ts->is_diff) {
52219c6b16b5SShri Abhyankar       const PetscInt *idx;
52229c6b16b5SShri Abhyankar       ierr = ISGetIndices(ts->is_diff,&idx);CHKERRQ(ierr);
52239c6b16b5SShri Abhyankar       ierr = ISGetLocalSize(ts->is_diff,&n);CHKERRQ(ierr);
52249c6b16b5SShri Abhyankar 
52259c6b16b5SShri Abhyankar       k = idx[0];
52269c6b16b5SShri Abhyankar       tol = ts->atol + PetscRealPart(rtol[k])*PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
52279c6b16b5SShri Abhyankar       max = PetscAbsScalar(y[k] - u[k]) / tol;
52289c6b16b5SShri Abhyankar       for(i=1; i < n; i++) {
52299c6b16b5SShri Abhyankar 	k = idx[i] - rstart;
52309c6b16b5SShri Abhyankar 	tol = ts->atol + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
52319c6b16b5SShri Abhyankar 	max = PetscMax(max,PetscAbsScalar(y[k] - u[k]) / tol);
52329c6b16b5SShri Abhyankar       }
52339c6b16b5SShri Abhyankar       ierr = ISRestoreIndices(ts->is_diff,&idx);CHKERRQ(ierr);
52349c6b16b5SShri Abhyankar     } else {
52359c6b16b5SShri Abhyankar       k = 0;
52369c6b16b5SShri Abhyankar       tol = ts->atol + PetscRealPart(rtol[k]) * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
52379c6b16b5SShri Abhyankar       max = PetscAbsScalar(y[k] - u[k]) / tol;
52389c6b16b5SShri Abhyankar       for (i=1; i<n; i++) {
52399c6b16b5SShri Abhyankar 	tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
52409c6b16b5SShri Abhyankar 	max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
52419c6b16b5SShri Abhyankar       }
52429c6b16b5SShri Abhyankar     }
52439c6b16b5SShri Abhyankar     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
52449c6b16b5SShri Abhyankar   } else {                      /* scalar atol, scalar rtol */
52459c6b16b5SShri Abhyankar     if (ts->is_diff) {
52469c6b16b5SShri Abhyankar       const PetscInt *idx;
52479c6b16b5SShri Abhyankar       ierr = ISGetIndices(ts->is_diff,&idx);CHKERRQ(ierr);
52489c6b16b5SShri Abhyankar       ierr = ISGetLocalSize(ts->is_diff,&n);CHKERRQ(ierr);
52499c6b16b5SShri Abhyankar 
52509c6b16b5SShri Abhyankar       k = idx[0];
52519c6b16b5SShri Abhyankar       tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
52529c6b16b5SShri Abhyankar       max = PetscAbsScalar(y[k] - u[k]) / tol;
52539c6b16b5SShri Abhyankar       for (i=1; i<n; i++) {
52549c6b16b5SShri Abhyankar 	k = idx[i] - rstart;
52559c6b16b5SShri Abhyankar 	tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
52569c6b16b5SShri Abhyankar 	max = PetscMax(max,PetscAbsScalar(y[k] - u[k]) / tol);
52579c6b16b5SShri Abhyankar       }
52589c6b16b5SShri Abhyankar       ierr = ISRestoreIndices(ts->is_diff,&idx);CHKERRQ(ierr);
52599c6b16b5SShri Abhyankar     } else {
52609c6b16b5SShri Abhyankar       k = 0;
52619c6b16b5SShri Abhyankar       tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[k]),PetscAbsScalar(y[k]));
52629c6b16b5SShri Abhyankar       max = PetscAbsScalar(y[k] - u[k]) / tol;
52639c6b16b5SShri Abhyankar       for (i=1; i<n; i++) {
52649c6b16b5SShri Abhyankar 	tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
52659c6b16b5SShri Abhyankar 	max = PetscMax(max,PetscAbsScalar(y[i] - u[i]) / tol);
52669c6b16b5SShri Abhyankar       }
52679c6b16b5SShri Abhyankar     }
52689c6b16b5SShri Abhyankar   }
52699c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
52709c6b16b5SShri Abhyankar   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
52719c6b16b5SShri Abhyankar 
52729c6b16b5SShri Abhyankar   ierr  = MPI_Allreduce(&max,&gmax,1,MPIU_REAL,MPIU_MAX,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
52739c6b16b5SShri Abhyankar   *norm = gmax;
52749c6b16b5SShri Abhyankar 
52759c6b16b5SShri Abhyankar   if (PetscIsInfOrNanScalar(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
52769c6b16b5SShri Abhyankar   PetscFunctionReturn(0);
52779c6b16b5SShri Abhyankar }
52789c6b16b5SShri Abhyankar 
52799c6b16b5SShri Abhyankar #undef __FUNCT__
52807619abb3SShri #define __FUNCT__ "TSErrorWeightedNorm"
52811c3436cfSJed Brown /*@
52827619abb3SShri    TSErrorWeightedNorm - compute a weighted norm of the difference between a vector and the current state
52831c3436cfSJed Brown 
52841c3436cfSJed Brown    Collective on TS
52851c3436cfSJed Brown 
52861c3436cfSJed Brown    Input Arguments:
52871c3436cfSJed Brown +  ts - time stepping context
52881c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
52891c3436cfSJed Brown 
52907619abb3SShri    Options Database Keys:
52917619abb3SShri .  -ts_adapt_wnormtype <wnormtype> - 2, INFINITY
52927619abb3SShri 
52931c3436cfSJed Brown    Output Arguments:
52941c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
52951c3436cfSJed Brown 
52961c3436cfSJed Brown    Level: developer
52971c3436cfSJed Brown 
5298deea92deSShri .seealso: TSErrorWeightedNormInfinity(), TSErrorWeightedNorm2()
52991c3436cfSJed Brown @*/
53007619abb3SShri PetscErrorCode TSErrorWeightedNorm(TS ts,Vec Y,PetscReal *norm)
53011c3436cfSJed Brown {
53021c3436cfSJed Brown 
53031c3436cfSJed Brown   PetscFunctionBegin;
53041c3436cfSJed Brown 
53057619abb3SShri   if(ts->adapt->wnormtype == NORM_2) {
53063ef54c4aSShri Abhyankar     PetscErrorCode    ierr;
53079c6b16b5SShri Abhyankar     ierr = TSErrorWeightedNorm2(ts,Y,norm);
53089c6b16b5SShri Abhyankar   } else if(ts->adapt->wnormtype == NORM_INFINITY) {
53093ef54c4aSShri Abhyankar     PetscErrorCode    ierr;
53109c6b16b5SShri Abhyankar     ierr = TSErrorWeightedNormInfinity(ts,Y,norm);
53117619abb3SShri   }
53127619abb3SShri 
53131c3436cfSJed Brown   PetscFunctionReturn(0);
53141c3436cfSJed Brown }
53151c3436cfSJed Brown 
53169c6b16b5SShri Abhyankar 
53171c3436cfSJed Brown #undef __FUNCT__
53188d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
53198d59e960SJed Brown /*@
53208d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
53218d59e960SJed Brown 
53228d59e960SJed Brown    Logically Collective on TS
53238d59e960SJed Brown 
53248d59e960SJed Brown    Input Arguments:
53258d59e960SJed Brown +  ts - time stepping context
53268d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
53278d59e960SJed Brown 
53288d59e960SJed Brown    Note:
53298d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
53308d59e960SJed Brown 
53318d59e960SJed Brown    Level: intermediate
53328d59e960SJed Brown 
53338d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
53348d59e960SJed Brown @*/
53358d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
53368d59e960SJed Brown {
53378d59e960SJed Brown   PetscFunctionBegin;
53388d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53398d59e960SJed Brown   ts->cfltime_local = cfltime;
53408d59e960SJed Brown   ts->cfltime       = -1.;
53418d59e960SJed Brown   PetscFunctionReturn(0);
53428d59e960SJed Brown }
53438d59e960SJed Brown 
53448d59e960SJed Brown #undef __FUNCT__
53458d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
53468d59e960SJed Brown /*@
53478d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
53488d59e960SJed Brown 
53498d59e960SJed Brown    Collective on TS
53508d59e960SJed Brown 
53518d59e960SJed Brown    Input Arguments:
53528d59e960SJed Brown .  ts - time stepping context
53538d59e960SJed Brown 
53548d59e960SJed Brown    Output Arguments:
53558d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
53568d59e960SJed Brown 
53578d59e960SJed Brown    Level: advanced
53588d59e960SJed Brown 
53598d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
53608d59e960SJed Brown @*/
53618d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
53628d59e960SJed Brown {
53638d59e960SJed Brown   PetscErrorCode ierr;
53648d59e960SJed Brown 
53658d59e960SJed Brown   PetscFunctionBegin;
53668d59e960SJed Brown   if (ts->cfltime < 0) {
5367ce94432eSBarry Smith     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
53688d59e960SJed Brown   }
53698d59e960SJed Brown   *cfltime = ts->cfltime;
53708d59e960SJed Brown   PetscFunctionReturn(0);
53718d59e960SJed Brown }
53728d59e960SJed Brown 
53738d59e960SJed Brown #undef __FUNCT__
5374d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
5375d6ebe24aSShri Abhyankar /*@
5376d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
5377d6ebe24aSShri Abhyankar 
5378d6ebe24aSShri Abhyankar    Input Parameters:
5379d6ebe24aSShri Abhyankar .  ts   - the TS context.
5380d6ebe24aSShri Abhyankar .  xl   - lower bound.
5381d6ebe24aSShri Abhyankar .  xu   - upper bound.
5382d6ebe24aSShri Abhyankar 
5383d6ebe24aSShri Abhyankar    Notes:
5384d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
5385e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
5386d6ebe24aSShri Abhyankar 
53872bd2b0e6SSatish Balay    Level: advanced
53882bd2b0e6SSatish Balay 
5389d6ebe24aSShri Abhyankar @*/
5390d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
5391d6ebe24aSShri Abhyankar {
5392d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
5393d6ebe24aSShri Abhyankar   SNES           snes;
5394d6ebe24aSShri Abhyankar 
5395d6ebe24aSShri Abhyankar   PetscFunctionBegin;
5396d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
5397d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
5398d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
5399d6ebe24aSShri Abhyankar }
5400d6ebe24aSShri Abhyankar 
5401325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
5402c6db04a5SJed Brown #include <mex.h>
5403325fc9f4SBarry Smith 
5404325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
5405325fc9f4SBarry Smith 
5406325fc9f4SBarry Smith #undef __FUNCT__
5407325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
5408325fc9f4SBarry Smith /*
5409325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
5410325fc9f4SBarry Smith                          TSSetFunctionMatlab().
5411325fc9f4SBarry Smith 
5412325fc9f4SBarry Smith    Collective on TS
5413325fc9f4SBarry Smith 
5414325fc9f4SBarry Smith    Input Parameters:
5415325fc9f4SBarry Smith +  snes - the TS context
54160910c330SBarry Smith -  u - input vector
5417325fc9f4SBarry Smith 
5418325fc9f4SBarry Smith    Output Parameter:
5419325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
5420325fc9f4SBarry Smith 
5421325fc9f4SBarry Smith    Notes:
5422325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
5423325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
5424325fc9f4SBarry Smith    themselves.
5425325fc9f4SBarry Smith 
5426325fc9f4SBarry Smith    Level: developer
5427325fc9f4SBarry Smith 
5428325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5429325fc9f4SBarry Smith 
5430325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5431325fc9f4SBarry Smith */
54320910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
5433325fc9f4SBarry Smith {
5434325fc9f4SBarry Smith   PetscErrorCode  ierr;
5435325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5436325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
5437325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
5438325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
5439325fc9f4SBarry Smith 
5440325fc9f4SBarry Smith   PetscFunctionBegin;
5441325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
54420910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
54430910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
5444325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
54450910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
5446325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
5447325fc9f4SBarry Smith 
5448325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
54490910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
54500910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
54510910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
5452bbd56ea5SKarl Rupp 
5453325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5454325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
5455325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5456325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5457325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
5458325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
5459325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
5460325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
5461325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5462325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5463325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5464325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5465325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5466325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5467325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5468325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5469325fc9f4SBarry Smith   PetscFunctionReturn(0);
5470325fc9f4SBarry Smith }
5471325fc9f4SBarry Smith 
5472325fc9f4SBarry Smith 
5473325fc9f4SBarry Smith #undef __FUNCT__
5474325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
5475325fc9f4SBarry Smith /*
5476325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
5477325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
5478e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
5479325fc9f4SBarry Smith 
5480325fc9f4SBarry Smith    Logically Collective on TS
5481325fc9f4SBarry Smith 
5482325fc9f4SBarry Smith    Input Parameters:
5483325fc9f4SBarry Smith +  ts - the TS context
5484325fc9f4SBarry Smith -  func - function evaluation routine
5485325fc9f4SBarry Smith 
5486325fc9f4SBarry Smith    Calling sequence of func:
54870910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
5488325fc9f4SBarry Smith 
5489325fc9f4SBarry Smith    Level: beginner
5490325fc9f4SBarry Smith 
5491325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5492325fc9f4SBarry Smith 
5493325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5494325fc9f4SBarry Smith */
5495cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
5496325fc9f4SBarry Smith {
5497325fc9f4SBarry Smith   PetscErrorCode  ierr;
5498325fc9f4SBarry Smith   TSMatlabContext *sctx;
5499325fc9f4SBarry Smith 
5500325fc9f4SBarry Smith   PetscFunctionBegin;
5501325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5502325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5503325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5504325fc9f4SBarry Smith   /*
5505325fc9f4SBarry Smith      This should work, but it doesn't
5506325fc9f4SBarry Smith   sctx->ctx = ctx;
5507325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5508325fc9f4SBarry Smith   */
5509325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5510bbd56ea5SKarl Rupp 
55110298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
5512325fc9f4SBarry Smith   PetscFunctionReturn(0);
5513325fc9f4SBarry Smith }
5514325fc9f4SBarry Smith 
5515325fc9f4SBarry Smith #undef __FUNCT__
5516325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
5517325fc9f4SBarry Smith /*
5518325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
5519325fc9f4SBarry Smith                          TSSetJacobianMatlab().
5520325fc9f4SBarry Smith 
5521325fc9f4SBarry Smith    Collective on TS
5522325fc9f4SBarry Smith 
5523325fc9f4SBarry Smith    Input Parameters:
5524cdcf91faSSean Farley +  ts - the TS context
55250910c330SBarry Smith .  u - input vector
5526325fc9f4SBarry Smith .  A, B - the matrices
5527325fc9f4SBarry Smith -  ctx - user context
5528325fc9f4SBarry Smith 
5529325fc9f4SBarry Smith    Level: developer
5530325fc9f4SBarry Smith 
5531325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5532325fc9f4SBarry Smith 
5533325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5534325fc9f4SBarry Smith @*/
5535f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
5536325fc9f4SBarry Smith {
5537325fc9f4SBarry Smith   PetscErrorCode  ierr;
5538325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5539325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
5540325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
5541325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
5542325fc9f4SBarry Smith 
5543325fc9f4SBarry Smith   PetscFunctionBegin;
5544cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
55450910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
5546325fc9f4SBarry Smith 
55470910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
5548325fc9f4SBarry Smith 
5549cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
55500910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
55510910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
55520910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
55530910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
5554bbd56ea5SKarl Rupp 
5555325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5556325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
5557325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5558325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5559325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
5560325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
5561325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
5562325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
5563325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
5564325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
5565325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5566325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5567325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5568325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5569325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5570325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5571325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5572325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
5573325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
5574325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5575325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
5576325fc9f4SBarry Smith   PetscFunctionReturn(0);
5577325fc9f4SBarry Smith }
5578325fc9f4SBarry Smith 
5579325fc9f4SBarry Smith 
5580325fc9f4SBarry Smith #undef __FUNCT__
5581325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
5582325fc9f4SBarry Smith /*
5583325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
5584e3c5b3baSBarry 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
5585325fc9f4SBarry Smith 
5586325fc9f4SBarry Smith    Logically Collective on TS
5587325fc9f4SBarry Smith 
5588325fc9f4SBarry Smith    Input Parameters:
5589cdcf91faSSean Farley +  ts - the TS context
5590325fc9f4SBarry Smith .  A,B - Jacobian matrices
5591325fc9f4SBarry Smith .  func - function evaluation routine
5592325fc9f4SBarry Smith -  ctx - user context
5593325fc9f4SBarry Smith 
5594325fc9f4SBarry Smith    Calling sequence of func:
55950910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
5596325fc9f4SBarry Smith 
5597325fc9f4SBarry Smith 
5598325fc9f4SBarry Smith    Level: developer
5599325fc9f4SBarry Smith 
5600325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5601325fc9f4SBarry Smith 
5602325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5603325fc9f4SBarry Smith */
5604cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
5605325fc9f4SBarry Smith {
5606325fc9f4SBarry Smith   PetscErrorCode  ierr;
5607325fc9f4SBarry Smith   TSMatlabContext *sctx;
5608325fc9f4SBarry Smith 
5609325fc9f4SBarry Smith   PetscFunctionBegin;
5610325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5611325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5612325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5613325fc9f4SBarry Smith   /*
5614325fc9f4SBarry Smith      This should work, but it doesn't
5615325fc9f4SBarry Smith   sctx->ctx = ctx;
5616325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5617325fc9f4SBarry Smith   */
5618325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5619bbd56ea5SKarl Rupp 
5620cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
5621325fc9f4SBarry Smith   PetscFunctionReturn(0);
5622325fc9f4SBarry Smith }
5623325fc9f4SBarry Smith 
5624b5b1a830SBarry Smith #undef __FUNCT__
5625b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
5626b5b1a830SBarry Smith /*
5627b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
5628b5b1a830SBarry Smith 
5629b5b1a830SBarry Smith    Collective on TS
5630b5b1a830SBarry Smith 
5631b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5632b5b1a830SBarry Smith @*/
56330910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
5634b5b1a830SBarry Smith {
5635b5b1a830SBarry Smith   PetscErrorCode  ierr;
5636b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5637a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
5638b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
5639b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
5640b5b1a830SBarry Smith 
5641b5b1a830SBarry Smith   PetscFunctionBegin;
5642cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
56430910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
5644b5b1a830SBarry Smith 
5645cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
56460910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
5647bbd56ea5SKarl Rupp 
5648b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5649b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
5650b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
5651b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
5652b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
5653b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
5654b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
5655b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5656b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
5657b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
5658b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
5659b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
5660b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
5661b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
5662b5b1a830SBarry Smith   PetscFunctionReturn(0);
5663b5b1a830SBarry Smith }
5664b5b1a830SBarry Smith 
5665b5b1a830SBarry Smith 
5666b5b1a830SBarry Smith #undef __FUNCT__
5667b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
5668b5b1a830SBarry Smith /*
5669b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
5670b5b1a830SBarry Smith 
5671b5b1a830SBarry Smith    Level: developer
5672b5b1a830SBarry Smith 
5673b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
5674b5b1a830SBarry Smith 
5675b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5676b5b1a830SBarry Smith */
5677cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
5678b5b1a830SBarry Smith {
5679b5b1a830SBarry Smith   PetscErrorCode  ierr;
5680b5b1a830SBarry Smith   TSMatlabContext *sctx;
5681b5b1a830SBarry Smith 
5682b5b1a830SBarry Smith   PetscFunctionBegin;
5683b5b1a830SBarry Smith   /* currently sctx is memory bleed */
5684b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5685b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5686b5b1a830SBarry Smith   /*
5687b5b1a830SBarry Smith      This should work, but it doesn't
5688b5b1a830SBarry Smith   sctx->ctx = ctx;
5689b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5690b5b1a830SBarry Smith   */
5691b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5692bbd56ea5SKarl Rupp 
56930298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
5694b5b1a830SBarry Smith   PetscFunctionReturn(0);
5695b5b1a830SBarry Smith }
5696325fc9f4SBarry Smith #endif
5697b3603a34SBarry Smith 
5698b3603a34SBarry Smith #undef __FUNCT__
56994f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
5700b3603a34SBarry Smith /*@C
57014f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
5702b3603a34SBarry Smith        in a time based line graph
5703b3603a34SBarry Smith 
5704b3603a34SBarry Smith    Collective on TS
5705b3603a34SBarry Smith 
5706b3603a34SBarry Smith    Input Parameters:
5707b3603a34SBarry Smith +  ts - the TS context
5708b3603a34SBarry Smith .  step - current time-step
5709b3603a34SBarry Smith .  ptime - current time
5710b3603a34SBarry Smith -  lg - a line graph object
5711b3603a34SBarry Smith 
5712b3d3934dSBarry Smith    Options Database:
57139ae14b6eSBarry Smith .   -ts_monitor_lg_solution_variables
5714b3d3934dSBarry Smith 
5715b3603a34SBarry Smith    Level: intermediate
5716b3603a34SBarry Smith 
57170b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
5718b3603a34SBarry Smith 
5719b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
5720b3603a34SBarry Smith 
5721b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5722b3603a34SBarry Smith @*/
57230910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
5724b3603a34SBarry Smith {
5725b3603a34SBarry Smith   PetscErrorCode    ierr;
57260b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
5727b3603a34SBarry Smith   const PetscScalar *yy;
572858ff32f7SBarry Smith   PetscInt          dim;
572980666b62SBarry Smith   Vec               v;
5730b3603a34SBarry Smith 
5731b3603a34SBarry Smith   PetscFunctionBegin;
573258ff32f7SBarry Smith   if (!step) {
5733a9f9c1f6SBarry Smith     PetscDrawAxis axis;
5734a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5735a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
5736387f4636SBarry Smith     if (ctx->names && !ctx->displaynames) {
5737387f4636SBarry Smith       char      **displaynames;
5738387f4636SBarry Smith       PetscBool flg;
5739387f4636SBarry Smith 
5740387f4636SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
5741387f4636SBarry Smith       ierr = PetscMalloc((dim+1)*sizeof(char*),&displaynames);CHKERRQ(ierr);
5742387f4636SBarry Smith       ierr = PetscMemzero(displaynames,(dim+1)*sizeof(char*));CHKERRQ(ierr);
57439ae14b6eSBarry Smith       ierr = PetscOptionsGetStringArray(((PetscObject)ts)->prefix,"-ts_monitor_lg_solution_variables",displaynames,&dim,&flg);CHKERRQ(ierr);
5744387f4636SBarry Smith       if (flg) {
5745a66092f1SBarry Smith         ierr = TSMonitorLGCtxSetDisplayVariables(ctx,(const char *const *)displaynames);CHKERRQ(ierr);
5746387f4636SBarry Smith       }
5747387f4636SBarry Smith       ierr = PetscStrArrayDestroy(&displaynames);CHKERRQ(ierr);
5748387f4636SBarry Smith     }
5749387f4636SBarry Smith     if (ctx->displaynames) {
5750387f4636SBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,ctx->ndisplayvariables);CHKERRQ(ierr);
5751387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->displaynames);CHKERRQ(ierr);
5752387f4636SBarry Smith     } else if (ctx->names) {
57530910c330SBarry Smith       ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
57540b039ecaSBarry Smith       ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
5755387f4636SBarry Smith       ierr = PetscDrawLGSetLegend(ctx->lg,(const char *const *)ctx->names);CHKERRQ(ierr);
5756387f4636SBarry Smith     }
57570b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
575858ff32f7SBarry Smith   }
575980666b62SBarry Smith   if (ctx->transform) {
5760e673d494SBarry Smith     ierr = (*ctx->transform)(ctx->transformctx,u,&v);CHKERRQ(ierr);
576180666b62SBarry Smith   } else {
576280666b62SBarry Smith     v = u;
576380666b62SBarry Smith   }
576480666b62SBarry Smith   ierr = VecGetArrayRead(v,&yy);CHKERRQ(ierr);
5765e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
5766e3efe391SJed Brown   {
5767e3efe391SJed Brown     PetscReal *yreal;
5768e3efe391SJed Brown     PetscInt  i,n;
576980666b62SBarry Smith     ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
5770785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
5771e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
57720b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
5773e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
5774e3efe391SJed Brown   }
5775e3efe391SJed Brown #else
5776387f4636SBarry Smith   if (ctx->displaynames) {
5777387f4636SBarry Smith     PetscInt i;
5778387f4636SBarry Smith     for (i=0; i<ctx->ndisplayvariables; i++) {
5779387f4636SBarry Smith       ctx->displayvalues[i] = yy[ctx->displayvariables[i]];
5780387f4636SBarry Smith     }
5781387f4636SBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,ctx->displayvalues);CHKERRQ(ierr);
5782387f4636SBarry Smith   } else {
57830b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
5784387f4636SBarry Smith   }
5785e3efe391SJed Brown #endif
578680666b62SBarry Smith   ierr = VecRestoreArrayRead(v,&yy);CHKERRQ(ierr);
578780666b62SBarry Smith   if (ctx->transform) {
578880666b62SBarry Smith     ierr = VecDestroy(&v);CHKERRQ(ierr);
578980666b62SBarry Smith   }
5790b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
57910b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
57923923b477SBarry Smith   }
5793b3603a34SBarry Smith   PetscFunctionReturn(0);
5794b3603a34SBarry Smith }
5795b3603a34SBarry Smith 
5796387f4636SBarry Smith 
5797b3603a34SBarry Smith #undef __FUNCT__
579831152f8aSBarry Smith #define __FUNCT__ "TSMonitorLGSetVariableNames"
5799b037adc7SBarry Smith /*@C
580031152f8aSBarry Smith    TSMonitorLGSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
5801b037adc7SBarry Smith 
5802b037adc7SBarry Smith    Collective on TS
5803b037adc7SBarry Smith 
5804b037adc7SBarry Smith    Input Parameters:
5805b037adc7SBarry Smith +  ts - the TS context
5806b3d3934dSBarry Smith -  names - the names of the components, final string must be NULL
5807b037adc7SBarry Smith 
5808b037adc7SBarry Smith    Level: intermediate
5809b037adc7SBarry Smith 
5810b037adc7SBarry Smith .keywords: TS,  vector, monitor, view
5811b037adc7SBarry Smith 
5812a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGCtxSetVariableNames()
5813b037adc7SBarry Smith @*/
581431152f8aSBarry Smith PetscErrorCode  TSMonitorLGSetVariableNames(TS ts,const char * const *names)
5815b037adc7SBarry Smith {
5816b037adc7SBarry Smith   PetscErrorCode    ierr;
5817b037adc7SBarry Smith   PetscInt          i;
5818b037adc7SBarry Smith 
5819b037adc7SBarry Smith   PetscFunctionBegin;
5820b037adc7SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
5821b037adc7SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
58225537e223SBarry Smith       ierr = TSMonitorLGCtxSetVariableNames((TSMonitorLGCtx)ts->monitorcontext[i],names);CHKERRQ(ierr);
5823b3d3934dSBarry Smith       break;
5824b3d3934dSBarry Smith     }
5825b3d3934dSBarry Smith   }
5826b3d3934dSBarry Smith   PetscFunctionReturn(0);
5827b3d3934dSBarry Smith }
5828b3d3934dSBarry Smith 
5829b3d3934dSBarry Smith #undef __FUNCT__
5830e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetVariableNames"
5831e673d494SBarry Smith /*@C
5832e673d494SBarry Smith    TSMonitorLGCtxSetVariableNames - Sets the name of each component in the solution vector so that it may be displayed in the plot
5833e673d494SBarry Smith 
5834e673d494SBarry Smith    Collective on TS
5835e673d494SBarry Smith 
5836e673d494SBarry Smith    Input Parameters:
5837e673d494SBarry Smith +  ts - the TS context
5838e673d494SBarry Smith -  names - the names of the components, final string must be NULL
5839e673d494SBarry Smith 
5840e673d494SBarry Smith    Level: intermediate
5841e673d494SBarry Smith 
5842e673d494SBarry Smith .keywords: TS,  vector, monitor, view
5843e673d494SBarry Smith 
5844a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables(), TSMonitorLGSetVariableNames()
5845e673d494SBarry Smith @*/
5846e673d494SBarry Smith PetscErrorCode  TSMonitorLGCtxSetVariableNames(TSMonitorLGCtx ctx,const char * const *names)
5847e673d494SBarry Smith {
5848e673d494SBarry Smith   PetscErrorCode    ierr;
5849e673d494SBarry Smith 
5850e673d494SBarry Smith   PetscFunctionBegin;
5851e673d494SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->names);CHKERRQ(ierr);
5852e673d494SBarry Smith   ierr = PetscStrArrayallocpy(names,&ctx->names);CHKERRQ(ierr);
5853e673d494SBarry Smith   PetscFunctionReturn(0);
5854e673d494SBarry Smith }
5855e673d494SBarry Smith 
5856e673d494SBarry Smith #undef __FUNCT__
5857b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorLGGetVariableNames"
5858b3d3934dSBarry Smith /*@C
5859b3d3934dSBarry Smith    TSMonitorLGGetVariableNames - Gets the name of each component in the solution vector so that it may be displayed in the plot
5860b3d3934dSBarry Smith 
5861b3d3934dSBarry Smith    Collective on TS
5862b3d3934dSBarry Smith 
5863b3d3934dSBarry Smith    Input Parameter:
5864b3d3934dSBarry Smith .  ts - the TS context
5865b3d3934dSBarry Smith 
5866b3d3934dSBarry Smith    Output Parameter:
5867b3d3934dSBarry Smith .  names - the names of the components, final string must be NULL
5868b3d3934dSBarry Smith 
5869b3d3934dSBarry Smith    Level: intermediate
5870b3d3934dSBarry Smith 
5871b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
5872b3d3934dSBarry Smith 
5873b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
5874b3d3934dSBarry Smith @*/
5875b3d3934dSBarry Smith PetscErrorCode  TSMonitorLGGetVariableNames(TS ts,const char *const **names)
5876b3d3934dSBarry Smith {
5877b3d3934dSBarry Smith   PetscInt       i;
5878b3d3934dSBarry Smith 
5879b3d3934dSBarry Smith   PetscFunctionBegin;
5880b3d3934dSBarry Smith   *names = NULL;
5881b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
5882b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
58835537e223SBarry Smith       TSMonitorLGCtx  ctx = (TSMonitorLGCtx) ts->monitorcontext[i];
5884b3d3934dSBarry Smith       *names = (const char *const *)ctx->names;
5885b3d3934dSBarry Smith       break;
5886387f4636SBarry Smith     }
5887387f4636SBarry Smith   }
5888387f4636SBarry Smith   PetscFunctionReturn(0);
5889387f4636SBarry Smith }
5890387f4636SBarry Smith 
5891387f4636SBarry Smith #undef __FUNCT__
5892a66092f1SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetDisplayVariables"
5893a66092f1SBarry Smith /*@C
5894a66092f1SBarry Smith    TSMonitorLGCtxSetDisplayVariables - Sets the variables that are to be display in the monitor
5895a66092f1SBarry Smith 
5896a66092f1SBarry Smith    Collective on TS
5897a66092f1SBarry Smith 
5898a66092f1SBarry Smith    Input Parameters:
5899a66092f1SBarry Smith +  ctx - the TSMonitorLG context
5900a66092f1SBarry Smith .  displaynames - the names of the components, final string must be NULL
5901a66092f1SBarry Smith 
5902a66092f1SBarry Smith    Level: intermediate
5903a66092f1SBarry Smith 
5904a66092f1SBarry Smith .keywords: TS,  vector, monitor, view
5905a66092f1SBarry Smith 
5906a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
5907a66092f1SBarry Smith @*/
5908a66092f1SBarry Smith PetscErrorCode  TSMonitorLGCtxSetDisplayVariables(TSMonitorLGCtx ctx,const char * const *displaynames)
5909a66092f1SBarry Smith {
5910a66092f1SBarry Smith   PetscInt          j = 0,k;
5911a66092f1SBarry Smith   PetscErrorCode    ierr;
5912a66092f1SBarry Smith 
5913a66092f1SBarry Smith   PetscFunctionBegin;
5914a66092f1SBarry Smith   if (!ctx->names) PetscFunctionReturn(0);
5915a66092f1SBarry Smith   ierr = PetscStrArrayDestroy(&ctx->displaynames);CHKERRQ(ierr);
5916a66092f1SBarry Smith   ierr = PetscStrArrayallocpy(displaynames,&ctx->displaynames);CHKERRQ(ierr);
5917a66092f1SBarry Smith   while (displaynames[j]) j++;
5918a66092f1SBarry Smith   ctx->ndisplayvariables = j;
5919a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvariables);CHKERRQ(ierr);
5920a66092f1SBarry Smith   ierr = PetscMalloc1(ctx->ndisplayvariables,&ctx->displayvalues);CHKERRQ(ierr);
5921a66092f1SBarry Smith   j = 0;
5922a66092f1SBarry Smith   while (displaynames[j]) {
5923a66092f1SBarry Smith     k = 0;
5924a66092f1SBarry Smith     while (ctx->names[k]) {
5925a66092f1SBarry Smith       PetscBool flg;
5926a66092f1SBarry Smith       ierr = PetscStrcmp(displaynames[j],ctx->names[k],&flg);CHKERRQ(ierr);
5927a66092f1SBarry Smith       if (flg) {
5928a66092f1SBarry Smith         ctx->displayvariables[j] = k;
5929a66092f1SBarry Smith         break;
5930a66092f1SBarry Smith       }
5931a66092f1SBarry Smith       k++;
5932a66092f1SBarry Smith     }
5933a66092f1SBarry Smith     j++;
5934a66092f1SBarry Smith   }
5935a66092f1SBarry Smith   PetscFunctionReturn(0);
5936a66092f1SBarry Smith }
5937a66092f1SBarry Smith 
5938a66092f1SBarry Smith 
5939a66092f1SBarry Smith #undef __FUNCT__
5940387f4636SBarry Smith #define __FUNCT__ "TSMonitorLGSetDisplayVariables"
5941387f4636SBarry Smith /*@C
5942387f4636SBarry Smith    TSMonitorLGSetDisplayVariables - Sets the variables that are to be display in the monitor
5943387f4636SBarry Smith 
5944387f4636SBarry Smith    Collective on TS
5945387f4636SBarry Smith 
5946387f4636SBarry Smith    Input Parameters:
5947387f4636SBarry Smith +  ts - the TS context
5948387f4636SBarry Smith .  displaynames - the names of the components, final string must be NULL
5949387f4636SBarry Smith 
5950387f4636SBarry Smith    Level: intermediate
5951387f4636SBarry Smith 
5952387f4636SBarry Smith .keywords: TS,  vector, monitor, view
5953387f4636SBarry Smith 
5954387f4636SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames()
5955387f4636SBarry Smith @*/
5956387f4636SBarry Smith PetscErrorCode  TSMonitorLGSetDisplayVariables(TS ts,const char * const *displaynames)
5957387f4636SBarry Smith {
5958a66092f1SBarry Smith   PetscInt          i;
5959387f4636SBarry Smith   PetscErrorCode    ierr;
5960387f4636SBarry Smith 
5961387f4636SBarry Smith   PetscFunctionBegin;
5962387f4636SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
5963387f4636SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
59645537e223SBarry Smith       ierr = TSMonitorLGCtxSetDisplayVariables((TSMonitorLGCtx)ts->monitorcontext[i],displaynames);CHKERRQ(ierr);
5965b3d3934dSBarry Smith       break;
5966b037adc7SBarry Smith     }
5967b037adc7SBarry Smith   }
5968b037adc7SBarry Smith   PetscFunctionReturn(0);
5969b037adc7SBarry Smith }
5970b037adc7SBarry Smith 
5971b037adc7SBarry Smith #undef __FUNCT__
597280666b62SBarry Smith #define __FUNCT__ "TSMonitorLGSetTransform"
597380666b62SBarry Smith /*@C
597480666b62SBarry Smith    TSMonitorLGSetTransform - Solution vector will be transformed by provided function before being displayed
597580666b62SBarry Smith 
597680666b62SBarry Smith    Collective on TS
597780666b62SBarry Smith 
597880666b62SBarry Smith    Input Parameters:
597980666b62SBarry Smith +  ts - the TS context
598080666b62SBarry Smith .  transform - the transform function
59817684fa3eSBarry Smith .  destroy - function to destroy the optional context
598280666b62SBarry Smith -  ctx - optional context used by transform function
598380666b62SBarry Smith 
598480666b62SBarry Smith    Level: intermediate
598580666b62SBarry Smith 
598680666b62SBarry Smith .keywords: TS,  vector, monitor, view
598780666b62SBarry Smith 
5988a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGCtxSetTransform()
598980666b62SBarry Smith @*/
59907684fa3eSBarry Smith PetscErrorCode  TSMonitorLGSetTransform(TS ts,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
599180666b62SBarry Smith {
599280666b62SBarry Smith   PetscInt          i;
5993a66092f1SBarry Smith   PetscErrorCode    ierr;
599480666b62SBarry Smith 
599580666b62SBarry Smith   PetscFunctionBegin;
599680666b62SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
599780666b62SBarry Smith     if (ts->monitor[i] == TSMonitorLGSolution) {
59985537e223SBarry Smith       ierr = TSMonitorLGCtxSetTransform((TSMonitorLGCtx)ts->monitorcontext[i],transform,destroy,tctx);CHKERRQ(ierr);
599980666b62SBarry Smith     }
600080666b62SBarry Smith   }
600180666b62SBarry Smith   PetscFunctionReturn(0);
600280666b62SBarry Smith }
600380666b62SBarry Smith 
600480666b62SBarry Smith #undef __FUNCT__
6005e673d494SBarry Smith #define __FUNCT__ "TSMonitorLGCtxSetTransform"
6006e673d494SBarry Smith /*@C
6007e673d494SBarry Smith    TSMonitorLGCtxSetTransform - Solution vector will be transformed by provided function before being displayed
6008e673d494SBarry Smith 
6009e673d494SBarry Smith    Collective on TSLGCtx
6010e673d494SBarry Smith 
6011e673d494SBarry Smith    Input Parameters:
6012e673d494SBarry Smith +  ts - the TS context
6013e673d494SBarry Smith .  transform - the transform function
60147684fa3eSBarry Smith .  destroy - function to destroy the optional context
6015e673d494SBarry Smith -  ctx - optional context used by transform function
6016e673d494SBarry Smith 
6017e673d494SBarry Smith    Level: intermediate
6018e673d494SBarry Smith 
6019e673d494SBarry Smith .keywords: TS,  vector, monitor, view
6020e673d494SBarry Smith 
6021a66092f1SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetVariableNames(), TSMonitorLGSetTransform()
6022e673d494SBarry Smith @*/
60237684fa3eSBarry Smith PetscErrorCode  TSMonitorLGCtxSetTransform(TSMonitorLGCtx ctx,PetscErrorCode (*transform)(void*,Vec,Vec*),PetscErrorCode (*destroy)(void*),void *tctx)
6024e673d494SBarry Smith {
6025e673d494SBarry Smith   PetscFunctionBegin;
6026e673d494SBarry Smith   ctx->transform    = transform;
60277684fa3eSBarry Smith   ctx->transformdestroy = destroy;
6028e673d494SBarry Smith   ctx->transformctx = tctx;
6029e673d494SBarry Smith   PetscFunctionReturn(0);
6030e673d494SBarry Smith }
6031e673d494SBarry Smith 
6032b3603a34SBarry Smith #undef __FUNCT__
60334f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
6034ef20d060SBarry Smith /*@C
60354f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
6036ef20d060SBarry Smith        in a time based line graph
6037ef20d060SBarry Smith 
6038ef20d060SBarry Smith    Collective on TS
6039ef20d060SBarry Smith 
6040ef20d060SBarry Smith    Input Parameters:
6041ef20d060SBarry Smith +  ts - the TS context
6042ef20d060SBarry Smith .  step - current time-step
6043ef20d060SBarry Smith .  ptime - current time
6044ef20d060SBarry Smith -  lg - a line graph object
6045ef20d060SBarry Smith 
6046ef20d060SBarry Smith    Level: intermediate
6047ef20d060SBarry Smith 
6048abd5a294SJed Brown    Notes:
6049abd5a294SJed Brown    Only for sequential solves.
6050abd5a294SJed Brown 
6051abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
6052abd5a294SJed Brown 
6053abd5a294SJed Brown    Options Database Keys:
60544f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
6055ef20d060SBarry Smith 
6056ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
6057ef20d060SBarry Smith 
6058abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
6059ef20d060SBarry Smith @*/
60600910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6061ef20d060SBarry Smith {
6062ef20d060SBarry Smith   PetscErrorCode    ierr;
60630b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
6064ef20d060SBarry Smith   const PetscScalar *yy;
6065ef20d060SBarry Smith   Vec               y;
6066a9f9c1f6SBarry Smith   PetscInt          dim;
6067ef20d060SBarry Smith 
6068ef20d060SBarry Smith   PetscFunctionBegin;
6069a9f9c1f6SBarry Smith   if (!step) {
6070a9f9c1f6SBarry Smith     PetscDrawAxis axis;
6071a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
60721ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
60730910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
6074a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
6075a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6076a9f9c1f6SBarry Smith   }
60770910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
6078ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
60790910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
6080ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
6081e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
6082e3efe391SJed Brown   {
6083e3efe391SJed Brown     PetscReal *yreal;
6084e3efe391SJed Brown     PetscInt  i,n;
6085e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
6086785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
6087e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
60880b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
6089e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
6090e3efe391SJed Brown   }
6091e3efe391SJed Brown #else
60920b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
6093e3efe391SJed Brown #endif
6094ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
6095ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
6096b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
60970b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
60983923b477SBarry Smith   }
6099ef20d060SBarry Smith   PetscFunctionReturn(0);
6100ef20d060SBarry Smith }
6101ef20d060SBarry Smith 
6102201da799SBarry Smith #undef __FUNCT__
6103201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
6104201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6105201da799SBarry Smith {
6106201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6107201da799SBarry Smith   PetscReal      x   = ptime,y;
6108201da799SBarry Smith   PetscErrorCode ierr;
6109201da799SBarry Smith   PetscInt       its;
6110201da799SBarry Smith 
6111201da799SBarry Smith   PetscFunctionBegin;
6112201da799SBarry Smith   if (!n) {
6113201da799SBarry Smith     PetscDrawAxis axis;
6114bbd56ea5SKarl Rupp 
6115201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6116201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
6117201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6118bbd56ea5SKarl Rupp 
6119201da799SBarry Smith     ctx->snes_its = 0;
6120201da799SBarry Smith   }
6121201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
6122201da799SBarry Smith   y    = its - ctx->snes_its;
6123201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
61243fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6125201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
6126201da799SBarry Smith   }
6127201da799SBarry Smith   ctx->snes_its = its;
6128201da799SBarry Smith   PetscFunctionReturn(0);
6129201da799SBarry Smith }
6130201da799SBarry Smith 
6131201da799SBarry Smith #undef __FUNCT__
6132201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
6133201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
6134201da799SBarry Smith {
6135201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
6136201da799SBarry Smith   PetscReal      x   = ptime,y;
6137201da799SBarry Smith   PetscErrorCode ierr;
6138201da799SBarry Smith   PetscInt       its;
6139201da799SBarry Smith 
6140201da799SBarry Smith   PetscFunctionBegin;
6141201da799SBarry Smith   if (!n) {
6142201da799SBarry Smith     PetscDrawAxis axis;
6143bbd56ea5SKarl Rupp 
6144201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
6145201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
6146201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
6147bbd56ea5SKarl Rupp 
6148201da799SBarry Smith     ctx->ksp_its = 0;
6149201da799SBarry Smith   }
6150201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
6151201da799SBarry Smith   y    = its - ctx->ksp_its;
6152201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
615399fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
6154201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
6155201da799SBarry Smith   }
6156201da799SBarry Smith   ctx->ksp_its = its;
6157201da799SBarry Smith   PetscFunctionReturn(0);
6158201da799SBarry Smith }
6159f9c1d6abSBarry Smith 
6160f9c1d6abSBarry Smith #undef __FUNCT__
6161f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
6162f9c1d6abSBarry Smith /*@
6163f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
6164f9c1d6abSBarry Smith 
6165f9c1d6abSBarry Smith    Collective on TS and Vec
6166f9c1d6abSBarry Smith 
6167f9c1d6abSBarry Smith    Input Parameters:
6168f9c1d6abSBarry Smith +  ts - the TS context
6169f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
6170f9c1d6abSBarry Smith 
6171f9c1d6abSBarry Smith    Output Parameters:
6172f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
6173f9c1d6abSBarry Smith 
6174f9c1d6abSBarry Smith    Level: developer
6175f9c1d6abSBarry Smith 
6176f9c1d6abSBarry Smith .keywords: TS, compute
6177f9c1d6abSBarry Smith 
6178f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
6179f9c1d6abSBarry Smith @*/
6180f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
6181f9c1d6abSBarry Smith {
6182f9c1d6abSBarry Smith   PetscErrorCode ierr;
6183f9c1d6abSBarry Smith 
6184f9c1d6abSBarry Smith   PetscFunctionBegin;
6185f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6186ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
6187f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
6188f9c1d6abSBarry Smith   PetscFunctionReturn(0);
6189f9c1d6abSBarry Smith }
619024655328SShri 
6191b3d3934dSBarry Smith /* ------------------------------------------------------------------------*/
6192b3d3934dSBarry Smith #undef __FUNCT__
6193b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxCreate"
6194b3d3934dSBarry Smith /*@C
6195b3d3934dSBarry Smith    TSMonitorEnvelopeCtxCreate - Creates a context for use with TSMonitorEnvelope()
6196b3d3934dSBarry Smith 
6197b3d3934dSBarry Smith    Collective on TS
6198b3d3934dSBarry Smith 
6199b3d3934dSBarry Smith    Input Parameters:
6200b3d3934dSBarry Smith .  ts  - the ODE solver object
6201b3d3934dSBarry Smith 
6202b3d3934dSBarry Smith    Output Parameter:
6203b3d3934dSBarry Smith .  ctx - the context
6204b3d3934dSBarry Smith 
6205b3d3934dSBarry Smith    Level: intermediate
6206b3d3934dSBarry Smith 
6207b3d3934dSBarry Smith .keywords: TS, monitor, line graph, residual, seealso
6208b3d3934dSBarry Smith 
6209b3d3934dSBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
6210b3d3934dSBarry Smith 
6211b3d3934dSBarry Smith @*/
6212b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxCreate(TS ts,TSMonitorEnvelopeCtx *ctx)
6213b3d3934dSBarry Smith {
6214b3d3934dSBarry Smith   PetscErrorCode ierr;
6215b3d3934dSBarry Smith 
6216b3d3934dSBarry Smith   PetscFunctionBegin;
6217a74656a8SBarry Smith   ierr = PetscNew(ctx);CHKERRQ(ierr);
6218b3d3934dSBarry Smith   PetscFunctionReturn(0);
6219b3d3934dSBarry Smith }
6220b3d3934dSBarry Smith 
6221b3d3934dSBarry Smith #undef __FUNCT__
6222b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelope"
6223b3d3934dSBarry Smith /*@C
6224b3d3934dSBarry Smith    TSMonitorEnvelope - Monitors the maximum and minimum value of each component of the solution
6225b3d3934dSBarry Smith 
6226b3d3934dSBarry Smith    Collective on TS
6227b3d3934dSBarry Smith 
6228b3d3934dSBarry Smith    Input Parameters:
6229b3d3934dSBarry Smith +  ts - the TS context
6230b3d3934dSBarry Smith .  step - current time-step
6231b3d3934dSBarry Smith .  ptime - current time
6232b3d3934dSBarry Smith -  ctx - the envelope context
6233b3d3934dSBarry Smith 
6234b3d3934dSBarry Smith    Options Database:
6235b3d3934dSBarry Smith .  -ts_monitor_envelope
6236b3d3934dSBarry Smith 
6237b3d3934dSBarry Smith    Level: intermediate
6238b3d3934dSBarry Smith 
6239b3d3934dSBarry Smith    Notes: after a solve you can use TSMonitorEnvelopeGetBounds() to access the envelope
6240b3d3934dSBarry Smith 
6241b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6242b3d3934dSBarry Smith 
6243b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorEnvelopeGetBounds()
6244b3d3934dSBarry Smith @*/
6245b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelope(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
6246b3d3934dSBarry Smith {
6247b3d3934dSBarry Smith   PetscErrorCode       ierr;
6248b3d3934dSBarry Smith   TSMonitorEnvelopeCtx ctx = (TSMonitorEnvelopeCtx)dummy;
6249b3d3934dSBarry Smith 
6250b3d3934dSBarry Smith   PetscFunctionBegin;
6251b3d3934dSBarry Smith   if (!ctx->max) {
6252b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->max);CHKERRQ(ierr);
6253b3d3934dSBarry Smith     ierr = VecDuplicate(u,&ctx->min);CHKERRQ(ierr);
6254b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->max);CHKERRQ(ierr);
6255b3d3934dSBarry Smith     ierr = VecCopy(u,ctx->min);CHKERRQ(ierr);
6256b3d3934dSBarry Smith   } else {
6257b3d3934dSBarry Smith     ierr = VecPointwiseMax(ctx->max,u,ctx->max);CHKERRQ(ierr);
6258b3d3934dSBarry Smith     ierr = VecPointwiseMin(ctx->min,u,ctx->min);CHKERRQ(ierr);
6259b3d3934dSBarry Smith   }
6260b3d3934dSBarry Smith   PetscFunctionReturn(0);
6261b3d3934dSBarry Smith }
6262b3d3934dSBarry Smith 
6263b3d3934dSBarry Smith 
6264b3d3934dSBarry Smith #undef __FUNCT__
6265b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeGetBounds"
6266b3d3934dSBarry Smith /*@C
6267b3d3934dSBarry Smith    TSMonitorEnvelopeGetBounds - Gets the bounds for the components of the solution
6268b3d3934dSBarry Smith 
6269b3d3934dSBarry Smith    Collective on TS
6270b3d3934dSBarry Smith 
6271b3d3934dSBarry Smith    Input Parameter:
6272b3d3934dSBarry Smith .  ts - the TS context
6273b3d3934dSBarry Smith 
6274b3d3934dSBarry Smith    Output Parameter:
6275b3d3934dSBarry Smith +  max - the maximum values
6276b3d3934dSBarry Smith -  min - the minimum values
6277b3d3934dSBarry Smith 
6278b3d3934dSBarry Smith    Level: intermediate
6279b3d3934dSBarry Smith 
6280b3d3934dSBarry Smith .keywords: TS,  vector, monitor, view
6281b3d3934dSBarry Smith 
6282b3d3934dSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorLGSetDisplayVariables()
6283b3d3934dSBarry Smith @*/
6284b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeGetBounds(TS ts,Vec *max,Vec *min)
6285b3d3934dSBarry Smith {
6286b3d3934dSBarry Smith   PetscInt i;
6287b3d3934dSBarry Smith 
6288b3d3934dSBarry Smith   PetscFunctionBegin;
6289b3d3934dSBarry Smith   if (max) *max = NULL;
6290b3d3934dSBarry Smith   if (min) *min = NULL;
6291b3d3934dSBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
6292b3d3934dSBarry Smith     if (ts->monitor[i] == TSMonitorEnvelope) {
62935537e223SBarry Smith       TSMonitorEnvelopeCtx  ctx = (TSMonitorEnvelopeCtx) ts->monitorcontext[i];
6294b3d3934dSBarry Smith       if (max) *max = ctx->max;
6295b3d3934dSBarry Smith       if (min) *min = ctx->min;
6296b3d3934dSBarry Smith       break;
6297b3d3934dSBarry Smith     }
6298b3d3934dSBarry Smith   }
6299b3d3934dSBarry Smith   PetscFunctionReturn(0);
6300b3d3934dSBarry Smith }
6301b3d3934dSBarry Smith 
6302b3d3934dSBarry Smith #undef __FUNCT__
6303b3d3934dSBarry Smith #define __FUNCT__ "TSMonitorEnvelopeCtxDestroy"
6304b3d3934dSBarry Smith /*@C
6305b3d3934dSBarry Smith    TSMonitorEnvelopeCtxDestroy - Destroys a context that was created  with TSMonitorEnvelopeCtxCreate().
6306b3d3934dSBarry Smith 
6307b3d3934dSBarry Smith    Collective on TSMonitorEnvelopeCtx
6308b3d3934dSBarry Smith 
6309b3d3934dSBarry Smith    Input Parameter:
6310b3d3934dSBarry Smith .  ctx - the monitor context
6311b3d3934dSBarry Smith 
6312b3d3934dSBarry Smith    Level: intermediate
6313b3d3934dSBarry Smith 
6314b3d3934dSBarry Smith .keywords: TS, monitor, line graph, destroy
6315b3d3934dSBarry Smith 
6316b3d3934dSBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
6317b3d3934dSBarry Smith @*/
6318b3d3934dSBarry Smith PetscErrorCode  TSMonitorEnvelopeCtxDestroy(TSMonitorEnvelopeCtx *ctx)
6319b3d3934dSBarry Smith {
6320b3d3934dSBarry Smith   PetscErrorCode ierr;
6321b3d3934dSBarry Smith 
6322b3d3934dSBarry Smith   PetscFunctionBegin;
6323b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->min);CHKERRQ(ierr);
6324b3d3934dSBarry Smith   ierr = VecDestroy(&(*ctx)->max);CHKERRQ(ierr);
6325b3d3934dSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
6326b3d3934dSBarry Smith   PetscFunctionReturn(0);
6327b3d3934dSBarry Smith }
6328f2dee214SBarry Smith 
632924655328SShri #undef __FUNCT__
633024655328SShri #define __FUNCT__ "TSRollBack"
633124655328SShri /*@
633224655328SShri    TSRollBack - Rolls back one time step
633324655328SShri 
633424655328SShri    Collective on TS
633524655328SShri 
633624655328SShri    Input Parameter:
633724655328SShri .  ts - the TS context obtained from TSCreate()
633824655328SShri 
633924655328SShri    Level: advanced
634024655328SShri 
634124655328SShri .keywords: TS, timestep, rollback
634224655328SShri 
634324655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
634424655328SShri @*/
634524655328SShri PetscErrorCode  TSRollBack(TS ts)
634624655328SShri {
634724655328SShri   PetscErrorCode ierr;
634824655328SShri 
634924655328SShri   PetscFunctionBegin;
635024655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
635124655328SShri 
635224655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
635324655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
635424655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
635524655328SShri   ts->ptime = ts->ptime_prev;
63568392e04aSShri Abhyankar   ts->steprollback = PETSC_TRUE; /* Flag to indicate that the step is rollbacked */
635724655328SShri   PetscFunctionReturn(0);
635824655328SShri }
6359aeb4809dSShri Abhyankar 
6360ff22ae23SHong Zhang #undef __FUNCT__
6361ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages"
6362ff22ae23SHong Zhang /*@
6363ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
6364ff22ae23SHong Zhang 
6365ff22ae23SHong Zhang    Input Parameter:
6366ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
6367ff22ae23SHong Zhang 
6368ff22ae23SHong Zhang    Level: advanced
6369ff22ae23SHong Zhang 
6370ff22ae23SHong Zhang .keywords: TS, getstages
6371ff22ae23SHong Zhang 
6372ff22ae23SHong Zhang .seealso: TSCreate()
6373ff22ae23SHong Zhang @*/
6374ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns, Vec **Y)
6375ff22ae23SHong Zhang {
6376ff22ae23SHong Zhang   PetscErrorCode ierr;
6377ff22ae23SHong Zhang 
6378ff22ae23SHong Zhang   PetscFunctionBegin;
6379ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
6380ff22ae23SHong Zhang   PetscValidPointer(ns,2);
6381ff22ae23SHong Zhang 
6382ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
6383ff22ae23SHong Zhang   else {
6384ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
6385ff22ae23SHong Zhang   }
6386ff22ae23SHong Zhang   PetscFunctionReturn(0);
6387ff22ae23SHong Zhang }
6388ff22ae23SHong Zhang 
6389847ff0e1SMatthew G. Knepley #undef __FUNCT__
6390847ff0e1SMatthew G. Knepley #define __FUNCT__ "TSComputeIJacobianDefaultColor"
6391847ff0e1SMatthew G. Knepley /*@C
6392847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
6393847ff0e1SMatthew G. Knepley 
6394847ff0e1SMatthew G. Knepley   Collective on SNES
6395847ff0e1SMatthew G. Knepley 
6396847ff0e1SMatthew G. Knepley   Input Parameters:
6397847ff0e1SMatthew G. Knepley + ts - the TS context
6398847ff0e1SMatthew G. Knepley . t - current timestep
6399847ff0e1SMatthew G. Knepley . U - state vector
6400847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
6401847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
6402847ff0e1SMatthew G. Knepley - ctx - an optional user context
6403847ff0e1SMatthew G. Knepley 
6404847ff0e1SMatthew G. Knepley   Output Parameters:
6405847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
6406847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
6407847ff0e1SMatthew G. Knepley 
6408847ff0e1SMatthew G. Knepley   Level: intermediate
6409847ff0e1SMatthew G. Knepley 
6410847ff0e1SMatthew G. Knepley   Notes:
6411847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
6412847ff0e1SMatthew G. Knepley 
6413847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
6414847ff0e1SMatthew G. Knepley 
6415847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
6416847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
6417847ff0e1SMatthew G. Knepley 
6418847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
6419847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
6420847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
6421847ff0e1SMatthew G. Knepley 
6422847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
6423847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
6424847ff0e1SMatthew G. Knepley @*/
6425847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
6426847ff0e1SMatthew G. Knepley {
6427847ff0e1SMatthew G. Knepley   SNES           snes;
6428847ff0e1SMatthew G. Knepley   MatFDColoring  color;
6429847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
6430847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
6431847ff0e1SMatthew G. Knepley 
6432847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
6433847ff0e1SMatthew G. Knepley   ierr = PetscOptionsGetBool(((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
6434847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
6435847ff0e1SMatthew G. Knepley   if (!color) {
6436847ff0e1SMatthew G. Knepley     DM         dm;
6437847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
6438847ff0e1SMatthew G. Knepley 
6439847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
6440847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
6441847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
6442847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
6443847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
6444847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
6445847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
6446847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
6447847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
6448847ff0e1SMatthew G. Knepley     } else {
6449847ff0e1SMatthew G. Knepley       MatColoring mc;
6450847ff0e1SMatthew G. Knepley 
6451847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
6452847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
6453847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
6454847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
6455847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
6456847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
6457847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
6458847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
6459847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
6460847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
6461847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
6462847ff0e1SMatthew G. Knepley     }
6463847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
6464847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
6465847ff0e1SMatthew G. Knepley   }
6466847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
6467847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
6468847ff0e1SMatthew G. Knepley   if (J != B) {
6469847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
6470847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
6471847ff0e1SMatthew G. Knepley   }
6472847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
6473847ff0e1SMatthew G. Knepley }
647493b34091SDebojyoti Ghosh 
647593b34091SDebojyoti Ghosh #undef  __FUNCT__
6476e5168f73SEmil Constantinescu #define __FUNCT__ "TSClone"
647793b34091SDebojyoti Ghosh /*@C
6478e5168f73SEmil Constantinescu   TSClone - This function clones a time step object.
647993b34091SDebojyoti Ghosh 
648093b34091SDebojyoti Ghosh   Collective on MPI_Comm
648193b34091SDebojyoti Ghosh 
648293b34091SDebojyoti Ghosh   Input Parameter:
648393b34091SDebojyoti Ghosh . tsin    - The input TS
648493b34091SDebojyoti Ghosh 
648593b34091SDebojyoti Ghosh   Output Parameter:
6486e5168f73SEmil Constantinescu . tsout   - The output TS (cloned)
648793b34091SDebojyoti Ghosh 
64885eca1a21SEmil Constantinescu   Notes:
64895eca1a21SEmil 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.
64905eca1a21SEmil Constantinescu 
6491*baa10174SEmil 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);
64925eca1a21SEmil Constantinescu 
64935eca1a21SEmil Constantinescu   Level: developer
649493b34091SDebojyoti Ghosh 
6495e5168f73SEmil Constantinescu .keywords: TS, clone
6496e5168f73SEmil Constantinescu .seealso: TSCreate(), TSSetType(), TSSetUp(), TSDestroy(), TSSetProblemType()
649793b34091SDebojyoti Ghosh @*/
6498*baa10174SEmil Constantinescu PetscErrorCode  TSClone(TS tsin, TS *tsout)
649993b34091SDebojyoti Ghosh {
650093b34091SDebojyoti Ghosh   TS             t;
650193b34091SDebojyoti Ghosh   PetscErrorCode ierr;
650293b34091SDebojyoti Ghosh 
650393b34091SDebojyoti Ghosh   PetscFunctionBegin;
650493b34091SDebojyoti Ghosh   PetscValidPointer(tsin,1);
650593b34091SDebojyoti Ghosh   *tsout = NULL;
650693b34091SDebojyoti Ghosh 
6507*baa10174SEmil Constantinescu   ierr = PetscHeaderCreate(t, _p_TS, struct _TSOps, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView);CHKERRQ(ierr);
650893b34091SDebojyoti Ghosh 
650993b34091SDebojyoti Ghosh   /* General TS description */
651093b34091SDebojyoti Ghosh   t->numbermonitors    = 0;
651193b34091SDebojyoti Ghosh   t->setupcalled       = 0;
651293b34091SDebojyoti Ghosh   t->ksp_its           = 0;
651393b34091SDebojyoti Ghosh   t->snes_its          = 0;
651493b34091SDebojyoti Ghosh   t->nwork             = 0;
651593b34091SDebojyoti Ghosh   t->rhsjacobian.time  = -1e20;
651693b34091SDebojyoti Ghosh   t->rhsjacobian.scale = 1.;
651793b34091SDebojyoti Ghosh   t->ijacobian.shift   = 1.;
651893b34091SDebojyoti Ghosh 
651934561852SEmil Constantinescu   SNES snes_start;
652093b34091SDebojyoti Ghosh   DM   dm;
652134561852SEmil Constantinescu   ierr = TSGetSNES(tsin,&snes_start);                   CHKERRQ(ierr);
652234561852SEmil Constantinescu   ierr = TSSetSNES(t,snes_start);                       CHKERRQ(ierr);
6523d15a3a53SEmil Constantinescu 
652493b34091SDebojyoti Ghosh   ierr = TSGetDM(tsin,&dm);                             CHKERRQ(ierr);
652593b34091SDebojyoti Ghosh   ierr = TSSetDM(t,dm);                                 CHKERRQ(ierr);
652693b34091SDebojyoti Ghosh 
652793b34091SDebojyoti Ghosh   t->adapt=tsin->adapt;
652893b34091SDebojyoti Ghosh   PetscObjectReference((PetscObject)t->adapt);
652993b34091SDebojyoti Ghosh 
653093b34091SDebojyoti Ghosh   t->problem_type      = tsin->problem_type;
653193b34091SDebojyoti Ghosh   t->ptime             = tsin->ptime;
653293b34091SDebojyoti Ghosh   t->time_step         = tsin->time_step;
653393b34091SDebojyoti Ghosh   t->time_step_orig    = tsin->time_step_orig;
653493b34091SDebojyoti Ghosh   t->max_time          = tsin->max_time;
653593b34091SDebojyoti Ghosh   t->steps             = tsin->steps;
653693b34091SDebojyoti Ghosh   t->max_steps         = tsin->max_steps;
653793b34091SDebojyoti Ghosh   t->equation_type     = tsin->equation_type;
653893b34091SDebojyoti Ghosh   t->atol              = tsin->atol;
653993b34091SDebojyoti Ghosh   t->rtol              = tsin->rtol;
654093b34091SDebojyoti Ghosh   t->max_snes_failures = tsin->max_snes_failures;
654193b34091SDebojyoti Ghosh   t->max_reject        = tsin->max_reject;
654293b34091SDebojyoti Ghosh   t->errorifstepfailed = tsin->errorifstepfailed;
654393b34091SDebojyoti Ghosh 
654493b34091SDebojyoti Ghosh   TSType type;
654593b34091SDebojyoti Ghosh   ierr = TSGetType(tsin,&type); CHKERRQ(ierr);
654693b34091SDebojyoti Ghosh   ierr = TSSetType(t,type);     CHKERRQ(ierr);
654793b34091SDebojyoti Ghosh 
654893b34091SDebojyoti Ghosh   t->vec_sol           = NULL;
654993b34091SDebojyoti Ghosh 
655093b34091SDebojyoti Ghosh   t->cfltime          = tsin->cfltime;
655193b34091SDebojyoti Ghosh   t->cfltime_local    = tsin->cfltime_local;
655293b34091SDebojyoti Ghosh   t->exact_final_time = tsin->exact_final_time;
655393b34091SDebojyoti Ghosh 
655493b34091SDebojyoti Ghosh   ierr = PetscMemcpy(t->ops,tsin->ops,sizeof(struct _TSOps));CHKERRQ(ierr);
655593b34091SDebojyoti Ghosh 
655693b34091SDebojyoti Ghosh   *tsout = t;
655793b34091SDebojyoti Ghosh   PetscFunctionReturn(0);
655893b34091SDebojyoti Ghosh }
6559