xref: /petsc/src/ts/interface/ts.c (revision 847ff0e1be28e109545e442742b43f55efd2ccb9)
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
36d2daff3dSHong Zhang .  -ts_checkpoint - checkpoint for adjoint sensitivity analysis
37bdad233fSMatthew Knepley .  -ts_max_steps maxsteps - maximum number of time-steps to take
383bca7d26SBarry Smith .  -ts_final_time time - maximum time to compute to
39bdad233fSMatthew Knepley .  -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
46*847ff0e1SMatthew G. Knepley .  -ts_fd_color - Use finite differences with coloring to compute IJacobian
47bdad233fSMatthew Knepley .  -ts_monitor - print information at each timestep
48de06c3feSJed Brown .  -ts_monitor_lg_timestep - Monitor timestep size graphically
49de06c3feSJed Brown .  -ts_monitor_lg_solution - Monitor solution graphically
50de06c3feSJed Brown .  -ts_monitor_lg_error - Monitor error graphically
51de06c3feSJed Brown .  -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically
52de06c3feSJed Brown .  -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically
53de06c3feSJed Brown .  -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically
54de06c3feSJed Brown .  -ts_monitor_draw_solution - Monitor solution graphically
552d5ee99bSBarry Smith .  -ts_monitor_draw_solution_phase - Monitor solution graphically with phase diagram
56de06c3feSJed Brown .  -ts_monitor_draw_error - Monitor error graphically
5791b97e58SBarry Smith .  -ts_monitor_solution_binary <filename> - Save each solution to a binary file
5891b97e58SBarry Smith -  -ts_monitor_solution_vtk <filename.vts> - Save each time step to a binary file, use filename-%%03D.vts
5991b97e58SBarry Smith 
6091b97e58SBarry Smith    Developer Note: We should unify all the -ts_monitor options in the way that -xxx_view has been unified
61bdad233fSMatthew Knepley 
62bdad233fSMatthew Knepley    Level: beginner
63bdad233fSMatthew Knepley 
64bdad233fSMatthew Knepley .keywords: TS, timestep, set, options, database
65bdad233fSMatthew Knepley 
66a313700dSBarry Smith .seealso: TSGetType()
67bdad233fSMatthew Knepley @*/
687087cfbeSBarry Smith PetscErrorCode  TSSetFromOptions(TS ts)
69bdad233fSMatthew Knepley {
70bc952696SBarry Smith   PetscBool              opt,flg,tflg;
71dfbe8321SBarry Smith   PetscErrorCode         ierr;
72649052a6SBarry Smith   PetscViewer            monviewer;
73eabae89aSBarry Smith   char                   monfilename[PETSC_MAX_PATH_LEN];
74089b2837SJed Brown   SNES                   snes;
751c3436cfSJed Brown   TSAdapt                adapt;
7631748224SBarry Smith   PetscReal              time_step;
7749354f04SShri Abhyankar   TSExactFinalTimeOption eftopt;
78d1212d36SBarry Smith   char                   dir[16];
796991f827SBarry Smith   const char             *defaultType;
806991f827SBarry Smith   char                   typeName[256];
81bdad233fSMatthew Knepley 
82bdad233fSMatthew Knepley   PetscFunctionBegin;
830700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
843194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)ts);CHKERRQ(ierr);
856991f827SBarry Smith   if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name;
866991f827SBarry Smith   else defaultType = TSEULER;
876991f827SBarry Smith 
886991f827SBarry Smith   ierr = TSRegisterAll();CHKERRQ(ierr);
896991f827SBarry Smith   ierr = PetscOptionsFList("-ts_type", "TS method"," TSSetType", TSList, defaultType, typeName, 256, &opt);CHKERRQ(ierr);
906991f827SBarry Smith   if (opt) {
916991f827SBarry Smith     ierr = TSSetType(ts, typeName);CHKERRQ(ierr);
926991f827SBarry Smith   } else {
936991f827SBarry Smith     ierr = TSSetType(ts, defaultType);CHKERRQ(ierr);
946991f827SBarry Smith   }
95bdad233fSMatthew Knepley 
96bdad233fSMatthew Knepley   /* Handle generic TS options */
97844594baSBarry Smith   if (ts->trajectory) tflg = PETSC_TRUE;
98844594baSBarry Smith   else tflg = PETSC_FALSE;
99bc952696SBarry Smith   ierr = PetscOptionsBool("-ts_save_trajectories","Checkpoint for adjoint sensitivity analysis","TSSetSaveTrajectories",tflg,&tflg,NULL);CHKERRQ(ierr);
100bc952696SBarry Smith   if (tflg) {ierr = TSSetSaveTrajectory(ts);CHKERRQ(ierr);}
1010298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,NULL);CHKERRQ(ierr);
1020298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_final_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,NULL);CHKERRQ(ierr);
1030298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_init_time","Initial time","TSSetTime",ts->ptime,&ts->ptime,NULL);CHKERRQ(ierr);
10431748224SBarry Smith   ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetTimeStep",ts->time_step,&time_step,&flg);CHKERRQ(ierr);
10531748224SBarry Smith   if (flg) {
10631748224SBarry Smith     ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
10731748224SBarry Smith   }
10849354f04SShri 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);
10949354f04SShri Abhyankar   if (flg) {ierr = TSSetExactFinalTime(ts,eftopt);CHKERRQ(ierr);}
1100298fd71SBarry 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);
1110298fd71SBarry Smith   ierr = PetscOptionsInt("-ts_max_reject","Maximum number of step rejections before step fails","TSSetMaxStepRejections",ts->max_reject,&ts->max_reject,NULL);CHKERRQ(ierr);
1120298fd71SBarry Smith   ierr = PetscOptionsBool("-ts_error_if_step_fails","Error if no step succeeds","TSSetErrorIfStepFails",ts->errorifstepfailed,&ts->errorifstepfailed,NULL);CHKERRQ(ierr);
1130298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_rtol","Relative tolerance for local truncation error","TSSetTolerances",ts->rtol,&ts->rtol,NULL);CHKERRQ(ierr);
1140298fd71SBarry Smith   ierr = PetscOptionsReal("-ts_atol","Absolute tolerance for local truncation error","TSSetTolerances",ts->atol,&ts->atol,NULL);CHKERRQ(ierr);
115bdad233fSMatthew Knepley 
11656f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS)
11756f85f32SBarry Smith   {
11856f85f32SBarry Smith   PetscBool set;
11956f85f32SBarry Smith   flg  = PETSC_FALSE;
12056f85f32SBarry Smith   ierr = PetscOptionsBool("-ts_saws_block","Block for SAWs memory snooper at end of TSSolve","PetscObjectSAWsBlock",((PetscObject)ts)->amspublishblock,&flg,&set);CHKERRQ(ierr);
12156f85f32SBarry Smith   if (set) {
12256f85f32SBarry Smith     ierr = PetscObjectSAWsSetBlock((PetscObject)ts,flg);CHKERRQ(ierr);
12356f85f32SBarry Smith   }
12456f85f32SBarry Smith   }
12556f85f32SBarry Smith #endif
12656f85f32SBarry Smith 
127bdad233fSMatthew Knepley   /* Monitor options */
128a6570f20SBarry Smith   ierr = PetscOptionsString("-ts_monitor","Monitor timestep size","TSMonitorDefault","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
129eabae89aSBarry Smith   if (flg) {
130ce94432eSBarry Smith     ierr = PetscViewerASCIIOpen(PetscObjectComm((PetscObject)ts),monfilename,&monviewer);CHKERRQ(ierr);
131649052a6SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
132bdad233fSMatthew Knepley   }
1335180491cSLisandro Dalcin   ierr = PetscOptionsString("-ts_monitor_python","Use Python function","TSMonitorSet",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1345180491cSLisandro Dalcin   if (flg) {ierr = PetscPythonMonitorSet((PetscObject)ts,monfilename);CHKERRQ(ierr);}
1355180491cSLisandro Dalcin 
1364f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",&opt);CHKERRQ(ierr);
137a7cc72afSBarry Smith   if (opt) {
1380b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1393923b477SBarry Smith     PetscInt       howoften = 1;
140a80ad3e0SBarry Smith 
1410298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_timestep","Monitor timestep size graphically","TSMonitorLGTimeStep",howoften,&howoften,NULL);CHKERRQ(ierr);
142ce94432eSBarry Smith     ierr = TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
1434f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGTimeStep,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
144b3603a34SBarry Smith   }
1454f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",&opt);CHKERRQ(ierr);
146b3603a34SBarry Smith   if (opt) {
1470b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1483923b477SBarry Smith     PetscInt       howoften = 1;
149b3603a34SBarry Smith 
1500298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_solution","Monitor solution graphically","TSMonitorLGSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
15122d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1524f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
153bdad233fSMatthew Knepley   }
1544f09c107SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",&opt);CHKERRQ(ierr);
155ef20d060SBarry Smith   if (opt) {
1560b039ecaSBarry Smith     TSMonitorLGCtx ctx;
1573923b477SBarry Smith     PetscInt       howoften = 1;
158ef20d060SBarry Smith 
1590298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_error","Monitor error graphically","TSMonitorLGError",howoften,&howoften,NULL);CHKERRQ(ierr);
16022d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1614f09c107SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGError,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
162ef20d060SBarry Smith   }
163201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",&opt);CHKERRQ(ierr);
164201da799SBarry Smith   if (opt) {
165201da799SBarry Smith     TSMonitorLGCtx ctx;
166201da799SBarry Smith     PetscInt       howoften = 1;
167201da799SBarry Smith 
1680298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_snes_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGSNESIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
16922d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
170201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGSNESIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
171201da799SBarry Smith   }
172201da799SBarry Smith   ierr = PetscOptionsName("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",&opt);CHKERRQ(ierr);
173201da799SBarry Smith   if (opt) {
174201da799SBarry Smith     TSMonitorLGCtx ctx;
175201da799SBarry Smith     PetscInt       howoften = 1;
176201da799SBarry Smith 
1770298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_lg_ksp_iterations","Monitor number nonlinear iterations for each timestep graphically","TSMonitorLGKSPIterations",howoften,&howoften,NULL);CHKERRQ(ierr);
17822d28d08SBarry Smith     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);CHKERRQ(ierr);
179201da799SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorLGKSPIterations,ctx,(PetscErrorCode (*)(void**))TSMonitorLGCtxDestroy);CHKERRQ(ierr);
180201da799SBarry Smith   }
1818189c53fSBarry Smith   ierr = PetscOptionsName("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",&opt);CHKERRQ(ierr);
1828189c53fSBarry Smith   if (opt) {
1838189c53fSBarry Smith     TSMonitorSPEigCtx ctx;
1848189c53fSBarry Smith     PetscInt          howoften = 1;
1858189c53fSBarry Smith 
1860298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_sp_eig","Monitor eigenvalues of linearized operator graphically","TSMonitorSPEig",howoften,&howoften,NULL);CHKERRQ(ierr);
18722d28d08SBarry Smith     ierr = TSMonitorSPEigCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
1888189c53fSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorSPEig,ctx,(PetscErrorCode (*)(void**))TSMonitorSPEigCtxDestroy);CHKERRQ(ierr);
1898189c53fSBarry Smith   }
190ef20d060SBarry Smith   opt  = PETSC_FALSE;
1910dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",&opt);CHKERRQ(ierr);
192a7cc72afSBarry Smith   if (opt) {
19383a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
19483a4ac43SBarry Smith     PetscInt         howoften = 1;
195a80ad3e0SBarry Smith 
1960298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_solution","Monitor solution graphically","TSMonitorDrawSolution",howoften,&howoften,NULL);CHKERRQ(ierr);
197ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
19883a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolution,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
199bdad233fSMatthew Knepley   }
200fb1732b5SBarry Smith   opt  = PETSC_FALSE;
2012d5ee99bSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",&opt);CHKERRQ(ierr);
2022d5ee99bSBarry Smith   if (opt) {
2032d5ee99bSBarry Smith     TSMonitorDrawCtx ctx;
2042d5ee99bSBarry Smith     PetscReal        bounds[4];
2052d5ee99bSBarry Smith     PetscInt         n = 4;
2062d5ee99bSBarry Smith     PetscDraw        draw;
2072d5ee99bSBarry Smith 
2082d5ee99bSBarry Smith     ierr = PetscOptionsRealArray("-ts_monitor_draw_solution_phase","Monitor solution graphically","TSMonitorDrawSolutionPhase",bounds,&n,NULL);CHKERRQ(ierr);
2092d5ee99bSBarry Smith     if (n != 4) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Must provide bounding box of phase field");
2102d5ee99bSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,1,&ctx);CHKERRQ(ierr);
2112d5ee99bSBarry Smith     ierr = PetscViewerDrawGetDraw(ctx->viewer,0,&draw);CHKERRQ(ierr);
2122d5ee99bSBarry Smith     ierr = PetscDrawClear(draw);CHKERRQ(ierr);
2134b363babSBarry Smith     ierr = PetscDrawAxisCreate(draw,&ctx->axis);CHKERRQ(ierr);
2144b363babSBarry Smith     ierr = PetscDrawAxisSetLimits(ctx->axis,bounds[0],bounds[2],bounds[1],bounds[3]);CHKERRQ(ierr);
215f54adfc0SBarry Smith     ierr = PetscDrawAxisSetLabels(ctx->axis,"Phase Diagram","Variable 1","Variable 2");CHKERRQ(ierr);
2164b363babSBarry Smith     ierr = PetscDrawAxisDraw(ctx->axis);CHKERRQ(ierr);
21707995780SPeter Brune     /* ierr = PetscDrawSetCoordinates(draw,bounds[0],bounds[1],bounds[2],bounds[3]);CHKERRQ(ierr); */
2182d5ee99bSBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawSolutionPhase,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2192d5ee99bSBarry Smith   }
2202d5ee99bSBarry Smith   opt  = PETSC_FALSE;
2210dcf80beSBarry Smith   ierr = PetscOptionsName("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",&opt);CHKERRQ(ierr);
2223a471f94SBarry Smith   if (opt) {
22383a4ac43SBarry Smith     TSMonitorDrawCtx ctx;
22483a4ac43SBarry Smith     PetscInt         howoften = 1;
2253a471f94SBarry Smith 
2260298fd71SBarry Smith     ierr = PetscOptionsInt("-ts_monitor_draw_error","Monitor error graphically","TSMonitorDrawError",howoften,&howoften,NULL);CHKERRQ(ierr);
227ce94432eSBarry Smith     ierr = TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&ctx);CHKERRQ(ierr);
22883a4ac43SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDrawError,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);CHKERRQ(ierr);
2293a471f94SBarry Smith   }
2303a471f94SBarry Smith   opt  = PETSC_FALSE;
23191b97e58SBarry Smith   ierr = PetscOptionsString("-ts_monitor_solution_binary","Save each solution to a binary file","TSMonitorSolutionBinary",0,monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
232fb1732b5SBarry Smith   if (flg) {
233fb1732b5SBarry Smith     PetscViewer ctx;
234fb1732b5SBarry Smith     if (monfilename[0]) {
235ce94432eSBarry Smith       ierr = PetscViewerBinaryOpen(PetscObjectComm((PetscObject)ts),monfilename,FILE_MODE_WRITE,&ctx);CHKERRQ(ierr);
236c2fbc07fSBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
237fb1732b5SBarry Smith     } else {
238ce94432eSBarry Smith       ctx = PETSC_VIEWER_BINARY_(PetscObjectComm((PetscObject)ts));
2390298fd71SBarry Smith       ierr = TSMonitorSet(ts,TSMonitorSolutionBinary,ctx,(PetscErrorCode (*)(void**))NULL);CHKERRQ(ierr);
240fb1732b5SBarry Smith     }
241fb1732b5SBarry Smith   }
242ed81e22dSJed Brown   opt  = PETSC_FALSE;
24391b97e58SBarry 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);
244ed81e22dSJed Brown   if (flg) {
245ed81e22dSJed Brown     const char *ptr,*ptr2;
246ed81e22dSJed Brown     char       *filetemplate;
247ce94432eSBarry Smith     if (!monfilename[0]) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
248ed81e22dSJed Brown     /* Do some cursory validation of the input. */
249ed81e22dSJed Brown     ierr = PetscStrstr(monfilename,"%",(char**)&ptr);CHKERRQ(ierr);
250ce94432eSBarry Smith     if (!ptr) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"-ts_monitor_solution_vtk requires a file template, e.g. filename-%%03D.vts");
251ed81e22dSJed Brown     for (ptr++; ptr && *ptr; ptr++) {
252ed81e22dSJed Brown       ierr = PetscStrchr("DdiouxX",*ptr,(char**)&ptr2);CHKERRQ(ierr);
253ce94432eSBarry 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");
254ed81e22dSJed Brown       if (ptr2) break;
255ed81e22dSJed Brown     }
256ed81e22dSJed Brown     ierr = PetscStrallocpy(monfilename,&filetemplate);CHKERRQ(ierr);
257ed81e22dSJed Brown     ierr = TSMonitorSet(ts,TSMonitorSolutionVTK,filetemplate,(PetscErrorCode (*)(void**))TSMonitorSolutionVTKDestroy);CHKERRQ(ierr);
258ed81e22dSJed Brown   }
259bdad233fSMatthew Knepley 
260d1212d36SBarry Smith   ierr = PetscOptionsString("-ts_monitor_dmda_ray","Display a ray of the solution","None","y=0",dir,16,&flg);CHKERRQ(ierr);
261d1212d36SBarry Smith   if (flg) {
262d1212d36SBarry Smith     TSMonitorDMDARayCtx *rayctx;
263d1212d36SBarry Smith     int                  ray = 0;
264d1212d36SBarry Smith     DMDADirection        ddir;
265d1212d36SBarry Smith     DM                   da;
266d1212d36SBarry Smith     PetscMPIInt          rank;
267d1212d36SBarry Smith 
268ce94432eSBarry Smith     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
269d1212d36SBarry Smith     if (dir[0] == 'x') ddir = DMDA_X;
270d1212d36SBarry Smith     else if (dir[0] == 'y') ddir = DMDA_Y;
271ce94432eSBarry Smith     else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Unknown ray %s",dir);
272d1212d36SBarry Smith     sscanf(dir+2,"%d",&ray);
273d1212d36SBarry Smith 
274d1212d36SBarry Smith     ierr = PetscInfo2(((PetscObject)ts),"Displaying DMDA ray %c = %D\n",dir[0],ray);CHKERRQ(ierr);
275b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
276d1212d36SBarry Smith     ierr = TSGetDM(ts,&da);CHKERRQ(ierr);
277d1212d36SBarry Smith     ierr = DMDAGetRay(da,ddir,ray,&rayctx->ray,&rayctx->scatter);CHKERRQ(ierr);
278ce94432eSBarry Smith     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ts),&rank);CHKERRQ(ierr);
279d1212d36SBarry Smith     if (!rank) {
280d1212d36SBarry Smith       ierr = PetscViewerDrawOpen(PETSC_COMM_SELF,0,0,0,0,600,300,&rayctx->viewer);CHKERRQ(ierr);
281d1212d36SBarry Smith     }
28251b4a12fSMatthew G. Knepley     rayctx->lgctx = NULL;
283d1212d36SBarry Smith     ierr = TSMonitorSet(ts,TSMonitorDMDARay,rayctx,TSMonitorDMDARayDestroy);CHKERRQ(ierr);
284d1212d36SBarry Smith   }
28551b4a12fSMatthew G. Knepley   ierr = PetscOptionsString("-ts_monitor_lg_dmda_ray","Display a ray of the solution","None","x=0",dir,16,&flg);CHKERRQ(ierr);
28651b4a12fSMatthew G. Knepley   if (flg) {
28751b4a12fSMatthew G. Knepley     TSMonitorDMDARayCtx *rayctx;
28851b4a12fSMatthew G. Knepley     int                 ray = 0;
28951b4a12fSMatthew G. Knepley     DMDADirection       ddir;
29051b4a12fSMatthew G. Knepley     DM                  da;
29151b4a12fSMatthew G. Knepley     PetscInt            howoften = 1;
292d1212d36SBarry Smith 
29351b4a12fSMatthew G. Knepley     if (dir[1] != '=') SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir);
29451b4a12fSMatthew G. Knepley     if      (dir[0] == 'x') ddir = DMDA_X;
29551b4a12fSMatthew G. Knepley     else if (dir[0] == 'y') ddir = DMDA_Y;
29651b4a12fSMatthew G. Knepley     else SETERRQ1(PetscObjectComm((PetscObject) ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir);
29751b4a12fSMatthew G. Knepley     sscanf(dir+2, "%d", &ray);
29851b4a12fSMatthew G. Knepley 
29951b4a12fSMatthew G. Knepley     ierr = PetscInfo2(((PetscObject) ts),"Displaying LG DMDA ray %c = %D\n", dir[0], ray);CHKERRQ(ierr);
300b00a9115SJed Brown     ierr = PetscNew(&rayctx);CHKERRQ(ierr);
30151b4a12fSMatthew G. Knepley     ierr = TSGetDM(ts, &da);CHKERRQ(ierr);
30251b4a12fSMatthew G. Knepley     ierr = DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter);CHKERRQ(ierr);
30351b4a12fSMatthew G. Knepley     ierr = TSMonitorLGCtxCreate(PETSC_COMM_SELF,0,0,PETSC_DECIDE,PETSC_DECIDE,600,400,howoften,&rayctx->lgctx);CHKERRQ(ierr);
30451b4a12fSMatthew G. Knepley     ierr = TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy);CHKERRQ(ierr);
30551b4a12fSMatthew G. Knepley   }
306a7a1495cSBarry Smith 
307*847ff0e1SMatthew G. Knepley   flg  = PETSC_FALSE;
308*847ff0e1SMatthew G. Knepley   ierr = PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeJacobianDefaultColor", flg, &flg, NULL);CHKERRQ(ierr);
309*847ff0e1SMatthew G. Knepley   if (flg) {
310*847ff0e1SMatthew G. Knepley     DM   dm;
311*847ff0e1SMatthew G. Knepley     DMTS tdm;
312*847ff0e1SMatthew G. Knepley 
313*847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
314*847ff0e1SMatthew G. Knepley     ierr = DMGetDMTS(dm, &tdm);CHKERRQ(ierr);
315*847ff0e1SMatthew G. Knepley     tdm->ijacobianctx = NULL;
316*847ff0e1SMatthew G. Knepley     ierr = TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, 0);CHKERRQ(ierr);
317*847ff0e1SMatthew G. Knepley     ierr = PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n");CHKERRQ(ierr);
318*847ff0e1SMatthew G. Knepley   }
319*847ff0e1SMatthew G. Knepley 
3207781c08eSBarry Smith   /*
3217781c08eSBarry Smith      This code is all wrong. One is creating objects inside the TSSetFromOptions() so if run with the options gui
3227781c08eSBarry Smith      will bleed memory. Also one is using a PetscOptionsBegin() inside a PetscOptionsBegin()
3237781c08eSBarry Smith   */
324552698daSJed Brown   ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
325e55864a3SBarry Smith   ierr = TSAdaptSetFromOptions(PetscOptionsObject,adapt);CHKERRQ(ierr);
326d763cef2SBarry Smith 
3276991f827SBarry Smith     /* Handle specific TS options */
3286991f827SBarry Smith   if (ts->ops->setfromoptions) {
3296991f827SBarry Smith     ierr = (*ts->ops->setfromoptions)(PetscOptionsObject,ts);CHKERRQ(ierr);
3306991f827SBarry Smith   }
3316991f827SBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
3326991f827SBarry Smith 
3336991f827SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
3346991f827SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)ts);CHKERRQ(ierr);
335d763cef2SBarry Smith 
336bc952696SBarry Smith   if (ts->trajectory) {
337bc952696SBarry Smith     ierr = TSTrajectorySetFromOptions(ts->trajectory);CHKERRQ(ierr);
338bc952696SBarry Smith   }
339bc952696SBarry Smith 
3406991f827SBarry Smith   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3416991f827SBarry Smith   if (snes) {
3426991f827SBarry Smith     if (ts->problem_type == TS_LINEAR) {ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);}
3436991f827SBarry Smith     ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
344d763cef2SBarry Smith   }
345d763cef2SBarry Smith   PetscFunctionReturn(0);
346d763cef2SBarry Smith }
347d763cef2SBarry Smith 
348d763cef2SBarry Smith #undef __FUNCT__
349bc952696SBarry Smith #define __FUNCT__ "TSSetSaveTrajectory"
350d2daff3dSHong Zhang /*@
351bc952696SBarry Smith    TSSetSaveTrajectory - Causes the TS to save its solutions as it iterates forward in time in a TSTrajectory object
352d2daff3dSHong Zhang 
353d2daff3dSHong Zhang    Collective on TS
354d2daff3dSHong Zhang 
355d2daff3dSHong Zhang    Input Parameters:
356bc952696SBarry Smith .  ts - the TS context obtained from TSCreate()
357bc952696SBarry Smith 
358d2daff3dSHong Zhang 
359d2daff3dSHong Zhang    Level: intermediate
360d2daff3dSHong Zhang 
361bc952696SBarry Smith .seealso: TSGetTrajectory(), TSAdjointSolve()
362d2daff3dSHong Zhang 
363bc952696SBarry Smith .keywords: TS, set, checkpoint,
364d2daff3dSHong Zhang @*/
365bc952696SBarry Smith PetscErrorCode  TSSetSaveTrajectory(TS ts)
366d2daff3dSHong Zhang {
367bc952696SBarry Smith   PetscErrorCode ierr;
368bc952696SBarry Smith 
369d2daff3dSHong Zhang   PetscFunctionBegin;
370d2daff3dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
371bc952696SBarry Smith   if (!ts->trajectory) {
372bc952696SBarry Smith     ierr = TSTrajectoryCreate(PetscObjectComm((PetscObject)ts),&ts->trajectory);CHKERRQ(ierr);
373bc952696SBarry Smith     /* should it set a default trajectory? */
374bc952696SBarry Smith   }
375d2daff3dSHong Zhang   PetscFunctionReturn(0);
376d2daff3dSHong Zhang }
377d2daff3dSHong Zhang 
378a7a1495cSBarry Smith #undef __FUNCT__
379a7a1495cSBarry Smith #define __FUNCT__ "TSComputeRHSJacobian"
380a7a1495cSBarry Smith /*@
381a7a1495cSBarry Smith    TSComputeRHSJacobian - Computes the Jacobian matrix that has been
382a7a1495cSBarry Smith       set with TSSetRHSJacobian().
383a7a1495cSBarry Smith 
384a7a1495cSBarry Smith    Collective on TS and Vec
385a7a1495cSBarry Smith 
386a7a1495cSBarry Smith    Input Parameters:
387316643e7SJed Brown +  ts - the TS context
388a7a1495cSBarry Smith .  t - current timestep
3890910c330SBarry Smith -  U - input vector
390a7a1495cSBarry Smith 
391a7a1495cSBarry Smith    Output Parameters:
392a7a1495cSBarry Smith +  A - Jacobian matrix
393a7a1495cSBarry Smith .  B - optional preconditioning matrix
394a7a1495cSBarry Smith -  flag - flag indicating matrix structure
395a7a1495cSBarry Smith 
396a7a1495cSBarry Smith    Notes:
397a7a1495cSBarry Smith    Most users should not need to explicitly call this routine, as it
398a7a1495cSBarry Smith    is used internally within the nonlinear solvers.
399a7a1495cSBarry Smith 
40094b7f48cSBarry Smith    See KSPSetOperators() for important information about setting the
401a7a1495cSBarry Smith    flag parameter.
402a7a1495cSBarry Smith 
403a7a1495cSBarry Smith    Level: developer
404a7a1495cSBarry Smith 
405a7a1495cSBarry Smith .keywords: SNES, compute, Jacobian, matrix
406a7a1495cSBarry Smith 
40794b7f48cSBarry Smith .seealso:  TSSetRHSJacobian(), KSPSetOperators()
408a7a1495cSBarry Smith @*/
409d1e9a80fSBarry Smith PetscErrorCode  TSComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat A,Mat B)
410a7a1495cSBarry Smith {
411dfbe8321SBarry Smith   PetscErrorCode ierr;
412270bf2e7SJed Brown   PetscObjectState Ustate;
41324989b8cSPeter Brune   DM             dm;
414942e3340SBarry Smith   DMTS           tsdm;
41524989b8cSPeter Brune   TSRHSJacobian  rhsjacobianfunc;
41624989b8cSPeter Brune   void           *ctx;
41724989b8cSPeter Brune   TSIJacobian    ijacobianfunc;
418a7a1495cSBarry Smith 
419a7a1495cSBarry Smith   PetscFunctionBegin;
4200700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4210910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
4220910c330SBarry Smith   PetscCheckSameComm(ts,1,U,3);
42324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
424942e3340SBarry Smith   ierr = DMGetDMTS(dm,&tsdm);CHKERRQ(ierr);
42524989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,&rhsjacobianfunc,&ctx);CHKERRQ(ierr);
4260298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobianfunc,NULL);CHKERRQ(ierr);
42759e4f3c8SBarry Smith   ierr = PetscObjectStateGet((PetscObject)U,&Ustate);CHKERRQ(ierr);
4280910c330SBarry Smith   if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.X == U && ts->rhsjacobian.Xstate == Ustate))) {
4290e4ef248SJed Brown     PetscFunctionReturn(0);
430f8ede8e7SJed Brown   }
431d90be118SSean Farley 
432ce94432eSBarry Smith   if (!rhsjacobianfunc && !ijacobianfunc) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
433d90be118SSean Farley 
434e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
43594ab13aaSBarry Smith     ierr = MatShift(A,-ts->rhsjacobian.shift);CHKERRQ(ierr);
43694ab13aaSBarry Smith     ierr = MatScale(A,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
43794ab13aaSBarry Smith     if (A != B) {
43894ab13aaSBarry Smith       ierr = MatShift(B,-ts->rhsjacobian.shift);CHKERRQ(ierr);
43994ab13aaSBarry Smith       ierr = MatScale(B,1./ts->rhsjacobian.scale);CHKERRQ(ierr);
440e1244c69SJed Brown     }
441e1244c69SJed Brown     ts->rhsjacobian.shift = 0;
442e1244c69SJed Brown     ts->rhsjacobian.scale = 1.;
443e1244c69SJed Brown   }
444e1244c69SJed Brown 
44524989b8cSPeter Brune   if (rhsjacobianfunc) {
44694ab13aaSBarry Smith     ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
447a7a1495cSBarry Smith     PetscStackPush("TS user Jacobian function");
448d1e9a80fSBarry Smith     ierr = (*rhsjacobianfunc)(ts,t,U,A,B,ctx);CHKERRQ(ierr);
449a7a1495cSBarry Smith     PetscStackPop;
45094ab13aaSBarry Smith     ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
451a7a1495cSBarry Smith     /* make sure user returned a correct Jacobian and preconditioner */
45294ab13aaSBarry Smith     PetscValidHeaderSpecific(A,MAT_CLASSID,4);
45394ab13aaSBarry Smith     PetscValidHeaderSpecific(B,MAT_CLASSID,5);
454ef66eb69SBarry Smith   } else {
45594ab13aaSBarry Smith     ierr = MatZeroEntries(A);CHKERRQ(ierr);
45694ab13aaSBarry Smith     if (A != B) {ierr = MatZeroEntries(B);CHKERRQ(ierr);}
457ef66eb69SBarry Smith   }
4580e4ef248SJed Brown   ts->rhsjacobian.time       = t;
4590910c330SBarry Smith   ts->rhsjacobian.X          = U;
46059e4f3c8SBarry Smith   ierr                       = PetscObjectStateGet((PetscObject)U,&ts->rhsjacobian.Xstate);CHKERRQ(ierr);
461a7a1495cSBarry Smith   PetscFunctionReturn(0);
462a7a1495cSBarry Smith }
463a7a1495cSBarry Smith 
4644a2ae208SSatish Balay #undef __FUNCT__
4654a2ae208SSatish Balay #define __FUNCT__ "TSComputeRHSFunction"
466316643e7SJed Brown /*@
467d763cef2SBarry Smith    TSComputeRHSFunction - Evaluates the right-hand-side function.
468d763cef2SBarry Smith 
469316643e7SJed Brown    Collective on TS and Vec
470316643e7SJed Brown 
471316643e7SJed Brown    Input Parameters:
472316643e7SJed Brown +  ts - the TS context
473316643e7SJed Brown .  t - current time
4740910c330SBarry Smith -  U - state vector
475316643e7SJed Brown 
476316643e7SJed Brown    Output Parameter:
477316643e7SJed Brown .  y - right hand side
478316643e7SJed Brown 
479316643e7SJed Brown    Note:
480316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
481316643e7SJed Brown    is used internally within the nonlinear solvers.
482316643e7SJed Brown 
483316643e7SJed Brown    Level: developer
484316643e7SJed Brown 
485316643e7SJed Brown .keywords: TS, compute
486316643e7SJed Brown 
487316643e7SJed Brown .seealso: TSSetRHSFunction(), TSComputeIFunction()
488316643e7SJed Brown @*/
4890910c330SBarry Smith PetscErrorCode TSComputeRHSFunction(TS ts,PetscReal t,Vec U,Vec y)
490d763cef2SBarry Smith {
491dfbe8321SBarry Smith   PetscErrorCode ierr;
49224989b8cSPeter Brune   TSRHSFunction  rhsfunction;
49324989b8cSPeter Brune   TSIFunction    ifunction;
49424989b8cSPeter Brune   void           *ctx;
49524989b8cSPeter Brune   DM             dm;
49624989b8cSPeter Brune 
497d763cef2SBarry Smith   PetscFunctionBegin;
4980700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4990910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5000700a824SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,4);
50124989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
50224989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,&rhsfunction,&ctx);CHKERRQ(ierr);
5030298fd71SBarry Smith   ierr = DMTSGetIFunction(dm,&ifunction,NULL);CHKERRQ(ierr);
504d763cef2SBarry Smith 
505ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
506d763cef2SBarry Smith 
5070910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
50824989b8cSPeter Brune   if (rhsfunction) {
509d763cef2SBarry Smith     PetscStackPush("TS user right-hand-side function");
5100910c330SBarry Smith     ierr = (*rhsfunction)(ts,t,U,y,ctx);CHKERRQ(ierr);
511d763cef2SBarry Smith     PetscStackPop;
512214bc6a2SJed Brown   } else {
513214bc6a2SJed Brown     ierr = VecZeroEntries(y);CHKERRQ(ierr);
514b2cd27e8SJed Brown   }
51544a41b28SSean Farley 
5160910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,y,0);CHKERRQ(ierr);
517d763cef2SBarry Smith   PetscFunctionReturn(0);
518d763cef2SBarry Smith }
519d763cef2SBarry Smith 
5204a2ae208SSatish Balay #undef __FUNCT__
521ef20d060SBarry Smith #define __FUNCT__ "TSComputeSolutionFunction"
522ef20d060SBarry Smith /*@
523ef20d060SBarry Smith    TSComputeSolutionFunction - Evaluates the solution function.
524ef20d060SBarry Smith 
525ef20d060SBarry Smith    Collective on TS and Vec
526ef20d060SBarry Smith 
527ef20d060SBarry Smith    Input Parameters:
528ef20d060SBarry Smith +  ts - the TS context
529ef20d060SBarry Smith -  t - current time
530ef20d060SBarry Smith 
531ef20d060SBarry Smith    Output Parameter:
5320910c330SBarry Smith .  U - the solution
533ef20d060SBarry Smith 
534ef20d060SBarry Smith    Note:
535ef20d060SBarry Smith    Most users should not need to explicitly call this routine, as it
536ef20d060SBarry Smith    is used internally within the nonlinear solvers.
537ef20d060SBarry Smith 
538ef20d060SBarry Smith    Level: developer
539ef20d060SBarry Smith 
540ef20d060SBarry Smith .keywords: TS, compute
541ef20d060SBarry Smith 
542abd5a294SJed Brown .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
543ef20d060SBarry Smith @*/
5440910c330SBarry Smith PetscErrorCode TSComputeSolutionFunction(TS ts,PetscReal t,Vec U)
545ef20d060SBarry Smith {
546ef20d060SBarry Smith   PetscErrorCode     ierr;
547ef20d060SBarry Smith   TSSolutionFunction solutionfunction;
548ef20d060SBarry Smith   void               *ctx;
549ef20d060SBarry Smith   DM                 dm;
550ef20d060SBarry Smith 
551ef20d060SBarry Smith   PetscFunctionBegin;
552ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5530910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
554ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
555ef20d060SBarry Smith   ierr = DMTSGetSolutionFunction(dm,&solutionfunction,&ctx);CHKERRQ(ierr);
556ef20d060SBarry Smith 
557ef20d060SBarry Smith   if (solutionfunction) {
5589b7cd975SBarry Smith     PetscStackPush("TS user solution function");
5590910c330SBarry Smith     ierr = (*solutionfunction)(ts,t,U,ctx);CHKERRQ(ierr);
560ef20d060SBarry Smith     PetscStackPop;
561ef20d060SBarry Smith   }
562ef20d060SBarry Smith   PetscFunctionReturn(0);
563ef20d060SBarry Smith }
5649b7cd975SBarry Smith #undef __FUNCT__
5659b7cd975SBarry Smith #define __FUNCT__ "TSComputeForcingFunction"
5669b7cd975SBarry Smith /*@
5679b7cd975SBarry Smith    TSComputeForcingFunction - Evaluates the forcing function.
5689b7cd975SBarry Smith 
5699b7cd975SBarry Smith    Collective on TS and Vec
5709b7cd975SBarry Smith 
5719b7cd975SBarry Smith    Input Parameters:
5729b7cd975SBarry Smith +  ts - the TS context
5739b7cd975SBarry Smith -  t - current time
5749b7cd975SBarry Smith 
5759b7cd975SBarry Smith    Output Parameter:
5769b7cd975SBarry Smith .  U - the function value
5779b7cd975SBarry Smith 
5789b7cd975SBarry Smith    Note:
5799b7cd975SBarry Smith    Most users should not need to explicitly call this routine, as it
5809b7cd975SBarry Smith    is used internally within the nonlinear solvers.
5819b7cd975SBarry Smith 
5829b7cd975SBarry Smith    Level: developer
5839b7cd975SBarry Smith 
5849b7cd975SBarry Smith .keywords: TS, compute
5859b7cd975SBarry Smith 
5869b7cd975SBarry Smith .seealso: TSSetSolutionFunction(), TSSetRHSFunction(), TSComputeIFunction()
5879b7cd975SBarry Smith @*/
5889b7cd975SBarry Smith PetscErrorCode TSComputeForcingFunction(TS ts,PetscReal t,Vec U)
5899b7cd975SBarry Smith {
5909b7cd975SBarry Smith   PetscErrorCode     ierr, (*forcing)(TS,PetscReal,Vec,void*);
5919b7cd975SBarry Smith   void               *ctx;
5929b7cd975SBarry Smith   DM                 dm;
5939b7cd975SBarry Smith 
5949b7cd975SBarry Smith   PetscFunctionBegin;
5959b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5969b7cd975SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
5979b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
5989b7cd975SBarry Smith   ierr = DMTSGetForcingFunction(dm,&forcing,&ctx);CHKERRQ(ierr);
5999b7cd975SBarry Smith 
6009b7cd975SBarry Smith   if (forcing) {
6019b7cd975SBarry Smith     PetscStackPush("TS user forcing function");
6029b7cd975SBarry Smith     ierr = (*forcing)(ts,t,U,ctx);CHKERRQ(ierr);
6039b7cd975SBarry Smith     PetscStackPop;
6049b7cd975SBarry Smith   }
6059b7cd975SBarry Smith   PetscFunctionReturn(0);
6069b7cd975SBarry Smith }
607ef20d060SBarry Smith 
608ef20d060SBarry Smith #undef __FUNCT__
609214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSVec_Private"
610214bc6a2SJed Brown static PetscErrorCode TSGetRHSVec_Private(TS ts,Vec *Frhs)
611214bc6a2SJed Brown {
6122dd45cf8SJed Brown   Vec            F;
613214bc6a2SJed Brown   PetscErrorCode ierr;
614214bc6a2SJed Brown 
615214bc6a2SJed Brown   PetscFunctionBegin;
6160298fd71SBarry Smith   *Frhs = NULL;
6170298fd71SBarry Smith   ierr  = TSGetIFunction(ts,&F,NULL,NULL);CHKERRQ(ierr);
618214bc6a2SJed Brown   if (!ts->Frhs) {
6192dd45cf8SJed Brown     ierr = VecDuplicate(F,&ts->Frhs);CHKERRQ(ierr);
620214bc6a2SJed Brown   }
621214bc6a2SJed Brown   *Frhs = ts->Frhs;
622214bc6a2SJed Brown   PetscFunctionReturn(0);
623214bc6a2SJed Brown }
624214bc6a2SJed Brown 
625214bc6a2SJed Brown #undef __FUNCT__
626214bc6a2SJed Brown #define __FUNCT__ "TSGetRHSMats_Private"
627214bc6a2SJed Brown static PetscErrorCode TSGetRHSMats_Private(TS ts,Mat *Arhs,Mat *Brhs)
628214bc6a2SJed Brown {
629214bc6a2SJed Brown   Mat            A,B;
6302dd45cf8SJed Brown   PetscErrorCode ierr;
631214bc6a2SJed Brown 
632214bc6a2SJed Brown   PetscFunctionBegin;
633c0cd0301SJed Brown   if (Arhs) *Arhs = NULL;
634c0cd0301SJed Brown   if (Brhs) *Brhs = NULL;
6350298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
636214bc6a2SJed Brown   if (Arhs) {
637214bc6a2SJed Brown     if (!ts->Arhs) {
638214bc6a2SJed Brown       ierr = MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&ts->Arhs);CHKERRQ(ierr);
639214bc6a2SJed Brown     }
640214bc6a2SJed Brown     *Arhs = ts->Arhs;
641214bc6a2SJed Brown   }
642214bc6a2SJed Brown   if (Brhs) {
643214bc6a2SJed Brown     if (!ts->Brhs) {
644bdb70873SJed Brown       if (A != B) {
645214bc6a2SJed Brown         ierr = MatDuplicate(B,MAT_DO_NOT_COPY_VALUES,&ts->Brhs);CHKERRQ(ierr);
646bdb70873SJed Brown       } else {
647bdb70873SJed Brown         ts->Brhs = ts->Arhs;
648bdb70873SJed Brown         ierr = PetscObjectReference((PetscObject)ts->Arhs);CHKERRQ(ierr);
649bdb70873SJed Brown       }
650214bc6a2SJed Brown     }
651214bc6a2SJed Brown     *Brhs = ts->Brhs;
652214bc6a2SJed Brown   }
653214bc6a2SJed Brown   PetscFunctionReturn(0);
654214bc6a2SJed Brown }
655214bc6a2SJed Brown 
656214bc6a2SJed Brown #undef __FUNCT__
657316643e7SJed Brown #define __FUNCT__ "TSComputeIFunction"
658316643e7SJed Brown /*@
6590910c330SBarry Smith    TSComputeIFunction - Evaluates the DAE residual written in implicit form F(t,U,Udot)=0
660316643e7SJed Brown 
661316643e7SJed Brown    Collective on TS and Vec
662316643e7SJed Brown 
663316643e7SJed Brown    Input Parameters:
664316643e7SJed Brown +  ts - the TS context
665316643e7SJed Brown .  t - current time
6660910c330SBarry Smith .  U - state vector
6670910c330SBarry Smith .  Udot - time derivative of state vector
668214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSFunction should be kept separate
669316643e7SJed Brown 
670316643e7SJed Brown    Output Parameter:
671316643e7SJed Brown .  Y - right hand side
672316643e7SJed Brown 
673316643e7SJed Brown    Note:
674316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
675316643e7SJed Brown    is used internally within the nonlinear solvers.
676316643e7SJed Brown 
677316643e7SJed Brown    If the user did did not write their equations in implicit form, this
678316643e7SJed Brown    function recasts them in implicit form.
679316643e7SJed Brown 
680316643e7SJed Brown    Level: developer
681316643e7SJed Brown 
682316643e7SJed Brown .keywords: TS, compute
683316643e7SJed Brown 
684316643e7SJed Brown .seealso: TSSetIFunction(), TSComputeRHSFunction()
685316643e7SJed Brown @*/
6860910c330SBarry Smith PetscErrorCode TSComputeIFunction(TS ts,PetscReal t,Vec U,Vec Udot,Vec Y,PetscBool imex)
687316643e7SJed Brown {
688316643e7SJed Brown   PetscErrorCode ierr;
68924989b8cSPeter Brune   TSIFunction    ifunction;
69024989b8cSPeter Brune   TSRHSFunction  rhsfunction;
69124989b8cSPeter Brune   void           *ctx;
69224989b8cSPeter Brune   DM             dm;
693316643e7SJed Brown 
694316643e7SJed Brown   PetscFunctionBegin;
6950700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
6960910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
6970910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
6980700a824SBarry Smith   PetscValidHeaderSpecific(Y,VEC_CLASSID,5);
699316643e7SJed Brown 
70024989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
70124989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,&ifunction,&ctx);CHKERRQ(ierr);
7020298fd71SBarry Smith   ierr = DMTSGetRHSFunction(dm,&rhsfunction,NULL);CHKERRQ(ierr);
70324989b8cSPeter Brune 
704ce94432eSBarry Smith   if (!rhsfunction && !ifunction) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSFunction() and / or TSSetIFunction()");
705d90be118SSean Farley 
7060910c330SBarry Smith   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
70724989b8cSPeter Brune   if (ifunction) {
708316643e7SJed Brown     PetscStackPush("TS user implicit function");
7090910c330SBarry Smith     ierr = (*ifunction)(ts,t,U,Udot,Y,ctx);CHKERRQ(ierr);
710316643e7SJed Brown     PetscStackPop;
711214bc6a2SJed Brown   }
712214bc6a2SJed Brown   if (imex) {
71324989b8cSPeter Brune     if (!ifunction) {
7140910c330SBarry Smith       ierr = VecCopy(Udot,Y);CHKERRQ(ierr);
7152dd45cf8SJed Brown     }
71624989b8cSPeter Brune   } else if (rhsfunction) {
71724989b8cSPeter Brune     if (ifunction) {
718214bc6a2SJed Brown       Vec Frhs;
719214bc6a2SJed Brown       ierr = TSGetRHSVec_Private(ts,&Frhs);CHKERRQ(ierr);
7200910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Frhs);CHKERRQ(ierr);
721214bc6a2SJed Brown       ierr = VecAXPY(Y,-1,Frhs);CHKERRQ(ierr);
7222dd45cf8SJed Brown     } else {
7230910c330SBarry Smith       ierr = TSComputeRHSFunction(ts,t,U,Y);CHKERRQ(ierr);
7240910c330SBarry Smith       ierr = VecAYPX(Y,-1,Udot);CHKERRQ(ierr);
725316643e7SJed Brown     }
7264a6899ffSJed Brown   }
7270910c330SBarry Smith   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,Udot,Y);CHKERRQ(ierr);
728316643e7SJed Brown   PetscFunctionReturn(0);
729316643e7SJed Brown }
730316643e7SJed Brown 
731316643e7SJed Brown #undef __FUNCT__
732316643e7SJed Brown #define __FUNCT__ "TSComputeIJacobian"
733316643e7SJed Brown /*@
734316643e7SJed Brown    TSComputeIJacobian - Evaluates the Jacobian of the DAE
735316643e7SJed Brown 
736316643e7SJed Brown    Collective on TS and Vec
737316643e7SJed Brown 
738316643e7SJed Brown    Input
739316643e7SJed Brown       Input Parameters:
740316643e7SJed Brown +  ts - the TS context
741316643e7SJed Brown .  t - current timestep
7420910c330SBarry Smith .  U - state vector
7430910c330SBarry Smith .  Udot - time derivative of state vector
744214bc6a2SJed Brown .  shift - shift to apply, see note below
745214bc6a2SJed Brown -  imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate
746316643e7SJed Brown 
747316643e7SJed Brown    Output Parameters:
748316643e7SJed Brown +  A - Jacobian matrix
749316643e7SJed Brown .  B - optional preconditioning matrix
750316643e7SJed Brown -  flag - flag indicating matrix structure
751316643e7SJed Brown 
752316643e7SJed Brown    Notes:
7530910c330SBarry Smith    If F(t,U,Udot)=0 is the DAE, the required Jacobian is
754316643e7SJed Brown 
7550910c330SBarry Smith    dF/dU + shift*dF/dUdot
756316643e7SJed Brown 
757316643e7SJed Brown    Most users should not need to explicitly call this routine, as it
758316643e7SJed Brown    is used internally within the nonlinear solvers.
759316643e7SJed Brown 
760316643e7SJed Brown    Level: developer
761316643e7SJed Brown 
762316643e7SJed Brown .keywords: TS, compute, Jacobian, matrix
763316643e7SJed Brown 
764316643e7SJed Brown .seealso:  TSSetIJacobian()
76563495f91SJed Brown @*/
766d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobian(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,PetscBool imex)
767316643e7SJed Brown {
768316643e7SJed Brown   PetscErrorCode ierr;
76924989b8cSPeter Brune   TSIJacobian    ijacobian;
77024989b8cSPeter Brune   TSRHSJacobian  rhsjacobian;
77124989b8cSPeter Brune   DM             dm;
77224989b8cSPeter Brune   void           *ctx;
773316643e7SJed Brown 
774316643e7SJed Brown   PetscFunctionBegin;
7750700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
7760910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
7770910c330SBarry Smith   PetscValidHeaderSpecific(Udot,VEC_CLASSID,4);
778316643e7SJed Brown   PetscValidPointer(A,6);
77994ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,6);
780316643e7SJed Brown   PetscValidPointer(B,7);
78194ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,7);
78224989b8cSPeter Brune 
78324989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
78424989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,&ijacobian,&ctx);CHKERRQ(ierr);
7850298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjacobian,NULL);CHKERRQ(ierr);
78624989b8cSPeter Brune 
787ce94432eSBarry Smith   if (!rhsjacobian && !ijacobian) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetRHSJacobian() and / or TSSetIJacobian()");
788316643e7SJed Brown 
78994ab13aaSBarry Smith   ierr = PetscLogEventBegin(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
79024989b8cSPeter Brune   if (ijacobian) {
791316643e7SJed Brown     PetscStackPush("TS user implicit Jacobian");
792d1e9a80fSBarry Smith     ierr = (*ijacobian)(ts,t,U,Udot,shift,A,B,ctx);CHKERRQ(ierr);
793316643e7SJed Brown     PetscStackPop;
794214bc6a2SJed Brown     /* make sure user returned a correct Jacobian and preconditioner */
79594ab13aaSBarry Smith     PetscValidHeaderSpecific(A,MAT_CLASSID,4);
79694ab13aaSBarry Smith     PetscValidHeaderSpecific(B,MAT_CLASSID,5);
7974a6899ffSJed Brown   }
798214bc6a2SJed Brown   if (imex) {
799b5abc632SBarry Smith     if (!ijacobian) {  /* system was written as Udot = G(t,U) */
80094ab13aaSBarry Smith       ierr = MatZeroEntries(A);CHKERRQ(ierr);
80194ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
80294ab13aaSBarry Smith       if (A != B) {
80394ab13aaSBarry Smith         ierr = MatZeroEntries(B);CHKERRQ(ierr);
80494ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
805214bc6a2SJed Brown       }
806214bc6a2SJed Brown     }
807214bc6a2SJed Brown   } else {
808e1244c69SJed Brown     Mat Arhs = NULL,Brhs = NULL;
809e1244c69SJed Brown     if (rhsjacobian) {
810bd373395SBarry Smith       if (ijacobian) {
811e1244c69SJed Brown         ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
812bd373395SBarry Smith       } else {
813bd373395SBarry Smith         ierr = TSGetIJacobian(ts,&Arhs,&Brhs,NULL,NULL);CHKERRQ(ierr);
814e1244c69SJed Brown       }
815d1e9a80fSBarry Smith       ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
816e1244c69SJed Brown     }
81794ab13aaSBarry Smith     if (Arhs == A) {           /* No IJacobian, so we only have the RHS matrix */
818e1244c69SJed Brown       ts->rhsjacobian.scale = -1;
819e1244c69SJed Brown       ts->rhsjacobian.shift = shift;
82094ab13aaSBarry Smith       ierr = MatScale(A,-1);CHKERRQ(ierr);
82194ab13aaSBarry Smith       ierr = MatShift(A,shift);CHKERRQ(ierr);
82294ab13aaSBarry Smith       if (A != B) {
82394ab13aaSBarry Smith         ierr = MatScale(B,-1);CHKERRQ(ierr);
82494ab13aaSBarry Smith         ierr = MatShift(B,shift);CHKERRQ(ierr);
825316643e7SJed Brown       }
826e1244c69SJed Brown     } else if (Arhs) {          /* Both IJacobian and RHSJacobian */
827e1244c69SJed Brown       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
828e1244c69SJed Brown       if (!ijacobian) {         /* No IJacobian provided, but we have a separate RHS matrix */
82994ab13aaSBarry Smith         ierr = MatZeroEntries(A);CHKERRQ(ierr);
83094ab13aaSBarry Smith         ierr = MatShift(A,shift);CHKERRQ(ierr);
83194ab13aaSBarry Smith         if (A != B) {
83294ab13aaSBarry Smith           ierr = MatZeroEntries(B);CHKERRQ(ierr);
83394ab13aaSBarry Smith           ierr = MatShift(B,shift);CHKERRQ(ierr);
834e1244c69SJed Brown         }
835e1244c69SJed Brown       }
83694ab13aaSBarry Smith       ierr = MatAXPY(A,-1,Arhs,axpy);CHKERRQ(ierr);
83794ab13aaSBarry Smith       if (A != B) {
83894ab13aaSBarry Smith         ierr = MatAXPY(B,-1,Brhs,axpy);CHKERRQ(ierr);
839214bc6a2SJed Brown       }
840316643e7SJed Brown     }
841316643e7SJed Brown   }
84294ab13aaSBarry Smith   ierr = PetscLogEventEnd(TS_JacobianEval,ts,U,A,B);CHKERRQ(ierr);
843316643e7SJed Brown   PetscFunctionReturn(0);
844316643e7SJed Brown }
845316643e7SJed Brown 
846316643e7SJed Brown #undef __FUNCT__
8474a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSFunction"
848d763cef2SBarry Smith /*@C
849d763cef2SBarry Smith     TSSetRHSFunction - Sets the routine for evaluating the function,
850b5abc632SBarry Smith     where U_t = G(t,u).
851d763cef2SBarry Smith 
8523f9fe445SBarry Smith     Logically Collective on TS
853d763cef2SBarry Smith 
854d763cef2SBarry Smith     Input Parameters:
855d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
8560298fd71SBarry Smith .   r - vector to put the computed right hand side (or NULL to have it created)
857d763cef2SBarry Smith .   f - routine for evaluating the right-hand-side function
858d763cef2SBarry Smith -   ctx - [optional] user-defined context for private data for the
8590298fd71SBarry Smith           function evaluation routine (may be NULL)
860d763cef2SBarry Smith 
861d763cef2SBarry Smith     Calling sequence of func:
86287828ca2SBarry Smith $     func (TS ts,PetscReal t,Vec u,Vec F,void *ctx);
863d763cef2SBarry Smith 
864d763cef2SBarry Smith +   t - current timestep
865d763cef2SBarry Smith .   u - input vector
866d763cef2SBarry Smith .   F - function vector
867d763cef2SBarry Smith -   ctx - [optional] user-defined function context
868d763cef2SBarry Smith 
869d763cef2SBarry Smith     Level: beginner
870d763cef2SBarry Smith 
8712bbac0d3SBarry Smith     Notes: You must call this function or TSSetIFunction() to define your ODE. You cannot use this function when solving a DAE.
8722bbac0d3SBarry Smith 
873d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
874d763cef2SBarry Smith 
875ae8867d6SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSSetIFunction()
876d763cef2SBarry Smith @*/
877089b2837SJed Brown PetscErrorCode  TSSetRHSFunction(TS ts,Vec r,PetscErrorCode (*f)(TS,PetscReal,Vec,Vec,void*),void *ctx)
878d763cef2SBarry Smith {
879089b2837SJed Brown   PetscErrorCode ierr;
880089b2837SJed Brown   SNES           snes;
8810298fd71SBarry Smith   Vec            ralloc = NULL;
88224989b8cSPeter Brune   DM             dm;
883d763cef2SBarry Smith 
884089b2837SJed Brown   PetscFunctionBegin;
8850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
886ca94891dSJed Brown   if (r) PetscValidHeaderSpecific(r,VEC_CLASSID,2);
88724989b8cSPeter Brune 
88824989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
88924989b8cSPeter Brune   ierr = DMTSSetRHSFunction(dm,f,ctx);CHKERRQ(ierr);
890089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
891e856ceecSJed Brown   if (!r && !ts->dm && ts->vec_sol) {
892e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&ralloc);CHKERRQ(ierr);
893e856ceecSJed Brown     r    = ralloc;
894e856ceecSJed Brown   }
895089b2837SJed Brown   ierr = SNESSetFunction(snes,r,SNESTSFormFunction,ts);CHKERRQ(ierr);
896e856ceecSJed Brown   ierr = VecDestroy(&ralloc);CHKERRQ(ierr);
897d763cef2SBarry Smith   PetscFunctionReturn(0);
898d763cef2SBarry Smith }
899d763cef2SBarry Smith 
9004a2ae208SSatish Balay #undef __FUNCT__
901ef20d060SBarry Smith #define __FUNCT__ "TSSetSolutionFunction"
902ef20d060SBarry Smith /*@C
903abd5a294SJed Brown     TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE
904ef20d060SBarry Smith 
905ef20d060SBarry Smith     Logically Collective on TS
906ef20d060SBarry Smith 
907ef20d060SBarry Smith     Input Parameters:
908ef20d060SBarry Smith +   ts - the TS context obtained from TSCreate()
909ef20d060SBarry Smith .   f - routine for evaluating the solution
910ef20d060SBarry Smith -   ctx - [optional] user-defined context for private data for the
9110298fd71SBarry Smith           function evaluation routine (may be NULL)
912ef20d060SBarry Smith 
913ef20d060SBarry Smith     Calling sequence of func:
914ef20d060SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
915ef20d060SBarry Smith 
916ef20d060SBarry Smith +   t - current timestep
917ef20d060SBarry Smith .   u - output vector
918ef20d060SBarry Smith -   ctx - [optional] user-defined function context
919ef20d060SBarry Smith 
920abd5a294SJed Brown     Notes:
921abd5a294SJed Brown     This routine is used for testing accuracy of time integration schemes when you already know the solution.
922abd5a294SJed Brown     If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to
923abd5a294SJed Brown     create closed-form solutions with non-physical forcing terms.
924abd5a294SJed Brown 
9254f09c107SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
926abd5a294SJed Brown 
927ef20d060SBarry Smith     Level: beginner
928ef20d060SBarry Smith 
929ef20d060SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
930ef20d060SBarry Smith 
9319b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetForcingFunction()
932ef20d060SBarry Smith @*/
933ef20d060SBarry Smith PetscErrorCode  TSSetSolutionFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
934ef20d060SBarry Smith {
935ef20d060SBarry Smith   PetscErrorCode ierr;
936ef20d060SBarry Smith   DM             dm;
937ef20d060SBarry Smith 
938ef20d060SBarry Smith   PetscFunctionBegin;
939ef20d060SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
940ef20d060SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
941ef20d060SBarry Smith   ierr = DMTSSetSolutionFunction(dm,f,ctx);CHKERRQ(ierr);
942ef20d060SBarry Smith   PetscFunctionReturn(0);
943ef20d060SBarry Smith }
944ef20d060SBarry Smith 
945ef20d060SBarry Smith #undef __FUNCT__
9469b7cd975SBarry Smith #define __FUNCT__ "TSSetForcingFunction"
9479b7cd975SBarry Smith /*@C
9489b7cd975SBarry Smith     TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE
9499b7cd975SBarry Smith 
9509b7cd975SBarry Smith     Logically Collective on TS
9519b7cd975SBarry Smith 
9529b7cd975SBarry Smith     Input Parameters:
9539b7cd975SBarry Smith +   ts - the TS context obtained from TSCreate()
9549b7cd975SBarry Smith .   f - routine for evaluating the forcing function
9559b7cd975SBarry Smith -   ctx - [optional] user-defined context for private data for the
9560298fd71SBarry Smith           function evaluation routine (may be NULL)
9579b7cd975SBarry Smith 
9589b7cd975SBarry Smith     Calling sequence of func:
9599b7cd975SBarry Smith $     func (TS ts,PetscReal t,Vec u,void *ctx);
9609b7cd975SBarry Smith 
9619b7cd975SBarry Smith +   t - current timestep
9629b7cd975SBarry Smith .   u - output vector
9639b7cd975SBarry Smith -   ctx - [optional] user-defined function context
9649b7cd975SBarry Smith 
9659b7cd975SBarry Smith     Notes:
9669b7cd975SBarry Smith     This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to
9679b7cd975SBarry Smith     create closed-form solutions with a non-physical forcing term.
9689b7cd975SBarry Smith 
9699b7cd975SBarry Smith     For low-dimensional problems solved in serial, such as small discrete systems, TSMonitorLGError() can be used to monitor the error history.
9709b7cd975SBarry Smith 
9719b7cd975SBarry Smith     Level: beginner
9729b7cd975SBarry Smith 
9739b7cd975SBarry Smith .keywords: TS, timestep, set, right-hand-side, function
9749b7cd975SBarry Smith 
9759b7cd975SBarry Smith .seealso: TSSetRHSJacobian(), TSSetIJacobian(), TSComputeSolutionFunction(), TSSetSolutionFunction()
9769b7cd975SBarry Smith @*/
9779b7cd975SBarry Smith PetscErrorCode  TSSetForcingFunction(TS ts,PetscErrorCode (*f)(TS,PetscReal,Vec,void*),void *ctx)
9789b7cd975SBarry Smith {
9799b7cd975SBarry Smith   PetscErrorCode ierr;
9809b7cd975SBarry Smith   DM             dm;
9819b7cd975SBarry Smith 
9829b7cd975SBarry Smith   PetscFunctionBegin;
9839b7cd975SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
9849b7cd975SBarry Smith   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
9859b7cd975SBarry Smith   ierr = DMTSSetForcingFunction(dm,f,ctx);CHKERRQ(ierr);
9869b7cd975SBarry Smith   PetscFunctionReturn(0);
9879b7cd975SBarry Smith }
9889b7cd975SBarry Smith 
9899b7cd975SBarry Smith #undef __FUNCT__
9904a2ae208SSatish Balay #define __FUNCT__ "TSSetRHSJacobian"
991d763cef2SBarry Smith /*@C
992f7ab8db6SBarry Smith    TSSetRHSJacobian - Sets the function to compute the Jacobian of G,
993b5abc632SBarry Smith    where U_t = G(U,t), as well as the location to store the matrix.
994d763cef2SBarry Smith 
9953f9fe445SBarry Smith    Logically Collective on TS
996d763cef2SBarry Smith 
997d763cef2SBarry Smith    Input Parameters:
998d763cef2SBarry Smith +  ts  - the TS context obtained from TSCreate()
999e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1000e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1001d763cef2SBarry Smith .  f   - the Jacobian evaluation routine
1002d763cef2SBarry Smith -  ctx - [optional] user-defined context for private data for the
10030298fd71SBarry Smith          Jacobian evaluation routine (may be NULL)
1004d763cef2SBarry Smith 
1005f7ab8db6SBarry Smith    Calling sequence of f:
1006ae8867d6SBarry Smith $     func (TS ts,PetscReal t,Vec u,Mat A,Mat B,void *ctx);
1007d763cef2SBarry Smith 
1008d763cef2SBarry Smith +  t - current timestep
1009d763cef2SBarry Smith .  u - input vector
1010e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1011e5d3d808SBarry Smith .  Pmat - matrix from which preconditioner is to be constructed (usually the same as Amat)
1012d763cef2SBarry Smith -  ctx - [optional] user-defined context for matrix evaluation routine
1013d763cef2SBarry Smith 
1014d763cef2SBarry Smith 
1015d763cef2SBarry Smith    Level: beginner
1016d763cef2SBarry Smith 
1017d763cef2SBarry Smith .keywords: TS, timestep, set, right-hand-side, Jacobian
1018d763cef2SBarry Smith 
1019ae8867d6SBarry Smith .seealso: SNESComputeJacobianDefaultColor(), TSSetRHSFunction(), TSRHSJacobianSetReuse(), TSSetIJacobian()
1020d763cef2SBarry Smith 
1021d763cef2SBarry Smith @*/
1022e5d3d808SBarry Smith PetscErrorCode  TSSetRHSJacobian(TS ts,Mat Amat,Mat Pmat,TSRHSJacobian f,void *ctx)
1023d763cef2SBarry Smith {
1024277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1025089b2837SJed Brown   SNES           snes;
102624989b8cSPeter Brune   DM             dm;
102724989b8cSPeter Brune   TSIJacobian    ijacobian;
1028277b19d0SLisandro Dalcin 
1029d763cef2SBarry Smith   PetscFunctionBegin;
10300700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1031e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1032e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1033e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1034e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
1035d763cef2SBarry Smith 
103624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
103724989b8cSPeter Brune   ierr = DMTSSetRHSJacobian(dm,f,ctx);CHKERRQ(ierr);
1038e1244c69SJed Brown   if (f == TSComputeRHSJacobianConstant) {
1039e1244c69SJed Brown     /* Handle this case automatically for the user; otherwise user should call themselves. */
1040e1244c69SJed Brown     ierr = TSRHSJacobianSetReuse(ts,PETSC_TRUE);CHKERRQ(ierr);
1041e1244c69SJed Brown   }
10420298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijacobian,NULL);CHKERRQ(ierr);
1043089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
10445f659677SPeter Brune   if (!ijacobian) {
1045e5d3d808SBarry Smith     ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
10460e4ef248SJed Brown   }
1047e5d3d808SBarry Smith   if (Amat) {
1048e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
10490e4ef248SJed Brown     ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
1050bbd56ea5SKarl Rupp 
1051e5d3d808SBarry Smith     ts->Arhs = Amat;
10520e4ef248SJed Brown   }
1053e5d3d808SBarry Smith   if (Pmat) {
1054e5d3d808SBarry Smith     ierr = PetscObjectReference((PetscObject)Pmat);CHKERRQ(ierr);
10550e4ef248SJed Brown     ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1056bbd56ea5SKarl Rupp 
1057e5d3d808SBarry Smith     ts->Brhs = Pmat;
10580e4ef248SJed Brown   }
1059d763cef2SBarry Smith   PetscFunctionReturn(0);
1060d763cef2SBarry Smith }
1061d763cef2SBarry Smith 
1062316643e7SJed Brown 
1063316643e7SJed Brown #undef __FUNCT__
1064316643e7SJed Brown #define __FUNCT__ "TSSetIFunction"
1065316643e7SJed Brown /*@C
1066b5abc632SBarry Smith    TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved.
1067316643e7SJed Brown 
10683f9fe445SBarry Smith    Logically Collective on TS
1069316643e7SJed Brown 
1070316643e7SJed Brown    Input Parameters:
1071316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
10720298fd71SBarry Smith .  r   - vector to hold the residual (or NULL to have it created internally)
1073316643e7SJed Brown .  f   - the function evaluation routine
10740298fd71SBarry Smith -  ctx - user-defined context for private data for the function evaluation routine (may be NULL)
1075316643e7SJed Brown 
1076316643e7SJed Brown    Calling sequence of f:
1077316643e7SJed Brown $  f(TS ts,PetscReal t,Vec u,Vec u_t,Vec F,ctx);
1078316643e7SJed Brown 
1079316643e7SJed Brown +  t   - time at step/stage being solved
1080316643e7SJed Brown .  u   - state vector
1081316643e7SJed Brown .  u_t - time derivative of state vector
1082316643e7SJed Brown .  F   - function vector
1083316643e7SJed Brown -  ctx - [optional] user-defined context for matrix evaluation routine
1084316643e7SJed Brown 
1085316643e7SJed Brown    Important:
10862bbac0d3SBarry Smith    The user MUST call either this routine or TSSetRHSFunction() to define the ODE.  When solving DAEs you must use this function.
1087316643e7SJed Brown 
1088316643e7SJed Brown    Level: beginner
1089316643e7SJed Brown 
1090316643e7SJed Brown .keywords: TS, timestep, set, DAE, Jacobian
1091316643e7SJed Brown 
1092d6cbdb99SBarry Smith .seealso: TSSetRHSJacobian(), TSSetRHSFunction(), TSSetIJacobian()
1093316643e7SJed Brown @*/
1094089b2837SJed Brown PetscErrorCode  TSSetIFunction(TS ts,Vec res,TSIFunction f,void *ctx)
1095316643e7SJed Brown {
1096089b2837SJed Brown   PetscErrorCode ierr;
1097089b2837SJed Brown   SNES           snes;
10980298fd71SBarry Smith   Vec            resalloc = NULL;
109924989b8cSPeter Brune   DM             dm;
1100316643e7SJed Brown 
1101316643e7SJed Brown   PetscFunctionBegin;
11020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1103ca94891dSJed Brown   if (res) PetscValidHeaderSpecific(res,VEC_CLASSID,2);
110424989b8cSPeter Brune 
110524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
110624989b8cSPeter Brune   ierr = DMTSSetIFunction(dm,f,ctx);CHKERRQ(ierr);
110724989b8cSPeter Brune 
1108089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1109e856ceecSJed Brown   if (!res && !ts->dm && ts->vec_sol) {
1110e856ceecSJed Brown     ierr = VecDuplicate(ts->vec_sol,&resalloc);CHKERRQ(ierr);
1111e856ceecSJed Brown     res  = resalloc;
1112e856ceecSJed Brown   }
1113089b2837SJed Brown   ierr = SNESSetFunction(snes,res,SNESTSFormFunction,ts);CHKERRQ(ierr);
1114e856ceecSJed Brown   ierr = VecDestroy(&resalloc);CHKERRQ(ierr);
1115089b2837SJed Brown   PetscFunctionReturn(0);
1116089b2837SJed Brown }
1117089b2837SJed Brown 
1118089b2837SJed Brown #undef __FUNCT__
1119089b2837SJed Brown #define __FUNCT__ "TSGetIFunction"
1120089b2837SJed Brown /*@C
1121089b2837SJed Brown    TSGetIFunction - Returns the vector where the implicit residual is stored and the function/contex to compute it.
1122089b2837SJed Brown 
1123089b2837SJed Brown    Not Collective
1124089b2837SJed Brown 
1125089b2837SJed Brown    Input Parameter:
1126089b2837SJed Brown .  ts - the TS context
1127089b2837SJed Brown 
1128089b2837SJed Brown    Output Parameter:
11290298fd71SBarry Smith +  r - vector to hold residual (or NULL)
11300298fd71SBarry Smith .  func - the function to compute residual (or NULL)
11310298fd71SBarry Smith -  ctx - the function context (or NULL)
1132089b2837SJed Brown 
1133089b2837SJed Brown    Level: advanced
1134089b2837SJed Brown 
1135089b2837SJed Brown .keywords: TS, nonlinear, get, function
1136089b2837SJed Brown 
1137089b2837SJed Brown .seealso: TSSetIFunction(), SNESGetFunction()
1138089b2837SJed Brown @*/
1139089b2837SJed Brown PetscErrorCode TSGetIFunction(TS ts,Vec *r,TSIFunction *func,void **ctx)
1140089b2837SJed Brown {
1141089b2837SJed Brown   PetscErrorCode ierr;
1142089b2837SJed Brown   SNES           snes;
114324989b8cSPeter Brune   DM             dm;
1144089b2837SJed Brown 
1145089b2837SJed Brown   PetscFunctionBegin;
1146089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1147089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11480298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
114924989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
115024989b8cSPeter Brune   ierr = DMTSGetIFunction(dm,func,ctx);CHKERRQ(ierr);
1151089b2837SJed Brown   PetscFunctionReturn(0);
1152089b2837SJed Brown }
1153089b2837SJed Brown 
1154089b2837SJed Brown #undef __FUNCT__
1155089b2837SJed Brown #define __FUNCT__ "TSGetRHSFunction"
1156089b2837SJed Brown /*@C
1157089b2837SJed Brown    TSGetRHSFunction - Returns the vector where the right hand side is stored and the function/context to compute it.
1158089b2837SJed Brown 
1159089b2837SJed Brown    Not Collective
1160089b2837SJed Brown 
1161089b2837SJed Brown    Input Parameter:
1162089b2837SJed Brown .  ts - the TS context
1163089b2837SJed Brown 
1164089b2837SJed Brown    Output Parameter:
11650298fd71SBarry Smith +  r - vector to hold computed right hand side (or NULL)
11660298fd71SBarry Smith .  func - the function to compute right hand side (or NULL)
11670298fd71SBarry Smith -  ctx - the function context (or NULL)
1168089b2837SJed Brown 
1169089b2837SJed Brown    Level: advanced
1170089b2837SJed Brown 
1171089b2837SJed Brown .keywords: TS, nonlinear, get, function
1172089b2837SJed Brown 
11732bbac0d3SBarry Smith .seealso: TSSetRHSFunction(), SNESGetFunction()
1174089b2837SJed Brown @*/
1175089b2837SJed Brown PetscErrorCode TSGetRHSFunction(TS ts,Vec *r,TSRHSFunction *func,void **ctx)
1176089b2837SJed Brown {
1177089b2837SJed Brown   PetscErrorCode ierr;
1178089b2837SJed Brown   SNES           snes;
117924989b8cSPeter Brune   DM             dm;
1180089b2837SJed Brown 
1181089b2837SJed Brown   PetscFunctionBegin;
1182089b2837SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1183089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
11840298fd71SBarry Smith   ierr = SNESGetFunction(snes,r,NULL,NULL);CHKERRQ(ierr);
118524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
118624989b8cSPeter Brune   ierr = DMTSGetRHSFunction(dm,func,ctx);CHKERRQ(ierr);
1187316643e7SJed Brown   PetscFunctionReturn(0);
1188316643e7SJed Brown }
1189316643e7SJed Brown 
1190316643e7SJed Brown #undef __FUNCT__
1191316643e7SJed Brown #define __FUNCT__ "TSSetIJacobian"
1192316643e7SJed Brown /*@C
1193a4f0a591SBarry Smith    TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function
1194ae8867d6SBarry Smith         provided with TSSetIFunction().
1195316643e7SJed Brown 
11963f9fe445SBarry Smith    Logically Collective on TS
1197316643e7SJed Brown 
1198316643e7SJed Brown    Input Parameters:
1199316643e7SJed Brown +  ts  - the TS context obtained from TSCreate()
1200e5d3d808SBarry Smith .  Amat - (approximate) Jacobian matrix
1201e5d3d808SBarry Smith .  Pmat - matrix used to compute preconditioner (usually the same as Amat)
1202316643e7SJed Brown .  f   - the Jacobian evaluation routine
12030298fd71SBarry Smith -  ctx - user-defined context for private data for the Jacobian evaluation routine (may be NULL)
1204316643e7SJed Brown 
1205316643e7SJed Brown    Calling sequence of f:
1206ae8867d6SBarry Smith $  f(TS ts,PetscReal t,Vec U,Vec U_t,PetscReal a,Mat Amat,Mat Pmat,void *ctx);
1207316643e7SJed Brown 
1208316643e7SJed Brown +  t    - time at step/stage being solved
12091b4a444bSJed Brown .  U    - state vector
12101b4a444bSJed Brown .  U_t  - time derivative of state vector
1211316643e7SJed Brown .  a    - shift
1212e5d3d808SBarry Smith .  Amat - (approximate) Jacobian of F(t,U,W+a*U), equivalent to dF/dU + a*dF/dU_t
1213e5d3d808SBarry Smith .  Pmat - matrix used for constructing preconditioner, usually the same as Amat
1214316643e7SJed Brown -  ctx  - [optional] user-defined context for matrix evaluation routine
1215316643e7SJed Brown 
1216316643e7SJed Brown    Notes:
1217e5d3d808SBarry Smith    The matrices Amat and Pmat are exactly the matrices that are used by SNES for the nonlinear solve.
1218316643e7SJed Brown 
1219a4f0a591SBarry Smith    The matrix dF/dU + a*dF/dU_t you provide turns out to be
1220b5abc632SBarry Smith    the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved.
1221a4f0a591SBarry Smith    The time integrator internally approximates U_t by W+a*U where the positive "shift"
1222a4f0a591SBarry Smith    a and vector W depend on the integration method, step size, and past states. For example with
1223a4f0a591SBarry Smith    the backward Euler method a = 1/dt and W = -a*U(previous timestep) so
1224a4f0a591SBarry Smith    W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt
1225a4f0a591SBarry Smith 
1226316643e7SJed Brown    Level: beginner
1227316643e7SJed Brown 
1228316643e7SJed Brown .keywords: TS, timestep, DAE, Jacobian
1229316643e7SJed Brown 
1230ae8867d6SBarry Smith .seealso: TSSetIFunction(), TSSetRHSJacobian(), SNESComputeJacobianDefaultColor(), SNESComputeJacobianDefault(), TSSetRHSFunction()
1231316643e7SJed Brown 
1232316643e7SJed Brown @*/
1233e5d3d808SBarry Smith PetscErrorCode  TSSetIJacobian(TS ts,Mat Amat,Mat Pmat,TSIJacobian f,void *ctx)
1234316643e7SJed Brown {
1235316643e7SJed Brown   PetscErrorCode ierr;
1236089b2837SJed Brown   SNES           snes;
123724989b8cSPeter Brune   DM             dm;
1238316643e7SJed Brown 
1239316643e7SJed Brown   PetscFunctionBegin;
12400700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1241e5d3d808SBarry Smith   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
1242e5d3d808SBarry Smith   if (Pmat) PetscValidHeaderSpecific(Pmat,MAT_CLASSID,3);
1243e5d3d808SBarry Smith   if (Amat) PetscCheckSameComm(ts,1,Amat,2);
1244e5d3d808SBarry Smith   if (Pmat) PetscCheckSameComm(ts,1,Pmat,3);
124524989b8cSPeter Brune 
124624989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
124724989b8cSPeter Brune   ierr = DMTSSetIJacobian(dm,f,ctx);CHKERRQ(ierr);
124824989b8cSPeter Brune 
1249089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1250e5d3d808SBarry Smith   ierr = SNESSetJacobian(snes,Amat,Pmat,SNESTSFormJacobian,ts);CHKERRQ(ierr);
1251316643e7SJed Brown   PetscFunctionReturn(0);
1252316643e7SJed Brown }
1253316643e7SJed Brown 
12544a2ae208SSatish Balay #undef __FUNCT__
1255e1244c69SJed Brown #define __FUNCT__ "TSRHSJacobianSetReuse"
1256e1244c69SJed Brown /*@
1257e1244c69SJed Brown    TSRHSJacobianSetReuse - restore RHS Jacobian before re-evaluating.  Without this flag, TS will change the sign and
1258e1244c69SJed Brown    shift the RHS Jacobian for a finite-time-step implicit solve, in which case the user function will need to recompute
1259e1244c69SJed Brown    the entire Jacobian.  The reuse flag must be set if the evaluation function will assume that the matrix entries have
1260e1244c69SJed Brown    not been changed by the TS.
1261e1244c69SJed Brown 
1262e1244c69SJed Brown    Logically Collective
1263e1244c69SJed Brown 
1264e1244c69SJed Brown    Input Arguments:
1265e1244c69SJed Brown +  ts - TS context obtained from TSCreate()
1266e1244c69SJed Brown -  reuse - PETSC_TRUE if the RHS Jacobian
1267e1244c69SJed Brown 
1268e1244c69SJed Brown    Level: intermediate
1269e1244c69SJed Brown 
1270e1244c69SJed Brown .seealso: TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
1271e1244c69SJed Brown @*/
1272e1244c69SJed Brown PetscErrorCode TSRHSJacobianSetReuse(TS ts,PetscBool reuse)
1273e1244c69SJed Brown {
1274e1244c69SJed Brown   PetscFunctionBegin;
1275e1244c69SJed Brown   ts->rhsjacobian.reuse = reuse;
1276e1244c69SJed Brown   PetscFunctionReturn(0);
1277e1244c69SJed Brown }
1278e1244c69SJed Brown 
1279e1244c69SJed Brown #undef __FUNCT__
128055849f57SBarry Smith #define __FUNCT__ "TSLoad"
128155849f57SBarry Smith /*@C
128255849f57SBarry Smith   TSLoad - Loads a KSP that has been stored in binary  with KSPView().
128355849f57SBarry Smith 
128455849f57SBarry Smith   Collective on PetscViewer
128555849f57SBarry Smith 
128655849f57SBarry Smith   Input Parameters:
128755849f57SBarry Smith + newdm - the newly loaded TS, this needs to have been created with TSCreate() or
128855849f57SBarry Smith            some related function before a call to TSLoad().
128955849f57SBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen()
129055849f57SBarry Smith 
129155849f57SBarry Smith    Level: intermediate
129255849f57SBarry Smith 
129355849f57SBarry Smith   Notes:
129455849f57SBarry Smith    The type is determined by the data in the file, any type set into the TS before this call is ignored.
129555849f57SBarry Smith 
129655849f57SBarry Smith   Notes for advanced users:
129755849f57SBarry Smith   Most users should not need to know the details of the binary storage
129855849f57SBarry Smith   format, since TSLoad() and TSView() completely hide these details.
129955849f57SBarry Smith   But for anyone who's interested, the standard binary matrix storage
130055849f57SBarry Smith   format is
130155849f57SBarry Smith .vb
130255849f57SBarry Smith      has not yet been determined
130355849f57SBarry Smith .ve
130455849f57SBarry Smith 
130555849f57SBarry Smith .seealso: PetscViewerBinaryOpen(), TSView(), MatLoad(), VecLoad()
130655849f57SBarry Smith @*/
1307f2c2a1b9SBarry Smith PetscErrorCode  TSLoad(TS ts, PetscViewer viewer)
130855849f57SBarry Smith {
130955849f57SBarry Smith   PetscErrorCode ierr;
131055849f57SBarry Smith   PetscBool      isbinary;
131155849f57SBarry Smith   PetscInt       classid;
131255849f57SBarry Smith   char           type[256];
13132d53ad75SBarry Smith   DMTS           sdm;
1314ad6bc421SBarry Smith   DM             dm;
131555849f57SBarry Smith 
131655849f57SBarry Smith   PetscFunctionBegin;
1317f2c2a1b9SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
131855849f57SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
131955849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
132055849f57SBarry Smith   if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
132155849f57SBarry Smith 
132255849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
1323ce94432eSBarry Smith   if (classid != TS_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONG,"Not TS next in file");
132455849f57SBarry Smith   ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
1325f2c2a1b9SBarry Smith   ierr = TSSetType(ts, type);CHKERRQ(ierr);
1326f2c2a1b9SBarry Smith   if (ts->ops->load) {
1327f2c2a1b9SBarry Smith     ierr = (*ts->ops->load)(ts,viewer);CHKERRQ(ierr);
1328f2c2a1b9SBarry Smith   }
1329ce94432eSBarry Smith   ierr = DMCreate(PetscObjectComm((PetscObject)ts),&dm);CHKERRQ(ierr);
1330ad6bc421SBarry Smith   ierr = DMLoad(dm,viewer);CHKERRQ(ierr);
1331ad6bc421SBarry Smith   ierr = TSSetDM(ts,dm);CHKERRQ(ierr);
1332f2c2a1b9SBarry Smith   ierr = DMCreateGlobalVector(ts->dm,&ts->vec_sol);CHKERRQ(ierr);
1333f2c2a1b9SBarry Smith   ierr = VecLoad(ts->vec_sol,viewer);CHKERRQ(ierr);
13342d53ad75SBarry Smith   ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
13352d53ad75SBarry Smith   ierr = DMTSLoad(sdm,viewer);CHKERRQ(ierr);
133655849f57SBarry Smith   PetscFunctionReturn(0);
133755849f57SBarry Smith }
133855849f57SBarry Smith 
13399804daf3SBarry Smith #include <petscdraw.h>
1340e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1341e04113cfSBarry Smith #include <petscviewersaws.h>
1342f05ece33SBarry Smith #endif
134355849f57SBarry Smith #undef __FUNCT__
13444a2ae208SSatish Balay #define __FUNCT__ "TSView"
13457e2c5f70SBarry Smith /*@C
1346d763cef2SBarry Smith     TSView - Prints the TS data structure.
1347d763cef2SBarry Smith 
13484c49b128SBarry Smith     Collective on TS
1349d763cef2SBarry Smith 
1350d763cef2SBarry Smith     Input Parameters:
1351d763cef2SBarry Smith +   ts - the TS context obtained from TSCreate()
1352d763cef2SBarry Smith -   viewer - visualization context
1353d763cef2SBarry Smith 
1354d763cef2SBarry Smith     Options Database Key:
1355d763cef2SBarry Smith .   -ts_view - calls TSView() at end of TSStep()
1356d763cef2SBarry Smith 
1357d763cef2SBarry Smith     Notes:
1358d763cef2SBarry Smith     The available visualization contexts include
1359b0a32e0cSBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
1360b0a32e0cSBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
1361d763cef2SBarry Smith          output where only the first processor opens
1362d763cef2SBarry Smith          the file.  All other processors send their
1363d763cef2SBarry Smith          data to the first processor to print.
1364d763cef2SBarry Smith 
1365d763cef2SBarry Smith     The user can open an alternative visualization context with
1366b0a32e0cSBarry Smith     PetscViewerASCIIOpen() - output to a specified file.
1367d763cef2SBarry Smith 
1368d763cef2SBarry Smith     Level: beginner
1369d763cef2SBarry Smith 
1370d763cef2SBarry Smith .keywords: TS, timestep, view
1371d763cef2SBarry Smith 
1372b0a32e0cSBarry Smith .seealso: PetscViewerASCIIOpen()
1373d763cef2SBarry Smith @*/
13747087cfbeSBarry Smith PetscErrorCode  TSView(TS ts,PetscViewer viewer)
1375d763cef2SBarry Smith {
1376dfbe8321SBarry Smith   PetscErrorCode ierr;
137719fd82e9SBarry Smith   TSType         type;
13782b0a91c0SBarry Smith   PetscBool      iascii,isstring,isundials,isbinary,isdraw;
13792d53ad75SBarry Smith   DMTS           sdm;
1380e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1381536b137fSBarry Smith   PetscBool      issaws;
1382f05ece33SBarry Smith #endif
1383d763cef2SBarry Smith 
1384d763cef2SBarry Smith   PetscFunctionBegin;
13850700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
13863050cee2SBarry Smith   if (!viewer) {
1387ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts),&viewer);CHKERRQ(ierr);
13883050cee2SBarry Smith   }
13890700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
1390c9780b6fSBarry Smith   PetscCheckSameComm(ts,1,viewer,2);
1391fd16b177SBarry Smith 
1392251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
1393251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr);
139455849f57SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
13952b0a91c0SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
1396e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1397536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
1398f05ece33SBarry Smith #endif
139932077d6dSBarry Smith   if (iascii) {
1400dae58748SBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)ts,viewer);CHKERRQ(ierr);
140177431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum steps=%D\n",ts->max_steps);CHKERRQ(ierr);
14027c8652ddSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"  maximum time=%g\n",(double)ts->max_time);CHKERRQ(ierr);
1403d763cef2SBarry Smith     if (ts->problem_type == TS_NONLINEAR) {
14045ef26d82SJed Brown       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solver iterations=%D\n",ts->snes_its);CHKERRQ(ierr);
1405c610991cSLisandro Dalcin       ierr = PetscViewerASCIIPrintf(viewer,"  total number of nonlinear solve failures=%D\n",ts->num_snes_failures);CHKERRQ(ierr);
1406d763cef2SBarry Smith     }
14075ef26d82SJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of linear solver iterations=%D\n",ts->ksp_its);CHKERRQ(ierr);
1408193ac0bcSJed Brown     ierr = PetscViewerASCIIPrintf(viewer,"  total number of rejected steps=%D\n",ts->reject);CHKERRQ(ierr);
14092d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14102d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
1411d52bd9f3SBarry Smith     if (ts->ops->view) {
1412d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1413d52bd9f3SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
1414d52bd9f3SBarry Smith       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1415d52bd9f3SBarry Smith     }
14160f5bd95cSBarry Smith   } else if (isstring) {
1417a313700dSBarry Smith     ierr = TSGetType(ts,&type);CHKERRQ(ierr);
1418b0a32e0cSBarry Smith     ierr = PetscViewerStringSPrintf(viewer," %-7.7s",type);CHKERRQ(ierr);
141955849f57SBarry Smith   } else if (isbinary) {
142055849f57SBarry Smith     PetscInt    classid = TS_FILE_CLASSID;
142155849f57SBarry Smith     MPI_Comm    comm;
142255849f57SBarry Smith     PetscMPIInt rank;
142355849f57SBarry Smith     char        type[256];
142455849f57SBarry Smith 
142555849f57SBarry Smith     ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
142655849f57SBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
142755849f57SBarry Smith     if (!rank) {
142855849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
142955849f57SBarry Smith       ierr = PetscStrncpy(type,((PetscObject)ts)->type_name,256);CHKERRQ(ierr);
143055849f57SBarry Smith       ierr = PetscViewerBinaryWrite(viewer,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
143155849f57SBarry Smith     }
143255849f57SBarry Smith     if (ts->ops->view) {
143355849f57SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
143455849f57SBarry Smith     }
1435f2c2a1b9SBarry Smith     ierr = DMView(ts->dm,viewer);CHKERRQ(ierr);
1436f2c2a1b9SBarry Smith     ierr = VecView(ts->vec_sol,viewer);CHKERRQ(ierr);
14372d53ad75SBarry Smith     ierr = DMGetDMTS(ts->dm,&sdm);CHKERRQ(ierr);
14382d53ad75SBarry Smith     ierr = DMTSView(sdm,viewer);CHKERRQ(ierr);
14392b0a91c0SBarry Smith   } else if (isdraw) {
14402b0a91c0SBarry Smith     PetscDraw draw;
14412b0a91c0SBarry Smith     char      str[36];
144289fd9fafSBarry Smith     PetscReal x,y,bottom,h;
14432b0a91c0SBarry Smith 
14442b0a91c0SBarry Smith     ierr   = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
14452b0a91c0SBarry Smith     ierr   = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
14462b0a91c0SBarry Smith     ierr   = PetscStrcpy(str,"TS: ");CHKERRQ(ierr);
14472b0a91c0SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)ts)->type_name);CHKERRQ(ierr);
14480298fd71SBarry Smith     ierr   = PetscDrawBoxedString(draw,x,y,PETSC_DRAW_BLACK,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
144989fd9fafSBarry Smith     bottom = y - h;
14502b0a91c0SBarry Smith     ierr   = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
14512b0a91c0SBarry Smith     if (ts->ops->view) {
14522b0a91c0SBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14532b0a91c0SBarry Smith     }
14542b0a91c0SBarry Smith     ierr = PetscDrawPopCurrentPoint(draw);CHKERRQ(ierr);
1455e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1456536b137fSBarry Smith   } else if (issaws) {
1457d45a07a7SBarry Smith     PetscMPIInt rank;
14582657e9d9SBarry Smith     const char  *name;
14592657e9d9SBarry Smith 
14602657e9d9SBarry Smith     ierr = PetscObjectGetName((PetscObject)ts,&name);CHKERRQ(ierr);
1461d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1462d45a07a7SBarry Smith     if (!((PetscObject)ts)->amsmem && !rank) {
1463d45a07a7SBarry Smith       char       dir[1024];
1464d45a07a7SBarry Smith 
1465e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)ts,viewer);CHKERRQ(ierr);
1466a0931e03SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time_step",name);CHKERRQ(ierr);
14672657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->steps,1,SAWs_READ,SAWs_INT));
14682657e9d9SBarry Smith       ierr = PetscSNPrintf(dir,1024,"/PETSc/Objects/%s/time",name);CHKERRQ(ierr);
14692657e9d9SBarry Smith       PetscStackCallSAWs(SAWs_Register,(dir,&ts->ptime,1,SAWs_READ,SAWs_DOUBLE));
1470d763cef2SBarry Smith     }
14710acecf5bSBarry Smith     if (ts->ops->view) {
14720acecf5bSBarry Smith       ierr = (*ts->ops->view)(ts,viewer);CHKERRQ(ierr);
14730acecf5bSBarry Smith     }
1474f05ece33SBarry Smith #endif
1475f05ece33SBarry Smith   }
1476f05ece33SBarry Smith 
1477b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
1478251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)ts,TSSUNDIALS,&isundials);CHKERRQ(ierr);
1479b0a32e0cSBarry Smith   ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
1480d763cef2SBarry Smith   PetscFunctionReturn(0);
1481d763cef2SBarry Smith }
1482d763cef2SBarry Smith 
1483d763cef2SBarry Smith 
14844a2ae208SSatish Balay #undef __FUNCT__
14854a2ae208SSatish Balay #define __FUNCT__ "TSSetApplicationContext"
1486b07ff414SBarry Smith /*@
1487d763cef2SBarry Smith    TSSetApplicationContext - Sets an optional user-defined context for
1488d763cef2SBarry Smith    the timesteppers.
1489d763cef2SBarry Smith 
14903f9fe445SBarry Smith    Logically Collective on TS
1491d763cef2SBarry Smith 
1492d763cef2SBarry Smith    Input Parameters:
1493d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1494d763cef2SBarry Smith -  usrP - optional user context
1495d763cef2SBarry Smith 
1496d763cef2SBarry Smith    Level: intermediate
1497d763cef2SBarry Smith 
1498d763cef2SBarry Smith .keywords: TS, timestep, set, application, context
1499d763cef2SBarry Smith 
1500d763cef2SBarry Smith .seealso: TSGetApplicationContext()
1501d763cef2SBarry Smith @*/
15027087cfbeSBarry Smith PetscErrorCode  TSSetApplicationContext(TS ts,void *usrP)
1503d763cef2SBarry Smith {
1504d763cef2SBarry Smith   PetscFunctionBegin;
15050700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1506d763cef2SBarry Smith   ts->user = usrP;
1507d763cef2SBarry Smith   PetscFunctionReturn(0);
1508d763cef2SBarry Smith }
1509d763cef2SBarry Smith 
15104a2ae208SSatish Balay #undef __FUNCT__
15114a2ae208SSatish Balay #define __FUNCT__ "TSGetApplicationContext"
1512b07ff414SBarry Smith /*@
1513d763cef2SBarry Smith     TSGetApplicationContext - Gets the user-defined context for the
1514d763cef2SBarry Smith     timestepper.
1515d763cef2SBarry Smith 
1516d763cef2SBarry Smith     Not Collective
1517d763cef2SBarry Smith 
1518d763cef2SBarry Smith     Input Parameter:
1519d763cef2SBarry Smith .   ts - the TS context obtained from TSCreate()
1520d763cef2SBarry Smith 
1521d763cef2SBarry Smith     Output Parameter:
1522d763cef2SBarry Smith .   usrP - user context
1523d763cef2SBarry Smith 
1524d763cef2SBarry Smith     Level: intermediate
1525d763cef2SBarry Smith 
1526d763cef2SBarry Smith .keywords: TS, timestep, get, application, context
1527d763cef2SBarry Smith 
1528d763cef2SBarry Smith .seealso: TSSetApplicationContext()
1529d763cef2SBarry Smith @*/
1530e71120c6SJed Brown PetscErrorCode  TSGetApplicationContext(TS ts,void *usrP)
1531d763cef2SBarry Smith {
1532d763cef2SBarry Smith   PetscFunctionBegin;
15330700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1534e71120c6SJed Brown   *(void**)usrP = ts->user;
1535d763cef2SBarry Smith   PetscFunctionReturn(0);
1536d763cef2SBarry Smith }
1537d763cef2SBarry Smith 
15384a2ae208SSatish Balay #undef __FUNCT__
15394a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStepNumber"
1540d763cef2SBarry Smith /*@
1541b8123daeSJed Brown    TSGetTimeStepNumber - Gets the number of time steps completed.
1542d763cef2SBarry Smith 
1543d763cef2SBarry Smith    Not Collective
1544d763cef2SBarry Smith 
1545d763cef2SBarry Smith    Input Parameter:
1546d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1547d763cef2SBarry Smith 
1548d763cef2SBarry Smith    Output Parameter:
1549b8123daeSJed Brown .  iter - number of steps completed so far
1550d763cef2SBarry Smith 
1551d763cef2SBarry Smith    Level: intermediate
1552d763cef2SBarry Smith 
1553d763cef2SBarry Smith .keywords: TS, timestep, get, iteration, number
15549be3e283SDebojyoti Ghosh .seealso: TSGetTime(), TSGetTimeStep(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSSetPostStep()
1555d763cef2SBarry Smith @*/
15567087cfbeSBarry Smith PetscErrorCode  TSGetTimeStepNumber(TS ts,PetscInt *iter)
1557d763cef2SBarry Smith {
1558d763cef2SBarry Smith   PetscFunctionBegin;
15590700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
15604482741eSBarry Smith   PetscValidIntPointer(iter,2);
1561d763cef2SBarry Smith   *iter = ts->steps;
1562d763cef2SBarry Smith   PetscFunctionReturn(0);
1563d763cef2SBarry Smith }
1564d763cef2SBarry Smith 
15654a2ae208SSatish Balay #undef __FUNCT__
15664a2ae208SSatish Balay #define __FUNCT__ "TSSetInitialTimeStep"
1567d763cef2SBarry Smith /*@
1568d763cef2SBarry Smith    TSSetInitialTimeStep - Sets the initial timestep to be used,
1569d763cef2SBarry Smith    as well as the initial time.
1570d763cef2SBarry Smith 
15713f9fe445SBarry Smith    Logically Collective on TS
1572d763cef2SBarry Smith 
1573d763cef2SBarry Smith    Input Parameters:
1574d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1575d763cef2SBarry Smith .  initial_time - the initial time
1576d763cef2SBarry Smith -  time_step - the size of the timestep
1577d763cef2SBarry Smith 
1578d763cef2SBarry Smith    Level: intermediate
1579d763cef2SBarry Smith 
1580d763cef2SBarry Smith .seealso: TSSetTimeStep(), TSGetTimeStep()
1581d763cef2SBarry Smith 
1582d763cef2SBarry Smith .keywords: TS, set, initial, timestep
1583d763cef2SBarry Smith @*/
15847087cfbeSBarry Smith PetscErrorCode  TSSetInitialTimeStep(TS ts,PetscReal initial_time,PetscReal time_step)
1585d763cef2SBarry Smith {
1586e144a568SJed Brown   PetscErrorCode ierr;
1587e144a568SJed Brown 
1588d763cef2SBarry Smith   PetscFunctionBegin;
15890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1590e144a568SJed Brown   ierr = TSSetTimeStep(ts,time_step);CHKERRQ(ierr);
1591d8cd7023SBarry Smith   ierr = TSSetTime(ts,initial_time);CHKERRQ(ierr);
1592d763cef2SBarry Smith   PetscFunctionReturn(0);
1593d763cef2SBarry Smith }
1594d763cef2SBarry Smith 
15954a2ae208SSatish Balay #undef __FUNCT__
15964a2ae208SSatish Balay #define __FUNCT__ "TSSetTimeStep"
1597d763cef2SBarry Smith /*@
1598d763cef2SBarry Smith    TSSetTimeStep - Allows one to reset the timestep at any time,
1599d763cef2SBarry Smith    useful for simple pseudo-timestepping codes.
1600d763cef2SBarry Smith 
16013f9fe445SBarry Smith    Logically Collective on TS
1602d763cef2SBarry Smith 
1603d763cef2SBarry Smith    Input Parameters:
1604d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
1605d763cef2SBarry Smith -  time_step - the size of the timestep
1606d763cef2SBarry Smith 
1607d763cef2SBarry Smith    Level: intermediate
1608d763cef2SBarry Smith 
1609d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1610d763cef2SBarry Smith 
1611d763cef2SBarry Smith .keywords: TS, set, timestep
1612d763cef2SBarry Smith @*/
16137087cfbeSBarry Smith PetscErrorCode  TSSetTimeStep(TS ts,PetscReal time_step)
1614d763cef2SBarry Smith {
1615d763cef2SBarry Smith   PetscFunctionBegin;
16160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1617c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,time_step,2);
1618d763cef2SBarry Smith   ts->time_step      = time_step;
161931748224SBarry Smith   ts->time_step_orig = time_step;
1620d763cef2SBarry Smith   PetscFunctionReturn(0);
1621d763cef2SBarry Smith }
1622d763cef2SBarry Smith 
16234a2ae208SSatish Balay #undef __FUNCT__
1624a43b19c4SJed Brown #define __FUNCT__ "TSSetExactFinalTime"
1625a43b19c4SJed Brown /*@
162649354f04SShri Abhyankar    TSSetExactFinalTime - Determines whether to adapt the final time step to
162749354f04SShri Abhyankar      match the exact final time, interpolate solution to the exact final time,
162849354f04SShri Abhyankar      or just return at the final time TS computed.
1629a43b19c4SJed Brown 
1630a43b19c4SJed Brown   Logically Collective on TS
1631a43b19c4SJed Brown 
1632a43b19c4SJed Brown    Input Parameter:
1633a43b19c4SJed Brown +   ts - the time-step context
163449354f04SShri Abhyankar -   eftopt - exact final time option
1635a43b19c4SJed Brown 
1636a43b19c4SJed Brown    Level: beginner
1637a43b19c4SJed Brown 
1638a2ea699eSBarry Smith .seealso: TSExactFinalTimeOption
1639a43b19c4SJed Brown @*/
164049354f04SShri Abhyankar PetscErrorCode  TSSetExactFinalTime(TS ts,TSExactFinalTimeOption eftopt)
1641a43b19c4SJed Brown {
1642a43b19c4SJed Brown   PetscFunctionBegin;
1643a43b19c4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
164449354f04SShri Abhyankar   PetscValidLogicalCollectiveEnum(ts,eftopt,2);
164549354f04SShri Abhyankar   ts->exact_final_time = eftopt;
1646a43b19c4SJed Brown   PetscFunctionReturn(0);
1647a43b19c4SJed Brown }
1648a43b19c4SJed Brown 
1649a43b19c4SJed Brown #undef __FUNCT__
16504a2ae208SSatish Balay #define __FUNCT__ "TSGetTimeStep"
1651d763cef2SBarry Smith /*@
1652d763cef2SBarry Smith    TSGetTimeStep - Gets the current timestep size.
1653d763cef2SBarry Smith 
1654d763cef2SBarry Smith    Not Collective
1655d763cef2SBarry Smith 
1656d763cef2SBarry Smith    Input Parameter:
1657d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1658d763cef2SBarry Smith 
1659d763cef2SBarry Smith    Output Parameter:
1660d763cef2SBarry Smith .  dt - the current timestep size
1661d763cef2SBarry Smith 
1662d763cef2SBarry Smith    Level: intermediate
1663d763cef2SBarry Smith 
1664d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
1665d763cef2SBarry Smith 
1666d763cef2SBarry Smith .keywords: TS, get, timestep
1667d763cef2SBarry Smith @*/
16687087cfbeSBarry Smith PetscErrorCode  TSGetTimeStep(TS ts,PetscReal *dt)
1669d763cef2SBarry Smith {
1670d763cef2SBarry Smith   PetscFunctionBegin;
16710700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1672f7cf8827SBarry Smith   PetscValidRealPointer(dt,2);
1673d763cef2SBarry Smith   *dt = ts->time_step;
1674d763cef2SBarry Smith   PetscFunctionReturn(0);
1675d763cef2SBarry Smith }
1676d763cef2SBarry Smith 
16774a2ae208SSatish Balay #undef __FUNCT__
16784a2ae208SSatish Balay #define __FUNCT__ "TSGetSolution"
1679d8e5e3e6SSatish Balay /*@
1680d763cef2SBarry Smith    TSGetSolution - Returns the solution at the present timestep. It
1681d763cef2SBarry Smith    is valid to call this routine inside the function that you are evaluating
1682d763cef2SBarry Smith    in order to move to the new timestep. This vector not changed until
1683d763cef2SBarry Smith    the solution at the next timestep has been calculated.
1684d763cef2SBarry Smith 
1685d763cef2SBarry Smith    Not Collective, but Vec returned is parallel if TS is parallel
1686d763cef2SBarry Smith 
1687d763cef2SBarry Smith    Input Parameter:
1688d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1689d763cef2SBarry Smith 
1690d763cef2SBarry Smith    Output Parameter:
1691d763cef2SBarry Smith .  v - the vector containing the solution
1692d763cef2SBarry Smith 
1693d763cef2SBarry Smith    Level: intermediate
1694d763cef2SBarry Smith 
1695d763cef2SBarry Smith .seealso: TSGetTimeStep()
1696d763cef2SBarry Smith 
1697d763cef2SBarry Smith .keywords: TS, timestep, get, solution
1698d763cef2SBarry Smith @*/
16997087cfbeSBarry Smith PetscErrorCode  TSGetSolution(TS ts,Vec *v)
1700d763cef2SBarry Smith {
1701d763cef2SBarry Smith   PetscFunctionBegin;
17020700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
17034482741eSBarry Smith   PetscValidPointer(v,2);
17048737fe31SLisandro Dalcin   *v = ts->vec_sol;
1705d763cef2SBarry Smith   PetscFunctionReturn(0);
1706d763cef2SBarry Smith }
1707d763cef2SBarry Smith 
1708c235aa8dSHong Zhang #undef __FUNCT__
1709419a887dSBarry Smith #define __FUNCT__ "TSAdjointGetGradients"
1710c235aa8dSHong Zhang /*@
1711419a887dSBarry Smith    TSAdjointGetGradients - Returns the gradients from the TSAdjointSolve()
1712c235aa8dSHong Zhang 
1713c235aa8dSHong Zhang    Not Collective, but Vec returned is parallel if TS is parallel
1714c235aa8dSHong Zhang 
1715c235aa8dSHong Zhang    Input Parameter:
1716c235aa8dSHong Zhang .  ts - the TS context obtained from TSCreate()
1717c235aa8dSHong Zhang 
1718c235aa8dSHong Zhang    Output Parameter:
1719419a887dSBarry Smith +  v - vectors containing the gradients with respect to the ODE/DAE solution variables
1720419a887dSBarry Smith -  w - vectors containing the gradients with respect to the problem parameters
1721c235aa8dSHong Zhang 
1722c235aa8dSHong Zhang    Level: intermediate
1723c235aa8dSHong Zhang 
1724c235aa8dSHong Zhang .seealso: TSGetTimeStep()
1725c235aa8dSHong Zhang 
1726c235aa8dSHong Zhang .keywords: TS, timestep, get, sensitivity
1727c235aa8dSHong Zhang @*/
17285bf8c567SBarry Smith PetscErrorCode  TSAdjointGetGradients(TS ts,PetscInt *numberadjs,Vec **v,Vec **w)
1729c235aa8dSHong Zhang {
1730c235aa8dSHong Zhang   PetscFunctionBegin;
1731c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1732b18ea86cSHong Zhang   if (numberadjs) *numberadjs = ts->numberadjs;
1733419a887dSBarry Smith   if (v)          *v          = ts->vecs_sensi;
1734419a887dSBarry Smith   if (w)          *w          = ts->vecs_sensip;
1735c235aa8dSHong Zhang   PetscFunctionReturn(0);
1736c235aa8dSHong Zhang }
1737c235aa8dSHong Zhang 
1738bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */
17394a2ae208SSatish Balay #undef __FUNCT__
1740bdad233fSMatthew Knepley #define __FUNCT__ "TSSetProblemType"
1741d8e5e3e6SSatish Balay /*@
1742bdad233fSMatthew Knepley   TSSetProblemType - Sets the type of problem to be solved.
1743d763cef2SBarry Smith 
1744bdad233fSMatthew Knepley   Not collective
1745d763cef2SBarry Smith 
1746bdad233fSMatthew Knepley   Input Parameters:
1747bdad233fSMatthew Knepley + ts   - The TS
1748bdad233fSMatthew Knepley - type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1749d763cef2SBarry Smith .vb
17500910c330SBarry Smith          U_t - A U = 0      (linear)
17510910c330SBarry Smith          U_t - A(t) U = 0   (linear)
17520910c330SBarry Smith          F(t,U,U_t) = 0     (nonlinear)
1753d763cef2SBarry Smith .ve
1754d763cef2SBarry Smith 
1755d763cef2SBarry Smith    Level: beginner
1756d763cef2SBarry Smith 
1757bdad233fSMatthew Knepley .keywords: TS, problem type
1758bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1759d763cef2SBarry Smith @*/
17607087cfbeSBarry Smith PetscErrorCode  TSSetProblemType(TS ts, TSProblemType type)
1761a7cc72afSBarry Smith {
17629e2a6581SJed Brown   PetscErrorCode ierr;
17639e2a6581SJed Brown 
1764d763cef2SBarry Smith   PetscFunctionBegin;
17650700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
1766bdad233fSMatthew Knepley   ts->problem_type = type;
17679e2a6581SJed Brown   if (type == TS_LINEAR) {
17689e2a6581SJed Brown     SNES snes;
17699e2a6581SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
17709e2a6581SJed Brown     ierr = SNESSetType(snes,SNESKSPONLY);CHKERRQ(ierr);
17719e2a6581SJed Brown   }
1772d763cef2SBarry Smith   PetscFunctionReturn(0);
1773d763cef2SBarry Smith }
1774d763cef2SBarry Smith 
1775bdad233fSMatthew Knepley #undef __FUNCT__
1776bdad233fSMatthew Knepley #define __FUNCT__ "TSGetProblemType"
1777bdad233fSMatthew Knepley /*@C
1778bdad233fSMatthew Knepley   TSGetProblemType - Gets the type of problem to be solved.
1779bdad233fSMatthew Knepley 
1780bdad233fSMatthew Knepley   Not collective
1781bdad233fSMatthew Knepley 
1782bdad233fSMatthew Knepley   Input Parameter:
1783bdad233fSMatthew Knepley . ts   - The TS
1784bdad233fSMatthew Knepley 
1785bdad233fSMatthew Knepley   Output Parameter:
1786bdad233fSMatthew Knepley . type - One of TS_LINEAR, TS_NONLINEAR where these types refer to problems of the forms
1787bdad233fSMatthew Knepley .vb
1788089b2837SJed Brown          M U_t = A U
1789089b2837SJed Brown          M(t) U_t = A(t) U
1790b5abc632SBarry Smith          F(t,U,U_t)
1791bdad233fSMatthew Knepley .ve
1792bdad233fSMatthew Knepley 
1793bdad233fSMatthew Knepley    Level: beginner
1794bdad233fSMatthew Knepley 
1795bdad233fSMatthew Knepley .keywords: TS, problem type
1796bdad233fSMatthew Knepley .seealso: TSSetUp(), TSProblemType, TS
1797bdad233fSMatthew Knepley @*/
17987087cfbeSBarry Smith PetscErrorCode  TSGetProblemType(TS ts, TSProblemType *type)
1799a7cc72afSBarry Smith {
1800bdad233fSMatthew Knepley   PetscFunctionBegin;
18010700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
18024482741eSBarry Smith   PetscValidIntPointer(type,2);
1803bdad233fSMatthew Knepley   *type = ts->problem_type;
1804bdad233fSMatthew Knepley   PetscFunctionReturn(0);
1805bdad233fSMatthew Knepley }
1806d763cef2SBarry Smith 
18074a2ae208SSatish Balay #undef __FUNCT__
18084a2ae208SSatish Balay #define __FUNCT__ "TSSetUp"
1809d763cef2SBarry Smith /*@
1810d763cef2SBarry Smith    TSSetUp - Sets up the internal data structures for the later use
1811d763cef2SBarry Smith    of a timestepper.
1812d763cef2SBarry Smith 
1813d763cef2SBarry Smith    Collective on TS
1814d763cef2SBarry Smith 
1815d763cef2SBarry Smith    Input Parameter:
1816d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1817d763cef2SBarry Smith 
1818d763cef2SBarry Smith    Notes:
1819d763cef2SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1820d763cef2SBarry Smith    TSSetUp(), since these actions will automatically occur during
1821d763cef2SBarry Smith    the call to TSStep().  However, if one wishes to control this
1822d763cef2SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1823d763cef2SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1824d763cef2SBarry Smith 
1825d763cef2SBarry Smith    Level: advanced
1826d763cef2SBarry Smith 
1827d763cef2SBarry Smith .keywords: TS, timestep, setup
1828d763cef2SBarry Smith 
1829d763cef2SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1830d763cef2SBarry Smith @*/
18317087cfbeSBarry Smith PetscErrorCode  TSSetUp(TS ts)
1832d763cef2SBarry Smith {
1833dfbe8321SBarry Smith   PetscErrorCode ierr;
18346c6b9e74SPeter Brune   DM             dm;
18356c6b9e74SPeter Brune   PetscErrorCode (*func)(SNES,Vec,Vec,void*);
1836d1e9a80fSBarry Smith   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*);
18376c6b9e74SPeter Brune   TSIJacobian    ijac;
18386c6b9e74SPeter Brune   TSRHSJacobian  rhsjac;
1839d763cef2SBarry Smith 
1840d763cef2SBarry Smith   PetscFunctionBegin;
18410700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1842277b19d0SLisandro Dalcin   if (ts->setupcalled) PetscFunctionReturn(0);
1843277b19d0SLisandro Dalcin 
18442c18e0fdSBarry Smith   ts->total_steps = 0;
18457adad957SLisandro Dalcin   if (!((PetscObject)ts)->type_name) {
18469596e0b4SJed Brown     ierr = TSSetType(ts,TSEULER);CHKERRQ(ierr);
1847d763cef2SBarry Smith   }
1848277b19d0SLisandro Dalcin 
1849277b19d0SLisandro Dalcin   if (!ts->vec_sol) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetSolution() first");
1850277b19d0SLisandro Dalcin 
1851c235aa8dSHong Zhang 
1852552698daSJed Brown   ierr = TSGetAdapt(ts,&ts->adapt);CHKERRQ(ierr);
18531c3436cfSJed Brown 
1854e1244c69SJed Brown   if (ts->rhsjacobian.reuse) {
1855e1244c69SJed Brown     Mat Amat,Pmat;
1856e1244c69SJed Brown     SNES snes;
1857e1244c69SJed Brown     ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
1858e1244c69SJed Brown     ierr = SNESGetJacobian(snes,&Amat,&Pmat,NULL,NULL);CHKERRQ(ierr);
1859e1244c69SJed Brown     /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would
1860e1244c69SJed Brown      * have displaced the RHS matrix */
1861e1244c69SJed Brown     if (Amat == ts->Arhs) {
1862e1244c69SJed Brown       ierr = MatDuplicate(ts->Arhs,MAT_DO_NOT_COPY_VALUES,&Amat);CHKERRQ(ierr);
1863e1244c69SJed Brown       ierr = SNESSetJacobian(snes,Amat,NULL,NULL,NULL);CHKERRQ(ierr);
1864e1244c69SJed Brown       ierr = MatDestroy(&Amat);CHKERRQ(ierr);
1865e1244c69SJed Brown     }
1866e1244c69SJed Brown     if (Pmat == ts->Brhs) {
1867e1244c69SJed Brown       ierr = MatDuplicate(ts->Brhs,MAT_DO_NOT_COPY_VALUES,&Pmat);CHKERRQ(ierr);
1868e1244c69SJed Brown       ierr = SNESSetJacobian(snes,NULL,Pmat,NULL,NULL);CHKERRQ(ierr);
1869e1244c69SJed Brown       ierr = MatDestroy(&Pmat);CHKERRQ(ierr);
1870e1244c69SJed Brown     }
1871e1244c69SJed Brown   }
1872277b19d0SLisandro Dalcin   if (ts->ops->setup) {
1873000e7ae3SMatthew Knepley     ierr = (*ts->ops->setup)(ts);CHKERRQ(ierr);
1874277b19d0SLisandro Dalcin   }
1875277b19d0SLisandro Dalcin 
18766c6b9e74SPeter Brune   /* in the case where we've set a DMTSFunction or what have you, we need the default SNESFunction
18776c6b9e74SPeter Brune    to be set right but can't do it elsewhere due to the overreliance on ctx=ts.
18786c6b9e74SPeter Brune    */
18796c6b9e74SPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
18800298fd71SBarry Smith   ierr = DMSNESGetFunction(dm,&func,NULL);CHKERRQ(ierr);
18816c6b9e74SPeter Brune   if (!func) {
18826c6b9e74SPeter Brune     ierr =DMSNESSetFunction(dm,SNESTSFormFunction,ts);CHKERRQ(ierr);
18836c6b9e74SPeter Brune   }
18846c6b9e74SPeter 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.
18856c6b9e74SPeter Brune      Otherwise, the SNES will use coloring internally to form the Jacobian.
18866c6b9e74SPeter Brune    */
18870298fd71SBarry Smith   ierr = DMSNESGetJacobian(dm,&jac,NULL);CHKERRQ(ierr);
18880298fd71SBarry Smith   ierr = DMTSGetIJacobian(dm,&ijac,NULL);CHKERRQ(ierr);
18890298fd71SBarry Smith   ierr = DMTSGetRHSJacobian(dm,&rhsjac,NULL);CHKERRQ(ierr);
18906c6b9e74SPeter Brune   if (!jac && (ijac || rhsjac)) {
18916c6b9e74SPeter Brune     ierr = DMSNESSetJacobian(dm,SNESTSFormJacobian,ts);CHKERRQ(ierr);
18926c6b9e74SPeter Brune   }
1893277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_TRUE;
1894277b19d0SLisandro Dalcin   PetscFunctionReturn(0);
1895277b19d0SLisandro Dalcin }
1896277b19d0SLisandro Dalcin 
1897277b19d0SLisandro Dalcin #undef __FUNCT__
1898f6a906c0SBarry Smith #define __FUNCT__ "TSAdjointSetUp"
1899f6a906c0SBarry Smith /*@
1900f6a906c0SBarry Smith    TSAdjointSetUp - Sets up the internal data structures for the later use
1901f6a906c0SBarry Smith    of an adjoint solver
1902f6a906c0SBarry Smith 
1903f6a906c0SBarry Smith    Collective on TS
1904f6a906c0SBarry Smith 
1905f6a906c0SBarry Smith    Input Parameter:
1906f6a906c0SBarry Smith .  ts - the TS context obtained from TSCreate()
1907f6a906c0SBarry Smith 
1908f6a906c0SBarry Smith    Notes:
1909f6a906c0SBarry Smith    For basic use of the TS solvers the user need not explicitly call
1910f6a906c0SBarry Smith    TSSetUp(), since these actions will automatically occur during
1911f6a906c0SBarry Smith    the call to TSStep().  However, if one wishes to control this
1912f6a906c0SBarry Smith    phase separately, TSSetUp() should be called after TSCreate()
1913f6a906c0SBarry Smith    and optional routines of the form TSSetXXX(), but before TSStep().
1914f6a906c0SBarry Smith 
1915f6a906c0SBarry Smith    Level: advanced
1916f6a906c0SBarry Smith 
1917f6a906c0SBarry Smith .keywords: TS, timestep, setup
1918f6a906c0SBarry Smith 
1919f6a906c0SBarry Smith .seealso: TSCreate(), TSStep(), TSDestroy()
1920f6a906c0SBarry Smith @*/
1921f6a906c0SBarry Smith PetscErrorCode  TSAdjointSetUp(TS ts)
1922f6a906c0SBarry Smith {
1923f6a906c0SBarry Smith   PetscErrorCode ierr;
1924f6a906c0SBarry Smith 
1925f6a906c0SBarry Smith   PetscFunctionBegin;
1926f6a906c0SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1927f6a906c0SBarry Smith   if (ts->adjointsetupcalled) PetscFunctionReturn(0);
1928419a887dSBarry Smith   if (!ts->vecs_sensi) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call TSAdjointSetGradients() first");
192942f2b339SBarry Smith   if (ts->ops->adjointsetup) {
193042f2b339SBarry Smith     ierr = (*ts->ops->adjointsetup)(ts);CHKERRQ(ierr);
1931f6a906c0SBarry Smith   }
1932f6a906c0SBarry Smith   ts->adjointsetupcalled = PETSC_TRUE;
1933f6a906c0SBarry Smith   PetscFunctionReturn(0);
1934f6a906c0SBarry Smith }
1935f6a906c0SBarry Smith 
1936f6a906c0SBarry Smith #undef __FUNCT__
1937277b19d0SLisandro Dalcin #define __FUNCT__ "TSReset"
1938277b19d0SLisandro Dalcin /*@
1939277b19d0SLisandro Dalcin    TSReset - Resets a TS context and removes any allocated Vecs and Mats.
1940277b19d0SLisandro Dalcin 
1941277b19d0SLisandro Dalcin    Collective on TS
1942277b19d0SLisandro Dalcin 
1943277b19d0SLisandro Dalcin    Input Parameter:
1944277b19d0SLisandro Dalcin .  ts - the TS context obtained from TSCreate()
1945277b19d0SLisandro Dalcin 
1946277b19d0SLisandro Dalcin    Level: beginner
1947277b19d0SLisandro Dalcin 
1948277b19d0SLisandro Dalcin .keywords: TS, timestep, reset
1949277b19d0SLisandro Dalcin 
1950277b19d0SLisandro Dalcin .seealso: TSCreate(), TSSetup(), TSDestroy()
1951277b19d0SLisandro Dalcin @*/
1952277b19d0SLisandro Dalcin PetscErrorCode  TSReset(TS ts)
1953277b19d0SLisandro Dalcin {
1954277b19d0SLisandro Dalcin   PetscErrorCode ierr;
1955277b19d0SLisandro Dalcin 
1956277b19d0SLisandro Dalcin   PetscFunctionBegin;
1957277b19d0SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
1958b18ea86cSHong Zhang 
1959277b19d0SLisandro Dalcin   if (ts->ops->reset) {
1960277b19d0SLisandro Dalcin     ierr = (*ts->ops->reset)(ts);CHKERRQ(ierr);
1961277b19d0SLisandro Dalcin   }
1962277b19d0SLisandro Dalcin   if (ts->snes) {ierr = SNESReset(ts->snes);CHKERRQ(ierr);}
1963bbd56ea5SKarl Rupp 
19644e684422SJed Brown   ierr = MatDestroy(&ts->Arhs);CHKERRQ(ierr);
19654e684422SJed Brown   ierr = MatDestroy(&ts->Brhs);CHKERRQ(ierr);
1966214bc6a2SJed Brown   ierr = VecDestroy(&ts->Frhs);CHKERRQ(ierr);
19676bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
1968e3d84a46SJed Brown   ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
1969e3d84a46SJed Brown   ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
197038637c2eSJed Brown   ierr = VecDestroyVecs(ts->nwork,&ts->work);CHKERRQ(ierr);
1971f8fee759SHong Zhang   ts->vecs_sensi = 0;
1972f8fee759SHong Zhang   ts->vecs_sensip = 0;
1973ad8e2604SHong Zhang   ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
19742c39e106SBarry Smith   ierr = VecDestroy(&ts->vec_costintegral);CHKERRQ(ierr);
197536eaed60SHong Zhang   ierr = VecDestroy(&ts->vec_costintegrand);CHKERRQ(ierr);
1976277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
1977d763cef2SBarry Smith   PetscFunctionReturn(0);
1978d763cef2SBarry Smith }
1979d763cef2SBarry Smith 
19804a2ae208SSatish Balay #undef __FUNCT__
19814a2ae208SSatish Balay #define __FUNCT__ "TSDestroy"
1982d8e5e3e6SSatish Balay /*@
1983d763cef2SBarry Smith    TSDestroy - Destroys the timestepper context that was created
1984d763cef2SBarry Smith    with TSCreate().
1985d763cef2SBarry Smith 
1986d763cef2SBarry Smith    Collective on TS
1987d763cef2SBarry Smith 
1988d763cef2SBarry Smith    Input Parameter:
1989d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
1990d763cef2SBarry Smith 
1991d763cef2SBarry Smith    Level: beginner
1992d763cef2SBarry Smith 
1993d763cef2SBarry Smith .keywords: TS, timestepper, destroy
1994d763cef2SBarry Smith 
1995d763cef2SBarry Smith .seealso: TSCreate(), TSSetUp(), TSSolve()
1996d763cef2SBarry Smith @*/
19976bf464f9SBarry Smith PetscErrorCode  TSDestroy(TS *ts)
1998d763cef2SBarry Smith {
19996849ba73SBarry Smith   PetscErrorCode ierr;
2000d763cef2SBarry Smith 
2001d763cef2SBarry Smith   PetscFunctionBegin;
20026bf464f9SBarry Smith   if (!*ts) PetscFunctionReturn(0);
20036bf464f9SBarry Smith   PetscValidHeaderSpecific((*ts),TS_CLASSID,1);
20046bf464f9SBarry Smith   if (--((PetscObject)(*ts))->refct > 0) {*ts = 0; PetscFunctionReturn(0);}
2005d763cef2SBarry Smith 
20066bf464f9SBarry Smith   ierr = TSReset((*ts));CHKERRQ(ierr);
2007277b19d0SLisandro Dalcin 
2008e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
2009e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*ts);CHKERRQ(ierr);
20106bf464f9SBarry Smith   if ((*ts)->ops->destroy) {ierr = (*(*ts)->ops->destroy)((*ts));CHKERRQ(ierr);}
20116d4c513bSLisandro Dalcin 
2012bc952696SBarry Smith   ierr = TSTrajectoryDestroy(&(*ts)->trajectory);CHKERRQ(ierr);
2013bc952696SBarry Smith 
201484df9cb4SJed Brown   ierr = TSAdaptDestroy(&(*ts)->adapt);CHKERRQ(ierr);
2015aeb4809dSShri Abhyankar   if ((*ts)->event) {
2016aeb4809dSShri Abhyankar     ierr = TSEventMonitorDestroy(&(*ts)->event);CHKERRQ(ierr);
2017aeb4809dSShri Abhyankar   }
20186bf464f9SBarry Smith   ierr = SNESDestroy(&(*ts)->snes);CHKERRQ(ierr);
20196bf464f9SBarry Smith   ierr = DMDestroy(&(*ts)->dm);CHKERRQ(ierr);
20206bf464f9SBarry Smith   ierr = TSMonitorCancel((*ts));CHKERRQ(ierr);
20216d4c513bSLisandro Dalcin 
2022a79aaaedSSatish Balay   ierr = PetscHeaderDestroy(ts);CHKERRQ(ierr);
2023d763cef2SBarry Smith   PetscFunctionReturn(0);
2024d763cef2SBarry Smith }
2025d763cef2SBarry Smith 
20264a2ae208SSatish Balay #undef __FUNCT__
20274a2ae208SSatish Balay #define __FUNCT__ "TSGetSNES"
2028d8e5e3e6SSatish Balay /*@
2029d763cef2SBarry Smith    TSGetSNES - Returns the SNES (nonlinear solver) associated with
2030d763cef2SBarry Smith    a TS (timestepper) context. Valid only for nonlinear problems.
2031d763cef2SBarry Smith 
2032d763cef2SBarry Smith    Not Collective, but SNES is parallel if TS is parallel
2033d763cef2SBarry Smith 
2034d763cef2SBarry Smith    Input Parameter:
2035d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2036d763cef2SBarry Smith 
2037d763cef2SBarry Smith    Output Parameter:
2038d763cef2SBarry Smith .  snes - the nonlinear solver context
2039d763cef2SBarry Smith 
2040d763cef2SBarry Smith    Notes:
2041d763cef2SBarry Smith    The user can then directly manipulate the SNES context to set various
2042d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
204394b7f48cSBarry Smith    KSP, KSP, and PC contexts as well.
2044d763cef2SBarry Smith 
2045d763cef2SBarry Smith    TSGetSNES() does not work for integrators that do not use SNES; in
20460298fd71SBarry Smith    this case TSGetSNES() returns NULL in snes.
2047d763cef2SBarry Smith 
2048d763cef2SBarry Smith    Level: beginner
2049d763cef2SBarry Smith 
2050d763cef2SBarry Smith .keywords: timestep, get, SNES
2051d763cef2SBarry Smith @*/
20527087cfbeSBarry Smith PetscErrorCode  TSGetSNES(TS ts,SNES *snes)
2053d763cef2SBarry Smith {
2054d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2055d372ba47SLisandro Dalcin 
2056d763cef2SBarry Smith   PetscFunctionBegin;
20570700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
20584482741eSBarry Smith   PetscValidPointer(snes,2);
2059d372ba47SLisandro Dalcin   if (!ts->snes) {
2060ce94432eSBarry Smith     ierr = SNESCreate(PetscObjectComm((PetscObject)ts),&ts->snes);CHKERRQ(ierr);
20610298fd71SBarry Smith     ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
20623bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->snes);CHKERRQ(ierr);
2063d372ba47SLisandro Dalcin     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr);
2064496e6a7aSJed Brown     if (ts->dm) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
20659e2a6581SJed Brown     if (ts->problem_type == TS_LINEAR) {
20669e2a6581SJed Brown       ierr = SNESSetType(ts->snes,SNESKSPONLY);CHKERRQ(ierr);
20679e2a6581SJed Brown     }
2068d372ba47SLisandro Dalcin   }
2069d763cef2SBarry Smith   *snes = ts->snes;
2070d763cef2SBarry Smith   PetscFunctionReturn(0);
2071d763cef2SBarry Smith }
2072d763cef2SBarry Smith 
20734a2ae208SSatish Balay #undef __FUNCT__
2074deb2cd25SJed Brown #define __FUNCT__ "TSSetSNES"
2075deb2cd25SJed Brown /*@
2076deb2cd25SJed Brown    TSSetSNES - Set the SNES (nonlinear solver) to be used by the timestepping context
2077deb2cd25SJed Brown 
2078deb2cd25SJed Brown    Collective
2079deb2cd25SJed Brown 
2080deb2cd25SJed Brown    Input Parameter:
2081deb2cd25SJed Brown +  ts - the TS context obtained from TSCreate()
2082deb2cd25SJed Brown -  snes - the nonlinear solver context
2083deb2cd25SJed Brown 
2084deb2cd25SJed Brown    Notes:
2085deb2cd25SJed Brown    Most users should have the TS created by calling TSGetSNES()
2086deb2cd25SJed Brown 
2087deb2cd25SJed Brown    Level: developer
2088deb2cd25SJed Brown 
2089deb2cd25SJed Brown .keywords: timestep, set, SNES
2090deb2cd25SJed Brown @*/
2091deb2cd25SJed Brown PetscErrorCode TSSetSNES(TS ts,SNES snes)
2092deb2cd25SJed Brown {
2093deb2cd25SJed Brown   PetscErrorCode ierr;
2094d1e9a80fSBarry Smith   PetscErrorCode (*func)(SNES,Vec,Mat,Mat,void*);
2095deb2cd25SJed Brown 
2096deb2cd25SJed Brown   PetscFunctionBegin;
2097deb2cd25SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2098deb2cd25SJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,2);
2099deb2cd25SJed Brown   ierr = PetscObjectReference((PetscObject)snes);CHKERRQ(ierr);
2100deb2cd25SJed Brown   ierr = SNESDestroy(&ts->snes);CHKERRQ(ierr);
2101bbd56ea5SKarl Rupp 
2102deb2cd25SJed Brown   ts->snes = snes;
2103bbd56ea5SKarl Rupp 
21040298fd71SBarry Smith   ierr = SNESSetFunction(ts->snes,NULL,SNESTSFormFunction,ts);CHKERRQ(ierr);
21050298fd71SBarry Smith   ierr = SNESGetJacobian(ts->snes,NULL,NULL,&func,NULL);CHKERRQ(ierr);
2106740132f1SEmil Constantinescu   if (func == SNESTSFormJacobian) {
21070298fd71SBarry Smith     ierr = SNESSetJacobian(ts->snes,NULL,NULL,SNESTSFormJacobian,ts);CHKERRQ(ierr);
2108740132f1SEmil Constantinescu   }
2109deb2cd25SJed Brown   PetscFunctionReturn(0);
2110deb2cd25SJed Brown }
2111deb2cd25SJed Brown 
2112deb2cd25SJed Brown #undef __FUNCT__
211394b7f48cSBarry Smith #define __FUNCT__ "TSGetKSP"
2114d8e5e3e6SSatish Balay /*@
211594b7f48cSBarry Smith    TSGetKSP - Returns the KSP (linear solver) associated with
2116d763cef2SBarry Smith    a TS (timestepper) context.
2117d763cef2SBarry Smith 
211894b7f48cSBarry Smith    Not Collective, but KSP is parallel if TS is parallel
2119d763cef2SBarry Smith 
2120d763cef2SBarry Smith    Input Parameter:
2121d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2122d763cef2SBarry Smith 
2123d763cef2SBarry Smith    Output Parameter:
212494b7f48cSBarry Smith .  ksp - the nonlinear solver context
2125d763cef2SBarry Smith 
2126d763cef2SBarry Smith    Notes:
212794b7f48cSBarry Smith    The user can then directly manipulate the KSP context to set various
2128d763cef2SBarry Smith    options, etc.  Likewise, the user can then extract and manipulate the
2129d763cef2SBarry Smith    KSP and PC contexts as well.
2130d763cef2SBarry Smith 
213194b7f48cSBarry Smith    TSGetKSP() does not work for integrators that do not use KSP;
21320298fd71SBarry Smith    in this case TSGetKSP() returns NULL in ksp.
2133d763cef2SBarry Smith 
2134d763cef2SBarry Smith    Level: beginner
2135d763cef2SBarry Smith 
213694b7f48cSBarry Smith .keywords: timestep, get, KSP
2137d763cef2SBarry Smith @*/
21387087cfbeSBarry Smith PetscErrorCode  TSGetKSP(TS ts,KSP *ksp)
2139d763cef2SBarry Smith {
2140d372ba47SLisandro Dalcin   PetscErrorCode ierr;
2141089b2837SJed Brown   SNES           snes;
2142d372ba47SLisandro Dalcin 
2143d763cef2SBarry Smith   PetscFunctionBegin;
21440700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
21454482741eSBarry Smith   PetscValidPointer(ksp,2);
214617186662SBarry Smith   if (!((PetscObject)ts)->type_name) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"KSP is not created yet. Call TSSetType() first");
2147e32f2f54SBarry Smith   if (ts->problem_type != TS_LINEAR) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Linear only; use TSGetSNES()");
2148089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
2149089b2837SJed Brown   ierr = SNESGetKSP(snes,ksp);CHKERRQ(ierr);
2150d763cef2SBarry Smith   PetscFunctionReturn(0);
2151d763cef2SBarry Smith }
2152d763cef2SBarry Smith 
2153d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */
2154d763cef2SBarry Smith 
21554a2ae208SSatish Balay #undef __FUNCT__
2156adb62b0dSMatthew Knepley #define __FUNCT__ "TSGetDuration"
2157adb62b0dSMatthew Knepley /*@
2158adb62b0dSMatthew Knepley    TSGetDuration - Gets the maximum number of timesteps to use and
2159adb62b0dSMatthew Knepley    maximum time for iteration.
2160adb62b0dSMatthew Knepley 
21613f9fe445SBarry Smith    Not Collective
2162adb62b0dSMatthew Knepley 
2163adb62b0dSMatthew Knepley    Input Parameters:
2164adb62b0dSMatthew Knepley +  ts       - the TS context obtained from TSCreate()
21650298fd71SBarry Smith .  maxsteps - maximum number of iterations to use, or NULL
21660298fd71SBarry Smith -  maxtime  - final time to iterate to, or NULL
2167adb62b0dSMatthew Knepley 
2168adb62b0dSMatthew Knepley    Level: intermediate
2169adb62b0dSMatthew Knepley 
2170adb62b0dSMatthew Knepley .keywords: TS, timestep, get, maximum, iterations, time
2171adb62b0dSMatthew Knepley @*/
21727087cfbeSBarry Smith PetscErrorCode  TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime)
2173adb62b0dSMatthew Knepley {
2174adb62b0dSMatthew Knepley   PetscFunctionBegin;
21750700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2176abc0a331SBarry Smith   if (maxsteps) {
21774482741eSBarry Smith     PetscValidIntPointer(maxsteps,2);
2178adb62b0dSMatthew Knepley     *maxsteps = ts->max_steps;
2179adb62b0dSMatthew Knepley   }
2180abc0a331SBarry Smith   if (maxtime) {
21814482741eSBarry Smith     PetscValidScalarPointer(maxtime,3);
2182adb62b0dSMatthew Knepley     *maxtime = ts->max_time;
2183adb62b0dSMatthew Knepley   }
2184adb62b0dSMatthew Knepley   PetscFunctionReturn(0);
2185adb62b0dSMatthew Knepley }
2186adb62b0dSMatthew Knepley 
2187adb62b0dSMatthew Knepley #undef __FUNCT__
21884a2ae208SSatish Balay #define __FUNCT__ "TSSetDuration"
2189d763cef2SBarry Smith /*@
2190d763cef2SBarry Smith    TSSetDuration - Sets the maximum number of timesteps to use and
2191d763cef2SBarry Smith    maximum time for iteration.
2192d763cef2SBarry Smith 
21933f9fe445SBarry Smith    Logically Collective on TS
2194d763cef2SBarry Smith 
2195d763cef2SBarry Smith    Input Parameters:
2196d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2197d763cef2SBarry Smith .  maxsteps - maximum number of iterations to use
2198d763cef2SBarry Smith -  maxtime - final time to iterate to
2199d763cef2SBarry Smith 
2200d763cef2SBarry Smith    Options Database Keys:
2201d763cef2SBarry Smith .  -ts_max_steps <maxsteps> - Sets maxsteps
22023bca7d26SBarry Smith .  -ts_final_time <maxtime> - Sets maxtime
2203d763cef2SBarry Smith 
2204d763cef2SBarry Smith    Notes:
2205d763cef2SBarry Smith    The default maximum number of iterations is 5000. Default time is 5.0
2206d763cef2SBarry Smith 
2207d763cef2SBarry Smith    Level: intermediate
2208d763cef2SBarry Smith 
2209d763cef2SBarry Smith .keywords: TS, timestep, set, maximum, iterations
2210a43b19c4SJed Brown 
2211a43b19c4SJed Brown .seealso: TSSetExactFinalTime()
2212d763cef2SBarry Smith @*/
22137087cfbeSBarry Smith PetscErrorCode  TSSetDuration(TS ts,PetscInt maxsteps,PetscReal maxtime)
2214d763cef2SBarry Smith {
2215d763cef2SBarry Smith   PetscFunctionBegin;
22160700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2217c5eb9154SBarry Smith   PetscValidLogicalCollectiveInt(ts,maxsteps,2);
2218c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,maxtime,2);
221939b7ec4bSSean Farley   if (maxsteps >= 0) ts->max_steps = maxsteps;
222039b7ec4bSSean Farley   if (maxtime != PETSC_DEFAULT) ts->max_time = maxtime;
2221d763cef2SBarry Smith   PetscFunctionReturn(0);
2222d763cef2SBarry Smith }
2223d763cef2SBarry Smith 
22244a2ae208SSatish Balay #undef __FUNCT__
22254a2ae208SSatish Balay #define __FUNCT__ "TSSetSolution"
2226d763cef2SBarry Smith /*@
2227d763cef2SBarry Smith    TSSetSolution - Sets the initial solution vector
2228d763cef2SBarry Smith    for use by the TS routines.
2229d763cef2SBarry Smith 
22303f9fe445SBarry Smith    Logically Collective on TS and Vec
2231d763cef2SBarry Smith 
2232d763cef2SBarry Smith    Input Parameters:
2233d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
22340910c330SBarry Smith -  u - the solution vector
2235d763cef2SBarry Smith 
2236d763cef2SBarry Smith    Level: beginner
2237d763cef2SBarry Smith 
2238d763cef2SBarry Smith .keywords: TS, timestep, set, solution, initial conditions
2239d763cef2SBarry Smith @*/
22400910c330SBarry Smith PetscErrorCode  TSSetSolution(TS ts,Vec u)
2241d763cef2SBarry Smith {
22428737fe31SLisandro Dalcin   PetscErrorCode ierr;
2243496e6a7aSJed Brown   DM             dm;
22448737fe31SLisandro Dalcin 
2245d763cef2SBarry Smith   PetscFunctionBegin;
22460700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
22470910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,2);
22480910c330SBarry Smith   ierr = PetscObjectReference((PetscObject)u);CHKERRQ(ierr);
22496bf464f9SBarry Smith   ierr = VecDestroy(&ts->vec_sol);CHKERRQ(ierr);
2250bbd56ea5SKarl Rupp 
22510910c330SBarry Smith   ts->vec_sol = u;
2252bbd56ea5SKarl Rupp 
2253496e6a7aSJed Brown   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
22540910c330SBarry Smith   ierr = DMShellSetGlobalVector(dm,u);CHKERRQ(ierr);
2255d763cef2SBarry Smith   PetscFunctionReturn(0);
2256d763cef2SBarry Smith }
2257d763cef2SBarry Smith 
2258e74ef692SMatthew Knepley #undef __FUNCT__
225973b18844SBarry Smith #define __FUNCT__ "TSAdjointSetSteps"
226073b18844SBarry Smith /*@
226173b18844SBarry Smith    TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time
226273b18844SBarry Smith 
226373b18844SBarry Smith    Logically Collective on TS
226473b18844SBarry Smith 
226573b18844SBarry Smith    Input Parameters:
226673b18844SBarry Smith +  ts - the TS context obtained from TSCreate()
226773b18844SBarry Smith .  steps - number of steps to use
226873b18844SBarry Smith 
226973b18844SBarry Smith    Level: intermediate
227073b18844SBarry Smith 
227173b18844SBarry Smith    Notes: Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this
227273b18844SBarry Smith           so as to integrate back to less than the original timestep
227373b18844SBarry Smith 
227473b18844SBarry Smith .keywords: TS, timestep, set, maximum, iterations
227573b18844SBarry Smith 
227673b18844SBarry Smith .seealso: TSSetExactFinalTime()
227773b18844SBarry Smith @*/
227873b18844SBarry Smith PetscErrorCode  TSAdjointSetSteps(TS ts,PetscInt steps)
227973b18844SBarry Smith {
228073b18844SBarry Smith   PetscFunctionBegin;
228173b18844SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
228273b18844SBarry Smith   PetscValidLogicalCollectiveInt(ts,steps,2);
228373b18844SBarry Smith   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps");
228473b18844SBarry 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");
228573b18844SBarry Smith   ts->adjoint_max_steps = steps;
228673b18844SBarry Smith   PetscFunctionReturn(0);
228773b18844SBarry Smith }
228873b18844SBarry Smith 
228973b18844SBarry Smith #undef __FUNCT__
2290419a887dSBarry Smith #define __FUNCT__ "TSAdjointSetGradients"
2291c235aa8dSHong Zhang /*@
2292419a887dSBarry 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.
2293c235aa8dSHong Zhang 
2294c235aa8dSHong Zhang    Logically Collective on TS and Vec
2295c235aa8dSHong Zhang 
2296c235aa8dSHong Zhang    Input Parameters:
2297c235aa8dSHong Zhang +  ts - the TS context obtained from TSCreate()
2298419a887dSBarry Smith .  u - gradients with respect to the initial condition variables
2299419a887dSBarry Smith -  w - gradients with respect to the parameters
2300c235aa8dSHong Zhang 
2301c235aa8dSHong Zhang    Level: beginner
2302c235aa8dSHong Zhang 
2303c235aa8dSHong Zhang .keywords: TS, timestep, set, sensitivity, initial conditions
2304c235aa8dSHong Zhang @*/
2305419a887dSBarry Smith PetscErrorCode  TSAdjointSetGradients(TS ts,PetscInt numberadjs,Vec *u,Vec *w)
2306c235aa8dSHong Zhang {
2307c235aa8dSHong Zhang   PetscFunctionBegin;
2308c235aa8dSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2309b18ea86cSHong Zhang   PetscValidPointer(u,2);
2310b18ea86cSHong Zhang   ts->vecs_sensi  = u;
2311419a887dSBarry Smith   ts->vecs_sensip = w;
2312b18ea86cSHong Zhang   ts->numberadjs  = numberadjs;
2313ad8e2604SHong Zhang   PetscFunctionReturn(0);
2314ad8e2604SHong Zhang }
2315ad8e2604SHong Zhang 
2316ad8e2604SHong Zhang #undef __FUNCT__
23175bf8c567SBarry Smith #define __FUNCT__ "TSAdjointSetRHSJacobian"
2318ad8e2604SHong Zhang /*@C
23195bf8c567SBarry Smith   TSAdjointSetRHSJacobian - Sets the function that computes the Jacobian w.r.t. parameters.
2320ad8e2604SHong Zhang 
2321ad8e2604SHong Zhang   Logically Collective on TS
2322ad8e2604SHong Zhang 
2323ad8e2604SHong Zhang   Input Parameters:
2324ad8e2604SHong Zhang + ts   - The TS context obtained from TSCreate()
2325ad8e2604SHong Zhang - func - The function
2326ad8e2604SHong Zhang 
2327ad8e2604SHong Zhang   Calling sequence of func:
232805755b9cSHong Zhang $ func (TS ts,PetscReal t,Vec u,Mat A,void *ctx);
232905755b9cSHong Zhang +   t - current timestep
233005755b9cSHong Zhang .   u - input vector
233105755b9cSHong Zhang .   A - output matrix
233205755b9cSHong Zhang -   ctx - [optional] user-defined function context
2333ad8e2604SHong Zhang 
2334ad8e2604SHong Zhang   Level: intermediate
2335ad8e2604SHong Zhang 
2336ad8e2604SHong Zhang .keywords: TS, sensitivity
2337ad8e2604SHong Zhang .seealso:
2338ad8e2604SHong Zhang @*/
23395bf8c567SBarry Smith PetscErrorCode  TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
2340ad8e2604SHong Zhang {
2341ad8e2604SHong Zhang   PetscErrorCode ierr;
2342ad8e2604SHong Zhang 
2343ad8e2604SHong Zhang   PetscFunctionBegin;
2344ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2345ad8e2604SHong Zhang   if (Amat) PetscValidHeaderSpecific(Amat,MAT_CLASSID,2);
2346ad8e2604SHong Zhang 
2347ad8e2604SHong Zhang   ts->rhsjacobianp    = func;
2348ad8e2604SHong Zhang   ts->rhsjacobianpctx = ctx;
2349ad8e2604SHong Zhang   if(Amat) {
2350ad8e2604SHong Zhang     ierr = PetscObjectReference((PetscObject)Amat);CHKERRQ(ierr);
2351ad8e2604SHong Zhang     ierr = MatDestroy(&ts->Jacp);CHKERRQ(ierr);
2352ad8e2604SHong Zhang 
2353ad8e2604SHong Zhang     ts->Jacp = Amat;
2354ad8e2604SHong Zhang   }
2355ad8e2604SHong Zhang   PetscFunctionReturn(0);
2356ad8e2604SHong Zhang }
2357ad8e2604SHong Zhang 
2358ad8e2604SHong Zhang #undef __FUNCT__
23595bf8c567SBarry Smith #define __FUNCT__ "TSAdjointComputeRHSJacobian"
2360ad8e2604SHong Zhang /*@
23615bf8c567SBarry Smith   TSAdjointComputeRHSJacobian - Runs the user-defined Jacobian function.
2362ad8e2604SHong Zhang 
2363ad8e2604SHong Zhang   Collective on TS
2364ad8e2604SHong Zhang 
2365ad8e2604SHong Zhang   Input Parameters:
2366ad8e2604SHong Zhang . ts   - The TS context obtained from TSCreate()
2367ad8e2604SHong Zhang 
2368ad8e2604SHong Zhang   Level: developer
2369ad8e2604SHong Zhang 
2370ad8e2604SHong Zhang .keywords: TS, sensitivity
23715bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian()
2372ad8e2604SHong Zhang @*/
23735bf8c567SBarry Smith PetscErrorCode  TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec X,Mat Amat)
2374ad8e2604SHong Zhang {
2375ad8e2604SHong Zhang   PetscErrorCode ierr;
2376ad8e2604SHong Zhang 
2377ad8e2604SHong Zhang   PetscFunctionBegin;
2378ad8e2604SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2379ad8e2604SHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
2380ad8e2604SHong Zhang   PetscValidPointer(Amat,4);
2381ad8e2604SHong Zhang 
2382ad8e2604SHong Zhang   PetscStackPush("TS user JacobianP function for sensitivity analysis");
2383ad8e2604SHong Zhang   ierr = (*ts->rhsjacobianp)(ts,t,X,Amat,ts->rhsjacobianpctx); CHKERRQ(ierr);
2384ad8e2604SHong Zhang   PetscStackPop;
2385ad8e2604SHong Zhang 
2386c235aa8dSHong Zhang   PetscFunctionReturn(0);
2387c235aa8dSHong Zhang }
2388c235aa8dSHong Zhang 
2389c235aa8dSHong Zhang #undef __FUNCT__
2390d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointSetCostIntegrand"
23916fd0887fSHong Zhang /*@C
23928a2f769dSBarry Smith     TSAdjointSetCostIntegrand - Sets the routine for evaluating the integral term in a cost function
23936fd0887fSHong Zhang 
23946fd0887fSHong Zhang     Logically Collective on TS
23956fd0887fSHong Zhang 
23966fd0887fSHong Zhang     Input Parameters:
23976fd0887fSHong Zhang +   ts - the TS context obtained from TSCreate()
23988a2f769dSBarry Smith .   numberadjs - number of gradients to be computed
23996fd0887fSHong Zhang .   fq - routine for evaluating the right-hand-side function
2400b612ec54SBarry Smith .   drdy - array of vectors to contain the gradient of the r's with respect to y, NULL if not a function of y
2401b612ec54SBarry Smith .   drdyf - function that computes the gradients of the r's with respect to y,NULL if not a function y
2402b612ec54SBarry Smith .   drdp - array of vectors to contain the gradient of the r's with respect to p, NULL if not a function of p
2403b612ec54SBarry Smith .   drdpf - function that computes the gradients of the r's with respect to p, NULL if not a function of p
2404b612ec54SBarry Smith -   ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL)
24056fd0887fSHong Zhang 
2406b612ec54SBarry Smith     Calling sequence of fq:
2407a2ae3dd2SHong Zhang $     TSCostIntegrand(TS ts,PetscReal t,Vec u,PetscReal *f,void *ctx);
24086fd0887fSHong Zhang 
24096fd0887fSHong Zhang +   t - current timestep
24106fd0887fSHong Zhang .   u - input vector
241136eaed60SHong Zhang .   f - function vector
24126fd0887fSHong Zhang -   ctx - [optional] user-defined function context
24136fd0887fSHong Zhang 
2414b612ec54SBarry Smith    Calling sequence of drdyf:
2415b612ec54SBarry Smith $    PetscErroCode drdyf(TS ts,PetscReal t,Vec U,Vec *drdy,void *ctx);
2416b612ec54SBarry Smith 
2417b612ec54SBarry Smith    Calling sequence of drdpf:
2418b612ec54SBarry Smith $    PetscErroCode drdpf(TS ts,PetscReal t,Vec U,Vec *drdp,void *ctx);
2419b612ec54SBarry Smith 
2420b612ec54SBarry Smith     Level: intermediate
24216fd0887fSHong Zhang 
24226fd0887fSHong Zhang .keywords: TS, sensitivity analysis, timestep, set, quadrature, function
24236fd0887fSHong Zhang 
24245bf8c567SBarry Smith .seealso: TSAdjointSetRHSJacobian(),TSAdjointGetGradients(), TSAdjointSetGradients()
24256fd0887fSHong Zhang @*/
24268a2f769dSBarry Smith PetscErrorCode  TSAdjointSetCostIntegrand(TS ts,PetscInt numberadjs,          PetscErrorCode (*fq)(TS,PetscReal,Vec,Vec,void*),
2427b612ec54SBarry Smith                                                                     Vec *drdy,PetscErrorCode (*drdyf)(TS,PetscReal,Vec,Vec*,void*),
2428b612ec54SBarry Smith                                                                     Vec *drdp,PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),void *ctx)
24296fd0887fSHong Zhang {
24306fd0887fSHong Zhang   PetscErrorCode ierr;
24316fd0887fSHong Zhang 
24326fd0887fSHong Zhang   PetscFunctionBegin;
24336fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2434419a887dSBarry Smith   if (!ts->numberadjs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Call TSAdjointSetGradients() first so that the number of cost functions can be determined.");
2435419a887dSBarry 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()");
24366fd0887fSHong Zhang 
24378a2f769dSBarry Smith   ierr                  = VecCreateSeq(PETSC_COMM_SELF,numberadjs,&ts->vec_costintegral);CHKERRQ(ierr);
24382c39e106SBarry Smith   ierr                  = VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);CHKERRQ(ierr);
2439e4680132SHong Zhang   ts->costintegrand     = fq;
244005755b9cSHong Zhang   ts->costintegrandctx  = ctx;
24416fd0887fSHong Zhang 
2442b612ec54SBarry Smith   ts->drdyfunction    = drdyf;
2443b612ec54SBarry Smith   ts->vecs_drdy       = drdy;
2444b612ec54SBarry Smith 
2445b612ec54SBarry Smith   ts->drdpfunction    = drdpf;
2446b612ec54SBarry Smith   ts->vecs_drdp       = drdp;
2447b612ec54SBarry Smith 
24486fd0887fSHong Zhang   PetscFunctionReturn(0);
24496fd0887fSHong Zhang }
24506fd0887fSHong Zhang 
24516fd0887fSHong Zhang #undef __FUNCT__
24522c39e106SBarry Smith #define __FUNCT__ "TSAdjointGetCostIntegral"
245336eaed60SHong Zhang /*@
24542c39e106SBarry Smith    TSAdjointGetCostIntegral - Returns the values of the integral term in the cost functions.
245536eaed60SHong Zhang    It is valid to call the routine after a backward run.
245636eaed60SHong Zhang 
245736eaed60SHong Zhang    Not Collective
245836eaed60SHong Zhang 
245936eaed60SHong Zhang    Input Parameter:
246036eaed60SHong Zhang .  ts - the TS context obtained from TSCreate()
246136eaed60SHong Zhang 
246236eaed60SHong Zhang    Output Parameter:
24632c39e106SBarry Smith .  v - the vector containing the integrals for each cost function
246436eaed60SHong Zhang 
246536eaed60SHong Zhang    Level: intermediate
246636eaed60SHong Zhang 
2467d4aa0a58SBarry Smith .seealso: TSAdjointSetCostIntegrand()
246836eaed60SHong Zhang 
246936eaed60SHong Zhang .keywords: TS, sensitivity analysis
247036eaed60SHong Zhang @*/
24712c39e106SBarry Smith PetscErrorCode  TSAdjointGetCostIntegral(TS ts,Vec *v)
247236eaed60SHong Zhang {
247336eaed60SHong Zhang   PetscFunctionBegin;
247436eaed60SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
247536eaed60SHong Zhang   PetscValidPointer(v,2);
24762c39e106SBarry Smith   *v = ts->vec_costintegral;
247736eaed60SHong Zhang   PetscFunctionReturn(0);
247836eaed60SHong Zhang }
247936eaed60SHong Zhang 
248036eaed60SHong Zhang #undef __FUNCT__
2481d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeCostIntegrand"
24826fd0887fSHong Zhang /*@
24832c39e106SBarry Smith    TSAdjointComputeCostIntegrand - Evaluates the integral function in the cost functions.
24846fd0887fSHong Zhang 
24856fd0887fSHong Zhang    Input Parameters:
24866fd0887fSHong Zhang +  ts - the TS context
24876fd0887fSHong Zhang .  t - current time
248805755b9cSHong Zhang -  U - state vector
24896fd0887fSHong Zhang 
24906fd0887fSHong Zhang    Output Parameter:
24916fd0887fSHong Zhang .  q - vector of size numberadjs to hold the outputs
24926fd0887fSHong Zhang 
24936fd0887fSHong Zhang    Note:
24946fd0887fSHong Zhang    Most users should not need to explicitly call this routine, as it
24956fd0887fSHong Zhang    is used internally within the sensitivity analysis context.
24966fd0887fSHong Zhang 
24976fd0887fSHong Zhang    Level: developer
24986fd0887fSHong Zhang 
24996fd0887fSHong Zhang .keywords: TS, compute
25006fd0887fSHong Zhang 
2501d4aa0a58SBarry Smith .seealso: TSAdjointSetCostIntegrand()
25026fd0887fSHong Zhang @*/
2503d4aa0a58SBarry Smith PetscErrorCode TSAdjointComputeCostIntegrand(TS ts,PetscReal t,Vec U,Vec q)
25046fd0887fSHong Zhang {
25056fd0887fSHong Zhang   PetscErrorCode ierr;
25066fd0887fSHong Zhang 
25076fd0887fSHong Zhang   PetscFunctionBegin;
25086fd0887fSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
25096fd0887fSHong Zhang   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
25106fd0887fSHong Zhang   PetscValidHeaderSpecific(q,VEC_CLASSID,4);
25116fd0887fSHong Zhang 
25126fd0887fSHong Zhang   ierr = PetscLogEventBegin(TS_FunctionEval,ts,U,q,0);CHKERRQ(ierr);
2513e4680132SHong Zhang   if (ts->costintegrand) {
2514e4680132SHong Zhang     PetscStackPush("TS user integrand in the cost function");
251505755b9cSHong Zhang     ierr = (*ts->costintegrand)(ts,t,U,q,ts->costintegrandctx);CHKERRQ(ierr);
25166fd0887fSHong Zhang     PetscStackPop;
25176fd0887fSHong Zhang   } else {
25186fd0887fSHong Zhang     ierr = VecZeroEntries(q);CHKERRQ(ierr);
25196fd0887fSHong Zhang   }
25206fd0887fSHong Zhang 
25216fd0887fSHong Zhang   ierr = PetscLogEventEnd(TS_FunctionEval,ts,U,q,0);CHKERRQ(ierr);
25226fd0887fSHong Zhang   PetscFunctionReturn(0);
25236fd0887fSHong Zhang }
25246fd0887fSHong Zhang 
25256fd0887fSHong Zhang #undef __FUNCT__
2526d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDYFunction"
252705755b9cSHong Zhang /*@
2528d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction - Runs the user-defined DRDY function.
252905755b9cSHong Zhang 
253005755b9cSHong Zhang   Collective on TS
253105755b9cSHong Zhang 
253205755b9cSHong Zhang   Input Parameters:
253305755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
253405755b9cSHong Zhang 
253505755b9cSHong Zhang   Notes:
2536d4aa0a58SBarry Smith   TSAdjointComputeDRDYFunction() is typically used for sensitivity implementation,
253705755b9cSHong Zhang   so most users would not generally call this routine themselves.
253805755b9cSHong Zhang 
253905755b9cSHong Zhang   Level: developer
254005755b9cSHong Zhang 
254105755b9cSHong Zhang .keywords: TS, sensitivity
2542d4aa0a58SBarry Smith .seealso: TSAdjointComputeDRDYFunction()
254305755b9cSHong Zhang @*/
2544d4aa0a58SBarry Smith PetscErrorCode  TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec X,Vec *drdy)
254505755b9cSHong Zhang {
254605755b9cSHong Zhang   PetscErrorCode ierr;
254705755b9cSHong Zhang 
254805755b9cSHong Zhang   PetscFunctionBegin;
254905755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
255005755b9cSHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
255105755b9cSHong Zhang 
255205755b9cSHong Zhang   PetscStackPush("TS user DRDY function for sensitivity analysis");
2553b612ec54SBarry Smith   ierr = (*ts->drdyfunction)(ts,t,X,drdy,ts->costintegrandctx); CHKERRQ(ierr);
255405755b9cSHong Zhang   PetscStackPop;
255505755b9cSHong Zhang   PetscFunctionReturn(0);
255605755b9cSHong Zhang }
255705755b9cSHong Zhang 
255805755b9cSHong Zhang #undef __FUNCT__
2559d4aa0a58SBarry Smith #define __FUNCT__ "TSAdjointComputeDRDPFunction"
256005755b9cSHong Zhang /*@
2561d4aa0a58SBarry Smith   TSAdjointComputeDRDPFunction - Runs the user-defined DRDP function.
256205755b9cSHong Zhang 
256305755b9cSHong Zhang   Collective on TS
256405755b9cSHong Zhang 
256505755b9cSHong Zhang   Input Parameters:
256605755b9cSHong Zhang . ts   - The TS context obtained from TSCreate()
256705755b9cSHong Zhang 
256805755b9cSHong Zhang   Notes:
256905755b9cSHong Zhang   TSDRDPFunction() is typically used for sensitivity implementation,
257005755b9cSHong Zhang   so most users would not generally call this routine themselves.
257105755b9cSHong Zhang 
257205755b9cSHong Zhang   Level: developer
257305755b9cSHong Zhang 
257405755b9cSHong Zhang .keywords: TS, sensitivity
2575d4aa0a58SBarry Smith .seealso: TSAdjointSetDRDPFunction()
257605755b9cSHong Zhang @*/
2577d4aa0a58SBarry Smith PetscErrorCode  TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec X,Vec *drdp)
257805755b9cSHong Zhang {
257905755b9cSHong Zhang   PetscErrorCode ierr;
258005755b9cSHong Zhang 
258105755b9cSHong Zhang   PetscFunctionBegin;
258205755b9cSHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
258305755b9cSHong Zhang   PetscValidHeaderSpecific(X,VEC_CLASSID,3);
258405755b9cSHong Zhang 
258505755b9cSHong Zhang   PetscStackPush("TS user DRDP function for sensitivity analysis");
2586b612ec54SBarry Smith   ierr = (*ts->drdpfunction)(ts,t,X,drdp,ts->costintegrandctx); CHKERRQ(ierr);
258705755b9cSHong Zhang   PetscStackPop;
258805755b9cSHong Zhang   PetscFunctionReturn(0);
258905755b9cSHong Zhang }
259005755b9cSHong Zhang 
259105755b9cSHong Zhang #undef __FUNCT__
2592e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPreStep"
2593ac226902SBarry Smith /*@C
2594000e7ae3SMatthew Knepley   TSSetPreStep - Sets the general-purpose function
25953f2090d5SJed Brown   called once at the beginning of each time step.
2596000e7ae3SMatthew Knepley 
25973f9fe445SBarry Smith   Logically Collective on TS
2598000e7ae3SMatthew Knepley 
2599000e7ae3SMatthew Knepley   Input Parameters:
2600000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2601000e7ae3SMatthew Knepley - func - The function
2602000e7ae3SMatthew Knepley 
2603000e7ae3SMatthew Knepley   Calling sequence of func:
2604000e7ae3SMatthew Knepley . func (TS ts);
2605000e7ae3SMatthew Knepley 
2606000e7ae3SMatthew Knepley   Level: intermediate
2607000e7ae3SMatthew Knepley 
2608b8123daeSJed Brown   Note:
2609b8123daeSJed Brown   If a step is rejected, TSStep() will call this routine again before each attempt.
2610b8123daeSJed Brown   The last completed time step number can be queried using TSGetTimeStepNumber(), the
2611b8123daeSJed Brown   size of the step being attempted can be obtained using TSGetTimeStep().
2612b8123daeSJed Brown 
2613000e7ae3SMatthew Knepley .keywords: TS, timestep
26149be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPostStage(), TSSetPostStep(), TSStep()
2615000e7ae3SMatthew Knepley @*/
26167087cfbeSBarry Smith PetscErrorCode  TSSetPreStep(TS ts, PetscErrorCode (*func)(TS))
2617000e7ae3SMatthew Knepley {
2618000e7ae3SMatthew Knepley   PetscFunctionBegin;
26190700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2620ae60f76fSBarry Smith   ts->prestep = func;
2621000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2622000e7ae3SMatthew Knepley }
2623000e7ae3SMatthew Knepley 
2624e74ef692SMatthew Knepley #undef __FUNCT__
26253f2090d5SJed Brown #define __FUNCT__ "TSPreStep"
262609ee8438SJed Brown /*@
26273f2090d5SJed Brown   TSPreStep - Runs the user-defined pre-step function.
26283f2090d5SJed Brown 
26293f2090d5SJed Brown   Collective on TS
26303f2090d5SJed Brown 
26313f2090d5SJed Brown   Input Parameters:
26323f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
26333f2090d5SJed Brown 
26343f2090d5SJed Brown   Notes:
26353f2090d5SJed Brown   TSPreStep() is typically used within time stepping implementations,
26363f2090d5SJed Brown   so most users would not generally call this routine themselves.
26373f2090d5SJed Brown 
26383f2090d5SJed Brown   Level: developer
26393f2090d5SJed Brown 
26403f2090d5SJed Brown .keywords: TS, timestep
26419be3e283SDebojyoti Ghosh .seealso: TSSetPreStep(), TSPreStage(), TSPostStage(), TSPostStep()
26423f2090d5SJed Brown @*/
26437087cfbeSBarry Smith PetscErrorCode  TSPreStep(TS ts)
26443f2090d5SJed Brown {
26453f2090d5SJed Brown   PetscErrorCode ierr;
26463f2090d5SJed Brown 
26473f2090d5SJed Brown   PetscFunctionBegin;
26480700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2649ae60f76fSBarry Smith   if (ts->prestep) {
2650ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestep),(ts));
2651312ce896SJed Brown   }
26523f2090d5SJed Brown   PetscFunctionReturn(0);
26533f2090d5SJed Brown }
26543f2090d5SJed Brown 
26553f2090d5SJed Brown #undef __FUNCT__
2656b8123daeSJed Brown #define __FUNCT__ "TSSetPreStage"
2657b8123daeSJed Brown /*@C
2658b8123daeSJed Brown   TSSetPreStage - Sets the general-purpose function
2659b8123daeSJed Brown   called once at the beginning of each stage.
2660b8123daeSJed Brown 
2661b8123daeSJed Brown   Logically Collective on TS
2662b8123daeSJed Brown 
2663b8123daeSJed Brown   Input Parameters:
2664b8123daeSJed Brown + ts   - The TS context obtained from TSCreate()
2665b8123daeSJed Brown - func - The function
2666b8123daeSJed Brown 
2667b8123daeSJed Brown   Calling sequence of func:
2668b8123daeSJed Brown . PetscErrorCode func(TS ts, PetscReal stagetime);
2669b8123daeSJed Brown 
2670b8123daeSJed Brown   Level: intermediate
2671b8123daeSJed Brown 
2672b8123daeSJed Brown   Note:
2673b8123daeSJed Brown   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
2674b8123daeSJed Brown   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
2675b8123daeSJed Brown   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
2676b8123daeSJed Brown 
2677b8123daeSJed Brown .keywords: TS, timestep
26789be3e283SDebojyoti Ghosh .seealso: TSSetPostStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
2679b8123daeSJed Brown @*/
2680b8123daeSJed Brown PetscErrorCode  TSSetPreStage(TS ts, PetscErrorCode (*func)(TS,PetscReal))
2681b8123daeSJed Brown {
2682b8123daeSJed Brown   PetscFunctionBegin;
2683b8123daeSJed Brown   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2684ae60f76fSBarry Smith   ts->prestage = func;
2685b8123daeSJed Brown   PetscFunctionReturn(0);
2686b8123daeSJed Brown }
2687b8123daeSJed Brown 
2688b8123daeSJed Brown #undef __FUNCT__
26899be3e283SDebojyoti Ghosh #define __FUNCT__ "TSSetPostStage"
26909be3e283SDebojyoti Ghosh /*@C
26919be3e283SDebojyoti Ghosh   TSSetPostStage - Sets the general-purpose function
26929be3e283SDebojyoti Ghosh   called once at the end of each stage.
26939be3e283SDebojyoti Ghosh 
26949be3e283SDebojyoti Ghosh   Logically Collective on TS
26959be3e283SDebojyoti Ghosh 
26969be3e283SDebojyoti Ghosh   Input Parameters:
26979be3e283SDebojyoti Ghosh + ts   - The TS context obtained from TSCreate()
26989be3e283SDebojyoti Ghosh - func - The function
26999be3e283SDebojyoti Ghosh 
27009be3e283SDebojyoti Ghosh   Calling sequence of func:
27019be3e283SDebojyoti Ghosh . PetscErrorCode func(TS ts, PetscReal stagetime, PetscInt stageindex, Vec* Y);
27029be3e283SDebojyoti Ghosh 
27039be3e283SDebojyoti Ghosh   Level: intermediate
27049be3e283SDebojyoti Ghosh 
27059be3e283SDebojyoti Ghosh   Note:
27069be3e283SDebojyoti Ghosh   There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried.
27079be3e283SDebojyoti Ghosh   The time step number being computed can be queried using TSGetTimeStepNumber() and the total size of the step being
27089be3e283SDebojyoti Ghosh   attempted can be obtained using TSGetTimeStep(). The time at the start of the step is available via TSGetTime().
27099be3e283SDebojyoti Ghosh 
27109be3e283SDebojyoti Ghosh .keywords: TS, timestep
27119be3e283SDebojyoti Ghosh .seealso: TSSetPreStage(), TSSetPreStep(), TSSetPostStep(), TSGetApplicationContext()
27129be3e283SDebojyoti Ghosh @*/
27139be3e283SDebojyoti Ghosh PetscErrorCode  TSSetPostStage(TS ts, PetscErrorCode (*func)(TS,PetscReal,PetscInt,Vec*))
27149be3e283SDebojyoti Ghosh {
27159be3e283SDebojyoti Ghosh   PetscFunctionBegin;
27169be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
27179be3e283SDebojyoti Ghosh   ts->poststage = func;
27189be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
27199be3e283SDebojyoti Ghosh }
27209be3e283SDebojyoti Ghosh 
27219be3e283SDebojyoti Ghosh #undef __FUNCT__
2722b8123daeSJed Brown #define __FUNCT__ "TSPreStage"
2723b8123daeSJed Brown /*@
2724b8123daeSJed Brown   TSPreStage - Runs the user-defined pre-stage function set using TSSetPreStage()
2725b8123daeSJed Brown 
2726b8123daeSJed Brown   Collective on TS
2727b8123daeSJed Brown 
2728b8123daeSJed Brown   Input Parameters:
2729b8123daeSJed Brown . ts          - The TS context obtained from TSCreate()
27309be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
2731b8123daeSJed Brown 
2732b8123daeSJed Brown   Notes:
2733b8123daeSJed Brown   TSPreStage() is typically used within time stepping implementations,
2734b8123daeSJed Brown   most users would not generally call this routine themselves.
2735b8123daeSJed Brown 
2736b8123daeSJed Brown   Level: developer
2737b8123daeSJed Brown 
2738b8123daeSJed Brown .keywords: TS, timestep
27399be3e283SDebojyoti Ghosh .seealso: TSPostStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
2740b8123daeSJed Brown @*/
2741b8123daeSJed Brown PetscErrorCode  TSPreStage(TS ts, PetscReal stagetime)
2742b8123daeSJed Brown {
2743b8123daeSJed Brown   PetscErrorCode ierr;
2744b8123daeSJed Brown 
2745b8123daeSJed Brown   PetscFunctionBegin;
2746b8123daeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2747ae60f76fSBarry Smith   if (ts->prestage) {
2748ae60f76fSBarry Smith     PetscStackCallStandard((*ts->prestage),(ts,stagetime));
2749b8123daeSJed Brown   }
2750b8123daeSJed Brown   PetscFunctionReturn(0);
2751b8123daeSJed Brown }
2752b8123daeSJed Brown 
2753b8123daeSJed Brown #undef __FUNCT__
27549be3e283SDebojyoti Ghosh #define __FUNCT__ "TSPostStage"
27559be3e283SDebojyoti Ghosh /*@
27569be3e283SDebojyoti Ghosh   TSPostStage - Runs the user-defined post-stage function set using TSSetPostStage()
27579be3e283SDebojyoti Ghosh 
27589be3e283SDebojyoti Ghosh   Collective on TS
27599be3e283SDebojyoti Ghosh 
27609be3e283SDebojyoti Ghosh   Input Parameters:
27619be3e283SDebojyoti Ghosh . ts          - The TS context obtained from TSCreate()
27629be3e283SDebojyoti Ghosh   stagetime   - The absolute time of the current stage
27639be3e283SDebojyoti Ghosh   stageindex  - Stage number
27649be3e283SDebojyoti Ghosh   Y           - Array of vectors (of size = total number
27659be3e283SDebojyoti Ghosh                 of stages) with the stage solutions
27669be3e283SDebojyoti Ghosh 
27679be3e283SDebojyoti Ghosh   Notes:
27689be3e283SDebojyoti Ghosh   TSPostStage() is typically used within time stepping implementations,
27699be3e283SDebojyoti Ghosh   most users would not generally call this routine themselves.
27709be3e283SDebojyoti Ghosh 
27719be3e283SDebojyoti Ghosh   Level: developer
27729be3e283SDebojyoti Ghosh 
27739be3e283SDebojyoti Ghosh .keywords: TS, timestep
27749be3e283SDebojyoti Ghosh .seealso: TSPreStage(), TSSetPreStep(), TSPreStep(), TSPostStep()
27759be3e283SDebojyoti Ghosh @*/
27769be3e283SDebojyoti Ghosh PetscErrorCode  TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)
27779be3e283SDebojyoti Ghosh {
27789be3e283SDebojyoti Ghosh   PetscErrorCode ierr;
27799be3e283SDebojyoti Ghosh 
27809be3e283SDebojyoti Ghosh   PetscFunctionBegin;
27819be3e283SDebojyoti Ghosh   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
27824beae5d8SLisandro Dalcin   if (ts->poststage) {
27839be3e283SDebojyoti Ghosh     PetscStackCallStandard((*ts->poststage),(ts,stagetime,stageindex,Y));
27849be3e283SDebojyoti Ghosh   }
27859be3e283SDebojyoti Ghosh   PetscFunctionReturn(0);
27869be3e283SDebojyoti Ghosh }
27879be3e283SDebojyoti Ghosh 
27889be3e283SDebojyoti Ghosh #undef __FUNCT__
2789e74ef692SMatthew Knepley #define __FUNCT__ "TSSetPostStep"
2790ac226902SBarry Smith /*@C
2791000e7ae3SMatthew Knepley   TSSetPostStep - Sets the general-purpose function
27923f2090d5SJed Brown   called once at the end of each time step.
2793000e7ae3SMatthew Knepley 
27943f9fe445SBarry Smith   Logically Collective on TS
2795000e7ae3SMatthew Knepley 
2796000e7ae3SMatthew Knepley   Input Parameters:
2797000e7ae3SMatthew Knepley + ts   - The TS context obtained from TSCreate()
2798000e7ae3SMatthew Knepley - func - The function
2799000e7ae3SMatthew Knepley 
2800000e7ae3SMatthew Knepley   Calling sequence of func:
2801b8123daeSJed Brown $ func (TS ts);
2802000e7ae3SMatthew Knepley 
2803000e7ae3SMatthew Knepley   Level: intermediate
2804000e7ae3SMatthew Knepley 
2805000e7ae3SMatthew Knepley .keywords: TS, timestep
2806b8123daeSJed Brown .seealso: TSSetPreStep(), TSSetPreStage(), TSGetTimeStep(), TSGetTimeStepNumber(), TSGetTime()
2807000e7ae3SMatthew Knepley @*/
28087087cfbeSBarry Smith PetscErrorCode  TSSetPostStep(TS ts, PetscErrorCode (*func)(TS))
2809000e7ae3SMatthew Knepley {
2810000e7ae3SMatthew Knepley   PetscFunctionBegin;
28110700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
2812ae60f76fSBarry Smith   ts->poststep = func;
2813000e7ae3SMatthew Knepley   PetscFunctionReturn(0);
2814000e7ae3SMatthew Knepley }
2815000e7ae3SMatthew Knepley 
2816e74ef692SMatthew Knepley #undef __FUNCT__
28173f2090d5SJed Brown #define __FUNCT__ "TSPostStep"
281809ee8438SJed Brown /*@
28193f2090d5SJed Brown   TSPostStep - Runs the user-defined post-step function.
28203f2090d5SJed Brown 
28213f2090d5SJed Brown   Collective on TS
28223f2090d5SJed Brown 
28233f2090d5SJed Brown   Input Parameters:
28243f2090d5SJed Brown . ts   - The TS context obtained from TSCreate()
28253f2090d5SJed Brown 
28263f2090d5SJed Brown   Notes:
28273f2090d5SJed Brown   TSPostStep() is typically used within time stepping implementations,
28283f2090d5SJed Brown   so most users would not generally call this routine themselves.
28293f2090d5SJed Brown 
28303f2090d5SJed Brown   Level: developer
28313f2090d5SJed Brown 
28323f2090d5SJed Brown .keywords: TS, timestep
28333f2090d5SJed Brown @*/
28347087cfbeSBarry Smith PetscErrorCode  TSPostStep(TS ts)
28353f2090d5SJed Brown {
28363f2090d5SJed Brown   PetscErrorCode ierr;
28373f2090d5SJed Brown 
28383f2090d5SJed Brown   PetscFunctionBegin;
28390700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2840ae60f76fSBarry Smith   if (ts->poststep) {
2841ae60f76fSBarry Smith     PetscStackCallStandard((*ts->poststep),(ts));
284272ac3e02SJed Brown   }
28433f2090d5SJed Brown   PetscFunctionReturn(0);
28443f2090d5SJed Brown }
28453f2090d5SJed Brown 
2846d763cef2SBarry Smith /* ------------ Routines to set performance monitoring options ----------- */
2847d763cef2SBarry Smith 
28484a2ae208SSatish Balay #undef __FUNCT__
2849a6570f20SBarry Smith #define __FUNCT__ "TSMonitorSet"
2850d763cef2SBarry Smith /*@C
2851a6570f20SBarry Smith    TSMonitorSet - Sets an ADDITIONAL function that is to be used at every
2852d763cef2SBarry Smith    timestep to display the iteration's  progress.
2853d763cef2SBarry Smith 
28543f9fe445SBarry Smith    Logically Collective on TS
2855d763cef2SBarry Smith 
2856d763cef2SBarry Smith    Input Parameters:
2857d763cef2SBarry Smith +  ts - the TS context obtained from TSCreate()
2858e213d8f1SJed Brown .  monitor - monitoring routine
2859329f5518SBarry Smith .  mctx - [optional] user-defined context for private data for the
28600298fd71SBarry Smith              monitor routine (use NULL if no context is desired)
2861b3006f0bSLois Curfman McInnes -  monitordestroy - [optional] routine that frees monitor context
28620298fd71SBarry Smith           (may be NULL)
2863d763cef2SBarry Smith 
2864e213d8f1SJed Brown    Calling sequence of monitor:
28650910c330SBarry Smith $    int monitor(TS ts,PetscInt steps,PetscReal time,Vec u,void *mctx)
2866d763cef2SBarry Smith 
2867d763cef2SBarry Smith +    ts - the TS context
286888c05cc5SBarry 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
286988c05cc5SBarry Smith                                been interpolated to)
28701f06c33eSBarry Smith .    time - current time
28710910c330SBarry Smith .    u - current iterate
2872d763cef2SBarry Smith -    mctx - [optional] monitoring context
2873d763cef2SBarry Smith 
2874d763cef2SBarry Smith    Notes:
2875d763cef2SBarry Smith    This routine adds an additional monitor to the list of monitors that
2876d763cef2SBarry Smith    already has been loaded.
2877d763cef2SBarry Smith 
2878025f1a04SBarry Smith    Fortran notes: Only a single monitor function can be set for each TS object
2879025f1a04SBarry Smith 
2880d763cef2SBarry Smith    Level: intermediate
2881d763cef2SBarry Smith 
2882d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2883d763cef2SBarry Smith 
2884a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorCancel()
2885d763cef2SBarry Smith @*/
2886c2efdce3SBarry Smith PetscErrorCode  TSMonitorSet(TS ts,PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,void*),void *mctx,PetscErrorCode (*mdestroy)(void**))
2887d763cef2SBarry Smith {
2888d763cef2SBarry Smith   PetscFunctionBegin;
28890700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
289017186662SBarry Smith   if (ts->numbermonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many monitors set");
2891d763cef2SBarry Smith   ts->monitor[ts->numbermonitors]          = monitor;
28928704b422SBarry Smith   ts->monitordestroy[ts->numbermonitors]   = mdestroy;
2893d763cef2SBarry Smith   ts->monitorcontext[ts->numbermonitors++] = (void*)mctx;
2894d763cef2SBarry Smith   PetscFunctionReturn(0);
2895d763cef2SBarry Smith }
2896d763cef2SBarry Smith 
28974a2ae208SSatish Balay #undef __FUNCT__
2898a6570f20SBarry Smith #define __FUNCT__ "TSMonitorCancel"
2899d763cef2SBarry Smith /*@C
2900a6570f20SBarry Smith    TSMonitorCancel - Clears all the monitors that have been set on a time-step object.
2901d763cef2SBarry Smith 
29023f9fe445SBarry Smith    Logically Collective on TS
2903d763cef2SBarry Smith 
2904d763cef2SBarry Smith    Input Parameters:
2905d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
2906d763cef2SBarry Smith 
2907d763cef2SBarry Smith    Notes:
2908d763cef2SBarry Smith    There is no way to remove a single, specific monitor.
2909d763cef2SBarry Smith 
2910d763cef2SBarry Smith    Level: intermediate
2911d763cef2SBarry Smith 
2912d763cef2SBarry Smith .keywords: TS, timestep, set, monitor
2913d763cef2SBarry Smith 
2914a6570f20SBarry Smith .seealso: TSMonitorDefault(), TSMonitorSet()
2915d763cef2SBarry Smith @*/
29167087cfbeSBarry Smith PetscErrorCode  TSMonitorCancel(TS ts)
2917d763cef2SBarry Smith {
2918d952e501SBarry Smith   PetscErrorCode ierr;
2919d952e501SBarry Smith   PetscInt       i;
2920d952e501SBarry Smith 
2921d763cef2SBarry Smith   PetscFunctionBegin;
29220700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2923d952e501SBarry Smith   for (i=0; i<ts->numbermonitors; i++) {
29248704b422SBarry Smith     if (ts->monitordestroy[i]) {
29258704b422SBarry Smith       ierr = (*ts->monitordestroy[i])(&ts->monitorcontext[i]);CHKERRQ(ierr);
2926d952e501SBarry Smith     }
2927d952e501SBarry Smith   }
2928d763cef2SBarry Smith   ts->numbermonitors = 0;
2929d763cef2SBarry Smith   PetscFunctionReturn(0);
2930d763cef2SBarry Smith }
2931d763cef2SBarry Smith 
29324a2ae208SSatish Balay #undef __FUNCT__
2933a6570f20SBarry Smith #define __FUNCT__ "TSMonitorDefault"
2934d8e5e3e6SSatish Balay /*@
2935a6570f20SBarry Smith    TSMonitorDefault - Sets the Default monitor
29365516499fSSatish Balay 
29375516499fSSatish Balay    Level: intermediate
293841251cbbSSatish Balay 
29395516499fSSatish Balay .keywords: TS, set, monitor
29405516499fSSatish Balay 
294141251cbbSSatish Balay .seealso: TSMonitorDefault(), TSMonitorSet()
294241251cbbSSatish Balay @*/
2943649052a6SBarry Smith PetscErrorCode TSMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,void *dummy)
2944d763cef2SBarry Smith {
2945dfbe8321SBarry Smith   PetscErrorCode ierr;
2946ce94432eSBarry Smith   PetscViewer    viewer = dummy ? (PetscViewer) dummy : PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)ts));
2947d132466eSBarry Smith 
2948d763cef2SBarry Smith   PetscFunctionBegin;
2949649052a6SBarry Smith   ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2950971832b6SBarry Smith   ierr = PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g\n",step,(double)ts->time_step,(double)ptime);CHKERRQ(ierr);
2951649052a6SBarry Smith   ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);CHKERRQ(ierr);
2952d763cef2SBarry Smith   PetscFunctionReturn(0);
2953d763cef2SBarry Smith }
2954d763cef2SBarry Smith 
29554a2ae208SSatish Balay #undef __FUNCT__
2956cd652676SJed Brown #define __FUNCT__ "TSSetRetainStages"
2957cd652676SJed Brown /*@
2958cd652676SJed Brown    TSSetRetainStages - Request that all stages in the upcoming step be stored so that interpolation will be available.
2959cd652676SJed Brown 
2960cd652676SJed Brown    Logically Collective on TS
2961cd652676SJed Brown 
2962cd652676SJed Brown    Input Argument:
2963cd652676SJed Brown .  ts - time stepping context
2964cd652676SJed Brown 
2965cd652676SJed Brown    Output Argument:
2966cd652676SJed Brown .  flg - PETSC_TRUE or PETSC_FALSE
2967cd652676SJed Brown 
2968cd652676SJed Brown    Level: intermediate
2969cd652676SJed Brown 
2970cd652676SJed Brown .keywords: TS, set
2971cd652676SJed Brown 
2972cd652676SJed Brown .seealso: TSInterpolate(), TSSetPostStep()
2973cd652676SJed Brown @*/
2974cd652676SJed Brown PetscErrorCode TSSetRetainStages(TS ts,PetscBool flg)
2975cd652676SJed Brown {
2976cd652676SJed Brown   PetscFunctionBegin;
2977cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
2978cd652676SJed Brown   ts->retain_stages = flg;
2979cd652676SJed Brown   PetscFunctionReturn(0);
2980cd652676SJed Brown }
2981cd652676SJed Brown 
2982cd652676SJed Brown #undef __FUNCT__
2983cd652676SJed Brown #define __FUNCT__ "TSInterpolate"
2984cd652676SJed Brown /*@
2985cd652676SJed Brown    TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval
2986cd652676SJed Brown 
2987cd652676SJed Brown    Collective on TS
2988cd652676SJed Brown 
2989cd652676SJed Brown    Input Argument:
2990cd652676SJed Brown +  ts - time stepping context
2991cd652676SJed Brown -  t - time to interpolate to
2992cd652676SJed Brown 
2993cd652676SJed Brown    Output Argument:
29940910c330SBarry Smith .  U - state at given time
2995cd652676SJed Brown 
2996cd652676SJed Brown    Notes:
2997cd652676SJed Brown    The user should call TSSetRetainStages() before taking a step in which interpolation will be requested.
2998cd652676SJed Brown 
2999cd652676SJed Brown    Level: intermediate
3000cd652676SJed Brown 
3001cd652676SJed Brown    Developer Notes:
3002cd652676SJed Brown    TSInterpolate() and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints.
3003cd652676SJed Brown 
3004cd652676SJed Brown .keywords: TS, set
3005cd652676SJed Brown 
3006cd652676SJed Brown .seealso: TSSetRetainStages(), TSSetPostStep()
3007cd652676SJed Brown @*/
30080910c330SBarry Smith PetscErrorCode TSInterpolate(TS ts,PetscReal t,Vec U)
3009cd652676SJed Brown {
3010cd652676SJed Brown   PetscErrorCode ierr;
3011cd652676SJed Brown 
3012cd652676SJed Brown   PetscFunctionBegin;
3013cd652676SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3014b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
30156712e2f1SBarry 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);
3016ce94432eSBarry Smith   if (!ts->ops->interpolate) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide interpolation",((PetscObject)ts)->type_name);
30170910c330SBarry Smith   ierr = (*ts->ops->interpolate)(ts,t,U);CHKERRQ(ierr);
3018cd652676SJed Brown   PetscFunctionReturn(0);
3019cd652676SJed Brown }
3020cd652676SJed Brown 
3021cd652676SJed Brown #undef __FUNCT__
30224a2ae208SSatish Balay #define __FUNCT__ "TSStep"
3023d763cef2SBarry Smith /*@
30246d9e5789SSean Farley    TSStep - Steps one time step
3025d763cef2SBarry Smith 
3026d763cef2SBarry Smith    Collective on TS
3027d763cef2SBarry Smith 
3028d763cef2SBarry Smith    Input Parameter:
3029d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3030d763cef2SBarry Smith 
303127829d71SBarry Smith    Level: developer
3032d763cef2SBarry Smith 
3033b8123daeSJed Brown    Notes:
303427829d71SBarry Smith    The public interface for the ODE/DAE solvers is TSSolve(), you should almost for sure be using that routine and not this routine.
303527829d71SBarry Smith 
3036b8123daeSJed Brown    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
3037b8123daeSJed Brown    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
3038b8123daeSJed Brown 
303925cb2221SBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
304025cb2221SBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
304125cb2221SBarry Smith 
3042d763cef2SBarry Smith .keywords: TS, timestep, solve
3043d763cef2SBarry Smith 
30449be3e283SDebojyoti Ghosh .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
3045d763cef2SBarry Smith @*/
3046193ac0bcSJed Brown PetscErrorCode  TSStep(TS ts)
3047d763cef2SBarry Smith {
30482fb8446cSMatthew G. Knepley   DM               dm;
3049dfbe8321SBarry Smith   PetscErrorCode   ierr;
3050fffbeea8SBarry Smith   static PetscBool cite = PETSC_FALSE;
3051d763cef2SBarry Smith 
3052d763cef2SBarry Smith   PetscFunctionBegin;
30530700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
3054fffbeea8SBarry Smith   ierr = PetscCitationsRegister("@techreport{tspaper,\n"
3055fffbeea8SBarry Smith                                 "  title       = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n"
3056fffbeea8SBarry Smith                                 "  author      = {Shrirang Abhyankar and Jed Brown and Emil Constantinescu and Debojyoti Ghosh and Barry F. Smith},\n"
3057fffbeea8SBarry Smith                                 "  type        = {Preprint},\n"
3058fffbeea8SBarry Smith                                 "  number      = {ANL/MCS-P5061-0114},\n"
3059fffbeea8SBarry Smith                                 "  institution = {Argonne National Laboratory},\n"
3060fffbeea8SBarry Smith                                 "  year        = {2014}\n}\n",&cite);
3061fffbeea8SBarry Smith 
30622fb8446cSMatthew G. Knepley   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
3063d405a339SMatthew Knepley   ierr = TSSetUp(ts);CHKERRQ(ierr);
3064d405a339SMatthew Knepley 
3065362cd11cSLisandro Dalcin   ts->reason = TS_CONVERGED_ITERATING;
306624655328SShri   ts->ptime_prev = ts->ptime;
3067cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3068bbd56ea5SKarl Rupp 
3069e04979a6SLisandro Dalcin   if (!ts->ops->step) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSStep not implemented for type '%s'",((PetscObject)ts)->type_name);
3070d5ba7fb7SMatthew Knepley   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3071193ac0bcSJed Brown   ierr = (*ts->ops->step)(ts);CHKERRQ(ierr);
3072d5ba7fb7SMatthew Knepley   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3073bbd56ea5SKarl Rupp 
307424655328SShri   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3075cdb7a50dSMatthew G. Knepley   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3076362cd11cSLisandro Dalcin 
3077362cd11cSLisandro Dalcin   if (ts->reason < 0) {
3078cef5090cSJed Brown     if (ts->errorifstepfailed) {
307908c7845fSBarry 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]);
308008c7845fSBarry Smith       else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3081d2daff3dSHong Zhang     }
308208c7845fSBarry Smith   } else if (!ts->reason) {
308308c7845fSBarry Smith     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
308408c7845fSBarry Smith     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
308508c7845fSBarry Smith   }
30862c18e0fdSBarry Smith   ts->total_steps++;
308708c7845fSBarry Smith   PetscFunctionReturn(0);
308808c7845fSBarry Smith }
308908c7845fSBarry Smith 
309008c7845fSBarry Smith #undef __FUNCT__
309108c7845fSBarry Smith #define __FUNCT__ "TSAdjointStep"
309208c7845fSBarry Smith /*@
309308c7845fSBarry Smith    TSAdjointStep - Steps one time step
309408c7845fSBarry Smith 
309508c7845fSBarry Smith    Collective on TS
309608c7845fSBarry Smith 
309708c7845fSBarry Smith    Input Parameter:
309808c7845fSBarry Smith .  ts - the TS context obtained from TSCreate()
309908c7845fSBarry Smith 
310008c7845fSBarry Smith    Level: intermediate
310108c7845fSBarry Smith 
310208c7845fSBarry Smith    Notes:
310308c7845fSBarry Smith    The hook set using TSSetPreStep() is called before each attempt to take the step. In general, the time step size may
310408c7845fSBarry Smith    be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages.
310508c7845fSBarry Smith 
310608c7845fSBarry Smith    This may over-step the final time provided in TSSetDuration() depending on the time-step used. TSSolve() interpolates to exactly the
310708c7845fSBarry Smith    time provided in TSSetDuration(). One can use TSInterpolate() to determine an interpolated solution within the final timestep.
310808c7845fSBarry Smith 
310908c7845fSBarry Smith .keywords: TS, timestep, solve
311008c7845fSBarry Smith 
311108c7845fSBarry Smith .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSSetPostStage(), TSInterpolate()
311208c7845fSBarry Smith @*/
311308c7845fSBarry Smith PetscErrorCode  TSAdjointStep(TS ts)
311408c7845fSBarry Smith {
311508c7845fSBarry Smith   DM               dm;
311608c7845fSBarry Smith   PetscErrorCode   ierr;
311708c7845fSBarry Smith 
311808c7845fSBarry Smith   PetscFunctionBegin;
311908c7845fSBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
312008c7845fSBarry Smith   ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
312108c7845fSBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
312208c7845fSBarry Smith 
312308c7845fSBarry Smith   ts->reason = TS_CONVERGED_ITERATING;
312408c7845fSBarry Smith   ts->ptime_prev = ts->ptime;
312508c7845fSBarry Smith   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
312608c7845fSBarry Smith   ierr = VecViewFromOptions(ts->vec_sol, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr);
312708c7845fSBarry Smith 
312808c7845fSBarry Smith   ierr = PetscLogEventBegin(TS_Step,ts,0,0,0);CHKERRQ(ierr);
312942f2b339SBarry 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);
313042f2b339SBarry Smith   ierr = (*ts->ops->adjointstep)(ts);CHKERRQ(ierr);
3131362cd11cSLisandro Dalcin   ierr = PetscLogEventEnd(TS_Step,ts,0,0,0);CHKERRQ(ierr);
3132362cd11cSLisandro Dalcin 
3133cef5090cSJed Brown   ts->time_step_prev = ts->ptime - ts->ptime_prev;
3134cef5090cSJed Brown   ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3135ce94432eSBarry Smith 
3136ce94432eSBarry Smith   if (ts->reason < 0) {
3137cef5090cSJed Brown     if (ts->errorifstepfailed) {
3138362cd11cSLisandro Dalcin       if (ts->reason == TS_DIVERGED_NONLINEAR_SOLVE) {
3139db4deed7SKarl 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]);
31402a3a10ceSLisandro Dalcin       } else if (ts->reason == TS_DIVERGED_STEP_REJECTED) {
31412a3a10ceSLisandro 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]);
3142362cd11cSLisandro Dalcin       } else SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed due to %s",TSConvergedReasons[ts->reason]);
3143362cd11cSLisandro Dalcin     }
3144362cd11cSLisandro Dalcin   } else if (!ts->reason) {
314573b18844SBarry Smith     if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
3146db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time)         ts->reason = TS_CONVERGED_TIME;
3147362cd11cSLisandro Dalcin   }
31482c18e0fdSBarry Smith   ts->total_steps--;
3149d763cef2SBarry Smith   PetscFunctionReturn(0);
3150d763cef2SBarry Smith }
3151d763cef2SBarry Smith 
31524a2ae208SSatish Balay #undef __FUNCT__
315305175c85SJed Brown #define __FUNCT__ "TSEvaluateStep"
315405175c85SJed Brown /*@
315505175c85SJed Brown    TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy.
315605175c85SJed Brown 
31571c3436cfSJed Brown    Collective on TS
315805175c85SJed Brown 
315905175c85SJed Brown    Input Arguments:
31601c3436cfSJed Brown +  ts - time stepping context
31611c3436cfSJed Brown .  order - desired order of accuracy
31620298fd71SBarry Smith -  done - whether the step was evaluated at this order (pass NULL to generate an error if not available)
316305175c85SJed Brown 
316405175c85SJed Brown    Output Arguments:
31650910c330SBarry Smith .  U - state at the end of the current step
316605175c85SJed Brown 
316705175c85SJed Brown    Level: advanced
316805175c85SJed Brown 
3169108c343cSJed Brown    Notes:
3170108c343cSJed Brown    This function cannot be called until all stages have been evaluated.
3171108c343cSJed 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.
3172108c343cSJed Brown 
31731c3436cfSJed Brown .seealso: TSStep(), TSAdapt
317405175c85SJed Brown @*/
31750910c330SBarry Smith PetscErrorCode TSEvaluateStep(TS ts,PetscInt order,Vec U,PetscBool *done)
317605175c85SJed Brown {
317705175c85SJed Brown   PetscErrorCode ierr;
317805175c85SJed Brown 
317905175c85SJed Brown   PetscFunctionBegin;
318005175c85SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
318105175c85SJed Brown   PetscValidType(ts,1);
31820910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,3);
3183ce94432eSBarry Smith   if (!ts->ops->evaluatestep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSEvaluateStep not implemented for type '%s'",((PetscObject)ts)->type_name);
31840910c330SBarry Smith   ierr = (*ts->ops->evaluatestep)(ts,order,U,done);CHKERRQ(ierr);
318505175c85SJed Brown   PetscFunctionReturn(0);
318605175c85SJed Brown }
318705175c85SJed Brown 
3188d67e68b7SBarry Smith 
318905175c85SJed Brown #undef __FUNCT__
31906a4d4014SLisandro Dalcin #define __FUNCT__ "TSSolve"
31916a4d4014SLisandro Dalcin /*@
31926a4d4014SLisandro Dalcin    TSSolve - Steps the requested number of timesteps.
31936a4d4014SLisandro Dalcin 
31946a4d4014SLisandro Dalcin    Collective on TS
31956a4d4014SLisandro Dalcin 
31966a4d4014SLisandro Dalcin    Input Parameter:
31976a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
3198cc708dedSBarry Smith -  u - the solution vector  (can be null if TSSetSolution() was used, otherwise must contain the initial conditions)
31995a3a76d0SJed Brown 
32006a4d4014SLisandro Dalcin    Level: beginner
32016a4d4014SLisandro Dalcin 
32025a3a76d0SJed Brown    Notes:
32035a3a76d0SJed Brown    The final time returned by this function may be different from the time of the internally
32045a3a76d0SJed Brown    held state accessible by TSGetSolution() and TSGetTime() because the method may have
32055a3a76d0SJed Brown    stepped over the final time.
32065a3a76d0SJed Brown 
32076a4d4014SLisandro Dalcin .keywords: TS, timestep, solve
32086a4d4014SLisandro Dalcin 
32096a4d4014SLisandro Dalcin .seealso: TSCreate(), TSSetSolution(), TSStep()
32106a4d4014SLisandro Dalcin @*/
3211cc708dedSBarry Smith PetscErrorCode TSSolve(TS ts,Vec u)
32126a4d4014SLisandro Dalcin {
3213b06615a5SLisandro Dalcin   Vec               solution;
32146a4d4014SLisandro Dalcin   PetscErrorCode    ierr;
3215f22f69f0SBarry Smith 
32166a4d4014SLisandro Dalcin   PetscFunctionBegin;
32170700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3218f2c2a1b9SBarry Smith   if (u) PetscValidHeaderSpecific(u,VEC_CLASSID,2);
321949354f04SShri 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 */
3220b06615a5SLisandro Dalcin     PetscValidHeaderSpecific(u,VEC_CLASSID,2);
32210910c330SBarry Smith     if (!ts->vec_sol || u == ts->vec_sol) {
3222b06615a5SLisandro Dalcin       ierr = VecDuplicate(u,&solution);CHKERRQ(ierr);
3223b06615a5SLisandro Dalcin       ierr = TSSetSolution(ts,solution);CHKERRQ(ierr);
3224b06615a5SLisandro Dalcin       ierr = VecDestroy(&solution);CHKERRQ(ierr); /* grant ownership */
32255a3a76d0SJed Brown     }
32260910c330SBarry Smith     ierr = VecCopy(u,ts->vec_sol);CHKERRQ(ierr);
3227bbd56ea5SKarl Rupp   } else if (u) {
32280910c330SBarry Smith     ierr = TSSetSolution(ts,u);CHKERRQ(ierr);
32295a3a76d0SJed Brown   }
3230d2daff3dSHong Zhang   ierr = TSSetUp(ts);CHKERRQ(ierr); /*compute adj coefficients if the reverse mode is on*/
32316a4d4014SLisandro Dalcin   /* reset time step and iteration counters */
3232193ac0bcSJed Brown   ts->steps             = 0;
32335ef26d82SJed Brown   ts->ksp_its           = 0;
32345ef26d82SJed Brown   ts->snes_its          = 0;
3235c610991cSLisandro Dalcin   ts->num_snes_failures = 0;
3236c610991cSLisandro Dalcin   ts->reject            = 0;
3237193ac0bcSJed Brown   ts->reason            = TS_CONVERGED_ITERATING;
3238193ac0bcSJed Brown 
3239ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view_pre");CHKERRQ(ierr);
3240ec8db0baSMatthew G. Knepley   {
3241ec8db0baSMatthew G. Knepley     DM dm;
3242ec8db0baSMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
3243ec8db0baSMatthew G. Knepley     ierr = DMSetOutputSequenceNumber(dm, ts->steps, ts->ptime);CHKERRQ(ierr);
3244ec8db0baSMatthew G. Knepley   }
3245f05ece33SBarry Smith 
3246193ac0bcSJed Brown   if (ts->ops->solve) {         /* This private interface is transitional and should be removed when all implementations are updated. */
3247193ac0bcSJed Brown     ierr = (*ts->ops->solve)(ts);CHKERRQ(ierr);
32480910c330SBarry Smith     ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);
3249cc708dedSBarry Smith     ts->solvetime = ts->ptime;
3250193ac0bcSJed Brown   } else {
32516a4d4014SLisandro Dalcin     /* steps the requested number of timesteps. */
3252db4deed7SKarl Rupp     if (ts->steps >= ts->max_steps)     ts->reason = TS_CONVERGED_ITS;
3253db4deed7SKarl Rupp     else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME;
3254e1a7a14fSJed Brown     while (!ts->reason) {
3255b06615a5SLisandro Dalcin       ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
3256bc952696SBarry Smith       ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
3257193ac0bcSJed Brown       ierr = TSStep(ts);CHKERRQ(ierr);
3258aeb4809dSShri Abhyankar       if (ts->event) {
3259aeb4809dSShri Abhyankar 	ierr = TSEventMonitor(ts);CHKERRQ(ierr);
32601eda64f1SShri Abhyankar 	if (ts->event->status != TSEVENT_PROCESSING) {
32611eda64f1SShri Abhyankar 	  ierr = TSPostStep(ts);CHKERRQ(ierr);
32621eda64f1SShri Abhyankar 	}
32631eda64f1SShri Abhyankar       } else {
32641eda64f1SShri Abhyankar 	ierr = TSPostStep(ts);CHKERRQ(ierr);
3265aeb4809dSShri Abhyankar       }
3266193ac0bcSJed Brown     }
326749354f04SShri Abhyankar     if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) {
32680910c330SBarry Smith       ierr = TSInterpolate(ts,ts->max_time,u);CHKERRQ(ierr);
3269cc708dedSBarry Smith       ts->solvetime = ts->max_time;
3270b06615a5SLisandro Dalcin       solution = u;
32710574a7fbSJed Brown     } else {
3272ad6bc421SBarry Smith       if (u) {ierr = VecCopy(ts->vec_sol,u);CHKERRQ(ierr);}
3273cc708dedSBarry Smith       ts->solvetime = ts->ptime;
3274b06615a5SLisandro Dalcin       solution = ts->vec_sol;
32750574a7fbSJed Brown     }
3276bc952696SBarry Smith     ierr = TSTrajectorySet(ts->trajectory,ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr);
3277b06615a5SLisandro Dalcin     ierr = TSMonitor(ts,ts->steps,ts->solvetime,solution);CHKERRQ(ierr);
327827829d71SBarry Smith     ierr = VecViewFromOptions(solution, ((PetscObject) ts)->prefix, "-ts_view_solution");CHKERRQ(ierr);
3279193ac0bcSJed Brown   }
3280d2daff3dSHong Zhang 
3281ce1779c8SBarry Smith   ierr = TSViewFromOptions(ts,NULL,"-ts_view");CHKERRQ(ierr);
328256f85f32SBarry Smith   ierr = PetscObjectSAWsBlock((PetscObject)ts);CHKERRQ(ierr);
32836a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
32846a4d4014SLisandro Dalcin }
32856a4d4014SLisandro Dalcin 
32866a4d4014SLisandro Dalcin #undef __FUNCT__
3287c88f2d44SBarry Smith #define __FUNCT__ "TSAdjointSolve"
3288c88f2d44SBarry Smith /*@
3289c88f2d44SBarry Smith    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE
3290c88f2d44SBarry Smith 
3291c88f2d44SBarry Smith    Collective on TS
3292c88f2d44SBarry Smith 
3293c88f2d44SBarry Smith    Input Parameter:
32942c39e106SBarry Smith .  ts - the TS context obtained from TSCreate()
3295c88f2d44SBarry Smith 
3296c88f2d44SBarry Smith    Level: intermediate
3297c88f2d44SBarry Smith 
3298c88f2d44SBarry Smith    Notes:
3299c88f2d44SBarry Smith    This must be called after a call to TSSolve() that solves the forward problem
3300c88f2d44SBarry Smith 
330173b18844SBarry Smith    By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time
330273b18844SBarry Smith 
3303c88f2d44SBarry Smith .keywords: TS, timestep, solve
3304c88f2d44SBarry Smith 
3305c88f2d44SBarry Smith .seealso: TSCreate(), TSSetSolution(), TSStep()
3306c88f2d44SBarry Smith @*/
33072c39e106SBarry Smith PetscErrorCode TSAdjointSolve(TS ts)
3308c88f2d44SBarry Smith {
3309c88f2d44SBarry Smith   PetscErrorCode    ierr;
3310c88f2d44SBarry Smith 
3311c88f2d44SBarry Smith   PetscFunctionBegin;
3312c88f2d44SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3313c88f2d44SBarry Smith   ierr = TSAdjointSetUp(ts);CHKERRQ(ierr);
3314c88f2d44SBarry Smith   /* reset time step and iteration counters */
3315c88f2d44SBarry Smith   ts->steps             = 0;
3316c88f2d44SBarry Smith   ts->ksp_its           = 0;
3317c88f2d44SBarry Smith   ts->snes_its          = 0;
3318c88f2d44SBarry Smith   ts->num_snes_failures = 0;
3319c88f2d44SBarry Smith   ts->reject            = 0;
3320c88f2d44SBarry Smith   ts->reason            = TS_CONVERGED_ITERATING;
3321c88f2d44SBarry Smith 
332273b18844SBarry Smith   if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->total_steps;
3323c88f2d44SBarry Smith 
332473b18844SBarry Smith   if (ts->steps >= ts->adjoint_max_steps)     ts->reason = TS_CONVERGED_ITS;
3325c88f2d44SBarry Smith   while (!ts->reason) {
332673b18844SBarry Smith     ierr = TSTrajectoryGet(ts->trajectory,ts,ts->adjoint_max_steps-ts->steps,ts->ptime);CHKERRQ(ierr);
332773b18844SBarry Smith     ierr = TSMonitor(ts,ts->adjoint_max_steps-ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr);
332808c7845fSBarry Smith     ierr = TSAdjointStep(ts);CHKERRQ(ierr);
3329c88f2d44SBarry Smith     if (ts->event) {
3330c88f2d44SBarry Smith       ierr = TSEventMonitor(ts);CHKERRQ(ierr);
3331c88f2d44SBarry Smith       if (ts->event->status != TSEVENT_PROCESSING) {
3332c88f2d44SBarry Smith         ierr = TSPostStep(ts);CHKERRQ(ierr);
3333c88f2d44SBarry Smith       }
3334c88f2d44SBarry Smith     } else {
3335c88f2d44SBarry Smith       ierr = TSPostStep(ts);CHKERRQ(ierr);
3336c88f2d44SBarry Smith     }
3337c88f2d44SBarry Smith   }
3338c88f2d44SBarry Smith   ts->solvetime = ts->ptime;
3339c88f2d44SBarry Smith   PetscFunctionReturn(0);
3340c88f2d44SBarry Smith }
3341c88f2d44SBarry Smith 
3342c88f2d44SBarry Smith #undef __FUNCT__
33434a2ae208SSatish Balay #define __FUNCT__ "TSMonitor"
3344228d79bcSJed Brown /*@
3345228d79bcSJed Brown    TSMonitor - Runs all user-provided monitor routines set using TSMonitorSet()
3346228d79bcSJed Brown 
3347228d79bcSJed Brown    Collective on TS
3348228d79bcSJed Brown 
3349228d79bcSJed Brown    Input Parameters:
3350228d79bcSJed Brown +  ts - time stepping context obtained from TSCreate()
3351228d79bcSJed Brown .  step - step number that has just completed
3352228d79bcSJed Brown .  ptime - model time of the state
33530910c330SBarry Smith -  u - state at the current model time
3354228d79bcSJed Brown 
3355228d79bcSJed Brown    Notes:
3356228d79bcSJed Brown    TSMonitor() is typically used within the time stepping implementations.
3357228d79bcSJed Brown    Users might call this function when using the TSStep() interface instead of TSSolve().
3358228d79bcSJed Brown 
3359228d79bcSJed Brown    Level: advanced
3360228d79bcSJed Brown 
3361228d79bcSJed Brown .keywords: TS, timestep
3362228d79bcSJed Brown @*/
33630910c330SBarry Smith PetscErrorCode TSMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u)
3364d763cef2SBarry Smith {
33656849ba73SBarry Smith   PetscErrorCode ierr;
3366a7cc72afSBarry Smith   PetscInt       i,n = ts->numbermonitors;
3367d763cef2SBarry Smith 
3368d763cef2SBarry Smith   PetscFunctionBegin;
3369b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3370b06615a5SLisandro Dalcin   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
33715edff71fSBarry Smith   ierr = VecLockPush(u);CHKERRQ(ierr);
3372d763cef2SBarry Smith   for (i=0; i<n; i++) {
33730910c330SBarry Smith     ierr = (*ts->monitor[i])(ts,step,ptime,u,ts->monitorcontext[i]);CHKERRQ(ierr);
3374d763cef2SBarry Smith   }
33755edff71fSBarry Smith   ierr = VecLockPop(u);CHKERRQ(ierr);
3376d763cef2SBarry Smith   PetscFunctionReturn(0);
3377d763cef2SBarry Smith }
3378d763cef2SBarry Smith 
3379d763cef2SBarry Smith /* ------------------------------------------------------------------------*/
33804a2ae208SSatish Balay #undef __FUNCT__
3381a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxCreate"
3382d763cef2SBarry Smith /*@C
3383a9f9c1f6SBarry Smith    TSMonitorLGCtxCreate - Creates a line graph context for use with
3384a9f9c1f6SBarry Smith    TS to monitor the solution process graphically in various ways
3385d763cef2SBarry Smith 
3386d763cef2SBarry Smith    Collective on TS
3387d763cef2SBarry Smith 
3388d763cef2SBarry Smith    Input Parameters:
3389d763cef2SBarry Smith +  host - the X display to open, or null for the local machine
3390d763cef2SBarry Smith .  label - the title to put in the title bar
33917c922b88SBarry Smith .  x, y - the screen coordinates of the upper left coordinate of the window
3392a9f9c1f6SBarry Smith .  m, n - the screen width and height in pixels
3393a9f9c1f6SBarry Smith -  howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
3394d763cef2SBarry Smith 
3395d763cef2SBarry Smith    Output Parameter:
33960b039ecaSBarry Smith .  ctx - the context
3397d763cef2SBarry Smith 
3398d763cef2SBarry Smith    Options Database Key:
33994f09c107SBarry Smith +  -ts_monitor_lg_timestep - automatically sets line graph monitor
34004f09c107SBarry Smith .  -ts_monitor_lg_solution -
340199fdda47SBarry Smith .  -ts_monitor_lg_error -
340299fdda47SBarry Smith .  -ts_monitor_lg_ksp_iterations -
340399fdda47SBarry Smith .  -ts_monitor_lg_snes_iterations -
340499fdda47SBarry Smith -  -lg_indicate_data_points <true,false> - indicate the data points (at each time step) on the plot; default is true
3405d763cef2SBarry Smith 
3406d763cef2SBarry Smith    Notes:
3407a9f9c1f6SBarry Smith    Use TSMonitorLGCtxDestroy() to destroy.
3408d763cef2SBarry Smith 
3409d763cef2SBarry Smith    Level: intermediate
3410d763cef2SBarry Smith 
34117c922b88SBarry Smith .keywords: TS, monitor, line graph, residual, seealso
3412d763cef2SBarry Smith 
34134f09c107SBarry Smith .seealso: TSMonitorLGTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
34147c922b88SBarry Smith 
3415d763cef2SBarry Smith @*/
3416a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorLGCtx *ctx)
3417d763cef2SBarry Smith {
3418b0a32e0cSBarry Smith   PetscDraw      win;
3419dfbe8321SBarry Smith   PetscErrorCode ierr;
3420d763cef2SBarry Smith 
3421d763cef2SBarry Smith   PetscFunctionBegin;
3422b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
3423a80ad3e0SBarry Smith   ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
34243fbbecb0SBarry Smith   ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
34250b039ecaSBarry Smith   ierr = PetscDrawLGCreate(win,1,&(*ctx)->lg);CHKERRQ(ierr);
34263bb1ff40SBarry Smith   ierr = PetscLogObjectParent((PetscObject)(*ctx)->lg,(PetscObject)win);CHKERRQ(ierr);
3427287de1a7SBarry Smith   ierr = PetscDrawLGIndicateDataPoints((*ctx)->lg,PETSC_TRUE);CHKERRQ(ierr);
3428287de1a7SBarry Smith   ierr = PetscDrawLGSetFromOptions((*ctx)->lg);CHKERRQ(ierr);
3429a9f9c1f6SBarry Smith   (*ctx)->howoften = howoften;
3430d763cef2SBarry Smith   PetscFunctionReturn(0);
3431d763cef2SBarry Smith }
3432d763cef2SBarry Smith 
34334a2ae208SSatish Balay #undef __FUNCT__
34344f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGTimeStep"
3435b06615a5SLisandro Dalcin PetscErrorCode TSMonitorLGTimeStep(TS ts,PetscInt step,PetscReal ptime,Vec v,void *monctx)
3436d763cef2SBarry Smith {
34370b039ecaSBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
3438c32365f1SBarry Smith   PetscReal      x   = ptime,y;
3439dfbe8321SBarry Smith   PetscErrorCode ierr;
3440d763cef2SBarry Smith 
3441d763cef2SBarry Smith   PetscFunctionBegin;
3442b06615a5SLisandro Dalcin   if (!step) {
3443a9f9c1f6SBarry Smith     PetscDrawAxis axis;
3444a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
3445a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Timestep as function of time","Time","Time step");CHKERRQ(ierr);
3446a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
3447287de1a7SBarry Smith     ierr = PetscDrawLGIndicateDataPoints(ctx->lg,PETSC_TRUE);CHKERRQ(ierr);
3448a9f9c1f6SBarry Smith   }
3449c32365f1SBarry Smith   ierr = TSGetTimeStep(ts,&y);CHKERRQ(ierr);
34500b039ecaSBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
3451b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
34520b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
34533923b477SBarry Smith   }
3454d763cef2SBarry Smith   PetscFunctionReturn(0);
3455d763cef2SBarry Smith }
3456d763cef2SBarry Smith 
34574a2ae208SSatish Balay #undef __FUNCT__
3458a9f9c1f6SBarry Smith #define __FUNCT__ "TSMonitorLGCtxDestroy"
3459d763cef2SBarry Smith /*@C
3460a9f9c1f6SBarry Smith    TSMonitorLGCtxDestroy - Destroys a line graph context that was created
3461a9f9c1f6SBarry Smith    with TSMonitorLGCtxCreate().
3462d763cef2SBarry Smith 
34630b039ecaSBarry Smith    Collective on TSMonitorLGCtx
3464d763cef2SBarry Smith 
3465d763cef2SBarry Smith    Input Parameter:
34660b039ecaSBarry Smith .  ctx - the monitor context
3467d763cef2SBarry Smith 
3468d763cef2SBarry Smith    Level: intermediate
3469d763cef2SBarry Smith 
3470d763cef2SBarry Smith .keywords: TS, monitor, line graph, destroy
3471d763cef2SBarry Smith 
34724f09c107SBarry Smith .seealso: TSMonitorLGCtxCreate(),  TSMonitorSet(), TSMonitorLGTimeStep();
3473d763cef2SBarry Smith @*/
3474a9f9c1f6SBarry Smith PetscErrorCode  TSMonitorLGCtxDestroy(TSMonitorLGCtx *ctx)
3475d763cef2SBarry Smith {
3476b0a32e0cSBarry Smith   PetscDraw      draw;
3477dfbe8321SBarry Smith   PetscErrorCode ierr;
3478d763cef2SBarry Smith 
3479d763cef2SBarry Smith   PetscFunctionBegin;
34800b039ecaSBarry Smith   ierr = PetscDrawLGGetDraw((*ctx)->lg,&draw);CHKERRQ(ierr);
34816bf464f9SBarry Smith   ierr = PetscDrawDestroy(&draw);CHKERRQ(ierr);
34820b039ecaSBarry Smith   ierr = PetscDrawLGDestroy(&(*ctx)->lg);CHKERRQ(ierr);
34830b039ecaSBarry Smith   ierr = PetscFree(*ctx);CHKERRQ(ierr);
3484d763cef2SBarry Smith   PetscFunctionReturn(0);
3485d763cef2SBarry Smith }
3486d763cef2SBarry Smith 
34874a2ae208SSatish Balay #undef __FUNCT__
34884a2ae208SSatish Balay #define __FUNCT__ "TSGetTime"
3489d763cef2SBarry Smith /*@
3490b8123daeSJed Brown    TSGetTime - Gets the time of the most recently completed step.
3491d763cef2SBarry Smith 
3492d763cef2SBarry Smith    Not Collective
3493d763cef2SBarry Smith 
3494d763cef2SBarry Smith    Input Parameter:
3495d763cef2SBarry Smith .  ts - the TS context obtained from TSCreate()
3496d763cef2SBarry Smith 
3497d763cef2SBarry Smith    Output Parameter:
3498d763cef2SBarry Smith .  t  - the current time
3499d763cef2SBarry Smith 
3500d763cef2SBarry Smith    Level: beginner
3501d763cef2SBarry Smith 
3502b8123daeSJed Brown    Note:
3503b8123daeSJed Brown    When called during time step evaluation (e.g. during residual evaluation or via hooks set using TSSetPreStep(),
35049be3e283SDebojyoti Ghosh    TSSetPreStage(), TSSetPostStage(), or TSSetPostStep()), the time is the time at the start of the step being evaluated.
3505b8123daeSJed Brown 
3506d763cef2SBarry Smith .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3507d763cef2SBarry Smith 
3508d763cef2SBarry Smith .keywords: TS, get, time
3509d763cef2SBarry Smith @*/
35107087cfbeSBarry Smith PetscErrorCode  TSGetTime(TS ts,PetscReal *t)
3511d763cef2SBarry Smith {
3512d763cef2SBarry Smith   PetscFunctionBegin;
35130700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3514f7cf8827SBarry Smith   PetscValidRealPointer(t,2);
3515d763cef2SBarry Smith   *t = ts->ptime;
3516d763cef2SBarry Smith   PetscFunctionReturn(0);
3517d763cef2SBarry Smith }
3518d763cef2SBarry Smith 
35194a2ae208SSatish Balay #undef __FUNCT__
3520e5e524a1SHong Zhang #define __FUNCT__ "TSGetPrevTime"
3521e5e524a1SHong Zhang /*@
3522e5e524a1SHong Zhang    TSGetPrevTime - Gets the starting time of the previously completed step.
3523e5e524a1SHong Zhang 
3524e5e524a1SHong Zhang    Not Collective
3525e5e524a1SHong Zhang 
3526e5e524a1SHong Zhang    Input Parameter:
3527e5e524a1SHong Zhang .  ts - the TS context obtained from TSCreate()
3528e5e524a1SHong Zhang 
3529e5e524a1SHong Zhang    Output Parameter:
3530e5e524a1SHong Zhang .  t  - the previous time
3531e5e524a1SHong Zhang 
3532e5e524a1SHong Zhang    Level: beginner
3533e5e524a1SHong Zhang 
3534e5e524a1SHong Zhang .seealso: TSSetInitialTimeStep(), TSGetTimeStep()
3535e5e524a1SHong Zhang 
3536e5e524a1SHong Zhang .keywords: TS, get, time
3537e5e524a1SHong Zhang @*/
3538e5e524a1SHong Zhang PetscErrorCode  TSGetPrevTime(TS ts,PetscReal *t)
3539e5e524a1SHong Zhang {
3540e5e524a1SHong Zhang   PetscFunctionBegin;
3541e5e524a1SHong Zhang   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3542e5e524a1SHong Zhang   PetscValidRealPointer(t,2);
3543e5e524a1SHong Zhang   *t = ts->ptime_prev;
3544e5e524a1SHong Zhang   PetscFunctionReturn(0);
3545e5e524a1SHong Zhang }
3546e5e524a1SHong Zhang 
3547e5e524a1SHong Zhang #undef __FUNCT__
35486a4d4014SLisandro Dalcin #define __FUNCT__ "TSSetTime"
35496a4d4014SLisandro Dalcin /*@
35506a4d4014SLisandro Dalcin    TSSetTime - Allows one to reset the time.
35516a4d4014SLisandro Dalcin 
35523f9fe445SBarry Smith    Logically Collective on TS
35536a4d4014SLisandro Dalcin 
35546a4d4014SLisandro Dalcin    Input Parameters:
35556a4d4014SLisandro Dalcin +  ts - the TS context obtained from TSCreate()
35566a4d4014SLisandro Dalcin -  time - the time
35576a4d4014SLisandro Dalcin 
35586a4d4014SLisandro Dalcin    Level: intermediate
35596a4d4014SLisandro Dalcin 
35606a4d4014SLisandro Dalcin .seealso: TSGetTime(), TSSetDuration()
35616a4d4014SLisandro Dalcin 
35626a4d4014SLisandro Dalcin .keywords: TS, set, time
35636a4d4014SLisandro Dalcin @*/
35647087cfbeSBarry Smith PetscErrorCode  TSSetTime(TS ts, PetscReal t)
35656a4d4014SLisandro Dalcin {
35666a4d4014SLisandro Dalcin   PetscFunctionBegin;
35670700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3568c5eb9154SBarry Smith   PetscValidLogicalCollectiveReal(ts,t,2);
35696a4d4014SLisandro Dalcin   ts->ptime = t;
35706a4d4014SLisandro Dalcin   PetscFunctionReturn(0);
35716a4d4014SLisandro Dalcin }
35726a4d4014SLisandro Dalcin 
35736a4d4014SLisandro Dalcin #undef __FUNCT__
35744a2ae208SSatish Balay #define __FUNCT__ "TSSetOptionsPrefix"
3575d763cef2SBarry Smith /*@C
3576d763cef2SBarry Smith    TSSetOptionsPrefix - Sets the prefix used for searching for all
3577d763cef2SBarry Smith    TS options in the database.
3578d763cef2SBarry Smith 
35793f9fe445SBarry Smith    Logically Collective on TS
3580d763cef2SBarry Smith 
3581d763cef2SBarry Smith    Input Parameter:
3582d763cef2SBarry Smith +  ts     - The TS context
3583d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3584d763cef2SBarry Smith 
3585d763cef2SBarry Smith    Notes:
3586d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3587d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3588d763cef2SBarry Smith    hyphen.
3589d763cef2SBarry Smith 
3590d763cef2SBarry Smith    Level: advanced
3591d763cef2SBarry Smith 
3592d763cef2SBarry Smith .keywords: TS, set, options, prefix, database
3593d763cef2SBarry Smith 
3594d763cef2SBarry Smith .seealso: TSSetFromOptions()
3595d763cef2SBarry Smith 
3596d763cef2SBarry Smith @*/
35977087cfbeSBarry Smith PetscErrorCode  TSSetOptionsPrefix(TS ts,const char prefix[])
3598d763cef2SBarry Smith {
3599dfbe8321SBarry Smith   PetscErrorCode ierr;
3600089b2837SJed Brown   SNES           snes;
3601d763cef2SBarry Smith 
3602d763cef2SBarry Smith   PetscFunctionBegin;
36030700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3604d763cef2SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3605089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3606089b2837SJed Brown   ierr = SNESSetOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3607d763cef2SBarry Smith   PetscFunctionReturn(0);
3608d763cef2SBarry Smith }
3609d763cef2SBarry Smith 
3610d763cef2SBarry Smith 
36114a2ae208SSatish Balay #undef __FUNCT__
36124a2ae208SSatish Balay #define __FUNCT__ "TSAppendOptionsPrefix"
3613d763cef2SBarry Smith /*@C
3614d763cef2SBarry Smith    TSAppendOptionsPrefix - Appends to the prefix used for searching for all
3615d763cef2SBarry Smith    TS options in the database.
3616d763cef2SBarry Smith 
36173f9fe445SBarry Smith    Logically Collective on TS
3618d763cef2SBarry Smith 
3619d763cef2SBarry Smith    Input Parameter:
3620d763cef2SBarry Smith +  ts     - The TS context
3621d763cef2SBarry Smith -  prefix - The prefix to prepend to all option names
3622d763cef2SBarry Smith 
3623d763cef2SBarry Smith    Notes:
3624d763cef2SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3625d763cef2SBarry Smith    The first character of all runtime options is AUTOMATICALLY the
3626d763cef2SBarry Smith    hyphen.
3627d763cef2SBarry Smith 
3628d763cef2SBarry Smith    Level: advanced
3629d763cef2SBarry Smith 
3630d763cef2SBarry Smith .keywords: TS, append, options, prefix, database
3631d763cef2SBarry Smith 
3632d763cef2SBarry Smith .seealso: TSGetOptionsPrefix()
3633d763cef2SBarry Smith 
3634d763cef2SBarry Smith @*/
36357087cfbeSBarry Smith PetscErrorCode  TSAppendOptionsPrefix(TS ts,const char prefix[])
3636d763cef2SBarry Smith {
3637dfbe8321SBarry Smith   PetscErrorCode ierr;
3638089b2837SJed Brown   SNES           snes;
3639d763cef2SBarry Smith 
3640d763cef2SBarry Smith   PetscFunctionBegin;
36410700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
3642d763cef2SBarry Smith   ierr = PetscObjectAppendOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3643089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3644089b2837SJed Brown   ierr = SNESAppendOptionsPrefix(snes,prefix);CHKERRQ(ierr);
3645d763cef2SBarry Smith   PetscFunctionReturn(0);
3646d763cef2SBarry Smith }
3647d763cef2SBarry Smith 
36484a2ae208SSatish Balay #undef __FUNCT__
36494a2ae208SSatish Balay #define __FUNCT__ "TSGetOptionsPrefix"
3650d763cef2SBarry Smith /*@C
3651d763cef2SBarry Smith    TSGetOptionsPrefix - Sets the prefix used for searching for all
3652d763cef2SBarry Smith    TS options in the database.
3653d763cef2SBarry Smith 
3654d763cef2SBarry Smith    Not Collective
3655d763cef2SBarry Smith 
3656d763cef2SBarry Smith    Input Parameter:
3657d763cef2SBarry Smith .  ts - The TS context
3658d763cef2SBarry Smith 
3659d763cef2SBarry Smith    Output Parameter:
3660d763cef2SBarry Smith .  prefix - A pointer to the prefix string used
3661d763cef2SBarry Smith 
3662d763cef2SBarry Smith    Notes: On the fortran side, the user should pass in a string 'prifix' of
3663d763cef2SBarry Smith    sufficient length to hold the prefix.
3664d763cef2SBarry Smith 
3665d763cef2SBarry Smith    Level: intermediate
3666d763cef2SBarry Smith 
3667d763cef2SBarry Smith .keywords: TS, get, options, prefix, database
3668d763cef2SBarry Smith 
3669d763cef2SBarry Smith .seealso: TSAppendOptionsPrefix()
3670d763cef2SBarry Smith @*/
36717087cfbeSBarry Smith PetscErrorCode  TSGetOptionsPrefix(TS ts,const char *prefix[])
3672d763cef2SBarry Smith {
3673dfbe8321SBarry Smith   PetscErrorCode ierr;
3674d763cef2SBarry Smith 
3675d763cef2SBarry Smith   PetscFunctionBegin;
36760700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
36774482741eSBarry Smith   PetscValidPointer(prefix,2);
3678d763cef2SBarry Smith   ierr = PetscObjectGetOptionsPrefix((PetscObject)ts,prefix);CHKERRQ(ierr);
3679d763cef2SBarry Smith   PetscFunctionReturn(0);
3680d763cef2SBarry Smith }
3681d763cef2SBarry Smith 
36824a2ae208SSatish Balay #undef __FUNCT__
36834a2ae208SSatish Balay #define __FUNCT__ "TSGetRHSJacobian"
3684d763cef2SBarry Smith /*@C
3685d763cef2SBarry Smith    TSGetRHSJacobian - Returns the Jacobian J at the present timestep.
3686d763cef2SBarry Smith 
3687d763cef2SBarry Smith    Not Collective, but parallel objects are returned if TS is parallel
3688d763cef2SBarry Smith 
3689d763cef2SBarry Smith    Input Parameter:
3690d763cef2SBarry Smith .  ts  - The TS context obtained from TSCreate()
3691d763cef2SBarry Smith 
3692d763cef2SBarry Smith    Output Parameters:
3693e4357dc4SBarry Smith +  Amat - The (approximate) Jacobian J of G, where U_t = G(U,t)  (or NULL)
3694e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, usually the same as Amat  (or NULL)
3695e4357dc4SBarry Smith .  func - Function to compute the Jacobian of the RHS  (or NULL)
3696e4357dc4SBarry Smith -  ctx - User-defined context for Jacobian evaluation routine  (or NULL)
3697d763cef2SBarry Smith 
36980298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
3699d763cef2SBarry Smith 
3700d763cef2SBarry Smith    Level: intermediate
3701d763cef2SBarry Smith 
370226d46c62SHong Zhang .seealso: TSGetTimeStep(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
3703d763cef2SBarry Smith 
3704d763cef2SBarry Smith .keywords: TS, timestep, get, matrix, Jacobian
3705d763cef2SBarry Smith @*/
3706e4357dc4SBarry Smith PetscErrorCode  TSGetRHSJacobian(TS ts,Mat *Amat,Mat *Pmat,TSRHSJacobian *func,void **ctx)
3707d763cef2SBarry Smith {
3708089b2837SJed Brown   PetscErrorCode ierr;
3709089b2837SJed Brown   SNES           snes;
371024989b8cSPeter Brune   DM             dm;
3711089b2837SJed Brown 
3712d763cef2SBarry Smith   PetscFunctionBegin;
3713089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3714e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
371524989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
371624989b8cSPeter Brune   ierr = DMTSGetRHSJacobian(dm,func,ctx);CHKERRQ(ierr);
3717d763cef2SBarry Smith   PetscFunctionReturn(0);
3718d763cef2SBarry Smith }
3719d763cef2SBarry Smith 
37201713a123SBarry Smith #undef __FUNCT__
37212eca1d9cSJed Brown #define __FUNCT__ "TSGetIJacobian"
37222eca1d9cSJed Brown /*@C
37232eca1d9cSJed Brown    TSGetIJacobian - Returns the implicit Jacobian at the present timestep.
37242eca1d9cSJed Brown 
37252eca1d9cSJed Brown    Not Collective, but parallel objects are returned if TS is parallel
37262eca1d9cSJed Brown 
37272eca1d9cSJed Brown    Input Parameter:
37282eca1d9cSJed Brown .  ts  - The TS context obtained from TSCreate()
37292eca1d9cSJed Brown 
37302eca1d9cSJed Brown    Output Parameters:
3731e4357dc4SBarry Smith +  Amat  - The (approximate) Jacobian of F(t,U,U_t)
3732e4357dc4SBarry Smith .  Pmat - The matrix from which the preconditioner is constructed, often the same as Amat
37332eca1d9cSJed Brown .  f   - The function to compute the matrices
37342eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine
37352eca1d9cSJed Brown 
37360298fd71SBarry Smith    Notes: You can pass in NULL for any return argument you do not need.
37372eca1d9cSJed Brown 
37382eca1d9cSJed Brown    Level: advanced
37392eca1d9cSJed Brown 
37402eca1d9cSJed Brown .seealso: TSGetTimeStep(), TSGetRHSJacobian(), TSGetMatrices(), TSGetTime(), TSGetTimeStepNumber()
37412eca1d9cSJed Brown 
37422eca1d9cSJed Brown .keywords: TS, timestep, get, matrix, Jacobian
37432eca1d9cSJed Brown @*/
3744e4357dc4SBarry Smith PetscErrorCode  TSGetIJacobian(TS ts,Mat *Amat,Mat *Pmat,TSIJacobian *f,void **ctx)
37452eca1d9cSJed Brown {
3746089b2837SJed Brown   PetscErrorCode ierr;
3747089b2837SJed Brown   SNES           snes;
374824989b8cSPeter Brune   DM             dm;
37490910c330SBarry Smith 
37502eca1d9cSJed Brown   PetscFunctionBegin;
3751089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
3752f7d39f7aSBarry Smith   ierr = SNESSetUpMatrices(snes);CHKERRQ(ierr);
3753e4357dc4SBarry Smith   ierr = SNESGetJacobian(snes,Amat,Pmat,NULL,NULL);CHKERRQ(ierr);
375424989b8cSPeter Brune   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
375524989b8cSPeter Brune   ierr = DMTSGetIJacobian(dm,f,ctx);CHKERRQ(ierr);
37562eca1d9cSJed Brown   PetscFunctionReturn(0);
37572eca1d9cSJed Brown }
37582eca1d9cSJed Brown 
37596083293cSBarry Smith 
37602eca1d9cSJed Brown #undef __FUNCT__
376183a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawSolution"
37621713a123SBarry Smith /*@C
376383a4ac43SBarry Smith    TSMonitorDrawSolution - Monitors progress of the TS solvers by calling
37641713a123SBarry Smith    VecView() for the solution at each timestep
37651713a123SBarry Smith 
37661713a123SBarry Smith    Collective on TS
37671713a123SBarry Smith 
37681713a123SBarry Smith    Input Parameters:
37691713a123SBarry Smith +  ts - the TS context
37701713a123SBarry Smith .  step - current time-step
3771142b95e3SSatish Balay .  ptime - current time
37720298fd71SBarry Smith -  dummy - either a viewer or NULL
37731713a123SBarry Smith 
377499fdda47SBarry Smith    Options Database:
377599fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
377699fdda47SBarry Smith 
377799fdda47SBarry 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
377899fdda47SBarry Smith        will look bad
377999fdda47SBarry Smith 
37801713a123SBarry Smith    Level: intermediate
37811713a123SBarry Smith 
37821713a123SBarry Smith .keywords: TS,  vector, monitor, view
37831713a123SBarry Smith 
3784a6570f20SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
37851713a123SBarry Smith @*/
37860910c330SBarry Smith PetscErrorCode  TSMonitorDrawSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
37871713a123SBarry Smith {
3788dfbe8321SBarry Smith   PetscErrorCode   ierr;
378983a4ac43SBarry Smith   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
3790473a3ab2SBarry Smith   PetscDraw        draw;
37911713a123SBarry Smith 
37921713a123SBarry Smith   PetscFunctionBegin;
37936083293cSBarry Smith   if (!step && ictx->showinitial) {
37946083293cSBarry Smith     if (!ictx->initialsolution) {
37950910c330SBarry Smith       ierr = VecDuplicate(u,&ictx->initialsolution);CHKERRQ(ierr);
37961713a123SBarry Smith     }
37970910c330SBarry Smith     ierr = VecCopy(u,ictx->initialsolution);CHKERRQ(ierr);
37986083293cSBarry Smith   }
3799b06615a5SLisandro Dalcin   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
38000dcf80beSBarry Smith 
38016083293cSBarry Smith   if (ictx->showinitial) {
38026083293cSBarry Smith     PetscReal pause;
38036083293cSBarry Smith     ierr = PetscViewerDrawGetPause(ictx->viewer,&pause);CHKERRQ(ierr);
38046083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,0.0);CHKERRQ(ierr);
38056083293cSBarry Smith     ierr = VecView(ictx->initialsolution,ictx->viewer);CHKERRQ(ierr);
38066083293cSBarry Smith     ierr = PetscViewerDrawSetPause(ictx->viewer,pause);CHKERRQ(ierr);
38076083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_TRUE);CHKERRQ(ierr);
38086083293cSBarry Smith   }
38090910c330SBarry Smith   ierr = VecView(u,ictx->viewer);CHKERRQ(ierr);
3810473a3ab2SBarry Smith   if (ictx->showtimestepandtime) {
3811473a3ab2SBarry Smith     PetscReal xl,yl,xr,yr,tw,w,h;
3812473a3ab2SBarry Smith     char      time[32];
3813473a3ab2SBarry Smith     size_t    len;
3814473a3ab2SBarry Smith 
3815473a3ab2SBarry Smith     ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
381685b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
3817473a3ab2SBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
3818473a3ab2SBarry Smith     ierr =  PetscStrlen(time,&len);CHKERRQ(ierr);
38190298fd71SBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
3820473a3ab2SBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
3821473a3ab2SBarry Smith     h    = yl + .95*(yr - yl);
3822473a3ab2SBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
3823473a3ab2SBarry Smith     ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
3824473a3ab2SBarry Smith   }
3825473a3ab2SBarry Smith 
38266083293cSBarry Smith   if (ictx->showinitial) {
38276083293cSBarry Smith     ierr = PetscViewerDrawSetHold(ictx->viewer,PETSC_FALSE);CHKERRQ(ierr);
38286083293cSBarry Smith   }
38291713a123SBarry Smith   PetscFunctionReturn(0);
38301713a123SBarry Smith }
38311713a123SBarry Smith 
38322d5ee99bSBarry Smith #undef __FUNCT__
38332d5ee99bSBarry Smith #define __FUNCT__ "TSMonitorDrawSolutionPhase"
38342d5ee99bSBarry Smith /*@C
38352d5ee99bSBarry Smith    TSMonitorDrawSolutionPhase - Monitors progress of the TS solvers by plotting the solution as a phase diagram
38362d5ee99bSBarry Smith 
38372d5ee99bSBarry Smith    Collective on TS
38382d5ee99bSBarry Smith 
38392d5ee99bSBarry Smith    Input Parameters:
38402d5ee99bSBarry Smith +  ts - the TS context
38412d5ee99bSBarry Smith .  step - current time-step
38422d5ee99bSBarry Smith .  ptime - current time
38432d5ee99bSBarry Smith -  dummy - either a viewer or NULL
38442d5ee99bSBarry Smith 
38452d5ee99bSBarry Smith    Level: intermediate
38462d5ee99bSBarry Smith 
38472d5ee99bSBarry Smith .keywords: TS,  vector, monitor, view
38482d5ee99bSBarry Smith 
38492d5ee99bSBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
38502d5ee99bSBarry Smith @*/
38512d5ee99bSBarry Smith PetscErrorCode  TSMonitorDrawSolutionPhase(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
38522d5ee99bSBarry Smith {
38532d5ee99bSBarry Smith   PetscErrorCode    ierr;
38542d5ee99bSBarry Smith   TSMonitorDrawCtx  ictx = (TSMonitorDrawCtx)dummy;
38552d5ee99bSBarry Smith   PetscDraw         draw;
38562d5ee99bSBarry Smith   MPI_Comm          comm;
38572d5ee99bSBarry Smith   PetscInt          n;
38582d5ee99bSBarry Smith   PetscMPIInt       size;
38592d5ee99bSBarry Smith   PetscReal         xl,yl,xr,yr,tw,w,h;
38602d5ee99bSBarry Smith   char              time[32];
38612d5ee99bSBarry Smith   size_t            len;
38622d5ee99bSBarry Smith   const PetscScalar *U;
38632d5ee99bSBarry Smith 
38642d5ee99bSBarry Smith   PetscFunctionBegin;
38652d5ee99bSBarry Smith   ierr = PetscObjectGetComm((PetscObject)ts,&comm);CHKERRQ(ierr);
38662d5ee99bSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
38672d5ee99bSBarry Smith   if (size != 1) SETERRQ(comm,PETSC_ERR_SUP,"Only allowed for sequential runs");
38682d5ee99bSBarry Smith   ierr = VecGetSize(u,&n);CHKERRQ(ierr);
38692d5ee99bSBarry Smith   if (n != 2) SETERRQ(comm,PETSC_ERR_SUP,"Only for ODEs with two unknowns");
38702d5ee99bSBarry Smith 
38712d5ee99bSBarry Smith   ierr = PetscViewerDrawGetDraw(ictx->viewer,0,&draw);CHKERRQ(ierr);
38722d5ee99bSBarry Smith 
38732d5ee99bSBarry Smith   ierr = VecGetArrayRead(u,&U);CHKERRQ(ierr);
38744b363babSBarry Smith   ierr = PetscDrawAxisGetLimits(ictx->axis,&xl,&xr,&yl,&yr);CHKERRQ(ierr);
3875ba5783adSMatthew G Knepley   if ((PetscRealPart(U[0]) < xl) || (PetscRealPart(U[1]) < yl) || (PetscRealPart(U[0]) > xr) || (PetscRealPart(U[1]) > yr)) {
3876a4494fc1SBarry Smith       ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
3877a4494fc1SBarry Smith       PetscFunctionReturn(0);
3878a4494fc1SBarry Smith   }
38792d5ee99bSBarry Smith   if (!step) ictx->color++;
38802d5ee99bSBarry Smith   ierr = PetscDrawPoint(draw,PetscRealPart(U[0]),PetscRealPart(U[1]),ictx->color);CHKERRQ(ierr);
38812d5ee99bSBarry Smith   ierr = VecRestoreArrayRead(u,&U);CHKERRQ(ierr);
38822d5ee99bSBarry Smith 
38832d5ee99bSBarry Smith   if (ictx->showtimestepandtime) {
38844b363babSBarry Smith     ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
388585b1acf9SLisandro Dalcin     ierr = PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);CHKERRQ(ierr);
38862d5ee99bSBarry Smith     ierr = PetscStrlen(time,&len);CHKERRQ(ierr);
38872d5ee99bSBarry Smith     ierr = PetscDrawStringGetSize(draw,&tw,NULL);CHKERRQ(ierr);
38882d5ee99bSBarry Smith     w    = xl + .5*(xr - xl) - .5*len*tw;
38892d5ee99bSBarry Smith     h    = yl + .95*(yr - yl);
38902d5ee99bSBarry Smith     ierr = PetscDrawString(draw,w,h,PETSC_DRAW_BLACK,time);CHKERRQ(ierr);
38912d5ee99bSBarry Smith   }
38922d5ee99bSBarry Smith   ierr = PetscDrawFlush(draw);CHKERRQ(ierr);
38932d5ee99bSBarry Smith   PetscFunctionReturn(0);
38942d5ee99bSBarry Smith }
38952d5ee99bSBarry Smith 
38961713a123SBarry Smith 
38976c699258SBarry Smith #undef __FUNCT__
389883a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxDestroy"
38996083293cSBarry Smith /*@C
390083a4ac43SBarry Smith    TSMonitorDrawCtxDestroy - Destroys the monitor context for TSMonitorDrawSolution()
39016083293cSBarry Smith 
39026083293cSBarry Smith    Collective on TS
39036083293cSBarry Smith 
39046083293cSBarry Smith    Input Parameters:
39056083293cSBarry Smith .    ctx - the monitor context
39066083293cSBarry Smith 
39076083293cSBarry Smith    Level: intermediate
39086083293cSBarry Smith 
39096083293cSBarry Smith .keywords: TS,  vector, monitor, view
39106083293cSBarry Smith 
391183a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawSolution(), TSMonitorDrawError()
39126083293cSBarry Smith @*/
391383a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxDestroy(TSMonitorDrawCtx *ictx)
39146083293cSBarry Smith {
39156083293cSBarry Smith   PetscErrorCode ierr;
39166083293cSBarry Smith 
39176083293cSBarry Smith   PetscFunctionBegin;
39184b363babSBarry Smith   ierr = PetscDrawAxisDestroy(&(*ictx)->axis);CHKERRQ(ierr);
391983a4ac43SBarry Smith   ierr = PetscViewerDestroy(&(*ictx)->viewer);CHKERRQ(ierr);
392083a4ac43SBarry Smith   ierr = VecDestroy(&(*ictx)->initialsolution);CHKERRQ(ierr);
392183a4ac43SBarry Smith   ierr = PetscFree(*ictx);CHKERRQ(ierr);
39226083293cSBarry Smith   PetscFunctionReturn(0);
39236083293cSBarry Smith }
39246083293cSBarry Smith 
39256083293cSBarry Smith #undef __FUNCT__
392683a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawCtxCreate"
39276083293cSBarry Smith /*@C
392883a4ac43SBarry Smith    TSMonitorDrawCtxCreate - Creates the monitor context for TSMonitorDrawCtx
39296083293cSBarry Smith 
39306083293cSBarry Smith    Collective on TS
39316083293cSBarry Smith 
39326083293cSBarry Smith    Input Parameter:
39336083293cSBarry Smith .    ts - time-step context
39346083293cSBarry Smith 
39356083293cSBarry Smith    Output Patameter:
39366083293cSBarry Smith .    ctx - the monitor context
39376083293cSBarry Smith 
393899fdda47SBarry Smith    Options Database:
393999fdda47SBarry Smith .   -ts_monitor_draw_solution_initial - show initial solution as well as current solution
394099fdda47SBarry Smith 
39416083293cSBarry Smith    Level: intermediate
39426083293cSBarry Smith 
39436083293cSBarry Smith .keywords: TS,  vector, monitor, view
39446083293cSBarry Smith 
394583a4ac43SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSMonitorDrawCtx()
39466083293cSBarry Smith @*/
394783a4ac43SBarry Smith PetscErrorCode  TSMonitorDrawCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorDrawCtx *ctx)
39486083293cSBarry Smith {
39496083293cSBarry Smith   PetscErrorCode   ierr;
39506083293cSBarry Smith 
39516083293cSBarry Smith   PetscFunctionBegin;
3952b00a9115SJed Brown   ierr = PetscNew(ctx);CHKERRQ(ierr);
395383a4ac43SBarry Smith   ierr = PetscViewerDrawOpen(comm,host,label,x,y,m,n,&(*ctx)->viewer);CHKERRQ(ierr);
3954e9457bf7SBarry Smith   ierr = PetscViewerSetFromOptions((*ctx)->viewer);CHKERRQ(ierr);
3955bbd56ea5SKarl Rupp 
395683a4ac43SBarry Smith   (*ctx)->howoften    = howoften;
3957473a3ab2SBarry Smith   (*ctx)->showinitial = PETSC_FALSE;
39580298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_initial",&(*ctx)->showinitial,NULL);CHKERRQ(ierr);
3959473a3ab2SBarry Smith 
3960473a3ab2SBarry Smith   (*ctx)->showtimestepandtime = PETSC_FALSE;
39610298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-ts_monitor_draw_solution_show_time",&(*ctx)->showtimestepandtime,NULL);CHKERRQ(ierr);
39622d5ee99bSBarry Smith   (*ctx)->color = PETSC_DRAW_WHITE;
39636083293cSBarry Smith   PetscFunctionReturn(0);
39646083293cSBarry Smith }
39656083293cSBarry Smith 
39666083293cSBarry Smith #undef __FUNCT__
396783a4ac43SBarry Smith #define __FUNCT__ "TSMonitorDrawError"
39683a471f94SBarry Smith /*@C
396983a4ac43SBarry Smith    TSMonitorDrawError - Monitors progress of the TS solvers by calling
39703a471f94SBarry Smith    VecView() for the error at each timestep
39713a471f94SBarry Smith 
39723a471f94SBarry Smith    Collective on TS
39733a471f94SBarry Smith 
39743a471f94SBarry Smith    Input Parameters:
39753a471f94SBarry Smith +  ts - the TS context
39763a471f94SBarry Smith .  step - current time-step
39773a471f94SBarry Smith .  ptime - current time
39780298fd71SBarry Smith -  dummy - either a viewer or NULL
39793a471f94SBarry Smith 
39803a471f94SBarry Smith    Level: intermediate
39813a471f94SBarry Smith 
39823a471f94SBarry Smith .keywords: TS,  vector, monitor, view
39833a471f94SBarry Smith 
39843a471f94SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
39853a471f94SBarry Smith @*/
39860910c330SBarry Smith PetscErrorCode  TSMonitorDrawError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
39873a471f94SBarry Smith {
39883a471f94SBarry Smith   PetscErrorCode   ierr;
398983a4ac43SBarry Smith   TSMonitorDrawCtx ctx    = (TSMonitorDrawCtx)dummy;
399083a4ac43SBarry Smith   PetscViewer      viewer = ctx->viewer;
39913a471f94SBarry Smith   Vec              work;
39923a471f94SBarry Smith 
39933a471f94SBarry Smith   PetscFunctionBegin;
3994b06615a5SLisandro Dalcin   if (!(((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason))) PetscFunctionReturn(0);
39950910c330SBarry Smith   ierr = VecDuplicate(u,&work);CHKERRQ(ierr);
39963a471f94SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,work);CHKERRQ(ierr);
39970910c330SBarry Smith   ierr = VecAXPY(work,-1.0,u);CHKERRQ(ierr);
39983a471f94SBarry Smith   ierr = VecView(work,viewer);CHKERRQ(ierr);
39993a471f94SBarry Smith   ierr = VecDestroy(&work);CHKERRQ(ierr);
40003a471f94SBarry Smith   PetscFunctionReturn(0);
40013a471f94SBarry Smith }
40023a471f94SBarry Smith 
40032a34c10cSBarry Smith #include <petsc-private/dmimpl.h>
40043a471f94SBarry Smith #undef __FUNCT__
40056c699258SBarry Smith #define __FUNCT__ "TSSetDM"
40066c699258SBarry Smith /*@
40076c699258SBarry Smith    TSSetDM - Sets the DM that may be used by some preconditioners
40086c699258SBarry Smith 
40093f9fe445SBarry Smith    Logically Collective on TS and DM
40106c699258SBarry Smith 
40116c699258SBarry Smith    Input Parameters:
40126c699258SBarry Smith +  ts - the preconditioner context
40136c699258SBarry Smith -  dm - the dm
40146c699258SBarry Smith 
40156c699258SBarry Smith    Level: intermediate
40166c699258SBarry Smith 
40176c699258SBarry Smith 
40186c699258SBarry Smith .seealso: TSGetDM(), SNESSetDM(), SNESGetDM()
40196c699258SBarry Smith @*/
40207087cfbeSBarry Smith PetscErrorCode  TSSetDM(TS ts,DM dm)
40216c699258SBarry Smith {
40226c699258SBarry Smith   PetscErrorCode ierr;
4023089b2837SJed Brown   SNES           snes;
4024942e3340SBarry Smith   DMTS           tsdm;
40256c699258SBarry Smith 
40266c699258SBarry Smith   PetscFunctionBegin;
40270700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
402870663e4aSLisandro Dalcin   ierr = PetscObjectReference((PetscObject)dm);CHKERRQ(ierr);
4029942e3340SBarry Smith   if (ts->dm) {               /* Move the DMTS context over to the new DM unless the new DM already has one */
40302a34c10cSBarry Smith     if (ts->dm->dmts && !dm->dmts) {
4031942e3340SBarry Smith       ierr = DMCopyDMTS(ts->dm,dm);CHKERRQ(ierr);
4032942e3340SBarry Smith       ierr = DMGetDMTS(ts->dm,&tsdm);CHKERRQ(ierr);
403324989b8cSPeter Brune       if (tsdm->originaldm == ts->dm) { /* Grant write privileges to the replacement DM */
403424989b8cSPeter Brune         tsdm->originaldm = dm;
403524989b8cSPeter Brune       }
403624989b8cSPeter Brune     }
40376bf464f9SBarry Smith     ierr = DMDestroy(&ts->dm);CHKERRQ(ierr);
403824989b8cSPeter Brune   }
40396c699258SBarry Smith   ts->dm = dm;
4040bbd56ea5SKarl Rupp 
4041089b2837SJed Brown   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
4042089b2837SJed Brown   ierr = SNESSetDM(snes,dm);CHKERRQ(ierr);
40436c699258SBarry Smith   PetscFunctionReturn(0);
40446c699258SBarry Smith }
40456c699258SBarry Smith 
40466c699258SBarry Smith #undef __FUNCT__
40476c699258SBarry Smith #define __FUNCT__ "TSGetDM"
40486c699258SBarry Smith /*@
40496c699258SBarry Smith    TSGetDM - Gets the DM that may be used by some preconditioners
40506c699258SBarry Smith 
40513f9fe445SBarry Smith    Not Collective
40526c699258SBarry Smith 
40536c699258SBarry Smith    Input Parameter:
40546c699258SBarry Smith . ts - the preconditioner context
40556c699258SBarry Smith 
40566c699258SBarry Smith    Output Parameter:
40576c699258SBarry Smith .  dm - the dm
40586c699258SBarry Smith 
40596c699258SBarry Smith    Level: intermediate
40606c699258SBarry Smith 
40616c699258SBarry Smith 
40626c699258SBarry Smith .seealso: TSSetDM(), SNESSetDM(), SNESGetDM()
40636c699258SBarry Smith @*/
40647087cfbeSBarry Smith PetscErrorCode  TSGetDM(TS ts,DM *dm)
40656c699258SBarry Smith {
4066496e6a7aSJed Brown   PetscErrorCode ierr;
4067496e6a7aSJed Brown 
40686c699258SBarry Smith   PetscFunctionBegin;
40690700a824SBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4070496e6a7aSJed Brown   if (!ts->dm) {
4071ce94432eSBarry Smith     ierr = DMShellCreate(PetscObjectComm((PetscObject)ts),&ts->dm);CHKERRQ(ierr);
4072496e6a7aSJed Brown     if (ts->snes) {ierr = SNESSetDM(ts->snes,ts->dm);CHKERRQ(ierr);}
4073496e6a7aSJed Brown   }
40746c699258SBarry Smith   *dm = ts->dm;
40756c699258SBarry Smith   PetscFunctionReturn(0);
40766c699258SBarry Smith }
40771713a123SBarry Smith 
40780f5c6efeSJed Brown #undef __FUNCT__
40790f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormFunction"
40800f5c6efeSJed Brown /*@
40810f5c6efeSJed Brown    SNESTSFormFunction - Function to evaluate nonlinear residual
40820f5c6efeSJed Brown 
40833f9fe445SBarry Smith    Logically Collective on SNES
40840f5c6efeSJed Brown 
40850f5c6efeSJed Brown    Input Parameter:
4086d42a1c89SJed Brown + snes - nonlinear solver
40870910c330SBarry Smith . U - the current state at which to evaluate the residual
4088d42a1c89SJed Brown - ctx - user context, must be a TS
40890f5c6efeSJed Brown 
40900f5c6efeSJed Brown    Output Parameter:
40910f5c6efeSJed Brown . F - the nonlinear residual
40920f5c6efeSJed Brown 
40930f5c6efeSJed Brown    Notes:
40940f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
40950f5c6efeSJed Brown    It is most frequently passed to MatFDColoringSetFunction().
40960f5c6efeSJed Brown 
40970f5c6efeSJed Brown    Level: advanced
40980f5c6efeSJed Brown 
40990f5c6efeSJed Brown .seealso: SNESSetFunction(), MatFDColoringSetFunction()
41000f5c6efeSJed Brown @*/
41010910c330SBarry Smith PetscErrorCode  SNESTSFormFunction(SNES snes,Vec U,Vec F,void *ctx)
41020f5c6efeSJed Brown {
41030f5c6efeSJed Brown   TS             ts = (TS)ctx;
41040f5c6efeSJed Brown   PetscErrorCode ierr;
41050f5c6efeSJed Brown 
41060f5c6efeSJed Brown   PetscFunctionBegin;
41070f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
41080910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
41090f5c6efeSJed Brown   PetscValidHeaderSpecific(F,VEC_CLASSID,3);
41100f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,4);
41110910c330SBarry Smith   ierr = (ts->ops->snesfunction)(snes,U,F,ts);CHKERRQ(ierr);
41120f5c6efeSJed Brown   PetscFunctionReturn(0);
41130f5c6efeSJed Brown }
41140f5c6efeSJed Brown 
41150f5c6efeSJed Brown #undef __FUNCT__
41160f5c6efeSJed Brown #define __FUNCT__ "SNESTSFormJacobian"
41170f5c6efeSJed Brown /*@
41180f5c6efeSJed Brown    SNESTSFormJacobian - Function to evaluate the Jacobian
41190f5c6efeSJed Brown 
41200f5c6efeSJed Brown    Collective on SNES
41210f5c6efeSJed Brown 
41220f5c6efeSJed Brown    Input Parameter:
41230f5c6efeSJed Brown + snes - nonlinear solver
41240910c330SBarry Smith . U - the current state at which to evaluate the residual
41250f5c6efeSJed Brown - ctx - user context, must be a TS
41260f5c6efeSJed Brown 
41270f5c6efeSJed Brown    Output Parameter:
41280f5c6efeSJed Brown + A - the Jacobian
41290f5c6efeSJed Brown . B - the preconditioning matrix (may be the same as A)
41300f5c6efeSJed Brown - flag - indicates any structure change in the matrix
41310f5c6efeSJed Brown 
41320f5c6efeSJed Brown    Notes:
41330f5c6efeSJed Brown    This function is not normally called by users and is automatically registered with the SNES used by TS.
41340f5c6efeSJed Brown 
41350f5c6efeSJed Brown    Level: developer
41360f5c6efeSJed Brown 
41370f5c6efeSJed Brown .seealso: SNESSetJacobian()
41380f5c6efeSJed Brown @*/
4139d1e9a80fSBarry Smith PetscErrorCode  SNESTSFormJacobian(SNES snes,Vec U,Mat A,Mat B,void *ctx)
41400f5c6efeSJed Brown {
41410f5c6efeSJed Brown   TS             ts = (TS)ctx;
41420f5c6efeSJed Brown   PetscErrorCode ierr;
41430f5c6efeSJed Brown 
41440f5c6efeSJed Brown   PetscFunctionBegin;
41450f5c6efeSJed Brown   PetscValidHeaderSpecific(snes,SNES_CLASSID,1);
41460910c330SBarry Smith   PetscValidHeaderSpecific(U,VEC_CLASSID,2);
41470f5c6efeSJed Brown   PetscValidPointer(A,3);
414894ab13aaSBarry Smith   PetscValidHeaderSpecific(A,MAT_CLASSID,3);
41490f5c6efeSJed Brown   PetscValidPointer(B,4);
415094ab13aaSBarry Smith   PetscValidHeaderSpecific(B,MAT_CLASSID,4);
41510f5c6efeSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,6);
4152d1e9a80fSBarry Smith   ierr = (ts->ops->snesjacobian)(snes,U,A,B,ts);CHKERRQ(ierr);
41530f5c6efeSJed Brown   PetscFunctionReturn(0);
41540f5c6efeSJed Brown }
4155325fc9f4SBarry Smith 
41560e4ef248SJed Brown #undef __FUNCT__
41570e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSFunctionLinear"
41580e4ef248SJed Brown /*@C
41590e4ef248SJed Brown    TSComputeRHSFunctionLinear - Evaluate the right hand side via the user-provided Jacobian, for linear problems only
41600e4ef248SJed Brown 
41610e4ef248SJed Brown    Collective on TS
41620e4ef248SJed Brown 
41630e4ef248SJed Brown    Input Arguments:
41640e4ef248SJed Brown +  ts - time stepping context
41650e4ef248SJed Brown .  t - time at which to evaluate
41660910c330SBarry Smith .  U - state at which to evaluate
41670e4ef248SJed Brown -  ctx - context
41680e4ef248SJed Brown 
41690e4ef248SJed Brown    Output Arguments:
41700e4ef248SJed Brown .  F - right hand side
41710e4ef248SJed Brown 
41720e4ef248SJed Brown    Level: intermediate
41730e4ef248SJed Brown 
41740e4ef248SJed Brown    Notes:
41750e4ef248SJed Brown    This function is intended to be passed to TSSetRHSFunction() to evaluate the right hand side for linear problems.
41760e4ef248SJed Brown    The matrix (and optionally the evaluation context) should be passed to TSSetRHSJacobian().
41770e4ef248SJed Brown 
41780e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSJacobianConstant()
41790e4ef248SJed Brown @*/
41800910c330SBarry Smith PetscErrorCode TSComputeRHSFunctionLinear(TS ts,PetscReal t,Vec U,Vec F,void *ctx)
41810e4ef248SJed Brown {
41820e4ef248SJed Brown   PetscErrorCode ierr;
41830e4ef248SJed Brown   Mat            Arhs,Brhs;
41840e4ef248SJed Brown 
41850e4ef248SJed Brown   PetscFunctionBegin;
41860e4ef248SJed Brown   ierr = TSGetRHSMats_Private(ts,&Arhs,&Brhs);CHKERRQ(ierr);
4187d1e9a80fSBarry Smith   ierr = TSComputeRHSJacobian(ts,t,U,Arhs,Brhs);CHKERRQ(ierr);
41880910c330SBarry Smith   ierr = MatMult(Arhs,U,F);CHKERRQ(ierr);
41890e4ef248SJed Brown   PetscFunctionReturn(0);
41900e4ef248SJed Brown }
41910e4ef248SJed Brown 
41920e4ef248SJed Brown #undef __FUNCT__
41930e4ef248SJed Brown #define __FUNCT__ "TSComputeRHSJacobianConstant"
41940e4ef248SJed Brown /*@C
41950e4ef248SJed Brown    TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent.
41960e4ef248SJed Brown 
41970e4ef248SJed Brown    Collective on TS
41980e4ef248SJed Brown 
41990e4ef248SJed Brown    Input Arguments:
42000e4ef248SJed Brown +  ts - time stepping context
42010e4ef248SJed Brown .  t - time at which to evaluate
42020910c330SBarry Smith .  U - state at which to evaluate
42030e4ef248SJed Brown -  ctx - context
42040e4ef248SJed Brown 
42050e4ef248SJed Brown    Output Arguments:
42060e4ef248SJed Brown +  A - pointer to operator
42070e4ef248SJed Brown .  B - pointer to preconditioning matrix
42080e4ef248SJed Brown -  flg - matrix structure flag
42090e4ef248SJed Brown 
42100e4ef248SJed Brown    Level: intermediate
42110e4ef248SJed Brown 
42120e4ef248SJed Brown    Notes:
42130e4ef248SJed Brown    This function is intended to be passed to TSSetRHSJacobian() to evaluate the Jacobian for linear time-independent problems.
42140e4ef248SJed Brown 
42150e4ef248SJed Brown .seealso: TSSetRHSFunction(), TSSetRHSJacobian(), TSComputeRHSFunctionLinear()
42160e4ef248SJed Brown @*/
4217d1e9a80fSBarry Smith PetscErrorCode TSComputeRHSJacobianConstant(TS ts,PetscReal t,Vec U,Mat A,Mat B,void *ctx)
42180e4ef248SJed Brown {
42190e4ef248SJed Brown   PetscFunctionBegin;
42200e4ef248SJed Brown   PetscFunctionReturn(0);
42210e4ef248SJed Brown }
42220e4ef248SJed Brown 
42230026cea9SSean Farley #undef __FUNCT__
42240026cea9SSean Farley #define __FUNCT__ "TSComputeIFunctionLinear"
42250026cea9SSean Farley /*@C
42260026cea9SSean Farley    TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only
42270026cea9SSean Farley 
42280026cea9SSean Farley    Collective on TS
42290026cea9SSean Farley 
42300026cea9SSean Farley    Input Arguments:
42310026cea9SSean Farley +  ts - time stepping context
42320026cea9SSean Farley .  t - time at which to evaluate
42330910c330SBarry Smith .  U - state at which to evaluate
42340910c330SBarry Smith .  Udot - time derivative of state vector
42350026cea9SSean Farley -  ctx - context
42360026cea9SSean Farley 
42370026cea9SSean Farley    Output Arguments:
42380026cea9SSean Farley .  F - left hand side
42390026cea9SSean Farley 
42400026cea9SSean Farley    Level: intermediate
42410026cea9SSean Farley 
42420026cea9SSean Farley    Notes:
42430910c330SBarry 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
42440026cea9SSean Farley    user is required to write their own TSComputeIFunction.
42450026cea9SSean Farley    This function is intended to be passed to TSSetIFunction() to evaluate the left hand side for linear problems.
42460026cea9SSean Farley    The matrix (and optionally the evaluation context) should be passed to TSSetIJacobian().
42470026cea9SSean Farley 
42480026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIJacobianConstant()
42490026cea9SSean Farley @*/
42500910c330SBarry Smith PetscErrorCode TSComputeIFunctionLinear(TS ts,PetscReal t,Vec U,Vec Udot,Vec F,void *ctx)
42510026cea9SSean Farley {
42520026cea9SSean Farley   PetscErrorCode ierr;
42530026cea9SSean Farley   Mat            A,B;
42540026cea9SSean Farley 
42550026cea9SSean Farley   PetscFunctionBegin;
42560298fd71SBarry Smith   ierr = TSGetIJacobian(ts,&A,&B,NULL,NULL);CHKERRQ(ierr);
4257d1e9a80fSBarry Smith   ierr = TSComputeIJacobian(ts,t,U,Udot,1.0,A,B,PETSC_TRUE);CHKERRQ(ierr);
42580910c330SBarry Smith   ierr = MatMult(A,Udot,F);CHKERRQ(ierr);
42590026cea9SSean Farley   PetscFunctionReturn(0);
42600026cea9SSean Farley }
42610026cea9SSean Farley 
42620026cea9SSean Farley #undef __FUNCT__
42630026cea9SSean Farley #define __FUNCT__ "TSComputeIJacobianConstant"
42640026cea9SSean Farley /*@C
4265b41af12eSJed Brown    TSComputeIJacobianConstant - Reuses a time-independent for a semi-implicit DAE or ODE
42660026cea9SSean Farley 
42670026cea9SSean Farley    Collective on TS
42680026cea9SSean Farley 
42690026cea9SSean Farley    Input Arguments:
42700026cea9SSean Farley +  ts - time stepping context
42710026cea9SSean Farley .  t - time at which to evaluate
42720910c330SBarry Smith .  U - state at which to evaluate
42730910c330SBarry Smith .  Udot - time derivative of state vector
42740026cea9SSean Farley .  shift - shift to apply
42750026cea9SSean Farley -  ctx - context
42760026cea9SSean Farley 
42770026cea9SSean Farley    Output Arguments:
42780026cea9SSean Farley +  A - pointer to operator
42790026cea9SSean Farley .  B - pointer to preconditioning matrix
42800026cea9SSean Farley -  flg - matrix structure flag
42810026cea9SSean Farley 
4282b41af12eSJed Brown    Level: advanced
42830026cea9SSean Farley 
42840026cea9SSean Farley    Notes:
42850026cea9SSean Farley    This function is intended to be passed to TSSetIJacobian() to evaluate the Jacobian for linear time-independent problems.
42860026cea9SSean Farley 
4287b41af12eSJed Brown    It is only appropriate for problems of the form
4288b41af12eSJed Brown 
4289b41af12eSJed Brown $     M Udot = F(U,t)
4290b41af12eSJed Brown 
4291b41af12eSJed Brown   where M is constant and F is non-stiff.  The user must pass M to TSSetIJacobian().  The current implementation only
4292b41af12eSJed Brown   works with IMEX time integration methods such as TSROSW and TSARKIMEX, since there is no support for de-constructing
4293b41af12eSJed Brown   an implicit operator of the form
4294b41af12eSJed Brown 
4295b41af12eSJed Brown $    shift*M + J
4296b41af12eSJed Brown 
4297b41af12eSJed 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
4298b41af12eSJed Brown   a copy of M or reassemble it when requested.
4299b41af12eSJed Brown 
43000026cea9SSean Farley .seealso: TSSetIFunction(), TSSetIJacobian(), TSComputeIFunctionLinear()
43010026cea9SSean Farley @*/
4302d1e9a80fSBarry Smith PetscErrorCode TSComputeIJacobianConstant(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat A,Mat B,void *ctx)
43030026cea9SSean Farley {
4304b41af12eSJed Brown   PetscErrorCode ierr;
4305b41af12eSJed Brown 
43060026cea9SSean Farley   PetscFunctionBegin;
430794ab13aaSBarry Smith   ierr = MatScale(A, shift / ts->ijacobian.shift);CHKERRQ(ierr);
4308b41af12eSJed Brown   ts->ijacobian.shift = shift;
43090026cea9SSean Farley   PetscFunctionReturn(0);
43100026cea9SSean Farley }
4311b41af12eSJed Brown 
4312e817cc15SEmil Constantinescu #undef __FUNCT__
4313e817cc15SEmil Constantinescu #define __FUNCT__ "TSGetEquationType"
4314e817cc15SEmil Constantinescu /*@
4315e817cc15SEmil Constantinescu    TSGetEquationType - Gets the type of the equation that TS is solving.
4316e817cc15SEmil Constantinescu 
4317e817cc15SEmil Constantinescu    Not Collective
4318e817cc15SEmil Constantinescu 
4319e817cc15SEmil Constantinescu    Input Parameter:
4320e817cc15SEmil Constantinescu .  ts - the TS context
4321e817cc15SEmil Constantinescu 
4322e817cc15SEmil Constantinescu    Output Parameter:
43234e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4324e817cc15SEmil Constantinescu 
4325e817cc15SEmil Constantinescu    Level: beginner
4326e817cc15SEmil Constantinescu 
4327e817cc15SEmil Constantinescu .keywords: TS, equation type
4328e817cc15SEmil Constantinescu 
4329e817cc15SEmil Constantinescu .seealso: TSSetEquationType(), TSEquationType
4330e817cc15SEmil Constantinescu @*/
4331e817cc15SEmil Constantinescu PetscErrorCode  TSGetEquationType(TS ts,TSEquationType *equation_type)
4332e817cc15SEmil Constantinescu {
4333e817cc15SEmil Constantinescu   PetscFunctionBegin;
4334e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4335e817cc15SEmil Constantinescu   PetscValidPointer(equation_type,2);
4336e817cc15SEmil Constantinescu   *equation_type = ts->equation_type;
4337e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4338e817cc15SEmil Constantinescu }
4339e817cc15SEmil Constantinescu 
4340e817cc15SEmil Constantinescu #undef __FUNCT__
4341e817cc15SEmil Constantinescu #define __FUNCT__ "TSSetEquationType"
4342e817cc15SEmil Constantinescu /*@
4343e817cc15SEmil Constantinescu    TSSetEquationType - Sets the type of the equation that TS is solving.
4344e817cc15SEmil Constantinescu 
4345e817cc15SEmil Constantinescu    Not Collective
4346e817cc15SEmil Constantinescu 
4347e817cc15SEmil Constantinescu    Input Parameter:
4348e817cc15SEmil Constantinescu +  ts - the TS context
43494e6b9ce4SEmil Constantinescu .  equation_type - see TSEquationType
4350e817cc15SEmil Constantinescu 
4351e817cc15SEmil Constantinescu    Level: advanced
4352e817cc15SEmil Constantinescu 
4353e817cc15SEmil Constantinescu .keywords: TS, equation type
4354e817cc15SEmil Constantinescu 
4355e817cc15SEmil Constantinescu .seealso: TSGetEquationType(), TSEquationType
4356e817cc15SEmil Constantinescu @*/
4357e817cc15SEmil Constantinescu PetscErrorCode  TSSetEquationType(TS ts,TSEquationType equation_type)
4358e817cc15SEmil Constantinescu {
4359e817cc15SEmil Constantinescu   PetscFunctionBegin;
4360e817cc15SEmil Constantinescu   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4361e817cc15SEmil Constantinescu   ts->equation_type = equation_type;
4362e817cc15SEmil Constantinescu   PetscFunctionReturn(0);
4363e817cc15SEmil Constantinescu }
43640026cea9SSean Farley 
43654af1b03aSJed Brown #undef __FUNCT__
43664af1b03aSJed Brown #define __FUNCT__ "TSGetConvergedReason"
43674af1b03aSJed Brown /*@
43684af1b03aSJed Brown    TSGetConvergedReason - Gets the reason the TS iteration was stopped.
43694af1b03aSJed Brown 
43704af1b03aSJed Brown    Not Collective
43714af1b03aSJed Brown 
43724af1b03aSJed Brown    Input Parameter:
43734af1b03aSJed Brown .  ts - the TS context
43744af1b03aSJed Brown 
43754af1b03aSJed Brown    Output Parameter:
43764af1b03aSJed Brown .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
43774af1b03aSJed Brown             manual pages for the individual convergence tests for complete lists
43784af1b03aSJed Brown 
4379487e0bb9SJed Brown    Level: beginner
43804af1b03aSJed Brown 
4381cd652676SJed Brown    Notes:
4382cd652676SJed Brown    Can only be called after the call to TSSolve() is complete.
43834af1b03aSJed Brown 
43844af1b03aSJed Brown .keywords: TS, nonlinear, set, convergence, test
43854af1b03aSJed Brown 
43864af1b03aSJed Brown .seealso: TSSetConvergenceTest(), TSConvergedReason
43874af1b03aSJed Brown @*/
43884af1b03aSJed Brown PetscErrorCode  TSGetConvergedReason(TS ts,TSConvergedReason *reason)
43894af1b03aSJed Brown {
43904af1b03aSJed Brown   PetscFunctionBegin;
43914af1b03aSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
43924af1b03aSJed Brown   PetscValidPointer(reason,2);
43934af1b03aSJed Brown   *reason = ts->reason;
43944af1b03aSJed Brown   PetscFunctionReturn(0);
43954af1b03aSJed Brown }
43964af1b03aSJed Brown 
4397fb1732b5SBarry Smith #undef __FUNCT__
4398d6ad946cSShri Abhyankar #define __FUNCT__ "TSSetConvergedReason"
4399d6ad946cSShri Abhyankar /*@
4400d6ad946cSShri Abhyankar    TSSetConvergedReason - Sets the reason for handling the convergence of TSSolve.
4401d6ad946cSShri Abhyankar 
4402d6ad946cSShri Abhyankar    Not Collective
4403d6ad946cSShri Abhyankar 
4404d6ad946cSShri Abhyankar    Input Parameter:
4405d6ad946cSShri Abhyankar +  ts - the TS context
4406d6ad946cSShri Abhyankar .  reason - negative value indicates diverged, positive value converged, see TSConvergedReason or the
4407d6ad946cSShri Abhyankar             manual pages for the individual convergence tests for complete lists
4408d6ad946cSShri Abhyankar 
4409f5abba47SShri Abhyankar    Level: advanced
4410d6ad946cSShri Abhyankar 
4411d6ad946cSShri Abhyankar    Notes:
4412d6ad946cSShri Abhyankar    Can only be called during TSSolve() is active.
4413d6ad946cSShri Abhyankar 
4414d6ad946cSShri Abhyankar .keywords: TS, nonlinear, set, convergence, test
4415d6ad946cSShri Abhyankar 
4416d6ad946cSShri Abhyankar .seealso: TSConvergedReason
4417d6ad946cSShri Abhyankar @*/
4418d6ad946cSShri Abhyankar PetscErrorCode  TSSetConvergedReason(TS ts,TSConvergedReason reason)
4419d6ad946cSShri Abhyankar {
4420d6ad946cSShri Abhyankar   PetscFunctionBegin;
4421d6ad946cSShri Abhyankar   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4422d6ad946cSShri Abhyankar   ts->reason = reason;
4423d6ad946cSShri Abhyankar   PetscFunctionReturn(0);
4424d6ad946cSShri Abhyankar }
4425d6ad946cSShri Abhyankar 
4426d6ad946cSShri Abhyankar #undef __FUNCT__
4427cc708dedSBarry Smith #define __FUNCT__ "TSGetSolveTime"
4428cc708dedSBarry Smith /*@
4429cc708dedSBarry Smith    TSGetSolveTime - Gets the time after a call to TSSolve()
4430cc708dedSBarry Smith 
4431cc708dedSBarry Smith    Not Collective
4432cc708dedSBarry Smith 
4433cc708dedSBarry Smith    Input Parameter:
4434cc708dedSBarry Smith .  ts - the TS context
4435cc708dedSBarry Smith 
4436cc708dedSBarry Smith    Output Parameter:
4437cc708dedSBarry Smith .  ftime - the final time. This time should correspond to the final time set with TSSetDuration()
4438cc708dedSBarry Smith 
4439487e0bb9SJed Brown    Level: beginner
4440cc708dedSBarry Smith 
4441cc708dedSBarry Smith    Notes:
4442cc708dedSBarry Smith    Can only be called after the call to TSSolve() is complete.
4443cc708dedSBarry Smith 
4444cc708dedSBarry Smith .keywords: TS, nonlinear, set, convergence, test
4445cc708dedSBarry Smith 
4446cc708dedSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
4447cc708dedSBarry Smith @*/
4448cc708dedSBarry Smith PetscErrorCode  TSGetSolveTime(TS ts,PetscReal *ftime)
4449cc708dedSBarry Smith {
4450cc708dedSBarry Smith   PetscFunctionBegin;
4451cc708dedSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4452cc708dedSBarry Smith   PetscValidPointer(ftime,2);
4453cc708dedSBarry Smith   *ftime = ts->solvetime;
4454cc708dedSBarry Smith   PetscFunctionReturn(0);
4455cc708dedSBarry Smith }
4456cc708dedSBarry Smith 
4457cc708dedSBarry Smith #undef __FUNCT__
44582c18e0fdSBarry Smith #define __FUNCT__ "TSGetTotalSteps"
44592c18e0fdSBarry Smith /*@
44602c18e0fdSBarry Smith    TSGetTotalSteps - Gets the total number of steps done since the last call to TSSetUp() or TSCreate()
44612c18e0fdSBarry Smith 
44622c18e0fdSBarry Smith    Not Collective
44632c18e0fdSBarry Smith 
44642c18e0fdSBarry Smith    Input Parameter:
44652c18e0fdSBarry Smith .  ts - the TS context
44662c18e0fdSBarry Smith 
44672c18e0fdSBarry Smith    Output Parameter:
44682c18e0fdSBarry Smith .  steps - the number of steps
44692c18e0fdSBarry Smith 
44702c18e0fdSBarry Smith    Level: beginner
44712c18e0fdSBarry Smith 
44722c18e0fdSBarry Smith    Notes:
44732c18e0fdSBarry Smith    Includes the number of steps for all calls to TSSolve() since TSSetUp() was called
44742c18e0fdSBarry Smith 
44752c18e0fdSBarry Smith .keywords: TS, nonlinear, set, convergence, test
44762c18e0fdSBarry Smith 
44772c18e0fdSBarry Smith .seealso: TSSetConvergenceTest(), TSConvergedReason
44782c18e0fdSBarry Smith @*/
44792c18e0fdSBarry Smith PetscErrorCode  TSGetTotalSteps(TS ts,PetscInt *steps)
44802c18e0fdSBarry Smith {
44812c18e0fdSBarry Smith   PetscFunctionBegin;
44822c18e0fdSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
44832c18e0fdSBarry Smith   PetscValidPointer(steps,2);
44842c18e0fdSBarry Smith   *steps = ts->total_steps;
44852c18e0fdSBarry Smith   PetscFunctionReturn(0);
44862c18e0fdSBarry Smith }
44872c18e0fdSBarry Smith 
44882c18e0fdSBarry Smith #undef __FUNCT__
44895ef26d82SJed Brown #define __FUNCT__ "TSGetSNESIterations"
44909f67acb7SJed Brown /*@
44915ef26d82SJed Brown    TSGetSNESIterations - Gets the total number of nonlinear iterations
44929f67acb7SJed Brown    used by the time integrator.
44939f67acb7SJed Brown 
44949f67acb7SJed Brown    Not Collective
44959f67acb7SJed Brown 
44969f67acb7SJed Brown    Input Parameter:
44979f67acb7SJed Brown .  ts - TS context
44989f67acb7SJed Brown 
44999f67acb7SJed Brown    Output Parameter:
45009f67acb7SJed Brown .  nits - number of nonlinear iterations
45019f67acb7SJed Brown 
45029f67acb7SJed Brown    Notes:
45039f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
45049f67acb7SJed Brown 
45059f67acb7SJed Brown    Level: intermediate
45069f67acb7SJed Brown 
45079f67acb7SJed Brown .keywords: TS, get, number, nonlinear, iterations
45089f67acb7SJed Brown 
45095ef26d82SJed Brown .seealso:  TSGetKSPIterations()
45109f67acb7SJed Brown @*/
45115ef26d82SJed Brown PetscErrorCode TSGetSNESIterations(TS ts,PetscInt *nits)
45129f67acb7SJed Brown {
45139f67acb7SJed Brown   PetscFunctionBegin;
45149f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45159f67acb7SJed Brown   PetscValidIntPointer(nits,2);
45165ef26d82SJed Brown   *nits = ts->snes_its;
45179f67acb7SJed Brown   PetscFunctionReturn(0);
45189f67acb7SJed Brown }
45199f67acb7SJed Brown 
45209f67acb7SJed Brown #undef __FUNCT__
45215ef26d82SJed Brown #define __FUNCT__ "TSGetKSPIterations"
45229f67acb7SJed Brown /*@
45235ef26d82SJed Brown    TSGetKSPIterations - Gets the total number of linear iterations
45249f67acb7SJed Brown    used by the time integrator.
45259f67acb7SJed Brown 
45269f67acb7SJed Brown    Not Collective
45279f67acb7SJed Brown 
45289f67acb7SJed Brown    Input Parameter:
45299f67acb7SJed Brown .  ts - TS context
45309f67acb7SJed Brown 
45319f67acb7SJed Brown    Output Parameter:
45329f67acb7SJed Brown .  lits - number of linear iterations
45339f67acb7SJed Brown 
45349f67acb7SJed Brown    Notes:
45359f67acb7SJed Brown    This counter is reset to zero for each successive call to TSSolve().
45369f67acb7SJed Brown 
45379f67acb7SJed Brown    Level: intermediate
45389f67acb7SJed Brown 
45399f67acb7SJed Brown .keywords: TS, get, number, linear, iterations
45409f67acb7SJed Brown 
45415ef26d82SJed Brown .seealso:  TSGetSNESIterations(), SNESGetKSPIterations()
45429f67acb7SJed Brown @*/
45435ef26d82SJed Brown PetscErrorCode TSGetKSPIterations(TS ts,PetscInt *lits)
45449f67acb7SJed Brown {
45459f67acb7SJed Brown   PetscFunctionBegin;
45469f67acb7SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
45479f67acb7SJed Brown   PetscValidIntPointer(lits,2);
45485ef26d82SJed Brown   *lits = ts->ksp_its;
45499f67acb7SJed Brown   PetscFunctionReturn(0);
45509f67acb7SJed Brown }
45519f67acb7SJed Brown 
45529f67acb7SJed Brown #undef __FUNCT__
4553cef5090cSJed Brown #define __FUNCT__ "TSGetStepRejections"
4554cef5090cSJed Brown /*@
4555cef5090cSJed Brown    TSGetStepRejections - Gets the total number of rejected steps.
4556cef5090cSJed Brown 
4557cef5090cSJed Brown    Not Collective
4558cef5090cSJed Brown 
4559cef5090cSJed Brown    Input Parameter:
4560cef5090cSJed Brown .  ts - TS context
4561cef5090cSJed Brown 
4562cef5090cSJed Brown    Output Parameter:
4563cef5090cSJed Brown .  rejects - number of steps rejected
4564cef5090cSJed Brown 
4565cef5090cSJed Brown    Notes:
4566cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4567cef5090cSJed Brown 
4568cef5090cSJed Brown    Level: intermediate
4569cef5090cSJed Brown 
4570cef5090cSJed Brown .keywords: TS, get, number
4571cef5090cSJed Brown 
45725ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetSNESFailures(), TSSetMaxSNESFailures(), TSSetErrorIfStepFails()
4573cef5090cSJed Brown @*/
4574cef5090cSJed Brown PetscErrorCode TSGetStepRejections(TS ts,PetscInt *rejects)
4575cef5090cSJed Brown {
4576cef5090cSJed Brown   PetscFunctionBegin;
4577cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4578cef5090cSJed Brown   PetscValidIntPointer(rejects,2);
4579cef5090cSJed Brown   *rejects = ts->reject;
4580cef5090cSJed Brown   PetscFunctionReturn(0);
4581cef5090cSJed Brown }
4582cef5090cSJed Brown 
4583cef5090cSJed Brown #undef __FUNCT__
4584cef5090cSJed Brown #define __FUNCT__ "TSGetSNESFailures"
4585cef5090cSJed Brown /*@
4586cef5090cSJed Brown    TSGetSNESFailures - Gets the total number of failed SNES solves
4587cef5090cSJed Brown 
4588cef5090cSJed Brown    Not Collective
4589cef5090cSJed Brown 
4590cef5090cSJed Brown    Input Parameter:
4591cef5090cSJed Brown .  ts - TS context
4592cef5090cSJed Brown 
4593cef5090cSJed Brown    Output Parameter:
4594cef5090cSJed Brown .  fails - number of failed nonlinear solves
4595cef5090cSJed Brown 
4596cef5090cSJed Brown    Notes:
4597cef5090cSJed Brown    This counter is reset to zero for each successive call to TSSolve().
4598cef5090cSJed Brown 
4599cef5090cSJed Brown    Level: intermediate
4600cef5090cSJed Brown 
4601cef5090cSJed Brown .keywords: TS, get, number
4602cef5090cSJed Brown 
46035ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSSetMaxSNESFailures()
4604cef5090cSJed Brown @*/
4605cef5090cSJed Brown PetscErrorCode TSGetSNESFailures(TS ts,PetscInt *fails)
4606cef5090cSJed Brown {
4607cef5090cSJed Brown   PetscFunctionBegin;
4608cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4609cef5090cSJed Brown   PetscValidIntPointer(fails,2);
4610cef5090cSJed Brown   *fails = ts->num_snes_failures;
4611cef5090cSJed Brown   PetscFunctionReturn(0);
4612cef5090cSJed Brown }
4613cef5090cSJed Brown 
4614cef5090cSJed Brown #undef __FUNCT__
4615cef5090cSJed Brown #define __FUNCT__ "TSSetMaxStepRejections"
4616cef5090cSJed Brown /*@
4617cef5090cSJed Brown    TSSetMaxStepRejections - Sets the maximum number of step rejections before a step fails
4618cef5090cSJed Brown 
4619cef5090cSJed Brown    Not Collective
4620cef5090cSJed Brown 
4621cef5090cSJed Brown    Input Parameter:
4622cef5090cSJed Brown +  ts - TS context
4623cef5090cSJed Brown -  rejects - maximum number of rejected steps, pass -1 for unlimited
4624cef5090cSJed Brown 
4625cef5090cSJed Brown    Notes:
4626cef5090cSJed Brown    The counter is reset to zero for each step
4627cef5090cSJed Brown 
4628cef5090cSJed Brown    Options Database Key:
4629cef5090cSJed Brown  .  -ts_max_reject - Maximum number of step rejections before a step fails
4630cef5090cSJed Brown 
4631cef5090cSJed Brown    Level: intermediate
4632cef5090cSJed Brown 
4633cef5090cSJed Brown .keywords: TS, set, maximum, number
4634cef5090cSJed Brown 
46355ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxSNESFailures(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4636cef5090cSJed Brown @*/
4637cef5090cSJed Brown PetscErrorCode TSSetMaxStepRejections(TS ts,PetscInt rejects)
4638cef5090cSJed Brown {
4639cef5090cSJed Brown   PetscFunctionBegin;
4640cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4641cef5090cSJed Brown   ts->max_reject = rejects;
4642cef5090cSJed Brown   PetscFunctionReturn(0);
4643cef5090cSJed Brown }
4644cef5090cSJed Brown 
4645cef5090cSJed Brown #undef __FUNCT__
4646cef5090cSJed Brown #define __FUNCT__ "TSSetMaxSNESFailures"
4647cef5090cSJed Brown /*@
4648cef5090cSJed Brown    TSSetMaxSNESFailures - Sets the maximum number of failed SNES solves
4649cef5090cSJed Brown 
4650cef5090cSJed Brown    Not Collective
4651cef5090cSJed Brown 
4652cef5090cSJed Brown    Input Parameter:
4653cef5090cSJed Brown +  ts - TS context
4654cef5090cSJed Brown -  fails - maximum number of failed nonlinear solves, pass -1 for unlimited
4655cef5090cSJed Brown 
4656cef5090cSJed Brown    Notes:
4657cef5090cSJed Brown    The counter is reset to zero for each successive call to TSSolve().
4658cef5090cSJed Brown 
4659cef5090cSJed Brown    Options Database Key:
4660cef5090cSJed Brown  .  -ts_max_snes_failures - Maximum number of nonlinear solve failures
4661cef5090cSJed Brown 
4662cef5090cSJed Brown    Level: intermediate
4663cef5090cSJed Brown 
4664cef5090cSJed Brown .keywords: TS, set, maximum, number
4665cef5090cSJed Brown 
46665ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), SNESGetConvergedReason(), TSGetConvergedReason()
4667cef5090cSJed Brown @*/
4668cef5090cSJed Brown PetscErrorCode TSSetMaxSNESFailures(TS ts,PetscInt fails)
4669cef5090cSJed Brown {
4670cef5090cSJed Brown   PetscFunctionBegin;
4671cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4672cef5090cSJed Brown   ts->max_snes_failures = fails;
4673cef5090cSJed Brown   PetscFunctionReturn(0);
4674cef5090cSJed Brown }
4675cef5090cSJed Brown 
4676cef5090cSJed Brown #undef __FUNCT__
46774e8de811SJed Brown #define __FUNCT__ "TSSetErrorIfStepFails"
4678cef5090cSJed Brown /*@
4679cef5090cSJed Brown    TSSetErrorIfStepFails - Error if no step succeeds
4680cef5090cSJed Brown 
4681cef5090cSJed Brown    Not Collective
4682cef5090cSJed Brown 
4683cef5090cSJed Brown    Input Parameter:
4684cef5090cSJed Brown +  ts - TS context
4685cef5090cSJed Brown -  err - PETSC_TRUE to error if no step succeeds, PETSC_FALSE to return without failure
4686cef5090cSJed Brown 
4687cef5090cSJed Brown    Options Database Key:
4688cef5090cSJed Brown  .  -ts_error_if_step_fails - Error if no step succeeds
4689cef5090cSJed Brown 
4690cef5090cSJed Brown    Level: intermediate
4691cef5090cSJed Brown 
4692cef5090cSJed Brown .keywords: TS, set, error
4693cef5090cSJed Brown 
46945ef26d82SJed Brown .seealso:  TSGetSNESIterations(), TSGetKSPIterations(), TSSetMaxStepRejections(), TSGetStepRejections(), TSGetSNESFailures(), TSSetErrorIfStepFails(), TSGetConvergedReason()
4695cef5090cSJed Brown @*/
4696cef5090cSJed Brown PetscErrorCode TSSetErrorIfStepFails(TS ts,PetscBool err)
4697cef5090cSJed Brown {
4698cef5090cSJed Brown   PetscFunctionBegin;
4699cef5090cSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
4700cef5090cSJed Brown   ts->errorifstepfailed = err;
4701cef5090cSJed Brown   PetscFunctionReturn(0);
4702cef5090cSJed Brown }
4703cef5090cSJed Brown 
4704cef5090cSJed Brown #undef __FUNCT__
4705fb1732b5SBarry Smith #define __FUNCT__ "TSMonitorSolutionBinary"
4706fb1732b5SBarry Smith /*@C
4707fb1732b5SBarry Smith    TSMonitorSolutionBinary - Monitors progress of the TS solvers by VecView() for the solution at each timestep. Normally the viewer is a binary file
4708fb1732b5SBarry Smith 
4709fb1732b5SBarry Smith    Collective on TS
4710fb1732b5SBarry Smith 
4711fb1732b5SBarry Smith    Input Parameters:
4712fb1732b5SBarry Smith +  ts - the TS context
4713fb1732b5SBarry Smith .  step - current time-step
4714fb1732b5SBarry Smith .  ptime - current time
47150910c330SBarry Smith .  u - current state
4716fb1732b5SBarry Smith -  viewer - binary viewer
4717fb1732b5SBarry Smith 
4718fb1732b5SBarry Smith    Level: intermediate
4719fb1732b5SBarry Smith 
4720fb1732b5SBarry Smith .keywords: TS,  vector, monitor, view
4721fb1732b5SBarry Smith 
4722fb1732b5SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4723fb1732b5SBarry Smith @*/
47240910c330SBarry Smith PetscErrorCode  TSMonitorSolutionBinary(TS ts,PetscInt step,PetscReal ptime,Vec u,void *viewer)
4725fb1732b5SBarry Smith {
4726fb1732b5SBarry Smith   PetscErrorCode ierr;
4727ed81e22dSJed Brown   PetscViewer    v = (PetscViewer)viewer;
4728fb1732b5SBarry Smith 
4729fb1732b5SBarry Smith   PetscFunctionBegin;
47300910c330SBarry Smith   ierr = VecView(u,v);CHKERRQ(ierr);
4731ed81e22dSJed Brown   PetscFunctionReturn(0);
4732ed81e22dSJed Brown }
4733ed81e22dSJed Brown 
4734ed81e22dSJed Brown #undef __FUNCT__
4735ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTK"
4736ed81e22dSJed Brown /*@C
4737ed81e22dSJed Brown    TSMonitorSolutionVTK - Monitors progress of the TS solvers by VecView() for the solution at each timestep.
4738ed81e22dSJed Brown 
4739ed81e22dSJed Brown    Collective on TS
4740ed81e22dSJed Brown 
4741ed81e22dSJed Brown    Input Parameters:
4742ed81e22dSJed Brown +  ts - the TS context
4743ed81e22dSJed Brown .  step - current time-step
4744ed81e22dSJed Brown .  ptime - current time
47450910c330SBarry Smith .  u - current state
4746ed81e22dSJed Brown -  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4747ed81e22dSJed Brown 
4748ed81e22dSJed Brown    Level: intermediate
4749ed81e22dSJed Brown 
4750ed81e22dSJed Brown    Notes:
4751ed81e22dSJed 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.
4752ed81e22dSJed Brown    These are named according to the file name template.
4753ed81e22dSJed Brown 
4754ed81e22dSJed Brown    This function is normally passed as an argument to TSMonitorSet() along with TSMonitorSolutionVTKDestroy().
4755ed81e22dSJed Brown 
4756ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4757ed81e22dSJed Brown 
4758ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
4759ed81e22dSJed Brown @*/
47600910c330SBarry Smith PetscErrorCode TSMonitorSolutionVTK(TS ts,PetscInt step,PetscReal ptime,Vec u,void *filenametemplate)
4761ed81e22dSJed Brown {
4762ed81e22dSJed Brown   PetscErrorCode ierr;
4763ed81e22dSJed Brown   char           filename[PETSC_MAX_PATH_LEN];
4764ed81e22dSJed Brown   PetscViewer    viewer;
4765ed81e22dSJed Brown 
4766ed81e22dSJed Brown   PetscFunctionBegin;
47678caf3d72SBarry Smith   ierr = PetscSNPrintf(filename,sizeof(filename),(const char*)filenametemplate,step);CHKERRQ(ierr);
4768ce94432eSBarry Smith   ierr = PetscViewerVTKOpen(PetscObjectComm((PetscObject)ts),filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr);
47690910c330SBarry Smith   ierr = VecView(u,viewer);CHKERRQ(ierr);
4770ed81e22dSJed Brown   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
4771ed81e22dSJed Brown   PetscFunctionReturn(0);
4772ed81e22dSJed Brown }
4773ed81e22dSJed Brown 
4774ed81e22dSJed Brown #undef __FUNCT__
4775ed81e22dSJed Brown #define __FUNCT__ "TSMonitorSolutionVTKDestroy"
4776ed81e22dSJed Brown /*@C
4777ed81e22dSJed Brown    TSMonitorSolutionVTKDestroy - Destroy context for monitoring
4778ed81e22dSJed Brown 
4779ed81e22dSJed Brown    Collective on TS
4780ed81e22dSJed Brown 
4781ed81e22dSJed Brown    Input Parameters:
4782ed81e22dSJed Brown .  filenametemplate - string containing a format specifier for the integer time step (e.g. %03D)
4783ed81e22dSJed Brown 
4784ed81e22dSJed Brown    Level: intermediate
4785ed81e22dSJed Brown 
4786ed81e22dSJed Brown    Note:
4787ed81e22dSJed Brown    This function is normally passed to TSMonitorSet() along with TSMonitorSolutionVTK().
4788ed81e22dSJed Brown 
4789ed81e22dSJed Brown .keywords: TS,  vector, monitor, view
4790ed81e22dSJed Brown 
4791ed81e22dSJed Brown .seealso: TSMonitorSet(), TSMonitorSolutionVTK()
4792ed81e22dSJed Brown @*/
4793ed81e22dSJed Brown PetscErrorCode TSMonitorSolutionVTKDestroy(void *filenametemplate)
4794ed81e22dSJed Brown {
4795ed81e22dSJed Brown   PetscErrorCode ierr;
4796ed81e22dSJed Brown 
4797ed81e22dSJed Brown   PetscFunctionBegin;
4798ed81e22dSJed Brown   ierr = PetscFree(*(char**)filenametemplate);CHKERRQ(ierr);
4799fb1732b5SBarry Smith   PetscFunctionReturn(0);
4800fb1732b5SBarry Smith }
4801fb1732b5SBarry Smith 
480284df9cb4SJed Brown #undef __FUNCT__
4803552698daSJed Brown #define __FUNCT__ "TSGetAdapt"
480484df9cb4SJed Brown /*@
4805552698daSJed Brown    TSGetAdapt - Get the adaptive controller context for the current method
480684df9cb4SJed Brown 
4807ed81e22dSJed Brown    Collective on TS if controller has not been created yet
480884df9cb4SJed Brown 
480984df9cb4SJed Brown    Input Arguments:
4810ed81e22dSJed Brown .  ts - time stepping context
481184df9cb4SJed Brown 
481284df9cb4SJed Brown    Output Arguments:
4813ed81e22dSJed Brown .  adapt - adaptive controller
481484df9cb4SJed Brown 
481584df9cb4SJed Brown    Level: intermediate
481684df9cb4SJed Brown 
4817ed81e22dSJed Brown .seealso: TSAdapt, TSAdaptSetType(), TSAdaptChoose()
481884df9cb4SJed Brown @*/
4819552698daSJed Brown PetscErrorCode TSGetAdapt(TS ts,TSAdapt *adapt)
482084df9cb4SJed Brown {
482184df9cb4SJed Brown   PetscErrorCode ierr;
482284df9cb4SJed Brown 
482384df9cb4SJed Brown   PetscFunctionBegin;
482484df9cb4SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
482584df9cb4SJed Brown   PetscValidPointer(adapt,2);
482684df9cb4SJed Brown   if (!ts->adapt) {
4827ce94432eSBarry Smith     ierr = TSAdaptCreate(PetscObjectComm((PetscObject)ts),&ts->adapt);CHKERRQ(ierr);
48283bb1ff40SBarry Smith     ierr = PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->adapt);CHKERRQ(ierr);
48291c3436cfSJed Brown     ierr = PetscObjectIncrementTabLevel((PetscObject)ts->adapt,(PetscObject)ts,1);CHKERRQ(ierr);
483084df9cb4SJed Brown   }
483184df9cb4SJed Brown   *adapt = ts->adapt;
483284df9cb4SJed Brown   PetscFunctionReturn(0);
483384df9cb4SJed Brown }
4834d6ebe24aSShri Abhyankar 
4835d6ebe24aSShri Abhyankar #undef __FUNCT__
48361c3436cfSJed Brown #define __FUNCT__ "TSSetTolerances"
48371c3436cfSJed Brown /*@
48381c3436cfSJed Brown    TSSetTolerances - Set tolerances for local truncation error when using adaptive controller
48391c3436cfSJed Brown 
48401c3436cfSJed Brown    Logically Collective
48411c3436cfSJed Brown 
48421c3436cfSJed Brown    Input Arguments:
48431c3436cfSJed Brown +  ts - time integration context
48441c3436cfSJed Brown .  atol - scalar absolute tolerances, PETSC_DECIDE to leave current value
48450298fd71SBarry Smith .  vatol - vector of absolute tolerances or NULL, used in preference to atol if present
48461c3436cfSJed Brown .  rtol - scalar relative tolerances, PETSC_DECIDE to leave current value
48470298fd71SBarry Smith -  vrtol - vector of relative tolerances or NULL, used in preference to atol if present
48481c3436cfSJed Brown 
4849a3cdaa26SBarry Smith    Options Database keys:
4850a3cdaa26SBarry Smith +  -ts_rtol <rtol> - relative tolerance for local truncation error
4851a3cdaa26SBarry Smith -  -ts_atol <atol> Absolute tolerance for local truncation error
4852a3cdaa26SBarry Smith 
48531c3436cfSJed Brown    Level: beginner
48541c3436cfSJed Brown 
4855c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSGetTolerances()
48561c3436cfSJed Brown @*/
48571c3436cfSJed Brown PetscErrorCode TSSetTolerances(TS ts,PetscReal atol,Vec vatol,PetscReal rtol,Vec vrtol)
48581c3436cfSJed Brown {
48591c3436cfSJed Brown   PetscErrorCode ierr;
48601c3436cfSJed Brown 
48611c3436cfSJed Brown   PetscFunctionBegin;
4862c5033834SJed Brown   if (atol != PETSC_DECIDE && atol != PETSC_DEFAULT) ts->atol = atol;
48631c3436cfSJed Brown   if (vatol) {
48641c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vatol);CHKERRQ(ierr);
48651c3436cfSJed Brown     ierr = VecDestroy(&ts->vatol);CHKERRQ(ierr);
4866bbd56ea5SKarl Rupp 
48671c3436cfSJed Brown     ts->vatol = vatol;
48681c3436cfSJed Brown   }
4869c5033834SJed Brown   if (rtol != PETSC_DECIDE && rtol != PETSC_DEFAULT) ts->rtol = rtol;
48701c3436cfSJed Brown   if (vrtol) {
48711c3436cfSJed Brown     ierr = PetscObjectReference((PetscObject)vrtol);CHKERRQ(ierr);
48721c3436cfSJed Brown     ierr = VecDestroy(&ts->vrtol);CHKERRQ(ierr);
4873bbd56ea5SKarl Rupp 
48741c3436cfSJed Brown     ts->vrtol = vrtol;
48751c3436cfSJed Brown   }
48761c3436cfSJed Brown   PetscFunctionReturn(0);
48771c3436cfSJed Brown }
48781c3436cfSJed Brown 
48791c3436cfSJed Brown #undef __FUNCT__
4880c5033834SJed Brown #define __FUNCT__ "TSGetTolerances"
4881c5033834SJed Brown /*@
4882c5033834SJed Brown    TSGetTolerances - Get tolerances for local truncation error when using adaptive controller
4883c5033834SJed Brown 
4884c5033834SJed Brown    Logically Collective
4885c5033834SJed Brown 
4886c5033834SJed Brown    Input Arguments:
4887c5033834SJed Brown .  ts - time integration context
4888c5033834SJed Brown 
4889c5033834SJed Brown    Output Arguments:
48900298fd71SBarry Smith +  atol - scalar absolute tolerances, NULL to ignore
48910298fd71SBarry Smith .  vatol - vector of absolute tolerances, NULL to ignore
48920298fd71SBarry Smith .  rtol - scalar relative tolerances, NULL to ignore
48930298fd71SBarry Smith -  vrtol - vector of relative tolerances, NULL to ignore
4894c5033834SJed Brown 
4895c5033834SJed Brown    Level: beginner
4896c5033834SJed Brown 
4897c5033834SJed Brown .seealso: TS, TSAdapt, TSVecNormWRMS(), TSSetTolerances()
4898c5033834SJed Brown @*/
4899c5033834SJed Brown PetscErrorCode TSGetTolerances(TS ts,PetscReal *atol,Vec *vatol,PetscReal *rtol,Vec *vrtol)
4900c5033834SJed Brown {
4901c5033834SJed Brown   PetscFunctionBegin;
4902c5033834SJed Brown   if (atol)  *atol  = ts->atol;
4903c5033834SJed Brown   if (vatol) *vatol = ts->vatol;
4904c5033834SJed Brown   if (rtol)  *rtol  = ts->rtol;
4905c5033834SJed Brown   if (vrtol) *vrtol = ts->vrtol;
4906c5033834SJed Brown   PetscFunctionReturn(0);
4907c5033834SJed Brown }
4908c5033834SJed Brown 
4909c5033834SJed Brown #undef __FUNCT__
49101c3436cfSJed Brown #define __FUNCT__ "TSErrorNormWRMS"
49111c3436cfSJed Brown /*@
49121c3436cfSJed Brown    TSErrorNormWRMS - compute a weighted norm of the difference between a vector and the current state
49131c3436cfSJed Brown 
49141c3436cfSJed Brown    Collective on TS
49151c3436cfSJed Brown 
49161c3436cfSJed Brown    Input Arguments:
49171c3436cfSJed Brown +  ts - time stepping context
49181c3436cfSJed Brown -  Y - state vector to be compared to ts->vec_sol
49191c3436cfSJed Brown 
49201c3436cfSJed Brown    Output Arguments:
49211c3436cfSJed Brown .  norm - weighted norm, a value of 1.0 is considered small
49221c3436cfSJed Brown 
49231c3436cfSJed Brown    Level: developer
49241c3436cfSJed Brown 
49251c3436cfSJed Brown .seealso: TSSetTolerances()
49261c3436cfSJed Brown @*/
49271c3436cfSJed Brown PetscErrorCode TSErrorNormWRMS(TS ts,Vec Y,PetscReal *norm)
49281c3436cfSJed Brown {
49291c3436cfSJed Brown   PetscErrorCode    ierr;
49301c3436cfSJed Brown   PetscInt          i,n,N;
49310910c330SBarry Smith   const PetscScalar *u,*y;
49320910c330SBarry Smith   Vec               U;
49331c3436cfSJed Brown   PetscReal         sum,gsum;
49341c3436cfSJed Brown 
49351c3436cfSJed Brown   PetscFunctionBegin;
49361c3436cfSJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
49371c3436cfSJed Brown   PetscValidHeaderSpecific(Y,VEC_CLASSID,2);
49381c3436cfSJed Brown   PetscValidPointer(norm,3);
49390910c330SBarry Smith   U = ts->vec_sol;
49400910c330SBarry Smith   PetscCheckSameTypeAndComm(U,1,Y,2);
4941ce94432eSBarry Smith   if (U == Y) SETERRQ(PetscObjectComm((PetscObject)U),PETSC_ERR_ARG_IDN,"Y cannot be the TS solution vector");
49421c3436cfSJed Brown 
49430910c330SBarry Smith   ierr = VecGetSize(U,&N);CHKERRQ(ierr);
49440910c330SBarry Smith   ierr = VecGetLocalSize(U,&n);CHKERRQ(ierr);
49450910c330SBarry Smith   ierr = VecGetArrayRead(U,&u);CHKERRQ(ierr);
49461c3436cfSJed Brown   ierr = VecGetArrayRead(Y,&y);CHKERRQ(ierr);
49471c3436cfSJed Brown   sum  = 0.;
4948ceefc113SJed Brown   if (ts->vatol && ts->vrtol) {
4949ceefc113SJed Brown     const PetscScalar *atol,*rtol;
4950ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4951ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4952ceefc113SJed Brown     for (i=0; i<n; i++) {
49530910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
49540910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4955ceefc113SJed Brown     }
4956ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4957ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4958ceefc113SJed Brown   } else if (ts->vatol) {       /* vector atol, scalar rtol */
4959ceefc113SJed Brown     const PetscScalar *atol;
4960ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4961ceefc113SJed Brown     for (i=0; i<n; i++) {
49620910c330SBarry Smith       PetscReal tol = PetscRealPart(atol[i]) + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
49630910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4964ceefc113SJed Brown     }
4965ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vatol,&atol);CHKERRQ(ierr);
4966ceefc113SJed Brown   } else if (ts->vrtol) {       /* scalar atol, vector rtol */
4967ceefc113SJed Brown     const PetscScalar *rtol;
4968ceefc113SJed Brown     ierr = VecGetArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4969ceefc113SJed Brown     for (i=0; i<n; i++) {
49700910c330SBarry Smith       PetscReal tol = ts->atol + PetscRealPart(rtol[i]) * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
49710910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
4972ceefc113SJed Brown     }
4973ceefc113SJed Brown     ierr = VecRestoreArrayRead(ts->vrtol,&rtol);CHKERRQ(ierr);
4974ceefc113SJed Brown   } else {                      /* scalar atol, scalar rtol */
49751c3436cfSJed Brown     for (i=0; i<n; i++) {
49760910c330SBarry Smith       PetscReal tol = ts->atol + ts->rtol * PetscMax(PetscAbsScalar(u[i]),PetscAbsScalar(y[i]));
49770910c330SBarry Smith       sum += PetscSqr(PetscAbsScalar(y[i] - u[i]) / tol);
49781c3436cfSJed Brown     }
4979ceefc113SJed Brown   }
49800910c330SBarry Smith   ierr = VecRestoreArrayRead(U,&u);CHKERRQ(ierr);
49811c3436cfSJed Brown   ierr = VecRestoreArrayRead(Y,&y);CHKERRQ(ierr);
49821c3436cfSJed Brown 
4983ce94432eSBarry Smith   ierr  = MPI_Allreduce(&sum,&gsum,1,MPIU_REAL,MPIU_SUM,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
49841c3436cfSJed Brown   *norm = PetscSqrtReal(gsum / N);
4985620fc8aaSSatish Balay   if (PetscIsInfOrNanReal(*norm)) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_FP,"Infinite or not-a-number generated in norm");
49861c3436cfSJed Brown   PetscFunctionReturn(0);
49871c3436cfSJed Brown }
49881c3436cfSJed Brown 
49891c3436cfSJed Brown #undef __FUNCT__
49908d59e960SJed Brown #define __FUNCT__ "TSSetCFLTimeLocal"
49918d59e960SJed Brown /*@
49928d59e960SJed Brown    TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler
49938d59e960SJed Brown 
49948d59e960SJed Brown    Logically Collective on TS
49958d59e960SJed Brown 
49968d59e960SJed Brown    Input Arguments:
49978d59e960SJed Brown +  ts - time stepping context
49988d59e960SJed Brown -  cfltime - maximum stable time step if using forward Euler (value can be different on each process)
49998d59e960SJed Brown 
50008d59e960SJed Brown    Note:
50018d59e960SJed Brown    After calling this function, the global CFL time can be obtained by calling TSGetCFLTime()
50028d59e960SJed Brown 
50038d59e960SJed Brown    Level: intermediate
50048d59e960SJed Brown 
50058d59e960SJed Brown .seealso: TSGetCFLTime(), TSADAPTCFL
50068d59e960SJed Brown @*/
50078d59e960SJed Brown PetscErrorCode TSSetCFLTimeLocal(TS ts,PetscReal cfltime)
50088d59e960SJed Brown {
50098d59e960SJed Brown   PetscFunctionBegin;
50108d59e960SJed Brown   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
50118d59e960SJed Brown   ts->cfltime_local = cfltime;
50128d59e960SJed Brown   ts->cfltime       = -1.;
50138d59e960SJed Brown   PetscFunctionReturn(0);
50148d59e960SJed Brown }
50158d59e960SJed Brown 
50168d59e960SJed Brown #undef __FUNCT__
50178d59e960SJed Brown #define __FUNCT__ "TSGetCFLTime"
50188d59e960SJed Brown /*@
50198d59e960SJed Brown    TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler
50208d59e960SJed Brown 
50218d59e960SJed Brown    Collective on TS
50228d59e960SJed Brown 
50238d59e960SJed Brown    Input Arguments:
50248d59e960SJed Brown .  ts - time stepping context
50258d59e960SJed Brown 
50268d59e960SJed Brown    Output Arguments:
50278d59e960SJed Brown .  cfltime - maximum stable time step for forward Euler
50288d59e960SJed Brown 
50298d59e960SJed Brown    Level: advanced
50308d59e960SJed Brown 
50318d59e960SJed Brown .seealso: TSSetCFLTimeLocal()
50328d59e960SJed Brown @*/
50338d59e960SJed Brown PetscErrorCode TSGetCFLTime(TS ts,PetscReal *cfltime)
50348d59e960SJed Brown {
50358d59e960SJed Brown   PetscErrorCode ierr;
50368d59e960SJed Brown 
50378d59e960SJed Brown   PetscFunctionBegin;
50388d59e960SJed Brown   if (ts->cfltime < 0) {
5039ce94432eSBarry Smith     ierr = MPI_Allreduce(&ts->cfltime_local,&ts->cfltime,1,MPIU_REAL,MPIU_MIN,PetscObjectComm((PetscObject)ts));CHKERRQ(ierr);
50408d59e960SJed Brown   }
50418d59e960SJed Brown   *cfltime = ts->cfltime;
50428d59e960SJed Brown   PetscFunctionReturn(0);
50438d59e960SJed Brown }
50448d59e960SJed Brown 
50458d59e960SJed Brown #undef __FUNCT__
5046d6ebe24aSShri Abhyankar #define __FUNCT__ "TSVISetVariableBounds"
5047d6ebe24aSShri Abhyankar /*@
5048d6ebe24aSShri Abhyankar    TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu
5049d6ebe24aSShri Abhyankar 
5050d6ebe24aSShri Abhyankar    Input Parameters:
5051d6ebe24aSShri Abhyankar .  ts   - the TS context.
5052d6ebe24aSShri Abhyankar .  xl   - lower bound.
5053d6ebe24aSShri Abhyankar .  xu   - upper bound.
5054d6ebe24aSShri Abhyankar 
5055d6ebe24aSShri Abhyankar    Notes:
5056d6ebe24aSShri Abhyankar    If this routine is not called then the lower and upper bounds are set to
5057e270355aSBarry Smith    PETSC_NINFINITY and PETSC_INFINITY respectively during SNESSetUp().
5058d6ebe24aSShri Abhyankar 
50592bd2b0e6SSatish Balay    Level: advanced
50602bd2b0e6SSatish Balay 
5061d6ebe24aSShri Abhyankar @*/
5062d6ebe24aSShri Abhyankar PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu)
5063d6ebe24aSShri Abhyankar {
5064d6ebe24aSShri Abhyankar   PetscErrorCode ierr;
5065d6ebe24aSShri Abhyankar   SNES           snes;
5066d6ebe24aSShri Abhyankar 
5067d6ebe24aSShri Abhyankar   PetscFunctionBegin;
5068d6ebe24aSShri Abhyankar   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
5069d6ebe24aSShri Abhyankar   ierr = SNESVISetVariableBounds(snes,xl,xu);CHKERRQ(ierr);
5070d6ebe24aSShri Abhyankar   PetscFunctionReturn(0);
5071d6ebe24aSShri Abhyankar }
5072d6ebe24aSShri Abhyankar 
5073325fc9f4SBarry Smith #if defined(PETSC_HAVE_MATLAB_ENGINE)
5074c6db04a5SJed Brown #include <mex.h>
5075325fc9f4SBarry Smith 
5076325fc9f4SBarry Smith typedef struct {char *funcname; mxArray *ctx;} TSMatlabContext;
5077325fc9f4SBarry Smith 
5078325fc9f4SBarry Smith #undef __FUNCT__
5079325fc9f4SBarry Smith #define __FUNCT__ "TSComputeFunction_Matlab"
5080325fc9f4SBarry Smith /*
5081325fc9f4SBarry Smith    TSComputeFunction_Matlab - Calls the function that has been set with
5082325fc9f4SBarry Smith                          TSSetFunctionMatlab().
5083325fc9f4SBarry Smith 
5084325fc9f4SBarry Smith    Collective on TS
5085325fc9f4SBarry Smith 
5086325fc9f4SBarry Smith    Input Parameters:
5087325fc9f4SBarry Smith +  snes - the TS context
50880910c330SBarry Smith -  u - input vector
5089325fc9f4SBarry Smith 
5090325fc9f4SBarry Smith    Output Parameter:
5091325fc9f4SBarry Smith .  y - function vector, as set by TSSetFunction()
5092325fc9f4SBarry Smith 
5093325fc9f4SBarry Smith    Notes:
5094325fc9f4SBarry Smith    TSComputeFunction() is typically used within nonlinear solvers
5095325fc9f4SBarry Smith    implementations, so most users would not generally call this routine
5096325fc9f4SBarry Smith    themselves.
5097325fc9f4SBarry Smith 
5098325fc9f4SBarry Smith    Level: developer
5099325fc9f4SBarry Smith 
5100325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5101325fc9f4SBarry Smith 
5102325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5103325fc9f4SBarry Smith */
51040910c330SBarry Smith PetscErrorCode  TSComputeFunction_Matlab(TS snes,PetscReal time,Vec u,Vec udot,Vec y, void *ctx)
5105325fc9f4SBarry Smith {
5106325fc9f4SBarry Smith   PetscErrorCode  ierr;
5107325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5108325fc9f4SBarry Smith   int             nlhs  = 1,nrhs = 7;
5109325fc9f4SBarry Smith   mxArray         *plhs[1],*prhs[7];
5110325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,ly = 0,ls = 0;
5111325fc9f4SBarry Smith 
5112325fc9f4SBarry Smith   PetscFunctionBegin;
5113325fc9f4SBarry Smith   PetscValidHeaderSpecific(snes,TS_CLASSID,1);
51140910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
51150910c330SBarry Smith   PetscValidHeaderSpecific(udot,VEC_CLASSID,4);
5116325fc9f4SBarry Smith   PetscValidHeaderSpecific(y,VEC_CLASSID,5);
51170910c330SBarry Smith   PetscCheckSameComm(snes,1,u,3);
5118325fc9f4SBarry Smith   PetscCheckSameComm(snes,1,y,5);
5119325fc9f4SBarry Smith 
5120325fc9f4SBarry Smith   ierr = PetscMemcpy(&ls,&snes,sizeof(snes));CHKERRQ(ierr);
51210910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
51220910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(udot));CHKERRQ(ierr);
51230910c330SBarry Smith   ierr = PetscMemcpy(&ly,&y,sizeof(u));CHKERRQ(ierr);
5124bbd56ea5SKarl Rupp 
5125325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5126325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar(time);
5127325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5128325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5129325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)ly);
5130325fc9f4SBarry Smith   prhs[5] =  mxCreateString(sctx->funcname);
5131325fc9f4SBarry Smith   prhs[6] =  sctx->ctx;
5132325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeFunctionInternal");CHKERRQ(ierr);
5133325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5134325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5135325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5136325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5137325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5138325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5139325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5140325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5141325fc9f4SBarry Smith   PetscFunctionReturn(0);
5142325fc9f4SBarry Smith }
5143325fc9f4SBarry Smith 
5144325fc9f4SBarry Smith 
5145325fc9f4SBarry Smith #undef __FUNCT__
5146325fc9f4SBarry Smith #define __FUNCT__ "TSSetFunctionMatlab"
5147325fc9f4SBarry Smith /*
5148325fc9f4SBarry Smith    TSSetFunctionMatlab - Sets the function evaluation routine and function
5149325fc9f4SBarry Smith    vector for use by the TS routines in solving ODEs
5150e3c5b3baSBarry Smith    equations from MATLAB. Here the function is a string containing the name of a MATLAB function
5151325fc9f4SBarry Smith 
5152325fc9f4SBarry Smith    Logically Collective on TS
5153325fc9f4SBarry Smith 
5154325fc9f4SBarry Smith    Input Parameters:
5155325fc9f4SBarry Smith +  ts - the TS context
5156325fc9f4SBarry Smith -  func - function evaluation routine
5157325fc9f4SBarry Smith 
5158325fc9f4SBarry Smith    Calling sequence of func:
51590910c330SBarry Smith $    func (TS ts,PetscReal time,Vec u,Vec udot,Vec f,void *ctx);
5160325fc9f4SBarry Smith 
5161325fc9f4SBarry Smith    Level: beginner
5162325fc9f4SBarry Smith 
5163325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5164325fc9f4SBarry Smith 
5165325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5166325fc9f4SBarry Smith */
5167cdcf91faSSean Farley PetscErrorCode  TSSetFunctionMatlab(TS ts,const char *func,mxArray *ctx)
5168325fc9f4SBarry Smith {
5169325fc9f4SBarry Smith   PetscErrorCode  ierr;
5170325fc9f4SBarry Smith   TSMatlabContext *sctx;
5171325fc9f4SBarry Smith 
5172325fc9f4SBarry Smith   PetscFunctionBegin;
5173325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5174325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5175325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5176325fc9f4SBarry Smith   /*
5177325fc9f4SBarry Smith      This should work, but it doesn't
5178325fc9f4SBarry Smith   sctx->ctx = ctx;
5179325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5180325fc9f4SBarry Smith   */
5181325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5182bbd56ea5SKarl Rupp 
51830298fd71SBarry Smith   ierr = TSSetIFunction(ts,NULL,TSComputeFunction_Matlab,sctx);CHKERRQ(ierr);
5184325fc9f4SBarry Smith   PetscFunctionReturn(0);
5185325fc9f4SBarry Smith }
5186325fc9f4SBarry Smith 
5187325fc9f4SBarry Smith #undef __FUNCT__
5188325fc9f4SBarry Smith #define __FUNCT__ "TSComputeJacobian_Matlab"
5189325fc9f4SBarry Smith /*
5190325fc9f4SBarry Smith    TSComputeJacobian_Matlab - Calls the function that has been set with
5191325fc9f4SBarry Smith                          TSSetJacobianMatlab().
5192325fc9f4SBarry Smith 
5193325fc9f4SBarry Smith    Collective on TS
5194325fc9f4SBarry Smith 
5195325fc9f4SBarry Smith    Input Parameters:
5196cdcf91faSSean Farley +  ts - the TS context
51970910c330SBarry Smith .  u - input vector
5198325fc9f4SBarry Smith .  A, B - the matrices
5199325fc9f4SBarry Smith -  ctx - user context
5200325fc9f4SBarry Smith 
5201325fc9f4SBarry Smith    Level: developer
5202325fc9f4SBarry Smith 
5203325fc9f4SBarry Smith .keywords: TS, nonlinear, compute, function
5204325fc9f4SBarry Smith 
5205325fc9f4SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5206325fc9f4SBarry Smith @*/
5207f3229a78SSatish Balay PetscErrorCode  TSComputeJacobian_Matlab(TS ts,PetscReal time,Vec u,Vec udot,PetscReal shift,Mat A,Mat B,void *ctx)
5208325fc9f4SBarry Smith {
5209325fc9f4SBarry Smith   PetscErrorCode  ierr;
5210325fc9f4SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5211325fc9f4SBarry Smith   int             nlhs  = 2,nrhs = 9;
5212325fc9f4SBarry Smith   mxArray         *plhs[2],*prhs[9];
5213325fc9f4SBarry Smith   long long int   lx = 0,lxdot = 0,lA = 0,ls = 0, lB = 0;
5214325fc9f4SBarry Smith 
5215325fc9f4SBarry Smith   PetscFunctionBegin;
5216cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
52170910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,3);
5218325fc9f4SBarry Smith 
52190910c330SBarry Smith   /* call Matlab function in ctx with arguments u and y */
5220325fc9f4SBarry Smith 
5221cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
52220910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
52230910c330SBarry Smith   ierr = PetscMemcpy(&lxdot,&udot,sizeof(u));CHKERRQ(ierr);
52240910c330SBarry Smith   ierr = PetscMemcpy(&lA,A,sizeof(u));CHKERRQ(ierr);
52250910c330SBarry Smith   ierr = PetscMemcpy(&lB,B,sizeof(u));CHKERRQ(ierr);
5226bbd56ea5SKarl Rupp 
5227325fc9f4SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5228325fc9f4SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)time);
5229325fc9f4SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)lx);
5230325fc9f4SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lxdot);
5231325fc9f4SBarry Smith   prhs[4] =  mxCreateDoubleScalar((double)shift);
5232325fc9f4SBarry Smith   prhs[5] =  mxCreateDoubleScalar((double)lA);
5233325fc9f4SBarry Smith   prhs[6] =  mxCreateDoubleScalar((double)lB);
5234325fc9f4SBarry Smith   prhs[7] =  mxCreateString(sctx->funcname);
5235325fc9f4SBarry Smith   prhs[8] =  sctx->ctx;
5236325fc9f4SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSComputeJacobianInternal");CHKERRQ(ierr);
5237325fc9f4SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5238325fc9f4SBarry Smith   mxDestroyArray(prhs[0]);
5239325fc9f4SBarry Smith   mxDestroyArray(prhs[1]);
5240325fc9f4SBarry Smith   mxDestroyArray(prhs[2]);
5241325fc9f4SBarry Smith   mxDestroyArray(prhs[3]);
5242325fc9f4SBarry Smith   mxDestroyArray(prhs[4]);
5243325fc9f4SBarry Smith   mxDestroyArray(prhs[5]);
5244325fc9f4SBarry Smith   mxDestroyArray(prhs[6]);
5245325fc9f4SBarry Smith   mxDestroyArray(prhs[7]);
5246325fc9f4SBarry Smith   mxDestroyArray(plhs[0]);
5247325fc9f4SBarry Smith   mxDestroyArray(plhs[1]);
5248325fc9f4SBarry Smith   PetscFunctionReturn(0);
5249325fc9f4SBarry Smith }
5250325fc9f4SBarry Smith 
5251325fc9f4SBarry Smith 
5252325fc9f4SBarry Smith #undef __FUNCT__
5253325fc9f4SBarry Smith #define __FUNCT__ "TSSetJacobianMatlab"
5254325fc9f4SBarry Smith /*
5255325fc9f4SBarry Smith    TSSetJacobianMatlab - Sets the Jacobian function evaluation routine and two empty Jacobian matrices
5256e3c5b3baSBarry 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
5257325fc9f4SBarry Smith 
5258325fc9f4SBarry Smith    Logically Collective on TS
5259325fc9f4SBarry Smith 
5260325fc9f4SBarry Smith    Input Parameters:
5261cdcf91faSSean Farley +  ts - the TS context
5262325fc9f4SBarry Smith .  A,B - Jacobian matrices
5263325fc9f4SBarry Smith .  func - function evaluation routine
5264325fc9f4SBarry Smith -  ctx - user context
5265325fc9f4SBarry Smith 
5266325fc9f4SBarry Smith    Calling sequence of func:
52670910c330SBarry Smith $    flag = func (TS ts,PetscReal time,Vec u,Vec udot,Mat A,Mat B,void *ctx);
5268325fc9f4SBarry Smith 
5269325fc9f4SBarry Smith 
5270325fc9f4SBarry Smith    Level: developer
5271325fc9f4SBarry Smith 
5272325fc9f4SBarry Smith .keywords: TS, nonlinear, set, function
5273325fc9f4SBarry Smith 
5274325fc9f4SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5275325fc9f4SBarry Smith */
5276cdcf91faSSean Farley PetscErrorCode  TSSetJacobianMatlab(TS ts,Mat A,Mat B,const char *func,mxArray *ctx)
5277325fc9f4SBarry Smith {
5278325fc9f4SBarry Smith   PetscErrorCode  ierr;
5279325fc9f4SBarry Smith   TSMatlabContext *sctx;
5280325fc9f4SBarry Smith 
5281325fc9f4SBarry Smith   PetscFunctionBegin;
5282325fc9f4SBarry Smith   /* currently sctx is memory bleed */
5283325fc9f4SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5284325fc9f4SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5285325fc9f4SBarry Smith   /*
5286325fc9f4SBarry Smith      This should work, but it doesn't
5287325fc9f4SBarry Smith   sctx->ctx = ctx;
5288325fc9f4SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5289325fc9f4SBarry Smith   */
5290325fc9f4SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5291bbd56ea5SKarl Rupp 
5292cdcf91faSSean Farley   ierr = TSSetIJacobian(ts,A,B,TSComputeJacobian_Matlab,sctx);CHKERRQ(ierr);
5293325fc9f4SBarry Smith   PetscFunctionReturn(0);
5294325fc9f4SBarry Smith }
5295325fc9f4SBarry Smith 
5296b5b1a830SBarry Smith #undef __FUNCT__
5297b5b1a830SBarry Smith #define __FUNCT__ "TSMonitor_Matlab"
5298b5b1a830SBarry Smith /*
5299b5b1a830SBarry Smith    TSMonitor_Matlab - Calls the function that has been set with TSMonitorSetMatlab().
5300b5b1a830SBarry Smith 
5301b5b1a830SBarry Smith    Collective on TS
5302b5b1a830SBarry Smith 
5303b5b1a830SBarry Smith .seealso: TSSetFunction(), TSGetFunction()
5304b5b1a830SBarry Smith @*/
53050910c330SBarry Smith PetscErrorCode  TSMonitor_Matlab(TS ts,PetscInt it, PetscReal time,Vec u, void *ctx)
5306b5b1a830SBarry Smith {
5307b5b1a830SBarry Smith   PetscErrorCode  ierr;
5308b5b1a830SBarry Smith   TSMatlabContext *sctx = (TSMatlabContext*)ctx;
5309a530c242SBarry Smith   int             nlhs  = 1,nrhs = 6;
5310b5b1a830SBarry Smith   mxArray         *plhs[1],*prhs[6];
5311b5b1a830SBarry Smith   long long int   lx = 0,ls = 0;
5312b5b1a830SBarry Smith 
5313b5b1a830SBarry Smith   PetscFunctionBegin;
5314cdcf91faSSean Farley   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
53150910c330SBarry Smith   PetscValidHeaderSpecific(u,VEC_CLASSID,4);
5316b5b1a830SBarry Smith 
5317cdcf91faSSean Farley   ierr = PetscMemcpy(&ls,&ts,sizeof(ts));CHKERRQ(ierr);
53180910c330SBarry Smith   ierr = PetscMemcpy(&lx,&u,sizeof(u));CHKERRQ(ierr);
5319bbd56ea5SKarl Rupp 
5320b5b1a830SBarry Smith   prhs[0] =  mxCreateDoubleScalar((double)ls);
5321b5b1a830SBarry Smith   prhs[1] =  mxCreateDoubleScalar((double)it);
5322b5b1a830SBarry Smith   prhs[2] =  mxCreateDoubleScalar((double)time);
5323b5b1a830SBarry Smith   prhs[3] =  mxCreateDoubleScalar((double)lx);
5324b5b1a830SBarry Smith   prhs[4] =  mxCreateString(sctx->funcname);
5325b5b1a830SBarry Smith   prhs[5] =  sctx->ctx;
5326b5b1a830SBarry Smith   ierr    =  mexCallMATLAB(nlhs,plhs,nrhs,prhs,"PetscTSMonitorInternal");CHKERRQ(ierr);
5327b5b1a830SBarry Smith   ierr    =  mxGetScalar(plhs[0]);CHKERRQ(ierr);
5328b5b1a830SBarry Smith   mxDestroyArray(prhs[0]);
5329b5b1a830SBarry Smith   mxDestroyArray(prhs[1]);
5330b5b1a830SBarry Smith   mxDestroyArray(prhs[2]);
5331b5b1a830SBarry Smith   mxDestroyArray(prhs[3]);
5332b5b1a830SBarry Smith   mxDestroyArray(prhs[4]);
5333b5b1a830SBarry Smith   mxDestroyArray(plhs[0]);
5334b5b1a830SBarry Smith   PetscFunctionReturn(0);
5335b5b1a830SBarry Smith }
5336b5b1a830SBarry Smith 
5337b5b1a830SBarry Smith 
5338b5b1a830SBarry Smith #undef __FUNCT__
5339b5b1a830SBarry Smith #define __FUNCT__ "TSMonitorSetMatlab"
5340b5b1a830SBarry Smith /*
5341b5b1a830SBarry Smith    TSMonitorSetMatlab - Sets the monitor function from Matlab
5342b5b1a830SBarry Smith 
5343b5b1a830SBarry Smith    Level: developer
5344b5b1a830SBarry Smith 
5345b5b1a830SBarry Smith .keywords: TS, nonlinear, set, function
5346b5b1a830SBarry Smith 
5347b5b1a830SBarry Smith .seealso: TSGetFunction(), TSComputeFunction(), TSSetJacobian(), TSSetFunction()
5348b5b1a830SBarry Smith */
5349cdcf91faSSean Farley PetscErrorCode  TSMonitorSetMatlab(TS ts,const char *func,mxArray *ctx)
5350b5b1a830SBarry Smith {
5351b5b1a830SBarry Smith   PetscErrorCode  ierr;
5352b5b1a830SBarry Smith   TSMatlabContext *sctx;
5353b5b1a830SBarry Smith 
5354b5b1a830SBarry Smith   PetscFunctionBegin;
5355b5b1a830SBarry Smith   /* currently sctx is memory bleed */
5356b5b1a830SBarry Smith   ierr = PetscMalloc(sizeof(TSMatlabContext),&sctx);CHKERRQ(ierr);
5357b5b1a830SBarry Smith   ierr = PetscStrallocpy(func,&sctx->funcname);CHKERRQ(ierr);
5358b5b1a830SBarry Smith   /*
5359b5b1a830SBarry Smith      This should work, but it doesn't
5360b5b1a830SBarry Smith   sctx->ctx = ctx;
5361b5b1a830SBarry Smith   mexMakeArrayPersistent(sctx->ctx);
5362b5b1a830SBarry Smith   */
5363b5b1a830SBarry Smith   sctx->ctx = mxDuplicateArray(ctx);
5364bbd56ea5SKarl Rupp 
53650298fd71SBarry Smith   ierr = TSMonitorSet(ts,TSMonitor_Matlab,sctx,NULL);CHKERRQ(ierr);
5366b5b1a830SBarry Smith   PetscFunctionReturn(0);
5367b5b1a830SBarry Smith }
5368325fc9f4SBarry Smith #endif
5369b3603a34SBarry Smith 
53700b039ecaSBarry Smith 
53710b039ecaSBarry Smith 
5372b3603a34SBarry Smith #undef __FUNCT__
53734f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGSolution"
5374b3603a34SBarry Smith /*@C
53754f09c107SBarry Smith    TSMonitorLGSolution - Monitors progress of the TS solvers by plotting each component of the solution vector
5376b3603a34SBarry Smith        in a time based line graph
5377b3603a34SBarry Smith 
5378b3603a34SBarry Smith    Collective on TS
5379b3603a34SBarry Smith 
5380b3603a34SBarry Smith    Input Parameters:
5381b3603a34SBarry Smith +  ts - the TS context
5382b3603a34SBarry Smith .  step - current time-step
5383b3603a34SBarry Smith .  ptime - current time
5384b3603a34SBarry Smith -  lg - a line graph object
5385b3603a34SBarry Smith 
5386b3603a34SBarry Smith    Level: intermediate
5387b3603a34SBarry Smith 
53880b039ecaSBarry Smith     Notes: each process in a parallel run displays its component solutions in a separate window
5389b3603a34SBarry Smith 
5390b3603a34SBarry Smith .keywords: TS,  vector, monitor, view
5391b3603a34SBarry Smith 
5392b3603a34SBarry Smith .seealso: TSMonitorSet(), TSMonitorDefault(), VecView()
5393b3603a34SBarry Smith @*/
53940910c330SBarry Smith PetscErrorCode  TSMonitorLGSolution(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
5395b3603a34SBarry Smith {
5396b3603a34SBarry Smith   PetscErrorCode    ierr;
53970b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
5398b3603a34SBarry Smith   const PetscScalar *yy;
539958ff32f7SBarry Smith   PetscInt          dim;
5400b3603a34SBarry Smith 
5401b3603a34SBarry Smith   PetscFunctionBegin;
540258ff32f7SBarry Smith   if (!step) {
5403a9f9c1f6SBarry Smith     PetscDrawAxis axis;
5404a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5405a9f9c1f6SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Solution as function of time","Time","Solution");CHKERRQ(ierr);
54060910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
54070b039ecaSBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
54080b039ecaSBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
540958ff32f7SBarry Smith   }
54100910c330SBarry Smith   ierr = VecGetArrayRead(u,&yy);CHKERRQ(ierr);
5411e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
5412e3efe391SJed Brown   {
5413e3efe391SJed Brown     PetscReal *yreal;
5414e3efe391SJed Brown     PetscInt  i,n;
54150910c330SBarry Smith     ierr = VecGetLocalSize(u,&n);CHKERRQ(ierr);
5416785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
5417e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
54180b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
5419e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
5420e3efe391SJed Brown   }
5421e3efe391SJed Brown #else
54220b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
5423e3efe391SJed Brown #endif
54240910c330SBarry Smith   ierr = VecRestoreArrayRead(u,&yy);CHKERRQ(ierr);
5425b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
54260b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
54273923b477SBarry Smith   }
5428b3603a34SBarry Smith   PetscFunctionReturn(0);
5429b3603a34SBarry Smith }
5430b3603a34SBarry Smith 
5431b3603a34SBarry Smith #undef __FUNCT__
54324f09c107SBarry Smith #define __FUNCT__ "TSMonitorLGError"
5433ef20d060SBarry Smith /*@C
54344f09c107SBarry Smith    TSMonitorLGError - Monitors progress of the TS solvers by plotting each component of the solution vector
5435ef20d060SBarry Smith        in a time based line graph
5436ef20d060SBarry Smith 
5437ef20d060SBarry Smith    Collective on TS
5438ef20d060SBarry Smith 
5439ef20d060SBarry Smith    Input Parameters:
5440ef20d060SBarry Smith +  ts - the TS context
5441ef20d060SBarry Smith .  step - current time-step
5442ef20d060SBarry Smith .  ptime - current time
5443ef20d060SBarry Smith -  lg - a line graph object
5444ef20d060SBarry Smith 
5445ef20d060SBarry Smith    Level: intermediate
5446ef20d060SBarry Smith 
5447abd5a294SJed Brown    Notes:
5448abd5a294SJed Brown    Only for sequential solves.
5449abd5a294SJed Brown 
5450abd5a294SJed Brown    The user must provide the solution using TSSetSolutionFunction() to use this monitor.
5451abd5a294SJed Brown 
5452abd5a294SJed Brown    Options Database Keys:
54534f09c107SBarry Smith .  -ts_monitor_lg_error - create a graphical monitor of error history
5454ef20d060SBarry Smith 
5455ef20d060SBarry Smith .keywords: TS,  vector, monitor, view
5456ef20d060SBarry Smith 
5457abd5a294SJed Brown .seealso: TSMonitorSet(), TSMonitorDefault(), VecView(), TSSetSolutionFunction()
5458ef20d060SBarry Smith @*/
54590910c330SBarry Smith PetscErrorCode  TSMonitorLGError(TS ts,PetscInt step,PetscReal ptime,Vec u,void *dummy)
5460ef20d060SBarry Smith {
5461ef20d060SBarry Smith   PetscErrorCode    ierr;
54620b039ecaSBarry Smith   TSMonitorLGCtx    ctx = (TSMonitorLGCtx)dummy;
5463ef20d060SBarry Smith   const PetscScalar *yy;
5464ef20d060SBarry Smith   Vec               y;
5465a9f9c1f6SBarry Smith   PetscInt          dim;
5466ef20d060SBarry Smith 
5467ef20d060SBarry Smith   PetscFunctionBegin;
5468a9f9c1f6SBarry Smith   if (!step) {
5469a9f9c1f6SBarry Smith     PetscDrawAxis axis;
5470a9f9c1f6SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
54711ae185eaSBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Error in solution as function of time","Time","Solution");CHKERRQ(ierr);
54720910c330SBarry Smith     ierr = VecGetLocalSize(u,&dim);CHKERRQ(ierr);
5473a9f9c1f6SBarry Smith     ierr = PetscDrawLGSetDimension(ctx->lg,dim);CHKERRQ(ierr);
5474a9f9c1f6SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5475a9f9c1f6SBarry Smith   }
54760910c330SBarry Smith   ierr = VecDuplicate(u,&y);CHKERRQ(ierr);
5477ef20d060SBarry Smith   ierr = TSComputeSolutionFunction(ts,ptime,y);CHKERRQ(ierr);
54780910c330SBarry Smith   ierr = VecAXPY(y,-1.0,u);CHKERRQ(ierr);
5479ef20d060SBarry Smith   ierr = VecGetArrayRead(y,&yy);CHKERRQ(ierr);
5480e3efe391SJed Brown #if defined(PETSC_USE_COMPLEX)
5481e3efe391SJed Brown   {
5482e3efe391SJed Brown     PetscReal *yreal;
5483e3efe391SJed Brown     PetscInt  i,n;
5484e3efe391SJed Brown     ierr = VecGetLocalSize(y,&n);CHKERRQ(ierr);
5485785e854fSJed Brown     ierr = PetscMalloc1(n,&yreal);CHKERRQ(ierr);
5486e3efe391SJed Brown     for (i=0; i<n; i++) yreal[i] = PetscRealPart(yy[i]);
54870b039ecaSBarry Smith     ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yreal);CHKERRQ(ierr);
5488e3efe391SJed Brown     ierr = PetscFree(yreal);CHKERRQ(ierr);
5489e3efe391SJed Brown   }
5490e3efe391SJed Brown #else
54910b039ecaSBarry Smith   ierr = PetscDrawLGAddCommonPoint(ctx->lg,ptime,yy);CHKERRQ(ierr);
5492e3efe391SJed Brown #endif
5493ef20d060SBarry Smith   ierr = VecRestoreArrayRead(y,&yy);CHKERRQ(ierr);
5494ef20d060SBarry Smith   ierr = VecDestroy(&y);CHKERRQ(ierr);
5495b06615a5SLisandro Dalcin   if (((ctx->howoften > 0) && (!(step % ctx->howoften))) || ((ctx->howoften == -1) && ts->reason)) {
54960b039ecaSBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
54973923b477SBarry Smith   }
5498ef20d060SBarry Smith   PetscFunctionReturn(0);
5499ef20d060SBarry Smith }
5500ef20d060SBarry Smith 
5501201da799SBarry Smith #undef __FUNCT__
5502201da799SBarry Smith #define __FUNCT__ "TSMonitorLGSNESIterations"
5503201da799SBarry Smith PetscErrorCode TSMonitorLGSNESIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
5504201da799SBarry Smith {
5505201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
5506201da799SBarry Smith   PetscReal      x   = ptime,y;
5507201da799SBarry Smith   PetscErrorCode ierr;
5508201da799SBarry Smith   PetscInt       its;
5509201da799SBarry Smith 
5510201da799SBarry Smith   PetscFunctionBegin;
5511201da799SBarry Smith   if (!n) {
5512201da799SBarry Smith     PetscDrawAxis axis;
5513bbd56ea5SKarl Rupp 
5514201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5515201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Nonlinear iterations as function of time","Time","SNES Iterations");CHKERRQ(ierr);
5516201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5517bbd56ea5SKarl Rupp 
5518201da799SBarry Smith     ctx->snes_its = 0;
5519201da799SBarry Smith   }
5520201da799SBarry Smith   ierr = TSGetSNESIterations(ts,&its);CHKERRQ(ierr);
5521201da799SBarry Smith   y    = its - ctx->snes_its;
5522201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
55233fbbecb0SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
5524201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
5525201da799SBarry Smith   }
5526201da799SBarry Smith   ctx->snes_its = its;
5527201da799SBarry Smith   PetscFunctionReturn(0);
5528201da799SBarry Smith }
5529201da799SBarry Smith 
5530201da799SBarry Smith #undef __FUNCT__
5531201da799SBarry Smith #define __FUNCT__ "TSMonitorLGKSPIterations"
5532201da799SBarry Smith PetscErrorCode TSMonitorLGKSPIterations(TS ts,PetscInt n,PetscReal ptime,Vec v,void *monctx)
5533201da799SBarry Smith {
5534201da799SBarry Smith   TSMonitorLGCtx ctx = (TSMonitorLGCtx) monctx;
5535201da799SBarry Smith   PetscReal      x   = ptime,y;
5536201da799SBarry Smith   PetscErrorCode ierr;
5537201da799SBarry Smith   PetscInt       its;
5538201da799SBarry Smith 
5539201da799SBarry Smith   PetscFunctionBegin;
5540201da799SBarry Smith   if (!n) {
5541201da799SBarry Smith     PetscDrawAxis axis;
5542bbd56ea5SKarl Rupp 
5543201da799SBarry Smith     ierr = PetscDrawLGGetAxis(ctx->lg,&axis);CHKERRQ(ierr);
5544201da799SBarry Smith     ierr = PetscDrawAxisSetLabels(axis,"Linear iterations as function of time","Time","KSP Iterations");CHKERRQ(ierr);
5545201da799SBarry Smith     ierr = PetscDrawLGReset(ctx->lg);CHKERRQ(ierr);
5546bbd56ea5SKarl Rupp 
5547201da799SBarry Smith     ctx->ksp_its = 0;
5548201da799SBarry Smith   }
5549201da799SBarry Smith   ierr = TSGetKSPIterations(ts,&its);CHKERRQ(ierr);
5550201da799SBarry Smith   y    = its - ctx->ksp_its;
5551201da799SBarry Smith   ierr = PetscDrawLGAddPoint(ctx->lg,&x,&y);CHKERRQ(ierr);
555299fdda47SBarry Smith   if (((ctx->howoften > 0) && (!(n % ctx->howoften)) && (n > -1)) || ((ctx->howoften == -1) && (n == -1))) {
5553201da799SBarry Smith     ierr = PetscDrawLGDraw(ctx->lg);CHKERRQ(ierr);
5554201da799SBarry Smith   }
5555201da799SBarry Smith   ctx->ksp_its = its;
5556201da799SBarry Smith   PetscFunctionReturn(0);
5557201da799SBarry Smith }
5558f9c1d6abSBarry Smith 
5559f9c1d6abSBarry Smith #undef __FUNCT__
5560f9c1d6abSBarry Smith #define __FUNCT__ "TSComputeLinearStability"
5561f9c1d6abSBarry Smith /*@
5562f9c1d6abSBarry Smith    TSComputeLinearStability - computes the linear stability function at a point
5563f9c1d6abSBarry Smith 
5564f9c1d6abSBarry Smith    Collective on TS and Vec
5565f9c1d6abSBarry Smith 
5566f9c1d6abSBarry Smith    Input Parameters:
5567f9c1d6abSBarry Smith +  ts - the TS context
5568f9c1d6abSBarry Smith -  xr,xi - real and imaginary part of input arguments
5569f9c1d6abSBarry Smith 
5570f9c1d6abSBarry Smith    Output Parameters:
5571f9c1d6abSBarry Smith .  yr,yi - real and imaginary part of function value
5572f9c1d6abSBarry Smith 
5573f9c1d6abSBarry Smith    Level: developer
5574f9c1d6abSBarry Smith 
5575f9c1d6abSBarry Smith .keywords: TS, compute
5576f9c1d6abSBarry Smith 
5577f9c1d6abSBarry Smith .seealso: TSSetRHSFunction(), TSComputeIFunction()
5578f9c1d6abSBarry Smith @*/
5579f9c1d6abSBarry Smith PetscErrorCode TSComputeLinearStability(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
5580f9c1d6abSBarry Smith {
5581f9c1d6abSBarry Smith   PetscErrorCode ierr;
5582f9c1d6abSBarry Smith 
5583f9c1d6abSBarry Smith   PetscFunctionBegin;
5584f9c1d6abSBarry Smith   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
5585ce94432eSBarry Smith   if (!ts->ops->linearstability) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"Linearized stability function not provided for this method");
5586f9c1d6abSBarry Smith   ierr = (*ts->ops->linearstability)(ts,xr,xi,yr,yi);CHKERRQ(ierr);
5587f9c1d6abSBarry Smith   PetscFunctionReturn(0);
5588f9c1d6abSBarry Smith }
558924655328SShri 
559024655328SShri #undef __FUNCT__
559124655328SShri #define __FUNCT__ "TSRollBack"
559224655328SShri /*@
559324655328SShri    TSRollBack - Rolls back one time step
559424655328SShri 
559524655328SShri    Collective on TS
559624655328SShri 
559724655328SShri    Input Parameter:
559824655328SShri .  ts - the TS context obtained from TSCreate()
559924655328SShri 
560024655328SShri    Level: advanced
560124655328SShri 
560224655328SShri .keywords: TS, timestep, rollback
560324655328SShri 
560424655328SShri .seealso: TSCreate(), TSSetUp(), TSDestroy(), TSSolve(), TSSetPreStep(), TSSetPreStage(), TSInterpolate()
560524655328SShri @*/
560624655328SShri PetscErrorCode  TSRollBack(TS ts)
560724655328SShri {
560824655328SShri   PetscErrorCode ierr;
560924655328SShri 
561024655328SShri   PetscFunctionBegin;
561124655328SShri   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
561224655328SShri 
561324655328SShri   if (!ts->ops->rollback) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"TSRollBack not implemented for type '%s'",((PetscObject)ts)->type_name);
561424655328SShri   ierr = (*ts->ops->rollback)(ts);CHKERRQ(ierr);
561524655328SShri   ts->time_step = ts->ptime - ts->ptime_prev;
561624655328SShri   ts->ptime = ts->ptime_prev;
561724655328SShri   PetscFunctionReturn(0);
561824655328SShri }
5619aeb4809dSShri Abhyankar 
5620ff22ae23SHong Zhang #undef __FUNCT__
5621ff22ae23SHong Zhang #define __FUNCT__ "TSGetStages"
5622ff22ae23SHong Zhang /*@
5623ff22ae23SHong Zhang    TSGetStages - Get the number of stages and stage values
5624ff22ae23SHong Zhang 
5625ff22ae23SHong Zhang    Input Parameter:
5626ff22ae23SHong Zhang .  ts - the TS context obtained from TSCreate()
5627ff22ae23SHong Zhang 
5628ff22ae23SHong Zhang    Level: advanced
5629ff22ae23SHong Zhang 
5630ff22ae23SHong Zhang .keywords: TS, getstages
5631ff22ae23SHong Zhang 
5632ff22ae23SHong Zhang .seealso: TSCreate()
5633ff22ae23SHong Zhang @*/
5634ff22ae23SHong Zhang PetscErrorCode  TSGetStages(TS ts,PetscInt *ns, Vec **Y)
5635ff22ae23SHong Zhang {
5636ff22ae23SHong Zhang   PetscErrorCode ierr;
5637ff22ae23SHong Zhang 
5638ff22ae23SHong Zhang   PetscFunctionBegin;
5639ff22ae23SHong Zhang   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
5640ff22ae23SHong Zhang   PetscValidPointer(ns,2);
5641ff22ae23SHong Zhang 
5642ff22ae23SHong Zhang   if (!ts->ops->getstages) *ns=0;
5643ff22ae23SHong Zhang   else {
5644ff22ae23SHong Zhang     ierr = (*ts->ops->getstages)(ts,ns,Y);CHKERRQ(ierr);
5645ff22ae23SHong Zhang   }
5646ff22ae23SHong Zhang   PetscFunctionReturn(0);
5647ff22ae23SHong Zhang }
5648ff22ae23SHong Zhang 
5649*847ff0e1SMatthew G. Knepley 
5650*847ff0e1SMatthew G. Knepley #undef __FUNCT__
5651*847ff0e1SMatthew G. Knepley #define __FUNCT__ "TSComputeIJacobianDefaultColor"
5652*847ff0e1SMatthew G. Knepley /*@C
5653*847ff0e1SMatthew G. Knepley   TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity.
5654*847ff0e1SMatthew G. Knepley 
5655*847ff0e1SMatthew G. Knepley   Collective on SNES
5656*847ff0e1SMatthew G. Knepley 
5657*847ff0e1SMatthew G. Knepley   Input Parameters:
5658*847ff0e1SMatthew G. Knepley + ts - the TS context
5659*847ff0e1SMatthew G. Knepley . t - current timestep
5660*847ff0e1SMatthew G. Knepley . U - state vector
5661*847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector
5662*847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below
5663*847ff0e1SMatthew G. Knepley - ctx - an optional user context
5664*847ff0e1SMatthew G. Knepley 
5665*847ff0e1SMatthew G. Knepley   Output Parameters:
5666*847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine)
5667*847ff0e1SMatthew G. Knepley - B - newly computed Jacobian matrix to use with preconditioner (generally the same as J)
5668*847ff0e1SMatthew G. Knepley 
5669*847ff0e1SMatthew G. Knepley   Level: intermediate
5670*847ff0e1SMatthew G. Knepley 
5671*847ff0e1SMatthew G. Knepley   Notes:
5672*847ff0e1SMatthew G. Knepley   If F(t,U,Udot)=0 is the DAE, the required Jacobian is
5673*847ff0e1SMatthew G. Knepley 
5674*847ff0e1SMatthew G. Knepley   dF/dU + shift*dF/dUdot
5675*847ff0e1SMatthew G. Knepley 
5676*847ff0e1SMatthew G. Knepley   Most users should not need to explicitly call this routine, as it
5677*847ff0e1SMatthew G. Knepley   is used internally within the nonlinear solvers.
5678*847ff0e1SMatthew G. Knepley 
5679*847ff0e1SMatthew G. Knepley   This will first try to get the coloring from the DM.  If the DM type has no coloring
5680*847ff0e1SMatthew G. Knepley   routine, then it will try to get the coloring from the matrix.  This requires that the
5681*847ff0e1SMatthew G. Knepley   matrix have nonzero entries precomputed.
5682*847ff0e1SMatthew G. Knepley 
5683*847ff0e1SMatthew G. Knepley .keywords: TS, finite differences, Jacobian, coloring, sparse
5684*847ff0e1SMatthew G. Knepley .seealso: TSSetIJacobian(), MatFDColoringCreate(), MatFDColoringSetFunction()
5685*847ff0e1SMatthew G. Knepley @*/
5686*847ff0e1SMatthew G. Knepley PetscErrorCode TSComputeIJacobianDefaultColor(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat J,Mat B,void *ctx)
5687*847ff0e1SMatthew G. Knepley {
5688*847ff0e1SMatthew G. Knepley   SNES           snes;
5689*847ff0e1SMatthew G. Knepley   MatFDColoring  color;
5690*847ff0e1SMatthew G. Knepley   PetscBool      hascolor, matcolor = PETSC_FALSE;
5691*847ff0e1SMatthew G. Knepley   PetscErrorCode ierr;
5692*847ff0e1SMatthew G. Knepley 
5693*847ff0e1SMatthew G. Knepley   PetscFunctionBegin;
5694*847ff0e1SMatthew G. Knepley   ierr = PetscOptionsGetBool(((PetscObject) ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL);CHKERRQ(ierr);
5695*847ff0e1SMatthew G. Knepley   ierr = PetscObjectQuery((PetscObject) B, "TSMatFDColoring", (PetscObject *) &color);CHKERRQ(ierr);
5696*847ff0e1SMatthew G. Knepley   if (!color) {
5697*847ff0e1SMatthew G. Knepley     DM         dm;
5698*847ff0e1SMatthew G. Knepley     ISColoring iscoloring;
5699*847ff0e1SMatthew G. Knepley 
5700*847ff0e1SMatthew G. Knepley     ierr = TSGetDM(ts, &dm);CHKERRQ(ierr);
5701*847ff0e1SMatthew G. Knepley     ierr = DMHasColoring(dm, &hascolor);CHKERRQ(ierr);
5702*847ff0e1SMatthew G. Knepley     if (hascolor && !matcolor) {
5703*847ff0e1SMatthew G. Knepley       ierr = DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring);CHKERRQ(ierr);
5704*847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
5705*847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
5706*847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
5707*847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
5708*847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
5709*847ff0e1SMatthew G. Knepley     } else {
5710*847ff0e1SMatthew G. Knepley       MatColoring mc;
5711*847ff0e1SMatthew G. Knepley 
5712*847ff0e1SMatthew G. Knepley       ierr = MatColoringCreate(B, &mc);CHKERRQ(ierr);
5713*847ff0e1SMatthew G. Knepley       ierr = MatColoringSetDistance(mc, 2);CHKERRQ(ierr);
5714*847ff0e1SMatthew G. Knepley       ierr = MatColoringSetType(mc, MATCOLORINGSL);CHKERRQ(ierr);
5715*847ff0e1SMatthew G. Knepley       ierr = MatColoringSetFromOptions(mc);CHKERRQ(ierr);
5716*847ff0e1SMatthew G. Knepley       ierr = MatColoringApply(mc, &iscoloring);CHKERRQ(ierr);
5717*847ff0e1SMatthew G. Knepley       ierr = MatColoringDestroy(&mc);CHKERRQ(ierr);
5718*847ff0e1SMatthew G. Knepley       ierr = MatFDColoringCreate(B, iscoloring, &color);CHKERRQ(ierr);
5719*847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFunction(color, (PetscErrorCode (*)(void)) SNESTSFormFunction, (void *) ts);CHKERRQ(ierr);
5720*847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetFromOptions(color);CHKERRQ(ierr);
5721*847ff0e1SMatthew G. Knepley       ierr = MatFDColoringSetUp(B, iscoloring, color);CHKERRQ(ierr);
5722*847ff0e1SMatthew G. Knepley       ierr = ISColoringDestroy(&iscoloring);CHKERRQ(ierr);
5723*847ff0e1SMatthew G. Knepley     }
5724*847ff0e1SMatthew G. Knepley     ierr = PetscObjectCompose((PetscObject) B, "TSMatFDColoring", (PetscObject) color);CHKERRQ(ierr);
5725*847ff0e1SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) color);CHKERRQ(ierr);
5726*847ff0e1SMatthew G. Knepley   }
5727*847ff0e1SMatthew G. Knepley   ierr = TSGetSNES(ts, &snes);CHKERRQ(ierr);
5728*847ff0e1SMatthew G. Knepley   ierr = MatFDColoringApply(B, color, U, snes);CHKERRQ(ierr);
5729*847ff0e1SMatthew G. Knepley   if (J != B) {
5730*847ff0e1SMatthew G. Knepley     ierr = MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
5731*847ff0e1SMatthew G. Knepley     ierr = MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
5732*847ff0e1SMatthew G. Knepley   }
5733*847ff0e1SMatthew G. Knepley   PetscFunctionReturn(0);
5734*847ff0e1SMatthew G. Knepley }
5735