xref: /petsc/src/ts/interface/ts.c (revision 3e4cdcaae173e6a501816f3cfc4322a7f21b5688)
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
36*3e4cdcaaSBarry Smith .  -ts_save_tracjectories - checkpoint the solution at each time-step
37*3e4cdcaaSBarry Smith .  -ts_max_steps <maxsteps> - maximum number of time-steps to take
38*3e4cdcaaSBarry Smith .  -ts_final_time <time> - maximum time to compute to
39*3e4cdcaaSBarry 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
46bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
47de06c3feSJed Brown .  -ts_monitor_lg_timestep - Monitor timestep size graphically
48de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
49de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
50de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
51de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
52de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
53de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
54*3e4cdcaaSBarry Smith .  -ts_monitor_draw_solution_phase  <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom
55*3e4cdcaaSBarry Smith .  -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction()
5691b97e58SBarry Smith .  -ts_monitor_solution_binary <filename> - Save each solution to a binary file
5791b97e58SBarry Smith -  -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
5891b97e58SBarry Smith 
5991b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
60bdad233fSMatthew Knepley 
61bdad233fSMatthew Knepley    Level: beginner
62bdad233fSMatthew Knepley 
63bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
64bdad233fSMatthew Knepley 
65a313700dSBarry Smith .seealso: TSGetType()
66bdad233fSMatthew Knepley @*/
677087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
68bdad233fSMatthew Knepley {
69bc952696SBarry Smith   PetscBool              opt,flg,tflg;
70dfbe8321SBarry Smith   PetscErrorCode         ierr;
71649052a6SBarry Smith   PetscViewer            monviewer;
72eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
73089b2837SJed Brown   SNES                   snes;
741c3436cfSJed Brown   TSAdapt                adapt;
7531748224SBarry Smith   PetscReal              time_step;
7649354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
77d1212d36SBarry Smith   char                   dir[16];
786991f827SBarry Smith   const char             *defaultType;
796991f827SBarry Smith   char                   typeName[256];
80bdad233fSMatthew Knepley 
81bdad233fSMatthew Knepley   PetscFunctionBegin;
820700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
833194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
846991f827SBarry Smith   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
856991f827SBarry Smith   else defaultType = TSEULER;
866991f827SBarry Smith 
876991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
886991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
896991f827SBarry Smith   if (opt) {
906991f827SBarry Smith     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
916991f827SBarry Smith   } else {
926991f827SBarry Smith     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
936991f827SBarry Smith   }
94bdad233fSMatthew Knepley 
95bdad233fSMatthew Knepley   /* Handle generic TS options */
96844594baSBarry Smith   if (ts->trajectory) tflg = PETSC_TRUE;
97844594baSBarry Smith   else tflg = PETSC_FALSE;
98bc952696SBarry Smith   ierr = PetscOptionsBool("-ts_save_trajectories","Checkpoint for adjoint sensitivity analysis","TSSetSaveTrajectories",tflg,&tflg,NULL);CHKERRQ(ierr);
99bc952696SBarry Smith   if (tflg) {ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);}
1000298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1010298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
1020298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
10331748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
10431748224SBarry Smith   if (flg) {
10531748224SBarry Smith     ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
10631748224SBarry Smith   }
10749354f04SShri 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);
10849354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1090298fd71SBarry 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);
1100298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1110298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1120298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1130298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
114bdad233fSMatthew Knepley 
11556f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
11656f85f32SBarry Smith   {
11756f85f32SBarry Smith   PetscBool set;
11856f85f32SBarry Smith   flg  = PETSC_FALSE;
11956f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
12056f85f32SBarry Smith   if (set) {
12156f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
12256f85f32SBarry Smith   }
12356f85f32SBarry Smith   }
12456f85f32SBarry Smith #endif
12556f85f32SBarry Smith 
126bdad233fSMatthew Knepley   /* Monitor options */
127a6570f20SBarry Smith   ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
128eabae89aSBarry Smith   if (flg) {
129ce94432eSBarry Smith     ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr);
130649052a6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
131bdad233fSMatthew Knepley   }
1325180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1335180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1345180491cSLisandro Dalcin 
1354f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
136a7cc72afSBarry Smith   if (opt) {
1370b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1383923b477SBarry Smith     PetscInt       howoften = 1;
139a80ad3e0SBarry Smith 
1400298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
141ce94432eSBarry Smith     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
1424f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
143b3603a34SBarry Smith   }
1444f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
145b3603a34SBarry Smith   if (opt) {
1460b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1473923b477SBarry Smith     PetscInt       howoften = 1;
148b3603a34SBarry Smith 
1490298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
15022d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1514f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
152bdad233fSMatthew Knepley   }
1534f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
154ef20d060SBarry Smith   if (opt) {
1550b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1563923b477SBarry Smith     PetscInt       howoften = 1;
157ef20d060SBarry Smith 
1580298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
15922d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1604f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
161ef20d060SBarry Smith   }
162201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
163201da799SBarry Smith   if (opt) {
164201da799SBarry Smith     TSMonitorLGCtx ctx;
165201da799SBarry Smith     PetscInt       howoften = 1;
166201da799SBarry Smith 
1670298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
16822d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
169201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
170201da799SBarry Smith   }
171201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
172201da799SBarry Smith   if (opt) {
173201da799SBarry Smith     TSMonitorLGCtx ctx;
174201da799SBarry Smith     PetscInt       howoften = 1;
175201da799SBarry Smith 
1760298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
17722d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
178201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
179201da799SBarry Smith   }
1808189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
1818189c53fSBarry Smith   if (opt) {
1828189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
1838189c53fSBarry Smith     PetscInt          howoften = 1;
1848189c53fSBarry Smith 
1850298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
18622d28d08SBarry Smith     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1878189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
1888189c53fSBarry Smith   }
189ef20d060SBarry Smith   opt  = PETSC_FALSE;
1900dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
191a7cc72afSBarry Smith   if (opt) {
19283a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
19383a4ac43SBarry Smith     PetscInt         howoften = 1;
194a80ad3e0SBarry Smith 
1950298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
196ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
19783a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
198bdad233fSMatthew Knepley   }
199fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2002d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2012d5ee99bSBarry Smith   if (opt) {
2022d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2032d5ee99bSBarry Smith     PetscReal        bounds[4];
2042d5ee99bSBarry Smith     PetscInt         n = 4;
2052d5ee99bSBarry Smith     PetscDraw        draw;
2062d5ee99bSBarry Smith 
2072d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2082d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2092d5ee99bSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr);
2102d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2112d5ee99bSBarry Smith     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
2124b363babSBarry Smith     ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr);
2134b363babSBarry Smith     ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
214f54adfc0SBarry Smith     ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2154b363babSBarry Smith     ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr);
21607995780SPeter Brune     /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */
2172d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2182d5ee99bSBarry Smith   }
2192d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2200dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2213a471f94SBarry Smith   if (opt) {
22283a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
22383a4ac43SBarry Smith     PetscInt         howoften = 1;
2243a471f94SBarry Smith 
2250298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
226ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
22783a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2283a471f94SBarry Smith   }
2293a471f94SBarry Smith   opt  = PETSC_FALSE;
23091b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
231fb1732b5SBarry Smith   if (flg) {
232fb1732b5SBarry Smith     PetscViewer ctx;
233fb1732b5SBarry Smith     if (monfilename[0]) {
234ce94432eSBarry Smith       ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
235c2fbc07fSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
236fb1732b5SBarry Smith     } else {
237ce94432eSBarry Smith       ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts));
2380298fd71SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr);
239fb1732b5SBarry Smith     }
240fb1732b5SBarry Smith   }
241ed81e22dSJed Brown   opt  = PETSC_FALSE;
24291b97e58SBarry 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);
243ed81e22dSJed Brown   if (flg) {
244ed81e22dSJed Brown     const char *ptr,*ptr2;
245ed81e22dSJed Brown     char       *filetemplate;
246ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
247ed81e22dSJed Brown     /* Do some cursory validation of the input. */
248ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
249ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
250ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
251ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
252ce94432eSBarry 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");
253ed81e22dSJed Brown       if (ptr2) break;
254ed81e22dSJed Brown     }
255ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
256ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
257ed81e22dSJed Brown   }
258bdad233fSMatthew Knepley 
259d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
260d1212d36SBarry Smith   if (flg) {
261d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
262d1212d36SBarry Smith     int                  ray = 0;
263d1212d36SBarry Smith     DMDADirection        ddir;
264d1212d36SBarry Smith     DM                   da;
265d1212d36SBarry Smith     PetscMPIInt          rank;
266d1212d36SBarry Smith 
267ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
268d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
269d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
270ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
271d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
272d1212d36SBarry Smith 
273d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
274b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
275d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
276d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
277ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
278d1212d36SBarry Smith     if (!rank) {
279d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
280d1212d36SBarry Smith     }
28151b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
282d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
283d1212d36SBarry Smith   }
28451b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
28551b4a12fSMatthew G. Knepley   if (flg) {
28651b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
28751b4a12fSMatthew G. Knepley     int                 ray = 0;
28851b4a12fSMatthew G. Knepley     DMDADirection       ddir;
28951b4a12fSMatthew G. Knepley     DM                  da;
29051b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
291d1212d36SBarry Smith 
29251b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
29351b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
29451b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
29551b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
29651b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
29751b4a12fSMatthew G. Knepley 
29851b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
299b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
30051b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
30151b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
30251b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
30351b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
30451b4a12fSMatthew G. Knepley   }
305a7a1495cSBarry Smith 
3067781c08eSBarry Smith   /*
3077781c08eSBarry Smith      This code is all wrong. One is creating objects inside the TSSetFromOptions() so if run with the options gui
3087781c08eSBarry Smith      will bleed memory. Also one is using a PetscOptionsBegin() inside a PetscOptionsBegin()
3097781c08eSBarry Smith   */
310552698daSJed Brown   ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
311e55864a3SBarry Smith   ierr = TSAdaptSetFromOptions(PetscOptionsObject,adapt);CHKERRQ(ierr);
312d763cef2SBarry Smith 
3136991f827SBarry Smith     /* Handle specific TS options */
3146991f827SBarry Smith   if (ts->ops->setfromoptions) {
3156991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
3166991f827SBarry Smith   }
3176991f827SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
3186991f827SBarry Smith 
3196991f827SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
3206991f827SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
321d763cef2SBarry Smith 
322bc952696SBarry Smith   if (ts->trajectory) {
323bc952696SBarry Smith     ierr = TSTrajectorySetFromOptions(ts->trajectory);CHKERRQ(ierr);
324bc952696SBarry Smith   }
325bc952696SBarry Smith 
3266991f827SBarry Smith   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3276991f827SBarry Smith   if (snes) {
3286991f827SBarry Smith     if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
3296991f827SBarry Smith     ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
330d763cef2SBarry Smith   }
331d763cef2SBarry Smith   PetscFunctionReturn(0);
332d763cef2SBarry Smith }
333d763cef2SBarry Smith 
334d763cef2SBarry Smith #undef __FUNCT__
335bc952696SBarry Smith #define __FUNCT__ "TSSetSaveTrajectory"
336d2daff3dSHong Zhang /*@
337bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
338d2daff3dSHong Zhang 
339d2daff3dSHong Zhang    Collective on TS
340d2daff3dSHong Zhang 
341d2daff3dSHong Zhang    Input Parameters:
342bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
343bc952696SBarry Smith 
344d2daff3dSHong Zhang 
345d2daff3dSHong Zhang    Level: intermediate
346d2daff3dSHong Zhang 
347bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve()
348d2daff3dSHong Zhang 
349bc952696SBarry Smith .keywords: TS, set, checkpoint,
350d2daff3dSHong Zhang @*/
351bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
352d2daff3dSHong Zhang {
353bc952696SBarry Smith   PetscErrorCode ierr;
354bc952696SBarry Smith 
355d2daff3dSHong Zhang   PetscFunctionBegin;
356d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
357bc952696SBarry Smith   if (!ts->trajectory) {
358bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
359bc952696SBarry Smith     /* should it set a default trajectory? */
360bc952696SBarry Smith   }
361d2daff3dSHong Zhang   PetscFunctionReturn(0);
362d2daff3dSHong Zhang }
363d2daff3dSHong Zhang 
364a7a1495cSBarry Smith #undef __FUNCT__
365a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian"
366a7a1495cSBarry Smith /*@
367a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
368a7a1495cSBarry Smith       set with TSSetRHSJacobian().
369a7a1495cSBarry Smith 
370a7a1495cSBarry Smith    Collective on TS and Vec
371a7a1495cSBarry Smith 
372a7a1495cSBarry Smith    Input Parameters:
373316643e7SJed Brown +  ts - the TS context
374a7a1495cSBarry Smith .  t - current timestep
3750910c330SBarry Smith -  U - input vector
376a7a1495cSBarry Smith 
377a7a1495cSBarry Smith    Output Parameters:
378a7a1495cSBarry Smith +  A - Jacobian matrix
379a7a1495cSBarry Smith .  B - optional preconditioning matrix
380a7a1495cSBarry Smith -  flag - flag indicating matrix structure
381a7a1495cSBarry Smith 
382a7a1495cSBarry Smith    Notes:
383a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
384a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
385a7a1495cSBarry Smith 
38694b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
387a7a1495cSBarry Smith    flag parameter.
388a7a1495cSBarry Smith 
389a7a1495cSBarry Smith    Level: developer
390a7a1495cSBarry Smith 
391a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
392a7a1495cSBarry Smith 
39394b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
394a7a1495cSBarry Smith @*/
395d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
396a7a1495cSBarry Smith {
397dfbe8321SBarry Smith   PetscErrorCode ierr;
398270bf2e7SJed Brown   PetscObjectState Ustate;
39924989b8cSPeter Brune   DM             dm;
400942e3340SBarry Smith   DMTS           tsdm;
40124989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
40224989b8cSPeter Brune   void           *ctx;
40324989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
404a7a1495cSBarry Smith 
405a7a1495cSBarry Smith   PetscFunctionBegin;
4060700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4070910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4080910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
40924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
410942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
41124989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
4120298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
41359e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
4140910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
4150e4ef248SJed Brown     PetscFunctionReturn(0);
416f8ede8e7SJed Brown   }
417d90be118SSean Farley 
418ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
419d90be118SSean Farley 
420e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
42194ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
42294ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
42394ab13aaSBarry Smith     if (A != B) {
42494ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
42594ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
426e1244c69SJed Brown     }
427e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
428e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
429e1244c69SJed Brown   }
430e1244c69SJed Brown 
43124989b8cSPeter Brune   if (rhsjacobianfunc) {
43294ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
433a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
434d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
435a7a1495cSBarry Smith     PetscStackPop;
43694ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
437a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
43894ab13aaSBarry Smith     PetscValidHeaderSpecific(A,MAT_CLASSID,4);
43994ab13aaSBarry Smith     PetscValidHeaderSpecific(B,MAT_CLASSID,5);
440ef66eb69SBarry Smith   } else {
44194ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
44294ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
443ef66eb69SBarry Smith   }
4440e4ef248SJed Brown   ts->rhsjacobian.time       = t;
4450910c330SBarry Smith   ts->rhsjacobian.X          = U;
44659e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
447a7a1495cSBarry Smith   PetscFunctionReturn(0);
448a7a1495cSBarry Smith }
449a7a1495cSBarry Smith 
4504a2ae208SSatish Balay #undef __FUNCT__
4514a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
452316643e7SJed Brown /*@
453d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
454d763cef2SBarry Smith 
455316643e7SJed Brown    Collective on TS and Vec
456316643e7SJed Brown 
457316643e7SJed Brown    Input Parameters:
458316643e7SJed Brown +  ts - the TS context
459316643e7SJed Brown .  t - current time
4600910c330SBarry Smith -  U - state vector
461316643e7SJed Brown 
462316643e7SJed Brown    Output Parameter:
463316643e7SJed Brown .  y - right hand side
464316643e7SJed Brown 
465316643e7SJed Brown    Note:
466316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
467316643e7SJed Brown    is used internally within the nonlinear solvers.
468316643e7SJed Brown 
469316643e7SJed Brown    Level: developer
470316643e7SJed Brown 
471316643e7SJed Brown .keywords: TS, compute
472316643e7SJed Brown 
473316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
474316643e7SJed Brown @*/
4750910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
476d763cef2SBarry Smith {
477dfbe8321SBarry Smith   PetscErrorCode ierr;
47824989b8cSPeter Brune   TSRHSFunction  rhsfunction;
47924989b8cSPeter Brune   TSIFunction    ifunction;
48024989b8cSPeter Brune   void           *ctx;
48124989b8cSPeter Brune   DM             dm;
48224989b8cSPeter Brune 
483d763cef2SBarry Smith   PetscFunctionBegin;
4840700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4850910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4860700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
48724989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
48824989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
4890298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
490d763cef2SBarry Smith 
491ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
492d763cef2SBarry Smith 
4930910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
49424989b8cSPeter Brune   if (rhsfunction) {
495d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
4960910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
497d763cef2SBarry Smith     PetscStackPop;
498214bc6a2SJed Brown   } else {
499214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
500b2cd27e8SJed Brown   }
50144a41b28SSean Farley 
5020910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
503d763cef2SBarry Smith   PetscFunctionReturn(0);
504d763cef2SBarry Smith }
505d763cef2SBarry Smith 
5064a2ae208SSatish Balay #undef __FUNCT__
507ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
508ef20d060SBarry Smith /*@
509ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
510ef20d060SBarry Smith 
511ef20d060SBarry Smith    Collective on TS and Vec
512ef20d060SBarry Smith 
513ef20d060SBarry Smith    Input Parameters:
514ef20d060SBarry Smith +  ts - the TS context
515ef20d060SBarry Smith -  t - current time
516ef20d060SBarry Smith 
517ef20d060SBarry Smith    Output Parameter:
5180910c330SBarry Smith .  U - the solution
519ef20d060SBarry Smith 
520ef20d060SBarry Smith    Note:
521ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
522ef20d060SBarry Smith    is used internally within the nonlinear solvers.
523ef20d060SBarry Smith 
524ef20d060SBarry Smith    Level: developer
525ef20d060SBarry Smith 
526ef20d060SBarry Smith .keywords: TS, compute
527ef20d060SBarry Smith 
528abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
529ef20d060SBarry Smith @*/
5300910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
531ef20d060SBarry Smith {
532ef20d060SBarry Smith   PetscErrorCode     ierr;
533ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
534ef20d060SBarry Smith   void               *ctx;
535ef20d060SBarry Smith   DM                 dm;
536ef20d060SBarry Smith 
537ef20d060SBarry Smith   PetscFunctionBegin;
538ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5390910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
540ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
541ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
542ef20d060SBarry Smith 
543ef20d060SBarry Smith   if (solutionfunction) {
5449b7cd975SBarry Smith     PetscStackPush("TS user solution function");
5450910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
546ef20d060SBarry Smith     PetscStackPop;
547ef20d060SBarry Smith   }
548ef20d060SBarry Smith   PetscFunctionReturn(0);
549ef20d060SBarry Smith }
5509b7cd975SBarry Smith #undef __FUNCT__
5519b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
5529b7cd975SBarry Smith /*@
5539b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
5549b7cd975SBarry Smith 
5559b7cd975SBarry Smith    Collective on TS and Vec
5569b7cd975SBarry Smith 
5579b7cd975SBarry Smith    Input Parameters:
5589b7cd975SBarry Smith +  ts - the TS context
5599b7cd975SBarry Smith -  t - current time
5609b7cd975SBarry Smith 
5619b7cd975SBarry Smith    Output Parameter:
5629b7cd975SBarry Smith .  U - the function value
5639b7cd975SBarry Smith 
5649b7cd975SBarry Smith    Note:
5659b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
5669b7cd975SBarry Smith    is used internally within the nonlinear solvers.
5679b7cd975SBarry Smith 
5689b7cd975SBarry Smith    Level: developer
5699b7cd975SBarry Smith 
5709b7cd975SBarry Smith .keywords: TS, compute
5719b7cd975SBarry Smith 
5729b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
5739b7cd975SBarry Smith @*/
5749b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
5759b7cd975SBarry Smith {
5769b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
5779b7cd975SBarry Smith   void               *ctx;
5789b7cd975SBarry Smith   DM                 dm;
5799b7cd975SBarry Smith 
5809b7cd975SBarry Smith   PetscFunctionBegin;
5819b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5829b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5839b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
5849b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
5859b7cd975SBarry Smith 
5869b7cd975SBarry Smith   if (forcing) {
5879b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
5889b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
5899b7cd975SBarry Smith     PetscStackPop;
5909b7cd975SBarry Smith   }
5919b7cd975SBarry Smith   PetscFunctionReturn(0);
5929b7cd975SBarry Smith }
593ef20d060SBarry Smith 
594ef20d060SBarry Smith #undef __FUNCT__
595214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
596214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
597214bc6a2SJed Brown {
5982dd45cf8SJed Brown   Vec            F;
599214bc6a2SJed Brown   PetscErrorCode ierr;
600214bc6a2SJed Brown 
601214bc6a2SJed Brown   PetscFunctionBegin;
6020298fd71SBarry Smith   *Frhs = NULL;
6030298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
604214bc6a2SJed Brown   if (!ts->Frhs) {
6052dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
606214bc6a2SJed Brown   }
607214bc6a2SJed Brown   *Frhs = ts->Frhs;
608214bc6a2SJed Brown   PetscFunctionReturn(0);
609214bc6a2SJed Brown }
610214bc6a2SJed Brown 
611214bc6a2SJed Brown #undef __FUNCT__
612214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
613214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
614214bc6a2SJed Brown {
615214bc6a2SJed Brown   Mat            A,B;
6162dd45cf8SJed Brown   PetscErrorCode ierr;
617214bc6a2SJed Brown 
618214bc6a2SJed Brown   PetscFunctionBegin;
619c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
620c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
6210298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
622214bc6a2SJed Brown   if (Arhs) {
623214bc6a2SJed Brown     if (!ts->Arhs) {
624214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
625214bc6a2SJed Brown     }
626214bc6a2SJed Brown     *Arhs = ts->Arhs;
627214bc6a2SJed Brown   }
628214bc6a2SJed Brown   if (Brhs) {
629214bc6a2SJed Brown     if (!ts->Brhs) {
630bdb70873SJed Brown       if (A != B) {
631214bc6a2SJed Brown         ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
632bdb70873SJed Brown       } else {
633bdb70873SJed Brown         ts->Brhs = ts->Arhs;
634bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
635bdb70873SJed Brown       }
636214bc6a2SJed Brown     }
637214bc6a2SJed Brown     *Brhs = ts->Brhs;
638214bc6a2SJed Brown   }
639214bc6a2SJed Brown   PetscFunctionReturn(0);
640214bc6a2SJed Brown }
641214bc6a2SJed Brown 
642214bc6a2SJed Brown #undef __FUNCT__
643316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
644316643e7SJed Brown /*@
6450910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
646316643e7SJed Brown 
647316643e7SJed Brown    Collective on TS and Vec
648316643e7SJed Brown 
649316643e7SJed Brown    Input Parameters:
650316643e7SJed Brown +  ts - the TS context
651316643e7SJed Brown .  t - current time
6520910c330SBarry Smith .  U - state vector
6530910c330SBarry Smith .  Udot - time derivative of state vector
654214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
655316643e7SJed Brown 
656316643e7SJed Brown    Output Parameter:
657316643e7SJed Brown .  Y - right hand side
658316643e7SJed Brown 
659316643e7SJed Brown    Note:
660316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
661316643e7SJed Brown    is used internally within the nonlinear solvers.
662316643e7SJed Brown 
663316643e7SJed Brown    If the user did did not write their equations in implicit form, this
664316643e7SJed Brown    function recasts them in implicit form.
665316643e7SJed Brown 
666316643e7SJed Brown    Level: developer
667316643e7SJed Brown 
668316643e7SJed Brown .keywords: TS, compute
669316643e7SJed Brown 
670316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
671316643e7SJed Brown @*/
6720910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
673316643e7SJed Brown {
674316643e7SJed Brown   PetscErrorCode ierr;
67524989b8cSPeter Brune   TSIFunction    ifunction;
67624989b8cSPeter Brune   TSRHSFunction  rhsfunction;
67724989b8cSPeter Brune   void           *ctx;
67824989b8cSPeter Brune   DM             dm;
679316643e7SJed Brown 
680316643e7SJed Brown   PetscFunctionBegin;
6810700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6820910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6830910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
6840700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
685316643e7SJed Brown 
68624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
68724989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
6880298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
68924989b8cSPeter Brune 
690ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
691d90be118SSean Farley 
6920910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
69324989b8cSPeter Brune   if (ifunction) {
694316643e7SJed Brown     PetscStackPush("TS user implicit function");
6950910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
696316643e7SJed Brown     PetscStackPop;
697214bc6a2SJed Brown   }
698214bc6a2SJed Brown   if (imex) {
69924989b8cSPeter Brune     if (!ifunction) {
7000910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
7012dd45cf8SJed Brown     }
70224989b8cSPeter Brune   } else if (rhsfunction) {
70324989b8cSPeter Brune     if (ifunction) {
704214bc6a2SJed Brown       Vec Frhs;
705214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
7060910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
707214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
7082dd45cf8SJed Brown     } else {
7090910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
7100910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
711316643e7SJed Brown     }
7124a6899ffSJed Brown   }
7130910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
714316643e7SJed Brown   PetscFunctionReturn(0);
715316643e7SJed Brown }
716316643e7SJed Brown 
717316643e7SJed Brown #undef __FUNCT__
718316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
719316643e7SJed Brown /*@
720316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
721316643e7SJed Brown 
722316643e7SJed Brown    Collective on TS and Vec
723316643e7SJed Brown 
724316643e7SJed Brown    Input
725316643e7SJed Brown       Input Parameters:
726316643e7SJed Brown +  ts - the TS context
727316643e7SJed Brown .  t - current timestep
7280910c330SBarry Smith .  U - state vector
7290910c330SBarry Smith .  Udot - time derivative of state vector
730214bc6a2SJed Brown .  shift - shift to apply, see note below
731214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
732316643e7SJed Brown 
733316643e7SJed Brown    Output Parameters:
734316643e7SJed Brown +  A - Jacobian matrix
735316643e7SJed Brown .  B - optional preconditioning matrix
736316643e7SJed Brown -  flag - flag indicating matrix structure
737316643e7SJed Brown 
738316643e7SJed Brown    Notes:
7390910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
740316643e7SJed Brown 
7410910c330SBarry Smith    dF/dU + shift*dF/dUdot
742316643e7SJed Brown 
743316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
744316643e7SJed Brown    is used internally within the nonlinear solvers.
745316643e7SJed Brown 
746316643e7SJed Brown    Level: developer
747316643e7SJed Brown 
748316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
749316643e7SJed Brown 
750316643e7SJed Brown .seealso:  TSSetIJacobian()
75163495f91SJed Brown @*/
752d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
753316643e7SJed Brown {
754316643e7SJed Brown   PetscErrorCode ierr;
75524989b8cSPeter Brune   TSIJacobian    ijacobian;
75624989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
75724989b8cSPeter Brune   DM             dm;
75824989b8cSPeter Brune   void           *ctx;
759316643e7SJed Brown 
760316643e7SJed Brown   PetscFunctionBegin;
7610700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7620910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7630910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
764316643e7SJed Brown   PetscValidPointer(A,6);
76594ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
766316643e7SJed Brown   PetscValidPointer(B,7);
76794ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
76824989b8cSPeter Brune 
76924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
77024989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
7710298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
77224989b8cSPeter Brune 
773ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
774316643e7SJed Brown 
77594ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
77624989b8cSPeter Brune   if (ijacobian) {
777316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
778d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
779316643e7SJed Brown     PetscStackPop;
780214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
78194ab13aaSBarry Smith     PetscValidHeaderSpecific(A,MAT_CLASSID,4);
78294ab13aaSBarry Smith     PetscValidHeaderSpecific(B,MAT_CLASSID,5);
7834a6899ffSJed Brown   }
784214bc6a2SJed Brown   if (imex) {
785b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
78694ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
78794ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
78894ab13aaSBarry Smith       if (A != B) {
78994ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
79094ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
791214bc6a2SJed Brown       }
792214bc6a2SJed Brown     }
793214bc6a2SJed Brown   } else {
794e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
795e1244c69SJed Brown     if (rhsjacobian) {
796bd373395SBarry Smith       if (ijacobian) {
797e1244c69SJed Brown         ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
798bd373395SBarry Smith       } else {
799bd373395SBarry Smith         ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr);
800e1244c69SJed Brown       }
801d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
802e1244c69SJed Brown     }
80394ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
804e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
805e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
80694ab13aaSBarry Smith       ierr = MatScale(A,-1);CHKERRQ(ierr);
80794ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
80894ab13aaSBarry Smith       if (A != B) {
80994ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
81094ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
811316643e7SJed Brown       }
812e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
813e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
814e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
81594ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
81694ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
81794ab13aaSBarry Smith         if (A != B) {
81894ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
81994ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
820e1244c69SJed Brown         }
821e1244c69SJed Brown       }
82294ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
82394ab13aaSBarry Smith       if (A != B) {
82494ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
825214bc6a2SJed Brown       }
826316643e7SJed Brown     }
827316643e7SJed Brown   }
82894ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
829316643e7SJed Brown   PetscFunctionReturn(0);
830316643e7SJed Brown }
831316643e7SJed Brown 
832316643e7SJed Brown #undef __FUNCT__
8334a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
834d763cef2SBarry Smith /*@C
835d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
836b5abc632SBarry Smith     where U_t = G(t,u).
837d763cef2SBarry Smith 
8383f9fe445SBarry Smith     Logically Collective on TS
839d763cef2SBarry Smith 
840d763cef2SBarry Smith     Input Parameters:
841d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
8420298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
843d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
844d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
8450298fd71SBarry Smith           function evaluation routine (may be NULL)
846d763cef2SBarry Smith 
847d763cef2SBarry Smith     Calling sequence of func:
84887828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
849d763cef2SBarry Smith 
850d763cef2SBarry Smith +   t - current timestep
851d763cef2SBarry Smith .   u - input vector
852d763cef2SBarry Smith .   F - function vector
853d763cef2SBarry Smith -   ctx - [optional] user-defined function context
854d763cef2SBarry Smith 
855d763cef2SBarry Smith     Level: beginner
856d763cef2SBarry Smith 
8572bbac0d3SBarry Smith     Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
8582bbac0d3SBarry Smith 
859d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
860d763cef2SBarry Smith 
861ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
862d763cef2SBarry Smith @*/
863089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
864d763cef2SBarry Smith {
865089b2837SJed Brown   PetscErrorCode ierr;
866089b2837SJed Brown   SNES           snes;
8670298fd71SBarry Smith   Vec            ralloc = NULL;
86824989b8cSPeter Brune   DM             dm;
869d763cef2SBarry Smith 
870089b2837SJed Brown   PetscFunctionBegin;
8710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
872ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
87324989b8cSPeter Brune 
87424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
87524989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
876089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
877e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
878e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
879e856ceecSJed Brown     r    = ralloc;
880e856ceecSJed Brown   }
881089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
882e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
883d763cef2SBarry Smith   PetscFunctionReturn(0);
884d763cef2SBarry Smith }
885d763cef2SBarry Smith 
8864a2ae208SSatish Balay #undef __FUNCT__
887ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
888ef20d060SBarry Smith /*@C
889abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
890ef20d060SBarry Smith 
891ef20d060SBarry Smith     Logically Collective on TS
892ef20d060SBarry Smith 
893ef20d060SBarry Smith     Input Parameters:
894ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
895ef20d060SBarry Smith .   f - routine for evaluating the solution
896ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
8970298fd71SBarry Smith           function evaluation routine (may be NULL)
898ef20d060SBarry Smith 
899ef20d060SBarry Smith     Calling sequence of func:
900ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
901ef20d060SBarry Smith 
902ef20d060SBarry Smith +   t - current timestep
903ef20d060SBarry Smith .   u - output vector
904ef20d060SBarry Smith -   ctx - [optional] user-defined function context
905ef20d060SBarry Smith 
906abd5a294SJed Brown     Notes:
907abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
908abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
909abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
910abd5a294SJed Brown 
9114f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
912abd5a294SJed Brown 
913ef20d060SBarry Smith     Level: beginner
914ef20d060SBarry Smith 
915ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
916ef20d060SBarry Smith 
9179b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
918ef20d060SBarry Smith @*/
919ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
920ef20d060SBarry Smith {
921ef20d060SBarry Smith   PetscErrorCode ierr;
922ef20d060SBarry Smith   DM             dm;
923ef20d060SBarry Smith 
924ef20d060SBarry Smith   PetscFunctionBegin;
925ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
926ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
927ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
928ef20d060SBarry Smith   PetscFunctionReturn(0);
929ef20d060SBarry Smith }
930ef20d060SBarry Smith 
931ef20d060SBarry Smith #undef __FUNCT__
9329b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
9339b7cd975SBarry Smith /*@C
9349b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
9359b7cd975SBarry Smith 
9369b7cd975SBarry Smith     Logically Collective on TS
9379b7cd975SBarry Smith 
9389b7cd975SBarry Smith     Input Parameters:
9399b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
9409b7cd975SBarry Smith .   f - routine for evaluating the forcing function
9419b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
9420298fd71SBarry Smith           function evaluation routine (may be NULL)
9439b7cd975SBarry Smith 
9449b7cd975SBarry Smith     Calling sequence of func:
9459b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
9469b7cd975SBarry Smith 
9479b7cd975SBarry Smith +   t - current timestep
9489b7cd975SBarry Smith .   u - output vector
9499b7cd975SBarry Smith -   ctx - [optional] user-defined function context
9509b7cd975SBarry Smith 
9519b7cd975SBarry Smith     Notes:
9529b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
9539b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
9549b7cd975SBarry Smith 
9559b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
9569b7cd975SBarry Smith 
9579b7cd975SBarry Smith     Level: beginner
9589b7cd975SBarry Smith 
9599b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
9609b7cd975SBarry Smith 
9619b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
9629b7cd975SBarry Smith @*/
9639b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
9649b7cd975SBarry Smith {
9659b7cd975SBarry Smith   PetscErrorCode ierr;
9669b7cd975SBarry Smith   DM             dm;
9679b7cd975SBarry Smith 
9689b7cd975SBarry Smith   PetscFunctionBegin;
9699b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9709b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
9719b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
9729b7cd975SBarry Smith   PetscFunctionReturn(0);
9739b7cd975SBarry Smith }
9749b7cd975SBarry Smith 
9759b7cd975SBarry Smith #undef __FUNCT__
9764a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
977d763cef2SBarry Smith /*@C
978f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
979b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
980d763cef2SBarry Smith 
9813f9fe445SBarry Smith    Logically Collective on TS
982d763cef2SBarry Smith 
983d763cef2SBarry Smith    Input Parameters:
984d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
985e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
986e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
987d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
988d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
9890298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
990d763cef2SBarry Smith 
991f7ab8db6SBarry Smith    Calling sequence of f:
992ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
993d763cef2SBarry Smith 
994d763cef2SBarry Smith +  t - current timestep
995d763cef2SBarry Smith .  u - input vector
996e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
997e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
998d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
999d763cef2SBarry Smith 
1000d763cef2SBarry Smith 
1001d763cef2SBarry Smith    Level: beginner
1002d763cef2SBarry Smith 
1003d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1004d763cef2SBarry Smith 
1005ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1006d763cef2SBarry Smith 
1007d763cef2SBarry Smith @*/
1008e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1009d763cef2SBarry Smith {
1010277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1011089b2837SJed Brown   SNES           snes;
101224989b8cSPeter Brune   DM             dm;
101324989b8cSPeter Brune   TSIJacobian    ijacobian;
1014277b19d0SLisandro Dalcin 
1015d763cef2SBarry Smith   PetscFunctionBegin;
10160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1017e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1018e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1019e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1020e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1021d763cef2SBarry Smith 
102224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
102324989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1024e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1025e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1026e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1027e1244c69SJed Brown   }
10280298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1029089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10305f659677SPeter Brune   if (!ijacobian) {
1031e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
10320e4ef248SJed Brown   }
1033e5d3d808SBarry Smith   if (Amat) {
1034e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
10350e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1036bbd56ea5SKarl Rupp 
1037e5d3d808SBarry Smith     ts->Arhs = Amat;
10380e4ef248SJed Brown   }
1039e5d3d808SBarry Smith   if (Pmat) {
1040e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
10410e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1042bbd56ea5SKarl Rupp 
1043e5d3d808SBarry Smith     ts->Brhs = Pmat;
10440e4ef248SJed Brown   }
1045d763cef2SBarry Smith   PetscFunctionReturn(0);
1046d763cef2SBarry Smith }
1047d763cef2SBarry Smith 
1048316643e7SJed Brown 
1049316643e7SJed Brown #undef __FUNCT__
1050316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
1051316643e7SJed Brown /*@C
1052b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1053316643e7SJed Brown 
10543f9fe445SBarry Smith    Logically Collective on TS
1055316643e7SJed Brown 
1056316643e7SJed Brown    Input Parameters:
1057316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
10580298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1059316643e7SJed Brown .  f   - the function evaluation routine
10600298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1061316643e7SJed Brown 
1062316643e7SJed Brown    Calling sequence of f:
1063316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1064316643e7SJed Brown 
1065316643e7SJed Brown +  t   - time at step/stage being solved
1066316643e7SJed Brown .  u   - state vector
1067316643e7SJed Brown .  u_t - time derivative of state vector
1068316643e7SJed Brown .  F   - function vector
1069316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1070316643e7SJed Brown 
1071316643e7SJed Brown    Important:
10722bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1073316643e7SJed Brown 
1074316643e7SJed Brown    Level: beginner
1075316643e7SJed Brown 
1076316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1077316643e7SJed Brown 
1078d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1079316643e7SJed Brown @*/
1080089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
1081316643e7SJed Brown {
1082089b2837SJed Brown   PetscErrorCode ierr;
1083089b2837SJed Brown   SNES           snes;
10840298fd71SBarry Smith   Vec            resalloc = NULL;
108524989b8cSPeter Brune   DM             dm;
1086316643e7SJed Brown 
1087316643e7SJed Brown   PetscFunctionBegin;
10880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1089ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
109024989b8cSPeter Brune 
109124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
109224989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
109324989b8cSPeter Brune 
1094089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1095e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
1096e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
1097e856ceecSJed Brown     res  = resalloc;
1098e856ceecSJed Brown   }
1099089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
1100e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
1101089b2837SJed Brown   PetscFunctionReturn(0);
1102089b2837SJed Brown }
1103089b2837SJed Brown 
1104089b2837SJed Brown #undef __FUNCT__
1105089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1106089b2837SJed Brown /*@C
1107089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1108089b2837SJed Brown 
1109089b2837SJed Brown    Not Collective
1110089b2837SJed Brown 
1111089b2837SJed Brown    Input Parameter:
1112089b2837SJed Brown .  ts - the TS context
1113089b2837SJed Brown 
1114089b2837SJed Brown    Output Parameter:
11150298fd71SBarry Smith +  r - vector to hold residual (or NULL)
11160298fd71SBarry Smith .  func - the function to compute residual (or NULL)
11170298fd71SBarry Smith -  ctx - the function context (or NULL)
1118089b2837SJed Brown 
1119089b2837SJed Brown    Level: advanced
1120089b2837SJed Brown 
1121089b2837SJed Brown .keywords: TS, nonlinear, get, function
1122089b2837SJed Brown 
1123089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1124089b2837SJed Brown @*/
1125089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1126089b2837SJed Brown {
1127089b2837SJed Brown   PetscErrorCode ierr;
1128089b2837SJed Brown   SNES           snes;
112924989b8cSPeter Brune   DM             dm;
1130089b2837SJed Brown 
1131089b2837SJed Brown   PetscFunctionBegin;
1132089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1133089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11340298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
113524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
113624989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1137089b2837SJed Brown   PetscFunctionReturn(0);
1138089b2837SJed Brown }
1139089b2837SJed Brown 
1140089b2837SJed Brown #undef __FUNCT__
1141089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1142089b2837SJed Brown /*@C
1143089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1144089b2837SJed Brown 
1145089b2837SJed Brown    Not Collective
1146089b2837SJed Brown 
1147089b2837SJed Brown    Input Parameter:
1148089b2837SJed Brown .  ts - the TS context
1149089b2837SJed Brown 
1150089b2837SJed Brown    Output Parameter:
11510298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
11520298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
11530298fd71SBarry Smith -  ctx - the function context (or NULL)
1154089b2837SJed Brown 
1155089b2837SJed Brown    Level: advanced
1156089b2837SJed Brown 
1157089b2837SJed Brown .keywords: TS, nonlinear, get, function
1158089b2837SJed Brown 
11592bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1160089b2837SJed Brown @*/
1161089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1162089b2837SJed Brown {
1163089b2837SJed Brown   PetscErrorCode ierr;
1164089b2837SJed Brown   SNES           snes;
116524989b8cSPeter Brune   DM             dm;
1166089b2837SJed Brown 
1167089b2837SJed Brown   PetscFunctionBegin;
1168089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1169089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11700298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
117124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
117224989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1173316643e7SJed Brown   PetscFunctionReturn(0);
1174316643e7SJed Brown }
1175316643e7SJed Brown 
1176316643e7SJed Brown #undef __FUNCT__
1177316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1178316643e7SJed Brown /*@C
1179a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1180ae8867d6SBarry Smith         provided with TSSetIFunction().
1181316643e7SJed Brown 
11823f9fe445SBarry Smith    Logically Collective on TS
1183316643e7SJed Brown 
1184316643e7SJed Brown    Input Parameters:
1185316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1186e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1187e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1188316643e7SJed Brown .  f   - the Jacobian evaluation routine
11890298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1190316643e7SJed Brown 
1191316643e7SJed Brown    Calling sequence of f:
1192ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1193316643e7SJed Brown 
1194316643e7SJed Brown +  t    - time at step/stage being solved
11951b4a444bSJed Brown .  U    - state vector
11961b4a444bSJed Brown .  U_t  - time derivative of state vector
1197316643e7SJed Brown .  a    - shift
1198e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1199e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1200316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1201316643e7SJed Brown 
1202316643e7SJed Brown    Notes:
1203e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1204316643e7SJed Brown 
1205a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1206b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1207a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1208a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1209a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1210a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1211a4f0a591SBarry Smith 
1212316643e7SJed Brown    Level: beginner
1213316643e7SJed Brown 
1214316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1215316643e7SJed Brown 
1216ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1217316643e7SJed Brown 
1218316643e7SJed Brown @*/
1219e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1220316643e7SJed Brown {
1221316643e7SJed Brown   PetscErrorCode ierr;
1222089b2837SJed Brown   SNES           snes;
122324989b8cSPeter Brune   DM             dm;
1224316643e7SJed Brown 
1225316643e7SJed Brown   PetscFunctionBegin;
12260700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1227e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1228e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1229e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1230e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
123124989b8cSPeter Brune 
123224989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
123324989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
123424989b8cSPeter Brune 
1235089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1236e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1237316643e7SJed Brown   PetscFunctionReturn(0);
1238316643e7SJed Brown }
1239316643e7SJed Brown 
12404a2ae208SSatish Balay #undef __FUNCT__
1241e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse"
1242e1244c69SJed Brown /*@
1243e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1244e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1245e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1246e1244c69SJed Brown    not been changed by the TS.
1247e1244c69SJed Brown 
1248e1244c69SJed Brown    Logically Collective
1249e1244c69SJed Brown 
1250e1244c69SJed Brown    Input Arguments:
1251e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1252e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1253e1244c69SJed Brown 
1254e1244c69SJed Brown    Level: intermediate
1255e1244c69SJed Brown 
1256e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1257e1244c69SJed Brown @*/
1258e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1259e1244c69SJed Brown {
1260e1244c69SJed Brown   PetscFunctionBegin;
1261e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1262e1244c69SJed Brown   PetscFunctionReturn(0);
1263e1244c69SJed Brown }
1264e1244c69SJed Brown 
1265e1244c69SJed Brown #undef __FUNCT__
126655849f57SBarry Smith #define __FUNCT__ "TSLoad"
126755849f57SBarry Smith /*@C
126855849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
126955849f57SBarry Smith 
127055849f57SBarry Smith   Collective on PetscViewer
127155849f57SBarry Smith 
127255849f57SBarry Smith   Input Parameters:
127355849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
127455849f57SBarry Smith            some related function before a call to TSLoad().
127555849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
127655849f57SBarry Smith 
127755849f57SBarry Smith    Level: intermediate
127855849f57SBarry Smith 
127955849f57SBarry Smith   Notes:
128055849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
128155849f57SBarry Smith 
128255849f57SBarry Smith   Notes for advanced users:
128355849f57SBarry Smith   Most users should not need to know the details of the binary storage
128455849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
128555849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
128655849f57SBarry Smith   format is
128755849f57SBarry Smith .vb
128855849f57SBarry Smith      has not yet been determined
128955849f57SBarry Smith .ve
129055849f57SBarry Smith 
129155849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
129255849f57SBarry Smith @*/
1293f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
129455849f57SBarry Smith {
129555849f57SBarry Smith   PetscErrorCode ierr;
129655849f57SBarry Smith   PetscBool      isbinary;
129755849f57SBarry Smith   PetscInt       classid;
129855849f57SBarry Smith   char           type[256];
12992d53ad75SBarry Smith   DMTS           sdm;
1300ad6bc421SBarry Smith   DM             dm;
130155849f57SBarry Smith 
130255849f57SBarry Smith   PetscFunctionBegin;
1303f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
130455849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
130555849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
130655849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
130755849f57SBarry Smith 
130855849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1309ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
131055849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1311f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1312f2c2a1b9SBarry Smith   if (ts->ops->load) {
1313f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1314f2c2a1b9SBarry Smith   }
1315ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1316ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1317ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1318f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1319f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
13202d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13212d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
132255849f57SBarry Smith   PetscFunctionReturn(0);
132355849f57SBarry Smith }
132455849f57SBarry Smith 
13259804daf3SBarry Smith #include <petscdraw.h>
1326e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1327e04113cfSBarry Smith #include <petscviewersaws.h>
1328f05ece33SBarry Smith #endif
132955849f57SBarry Smith #undef __FUNCT__
13304a2ae208SSatish Balay #define __FUNCT__ "TSView"
13317e2c5f70SBarry Smith /*@C
1332d763cef2SBarry Smith     TSView - Prints the TS data structure.
1333d763cef2SBarry Smith 
13344c49b128SBarry Smith     Collective on TS
1335d763cef2SBarry Smith 
1336d763cef2SBarry Smith     Input Parameters:
1337d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1338d763cef2SBarry Smith -   viewer - visualization context
1339d763cef2SBarry Smith 
1340d763cef2SBarry Smith     Options Database Key:
1341d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1342d763cef2SBarry Smith 
1343d763cef2SBarry Smith     Notes:
1344d763cef2SBarry Smith     The available visualization contexts include
1345b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1346b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1347d763cef2SBarry Smith          output where only the first processor opens
1348d763cef2SBarry Smith          the file.  All other processors send their
1349d763cef2SBarry Smith          data to the first processor to print.
1350d763cef2SBarry Smith 
1351d763cef2SBarry Smith     The user can open an alternative visualization context with
1352b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1353d763cef2SBarry Smith 
1354d763cef2SBarry Smith     Level: beginner
1355d763cef2SBarry Smith 
1356d763cef2SBarry Smith .keywords: TS, timestep, view
1357d763cef2SBarry Smith 
1358b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1359d763cef2SBarry Smith @*/
13607087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1361d763cef2SBarry Smith {
1362dfbe8321SBarry Smith   PetscErrorCode ierr;
136319fd82e9SBarry Smith   TSType         type;
13642b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
13652d53ad75SBarry Smith   DMTS           sdm;
1366e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1367536b137fSBarry Smith   PetscBool      issaws;
1368f05ece33SBarry Smith #endif
1369d763cef2SBarry Smith 
1370d763cef2SBarry Smith   PetscFunctionBegin;
13710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13723050cee2SBarry Smith   if (!viewer) {
1373ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
13743050cee2SBarry Smith   }
13750700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1376c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1377fd16b177SBarry Smith 
1378251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1379251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
138055849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
13812b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1382e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1383536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1384f05ece33SBarry Smith #endif
138532077d6dSBarry Smith   if (iascii) {
1386dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
138777431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
13887c8652ddSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1389d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
13905ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1391c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1392d763cef2SBarry Smith     }
13935ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1394193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
13952d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13962d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1397d52bd9f3SBarry Smith     if (ts->ops->view) {
1398d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1399d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1400d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1401d52bd9f3SBarry Smith     }
14020f5bd95cSBarry Smith   } else if (isstring) {
1403a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1404b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
140555849f57SBarry Smith   } else if (isbinary) {
140655849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
140755849f57SBarry Smith     MPI_Comm    comm;
140855849f57SBarry Smith     PetscMPIInt rank;
140955849f57SBarry Smith     char        type[256];
141055849f57SBarry Smith 
141155849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
141255849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
141355849f57SBarry Smith     if (!rank) {
141455849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
141555849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
141655849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
141755849f57SBarry Smith     }
141855849f57SBarry Smith     if (ts->ops->view) {
141955849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
142055849f57SBarry Smith     }
1421f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1422f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
14232d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14242d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
14252b0a91c0SBarry Smith   } else if (isdraw) {
14262b0a91c0SBarry Smith     PetscDraw draw;
14272b0a91c0SBarry Smith     char      str[36];
142889fd9fafSBarry Smith     PetscReal x,y,bottom,h;
14292b0a91c0SBarry Smith 
14302b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
14312b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
14322b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
14332b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
14340298fd71SBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
143589fd9fafSBarry Smith     bottom = y - h;
14362b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
14372b0a91c0SBarry Smith     if (ts->ops->view) {
14382b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14392b0a91c0SBarry Smith     }
14402b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1441e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1442536b137fSBarry Smith   } else if (issaws) {
1443d45a07a7SBarry Smith     PetscMPIInt rank;
14442657e9d9SBarry Smith     const char  *name;
14452657e9d9SBarry Smith 
14462657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1447d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1448d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1449d45a07a7SBarry Smith       char       dir[1024];
1450d45a07a7SBarry Smith 
1451e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
1452a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
14532657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
14542657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
14552657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
1456d763cef2SBarry Smith     }
14570acecf5bSBarry Smith     if (ts->ops->view) {
14580acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14590acecf5bSBarry Smith     }
1460f05ece33SBarry Smith #endif
1461f05ece33SBarry Smith   }
1462f05ece33SBarry Smith 
1463b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1464251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1465b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1466d763cef2SBarry Smith   PetscFunctionReturn(0);
1467d763cef2SBarry Smith }
1468d763cef2SBarry Smith 
1469d763cef2SBarry Smith 
14704a2ae208SSatish Balay #undef __FUNCT__
14714a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1472b07ff414SBarry Smith /*@
1473d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1474d763cef2SBarry Smith    the timesteppers.
1475d763cef2SBarry Smith 
14763f9fe445SBarry Smith    Logically Collective on TS
1477d763cef2SBarry Smith 
1478d763cef2SBarry Smith    Input Parameters:
1479d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1480d763cef2SBarry Smith -  usrP - optional user context
1481d763cef2SBarry Smith 
1482d763cef2SBarry Smith    Level: intermediate
1483d763cef2SBarry Smith 
1484d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1485d763cef2SBarry Smith 
1486d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1487d763cef2SBarry Smith @*/
14887087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1489d763cef2SBarry Smith {
1490d763cef2SBarry Smith   PetscFunctionBegin;
14910700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1492d763cef2SBarry Smith   ts->user = usrP;
1493d763cef2SBarry Smith   PetscFunctionReturn(0);
1494d763cef2SBarry Smith }
1495d763cef2SBarry Smith 
14964a2ae208SSatish Balay #undef __FUNCT__
14974a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1498b07ff414SBarry Smith /*@
1499d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1500d763cef2SBarry Smith     timestepper.
1501d763cef2SBarry Smith 
1502d763cef2SBarry Smith     Not Collective
1503d763cef2SBarry Smith 
1504d763cef2SBarry Smith     Input Parameter:
1505d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1506d763cef2SBarry Smith 
1507d763cef2SBarry Smith     Output Parameter:
1508d763cef2SBarry Smith .   usrP - user context
1509d763cef2SBarry Smith 
1510d763cef2SBarry Smith     Level: intermediate
1511d763cef2SBarry Smith 
1512d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1513d763cef2SBarry Smith 
1514d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1515d763cef2SBarry Smith @*/
1516e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1517d763cef2SBarry Smith {
1518d763cef2SBarry Smith   PetscFunctionBegin;
15190700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1520e71120c6SJed Brown   *(void**)usrP = ts->user;
1521d763cef2SBarry Smith   PetscFunctionReturn(0);
1522d763cef2SBarry Smith }
1523d763cef2SBarry Smith 
15244a2ae208SSatish Balay #undef __FUNCT__
15254a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1526d763cef2SBarry Smith /*@
1527b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1528d763cef2SBarry Smith 
1529d763cef2SBarry Smith    Not Collective
1530d763cef2SBarry Smith 
1531d763cef2SBarry Smith    Input Parameter:
1532d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1533d763cef2SBarry Smith 
1534d763cef2SBarry Smith    Output Parameter:
1535b8123daeSJed Brown .  iter - number of steps completed so far
1536d763cef2SBarry Smith 
1537d763cef2SBarry Smith    Level: intermediate
1538d763cef2SBarry Smith 
1539d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
15409be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
1541d763cef2SBarry Smith @*/
15427087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1543d763cef2SBarry Smith {
1544d763cef2SBarry Smith   PetscFunctionBegin;
15450700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15464482741eSBarry Smith   PetscValidIntPointer(iter,2);
1547d763cef2SBarry Smith   *iter = ts->steps;
1548d763cef2SBarry Smith   PetscFunctionReturn(0);
1549d763cef2SBarry Smith }
1550d763cef2SBarry Smith 
15514a2ae208SSatish Balay #undef __FUNCT__
15524a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1553d763cef2SBarry Smith /*@
1554d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1555d763cef2SBarry Smith    as well as the initial time.
1556d763cef2SBarry Smith 
15573f9fe445SBarry Smith    Logically Collective on TS
1558d763cef2SBarry Smith 
1559d763cef2SBarry Smith    Input Parameters:
1560d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1561d763cef2SBarry Smith .  initial_time - the initial time
1562d763cef2SBarry Smith -  time_step - the size of the timestep
1563d763cef2SBarry Smith 
1564d763cef2SBarry Smith    Level: intermediate
1565d763cef2SBarry Smith 
1566d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1567d763cef2SBarry Smith 
1568d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1569d763cef2SBarry Smith @*/
15707087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1571d763cef2SBarry Smith {
1572e144a568SJed Brown   PetscErrorCode ierr;
1573e144a568SJed Brown 
1574d763cef2SBarry Smith   PetscFunctionBegin;
15750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1576e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1577d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1578d763cef2SBarry Smith   PetscFunctionReturn(0);
1579d763cef2SBarry Smith }
1580d763cef2SBarry Smith 
15814a2ae208SSatish Balay #undef __FUNCT__
15824a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1583d763cef2SBarry Smith /*@
1584d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1585d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1586d763cef2SBarry Smith 
15873f9fe445SBarry Smith    Logically Collective on TS
1588d763cef2SBarry Smith 
1589d763cef2SBarry Smith    Input Parameters:
1590d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1591d763cef2SBarry Smith -  time_step - the size of the timestep
1592d763cef2SBarry Smith 
1593d763cef2SBarry Smith    Level: intermediate
1594d763cef2SBarry Smith 
1595d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1596d763cef2SBarry Smith 
1597d763cef2SBarry Smith .keywords: TS, set, timestep
1598d763cef2SBarry Smith @*/
15997087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1600d763cef2SBarry Smith {
1601d763cef2SBarry Smith   PetscFunctionBegin;
16020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1603c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1604d763cef2SBarry Smith   ts->time_step      = time_step;
160531748224SBarry Smith   ts->time_step_orig = time_step;
1606d763cef2SBarry Smith   PetscFunctionReturn(0);
1607d763cef2SBarry Smith }
1608d763cef2SBarry Smith 
16094a2ae208SSatish Balay #undef __FUNCT__
1610a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1611a43b19c4SJed Brown /*@
161249354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
161349354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
161449354f04SShri Abhyankar      or just return at the final time TS computed.
1615a43b19c4SJed Brown 
1616a43b19c4SJed Brown   Logically Collective on TS
1617a43b19c4SJed Brown 
1618a43b19c4SJed Brown    Input Parameter:
1619a43b19c4SJed Brown +   ts - the time-step context
162049354f04SShri Abhyankar -   eftopt - exact final time option
1621a43b19c4SJed Brown 
1622a43b19c4SJed Brown    Level: beginner
1623a43b19c4SJed Brown 
1624a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1625a43b19c4SJed Brown @*/
162649354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1627a43b19c4SJed Brown {
1628a43b19c4SJed Brown   PetscFunctionBegin;
1629a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
163049354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
163149354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1632a43b19c4SJed Brown   PetscFunctionReturn(0);
1633a43b19c4SJed Brown }
1634a43b19c4SJed Brown 
1635a43b19c4SJed Brown #undef __FUNCT__
16364a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1637d763cef2SBarry Smith /*@
1638d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1639d763cef2SBarry Smith 
1640d763cef2SBarry Smith    Not Collective
1641d763cef2SBarry Smith 
1642d763cef2SBarry Smith    Input Parameter:
1643d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1644d763cef2SBarry Smith 
1645d763cef2SBarry Smith    Output Parameter:
1646d763cef2SBarry Smith .  dt - the current timestep size
1647d763cef2SBarry Smith 
1648d763cef2SBarry Smith    Level: intermediate
1649d763cef2SBarry Smith 
1650d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1651d763cef2SBarry Smith 
1652d763cef2SBarry Smith .keywords: TS, get, timestep
1653d763cef2SBarry Smith @*/
16547087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1655d763cef2SBarry Smith {
1656d763cef2SBarry Smith   PetscFunctionBegin;
16570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1658f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1659d763cef2SBarry Smith   *dt = ts->time_step;
1660d763cef2SBarry Smith   PetscFunctionReturn(0);
1661d763cef2SBarry Smith }
1662d763cef2SBarry Smith 
16634a2ae208SSatish Balay #undef __FUNCT__
16644a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1665d8e5e3e6SSatish Balay /*@
1666d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1667d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1668d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1669d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1670d763cef2SBarry Smith 
1671d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1672d763cef2SBarry Smith 
1673d763cef2SBarry Smith    Input Parameter:
1674d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1675d763cef2SBarry Smith 
1676d763cef2SBarry Smith    Output Parameter:
1677d763cef2SBarry Smith .  v - the vector containing the solution
1678d763cef2SBarry Smith 
1679d763cef2SBarry Smith    Level: intermediate
1680d763cef2SBarry Smith 
1681d763cef2SBarry Smith .seealso: TSGetTimeStep()
1682d763cef2SBarry Smith 
1683d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1684d763cef2SBarry Smith @*/
16857087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1686d763cef2SBarry Smith {
1687d763cef2SBarry Smith   PetscFunctionBegin;
16880700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
16894482741eSBarry Smith   PetscValidPointer(v,2);
16908737fe31SLisandro Dalcin   *v = ts->vec_sol;
1691d763cef2SBarry Smith   PetscFunctionReturn(0);
1692d763cef2SBarry Smith }
1693d763cef2SBarry Smith 
1694c235aa8dSHong Zhang #undef __FUNCT__
1695419a887dSBarry Smith #define __FUNCT__ "TSAdjointGetGradients"
1696c235aa8dSHong Zhang /*@
1697419a887dSBarry Smith    TSAdjointGetGradients - Returns the gradients from the TSAdjointSolve()
1698c235aa8dSHong Zhang 
1699c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
1700c235aa8dSHong Zhang 
1701c235aa8dSHong Zhang    Input Parameter:
1702c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
1703c235aa8dSHong Zhang 
1704c235aa8dSHong Zhang    Output Parameter:
1705419a887dSBarry Smith +  v - vectors containing the gradients with respect to the ODE/DAE solution variables
1706419a887dSBarry Smith -  w - vectors containing the gradients with respect to the problem parameters
1707c235aa8dSHong Zhang 
1708c235aa8dSHong Zhang    Level: intermediate
1709c235aa8dSHong Zhang 
1710c235aa8dSHong Zhang .seealso: TSGetTimeStep()
1711c235aa8dSHong Zhang 
1712c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
1713c235aa8dSHong Zhang @*/
17145bf8c567SBarry Smith PetscErrorCode  TSAdjointGetGradients(TS ts,PetscInt *numberadjs,Vec **v,Vec **w)
1715c235aa8dSHong Zhang {
1716c235aa8dSHong Zhang   PetscFunctionBegin;
1717c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1718b18ea86cSHong Zhang   if (numberadjs) *numberadjs = ts->numberadjs;
1719419a887dSBarry Smith   if (v)          *v          = ts->vecs_sensi;
1720419a887dSBarry Smith   if (w)          *w          = ts->vecs_sensip;
1721c235aa8dSHong Zhang   PetscFunctionReturn(0);
1722c235aa8dSHong Zhang }
1723c235aa8dSHong Zhang 
1724bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
17254a2ae208SSatish Balay #undef __FUNCT__
1726bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1727d8e5e3e6SSatish Balay /*@
1728bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1729d763cef2SBarry Smith 
1730bdad233fSMatthew Knepley   Not collective
1731d763cef2SBarry Smith 
1732bdad233fSMatthew Knepley   Input Parameters:
1733bdad233fSMatthew Knepley + ts   - The TS
1734bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1735d763cef2SBarry Smith .vb
17360910c330SBarry Smith          U_t - A U = 0      (linear)
17370910c330SBarry Smith          U_t - A(t) U = 0   (linear)
17380910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1739d763cef2SBarry Smith .ve
1740d763cef2SBarry Smith 
1741d763cef2SBarry Smith    Level: beginner
1742d763cef2SBarry Smith 
1743bdad233fSMatthew Knepley .keywords: TS, problem type
1744bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1745d763cef2SBarry Smith @*/
17467087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1747a7cc72afSBarry Smith {
17489e2a6581SJed Brown   PetscErrorCode ierr;
17499e2a6581SJed Brown 
1750d763cef2SBarry Smith   PetscFunctionBegin;
17510700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1752bdad233fSMatthew Knepley   ts->problem_type = type;
17539e2a6581SJed Brown   if (type == TS_LINEAR) {
17549e2a6581SJed Brown     SNES snes;
17559e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
17569e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
17579e2a6581SJed Brown   }
1758d763cef2SBarry Smith   PetscFunctionReturn(0);
1759d763cef2SBarry Smith }
1760d763cef2SBarry Smith 
1761bdad233fSMatthew Knepley #undef __FUNCT__
1762bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1763bdad233fSMatthew Knepley /*@C
1764bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1765bdad233fSMatthew Knepley 
1766bdad233fSMatthew Knepley   Not collective
1767bdad233fSMatthew Knepley 
1768bdad233fSMatthew Knepley   Input Parameter:
1769bdad233fSMatthew Knepley . ts   - The TS
1770bdad233fSMatthew Knepley 
1771bdad233fSMatthew Knepley   Output Parameter:
1772bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1773bdad233fSMatthew Knepley .vb
1774089b2837SJed Brown          M U_t = A U
1775089b2837SJed Brown          M(t) U_t = A(t) U
1776b5abc632SBarry Smith          F(t,U,U_t)
1777bdad233fSMatthew Knepley .ve
1778bdad233fSMatthew Knepley 
1779bdad233fSMatthew Knepley    Level: beginner
1780bdad233fSMatthew Knepley 
1781bdad233fSMatthew Knepley .keywords: TS, problem type
1782bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1783bdad233fSMatthew Knepley @*/
17847087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1785a7cc72afSBarry Smith {
1786bdad233fSMatthew Knepley   PetscFunctionBegin;
17870700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
17884482741eSBarry Smith   PetscValidIntPointer(type,2);
1789bdad233fSMatthew Knepley   *type = ts->problem_type;
1790bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1791bdad233fSMatthew Knepley }
1792d763cef2SBarry Smith 
17934a2ae208SSatish Balay #undef __FUNCT__
17944a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1795d763cef2SBarry Smith /*@
1796d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1797d763cef2SBarry Smith    of a timestepper.
1798d763cef2SBarry Smith 
1799d763cef2SBarry Smith    Collective on TS
1800d763cef2SBarry Smith 
1801d763cef2SBarry Smith    Input Parameter:
1802d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1803d763cef2SBarry Smith 
1804d763cef2SBarry Smith    Notes:
1805d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1806d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1807d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1808d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1809d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1810d763cef2SBarry Smith 
1811d763cef2SBarry Smith    Level: advanced
1812d763cef2SBarry Smith 
1813d763cef2SBarry Smith .keywords: TS, timestep, setup
1814d763cef2SBarry Smith 
1815d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1816d763cef2SBarry Smith @*/
18177087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1818d763cef2SBarry Smith {
1819dfbe8321SBarry Smith   PetscErrorCode ierr;
18206c6b9e74SPeter Brune   DM             dm;
18216c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
1822d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
18236c6b9e74SPeter Brune   TSIJacobian    ijac;
18246c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1825d763cef2SBarry Smith 
1826d763cef2SBarry Smith   PetscFunctionBegin;
18270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1828277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1829277b19d0SLisandro Dalcin 
18302c18e0fdSBarry Smith   ts->total_steps = 0;
18317adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
18329596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1833d763cef2SBarry Smith   }
1834277b19d0SLisandro Dalcin 
1835277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1836277b19d0SLisandro Dalcin 
1837c235aa8dSHong Zhang 
1838552698daSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
18391c3436cfSJed Brown 
1840e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
1841e1244c69SJed Brown     Mat Amat,Pmat;
1842e1244c69SJed Brown     SNES snes;
1843e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1844e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
1845e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
1846e1244c69SJed Brown      * have displaced the RHS matrix */
1847e1244c69SJed Brown     if (Amat == ts->Arhs) {
1848e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
1849e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
1850e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
1851e1244c69SJed Brown     }
1852e1244c69SJed Brown     if (Pmat == ts->Brhs) {
1853e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
1854e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
1855e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
1856e1244c69SJed Brown     }
1857e1244c69SJed Brown   }
1858277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1859000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1860277b19d0SLisandro Dalcin   }
1861277b19d0SLisandro Dalcin 
18626c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
18636c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
18646c6b9e74SPeter Brune    */
18656c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
18660298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
18676c6b9e74SPeter Brune   if (!func) {
18686c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
18696c6b9e74SPeter Brune   }
18706c6b9e74SPeter 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.
18716c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
18726c6b9e74SPeter Brune    */
18730298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
18740298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
18750298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
18766c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
18776c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
18786c6b9e74SPeter Brune   }
1879277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1880277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1881277b19d0SLisandro Dalcin }
1882277b19d0SLisandro Dalcin 
1883277b19d0SLisandro Dalcin #undef __FUNCT__
1884f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp"
1885f6a906c0SBarry Smith /*@
1886f6a906c0SBarry Smith    TSAdjointSetUp - Sets up the internal data structures for the later use
1887f6a906c0SBarry Smith    of an adjoint solver
1888f6a906c0SBarry Smith 
1889f6a906c0SBarry Smith    Collective on TS
1890f6a906c0SBarry Smith 
1891f6a906c0SBarry Smith    Input Parameter:
1892f6a906c0SBarry Smith .  ts - the TS context obtained from TSCreate()
1893f6a906c0SBarry Smith 
1894f6a906c0SBarry Smith    Notes:
1895f6a906c0SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1896f6a906c0SBarry Smith    TSSetUp(), since these actions will automatically occur during
1897f6a906c0SBarry Smith    the call to TSStep().  However, if one wishes to control this
1898f6a906c0SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1899f6a906c0SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1900f6a906c0SBarry Smith 
1901f6a906c0SBarry Smith    Level: advanced
1902f6a906c0SBarry Smith 
1903f6a906c0SBarry Smith .keywords: TS, timestep, setup
1904f6a906c0SBarry Smith 
1905f6a906c0SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1906f6a906c0SBarry Smith @*/
1907f6a906c0SBarry Smith PetscErrorCode  TSAdjointSetUp(TS ts)
1908f6a906c0SBarry Smith {
1909f6a906c0SBarry Smith   PetscErrorCode ierr;
1910f6a906c0SBarry Smith 
1911f6a906c0SBarry Smith   PetscFunctionBegin;
1912f6a906c0SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1913f6a906c0SBarry Smith   if (ts->adjointsetupcalled) PetscFunctionReturn(0);
1914419a887dSBarry Smith   if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSAdjointSetGradients() first");
191542f2b339SBarry Smith   if (ts->ops->adjointsetup) {
191642f2b339SBarry Smith     ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr);
1917f6a906c0SBarry Smith   }
1918f6a906c0SBarry Smith   ts->adjointsetupcalled = PETSC_TRUE;
1919f6a906c0SBarry Smith   PetscFunctionReturn(0);
1920f6a906c0SBarry Smith }
1921f6a906c0SBarry Smith 
1922f6a906c0SBarry Smith #undef __FUNCT__
1923277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1924277b19d0SLisandro Dalcin /*@
1925277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1926277b19d0SLisandro Dalcin 
1927277b19d0SLisandro Dalcin    Collective on TS
1928277b19d0SLisandro Dalcin 
1929277b19d0SLisandro Dalcin    Input Parameter:
1930277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1931277b19d0SLisandro Dalcin 
1932277b19d0SLisandro Dalcin    Level: beginner
1933277b19d0SLisandro Dalcin 
1934277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1935277b19d0SLisandro Dalcin 
1936277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1937277b19d0SLisandro Dalcin @*/
1938277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1939277b19d0SLisandro Dalcin {
1940277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1941277b19d0SLisandro Dalcin 
1942277b19d0SLisandro Dalcin   PetscFunctionBegin;
1943277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1944b18ea86cSHong Zhang 
1945277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1946277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1947277b19d0SLisandro Dalcin   }
1948277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1949bbd56ea5SKarl Rupp 
19504e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
19514e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1952214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
19536bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1954e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1955e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
195638637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1957f8fee759SHong Zhang   ts->vecs_sensi = 0;
1958f8fee759SHong Zhang   ts->vecs_sensip = 0;
1959ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
19602c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
196136eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
1962277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1963d763cef2SBarry Smith   PetscFunctionReturn(0);
1964d763cef2SBarry Smith }
1965d763cef2SBarry Smith 
19664a2ae208SSatish Balay #undef __FUNCT__
19674a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1968d8e5e3e6SSatish Balay /*@
1969d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1970d763cef2SBarry Smith    with TSCreate().
1971d763cef2SBarry Smith 
1972d763cef2SBarry Smith    Collective on TS
1973d763cef2SBarry Smith 
1974d763cef2SBarry Smith    Input Parameter:
1975d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1976d763cef2SBarry Smith 
1977d763cef2SBarry Smith    Level: beginner
1978d763cef2SBarry Smith 
1979d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1980d763cef2SBarry Smith 
1981d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1982d763cef2SBarry Smith @*/
19836bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1984d763cef2SBarry Smith {
19856849ba73SBarry Smith   PetscErrorCode ierr;
1986d763cef2SBarry Smith 
1987d763cef2SBarry Smith   PetscFunctionBegin;
19886bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
19896bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
19906bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
1991d763cef2SBarry Smith 
19926bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
1993277b19d0SLisandro Dalcin 
1994e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
1995e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
19966bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
19976d4c513bSLisandro Dalcin 
1998bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
1999bc952696SBarry Smith 
200084df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
2001aeb4809dSShri Abhyankar   if ((*ts)->event) {
2002aeb4809dSShri Abhyankar     ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr);
2003aeb4809dSShri Abhyankar   }
20046bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
20056bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
20066bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
20076d4c513bSLisandro Dalcin 
2008a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2009d763cef2SBarry Smith   PetscFunctionReturn(0);
2010d763cef2SBarry Smith }
2011d763cef2SBarry Smith 
20124a2ae208SSatish Balay #undef __FUNCT__
20134a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
2014d8e5e3e6SSatish Balay /*@
2015d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2016d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2017d763cef2SBarry Smith 
2018d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2019d763cef2SBarry Smith 
2020d763cef2SBarry Smith    Input Parameter:
2021d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2022d763cef2SBarry Smith 
2023d763cef2SBarry Smith    Output Parameter:
2024d763cef2SBarry Smith .  snes - the nonlinear solver context
2025d763cef2SBarry Smith 
2026d763cef2SBarry Smith    Notes:
2027d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2028d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
202994b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2030d763cef2SBarry Smith 
2031d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
20320298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2033d763cef2SBarry Smith 
2034d763cef2SBarry Smith    Level: beginner
2035d763cef2SBarry Smith 
2036d763cef2SBarry Smith .keywords: timestep, get, SNES
2037d763cef2SBarry Smith @*/
20387087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2039d763cef2SBarry Smith {
2040d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2041d372ba47SLisandro Dalcin 
2042d763cef2SBarry Smith   PetscFunctionBegin;
20430700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20444482741eSBarry Smith   PetscValidPointer(snes,2);
2045d372ba47SLisandro Dalcin   if (!ts->snes) {
2046ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
20470298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
20483bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2049d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2050496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
20519e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
20529e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
20539e2a6581SJed Brown     }
2054d372ba47SLisandro Dalcin   }
2055d763cef2SBarry Smith   *snes = ts->snes;
2056d763cef2SBarry Smith   PetscFunctionReturn(0);
2057d763cef2SBarry Smith }
2058d763cef2SBarry Smith 
20594a2ae208SSatish Balay #undef __FUNCT__
2060deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
2061deb2cd25SJed Brown /*@
2062deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2063deb2cd25SJed Brown 
2064deb2cd25SJed Brown    Collective
2065deb2cd25SJed Brown 
2066deb2cd25SJed Brown    Input Parameter:
2067deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2068deb2cd25SJed Brown -  snes - the nonlinear solver context
2069deb2cd25SJed Brown 
2070deb2cd25SJed Brown    Notes:
2071deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2072deb2cd25SJed Brown 
2073deb2cd25SJed Brown    Level: developer
2074deb2cd25SJed Brown 
2075deb2cd25SJed Brown .keywords: timestep, set, SNES
2076deb2cd25SJed Brown @*/
2077deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2078deb2cd25SJed Brown {
2079deb2cd25SJed Brown   PetscErrorCode ierr;
2080d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2081deb2cd25SJed Brown 
2082deb2cd25SJed Brown   PetscFunctionBegin;
2083deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2084deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2085deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2086deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2087bbd56ea5SKarl Rupp 
2088deb2cd25SJed Brown   ts->snes = snes;
2089bbd56ea5SKarl Rupp 
20900298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
20910298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2092740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
20930298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2094740132f1SEmil Constantinescu   }
2095deb2cd25SJed Brown   PetscFunctionReturn(0);
2096deb2cd25SJed Brown }
2097deb2cd25SJed Brown 
2098deb2cd25SJed Brown #undef __FUNCT__
209994b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
2100d8e5e3e6SSatish Balay /*@
210194b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2102d763cef2SBarry Smith    a TS (timestepper) context.
2103d763cef2SBarry Smith 
210494b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2105d763cef2SBarry Smith 
2106d763cef2SBarry Smith    Input Parameter:
2107d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2108d763cef2SBarry Smith 
2109d763cef2SBarry Smith    Output Parameter:
211094b7f48cSBarry Smith .  ksp - the nonlinear solver context
2111d763cef2SBarry Smith 
2112d763cef2SBarry Smith    Notes:
211394b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2114d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2115d763cef2SBarry Smith    KSP and PC contexts as well.
2116d763cef2SBarry Smith 
211794b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
21180298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2119d763cef2SBarry Smith 
2120d763cef2SBarry Smith    Level: beginner
2121d763cef2SBarry Smith 
212294b7f48cSBarry Smith .keywords: timestep, get, KSP
2123d763cef2SBarry Smith @*/
21247087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2125d763cef2SBarry Smith {
2126d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2127089b2837SJed Brown   SNES           snes;
2128d372ba47SLisandro Dalcin 
2129d763cef2SBarry Smith   PetscFunctionBegin;
21300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21314482741eSBarry Smith   PetscValidPointer(ksp,2);
213217186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2133e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2134089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2135089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2136d763cef2SBarry Smith   PetscFunctionReturn(0);
2137d763cef2SBarry Smith }
2138d763cef2SBarry Smith 
2139d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2140d763cef2SBarry Smith 
21414a2ae208SSatish Balay #undef __FUNCT__
2142adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
2143adb62b0dSMatthew Knepley /*@
2144adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2145adb62b0dSMatthew Knepley    maximum time for iteration.
2146adb62b0dSMatthew Knepley 
21473f9fe445SBarry Smith    Not Collective
2148adb62b0dSMatthew Knepley 
2149adb62b0dSMatthew Knepley    Input Parameters:
2150adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
21510298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
21520298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2153adb62b0dSMatthew Knepley 
2154adb62b0dSMatthew Knepley    Level: intermediate
2155adb62b0dSMatthew Knepley 
2156adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2157adb62b0dSMatthew Knepley @*/
21587087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2159adb62b0dSMatthew Knepley {
2160adb62b0dSMatthew Knepley   PetscFunctionBegin;
21610700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2162abc0a331SBarry Smith   if (maxsteps) {
21634482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2164adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2165adb62b0dSMatthew Knepley   }
2166abc0a331SBarry Smith   if (maxtime) {
21674482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2168adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2169adb62b0dSMatthew Knepley   }
2170adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2171adb62b0dSMatthew Knepley }
2172adb62b0dSMatthew Knepley 
2173adb62b0dSMatthew Knepley #undef __FUNCT__
21744a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
2175d763cef2SBarry Smith /*@
2176d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2177d763cef2SBarry Smith    maximum time for iteration.
2178d763cef2SBarry Smith 
21793f9fe445SBarry Smith    Logically Collective on TS
2180d763cef2SBarry Smith 
2181d763cef2SBarry Smith    Input Parameters:
2182d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2183d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2184d763cef2SBarry Smith -  maxtime - final time to iterate to
2185d763cef2SBarry Smith 
2186d763cef2SBarry Smith    Options Database Keys:
2187d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
21883bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2189d763cef2SBarry Smith 
2190d763cef2SBarry Smith    Notes:
2191d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2192d763cef2SBarry Smith 
2193d763cef2SBarry Smith    Level: intermediate
2194d763cef2SBarry Smith 
2195d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2196a43b19c4SJed Brown 
2197a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2198d763cef2SBarry Smith @*/
21997087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2200d763cef2SBarry Smith {
2201d763cef2SBarry Smith   PetscFunctionBegin;
22020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2203c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2204c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
220539b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
220639b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2207d763cef2SBarry Smith   PetscFunctionReturn(0);
2208d763cef2SBarry Smith }
2209d763cef2SBarry Smith 
22104a2ae208SSatish Balay #undef __FUNCT__
22114a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2212d763cef2SBarry Smith /*@
2213d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2214d763cef2SBarry Smith    for use by the TS routines.
2215d763cef2SBarry Smith 
22163f9fe445SBarry Smith    Logically Collective on TS and Vec
2217d763cef2SBarry Smith 
2218d763cef2SBarry Smith    Input Parameters:
2219d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
22200910c330SBarry Smith -  u - the solution vector
2221d763cef2SBarry Smith 
2222d763cef2SBarry Smith    Level: beginner
2223d763cef2SBarry Smith 
2224d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2225d763cef2SBarry Smith @*/
22260910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2227d763cef2SBarry Smith {
22288737fe31SLisandro Dalcin   PetscErrorCode ierr;
2229496e6a7aSJed Brown   DM             dm;
22308737fe31SLisandro Dalcin 
2231d763cef2SBarry Smith   PetscFunctionBegin;
22320700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22330910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
22340910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
22356bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2236bbd56ea5SKarl Rupp 
22370910c330SBarry Smith   ts->vec_sol = u;
2238bbd56ea5SKarl Rupp 
2239496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
22400910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2241d763cef2SBarry Smith   PetscFunctionReturn(0);
2242d763cef2SBarry Smith }
2243d763cef2SBarry Smith 
2244e74ef692SMatthew Knepley #undef __FUNCT__
224573b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps"
224673b18844SBarry Smith /*@
224773b18844SBarry Smith    TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time
224873b18844SBarry Smith 
224973b18844SBarry Smith    Logically Collective on TS
225073b18844SBarry Smith 
225173b18844SBarry Smith    Input Parameters:
225273b18844SBarry Smith +  ts - the TS context obtained from TSCreate()
225373b18844SBarry Smith .  steps - number of steps to use
225473b18844SBarry Smith 
225573b18844SBarry Smith    Level: intermediate
225673b18844SBarry Smith 
225773b18844SBarry Smith    Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this
225873b18844SBarry Smith           so as to integrate back to less than the original timestep
225973b18844SBarry Smith 
226073b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations
226173b18844SBarry Smith 
226273b18844SBarry Smith .seealso: TSSetExactFinalTime()
226373b18844SBarry Smith @*/
226473b18844SBarry Smith PetscErrorCode  TSAdjointSetSteps(TS ts,PetscInt steps)
226573b18844SBarry Smith {
226673b18844SBarry Smith   PetscFunctionBegin;
226773b18844SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
226873b18844SBarry Smith   PetscValidLogicalCollectiveInt(ts,steps,2);
226973b18844SBarry Smith   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps");
227073b18844SBarry 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");
227173b18844SBarry Smith   ts->adjoint_max_steps = steps;
227273b18844SBarry Smith   PetscFunctionReturn(0);
227373b18844SBarry Smith }
227473b18844SBarry Smith 
227573b18844SBarry Smith #undef __FUNCT__
2276419a887dSBarry Smith #define __FUNCT__ "TSAdjointSetGradients"
2277c235aa8dSHong Zhang /*@
2278419a887dSBarry Smith    TSAdjointSetGradients - Sets the initial value of gradients w.r.t. initial conditions and w.r.t. the problem parameters  for use by the TS routines.
2279c235aa8dSHong Zhang 
2280c235aa8dSHong Zhang    Logically Collective on TS and Vec
2281c235aa8dSHong Zhang 
2282c235aa8dSHong Zhang    Input Parameters:
2283c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
2284419a887dSBarry Smith .  u - gradients with respect to the initial condition variables
2285419a887dSBarry Smith -  w - gradients with respect to the parameters
2286c235aa8dSHong Zhang 
2287c235aa8dSHong Zhang    Level: beginner
2288c235aa8dSHong Zhang 
2289c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2290c235aa8dSHong Zhang @*/
2291419a887dSBarry Smith PetscErrorCode  TSAdjointSetGradients(TS ts,PetscInt numberadjs,Vec *u,Vec *w)
2292c235aa8dSHong Zhang {
2293c235aa8dSHong Zhang   PetscFunctionBegin;
2294c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2295b18ea86cSHong Zhang   PetscValidPointer(u,2);
2296b18ea86cSHong Zhang   ts->vecs_sensi  = u;
2297419a887dSBarry Smith   ts->vecs_sensip = w;
2298b18ea86cSHong Zhang   ts->numberadjs  = numberadjs;
2299ad8e2604SHong Zhang   PetscFunctionReturn(0);
2300ad8e2604SHong Zhang }
2301ad8e2604SHong Zhang 
2302ad8e2604SHong Zhang #undef __FUNCT__
23035bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian"
2304ad8e2604SHong Zhang /*@C
23055bf8c567SBarry Smith   TSAdjointSetRHSJacobian - Sets the function that computes the Jacobian w.r.t. parameters.
2306ad8e2604SHong Zhang 
2307ad8e2604SHong Zhang   Logically Collective on TS
2308ad8e2604SHong Zhang 
2309ad8e2604SHong Zhang   Input Parameters:
2310ad8e2604SHong Zhang + ts   - The TS context obtained from TSCreate()
2311ad8e2604SHong Zhang - func - The function
2312ad8e2604SHong Zhang 
2313ad8e2604SHong Zhang   Calling sequence of func:
231405755b9cSHong Zhang $ func (TS ts,PetscReal t,Vec u,Mat A,void *ctx);
231505755b9cSHong Zhang +   t - current timestep
231605755b9cSHong Zhang .   u - input vector
231705755b9cSHong Zhang .   A - output matrix
231805755b9cSHong Zhang -   ctx - [optional] user-defined function context
2319ad8e2604SHong Zhang 
2320ad8e2604SHong Zhang   Level: intermediate
2321ad8e2604SHong Zhang 
2322ad8e2604SHong Zhang .keywords: TS, sensitivity
2323ad8e2604SHong Zhang .seealso:
2324ad8e2604SHong Zhang @*/
23255bf8c567SBarry Smith PetscErrorCode  TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
2326ad8e2604SHong Zhang {
2327ad8e2604SHong Zhang   PetscErrorCode ierr;
2328ad8e2604SHong Zhang 
2329ad8e2604SHong Zhang   PetscFunctionBegin;
2330ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2331ad8e2604SHong Zhang   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
2332ad8e2604SHong Zhang 
2333ad8e2604SHong Zhang   ts->rhsjacobianp    = func;
2334ad8e2604SHong Zhang   ts->rhsjacobianpctx = ctx;
2335ad8e2604SHong Zhang   if(Amat) {
2336ad8e2604SHong Zhang     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
2337ad8e2604SHong Zhang     ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2338ad8e2604SHong Zhang 
2339ad8e2604SHong Zhang     ts->Jacp = Amat;
2340ad8e2604SHong Zhang   }
2341ad8e2604SHong Zhang   PetscFunctionReturn(0);
2342ad8e2604SHong Zhang }
2343ad8e2604SHong Zhang 
2344ad8e2604SHong Zhang #undef __FUNCT__
23455bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian"
2346ad8e2604SHong Zhang /*@
23475bf8c567SBarry Smith   TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function.
2348ad8e2604SHong Zhang 
2349ad8e2604SHong Zhang   Collective on TS
2350ad8e2604SHong Zhang 
2351ad8e2604SHong Zhang   Input Parameters:
2352ad8e2604SHong Zhang . ts   - The TS context obtained from TSCreate()
2353ad8e2604SHong Zhang 
2354ad8e2604SHong Zhang   Level: developer
2355ad8e2604SHong Zhang 
2356ad8e2604SHong Zhang .keywords: TS, sensitivity
23575bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian()
2358ad8e2604SHong Zhang @*/
23595bf8c567SBarry Smith PetscErrorCode  TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat)
2360ad8e2604SHong Zhang {
2361ad8e2604SHong Zhang   PetscErrorCode ierr;
2362ad8e2604SHong Zhang 
2363ad8e2604SHong Zhang   PetscFunctionBegin;
2364ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2365ad8e2604SHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
2366ad8e2604SHong Zhang   PetscValidPointer(Amat,4);
2367ad8e2604SHong Zhang 
2368ad8e2604SHong Zhang   PetscStackPush("TS user JacobianP function for sensitivity analysis");
2369ad8e2604SHong Zhang   ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr);
2370ad8e2604SHong Zhang   PetscStackPop;
2371ad8e2604SHong Zhang 
2372c235aa8dSHong Zhang   PetscFunctionReturn(0);
2373c235aa8dSHong Zhang }
2374c235aa8dSHong Zhang 
2375c235aa8dSHong Zhang #undef __FUNCT__
2376d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetCostIntegrand"
23776fd0887fSHong Zhang /*@C
23788a2f769dSBarry Smith     TSAdjointSetCostIntegrand - Sets the routine for evaluating the integral term in a cost function
23796fd0887fSHong Zhang 
23806fd0887fSHong Zhang     Logically Collective on TS
23816fd0887fSHong Zhang 
23826fd0887fSHong Zhang     Input Parameters:
23836fd0887fSHong Zhang +   ts - the TS context obtained from TSCreate()
23848a2f769dSBarry Smith .   numberadjs - number of gradients to be computed
23856fd0887fSHong Zhang .   fq - routine for evaluating the right-hand-side function
2386b612ec54SBarry Smith .   drdy - array of vectors to contain the gradient of the r's with respect to y, NULL if not a function of y
2387b612ec54SBarry Smith .   drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y
2388b612ec54SBarry Smith .   drdp - array of vectors to contain the gradient of the r's with respect to p, NULL if not a function of p
2389b612ec54SBarry Smith .   drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p
2390b612ec54SBarry Smith -   ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL)
23916fd0887fSHong Zhang 
2392b612ec54SBarry Smith     Calling sequence of fq:
2393a2ae3dd2SHong Zhang $     TSCostIntegrand(TS ts,PetscReal t,Vec u,PetscReal *f,void *ctx);
23946fd0887fSHong Zhang 
23956fd0887fSHong Zhang +   t - current timestep
23966fd0887fSHong Zhang .   u - input vector
239736eaed60SHong Zhang .   f - function vector
23986fd0887fSHong Zhang -   ctx - [optional] user-defined function context
23996fd0887fSHong Zhang 
2400b612ec54SBarry Smith    Calling sequence of drdyf:
2401b612ec54SBarry Smith $    PetscErroCode drdyf(TS ts,PetscReal t,Vec U,Vec *drdy,void *ctx);
2402b612ec54SBarry Smith 
2403b612ec54SBarry Smith    Calling sequence of drdpf:
2404b612ec54SBarry Smith $    PetscErroCode drdpf(TS ts,PetscReal t,Vec U,Vec *drdp,void *ctx);
2405b612ec54SBarry Smith 
2406b612ec54SBarry Smith     Level: intermediate
24076fd0887fSHong Zhang 
24086fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function
24096fd0887fSHong Zhang 
24105bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian(),TSAdjointGetGradients(), TSAdjointSetGradients()
24116fd0887fSHong Zhang @*/
24128a2f769dSBarry Smith PetscErrorCode  TSAdjointSetCostIntegrand(TS ts,PetscInt numberadjs,          PetscErrorCode (*fq)(TS,PetscReal,Vec,Vec,void*),
2413b612ec54SBarry Smith                                                                     Vec *drdy,PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*),
2414b612ec54SBarry Smith                                                                     Vec *drdp,PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),void *ctx)
24156fd0887fSHong Zhang {
24166fd0887fSHong Zhang   PetscErrorCode ierr;
24176fd0887fSHong Zhang 
24186fd0887fSHong Zhang   PetscFunctionBegin;
24196fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2420419a887dSBarry Smith   if (!ts->numberadjs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Call TSAdjointSetGradients() first so that the number of cost functions can be determined.");
2421419a887dSBarry Smith   if (ts->numberadjs && ts->numberadjs!=numberadjs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSAdjointSetCostIntegrand()) is inconsistent with the one set by TSAdjointSetGradients()");
24226fd0887fSHong Zhang 
24238a2f769dSBarry Smith   ierr                  = VecCreateSeq(PETSC_COMM_SELF,numberadjs,&ts->vec_costintegral);CHKERRQ(ierr);
24242c39e106SBarry Smith   ierr                  = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr);
2425e4680132SHong Zhang   ts->costintegrand     = fq;
242605755b9cSHong Zhang   ts->costintegrandctx  = ctx;
24276fd0887fSHong Zhang 
2428b612ec54SBarry Smith   ts->drdyfunction    = drdyf;
2429b612ec54SBarry Smith   ts->vecs_drdy       = drdy;
2430b612ec54SBarry Smith 
2431b612ec54SBarry Smith   ts->drdpfunction    = drdpf;
2432b612ec54SBarry Smith   ts->vecs_drdp       = drdp;
2433b612ec54SBarry Smith 
24346fd0887fSHong Zhang   PetscFunctionReturn(0);
24356fd0887fSHong Zhang }
24366fd0887fSHong Zhang 
24376fd0887fSHong Zhang #undef __FUNCT__
24382c39e106SBarry Smith #define __FUNCT__ "TSAdjointGetCostIntegral"
243936eaed60SHong Zhang /*@
24402c39e106SBarry Smith    TSAdjointGetCostIntegral - Returns the values of the integral term in the cost functions.
244136eaed60SHong Zhang    It is valid to call the routine after a backward run.
244236eaed60SHong Zhang 
244336eaed60SHong Zhang    Not Collective
244436eaed60SHong Zhang 
244536eaed60SHong Zhang    Input Parameter:
244636eaed60SHong Zhang .  ts - the TS context obtained from TSCreate()
244736eaed60SHong Zhang 
244836eaed60SHong Zhang    Output Parameter:
24492c39e106SBarry Smith .  v - the vector containing the integrals for each cost function
245036eaed60SHong Zhang 
245136eaed60SHong Zhang    Level: intermediate
245236eaed60SHong Zhang 
2453d4aa0a58SBarry Smith .seealso: TSAdjointSetCostIntegrand()
245436eaed60SHong Zhang 
245536eaed60SHong Zhang .keywords: TS, sensitivity analysis
245636eaed60SHong Zhang @*/
24572c39e106SBarry Smith PetscErrorCode  TSAdjointGetCostIntegral(TS ts,Vec *v)
245836eaed60SHong Zhang {
245936eaed60SHong Zhang   PetscFunctionBegin;
246036eaed60SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
246136eaed60SHong Zhang   PetscValidPointer(v,2);
24622c39e106SBarry Smith   *v = ts->vec_costintegral;
246336eaed60SHong Zhang   PetscFunctionReturn(0);
246436eaed60SHong Zhang }
246536eaed60SHong Zhang 
246636eaed60SHong Zhang #undef __FUNCT__
2467d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand"
24686fd0887fSHong Zhang /*@
24692c39e106SBarry Smith    TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions.
24706fd0887fSHong Zhang 
24716fd0887fSHong Zhang    Input Parameters:
24726fd0887fSHong Zhang +  ts - the TS context
24736fd0887fSHong Zhang .  t - current time
247405755b9cSHong Zhang -  U - state vector
24756fd0887fSHong Zhang 
24766fd0887fSHong Zhang    Output Parameter:
24776fd0887fSHong Zhang .  q - vector of size numberadjs to hold the outputs
24786fd0887fSHong Zhang 
24796fd0887fSHong Zhang    Note:
24806fd0887fSHong Zhang    Most users should not need to explicitly call this routine, as it
24816fd0887fSHong Zhang    is used internally within the sensitivity analysis context.
24826fd0887fSHong Zhang 
24836fd0887fSHong Zhang    Level: developer
24846fd0887fSHong Zhang 
24856fd0887fSHong Zhang .keywords: TS, compute
24866fd0887fSHong Zhang 
2487d4aa0a58SBarry Smith .seealso: TSAdjointSetCostIntegrand()
24886fd0887fSHong Zhang @*/
2489d4aa0a58SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec U,Vec q)
24906fd0887fSHong Zhang {
24916fd0887fSHong Zhang   PetscErrorCode ierr;
24926fd0887fSHong Zhang 
24936fd0887fSHong Zhang   PetscFunctionBegin;
24946fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
24956fd0887fSHong Zhang   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
24966fd0887fSHong Zhang   PetscValidHeaderSpecific(q,VEC_CLASSID,4);
24976fd0887fSHong Zhang 
24986fd0887fSHong Zhang   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,q,0);CHKERRQ(ierr);
2499e4680132SHong Zhang   if (ts->costintegrand) {
2500e4680132SHong Zhang     PetscStackPush("TS user integrand in the cost function");
250105755b9cSHong Zhang     ierr = (*ts->costintegrand)(ts,t,U,q,ts->costintegrandctx);CHKERRQ(ierr);
25026fd0887fSHong Zhang     PetscStackPop;
25036fd0887fSHong Zhang   } else {
25046fd0887fSHong Zhang     ierr = VecZeroEntries(q);CHKERRQ(ierr);
25056fd0887fSHong Zhang   }
25066fd0887fSHong Zhang 
25076fd0887fSHong Zhang   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,q,0);CHKERRQ(ierr);
25086fd0887fSHong Zhang   PetscFunctionReturn(0);
25096fd0887fSHong Zhang }
25106fd0887fSHong Zhang 
25116fd0887fSHong Zhang #undef __FUNCT__
2512d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction"
251305755b9cSHong Zhang /*@
2514d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.
251505755b9cSHong Zhang 
251605755b9cSHong Zhang   Collective on TS
251705755b9cSHong Zhang 
251805755b9cSHong Zhang   Input Parameters:
251905755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
252005755b9cSHong Zhang 
252105755b9cSHong Zhang   Notes:
2522d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
252305755b9cSHong Zhang   so most users would not generally call this routine themselves.
252405755b9cSHong Zhang 
252505755b9cSHong Zhang   Level: developer
252605755b9cSHong Zhang 
252705755b9cSHong Zhang .keywords: TS, sensitivity
2528d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction()
252905755b9cSHong Zhang @*/
2530d4aa0a58SBarry Smith PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec X,Vec *drdy)
253105755b9cSHong Zhang {
253205755b9cSHong Zhang   PetscErrorCode ierr;
253305755b9cSHong Zhang 
253405755b9cSHong Zhang   PetscFunctionBegin;
253505755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
253605755b9cSHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
253705755b9cSHong Zhang 
253805755b9cSHong Zhang   PetscStackPush("TS user DRDY function for sensitivity analysis");
2539b612ec54SBarry Smith   ierr = (*ts->drdyfunction)(ts,t,X,drdy,ts->costintegrandctx); CHKERRQ(ierr);
254005755b9cSHong Zhang   PetscStackPop;
254105755b9cSHong Zhang   PetscFunctionReturn(0);
254205755b9cSHong Zhang }
254305755b9cSHong Zhang 
254405755b9cSHong Zhang #undef __FUNCT__
2545d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction"
254605755b9cSHong Zhang /*@
2547d4aa0a58SBarry Smith   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function.
254805755b9cSHong Zhang 
254905755b9cSHong Zhang   Collective on TS
255005755b9cSHong Zhang 
255105755b9cSHong Zhang   Input Parameters:
255205755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
255305755b9cSHong Zhang 
255405755b9cSHong Zhang   Notes:
255505755b9cSHong Zhang   TSDRDPFunction() is typically used for sensitivity implementation,
255605755b9cSHong Zhang   so most users would not generally call this routine themselves.
255705755b9cSHong Zhang 
255805755b9cSHong Zhang   Level: developer
255905755b9cSHong Zhang 
256005755b9cSHong Zhang .keywords: TS, sensitivity
2561d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction()
256205755b9cSHong Zhang @*/
2563d4aa0a58SBarry Smith PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec X,Vec *drdp)
256405755b9cSHong Zhang {
256505755b9cSHong Zhang   PetscErrorCode ierr;
256605755b9cSHong Zhang 
256705755b9cSHong Zhang   PetscFunctionBegin;
256805755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
256905755b9cSHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
257005755b9cSHong Zhang 
257105755b9cSHong Zhang   PetscStackPush("TS user DRDP function for sensitivity analysis");
2572b612ec54SBarry Smith   ierr = (*ts->drdpfunction)(ts,t,X,drdp,ts->costintegrandctx); CHKERRQ(ierr);
257305755b9cSHong Zhang   PetscStackPop;
257405755b9cSHong Zhang   PetscFunctionReturn(0);
257505755b9cSHong Zhang }
257605755b9cSHong Zhang 
257705755b9cSHong Zhang #undef __FUNCT__
2578e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2579ac226902SBarry Smith /*@C
2580000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
25813f2090d5SJed Brown   called once at the beginning of each time step.
2582000e7ae3SMatthew Knepley 
25833f9fe445SBarry Smith   Logically Collective on TS
2584000e7ae3SMatthew Knepley 
2585000e7ae3SMatthew Knepley   Input Parameters:
2586000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2587000e7ae3SMatthew Knepley - func - The function
2588000e7ae3SMatthew Knepley 
2589000e7ae3SMatthew Knepley   Calling sequence of func:
2590000e7ae3SMatthew Knepley . func (TS ts);
2591000e7ae3SMatthew Knepley 
2592000e7ae3SMatthew Knepley   Level: intermediate
2593000e7ae3SMatthew Knepley 
2594b8123daeSJed Brown   Note:
2595b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2596b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2597b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2598b8123daeSJed Brown 
2599000e7ae3SMatthew Knepley .keywords: TS, timestep
26009be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
2601000e7ae3SMatthew Knepley @*/
26027087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2603000e7ae3SMatthew Knepley {
2604000e7ae3SMatthew Knepley   PetscFunctionBegin;
26050700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2606ae60f76fSBarry Smith   ts->prestep = func;
2607000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2608000e7ae3SMatthew Knepley }
2609000e7ae3SMatthew Knepley 
2610e74ef692SMatthew Knepley #undef __FUNCT__
26113f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
261209ee8438SJed Brown /*@
26133f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
26143f2090d5SJed Brown 
26153f2090d5SJed Brown   Collective on TS
26163f2090d5SJed Brown 
26173f2090d5SJed Brown   Input Parameters:
26183f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
26193f2090d5SJed Brown 
26203f2090d5SJed Brown   Notes:
26213f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
26223f2090d5SJed Brown   so most users would not generally call this routine themselves.
26233f2090d5SJed Brown 
26243f2090d5SJed Brown   Level: developer
26253f2090d5SJed Brown 
26263f2090d5SJed Brown .keywords: TS, timestep
26279be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
26283f2090d5SJed Brown @*/
26297087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
26303f2090d5SJed Brown {
26313f2090d5SJed Brown   PetscErrorCode ierr;
26323f2090d5SJed Brown 
26333f2090d5SJed Brown   PetscFunctionBegin;
26340700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2635ae60f76fSBarry Smith   if (ts->prestep) {
2636ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
2637312ce896SJed Brown   }
26383f2090d5SJed Brown   PetscFunctionReturn(0);
26393f2090d5SJed Brown }
26403f2090d5SJed Brown 
26413f2090d5SJed Brown #undef __FUNCT__
2642b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2643b8123daeSJed Brown /*@C
2644b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2645b8123daeSJed Brown   called once at the beginning of each stage.
2646b8123daeSJed Brown 
2647b8123daeSJed Brown   Logically Collective on TS
2648b8123daeSJed Brown 
2649b8123daeSJed Brown   Input Parameters:
2650b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2651b8123daeSJed Brown - func - The function
2652b8123daeSJed Brown 
2653b8123daeSJed Brown   Calling sequence of func:
2654b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2655b8123daeSJed Brown 
2656b8123daeSJed Brown   Level: intermediate
2657b8123daeSJed Brown 
2658b8123daeSJed Brown   Note:
2659b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2660b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2661b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2662b8123daeSJed Brown 
2663b8123daeSJed Brown .keywords: TS, timestep
26649be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2665b8123daeSJed Brown @*/
2666b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2667b8123daeSJed Brown {
2668b8123daeSJed Brown   PetscFunctionBegin;
2669b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2670ae60f76fSBarry Smith   ts->prestage = func;
2671b8123daeSJed Brown   PetscFunctionReturn(0);
2672b8123daeSJed Brown }
2673b8123daeSJed Brown 
2674b8123daeSJed Brown #undef __FUNCT__
26759be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage"
26769be3e283SDebojyoti Ghosh /*@C
26779be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
26789be3e283SDebojyoti Ghosh   called once at the end of each stage.
26799be3e283SDebojyoti Ghosh 
26809be3e283SDebojyoti Ghosh   Logically Collective on TS
26819be3e283SDebojyoti Ghosh 
26829be3e283SDebojyoti Ghosh   Input Parameters:
26839be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
26849be3e283SDebojyoti Ghosh - func - The function
26859be3e283SDebojyoti Ghosh 
26869be3e283SDebojyoti Ghosh   Calling sequence of func:
26879be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
26889be3e283SDebojyoti Ghosh 
26899be3e283SDebojyoti Ghosh   Level: intermediate
26909be3e283SDebojyoti Ghosh 
26919be3e283SDebojyoti Ghosh   Note:
26929be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
26939be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
26949be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
26959be3e283SDebojyoti Ghosh 
26969be3e283SDebojyoti Ghosh .keywords: TS, timestep
26979be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
26989be3e283SDebojyoti Ghosh @*/
26999be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
27009be3e283SDebojyoti Ghosh {
27019be3e283SDebojyoti Ghosh   PetscFunctionBegin;
27029be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
27039be3e283SDebojyoti Ghosh   ts->poststage = func;
27049be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
27059be3e283SDebojyoti Ghosh }
27069be3e283SDebojyoti Ghosh 
27079be3e283SDebojyoti Ghosh #undef __FUNCT__
2708b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2709b8123daeSJed Brown /*@
2710b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2711b8123daeSJed Brown 
2712b8123daeSJed Brown   Collective on TS
2713b8123daeSJed Brown 
2714b8123daeSJed Brown   Input Parameters:
2715b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
27169be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
2717b8123daeSJed Brown 
2718b8123daeSJed Brown   Notes:
2719b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2720b8123daeSJed Brown   most users would not generally call this routine themselves.
2721b8123daeSJed Brown 
2722b8123daeSJed Brown   Level: developer
2723b8123daeSJed Brown 
2724b8123daeSJed Brown .keywords: TS, timestep
27259be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
2726b8123daeSJed Brown @*/
2727b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2728b8123daeSJed Brown {
2729b8123daeSJed Brown   PetscErrorCode ierr;
2730b8123daeSJed Brown 
2731b8123daeSJed Brown   PetscFunctionBegin;
2732b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2733ae60f76fSBarry Smith   if (ts->prestage) {
2734ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
2735b8123daeSJed Brown   }
2736b8123daeSJed Brown   PetscFunctionReturn(0);
2737b8123daeSJed Brown }
2738b8123daeSJed Brown 
2739b8123daeSJed Brown #undef __FUNCT__
27409be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage"
27419be3e283SDebojyoti Ghosh /*@
27429be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
27439be3e283SDebojyoti Ghosh 
27449be3e283SDebojyoti Ghosh   Collective on TS
27459be3e283SDebojyoti Ghosh 
27469be3e283SDebojyoti Ghosh   Input Parameters:
27479be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
27489be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
27499be3e283SDebojyoti Ghosh   stageindex  - Stage number
27509be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
27519be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
27529be3e283SDebojyoti Ghosh 
27539be3e283SDebojyoti Ghosh   Notes:
27549be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
27559be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
27569be3e283SDebojyoti Ghosh 
27579be3e283SDebojyoti Ghosh   Level: developer
27589be3e283SDebojyoti Ghosh 
27599be3e283SDebojyoti Ghosh .keywords: TS, timestep
27609be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
27619be3e283SDebojyoti Ghosh @*/
27629be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
27639be3e283SDebojyoti Ghosh {
27649be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
27659be3e283SDebojyoti Ghosh 
27669be3e283SDebojyoti Ghosh   PetscFunctionBegin;
27679be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27684beae5d8SLisandro Dalcin   if (ts->poststage) {
27699be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
27709be3e283SDebojyoti Ghosh   }
27719be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
27729be3e283SDebojyoti Ghosh }
27739be3e283SDebojyoti Ghosh 
27749be3e283SDebojyoti Ghosh #undef __FUNCT__
2775e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2776ac226902SBarry Smith /*@C
2777000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
27783f2090d5SJed Brown   called once at the end of each time step.
2779000e7ae3SMatthew Knepley 
27803f9fe445SBarry Smith   Logically Collective on TS
2781000e7ae3SMatthew Knepley 
2782000e7ae3SMatthew Knepley   Input Parameters:
2783000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2784000e7ae3SMatthew Knepley - func - The function
2785000e7ae3SMatthew Knepley 
2786000e7ae3SMatthew Knepley   Calling sequence of func:
2787b8123daeSJed Brown $ func (TS ts);
2788000e7ae3SMatthew Knepley 
2789000e7ae3SMatthew Knepley   Level: intermediate
2790000e7ae3SMatthew Knepley 
2791000e7ae3SMatthew Knepley .keywords: TS, timestep
2792b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2793000e7ae3SMatthew Knepley @*/
27947087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2795000e7ae3SMatthew Knepley {
2796000e7ae3SMatthew Knepley   PetscFunctionBegin;
27970700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2798ae60f76fSBarry Smith   ts->poststep = func;
2799000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2800000e7ae3SMatthew Knepley }
2801000e7ae3SMatthew Knepley 
2802e74ef692SMatthew Knepley #undef __FUNCT__
28033f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
280409ee8438SJed Brown /*@
28053f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
28063f2090d5SJed Brown 
28073f2090d5SJed Brown   Collective on TS
28083f2090d5SJed Brown 
28093f2090d5SJed Brown   Input Parameters:
28103f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
28113f2090d5SJed Brown 
28123f2090d5SJed Brown   Notes:
28133f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
28143f2090d5SJed Brown   so most users would not generally call this routine themselves.
28153f2090d5SJed Brown 
28163f2090d5SJed Brown   Level: developer
28173f2090d5SJed Brown 
28183f2090d5SJed Brown .keywords: TS, timestep
28193f2090d5SJed Brown @*/
28207087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
28213f2090d5SJed Brown {
28223f2090d5SJed Brown   PetscErrorCode ierr;
28233f2090d5SJed Brown 
28243f2090d5SJed Brown   PetscFunctionBegin;
28250700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2826ae60f76fSBarry Smith   if (ts->poststep) {
2827ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
282872ac3e02SJed Brown   }
28293f2090d5SJed Brown   PetscFunctionReturn(0);
28303f2090d5SJed Brown }
28313f2090d5SJed Brown 
2832d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2833d763cef2SBarry Smith 
28344a2ae208SSatish Balay #undef __FUNCT__
2835a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2836d763cef2SBarry Smith /*@C
2837a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2838d763cef2SBarry Smith    timestep to display the iteration's  progress.
2839d763cef2SBarry Smith 
28403f9fe445SBarry Smith    Logically Collective on TS
2841d763cef2SBarry Smith 
2842d763cef2SBarry Smith    Input Parameters:
2843d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2844e213d8f1SJed Brown .  monitor - monitoring routine
2845329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
28460298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
2847b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
28480298fd71SBarry Smith           (may be NULL)
2849d763cef2SBarry Smith 
2850e213d8f1SJed Brown    Calling sequence of monitor:
28510910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2852d763cef2SBarry Smith 
2853d763cef2SBarry Smith +    ts - the TS context
285488c05cc5SBarry 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
285588c05cc5SBarry Smith                                been interpolated to)
28561f06c33eSBarry Smith .    time - current time
28570910c330SBarry Smith .    u - current iterate
2858d763cef2SBarry Smith -    mctx - [optional] monitoring context
2859d763cef2SBarry Smith 
2860d763cef2SBarry Smith    Notes:
2861d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2862d763cef2SBarry Smith    already has been loaded.
2863d763cef2SBarry Smith 
2864025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2865025f1a04SBarry Smith 
2866d763cef2SBarry Smith    Level: intermediate
2867d763cef2SBarry Smith 
2868d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2869d763cef2SBarry Smith 
2870a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2871d763cef2SBarry Smith @*/
2872c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2873d763cef2SBarry Smith {
2874d763cef2SBarry Smith   PetscFunctionBegin;
28750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
287617186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2877d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
28788704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2879d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2880d763cef2SBarry Smith   PetscFunctionReturn(0);
2881d763cef2SBarry Smith }
2882d763cef2SBarry Smith 
28834a2ae208SSatish Balay #undef __FUNCT__
2884a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2885d763cef2SBarry Smith /*@C
2886a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2887d763cef2SBarry Smith 
28883f9fe445SBarry Smith    Logically Collective on TS
2889d763cef2SBarry Smith 
2890d763cef2SBarry Smith    Input Parameters:
2891d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2892d763cef2SBarry Smith 
2893d763cef2SBarry Smith    Notes:
2894d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2895d763cef2SBarry Smith 
2896d763cef2SBarry Smith    Level: intermediate
2897d763cef2SBarry Smith 
2898d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2899d763cef2SBarry Smith 
2900a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2901d763cef2SBarry Smith @*/
29027087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2903d763cef2SBarry Smith {
2904d952e501SBarry Smith   PetscErrorCode ierr;
2905d952e501SBarry Smith   PetscInt       i;
2906d952e501SBarry Smith 
2907d763cef2SBarry Smith   PetscFunctionBegin;
29080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2909d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
29108704b422SBarry Smith     if (ts->monitordestroy[i]) {
29118704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2912d952e501SBarry Smith     }
2913d952e501SBarry Smith   }
2914d763cef2SBarry Smith   ts->numbermonitors = 0;
2915d763cef2SBarry Smith   PetscFunctionReturn(0);
2916d763cef2SBarry Smith }
2917d763cef2SBarry Smith 
29184a2ae208SSatish Balay #undef __FUNCT__
2919a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2920d8e5e3e6SSatish Balay /*@
2921a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
29225516499fSSatish Balay 
29235516499fSSatish Balay    Level: intermediate
292441251cbbSSatish Balay 
29255516499fSSatish Balay .keywords: TS, set, monitor
29265516499fSSatish Balay 
292741251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
292841251cbbSSatish Balay @*/
2929649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2930d763cef2SBarry Smith {
2931dfbe8321SBarry Smith   PetscErrorCode ierr;
2932ce94432eSBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts));
2933d132466eSBarry Smith 
2934d763cef2SBarry Smith   PetscFunctionBegin;
2935649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2936971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2937649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2938d763cef2SBarry Smith   PetscFunctionReturn(0);
2939d763cef2SBarry Smith }
2940d763cef2SBarry Smith 
29414a2ae208SSatish Balay #undef __FUNCT__
2942cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2943cd652676SJed Brown /*@
2944cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2945cd652676SJed Brown 
2946cd652676SJed Brown    Logically Collective on TS
2947cd652676SJed Brown 
2948cd652676SJed Brown    Input Argument:
2949cd652676SJed Brown .  ts - time stepping context
2950cd652676SJed Brown 
2951cd652676SJed Brown    Output Argument:
2952cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2953cd652676SJed Brown 
2954cd652676SJed Brown    Level: intermediate
2955cd652676SJed Brown 
2956cd652676SJed Brown .keywords: TS, set
2957cd652676SJed Brown 
2958cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2959cd652676SJed Brown @*/
2960cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2961cd652676SJed Brown {
2962cd652676SJed Brown   PetscFunctionBegin;
2963cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2964cd652676SJed Brown   ts->retain_stages = flg;
2965cd652676SJed Brown   PetscFunctionReturn(0);
2966cd652676SJed Brown }
2967cd652676SJed Brown 
2968cd652676SJed Brown #undef __FUNCT__
2969cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2970cd652676SJed Brown /*@
2971cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2972cd652676SJed Brown 
2973cd652676SJed Brown    Collective on TS
2974cd652676SJed Brown 
2975cd652676SJed Brown    Input Argument:
2976cd652676SJed Brown +  ts - time stepping context
2977cd652676SJed Brown -  t - time to interpolate to
2978cd652676SJed Brown 
2979cd652676SJed Brown    Output Argument:
29800910c330SBarry Smith .  U - state at given time
2981cd652676SJed Brown 
2982cd652676SJed Brown    Notes:
2983cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2984cd652676SJed Brown 
2985cd652676SJed Brown    Level: intermediate
2986cd652676SJed Brown 
2987cd652676SJed Brown    Developer Notes:
2988cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
2989cd652676SJed Brown 
2990cd652676SJed Brown .keywords: TS, set
2991cd652676SJed Brown 
2992cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
2993cd652676SJed Brown @*/
29940910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
2995cd652676SJed Brown {
2996cd652676SJed Brown   PetscErrorCode ierr;
2997cd652676SJed Brown 
2998cd652676SJed Brown   PetscFunctionBegin;
2999cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3000b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
30016712e2f1SBarry 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);
3002ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
30030910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3004cd652676SJed Brown   PetscFunctionReturn(0);
3005cd652676SJed Brown }
3006cd652676SJed Brown 
3007cd652676SJed Brown #undef __FUNCT__
30084a2ae208SSatish Balay #define __FUNCT__ "TSStep"
3009d763cef2SBarry Smith /*@
30106d9e5789SSean Farley    TSStep - Steps one time step
3011d763cef2SBarry Smith 
3012d763cef2SBarry Smith    Collective on TS
3013d763cef2SBarry Smith 
3014d763cef2SBarry Smith    Input Parameter:
3015d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3016d763cef2SBarry Smith 
301727829d71SBarry Smith    Level: developer
3018d763cef2SBarry Smith 
3019b8123daeSJed Brown    Notes:
302027829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
302127829d71SBarry Smith 
3022b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3023b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3024b8123daeSJed Brown 
302525cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
302625cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
302725cb2221SBarry Smith 
3028d763cef2SBarry Smith .keywords: TS, timestep, solve
3029d763cef2SBarry Smith 
30309be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3031d763cef2SBarry Smith @*/
3032193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3033d763cef2SBarry Smith {
30342fb8446cSMatthew G. Knepley   DM               dm;
3035dfbe8321SBarry Smith   PetscErrorCode   ierr;
3036fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3037d763cef2SBarry Smith 
3038d763cef2SBarry Smith   PetscFunctionBegin;
30390700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3040fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3041fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3042fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3043fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3044fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3045fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3046fffbeea8SBarry Smith                                 "  year        = {2014}\n}\n",&cite);
3047fffbeea8SBarry Smith 
30482fb8446cSMatthew G. Knepley   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
3049d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
3050d405a339SMatthew Knepley 
3051362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
305224655328SShri   ts->ptime_prev = ts->ptime;
3053cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3054bbd56ea5SKarl Rupp 
3055e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3056d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3057193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3058d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3059bbd56ea5SKarl Rupp 
306024655328SShri   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3061cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3062362cd11cSLisandro Dalcin 
3063362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3064cef5090cSJed Brown     if (ts->errorifstepfailed) {
306508c7845fSBarry 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]);
306608c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3067d2daff3dSHong Zhang     }
306808c7845fSBarry Smith   } else if (!ts->reason) {
306908c7845fSBarry Smith     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
307008c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
307108c7845fSBarry Smith   }
30722c18e0fdSBarry Smith   ts->total_steps++;
307308c7845fSBarry Smith   PetscFunctionReturn(0);
307408c7845fSBarry Smith }
307508c7845fSBarry Smith 
307608c7845fSBarry Smith #undef __FUNCT__
307708c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep"
307808c7845fSBarry Smith /*@
307908c7845fSBarry Smith    TSAdjointStep - Steps one time step
308008c7845fSBarry Smith 
308108c7845fSBarry Smith    Collective on TS
308208c7845fSBarry Smith 
308308c7845fSBarry Smith    Input Parameter:
308408c7845fSBarry Smith .  ts - the TS context obtained from TSCreate()
308508c7845fSBarry Smith 
308608c7845fSBarry Smith    Level: intermediate
308708c7845fSBarry Smith 
308808c7845fSBarry Smith    Notes:
308908c7845fSBarry Smith    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
309008c7845fSBarry Smith    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
309108c7845fSBarry Smith 
309208c7845fSBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
309308c7845fSBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
309408c7845fSBarry Smith 
309508c7845fSBarry Smith .keywords: TS, timestep, solve
309608c7845fSBarry Smith 
309708c7845fSBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
309808c7845fSBarry Smith @*/
309908c7845fSBarry Smith PetscErrorCode  TSAdjointStep(TS ts)
310008c7845fSBarry Smith {
310108c7845fSBarry Smith   DM               dm;
310208c7845fSBarry Smith   PetscErrorCode   ierr;
310308c7845fSBarry Smith 
310408c7845fSBarry Smith   PetscFunctionBegin;
310508c7845fSBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
310608c7845fSBarry Smith   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
310708c7845fSBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
310808c7845fSBarry Smith 
310908c7845fSBarry Smith   ts->reason = TS_CONVERGED_ITERATING;
311008c7845fSBarry Smith   ts->ptime_prev = ts->ptime;
311108c7845fSBarry Smith   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
311208c7845fSBarry Smith   ierr = VecViewFromOptions(ts->vec_sol, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr);
311308c7845fSBarry Smith 
311408c7845fSBarry Smith   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
311542f2b339SBarry 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);
311642f2b339SBarry Smith   ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr);
3117362cd11cSLisandro Dalcin   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3118362cd11cSLisandro Dalcin 
3119cef5090cSJed Brown   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3120cef5090cSJed Brown   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3121ce94432eSBarry Smith 
3122ce94432eSBarry Smith   if (ts->reason < 0) {
3123cef5090cSJed Brown     if (ts->errorifstepfailed) {
3124362cd11cSLisandro Dalcin       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
3125db4deed7SKarl Rupp         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]);
31262a3a10ceSLisandro Dalcin       } else if (ts->reason == TS_DIVERGED_STEP_REJECTED) {
31272a3a10ceSLisandro 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]);
3128362cd11cSLisandro Dalcin       } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3129362cd11cSLisandro Dalcin     }
3130362cd11cSLisandro Dalcin   } else if (!ts->reason) {
313173b18844SBarry Smith     if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
3132db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time)         ts->reason = TS_CONVERGED_TIME;
3133362cd11cSLisandro Dalcin   }
31342c18e0fdSBarry Smith   ts->total_steps--;
3135d763cef2SBarry Smith   PetscFunctionReturn(0);
3136d763cef2SBarry Smith }
3137d763cef2SBarry Smith 
31384a2ae208SSatish Balay #undef __FUNCT__
313905175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
314005175c85SJed Brown /*@
314105175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
314205175c85SJed Brown 
31431c3436cfSJed Brown    Collective on TS
314405175c85SJed Brown 
314505175c85SJed Brown    Input Arguments:
31461c3436cfSJed Brown +  ts - time stepping context
31471c3436cfSJed Brown .  order - desired order of accuracy
31480298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
314905175c85SJed Brown 
315005175c85SJed Brown    Output Arguments:
31510910c330SBarry Smith .  U - state at the end of the current step
315205175c85SJed Brown 
315305175c85SJed Brown    Level: advanced
315405175c85SJed Brown 
3155108c343cSJed Brown    Notes:
3156108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3157108c343cSJed 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.
3158108c343cSJed Brown 
31591c3436cfSJed Brown .seealso: TSStep(), TSAdapt
316005175c85SJed Brown @*/
31610910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
316205175c85SJed Brown {
316305175c85SJed Brown   PetscErrorCode ierr;
316405175c85SJed Brown 
316505175c85SJed Brown   PetscFunctionBegin;
316605175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
316705175c85SJed Brown   PetscValidType(ts,1);
31680910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3169ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
31700910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
317105175c85SJed Brown   PetscFunctionReturn(0);
317205175c85SJed Brown }
317305175c85SJed Brown 
3174d67e68b7SBarry Smith 
317505175c85SJed Brown #undef __FUNCT__
31766a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
31776a4d4014SLisandro Dalcin /*@
31786a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
31796a4d4014SLisandro Dalcin 
31806a4d4014SLisandro Dalcin    Collective on TS
31816a4d4014SLisandro Dalcin 
31826a4d4014SLisandro Dalcin    Input Parameter:
31836a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3184cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
31855a3a76d0SJed Brown 
31866a4d4014SLisandro Dalcin    Level: beginner
31876a4d4014SLisandro Dalcin 
31885a3a76d0SJed Brown    Notes:
31895a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
31905a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
31915a3a76d0SJed Brown    stepped over the final time.
31925a3a76d0SJed Brown 
31936a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
31946a4d4014SLisandro Dalcin 
31956a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
31966a4d4014SLisandro Dalcin @*/
3197cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
31986a4d4014SLisandro Dalcin {
3199b06615a5SLisandro Dalcin   Vec               solution;
32006a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
3201f22f69f0SBarry Smith 
32026a4d4014SLisandro Dalcin   PetscFunctionBegin;
32030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3204f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
320549354f04SShri 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 */
3206b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
32070910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3208b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3209b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3210b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
32115a3a76d0SJed Brown     }
32120910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3213bbd56ea5SKarl Rupp   } else if (u) {
32140910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
32155a3a76d0SJed Brown   }
3216d2daff3dSHong Zhang   ierr = TSSetUp(ts);CHKERRQ(ierr); /*compute adj coefficients if the reverse mode is on*/
32176a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
3218193ac0bcSJed Brown   ts->steps             = 0;
32195ef26d82SJed Brown   ts->ksp_its           = 0;
32205ef26d82SJed Brown   ts->snes_its          = 0;
3221c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
3222c610991cSLisandro Dalcin   ts->reject            = 0;
3223193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
3224193ac0bcSJed Brown 
3225ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3226f05ece33SBarry Smith 
3227193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
3228193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
32290910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
3230cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3231193ac0bcSJed Brown   } else {
32326a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
3233db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
3234db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3235e1a7a14fSJed Brown     while (!ts->reason) {
3236b06615a5SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
3237bc952696SBarry Smith       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
3238193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
3239aeb4809dSShri Abhyankar       if (ts->event) {
3240aeb4809dSShri Abhyankar 	ierr = TSEventMonitor(ts);CHKERRQ(ierr);
32411eda64f1SShri Abhyankar 	if (ts->event->status != TSEVENT_PROCESSING) {
32421eda64f1SShri Abhyankar 	  ierr = TSPostStep(ts);CHKERRQ(ierr);
32431eda64f1SShri Abhyankar 	}
32441eda64f1SShri Abhyankar       } else {
32451eda64f1SShri Abhyankar 	ierr = TSPostStep(ts);CHKERRQ(ierr);
3246aeb4809dSShri Abhyankar       }
3247193ac0bcSJed Brown     }
324849354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
32490910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3250cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3251b06615a5SLisandro Dalcin       solution = u;
32520574a7fbSJed Brown     } else {
3253ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3254cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3255b06615a5SLisandro Dalcin       solution = ts->vec_sol;
32560574a7fbSJed Brown     }
3257bc952696SBarry Smith     ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr);
3258b06615a5SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr);
325927829d71SBarry Smith     ierr = VecViewFromOptions(solution, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr);
3260193ac0bcSJed Brown   }
3261d2daff3dSHong Zhang 
3262ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
326356f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
32646a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
32656a4d4014SLisandro Dalcin }
32666a4d4014SLisandro Dalcin 
32676a4d4014SLisandro Dalcin #undef __FUNCT__
3268c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve"
3269c88f2d44SBarry Smith /*@
3270c88f2d44SBarry Smith    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE
3271c88f2d44SBarry Smith 
3272c88f2d44SBarry Smith    Collective on TS
3273c88f2d44SBarry Smith 
3274c88f2d44SBarry Smith    Input Parameter:
32752c39e106SBarry Smith .  ts - the TS context obtained from TSCreate()
3276c88f2d44SBarry Smith 
3277c88f2d44SBarry Smith    Level: intermediate
3278c88f2d44SBarry Smith 
3279c88f2d44SBarry Smith    Notes:
3280c88f2d44SBarry Smith    This must be called after a call to TSSolve() that solves the forward problem
3281c88f2d44SBarry Smith 
328273b18844SBarry Smith    By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time
328373b18844SBarry Smith 
3284c88f2d44SBarry Smith .keywords: TS, timestep, solve
3285c88f2d44SBarry Smith 
3286c88f2d44SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep()
3287c88f2d44SBarry Smith @*/
32882c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts)
3289c88f2d44SBarry Smith {
3290c88f2d44SBarry Smith   PetscErrorCode    ierr;
3291c88f2d44SBarry Smith 
3292c88f2d44SBarry Smith   PetscFunctionBegin;
3293c88f2d44SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3294c88f2d44SBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
3295c88f2d44SBarry Smith   /* reset time step and iteration counters */
3296c88f2d44SBarry Smith   ts->steps             = 0;
3297c88f2d44SBarry Smith   ts->ksp_its           = 0;
3298c88f2d44SBarry Smith   ts->snes_its          = 0;
3299c88f2d44SBarry Smith   ts->num_snes_failures = 0;
3300c88f2d44SBarry Smith   ts->reject            = 0;
3301c88f2d44SBarry Smith   ts->reason            = TS_CONVERGED_ITERATING;
3302c88f2d44SBarry Smith 
330373b18844SBarry Smith   if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps;
3304c88f2d44SBarry Smith 
330573b18844SBarry Smith   if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
3306c88f2d44SBarry Smith   while (!ts->reason) {
330773b18844SBarry Smith     ierr = TSTrajectoryGet(ts->trajectory,ts,ts->adjoint_max_steps-ts->steps,ts->ptime);CHKERRQ(ierr);
330873b18844SBarry Smith     ierr = TSMonitor(ts,ts->adjoint_max_steps-ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
330908c7845fSBarry Smith     ierr = TSAdjointStep(ts);CHKERRQ(ierr);
3310c88f2d44SBarry Smith     if (ts->event) {
3311c88f2d44SBarry Smith       ierr = TSEventMonitor(ts);CHKERRQ(ierr);
3312c88f2d44SBarry Smith       if (ts->event->status != TSEVENT_PROCESSING) {
3313c88f2d44SBarry Smith         ierr = TSPostStep(ts);CHKERRQ(ierr);
3314c88f2d44SBarry Smith       }
3315c88f2d44SBarry Smith     } else {
3316c88f2d44SBarry Smith       ierr = TSPostStep(ts);CHKERRQ(ierr);
3317c88f2d44SBarry Smith     }
3318c88f2d44SBarry Smith   }
3319c88f2d44SBarry Smith   ts->solvetime = ts->ptime;
3320c88f2d44SBarry Smith   PetscFunctionReturn(0);
3321c88f2d44SBarry Smith }
3322c88f2d44SBarry Smith 
3323c88f2d44SBarry Smith #undef __FUNCT__
33244a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
3325228d79bcSJed Brown /*@
3326228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3327228d79bcSJed Brown 
3328228d79bcSJed Brown    Collective on TS
3329228d79bcSJed Brown 
3330228d79bcSJed Brown    Input Parameters:
3331228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3332228d79bcSJed Brown .  step - step number that has just completed
3333228d79bcSJed Brown .  ptime - model time of the state
33340910c330SBarry Smith -  u - state at the current model time
3335228d79bcSJed Brown 
3336228d79bcSJed Brown    Notes:
3337228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
3338228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
3339228d79bcSJed Brown 
3340228d79bcSJed Brown    Level: advanced
3341228d79bcSJed Brown 
3342228d79bcSJed Brown .keywords: TS, timestep
3343228d79bcSJed Brown @*/
33440910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3345d763cef2SBarry Smith {
33466849ba73SBarry Smith   PetscErrorCode ierr;
3347a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
3348d763cef2SBarry Smith 
3349d763cef2SBarry Smith   PetscFunctionBegin;
3350b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3351b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
33525edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
3353d763cef2SBarry Smith   for (i=0; i<n; i++) {
33540910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
3355d763cef2SBarry Smith   }
33565edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
3357d763cef2SBarry Smith   PetscFunctionReturn(0);
3358d763cef2SBarry Smith }
3359d763cef2SBarry Smith 
3360d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
33614a2ae208SSatish Balay #undef __FUNCT__
3362a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
3363d763cef2SBarry Smith /*@C
3364a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
3365a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
3366d763cef2SBarry Smith 
3367d763cef2SBarry Smith    Collective on TS
3368d763cef2SBarry Smith 
3369d763cef2SBarry Smith    Input Parameters:
3370d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
3371d763cef2SBarry Smith .  label - the title to put in the title bar
33727c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
3373a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
3374a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
3375d763cef2SBarry Smith 
3376d763cef2SBarry Smith    Output Parameter:
33770b039ecaSBarry Smith .  ctx - the context
3378d763cef2SBarry Smith 
3379d763cef2SBarry Smith    Options Database Key:
33804f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
33814f09c107SBarry Smith .  -ts_monitor_lg_solution -
338299fdda47SBarry Smith .  -ts_monitor_lg_error -
338399fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
338499fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
338599fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
3386d763cef2SBarry Smith 
3387d763cef2SBarry Smith    Notes:
3388a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
3389d763cef2SBarry Smith 
3390d763cef2SBarry Smith    Level: intermediate
3391d763cef2SBarry Smith 
33927c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
3393d763cef2SBarry Smith 
33944f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
33957c922b88SBarry Smith 
3396d763cef2SBarry Smith @*/
3397a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
3398d763cef2SBarry Smith {
3399b0a32e0cSBarry Smith   PetscDraw      win;
3400dfbe8321SBarry Smith   PetscErrorCode ierr;
3401d763cef2SBarry Smith 
3402d763cef2SBarry Smith   PetscFunctionBegin;
3403b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
3404a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
34053fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
34060b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
34073bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)(*ctx)->lg,(PetscObject)win);CHKERRQ(ierr);
3408287de1a7SBarry Smith   ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr);
3409287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
3410a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
3411d763cef2SBarry Smith   PetscFunctionReturn(0);
3412d763cef2SBarry Smith }
3413d763cef2SBarry Smith 
34144a2ae208SSatish Balay #undef __FUNCT__
34154f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
3416b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
3417d763cef2SBarry Smith {
34180b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
3419c32365f1SBarry Smith   PetscReal      x   = ptime,y;
3420dfbe8321SBarry Smith   PetscErrorCode ierr;
3421d763cef2SBarry Smith 
3422d763cef2SBarry Smith   PetscFunctionBegin;
3423b06615a5SLisandro Dalcin   if (!step) {
3424a9f9c1f6SBarry Smith     PetscDrawAxis axis;
3425a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
3426a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
3427a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
3428287de1a7SBarry Smith     ierr = PetscDrawLGIndicateDataPoints(ctx->lg,PETSC_TRUE);CHKERRQ(ierr);
3429a9f9c1f6SBarry Smith   }
3430c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
34310b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
3432b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
34330b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
34343923b477SBarry Smith   }
3435d763cef2SBarry Smith   PetscFunctionReturn(0);
3436d763cef2SBarry Smith }
3437d763cef2SBarry Smith 
34384a2ae208SSatish Balay #undef __FUNCT__
3439a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
3440d763cef2SBarry Smith /*@C
3441a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
3442a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
3443d763cef2SBarry Smith 
34440b039ecaSBarry Smith    Collective on TSMonitorLGCtx
3445d763cef2SBarry Smith 
3446d763cef2SBarry Smith    Input Parameter:
34470b039ecaSBarry Smith .  ctx - the monitor context
3448d763cef2SBarry Smith 
3449d763cef2SBarry Smith    Level: intermediate
3450d763cef2SBarry Smith 
3451d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
3452d763cef2SBarry Smith 
34534f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
3454d763cef2SBarry Smith @*/
3455a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
3456d763cef2SBarry Smith {
3457b0a32e0cSBarry Smith   PetscDraw      draw;
3458dfbe8321SBarry Smith   PetscErrorCode ierr;
3459d763cef2SBarry Smith 
3460d763cef2SBarry Smith   PetscFunctionBegin;
34610b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
34626bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
34630b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
34640b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
3465d763cef2SBarry Smith   PetscFunctionReturn(0);
3466d763cef2SBarry Smith }
3467d763cef2SBarry Smith 
34684a2ae208SSatish Balay #undef __FUNCT__
34694a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
3470d763cef2SBarry Smith /*@
3471b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
3472d763cef2SBarry Smith 
3473d763cef2SBarry Smith    Not Collective
3474d763cef2SBarry Smith 
3475d763cef2SBarry Smith    Input Parameter:
3476d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3477d763cef2SBarry Smith 
3478d763cef2SBarry Smith    Output Parameter:
3479d763cef2SBarry Smith .  t  - the current time
3480d763cef2SBarry Smith 
3481d763cef2SBarry Smith    Level: beginner
3482d763cef2SBarry Smith 
3483b8123daeSJed Brown    Note:
3484b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
34859be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
3486b8123daeSJed Brown 
3487d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3488d763cef2SBarry Smith 
3489d763cef2SBarry Smith .keywords: TS, get, time
3490d763cef2SBarry Smith @*/
34917087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
3492d763cef2SBarry Smith {
3493d763cef2SBarry Smith   PetscFunctionBegin;
34940700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3495f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
3496d763cef2SBarry Smith   *t = ts->ptime;
3497d763cef2SBarry Smith   PetscFunctionReturn(0);
3498d763cef2SBarry Smith }
3499d763cef2SBarry Smith 
35004a2ae208SSatish Balay #undef __FUNCT__
3501e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime"
3502e5e524a1SHong Zhang /*@
3503e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
3504e5e524a1SHong Zhang 
3505e5e524a1SHong Zhang    Not Collective
3506e5e524a1SHong Zhang 
3507e5e524a1SHong Zhang    Input Parameter:
3508e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
3509e5e524a1SHong Zhang 
3510e5e524a1SHong Zhang    Output Parameter:
3511e5e524a1SHong Zhang .  t  - the previous time
3512e5e524a1SHong Zhang 
3513e5e524a1SHong Zhang    Level: beginner
3514e5e524a1SHong Zhang 
3515e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3516e5e524a1SHong Zhang 
3517e5e524a1SHong Zhang .keywords: TS, get, time
3518e5e524a1SHong Zhang @*/
3519e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
3520e5e524a1SHong Zhang {
3521e5e524a1SHong Zhang   PetscFunctionBegin;
3522e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3523e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
3524e5e524a1SHong Zhang   *t = ts->ptime_prev;
3525e5e524a1SHong Zhang   PetscFunctionReturn(0);
3526e5e524a1SHong Zhang }
3527e5e524a1SHong Zhang 
3528e5e524a1SHong Zhang #undef __FUNCT__
35296a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
35306a4d4014SLisandro Dalcin /*@
35316a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
35326a4d4014SLisandro Dalcin 
35333f9fe445SBarry Smith    Logically Collective on TS
35346a4d4014SLisandro Dalcin 
35356a4d4014SLisandro Dalcin    Input Parameters:
35366a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
35376a4d4014SLisandro Dalcin -  time - the time
35386a4d4014SLisandro Dalcin 
35396a4d4014SLisandro Dalcin    Level: intermediate
35406a4d4014SLisandro Dalcin 
35416a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
35426a4d4014SLisandro Dalcin 
35436a4d4014SLisandro Dalcin .keywords: TS, set, time
35446a4d4014SLisandro Dalcin @*/
35457087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
35466a4d4014SLisandro Dalcin {
35476a4d4014SLisandro Dalcin   PetscFunctionBegin;
35480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3549c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
35506a4d4014SLisandro Dalcin   ts->ptime = t;
35516a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
35526a4d4014SLisandro Dalcin }
35536a4d4014SLisandro Dalcin 
35546a4d4014SLisandro Dalcin #undef __FUNCT__
35554a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
3556d763cef2SBarry Smith /*@C
3557d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
3558d763cef2SBarry Smith    TS options in the database.
3559d763cef2SBarry Smith 
35603f9fe445SBarry Smith    Logically Collective on TS
3561d763cef2SBarry Smith 
3562d763cef2SBarry Smith    Input Parameter:
3563d763cef2SBarry Smith +  ts     - The TS context
3564d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3565d763cef2SBarry Smith 
3566d763cef2SBarry Smith    Notes:
3567d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3568d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3569d763cef2SBarry Smith    hyphen.
3570d763cef2SBarry Smith 
3571d763cef2SBarry Smith    Level: advanced
3572d763cef2SBarry Smith 
3573d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
3574d763cef2SBarry Smith 
3575d763cef2SBarry Smith .seealso: TSSetFromOptions()
3576d763cef2SBarry Smith 
3577d763cef2SBarry Smith @*/
35787087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
3579d763cef2SBarry Smith {
3580dfbe8321SBarry Smith   PetscErrorCode ierr;
3581089b2837SJed Brown   SNES           snes;
3582d763cef2SBarry Smith 
3583d763cef2SBarry Smith   PetscFunctionBegin;
35840700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3585d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3586089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3587089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3588d763cef2SBarry Smith   PetscFunctionReturn(0);
3589d763cef2SBarry Smith }
3590d763cef2SBarry Smith 
3591d763cef2SBarry Smith 
35924a2ae208SSatish Balay #undef __FUNCT__
35934a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
3594d763cef2SBarry Smith /*@C
3595d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
3596d763cef2SBarry Smith    TS options in the database.
3597d763cef2SBarry Smith 
35983f9fe445SBarry Smith    Logically Collective on TS
3599d763cef2SBarry Smith 
3600d763cef2SBarry Smith    Input Parameter:
3601d763cef2SBarry Smith +  ts     - The TS context
3602d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3603d763cef2SBarry Smith 
3604d763cef2SBarry Smith    Notes:
3605d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3606d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3607d763cef2SBarry Smith    hyphen.
3608d763cef2SBarry Smith 
3609d763cef2SBarry Smith    Level: advanced
3610d763cef2SBarry Smith 
3611d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
3612d763cef2SBarry Smith 
3613d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
3614d763cef2SBarry Smith 
3615d763cef2SBarry Smith @*/
36167087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
3617d763cef2SBarry Smith {
3618dfbe8321SBarry Smith   PetscErrorCode ierr;
3619089b2837SJed Brown   SNES           snes;
3620d763cef2SBarry Smith 
3621d763cef2SBarry Smith   PetscFunctionBegin;
36220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3623d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3624089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3625089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3626d763cef2SBarry Smith   PetscFunctionReturn(0);
3627d763cef2SBarry Smith }
3628d763cef2SBarry Smith 
36294a2ae208SSatish Balay #undef __FUNCT__
36304a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
3631d763cef2SBarry Smith /*@C
3632d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
3633d763cef2SBarry Smith    TS options in the database.
3634d763cef2SBarry Smith 
3635d763cef2SBarry Smith    Not Collective
3636d763cef2SBarry Smith 
3637d763cef2SBarry Smith    Input Parameter:
3638d763cef2SBarry Smith .  ts - The TS context
3639d763cef2SBarry Smith 
3640d763cef2SBarry Smith    Output Parameter:
3641d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
3642d763cef2SBarry Smith 
3643d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
3644d763cef2SBarry Smith    sufficient length to hold the prefix.
3645d763cef2SBarry Smith 
3646d763cef2SBarry Smith    Level: intermediate
3647d763cef2SBarry Smith 
3648d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
3649d763cef2SBarry Smith 
3650d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
3651d763cef2SBarry Smith @*/
36527087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
3653d763cef2SBarry Smith {
3654dfbe8321SBarry Smith   PetscErrorCode ierr;
3655d763cef2SBarry Smith 
3656d763cef2SBarry Smith   PetscFunctionBegin;
36570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36584482741eSBarry Smith   PetscValidPointer(prefix,2);
3659d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3660d763cef2SBarry Smith   PetscFunctionReturn(0);
3661d763cef2SBarry Smith }
3662d763cef2SBarry Smith 
36634a2ae208SSatish Balay #undef __FUNCT__
36644a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
3665d763cef2SBarry Smith /*@C
3666d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
3667d763cef2SBarry Smith 
3668d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
3669d763cef2SBarry Smith 
3670d763cef2SBarry Smith    Input Parameter:
3671d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
3672d763cef2SBarry Smith 
3673d763cef2SBarry Smith    Output Parameters:
3674e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
3675e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
3676e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
3677e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
3678d763cef2SBarry Smith 
36790298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
3680d763cef2SBarry Smith 
3681d763cef2SBarry Smith    Level: intermediate
3682d763cef2SBarry Smith 
368326d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
3684d763cef2SBarry Smith 
3685d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
3686d763cef2SBarry Smith @*/
3687e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
3688d763cef2SBarry Smith {
3689089b2837SJed Brown   PetscErrorCode ierr;
3690089b2837SJed Brown   SNES           snes;
369124989b8cSPeter Brune   DM             dm;
3692089b2837SJed Brown 
3693d763cef2SBarry Smith   PetscFunctionBegin;
3694089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3695e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
369624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
369724989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
3698d763cef2SBarry Smith   PetscFunctionReturn(0);
3699d763cef2SBarry Smith }
3700d763cef2SBarry Smith 
37011713a123SBarry Smith #undef __FUNCT__
37022eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
37032eca1d9cSJed Brown /*@C
37042eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
37052eca1d9cSJed Brown 
37062eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
37072eca1d9cSJed Brown 
37082eca1d9cSJed Brown    Input Parameter:
37092eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
37102eca1d9cSJed Brown 
37112eca1d9cSJed Brown    Output Parameters:
3712e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
3713e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
37142eca1d9cSJed Brown .  f   - The function to compute the matrices
37152eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
37162eca1d9cSJed Brown 
37170298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
37182eca1d9cSJed Brown 
37192eca1d9cSJed Brown    Level: advanced
37202eca1d9cSJed Brown 
37212eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
37222eca1d9cSJed Brown 
37232eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
37242eca1d9cSJed Brown @*/
3725e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
37262eca1d9cSJed Brown {
3727089b2837SJed Brown   PetscErrorCode ierr;
3728089b2837SJed Brown   SNES           snes;
372924989b8cSPeter Brune   DM             dm;
37300910c330SBarry Smith 
37312eca1d9cSJed Brown   PetscFunctionBegin;
3732089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3733f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
3734e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
373524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
373624989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
37372eca1d9cSJed Brown   PetscFunctionReturn(0);
37382eca1d9cSJed Brown }
37392eca1d9cSJed Brown 
37406083293cSBarry Smith 
37412eca1d9cSJed Brown #undef __FUNCT__
374283a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
37431713a123SBarry Smith /*@C
374483a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
37451713a123SBarry Smith    VecView() for the solution at each timestep
37461713a123SBarry Smith 
37471713a123SBarry Smith    Collective on TS
37481713a123SBarry Smith 
37491713a123SBarry Smith    Input Parameters:
37501713a123SBarry Smith +  ts - the TS context
37511713a123SBarry Smith .  step - current time-step
3752142b95e3SSatish Balay .  ptime - current time
37530298fd71SBarry Smith -  dummy - either a viewer or NULL
37541713a123SBarry Smith 
375599fdda47SBarry Smith    Options Database:
375699fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
375799fdda47SBarry Smith 
375899fdda47SBarry Smith    Notes: the initial solution and current solution are not displayed with a common axis scaling so generally the option -ts_monitor_draw_solution_initial
375999fdda47SBarry Smith        will look bad
376099fdda47SBarry Smith 
37611713a123SBarry Smith    Level: intermediate
37621713a123SBarry Smith 
37631713a123SBarry Smith .keywords: TS,  vector, monitor, view
37641713a123SBarry Smith 
3765a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
37661713a123SBarry Smith @*/
37670910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
37681713a123SBarry Smith {
3769dfbe8321SBarry Smith   PetscErrorCode   ierr;
377083a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
3771473a3ab2SBarry Smith   PetscDraw        draw;
37721713a123SBarry Smith 
37731713a123SBarry Smith   PetscFunctionBegin;
37746083293cSBarry Smith   if (!step && ictx->showinitial) {
37756083293cSBarry Smith     if (!ictx->initialsolution) {
37760910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
37771713a123SBarry Smith     }
37780910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
37796083293cSBarry Smith   }
3780b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
37810dcf80beSBarry Smith 
37826083293cSBarry Smith   if (ictx->showinitial) {
37836083293cSBarry Smith     PetscReal pause;
37846083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
37856083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
37866083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
37876083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
37886083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
37896083293cSBarry Smith   }
37900910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
3791473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
3792473a3ab2SBarry Smith     PetscReal xl,yl,xr,yr,tw,w,h;
3793473a3ab2SBarry Smith     char      time[32];
3794473a3ab2SBarry Smith     size_t    len;
3795473a3ab2SBarry Smith 
3796473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
379785b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
3798473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
3799473a3ab2SBarry Smith     ierr =  PetscStrlen(time,&len);CHKERRQ(ierr);
38000298fd71SBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
3801473a3ab2SBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
3802473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
3803473a3ab2SBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
3804473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
3805473a3ab2SBarry Smith   }
3806473a3ab2SBarry Smith 
38076083293cSBarry Smith   if (ictx->showinitial) {
38086083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
38096083293cSBarry Smith   }
38101713a123SBarry Smith   PetscFunctionReturn(0);
38111713a123SBarry Smith }
38121713a123SBarry Smith 
38132d5ee99bSBarry Smith #undef __FUNCT__
38142d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
38152d5ee99bSBarry Smith /*@C
38162d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
38172d5ee99bSBarry Smith 
38182d5ee99bSBarry Smith    Collective on TS
38192d5ee99bSBarry Smith 
38202d5ee99bSBarry Smith    Input Parameters:
38212d5ee99bSBarry Smith +  ts - the TS context
38222d5ee99bSBarry Smith .  step - current time-step
38232d5ee99bSBarry Smith .  ptime - current time
38242d5ee99bSBarry Smith -  dummy - either a viewer or NULL
38252d5ee99bSBarry Smith 
38262d5ee99bSBarry Smith    Level: intermediate
38272d5ee99bSBarry Smith 
38282d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
38292d5ee99bSBarry Smith 
38302d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
38312d5ee99bSBarry Smith @*/
38322d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
38332d5ee99bSBarry Smith {
38342d5ee99bSBarry Smith   PetscErrorCode    ierr;
38352d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
38362d5ee99bSBarry Smith   PetscDraw         draw;
38372d5ee99bSBarry Smith   MPI_Comm          comm;
38382d5ee99bSBarry Smith   PetscInt          n;
38392d5ee99bSBarry Smith   PetscMPIInt       size;
38402d5ee99bSBarry Smith   PetscReal         xl,yl,xr,yr,tw,w,h;
38412d5ee99bSBarry Smith   char              time[32];
38422d5ee99bSBarry Smith   size_t            len;
38432d5ee99bSBarry Smith   const PetscScalar *U;
38442d5ee99bSBarry Smith 
38452d5ee99bSBarry Smith   PetscFunctionBegin;
38462d5ee99bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
38472d5ee99bSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
38482d5ee99bSBarry Smith   if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs");
38492d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
38502d5ee99bSBarry Smith   if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns");
38512d5ee99bSBarry Smith 
38522d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
38532d5ee99bSBarry Smith 
38542d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
38554b363babSBarry Smith   ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
3856ba5783adSMatthew G Knepley   if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) {
3857a4494fc1SBarry Smith       ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
3858a4494fc1SBarry Smith       PetscFunctionReturn(0);
3859a4494fc1SBarry Smith   }
38602d5ee99bSBarry Smith   if (!step) ictx->color++;
38612d5ee99bSBarry Smith   ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr);
38622d5ee99bSBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
38632d5ee99bSBarry Smith 
38642d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
38654b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
386685b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
38672d5ee99bSBarry Smith     ierr = PetscStrlen(time,&len);CHKERRQ(ierr);
38682d5ee99bSBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
38692d5ee99bSBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
38702d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
38712d5ee99bSBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
38722d5ee99bSBarry Smith   }
38732d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
38742d5ee99bSBarry Smith   PetscFunctionReturn(0);
38752d5ee99bSBarry Smith }
38762d5ee99bSBarry Smith 
38771713a123SBarry Smith 
38786c699258SBarry Smith #undef __FUNCT__
387983a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
38806083293cSBarry Smith /*@C
388183a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
38826083293cSBarry Smith 
38836083293cSBarry Smith    Collective on TS
38846083293cSBarry Smith 
38856083293cSBarry Smith    Input Parameters:
38866083293cSBarry Smith .    ctx - the monitor context
38876083293cSBarry Smith 
38886083293cSBarry Smith    Level: intermediate
38896083293cSBarry Smith 
38906083293cSBarry Smith .keywords: TS,  vector, monitor, view
38916083293cSBarry Smith 
389283a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
38936083293cSBarry Smith @*/
389483a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
38956083293cSBarry Smith {
38966083293cSBarry Smith   PetscErrorCode ierr;
38976083293cSBarry Smith 
38986083293cSBarry Smith   PetscFunctionBegin;
38994b363babSBarry Smith   ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr);
390083a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
390183a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
390283a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
39036083293cSBarry Smith   PetscFunctionReturn(0);
39046083293cSBarry Smith }
39056083293cSBarry Smith 
39066083293cSBarry Smith #undef __FUNCT__
390783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
39086083293cSBarry Smith /*@C
390983a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
39106083293cSBarry Smith 
39116083293cSBarry Smith    Collective on TS
39126083293cSBarry Smith 
39136083293cSBarry Smith    Input Parameter:
39146083293cSBarry Smith .    ts - time-step context
39156083293cSBarry Smith 
39166083293cSBarry Smith    Output Patameter:
39176083293cSBarry Smith .    ctx - the monitor context
39186083293cSBarry Smith 
391999fdda47SBarry Smith    Options Database:
392099fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
392199fdda47SBarry Smith 
39226083293cSBarry Smith    Level: intermediate
39236083293cSBarry Smith 
39246083293cSBarry Smith .keywords: TS,  vector, monitor, view
39256083293cSBarry Smith 
392683a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
39276083293cSBarry Smith @*/
392883a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
39296083293cSBarry Smith {
39306083293cSBarry Smith   PetscErrorCode   ierr;
39316083293cSBarry Smith 
39326083293cSBarry Smith   PetscFunctionBegin;
3933b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
393483a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
3935e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
3936bbd56ea5SKarl Rupp 
393783a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
3938473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
39390298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
3940473a3ab2SBarry Smith 
3941473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
39420298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
39432d5ee99bSBarry Smith   (*ctx)->color = PETSC_DRAW_WHITE;
39446083293cSBarry Smith   PetscFunctionReturn(0);
39456083293cSBarry Smith }
39466083293cSBarry Smith 
39476083293cSBarry Smith #undef __FUNCT__
394883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
39493a471f94SBarry Smith /*@C
395083a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
39513a471f94SBarry Smith    VecView() for the error at each timestep
39523a471f94SBarry Smith 
39533a471f94SBarry Smith    Collective on TS
39543a471f94SBarry Smith 
39553a471f94SBarry Smith    Input Parameters:
39563a471f94SBarry Smith +  ts - the TS context
39573a471f94SBarry Smith .  step - current time-step
39583a471f94SBarry Smith .  ptime - current time
39590298fd71SBarry Smith -  dummy - either a viewer or NULL
39603a471f94SBarry Smith 
39613a471f94SBarry Smith    Level: intermediate
39623a471f94SBarry Smith 
39633a471f94SBarry Smith .keywords: TS,  vector, monitor, view
39643a471f94SBarry Smith 
39653a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
39663a471f94SBarry Smith @*/
39670910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
39683a471f94SBarry Smith {
39693a471f94SBarry Smith   PetscErrorCode   ierr;
397083a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
397183a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
39723a471f94SBarry Smith   Vec              work;
39733a471f94SBarry Smith 
39743a471f94SBarry Smith   PetscFunctionBegin;
3975b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
39760910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
39773a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
39780910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
39793a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
39803a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
39813a471f94SBarry Smith   PetscFunctionReturn(0);
39823a471f94SBarry Smith }
39833a471f94SBarry Smith 
39842a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
39853a471f94SBarry Smith #undef __FUNCT__
39866c699258SBarry Smith #define __FUNCT__ "TSSetDM"
39876c699258SBarry Smith /*@
39886c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
39896c699258SBarry Smith 
39903f9fe445SBarry Smith    Logically Collective on TS and DM
39916c699258SBarry Smith 
39926c699258SBarry Smith    Input Parameters:
39936c699258SBarry Smith +  ts - the preconditioner context
39946c699258SBarry Smith -  dm - the dm
39956c699258SBarry Smith 
39966c699258SBarry Smith    Level: intermediate
39976c699258SBarry Smith 
39986c699258SBarry Smith 
39996c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
40006c699258SBarry Smith @*/
40017087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
40026c699258SBarry Smith {
40036c699258SBarry Smith   PetscErrorCode ierr;
4004089b2837SJed Brown   SNES           snes;
4005942e3340SBarry Smith   DMTS           tsdm;
40066c699258SBarry Smith 
40076c699258SBarry Smith   PetscFunctionBegin;
40080700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
400970663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4010942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
40112a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4012942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4013942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
401424989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
401524989b8cSPeter Brune         tsdm->originaldm = dm;
401624989b8cSPeter Brune       }
401724989b8cSPeter Brune     }
40186bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
401924989b8cSPeter Brune   }
40206c699258SBarry Smith   ts->dm = dm;
4021bbd56ea5SKarl Rupp 
4022089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4023089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
40246c699258SBarry Smith   PetscFunctionReturn(0);
40256c699258SBarry Smith }
40266c699258SBarry Smith 
40276c699258SBarry Smith #undef __FUNCT__
40286c699258SBarry Smith #define __FUNCT__ "TSGetDM"
40296c699258SBarry Smith /*@
40306c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
40316c699258SBarry Smith 
40323f9fe445SBarry Smith    Not Collective
40336c699258SBarry Smith 
40346c699258SBarry Smith    Input Parameter:
40356c699258SBarry Smith . ts - the preconditioner context
40366c699258SBarry Smith 
40376c699258SBarry Smith    Output Parameter:
40386c699258SBarry Smith .  dm - the dm
40396c699258SBarry Smith 
40406c699258SBarry Smith    Level: intermediate
40416c699258SBarry Smith 
40426c699258SBarry Smith 
40436c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
40446c699258SBarry Smith @*/
40457087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
40466c699258SBarry Smith {
4047496e6a7aSJed Brown   PetscErrorCode ierr;
4048496e6a7aSJed Brown 
40496c699258SBarry Smith   PetscFunctionBegin;
40500700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4051496e6a7aSJed Brown   if (!ts->dm) {
4052ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4053496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4054496e6a7aSJed Brown   }
40556c699258SBarry Smith   *dm = ts->dm;
40566c699258SBarry Smith   PetscFunctionReturn(0);
40576c699258SBarry Smith }
40581713a123SBarry Smith 
40590f5c6efeSJed Brown #undef __FUNCT__
40600f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
40610f5c6efeSJed Brown /*@
40620f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
40630f5c6efeSJed Brown 
40643f9fe445SBarry Smith    Logically Collective on SNES
40650f5c6efeSJed Brown 
40660f5c6efeSJed Brown    Input Parameter:
4067d42a1c89SJed Brown + snes - nonlinear solver
40680910c330SBarry Smith . U - the current state at which to evaluate the residual
4069d42a1c89SJed Brown - ctx - user context, must be a TS
40700f5c6efeSJed Brown 
40710f5c6efeSJed Brown    Output Parameter:
40720f5c6efeSJed Brown . F - the nonlinear residual
40730f5c6efeSJed Brown 
40740f5c6efeSJed Brown    Notes:
40750f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
40760f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
40770f5c6efeSJed Brown 
40780f5c6efeSJed Brown    Level: advanced
40790f5c6efeSJed Brown 
40800f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
40810f5c6efeSJed Brown @*/
40820910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
40830f5c6efeSJed Brown {
40840f5c6efeSJed Brown   TS             ts = (TS)ctx;
40850f5c6efeSJed Brown   PetscErrorCode ierr;
40860f5c6efeSJed Brown 
40870f5c6efeSJed Brown   PetscFunctionBegin;
40880f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
40890910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
40900f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
40910f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
40920910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
40930f5c6efeSJed Brown   PetscFunctionReturn(0);
40940f5c6efeSJed Brown }
40950f5c6efeSJed Brown 
40960f5c6efeSJed Brown #undef __FUNCT__
40970f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
40980f5c6efeSJed Brown /*@
40990f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
41000f5c6efeSJed Brown 
41010f5c6efeSJed Brown    Collective on SNES
41020f5c6efeSJed Brown 
41030f5c6efeSJed Brown    Input Parameter:
41040f5c6efeSJed Brown + snes - nonlinear solver
41050910c330SBarry Smith . U - the current state at which to evaluate the residual
41060f5c6efeSJed Brown - ctx - user context, must be a TS
41070f5c6efeSJed Brown 
41080f5c6efeSJed Brown    Output Parameter:
41090f5c6efeSJed Brown + A - the Jacobian
41100f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
41110f5c6efeSJed Brown - flag - indicates any structure change in the matrix
41120f5c6efeSJed Brown 
41130f5c6efeSJed Brown    Notes:
41140f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
41150f5c6efeSJed Brown 
41160f5c6efeSJed Brown    Level: developer
41170f5c6efeSJed Brown 
41180f5c6efeSJed Brown .seealso: SNESSetJacobian()
41190f5c6efeSJed Brown @*/
4120d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
41210f5c6efeSJed Brown {
41220f5c6efeSJed Brown   TS             ts = (TS)ctx;
41230f5c6efeSJed Brown   PetscErrorCode ierr;
41240f5c6efeSJed Brown 
41250f5c6efeSJed Brown   PetscFunctionBegin;
41260f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
41270910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
41280f5c6efeSJed Brown   PetscValidPointer(A,3);
412994ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
41300f5c6efeSJed Brown   PetscValidPointer(B,4);
413194ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
41320f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4133d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
41340f5c6efeSJed Brown   PetscFunctionReturn(0);
41350f5c6efeSJed Brown }
4136325fc9f4SBarry Smith 
41370e4ef248SJed Brown #undef __FUNCT__
41380e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
41390e4ef248SJed Brown /*@C
41400e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
41410e4ef248SJed Brown 
41420e4ef248SJed Brown    Collective on TS
41430e4ef248SJed Brown 
41440e4ef248SJed Brown    Input Arguments:
41450e4ef248SJed Brown +  ts - time stepping context
41460e4ef248SJed Brown .  t - time at which to evaluate
41470910c330SBarry Smith .  U - state at which to evaluate
41480e4ef248SJed Brown -  ctx - context
41490e4ef248SJed Brown 
41500e4ef248SJed Brown    Output Arguments:
41510e4ef248SJed Brown .  F - right hand side
41520e4ef248SJed Brown 
41530e4ef248SJed Brown    Level: intermediate
41540e4ef248SJed Brown 
41550e4ef248SJed Brown    Notes:
41560e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
41570e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
41580e4ef248SJed Brown 
41590e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
41600e4ef248SJed Brown @*/
41610910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
41620e4ef248SJed Brown {
41630e4ef248SJed Brown   PetscErrorCode ierr;
41640e4ef248SJed Brown   Mat            Arhs,Brhs;
41650e4ef248SJed Brown 
41660e4ef248SJed Brown   PetscFunctionBegin;
41670e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4168d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
41690910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
41700e4ef248SJed Brown   PetscFunctionReturn(0);
41710e4ef248SJed Brown }
41720e4ef248SJed Brown 
41730e4ef248SJed Brown #undef __FUNCT__
41740e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
41750e4ef248SJed Brown /*@C
41760e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
41770e4ef248SJed Brown 
41780e4ef248SJed Brown    Collective on TS
41790e4ef248SJed Brown 
41800e4ef248SJed Brown    Input Arguments:
41810e4ef248SJed Brown +  ts - time stepping context
41820e4ef248SJed Brown .  t - time at which to evaluate
41830910c330SBarry Smith .  U - state at which to evaluate
41840e4ef248SJed Brown -  ctx - context
41850e4ef248SJed Brown 
41860e4ef248SJed Brown    Output Arguments:
41870e4ef248SJed Brown +  A - pointer to operator
41880e4ef248SJed Brown .  B - pointer to preconditioning matrix
41890e4ef248SJed Brown -  flg - matrix structure flag
41900e4ef248SJed Brown 
41910e4ef248SJed Brown    Level: intermediate
41920e4ef248SJed Brown 
41930e4ef248SJed Brown    Notes:
41940e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
41950e4ef248SJed Brown 
41960e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
41970e4ef248SJed Brown @*/
4198d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
41990e4ef248SJed Brown {
42000e4ef248SJed Brown   PetscFunctionBegin;
42010e4ef248SJed Brown   PetscFunctionReturn(0);
42020e4ef248SJed Brown }
42030e4ef248SJed Brown 
42040026cea9SSean Farley #undef __FUNCT__
42050026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
42060026cea9SSean Farley /*@C
42070026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
42080026cea9SSean Farley 
42090026cea9SSean Farley    Collective on TS
42100026cea9SSean Farley 
42110026cea9SSean Farley    Input Arguments:
42120026cea9SSean Farley +  ts - time stepping context
42130026cea9SSean Farley .  t - time at which to evaluate
42140910c330SBarry Smith .  U - state at which to evaluate
42150910c330SBarry Smith .  Udot - time derivative of state vector
42160026cea9SSean Farley -  ctx - context
42170026cea9SSean Farley 
42180026cea9SSean Farley    Output Arguments:
42190026cea9SSean Farley .  F - left hand side
42200026cea9SSean Farley 
42210026cea9SSean Farley    Level: intermediate
42220026cea9SSean Farley 
42230026cea9SSean Farley    Notes:
42240910c330SBarry 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
42250026cea9SSean Farley    user is required to write their own TSComputeIFunction.
42260026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
42270026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
42280026cea9SSean Farley 
42290026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
42300026cea9SSean Farley @*/
42310910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
42320026cea9SSean Farley {
42330026cea9SSean Farley   PetscErrorCode ierr;
42340026cea9SSean Farley   Mat            A,B;
42350026cea9SSean Farley 
42360026cea9SSean Farley   PetscFunctionBegin;
42370298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4238d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
42390910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
42400026cea9SSean Farley   PetscFunctionReturn(0);
42410026cea9SSean Farley }
42420026cea9SSean Farley 
42430026cea9SSean Farley #undef __FUNCT__
42440026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
42450026cea9SSean Farley /*@C
4246b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
42470026cea9SSean Farley 
42480026cea9SSean Farley    Collective on TS
42490026cea9SSean Farley 
42500026cea9SSean Farley    Input Arguments:
42510026cea9SSean Farley +  ts - time stepping context
42520026cea9SSean Farley .  t - time at which to evaluate
42530910c330SBarry Smith .  U - state at which to evaluate
42540910c330SBarry Smith .  Udot - time derivative of state vector
42550026cea9SSean Farley .  shift - shift to apply
42560026cea9SSean Farley -  ctx - context
42570026cea9SSean Farley 
42580026cea9SSean Farley    Output Arguments:
42590026cea9SSean Farley +  A - pointer to operator
42600026cea9SSean Farley .  B - pointer to preconditioning matrix
42610026cea9SSean Farley -  flg - matrix structure flag
42620026cea9SSean Farley 
4263b41af12eSJed Brown    Level: advanced
42640026cea9SSean Farley 
42650026cea9SSean Farley    Notes:
42660026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
42670026cea9SSean Farley 
4268b41af12eSJed Brown    It is only appropriate for problems of the form
4269b41af12eSJed Brown 
4270b41af12eSJed Brown $     M Udot = F(U,t)
4271b41af12eSJed Brown 
4272b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4273b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4274b41af12eSJed Brown   an implicit operator of the form
4275b41af12eSJed Brown 
4276b41af12eSJed Brown $    shift*M + J
4277b41af12eSJed Brown 
4278b41af12eSJed 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
4279b41af12eSJed Brown   a copy of M or reassemble it when requested.
4280b41af12eSJed Brown 
42810026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
42820026cea9SSean Farley @*/
4283d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
42840026cea9SSean Farley {
4285b41af12eSJed Brown   PetscErrorCode ierr;
4286b41af12eSJed Brown 
42870026cea9SSean Farley   PetscFunctionBegin;
428894ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4289b41af12eSJed Brown   ts->ijacobian.shift = shift;
42900026cea9SSean Farley   PetscFunctionReturn(0);
42910026cea9SSean Farley }
4292b41af12eSJed Brown 
4293e817cc15SEmil Constantinescu #undef __FUNCT__
4294e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
4295e817cc15SEmil Constantinescu /*@
4296e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4297e817cc15SEmil Constantinescu 
4298e817cc15SEmil Constantinescu    Not Collective
4299e817cc15SEmil Constantinescu 
4300e817cc15SEmil Constantinescu    Input Parameter:
4301e817cc15SEmil Constantinescu .  ts - the TS context
4302e817cc15SEmil Constantinescu 
4303e817cc15SEmil Constantinescu    Output Parameter:
43044e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4305e817cc15SEmil Constantinescu 
4306e817cc15SEmil Constantinescu    Level: beginner
4307e817cc15SEmil Constantinescu 
4308e817cc15SEmil Constantinescu .keywords: TS, equation type
4309e817cc15SEmil Constantinescu 
4310e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4311e817cc15SEmil Constantinescu @*/
4312e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4313e817cc15SEmil Constantinescu {
4314e817cc15SEmil Constantinescu   PetscFunctionBegin;
4315e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4316e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4317e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4318e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4319e817cc15SEmil Constantinescu }
4320e817cc15SEmil Constantinescu 
4321e817cc15SEmil Constantinescu #undef __FUNCT__
4322e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
4323e817cc15SEmil Constantinescu /*@
4324e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4325e817cc15SEmil Constantinescu 
4326e817cc15SEmil Constantinescu    Not Collective
4327e817cc15SEmil Constantinescu 
4328e817cc15SEmil Constantinescu    Input Parameter:
4329e817cc15SEmil Constantinescu +  ts - the TS context
43304e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4331e817cc15SEmil Constantinescu 
4332e817cc15SEmil Constantinescu    Level: advanced
4333e817cc15SEmil Constantinescu 
4334e817cc15SEmil Constantinescu .keywords: TS, equation type
4335e817cc15SEmil Constantinescu 
4336e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4337e817cc15SEmil Constantinescu @*/
4338e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4339e817cc15SEmil Constantinescu {
4340e817cc15SEmil Constantinescu   PetscFunctionBegin;
4341e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4342e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4343e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4344e817cc15SEmil Constantinescu }
43450026cea9SSean Farley 
43464af1b03aSJed Brown #undef __FUNCT__
43474af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
43484af1b03aSJed Brown /*@
43494af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
43504af1b03aSJed Brown 
43514af1b03aSJed Brown    Not Collective
43524af1b03aSJed Brown 
43534af1b03aSJed Brown    Input Parameter:
43544af1b03aSJed Brown .  ts - the TS context
43554af1b03aSJed Brown 
43564af1b03aSJed Brown    Output Parameter:
43574af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
43584af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
43594af1b03aSJed Brown 
4360487e0bb9SJed Brown    Level: beginner
43614af1b03aSJed Brown 
4362cd652676SJed Brown    Notes:
4363cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
43644af1b03aSJed Brown 
43654af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
43664af1b03aSJed Brown 
43674af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
43684af1b03aSJed Brown @*/
43694af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
43704af1b03aSJed Brown {
43714af1b03aSJed Brown   PetscFunctionBegin;
43724af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
43734af1b03aSJed Brown   PetscValidPointer(reason,2);
43744af1b03aSJed Brown   *reason = ts->reason;
43754af1b03aSJed Brown   PetscFunctionReturn(0);
43764af1b03aSJed Brown }
43774af1b03aSJed Brown 
4378fb1732b5SBarry Smith #undef __FUNCT__
4379d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
4380d6ad946cSShri Abhyankar /*@
4381d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4382d6ad946cSShri Abhyankar 
4383d6ad946cSShri Abhyankar    Not Collective
4384d6ad946cSShri Abhyankar 
4385d6ad946cSShri Abhyankar    Input Parameter:
4386d6ad946cSShri Abhyankar +  ts - the TS context
4387d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4388d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4389d6ad946cSShri Abhyankar 
4390f5abba47SShri Abhyankar    Level: advanced
4391d6ad946cSShri Abhyankar 
4392d6ad946cSShri Abhyankar    Notes:
4393d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
4394d6ad946cSShri Abhyankar 
4395d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
4396d6ad946cSShri Abhyankar 
4397d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4398d6ad946cSShri Abhyankar @*/
4399d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4400d6ad946cSShri Abhyankar {
4401d6ad946cSShri Abhyankar   PetscFunctionBegin;
4402d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4403d6ad946cSShri Abhyankar   ts->reason = reason;
4404d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4405d6ad946cSShri Abhyankar }
4406d6ad946cSShri Abhyankar 
4407d6ad946cSShri Abhyankar #undef __FUNCT__
4408cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
4409cc708dedSBarry Smith /*@
4410cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4411cc708dedSBarry Smith 
4412cc708dedSBarry Smith    Not Collective
4413cc708dedSBarry Smith 
4414cc708dedSBarry Smith    Input Parameter:
4415cc708dedSBarry Smith .  ts - the TS context
4416cc708dedSBarry Smith 
4417cc708dedSBarry Smith    Output Parameter:
4418cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
4419cc708dedSBarry Smith 
4420487e0bb9SJed Brown    Level: beginner
4421cc708dedSBarry Smith 
4422cc708dedSBarry Smith    Notes:
4423cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4424cc708dedSBarry Smith 
4425cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
4426cc708dedSBarry Smith 
4427cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4428cc708dedSBarry Smith @*/
4429cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4430cc708dedSBarry Smith {
4431cc708dedSBarry Smith   PetscFunctionBegin;
4432cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4433cc708dedSBarry Smith   PetscValidPointer(ftime,2);
4434cc708dedSBarry Smith   *ftime = ts->solvetime;
4435cc708dedSBarry Smith   PetscFunctionReturn(0);
4436cc708dedSBarry Smith }
4437cc708dedSBarry Smith 
4438cc708dedSBarry Smith #undef __FUNCT__
44392c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps"
44402c18e0fdSBarry Smith /*@
44412c18e0fdSBarry Smith    TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate()
44422c18e0fdSBarry Smith 
44432c18e0fdSBarry Smith    Not Collective
44442c18e0fdSBarry Smith 
44452c18e0fdSBarry Smith    Input Parameter:
44462c18e0fdSBarry Smith .  ts - the TS context
44472c18e0fdSBarry Smith 
44482c18e0fdSBarry Smith    Output Parameter:
44492c18e0fdSBarry Smith .  steps - the number of steps
44502c18e0fdSBarry Smith 
44512c18e0fdSBarry Smith    Level: beginner
44522c18e0fdSBarry Smith 
44532c18e0fdSBarry Smith    Notes:
44542c18e0fdSBarry Smith    Includes the number of steps for all calls to TSSolve() since TSSetUp() was called
44552c18e0fdSBarry Smith 
44562c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test
44572c18e0fdSBarry Smith 
44582c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
44592c18e0fdSBarry Smith @*/
44602c18e0fdSBarry Smith PetscErrorCode  TSGetTotalSteps(TS ts,PetscInt *steps)
44612c18e0fdSBarry Smith {
44622c18e0fdSBarry Smith   PetscFunctionBegin;
44632c18e0fdSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44642c18e0fdSBarry Smith   PetscValidPointer(steps,2);
44652c18e0fdSBarry Smith   *steps = ts->total_steps;
44662c18e0fdSBarry Smith   PetscFunctionReturn(0);
44672c18e0fdSBarry Smith }
44682c18e0fdSBarry Smith 
44692c18e0fdSBarry Smith #undef __FUNCT__
44705ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
44719f67acb7SJed Brown /*@
44725ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
44739f67acb7SJed Brown    used by the time integrator.
44749f67acb7SJed Brown 
44759f67acb7SJed Brown    Not Collective
44769f67acb7SJed Brown 
44779f67acb7SJed Brown    Input Parameter:
44789f67acb7SJed Brown .  ts - TS context
44799f67acb7SJed Brown 
44809f67acb7SJed Brown    Output Parameter:
44819f67acb7SJed Brown .  nits - number of nonlinear iterations
44829f67acb7SJed Brown 
44839f67acb7SJed Brown    Notes:
44849f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
44859f67acb7SJed Brown 
44869f67acb7SJed Brown    Level: intermediate
44879f67acb7SJed Brown 
44889f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
44899f67acb7SJed Brown 
44905ef26d82SJed Brown .seealso:  TSGetKSPIterations()
44919f67acb7SJed Brown @*/
44925ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
44939f67acb7SJed Brown {
44949f67acb7SJed Brown   PetscFunctionBegin;
44959f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44969f67acb7SJed Brown   PetscValidIntPointer(nits,2);
44975ef26d82SJed Brown   *nits = ts->snes_its;
44989f67acb7SJed Brown   PetscFunctionReturn(0);
44999f67acb7SJed Brown }
45009f67acb7SJed Brown 
45019f67acb7SJed Brown #undef __FUNCT__
45025ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
45039f67acb7SJed Brown /*@
45045ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
45059f67acb7SJed Brown    used by the time integrator.
45069f67acb7SJed Brown 
45079f67acb7SJed Brown    Not Collective
45089f67acb7SJed Brown 
45099f67acb7SJed Brown    Input Parameter:
45109f67acb7SJed Brown .  ts - TS context
45119f67acb7SJed Brown 
45129f67acb7SJed Brown    Output Parameter:
45139f67acb7SJed Brown .  lits - number of linear iterations
45149f67acb7SJed Brown 
45159f67acb7SJed Brown    Notes:
45169f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
45179f67acb7SJed Brown 
45189f67acb7SJed Brown    Level: intermediate
45199f67acb7SJed Brown 
45209f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
45219f67acb7SJed Brown 
45225ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
45239f67acb7SJed Brown @*/
45245ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
45259f67acb7SJed Brown {
45269f67acb7SJed Brown   PetscFunctionBegin;
45279f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45289f67acb7SJed Brown   PetscValidIntPointer(lits,2);
45295ef26d82SJed Brown   *lits = ts->ksp_its;
45309f67acb7SJed Brown   PetscFunctionReturn(0);
45319f67acb7SJed Brown }
45329f67acb7SJed Brown 
45339f67acb7SJed Brown #undef __FUNCT__
4534cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
4535cef5090cSJed Brown /*@
4536cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
4537cef5090cSJed Brown 
4538cef5090cSJed Brown    Not Collective
4539cef5090cSJed Brown 
4540cef5090cSJed Brown    Input Parameter:
4541cef5090cSJed Brown .  ts - TS context
4542cef5090cSJed Brown 
4543cef5090cSJed Brown    Output Parameter:
4544cef5090cSJed Brown .  rejects - number of steps rejected
4545cef5090cSJed Brown 
4546cef5090cSJed Brown    Notes:
4547cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4548cef5090cSJed Brown 
4549cef5090cSJed Brown    Level: intermediate
4550cef5090cSJed Brown 
4551cef5090cSJed Brown .keywords: TS, get, number
4552cef5090cSJed Brown 
45535ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
4554cef5090cSJed Brown @*/
4555cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
4556cef5090cSJed Brown {
4557cef5090cSJed Brown   PetscFunctionBegin;
4558cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4559cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
4560cef5090cSJed Brown   *rejects = ts->reject;
4561cef5090cSJed Brown   PetscFunctionReturn(0);
4562cef5090cSJed Brown }
4563cef5090cSJed Brown 
4564cef5090cSJed Brown #undef __FUNCT__
4565cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
4566cef5090cSJed Brown /*@
4567cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
4568cef5090cSJed Brown 
4569cef5090cSJed Brown    Not Collective
4570cef5090cSJed Brown 
4571cef5090cSJed Brown    Input Parameter:
4572cef5090cSJed Brown .  ts - TS context
4573cef5090cSJed Brown 
4574cef5090cSJed Brown    Output Parameter:
4575cef5090cSJed Brown .  fails - number of failed nonlinear solves
4576cef5090cSJed Brown 
4577cef5090cSJed Brown    Notes:
4578cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4579cef5090cSJed Brown 
4580cef5090cSJed Brown    Level: intermediate
4581cef5090cSJed Brown 
4582cef5090cSJed Brown .keywords: TS, get, number
4583cef5090cSJed Brown 
45845ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
4585cef5090cSJed Brown @*/
4586cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
4587cef5090cSJed Brown {
4588cef5090cSJed Brown   PetscFunctionBegin;
4589cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4590cef5090cSJed Brown   PetscValidIntPointer(fails,2);
4591cef5090cSJed Brown   *fails = ts->num_snes_failures;
4592cef5090cSJed Brown   PetscFunctionReturn(0);
4593cef5090cSJed Brown }
4594cef5090cSJed Brown 
4595cef5090cSJed Brown #undef __FUNCT__
4596cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
4597cef5090cSJed Brown /*@
4598cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
4599cef5090cSJed Brown 
4600cef5090cSJed Brown    Not Collective
4601cef5090cSJed Brown 
4602cef5090cSJed Brown    Input Parameter:
4603cef5090cSJed Brown +  ts - TS context
4604cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
4605cef5090cSJed Brown 
4606cef5090cSJed Brown    Notes:
4607cef5090cSJed Brown    The counter is reset to zero for each step
4608cef5090cSJed Brown 
4609cef5090cSJed Brown    Options Database Key:
4610cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
4611cef5090cSJed Brown 
4612cef5090cSJed Brown    Level: intermediate
4613cef5090cSJed Brown 
4614cef5090cSJed Brown .keywords: TS, set, maximum, number
4615cef5090cSJed Brown 
46165ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4617cef5090cSJed Brown @*/
4618cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
4619cef5090cSJed Brown {
4620cef5090cSJed Brown   PetscFunctionBegin;
4621cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4622cef5090cSJed Brown   ts->max_reject = rejects;
4623cef5090cSJed Brown   PetscFunctionReturn(0);
4624cef5090cSJed Brown }
4625cef5090cSJed Brown 
4626cef5090cSJed Brown #undef __FUNCT__
4627cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
4628cef5090cSJed Brown /*@
4629cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
4630cef5090cSJed Brown 
4631cef5090cSJed Brown    Not Collective
4632cef5090cSJed Brown 
4633cef5090cSJed Brown    Input Parameter:
4634cef5090cSJed Brown +  ts - TS context
4635cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
4636cef5090cSJed Brown 
4637cef5090cSJed Brown    Notes:
4638cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
4639cef5090cSJed Brown 
4640cef5090cSJed Brown    Options Database Key:
4641cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
4642cef5090cSJed Brown 
4643cef5090cSJed Brown    Level: intermediate
4644cef5090cSJed Brown 
4645cef5090cSJed Brown .keywords: TS, set, maximum, number
4646cef5090cSJed Brown 
46475ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
4648cef5090cSJed Brown @*/
4649cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
4650cef5090cSJed Brown {
4651cef5090cSJed Brown   PetscFunctionBegin;
4652cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4653cef5090cSJed Brown   ts->max_snes_failures = fails;
4654cef5090cSJed Brown   PetscFunctionReturn(0);
4655cef5090cSJed Brown }
4656cef5090cSJed Brown 
4657cef5090cSJed Brown #undef __FUNCT__
46584e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails"
4659cef5090cSJed Brown /*@
4660cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
4661cef5090cSJed Brown 
4662cef5090cSJed Brown    Not Collective
4663cef5090cSJed Brown 
4664cef5090cSJed Brown    Input Parameter:
4665cef5090cSJed Brown +  ts - TS context
4666cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
4667cef5090cSJed Brown 
4668cef5090cSJed Brown    Options Database Key:
4669cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
4670cef5090cSJed Brown 
4671cef5090cSJed Brown    Level: intermediate
4672cef5090cSJed Brown 
4673cef5090cSJed Brown .keywords: TS, set, error
4674cef5090cSJed Brown 
46755ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4676cef5090cSJed Brown @*/
4677cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
4678cef5090cSJed Brown {
4679cef5090cSJed Brown   PetscFunctionBegin;
4680cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4681cef5090cSJed Brown   ts->errorifstepfailed = err;
4682cef5090cSJed Brown   PetscFunctionReturn(0);
4683cef5090cSJed Brown }
4684cef5090cSJed Brown 
4685cef5090cSJed Brown #undef __FUNCT__
4686fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
4687fb1732b5SBarry Smith /*@C
4688fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
4689fb1732b5SBarry Smith 
4690fb1732b5SBarry Smith    Collective on TS
4691fb1732b5SBarry Smith 
4692fb1732b5SBarry Smith    Input Parameters:
4693fb1732b5SBarry Smith +  ts - the TS context
4694fb1732b5SBarry Smith .  step - current time-step
4695fb1732b5SBarry Smith .  ptime - current time
46960910c330SBarry Smith .  u - current state
4697fb1732b5SBarry Smith -  viewer - binary viewer
4698fb1732b5SBarry Smith 
4699fb1732b5SBarry Smith    Level: intermediate
4700fb1732b5SBarry Smith 
4701fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
4702fb1732b5SBarry Smith 
4703fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4704fb1732b5SBarry Smith @*/
47050910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
4706fb1732b5SBarry Smith {
4707fb1732b5SBarry Smith   PetscErrorCode ierr;
4708ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
4709fb1732b5SBarry Smith 
4710fb1732b5SBarry Smith   PetscFunctionBegin;
47110910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
4712ed81e22dSJed Brown   PetscFunctionReturn(0);
4713ed81e22dSJed Brown }
4714ed81e22dSJed Brown 
4715ed81e22dSJed Brown #undef __FUNCT__
4716ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
4717ed81e22dSJed Brown /*@C
4718ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
4719ed81e22dSJed Brown 
4720ed81e22dSJed Brown    Collective on TS
4721ed81e22dSJed Brown 
4722ed81e22dSJed Brown    Input Parameters:
4723ed81e22dSJed Brown +  ts - the TS context
4724ed81e22dSJed Brown .  step - current time-step
4725ed81e22dSJed Brown .  ptime - current time
47260910c330SBarry Smith .  u - current state
4727ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4728ed81e22dSJed Brown 
4729ed81e22dSJed Brown    Level: intermediate
4730ed81e22dSJed Brown 
4731ed81e22dSJed Brown    Notes:
4732ed81e22dSJed 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.
4733ed81e22dSJed Brown    These are named according to the file name template.
4734ed81e22dSJed Brown 
4735ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
4736ed81e22dSJed Brown 
4737ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4738ed81e22dSJed Brown 
4739ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4740ed81e22dSJed Brown @*/
47410910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
4742ed81e22dSJed Brown {
4743ed81e22dSJed Brown   PetscErrorCode ierr;
4744ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
4745ed81e22dSJed Brown   PetscViewer    viewer;
4746ed81e22dSJed Brown 
4747ed81e22dSJed Brown   PetscFunctionBegin;
47488caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
4749ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
47500910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
4751ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4752ed81e22dSJed Brown   PetscFunctionReturn(0);
4753ed81e22dSJed Brown }
4754ed81e22dSJed Brown 
4755ed81e22dSJed Brown #undef __FUNCT__
4756ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
4757ed81e22dSJed Brown /*@C
4758ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
4759ed81e22dSJed Brown 
4760ed81e22dSJed Brown    Collective on TS
4761ed81e22dSJed Brown 
4762ed81e22dSJed Brown    Input Parameters:
4763ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4764ed81e22dSJed Brown 
4765ed81e22dSJed Brown    Level: intermediate
4766ed81e22dSJed Brown 
4767ed81e22dSJed Brown    Note:
4768ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
4769ed81e22dSJed Brown 
4770ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4771ed81e22dSJed Brown 
4772ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
4773ed81e22dSJed Brown @*/
4774ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
4775ed81e22dSJed Brown {
4776ed81e22dSJed Brown   PetscErrorCode ierr;
4777ed81e22dSJed Brown 
4778ed81e22dSJed Brown   PetscFunctionBegin;
4779ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
4780fb1732b5SBarry Smith   PetscFunctionReturn(0);
4781fb1732b5SBarry Smith }
4782fb1732b5SBarry Smith 
478384df9cb4SJed Brown #undef __FUNCT__
4784552698daSJed Brown #define __FUNCT__ "TSGetAdapt"
478584df9cb4SJed Brown /*@
4786552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
478784df9cb4SJed Brown 
4788ed81e22dSJed Brown    Collective on TS if controller has not been created yet
478984df9cb4SJed Brown 
479084df9cb4SJed Brown    Input Arguments:
4791ed81e22dSJed Brown .  ts - time stepping context
479284df9cb4SJed Brown 
479384df9cb4SJed Brown    Output Arguments:
4794ed81e22dSJed Brown .  adapt - adaptive controller
479584df9cb4SJed Brown 
479684df9cb4SJed Brown    Level: intermediate
479784df9cb4SJed Brown 
4798ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
479984df9cb4SJed Brown @*/
4800552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
480184df9cb4SJed Brown {
480284df9cb4SJed Brown   PetscErrorCode ierr;
480384df9cb4SJed Brown 
480484df9cb4SJed Brown   PetscFunctionBegin;
480584df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
480684df9cb4SJed Brown   PetscValidPointer(adapt,2);
480784df9cb4SJed Brown   if (!ts->adapt) {
4808ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
48093bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
48101c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
481184df9cb4SJed Brown   }
481284df9cb4SJed Brown   *adapt = ts->adapt;
481384df9cb4SJed Brown   PetscFunctionReturn(0);
481484df9cb4SJed Brown }
4815d6ebe24aSShri Abhyankar 
4816d6ebe24aSShri Abhyankar #undef __FUNCT__
48171c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
48181c3436cfSJed Brown /*@
48191c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
48201c3436cfSJed Brown 
48211c3436cfSJed Brown    Logically Collective
48221c3436cfSJed Brown 
48231c3436cfSJed Brown    Input Arguments:
48241c3436cfSJed Brown +  ts - time integration context
48251c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
48260298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
48271c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
48280298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
48291c3436cfSJed Brown 
4830a3cdaa26SBarry Smith    Options Database keys:
4831a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
4832a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
4833a3cdaa26SBarry Smith 
48341c3436cfSJed Brown    Level: beginner
48351c3436cfSJed Brown 
4836c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
48371c3436cfSJed Brown @*/
48381c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
48391c3436cfSJed Brown {
48401c3436cfSJed Brown   PetscErrorCode ierr;
48411c3436cfSJed Brown 
48421c3436cfSJed Brown   PetscFunctionBegin;
4843c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
48441c3436cfSJed Brown   if (vatol) {
48451c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
48461c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
4847bbd56ea5SKarl Rupp 
48481c3436cfSJed Brown     ts->vatol = vatol;
48491c3436cfSJed Brown   }
4850c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
48511c3436cfSJed Brown   if (vrtol) {
48521c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
48531c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
4854bbd56ea5SKarl Rupp 
48551c3436cfSJed Brown     ts->vrtol = vrtol;
48561c3436cfSJed Brown   }
48571c3436cfSJed Brown   PetscFunctionReturn(0);
48581c3436cfSJed Brown }
48591c3436cfSJed Brown 
48601c3436cfSJed Brown #undef __FUNCT__
4861c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
4862c5033834SJed Brown /*@
4863c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
4864c5033834SJed Brown 
4865c5033834SJed Brown    Logically Collective
4866c5033834SJed Brown 
4867c5033834SJed Brown    Input Arguments:
4868c5033834SJed Brown .  ts - time integration context
4869c5033834SJed Brown 
4870c5033834SJed Brown    Output Arguments:
48710298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
48720298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
48730298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
48740298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
4875c5033834SJed Brown 
4876c5033834SJed Brown    Level: beginner
4877c5033834SJed Brown 
4878c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
4879c5033834SJed Brown @*/
4880c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
4881c5033834SJed Brown {
4882c5033834SJed Brown   PetscFunctionBegin;
4883c5033834SJed Brown   if (atol)  *atol  = ts->atol;
4884c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
4885c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
4886c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
4887c5033834SJed Brown   PetscFunctionReturn(0);
4888c5033834SJed Brown }
4889c5033834SJed Brown 
4890c5033834SJed Brown #undef __FUNCT__
48911c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
48921c3436cfSJed Brown /*@
48931c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
48941c3436cfSJed Brown 
48951c3436cfSJed Brown    Collective on TS
48961c3436cfSJed Brown 
48971c3436cfSJed Brown    Input Arguments:
48981c3436cfSJed Brown +  ts - time stepping context
48991c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
49001c3436cfSJed Brown 
49011c3436cfSJed Brown    Output Arguments:
49021c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
49031c3436cfSJed Brown 
49041c3436cfSJed Brown    Level: developer
49051c3436cfSJed Brown 
49061c3436cfSJed Brown .seealso: TSSetTolerances()
49071c3436cfSJed Brown @*/
49081c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
49091c3436cfSJed Brown {
49101c3436cfSJed Brown   PetscErrorCode    ierr;
49111c3436cfSJed Brown   PetscInt          i,n,N;
49120910c330SBarry Smith   const PetscScalar *u,*y;
49130910c330SBarry Smith   Vec               U;
49141c3436cfSJed Brown   PetscReal         sum,gsum;
49151c3436cfSJed Brown 
49161c3436cfSJed Brown   PetscFunctionBegin;
49171c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49181c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
49191c3436cfSJed Brown   PetscValidPointer(norm,3);
49200910c330SBarry Smith   U = ts->vec_sol;
49210910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
4922ce94432eSBarry Smith   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
49231c3436cfSJed Brown 
49240910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
49250910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
49260910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
49271c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
49281c3436cfSJed Brown   sum  = 0.;
4929ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
4930ceefc113SJed Brown     const PetscScalar *atol,*rtol;
4931ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4932ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4933ceefc113SJed Brown     for (i=0; i<n; i++) {
49340910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
49350910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4936ceefc113SJed Brown     }
4937ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4938ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4939ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
4940ceefc113SJed Brown     const PetscScalar *atol;
4941ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4942ceefc113SJed Brown     for (i=0; i<n; i++) {
49430910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
49440910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4945ceefc113SJed Brown     }
4946ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4947ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
4948ceefc113SJed Brown     const PetscScalar *rtol;
4949ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4950ceefc113SJed Brown     for (i=0; i<n; i++) {
49510910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
49520910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4953ceefc113SJed Brown     }
4954ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4955ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
49561c3436cfSJed Brown     for (i=0; i<n; i++) {
49570910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
49580910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
49591c3436cfSJed Brown     }
4960ceefc113SJed Brown   }
49610910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
49621c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
49631c3436cfSJed Brown 
4964ce94432eSBarry Smith   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
49651c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
4966620fc8aaSSatish Balay   if (PetscIsInfOrNanReal(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
49671c3436cfSJed Brown   PetscFunctionReturn(0);
49681c3436cfSJed Brown }
49691c3436cfSJed Brown 
49701c3436cfSJed Brown #undef __FUNCT__
49718d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
49728d59e960SJed Brown /*@
49738d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
49748d59e960SJed Brown 
49758d59e960SJed Brown    Logically Collective on TS
49768d59e960SJed Brown 
49778d59e960SJed Brown    Input Arguments:
49788d59e960SJed Brown +  ts - time stepping context
49798d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
49808d59e960SJed Brown 
49818d59e960SJed Brown    Note:
49828d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
49838d59e960SJed Brown 
49848d59e960SJed Brown    Level: intermediate
49858d59e960SJed Brown 
49868d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
49878d59e960SJed Brown @*/
49888d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
49898d59e960SJed Brown {
49908d59e960SJed Brown   PetscFunctionBegin;
49918d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49928d59e960SJed Brown   ts->cfltime_local = cfltime;
49938d59e960SJed Brown   ts->cfltime       = -1.;
49948d59e960SJed Brown   PetscFunctionReturn(0);
49958d59e960SJed Brown }
49968d59e960SJed Brown 
49978d59e960SJed Brown #undef __FUNCT__
49988d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
49998d59e960SJed Brown /*@
50008d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
50018d59e960SJed Brown 
50028d59e960SJed Brown    Collective on TS
50038d59e960SJed Brown 
50048d59e960SJed Brown    Input Arguments:
50058d59e960SJed Brown .  ts - time stepping context
50068d59e960SJed Brown 
50078d59e960SJed Brown    Output Arguments:
50088d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
50098d59e960SJed Brown 
50108d59e960SJed Brown    Level: advanced
50118d59e960SJed Brown 
50128d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
50138d59e960SJed Brown @*/
50148d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
50158d59e960SJed Brown {
50168d59e960SJed Brown   PetscErrorCode ierr;
50178d59e960SJed Brown 
50188d59e960SJed Brown   PetscFunctionBegin;
50198d59e960SJed Brown   if (ts->cfltime < 0) {
5020ce94432eSBarry Smith     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
50218d59e960SJed Brown   }
50228d59e960SJed Brown   *cfltime = ts->cfltime;
50238d59e960SJed Brown   PetscFunctionReturn(0);
50248d59e960SJed Brown }
50258d59e960SJed Brown 
50268d59e960SJed Brown #undef __FUNCT__
5027d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
5028d6ebe24aSShri Abhyankar /*@
5029d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
5030d6ebe24aSShri Abhyankar 
5031d6ebe24aSShri Abhyankar    Input Parameters:
5032d6ebe24aSShri Abhyankar .  ts   - the TS context.
5033d6ebe24aSShri Abhyankar .  xl   - lower bound.
5034d6ebe24aSShri Abhyankar .  xu   - upper bound.
5035d6ebe24aSShri Abhyankar 
5036d6ebe24aSShri Abhyankar    Notes:
5037d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
5038e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
5039d6ebe24aSShri Abhyankar 
50402bd2b0e6SSatish Balay    Level: advanced
50412bd2b0e6SSatish Balay 
5042d6ebe24aSShri Abhyankar @*/
5043d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
5044d6ebe24aSShri Abhyankar {
5045d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
5046d6ebe24aSShri Abhyankar   SNES           snes;
5047d6ebe24aSShri Abhyankar 
5048d6ebe24aSShri Abhyankar   PetscFunctionBegin;
5049d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
5050d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
5051d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
5052d6ebe24aSShri Abhyankar }
5053d6ebe24aSShri Abhyankar 
5054325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
5055c6db04a5SJed Brown #include <mex.h>
5056325fc9f4SBarry Smith 
5057325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
5058325fc9f4SBarry Smith 
5059325fc9f4SBarry Smith #undef __FUNCT__
5060325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
5061325fc9f4SBarry Smith /*
5062325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
5063325fc9f4SBarry Smith                          TSSetFunctionMatlab().
5064325fc9f4SBarry Smith 
5065325fc9f4SBarry Smith    Collective on TS
5066325fc9f4SBarry Smith 
5067325fc9f4SBarry Smith    Input Parameters:
5068325fc9f4SBarry Smith +  snes - the TS context
50690910c330SBarry Smith -  u - input vector
5070325fc9f4SBarry Smith 
5071325fc9f4SBarry Smith    Output Parameter:
5072325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
5073325fc9f4SBarry Smith 
5074325fc9f4SBarry Smith    Notes:
5075325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
5076325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
5077325fc9f4SBarry Smith    themselves.
5078325fc9f4SBarry Smith 
5079325fc9f4SBarry Smith    Level: developer
5080325fc9f4SBarry Smith 
5081325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5082325fc9f4SBarry Smith 
5083325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5084325fc9f4SBarry Smith */
50850910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
5086325fc9f4SBarry Smith {
5087325fc9f4SBarry Smith   PetscErrorCode  ierr;
5088325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5089325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
5090325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
5091325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
5092325fc9f4SBarry Smith 
5093325fc9f4SBarry Smith   PetscFunctionBegin;
5094325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
50950910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
50960910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
5097325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
50980910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
5099325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
5100325fc9f4SBarry Smith 
5101325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
51020910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
51030910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
51040910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
5105bbd56ea5SKarl Rupp 
5106325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5107325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
5108325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5109325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5110325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
5111325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
5112325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
5113325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
5114325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5115325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5116325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5117325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5118325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5119325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5120325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5121325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5122325fc9f4SBarry Smith   PetscFunctionReturn(0);
5123325fc9f4SBarry Smith }
5124325fc9f4SBarry Smith 
5125325fc9f4SBarry Smith 
5126325fc9f4SBarry Smith #undef __FUNCT__
5127325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
5128325fc9f4SBarry Smith /*
5129325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
5130325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
5131e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
5132325fc9f4SBarry Smith 
5133325fc9f4SBarry Smith    Logically Collective on TS
5134325fc9f4SBarry Smith 
5135325fc9f4SBarry Smith    Input Parameters:
5136325fc9f4SBarry Smith +  ts - the TS context
5137325fc9f4SBarry Smith -  func - function evaluation routine
5138325fc9f4SBarry Smith 
5139325fc9f4SBarry Smith    Calling sequence of func:
51400910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
5141325fc9f4SBarry Smith 
5142325fc9f4SBarry Smith    Level: beginner
5143325fc9f4SBarry Smith 
5144325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5145325fc9f4SBarry Smith 
5146325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5147325fc9f4SBarry Smith */
5148cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
5149325fc9f4SBarry Smith {
5150325fc9f4SBarry Smith   PetscErrorCode  ierr;
5151325fc9f4SBarry Smith   TSMatlabContext *sctx;
5152325fc9f4SBarry Smith 
5153325fc9f4SBarry Smith   PetscFunctionBegin;
5154325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5155325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5156325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5157325fc9f4SBarry Smith   /*
5158325fc9f4SBarry Smith      This should work, but it doesn't
5159325fc9f4SBarry Smith   sctx->ctx = ctx;
5160325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5161325fc9f4SBarry Smith   */
5162325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5163bbd56ea5SKarl Rupp 
51640298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
5165325fc9f4SBarry Smith   PetscFunctionReturn(0);
5166325fc9f4SBarry Smith }
5167325fc9f4SBarry Smith 
5168325fc9f4SBarry Smith #undef __FUNCT__
5169325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
5170325fc9f4SBarry Smith /*
5171325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
5172325fc9f4SBarry Smith                          TSSetJacobianMatlab().
5173325fc9f4SBarry Smith 
5174325fc9f4SBarry Smith    Collective on TS
5175325fc9f4SBarry Smith 
5176325fc9f4SBarry Smith    Input Parameters:
5177cdcf91faSSean Farley +  ts - the TS context
51780910c330SBarry Smith .  u - input vector
5179325fc9f4SBarry Smith .  A, B - the matrices
5180325fc9f4SBarry Smith -  ctx - user context
5181325fc9f4SBarry Smith 
5182325fc9f4SBarry Smith    Level: developer
5183325fc9f4SBarry Smith 
5184325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5185325fc9f4SBarry Smith 
5186325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5187325fc9f4SBarry Smith @*/
5188f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
5189325fc9f4SBarry Smith {
5190325fc9f4SBarry Smith   PetscErrorCode  ierr;
5191325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5192325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
5193325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
5194325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
5195325fc9f4SBarry Smith 
5196325fc9f4SBarry Smith   PetscFunctionBegin;
5197cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
51980910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
5199325fc9f4SBarry Smith 
52000910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
5201325fc9f4SBarry Smith 
5202cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
52030910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
52040910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
52050910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
52060910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
5207bbd56ea5SKarl Rupp 
5208325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5209325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
5210325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5211325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5212325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
5213325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
5214325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
5215325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
5216325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
5217325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
5218325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5219325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5220325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5221325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5222325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5223325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5224325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5225325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
5226325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
5227325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5228325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
5229325fc9f4SBarry Smith   PetscFunctionReturn(0);
5230325fc9f4SBarry Smith }
5231325fc9f4SBarry Smith 
5232325fc9f4SBarry Smith 
5233325fc9f4SBarry Smith #undef __FUNCT__
5234325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
5235325fc9f4SBarry Smith /*
5236325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
5237e3c5b3baSBarry 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
5238325fc9f4SBarry Smith 
5239325fc9f4SBarry Smith    Logically Collective on TS
5240325fc9f4SBarry Smith 
5241325fc9f4SBarry Smith    Input Parameters:
5242cdcf91faSSean Farley +  ts - the TS context
5243325fc9f4SBarry Smith .  A,B - Jacobian matrices
5244325fc9f4SBarry Smith .  func - function evaluation routine
5245325fc9f4SBarry Smith -  ctx - user context
5246325fc9f4SBarry Smith 
5247325fc9f4SBarry Smith    Calling sequence of func:
52480910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
5249325fc9f4SBarry Smith 
5250325fc9f4SBarry Smith 
5251325fc9f4SBarry Smith    Level: developer
5252325fc9f4SBarry Smith 
5253325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5254325fc9f4SBarry Smith 
5255325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5256325fc9f4SBarry Smith */
5257cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
5258325fc9f4SBarry Smith {
5259325fc9f4SBarry Smith   PetscErrorCode  ierr;
5260325fc9f4SBarry Smith   TSMatlabContext *sctx;
5261325fc9f4SBarry Smith 
5262325fc9f4SBarry Smith   PetscFunctionBegin;
5263325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5264325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5265325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5266325fc9f4SBarry Smith   /*
5267325fc9f4SBarry Smith      This should work, but it doesn't
5268325fc9f4SBarry Smith   sctx->ctx = ctx;
5269325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5270325fc9f4SBarry Smith   */
5271325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5272bbd56ea5SKarl Rupp 
5273cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
5274325fc9f4SBarry Smith   PetscFunctionReturn(0);
5275325fc9f4SBarry Smith }
5276325fc9f4SBarry Smith 
5277b5b1a830SBarry Smith #undef __FUNCT__
5278b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
5279b5b1a830SBarry Smith /*
5280b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
5281b5b1a830SBarry Smith 
5282b5b1a830SBarry Smith    Collective on TS
5283b5b1a830SBarry Smith 
5284b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5285b5b1a830SBarry Smith @*/
52860910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
5287b5b1a830SBarry Smith {
5288b5b1a830SBarry Smith   PetscErrorCode  ierr;
5289b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5290a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
5291b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
5292b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
5293b5b1a830SBarry Smith 
5294b5b1a830SBarry Smith   PetscFunctionBegin;
5295cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52960910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
5297b5b1a830SBarry Smith 
5298cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
52990910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
5300bbd56ea5SKarl Rupp 
5301b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5302b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
5303b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
5304b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
5305b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
5306b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
5307b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
5308b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5309b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
5310b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
5311b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
5312b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
5313b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
5314b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
5315b5b1a830SBarry Smith   PetscFunctionReturn(0);
5316b5b1a830SBarry Smith }
5317b5b1a830SBarry Smith 
5318b5b1a830SBarry Smith 
5319b5b1a830SBarry Smith #undef __FUNCT__
5320b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
5321b5b1a830SBarry Smith /*
5322b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
5323b5b1a830SBarry Smith 
5324b5b1a830SBarry Smith    Level: developer
5325b5b1a830SBarry Smith 
5326b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
5327b5b1a830SBarry Smith 
5328b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5329b5b1a830SBarry Smith */
5330cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
5331b5b1a830SBarry Smith {
5332b5b1a830SBarry Smith   PetscErrorCode  ierr;
5333b5b1a830SBarry Smith   TSMatlabContext *sctx;
5334b5b1a830SBarry Smith 
5335b5b1a830SBarry Smith   PetscFunctionBegin;
5336b5b1a830SBarry Smith   /* currently sctx is memory bleed */
5337b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5338b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5339b5b1a830SBarry Smith   /*
5340b5b1a830SBarry Smith      This should work, but it doesn't
5341b5b1a830SBarry Smith   sctx->ctx = ctx;
5342b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5343b5b1a830SBarry Smith   */
5344b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5345bbd56ea5SKarl Rupp 
53460298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
5347b5b1a830SBarry Smith   PetscFunctionReturn(0);
5348b5b1a830SBarry Smith }
5349325fc9f4SBarry Smith #endif
5350b3603a34SBarry Smith 
53510b039ecaSBarry Smith 
53520b039ecaSBarry Smith 
5353b3603a34SBarry Smith #undef __FUNCT__
53544f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
5355b3603a34SBarry Smith /*@C
53564f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
5357b3603a34SBarry Smith        in a time based line graph
5358b3603a34SBarry Smith 
5359b3603a34SBarry Smith    Collective on TS
5360b3603a34SBarry Smith 
5361b3603a34SBarry Smith    Input Parameters:
5362b3603a34SBarry Smith +  ts - the TS context
5363b3603a34SBarry Smith .  step - current time-step
5364b3603a34SBarry Smith .  ptime - current time
5365b3603a34SBarry Smith -  lg - a line graph object
5366b3603a34SBarry Smith 
5367b3603a34SBarry Smith    Level: intermediate
5368b3603a34SBarry Smith 
53690b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
5370b3603a34SBarry Smith 
5371b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
5372b3603a34SBarry Smith 
5373b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5374b3603a34SBarry Smith @*/
53750910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
5376b3603a34SBarry Smith {
5377b3603a34SBarry Smith   PetscErrorCode    ierr;
53780b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
5379b3603a34SBarry Smith   const PetscScalar *yy;
538058ff32f7SBarry Smith   PetscInt          dim;
5381b3603a34SBarry Smith 
5382b3603a34SBarry Smith   PetscFunctionBegin;
538358ff32f7SBarry Smith   if (!step) {
5384a9f9c1f6SBarry Smith     PetscDrawAxis axis;
5385a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5386a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
53870910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
53880b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
53890b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
539058ff32f7SBarry Smith   }
53910910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
5392e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
5393e3efe391SJed Brown   {
5394e3efe391SJed Brown     PetscReal *yreal;
5395e3efe391SJed Brown     PetscInt  i,n;
53960910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
5397785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
5398e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
53990b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
5400e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
5401e3efe391SJed Brown   }
5402e3efe391SJed Brown #else
54030b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
5404e3efe391SJed Brown #endif
54050910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
5406b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
54070b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
54083923b477SBarry Smith   }
5409b3603a34SBarry Smith   PetscFunctionReturn(0);
5410b3603a34SBarry Smith }
5411b3603a34SBarry Smith 
5412b3603a34SBarry Smith #undef __FUNCT__
54134f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
5414ef20d060SBarry Smith /*@C
54154f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
5416ef20d060SBarry Smith        in a time based line graph
5417ef20d060SBarry Smith 
5418ef20d060SBarry Smith    Collective on TS
5419ef20d060SBarry Smith 
5420ef20d060SBarry Smith    Input Parameters:
5421ef20d060SBarry Smith +  ts - the TS context
5422ef20d060SBarry Smith .  step - current time-step
5423ef20d060SBarry Smith .  ptime - current time
5424ef20d060SBarry Smith -  lg - a line graph object
5425ef20d060SBarry Smith 
5426ef20d060SBarry Smith    Level: intermediate
5427ef20d060SBarry Smith 
5428abd5a294SJed Brown    Notes:
5429abd5a294SJed Brown    Only for sequential solves.
5430abd5a294SJed Brown 
5431abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
5432abd5a294SJed Brown 
5433abd5a294SJed Brown    Options Database Keys:
54344f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
5435ef20d060SBarry Smith 
5436ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
5437ef20d060SBarry Smith 
5438abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
5439ef20d060SBarry Smith @*/
54400910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
5441ef20d060SBarry Smith {
5442ef20d060SBarry Smith   PetscErrorCode    ierr;
54430b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
5444ef20d060SBarry Smith   const PetscScalar *yy;
5445ef20d060SBarry Smith   Vec               y;
5446a9f9c1f6SBarry Smith   PetscInt          dim;
5447ef20d060SBarry Smith 
5448ef20d060SBarry Smith   PetscFunctionBegin;
5449a9f9c1f6SBarry Smith   if (!step) {
5450a9f9c1f6SBarry Smith     PetscDrawAxis axis;
5451a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
54521ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
54530910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
5454a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
5455a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5456a9f9c1f6SBarry Smith   }
54570910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
5458ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
54590910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
5460ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
5461e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
5462e3efe391SJed Brown   {
5463e3efe391SJed Brown     PetscReal *yreal;
5464e3efe391SJed Brown     PetscInt  i,n;
5465e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
5466785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
5467e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
54680b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
5469e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
5470e3efe391SJed Brown   }
5471e3efe391SJed Brown #else
54720b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
5473e3efe391SJed Brown #endif
5474ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
5475ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
5476b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
54770b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
54783923b477SBarry Smith   }
5479ef20d060SBarry Smith   PetscFunctionReturn(0);
5480ef20d060SBarry Smith }
5481ef20d060SBarry Smith 
5482201da799SBarry Smith #undef __FUNCT__
5483201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
5484201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
5485201da799SBarry Smith {
5486201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
5487201da799SBarry Smith   PetscReal      x   = ptime,y;
5488201da799SBarry Smith   PetscErrorCode ierr;
5489201da799SBarry Smith   PetscInt       its;
5490201da799SBarry Smith 
5491201da799SBarry Smith   PetscFunctionBegin;
5492201da799SBarry Smith   if (!n) {
5493201da799SBarry Smith     PetscDrawAxis axis;
5494bbd56ea5SKarl Rupp 
5495201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5496201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
5497201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5498bbd56ea5SKarl Rupp 
5499201da799SBarry Smith     ctx->snes_its = 0;
5500201da799SBarry Smith   }
5501201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
5502201da799SBarry Smith   y    = its - ctx->snes_its;
5503201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
55043fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
5505201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
5506201da799SBarry Smith   }
5507201da799SBarry Smith   ctx->snes_its = its;
5508201da799SBarry Smith   PetscFunctionReturn(0);
5509201da799SBarry Smith }
5510201da799SBarry Smith 
5511201da799SBarry Smith #undef __FUNCT__
5512201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
5513201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
5514201da799SBarry Smith {
5515201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
5516201da799SBarry Smith   PetscReal      x   = ptime,y;
5517201da799SBarry Smith   PetscErrorCode ierr;
5518201da799SBarry Smith   PetscInt       its;
5519201da799SBarry Smith 
5520201da799SBarry Smith   PetscFunctionBegin;
5521201da799SBarry Smith   if (!n) {
5522201da799SBarry Smith     PetscDrawAxis axis;
5523bbd56ea5SKarl Rupp 
5524201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5525201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
5526201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5527bbd56ea5SKarl Rupp 
5528201da799SBarry Smith     ctx->ksp_its = 0;
5529201da799SBarry Smith   }
5530201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
5531201da799SBarry Smith   y    = its - ctx->ksp_its;
5532201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
553399fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
5534201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
5535201da799SBarry Smith   }
5536201da799SBarry Smith   ctx->ksp_its = its;
5537201da799SBarry Smith   PetscFunctionReturn(0);
5538201da799SBarry Smith }
5539f9c1d6abSBarry Smith 
5540f9c1d6abSBarry Smith #undef __FUNCT__
5541f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
5542f9c1d6abSBarry Smith /*@
5543f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
5544f9c1d6abSBarry Smith 
5545f9c1d6abSBarry Smith    Collective on TS and Vec
5546f9c1d6abSBarry Smith 
5547f9c1d6abSBarry Smith    Input Parameters:
5548f9c1d6abSBarry Smith +  ts - the TS context
5549f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
5550f9c1d6abSBarry Smith 
5551f9c1d6abSBarry Smith    Output Parameters:
5552f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
5553f9c1d6abSBarry Smith 
5554f9c1d6abSBarry Smith    Level: developer
5555f9c1d6abSBarry Smith 
5556f9c1d6abSBarry Smith .keywords: TS, compute
5557f9c1d6abSBarry Smith 
5558f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
5559f9c1d6abSBarry Smith @*/
5560f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
5561f9c1d6abSBarry Smith {
5562f9c1d6abSBarry Smith   PetscErrorCode ierr;
5563f9c1d6abSBarry Smith 
5564f9c1d6abSBarry Smith   PetscFunctionBegin;
5565f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5566ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
5567f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
5568f9c1d6abSBarry Smith   PetscFunctionReturn(0);
5569f9c1d6abSBarry Smith }
557024655328SShri 
557124655328SShri #undef __FUNCT__
557224655328SShri #define __FUNCT__ "TSRollBack"
557324655328SShri /*@
557424655328SShri    TSRollBack - Rolls back one time step
557524655328SShri 
557624655328SShri    Collective on TS
557724655328SShri 
557824655328SShri    Input Parameter:
557924655328SShri .  ts - the TS context obtained from TSCreate()
558024655328SShri 
558124655328SShri    Level: advanced
558224655328SShri 
558324655328SShri .keywords: TS, timestep, rollback
558424655328SShri 
558524655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
558624655328SShri @*/
558724655328SShri PetscErrorCode  TSRollBack(TS ts)
558824655328SShri {
558924655328SShri   PetscErrorCode ierr;
559024655328SShri 
559124655328SShri   PetscFunctionBegin;
559224655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
559324655328SShri 
559424655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
559524655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
559624655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
559724655328SShri   ts->ptime = ts->ptime_prev;
559824655328SShri   PetscFunctionReturn(0);
559924655328SShri }
5600aeb4809dSShri Abhyankar 
5601ff22ae23SHong Zhang #undef __FUNCT__
5602ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages"
5603ff22ae23SHong Zhang /*@
5604ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
5605ff22ae23SHong Zhang 
5606ff22ae23SHong Zhang    Input Parameter:
5607ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
5608ff22ae23SHong Zhang 
5609ff22ae23SHong Zhang    Level: advanced
5610ff22ae23SHong Zhang 
5611ff22ae23SHong Zhang .keywords: TS, getstages
5612ff22ae23SHong Zhang 
5613ff22ae23SHong Zhang .seealso: TSCreate()
5614ff22ae23SHong Zhang @*/
5615ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns, Vec **Y)
5616ff22ae23SHong Zhang {
5617ff22ae23SHong Zhang   PetscErrorCode ierr;
5618ff22ae23SHong Zhang 
5619ff22ae23SHong Zhang   PetscFunctionBegin;
5620ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
5621ff22ae23SHong Zhang   PetscValidPointer(ns,2);
5622ff22ae23SHong Zhang 
5623ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
5624ff22ae23SHong Zhang   else {
5625ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
5626ff22ae23SHong Zhang   }
5627ff22ae23SHong Zhang   PetscFunctionReturn(0);
5628ff22ae23SHong Zhang }
5629ff22ae23SHong Zhang 
5630